Esempio n. 1
0
        public ActionResult PurchaseContractEdit(int id, int? supplierId, int? contractid)
        {
            if (!UserInfo.CurUser.HasRight("业务管理-采购合同录入")) return Redirect("~/content/AccessDeny.htm");
            PurchaseContract p = null;
            List<PurchaseContractItem> items = new List<PurchaseContractItem>();
            if (id == 0)
            {
                p = new PurchaseContract { SupplierId = (int)supplierId, SalesContractId = (int)contractid };
                List<SalesContractItem> contractItems = (from o in db.SaleContractItems where o.ContractId == contractid select o).ToList();
                List<Product> products = new List<Product>();
                foreach (var i in contractItems)
                {
                    Product prod = db.Products.Find(i.ProductId);
                    if (p.SupplierId == prod.SupplierId)
                    {
                        products.Add(prod);
                        PurchaseContractItem item = new PurchaseContractItem
                                                        {
                                                            Num = i.Num,
                                                            ProductId = prod.Id,
                                                            PurchasePrice = prod.PurchasePrice,
                                                            Total = i.Num * prod.PurchasePrice
                                                        };
                        items.Add(item);
                    }
                }
                ViewBag.Products = products;
            }
            else
            {
                p = db.PurchaseContracts.Find(id);
                if (p == null) return View("ShowError", "", "找不到合同");
                items = (from o in db.PurchaseContractItems where o.ContractId == id select o).ToList();
                List<Product> products = new List<Product>();
                items.ForEach(o => products.Add((from q in db.Products where q.Id == o.ProductId select q).AsNoTracking().FirstOrDefault()));
                ViewBag.Products = products;
            }
            ViewBag.Items = items;
            SalesContract s = db.SalesContracts.Find(p.SalesContractId);
            if (s == null) return View("ShowError", "", "找不到销售合同");

            if (!UserInfo.CurUser.HasRight("业务管理-销售记录录入")) return Redirect("~/content/AccessDeny.htm");
            return View(p);
        }
Esempio n. 2
0
        public ActionResult PurchaseContractEdit(int id, int? supplierId, int? contractid, FormCollection collection)
        {
            if (!UserInfo.CurUser.HasRight("业务管理-采购合同录入")) return Redirect("~/content/AccessDeny.htm");
            using (TransactionScope transcope = new TransactionScope())
            {
                if (!UserInfo.CurUser.HasRight("业务管理-销售记录录入")) return Redirect("~/content/AccessDeny.htm");
                PurchaseContract p = null;
                List<PurchaseContractItem> items = new List<PurchaseContractItem>();
                if (id == 0)
                {
                    p = new PurchaseContract { SupplierId = (int)supplierId, SalesContractId = (int)contractid };
                    db.PurchaseContracts.Add(p);
                    List<SalesContractItem> contractItems =
                        (from o in db.SaleContractItems where o.ContractId == contractid select o).ToList();
                    List<Product> products = new List<Product>();
                    foreach (var i in contractItems)
                    {
                        Product prod = db.Products.Find(i.ProductId);
                        if (prod.SupplierId == p.SupplierId)
                        {
                            products.Add(prod);
                            PurchaseContractItem item = new PurchaseContractItem
                                                            {
                                                                Num = i.Num,
                                                                ProductId = prod.Id,
                                                                PurchasePrice = prod.PurchasePrice,
                                                                Total = i.Num * prod.PurchasePrice
                                                            };
                            items.Add(item);
                        }
                    }
                    ViewBag.Products = products;
                }
                else
                {
                    p = db.PurchaseContracts.Find(id);
                    if (p == null) return View("ShowError", "", "找不到合同");
                    items = (from o in db.PurchaseContractItems where o.ContractId == id select o).ToList();
                    List<Product> products = new List<Product>();
                    items.ForEach(
                        o =>
                        products.Add(
                            (from q in db.Products where q.Id == o.ProductId select q).AsNoTracking().FirstOrDefault()));
                    ViewBag.Products = products;
                }
                ViewBag.Items = items;
                TryUpdateModel(p, collection);
                var contract =
                  (from o in db.PurchaseContracts where o.Code == p.Code && o.Id != p.Id select o).AsNoTracking().
                      FirstOrDefault();
                if (contract != null)
                {
                    ModelState.AddModelError("Code", "SN existed");
                }
                if (ModelState.IsValid)
                {

                    db.SaveChanges();
                    if (id == 0)
                    {
                        items.ForEach(o =>
                                          {
                                              o.ContractId = p.Id;
                                              db.PurchaseContractItems.Add(o);
                                          });
                        db.SaveChanges();
                    }
                    transcope.Complete();
                    return Redirect("../PurchaseContractView/" + @p.Id);
                }
                SalesContract s = db.SalesContracts.Find(p.SalesContractId);
                if (s == null) return View("ShowError", "", "找不到销售合同");
                if (!UserInfo.CurUser.HasRight("业务管理-销售记录录入")) return Redirect("~/content/AccessDeny.htm");
                return View(p);
            }
        }