コード例 #1
0
        public ActionResult Index(string searchstring)
        {
            IngredientsUsageRequestViewModels model = new IngredientsUsageRequestViewModels();

            //model.StoreId = CurrentUser.StoreId;
            return(View(model));
        }
コード例 #2
0
        public ActionResult CreateAllocate(List <string> lstId, string storeId, string dFrom, string dTo)
        {
            //List<IngredientsUsageRequestViewModels> lstInput = new List<IngredientsUsageModels>();
            IngredientsUsageRequestViewModels model = new IngredientsUsageRequestViewModels();

            model.StoreId   = storeId;
            model.ApplyFrom = new DateTime(int.Parse(dFrom.Substring(6, 4)), int.Parse(dFrom.Substring(0, 2)), int.Parse(dFrom.Substring(3, 2)));
            model.ApplyTo   = new DateTime(int.Parse(dTo.Substring(6, 4)), int.Parse(dTo.Substring(0, 2)), int.Parse(dTo.Substring(3, 2)));
            var data = _factory.GetListIngredientUsage(model);

            model.ListItem = data.Where(ww => lstId.Contains(ww.Id)).ToList();
            return(PartialView("_ListAllocation", model));
        }
コード例 #3
0
        public ActionResult Search(IngredientsUsageRequestViewModels model)
        {
            var dataReturn = new IngredientsUsageRequestViewModels();

            try
            {
                if (!string.IsNullOrEmpty(model.StoreId))
                {
                    var data = _factory.GetListIngredientUsage(model);

                    dataReturn.ListItem = data;
                    //CurrentUser.StoreId = model.StoreId;
                }
            }
            catch (Exception e)
            {
                _logger.Error("IngredientsUsageSearch: " + e);
                return(new HttpStatusCodeResult(400, e.Message));
            }
            return(PartialView("_ListData", dataReturn));
        }
コード例 #4
0
        public ActionResult SaveAllocateVariance(IngredientsUsageRequestViewModels input)
        {
            try
            {
                foreach (var item in input.ListItem)
                {
                    item.Date = new DateTime(int.Parse(item.DateDisplay.Substring(6, 4)), int.Parse(item.DateDisplay.Substring(0, 2))
                                             , int.Parse(item.DateDisplay.Substring(3, 2)));
                    //if(item.Damage <=0 && item.Wast <=0 && item.Others <= 0)
                    //{
                    //    ModelState.AddModelError("Allocation", "Please enter value for at least 1 field before saving...");
                    //}
                }
                if (!ModelState.IsValid)
                {
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(PartialView("_ListAllocation", input));
                }
                string msg    = "";
                bool   result = _factory.InsertAlocation(input.ListItem, CurrentUser.UserId, ref msg);

                if (result)
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    ModelState.AddModelError("StoreId", msg);
                    return(PartialView("_ListAllocation", input));
                }
            }
            catch (Exception ex)
            {
                _logger.Error("IngredientsUsageCreate: " + ex);
                return(new HttpStatusCodeResult(400, ex.Message));
            }
        }
