Пример #1
0
        public IHttpActionResult StockTransfer()
        {
            HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];
            HttpRequestBase request = context.Request;

            JavaScriptSerializer js         = new JavaScriptSerializer();
            string frmWarId                 = request.Params["frmWarId"];
            string toWarId                  = request.Params["toWarId"];
            string purchaseDate             = request.Params["purchaseDate"];
            List <Stockrecords>    listStoc = js.Deserialize <List <Stockrecords> >(request.Params["tsfDtl"]);
            List <twhstockrecords> listTwh  = new List <twhstockrecords>();

            using (var tran = db.Database.BeginTransaction())
            {
                try
                {
                    //调整库存前先锁住两个库的对应所有产品的行,如果产品在目标库不存在,先新建记录再锁住
                    string sqlQuery = "select * from twhinventory where whid ='{0}' and areaid ='0' and posiid='0' and pdtid='{1}' for update";
                    for (int i = 0; i < listStoc.Count; i++)
                    {
                        string sql1 = string.Format(sqlQuery, frmWarId, listStoc[i].pdtID);
                        string sql2 = string.Format(sqlQuery, toWarId, listStoc[i].pdtID);

                        var inv1 = db.twhinventory.SqlQuery(sql1).FirstOrDefault();
                        if (inv1 == null || inv1.PdtID == null || inv1.PdtID == "")
                        {
                            twhinventory newInv1 = new twhinventory();
                            newInv1.WHID   = frmWarId;
                            newInv1.PdtID  = listStoc[i].pdtID;
                            newInv1.AreaID = "0";
                            newInv1.PosiID = "0";
                            newInv1.InvNum = 0;
                            db.twhinventory.Add(newInv1);
                            db.SaveChanges();
                            inv1 = db.twhinventory.SqlQuery(sql1).FirstOrDefault();
                        }
                        //实时库存不够,回滚,返回
                        if (inv1.InvNum < listStoc[i].lftNum)
                        {
                            tran.Rollback();
                            return(new PageResult("库存不足", Request));
                        }

                        //来源库减库存
                        inv1.InvNum = inv1.InvNum - listStoc[i].lftNum;

                        var inv2 = db.twhinventory.SqlQuery(sql2).FirstOrDefault();
                        if (inv2 == null || inv2.PdtID == null || inv2.PdtID == "")
                        {
                            twhinventory newInv2 = new twhinventory();
                            newInv2.WHID   = toWarId;
                            newInv2.PdtID  = listStoc[i].pdtID;
                            newInv2.AreaID = "0";
                            newInv2.PosiID = "0";
                            newInv2.InvNum = 0;
                            db.twhinventory.Add(newInv2);
                            db.SaveChanges();

                            inv2 = db.twhinventory.SqlQuery(sql2).FirstOrDefault();
                        }

                        //目标库加库存
                        inv2.InvNum = inv2.InvNum + listStoc[i].lftNum;

                        //新增出入库记录,移库新增两条
                        //新增出库记录
                        float           lftNum = listStoc[i].lftNum;
                        twhstockrecords t      = new twhstockrecords();
                        t.WHID        = frmWarId;
                        t.AreaID      = "0";
                        t.PosiID      = "0";
                        t.PdtID       = listStoc[i].pdtID;
                        t.Num         = lftNum;
                        t.RefWHID     = toWarId;
                        t.RefAreaID   = "0";
                        t.RefPosiID   = "0";
                        t.OpeType     = "TO";
                        t.SaleOrderNO = "";
                        t.No          = "";
                        var dateNow = DateTime.Now;
                        t.CreateDate = dateNow;
                        t.UpdateDate = dateNow;
                        if (!string.IsNullOrEmpty(purchaseDate))
                        {
                            DateTime date = Convert.ToDateTime(purchaseDate);
                            var      tmp0 = date.ToString("yyyy-MM-dd");
                            var      tmp1 = dateNow.ToLongTimeString().ToString();
                            purchaseDate = tmp0 + " " + tmp1;
                            date         = Convert.ToDateTime(purchaseDate);
                            t.UpdateDate = date;
                        }
                        listTwh.Add(t);

                        //新增入库记录
                        twhstockrecords t2 = new twhstockrecords();
                        t2.WHID        = toWarId;
                        t2.AreaID      = "0";
                        t2.PosiID      = "0";
                        t2.PdtID       = listStoc[i].pdtID;
                        t2.Num         = lftNum;
                        t2.RefWHID     = frmWarId;
                        t2.RefAreaID   = "0";
                        t2.RefPosiID   = "0";
                        t2.OpeType     = "TI";
                        t2.SaleOrderNO = "";
                        t2.No          = "";
                        t2.CreateDate  = dateNow;
                        t2.UpdateDate  = dateNow;
                        if (!string.IsNullOrEmpty(purchaseDate))
                        {
                            DateTime date = Convert.ToDateTime(purchaseDate);
                            var      tmp0 = date.ToString("yyyy-MM-dd");
                            var      tmp1 = dateNow.ToLongTimeString().ToString();
                            purchaseDate  = tmp0 + " " + tmp1;
                            date          = Convert.ToDateTime(purchaseDate);
                            t2.UpdateDate = date;
                        }
                        listTwh.Add(t2);
                    }

                    db.twhstockrecords.AddRange(listTwh);

                    db.SaveChanges();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    return(new PageResult(ex.ToString(), Request));
                }
            }
            return(Content <string>(HttpStatusCode.OK, "OK"));
        }
