private Boolean IsCalculatedToday()
 {
     AmcReportRepository _amcReportRepository = new AmcReportRepository();
     int count = _amcReportRepository.AllAmcReport().Where(amc => amc.LastIndexedTime.Value.Date == DateTime.Today.Date).Select(amc => amc.ID).Count();
     if (count > 0)
     {
         return true;
     }
     else
     {
         return false;
     }
 }
        public void BuildAMC(bool forceCalc = false)
        {
            if ((IsCalculatedToday()) && (forceCalc == false))
            {
                return;
            }

            StockoutRepository _repository = new StockoutRepository();
            StoreRepository _storerepository = new StoreRepository();
            GeneralInfoRepository _generalInfoRepository = new GeneralInfoRepository();
            ReceiveDocRepository _receiveDocRepository = new ReceiveDocRepository();
            AmcReportRepository _amcReportRepository = new AmcReportRepository();
            List<AMCViewModel> _datasource;

            //clear AMC table before another build
            _amcReportRepository.DeleteAll();

            var generalinfo = _generalInfoRepository.AllGeneralInfos().First();
            var storeList = _storerepository.AllStores();
            foreach (var store in storeList)
            {
                var storeId = store.ID;
                var itemsrecieved = _receiveDocRepository.RecievedItems().Where(m => m.StoreID == storeId).Select(m => m.ItemID).Distinct();

                _datasource = new List<AMCViewModel>();
                var receiveDocs = _repository.AllItems().Where(m => itemsrecieved.Contains(m.ID)).ToList();
                double increment = 80.0 / Convert.ToDouble(receiveDocs.Count());

                IEnumerable<int?> unitid;
                if (VisibilitySetting.HandleUnits != 1)
                {
                    foreach (var item in receiveDocs)
                    {
                        unitid = _receiveDocRepository.RecievedItems().Where(m => m.ItemID == item.ID && m.StoreID == storeId).Select(m => m.UnitID).Distinct();
                        foreach (var i in unitid)
                        {
                            AMCViewModel.OptimizedCreate(item.ID, item.FullItemName, storeId, i.GetValueOrDefault(), generalinfo.AMCRange, DateTime.Today);
                        }
                    }
                }
                else
                {
                    foreach (var item in receiveDocs)
                    {
                        AMCViewModel.OptimizedCreate(item.ID, item.FullItemName, storeId, generalinfo.AMCRange, DateTime.Today);
                    }
                }
            }
        }
 public static AMCViewModel Create(int itemId, int storeId, int unitid,int amcRange, DateTime endDate)
 {
     var vwGetAllItemsRepository =new vwGetAllItemsRepository();
     var amcrepo = new AmcReportRepository();
     var allItemIdsonamc = amcrepo.AllAmcReport().SingleOrDefault(m => m.ItemID == itemId && m.StoreID == storeId && m.UnitID ==unitid);
     var products = vwGetAllItemsRepository.AllItems().SingleOrDefault(m => m.ID == itemId);
     var startDate = endDate.Subtract(TimeSpan.FromDays(amcRange*30));
     var viewModel = new AMCViewModel
                         {
                             ItemID = itemId,
                             StoreID = storeId,
                             AmcRange = amcRange,
                             IssueInAmcRange =Builder.CalculateTotalConsumptionWithoutDOS(itemId, storeId, startDate, endDate),
                             DaysOutOfStock =Builder.CalculateStockoutDays(itemId, storeId, startDate, DateTime.Now),
                             AmcWithDos =Builder.CalculateAverageConsumption(itemId, storeId, startDate, endDate,CalculationOptions.Monthly),
                             IssueWithDOS =Builder.CalculateTotalConsumptionAMC(itemId, storeId, startDate, endDate),
                             UnitID = unitid,
                         };
     AddorUpdateAmc(itemId, storeId, unitid,amcRange, endDate, amcrepo, viewModel, allItemIdsonamc, startDate);
     if (products != null)
     viewModel.FullItemName = products.FullItemName;
     viewModel.AmcWithoutDos = Convert.ToDouble(Builder.CalculateTotalConsumptionWithoutDOS(itemId,storeId,startDate,endDate)) / Convert.ToDouble(viewModel.AmcRange);
     return viewModel;
 }
 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);
        }