private static void AddorUpdateAmc(int itemId, int storeId,int amcRange, DateTime endDate, AmcReportRepository amcrepo, AMCViewModel viewModel, AmcReport allItemIds, DateTime startDate)
 {
     if (allItemIds != null)
     {
         allItemIds.IssueInAmcRange = Builder.CalculateTotalConsumptionWithoutDOS(itemId, storeId, startDate, endDate);
         allItemIds.IssueWithDOS = Builder.CalculateTotalConsumptionAMC(itemId, storeId, startDate, endDate);
         allItemIds.AmcWithDOS = Builder.CalculateTotalConsumptionAMC(itemId, storeId, startDate, endDate) / Convert.ToDouble(viewModel.AmcRange);
         allItemIds.AmcWithOutDOS = Builder.CalculateTotalConsumptionWithoutDOS(itemId, storeId, startDate, endDate) / Convert.ToDouble(viewModel.AmcRange);
         allItemIds.DaysOutOfStock = Builder.CalculateStockoutDays(itemId, storeId, startDate, DateTime.Now);
         allItemIds.AmcRange = amcRange;
         allItemIds.LastIndexedTime = DateTime.Now;
         amcrepo.Update(allItemIds);
     }
     else
     {
         var amcreport = new AmcReport
         {
             ItemID = itemId,
             StoreID = storeId,
             AmcRange = amcRange,
             IssueWithDOS = Builder.CalculateTotalConsumptionAMC(itemId, storeId, startDate, endDate),
             IssueInAmcRange = Builder.CalculateTotalConsumptionWithoutDOS(itemId, storeId, startDate, endDate),
             DaysOutOfStock = Builder.CalculateStockoutDays(itemId, storeId, startDate, DateTime.Now),
             AmcWithDOS = Builder.CalculateTotalConsumptionAMC(itemId, storeId, startDate, endDate) / Convert.ToDouble(viewModel.AmcRange),
             AmcWithOutDOS = Builder.CalculateTotalConsumptionWithoutDOS(itemId, storeId, startDate, endDate) / Convert.ToDouble(viewModel.AmcRange),
             LastIndexedTime = DateTime.Now
         };
         amcrepo.Add(amcreport);
     }
 }
        private static void AddAMCForUnit(int itemId, string itemFullName, int storeId,int unitid, int amcRange, DateTime endDate, DateTime startDate)
        {
            var amcrepo = new AmcReportRepository();
            var DOS = Builder.CalculateStockoutDays(itemId, storeId, startDate, DateTime.Now);
            var totalCAMC = Builder.CalculateTotalConsumptionAMC(itemId, storeId, startDate, endDate, amcRange, DOS);
            var totalCWDOS = Builder.CalculateTotalConsumptionWithoutDOS(itemId, storeId, startDate,endDate);

            var amcreport = new AmcReport
                            {
                                ItemID = itemId,
                                StoreID = storeId,
                                AmcRange = amcRange,
                                IssueWithDOS = totalCAMC,
                                IssueInAmcRange = totalCWDOS,
                                DaysOutOfStock = DOS,
                                AmcWithDOS = totalCAMC / amcRange,
                                AmcWithOutDOS = totalCWDOS / amcRange,
                                LastIndexedTime = DateTime.Now,
                                UnitID = unitid
                            };

            amcreport.FullItemName = itemFullName;
            amcrepo.Add(amcreport);
        }
 void Remove(AmcReport amcreport)
 {
     context.AmcReports.Remove(amcreport);
 }
 public void Update(AmcReport amcReport)
 {
     context.Entry(amcReport).State = EntityState.Modified;
        context.SaveChanges();
 }
 public void Add(AmcReport amcReport)
 {
     context.AmcReports.Add(amcReport);
       context.SaveChanges();
 }
        public static void RefreshAMCValues(int storeId, Dictionary<int,long> items,int unitId)
        {
            var context = new StockoutEntities();
            var genaralinfo = context.GenralInfos.First();
            var endDate = DateTime.Now;
            var startDate = endDate.Subtract(TimeSpan.FromDays(genaralinfo.AMCRange * 30));

            try
            {
              foreach (var row in items)
              {
                  if (row.Value == 0)
                  {
                      var stockOut = new Stockout()
                      {
                          ItemID = row.Key,
                          StoreID = storeId,
                          StartDate = DateTime.Now,
                          EndDate = null,
                          LastIndexedTime = DateTime.Now
                      };
                      context.Stockouts.Add(stockOut);
                      context.SaveChanges();
                  }

                  var allItemIds = context.AmcReports.SingleOrDefault(m => m.ItemID == row.Key && m.StoreID == storeId && m.UnitID ==unitId);
                  if(allItemIds==null)
                  {

                          var amcreport = new AmcReport
                                              {
                                                  ItemID = row.Key,
                                                  StoreID = storeId,
                                                  AmcRange = genaralinfo.AMCRange,
                                                  IssueInAmcRange =
                                                      CalculateTotalConsumptionWithoutDOS(row.Key, storeId, startDate,
                                                                                          DateTime.Today),
                                                  DaysOutOfStock =
                                                      CalculateStockoutDays(row.Key, storeId, startDate, endDate),
                                                  AmcWithDOS =
                                                      CalculateAverageConsumption(row.Key, storeId, startDate,endDate,CalculationOptions.Monthly),
                                                  AmcWithOutDOS = CalculateTotalConsumptionWithoutDOS(row.Key, storeId, startDate, endDate) / Convert.ToDouble(genaralinfo.AMCRange),
                                                  LastIndexedTime = DateTime.Now,
                                                  IssueWithDOS =Builder.CalculateTotalConsumption(row.Key, storeId, startDate, DateTime.Now),
                                                  UnitID = unitId
                                              };
                          context.AmcReports.Add(amcreport);
              }
                  // Update AMC value
                  else
                  {
                      allItemIds.IssueInAmcRange = CalculateTotalConsumptionWithoutDOS(row.Key, storeId, startDate,
                                                                             DateTime.Now);
                      allItemIds.DaysOutOfStock = CalculateStockoutDays(row.Key, storeId, startDate, DateTime.Now);
                      allItemIds.AmcWithDOS = CalculateAverageConsumption(row.Key, storeId, startDate, endDate,CalculationOptions.Monthly);
                      allItemIds.AmcWithOutDOS =CalculateTotalConsumptionWithoutDOS(row.Key, storeId, startDate, endDate)/Convert.ToDouble(genaralinfo.AMCRange);
                      allItemIds.IssueWithDOS = Builder.CalculateTotalConsumption(row.Key, storeId, startDate, DateTime.Now);
                      allItemIds.LastIndexedTime = DateTime.Now;
                      allItemIds.UnitID = unitId;
                  }

                  context.SaveChanges();

              }

            }
            catch (DbUpdateException ex)
            {

                throw;
            }
        }