Example #1
0
        public ActionResult _Instock(int id, int? _purchaseid, FormCollection collection)
        {
            FoodMaterialInstock instock = null;
            FoodMaterialPurchase purchase = new FoodMaterialPurchase();
            FoodMaterialRequest req = new FoodMaterialRequest();
            FoodMaterialType food = new FoodMaterialType();
            FoodMaterialSpec spec=new FoodMaterialSpec();
            int purchaseid = 0;

            if (id == 0)
            {
                if (_purchaseid == null)
                {
                    instock = new FoodMaterialInstock
                    {
                        PurchaseId = purchaseid
                        ,
                        QualityLevel = (int)QualityLevel.合格
                        ,
                        PersonId = UserInfo.CurUser.Id
                        ,
                        Code = "自动产生"
                        ,ProductDate = DateTime.Today
                    };
                }
                else
                {
                    purchaseid = (int)_purchaseid;
                    purchase = db.FoodMaterialPurchases.Find(purchaseid);
                    if (purchase == null)
                    {
                        return View("ShowError", "", "找不到采购单");
                    }
                    instock = new FoodMaterialInstock
                    {
                        TypeId = purchase.TypeId
                        ,
                        SpecId = purchase.SpecId
                        ,
                        SupplierId = purchase.SupplierId
                        ,
                        PurchaseId = purchaseid
                        ,
                        QualityLevel = (int)QualityLevel.合格
                        ,
                        PersonId = UserInfo.CurUser.Id
                        ,
                        Code = "自动产生"
                        ,
                        InstockDate = DateTime.Today
                        ,
                        InstockUnit = purchase.InstockUnit
                        ,
                        InstockUnitNum = purchase.InstockUnitNum
                        ,ProductDate = DateTime.Today
                    };
                }
            }
            else
            {
                instock = db.FoodMaterialInstocks.Find(id);
                if (instock == null)
                {
                    return View("ShowError", "", "找不到入库单");
                }
                purchase = db.FoodMaterialPurchases.Find(instock.PurchaseId);
            }

            if (collection != null)//POST
            {
                TryUpdateModel(instock, "", null, new string[] { "TypeId","SpecId","SupplierId", "State", "InstockUnit", "InstockUnitNum", "Payed", "PersonId", "PriceNum", "QualityLevel", "QualityRemark" }, collection);
                if (ModelState.IsValid)
                {
                    if (instock.Id == 0)
                    {
                        instock.Code = SysCode.GetCode_ByDate(db, FoodMaterialInstock.LogClass, DateTime.Today);
                        db.FoodMaterialInstocks.Add(instock);
                    }
                    instock.TypeId = purchase.TypeId;
                    instock.SpecId = purchase.SpecId;
                    instock.SupplierId = purchase.SupplierId;
                    instock.InstockUnit = purchase.InstockUnit;
                    instock.InstockUnitNum = purchase.InstockUnitNum;
                    decimal instocknum = (from o in db.FoodMaterialInstocks where o.PurchaseId == instock.PurchaseId select o.PriceNum).ToList().Sum();
                    if (purchase.Num <= instocknum)
                    {
                        purchase.State = (int)FoodMaterialPurchaseState.完成入库;
                    }
                    else
                    {
                        purchase.State = (int)FoodMaterialPurchaseState.未完成入库;
                    }
                    instock.PersonId = UserInfo.CurUser.Id;
                    db.SaveChanges();
                    BLL.Utilities.AddLog(instock.Id, FoodMaterialInstock.LogClass, "添加或修改", "");
                    return Redirect("../InstockView/" + instock.Id);
                }
            }
            if (purchase.Id != 0)
            {
                req = db.FoodMaterialRequests.Find(purchase.RequestId);
                if (req == null)
                {
                    return View("ShowError", "", "找不到申购单");
                }
                //food = db.FoodMaterialType.Find(req.TypeId);
                //if (food == null)
                //{
                //    return View("ShowError", "", "找不到原料");
                //}
            }
            if(instock.TypeId>0)
            {
                food = db.FoodMaterialType.Find(instock.TypeId);
            }
            if(instock.SpecId>0)
            {
                spec = db.FoodMaterialSpecs.Find(instock.SpecId);
            }
            ViewBag.Purchase = purchase;
            ViewBag.Request = req;
            ViewBag.Food = food;
            ViewBag.Spec = spec;
            if(food.StorePlace!=null&&instock.StorePlace==null)
            {
                instock.StorePlace = food.StorePlace;
            }
            if (instock.Id != 0)
            {
                ViewBag.ReturnList = (from o in db.FoodMaterialInstockReturns where o.InstockId == instock.Id select o).ToList();
            }
            return View(instock);
        }
        private ActionResult _PurchaseEdit(int id, int? requestId, FormCollection collection)
        {
            FoodMaterialPurchase p = null;
            if (id == 0 && requestId != null)
            {
                p = new FoodMaterialPurchase();
                p.RequestId = (int)requestId;
                p.Code = "自动产生";// DateTime.Today.ToString("yyyyMMdd");
            }
            else if (id != 0)
            {
                p = (from o in db.FoodMaterialPurchases where o.Id == id select o).FirstOrDefault();
                if (p == null)
                {
                    return View("ShowError", "", "找不到采购单");
                }
            }
            else
            {
                return View("ShowError", "", "错误的参数");
            }
            if (p.State != (int)FoodMaterialPurchaseState.未到货)
            {
                ViewBag.CanChangeSupplier = false;
            }
            if(collection!=null)//POST部分
            {
                if (p.Id == 0)
                {
                    p.RequestId = (int)requestId;
                    p.PurchaseDate = DateTime.Today;
                    p.InstockUnit = "-";
                    db.FoodMaterialPurchases.Add(p);
                }else if (p.State != (int)FoodMaterialPurchaseState.未到货)
                {
                    if (p.SupplierId.ToString() != collection["SupplierId"])
                    {
                        return View("ShowError", "", "不可以变更供应商");
                    }
                }
                TryUpdateModel(p, "", new string[] { }, new string[] { "TypeId", "RequestId", "PurchaseDate", "InstockUnit", "InstockUnitNum" }, collection);
                if (p.Num == 0)
                {
                    ModelState.AddModelError("Num", "数量不能为0");
                }
                if (ModelState.IsValid)
                {
                    if(p.Id==0)
                    {
                        p.Code=SysCode.GetCode_ByDate(db,FoodMaterialPurchase.LogClass,DateTime.Today);
                    }
                    FoodMaterialSpec spec = db.FoodMaterialSpecs.Find(p.SpecId);
                    p.TypeId = spec.TypeId;
                    p.InstockUnit = spec.InstockUnit;
                    p.InstockUnitNum = spec.InstockUnitNum;
                    db.SaveChanges();
                    return Redirect("../PurchaseView/" + p.Id);
                }
            }
            FoodMaterialRequest req = (from o in db.FoodMaterialRequests where o.Id == p.RequestId select o).FirstOrDefault();
            if (req == null) return View("ShowError", "", "找不到申购单");
            if (p.Id == 0)
            {
                p.OrderDate = req.OrderDate;
            }
            ViewBag.Request = req;
            FoodMaterialType food = db.FoodMaterialType.Find(req.TypeId);
            ViewBag.Food = food;
            var supplierlist = (from o in db.FoodMaterialPrice
                         join supplier in db.Suppliers on o.SupplierId equals supplier.Id
                         join s in db.FoodMaterialSpecs on o.SpecId equals s.Id
                         join f in db.FoodMaterialType on s.TypeId equals f.Id
                         where f.Id == req.TypeId && o.State == 0
                         select new { id = supplier.Id, name = supplier.Name}
                                                 ).Distinct().ToList();
            List<SelectListItem> supplierList = supplierlist.Select(o => new SelectListItem { Value = o.id.ToString(), Text = o.name }).ToList();
            ViewBag.Suppliers = supplierList;
            var specList = (from o in db.FoodMaterialPrice
                                join s in db.FoodMaterialSpecs on o.SpecId equals s.Id
                                join f in db.FoodMaterialType on s.TypeId equals f.Id
                                where f.Id == req.TypeId && o.State == 0
                                select new { supplierId =o.SupplierId
                                    ,brand=s.Brand
                                    ,productArea=s.ProductArea
                                    ,instock=s.InStock
                                    ,instockUnit=s.InstockUnit
                                    ,instockUnitNum=s.InstockUnitNum
                                    ,priceUnit=s.PriceUnit
                                    ,priceUnitNum=s.PriceUnitNum
                                    ,id=s.Id
                                    ,price=o.Price
                                    ,priceDate=o.PriceDate
                                    ,orderPeriod=o.OrderPeriod
                                    ,pricePeriod=o.PricePeriod

                                }).Distinct().ToList();
            ViewBag.Prices = specList;
            ViewBag.PurchasedNum = (from o in db.FoodMaterialPurchases where o.State != (int)FoodMaterialPurchaseState.作废 && o.RequestId == req.Id && o.Id != p.Id select o.Num*o.InstockUnitNum).ToArray().Sum();
            //if (p.Id == 0)
            //{p.Num = req.Num - ViewBag.PurchasedNum;
            //}
            //else
            //{
            //    ViewBag.PurchasedNum = 0;
            //}
            return View(p);
        }