| | |
| | | |
| | | 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.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.order.ErpPurchaseOrderPageReqVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.order.ErpPurchaseOrderSaveReqVO; |
| | | 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.ErpPurchaseOrderInStatusEnum; |
| | | import cn.iocoder.yudao.module.erp.enums.ErpPurchaseOrderStatusEnum; |
| | | import cn.iocoder.yudao.module.erp.service.finance.ErpAccountService; |
| | | import cn.iocoder.yudao.module.mes.api.arrivalnotice.MesArrivalNoticeApi; |
| | | 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.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | |
| | | 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 ErpPurchaseOrderServiceImpl implements ErpPurchaseOrderService { |
| | | |
| | | /** |
| | | * BPM 采购订单审批分类编码 |
| | | * 采购订单审批业务类型编码(对应 system_approval_config.biz_type) |
| | | * |
| | | * 注意:改造为轻量级状态机审批前,此处原为 "purchase_request_approve", |
| | | * 与采购申请(ErpPurchaseRequestServiceImpl)的 BPM 分类编码完全相同, |
| | | * 导致采购订单查询审批流程时会命中采购申请的流程定义,两者互相串用。 |
| | | * 现更正为采购订单专属编码。 |
| | | */ |
| | | private static final String PURCHASE_ORDER_APPROVE_CATEGORY_CODE = "purchase_request_approve"; |
| | | private static final String PURCHASE_ORDER_APPROVE_BIZ_TYPE = "erp_purchase_order_approve"; |
| | | |
| | | @Resource |
| | | private ErpPurchaseOrderMapper purchaseOrderMapper; |
| | |
| | | private ErpAccountService accountService; |
| | | |
| | | @Resource |
| | | private BpmProcessInstanceApi processInstanceApi; |
| | | private ApprovalConfigApi approvalConfigApi; |
| | | @Resource |
| | | private BpmCategoryService bpmCategoryService; |
| | | @Resource |
| | | private BpmProcessDefinitionService bpmProcessDefinitionService; |
| | | private AdminUserApi adminUserApi; |
| | | |
| | | @Resource |
| | | @Lazy |
| | |
| | | throw exception(PURCHASE_ORDER_NO_EXISTS); |
| | | } |
| | | |
| | | // 2.1 插入订单(采购订单创建后为未审核状态,需要提交审批) |
| | | // 2.1 插入订单(采购订单创建后为【未提交】状态,需提交进入审批流程) |
| | | ErpPurchaseOrderDO purchaseOrder = BeanUtils.toBean(createReqVO, ErpPurchaseOrderDO.class, in -> in |
| | | .setNo(no).setStatus(ErpAuditStatus.PROCESS.getStatus()) |
| | | .setNo(no).setStatus(ErpPurchaseOrderStatusEnum.DRAFT.getStatus()) |
| | | .setInCount(BigDecimal.ZERO).setInStatus(ErpPurchaseOrderInStatusEnum.NOT_IN.getStatus())); |
| | | calculateTotalPrice(purchaseOrder, purchaseOrderItems); |
| | | purchaseOrderMapper.insert(purchaseOrder); |
| | |
| | | public void updatePurchaseOrder(ErpPurchaseOrderSaveReqVO updateReqVO) { |
| | | // 1.1 校验存在 |
| | | ErpPurchaseOrderDO purchaseOrder = validatePurchaseOrderExists(updateReqVO.getId()); |
| | | // 采购订单不允许修改(已审核状态) |
| | | if (ErpAuditStatus.APPROVE.getStatus().equals(purchaseOrder.getStatus())) { |
| | | // 已审核状态不允许修改 |
| | | if (ErpPurchaseOrderStatusEnum.APPROVE.getStatus().equals(purchaseOrder.getStatus())) { |
| | | throw exception(PURCHASE_ORDER_UPDATE_FAIL_APPROVE, purchaseOrder.getNo()); |
| | | } |
| | | // 审批中不允许修改:需先由审批人「审核不通过」退回,才能重新修改并提交 |
| | | if (ErpPurchaseOrderStatusEnum.PROCESS.getStatus().equals(purchaseOrder.getStatus())) { |
| | | throw exception(PURCHASE_ORDER_UPDATE_FAIL_PROCESS, purchaseOrder.getNo()); |
| | | } |
| | | // 1.2 校验供应商 |
| | | supplierService.validateSupplier(updateReqVO.getSupplierId()); |
| | |
| | | boolean approve = ErpAuditStatus.APPROVE.getStatus().equals(status); |
| | | // 1.1 校验存在 |
| | | ErpPurchaseOrderDO purchaseOrder = validatePurchaseOrderExists(id); |
| | | // 1.1.1 审核通过属于审批动作,必须由配置内的审批人执行(服务端强制,避免绕过审核按钮自助通过) |
| | | if (approve) { |
| | | approvalConfigApi.validateApprover(PURCHASE_ORDER_APPROVE_BIZ_TYPE, getLoginUserId()); |
| | | } |
| | | // 1.2 校验状态 |
| | | if (purchaseOrder.getStatus().equals(status)) { |
| | | throw exception(approve ? PURCHASE_ORDER_APPROVE_FAIL : PURCHASE_ORDER_PROCESS_FAIL); |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void submitPurchaseOrder(Long id, String processDefinitionKey, Long userId) { |
| | | // 1. 校验存在 + 未审核状态 |
| | | public void submitPurchaseOrder(Long id) { |
| | | // 1.1 校验存在 |
| | | ErpPurchaseOrderDO purchaseOrder = validatePurchaseOrderExists(id); |
| | | if (!ErpAuditStatus.PROCESS.getStatus().equals(purchaseOrder.getStatus())) { |
| | | throw exception(PURCHASE_ORDER_SUBMIT_FAIL_STATUS); |
| | | // 1.2 只有【未提交】或【审核不通过】才允许提交 |
| | | if (!ErpPurchaseOrderStatusEnum.DRAFT.getStatus().equals(purchaseOrder.getStatus()) |
| | | && !ErpPurchaseOrderStatusEnum.REJECT.getStatus().equals(purchaseOrder.getStatus())) { |
| | | throw exception(PURCHASE_ORDER_SUBMIT_FAIL_NOT_ALLOWED); |
| | | } |
| | | // 1.3 必须配置订单项 |
| | | List<ErpPurchaseOrderItemDO> items = purchaseOrderItemMapper.selectListByOrderId(id); |
| | | if (CollUtil.isEmpty(items)) { |
| | | throw exception(PURCHASE_ORDER_SUBMIT_FAIL_ITEM_EMPTY); |
| | | } |
| | | |
| | | // 2. 创建 BPM 流程实例 |
| | | String processInstanceId = processInstanceApi.createProcessInstance(userId, |
| | | new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(processDefinitionKey) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | // 2. 校验审批已开启且配置了有效审批人(未开启审批时不允许提交) |
| | | approvalConfigApi.validateApprovalEnabledAndGetApprovers(PURCHASE_ORDER_APPROVE_BIZ_TYPE); |
| | | |
| | | // 3. 更新采购订单状态为审批中 |
| | | purchaseOrderMapper.updateById(new ErpPurchaseOrderDO() |
| | | .setId(id) |
| | | .setStatus(ErpAuditStatus.APPROVING.getStatus()) |
| | | .setProcessInstanceId(processInstanceId)); |
| | | // 3. 置为审批中,并清空上一轮审核信息,避免驳回原因残留 |
| | | purchaseOrderMapper.submitAndResetAuditInfo(id, ErpPurchaseOrderStatusEnum.PROCESS.getStatus()); |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getPurchaseOrderApproveProcessDefinitionList() { |
| | | // 1. 校验分类和流程定义是否存在 |
| | | validatePurchaseOrderApproveCategoryAndProcessDefinition(); |
| | | public Set<Long> getPurchaseOrderApproverUserIds() { |
| | | return approvalConfigApi.getApproverUserIds(PURCHASE_ORDER_APPROVE_BIZ_TYPE); |
| | | } |
| | | |
| | | // 2. 获取分类下的流程定义信息 |
| | | List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService |
| | | .getProcessDefinitionInfoListByCategory(PURCHASE_ORDER_APPROVE_CATEGORY_CODE); |
| | | if (CollUtil.isEmpty(definitionInfoList)) { |
| | | return Collections.emptyList(); |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void auditPurchaseOrder(Long id, Boolean pass, String reviewRemark) { |
| | | // 1.1 校验存在 + 审批中状态 |
| | | ErpPurchaseOrderDO purchaseOrder = validatePurchaseOrderExists(id); |
| | | if (!ErpPurchaseOrderStatusEnum.PROCESS.getStatus().equals(purchaseOrder.getStatus())) { |
| | | throw exception(PURCHASE_ORDER_AUDIT_FAIL_NOT_PROCESS); |
| | | } |
| | | // 1.2 校验当前登录人是配置的审批人(或签:任一人审核即可) |
| | | Long userId = getLoginUserId(); |
| | | approvalConfigApi.validateApprover(PURCHASE_ORDER_APPROVE_BIZ_TYPE, userId); |
| | | |
| | | // 3. 遍历获取流程定义详情,过滤激活状态,保留最新版本 |
| | | Map<String, ProcessDefinition> latestVersionMap = new HashMap<>(); |
| | | for (BpmProcessDefinitionInfoDO info : definitionInfoList) { |
| | | ProcessDefinition pd = bpmProcessDefinitionService.getProcessDefinition(info.getProcessDefinitionId()); |
| | | if (pd == null || pd.isSuspended()) { |
| | | continue; |
| | | // 2. 审核不通过:必须填写驳回原因,单据退回后可编辑并重新提交 |
| | | if (!Boolean.TRUE.equals(pass)) { |
| | | if (StrUtil.isBlank(reviewRemark)) { |
| | | throw exception(PURCHASE_ORDER_AUDIT_REJECT_REASON_REQUIRED); |
| | | } |
| | | ProcessDefinition existing = latestVersionMap.get(pd.getKey()); |
| | | if (existing == null || pd.getVersion() > existing.getVersion()) { |
| | | latestVersionMap.put(pd.getKey(), pd); |
| | | } |
| | | purchaseOrderMapper.auditPurchaseOrder(id, ErpPurchaseOrderStatusEnum.REJECT.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | return; |
| | | } |
| | | |
| | | // 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); |
| | | // 3. 审核通过 |
| | | purchaseOrderMapper.auditPurchaseOrder(id, ErpPurchaseOrderStatusEnum.APPROVE.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户昵称,用于冗余存储审核人姓名 |
| | | */ |
| | | private String getUserNickname(Long userId) { |
| | | if (userId == null) { |
| | | return null; |
| | | } |
| | | return result; |
| | | AdminUserRespDTO user = adminUserApi.getUser(userId); |
| | | return user == null ? null : user.getNickname(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | public void updatePurchaseOrderAuditStatus(Long id, Integer bpmResult) { |
| | | // 1. 校验存在 |
| | | ErpPurchaseOrderDO purchaseOrder = validatePurchaseOrderExists(id); |
| | | if (!ErpAuditStatus.APPROVING.getStatus().equals(purchaseOrder.getStatus())) { |
| | | if (!ErpPurchaseOrderStatusEnum.PROCESS.getStatus().equals(purchaseOrder.getStatus())) { |
| | | log.warn("[updatePurchaseOrderAuditStatus] 采购订单({}) 不处于审批中状态", id); |
| | | return; |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * 校验采购订单审批分类和流程定义是否存在 |
| | | */ |
| | | private void validatePurchaseOrderApproveCategoryAndProcessDefinition() { |
| | | // 1. 校验分类是否存在 |
| | | List<BpmCategoryDO> categories = bpmCategoryService.getCategoryListByCode( |
| | | Collections.singletonList(PURCHASE_ORDER_APPROVE_CATEGORY_CODE)); |
| | | if (CollUtil.isEmpty(categories)) { |
| | | throw exception(PURCHASE_ORDER_BPM_CATEGORY_NOT_EXISTS); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转换 BPM 审批结果为审核状态 |
| | | * |
| | | * 已废弃:采购订单不再走 BPM 流程,本方法仅为历史代码保留,不再被调用。 |
| | | */ |
| | | @Deprecated |
| | | private Integer convertBpmResultToAuditStatus(Integer bpmResult) { |
| | | if (BpmTaskStatusEnum.APPROVE.getStatus().equals(bpmResult)) { |
| | | return ErpAuditStatus.APPROVE.getStatus(); |
| | | return ErpPurchaseOrderStatusEnum.APPROVE.getStatus(); |
| | | } else if (BpmTaskStatusEnum.REJECT.getStatus().equals(bpmResult)) { |
| | | return ErpAuditStatus.PROCESS.getStatus(); |
| | | return ErpPurchaseOrderStatusEnum.REJECT.getStatus(); |
| | | } else if (BpmTaskStatusEnum.CANCEL.getStatus().equals(bpmResult)) { |
| | | return ErpAuditStatus.CANCEL.getStatus(); |
| | | return ErpPurchaseOrderStatusEnum.CANCEL.getStatus(); |
| | | } |
| | | return null; |
| | | } |