public virtual GTINValidationCodes ValidationFormat(GTIN gTIN)
        {
            if (gTIN.CompanyCode <= 0)
            {
                return(GTINValidationCodes.CompanyCodeMustBeGreaterThanZero);
            }

            if (gTIN.CompanyCode.ToString().Length > this.MaxLengthNumeric - 1)
            {
                return(GTINValidationCodes.CompanyCodeTooLong);
            }

            if (gTIN.Numeric <= 0)
            {
                return(GTINValidationCodes.NumericMustBeGreaterThanZero);
            }

            if (gTIN.Numeric.ToString().Length > this.MaxLengthNumeric - 1)
            {
                return(GTINValidationCodes.NumericTooLong);
            }

            var gTINChecking = CalculateCheckDigit(gTIN);

            if (gTINChecking == null || gTINChecking.CheckDigit != gTIN.CheckDigit)
            {
                return(GTINValidationCodes.CheckDigitInValid);
            }
            return(GTINValidationCodes.GTINValid);
        }
示例#2
0
        public async Task <int> AddAsync(GTIN gTIN)
        {
            var cmd = QueriesCreatingHelper.CreateQueryInsert(gTIN);

            cmd += ";SELECT LAST_INSERT_ID();";
            return((await DALHelper.ExecuteQuery <int>(cmd, dbTransaction: DbTransaction, connection: DbConnection)).First());
        }
示例#3
0
        public static GTINInformation ToInformation(this GTIN gTIN)
        {
            var serializedParent = JsonConvert.SerializeObject(gTIN);
            var info             = JsonConvert.DeserializeObject <GTINInformation>(serializedParent);

            return(info);
        }
示例#4
0
        public async Task <GTIN> InsertOrUpdateGTINAsync(int partnerId, GTIN gTIN, UserSession session)
        {
            gTIN.PartnerId = partnerId;
            var gTINService = gTIN.Type.GetGTINService(gTINQueries, gTINRepository, _sessionBufferService);
            var rs          = gTINService.ValidationFormat(gTIN);

            if (rs != GTINValidationCodes.GTINValid && rs != GTINValidationCodes.CheckDigitInValid)
            {
                throw rs.GetException();
            }
            if (rs == GTINValidationCodes.CheckDigitInValid)
            {
                gTIN = gTINService.CalculateCheckDigit(gTIN);
            }

            int id = gTIN.Id;

            gTIN.ModifiedBy   = session.Id;
            gTIN.ModifiedDate = DateTime.Now;
            if (gTIN.Id <= 0)
            {
                gTIN.CreatedBy   = session.Id;
                gTIN.CreatedDate = DateTime.Now;
                gTIN.UsedDate    = DateTime.Now;
                id = await gTINRepository.AddAsync(gTIN);
            }
            else
            {
                await gTINRepository.UpdateAsync(gTIN);
            }

            return(await gTINQueries.GetByIdAsync(id));
        }
示例#5
0
        public async Task <GTINValidationCodes> CheckNewGTINAsync(int partnerId, GTIN gTIN, UserSession session)
        {
            var company = await companyQueries.GetByIdAsync(partnerId);

            var gTINService = gTIN.Type.GetGTINService(gTINQueries, gTINRepository, _sessionBufferService);

            return(await gTINService.CheckNewGTINAsync(company, gTIN, session));
        }
示例#6
0
        public async Task <GTIN> CalculateCheckDigitAsync(GTIN gTIN)
        {
            var gTINService = gTIN.Type.GetGTINService(gTINQueries, gTINRepository, _sessionBufferService);
            var rs          = gTINService.ValidationFormat(gTIN);

            if (rs != GTINValidationCodes.GTINValid && rs != GTINValidationCodes.CheckDigitInValid)
            {
                throw rs.GetException();
            }

            return(gTINService.CalculateCheckDigit(gTIN));
        }
示例#7
0
 public override string GetCode(GTIN gTin, bool isRecheck = false)
 {
     if (isRecheck)
     {
         return(GetCode(CalculateCheckDigit(gTin), false));
     }
     else
     {
         var companyCode = gTin.CompanyCode.ToString();
         var numeric     = gTin.Numeric.ToString().PadLeft(this.MaxLengthNumeric - companyCode.Length, '0');
         return($"{companyCode}{numeric}{gTin.CheckDigit}");
     }
 }
        public GTIN CalculateCheckDigit(GTIN gTIN)
        {
            string code = GetCode(gTIN, false);

            var sumPos = 0;
            var pos    = 3;

            for (var i = code.Length - 2; i >= 0; i--)
            {
                sumPos += pos * (int)(code[i] - '0');
                pos     = (pos == 3) ? 1 : 3;
            }

            gTIN.CheckDigit = (10 - sumPos % 10) % 10;
            return(gTIN);
        }
