| | |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | 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.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.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.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.flowable.engine.repository.ProcessDefinition; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | |
| | | |
| | | 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.*; |
| | | |
| | | /** |
| | |
| | | public class ErpFinancePaymentServiceImpl implements ErpFinancePaymentService { |
| | | |
| | | /** |
| | | * BPM 付款单审批分类编码 |
| | | * 通用审批配置的业务类型编码(对应 system_approval_config.biz_type) |
| | | */ |
| | | private static final String BPM_PROCESS_DEFINITION_CATEGORY_CODE = "erp_finance_payment_approve"; |
| | | private static final String FINANCE_PAYMENT_APPROVE_BIZ_TYPE = "erp_finance_payment_approve"; |
| | | |
| | | @Resource |
| | | private ErpFinancePaymentMapper financePaymentMapper; |
| | |
| | | private StorageAttachmentApi storageAttachmentApi; |
| | | |
| | | @Resource |
| | | private BpmProcessInstanceApi processInstanceApi; |
| | | @Resource |
| | | private BpmCategoryService bpmCategoryService; |
| | | @Resource |
| | | private BpmProcessDefinitionService bpmProcessDefinitionService; |
| | | private ApprovalConfigApi approvalConfigApi; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void submitFinancePayment(Long id, String processDefinitionKey, Long userId) { |
| | | // 1. 校验存在 + 未审核状态 |
| | | public void submitFinancePayment(Long id) { |
| | | // 1. 校验存在 + 可提交状态(未审核:含审核不通过退回后重新提交) |
| | | ErpFinancePaymentDO payment = validateFinancePaymentExists(id); |
| | | if (!ErpAuditStatus.PROCESS.getStatus().equals(payment.getStatus())) { |
| | | throw exception(FINANCE_PAYMENT_SUBMIT_FAIL_STATUS); |
| | | throw exception(FINANCE_PAYMENT_SUBMIT_FAIL_NOT_ALLOWED); |
| | | } |
| | | |
| | | // 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)); |
| | | // 2. 校验审批配置已启用且配置了有效审批人 |
| | | approvalConfigApi.validateApprovalEnabledAndGetApprovers(FINANCE_PAYMENT_APPROVE_BIZ_TYPE); |
| | | // 3. 状态置为审批中,并清空上一轮审核结果 |
| | | financePaymentMapper.submitAndResetAuditInfo(id, ErpAuditStatus.APPROVING.getStatus()); |
| | | } |
| | | |
| | | @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; |
| | | public Set<Long> getFinancePaymentApproverUserIds() { |
| | | return approvalConfigApi.getApproverUserIds(FINANCE_PAYMENT_APPROVE_BIZ_TYPE); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateFinancePaymentAuditStatus(Long id, Integer bpmResult) { |
| | | public void auditFinancePayment(Long id, Boolean pass, String reviewRemark) { |
| | | // 1. 校验存在 |
| | | ErpFinancePaymentDO payment = validateFinancePaymentExists(id); |
| | | // 2. 校验处于审批中状态 |
| | | if (!ErpAuditStatus.APPROVING.getStatus().equals(payment.getStatus())) { |
| | | log.warn("[updateFinancePaymentAuditStatus] 付款单({}) 不处于审批中状态", id); |
| | | 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; |
| | | } |
| | | |
| | | // 2. 根据审批结果更新状态 |
| | | Integer newStatus = convertBpmResultToAuditStatus(bpmResult); |
| | | if (newStatus != null) { |
| | | financePaymentMapper.updateById(new ErpFinancePaymentDO() |
| | | .setId(id).setStatus(newStatus)); |
| | | } |
| | | // 5. 审核通过:记录审核人与审核意见,状态置为已审核 |
| | | financePaymentMapper.auditFinancePayment(id, ErpAuditStatus.APPROVE.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | } |
| | | |
| | | /** |
| | | * 校验付款单审批分类和流程定义是否存在 |
| | | * 获取用户昵称 |
| | | * |
| | | * @param userId 用户编号 |
| | | * @return 昵称,用户不存在时返回 null |
| | | */ |
| | | 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); |
| | | private String getUserNickname(Long userId) { |
| | | if (userId == null) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转换 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; |
| | | AdminUserRespDTO user = adminUserApi.getUser(userId); |
| | | return user == null ? null : user.getNickname(); |
| | | } |
| | | |
| | | private List<ErpFinancePaymentItemDO> validateFinancePaymentItems( |
| | |
| | | } |
| | | |
| | | /** |
| | | * 校验付款单关联的来票:存在、审批通过、已上传发票附件、供应商与来票一致 |
| | | * 校验付款单关联的来票:存在、已上传发票附件、供应商与来票一致 |
| | | * |
| | | * @param invoiceId 来票编号 |
| | | * @param supplierId 供应商编号 |
| | |
| | | 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. 校验已上传发票附件 |
| | | // 2. 校验已上传发票附件 |
| | | if (CollUtil.isEmpty(storageAttachmentApi.listAttachments( |
| | | ErpPurchaseInvoiceServiceImpl.PURCHASE_INVOICE_RECORD_TYPE, invoiceId))) { |
| | | throw exception(FINANCE_PAYMENT_CREATE_FAIL_INVOICE_NOT_ATTACHMENT); |
| | | } |
| | | // 4. 校验供应商与来票一致(防脏数据) |
| | | // 3. 校验供应商与来票一致(防脏数据) |
| | | if (ObjectUtil.notEqual(invoice.getSupplierId(), supplierId)) { |
| | | throw exception(FINANCE_PAYMENT_CREATE_FAIL_INVOICE_SUPPLIER_MISMATCH); |
| | | } |