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); } }
private void MakeReplenishments(IEnumerable<ReplenishmentDetail> replenishments) { Replenishment newReplenishment = new Replenishment() { DateAndTime = DateTime.Now, TotalAmountPaid = replenishments.Sum(r => new Decimal(r.Amount) * r.PricePerUnit), }; _context.Replenishments.Add(newReplenishment); //Check for a product availability info for today ProductAvailability todayAvailabilityInfo = _context.ProductAvailabilities.ToList().SingleOrDefault(x => x.Date.Date.Equals(DateTime.Now.Date)); if (todayAvailabilityInfo == null) { todayAvailabilityInfo = AddBaseAvailabilityForToday(); } WriteReplenishmentDetailsToDB(replenishments, newReplenishment, todayAvailabilityInfo); }