From 4ea1509f0e88ee331e821459d4eefb0c9902b3e7 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期三, 12 八月 2026 13:16:04 +0800
Subject: [PATCH] feat(account): 实现采购付款和销售回款功能

---
 src/main/java/com/ruoyi/account/service/impl/purchase/AccountPurchasePaymentServiceImpl.java |   77 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 76 insertions(+), 1 deletions(-)

diff --git a/src/main/java/com/ruoyi/account/service/impl/purchase/AccountPurchasePaymentServiceImpl.java b/src/main/java/com/ruoyi/account/service/impl/purchase/AccountPurchasePaymentServiceImpl.java
index 14936f2..1246d68 100644
--- a/src/main/java/com/ruoyi/account/service/impl/purchase/AccountPurchasePaymentServiceImpl.java
+++ b/src/main/java/com/ruoyi/account/service/impl/purchase/AccountPurchasePaymentServiceImpl.java
@@ -1,28 +1,37 @@
 package com.ruoyi.account.service.impl.purchase;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
 import com.ruoyi.account.bean.dto.purchase.AccountPurchasePaymentDto;
 import com.ruoyi.account.bean.vo.purchase.AccountPurchasePaymentVo;
 import com.ruoyi.account.mapper.AccountStatementDetailsMapper;
+import com.ruoyi.account.mapper.purchase.AccountPaymentApplicationMapper;
 import com.ruoyi.account.mapper.purchase.AccountPurchasePaymentMapper;
 import com.ruoyi.account.pojo.AccountStatementDetails;
+import com.ruoyi.account.pojo.purchase.AccountPaymentApplication;
 import com.ruoyi.account.pojo.purchase.AccountPurchasePayment;
 import com.ruoyi.account.service.purchase.AccountPurchasePaymentService;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
+import com.ruoyi.purchase.dto.SavePaymentDto;
+import com.ruoyi.purchase.mapper.PurchaseLedgerMapper;
+import com.ruoyi.purchase.pojo.PurchaseLedger;
 import jakarta.servlet.http.HttpServletResponse;
 import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
+import java.math.BigDecimal;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.List;
 import java.util.Random;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -39,6 +48,8 @@
     private static final DateTimeFormatter CODE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyMMddHHmmss");
     private final AccountPurchasePaymentMapper accountPurchasePaymentMapper;
     private final AccountStatementDetailsMapper accountStatementDetailsMapper;
+    private final AccountPaymentApplicationMapper accountPaymentApplicationMapper;
+    private final PurchaseLedgerMapper purchaseLedgerMapper;
 
     @Override
     public IPage<AccountPurchasePaymentVo> listPageAccountPurchasePayment(Page page, AccountPurchasePaymentDto accountPurchasePaymentDto) {
@@ -49,6 +60,13 @@
     public boolean addAccountPurchasePayment(AccountPurchasePayment accountPurchasePayment) {
         if (StringUtils.isEmpty(accountPurchasePayment.getPaymentNumber())) {
             accountPurchasePayment.setPaymentNumber(genAccountPurchasePaymentNo());
+        }
+        //鏍¢獙绱浠樻閲戦涓嶈兘瓒呰繃鐢宠閲戦
+        AccountPaymentApplication accountPaymentApplication = accountPaymentApplicationMapper.selectById(accountPurchasePayment.getAccountPaymentApplicationId());
+        List<AccountPurchasePayment> accountPurchasePayments = accountPurchasePaymentMapper.selectList(Wrappers.<AccountPurchasePayment>lambdaQuery().eq(AccountPurchasePayment::getAccountPaymentApplicationId, accountPurchasePayment.getAccountPaymentApplicationId()));
+        BigDecimal totalPaymentAmount = accountPurchasePayments.stream().map(AccountPurchasePayment::getPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
+        if (accountPaymentApplication.getPaymentAmount().compareTo(totalPaymentAmount) < 0) {
+            throw new ServiceException("绱浠樻閲戦涓嶈兘瓒呰繃鐢宠閲戦");
         }
         return save(accountPurchasePayment);
     }
@@ -73,6 +91,63 @@
         return removeByIds(ids);
     }
 
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public boolean savePayment(SavePaymentDto dto) {
+        if (dto.getPaymentDate() == null) {
+            throw new ServiceException("璇烽�夋嫨浠樻鏃ユ湡");
+        }
+        if (dto.getPaymentAmount() == null || dto.getPaymentAmount().compareTo(BigDecimal.valueOf(0.01)) < 0) {
+            throw new ServiceException("璇疯緭鍏ヤ粯娆鹃噾棰�");
+        }
+        if (StringUtils.isEmpty(dto.getPurchaseContractNumber())) {
+            throw new ServiceException("閲囪喘鍚堝悓鍙蜂笉鑳戒负绌�");
+        }
+        if (dto.getSupplierId() == null) {
+            throw new ServiceException("渚涘簲鍟嗕笉鑳戒负绌�");
+        }
+
+        PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectOne(new LambdaQueryWrapper<PurchaseLedger>()
+                .eq(PurchaseLedger::getPurchaseContractNumber, dto.getPurchaseContractNumber()));
+        if (purchaseLedger == null) {
+            throw new ServiceException("閲囪喘鍚堝悓涓嶅瓨鍦�");
+        }
+        if (purchaseLedger.getSupplierId() == null || purchaseLedger.getSupplierId().longValue() != dto.getSupplierId().longValue()) {
+            throw new ServiceException("渚涘簲鍟嗕笌鍚堝悓涓嶅尮閰�");
+        }
+
+        List<Long> stockInRecordIds = accountPurchasePaymentMapper.selectStockInRecordIdsByPurchaseLedgerId(purchaseLedger.getId());
+        if (CollectionUtils.isEmpty(stockInRecordIds)) {
+            throw new ServiceException("璇ュ悎鍚屾殏鏃犲彲浠樻鐨勯噰璐叆搴撹褰�");
+        }
+
+        AccountPaymentApplication application = new AccountPaymentApplication();
+        application.setSupplierId(dto.getSupplierId().intValue());
+        application.setStockInRecordIds(stockInRecordIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
+        application.setInvoiceApplicationNo(genPaymentApplicationNo());
+        application.setPaymentMethod(dto.getPaymentMethod());
+        application.setPaymentContent(dto.getPurchaseContractNumber());
+        application.setApplyDate(dto.getPaymentDate());
+        application.setRemark(dto.getRemark());
+        application.setStatus(1);
+        application.setPaymentAmount(dto.getPaymentAmount());
+        accountPaymentApplicationMapper.insert(application);
+
+        AccountPurchasePayment payment = new AccountPurchasePayment();
+        payment.setAccountPaymentApplicationId(application.getId());
+        payment.setSupplierId(dto.getSupplierId().intValue());
+        payment.setPaymentDate(dto.getPaymentDate());
+        payment.setPaymentMethod(dto.getPaymentMethod());
+        payment.setPaymentAmount(dto.getPaymentAmount());
+        payment.setPaymentNumber(genAccountPurchasePaymentNo());
+        payment.setRemark(dto.getRemark());
+        return save(payment);
+    }
+
+    private String genPaymentApplicationNo() {
+        return "FK" + LocalDateTime.now().format(CODE_TIME_FORMATTER) + new Random().nextInt(10);
+    }
+
     private String genAccountPurchasePaymentNo() {
         return "SK" + LocalDateTime.now().format(CODE_TIME_FORMATTER) + new Random().nextInt(10);
     }

--
Gitblit v1.9.3