Пример #2
0
        public IHttpActionResult PostInWar()
        {
            HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];
            HttpRequestBase request = context.Request;

            JavaScriptSerializer js          = new JavaScriptSerializer();
            string purOrderNO                = request.Params["purOrderNO"];
            string warId                     = request.Params["warId"];
            string purchaseDate              = request.Params["purchaseDate"];
            List <Stockrecords>     listStoc = js.Deserialize <List <Stockrecords> >(request.Params["purorderdtl"]);
            List <WarehouseReceipt> listWr   = js.Deserialize <List <WarehouseReceipt> >(request.Params["purorderdtl"]);
            List <twhstockrecords>  listTwh  = new List <twhstockrecords>();
            string no = "";

            using (var tran = db.Database.BeginTransaction())
            {
                try
                {
                    int fNum = 0;
                    no = purOrderNO + DateTime.Now.ToString("HHmmss");
                    for (int i = 0; i < listStoc.Count; i++)
                    {
                        int    seqNo  = listStoc[i].seqNo;
                        string pdtId  = listStoc[i].pdtID;
                        float  lftNum = listStoc[i].lftNum;
                        string remark = listStoc[i].remark;

                        twhstockrecords t = new twhstockrecords();
                        t.WHID       = warId;
                        t.AreaID     = i.ToString();
                        t.PosiID     = i.ToString();
                        t.PdtID      = pdtId;
                        t.Num        = lftNum;
                        t.RefWHID    = i.ToString();
                        t.RefAreaID  = i.ToString();
                        t.RefPosiID  = i.ToString();
                        t.OpeType    = "I";
                        t.PurOrderNO = purOrderNO;
                        t.RefSeqNo   = seqNo;
                        t.No         = no;
                        var dateNow = DateTime.Now;
                        t.CreateDate = dateNow;
                        t.UpdateDate = dateNow;
                        if (!string.IsNullOrEmpty(purchaseDate))
                        {
                            DateTime date = Convert.ToDateTime(purchaseDate);
                            var      tmp0 = date.ToString("yyyy-MM-dd");
                            var      tmp1 = dateNow.ToLongTimeString().ToString();
                            purchaseDate = tmp0 + " " + tmp1;
                            date         = Convert.ToDateTime(purchaseDate);
                            t.UpdateDate = date;
                        }
                        t.Remark = remark;
                        listTwh.Add(t);

                        var purorderdtl = db.tpurorderdtl
                                          .Where(p => p.PurOrderNO == purOrderNO && p.PdtID == pdtId && p.SeqNo == seqNo).FirstOrDefault();

                        if (purorderdtl.LftNum > 0)
                        {
                            float num    = (float)purorderdtl.LftNum - lftNum;
                            float delNum = (float)purorderdtl.OrderNum - num;
                            db.tpurorderdtl.Where(p => p.PurOrderNO == purOrderNO && p.PdtID == pdtId && p.SeqNo == seqNo)
                            .Update(p => new tpurorderdtl {
                                LftNum = num, DelNum = delNum, UpdateDate = DateTime.Now
                            });

                            if (num == 0)
                            {
                                purorderdtl.State = "F";
                            }
                            else
                            {
                                fNum++;
                            }
                        }
                    }

                    var repeat = listStoc.GroupBy(s => s.pdtID).Select(s => new
                    {
                        pdtID  = s.Key,
                        lftNum = s.Sum(a => a.lftNum)
                    });

                    foreach (var r in repeat)
                    {
                        if (twhinventoryExists(warId, r.pdtID))
                        {
                            //var repeatUpdate = listStoc.GroupBy(s => s.pdtID).Select(s => new
                            //{
                            //    pdtID = s.Key,
                            //    lftNum = s.Sum(a => a.lftNum)
                            //}).Where(s => s.pdtID == r.pdtID).FirstOrDefault();

                            //float lftNum = repeatUpdate.lftNum;

                            var   inventory = db.twhinventory.Where(inv => inv.WHID == warId && inv.PdtID == r.pdtID).FirstOrDefault();
                            float total     = (float)inventory.InvNum + r.lftNum;
                            db.twhinventory.Where(inv => inv.WHID == warId && inv.PdtID == r.pdtID)
                            .Update(inv => new twhinventory()
                            {
                                InvNum = total, UpdateDate = DateTime.Now
                            });
                        }
                        else
                        {
                            //var query = listStoc.GroupBy(x => x.pdtID).Where(g => g.Count() > 1 && g.Key == pdtId).Select(y => y.Key).ToList();

                            //if (query.Count > 0) lftNum = 0;

                            //var repeatAdd = listStoc.GroupBy(s => s.pdtID).Select(s => new
                            //{
                            //    pdtID = s.Key,
                            //    lftNum = s.Sum(a => a.lftNum)
                            //}).Where(s => s.pdtID == r.pdtID).FirstOrDefault();

                            //float lftNum = repeatAdd.lftNum;

                            twhinventory inv = new twhinventory();
                            inv.WHID       = warId;
                            inv.AreaID     = "0";
                            inv.PosiID     = "0";
                            inv.PdtID      = r.pdtID;
                            inv.InvNum     = r.lftNum;
                            inv.UpdateDate = DateTime.Now;
                            db.twhinventory.Add(inv);
                        }
                    }

                    //部分入库时fNum也会是0,这种情况下用fNum判断会有问题
                    //if (fNum == 0)
                    //{
                    //    db.tpurorder.Where(p => p.PurOrderNO == purOrderNO)
                    //        .Update(p => new tpurorder() { State = "F", UpdateDate = DateTime.Now });
                    //}

                    //全部入库完成后才修改单头状态为F
                    db.SaveChanges();
                    var isNotFinish = db.tpurorderdtl.AsNoTracking().Any(w => w.PurOrderNO == purOrderNO && (w.State == "N" || w.State == null));
                    if (!isNotFinish)
                    {
                        db.tpurorder.Where(s => s.PurOrderNO == purOrderNO)
                        .Update(p => new tpurorder()
                        {
                            State = "F", UpdateDate = DateTime.Now
                        });
                    }

                    db.twhstockrecords.AddRange(listTwh);


                    db.SaveChanges();
                    tran.Commit();

                    excelHelper.SaveInWarInfo(purOrderNO, warId, purchaseDate, listWr, no);
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    return(new PageResult(ex.ToString(), Request));
                }
            }
            return(Content <string>(HttpStatusCode.OK, "OK"));
        }