public async Task <ActionResult> UpdateOrder(StorageWarehousingRequest request) { var customer = _customerDa.GetByQrCode(request.CustomerCode); if (customer == null) { return(Json(new JsonMessage() { Erros = true, Message = "Customer is not exits" }, JsonRequestBehavior.AllowGet)); } const StatusWarehouse status = StatusWarehouse.New | StatusWarehouse.Pending | StatusWarehouse.WattingConfirm; var order = _da.GetById(request.Code, customer.ID); if (!status.HasFlag((StatusWarehouse)order.Status)) { return(Json(new JsonMessage() { Erros = true, Message = "Đơn hàng đã được xác nhận" }, JsonRequestBehavior.AllowGet)); } //get remove item var removeitems = order.DN_RequestWare.Where(m => request.RequestWares.All(n => n.CateId != m.CateID) && m.IsDelete == false).ToList(); foreach (var dnRequestWare in removeitems) { dnRequestWare.IsDelete = true; } //update item var updateitems = order.DN_RequestWare.Where(m => request.RequestWares.Any(n => m.CateID == n.CateId && m.IsDelete == false)).ToList(); foreach (var dnRequestWare in updateitems) { var requestitem = request.RequestWares.FirstOrDefault(m => m.CateId == dnRequestWare.CateID); dnRequestWare.Quantity = requestitem.Quantity; dnRequestWare.DN_RequestWareDetail.Clear(); foreach (var detail in requestitem.Details) { dnRequestWare.DN_RequestWareDetail.Add(new DN_RequestWareDetail() { RequestWareId = Guid.NewGuid(), ProductId = detail.ProductId, Quantity = detail.Quantity, }); } } //get add item var additems = request.RequestWares.Where(m => !order.DN_RequestWare.Any(n => n.CateID == m.CateId && n.IsDelete == false)).ToList(); foreach (var item in additems) { var requestWare = new DN_RequestWare() { GID = Guid.NewGuid(), CateID = item.CateId, Quantity = item.Quantity, QuantityActive = item.Quantity, Price = item.Price, TotalPrice = item.TotalPrice, Today = request.ReceiveDate.DecimalToDate().Date.TotalSeconds(), IsDelete = false }; foreach (var detail in item.Details) { requestWare.DN_RequestWareDetail.Add(new DN_RequestWareDetail() { RequestWareId = Guid.NewGuid(), ProductId = detail.ProductId, Quantity = detail.Quantity, }); } order.DN_RequestWare.Add(requestWare); } _da.Save(); return(Json(new JsonMessage { Erros = false })); }