gaoluyang
9 天以前 8e4eee5ad4d74e1a4e6ab4de3a9c28000700dce0
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/itemreceipt/MesWmItemReceiptServiceImpl.java
@@ -2,6 +2,7 @@
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
@@ -27,6 +28,8 @@
import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO;
import cn.iocoder.yudao.module.erp.api.purchase.ErpPurchaseOrderApi;
import cn.iocoder.yudao.module.srm.api.supplier.SrmSupplierApi;
import cn.iocoder.yudao.module.system.api.notify.NotifyMessageSendApi;
import cn.iocoder.yudao.module.system.api.notify.dto.NotifySendSingleToUserReqDTO;
import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi;
import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO;
import org.springframework.security.core.Authentication;
@@ -39,9 +42,13 @@
import lombok.extern.slf4j.Slf4j;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
@@ -85,6 +92,9 @@
    @Resource
    private MdmItemApi mdmItemApi;
    @Resource
    private NotifyMessageSendApi notifyMessageSendApi;
    @Override
    public Long createItemReceipt(MesWmItemReceiptSaveReqVO createReqVO) {
@@ -240,13 +250,70 @@
        }
        // 5. 回写 ERP 采购订单的入库数量
        updateErpPurchaseOrderInCount(receipt);
        Long purchaseOrderId = updateErpPurchaseOrderInCount(receipt);
        // 6. 需求 5.2①:采购入库完成后触发付款流程(站内信提醒财务生成付款单)
        notifyPaymentRemind(receipt, purchaseOrderId);
    }
    /** 付款提醒站内信模板编码 */
    private static final String PAYMENT_REMIND_TEMPLATE_CODE = "purchase_receipt_payment_remind";
    /**
     * 采购入库完成后,提醒相关人凭采购订单生成付款单
     *
     * <p>需求 5.2①:采购入库确认收货后触发付款流程。系统不自动创建付款单——
     * ERP 付款单必须关联来票(发票),入库时通常尚未取票,自动建单会生成无效单据;
     * 因此这里以站内信方式提醒,由财务在「付款单」页面按来票补录。</p>
     */
    private void notifyPaymentRemind(MesWmItemReceiptDO receipt, Long purchaseOrderId) {
        try {
            // 1. 采购订单号与订单金额
            String purchaseOrderCode = receipt.getPurchaseOrderCode();
            BigDecimal totalPrice = null;
            if (purchaseOrderId != null) {
                cn.iocoder.yudao.module.erp.api.purchase.dto.ErpPurchaseOrderRespDTO order =
                        purchaseOrderApi.getPurchaseOrder(purchaseOrderId).getCheckedData();
                if (order != null) {
                    if (StrUtil.isBlank(purchaseOrderCode)) {
                        purchaseOrderCode = order.getNo();
                    }
                    totalPrice = order.getTotalPrice();
                }
            }
            Map<String, Object> params = new HashMap<>();
            params.put("receiptCode", receipt.getCode());
            params.put("purchaseOrderCode", StrUtil.isNotBlank(purchaseOrderCode) ? purchaseOrderCode : "-");
            params.put("totalPrice", totalPrice != null
                    ? totalPrice.setScale(2, RoundingMode.HALF_UP).toPlainString() : "-");
            // 2. 接收人:入库单创建人(仓管)+ 管理员,去重后逐个发送
            Set<Long> recipientIds = new LinkedHashSet<>();
            if (StrUtil.isNotBlank(receipt.getCreator())) {
                try {
                    recipientIds.add(Long.parseLong(receipt.getCreator()));
                } catch (NumberFormatException ignored) {
                    log.warn("采购入库单({}) 创建人({}) 不是合法用户编号,付款提醒将只发给管理员",
                            receipt.getId(), receipt.getCreator());
                }
            }
            recipientIds.add(1L);
            for (Long userId : recipientIds) {
                NotifySendSingleToUserReqDTO message = new NotifySendSingleToUserReqDTO();
                message.setUserId(userId);
                message.setTemplateCode(PAYMENT_REMIND_TEMPLATE_CODE);
                message.setTemplateParams(params);
                notifyMessageSendApi.sendSingleMessageToAdmin(message);
            }
        } catch (Exception ex) {
            // 提醒失败不影响入库业务
            log.warn("采购入库单({}) 付款提醒发送失败", receipt.getId(), ex);
        }
    }
    /**
     * 回写 ERP 采购订单的入库数量
     */
    private void updateErpPurchaseOrderInCount(MesWmItemReceiptDO receipt) {
    private Long updateErpPurchaseOrderInCount(MesWmItemReceiptDO receipt) {
        // 1. 确定采购订单 ID:优先从入库单头取(从到货通知单带入),其次从行反查
        Long purchaseOrderId = receipt.getPurchaseOrderId();
        if (purchaseOrderId == null && receipt.getNoticeId() != null) {
@@ -293,6 +360,7 @@
        if (purchaseOrderId != null) {
            purchaseOrderApi.updateOrderInStatus(purchaseOrderId);
        }
        return purchaseOrderId;
    }
    private void createTransactionList(MesWmItemReceiptDO receipt) {