From 9fceeaad9af96bc58ba714560814c6e94f8af406 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期一, 10 八月 2026 17:13:01 +0800
Subject: [PATCH] feat(crm): 增强合同开票与回款功能

---
 yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/finance/ErpFinancePaymentServiceImpl.java |   92 ++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 88 insertions(+), 4 deletions(-)

diff --git a/yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/finance/ErpFinancePaymentServiceImpl.java b/yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/finance/ErpFinancePaymentServiceImpl.java
index e4c6b89..6be9fa0 100644
--- a/yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/finance/ErpFinancePaymentServiceImpl.java
+++ b/yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/finance/ErpFinancePaymentServiceImpl.java
@@ -1,17 +1,23 @@
 package cn.iocoder.yudao.module.erp.service.finance;
 
 import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.util.ObjectUtil;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
 import cn.iocoder.yudao.module.erp.controller.admin.finance.vo.payment.ErpFinancePaymentPageReqVO;
 import cn.iocoder.yudao.module.erp.controller.admin.finance.vo.payment.ErpFinancePaymentSaveReqVO;
 import cn.iocoder.yudao.module.erp.dal.dataobject.finance.ErpFinancePaymentDO;
 import cn.iocoder.yudao.module.erp.dal.dataobject.finance.ErpFinancePaymentItemDO;
+import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseInvoiceDO;
 import cn.iocoder.yudao.module.erp.dal.mysql.finance.ErpFinancePaymentItemMapper;
 import cn.iocoder.yudao.module.erp.dal.mysql.finance.ErpFinancePaymentMapper;
 import cn.iocoder.yudao.module.erp.dal.redis.no.ErpNoRedisDAO;
 import cn.iocoder.yudao.module.erp.enums.ErpAuditStatus;
+import cn.iocoder.yudao.module.erp.enums.ErpPurchaseInvoiceAuditStatusEnum;
+import cn.iocoder.yudao.module.erp.service.purchase.ErpPurchaseInvoiceService;
+import cn.iocoder.yudao.module.erp.service.purchase.ErpPurchaseInvoiceServiceImpl;
 import cn.iocoder.yudao.module.erp.service.purchase.ErpSupplierService;
+import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi;
 import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
 import jakarta.annotation.Resource;
 import org.springframework.stereotype.Service;
@@ -22,6 +28,7 @@
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
@@ -30,7 +37,7 @@
 /**
  * ERP 浠樻鍗� Service 瀹炵幇绫�
  *
- * @author 鑺嬮亾婧愮爜
+ * @author 瓒呯骇绠$悊鍛�
  */
 @Service
 @Validated
@@ -48,9 +55,14 @@
     private ErpSupplierService supplierService;
     @Resource
     private ErpAccountService accountService;
+    @Resource
+    private ErpPurchaseInvoiceService invoiceService;
 
     @Resource
     private AdminUserApi adminUserApi;
+
+    @Resource
+    private StorageAttachmentApi storageAttachmentApi;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -68,7 +80,13 @@
         if (createReqVO.getFinanceUserId() != null) {
             adminUserApi.validateUser(createReqVO.getFinanceUserId());
         }
