public IActionResult UpdateChinaOrder(int Id) { ChinaOrder chinaOrder = _unitOfWork.ChinaOrder.GetAll().Where(a => a.Id == Id).FirstOrDefault(); ChinaOrderVM chinaOrderVM = new ChinaOrderVM() { chinaOrder = chinaOrder, ProductList = _unitOfWork.Product.GetAll().OrderBy(a => a.ProductName). Select(i => new SelectListItem { Text = i.ProductName, Value = i.Id.ToString() }) }; if (chinaOrder.IgnoreMissingQuantity) { ViewBag.Ignore = true; } else { ViewBag.Ignore = false; } if (chinaOrder.ReceivedAll) { ViewBag.Received = true; } else { ViewBag.Received = false; } ViewBag.QuantityZero = false; return(View(chinaOrderVM)); }
public IActionResult UpdateChinaOrder(ChinaOrderVM chinaOrderVM) { ChinaOrderVM chinaOrderVM2 = new ChinaOrderVM() { chinaOrder = _unitOfWork.ChinaOrder.GetAll().Where(a => a.Id == chinaOrderVM.chinaOrder.Id).FirstOrDefault(), ProductList = _unitOfWork.Product.GetAll().OrderBy(a => a.ProductName). Select(i => new SelectListItem { Text = i.ProductName, Value = i.Id.ToString() }) }; ViewBag.QuantityZero = false; if (ModelState.IsValid) { if (chinaOrderVM.chinaOrder.QuantityReceived <= 0) { ViewBag.QuantityZero = true; } else { ChinaOrder oldChinaOrder = _unitOfWork.ChinaOrder.GetAll().Where(a => a.Id == chinaOrderVM.chinaOrder.Id).FirstOrDefault(); Product product = _unitOfWork.Product.GetAll().Where(a => a.Id == chinaOrderVM.chinaOrder.ProductId).FirstOrDefault(); int QuantityUpdate = chinaOrderVM.chinaOrder.QuantityReceived - oldChinaOrder.QuantityReceived; //update Inventory product.InventoryCount = product.InventoryCount + QuantityUpdate; //update onTheWayInventory if (QuantityUpdate == 0 && !chinaOrderVM.chinaOrder.IgnoreMissingQuantity)//ignore didnt change and quantity didnt change { } else if (QuantityUpdate == 0 && chinaOrderVM.chinaOrder.IgnoreMissingQuantity && !oldChinaOrder.IgnoreMissingQuantity)//quantity didnt change ignore did { int missingQ = chinaOrderVM.chinaOrder.Quantity - chinaOrderVM.chinaOrder.QuantityReceived; if (missingQ >= 0) { product.OnTheWayInventory = product.OnTheWayInventory - missingQ; } } else if (QuantityUpdate != 0 && !chinaOrderVM.chinaOrder.IgnoreMissingQuantity)//quantity changed and ignore didnt { if (chinaOrderVM.chinaOrder.QuantityReceived > chinaOrderVM.chinaOrder.Quantity) { product.OnTheWayInventory = product.OnTheWayInventory - QuantityUpdate + (chinaOrderVM.chinaOrder.QuantityReceived - chinaOrderVM.chinaOrder.Quantity); } else { product.OnTheWayInventory = product.OnTheWayInventory - QuantityUpdate; } } else if (QuantityUpdate != 0 && chinaOrderVM.chinaOrder.IgnoreMissingQuantity && !oldChinaOrder.IgnoreMissingQuantity)//quantity changed and ignore changed { if (chinaOrderVM.chinaOrder.QuantityReceived > chinaOrderVM.chinaOrder.Quantity) { product.OnTheWayInventory = product.OnTheWayInventory - QuantityUpdate + (chinaOrderVM.chinaOrder.QuantityReceived - chinaOrderVM.chinaOrder.Quantity); } else { product.OnTheWayInventory = product.OnTheWayInventory - QuantityUpdate - (chinaOrderVM.chinaOrder.Quantity - chinaOrderVM.chinaOrder.QuantityReceived); } } _unitOfWork.ChinaOrder.update(chinaOrderVM.chinaOrder); _unitOfWork.Save(); ViewBag.ShowMsg = 1; } } ViewBag.ShowMsg = 1; return(View(chinaOrderVM2)); }