fix:推物料凭证,部门领料代码恢复

This commit is contained in:
zhaoyf
2026-11-11 14:28:42 +08:00
parent 840f9ec224
commit 0b7ad4abc9
5 changed files with 208 additions and 33 deletions

View File

@@ -8,6 +8,8 @@ import org.nl.common.base.ResponseData;
import org.nl.common.logging.annotation.Log;
import org.nl.wms.ext_manage.sap.dto.SapBillFeedbackRequest;
import org.nl.wms.ext_manage.sap.service.SapBillFeedbackService;
import org.nl.wms.pm_manage.sap.deptPicking.service.DeptPickingService;
import org.nl.wms.pm_manage.sap.deptPicking.service.dto.DeptPickingSyncDTO;
import org.nl.wms.pm_manage.sap.materialDocument.service.MaterialDocumentService;
import org.nl.wms.pm_manage.sap.materialDocument.service.dto.MaterialDocumentDTO;
import org.nl.wms.pm_manage.sap.outBoundDelivery.service.OutboundDeliveryService;
@@ -33,6 +35,7 @@ public class SapSyncController {
private final MaterialDocumentService materialDocumentService;
private final OutboundDeliveryService outboundDeliveryService;
private final SalesReturnOutService salesReturnOutService;
private final DeptPickingService deptPickingService;
private final SapBillFeedbackService sapBillFeedbackService;
@PostMapping("/insertSapJhd")
@@ -42,6 +45,13 @@ public class SapSyncController {
return ResponseData.build(result, HttpStatus.OK);
}
@PostMapping("/insertSapLld")
@Log("SAP部门领料出库单同步(PP113)-下推出库完成后按PP055回传SAP")
public ResponseEntity<Object> insertSapLld(@RequestBody DeptPickingSyncDTO dto) {
JSONObject result = deptPickingService.insertSapLld(dto);
return ResponseData.build(result, HttpStatus.OK);
}
@PostMapping("/insertSapWwJhd")
@Log("SAP委外交货出库单同步(MM062)-做完需要回传SAP")
public ResponseEntity<Object> insertSapWwjhd(@RequestBody DeliveryOrderDTO deliveryOrderDTO) {

View File

@@ -115,6 +115,7 @@ public class MaterialDocumentServiceImpl implements MaterialDocumentService {
}
String operator = StrUtil.blankToDefault(body.getUsnam(), "SAP");
MaterialDocumentBillType billType = MaterialDocumentBillTypeResolver.resolve(body.getItem());
if (Objects.isNull(billType)) return;
List<MaterialDocumentItemBody> itemsToStore = MaterialDocumentBillTypeResolver.filterItemsForStorage(
body.getItem(), billType);
if (CollectionUtils.isEmpty(itemsToStore)) {

View File

@@ -705,7 +705,7 @@ public class OutboundDeliveryServiceImpl extends ServiceImpl<OutboundDeliveryMap
order.setStatus(OutboundDeliveryStatus.CREATED.getCode());
if (header != null) {
order.setIntf_id(StrUtil.blankToDefault(header.getIntfId(), SapConstant.INTF_MM062));
order.setSrc_system(StrUtil.blankToDefault(header.getSrcSystem(), "WMS4"));
order.setSrc_system(StrUtil.blankToDefault(header.getSrcSystem(), "WMS5"));
order.setDest_system(StrUtil.blankToDefault(header.getDestSystem(), "SAP"));
order.setSrc_msgid(header.getSrcMsgid());
order.setRequest_time(parseDateTime(header.getRequesttime()));
@@ -713,7 +713,7 @@ public class OutboundDeliveryServiceImpl extends ServiceImpl<OutboundDeliveryMap
order.setBackup2(header.getBackup2());
} else {
order.setIntf_id(SapConstant.INTF_MM062);
order.setSrc_system("WMS4");
order.setSrc_system("WMS5");
order.setDest_system("SAP");
}
orderMapper.insert(order);

View File

@@ -31,7 +31,6 @@ import org.nl.wms.basedata_manage.service.dto.MdPbStoragevehicleextDto;
import org.nl.wms.basedata_manage.service.dto.StrategyMater;
import org.nl.wms.basedata_manage.service.dto.StrategyStructMaterialVO;
import org.nl.wms.basedata_manage.service.dto.StrategyStructParam;
import org.nl.wms.pm_manage.SourceBillTypeEnum;
import org.nl.wms.sch_manage.enums.TaskStatus;
import org.nl.wms.sch_manage.service.ISchBasePointService;
import org.nl.wms.sch_manage.service.ISchBaseTaskService;
@@ -881,36 +880,6 @@ public class OutBillServiceImpl extends ServiceImpl<IOStorInvMapper,IOStorInv> i
}
//库存变动:根据冻结数更新物料库存
iStInventoryService.changeInventory(InventoryChangeType.OUT_FINS, outFinsParams);
// if (org.nl.config.SpringContextHolder.getBean(org.nl.wms.welding_manage.service.work_order.impl.WorkOrderOutboundService.class).isWorkOrderBill(lockedBill.getIostorinv_id())) {
// // 整托可能含同物料同批次的多次换托记录,逐组盘扣减避免通用getOne查询歧义。
// for (String pallet : disList.stream().map(IOStorInvDis::getStoragevehicle_code).collect(Collectors.toSet())) {
// List<GroupPlate> stock = iMdPbGroupPlateService.list(new LambdaQueryWrapper<GroupPlate>().eq(GroupPlate::getStoragevehicle_code, pallet)
// .eq(GroupPlate::getStatus, IOSEnum.GROUP_PLATE_STATUS.code("入库")).gt(GroupPlate::getQty, BigDecimal.ZERO).last("for update"));
// BigDecimal expected = disList.stream().filter(d -> pallet.equals(d.getStoragevehicle_code())).map(IOStorInvDis::getPlan_qty).reduce(BigDecimal.ZERO, BigDecimal::add);
// if (stock.stream().map(GroupPlate::getQty).reduce(BigDecimal.ZERO, BigDecimal::add).compareTo(expected) != 0
// || stock.stream().anyMatch(g -> g.getFrozen_qty() == null || g.getFrozen_qty().compareTo(g.getQty()) != 0))
// throw new BadRequestException("出库托盘库存或冻结数量已变化");
// for (GroupPlate g : stock) {
// org.nl.wms.warehouse_manage.record.service.dao.StIvtStructivtflow f = new org.nl.wms.warehouse_manage.record.service.dao.StIvtStructivtflow();
// f.setId(IdUtil.getStringId());
// f.setUpdate_time(DateUtil.now());
// f.setVehicle_code(pallet);
// f.setMaterial_id(g.getMaterial_code());
// f.setPcsn(g.getPcsn());
// f.setQty(BigDecimal.ZERO);
// f.setFrozen_qty(BigDecimal.ZERO);
// f.setChange_qty(g.getQty());
// f.setGrowth(false);
// f.setStor_code(lockedBill.getStor_code());
// f.setSource_form_id(lockedBill.getIostorinv_id());
// f.setTask_type("工单领料出库");
// SpringContextHolder.getBean(org.nl.wms.warehouse_manage.record.service.IStIvtStructivtflowService.class).save(f);
// iMdPbGroupPlateService.removeById(g.getGroup_id());
// }
// }
// } else {
// iStInventoryService.changeInventory(InventoryChangeType.OUT_FINS, outFinsParams);
// }
// 混料整托任务同时关联多个出库明细,逐明细完成并触发LLCK。
for (String completedDetailId : disList.stream().map(IOStorInvDis::getIostorinvdtl_id).collect(Collectors.toSet())) {
IOStorInvDis item = disList.stream().filter(d -> completedDetailId.equals(d.getIostorinvdtl_id())).findFirst().get();

View File

@@ -0,0 +1,195 @@
package org.nl.wms.warehouse_manage.stockReturn.strategy.core.impl.sap;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.nl.common.exception.BadRequestException;
import org.nl.wms.ext_manage.sap.api.SapApi;
import org.nl.wms.ext_manage.sap.api.dto.ProductionOutboundBody;
import org.nl.wms.ext_manage.sap.api.dto.ProductionOutboundHeader;
import org.nl.wms.ext_manage.sap.api.dto.ProductionOutboundRequest;
import org.nl.wms.ext_manage.sap.enums.SapConstant;
import org.nl.wms.pm_manage.sap.deptPicking.service.dao.DeptPickingItem;
import org.nl.wms.pm_manage.sap.deptPicking.service.dao.mapper.DeptPickingItemMapper;
import org.nl.wms.warehouse_manage.enums.IOSEnum;
import org.nl.wms.warehouse_manage.inAndOut.service.dao.IOStorInv;
import org.nl.wms.warehouse_manage.inAndOut.service.dao.IOStorInvDtl;
import org.nl.wms.warehouse_manage.inAndOut.service.dao.mapper.IOStorInvDtlMapper;
import org.nl.wms.warehouse_manage.inAndOut.service.dao.mapper.IOStorInvMapper;
import org.nl.wms.warehouse_manage.stockReturn.service.IPmStockReturnService;
import org.nl.wms.warehouse_manage.stockReturn.service.dao.PmStockReturn;
import org.nl.wms.warehouse_manage.stockReturn.service.enums.StockReturnStatusEnum;
import org.nl.wms.warehouse_manage.stockReturn.strategy.core.CallbackStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
/**
* SAP部门领料出库(PP113)下推出的出库单完成回传 SAP PP055 生产投料。
* <p>
* 出库明细的 {@code source_billdtl_id} 存的是 {@code sap_dept_picking_item.id},
* 回传时按明细取预留号/行项目、工单号、工厂、库存地点,组装 PP055:
* aufnr=工单号、matnr=物料、erfmg=实际出库数量、rsnum/rspos=预留信息、tl=空(投料)。
* 201(成本中心领料)行没有工单号,按空 aufnr 回传并记录告警。
*/
@Slf4j
@Service("sapDeptPickingOut")
public class SapDeptPickingOut implements CallbackStrategy {
private static final DateTimeFormatter DATE_BASIC = DateTimeFormatter.BASIC_ISO_DATE;
@Autowired
private IPmStockReturnService iPmStockReturnService;
@Autowired
private IOStorInvMapper ioStorInvMapper;
@Autowired
private IOStorInvDtlMapper ioStorInvDtlMapper;
@Autowired
private DeptPickingItemMapper deptPickingItemMapper;
@Autowired
private SapApi sapApi;
@Override
public void buildCallback(IOStorInvDtl param) {
if (param == null || StrUtil.isBlank(param.getIostorinv_id())) {
throw new BadRequestException("部门领料出库回传失败:出库单为空");
}
IOStorInv bill = ioStorInvMapper.selectById(param.getIostorinv_id());
if (bill == null) {
throw new BadRequestException("部门领料出库回传失败:出库单不存在" + param.getIostorinv_id());
}
// 整单完成才过账
String finishedStatus = IOSEnum.BILL_STATUS.code("完成");
Integer unFinishCount = ioStorInvDtlMapper.selectCount(new LambdaQueryWrapper<IOStorInvDtl>()
.eq(IOStorInvDtl::getIostorinv_id, bill.getIostorinv_id())
.ne(IOStorInvDtl::getBill_status, finishedStatus));
if (unFinishCount != null && unFinishCount > 0) {
return;
}
// 同一出库单只回传一次,失败记录走回传记录页面手动重发
PmStockReturn exist = iPmStockReturnService.getOne(new LambdaQueryWrapper<PmStockReturn>()
.eq(PmStockReturn::getRequest_Id, bill.getIostorinv_id())
.eq(PmStockReturn::getRequest_type, IOSEnum.BILL_TYPE.code("生产出库"))
.last("limit 1"));
if (exist != null && StockReturnStatusEnum.SUCESS.getCode().equals(exist.getStatus())) {
return;
}
List<IOStorInvDtl> details = ioStorInvDtlMapper.selectList(new LambdaQueryWrapper<IOStorInvDtl>()
.eq(IOStorInvDtl::getIostorinv_id, bill.getIostorinv_id())
.orderByAsc(IOStorInvDtl::getSeq_no));
if (details == null || details.isEmpty()) {
throw new BadRequestException("部门领料出库回传失败:出库明细不存在" + bill.getBill_code());
}
String today = LocalDate.now().format(DATE_BASIC);
String docno = "FEED" + System.currentTimeMillis();
List<ProductionOutboundBody> bodyList = new ArrayList<>();
for (IOStorInvDtl detail : details) {
BigDecimal qty = resolveQty(detail);
if (qty.signum() <= 0) {
log.warn("部门领料出库单{}明细{}数量为0,跳过回传", bill.getBill_code(), detail.getSeq_no());
continue;
}
DeptPickingItem item = loadItem(detail.getSource_billdtl_id());
bodyList.add(buildBody(docno, today, bill, detail, item, qty));
}
if (bodyList.isEmpty()) {
log.warn("部门领料出库单{}无有效明细,跳过回传SAP", bill.getBill_code());
return;
}
ProductionOutboundRequest request = new ProductionOutboundRequest();
ProductionOutboundHeader header = new ProductionOutboundHeader();
header.setIntfId(SapConstant.INTF_PP055);
header.setSrcSystem(SapConstant.SRC_SYSTEM);
header.setDestSystem(SapConstant.DEST_SYSTEM);
header.setRequesttime(System.currentTimeMillis());
request.setHeader(header);
request.setBody(bodyList);
PmStockReturn record = exist == null ? new PmStockReturn() : exist;
record.setRequest_Id(bill.getIostorinv_id());
record.setBill_code(bill.getBill_code());
record.setRequest_type(IOSEnum.BILL_TYPE.code("生产出库"));
record.setCreate_time(exist == null ? DateUtil.now() : exist.getCreate_time());
record.setRequest_data(JSON.toJSONString(request));
record.setError_msg(null);
try {
JSONObject sapResult = sapApi.productionOutbound(request);
record.setStatus(StockReturnStatusEnum.SUCESS.getCode());
record.setProcess_time(DateUtil.now());
JSONObject result = new JSONObject();
result.put("request", request);
result.put("response", sapResult);
record.setRequest_data(result.toJSONString());
} catch (Exception e) {
log.error("部门领料出库单{}回传SAP失败", bill.getBill_code(), e);
record.setStatus(StockReturnStatusEnum.FAIL.getCode());
record.setError_msg(StrUtil.blankToDefault(e.getMessage(), "回传SAP失败"));
record.setProcess_time(DateUtil.now());
}
if (exist == null) {
iPmStockReturnService.save(record);
} else {
iPmStockReturnService.updateById(record);
}
}
private ProductionOutboundBody buildBody(String docno, String today, IOStorInv bill, IOStorInvDtl detail,
DeptPickingItem item, BigDecimal qty) {
ProductionOutboundBody body = new ProductionOutboundBody();
body.setDocno(docno);
body.setBudat(today);
body.setBldat(today);
body.setMatnr(detail.getMaterial_code());
body.setWerks(item != null && StrUtil.isNotBlank(item.getWerks())
? item.getWerks() : SapConstant.WERKS);
// 优先取 SAP 下发的库存地点,兜底出库单仓库
body.setLgort(item != null && StrUtil.isNotBlank(item.getLgort())
? item.getLgort() : StrUtil.blankToDefault(bill.getStor_code(), ""));
body.setAufnr(item == null ? "" : StrUtil.blankToDefault(StrUtil.trim(item.getAufnr()), ""));
body.setErfmg(qty.doubleValue());
body.setRsnum(item == null ? "" : StrUtil.blankToDefault(StrUtil.trim(item.getRsnum()), ""));
body.setRspos(item == null ? "" : StrUtil.blankToDefault(StrUtil.trim(item.getRspos()), ""));
body.setTl("");
if (item != null && StrUtil.isBlank(item.getAufnr())) {
log.warn("部门领料出库单{}物料{}无工单号(移动类型{},成本中心{}),PP055按空工单号回传",
bill.getBill_code(), detail.getMaterial_code(), item.getBwart(), item.getKostl());
}
return body;
}
private DeptPickingItem loadItem(String itemId) {
if (StrUtil.isBlank(itemId)) {
return null;
}
try {
return deptPickingItemMapper.selectById(Long.valueOf(itemId.trim()));
} catch (NumberFormatException e) {
log.warn("部门领料出库明细来源明细ID非法: {}", itemId);
return null;
}
}
private BigDecimal resolveQty(IOStorInvDtl detail) {
if (detail.getReal_qty() != null && detail.getReal_qty().compareTo(BigDecimal.ZERO) > 0) {
return detail.getReal_qty();
}
if (detail.getAssign_qty() != null && detail.getAssign_qty().compareTo(BigDecimal.ZERO) > 0) {
return detail.getAssign_qty();
}
if (detail.getPlan_qty() != null && detail.getPlan_qty().compareTo(BigDecimal.ZERO) > 0) {
return detail.getPlan_qty();
}
return BigDecimal.ZERO;
}
}