chengsc
2024-11-03 5ce969ed34e91c931cce38e2feb5d2c208633a96
Wms/WMS.BLL/BllAsnServer/BllBoxInfoServer.cs
@@ -15,6 +15,7 @@
using WMS.Entity.Context;
using WMS.Entity.SysEntity;
using WMS.IBLL.IBllAsnServer;
using ZXing;
namespace WMS.BLL.BllAsnServer
{
@@ -264,7 +265,7 @@
                  .AndIF(!string.IsNullOrWhiteSpace(model.ProductionTime), it => it.ProductionTime >= Convert.ToDateTime(model.ProductionTime))
                  .AndIF(!string.IsNullOrWhiteSpace(model.InspectMark), it => it.InspectMark == model.InspectMark.Trim())
                  .AndIF(!string.IsNullOrWhiteSpace(model.BitBoxMark), it => it.BitBoxMark == model.BitBoxMark.Trim())
                  .AndIF(!string.IsNullOrWhiteSpace(model.Origin), it => it.Origin == model.Origin.Trim())
                  .AndIF(!string.IsNullOrWhiteSpace(model.Origin), it => model.Origin.Trim().Contains(it.Origin))
                  .And(m => m.SkuNo != "100099" && m.IsDel == "0" && m.Status == "0")
                  .ToExpression();//注意 这一句 不能少
                var total = 0;
@@ -296,6 +297,7 @@
                        CreateTime = a.CreateTime,
                        UpdateTime = a.UpdateTime
                    })
                    .OrderByDescending(a=>a.BoxNo)
                    .OrderByDescending(a => a.CreateTime)
                    .ToOffsetPage(model.Page, model.Limit, ref total);
                count = total;
@@ -463,7 +465,7 @@
                        CreateTime = a.CreateTime,
                        UpdateTime = a.UpdateTime
                    }).OrderByDescending(a => a.CreateTime).OrderBy(a => a.BoxNo).ToOffsetPage(model.Page, model.Limit, ref total);
                    }).OrderBy(a => a.BoxNo).OrderByDescending(a => a.CreateTime).ToOffsetPage(model.Page, model.Limit, ref total);
                count = total;
                return data;
            }
