private ProductAvailability AddBaseAvailabilityForToday()
 {
     //Add availability info for today
     ProductAvailability todayAvailabilityInfo = new ProductAvailability() { Date = DateTime.Now.Date, ProductInfos = new HashSet<ProductAvailabilityDetail>() };
     ProductAvailability lastInfo = _context.ProductAvailabilities
         .OrderByDescending(x => x.Date)
         .FirstOrDefault(x => x.Date.CompareTo(todayAvailabilityInfo.Date) < 0);
     if (lastInfo != null)
     {
         //We have data from previous availabilities, copy it over
         foreach (var oldDetail in lastInfo.ProductInfos)
         {
             todayAvailabilityInfo.ProductInfos.Add(new ProductAvailabilityDetail()
             {
                 Amount = oldDetail.Amount,
                 ProductId = oldDetail.ProductId,
                 ProductAvailability = todayAvailabilityInfo
             });
         }
     }
     _context.ProductAvailabilities.Add(todayAvailabilityInfo);
     return todayAvailabilityInfo;
 }
 private void HandleProductAvailability(ReplenishmentDetail replenishment, ProductAvailability todayAvailabilityInfo)
 {
     var stockInfo = todayAvailabilityInfo.ProductInfos.SingleOrDefault(x => x.ProductId == replenishment.ProductId);
     if (stockInfo == null)
     {
         //We've replenished a new product
         todayAvailabilityInfo.ProductInfos.Add(new ProductAvailabilityDetail()
         {
             ProductId = replenishment.ProductId,
             Amount = replenishment.Amount,
             ProductAvailabilityId = todayAvailabilityInfo.Id
         });
     }
     else
     {
         stockInfo.Amount += replenishment.Amount;
     }
 }
        private void WriteReplenishmentDetailsToDB(IEnumerable<ReplenishmentDetail> replenishments, Replenishment newReplenishment, ProductAvailability todayAvailabilityInfo)
        {
            foreach (var replenish in replenishments)
            {
                var product = _context.Products.SingleOrDefault(x => x.Id == replenish.ProductId);
                if (product == null || product.IsActive == false)
                {
                    throw new InvalidOperationException("You can't replenish deleted product " + product.Name);
                }
                replenish.Replenishment = newReplenishment;
                _context.ReplenishmentDetails.Add(replenish);

                HandleProductAvailability(replenish, todayAvailabilityInfo);
            }
        }
예제 #4
0
        private void UpdateAvailabilities(IEnumerable<SaleDetail> saleDetails, Sale newSale, ProductAvailability todaysAvailability)
        {
            foreach (var saleDetail in saleDetails)
            {
                var product = _context.Products.SingleOrDefault(x => x.Id == saleDetail.ProductId);
                if (product == null || product.IsActive == false)
                {
                    throw new InvalidOperationException("You can't sell deleted product " + product.Name);
                }
                saleDetail.Sale = newSale;
                _context.SaleDetails.Add(saleDetail);

                var stockInfo = todaysAvailability.ProductInfos.Single(x => x.ProductId == saleDetail.ProductId);
                stockInfo.Amount -= saleDetail.Amount;
            }
        }