public void Payment_Order(int employeeid, decimal total, List <ShoppingCart> info) { using (var context = new eRaceContext()) { Invoice products = new Invoice(); SalesCartItem cartItem = new SalesCartItem(); SalesCartItem exists = (from x in context.SalesCartItems .Where(x => x.EmployeeID.Equals(employeeid)) select x).FirstOrDefault(); var controller = new ShoppingCartController(); var totalCalculate = controller.ShoppingCart_OrderList(); products.InvoiceID = products.InvoiceID; products.EmployeeID = employeeid; products.InvoiceDate = DateTime.Now; products.SubTotal = decimal.Parse(totalCalculate.Sum(x => x.Amount).ToString()); products.GST = decimal.Parse(totalCalculate.Sum(t => t.Quantity * t.Price * decimal.Parse(0.05.ToString())).ToString()); products.Total = total; products = context.Invoices.Add(products); context.SaveChanges(); //InvoiceDetail infoInvoice = new InvoiceDetail(); var invoiceDetails = context.InvoiceDetails.Add(new InvoiceDetail()); foreach (var item in info) { invoiceDetails.InvoiceID = products.InvoiceID; var productfind = context.SalesCartItems.Where(x => x.Product.ProductID == item.ProductID).Single(); productfind.Product.QuantityOnHand = productfind.Product.QuantityOnHand - item.Quantity; invoiceDetails.InvoiceDetailID = invoiceDetails.InvoiceDetailID++; invoiceDetails.ProductID = productfind.ProductID; invoiceDetails.Price = productfind.Product.ItemPrice; invoiceDetails.Quantity = item.Quantity; invoiceDetails = context.InvoiceDetails.Add(invoiceDetails); context.SaveChanges(); } } }
public int Pay_ForCart(NewInvoice invoice) { using (var context = new eRaceContext()) { Invoice newitem = new Invoice(); newitem.EmployeeID = invoice.EmployeeID; newitem.InvoiceDate = invoice.InvoiceDate; newitem.SubTotal = invoice.Subtotal; newitem.GST = invoice.GST; newitem.Total = invoice.Total; newitem = context.Invoices.Add(newitem); InvoiceDetail newDetail = null; foreach (var item in invoice.NewDetails) { newDetail = new InvoiceDetail(); newDetail.ProductID = item.ProductID; newDetail.Quantity = item.Quantity; newDetail.Price = item.Price; context.InvoiceDetails.Add(newDetail); var product = (from x in context.Products where x.ProductID == item.ProductID select x).FirstOrDefault(); product.QuantityOnHand -= newDetail.Quantity; context.Entry(product).Property(y => y.QuantityOnHand).IsModified = true; } return(context.SaveChanges()); } }
public void AddItemToCart(int productID, int quantity, int EmployeeId) { using (var context = new eRaceContext()) { var exists = (from item in context.SalesCartItems where item.EmployeeID == EmployeeId && item.ProductID == productID select item).FirstOrDefault(); if (exists == null) { SalesCartItem inCart = new SalesCartItem(); inCart.EmployeeID = EmployeeId; inCart.ProductID = productID; inCart.Quantity = quantity; context.SalesCartItems.Add(inCart); } else { var newQty = context.SalesCartItems.Where(x => x.EmployeeID == EmployeeId && x.ProductID == productID).FirstOrDefault(); newQty.Quantity += quantity; } context.SaveChanges(); } }
public void ForceCloseOrder(ForceCloseDetails order) { /* command modify Products, Order and OrderDeatails */ // Validation List <Exception> errors = new List <Exception>(); if (order == null) { throw new ArgumentNullException(nameof(order), $"No {nameof(ForceCloseDetails)} was supplied for an existing PO."); } else { if (order.Comment.Trim().Equals(null)) { throw new Exception("Please give a reason in the comment box for why the order is being closed."); } } using (var context = new eRaceContext()) { var given = context.Orders.Find(order.OrderID); if (given == null) { throw new ArgumentException($"The given order id of {order.OrderID} does not exist in the database."); } given.Comment = order.Comment; given.Closed = true; var existingOrder = context.Entry(given); existingOrder.Property(nameof(given.Comment)).IsModified = true; existingOrder.Property(nameof(given.Closed)).IsModified = true; foreach (var item in order.Items) { var givenOrder = context.OrderDetails.Find(item.ProductID); var givenProduct = context.Products.Find(givenOrder.ProductID); if (givenProduct == null) { throw new ArgumentException($"The given product id of {item.ProductID} does not exist in the database."); } givenProduct.QuantityOnOrder = givenProduct.QuantityOnOrder - item.QuantityOutstanding; if (givenProduct.QuantityOnOrder < 0) { givenProduct.QuantityOnOrder = 0; } var existingProduct = context.Entry(givenProduct); existingProduct.Property(nameof(givenProduct.QuantityOnOrder)).IsModified = true; } context.SaveChanges(); } }
public void Force_Closure(int orderid, string comment) { using (var context = new eRaceContext()) { var ForceClosure = from x in context.OrderDetails where x.OrderID == orderid //select x; select x.OrderDetailID; //var OrderDetail = context.OrderDetails.Find(orderid); //var OrderDetailQuantity = OrderDetail.Quantity; //var results = (from x in context.ReceiveOrderItems // where x.OrderDetail.OrderID == orderid // select x.OrderDetailID).ToList(); //foreach (var order in orderlist) //{ // var quantityzero = from x in context.OrderDetails // where //} var commitClosure = context.Orders.Find(orderid); commitClosure.Comment = comment; commitClosure.Closed = true; context.Entry(commitClosure).State = EntityState.Modified; context.SaveChanges(); } }
public void Add_ItemToCart(int employeid, int productid, int qty) { if (qty < 0) { throw new Exception("Quantity cannot be zero"); } else { //var buyItem = context.SalesCartItems.Add(new SalesCartItem()); //buyItem.ProductID = productid; //buyItem.EmployeeID = employeid; //buyItem.Quantity = qty; using (var context = new eRaceContext()) { Product newProduct = null; int product = 0; Product products = new Product(); Product prdct = (from x in context.Products .Where(x => x.ProductID.Equals(productid)) select x).FirstOrDefault(); SalesCartItem exists = (from x in context.SalesCartItems .Where(x => x.EmployeeID.Equals(employeid) && x.ProductID.Equals(productid)) select x).FirstOrDefault(); if (exists == null) { exists = new SalesCartItem(); exists.EmployeeID = employeid; exists.ProductID = productid; exists.UnitPrice = prdct.ItemPrice; exists.Quantity = qty; exists = context.SalesCartItems.Add(exists); product = 1; } else { newProduct = exists.Product.Category.Products.SingleOrDefault(x => x.ProductID == productid); if (newProduct == null) { product = exists.Product.Category.Products.Count() + 1; } else { product = exists.Quantity++; } } context.SaveChanges(); } } }//end of Add_ItemToCart
public void ClearCart(int employeeID, int productID) { using (var context = new eRaceContext()) { var result = context.SalesCartItems.Find(employeeID, productID); context.SalesCartItems.Remove(result); context.SaveChanges(); } }
public void DeleteUnorderedItem(UnorderedInfo item) { using (var context = new eRaceContext()) { var existing = context.UnOrderedItems.Find(item.ItemID); context.UnOrderedItems.Remove(existing); context.SaveChanges(); } }
public int ProcessRefund(List <Refund> returns, int employeeId, decimal total, int orderID) { using (var context = new eRaceContext()) { if (total == 0) { throw new Exception("There are no returns on this order."); } List <int> returned = (from item in context.StoreRefunds where item.OriginalInvoiceID == orderID select item.ProductID).ToList(); var invoice = new Invoice { EmployeeID = employeeId, InvoiceDate = DateTime.Now, SubTotal = total, GST = Math.Round(total * (decimal)0.05, 2), Total = Math.Round(total * (decimal)1.05, 2) }; foreach (var item in returns) { bool alreadyReturned = returned.Contains(item.ProductID); if (alreadyReturned == false) { if (item.Reason != "") { invoice.StoreRefunds.Add(new StoreRefund { InvoiceID = invoice.InvoiceID, ProductID = item.ProductID, OriginalInvoiceID = orderID, Reason = item.Reason }); var newQOH = context.Products.Where(x => x.ProductID == item.ProductID).FirstOrDefault(); newQOH.QuantityOnHand += item.Quantity; } else { throw new Exception($"{item.ItemName} requires a return reason."); } } else { throw new Exception($"{item.ItemName} has already been returned"); } } context.Invoices.Add(invoice); context.SaveChanges(); return(invoice.InvoiceID); } }
public int Driver_Update(RaceDetail driver) { using (var context = new eRaceContext()) { context.Entry(driver).State = System.Data.Entity.EntityState.Modified; return(context.SaveChanges()); } }
public void RemoveCartItem(int productID, int employeeID) { using (var context = new eRaceContext()) { var result = context.SalesCartItems.Where(x => x.ProductID == productID && x.EmployeeID == employeeID).FirstOrDefault(); context.SalesCartItems.Remove(result); context.SaveChanges(); } }
public int Insert_ReturnItems(UnOrderedItem item) { using (var context = new eRaceContext()) { context.UnOrderedItems.Add(item); context.SaveChanges(); return(item.ItemID); } }
public int Driver_Add(RaceDetail driver) { using (var context = new eRaceContext()) { //yet to develop the business logic and error handling context.RaceDetails.Add(driver); //staging context.SaveChanges(); //committed return(driver.RaceDetailID); //return new id value } }
}//end of Add_ItemToCart public void Delete_ProductItem(int employeeid, int productid) { using (var context = new eRaceContext()) { SalesCartItem exists = (from x in context.SalesCartItems .Where(x => x.EmployeeID.Equals(employeeid) && x.ProductID.Equals(productid)) select x).FirstOrDefault(); var findItem = context.SalesCartItems.Where(x => x.ProductID == productid).FirstOrDefault(); context.Entry(findItem).State = System.Data.Entity.EntityState.Deleted; context.SaveChanges(); } }//end of Delete_ProductItem
public void AddingNewRecieveOrder(int orderid, int employeeid) { using (var context = new eRaceContext()) { var addrecieveorder = context.ReceiveOrders.Add(new ReceiveOrder()); addrecieveorder.OrderID = orderid; addrecieveorder.ReceiveDate = DateTime.Now; addrecieveorder.EmployeeID = employeeid; context.SaveChanges(); } }
}//end of Delete_ProductItem public void Delete_Order(int OrderID) { using (var context = new eRaceContext()) { OrderDetail exists = (from x in context.OrderDetails .Where(x => x.OrderID.Equals(OrderID)) select x).FirstOrDefault(); var findItem = context.OrderDetails.Where(x => x.OrderDetailID == OrderID).FirstOrDefault(); context.Entry(findItem).State = System.Data.Entity.EntityState.Deleted; context.SaveChanges(); } }//end of Delete_ProductItem
public void Quantity_Refresh(int orderDetailID, int quantity, decimal unitCost) { using (var context = new eRaceContext()) { OrderDetail exists = (from x in context.OrderDetails .Where(x => x.OrderDetailID.Equals(orderDetailID)) select x).FirstOrDefault(); exists.OrderDetailID = orderDetailID; exists.Quantity = quantity; exists.Cost = unitCost; context.Entry(exists).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } }//End of Quantity_Refresh
public void Remove_UnorderedItems() { using (var context = new eRaceContext()) { var removeunordereditems = (from x in context.UnOrderedItems select x).ToList(); foreach (var removeitem in removeunordereditems) { var RemovingItem = context.UnOrderedItems.Find(removeitem.ItemID); var letsremoveitem = context.UnOrderedItems.Remove(RemovingItem); } context.SaveChanges(); } }
public void AddUnorderedItem(UnorderedInfo item) { using (var context = new eRaceContext()) { var newItem = new UnOrderedItem { ItemName = item.ItemName, VendorProductID = item.VendorID, Quantity = item.Quantity }; context.UnOrderedItems.Add(newItem); context.SaveChanges(); } }
public void ClearUnOrderedItems() { /* use CRUD to delete all items in table (Try .Clear()) */ using (var context = new eRaceContext()) { var results = from item in context.UnOrderedItems select item; foreach (var detail in results) { context.UnOrderedItems.Remove(detail); } context.SaveChanges(); } }
public void CreateRefund(RefundRequired request) { using (var context = new eRaceContext()) { foreach (var item in request.RequiredInvoice) { var newinvoice = new Invoice { EmployeeID = item.EmployeeID, InvoiceDate = item.InvoiceDate, SubTotal = item.SubTotal, GST = item.GST, Total = item.Total }; context.Invoices.Add(newinvoice); } foreach (var item in request.ReuquiredDetail) { var newdetail = new InvoiceDetail { ProductID = item.ProductID, Quantity = item.Quantity }; var product = (from x in context.Products where x.ProductID == item.ProductID select x).FirstOrDefault(); product.QuantityOnHand += newdetail.Quantity; context.Entry(product).Property(y => y.QuantityOnHand).IsModified = true; } foreach (var item in request.RequiredStore) { var newstore = new StoreRefund { OriginalInvoiceID = item.OriginalInvoiceID, ProductID = item.ProductID, Reason = item.Reason, }; context.StoreRefunds.Add(newstore); } context.SaveChanges(); } }
public void ClearButton_Shopping(int employeeid) { using (var context = new eRaceContext()) { SalesCartItem exists = (from x in context.SalesCartItems .Where(x => x.EmployeeID.Equals(employeeid)) select x).FirstOrDefault(); var clearCart = context.SalesCartItems.Where(x => x.EmployeeID == employeeid).ToList(); foreach (var item in clearCart) { context.Entry(item).State = System.Data.Entity.EntityState.Deleted; } context.Entry(exists).State = System.Data.Entity.EntityState.Deleted; context.SaveChanges(); } }
public void DeleteOrder(OrderInfo a) { using (var context = new eRaceContext()) { var order = context.Orders.Find(a.OrderID); foreach (var x in a.OrderDetails) { var orderitem = context.OrderDetails.Find(x.OrderDetailID); context.OrderDetails.Remove(orderitem); } context.Orders.Remove(order); context.SaveChanges(); } }
}//end of Delete_ProductItem public void Quantity_Refresh(int employeeid, int productid, int quantity) { using (var context = new eRaceContext()) { SalesCartItem exists = (from x in context.SalesCartItems .Where(x => x.EmployeeID.Equals(employeeid) && x.ProductID.Equals(productid)) select x).FirstOrDefault(); exists.EmployeeID = employeeid; exists.ProductID = productid; exists.Quantity = quantity; context.Entry(exists).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } }//End of Quantity_Refresh
public int UnorderedItem_Delete(int itemid) { using (var context = new eRaceContext()) { var existing = context.UnOrderedItems.Find(itemid); if (existing == null) { throw new Exception("Item does not exist. No Delete required"); } else { context.UnOrderedItems.Remove(existing); return(context.SaveChanges()); } } }
public void Add_DriverToRaceAndGenerateInvoice(int raceid, int memberid, int carid, int employeeid, decimal racefee, decimal rentalfee) { using (var context = new eRaceContext()) { List <string> reasons = new List <string>(); RaceDetail exists = (from x in context.RaceDetails where x.MemberID == memberid select x).FirstOrDefault(); if (exists == null) { exists = new RaceDetail(); exists.RaceID = raceid; exists.MemberID = memberid; exists.RaceFee = racefee; exists.RentalFee = rentalfee; exists.CarID = carid; exists = context.RaceDetails.Add(exists); } else { reasons.Add("Driver already registered for this race"); } if (reasons.Count() > 0) { throw new BusinessRuleException("Adding driver to race", reasons); } else { Invoice newInvoice = new Invoice(); newInvoice.EmployeeID = employeeid; newInvoice.InvoiceDate = DateTime.Today; newInvoice.SubTotal = racefee + rentalfee; context.Invoices.Add(newInvoice); context.SaveChanges(); } } }
public int ProcessSale(List <CartItems> cartItems, int EmployeeID, decimal total) { using (var context = new eRaceContext()) { if (cartItems == null) { throw new Exception("There are no items in the cart."); } var invoice = new Invoice { EmployeeID = EmployeeID, InvoiceDate = DateTime.Now, SubTotal = total, GST = Math.Round(total * (decimal)0.05, 2), Total = Math.Round(total * (decimal)1.05, 2) }; foreach (var item in cartItems) { invoice.InvoiceDetails.Add(new InvoiceDetail { ProductID = item.ProductID, Quantity = item.Quantity, Price = item.ItemPrice }); var newQOH = context.Products.Where(x => x.ProductID == item.ProductID).FirstOrDefault(); newQOH.QuantityOnHand -= item.Quantity; ClearCart(EmployeeID, item.ProductID); } context.Invoices.Add(invoice); context.SaveChanges(); return(invoice.InvoiceID); } }
public void PlaceOrder(UpdatedOrder item) { using (var context = new eRaceContext()) { if (item.OrderID == 0) { // adding a new order var order = new Order(); order.TaxGST = item.Tax; order.SubTotal = item.SubTotal; order.VendorID = item.VendorID; order.Comment = item.VendorComment; order.Closed = false; order.OrderDate = DateTime.Now; var list = (from a in context.Orders where a.OrderNumber != null select a.OrderNumber); order.OrderNumber = int.Parse(list.Max().ToString()) + 1; order.EmployeeID = 1; context.Orders.Add(order); foreach (var y in item.UpdatedOrderDetails) { var additem = new OrderDetail { ProductID = y.ProductID, Quantity = y.OrderQty, Cost = y.UnitCost, OrderUnitSize = y.UnitSize }; var product = context.Products.Find(additem.ProductID); product.QuantityOnOrder += additem.Quantity * additem.OrderUnitSize; context.Entry(product).State = System.Data.Entity.EntityState.Modified; order.OrderDetails.Add(additem); } context.SaveChanges(); } else // updating an pending order { var order = context.Orders.Find(item.OrderID); var orderdetails = context.OrderDetails.Where(x => x.OrderID == order.OrderID).ToList(); order.OrderID = item.OrderID; order.TaxGST = item.Tax; order.SubTotal = item.SubTotal; order.VendorID = item.VendorID; order.Comment = item.VendorComment; order.Closed = false; order.OrderDate = DateTime.Now; var list = (from a in context.Orders where a.OrderNumber != null select a.OrderNumber); order.OrderNumber = int.Parse(list.Max().ToString()) + 1; order.EmployeeID = 1; foreach (var a in orderdetails) { var existingitem = item.UpdatedOrderDetails.SingleOrDefault(x => x.ProductID == a.ProductID); if (existingitem == null) { context.Entry(a).State = System.Data.Entity.EntityState.Deleted; } else { a.Cost = existingitem.UnitCost; a.Quantity = existingitem.OrderQty; context.Entry(a).State = System.Data.Entity.EntityState.Modified; var product = context.Products.Find(a.ProductID); product.QuantityOnOrder += existingitem.OrderQty * existingitem.UnitSize; context.Entry(product).State = System.Data.Entity.EntityState.Modified; } } foreach (var y in item.UpdatedOrderDetails) { var newproduct = !orderdetails.Any(x => x.ProductID == y.ProductID); if (newproduct == true) { var additem = new OrderDetail { ProductID = y.ProductID, OrderID = item.OrderID, Quantity = y.OrderQty, Cost = y.UnitCost, OrderUnitSize = y.UnitSize }; order.OrderDetails.Add(additem); var product = context.Products.Find(additem.ProductID); product.QuantityOnOrder += additem.Quantity * additem.OrderUnitSize; context.Entry(product).State = System.Data.Entity.EntityState.Modified; } } context.Entry(order).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } } }
public void SaveOrder(UpdatedOrder item, int?EmployeeID) { using (var context = new eRaceContext()) { if (item.OrderID == 0) // adding a new order { var order = new Order(); order.TaxGST = item.Tax; order.SubTotal = item.SubTotal; order.VendorID = item.VendorID; order.Comment = item.VendorComment; order.Closed = false; order.OrderDate = null; order.OrderNumber = null; order.EmployeeID = EmployeeID.Value; context.Orders.Add(order); foreach (var y in item.UpdatedOrderDetails) { var addskill = new OrderDetail { ProductID = y.ProductID, Quantity = y.OrderQty, Cost = y.UnitCost, OrderUnitSize = y.UnitSize }; order.OrderDetails.Add(addskill); } context.SaveChanges(); } else // updating an existing order { var order = context.Orders.Find(item.OrderID); var orderdetails = context.OrderDetails.Where(x => x.OrderID == order.OrderID).ToList(); order.OrderID = item.OrderID; order.TaxGST = item.Tax; order.SubTotal = item.SubTotal; order.VendorID = item.VendorID; order.Comment = item.VendorComment; order.Closed = false; order.OrderDate = null; order.OrderNumber = null; order.EmployeeID = 1; foreach (var a in orderdetails) { var existingitem = item.UpdatedOrderDetails.SingleOrDefault(x => x.ProductID == a.ProductID); if (existingitem == null) { context.Entry(a).State = System.Data.Entity.EntityState.Deleted; } else { a.Cost = existingitem.UnitCost; a.Quantity = existingitem.OrderQty; context.Entry(a).State = System.Data.Entity.EntityState.Modified; } } foreach (var y in item.UpdatedOrderDetails) { var newproduct = !orderdetails.Any(x => x.ProductID == y.ProductID); if (newproduct == true) { var addskill = new OrderDetail { ProductID = y.ProductID, OrderID = item.OrderID, Quantity = y.OrderQty, Cost = y.UnitCost, OrderUnitSize = y.UnitSize }; order.OrderDetails.Add(addskill); } } context.Entry(order).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } } }
public void ReceiveShimpmentOrder(ReceivedOrderDetails order) { /* command modify Products, ReceiveOrders, ReceiveOrderItems and ReturnOrderItems */ List <Exception> errors = new List <Exception>(); using (var context = new eRaceContext()) { var existing = context.Orders.Find(order.OrderID); if (order == null) { errors.Add(new ArgumentNullException(nameof(order), $"No {nameof(order)} was supplied for ")); } else { if (existing.Closed == true) { errors.Add(new BusinessRuleException <bool>("The order you are accessing is already closed.", nameof(existing.Closed), existing.Closed)); } if (order.OrderID == 0) { errors.Add(new BusinessRuleException <int>("The OrderID is required.", nameof(order.OrderID), order.OrderID)); } if (order.ReceivedOrder.OrderID == 0) { errors.Add(new BusinessRuleException <int>("The OrderID is required.", nameof(order.ReceivedOrder.OrderID), order.ReceivedOrder.OrderID)); } if (order.ReceivedOrder.EmployeeID == 0) { errors.Add(new BusinessRuleException <int>("The Employee ID is required.", nameof(order.ReceivedOrder.EmployeeID), order.ReceivedOrder.EmployeeID)); } foreach (var item in order.ReceiveOrderItems) { var existingOrderDetail = context.OrderDetails.Find(item.OrderDetailID); var itemsize = context.OrderDetails.Find(item.OrderDetailID).OrderUnitSize; if (existingOrderDetail == null) { errors.Add(new ArgumentNullException(nameof(item.OrderDetailID), $"There is no Order Detail with the ID given.")); } if (item.ItemQuantity < 1) { errors.Add(new BusinessRuleException <int>("The item quantity must be greater than 0", nameof(item.ItemQuantity), item.ItemQuantity)); } if (item.ItemQuantity / itemsize >= context.OrderDetails.Find(item.OrderDetailID).Quantity + 1) { errors.Add(new BusinessRuleException <int>("The total amount received cannot exceed the amount ordered by 1 package.", nameof(item.ItemQuantity), item.ItemQuantity)); } } foreach (var item in order.Products) { if (item.QuantityReceived < 1) { errors.Add(new BusinessRuleException <int>("The number of received items must be postive. ", nameof(item.QuantityReceived), item.QuantityReceived)); } } foreach (var item in order.ReturnOrderItems) { if (item.ItemQuantity < 1) { errors.Add(new BusinessRuleException <int>("The number of returned items is required.", nameof(item.ItemQuantity), item.ItemQuantity)); } } } if (errors.Any()) { throw new BusinessRuleCollectionException("Unable to receive shipment.", errors); } // ReceiveOrder var newReceiveOrder = new ReceiveOrder { OrderID = order.ReceivedOrder.OrderID, ReceiveDate = order.ReceivedOrder.ReceiveDate, EmployeeID = order.ReceivedOrder.EmployeeID }; context.ReceiveOrders.Add(newReceiveOrder); int id = newReceiveOrder.ReceiveOrderID; // ReceiveOrderItems foreach (var item in order.ReceiveOrderItems) { var newReceiveOrderItem = new ReceiveOrderItem { ReceiveOrderID = id, OrderDetailID = item.OrderDetailID, ItemQuantity = item.ItemQuantity }; context.ReceiveOrderItems.Add(newReceiveOrderItem); } // ReturnOrderItems foreach (var item in order.ReturnOrderItems) { var newReturnOrderItem = new ReturnOrderItem { ReceiveOrderID = id, OrderDetailID = item.OrderDetailID, ItemQuantity = item.ItemQuantity, Comment = item.Comment, UnOrderedItem = item.UnOrderedItem, VendorProductID = item.VendorProductID }; context.ReturnOrderItems.Add(newReturnOrderItem); } // Products foreach (var item in order.Products) { var given = context.Products.Find(context.OrderDetails.Find(item.ProductID).ProductID); if (given == null) { throw new ArgumentException($"The given product id of {item.ProductID} does not exist in the database.", nameof(item.ProductID)); } given.QuantityOnHand = given.QuantityOnHand + item.QuantityReceived; given.QuantityOnOrder = given.QuantityOnOrder - item.QuantityReceived; if (given.QuantityOnOrder < 0) { given.QuantityOnOrder = 0; } var existingProduct = context.Entry(given); existingProduct.Property(nameof(given.QuantityOnHand)).IsModified = true; existingProduct.Property(nameof(given.QuantityOnOrder)).IsModified = true; } // Check if the order has been filled var outstanding = context.Orders.Find(order.OrderID).OrderDetails; var num = 0; foreach (var item in outstanding) { if (context.OrderDetails.Find(item.OrderDetailID).Quantity *context.OrderDetails.Find(item.OrderDetailID).OrderUnitSize - (context.OrderDetails.Find(item.OrderDetailID).ReceiveOrderItems.Select(x => x.ItemQuantity)).FirstOrDefault() == 0) { num++; } } if (num == outstanding.Count) { var givenOrder = context.Orders.Find(order.OrderID); givenOrder.Closed = true; givenOrder.Comment = "Order Complete"; var existingOrder = context.Entry(givenOrder); existingOrder.Property(nameof(givenOrder.Comment)).IsModified = true; existingOrder.Property(nameof(givenOrder.Closed)).IsModified = true; } context.SaveChanges(); } }