@@ -529,17 +531,50 @@
        }
        //获取标签信息(生成标签)
        public List<BllBoxInfo> AddLabelBox(int imId, string isReset, decimal arriveQty, string productionTime, string expirationTime, string storeTime, int userId)
        public List<BllBoxInfo> AddLabelBox(int imId, string isReset, decimal arriveQty, string productionTime, string expirationTime, string storeTime,string supplierLot, int userId)
        {
            try
            {
            {
                #region 单据、物料、包装、标签信息获取
                if (string.IsNullOrWhiteSpace(productionTime))
                {
                    throw new Exception("生产日期不能为空");
                }
                if (!string.IsNullOrWhiteSpace(expirationTime) && !string.IsNullOrWhiteSpace(storeTime))
                {
                    throw new Exception("有效期和储存期不能同时填写");
                }
                if (isReset != "0")
                {
                    throw new Exception("不可重置批次");
                }
                DateTime proTime;
                DateTime expTime;
                DateTime stoTime;
                var bl1= DateTime.TryParse(productionTime, out proTime);
                var bl2= DateTime.TryParse(expirationTime, out expTime);
                var bl3= DateTime.TryParse(storeTime, out stoTime);
                if (!bl1)
                {
                    throw new Exception("生产日期转换失败");
                }
                if (!bl2 && !string.IsNullOrWhiteSpace(expirationTime))
                {
                    throw new Exception("有效期转换失败");
                }
                if (!bl3 && !string.IsNullOrWhiteSpace(storeTime))
                {
                    throw new Exception("储存期转换失败");
                }
                var asnList = Db.Queryable<BllArrivalNoticeDetail>().First(m => m.Id == imId && m.IsDel == "0");
                if (asnList == null)
                {
                    throw new Exception("未查询到单据明细信息");
                }
                if (asnList.LotNo.Length != 10)
                {
                    throw new Exception("单据进厂编号位数长度错误");
                }
                var asn = Db.Queryable<BllArrivalNotice>().First(m => m.ASNNo == asnList.ASNNo && m.IsDel == "0");
                if (asn == null)
@@ -576,7 +611,7 @@
                    packStr += "-" + pack.L5Num + "/" + pack.L5Name;
                }
                //标签表
                var label = Db.Queryable<BllBoxInfo>().Where(m => m.IsDel == "0" && m.ASNDetailNo == imId).ToList();
                var label = Db.Queryable<BllBoxInfo>().Where(m => m.IsDel == "0" && m.Origin == "WMS生成").ToList();
                #endregion
@@ -605,7 +640,7 @@
                    zNum = Convert.ToInt32(pack.L1Num);
                }
                if (packLevel <= 1)//包装等级小于1,按照1级标签打印
                if (packLevel < 1)//包装等级小于1,按照1级标签打印
                {
                    throw new Exception("包装信息有误,小于一级");
                }
@@ -614,30 +649,35 @@
                var modelList = new List<BllBoxInfo>();
                var addLotNo = "";
                if (arriveQty <= 0)
                {
                    throw new Exception("到货数量不能小于等于0!");
                }
                Db.BeginTran();
                if (packLevel == 1)
                {
                    #region 一级包装
                    // 根据用户输入的箱数量计算需要的条码数
                    var labQty = label.Sum(m => m.Qty);
                    var qty = asnList.Qty - labQty;//当前单据剩余需要打印的物料数量(不是标签数量)
                    if (qty <= 0)
                    {
                        throw new Exception("当前单据物料标签已打印完毕,如有需要请补打");
                    }
                    //var labQty = label.Sum(m => m.Qty);
                    //var qty = asnList.Qty - labQty;//当前单据剩余需要打印的物料数量(不是标签数量)
                    //if (qty <= 0)
                    //{
                    //    throw new Exception("当前单据物料标签已打印完毕,如有需要请补打");
                    //}
                    #region 到货数量
                    if (arriveQty - qty > 0)
                    {
                        throw new Exception("到货数量大于未打标签数量,请重新输入到货数量并核实!");
                    }
                    else
                    {
                        qty = arriveQty;
                    }
                    //if (arriveQty - qty > 0)
                    //{
                    //    throw new Exception("到货数量大于未打标签数量,请重新输入到货数量并核实!");
                    //}
                    //else
                    //{
                    //    qty = arriveQty;
                    //}
                    var qty = arriveQty;
                    #endregion
                    int labelNum = 1; //生产条码数量
@@ -656,23 +696,39 @@
                    string maxBoxCode = ""; //箱码号
                    // 生成自编批号
                    string toDayTime = DateTime.Now.ToString("yyyyMMdd");
                    var maxLotNo = label.Max(a => a.LotNo);
                    var maxBoxNo = label.Max(a => a.BoxNo);
                    if (isReset == "0")
                    {
                        maxLotNoStr = maxLotNo;
                        maxBoxCode = maxBoxNo;
                        if (string.IsNullOrWhiteSpace(maxLotNoStr))
                        //判断单据是否是余料退回单
                        if (asn.Type == "4")
                        {
                            maxLotNoStr = toDayTime.Substring(2, 6) + "0001";
                            maxLotNoStr = asnList.LotNo;
                            maxBoxCode = label.Where(m=>m.LotNo == maxLotNoStr).Max(a => a.BoxNo);
                            if (string.IsNullOrWhiteSpace(maxLotNoStr))
                            {
                                throw new Exception("车间余料退回单据批次不能为空");
                            }
                        }
                        else
                        {
                            //var labelCount = label.Where(m => m.ASNDetailNo == imId).ToList();
                            //if (labelCount.Count <= 0)
                            //{
                            //    throw new Exception("当前单据明细还未生成标签,请选择重置批次选项为‘是’;");
                            //}
                            //maxLotNoStr = labelCount.Max(a => a.LotNo);
                            maxLotNoStr = asnList.LotNo;
                            //if (string.IsNullOrWhiteSpace(maxLotNoStr))
                            //{
                            //    maxLotNoStr = toDayTime.Substring(2, 6) + "0001";
                            //}
                            maxBoxCode = label.Where(m => m.LotNo == maxLotNoStr).Max(a => a.BoxNo);
                        }
                    }
                    else
                    {
                        var maxCode = Db.Queryable<BllBoxInfo>().Where(m=>m.Origin == "WMS生成").Max(a => a.LotNo);                        // 获取今天最大批次号
                        var maxCode = Db.Queryable<BllBoxInfo>().Where(m=>m.Origin == "WMS生成").Max(a => a.LotNo); // 获取今天最大批次号 20241013
                        if (string.IsNullOrWhiteSpace(maxCode))
                        {
                            maxLotNoStr = toDayTime.Substring(2, 6) + "0001";
@@ -720,7 +776,7 @@
                        }
                        //获取箱码
                        if (maxBoxCode == "")
                        if (string.IsNullOrWhiteSpace(maxBoxCode))
                        {
                            var str = "B"+maxLotNoStr + "000001"; //批号+ 流水  2302010001000001
                            maxBoxCode = str;
@@ -736,7 +792,7 @@
                            ASNNo = asnList.ASNNo,
                            ASNDetailNo = asnList.Id,
                            BoxNo = maxBoxCode, 
                            Qty = int.Parse(boxQty.ToString()),
                            Qty = boxQty,
                            FullQty = bNum,
                            Status = "0",
                            SkuNo = asnList.SkuNo,
@@ -744,20 +800,25 @@
                            LotNo = maxLotNoStr,
                            Standard = asnList.Standard,
                            PackageStandard = packStr,
                            SupplierLot = asnList.SupplierLot,
                            SupplierLot = supplierLot,
                            InspectMark = "0",
                            BitBoxMark = bNum > boxQty ? "1":"0",
                            InspectStatus = "",
                            InspectStatus = "0",
                            ProductionTime = DateTime.Parse(productionTime),
                            StoreTime = DateTime.Parse(storeTime),
                            ExpirationTime = DateTime.Parse(expirationTime),
                            ProductionTime = proTime,
                            Origin = "WMS生成",
                            CreateUser = userId,
                            CreateTime = DateTime.Now,
                        };
                        if (bl2)
                        {
                            labelModel.ExpirationTime = expTime;
                        }
                        if (bl3)
                        {
                            labelModel.StoreTime = stoTime;
                        }
                        Db.Insertable(labelModel).ExecuteCommand();
                        modelList.Add(labelModel);
                    }
@@ -769,25 +830,27 @@
                    #region 二级包装
                    // 根据用户输入的箱数量计算需要的条码数
                    var labQty = label.Sum(m => m.Qty);
                    var qty2 = asnList.Qty - labQty;
                    if (qty2 <= 0)
                    {
                        throw new Exception("当前单据物料标签已打印完毕,如有需要请补打");
                    }
                    //var labQty = label.Sum(m => m.Qty);
                    //var qty2 = asnList.Qty - labQty;
                    //if (qty2 <= 0)
                    //{
                    //    throw new Exception("当前单据物料标签已打印完毕,如有需要请补打");
                    //}
                    #region 增加到货数量
                    if (arriveQty - qty2 > 0)
                    {
                        throw new Exception("到货数量大于未打标签数量,请重新输入到货数量!");
                    }
                    else
                    {
                        qty2 = arriveQty;
                    }
                    //if (arriveQty - qty2 > 0)
                    //{
                    //    throw new Exception("到货数量大于未打标签数量,请重新输入到货数量!");
                    //}
                    //else
                    //{
                    //    qty2 = arriveQty;
                    //}
                    #endregion
                    var qty2 = arriveQty;
                    int labelNum2 = 1; //生产箱条码数量
                    labelNum2 = int.Parse(Math.Ceiling(qty2 / bNum).ToString());
@@ -806,16 +869,35 @@
                    string toDayTime2 = DateTime.Now.ToString("yyyyMMdd");
                    if (isReset == "0")
                    {
                        var maxCode = label.Max(a => a.LotNo);
                        var boxCode1 = label.Max(a => a.BoxNo);//箱号
                        var boxCode2 = label.Max(a => a.BoxNo3);//支号
                        maxCodestr2 = maxCode;
                        maxboxcode2 = boxCode1;
                        maxboxcode3 = boxCode2;
                        if (string.IsNullOrWhiteSpace(maxCodestr2))
                        //判断单据是否是余料退回单
                        if (asn.Type == "4")
                        {
                            maxCodestr2 = toDayTime2.Substring(2, 6) + "0001";
                            maxCodestr2 = asnList.LotNo;
                            maxboxcode2 = label.Where(m => m.LotNo == maxCodestr2).Max(a => a.BoxNo);
                            maxboxcode3 = label.Where(m => m.LotNo == maxCodestr2).Max(a => a.BoxNo3);//支号
                            if (string.IsNullOrWhiteSpace(maxCodestr2))
                            {
                                throw new Exception("车间余料退回单据批次不能为空");
                            }
                        }
                        else
                        {
                            //var labelCount = label.Where(m => m.ASNDetailNo == imId).ToList();
                            //if (labelCount.Count <= 0)
                            //{
                            //    throw new Exception("当前单据明细还未生成标签,请选择重置批次选项为‘是’;");
                            //}
                            //maxCodestr2 = labelCount.Max(a => a.LotNo); ;
                            maxCodestr2 = asnList.LotNo;
                            //if (string.IsNullOrWhiteSpace(maxCodestr2))
                            //{
                            //    maxCodestr2 = toDayTime2.Substring(2, 6) + "0001";
                            //}
                            maxboxcode2 = label.Where(m => m.LotNo == maxCodestr2).Max(a => a.BoxNo);//箱号;
                            maxboxcode3 = label.Where(m => m.LotNo == maxCodestr2).Max(a => a.BoxNo3);//支号;
                        }
                    }
                    else
                    {
@@ -868,7 +950,7 @@
                        }
                        //获取箱码
                        if (maxboxcode2 == "")
                        if (string.IsNullOrWhiteSpace(maxboxcode2))
                        {
                            maxboxcode2 = "B"+maxCodestr2 + "000001"; // 箱号     批号+第几箱
                        }
@@ -912,7 +994,7 @@
                                }
                            }
                            
                            if (maxboxcode3 == "")
                            if (string.IsNullOrWhiteSpace(maxboxcode3))
                            {
                                maxboxcode3 = "Z"+maxCodestr2 + "0001"; // 支号     批号+第几箱 
                            }
