using Rhea.Common; using Microsoft.AspNetCore.Http; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading; using System.Threading.Tasks; using Tiger.Model; using Tiger.IBusiness; using Tiger.Model.Sharetronic.Shelf; using Tiger.Business.WMS.Sharetronic.Shelf; namespace Tiger.Business.WMS.Transaction { /// /// 客供料入库清点事务 /// public class OtherInLocation : WMSTransactionBase, IOtherInLocation { public IOtherInLocation Init(string id, string userCode, string apiHost, string orgCode) { TransID = id; UserCode = userCode; ApiHost = apiHost; OrgCode = orgCode; Logger.Console.Info($"Start {this.GetType().Name} Transaction[ID: {TransID}]"); return this; } #region Propertys & Variables public string UserCode { get; set; } public long UserId { get; set; } public string RackCode { get; set; } public string WarehouseCode { get; set; } public string WarehouseName { get; set; } public string OrgCode { get; set; } public WMS_SHELF Shelf { get; set; } public Model.Minsun.CustomerSupplyPuton CustomerSupplyPuton { get; set; } #endregion #region Functions /// /// 扫描物料并复核,如果物料已经完成移库则货架上亮灯提醒储位 /// public async Task ScanItem(InReceiptInInput input) { var action = new ApiAction(); var multiRet = new Model.Minsun.MultiInvInStorageOutput(); try { if (string.IsNullOrEmpty(input.SN)) { action.IsSuccessed = false; action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.SnEmptyFailure"); // $"条码不能为空!"; return action; } //货架为空则扫描的认为是货架 if (string.IsNullOrEmpty(RackCode)) { var _action = await ScanShelf(input.SN); action.LocaleMsg = _action.LocaleMsg; action.IsSuccessed = _action.IsSuccessed; action.Data = _action.Data; return action; } var nLocation = new WMS_LOCATION(); //当input.ReqType为0时:无工单入库 if (input.ReqType == 0) { Result res = new(); if (WarehouseName.Contains("客供")) { res = WMS_ITEM_Biz.WmsItem.GetK(input.SN, new() { UserId = "admin" }, true); } else { res = WMS_ITEM_Biz.WmsItem.Get(input.SN, new() { UserId = "admin" }, true); } //判断是否一维码 if (!res.Data.IsQRCode) { action.IsSuccessed = false; action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.NotQrCode", res.Data.Barcode.MetaSn); return action; } if (!res.IsSuccessed) { action.IsSuccessed = false; action.LocaleMsg = res.LocaleMsg; return action; } if (res.Data.Status.GetValue() != WMS_ITEM.STATUSs.NotExists.GetValue() && res.Data.Status.GetValue() <= WMS_ITEM.STATUSs.InStore.GetValue()) { action.IsSuccessed = false; action.LocaleMsg = Biz.L("状态异常{0}", string.Join(',', res.Data.StatusList.Select(x => x.GetDesc()))); //Biz.L("WMS.CustSupChk.ScanItem.StatusException", string.Join(',', res.Data.StatusList.Select(x => x.GetDesc()))); return action; } //验证条码是否正确 //if (!res.Data.isNormalStatus || !new[] { WMS_ITEM.STATUSs.WaitIn }.Contains(res.Data.Status)) //{ // action.IsSuccessed = false; // action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.StatusException", string.Join(',', res.Data.StatusList)); // return action; //} //物料验证 if (res.Data.ItemInfo.IsNullOrEmpty() || res.Data.ItemInfo.IS_ACTIVE == "N") { action.IsSuccessed = false; action.LocaleMsg = Biz.L("WMS.ProdMReq.ScanItem.ItemCodeNotExistsOrNotActive", res.Data.ItemInfo.ITEM_CODE.IsNullOrEmpty(res.Data.Barcode.ItemCode)); return action; } List ItemHistorys = new List(); WMS_ITEM Item; WMS_ITEM_PKG ItemPkgs; var IsNull = false; if (!res.Data.IsNullOrEmpty()) { if (res.Data.Items.Count == 0) { IsNull = true; Item = new() { SN = res.Data.Barcode.SN, ITEM_CODE = WarehouseName.Contains("客供") && !res.Data.Barcode.ItemCode.StartsWith("K") ? ("K" + res.Data.Barcode.ItemCode) : (res.Data.Barcode.ItemCode), AUTH_ORG = input.AuthOption.CurOrg, STATUS = WMS_ITEM.STATUSs.WaitIn.GetValue(), QTY = res.Data.Barcode.Qty.ToDecimal(), PROD_DATE = res.Data.Barcode.DateCode.ToDateTime(), ERP_WH = WarehouseCode, UNIT = res.Data.Barcode.Unit, }; WMS_ITEM_HIS his = new WMS_ITEM_HIS(Item, $"无单据入库"); ItemHistorys.Add(his); ItemPkgs = new() { SN = res.Data.Barcode.SN, AUTH_ORG = input.AuthOption.CurOrg, ITEM_CODE = WarehouseName.Contains("客供") && !res.Data.Barcode.ItemCode.StartsWith("K") ? ("K" + res.Data.Barcode.ItemCode) : (res.Data.Barcode.ItemCode), QTY = res.Data.Barcode.Qty.ToDecimal(), ERP_WH = WarehouseCode, UNIT = res.Data.Barcode.Unit, }; res.Data.ItemsExt = Biz.Db.Queryable().Where(q => res.Data.Items.Any(s => s.SN == q.SN)).ToList();//扩展表 //var data = result.Data.Items.Select(i => i.SN).ToList().Except(result.Data.ItemsExt.Select(i => i.SN).ToList()); var data = res.Data.Items.Where(q => !res.Data.ItemsExt.Any(s => s.SN == q.SN)).ToList(); //扩展表不存在时新建 if (data.Count > 0) { foreach (var item in data) { res.Data.ItemsExt.Add(new() { SN = item.SN, SUPP_ITEM_CODE = res.Data.Barcode.OEMItemCode, META_SN = res.Data.Barcode.MetaSn, QR_CODE = res.Data.Barcode.MetaSn }); } } res.Data.Items.Add(Item); res.Data.History.Add(his); res.Data.Packages.Add(ItemPkgs); } else { foreach (var item in res.Data.Items) { item.QTY = res.Data.Barcode.Qty.ToDecimal(); item.ERP_WH = WarehouseCode; item.AUTH_ORG = input.AuthOption.CurOrg; } foreach (var item in res.Data.Packages) { item.QTY = res.Data.Barcode.Qty.ToDecimal(); item.ERP_WH = WarehouseCode; item.AUTH_ORG = input.AuthOption.CurOrg; } } //CustomerSupplyPuton.LocationCode = nLocation.LOCATION_CODE; } foreach (var item in res.Data.Items) { item.SOURCE_CODE = item.TRANS_CODE; item.SOURCE_LINE = item.TRANS_LINE; item.SOURCE_ORDER = item.TRANS_NO; item.TRANS_CODE = nameof(BIZ_ERP_OTH_IN); item.TRANS_NO = "NO BILLCODE"; //item.TRANS_LINE = billLine.ToString(); } // 判断是否智能货架 if (CustomerSupplyPuton.IsSmartRack) { ShelfApiResult shelfApiResult = await Share.Shelf.PutOn(TransID, Shelf, res.Data.Items[0]); if (!shelfApiResult.IsSuccess) { action.IsSuccessed = false; action.LocaleMsg = Biz.L(shelfApiResult.GetData()); return action; } var reaultShelf = shelfApiResult.GetData(); nLocation = reaultShelf.GetLocation(); if (nLocation == null) { action.IsSuccessed = false; action.LocaleMsg = Biz.L($"系统不存在储位,请先维护货架信息"); return action; } var locationData = Biz.Db.Queryable().Where(q => q.LOCATION_ID == nLocation.ID).First(); if (!locationData.IsNullOrEmpty()) { action.IsSuccessed = false; action.LocaleMsg = Biz.L($"系统当前库位存在料盘,请检查系统库存信息"); return action; } CustomerSupplyPuton.LocationCode = nLocation.LOCATION_CODE; } else { if (CustomerSupplyPuton.LocationCode.IsNullOrEmpty() || CustomerSupplyPuton.WarehouseCode.IsNullOrEmpty()) { action.IsSuccessed = false; action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.LocationEmptyFailure"); //$"请先扫描库位码"; return action; } nLocation = await Biz.Db.Queryable().Where(t => t.LOCATION_CODE == CustomerSupplyPuton.LocationCode && t.AUTH_ORG == OrgCode).FirstAsync(); } //判断储位是否单放 if (nLocation.IS_SINGLE == "Y" && res.Data.Items.Count > 1) { action.IsSuccessed = false; action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.LocationSingleFailure"); //$"储位只能单放一个物料"; return action; } //执行入库 Result putonResult = WMS_ITEM_Biz.WmsItem.PutOn(res.Data, input.AuthOption, nLocation.LOCATION_CODE); if (!putonResult.IsSuccessed) { action.IsSuccessed = false; action.LocaleMsg = putonResult.LocaleMsg; return action; } PutOnInfo putOnInfo1 = putonResult.Data; //保存到数据库 var db1 = Business.Biz.Db; var dbTran1 = db1.UseTran(() => { //入库 if (IsNull) { db1.Insertable(putOnInfo1.Items, UserCode).ExecuteCommand(); //db1.Insertable(putOnInfo1.ItemsExt, UserCode).ExecuteCommand(); db1.Insertable(putOnInfo1.Packages, UserCode).ExecuteCommand(); } else { db1.Updateable(putOnInfo1.Items, UserCode).ExecuteCommand(); db1.Updateable(putOnInfo1.Packages, UserCode).ExecuteCommand(); } db1.Insertable(putOnInfo1.History, UserCode).ExecuteCommand(); var x = db1.Storageable(putOnInfo1.ItemsExt, UserCode).ToStorage(); x.AsInsertable.ExecuteCommand();//不存在插入 x.AsUpdateable.ExecuteCommand();//存在更新 }); if (!dbTran1.IsSuccess) { throw dbTran1.ErrorException; } // 返回其他入库对象 action.Data = new Model.Minsun.OtherInstockBarcodeOutput { Barcode = res.Data.SN, MaterialCode = res.Data.Items[0].ITEM_CODE, //MaterialName = res.Data.Items[0].ITEM_NAME, //MaterialStandard = res.Data.ItemInfo.SPEC, InitialQty = res.Data.Barcode.Qty ?? 0, CurrentQty = res.Data.Barcode.Qty ?? 0, Unit = res.Data.Items[0].UNIT, DateCode = res.Data.Barcode.ProdDate ?? DateTime.MinValue, LotNo = res.Data.SN, //批次号默认为外包装条码 //SourceBillCode = billCode, LocationCode = nLocation.LOCATION_CODE, //ReceiptCode = barcodeScanned?.BUSINESSCODE, //IncompleteTips = isCompleted ? "数量:0 (0盘)" : iWMS.GetIncompleteTipsNew(barcodeScanned?.BUSINESSCODE, barcodeScanned?.ITEM_CODE), IsScanShelf = false }; action.LocaleMsg = Biz.L("入库成功{0}", res.Data.SN); } //其他入库 if (input.ReqType == 1) { Result result = WMS_ITEM_Biz.WmsItem.Get(input.SN, input.AuthOption, true); if (!result.IsSuccessed) { action.IsSuccessed = false; action.LocaleMsg = result.LocaleMsg; return action; } var inv = result.Data; //验证条码是否正确 if (!inv.isNormalStatus || !new[] { WMS_ITEM.STATUSs.WaitIn }.Contains(inv.Status)) { action.IsSuccessed = false; action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.StatusException", string.Join(',', inv.StatusList.Select(x => x.GetDesc()))); return action; } // 根据物料Barcode 检索清点扫描表 //var barcodeScanned = Biz.Db.Queryable().Where(t => t.SN.ToUpper() == inv.SN.ToUpper()).First(); var barcodeScanneds = Biz.Db.Queryable().Where(t => inv.Items.Select(q => q.SN).Contains(t.SN)).ToList(); if (barcodeScanneds.Count != inv.Items.Count) { action.IsSuccessed = false; action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.NotCounted", inv.SN); // $"此物料条码未做清点,不能入库"; return action; } if (inv.Items.Any(t => t.STATUS != WMS_ITEM.STATUSs.WaitIn.GetValue() || barcodeScanneds.Any(q => q.SN == t.SN && !q.BUSINESSCODE.StartsWith("OI")))) { action.IsSuccessed = false; action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.NotGenerateBill", inv.SN); //$"此物料条码未生成库存单据,不能入库"; return action; } if (barcodeScanneds[0].WAREHOUSECODE.ToUpper() != WarehouseCode.ToUpper()) { action.IsSuccessed = false; action.LocaleMsg = Biz.L($"储位[{CustomerSupplyPuton.LocationCode}]不属于当前据点[{OrgCode}]"); //Biz.L("WMS.CustSupChk.ScanItem.NotGenerateBill", inv.SN); //$"此物料条码清点仓库与库位仓库不同,不能入库"; return action; } if (inv.Items.Any(t => barcodeScanneds.Any(q => q.SN == t.SN && q.QTY != t.QTY))) { action.IsSuccessed = false; action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.DiffQty", inv.SN); //$"此物料条码入库数量与清点数量不同,不能入库"; return action; } // 判断是否智能货架 if (CustomerSupplyPuton.IsSmartRack) { ShelfApiResult shelfApiResult = await Share.Shelf.PutOn(TransID, Shelf, inv.Items[0]); if (!shelfApiResult.IsSuccess) { action.IsSuccessed = false; action.LocaleMsg = Biz.L(shelfApiResult.GetData()); return action; } var reaultShelf = shelfApiResult.GetData(); nLocation = reaultShelf.GetLocation(); if (nLocation == null) { action.IsSuccessed = false; action.LocaleMsg = Biz.L($"系统不存在储位,请先维护货架信息"); return action; } var locationData = Biz.Db.Queryable().Where(q => q.LOCATION_ID == nLocation.ID).First(); if (!locationData.IsNullOrEmpty()) { action.IsSuccessed = false; action.LocaleMsg = Biz.L($"系统当前库位存在料盘,请检查系统库存信息"); return action; } CustomerSupplyPuton.LocationCode = nLocation.LOCATION_CODE; } else { if (CustomerSupplyPuton.LocationCode.IsNullOrEmpty() || CustomerSupplyPuton.WarehouseCode.IsNullOrEmpty()) { action.IsSuccessed = false; action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.LocationEmptyFailure"); //$"请先扫描库位码"; return action; } nLocation = await Biz.Db.Queryable().Where(t => t.LOCATION_CODE == CustomerSupplyPuton.LocationCode).FirstAsync(); } string billCode = barcodeScanneds[0].BUSINESSCODE; //string billLine = barcodeScanned.BUSINESSLINE; foreach (var item in inv.Items) { item.SOURCE_CODE = item.TRANS_CODE; item.SOURCE_LINE = item.TRANS_LINE; item.SOURCE_ORDER = item.TRANS_NO; item.TRANS_CODE = nameof(BIZ_ERP_OTH_IN); item.TRANS_NO = billCode; item.ERP_WH = WarehouseCode; //item.TRANS_LINE = billLine.ToString(); } //执行入库 Result onResult = WMS_ITEM_Biz.WmsItem.PutOn(inv, input.AuthOption, nLocation.LOCATION_CODE); if (!onResult.IsSuccessed) { action.IsSuccessed = false; action.LocaleMsg = onResult.LocaleMsg; return action; } PutOnInfo putOnInfo = onResult.Data; #region 更新单据信息 foreach (var barcodeScanned in barcodeScanneds) { barcodeScanned.STATUS = WMS_ITEM.STATUSs.InStore.GetValue(); } //其他出入库单 //新单据行状态 var billDetail = Biz.Db.Queryable().Where(t => t.BILLCODE.ToUpper() == billCode.ToUpper() && t.ITEM_CODE == inv.ItemInfo.ITEM_CODE).ToList(); var total = Biz.Db.Queryable() .Where(t => t.BUSINESSCODE.ToUpper() == billCode.ToUpper() && t.ITEM_CODE == inv.ItemInfo.ITEM_CODE) .Where(t => t.STATUS == WMS_ITEM.STATUSs.InStore.GetValue() || inv.Items.Select(q => q.SN).Contains(t.SN)) .Sum(x => x.QTY); foreach (var dtl in billDetail) { dtl.WAREHOUSECODE = WarehouseCode; dtl.QTY = total; } var isCompleted = false; // 更新单据HEADER状态 var billHeader = Biz.Db.Queryable().Where(t => t.BILLCODE.ToUpper() == billCode).First(); if (billDetail.Sum(t => t.PRQTY) == total) { foreach (var dtl in billDetail) { dtl.LINESTATUS = BIZ_ERP_OTH_IN.STATUSs.COMPLETE.GetValue(); } if (!Biz.Db.Queryable().Where(t => t.BILLCODE.ToUpper() == billCode.ToUpper() && t.ITEM_CODE != inv.ItemInfo.ITEM_CODE && t.LINESTATUS != BIZ_ERP_OTH_IN.STATUSs.COMPLETE.GetValue()).Any()) { billHeader.STATUS = BIZ_ERP_OTH_IN.STATUSs.COMPLETE.GetValue(); isCompleted = true; } } else { foreach (var dtl in billDetail) { dtl.LINESTATUS = BIZ_ERP_OTH_IN.STATUSs.WORKING.GetValue(); } } Logger.Default.Info($"更新单据明细信息[BIZ_ERP_OTH_IN_DTL]:单号[{billCode}],项次[{string.Join(",", billDetail.Select(q => q.BILLLINE))}],数量[{string.Join(",", billDetail.Select(q => q.QTY))}],状态[{string.Join(",", billDetail.Select(q => q.LINESTATUS))}]"); #endregion //保存到数据库 var db = Business.Biz.Db; var dbTran = db.UseTran(() => { //入库 db.Updateable(putOnInfo.Items, UserCode).ExecuteCommand(); var x = db.Storageable(putOnInfo.ItemsExt, UserCode).ToStorage(); x.AsInsertable.ExecuteCommand();//不存在插入 x.AsUpdateable.ExecuteCommand();//存在更新 db.Insertable(putOnInfo.History, UserCode).ExecuteCommand(); db.Updateable(putOnInfo.Packages, UserCode).ExecuteCommand(); db.Updateable(barcodeScanneds, UserCode).ExecuteCommand(); db.Updateable(billDetail, UserCode).ExecuteCommand(); db.Updateable(billHeader, UserCode).ExecuteCommand(); }); if (!dbTran.IsSuccess) { Logger.Default.Fatal(dbTran.ErrorException, "Database transaction save exception"); this.Close(!dbTran.IsSuccess); throw dbTran.ErrorException; } // 返回其他入库对象 action.Data = new Model.Minsun.OtherInstockBarcodeOutput { Barcode = inv.SN, MaterialCode = inv.ItemInfo.ITEM_CODE, MaterialName = inv.ItemInfo.ITEM_NAME, MaterialStandard = inv.ItemInfo.SPEC, InitialQty = inv.Barcode.Qty ?? 0, CurrentQty = inv.Barcode.Qty ?? 0, Unit = inv.ItemInfo.UNIT, DateCode = inv.Barcode.ProdDate ?? DateTime.MinValue, LotNo = inv.SN, //批次号默认为外包装条码 SourceBillCode = billCode, //SourceBillLine = Convert.ToInt32(billLine), LocationCode = nLocation.LOCATION_CODE, //ReceiptCode = barcodeScanned?.BUSINESSCODE, IncompleteTips = isCompleted ? "数量:0 (0盘)" : iWMS.GetERP_OTH_INTips(billCode, inv.ItemInfo?.ITEM_CODE), IsScanShelf = false }; action.Message = $"其他入库单[{billCode}],条码[{inv.SN}],上架成功"; } } catch (Exception ex) { //取消当前操作 action.CatchExceptionWithLog(ex, $"扫描物料[{input.SN}]复核异常"); } return action; } /// /// 扫描货架或者储位 /// public async Task> ScanShelf(string Code) { var action = new ApiAction(); try { if (Code.IsNullOrEmpty()) { action.IsSuccessed = false; action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.ShelfCanNotEmpty"); return action; } CustomerSupplyPuton = new Model.Minsun.CustomerSupplyPuton(); // 查询货架信息 var whUnit = await Biz.Db.Queryable().Where(t => t.SHELF_CODE.ToUpper() == Code.ToUpper() && t.AUTH_ORG == OrgCode).IncludesAllFirstLayer().FirstAsync(); // 扫描货架代码,且为智能货架 if (whUnit != null && whUnit.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.Smart.GetValue()) { WarehouseName = whUnit.WH_NAME; CustomerSupplyPuton.WarehouseCode = whUnit.WH_CODE; CustomerSupplyPuton.RackCode = whUnit.SHELF_CODE; CustomerSupplyPuton.ShelfType = whUnit.SHELF_TYPE; CustomerSupplyPuton.IsSmartRack = true; Shelf = whUnit.Shelf; } // 扫描库位代码 else { var nLocation = await Biz.Db.Queryable().Where(t => t.LOCATION_CODE.ToUpper() == Code.ToUpper() && t.AUTH_ORG == OrgCode).FirstAsync(); WarehouseName = nLocation.WH_NAME; if (nLocation == null) { action.IsSuccessed = false; action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.NotExist", Code); return action; } if (nLocation.IS_ACTIVE == "N") { action.IsSuccessed = false; action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.ShelfOrLocationDisabled"); return action; } if (nLocation.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.Smart.GetValue() || nLocation.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.QRCode.GetValue()) { var locationData = Biz.Db.Queryable().Where(q => q.LOCATION_ID == nLocation.LOCATION_ID && q.AUTH_ORG == OrgCode).First(); if (!locationData.IsNullOrEmpty()) { action.IsSuccessed = false; action.LocaleMsg = Biz.L($"系统当前库位存在料盘,请检查系统库存信息"); return action; } } CustomerSupplyPuton.LocationCode = nLocation.LOCATION_CODE; CustomerSupplyPuton.WarehouseCode = nLocation.WH_CODE; //CustomerSupplyPuton.Capacity = nLocation.MAXSIZE - nUsedCapacity; //CustomerSupplyPuton.LotCount = nLocation.LOTCOUNT; //CustomerSupplyPuton.MaxSize = nLocation.MAXSIZE; CustomerSupplyPuton.IsSmartRack = false; CustomerSupplyPuton.RackCode = nLocation.SHELF_CODE; } WarehouseCode = CustomerSupplyPuton.WarehouseCode; RackCode = CustomerSupplyPuton.RackCode; CustomerSupplyPuton.IsScanShelf = true; action.LocaleMsg = Biz.L("扫描货架或储位成功"); action.Data = CustomerSupplyPuton; } catch (Exception ex) { //取消当前操作 action.CatchExceptionWithLog(ex, $"扫描货架或者储位[{Code}]异常"); } return action; } #endregion public override bool Close(bool needSaveHistoryLog = false) { this.IsFinished = true; return IsFinished ? base.Close(needSaveHistoryLog) : IsFinished; } }//endClass }