| | |
| | | package cn.iocoder.yudao.module.erp.service.purchase; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.number.MoneyUtils; |
| | | 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.purchase.vo.request.ErpPurchaseRequestPageReqVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.request.ErpPurchaseRequestSaveReqVO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseOrderDO; |
| | |
| | | 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.ErpPurchaseRequestStatusEnum; |
| | | import cn.iocoder.yudao.module.system.api.approval.ApprovalConfigApi; |
| | | import cn.iocoder.yudao.module.system.api.dept.DeptApi; |
| | | 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 ErpPurchaseRequestServiceImpl implements ErpPurchaseRequestService { |
| | | |
| | | /** |
| | | * BPM 采购申请审批分类编码 |
| | | * 通用审批配置的业务类型编码(对应 system_approval_config.biz_type) |
| | | */ |
| | | private static final String PURCHASE_REQUEST_APPROVE_CATEGORY_CODE = "purchase_request_approve"; |
| | | private static final String PURCHASE_REQUEST_APPROVE_BIZ_TYPE = "purchase_request_approve"; |
| | | |
| | | @Resource |
| | | private ErpPurchaseRequestMapper purchaseRequestMapper; |
| | |
| | | private ErpSupplierService supplierService; |
| | | |
| | | @Resource |
| | | private BpmProcessInstanceApi processInstanceApi; |
| | | @Resource |
| | | private BpmCategoryService bpmCategoryService; |
| | | @Resource |
| | | private BpmProcessDefinitionService bpmProcessDefinitionService; |
| | | private ApprovalConfigApi approvalConfigApi; |
| | | |
| | | @Resource |
| | | private AdminUserApi adminUserApi; |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updatePurchaseRequest(ErpPurchaseRequestSaveReqVO updateReqVO) { |
| | | // 1. 校验存在 + 草稿状态 |
| | | ErpPurchaseRequestDO purchaseRequest = validatePurchaseRequestExistsAndDraft(updateReqVO.getId()); |
| | | ErpPurchaseRequestDO purchaseRequest = validatePurchaseRequestCanModify(updateReqVO.getId()); |
| | | // 2. 校验供应商(如果指定) |
| | | if (updateReqVO.getSupplierId() != null) { |
| | | supplierService.validateSupplier(updateReqVO.getSupplierId()); |
| | |
| | | updatePurchaseRequestItemList(updateReqVO.getId(), requestItems); |
| | | |
| | | // 6. 更新附件 |
| | | if (updateReqVO.getBlobIds() != null) { |
| | | storageAttachmentApi.deleteAttachmentsByRecord("erp_purchase_request", updateReqVO.getId()); |
| | | storageAttachmentApi.bindAttachments("file", "erp_purchase_request", |
| | | updateReqVO.getId(), updateReqVO.getBlobIds()); |
| | | } |
| | | storageAttachmentApi.updateAttachments("file", "erp_purchase_request", updateReqVO.getId(), updateReqVO.getBlobIds()); |
| | | } |
| | | |
| | | private void calculateTotalPrice(ErpPurchaseRequestDO purchaseRequest, List<ErpPurchaseRequestItemDO> requestItems) { |
| | |
| | | return; |
| | | } |
| | | purchaseRequests.forEach(purchaseRequest -> { |
| | | // 只有草稿状态才能删除 |
| | | if (!ErpPurchaseRequestStatusEnum.DRAFT.getStatus().equals(purchaseRequest.getStatus())) { |
| | | Integer status = purchaseRequest.getStatus(); |
| | | if (!ErpPurchaseRequestStatusEnum.DRAFT.getStatus().equals(status) |
| | | && !ErpPurchaseRequestStatusEnum.REJECT.getStatus().equals(status)) { |
| | | throw exception(PURCHASE_REQUEST_DELETE_FAIL_NOT_DRAFT, purchaseRequest.getNo()); |
| | | } |
| | | }); |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void submitPurchaseRequest(Long id, String processDefinitionKey, Long userId) { |
| | | // 1. 校验存在 + 草稿状态 |
| | | ErpPurchaseRequestDO purchaseRequest = validatePurchaseRequestExistsAndDraft(id); |
| | | |
| | | public void submitPurchaseRequest(Long id) { |
| | | // 1. 校验存在 + 可提交状态(未提交 / 审核不通过) |
| | | ErpPurchaseRequestDO purchaseRequest = validatePurchaseRequestExists(id); |
| | | if (!ErpPurchaseRequestStatusEnum.DRAFT.getStatus().equals(purchaseRequest.getStatus()) |
| | | && !ErpPurchaseRequestStatusEnum.REJECT.getStatus().equals(purchaseRequest.getStatus())) { |
| | | throw exception(PURCHASE_REQUEST_SUBMIT_FAIL_NOT_ALLOWED); |
| | | } |
| | | // 2. 校验是否有明细 |
| | | List<ErpPurchaseRequestItemDO> items = purchaseRequestItemMapper.selectListByRequestId(id); |
| | | if (CollUtil.isEmpty(items)) { |
| | | throw exception(PURCHASE_REQUEST_NOT_EXISTS); // TODO: 添加明细为空的错误码 |
| | | throw exception(PURCHASE_REQUEST_ITEM_EMPTY); |
| | | } |
| | | |
| | | // 3. 创建 BPM 流程实例 |
| | | String processInstanceId = processInstanceApi.createProcessInstance(userId, |
| | | new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(processDefinitionKey) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | |
| | | // 4. 更新采购申请状态为审批中 |
| | | purchaseRequestMapper.updateById(new ErpPurchaseRequestDO() |
| | | .setId(id) |
| | | .setStatus(ErpPurchaseRequestStatusEnum.PROCESS.getStatus()) |
| | | .setProcessInstanceId(processInstanceId)); |
| | | // 3. 校验审批配置已启用且配置了有效审批人 |
| | | approvalConfigApi.validateApprovalEnabledAndGetApprovers(PURCHASE_REQUEST_APPROVE_BIZ_TYPE); |
| | | // 4. 状态置为审批中,并清空上一轮审核结果 |
| | | purchaseRequestMapper.submitAndResetAuditInfo(id, ErpPurchaseRequestStatusEnum.PROCESS.getStatus()); |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getPurchaseRequestApproveProcessDefinitionList() { |
| | | // 1. 校验分类和流程定义是否存在 |
| | | validatePurchaseRequestApproveCategoryAndProcessDefinition(); |
| | | |
| | | // 2. 获取分类下的流程定义信息 |
| | | List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService |
| | | .getProcessDefinitionInfoListByCategory(PURCHASE_REQUEST_APPROVE_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> getPurchaseRequestApproverUserIds() { |
| | | return approvalConfigApi.getApproverUserIds(PURCHASE_REQUEST_APPROVE_BIZ_TYPE); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updatePurchaseRequestAuditStatus(Long id, Integer bpmResult) { |
| | | public void auditPurchaseRequest(Long id, Boolean pass, String reviewRemark) { |
| | | // 1. 校验存在 |
| | | ErpPurchaseRequestDO purchaseRequest = validatePurchaseRequestExists(id); |
| | | // 2. 校验处于审批中状态 |
| | | if (!ErpPurchaseRequestStatusEnum.PROCESS.getStatus().equals(purchaseRequest.getStatus())) { |
| | | log.warn("[updatePurchaseRequestAuditStatus] 采购申请({}) 不处于审批中状态", id); |
| | | throw exception(PURCHASE_REQUEST_AUDIT_FAIL_NOT_PROCESS); |
| | | } |
| | | // 3. 校验当前登录用户是该业务类型的审批人(或签:任一人均可审核) |
| | | Long userId = getLoginUserId(); |
| | | approvalConfigApi.validateApprover(PURCHASE_REQUEST_APPROVE_BIZ_TYPE, userId); |
| | | // 4. 审核不通过:必须填写原因,退回申请人修改后可重新提交 |
| | | if (!Boolean.TRUE.equals(pass)) { |
| | | if (StrUtil.isBlank(reviewRemark)) { |
| | | throw exception(PURCHASE_REQUEST_AUDIT_REJECT_REASON_REQUIRED); |
| | | } |
| | | purchaseRequestMapper.auditPurchaseRequest(id, ErpPurchaseRequestStatusEnum.REJECT.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | return; |
| | | } |
| | | |
| | | // 2. 根据审批结果更新状态 |
| | | Integer newStatus = convertBpmResultToStatus(bpmResult); |
| | | if (newStatus != null) { |
| | | purchaseRequestMapper.updateById(new ErpPurchaseRequestDO() |
| | | .setId(id).setStatus(newStatus)); |
| | | } |
| | | |
| | | // 3. 审批通过且有供应商,自动生成采购订单 |
| | | if (ErpPurchaseRequestStatusEnum.APPROVE.getStatus().equals(newStatus) |
| | | && purchaseRequest.getSupplierId() != null |
| | | && purchaseRequest.getOrderId() == null) { |
| | | // 5. 审核通过:记录审核人与审核意见 |
| | | purchaseRequestMapper.auditPurchaseRequest(id, ErpPurchaseRequestStatusEnum.APPROVE.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | // 6. 审核通过且有供应商、未生成订单时,自动生成采购订单 |
| | | if (purchaseRequest.getSupplierId() != null && purchaseRequest.getOrderId() == null) { |
| | | try { |
| | | Long orderId = doGeneratePurchaseOrder(purchaseRequest); |
| | | log.info("[updatePurchaseRequestAuditStatus] 采购申请({}) 自动生成采购订单({})", id, orderId); |
| | | log.info("[auditPurchaseRequest] 采购申请({}) 自动生成采购订单({})", id, orderId); |
| | | } catch (Exception e) { |
| | | log.error("[updatePurchaseRequestAuditStatus] 采购申请({}) 自动生成采购订单失败", id, e); |
| | | // 不抛出异常,避免影响状态更新 |
| | | // 不抛出异常,避免影响审核结果落库;生成失败可由用户手动触发 |
| | | log.error("[auditPurchaseRequest] 采购申请({}) 自动生成采购订单失败", id, e); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取用户昵称 |
| | | * |
| | | * @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(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | if (CollUtil.isEmpty(requestItems)) { |
| | | throw exception(PURCHASE_REQUEST_NOT_EXISTS); |
| | | } |
| | | requestItems.forEach(item -> { |
| | | if (item.getProductPrice() == null || item.getTotalPrice() == null || item.getTaxPrice() == null) { |
| | | throw exception(PURCHASE_REQUEST_ITEM_PRICE_EMPTY, item.getId()); |
| | | } |
| | | }); |
| | | |
| | | // 2. 生成采购订单号 |
| | | String orderNo = noRedisDAO.generate(ErpNoRedisDAO.PURCHASE_ORDER_NO_PREFIX); |
| | |
| | | return purchaseOrder.getId(); |
| | | } |
| | | |
| | | /** |
| | | * 校验采购申请审批分类和流程定义是否存在 |
| | | */ |
| | | private void validatePurchaseRequestApproveCategoryAndProcessDefinition() { |
| | | List<BpmCategoryDO> categories = bpmCategoryService.getCategoryListByCode( |
| | | Collections.singletonList(PURCHASE_REQUEST_APPROVE_CATEGORY_CODE)); |
| | | if (CollUtil.isEmpty(categories)) { |
| | | throw exception(PURCHASE_REQUEST_BPM_CATEGORY_NOT_EXISTS); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转换 BPM 审批结果为状态 |
| | | */ |
| | | private Integer convertBpmResultToStatus(Integer bpmResult) { |
| | | if (BpmTaskStatusEnum.APPROVE.getStatus().equals(bpmResult)) { |
| | | return ErpPurchaseRequestStatusEnum.APPROVE.getStatus(); |
| | | } else if (BpmTaskStatusEnum.REJECT.getStatus().equals(bpmResult)) { |
| | | return ErpPurchaseRequestStatusEnum.REJECT.getStatus(); |
| | | } else if (BpmTaskStatusEnum.CANCEL.getStatus().equals(bpmResult)) { |
| | | return ErpPurchaseRequestStatusEnum.CANCEL.getStatus(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private ErpPurchaseRequestDO validatePurchaseRequestExists(Long id) { |
| | | ErpPurchaseRequestDO purchaseRequest = purchaseRequestMapper.selectById(id); |
| | | if (purchaseRequest == null) { |
| | |
| | | return purchaseRequest; |
| | | } |
| | | |
| | | private ErpPurchaseRequestDO validatePurchaseRequestExistsAndDraft(Long id) { |
| | | private ErpPurchaseRequestDO validatePurchaseRequestCanModify(Long id) { |
| | | ErpPurchaseRequestDO purchaseRequest = validatePurchaseRequestExists(id); |
| | | if (!ErpPurchaseRequestStatusEnum.DRAFT.getStatus().equals(purchaseRequest.getStatus())) { |
| | | Integer status = purchaseRequest.getStatus(); |
| | | if (!ErpPurchaseRequestStatusEnum.DRAFT.getStatus().equals(status) |
| | | && !ErpPurchaseRequestStatusEnum.REJECT.getStatus().equals(status)) { |
| | | throw exception(PURCHASE_REQUEST_UPDATE_FAIL_NOT_DRAFT, purchaseRequest.getNo()); |
| | | } |
| | | return purchaseRequest; |