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;
namespace Tiger.Business.WMS.Transaction
{
///
/// 客供料入库清点事务
///
public class OutNoBillcode : WMSTransactionBase, IOutNoBillcode
{
public IOutNoBillcode Init(string id, string userCode, string apiHost, string orgCode)
{
TransID = id;
UserCode = userCode;
ApiHost = apiHost;
OrgCode = orgCode;
Result ruleResult = Cache.CodeRule["NB001"].Generate("NB"); //iWMS.GetBillNoByType("CI");
if (!ruleResult.IsSuccessed)
{
throw new Exception(ruleResult.ExceptionMsg.Message);
}
BillCode = ruleResult.Data.ToString();
NoBillcodeHeader = new()
{
CREATE_TIME = DateTime.Now,
CREATE_USER = userCode,
UPDATE_USER = userCode,
BILLCODE = BillCode,
AUTH_ORG = orgCode,
STATUS = BIZ_ERP_OTH_OUT.STATUSs.WORKING.GetValue()
};
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 BillCode { get; set; }
public Inventory CurInv { get; set; }
public BIZ_ERP_OTH_OUT NoBillcodeHeader { get; set; }
public List NoBillcodeDtl { get; set; }//单据明细
//public List NoBillcodeSN { 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(BaseInput input)
{
var action = new ApiAction();
try
{
if (input.SN.IsNullOrEmpty())
{
action.IsSuccessed = false;
action.LocaleMsg = Biz.L("条码不能为空");
return action;
}
//解析条码
Result result = WMS_ITEM_Biz.WmsItem.Get(input.SN, input.AuthOption, true);
if (!result.IsSuccessed)
{
action.IsSuccessed = false;
action.LocaleMsg = result.LocaleMsg;
return action;
}
CurInv = result.Data as Inventory;
//验证条码是否正确
if (!CurInv.isNormalStatus || CurInv.Status != WMS_ITEM.STATUSs.InStore)
{
action.IsSuccessed = false;
action.LocaleMsg = Biz.L("WMS.ProdMReq.ScanItem.StatusException", string.Join(',', CurInv.StatusList.Select(x => x.GetDesc())));
return action;
}
//储位验证
if (CurInv.Location.IsNullOrEmpty())
{
action.IsSuccessed = false;
action.LocaleMsg = Biz.L("WMS.ProdMReq.ScanItem.LocationIsNull", CurInv.CurPkg.SN, CurInv.CurPkg.LOCATION_ID);
return action;
}
//物料验证
if (CurInv.ItemInfo.IsNullOrEmpty() || CurInv.ItemInfo.IS_ACTIVE == "N")
{
action.IsSuccessed = false;
action.LocaleMsg = Biz.L("WMS.ProdMReq.ScanItem.ItemCodeNotExistsOrNotActive", CurInv.ItemInfo.ITEM_CODE.IsNullOrEmpty(CurInv.Barcode.ItemCode));
return action;
}
//创建单据
NoBillcodeHeader.STATUS = BIZ_ERP_OTH_OUT.STATUSs.COMPLETE.GetValue();//扫完单据就为完成
BIZ_ERP_OTH_OUT_SN NoBillcodeSn = new()
{
CREATE_USER = input.AuthOption.UserId,
CREATE_TIME = DateTime.Now,
AUTH_ORG = input.AuthOption.OrgCode,
ORDER_NO = BillCode,
SN = CurInv.SN,
META_SN = CurInv.Barcode.MetaSn,
ITEM_CODE = CurInv.ItemInfo.ITEM_CODE,
STATUS = WMS_ITEM.STATUSs.OffShelf.GetValue(),
LOCATION_CODE=CurInv.Location.LOCATION_CODE,
ERP_WH=CurInv.Warehouse.WH_CODE,
ALLOC_QTY=CurInv.CurPkg.QTY,
LOAD_SEQ=0,
NEED_CUTTING="N"
};
NoBillcodeDtl = new List();
//首次直接新增数据
if (NoBillcodeDtl.Count > 0)
{
//查询这个料号是否存在明细,存在就修改数量,不存在添加一条新数据
var noBillcode = NoBillcodeDtl.Where(q => q.ITEM_CODE == CurInv.ItemInfo.ITEM_CODE).FirstOrDefault();
if (noBillcode.IsNullOrEmpty())
{
BIZ_ERP_OTH_OUT_DTL dtl = new()
{
CREATE_USER = input.AuthOption.UserId,
CREATE_TIME = DateTime.Now,
AUTH_ORG = input.AuthOption.OrgCode,
BILLCODE = BillCode,
BILLLINE=(int.Parse(NoBillcodeDtl.Max(q => q.BILLLINE))+1).ToString(),
LINESTATUS=BIZ_ERP_OTH_OUT.STATUSs.COMPLETE.GetValue(),
ITEM_CODE=CurInv.ItemInfo.ITEM_CODE,
UNITCODE=CurInv.CurPkg.UNIT,
PRQTY=CurInv.CurPkg.QTY,
QTY=CurInv.CurPkg.QTY,
WAREHOUSECODE=CurInv.Warehouse.WH_CODE
};
NoBillcodeDtl.Add(dtl);
}
else
{
noBillcode.QTY += CurInv.CurPkg.QTY;
}
}
else
{
BIZ_ERP_OTH_OUT_DTL dtl = new()
{
CREATE_USER = input.AuthOption.UserId,
CREATE_TIME = DateTime.Now,
AUTH_ORG = input.AuthOption.OrgCode,
BILLCODE = BillCode,
BILLLINE = "1",
LINESTATUS = BIZ_ERP_OTH_OUT.STATUSs.COMPLETE.GetValue(),
ITEM_CODE = CurInv.ItemInfo.ITEM_CODE,
UNITCODE = CurInv.CurPkg.UNIT,
PRQTY = CurInv.CurPkg.QTY,
QTY = CurInv.CurPkg.QTY,
WAREHOUSECODE = CurInv.Warehouse.WH_CODE
};
NoBillcodeDtl.Add(dtl);
}
//处理库存和包装
CurInv.CurPkg.REGION_ID = "";
CurInv.CurPkg.SHELF_ID = "";
CurInv.CurPkg.LOCATION_ID = "";
CurInv.Items[0].REGION_ID = "";
CurInv.Items[0].SHELF_ID = "";
CurInv.Items[0].LOCATION_ID = "";
CurInv.Items[0].TRANS_CODE = "No Billcode Out";
CurInv.Items[0].TRANS_NO = BillCode;
CurInv.Items[0].STATUS = WMS_ITEM.STATUSs.OffShelf.GetValue();
WMS_ITEM_HIS his = new WMS_ITEM_HIS(CurInv.Items[0], $"无工单出库");
CurInv.History.Add(his);
//灭灯
if (CurInv.Shelf.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.Smart.GetValue() || CurInv.Shelf.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.QRCode.GetValue())
{
await Share.Shelf.DownSingle(TransID, CurInv.Location);
}
action.Message = "无工单出库成功";
//保存数据库
var db = Business.Biz.Db;
var dbTran = db.UseTran(() =>
{
//出库
db.Updateable(CurInv.Items, UserCode).ExecuteCommand();
db.Updateable(CurInv.CurPkg, UserCode).ExecuteCommand();
db.Insertable(CurInv.History, UserCode).ExecuteCommand();
db.Insertable(NoBillcodeHeader, UserCode).ExecuteCommand();
db.Insertable(NoBillcodeDtl, UserCode).ExecuteCommand();
db.Insertable(NoBillcodeSn, UserCode).ExecuteCommand();
});
if (!dbTran.IsSuccess)
{
throw dbTran.ErrorException;
}
action.Data = new DefaultScanItemOutput()
{
SN = CurInv.SN,
ItemCode = CurInv.ItemInfo.ITEM_CODE,
MaterialName=CurInv.ItemInfo.ITEM_NAME,
Qty = CurInv.CurPkg.QTY,
//CutQty = CurInv.CurPkg.QTY - cutQty,
//isCutting = _isCutting,
//isExceed = _isExceed,
ReqNo = BillCode,
Unit=CurInv.CurPkg.UNIT,
regionCode = CurInv.Region.REGION_CODE,
locationCode = CurInv.Location?.LOCATION_CODE,
DateCode = CurInv.Items[0].PROD_DATE,
ScanAfCut = CurInv.Warehouse.SCAN_AF_CUT
};
}
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
}