コード例 #5
0
        public List <IngredientsUsageModels> GetListIngredientUsage(IngredientsUsageRequestViewModels model)
        {
            var lstResult = new List <IngredientsUsageModels>();

            if (string.IsNullOrEmpty(model.StoreId))
            {
                return(lstResult);
            }
            using (NuWebContext cxt = new NuWebContext())
            {
                try
                {
                    lstResult = (from h in cxt.I_StockCount
                                 join d in cxt.I_StockCountDetail on h.Id equals d.StockCountId
                                 from i in cxt.I_Ingredient.Where(ww => ww.Id == d.IngredientId).DefaultIfEmpty()
                                 where h.StoreId == model.StoreId && DbFunctions.TruncateTime(h.StockCountDate) >= DbFunctions.TruncateTime(model.ApplyFrom) &&
                                 DbFunctions.TruncateTime(h.StockCountDate) <= DbFunctions.TruncateTime(model.ApplyTo)
                                 //&& i.Id == "d470f8bd-dd22-437e-9705-3d916999050e" && h.BusinessId == "b0379b38-169d-4508-bd08-ce1dd7f950a7"
                                 && h.Status == (int)Commons.EStockCountStatus.Approved
                                 select new IngredientsUsageModels()
                    {
                        Id = d.Id,
                        Date = h.StockCountDate,
                        StoreId = h.StoreId,
                        IngredientId = d.IngredientId,
                        IngredientCode = i.Code,
                        IngredientName = i.Name,
                        OpenBal = d.OpenBal.HasValue ? d.OpenBal.Value : 0,
                        CloseBal = d.CloseBal,
                        BusinessId = h.BusinessId,
                        StartedOn = h.StartedOn,
                        ClosedOn = h.ClosedOn,
                        AutoCloseBal = d.AutoCloseBal
                    }).Distinct().ToList();


                    //Check Stock In
                    //var lstIngredientId = lstResult.Select(ss => ss.IngredientId).Distinct().ToList();
                    //if (lstIngredientId != null && lstIngredientId.Any())
                    //{
                    var lstBusinessIds = lstResult.Select(ss => ss.BusinessId).Distinct().ToList();
                    var lstStockIn     = GetStockInOut(model.StoreId, lstBusinessIds);
                    //lstStockIn = lstStockIn.Where(ww => ww.IngredientId == "38107504-78c4-46a9-831e-5688832c591e").ToList();
                    foreach (var item in lstResult)
                    {
                        var lstObj = lstStockIn.Where(ww => ww.IngredientId == item.IngredientId && ww.BusinessId == item.BusinessId &&
                                                      ww.StoreId == item.StoreId).ToList();
                        if (lstObj != null && lstObj.Any())
                        {
                            item.StockIn  = lstObj.Sum(ss => ss.StockIn);
                            item.StockOut = lstObj.Sum(ss => ss.StockOut);
                        }
                        if (item.StartedOn.HasValue)
                        {
                            item.BusinessDayDisplay = item.StartedOn.Value.ToString("MM/dd/yyyy HH:mm") + " - ";
                            if (item.ClosedOn.HasValue && item.ClosedOn.Value != Commons.MinDate)
                            {
                                item.BusinessDayDisplay += (item.ClosedOn.HasValue ? item.ClosedOn.Value.ToString("MM/dd/yyyy HH:mm") : "");
                            }
                        }
                    }
                    //}

                    //Check usage
                    var lstUsage = (from d in cxt.I_UsageManagementDetail
                                    from h in cxt.I_UsageManagement.Where(ww => ww.Id == d.UsageManagementId).DefaultIfEmpty()
                                    join i in cxt.I_Ingredient on d.IngredientId equals i.Id
                                    where h.StoreId == model.StoreId
                                    //  && DbFunctions.TruncateTime(h.DateFrom) >= DbFunctions.TruncateTime(model.ApplyFrom)
                                    //&& DbFunctions.TruncateTime(h.DateFrom) <= DbFunctions.TruncateTime(model.ApplyTo)

                                    && lstBusinessIds.Contains(h.BusinessId)
                                    select new UsageManagementDetailModel()
                    {
                        ItemId = d.IngredientId,
                        Usage = d.Usage,
                        ApplyDate = h.DateFrom,
                        BusinessId = h.BusinessId,
                        IsSeftMade = i.IsSelfMode,
                        StockAble = i.StockAble.HasValue? i.StockAble.Value: false
                    }).ToList();

                    //if (lstUsage != null && lstUsage.Any())
                    //{
                    //    foreach (var item in lstResult)
                    //    {
                    //        var obj = lstUsage.Where(ww => ww.ItemId == item.IngredientId
                    //        && item.Date.Date == ww.ApplyDate.Date).FirstOrDefault();

                    //        item.Sales = obj != null ? obj.Usage : 0;
                    //    }
                    //}
                    if (lstUsage == null)
                    {
                        lstUsage = new List <UsageManagementDetailModel>();
                    }

                    var lstTmpId = lstUsage.Where(ww => ww.IsSeftMade && !ww.StockAble).Select(ss => ss.ItemId).ToList();
                    if (lstTmpId != null && lstTmpId.Any())
                    {
                        //check ingredient dependent
                        RecipeFactory _recipeFactory          = new RecipeFactory();
                        var           _objIngredientDependent = _recipeFactory.GetRecipesByIngredientSeftMade(lstTmpId);

                        var lstIngredientSMNotStockAble = lstUsage.Where(ww => ww.IsSeftMade && !ww.StockAble).ToList();
                        var businessIds = lstIngredientSMNotStockAble.Select(ss => ss.BusinessId).Distinct().ToList();
                        foreach (var item in businessIds)
                        {
                            var tmp = lstIngredientSMNotStockAble.Where(ww => ww.BusinessId == item).ToList();
                            foreach (var subitem in tmp)
                            {
                                var obj = _objIngredientDependent.ListChilds.Where(ww => ww.MixtureIngredientId == subitem.ItemId).ToList();

                                foreach (var item1 in obj)
                                {
                                    lstUsage.Add(new UsageManagementDetailModel()
                                    {
                                        ItemId     = item1.Id,
                                        Usage      = (item1.BaseUsage * subitem.Usage),
                                        BusinessId = item
                                    });
                                }
                                //obj.ForEach(ss => ss.BaseUsage = ss.BaseUsage * subitem.Usage);
                            }
                            //var lstGroup = _objIngredientDependent.ListChilds.GroupBy(gg =>  gg.Id);
                            //foreach (var item1 in lstGroup)
                            //{
                            //    lstUsage.Add(new UsageManagementDetailModel()
                            //    {
                            //        ItemId = item1.Key,
                            //        Usage = item1.Sum(ss => ss.BaseUsage),
                            //        BusinessId = item
                            //    });
                            //}
                        }

                        lstUsage = lstUsage.Where(ww => !lstTmpId.Contains(ww.ItemId)).ToList();
                    }
                    //Check allocation
                    var lstAllocation = (from d in cxt.I_AllocationDetail
                                         from h in cxt.I_Allocation.Where(ww => ww.Id == d.AllocationId).DefaultIfEmpty()
                                         where h.StoreId == model.StoreId &&
                                         lstBusinessIds.Contains(h.BusinessId)
                                         //  && DbFunctions.TruncateTime(h.ApplyDate) >= DbFunctions.TruncateTime(model.ApplyFrom)
                                         //&& DbFunctions.TruncateTime(h.ApplyDate) <= DbFunctions.TruncateTime(model.ApplyTo)
                                         select new IngredientsUsageModels()
                    {
                        IngredientId = d.IngredientId,
                        StoreId = h.StoreId,
                        Damage = d.Damage,
                        Wast = d.Wast,
                        Others = d.Others,
                        BusinessId = h.BusinessId
                    }).ToList();
                    if (lstAllocation == null)
                    {
                        lstAllocation = new List <IngredientsUsageModels>();
                    }
                    //lst dataEntry
                    var lstDataEntry = (from d in cxt.I_DataEntryDetail
                                        join h in cxt.I_DataEntry on d.DataEntryId equals h.Id
                                        where h.StoreId == model.StoreId &&
                                        lstBusinessIds.Contains(h.BusinessId)
                                        //&& DbFunctions.TruncateTime(h.EntryDate) >= DbFunctions.TruncateTime(model.ApplyFrom)
                                        //&& DbFunctions.TruncateTime(h.EntryDate) <= DbFunctions.TruncateTime(model.ApplyTo)
                                        select new IngredientsUsageModels()
                    {
                        IngredientId = d.IngredientId,
                        StoreId = h.StoreId,
                        Damage = d.Damage.HasValue? d.Damage.Value:0,
                        Wast = d.Wastage.HasValue? d.Wastage.Value:0,
                        Others = d.OrderQty.HasValue? d.OrderQty.Value:0,
                        BusinessId = h.BusinessId
                    }).ToList();
                    if (lstDataEntry == null)
                    {
                        lstDataEntry = new List <IngredientsUsageModels>();
                    }

                    var     allocations = new List <IngredientsUsageModels>();
                    var     dataEntrys = new List <IngredientsUsageModels>();
                    decimal openBal = 0; decimal stockIn = 0; decimal closeBal = 0; decimal stockOut = 0;
                    foreach (var item in lstResult)
                    {
                        var obj = (decimal)lstUsage.Where(ww => ww.ItemId == item.IngredientId
                                                          //&& item.Date.Date == ww.ApplyDate.Date).FirstOrDefault();
                                                          && item.BusinessId == ww.BusinessId).Sum(ss => ss.Usage);
                        item.Sales  = Math.Round((double)obj, 2);
                        allocations = lstAllocation.Where(ww => ww.IngredientId == item.IngredientId && ww.StoreId == item.StoreId && ww.BusinessId == item.BusinessId).ToList();
                        if (allocations != null && allocations.Any())
                        {
                            item.Damage            = allocations.Sum(ss => ss.Damage);
                            item.Wast              = allocations.Sum(ss => ss.Wast);
                            item.Others            = allocations.Sum(ss => ss.Others);
                            item.IsExistAllocation = true;
                        }
                        decimal.TryParse(item.OpenBal.ToString(), out openBal);
                        decimal.TryParse(item.StockIn.ToString(), out stockIn);
                        decimal.TryParse(item.CloseBal.ToString(), out closeBal);
                        decimal.TryParse(item.StockOut.ToString(), out stockOut);
                        item.ActualSold = (double)((openBal + stockIn) - closeBal - stockOut);
                        //item.IsAllocation = Math.Abs(item.ActualSold - item.Sales) != (item.Damage + item.Wast + item.Others);//(item.ActualSold != item.Sales);
                        item.IsAllocation = (item.ActualSold != item.Sales);
                        //if (!item.IsExistAllocation)
                        //{
                        //item.Damage = 0;
                        //item.Wast = 0;
                        //item.Others = 0;

                        //
                        //dataEntrys = lstDataEntry.Where(ww => ww.IngredientId == item.IngredientId && ww.StoreId == item.StoreId && ww.BusinessId == item.BusinessId).ToList();
                        //if (dataEntrys != null && dataEntrys.Any())
                        //{
                        //    item.Damage = dataEntrys.Sum(ss => ss.Damage);
                        //    item.Wast = dataEntrys.Sum(ss => ss.Wast);
                        //    item.Others = dataEntrys.Sum(ss => ss.Others);
                        //}
                        //}
                        item.VarianceQty = Math.Abs((double)((decimal)item.ActualSold - (decimal)item.Sales));
                        //item.IsAllocation = true;
                        item.DateDisplay = item.Date.ToString("MM/dd/yyyy");

                        //item.IsAllocation = true;
                        item.Adjust = item.AutoCloseBal.HasValue ? Math.Round((item.CloseBal - item.AutoCloseBal.Value), 4) : 0;
                    }

                    return(lstResult);
                }
                catch (Exception ex)
                {
                    _logger.Error(ex.Message);
                    return(lstResult);
                }
            }
        }