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; } }
private Sale GenerateNewSale(decimal totalPayed) { Sale newSale = new Sale() { DateAndTime = DateTime.Now, TotalAmountPaid = totalPayed }; _context.Sales.Add(newSale); return newSale; }