public ActionResult PackingListEdit(int id, int? contractId, FormCollection collection) { if (!UserInfo.CurUser.HasRight("业务管理-PackingList&Invoice")) return Redirect("~/content/AccessDeny.htm"); using (TransactionScope transcope = new TransactionScope()) { PackingList p = db.PackingLists.Find(id); if (p == null) { p = new PackingList { ContractId = (int)contractId }; db.PackingLists.Add(p); } var contractItems = (from o in db.SaleContractItems where o.ContractId == p.ContractId select o).AsNoTracking().ToList(); List<Product> products = new List<Product>(); foreach (var item in contractItems) { item.Num -= p.GetPackagedNumByContract(db, item.ProductId); ; products.Add(db.Products.Find(item.ProductId)); } List<PackingListItem> newList = JsonConvert.DeserializeObject<List<PackingListItem>>(collection["itemsString"]); TryUpdateModel(p, "", new string[] { }, new string[] { }, collection); PackingList packing = null; if (p.Code == null) { packing = (from o in db.PackingLists where o.Code == null && o.ContractId == p.ContractId && o.Id != p.Id select o). AsNoTracking().FirstOrDefault(); } else { packing = (from o in db.PackingLists where o.Code == p.Code && o.ContractId == p.ContractId && o.Id != p.Id select o). AsNoTracking().FirstOrDefault(); } if (packing != null) { ModelState.AddModelError("Code", "SN existed"); } foreach (var item in newList) { if (item.Num == decimal.Zero) continue; //出货数不能大于合同数 //contractItems.ForEach(o => //{ // if (o.ProductId == item.ProductId && o.Num < item.Num) // { // string prodname = ""; // products.ForEach(prod => // { // if (prod.Id == o.ProductId) prodname = p.Code + " " + prod.ItemName; // }); // ModelState.AddModelError("", "出货数量不能大于剩余未出数量" + prodname); // } //}); } if (ModelState.IsValid) { db.SaveChanges(); List<PackingListItem> oldList = (from o in db.PackingListItems where o.PackingListId == id select o).ToList(); foreach (var item in oldList) { db.PackingListItems.Remove(item); } foreach (var item in newList) { if (item.Num == decimal.Zero) continue; item.PackingListId = p.Id; item.Id = 0; db.PackingListItems.Add(item); } db.SaveChanges(); transcope.Complete(); return Redirect(string.Format("../SalesContractView/{0}?tab=pack#pack{1}", p.ContractId, p.Id, "pack")); } ViewBag.ItemsString = JsonConvert.SerializeObject(newList); ViewBag.ContractItemsString = JsonConvert.SerializeObject(contractItems); ViewBag.ProductsString = JsonConvert.SerializeObject(products); return View(p); } }
public ActionResult PackingListEdit(int id, int? contractId) { if (!UserInfo.CurUser.HasRight("业务管理-PackingList&Invoice")) return Redirect("~/content/AccessDeny.htm"); PackingList p = db.PackingLists.Find(id); if (p == null) { p = new PackingList { PackingDate = DateTime.Today, ContractId = (int)contractId }; } else { } var contractItems = (from o in db.SaleContractItems where o.ContractId == p.ContractId select o).AsNoTracking().ToList(); List<Product> products = new List<Product>(); foreach (var item in contractItems) { //这里取product,小心price问题 item.Num -= p.GetPackagedNumByContract(db, item.ProductId); products.Add(db.Products.Find(item.ProductId)); } if (id != 0) { var items = (from o in db.PackingListItems where o.PackingListId == p.Id select o); ViewBag.ItemsString = JsonConvert.SerializeObject(items); } else { var enums = Enum.GetValues(typeof(SalesContractState)); for (var i = 0; i < enums.Length; i++) { Enum.GetName(typeof(SalesContractState), enums.GetValue(i)); } var items = new List<PackingListItem>(); foreach (var contractItem in contractItems) { items.Add(new PackingListItem { Num = contractItem.Num, ProductId = contractItem.ProductId }); } ViewBag.ItemsString = JsonConvert.SerializeObject(items); } ViewBag.ContractItemsString = JsonConvert.SerializeObject(contractItems); ViewBag.ProductsString = JsonConvert.SerializeObject(products); return View(p); }