@@ -921,8 +1003,8 @@
                                maxboxcode3 = maxboxcode3.Substring(0, 11) + (int.Parse(maxboxcode3.Substring(11, 4)) + 1).ToString().PadLeft(4, '0'); 
                            }
                            var ssss = d.ToString("0");
                            var sss = int.Parse(ssss);
                            //var ssss = d.ToString("0");
                            //var sss = int.Parse(ssss);
                            // 将条码保存到原料条码表
                            var labelModel = new BllBoxInfo()
@@ -931,7 +1013,7 @@
                                ASNDetailNo = asnList.Id,
                                BoxNo = maxboxcode2,
                                BoxNo3 = maxboxcode3,
                                Qty = sss,
                                Qty = d,
                                FullQty = bNum,
                                Status = "0",
                                SkuNo = asnList.SkuNo,
@@ -939,20 +1021,25 @@
                                LotNo = maxCodestr2,
                                Standard = asnList.Standard,
                                PackageStandard = packStr,
                                SupplierLot = asnList.SupplierLot,
                                SupplierLot = supplierLot,
                                InspectMark = "0",
                                BitBoxMark = bNum > boxQty ? "1" : "0",
                                InspectStatus = "0",
                                ProductionTime = DateTime.Parse(productionTime),
                                StoreTime = DateTime.Parse(storeTime),
                                ExpirationTime = DateTime.Parse(expirationTime),
                                ProductionTime = proTime,
                                Origin = "WMS生成",
                                CreateUser = userId,
                                CreateTime = DateTime.Now,
                            };
                            if (bl2)
                            {
                                labelModel.ExpirationTime = expTime;
                            }
                            if (bl3)
                            {
                                labelModel.StoreTime = stoTime;
                            }
                            Db.Insertable(labelModel).ExecuteCommand();
                            modelList.Add(labelModel);
                        }