示例#9
0
        public async Task <GTIN> GenerateGTINAsync(int partnerId, GTINTypes gTINTypes, UserSession session)
        {
            var company = await companyQueries.GetByIdAsync(partnerId);

            var productions = (await productionQueries.GetAllAsync(company.Id)).ToList();

            var gTINId = productions.Select(x => x.GTINId);
            var gTINs  = await gTINQueries.GetByCompanyCodeAsync(company.GS1Code);

            long maxNumericExist = (gTINs.Max(x => (long?)x.Numeric) ?? 0) + 1;

            var sessionBuffers = await _sessionBufferService.GetByCompanyIdAsync(company.Id, SessionBufferTypes.GTIN);

            var  now = DateTime.Now;
            long maxNumericBuffer = sessionBuffers
                                    .Where(x => x.ExpiredDate > now && x.PartnerId == partnerId)
                                    .Select(x => JsonConvert.DeserializeObject <GTIN>(x.DataJson)?.Numeric)
                                    .Max() ?? 0;

            long numericGenerate = (maxNumericExist > maxNumericBuffer ? maxNumericExist : maxNumericBuffer) + 1;
            GTIN gTIN            = new GTIN()
            {
                CompanyCode  = company.GS1Code,
                Numeric      = numericGenerate,
                PartnerId    = partnerId,
                Type         = gTINTypes,
                UsedDate     = DateTime.Now,
                IsUsed       = true,
                ModifiedBy   = session.Id,
                ModifiedDate = DateTime.Now,
                CreatedBy    = session.Id,
                CreatedDate  = DateTime.Now
            };

            var id = await _sessionBufferService.InsertOrUpdateAsync(new SessionBuffer()
            {
                PartnerId   = company.Id,
                SessionId   = session.SessionId,
                DataJson    = JsonConvert.SerializeObject(gTIN),
                ExpiredDate = DateTime.Now.AddHours(2),
                Type        = SessionBufferTypes.GTIN,
            }, session);

            return(gTIN);
        }
        public override async Task <GTINInformation> HandleCommand(InsertOrUpdateGTINCommand request, CancellationToken cancellationToken)
        {
            var company = await companyQueries.GetByUserIdAsync(request.LoginSession.Id);

            if (company == null)
            {
                throw new BusinessException("Common.NoPermission");
            }

            GTIN gTIN = (await gTINService.InsertOrUpdateGTINAsync(company.Id, request.Model, request.LoginSession)).ToInformation();

            if (gTIN == null)
            {
                throw new BusinessException("Common.TaskFailed");
            }

            var rs = gTIN.ToInformation();

            rs.Code = await gTINService.GetCodeGTINAsync(gTIN);

            return(rs);
        }
 public InsertOrUpdateGTINCommand(GTIN gTIN)
 {
     Model = gTIN;
 }
示例#12
0
        public virtual async Task <GTINValidationCodes> CheckNewGTINAsync(Company company, GTIN gTIN, UserSession session)
        {
            var rs = this.ValidationFormat(gTIN);

            if (rs != GTINValidationCodes.GTINValid)
            {
                throw rs.GetException();
            }

            var gTINs = await _gTINQueries.GetByCompanyCodeAsync(company.GS1Code);

            if (gTINs.Where(x => x.Numeric == gTIN.Numeric && x.PartnerId == company.Id).Count() > 0)
            {
                return(GTINValidationCodes.AlreadyExist);
            }

            var sessionBuffers = await _sessionBufferService.GetByCompanyIdAsync(company.Id, SessionBufferTypes.GTIN);

            var now = DateTime.Now;

            if (sessionBuffers.Where(x => x.ExpiredDate > now && x.SessionId != session.SessionId && (JsonConvert.DeserializeObject <GTIN>(x.DataJson)?.Numeric ?? 0) == gTIN.Numeric)
                .Count() > 0)
            {
                return(GTINValidationCodes.UsedByAnotherSession);
            }

            return(GTINValidationCodes.GTINValid);
        }
示例#13
0
 public abstract string GetCode(GTIN gTin, bool isRecheck = false);
示例#14
0
        public override async Task <GTINValidationCodes> CheckNewGTINAsync(Company company, GTIN gTIN, UserSession session)
        {
            var rs = this.ValidationFormat(gTIN);

            if (rs != GTINValidationCodes.GTINValid)
            {
                throw rs.GetException();
            }

            var gTINs = await _gTINQueries.GetByCompanyCodeAsync(company.GS1Code);

            if (gTINs.Where(x => x.Numeric == gTIN.Numeric && x.IndicatorDigit == gTIN.IndicatorDigit && x.PartnerId == company.Id).Count() > 0)
            {
                return(GTINValidationCodes.AlreadyExist);
            }

            if (gTIN.IndicatorDigit > 0 &&
                gTINs.Where(x => x.Numeric == gTIN.Numeric && x.IndicatorDigit == 0 && x.PartnerId == company.Id).Count() <= 0)
            {
                return(GTINValidationCodes.BaseNotExist);
            }

            var sessionBuffers = await _sessionBufferService.GetByCompanyIdAsync(company.Id, SessionBufferTypes.GTIN);

            var now = DateTime.Now;

            if (sessionBuffers
                .Select(x => new { Buffer = x, Data = JsonConvert.DeserializeObject <GTIN>(x.DataJson) })
                .Where(x => x.Buffer.ExpiredDate > now &&
                       x.Buffer.SessionId != session.SessionId &&
                       x.Buffer.PartnerId == company.Id &&
                       (x.Data?.Numeric ?? 0) == gTIN.Numeric &&
                       (x.Data?.IndicatorDigit ?? 0) == gTIN.IndicatorDigit)
                .Count() > 0)
            {
                return(GTINValidationCodes.UsedByAnotherSession);
            }

            return(GTINValidationCodes.GTINValid);
        }
示例#15
0
        public async Task <int> UpdateAsync(GTIN gTIN)
        {
            var cmd = QueriesCreatingHelper.CreateQueryUpdate(gTIN);

            return(await DALHelper.Execute(cmd, dbTransaction : DbTransaction, connection : DbConnection));
        }
示例#16
0
 public CheckNewGTINCommand(GTIN gTIN)
 {
     Model = gTIN;
 }