feat: 接入ERP成品发货出库流程

This commit is contained in:
mtf committed 2026-07-18 13:41:54 +08:00
1 parent 66a1f60428
commit 7c2722c88d
25 files changed
+2547 -22

No files matched your search

@@ -40,6 +40,9 @@ namespace HHWMS.Web
this.MakeAction("InventoryTransaction", "RetryAddOtherOut", "补传ERP领料出库", "", GridActionParameterTypesEnum.SingleId, "inventory")
.SetShowDialog(false).SetShowInRow(true).SetHideOnToolBar()
.SetBindVisiableColName(nameof(InventoryTransaction_View.CanRetryOtherOut)),
this.MakeAction("InventoryTransaction", "RetryAddSaleOut", "补传ERP成品出库", "", GridActionParameterTypesEnum.SingleId, "inventory")
.SetShowDialog(false).SetShowInRow(true).SetHideOnToolBar()
.SetBindVisiableColName(nameof(InventoryTransaction_View.CanRetrySaleOut)),
};
}
@@ -75,17 +78,24 @@ namespace HHWMS.Web
this.MakeGridHeader(x => x.ExpirationDate).SetHide(),
this.MakeGridHeader(x => x.CompanyCode),
this.MakeGridHeader(x => x.SupplierCode),
this.MakeGridHeader(x => x.BillType).SetHide(),
this.MakeGridHeader(x => x.SourceCode).SetHide(),
this.MakeGridHeader(x => x.BillCode).SetHide(),
this.MakeGridHeader(x => x.BillType)
.SetFormat((a, b) => a.BillType == InventoryTransaction.ErpOtherOutBillType
? "ERP领料出库"
: a.BillType == InventoryTransaction.ErpSaleOutBillType
? "ERP成品出库"
: a.BillType),
this.MakeGridHeader(x => x.SourceCode),
this.MakeGridHeader(x => x.BillCode),
this.MakeGridHeader(x => x.BillDetailId).SetHide(),
this.MakeGridHeader(x => x.Status).SetHide(),
this.MakeGridHeader(x => x.UploadErpFlag)
.SetFormat((a, b) => a.BillType != InventoryTransaction.ErpOtherOutBillType
&& a.BillType != InventoryTransaction.ErpSaleOutBillType
? "无需回传"
: a.UploadErpFlag == 1 ? "成功" : "失败"),
this.MakeGridHeader(x => x.CanRetryOtherOut).SetHide(),
this.MakeGridHeaderAction(width: 200)
this.MakeGridHeader(x => x.CanRetrySaleOut).SetHide(),
this.MakeGridHeaderAction(width: 260)
};
}
@@ -136,6 +146,9 @@ namespace HHWMS.Web
CanRetryOtherOut = x.Type == InventoryTranType.出库
&& x.BillType == InventoryTransaction.ErpOtherOutBillType
&& x.UploadErpFlag != 1 ? "true" : "false",
CanRetrySaleOut = x.Type == InventoryTranType.出库
&& x.BillType == InventoryTransaction.ErpSaleOutBillType
&& x.UploadErpFlag != 1 ? "true" : "false",
CreateTime = x.CreateTime,
})
.OrderBy(x => x.ID);
@@ -146,5 +159,6 @@ namespace HHWMS.Web
public class InventoryTransaction_View : InventoryTransaction
{
public string CanRetryOtherOut { get; set; }
public string CanRetrySaleOut { get; set; }
}
}
@@ -225,5 +225,139 @@ namespace HHWMS.Web
}
return success;
}
/// <summary>
/// 回传ERP成品出库。同一BillCode代表同一次人工确认、同一张ERP发货通知单。
/// 补传时按既定规则重新生成CK单号,但不会再次扣减WMS库存。
/// </summary>
public async Task<bool> ProcessErpSaleOut(List<InventoryTransaction> transactions,
bool regenerateOutCode = true)
{
transactions = transactions?
.Where(x => x.Type == InventoryTranType.出库
&& x.BillType == InventoryTransaction.ErpSaleOutBillType)
.ToList();
if (transactions == null || transactions.Count == 0)
{
return false;
}
try
{
var detailIds = transactions.Where(x => x.BillDetailId.HasValue)
.Select(x => x.BillDetailId.Value)
.Distinct()
.ToList();
var shipmentDetails = unitWork.Find<ShipmentDetail>(x => detailIds.Contains(x.ID))
.ToDictionary(x => x.ID);
if (shipmentDetails.Count != detailIds.Count
|| shipmentDetails.Values.Any(x => !x.DLsID.HasValue))
{
throw new InvalidOperationException("成品出库交易缺少ERP发货明细ID");
}
var createBy = transactions.First().CreateBy;
var maker = unitWork.FindSingle<FrameworkUser>(x => x.ITCode == createBy)?.Name
?? createBy
?? "WMS";
var outCode = transactions.First().BillCode;
if (regenerateOutCode || string.IsNullOrWhiteSpace(outCode)
|| outCode.StartsWith("SALE-", StringComparison.OrdinalIgnoreCase))
{
outCode = unitWork.GetSerialNumber("CK");
}
foreach (var transaction in transactions)
{
transaction.BillCode = outCode;
unitWork.Update(transaction);
}
var request = new AddSaleOutRequest
{
Main = new AddSaleOutMain
{
ErpCode = "003",
OutCode = outCode,
WhCode = "cp",
Maker = maker
},
Details = transactions
.GroupBy(x => new
{
x.BillDetailId,
x.MaterialCode
})
.Select(group => new AddSaleOutDetail
{
DLsID = shipmentDetails[group.Key.BillDetailId.Value].DLsID.Value,
InvCode = group.Key.MaterialCode,
OutQty = group.Sum(x => x.Qty ?? 0)
})
.ToList()
};
var apiResult = await Wtm.CallAPI<FinishInResponse>("ERP", "AddSaleOut",
HttpMethodEnum.POST, request);
var success = apiResult?.Data != null && apiResult.Data.Result;
foreach (var transaction in transactions)
{
transaction.UploadErpFlag = success ? 1 : 0;
unitWork.Update(transaction);
}
if (success)
{
UpdateSaleOutShipmentStatus(shipmentDetails.Values.Select(x => x.HeaderId).Distinct().ToList());
}
else
{
Wtm.DoLog("ERP成品出库回传失败", ActionLogTypesEnum.Normal, "AddSaleOut",
request: outCode, result: apiResult?.Data?.Message ?? apiResult?.ErrorMsg ?? "ERP返回失败");
}
return success;
}
catch (Exception ex)
{
foreach (var transaction in transactions)
{
transaction.UploadErpFlag = 0;
unitWork.Update(transaction);
}
Wtm.DoLog("ERP成品出库回传异常", ActionLogTypesEnum.Normal, "AddSaleOut",
request: transactions.First().BillCode, result: ex.Message);
return false;
}
}
private void UpdateSaleOutShipmentStatus(List<int> shipmentHeaderIds)
{
foreach (var headerId in shipmentHeaderIds)
{
var header = unitWork.FindSingle<ShipmentHeader>(x => x.ID == headerId);
if (header == null)
{
continue;
}
var details = unitWork.Find<ShipmentDetail>(x => x.HeaderId == headerId).ToList();
var detailIds = details.Select(x => x.ID).ToList();
var hasPendingUpload = unitWork.IsExit<InventoryTransaction>(x =>
x.BillType == InventoryTransaction.ErpSaleOutBillType
&& x.BillDetailId.HasValue
&& detailIds.Contains(x.BillDetailId.Value)
&& x.UploadErpFlag != 1);
if (details.Any()
&& details.All(x => (x.CompletedQty ?? 0) >= (x.Qty ?? 0))
&& !hasPendingUpload)
{
header.FirstStatus = ShipStatusEnum.回传;
header.LastStatus = ShipStatusEnum.回传;
header.UpdateBy = "ERP";
header.UpdateTime = DateTime.Now;
unitWork.Update(header);
}
}
}
}
}