Exemplo n.º 1
0
        public JsonResult SaveOuterBoxes(FormCollection fc)
        {
            OuterBoxes box = new OuterBoxes();

            MyUtils.SetFieldValueToModel(fc, box);

            if (!box.account.Equals(currentAccount))
            {
                return(Json(new SRM(false, "当前登录公司与此外箱所在公司不一致,请刷新页面后再制作箱子")));
            }

            if (box.package_date == DateTime.MinValue)
            {
                return(Json(new SRM(false, "请填写正确的包装日期")));
            }
            if (box.produce_date == DateTime.MinValue)
            {
                return(Json(new SRM(false, "请填写正确的生产日期")));
            }
            if (box.expire_date == DateTime.MinValue)
            {
                return(Json(new SRM(false, "请填写正确的有效期")));
            }

            try {
                List <OuterBoxPOs> poList = JsonConvert.DeserializeObject <List <OuterBoxPOs> >(fc.Get("poRows"));

                //关联小标签信息
                List <int> innerBoxIds = new List <int>();
                if (!string.IsNullOrEmpty(fc.Get("innerBoxIds")))
                {
                    innerBoxIds = fc.Get("innerBoxIds").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(i => Int32.Parse(i)).ToList();
                }
                else
                {
                    if (new BoxSv().GetNoRelatedInnerBox(currentUser.userName, box.item_number, box.trade_type_name, currentAccount, canCheckAll).Count() > 0)
                    {
                        return(Json(new SRM(false, string.Format("检测到型号为【{0}】的物料存在未关联的小标签,必须关联后才能保存外箱。关联路径:标签管理--->新增外箱", box.item_model))));
                    }
                }
                string boxNumber = new BoxSv().SaveOuterBox(box, poList, innerBoxIds);
                WLog("新增外箱", "保存外箱,箱号:" + boxNumber);
            }
            catch (Exception ex) {
                return(Json(new SRM(ex)));
            }

            return(Json(new SRM()));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 拆分外箱
        /// </summary>
        /// <param name="outerBoxId">外箱id</param>
        /// <param name="splitNum">拆分出来的件数</param>
        public string[] SplitOuterbox(int outerBoxId, int splitNum)
        {
            var b1 = db.OuterBoxes.Where(o => o.outer_box_id == outerBoxId).FirstOrDefault();

            if (b1 == null)
            {
                throw new Exception("此外箱不存在");
            }
            if (b1.pack_num <= splitNum)
            {
                throw new Exception("拆分件数必须少于外箱件数");
            }

            string boxNumberBefore = b1.box_number;

            //判断关联的内箱能否按照外箱的拆分按比例分割,因为内箱件数必须是外箱整数倍,所以不必再验证
            //foreach (var ib in db.InneBoxes.Where(i => i.outer_box_id == outerBoxId).ToList()) {
            //    if (ib.pack_num * 1.0m * splitNum / b1.pack_num - ib.pack_num * splitNum / b1.pack_num != 0) {
            //        throw new Exception("内箱箱号[" + ib.box_number + "]不能根据拆分件数按比例平均分割,拆分失败");
            //    }
            //}


            //通过序列化和反序列化的方式拷贝外箱对象
            OuterBoxes b2 = JsonConvert.DeserializeObject <OuterBoxes>(JsonConvert.SerializeObject(b1));

            string longBoxNum = b1.box_number_long;
            int    packNum    = (int)b1.pack_num;

            string[] boxNumArr = longBoxNum.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            var num1 = boxNumArr.Take(boxNumArr.Length - splitNum);
            var num2 = boxNumArr.Skip(boxNumArr.Length - splitNum);

            //更新原始外箱
            if (num1.Count() == 1)
            {
                b1.box_number = num1.First();
            }
            else
            {
                b1.box_number = num1.First() + "~" + num1.Last();
            }
            b1.box_number_long = string.Join(",", num1);
            b1.pack_num        = packNum - splitNum;

            //更新第二个外箱
            b2.outer_box_id = 0;
            if (num2.Count() == 1)
            {
                b2.box_number = num2.First();
            }
            else
            {
                b2.box_number = num2.First() + "~" + num2.Last();
            }
            b2.box_number_long = string.Join(",", num2);
            b2.pack_num        = splitNum;
            db.OuterBoxes.InsertOnSubmit(b2);

            db.SubmitChanges();

            try {
                //第二个外箱关联的PO信息也从第一个外箱拷贝
                var pos = db.OuterBoxPOs.Where(o => o.out_box_id == outerBoxId).ToList();
                if (pos.Count() > 0)
                {
                    var posList = JsonConvert.DeserializeObject <List <OuterBoxPOs> >(JsonConvert.SerializeObject(pos));
                    posList.ForEach(p => { p.out_box_id = b2.outer_box_id; p.out_box_po_id = 0; });
                    db.OuterBoxPOs.InsertAllOnSubmit(posList);
                }

                //第二个外箱关联的内箱也要跟着拆分
                var ins = db.InneBoxes.Where(i => i.outer_box_id == outerBoxId).ToList();
                if (ins.Count() > 0)
                {
                    foreach (var i in ins)
                    {
                        var i1 = i;
                        var i2 = JsonConvert.DeserializeObject <InneBoxes>(JsonConvert.SerializeObject(i));

                        int      iPackNum = (int)i.pack_num;
                        string   iLongNum = i.box_number_long;
                        string[] iNumArr  = iLongNum.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                        var iNum1 = iNumArr.Take(iPackNum * (packNum - splitNum) / packNum);
                        var iNum2 = iNumArr.Skip(iPackNum * (packNum - splitNum) / packNum);

                        i1.pack_num = iNum1.Count();
                        if (iNum1.Count() == 1)
                        {
                            i.box_number = iNum1.First();
                        }
                        else
                        {
                            i1.box_number = iNum1.First() + "~" + iNum1.Last();
                        }
                        i1.box_number_long = string.Join(",", iNum1);

                        i2.inner_box_id = 0;
                        i2.outer_box_id = b2.outer_box_id;
                        i2.pack_num     = iNum2.Count();
                        if (iNum2.Count() == 1)
                        {
                            i2.box_number = iNum2.First();
                        }
                        else
                        {
                            i2.box_number = iNum2.First() + "~" + iNum2.Last();
                        }
                        i2.box_number_long = string.Join(",", iNum2);
                        db.InneBoxes.InsertOnSubmit(i2);
                    }
                }
                db.SubmitChanges();

                //更新箱子明细
                UpdateBoxDetaiId(outerBoxId, b2.outer_box_id);

                return(new string[] { boxNumberBefore, b1.box_number, b2.box_number });
            }
            catch (Exception ex) {
                //保存失败,回滚操作
                db.OuterBoxes.DeleteOnSubmit(b2);
                b1.box_number      = boxNumArr[0] + "~" + boxNumArr[boxNumArr.Length - 1];
                b1.box_number_long = longBoxNum;
                b1.pack_num        = packNum;
                db.SubmitChanges();
                throw new Exception("保存拆分外箱关联的PO信息或内箱信息失败,原因:" + ex.Message);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 保存外箱
        /// </summary>
        /// <param name="box">外箱信息</param>
        /// <param name="poList">外箱关联的PO信息</param>
        /// <returns>箱号</returns>
        public string SaveOuterBox(OuterBoxes box, List <OuterBoxPOs> poList, List <int> innerBoxIds)
        {
            //验证可做外箱数量
            decimal everyBoxQty = 0;

            foreach (var po in poList)
            {
                //所有未关联的数量加上此张申请已关联的数量+本次新增的数量不能大于可申请数量
                var notRelatedQty = (from op in db.OuterBoxPOs
                                     join o in db.OuterBoxes on op.out_box_id equals o.outer_box_id
                                     where op.po_number == po.po_number && op.po_entry_id == po.po_entry_id &&
                                     (o.bill_id == null || o.bill_id == box.bill_id)
                                     select op.send_num * o.pack_num).Sum();
                var sendNum = po.send_num * box.pack_num; //本次制作数量
                if (notRelatedQty + sendNum > po.can_send_qty)
                {
                    throw new Exception(string.Format("订单号[{0}]分录号[{1}]的可制作标签数量不足:可申请数量[{4}],已制作标签数量[{2}],本次制作数量[{3}]",
                                                      po.po_number, po.po_entry_id, notRelatedQty, sendNum, po.can_send_qty));
                }
                everyBoxQty += (decimal)po.send_num;
            }

            //如果有关联小标签,验证小标签是否已被关联,因为偶有出现小标签被外箱重复关联的情况 2019-11-19
            if (innerBoxIds.Count() > 0)
            {
                var relatedInnerBoxes = db.InneBoxes.Where(i => innerBoxIds.Contains(i.inner_box_id) && i.outer_box_id != null).ToList();
                if (relatedInnerBoxes.Count() > 0)
                {
                    throw new Exception("已选择的小标签[" + relatedInnerBoxes.First().box_number + "]已被其它外箱关联,不能重复关联,请重新加载最新小标签后再选择");
                }
            }

            //数量没问题之后,就保存
            var boxNumber = new ItemSv().GetBoxNumber("O", (int)box.pack_num);

            box.box_number      = boxNumber[0];
            box.box_number_long = boxNumber[1];
            box.create_date     = DateTime.Now;
            box.every_qty       = everyBoxQty;
            box.bill_id         = box.bill_id == 0 ? null : box.bill_id;

            db.OuterBoxes.InsertOnSubmit(box);
            db.OuterBoxPOs.InsertAllOnSubmit(poList);
            db.SubmitChanges();

            //因为取消了外键,保存后还需要再保存两者的关系
            foreach (var po in poList)
            {
                po.out_box_id = box.outer_box_id;
            }
            //保存外箱明细 2018-12-04
            foreach (var num in box.box_number_long.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                db.OuterBoxesDetail.InsertOnSubmit(new OuterBoxesDetail()
                {
                    outer_box_id = box.outer_box_id,
                    box_number   = num
                });
            }

            //关联小标签信息 2018-12-12
            if (innerBoxIds.Count() > 0)
            {
                var innerBoxes = db.InneBoxes.Where(i => innerBoxIds.Contains(i.inner_box_id)).ToList();
                foreach (var ib in innerBoxes)
                {
                    ib.outer_box_id = box.outer_box_id; //关联到外箱
                    //添加内箱明细
                    var innerBoxArr = ib.box_number_long.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    for (var j = 0; j < innerBoxArr.Count(); j++)
                    {
                        db.InnerBoxesDetail.InsertOnSubmit(new InnerBoxesDetail()
                        {
                            inner_box_id     = ib.inner_box_id,
                            outer_box_id     = box.outer_box_id,
                            inner_box_number = innerBoxArr[j],
                            outer_box_number = box.box_number
                        });
                    }
                }
            }

            db.SubmitChanges();

            return(box.box_number);
        }