Exemple #1
0
        public ActionResult SalesContractEdit(int id, int? clientId, FormCollection collection)
        {
            if (!UserInfo.CurUser.HasRight("业务管理-销售合同录入")) return Redirect("~/content/AccessDeny.htm");
            using (TransactionScope transcope = new TransactionScope())
            {
                SalesContract s = db.SalesContracts.Find(id);
                if (s == null)
                {
                    s = new SalesContract { ClientId = (int)clientId };
                    db.SalesContracts.Add(s);
                }
                List<SalesContractItem> newList =
                        JsonConvert.DeserializeObject<List<SalesContractItem>>(collection["itemsString"]);

                if(newList==null)
                {
                    ModelState.AddModelError("", "Product in contract can not be empty");
                }

                TryUpdateModel(s, "", new string[] { }, new string[] { }, collection);
                var contract =
                    (from o in db.SalesContracts where o.Code == s.Code && o.Id != s.Id select o).AsNoTracking().
                        FirstOrDefault();
                if (contract != null)
                {
                    ModelState.AddModelError("Code", "SN existed");
                }
                if (ModelState.IsValid)
                {
                    db.SaveChanges();

                    List<SalesContractItem> oldList =
                        (from o in db.SaleContractItems where o.ContractId == id select o).ToList();
                    foreach (var item in oldList)
                    {
                        db.SaleContractItems.Remove(item);
                    }
                    foreach (var item in newList)
                    {
                        if (item.Num == decimal.Zero) continue;
                        item.ContractId = s.Id;
                        item.Id = 0;
                        db.SaleContractItems.Add(item);
                    }
                    db.SaveChanges();
                    AccessLog.AddLog(db, UserInfo.CurUser.Name, s.Id, SalesContract.LogClass, id == 0 ? "创建" : "修改", "");
                    transcope.Complete();

                    return Redirect(string.Format("../SalesContractView/{0}", s.Id));
                }
                ViewBag.ItemsString = JsonConvert.SerializeObject(newList);
                List<Product> products = (from o in db.Products select o).AsNoTracking().ToList();
                ViewBag.ProductsString = JsonConvert.SerializeObject(products);
                var preSales =
                db.Database.SqlQuery<int>(
                    string.Format(
                        "select distinct i.productid from salescontractitems i join salescontracts c on i.contractid=c.id where c.clientid={0}",
                        s.ClientId)).ToList();
                ViewBag.preSales = preSales;
                return View(s);
            }
        }
Exemple #2
0
        public ActionResult SalesContractEdit(int id, int? clientId)
        {
            if (!UserInfo.CurUser.HasRight("业务管理-销售合同录入")) return Redirect("~/content/AccessDeny.htm");
            SalesContract s = db.SalesContracts.Find(id);
            List<SalesContractItem> items = new List<SalesContractItem>();
            if (s == null && clientId == null) return View("ShowError", "", "找不到合同");
            if (s == null)
            {
                s = new SalesContract { ClientId = (int)clientId, SalesDate = DateTime.Today };
            }
            else
            {
                items = (from o in db.SaleContractItems where o.ContractId == id select o).ToList();
            }
            ViewBag.ItemsString = JsonConvert.SerializeObject(items);

            List<Product> products = (from o in db.Products select o).AsNoTracking().ToList();
            ViewBag.ProductsString = JsonConvert.SerializeObject(products);
            var preSales =
                db.Database.SqlQuery<int>(
                    string.Format(
                        "select distinct i.productid from salescontractitems i join salescontracts c on i.contractid=c.id where c.clientid={0}",
                        s.ClientId)).ToList();
            ViewBag.preSales = preSales;
            return View(s);
        }