-        // 1.5 鐢熸垚浠樻鍗曞彿锛屽苟鏍¢獙鍞竴鎬�
+        // 1.5 鏍¢獙鍏宠仈鏉ョエ
+        validateInvoiceForPayment(createReqVO.getInvoiceId(), createReqVO.getSupplierId());
+        // 1.6 鏍¢獙浠樻閲戦涓嶈秴杩囪鏉ョエ鍓╀綑鍙粯閲戦
+        validatePaymentPriceExceedsInvoiceRemaining(createReqVO.getInvoiceId(),
+                getSumValue(paymentItems, ErpFinancePaymentItemDO::getPaymentPrice, BigDecimal::add, BigDecimal.ZERO)
+                        .subtract(createReqVO.getDiscountPrice()), null);
+        // 1.7 鐢熸垚浠樻鍗曞彿锛屽苟鏍¢獙鍞竴鎬�
         String no = noRedisDAO.generate(ErpNoRedisDAO.FINANCE_PAYMENT_NO_PREFIX);
         if (financePaymentMapper.selectByNo(no) != null) {
             throw exception(FINANCE_PAYMENT_NO_EXISTS);
@@ -82,6 +100,10 @@
         // 2.2 鎻掑叆浠樻鍗曢」
         paymentItems.forEach(o -> o.setPaymentId(payment.getId()));
         financePaymentItemMapper.insertBatch(paymentItems);
+
+        // 2.3 缁戝畾闄勪欢
+        storageAttachmentApi.bindAttachments("file", "erp_finance_payment",
+                payment.getId(), createReqVO.getBlobIds());
 
         // 3. 鏇存柊閲囪喘鍏ュ簱銆侀��璐х殑浠樻閲戦鎯呭喌
         updatePurchasePrice(paymentItems);
@@ -109,6 +131,12 @@
         // 1.5 鏍¢獙浠樻鍗曢」鐨勬湁鏁堟��
         List<ErpFinancePaymentItemDO> paymentItems = validateFinancePaymentItems(
                 updateReqVO.getSupplierId(), updateReqVO.getItems());
+        // 1.6 鏍¢獙鍏宠仈鏉ョエ
+        validateInvoiceForPayment(updateReqVO.getInvoiceId(), updateReqVO.getSupplierId());
+        // 1.7 鏍¢獙浠樻閲戦涓嶈秴杩囪鏉ョエ鍓╀綑鍙粯閲戦锛堟帓闄よ浠樻鍗曡嚜韬綋鍓嶅凡浠橀噾棰濓級
+        validatePaymentPriceExceedsInvoiceRemaining(updateReqVO.getInvoiceId(),
+                getSumValue(paymentItems, ErpFinancePaymentItemDO::getPaymentPrice, BigDecimal::add, BigDecimal.ZERO)
+                        .subtract(updateReqVO.getDiscountPrice()), payment.getPaymentPrice());
 
         // 2.1 鏇存柊浠樻鍗�
         ErpFinancePaymentDO updateObj = BeanUtils.toBean(updateReqVO, ErpFinancePaymentDO.class);
@@ -116,6 +144,8 @@
         financePaymentMapper.updateById(updateObj);
         // 2.2 鏇存柊浠樻鍗曢」
         updateFinancePaymentItemList(updateReqVO.getId(), paymentItems);
+        // 2.3 鏇存柊闄勪欢
+        storageAttachmentApi.updateAttachments("file", "erp_finance_payment", updateReqVO.getId(), updateReqVO.getBlobIds());
     }
 
     private void calculateTotalPrice(ErpFinancePaymentDO payment, List<ErpFinancePaymentItemDO> paymentItems) {
@@ -193,9 +223,11 @@
 
         // 2. 閬嶅巻鍒犻櫎锛屽苟璁板綍鎿嶄綔鏃ュ織
         payments.forEach(payment -> {
-            // 2.1 鍒犻櫎浠樻鍗�
+            // 2.1 鍒犻櫎闄勪欢
+            storageAttachmentApi.deleteAttachmentsByRecord("erp_finance_payment", payment.getId());
+            // 2.2 鍒犻櫎浠樻鍗�
             financePaymentMapper.deleteById(payment.getId());
-            // 2.2 鍒犻櫎浠樻鍗曢」
+            // 2.3 鍒犻櫎浠樻鍗曢」
             List<ErpFinancePaymentItemDO> paymentItems = financePaymentItemMapper.selectListByPaymentId(payment.getId());
             financePaymentItemMapper.deleteByIds(convertSet(paymentItems, ErpFinancePaymentItemDO::getId));
 
@@ -212,6 +244,53 @@
         return payment;
     }
 
+    /**
+     * 鏍¢獙浠樻鍗曞叧鑱旂殑鏉ョエ锛氬瓨鍦ㄣ�佸鎵归�氳繃銆佸凡涓婁紶鍙戠エ闄勪欢銆佷緵搴斿晢涓庢潵绁ㄤ竴鑷�
+     *
+     * @param invoiceId 鏉ョエ缂栧彿
+     * @param supplierId 渚涘簲鍟嗙紪鍙�
+     */
+    private void validateInvoiceForPayment(Long invoiceId, Long supplierId) {
+        // 1. 鏍¢獙鏉ョエ瀛樺湪
+        ErpPurchaseInvoiceDO invoice = invoiceService.getPurchaseInvoice(invoiceId);
+        if (invoice == null) {
+            throw exception(FINANCE_PAYMENT_CREATE_FAIL_INVOICE_NOT_EXISTS);
+        }
+        // 2. 鏍¢獙鏉ョエ瀹℃壒閫氳繃
+        if (ObjectUtil.notEqual(invoice.getAuditStatus(), ErpPurchaseInvoiceAuditStatusEnum.APPROVE.getStatus())) {
+            throw exception(FINANCE_PAYMENT_CREATE_FAIL_INVOICE_NOT_APPROVE);
+        }
+        // 3. 鏍¢獙宸蹭笂浼犲彂绁ㄩ檮浠�
+        if (CollUtil.isEmpty(storageAttachmentApi.listAttachments(
+                ErpPurchaseInvoiceServiceImpl.PURCHASE_INVOICE_RECORD_TYPE, invoiceId))) {
+            throw exception(FINANCE_PAYMENT_CREATE_FAIL_INVOICE_NOT_ATTACHMENT);
+        }
+        // 4. 鏍¢獙渚涘簲鍟嗕笌鏉ョエ涓�鑷达紙闃茶剰鏁版嵁锛�
+        if (ObjectUtil.notEqual(invoice.getSupplierId(), supplierId)) {
+            throw exception(FINANCE_PAYMENT_CREATE_FAIL_INVOICE_SUPPLIER_MISMATCH);
+        }
+    }
+
+    /**
+     * 鏍¢獙浠樻閲戦涓嶈秴杩囪鏉ョエ鍓╀綑鍙粯閲戦锛堟潵绁ㄩ噾棰� - 璇ユ潵绁ㄥ凡浠樻閲戦锛�
+     *
+     * @param invoiceId 鏉ョエ缂栧彿
+     * @param paymentPrice 鏈浠樻閲戦
+     * @param oldPaymentPrice 鏇存柊鍦烘櫙涓嬶紝璇ヤ粯娆惧崟鑷韩褰撳墠鐨勪粯娆鹃噾棰濓紙鐢ㄤ簬鎺掗櫎鑷韩锛夛紝鍒涘缓鍦烘櫙浼� null
+     */
+    private void validatePaymentPriceExceedsInvoiceRemaining(Long invoiceId, BigDecimal paymentPrice, BigDecimal oldPaymentPrice) {
+        ErpPurchaseInvoiceDO invoice = invoiceService.getPurchaseInvoice(invoiceId);
+        BigDecimal paidPrice = financePaymentMapper.selectPaymentPriceMapByInvoiceId(List.of(invoiceId))
+                .getOrDefault(invoiceId, BigDecimal.ZERO);
+        if (oldPaymentPrice != null) {
+            paidPrice = paidPrice.subtract(oldPaymentPrice); // 鎺掗櫎璇ヤ粯娆惧崟鑷韩褰撳墠宸蹭粯閲戦
+        }
+        BigDecimal remainingPrice = invoice.getPrice().subtract(paidPrice);
+        if (paymentPrice.compareTo(remainingPrice) > 0) {
+            throw exception(FINANCE_PAYMENT_CREATE_FAIL_INVOICE_PRICE_EXCEEDS, remainingPrice);
+        }
+    }
+
     @Override
     public ErpFinancePaymentDO getFinancePayment(Long id) {
         return financePaymentMapper.selectById(id);
@@ -222,6 +301,11 @@
         return financePaymentMapper.selectPage(pageReqVO);
     }
 
+    @Override
+    public Map<Long, BigDecimal> getPaymentPriceMapByInvoiceId(Collection<Long> invoiceIds) {
+        return financePaymentMapper.selectPaymentPriceMapByInvoiceId(invoiceIds);
+    }
+
     // ==================== 浠樻鍗曢」 ====================
 
     @Override

--
Gitblit v1.9.3