private void AddBillToStorage(Bill bill, int sellerLocation) { var storage = _context.SalePoints.Find(sellerLocation).Storage; foreach (var item in bill.Products) { SaleStorage saleStorage = new SaleStorage { Count = item.Count, ProductId = item.Product.Id, SalePointId = sellerLocation }; storage.Add(saleStorage); } }
public ActionResult CreateBill() { // [HttpPost] if (Request.IsAjaxRequest()) { int salePoint = GetSellerLocation(); Bill bill = new Bill { Amount = CalculateAmount(), DateTime = DateTime.Now, SalePointId = salePoint }; _context.Bills.Add(bill); // записать елементы чека (продукт) в БД foreach (var item in _currentBill) { ProductToBill productToBill = new ProductToBill(); productToBill.BillId = bill.Id; productToBill.ProductId = item.Key.Id; productToBill.Count = item.Value; _context.ProductToBills.Add(productToBill); #region Delete product count from storage _recivedProducts[item.Key] -= item.Value; _context.SalePoints .Find(salePoint).Storage .ToList() .Find(p => p.ProductId == item.Key.Id) .Count -= item.Value; #endregion } _context.SaveChanges(); _currentBill.Clear(); return PartialView("AddProductToBill", _currentBill); } _recivedProducts = GetProducts(); _currentBill.Clear(); return View(_recivedProducts); }