| | |
| | | * 回写 ERP 采购订单的入库数量 |
| | | */ |
| | | private void updateErpPurchaseOrderInCount(MesWmItemReceiptDO receipt) { |
| | | // 获取入库单行 |
| | | List<MesWmItemReceiptLineDO> lines = itemReceiptLineService.getItemReceiptLineListByReceiptId(receipt.getId()); |
| | | if (CollUtil.isEmpty(lines)) { |
| | | return; |
| | | // 1. 确定采购订单 ID:优先从入库单头取(从到货通知单带入),其次从行反查 |
| | | Long purchaseOrderId = receipt.getPurchaseOrderId(); |
| | | if (purchaseOrderId == null && receipt.getNoticeId() != null) { |
| | | // 从关联的到货通知单获取采购订单 ID |
| | | MesWmArrivalNoticeDO notice = arrivalNoticeService.getArrivalNotice(receipt.getNoticeId()); |
| | | if (notice != null) { |
| | | purchaseOrderId = notice.getPurchaseOrderId(); |
| | | } |
| | | // 用于存储采购订单 ID,避免重复更新 |
| | | Long purchaseOrderId = null; |
| | | // 遍历行,按 purchaseOrderItemId 更新入库数量 |
| | | } |
| | | |
| | | // 2. 获取入库单行,按 purchaseOrderItemId 更新入库数量 |
| | | List<MesWmItemReceiptLineDO> lines = itemReceiptLineService.getItemReceiptLineListByReceiptId(receipt.getId()); |
| | | if (CollUtil.isNotEmpty(lines)) { |
| | | for (MesWmItemReceiptLineDO line : lines) { |
| | | if (line.getPurchaseOrderItemId() != null && line.getReceivedQuantity() != null) { |
| | | purchaseOrderApi.updateOrderItemInCount(line.getPurchaseOrderItemId(), line.getReceivedQuantity()); |
| | | // 获取采购订单 ID(从第一个有效的明细项获取) |
| | | // 如果还没拿到 purchaseOrderId,从明细项反查 |
| | | if (purchaseOrderId == null) { |
| | | cn.iocoder.yudao.module.erp.api.purchase.dto.ErpPurchaseOrderItemRespDTO itemDTO = |
| | | purchaseOrderApi.getPurchaseOrderItem(line.getPurchaseOrderItemId()).getCheckedData(); |
| | |
| | | } |
| | | } |
| | | } |
| | | // 更新采购订单的入库状态 |
| | | } else if (receipt.getNoticeId() != null) { |
| | | // 兜底:入库行为空时,从到货通知单行回写入库数量 |
| | | List<MesWmArrivalNoticeLineDO> noticeLines = arrivalNoticeLineService |
| | | .getArrivalNoticeLineListByNoticeId(receipt.getNoticeId()); |
| | | for (MesWmArrivalNoticeLineDO noticeLine : noticeLines) { |
| | | if (noticeLine.getPurchaseOrderItemId() != null) { |
| | | // 使用合格数量(有 IQC 时为合格数量,否则为到货数量) |
| | | BigDecimal qty = noticeLine.getQualifiedQuantity() != null |
| | | ? noticeLine.getQualifiedQuantity() : noticeLine.getArrivalQuantity(); |
| | | if (qty != null && qty.compareTo(BigDecimal.ZERO) > 0) { |
| | | purchaseOrderApi.updateOrderItemInCount(noticeLine.getPurchaseOrderItemId(), qty); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 3. 更新采购订单的入库状态 |
| | | if (purchaseOrderId != null) { |
| | | purchaseOrderApi.updateOrderInStatus(purchaseOrderId); |
| | | } |
| | |
| | | receipt.setCode(code); |
| | | receipt.setName(notice.getName() + "入库单"); |
| | | receipt.setNoticeId(arrivalNoticeId); |
| | | receipt.setPurchaseOrderId(notice.getPurchaseOrderId()); |
| | | receipt.setPurchaseOrderCode(notice.getPurchaseOrderCode()); |
| | | receipt.setVendorId(notice.getVendorId()); |
| | | receipt.setReceiptDate(java.time.LocalDateTime.now()); // 入库时间为当前时间 |