@@ -975,7 +1062,7 @@
                        Db.Updateable(asnList).ExecuteCommand();
                    } 
                }
                Db.CommitTran();
                return modelList;
@@ -983,6 +1070,7 @@
            }
            catch (Exception e)
            {
                Db.RollbackTran();
                throw new Exception(e.Message);
            }
        }
@@ -1550,5 +1638,188 @@
                throw new Exception(e.Message);
            }
        }
        //根据入库单号过去单据下所有批次号
        public List<string> GetLotNoListByAsn(string asnNo)
        {
            try
            {
                var data = new List<string>();
                var asnList = Db.Queryable<BllArrivalNoticeDetail>().Where(m => m.IsDel == "0" && m.ASNNo == asnNo).ToList();
                if (asnList.Count == 0)
                {
                    throw new Exception("未查询到该单据号的信息");
                }
                foreach (var item in asnList)
                {
                    if (string.IsNullOrWhiteSpace(item.LotNo))
                    {
                        continue;
                    }
                    var strList = item.LotNo.Split(";");
                    foreach (var str in strList)
                    {
                        if (string.IsNullOrWhiteSpace(str))
                        {
                            continue;
                        }
                        data.Add(str);
                    }
                }
                return data;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        //删除单据下单个或多个批次的标签
        public void DelLabelByAsnNo(string asnNo,string lotNo, int userId)
        {
            try
            {
                var asnList = Db.Queryable<BllArrivalNoticeDetail>().Where(m => m.IsDel == "0" && m.ASNNo == asnNo).ToList();
                if (asnList.Count == 0)
                {
                    throw new Exception("未查询到该单据号的信息");
                }
                Db.BeginTran();
                //批次为空:当前单据下所有批次的标签全部删除; 有批次:伤处当前单据下当前批次的标签
                if (string.IsNullOrWhiteSpace(lotNo)) //删除全部标签
                {
                    var labelList = Db.Queryable<BllBoxInfo>().Where(m => m.IsDel == "0" && m.ASNNo == asnNo).ToList();
                    if (labelList.Count(m => m.Status != "0") >= 1)
                    {
                        throw new Exception("当前单据批次的标签已有已使用,不能删除");
                    }
                    //foreach (var item in asnList)
                    //{
                    //    if (string.IsNullOrWhiteSpace(item.LotNo))
                    //    {
                    //        continue;
                    //    }
                    //    item.LotNo = "";
                    //    Db.Updateable(item).ExecuteCommand();
                    //}
                    Db.Deleteable(labelList).ExecuteCommand();
                }
                else  //删除固定批次标签
                {
                    var labelList = Db.Queryable<BllBoxInfo>().Where(m => m.IsDel == "0" && m.ASNNo == asnNo && m.LotNo == lotNo).ToList();
                    if (labelList.Count(m=>m.Status != "0") >= 1 )
                    {
                        throw new Exception("当前单据批次的标签已有已使用,不能删除");
                    }
                    //foreach (var item in asnList)
                    //{
                    //    if (string.IsNullOrWhiteSpace(item.LotNo) || item.LotNo!=lotNo)
                    //    {
                    //        continue;
                    //    }
                    //    var strList = item.LotNo.Split(";");
                    //    var updateLotNo = "";
                    //    foreach (var str in strList)
                    //    {
                    //        if (string.IsNullOrWhiteSpace(str) || str == lotNo)
                    //        {
                    //            continue;
                    //        }
                    //        if (string.IsNullOrWhiteSpace(updateLotNo))
                    //        {
                    //            updateLotNo = str;
                    //        }
                    //        else
                    //        {
                    //            updateLotNo += ";" + str;
                    //        }
                    //    }
                    //    item.LotNo = updateLotNo;
                    //    Db.Updateable(item).ExecuteCommand();
                    //}
                    Db.Deleteable(labelList).ExecuteCommand();
                }
                Db.CommitTran();
                var msg = $"删除了单据号:{asnNo}";
                if (!string.IsNullOrWhiteSpace(lotNo))
                {
                    msg += $"、批次号:{lotNo}";
                }
                msg += "的标签信息";
                new OperationASNServer().AddLogOperationAsn("入库作业", "物料标签", asnNo, "删除", msg, userId);
            }
            catch (Exception e)
            {
                Db.RollbackTran();
                throw new Exception(e.Message);
            }
        }
        //编辑标签数量
        public void EditLabelQty(int id, string qty, int userId)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(qty))
                {
                    throw new Exception("编辑数量不可为空");
                }
                var qtyData = 0m;
                var isOk = decimal.TryParse(qty, out qtyData);
                var boxInfo = Db.Queryable<BllBoxInfo>().First(m => m.IsDel == "0" && m.Id == id);
                if (boxInfo == null)
                {
                    throw new Exception("未查询到该标签信息");
                }
                if (isOk)
                {
                    if (qtyData <= 0)
                    {
                        throw new Exception("数量不能小于等于0");
                    }
                    boxInfo.Qty = qtyData;
                    Db.Updateable(boxInfo).ExecuteCommand();
                }
                else
                {
                    throw new Exception("数量转换失败");
                }
                var msg = $"编辑了箱号:{boxInfo.BoxNo}";
                if (!string.IsNullOrWhiteSpace(boxInfo.BoxNo3))
                {
                    msg += $"、支号:{boxInfo.BoxNo3}";
                }
                msg += "的标签数量信息";
                new OperationASNServer().AddLogOperationAsn("入库作业", "物料标签", boxInfo.BoxNo, "编辑", msg, userId);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
    }
}