| | |
| | | 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.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.bpm.api.task.BpmProcessInstanceApi; |
| | | import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO; |
| | | import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmCategoryDO; |
| | | import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionInfoDO; |
| | | import cn.iocoder.yudao.module.bpm.enums.task.BpmTaskStatusEnum; |
| | | import cn.iocoder.yudao.module.bpm.service.definition.BpmCategoryService; |
| | | import cn.iocoder.yudao.module.bpm.service.definition.BpmProcessDefinitionService; |
| | | 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.storage.StorageAttachmentApi; |
| | | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| | | import jakarta.annotation.Resource; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.flowable.engine.repository.ProcessDefinition; |
| | | 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.module.erp.enums.ErrorCodeConstants.*; |
| | | |
| | | // TODO 芋艿:记录操作日志 |
| | | |
| | | /** |
| | | * ERP 付款单 Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | | @Slf4j |
| | | public class ErpFinancePaymentServiceImpl implements ErpFinancePaymentService { |
| | | |
| | | /** |
| | | * BPM 付款单审批分类编码 |
| | | */ |
| | | private static final String BPM_PROCESS_DEFINITION_CATEGORY_CODE = "erp_finance_payment_approve"; |
| | | |
| | | @Resource |
| | | private ErpFinancePaymentMapper financePaymentMapper; |
| | |
| | | @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 BpmProcessInstanceApi processInstanceApi; |
| | | @Resource |
| | | private BpmCategoryService bpmCategoryService; |
| | | @Resource |
| | | private BpmProcessDefinitionService bpmProcessDefinitionService; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | 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); |
| | |
| | | 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(); |
| | |
| | | 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()); |
| | |
| | | // 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); |
| | |
| | | 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) { |
| | |
| | | |
| | | @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, String processDefinitionKey, Long userId) { |
| | | // 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_STATUS); |
| | | } |
| | | |
| | | // 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); |
| | | // 2. 创建 BPM 流程实例 |
| | | String processInstanceId = processInstanceApi.createProcessInstance(userId, |
| | | new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(processDefinitionKey) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | |
| | | // 3. 更新付款单状态为审批中 |
| | | financePaymentMapper.updateById(new ErpFinancePaymentDO() |
| | | .setId(id) |
| | | .setStatus(ErpAuditStatus.APPROVING.getStatus()) |
| | | .setProcessInstanceId(processInstanceId)); |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getFinancePaymentApproveProcessDefinitionList() { |
| | | // 1. 校验分类和流程定义是否存在 |
| | | validateFinancePaymentApproveCategoryAndProcessDefinition(); |
| | | |
| | | // 2. 获取分类下的流程定义信息 |
| | | List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService |
| | | .getProcessDefinitionInfoListByCategory(BPM_PROCESS_DEFINITION_CATEGORY_CODE); |
| | | if (CollUtil.isEmpty(definitionInfoList)) { |
| | | return Collections.emptyList(); |
| | | } |
| | | |
| | | // 3. 遍历获取流程定义详情,过滤激活状态,保留最新版本 |
| | | Map<String, ProcessDefinition> latestVersionMap = new HashMap<>(); |
| | | for (BpmProcessDefinitionInfoDO info : definitionInfoList) { |
| | | ProcessDefinition pd = bpmProcessDefinitionService.getProcessDefinition(info.getProcessDefinitionId()); |
| | | if (pd == null || pd.isSuspended()) { |
| | | continue; |
| | | } |
| | | ProcessDefinition existing = latestVersionMap.get(pd.getKey()); |
| | | if (existing == null || pd.getVersion() > existing.getVersion()) { |
| | | latestVersionMap.put(pd.getKey(), pd); |
| | | } |
| | | } |
| | | |
| | | // 4. 返回流程定义列表 |
| | | List<Map<String, Object>> result = new ArrayList<>(); |
| | | for (ProcessDefinition pd : latestVersionMap.values()) { |
| | | Map<String, Object> item = new HashMap<>(); |
| | | item.put("id", pd.getId()); |
| | | item.put("key", pd.getKey()); |
| | | item.put("name", pd.getName()); |
| | | result.add(item); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateFinancePaymentAuditStatus(Long id, Integer bpmResult) { |
| | | // 1. 校验存在 |
| | | ErpFinancePaymentDO payment = validateFinancePaymentExists(id); |
| | | if (!ErpAuditStatus.APPROVING.getStatus().equals(payment.getStatus())) { |
| | | log.warn("[updateFinancePaymentAuditStatus] 付款单({}) 不处于审批中状态", id); |
| | | return; |
| | | } |
| | | |
| | | // 2. 根据审批结果更新状态 |
| | | Integer newStatus = convertBpmResultToAuditStatus(bpmResult); |
| | | if (newStatus != null) { |
| | | financePaymentMapper.updateById(new ErpFinancePaymentDO() |
| | | .setId(id).setStatus(newStatus)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 校验付款单审批分类和流程定义是否存在 |
| | | */ |
| | | private void validateFinancePaymentApproveCategoryAndProcessDefinition() { |
| | | // 1. 校验分类是否存在 |
| | | List<BpmCategoryDO> categories = bpmCategoryService.getCategoryListByCode( |
| | | Collections.singletonList(BPM_PROCESS_DEFINITION_CATEGORY_CODE)); |
| | | if (CollUtil.isEmpty(categories)) { |
| | | throw exception(FINANCE_PAYMENT_BPM_CATEGORY_NOT_EXISTS); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转换 BPM 审批结果为审核状态 |
| | | */ |
| | | private Integer convertBpmResultToAuditStatus(Integer bpmResult) { |
| | | if (BpmTaskStatusEnum.APPROVE.getStatus().equals(bpmResult)) { |
| | | return ErpAuditStatus.APPROVE.getStatus(); |
| | | } else if (BpmTaskStatusEnum.REJECT.getStatus().equals(bpmResult)) { |
| | | return ErpAuditStatus.PROCESS.getStatus(); |
| | | } else if (BpmTaskStatusEnum.CANCEL.getStatus().equals(bpmResult)) { |
| | | return ErpAuditStatus.CANCEL.getStatus(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | 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)); |
| | |
| | | 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 |
| | |
| | | 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)); |
| | | |
| | |
| | | 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); |
| | |
| | | return financePaymentMapper.selectPage(pageReqVO); |
| | | } |
| | | |
| | | @Override |
| | | public Map<Long, BigDecimal> getPaymentPriceMapByInvoiceId(Collection<Long> invoiceIds) { |
| | | return financePaymentMapper.selectPaymentPriceMapByInvoiceId(invoiceIds); |
| | | } |
| | | |
| | | // ==================== 付款单项 ==================== |
| | | |
| | | @Override |