public ActionResult Add(SupplierAmountProductItem request) { var model = new Base.SupplierAmountProduct() { SupplierId = request.SupplierId, ProductID = request.ProductID, PublicationDate = request.PublicationDate, ExpireDate = request.ExpireDate, IsAlwayExist = request.IsAlwayExist, AmountEstimate = request.AmountEstimate, AmountPayed = request.AmountPayed, CallDate = request.CallDate, Note = request.Note, IsDelete = false, CreatedDate = DateTime.Now.TotalSeconds() }; var temp = _cateRecipeDa.GetItemByCateIdUser(request.ProductID ?? 0); if (temp != null) { var lstProduct = temp.LstCategoryRecipeItems.Select(item => new Shop_Product_Comingsoon { ProductID = item.ProductId, DateEx = request.PublicationDate, Quantity = request.AmountEstimate * item.Quantity, Price = item.PriceProduct, TotalPrice = request.AmountEstimate * item.Quantity * item.PriceProduct, QuantityOut = 0, }) .ToList(); foreach (var itema in temp.LstMappingCategoryRecipeItems) { temp = _cateRecipeDa.GetItemByCateIdUser(itema.CategoryID ?? 0); if (temp != null) { lstProduct.AddRange(temp.LstCategoryRecipeItems.Select(item1 => new Shop_Product_Comingsoon { ProductID = item1.ProductId, DateEx = request.PublicationDate, Quantity = request.AmountEstimate * item1.Quantity, Price = item1.PriceProduct, TotalPrice = request.AmountEstimate * item1.Quantity * item1.PriceProduct, QuantityOut = 0, })); } } model.Shop_Product_Comingsoon = lstProduct; _da.Add(model); _da.Save(); } return(Json(new JsonMessage() { Erros = false }, JsonRequestBehavior.AllowGet)); }
public ActionResult SaveSupplier(RequestWareSupplierRequest request) { var msg = new JsonMessage(false, "Cập nhật dữ liệu thành công."); var item = _da.GetbyId(request.ToDayCode, request.ProductId, request.SupplierId); var lst = _daSupplierAmountProduct.GetItem(request.ToDayCode, request.SupplierId, request.ProductId); var valueReady = lst.Sum(m => m.AmountEstimate - m.AmountPayed); if (item == null) { if (valueReady < request.Quantity) { return(Json(new JsonMessage() { Erros = true, Message = "Số lượng nhà cung câp không đủ" })); } _da.AddSupplier(request); var amountUpdate = request.Quantity; // tinh lai so da ban o nha cung cap foreach (var supplierAmountProduct in lst) { if (supplierAmountProduct.AmountEstimate - supplierAmountProduct.AmountPayed >= amountUpdate) { supplierAmountProduct.AmountPayed += amountUpdate; amountUpdate = 0; break; } else if (supplierAmountProduct.AmountEstimate > supplierAmountProduct.AmountPayed) { amountUpdate -= supplierAmountProduct.AmountEstimate - supplierAmountProduct.AmountPayed; supplierAmountProduct.AmountPayed = supplierAmountProduct.AmountEstimate; } } if (amountUpdate != 0) { throw new Exception("Quantity is error"); } } else { var amountDiff = request.Quantity - item.Quantity; foreach (var supplierAmountProduct in lst) { if (supplierAmountProduct.AmountPayed + amountDiff < 0) { supplierAmountProduct.AmountPayed = 0; amountDiff += supplierAmountProduct.AmountPayed; } else if (supplierAmountProduct.AmountPayed + amountDiff > supplierAmountProduct.AmountEstimate) { amountDiff += supplierAmountProduct.AmountEstimate - supplierAmountProduct.AmountPayed; supplierAmountProduct.AmountPayed = supplierAmountProduct.AmountEstimate; } else { supplierAmountProduct.AmountPayed = request.Quantity; break; } } item.Quantity = request.Quantity; item.Price = request.Price; } _da.Save(); _daSupplierAmountProduct.Save(); return(Json(msg, JsonRequestBehavior.AllowGet)); }