opt:结构件仓库领料回传sap分计划内计划外

This commit is contained in:
zhaoyf
2026-10-30 15:08:14 +08:00
parent 6d989e8213
commit 1be61ecee9

View File

@@ -39,10 +39,10 @@ import java.util.List;
/**
* 工单领料出库(LLCK)回传 SAP PP055 生产投料。
* <p>
* 与 EAS 版 {@code WorkOrderLLCKCallback} 的取数口径保持一致:
* 以出库明细下已完成分配明细的累计数量为准,按来源工单/BOM份额拆分增量,
* 同步累计 BOM 已领数量,再逐条组装 PP055 报文回传 SAP。
* 回传结果写入 pm_stock_return,失败记录可通过回传记录页面手动重发。
* 数量口径:出库明细 {@code plan_qty} 为托盘实际出库数量,来源工单BOM的 {@code bom_qty} 为计划数量;
* 实际出库数量超出BOM计划数量的部分按「计划外领料」回传(不带预留号/行项目)。
* 按来源工单/BOM顺序拆分,同步累计BOM已领数量,回传结果写入 pm_stock_return,
* 失败记录可通过回传记录页面手动重发。
*/
@Slf4j
@Service("sapWorkOrderLLCKCallback")
@@ -68,15 +68,17 @@ public class SapWorkOrderLLCKCallback implements CallbackStrategy {
if (detail == null || StrUtil.isBlank(detail.getIostorinvdtl_id())) {
throw new BadRequestException("工单领料回传失败:出库明细为空");
}
// 已完成分配明细的累计数量,与明细已回传数量求差得到本次增量
// 已完成分配明细(用于兜底取数与回传批次标识)
List<IOStorInvDis> completed = distributions.selectList(new LambdaQueryWrapper<IOStorInvDis>()
.eq(IOStorInvDis::getIostorinvdtl_id, detail.getIostorinvdtl_id())
.eq(IOStorInvDis::getWork_status, IOSEnum.INBILL_DIS_STATUS.code("完成")));
BigDecimal total = completed.stream().map(IOStorInvDis::getPlan_qty)
// 实际出库数量以出库明细 plan_qty 为准(已完成分配明细累计作为兜底)
BigDecimal completedQty = completed.stream().map(IOStorInvDis::getPlan_qty)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal actual = detail.getPlan_qty() != null && detail.getPlan_qty().signum() > 0
? detail.getPlan_qty() : completedQty;
BigDecimal reported = detail.getReal_qty() == null ? BigDecimal.ZERO : detail.getReal_qty();
BigDecimal qty = total.subtract(reported);
if (qty.signum() <= 0) {
if (actual.subtract(reported).signum() <= 0) {
return;
}
// 同一明细同一完成批次只回传一次,成功过账后不再重复发送
@@ -84,7 +86,7 @@ public class SapWorkOrderLLCKCallback implements CallbackStrategy {
PmStockReturn exist = returns.getOne(new LambdaQueryWrapper<PmStockReturn>()
.eq(PmStockReturn::getRequest_Id, requestId).last("limit 1"));
if (exist != null && StockReturnStatusEnum.SUCESS.getCode().equals(exist.getStatus())) {
detail.setReal_qty(total);
detail.setReal_qty(actual);
return;
}
IOStorInv bill = bills.selectById(detail.getIostorinv_id());
@@ -100,32 +102,45 @@ public class SapWorkOrderLLCKCallback implements CallbackStrategy {
BigDecimal offset = BigDecimal.ZERO;
for (int i = 0; i < sources.size(); i++) {
JSONObject source = sources.getJSONObject(i);
BigDecimal sourceQty = source.getBigDecimal("qty") == null ? BigDecimal.ZERO : source.getBigDecimal("qty");
BigDecimal end = offset.add(sourceQty);
// 顺序归属来源份额,超计划整托数量归到最后一个来源BOM
BigDecimal current = i == sources.size() - 1
? total.subtract(offset).max(BigDecimal.ZERO)
: total.min(end).subtract(offset).max(BigDecimal.ZERO);
BigDecimal previous = i == sources.size() - 1
? reported.subtract(offset).max(BigDecimal.ZERO)
: reported.min(end).subtract(offset).max(BigDecimal.ZERO);
BigDecimal delta = current.subtract(previous);
offset = end;
if (delta.signum() <= 0) {
continue;
}
WorkOrderBomDao bom = boms.selectOne(new LambdaQueryWrapper<WorkOrderBomDao>()
.eq(WorkOrderBomDao::getId, source.getString("bom_id")).last("for update"));
if (bom == null) {
throw new BadRequestException("来源BOM已变更,无法累计领料数量");
}
bodyList.add(buildBody(docno, today, bill, detail, source, bom, delta));
// 累计工单BOM已领数量,与EAS口径一致
// 计划数量取工单BOM的 bom_qty;超出计划的部分顺序归到最后一个来源,按计划外领料处理
BigDecimal planned = bom.getBom_qty() == null ? BigDecimal.ZERO : bom.getBom_qty();
boolean last = i == sources.size() - 1;
BigDecimal end = offset.add(planned);
BigDecimal current = last
? actual.subtract(offset).max(BigDecimal.ZERO)
: actual.min(end).subtract(offset).max(BigDecimal.ZERO);
BigDecimal previous = last
? reported.subtract(offset).max(BigDecimal.ZERO)
: reported.min(end).subtract(offset).max(BigDecimal.ZERO);
BigDecimal delta = current.subtract(previous);
BigDecimal planDelta = current.min(planned).subtract(previous.min(planned)).max(BigDecimal.ZERO);
BigDecimal unplannedDelta = delta.subtract(planDelta);
offset = end;
if (delta.signum() <= 0) {
continue;
}
// 计划内:带预留号/行号;计划外:不带预留信息
if (planDelta.signum() > 0) {
if (StrUtil.isBlank(bom.getRsnum()) || StrUtil.isBlank(bom.getRspos())) {
log.warn("工单{}物料{}计划内领料缺少预留号/行号(rsnum={}, rspos={}),SAP可能拒收",
source.getString("order_code"), detail.getMaterial_code(), bom.getRsnum(), bom.getRspos());
}
bodyList.add(buildBody(docno, today, bill, detail, source, bom, planDelta, true));
}
if (unplannedDelta.signum() > 0) {
bodyList.add(buildBody(docno, today, bill, detail, source, bom, unplannedDelta, false));
}
// 累计工单BOM已领数量(含超领部分)
bom.setUse_bom_qty((bom.getUse_bom_qty() == null ? BigDecimal.ZERO : bom.getUse_bom_qty()).add(delta));
boms.updateById(bom);
}
if (bodyList.isEmpty()) {
detail.setReal_qty(total);
detail.setReal_qty(actual);
return;
}
@@ -164,11 +179,16 @@ public class SapWorkOrderLLCKCallback implements CallbackStrategy {
} else {
returns.updateById(record);
}
detail.setReal_qty(total);
detail.setReal_qty(actual);
}
/**
* 组装 PP055 报文体
*
* @param planned true=计划内领料(带预留号/行号);false=计划外领料(预留号/行号留空)
*/
private ProductionOutboundBody buildBody(String docno, String today, IOStorInv bill, IOStorInvDtl detail,
JSONObject source, WorkOrderBomDao bom, BigDecimal delta) {
JSONObject source, WorkOrderBomDao bom, BigDecimal qty, boolean planned) {
ProductionOutboundBody body = new ProductionOutboundBody();
body.setDocno(docno);
body.setBudat(today);
@@ -177,9 +197,15 @@ public class SapWorkOrderLLCKCallback implements CallbackStrategy {
body.setWerks(SapConstant.WERKS);
body.setLgort(bill.getStor_code());
body.setAufnr(source.getString("order_code"));
body.setErfmg(delta.doubleValue());
body.setRsnum(StrUtil.blankToDefault(bom.getRsnum(), ""));
body.setRspos(StrUtil.blankToDefault(bom.getRspos(), ""));
body.setErfmg(qty.doubleValue());
if (planned) {
body.setRsnum(StrUtil.blankToDefault(bom.getRsnum(), ""));
body.setRspos(StrUtil.blankToDefault(bom.getRspos(), ""));
} else {
// 计划外领料:预留号与行项目不填(fastjson 默认忽略 null,报文不输出该字段)
body.setRsnum(null);
body.setRspos(null);
}
body.setTl("");
return body;
}