chengsc
2024-07-21 4df579fcaba629e57397b86ac952a423637f8f97
Wms/WMS.BLL/BllPdaServer/PdaAsnServer.cs
@@ -3351,6 +3351,1019 @@
                throw new Exception(e.Message);
            }
        }
        //JC26绑定托盘 贴标物料
        public void BindPalletPasteCode(PdaPalletBindVm model, int userId)
        {
            try
            {
                #region 判断
                //0:成品入库 1:采购入库 3:退货入库 4:车间余料入库 5:其它入库 6:代储入库
                var TypeLot = "5, 6";
                if (string.IsNullOrEmpty(model.AsnNo))
                {
                    throw new Exception("单据号不可为空!");
                }
                if (model.AsnDetailId == null || model.AsnDetailId == 0)
                {
                    throw new Exception("物料不可为空!");
                }
                if (string.IsNullOrEmpty(model.PalletNo))
                {
                    throw new Exception("托盘号不可为空!");
                }
                //根据单据号获取入库单总单
                var notice = Db.Queryable<BllArrivalNotice>().First(a => a.IsDel == "0" && a.ASNNo == model.AsnNo);
                if (notice.Status != "0" && notice.Status != "1" && notice.Status != "2")
                {
                    throw new Exception("该单据已关单!");
                }
                if (string.IsNullOrEmpty(model.LotNo))
                {
                    throw new Exception("物料不为空!");
                }
                else
                {
                    //从物料名称-批次中取出批次
                    int indexOfDash = model.LotNo.IndexOf("-");
                    if (indexOfDash != -1)
                    {
                        model.LotNo = model.LotNo.Substring(indexOfDash + 1);
                    }
                    else
                    {
                        if (!TypeLot.Contains(notice.Type))
                        {
                            throw new Exception("物料批次不可为空!");
                        }
                        model.LotNo = "";
                    }
                }
                if (string.IsNullOrEmpty(model.BoxNo))
                {
                    throw new Exception("箱码信息不可为空!");
                }
                if (model.IsContinue == "1")
                {
                    if (string.IsNullOrWhiteSpace(model.TailBoxNo))
                    {
                        throw new Exception("开启连续组托时,尾箱码信息不可为空!");
                    }
                }
                #endregion
                var comTime = DateTime.Now;
                //托盘是否存在
                var pallet = Db.Queryable<SysPallets>().First(m => m.IsDel == "0" && m.PalletNo == model.PalletNo);
                if (pallet == null)
                {
                    throw new Exception("未查询到托盘信息,请核实!");
                }
                // 验证入库单明细是否存在
                var detail = Db.Queryable<BllArrivalNoticeDetail>().First(m => m.IsDel == "0" && m.Id == model.AsnDetailId && m.ASNNo == model.AsnNo && m.LotNo.Contains(model.LotNo));
                if (detail == null)
                {
                    throw new Exception("当前物料及批次与单据无关联,请核实!");
                }
                //判断托盘是否在库外
                var stockDetail = Db.Queryable<DataStockDetail>().First(m => m.IsDel == "0" && m.PalletNo == model.PalletNo);
                if (stockDetail != null && !string.IsNullOrEmpty(stockDetail.WareHouseNo))
                {
                    throw new Exception("该托盘已有储位信息,请核实!");
                }
                if (stockDetail != null && (stockDetail.SkuNo != detail.SkuNo || stockDetail.LotNo != model.LotNo))
                {
                    throw new Exception("该托盘在库存已有其它物料批次信息,请核实!");
                }
                #region 包装
                var sku = Db.Queryable<SysMaterials>().First(m => m.IsDel == "0" && m.SkuNo == detail.SkuNo);
                var pack = Db.Queryable<SysPackag>().First(m => m.IsDel == "0" && m.PackagNo == detail.PackagNo);    // liudl 由Sku包装编号变更为入库单明细包装编号
                var pNum = 0;//托盘物品数量
                var bNum = 0;//箱码物品数量
                if (pack == null)
                {
                    throw new Exception("获取物料包装失败,请核实!");
                }
                if (pack.L5Num.HasValue)
                {
                    pNum = (int)pack.L5Num;
                    bNum = (int)pack.L4Num;
                }
                else if (pack.L4Num.HasValue)
                {
                    pNum = (int)pack.L4Num;
                    bNum = (int)pack.L3Num;
                }
                else if (pack.L3Num.HasValue)
                {
                    pNum = (int)pack.L3Num;
                    bNum = (int)pack.L2Num;
                }
                else if (pack.L2Num.HasValue)
                {
                    pNum = (int)pack.L2Num;
                    bNum = (int)pack.L1Num;
                }
                else if (pack.L1Num.HasValue)
                {
                    pNum = (int)pack.L1Num;
                    bNum = (int)pack.L1Num;
                }
                if (pNum == 0 || bNum == 0)
                {
                    throw new Exception($"绑定失败,{detail.SkuNo}物品包装未找到!");
                }
                #endregion
                #region 验证是否允许立库同托盘不同物料入库或同托盘同物料不同批次入库
                var palletBind = Db.Queryable<BllPalletBind>().First(m => m.IsDel == "0" && m.ASNNo == model.AsnNo && m.PalletNo == model.PalletNo && m.ASNDetailNo != model.AsnDetailId);
                if (palletBind != null)
                {
                    var box = Db.Queryable<BllBoxInfo>().First(m => m.IsDel == "0" && m.BindNo == palletBind.Id && m.BitBoxMark == "0");
                    if (box != null && (box.SkuNo != detail.SkuNo || !detail.LotNo.Contains(box.LotNo)))
                    {
                        var funSetting = Db.Queryable<SysFunSetting>().First(a => a.IsDel == "0" && a.FunSetNo == "Fun045");
                        if (funSetting == null || funSetting.IsEnable == "OFF")
                        {
                            throw new Exception($"不允许立库同托盘不同物料入库或同托盘不同批次入库!");
                        }
                    }
                }
                #endregion
                //liudl  && m.Status != "2"  组盘既入库完成无需根据托盘状态判断
                var bind = Db.Queryable<BllPalletBind>().First(m => m.IsDel == "0" && m.ASNDetailNo == model.AsnDetailId && m.PalletNo == model.PalletNo);
                var bindId = 0;
                if (bind == null)
                {
                    bind = new BllPalletBind
                    {
                        ASNNo = model.AsnNo,
                        ASNDetailNo = (int)model.AsnDetailId,
                        PalletNo = model.PalletNo,
                        Qty = model.SkuQty,
                        FullQty = pNum,
                        Status = "2",//入库完成
                        Type = "0",
                        LotNo = model.LotNo,
                        LotText = detail.LotText,
                        SupplierLot = detail.SupplierLot,
                        //InspectMark = model.IsSample,      //是否取样托盘  后期业务开发时 接口传值需添加对应字段判断
                        //SamplingQty = 0,     //取样数量  后期业务开发时 接口传值需添加对应字段判断
                        BitPalletMark = "1",
                        IsBale = "0",
                        IsBelt = "0",
                        CreateUser = userId,
                        Demo = model.Demo,
                    };
                    // 插入托盘绑定表
                    bindId = Db.Insertable(bind).ExecuteReturnIdentity();
                }
                else
                {
                    bindId = bind.Id;
                    bind.Qty += model.SkuQty;
                }
                if (bind.Qty < 0)
                {
                    throw new Exception("组盘数量不能小于0");
                }
                #region 库存明细
                //获取该批次最终质检结果
                BllQualityInspect quality = new BllQualityInspect();
                //判断是否为退货入库单
                if (notice.Type == "3")
                {
                    quality = Db.Queryable<BllQualityInspect>().Where(a => a.IsDel == "0" && a.ASNNo == detail.ASNNo && a.SkuNo == detail.SkuNo && a.LotNo == detail.LotNo).OrderByDescending(a => a.CreateTime).First();
                }
                else
                {
                    quality = Db.Queryable<BllQualityInspect>().Where(a => a.IsDel == "0" && a.SkuNo == detail.SkuNo && a.LotNo == detail.LotNo).OrderByDescending(a => a.CreateTime).First();
                }
                var tags = "0";
                if (notice.Type == "3" || notice.Type == "4")
                {
                    tags = "1";
                }
                // 判断库存明细是否已有此托盘信息
                var sd1 = Db.Queryable<DataStockDetail>()
                    .First(m => m.IsDel == "0" && m.ASNDetailNo == bind.ASNDetailNo && m.PalletNo == model.PalletNo);
                var sdId1 = 0;
                if (sd1 != null)
                {
                    sdId1 = sd1.Id;
                    // 库存已存在 更新数据
                    sd1.Qty = bind.Qty;
                    sd1.CompleteTime = comTime;
                    sd1.UpdateUser = userId;
                    sd1.UpdateTime = comTime;
                    Db.Updateable(sd1).ExecuteCommand(); //修改质检信息
                }
                else
                {
                    // 库存不存在 插入数据
                    sd1 = new DataStockDetail()
                    {
                        LotNo = bind.LotNo,
                        LotText = bind.LotText,
                        SupplierLot = bind.SupplierLot,
                        SkuNo = sku.SkuNo,
                        SkuName = sku.SkuName,
                        Standard = sku.Standard,
                        Qty = bind.Qty,
                        LockQty = 0,
                        FrozenQty = 0,
                        InspectQty = 0,
                        ASNNo = bind.ASNNo,
                        ASNDetailNo = bind.ASNDetailNo,
                        WareHouseNo = "",//所属仓库
                        RoadwayNo = "",//所属巷道
                        AreaNo = "",//所属区域
                        LocatNo = "",//储位地址
                        PalletNo = bind.PalletNo,
                        PalletNo2 = bind.PalletNo2,
                        PalletNo3 = bind.PalletNo3,
                        PalletTags = tags,
                        CompleteTime = comTime,
                        ProductionTime = bind.ProductionTime,
                        ExpirationTime = bind.ExpirationTime,
                        Status = "0",
                        InspectMark = bind.InspectMark,
                        InspectStatus = sku.IsInspect,
                        BitPalletMark = bind.BitPalletMark,
                        PackagNo = detail.PackagNo,                    //liudl 由sku的包装编号变为入库单明细的包装编码
                        IsBale = bind.IsBale,
                        IsBelt = bind.IsBelt,
                        IsDel = "0",
                        CreateUser = 0,
                        CreateTime = comTime
                    };
                    //维护库存明细货主/供应商信息
                    if (notice.Type == "0" || notice.Type == "2" || notice.Type == "4" || notice.Type == "6" || notice.Type == "7")//0:成品入库,2:中间品入库,4:车间余料入库,6:代储入库,7:寄存入库
                    {
                        sd1.OwnerNo = notice.CustomerNo;//货主编码
                        sd1.OwnerName = notice.CustomerName;//货主名称
                    }
                    else if (notice.Type == "1" || notice.Type == "5")//1:采购入库,2:其它入库
                    {
                        sd1.SupplierNo = notice.CustomerNo;//供应商编码
                        sd1.SupplierName = notice.CustomerName;//供应商名称
                    }
                    if (quality != null)
                    {
                        //修改合格不合格数量
                        if (quality.IsQualified == "1") //合格
                        {
                            //增加合格数量
                            quality.PassQty += bind.Qty;
                            sd1.InspectStatus = "1";
                        }
                        else if (quality.IsQualified == "0") //不合格
                        {
                            //增加不合格数量
                            quality.FailQty += bind.Qty;
                            sd1.InspectStatus = "2";
                        }
                        Db.Updateable(quality).ExecuteCommand(); //修改质检信息
                    }
                    //添加库存明细
                    sdId1 = Db.Insertable(sd1).ExecuteReturnIdentity();
                }
                #endregion
                #region 箱码信息
                var msgStr = $"箱号为{model.BoxNo}";
                var boxInfoList = new List<BllBoxInfo>();
                //首箱
                var boxInfo = Db.Queryable<BllBoxInfo>().Where(m => m.IsDel == "0" && m.BoxNo == model.BoxNo).ToList();
                if (boxInfo.Count == 0)
                {
                    throw new Exception("箱码信息不存在!");
                }
                boxInfo = boxInfo.Where(m => m.Status == "0").ToList();
                if (boxInfo.Count == 0)
                {
                    throw new Exception("箱码已被使用!");
                }
                boxInfoList.AddRange(boxInfo);
                //是否连续组托
                if (model.IsContinue == "1")
                {
                    //尾箱
                    var boxInfo2 = Db.Queryable<BllBoxInfo>().Where(m => m.IsDel == "0" && m.BoxNo == model.TailBoxNo).ToList();
                    if (boxInfo2.Count == 0)
                    {
                        throw new Exception("尾箱码信息不存在!");
                    }
                    boxInfo2 = boxInfo2.Where(m => m.Status == "0").ToList();
                    if (boxInfo2.Count == 0)
                    {
                        throw new Exception("尾箱箱码已被使用!");
                    }
                    boxInfoList.AddRange(boxInfo2);
                    var sql = $"select * from BllBoxInfo where IsDel = '0' and Status = '0' and boxNo>'{model.BoxNo}' and boxNo<'{model.TailBoxNo}'; ";
                    var list = Db.Ado.SqlQuery<BllBoxInfo>(sql);
                    boxInfoList.AddRange(list);
                    msgStr += $"尾箱号为{model.TailBoxNo}";
                }
                #endregion
                // 更改箱支关系表
                decimal factQty = 0.00m;//托盘总数量
                var isSample = "0";//是否取样
                var sampleQty = 0m;//取样数量
                var boxGroup = boxInfoList.GroupBy(m => m.BoxNo).ToList();
                foreach (var g in boxGroup)
                {
                    decimal boxFullQty = 0;//箱内总数量
                    foreach (var box in g)
                    {
                        if (box.BindNo != null && box.BindNo != 0)
                        {
                            continue;
                        }
                        //箱内物料批次与单据明细不符合
                        if (box.SkuNo != detail.SkuNo || box.LotNo != model.LotNo)
                        {
                            throw new Exception($"-1:{box.BoxNo}箱内物料及批次与单据不一致,请核实!");
                        }
                        box.ASNNo = model.AsnNo;
                        box.ASNDetailNo = model.AsnDetailId;
                        box.BindNo = bindId;
                        box.PalletNo = model.PalletNo;
                        box.Status = "2";
                        //box.InspectMark = model.IsSample;  //是否取样托盘  后期业务开发时 接口传值需添加对应字段判断
                        //box.SamplingQty = 0;    //取样数量  后期业务开发时 接口传值需添加对应字段判断
                        box.CompleteTime = comTime;
                        box.UpdateTime = comTime;
                        box.UpdateUser = userId;
                        //box.Qty = box.Qty;
                        //box.BitBoxMark = box.Qty == box.FullQty ? "0" : "1";
                        factQty += box.Qty;
                        boxFullQty += box.Qty;
                        if (box.InspectMark == "1") //判断是否取样
                        {
                            isSample = "1";
                            sampleQty += (decimal)box.SamplingQty;
                        }
                        #region 库存箱码明细
                        var box2 = new DataBoxInfo()
                        {
                            StockDetailId = sdId1,
                            BindNo = bind.Id,
                            BoxNo = box.BoxNo,
                            BoxNo2 = box.BoxNo2,
                            BoxNo3 = box.BoxNo3,
                            PalletNo = box.PalletNo,
                            PalletNo2 = box.PalletNo2,
                            PalletNo3 = box.PalletNo3,
                            Qty = box.Qty,
                            FullQty = box.FullQty,
                            Status = "2",//0:未组托  1:已组托 2:已入库 3:已出库 4:已分配 5:已拣货
                            LotNo = box.LotNo,
                            LotText = box.LotText,
                            SkuNo = box.SkuNo,
                            SkuName = box.SkuName,
                            Standard = sku.Standard,
                            ProductionTime = box.ProductionTime,
                            SupplierLot = box.SupplierLot,
                            InspectStatus = sku.IsInspect,
                            InspectMark = box.InspectMark,
                            BitBoxMark = box.BitBoxMark,
                            ExpirationTime = box.ExpirationTime,
                            CreateUser = 0,
                            CreateTime = comTime
                        };
                        //添加库存箱码明细
                        Db.Insertable(box2).ExecuteCommand();
                        #endregion
                    }
                    if (boxFullQty > bNum)
                    {
                        throw new Exception($"绑定失败,{g.Key}箱码绑定数量大于该物品包装数量!");
                    }
                }
                Db.Updateable(boxInfoList).ExecuteCommand();
                // 更新托盘绑定表
                bind.Qty += factQty;
                //if (bind.FullQty < bind.Qty && isTextTable == 0)
                if (bind.FullQty < bind.Qty)
                {
                    throw new Exception("托盘绑定数量已超出该物料包装数量");
                }
                if (bind.FullQty == bind.Qty)
                {
                    bind.BitPalletMark = "0";
                    sd1.BitPalletMark = "0";
                }
                //if (bind.Qty > pNum && isTextTable == 0)
                if (bind.Qty > pNum)
                {
                    throw new Exception($"绑定失败,{bind.PalletNo}托盘绑定数量大于该物品托盘包装数量!");
                }
                if (isSample == "1")
                {
                    bind.InspectMark = "1";
                    bind.SamplingQty = bind.SamplingQty == null ? sampleQty : bind.SamplingQty + sampleQty;
                }
                Db.Updateable(bind).Where(m => m.Id == bindId).ExecuteCommand();
                #region 入库单及明细
                decimal addQty = factQty;
                detail.FactQty += addQty;//已组数量
                detail.CompleteQty += addQty;//完成数量
                if (isSample == "1") //后期业务开发时 接口传值改为判断是否取样
                {
                    detail.IsSampling = "1";
                }
                detail.Status = "1";//0:等待执行 1:正在执行 2:执行完成
                if (detail.CompleteQty >= detail.Qty)
                {
                    detail.Status = "2";
                    detail.CompleteTime = comTime;
                }
                detail.UpdateUser = userId;
                detail.UpdateTime = comTime;
                //更新入库单明细
                Db.Updateable(detail).ExecuteCommand();
                notice.UpdateUser = userId;
                notice.UpdateTime = comTime;
                if (notice.Status == "0")
                {
                    notice.Status = "1";
                }
                var asnDetailNum = Db.Queryable<BllArrivalNoticeDetail>()
                                .Count(m => m.IsDel == "0" && m.ASNNo == detail.ASNNo && m.Status != "2");
                if (asnDetailNum == 0)
                {
                    notice.Status = "2";
                    notice.CompleteTime = comTime;//完成时间
                }
                //更新入库单
                Db.Updateable(notice).ExecuteCommand();
                #endregion
                #region 库存明细
                sd1.Qty = bind.Qty;
                //更改库存明细数量
                Db.Updateable(sd1).Where(m => m.Id == sdId1).ExecuteCommand();
                #endregion
                #region 库存
                var dataStock = Db.Queryable<DataStock>().First(m => m.IsDel == "0" && m.SkuNo == sku.SkuNo && m.LotNo == bind.LotNo);
                if (dataStock != null)
                {
                    dataStock.Qty += factQty;
                    if (bind.InspectMark == "1")
                    {
                        dataStock.IsSampling = bind.InspectMark;
                    }
                    Db.Updateable(dataStock).ExecuteCommand();
                }
                else
                {
                    var stock = new DataStock()
                    {
                        SkuNo = sku.SkuNo,
                        SkuName = sku.SkuName,
                        Standard = sku.Standard,
                        LotNo = bind.LotNo,
                        LotText = bind.LotText,
                        Qty = bind.Qty,
                        LockQty = 0,
                        FrozenQty = 0,
                        IsSampling = bind.InspectMark,
                        IsDel = "0",
                        CreateUser = userId,
                        CreateTime = comTime
                    };
                    //维护库存货主信息
                    if (notice.Type == "0" || notice.Type == "2" || notice.Type == "4" || notice.Type == "6" || notice.Type == "7")//0:成品入库,2:中间品入库,4:车间余料入库,6:代储入库,7:寄存入库
                    {
                        stock.OwnerNo = notice.CustomerNo;//货主编码
                        stock.OwnerName = notice.CustomerName;//货主名称
                    }
                    Db.Insertable(stock).ExecuteCommand();
                }
                #endregion
                #region 质检请验
                if ((notice.Type == "1" || notice.Type == "4" || notice.Type == "5") && sku.IsInspect != "1")
                {
                    var qualityRequest = Db.Queryable<BllQualityInspectionRequest>().First(m => m.IsDel == "0" && m.SkuNo == sku.SkuNo && m.LotNo == bind.LotNo && m.SupplierLot == bind.SupplierLot);
                    if (qualityRequest == null)
                    {
                        string qcNo = new Common().GetMaxNo("QC");
                        qualityRequest = new BllQualityInspectionRequest();
                        qualityRequest.QcNo = qcNo;
                        qualityRequest.Status = "0";
                        qualityRequest.SkuNo = sku.SkuNo;
                        qualityRequest.SkuName = sku.SkuName;
                        qualityRequest.LotNo = bind.LotNo;
                        qualityRequest.SupplierLot = bind.SupplierLot;
                        qualityRequest.Qty = detail.Qty;
                        qualityRequest.SamplingQty = 0;
                        qualityRequest.ASNNo = detail.ASNNo;
                        qualityRequest.CreateUser = userId;
                        qualityRequest.CreateTime = comTime;
                        //添加质检请验单
                        Db.Insertable(qualityRequest).ExecuteCommand();
                    }
                }
                #endregion
                // 更改托盘使用状态
                var sqlStr = $"update SysPallets set Status = '1' where PalletNo = '{model.PalletNo}';";
                //添加托盘记录表数据
                sqlStr += $"insert into LogPalletTrack values('{model.PalletNo}','{model.AsnNo}','组盘','0',getDate(),{userId},NULL,NULL);";
                Db.Ado.ExecuteCommand(sqlStr);
                new OperationASNServer().AddLogOperationAsn("PDA模块", "托盘绑定", model.AsnNo, "添加", $"添加了托盘码为:{model.PalletNo}、{msgStr}的组盘信息", userId);
                Db.CommitTran();
            }
            catch (Exception e)
            {
                Db.RollbackTran();
                throw new Exception(e.Message);
            }
        }
        //JC26绑定托盘 不贴标物料
        public void BindPalletNoPasteCode(PdaPalletBindVm model, int userId)
        {
                #region 判断
                //0:成品入库 1:采购入库 3:退货入库 4:车间余料入库 5:其它入库 6:代储入库
                var TypeLot = "5, 6";
                if (string.IsNullOrEmpty(model.AsnNo))
                {
                    throw new Exception("单据号不可为空!");
                }
                if (model.AsnDetailId == null || model.AsnDetailId == 0)
                {
                    throw new Exception("物料不可为空!");
                }
                if (string.IsNullOrEmpty(model.PalletNo))
                {
                    throw new Exception("托盘号不可为空!");
                }
                if (model.SkuQty < 0)
                {
                    throw new Exception("物料数量不能小于0!");
                }
                //根据单据号获取入库单总单
                var notice = Db.Queryable<BllArrivalNotice>().First(a => a.IsDel == "0" && a.ASNNo == model.AsnNo);
                if (notice.Status != "0" && notice.Status != "1" && notice.Status != "2")
                {
                    throw new Exception("该单据已关单!");
                }
                if (string.IsNullOrEmpty(model.LotNo))
                {
                    throw new Exception("物料批次不为空!");
                }
                else
                {
                    //从物料名称-批次中取出批次
                    int indexOfDash = model.LotNo.IndexOf("-");
                    if (indexOfDash != -1)
                    {
                        model.LotNo = model.LotNo.Substring(indexOfDash + 1);
                    }
                    else
                    {
                        if (!TypeLot.Contains(notice.Type))
                        {
                            throw new Exception("物料批次不可为空!");
                        }
                        model.LotNo = "";
                    }
                }
                //int isTextTable = model.TableType;
                #endregion
                var comTime = DateTime.Now;
                //托盘是否存在
                var pallet = Db.Queryable<SysPallets>().First(m => m.IsDel == "0" && m.PalletNo == model.PalletNo);
                if (pallet == null)
                {
                    throw new Exception("未查询到托盘信息,请核实!");
                }
                // 验证入库单明细是否存在
                var detail = Db.Queryable<BllArrivalNoticeDetail>().First(m => m.IsDel == "0" && m.Id == model.AsnDetailId && m.ASNNo == model.AsnNo && m.LotNo.Contains(model.LotNo));
                if (detail == null)
                {
                    throw new Exception("当前物料及批次与单据无关联,请核实!");
                }
                //判断托盘是否在库外
                var stockDetail = Db.Queryable<DataStockDetail>().First(m => m.IsDel == "0" && m.PalletNo == model.PalletNo);
                if (stockDetail != null && !string.IsNullOrEmpty(stockDetail.WareHouseNo))
                {
                    throw new Exception("该托盘已有储位信息,请核实!");
                }
                if (stockDetail != null && (stockDetail.SkuNo != detail.SkuNo || stockDetail.LotNo != model.LotNo))
                {
                    throw new Exception("该托盘在库存已有其它物料批次信息,请核实!");
                }
                #region 包装
                var sku = Db.Queryable<SysMaterials>().First(m => m.IsDel == "0" && m.SkuNo == detail.SkuNo);
                var pack = Db.Queryable<SysPackag>().First(m => m.IsDel == "0" && m.PackagNo == detail.PackagNo);    // liudl 由Sku包装编号变更为入库单明细包装编号
                var pNum = 0;//托盘物品数量
                var bNum = 0;//箱码物品数量
                if (pack == null)
                {
                    throw new Exception("获取物料包装失败,请核实!");
                }
                if (pack.L5Num.HasValue)
                {
                    pNum = (int)pack.L5Num;
                    bNum = (int)pack.L4Num;
                }
                else if (pack.L4Num.HasValue)
                {
                    pNum = (int)pack.L4Num;
                    bNum = (int)pack.L3Num;
                }
                else if (pack.L3Num.HasValue)
                {
                    pNum = (int)pack.L3Num;
                    bNum = (int)pack.L2Num;
                }
                else if (pack.L2Num.HasValue)
                {
                    pNum = (int)pack.L2Num;
                    bNum = (int)pack.L1Num;
                }
                else if (pack.L1Num.HasValue)
                {
                    pNum = (int)pack.L1Num;
                    bNum = (int)pack.L1Num;
                }
                if (pNum == 0 || bNum == 0)
                {
                    throw new Exception($"绑定失败,{detail.SkuNo}物品包装未找到!");
                }
                #endregion
                #region 验证是否允许立库同托盘不同物料入库或同托盘同物料不同批次入库
                var palletBind = Db.Queryable<BllPalletBind>().First(m => m.IsDel == "0" && m.ASNNo == model.AsnNo && m.PalletNo == model.PalletNo && m.ASNDetailNo != model.AsnDetailId);
                if (palletBind != null)
                {
                    var box = Db.Queryable<BllBoxInfo>().First(m => m.IsDel == "0" && m.BindNo == palletBind.Id && m.BitBoxMark == "0");
                    if (box != null && (box.SkuNo != detail.SkuNo || !detail.LotNo.Contains(box.LotNo)))
                    {
                        var funSetting = Db.Queryable<SysFunSetting>().First(a => a.IsDel == "0" && a.FunSetNo == "Fun045");
                        if (funSetting == null || funSetting.IsEnable == "OFF")
                        {
                            throw new Exception($"不允许立库同托盘不同物料入库或同托盘不同批次入库!");
                        }
                    }
                }
                #endregion
                //liudl  && m.Status != "2"  组盘既入库完成无需根据托盘状态判断
                var bind = Db.Queryable<BllPalletBind>().First(m => m.IsDel == "0" && m.ASNDetailNo == model.AsnDetailId && m.PalletNo == model.PalletNo);
                var bindId = 0;
                if (bind == null)
                {
                    bind = new BllPalletBind
                    {
                        ASNNo = model.AsnNo,
                        ASNDetailNo = (int)model.AsnDetailId,
                        PalletNo = model.PalletNo,
                        Qty = model.SkuQty,
                        FullQty = pNum,
                        Status = "2",//入库完成
                        Type = "0",
                        LotNo = model.LotNo,
                        LotText = detail.LotText,
                        SupplierLot = detail.SupplierLot,
                        //InspectMark = model.IsSample,      //是否取样托盘  后期业务开发时 接口传值需添加对应字段判断
                        //SamplingQty = 0,     //取样数量  后期业务开发时 接口传值需添加对应字段判断
                        BitPalletMark = "1",
                        IsBale = "0",
                        IsBelt = "0",
                        CreateUser = userId,
                        Demo = model.Demo,
                    };
                    // 插入托盘绑定表
                    bindId = Db.Insertable(bind).ExecuteReturnIdentity();
                }
                else
                {
                    bindId = bind.Id;
                    bind.Qty += model.SkuQty;
                }
                if (bind.Qty < 0)
                {
                    throw new Exception("组盘数量不能小于0");
                }
                #region 库存明细
                //获取该批次最终质检结果
                BllQualityInspect quality = new BllQualityInspect();
                //判断是否为退货入库单
                if (notice.Type == "3")
                {
                    quality = Db.Queryable<BllQualityInspect>().Where(a => a.IsDel == "0" && a.ASNNo == detail.ASNNo && a.SkuNo == detail.SkuNo && a.LotNo == detail.LotNo).OrderByDescending(a => a.CreateTime).First();
                }
                else
                {
                    quality = Db.Queryable<BllQualityInspect>().Where(a => a.IsDel == "0" && a.SkuNo == detail.SkuNo && a.LotNo == detail.LotNo).OrderByDescending(a => a.CreateTime).First();
                }
                var tags = "0";
                if (notice.Type == "3" || notice.Type == "4")
                {
                    tags = "1";
                }
                // 判断库存明细是否已有此托盘信息
                var sd1 = Db.Queryable<DataStockDetail>()
                    .First(m => m.IsDel == "0" && m.ASNDetailNo == bind.ASNDetailNo && m.PalletNo == model.PalletNo && m.LotNo == bind.LotNo);
                var sdId1 = 0;
                if (sd1 != null)
                {
                    sdId1 = sd1.Id;
                    // 库存已存在 更新数据
                    sd1.Qty = bind.Qty;
                    sd1.CompleteTime = comTime;
                    sd1.UpdateUser = userId;
                    sd1.UpdateTime = comTime;
                    Db.Updateable(sd1).ExecuteCommand(); //修改质检信息
                }
                else
                {
                    // 库存不存在 插入数据
                    sd1 = new DataStockDetail()
                    {
                        LotNo = bind.LotNo,
                        LotText = bind.LotText,
                        SupplierLot = bind.SupplierLot,
                        SkuNo = sku.SkuNo,
                        SkuName = sku.SkuName,
                        Standard = sku.Standard,
                        Qty = bind.Qty,
                        LockQty = 0,
                        FrozenQty = 0,
                        InspectQty = 0,
                        ASNNo = bind.ASNNo,
                        ASNDetailNo = bind.ASNDetailNo,
                        WareHouseNo = "",//所属仓库
                        RoadwayNo = "",//所属巷道
                        AreaNo = "",//所属区域
                        LocatNo = "",//储位地址
                        PalletNo = bind.PalletNo,
                        PalletNo2 = bind.PalletNo2,
                        PalletNo3 = bind.PalletNo3,
                        PalletTags = tags,
                        CompleteTime = comTime,
                        ProductionTime = bind.ProductionTime,
                        ExpirationTime = bind.ExpirationTime,
                        Status = "0",
                        InspectMark = bind.InspectMark,
                        InspectStatus = sku.IsInspect,
                        BitPalletMark = bind.BitPalletMark,
                        PackagNo = detail.PackagNo,                    //liudl 由sku的包装编号变为入库单明细的包装编码
                        IsBale = bind.IsBale,
                        IsBelt = bind.IsBelt,
                        IsDel = "0",
                        CreateUser = 0,
                        CreateTime = comTime
                    };
                    //维护库存明细货主/供应商信息
                    if (notice.Type == "0" || notice.Type == "2" || notice.Type == "4" || notice.Type == "6" || notice.Type == "7")//0:成品入库,2:中间品入库,4:车间余料入库,6:代储入库,7:寄存入库
                    {
                        sd1.OwnerNo = notice.CustomerNo;//货主编码
                        sd1.OwnerName = notice.CustomerName;//货主名称
                    }
                    else if (notice.Type == "1" || notice.Type == "5")//1:采购入库,2:其它入库
                    {
                        sd1.SupplierNo = notice.CustomerNo;//供应商编码
                        sd1.SupplierName = notice.CustomerName;//供应商名称
                    }
                    if (quality != null)
                    {
                        //修改合格不合格数量
                        if (quality.IsQualified == "1") //合格
                        {
                            //增加合格数量
                            quality.PassQty += bind.Qty;
                            sd1.InspectStatus = "1";
                        }
                        else if (quality.IsQualified == "0") //不合格
                        {
                            //增加不合格数量
                            quality.FailQty += bind.Qty;
                            sd1.InspectStatus = "2";
                        }
                        Db.Updateable(quality).ExecuteCommand(); //修改质检信息
                    }
                    //添加库存明细
                    sdId1 = Db.Insertable(sd1).ExecuteReturnIdentity();
                }
                #endregion
                // 更改箱支关系表
                var isSample = "0";//是否取样
                var sampleQty = 0m;//取样数量
                //if (bind.FullQty < bind.Qty && isTextTable == 0)
                if (bind.FullQty < bind.Qty)
                {
                    throw new Exception("托盘绑定数量已超出该物料包装数量");
                }
                if (bind.FullQty == bind.Qty)
                {
                    bind.BitPalletMark = "0";
                    sd1.BitPalletMark = "0";
                }
                //if (bind.Qty > pNum && isTextTable == 0)
                if (bind.Qty > pNum)
                {
                    throw new Exception($"绑定失败,{bind.PalletNo}托盘绑定数量大于该物品托盘包装数量!");
                }
                if (isSample == "1")
                {
                    bind.InspectMark = "1";
                    bind.SamplingQty = bind.SamplingQty == null ? sampleQty : bind.SamplingQty + sampleQty;
                }
                Db.Updateable(bind).Where(m => m.Id == bindId).ExecuteCommand();
                #region 入库单及明细
                decimal addQty = model.SkuQty;
                detail.FactQty += addQty;//已组数量
                detail.CompleteQty += addQty;//完成数量
                if (isSample == "1") //后期业务开发时 接口传值改为判断是否取样
                {
                    detail.IsSampling = "1";
                }
                detail.Status = "1";//0:等待执行 1:正在执行 2:执行完成
                if (detail.CompleteQty >= detail.Qty)
                {
                    detail.Status = "2";
                    detail.CompleteTime = comTime;
                }
                detail.UpdateUser = userId;
                detail.UpdateTime = comTime;
                //更新入库单明细
                Db.Updateable(detail).ExecuteCommand();
                notice.UpdateUser = userId;
                notice.UpdateTime = comTime;
                if (notice.Status == "0")
                {
                    notice.Status = "1";
                }
                var asnDetailNum = Db.Queryable<BllArrivalNoticeDetail>()
                                .Count(m => m.IsDel == "0" && m.ASNNo == detail.ASNNo && m.Status != "2");
                if (asnDetailNum == 0)
                {
                    notice.Status = "2";
                    notice.CompleteTime = comTime;//完成时间
                }
                //更新入库单
                Db.Updateable(notice).ExecuteCommand();
                #endregion
                #region 库存明细
                sd1.Qty = bind.Qty;
                //更改库存明细数量
                Db.Updateable(sd1).Where(m => m.Id == sdId1).ExecuteCommand();
                #endregion
                #region 库存
                var dataStock = Db.Queryable<DataStock>().First(m => m.IsDel == "0" && m.SkuNo == sku.SkuNo && m.LotNo == bind.LotNo);
                if (dataStock != null)
                {
                    dataStock.Qty += model.SkuQty;
                    if (bind.InspectMark == "1")
                    {
                        dataStock.IsSampling = bind.InspectMark;
                    }
                    Db.Updateable(dataStock).ExecuteCommand();
                }
                else
                {
                    var stock = new DataStock()
                    {
                        SkuNo = sku.SkuNo,
                        SkuName = sku.SkuName,
                        Standard = sku.Standard,
                        LotNo = bind.LotNo,
                        LotText = bind.LotText,
                        Qty = bind.Qty,
                        LockQty = 0,
                        FrozenQty = 0,
                        IsSampling = bind.InspectMark,
                        IsDel = "0",
                        CreateUser = userId,
                        CreateTime = comTime
                    };
                    //维护库存货主信息
                    if (notice.Type == "0" || notice.Type == "2" || notice.Type == "4" || notice.Type == "6" || notice.Type == "7")//0:成品入库,2:中间品入库,4:车间余料入库,6:代储入库,7:寄存入库
                    {
                        stock.OwnerNo = notice.CustomerNo;//货主编码
                        stock.OwnerName = notice.CustomerName;//货主名称
                    }
                    Db.Insertable(stock).ExecuteCommand();
                }
                #endregion
                #region 质检请验
                if ((notice.Type == "1" || notice.Type == "4" || notice.Type == "5") && sku.IsInspect != "1")
                {
                    var qualityRequest = Db.Queryable<BllQualityInspectionRequest>().First(m => m.IsDel == "0" && m.SkuNo == sku.SkuNo && m.LotNo == bind.LotNo && m.SupplierLot == bind.SupplierLot);
                    if (qualityRequest == null)
                    {
                        string qcNo = new Common().GetMaxNo("QC");
                        qualityRequest = new BllQualityInspectionRequest();
                        qualityRequest.QcNo = qcNo;
                        qualityRequest.Status = "0";
                        qualityRequest.SkuNo = sku.SkuNo;
                        qualityRequest.SkuName = sku.SkuName;
                        qualityRequest.LotNo = bind.LotNo;
                        qualityRequest.SupplierLot = bind.SupplierLot;
                        qualityRequest.Qty = detail.Qty;
                        qualityRequest.SamplingQty = 0;
                        qualityRequest.ASNNo = detail.ASNNo;
                        qualityRequest.CreateUser = userId;
                        qualityRequest.CreateTime = comTime;
                        //添加质检请验单
                        Db.Insertable(qualityRequest).ExecuteCommand();
                    }
                }
                #endregion
                // 更改托盘使用状态
                var sqlStr = $"update SysPallets set Status = '1' where PalletNo = '{model.PalletNo}';";
                //添加托盘记录表数据
                sqlStr += $"insert into LogPalletTrack values('{model.PalletNo}','{model.AsnNo}','组盘','0',getDate(),{userId},NULL,NULL);";
                Db.Ado.ExecuteCommand(sqlStr);
                new OperationASNServer().AddLogOperationAsn("PDA模块", "托盘绑定", model.AsnNo, "添加", $"添加了托盘码为:{model.PalletNo}的组盘信息", userId);
        }
        #endregion
        #region 平库入库