public Bale Create(Bale model)
        {
            model.DTCreated   = DateTime.Now;
            model.DT          = model.DT;
            model.FIFORemarks = "First In, First Out";
            model.IsReject    = false;

            var product = productRepository.Get().Select(a =>
                                                         new
            {
                a.ProductId,
                a.ProductDesc,
                CategoryId   = a.Category == null ? 0 : a.Category.CategoryId,
                CategoryDesc = a.Category == null ? "" : a.Category.CategoryDesc
            })
                          .FirstOrDefault(b => b.ProductId == model.ProductId);

            model.ProductId    = product?.ProductId ?? 0;
            model.ProductDesc  = product?.ProductDesc;
            model.CategoryId   = product?.CategoryId ?? 0;
            model.CategoryDesc = product?.CategoryDesc;

            dbContext.Bales.Add(model);
            dbContext.SaveChanges();
            dbContext.Entry <Bale>(model).State = EntityState.Detached;

            CheckAndCreateBaleOverageReminder();
            baleStationRepository.CheckAndCreateStockStatusReminder();

            return(model);
        }
        public IActionResult Post([FromBody] Bale model)
        {
            try
            {
                if (model.DT.IsEmpty())
                {
                    model.DT = DateTime.Now;
                }

                if (!ModelState.IsValid)
                {
                    return(InvalidModelStateResult());
                }
                repository.GenerateBaleCode(model, out model);
                var modelStateDic = repository.ValidateEntity(model);
                if (modelStateDic.Count > 0)
                {
                    ModelState.AddModelErrors(modelStateDic);
                    return(InvalidModelStateResult());
                }

                return(Accepted(repository.Create(model)));
            }
            catch (Exception ex)
            {
                logger.LogError(ex.GetExceptionMessages());
                return(StatusCode(StatusCodes.Status500InternalServerError, Constants.ErrorMessages.CreateError));
            }
        }
        public bool ValidateCode(Bale model)
        {
            var existing = Get().FirstOrDefault(a => a.BaleCode == model.BaleCode);

            if (existing == null)
            {
                return(true);
            }
            return(existing.BaleId == model.BaleId);
        }
        public Bale Update(Bale model)
        {
            var entity = dbContext.Bales.Where(a => a.BaleId == model.BaleId).FirstOrDefault();

            if (entity == null)
            {
                throw new Exception("Selected Record does not exists.");
            }

            if (entity.DT.Year != model.DT.Year ||
                entity.DT.Month != model.DT.Month ||
                entity.ProductId != model.ProductId)
            {
                GenerateBaleCode(model, out model);
            }
            entity.DT = model.DT;

            entity.FIFORemarks = "First In, First Out";
            //entity.DTCreated = model.DTCreated;
            entity.BaleCode    = model.BaleCode;
            entity.BaleWt      = model.BaleWt;
            entity.BaleWt10    = model.BaleWt10;
            entity.ProductId   = model.ProductId;
            entity.ProductDesc = model.ProductDesc;
            entity.BaleNum     = model.BaleNum;
            entity.IsReject    = model.IsReject;
            var product = productRepository.Get().Select(a =>
                                                         new
            {
                a.ProductId,
                a.ProductDesc,
                CategoryId   = a.Category == null ? 0 : a.Category.CategoryId,
                CategoryDesc = a.Category == null ? "" : a.Category.CategoryDesc
            })
                          .FirstOrDefault(b => b.ProductId == model.ProductId);

            entity.ProductId    = product?.ProductId ?? 0;
            entity.ProductDesc  = product?.ProductDesc;
            entity.CategoryId   = product?.CategoryId ?? 0;
            entity.CategoryDesc = product?.CategoryDesc;


            dbContext.Bales.Update(entity);
            dbContext.SaveChanges();
            dbContext.Entry <Bale>(entity).State = EntityState.Detached;

            CheckAndCreateBaleOverageReminder();
            baleStationRepository.CheckAndCreateStockStatusReminder();

            return(entity);
        }
        public Dictionary <string, string> ValidateEntity(Bale model)
        {
            var modelStateDict = new Dictionary <string, string>();
            var validCode      = ValidateCode(model);

            if (!validCode)
            {
                modelStateDict.Add(nameof(Bale.BaleCode), Constants.ErrorMessages.EntityExists("Code"));
            }
            var productCount = productRepository.Get().AsNoTracking().Count(a => a.ProductId == model.ProductId);

            if (productCount == 0)
            {
                modelStateDict.Add(nameof(Bale.ProductId), Constants.ErrorMessages.NotFoundProperty("Product"));
            }
            return(modelStateDict);
        }
        public bool Delete(Bale model)
        {
            var bs = dbContext.Bales.Where(a => a.BaleId == model.BaleId).AsNoTracking().FirstOrDefault();

            var sbale = dbContext.SaleBales.Where(a => a.BaleId == model.BaleId).AsNoTracking().FirstOrDefault();

            dbContext.Bales.Remove(bs);

            if (sbale != null)
            {
                dbContext.SaleBales.Remove(sbale);
            }
            dbContext.SaveChanges();

            CheckAndCreateBaleOverageReminder();
            baleStationRepository.CheckAndCreateStockStatusReminder();

            return(true);
        }
        public IActionResult ValidateCode([FromBody] Bale model)
        {
            if (model == null)
            {
                return(NotFound());
            }
            if (General.IsDevelopment)
            {
                logger.LogDebug(ModelState.ToJson());
            }
            var result = repository.ValidateCode(model);

            if (result)
            {
                return(Accepted(true));
            }
            else
            {
                return(UnprocessableEntity(Constants.ErrorMessages.EntityExists("Bale Code")));
            }
        }
        public void GenerateBaleCode(Bale model, out Bale outModel)
        {
            var product = productRepository.Get().AsNoTracking().Where(a => a.ProductId == model.ProductId).Select(a => new { a.ProductDesc, a.CategoryId }).FirstOrDefault();

            model.CategoryId  = product.CategoryId;
            model.ProductDesc = product.ProductDesc;

            var catcode = catRepository.Get().AsNoTracking().Where(a => a.CategoryId == model.CategoryId).Select(a => a.CategoryCode).FirstOrDefault();

            var bs = baleStationRepository.Get().AsNoTracking().Where(a => a.Selected).Select(a => a.BalingStationNum).FirstOrDefault();
            var mn = model.DT.GetBaleMonthLetter();
            var yr = model.DT.Year;

            if (model.BaleNum == 0)
            {
                model.BaleNum = GetLastBaleNum(model.DT, model.CategoryId) + 1;
            }

            model.BaleCode = catcode + bs + mn + yr + model.BaleNum.PadZero(4);

            outModel = model;
        }