From 7d2ee3cef2e0d68ba6400bc5957640d2771bbf4c Mon Sep 17 00:00:00 2001
From: liu <2021943741@qq.com>
Date: 星期三, 16 九月 2026 18:05:40 +0800
Subject: [PATCH] refactor: 单据审核改为审批配置状态机(新增:audit接口/审核人字段/DB变更脚本)
---
yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/finance/ErpFinancePaymentServiceImpl.java | 217 ++++++++++++++++++++++++++++++++++++++++--------------
1 files changed, 160 insertions(+), 57 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 afb9a31..397afd9 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,50 +1,54 @@
package cn.iocoder.yudao.module.erp.service.finance;
import cn.hutool.core.collection.CollUtil;
-import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
+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;
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.ErpPurchaseInDO;
-import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseReturnDO;
+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.common.ErpBizTypeEnum;
-import cn.iocoder.yudao.module.erp.service.purchase.ErpPurchaseInService;
-import cn.iocoder.yudao.module.erp.service.purchase.ErpPurchaseReturnService;
+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.approval.ApprovalConfigApi;
+import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
+import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import jakarta.annotation.Resource;
+import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import java.math.BigDecimal;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
+import java.util.*;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
+import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;
-
-// TODO 鑺嬭壙锛氳褰曟搷浣滄棩蹇�
/**
* ERP 浠樻鍗� Service 瀹炵幇绫�
*
- * @author 鑺嬮亾婧愮爜
+ * @author 瓒呯骇绠$悊鍛�
*/
@Service
@Validated
+@Slf4j
public class ErpFinancePaymentServiceImpl implements ErpFinancePaymentService {
+
+ /**
+ * 閫氱敤瀹℃壒閰嶇疆鐨勪笟鍔$被鍨嬬紪鐮侊紙瀵瑰簲 system_approval_config.biz_type锛�
+ */
+ private static final String FINANCE_PAYMENT_APPROVE_BIZ_TYPE = "erp_finance_payment_approve";
@Resource
private ErpFinancePaymentMapper financePaymentMapper;
@@ -59,12 +63,16 @@
@Resource
private ErpAccountService accountService;
@Resource
- private ErpPurchaseInService purchaseInService;
- @Resource
- private ErpPurchaseReturnService purchaseReturnService;
+ private ErpPurchaseInvoiceService invoiceService;
@Resource
private AdminUserApi adminUserApi;
+
+ @Resource
+ private StorageAttachmentApi storageAttachmentApi;
+
+ @Resource
+ private ApprovalConfigApi approvalConfigApi;
@Override
@Transactional(rollbackFor = Exception.class)
@@ -82,7 +90,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);
@@ -97,6 +111,10 @@
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);
return payment.getId();
@@ -109,6 +127,9 @@
ErpFinancePaymentDO payment = validateFinancePaymentExists(updateReqVO.getId());
if (ErpAuditStatus.APPROVE.getStatus().equals(payment.getStatus())) {
throw exception(FINANCE_PAYMENT_UPDATE_FAIL_APPROVE, payment.getNo());
+ }
+ if (ErpAuditStatus.APPROVING.getStatus().equals(payment.getStatus())) {
+ throw exception(FINANCE_PAYMENT_UPDATE_FAIL_APPROVING, payment.getNo());
}
// 1.2 鏍¢獙渚涘簲鍟�
supplierService.validateSupplier(updateReqVO.getSupplierId());
@@ -123,6 +144,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);
@@ -130,6 +157,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) {
@@ -139,48 +168,82 @@
@Override
@Transactional(rollbackFor = Exception.class)
- public void updateFinancePaymentStatus(Long id, Integer status) {
- boolean approve = ErpAuditStatus.APPROVE.getStatus().equals(status);
- // 1.1 鏍¢獙瀛樺湪
+ public void submitFinancePayment(Long id) {
+ // 1. 鏍¢獙瀛樺湪 + 鍙彁浜ょ姸鎬侊紙鏈鏍革細鍚鏍镐笉閫氳繃閫�鍥炲悗閲嶆柊鎻愪氦锛�
ErpFinancePaymentDO payment = validateFinancePaymentExists(id);
- // 1.2 鏍¢獙鐘舵��
- if (payment.getStatus().equals(status)) {
- throw exception(approve ? FINANCE_PAYMENT_APPROVE_FAIL : FINANCE_PAYMENT_PROCESS_FAIL);
+ if (!ErpAuditStatus.PROCESS.getStatus().equals(payment.getStatus())) {
+ throw exception(FINANCE_PAYMENT_SUBMIT_FAIL_NOT_ALLOWED);
}
+ // 2. 鏍¢獙瀹℃壒閰嶇疆宸插惎鐢ㄤ笖閰嶇疆浜嗘湁鏁堝鎵逛汉
+ approvalConfigApi.validateApprovalEnabledAndGetApprovers(FINANCE_PAYMENT_APPROVE_BIZ_TYPE);
+ // 3. 鐘舵�佺疆涓哄鎵逛腑锛屽苟娓呯┖涓婁竴杞鏍哥粨鏋�
+ financePaymentMapper.submitAndResetAuditInfo(id, ErpAuditStatus.APPROVING.getStatus());
+ }
- // 2. 鏇存柊鐘舵��
- int updateCount = financePaymentMapper.updateByIdAndStatus(id, payment.getStatus(),
- new ErpFinancePaymentDO().setStatus(status));
- if (updateCount == 0) {
- throw exception(approve ? FINANCE_PAYMENT_APPROVE_FAIL : FINANCE_PAYMENT_PROCESS_FAIL);
+ @Override
+ public Set<Long> getFinancePaymentApproverUserIds() {
+ return approvalConfigApi.getApproverUserIds(FINANCE_PAYMENT_APPROVE_BIZ_TYPE);
+ }
+
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public void auditFinancePayment(Long id, Boolean pass, String reviewRemark) {
+ // 1. 鏍¢獙瀛樺湪
+ ErpFinancePaymentDO payment = validateFinancePaymentExists(id);
+ // 2. 鏍¢獙澶勪簬瀹℃壒涓姸鎬�
+ if (!ErpAuditStatus.APPROVING.getStatus().equals(payment.getStatus())) {
+ throw exception(FINANCE_PAYMENT_AUDIT_FAIL_NOT_PROCESS);
}
+ // 3. 鏍¢獙褰撳墠鐧诲綍鐢ㄦ埛鏄涓氬姟绫诲瀷鐨勫鎵逛汉锛堟垨绛撅細浠讳竴浜哄潎鍙鏍革級
+ Long userId = getLoginUserId();
+ approvalConfigApi.validateApprover(FINANCE_PAYMENT_APPROVE_BIZ_TYPE, userId);
+ // 4. 瀹℃牳涓嶉�氳繃锛氬繀椤诲~鍐欏師鍥狅紝閫�鍥炶储鍔′汉鍛樹慨鏀瑰悗鍙噸鏂版彁浜�
+ if (!Boolean.TRUE.equals(pass)) {
+ if (StrUtil.isBlank(reviewRemark)) {
+ throw exception(FINANCE_PAYMENT_AUDIT_REJECT_REASON_REQUIRED);
+ }
+ financePaymentMapper.auditFinancePayment(id, ErpAuditStatus.PROCESS.getStatus(),
+ userId, getUserNickname(userId), reviewRemark);
+ return;
+ }
+ // 5. 瀹℃牳閫氳繃锛氳褰曞鏍镐汉涓庡鏍告剰瑙侊紝鐘舵�佺疆涓哄凡瀹℃牳
+ financePaymentMapper.auditFinancePayment(id, ErpAuditStatus.APPROVE.getStatus(),
+ userId, getUserNickname(userId), reviewRemark);
+ }
+
+ /**
+ * 鑾峰彇鐢ㄦ埛鏄电О
+ *
+ * @param userId 鐢ㄦ埛缂栧彿
+ * @return 鏄电О锛岀敤鎴蜂笉瀛樺湪鏃惰繑鍥� null
+ */
+ private String getUserNickname(Long userId) {
+ if (userId == null) {
+ return null;
+ }
+ AdminUserRespDTO user = adminUserApi.getUser(userId);
+ return user == null ? null : user.getNickname();
}
private List<ErpFinancePaymentItemDO> validateFinancePaymentItems(
Long supplierId,
List<ErpFinancePaymentSaveReqVO.Item> list) {
+ // 閲囪喘鍏ュ簱/閫�璐у姛鑳藉凡绉婚櫎锛屾殏鏃跺彧鏀寔鎵嬪姩褰曞叆浠樻鏄庣粏
return convertList(list, o -> BeanUtils.toBean(o, ErpFinancePaymentItemDO.class, item -> {
- if (ObjectUtil.equal(item.getBizType(), ErpBizTypeEnum.PURCHASE_IN.getType())) {
- ErpPurchaseInDO purchaseIn = purchaseInService.validatePurchaseIn(item.getBizId());
- Assert.equals(purchaseIn.getSupplierId(), supplierId, "渚涘簲鍟嗗繀椤荤浉鍚�");
- item.setTotalPrice(purchaseIn.getTotalPrice()).setBizNo(purchaseIn.getNo());
- } else if (ObjectUtil.equal(item.getBizType(), ErpBizTypeEnum.PURCHASE_RETURN.getType())) {
- ErpPurchaseReturnDO purchaseReturn = purchaseReturnService.validatePurchaseReturn(item.getBizId());
- Assert.equals(purchaseReturn.getSupplierId(), supplierId, "渚涘簲鍟嗗繀椤荤浉鍚�");
- item.setTotalPrice(purchaseReturn.getTotalPrice().negate()).setBizNo(purchaseReturn.getNo());
- } else {
- throw new IllegalArgumentException("涓氬姟绫诲瀷涓嶆纭細" + item.getBizType());
+ if (item.getPaymentPrice() == null) {
+ item.setPaymentPrice(BigDecimal.ZERO);
+ }
+ if (item.getTotalPrice() == null) {
+ item.setTotalPrice(BigDecimal.ZERO);
}
}));
}
private void updateFinancePaymentItemList(Long id, List<ErpFinancePaymentItemDO> newList) {
- // 绗竴姝ワ紝瀵规瘮鏂拌�佹暟鎹紝鑾峰緱娣诲姞銆佷慨鏀广�佸垹闄ょ殑鍒楄〃
List<ErpFinancePaymentItemDO> oldList = financePaymentItemMapper.selectListByPaymentId(id);
- List<List<ErpFinancePaymentItemDO>> diffList = diffList(oldList, newList, // id 涓嶅悓锛屽氨璁や负鏄笉鍚岀殑璁板綍
+ List<List<ErpFinancePaymentItemDO>> diffList = diffList(oldList, newList,
(oldVal, newVal) -> oldVal.getId().equals(newVal.getId()));
- // 绗簩姝ワ紝鎵归噺娣诲姞銆佷慨鏀广�佸垹闄�
if (CollUtil.isNotEmpty(diffList.get(0))) {
diffList.get(0).forEach(o -> o.setPaymentId(id));
financePaymentItemMapper.insertBatch(diffList.get(0));
@@ -191,23 +254,10 @@
if (CollUtil.isNotEmpty(diffList.get(2))) {
financePaymentItemMapper.deleteByIds(convertList(diffList.get(2), ErpFinancePaymentItemDO::getId));
}
-
- // 绗笁姝ワ紝鏇存柊閲囪喘鍏ュ簱銆侀��璐х殑浠樻閲戦鎯呭喌
- updatePurchasePrice(CollectionUtils.newArrayList(diffList));
}
private void updatePurchasePrice(List<ErpFinancePaymentItemDO> paymentItems) {
- paymentItems.forEach(paymentItem -> {
- BigDecimal totalPaymentPrice = financePaymentItemMapper.selectPaymentPriceSumByBizIdAndBizType(
- paymentItem.getBizId(), paymentItem.getBizType());
- if (ErpBizTypeEnum.PURCHASE_IN.getType().equals(paymentItem.getBizType())) {
- purchaseInService.updatePurchaseInPaymentPrice(paymentItem.getBizId(), totalPaymentPrice);
- } else if (ErpBizTypeEnum.PURCHASE_RETURN.getType().equals(paymentItem.getBizType())) {
- purchaseReturnService.updatePurchaseReturnRefundPrice(paymentItem.getBizId(), totalPaymentPrice.negate());
- } else {
- throw new IllegalArgumentException("涓氬姟绫诲瀷涓嶆纭細" + paymentItem.getBizType());
- }
- });
+ // 閲囪喘鍏ュ簱/閫�璐у姛鑳藉凡绉婚櫎锛屼笉鍐嶆洿鏂板叧鑱斾笟鍔¢噾棰�
}
@Override
@@ -222,13 +272,18 @@
if (ErpAuditStatus.APPROVE.getStatus().equals(payment.getStatus())) {
throw exception(FINANCE_PAYMENT_DELETE_FAIL_APPROVE, payment.getNo());
}
+ if (ErpAuditStatus.APPROVING.getStatus().equals(payment.getStatus())) {
+ throw exception(FINANCE_PAYMENT_DELETE_FAIL_APPROVING, payment.getNo());
+ }
});
// 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));
@@ -245,6 +300,49 @@
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 (CollUtil.isEmpty(storageAttachmentApi.listAttachments(
+ ErpPurchaseInvoiceServiceImpl.PURCHASE_INVOICE_RECORD_TYPE, invoiceId))) {
+ throw exception(FINANCE_PAYMENT_CREATE_FAIL_INVOICE_NOT_ATTACHMENT);
+ }
+ // 3. 鏍¢獙渚涘簲鍟嗕笌鏉ョエ涓�鑷达紙闃茶剰鏁版嵁锛�
+ 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);
@@ -255,6 +353,11 @@
return financePaymentMapper.selectPage(pageReqVO);
}
+ @Override
+ public Map<Long, BigDecimal> getPaymentPriceMapByInvoiceId(Collection<Long> invoiceIds) {
+ return financePaymentMapper.selectPaymentPriceMapByInvoiceId(invoiceIds);
+ }
+
// ==================== 浠樻鍗曢」 ====================
@Override
--
Gitblit v1.9.3