using Apache.NMS.ActiveMQ.Commands;
using Rhea.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Tiger.IBusiness;
using Tiger.Model;
using Tiger.Model.Minsun;
namespace Tiger.Business.WMS
{
public class ReceiptInfo : IReceiptInfo
{
///
/// T100调用WMS返回收货单信息
///
///
///
public ApiAction GetReceiptInfo(iReceiptParam input)
{
ApiAction response = new ApiAction();
try
{
DbClient db = Business.Biz.Db;
switch (input.StrongHoid)
{
case "TXXC":
case "XCTC":
//db = Biz.DataSource["Main"].Client;
//break;
case "XCSJ":
case "DGXC":
//db = Biz.DataSource["DGXC"].Client;
//break;
case "AHXC":
db = Biz.DataSource["WMS57"].Client;
break;
}
List list = new List();
var entity_h = db.Queryable().Where(x => x.DELIVERY_NO == input.DeliveryNo).IncludesAllFirstLayer().First();
if (entity_h == null)
{
response.IsSuccessed = false;
response.Message = $"送货单[{input.DeliveryNo}]在WMS系统中查不到记录!";
return response;
}
if (entity_h.STATUS == 4)
{
response.IsSuccessed = false;
response.Message = $"送货单[{input.DeliveryNo}]状态为作废,不能收货!";
return response;
}
if (entity_h != null)
{
var _vender = entity_h.SUPP_CODE;
foreach (var item in entity_h.Dtls)
{
var pmdt004 = item.BATCH_NUMBER; // (item.poLineNo ?? "1").Split('-').Length > 2 ? (item.poLineNo ?? "1").Split('-')[2] : (item.poLineNo ?? "1");
var receiptInfo = new iReceiptInfo
{
BillCode = item.ERP_PO_NO,
lineNumber = item.PO_LINE_NO.ToString(), //!string.IsNullOrEmpty(item.extendN01) ? item.extendN01 : item.lineNo.ToString(),
SupplierCode = _vender,
ItemNumber = item.LINE_NO.ToString(), //!string.IsNullOrEmpty(item.extendN01) ? item.lineNo.ToString() : (item.poLineNo ?? "1").Split('-')[0], 采购单项次
SeqNumber = "1",
BatchOrder = pmdt004.ToString(),
ItemCode = item.ITEM_CODE,
Qty = item.QTY.ToString()
};
list.Add(receiptInfo);
}
}
response.Data = list;
response.Message = $"送货单[{input.DeliveryNo}]明细信息获取成功";
}
catch (Exception ex)
{
response.GetResponse().CatchExceptionWithLog(ex);
}
return response;
}
///
/// 到货单生成接口 清点系统 --> wms,新数据库
///
///
///
public ApiAction GenerateReceipt(iReceiptInput input)
{
var action = new ApiAction();
try
{
DbClient db = Biz.DataSource["WMS57"].Client;
//待提交数据按来源单号分组(ASN/PO)
var dlvys = input.iReceipts
.Select(p => p.DeliveryNo)
.Distinct()
.ToList();
List receiptHeaders = new List();
List detailList = new List();
List receiptScans = new List();
List details = new List();
List iqcHeaders = new List();
List ItemHistorys = new List();
List ItemPkgs = new List();
List items = db.Queryable().Where(x => dlvys.Contains(x.TRANS_NO)).ToList();
bool isIQCOk = true;
string OrgCode = input.StrongHold;
var usercode = "00000";
switch (input.CompanyCode)
{
case "XCQX":
usercode = "LX0001";
break;
case "XCHF":
usercode = "AA0001";
break;
case "XCTH":
//usercode = "AA0001";
break;
case "XCTX":
usercode = "TA0001";
break;
}
//所有送货单
var srmDlvys = db.Queryable().Where(x => dlvys.Contains(x.DELIVERY_NO)).IncludesAllFirstLayer().ToList();
if (srmDlvys == null)
{
action.IsSuccessed = false;
action.LocaleMsg = Biz.L($"SRM中查不到明细");
return action;
}
foreach (var srmDlvy in srmDlvys.OrderBy(o => o.DELIVERY_NO))
{
//Logger.Interface.Info($"送货单[{srcBill.Key}]总数[{asnBarcodeList.Count}],扫描总数[{input.iReceipts.Where(x => x.DeliveryNo == srcBill.Key).ToList().Count}]");
// 生成收料单
//新建单据
Result ruleResult = Biz.CodeRule["R001"].Generate("RE");
if (!ruleResult.IsSuccessed)
{
throw new Exception(ruleResult.ExceptionMsg.Message);
}
var billcode = ruleResult.Data.ToString();
var nBillHeader = new BIZ_ERP_RECEIPT
{
BILLCODE = billcode,
BILLDATE = DateTime.Now.Date,
//WAREHOUSECODE = WarehouseCode,
STATUS = BIZ_ERP_RECEIPT.STATUSs.WORKING.GetValue(),
//SUPPLIERCODE = nScannedBarcodes[0].SUPPLIERCODE,
AUTH_ORG = OrgCode
};
var supplyCode = "";
//创建收货单明细
nBillHeader.SUPPLIERCODE = srmDlvy.SUPP_CODE;
foreach (var item in srmDlvy.Dtls.OrderBy(o => o.PO_LINE_NO))
{
//供应商代码
supplyCode = srmDlvy.SUPP_CODE;
//插入单据明细行
if (!detailList.Where(x => x.BILLLINE == item.PO_LINE_NO.ToString()).Any())
{
detailList.Add(new BIZ_ERP_RECEIPT_DTL
{
//WAREHOUSECODE = WarehouseCode,
BILLCODE = billcode,
BILLID = Guid.NewGuid().ToString("N"),
BILLLINE = item.PO_LINE_NO.ToString(),
LINESTATUS = BIZ_ERP_RECEIPT.STATUSs.WORKING.GetValue(),
ITEM_CODE = item.ITEM_CODE,
UNITCODE = item.UNIT,
QTYPASS = 0,
INSTOCKQTY = 0,
RETURNQTY = 0,
PRINTQTY = 0,
ISGIVEAWAY = false,
SOURCETYPE = nameof(BIZ_SRM_DLVY),
SOURCECODE = item.DELIVERY_NO,
SOURCELINE = item.PO_LINE_NO.ToString(),
AUTH_ORG = OrgCode,
QTY = item.QTY
});
Result iqcResult = Biz.CodeRule["IQC001"].Generate("IQC");
if (!iqcResult.IsSuccessed)
{
throw new Exception(iqcResult.ExceptionMsg.Message);
}
iqcHeaders.Add(new BIZ_ERP_IQC
{
BILLCODE = iqcResult.Data.ToString(),
SOURCETYPE = (int)BIZ_ERP_IQC.BillType.CustSupplyInStock,
SOURCECODE = billcode,
SOURCELINE = item.PO_LINE_NO.ToString(),
PONO = item.ERP_PO_NO,
POLINE = item.LINE_NO.ToString(),
MEASURE = "Y",
AUTH_ORG = OrgCode,
QCTIMES = 0,
QCSTATUS = (int)BIZ_ERP_IQC.QCSTATUSs.Finished, /*(int)IqcManagementSides.QCStatus.UnFinish,*/
QCRESULT = (int)BIZ_ERP_IQC.QCRESULTs.Wait,
QCREVIEW = (int)BIZ_ERP_IQC.QCReview.UnReview,
QCLEVEL = 11,
ITEM_CODE = item.ITEM_CODE,
RECEIVEQTY = item.QTY
//ReceiveQty = detail.Qty,
});
}
//到货明细
if (!details.Where(x => x.pmdtseq == item.PO_LINE_NO.ToString()).Any())
{
details.Add(new Detail
{
pmdtseq = item.PO_LINE_NO.ToString(),
pmdt001 = item.ERP_PO_NO, //lineNumber
pmdt002 = item.LINE_NO.ToString(), //itemNumber
pmdt003 = "1",
pmdt004 = item.BATCH_NUMBER,
pmdt006 = item.ITEM_CODE, //"K7015AA016141",
//pmdt018 = "创米科技",
pmdt020 = item.QTY.ToString(), //"100.00000",
pmdtud011 = "0"
});
}
}
//更新SN单号和状态
foreach (var scanTemp in input.iReceipts.Where(x => x.DeliveryNo == srmDlvy.DELIVERY_NO).ToList())
{
BIZ_ERP_RECEIPT_SN scanObj = new BIZ_ERP_RECEIPT_SN
{
BILLCODE = billcode,
BILLLINE = "SHReceipt",
SOURCETYPE = (int)WMS_RECEIPT_H.SourceType.Asn,
ITEM_CODE = scanTemp.MaterialCode,
SN = scanTemp.SmallBarcode,
META_SN = scanTemp.SCAN_BARCODE,
CARTON = scanTemp.BigBarcode,
PALLET = scanTemp.OuterBarcode,
SUPPLIERCODE = scanTemp.innerVendorCode,
DATECODE = DateTime.Now,
SCANQTY = scanTemp.SCANQTY,
STATUS = WMS_ITEM.STATUSs.WaitTest.GetValue(),
AUTH_ORG = OrgCode,
DELIVERY_NO = srmDlvy.DELIVERY_NO, //送货单据
};
receiptScans.Add(scanObj);
}
//更新库存表
foreach (var wi in items.Where(x => x.TRANS_NO == srmDlvy.DELIVERY_NO))
{
wi.STATUS = WMS_ITEM.STATUSs.WaitTest.GetValue();
wi.TRANS_CODE = nameof(BIZ_ERP_RECEIPT);
wi.TRANS_NO = billcode;
wi.SOURCE_CODE = nameof(srmDlvy);
wi.SOURCE_ORDER = srmDlvy.DELIVERY_NO;
wi.AUTH_ORG = OrgCode;
WMS_ITEM_HIS his = new WMS_ITEM_HIS(wi, $"自购来料清点,到货单[{billcode}],送货单[{srmDlvy.DELIVERY_NO}]");
ItemHistorys.Add(his);
}
//调用T100接口生成到货单
var ret = ErpT100.GenerateCustReceipt(new CustReceiptInputParameter
{
parameter = new Parameter
{
pmds000 = "1",
pmdsdocno = "3438",
pmdsdocdt = DateTime.Now.ToString("yyyy-MM-dd"), //"2023-04-26", //当天时间
pmds002 = usercode, //用户ID
pmds003 = "",
pmds007 = supplyCode, //"C0006", //供应商代码
pmds010 = srmDlvy.DELIVERY_NO, //"测试客供料无采购收货", //送货单号
//pmdsud001 = SecondVenderCode, //"CM017", //二级供应编号
detail = details
},
datakey = new Datakey
{
EntId = "88",
CompanyId = OrgCode
}
});
if (ret.payload.std_data.execution.code == "0") //成功
{
nBillHeader.ERP_BILL_CODE = ret.payload.std_data.parameter.docno;
action.Message += $"{action.Message.IsNullOrEmpty("", ";")}{ret.payload.std_data.execution.description},返回的收货单号:[{ret.payload.std_data.parameter.docno}]";
#region 生成送检单
foreach (var item in iqcHeaders)
{
var retIns = ErpT100.GenerateInspection(new InsInputParameter
{
parameter = new InsParameter
{
pmdsdocno = ret.payload.std_data.parameter.docno,
pmdsdocdt = DateTime.Now.ToString("yyyy-MM-dd"),
pmdtseq = item.SOURCELINE.ToString(),
pmdt020 = item.RECEIVEQTY.ToString(),
qcbadocno = "3701",
},
datakey = new Datakey
{
EntId = "88",
CompanyId = OrgCode
}
});
isIQCOk &= retIns.payload.std_data.execution.code == "0";
if (retIns.payload.std_data.execution.code != "0")
{
action.IsSuccessed = false;
action.Message = $"返回到货单[{ret.payload.std_data.parameter.docno}],当前用户[{usercode}],送货单号[{srmDlvy.DELIVERY_NO}],到货单号[{billcode}],返回的信息:{retIns.payload.std_data.execution.description}"; ;
}
else
{
var receipDtl = detailList.Where(q => q.BILLCODE == item.SOURCECODE && q.BILLLINE == item.SOURCELINE).FirstOrDefault();
if (!receipDtl.IsNullOrEmpty())
{
receipDtl.ERP_BILL_CODE = retIns.payload.std_data.parameter.docno;
}
item.ERP_BILL_CODE = retIns.payload.std_data.parameter.docno;
action.Message = $"到货单[{ret.payload.std_data.parameter.docno}],当前用户[{usercode}],返回的信息:{retIns.payload.std_data.execution.description}"; ;
}
}
receiptHeaders.Add(nBillHeader);
#endregion 生成送检单
}
else //失败
{
action.IsSuccessed = false;
action.Message = $"当前用户[{usercode}],送货单号[{srmDlvy.DELIVERY_NO}],到货单号[{billcode}],返回的信息:{ret.payload.std_data.execution.description}";
return action;
}
}
db = Biz.DataSource["WMS57"].Client;
//保存到数据库
var dbTran = db.UseTran(() =>
{
if (receiptHeaders.Any())
{
db.Insertable(receiptHeaders, usercode).ExecuteCommand();
}
if (detailList.Any())
{
db.Insertable(detailList, usercode).ExecuteCommand();
}
if (receiptScans.Any())
{
db.Insertable(receiptScans, usercode).ExecuteCommand();
}
if (action.IsSuccessed && isIQCOk)
{
db.Insertable(iqcHeaders).ExecuteCommand();
}
db.Updateable(items, usercode).UpdateColumns(x => new { x.UPDATE_USER, x.UPDATE_TIME, x.STATUS, x.TRANS_CODE, x.TRANS_NO, x.TRANS_LINE, x.SOURCE_CODE, x.SOURCE_ORDER }).ExecuteCommand();
db.Insertable(ItemHistorys, usercode).ExecuteCommand();
});
if (!dbTran.IsSuccess)
{
action.GetResponse().CatchException(dbTran.ErrorException, $"到货单生成异常");
}
action.LocaleMsg = Biz.L($"到货单和送检单生成成功!");
return action;
}
catch (Exception ex)
{
return action.GetResponse().CatchException(ex, $"到货单生成异常");
}
}
}
}