| | |
| | | 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.dataobject.purchase.ErpPurchaseOrderItemDO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseRequestDO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseRequestItemDO; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpPurchaseOrderItemMapper; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpPurchaseOrderMapper; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpPurchaseRequestItemMapper; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpPurchaseRequestMapper; |
| | | 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.service.finance.ErpAccountService; |
| | | import cn.iocoder.yudao.module.mes.api.arrivalnotice.MesArrivalNoticeApi; |
| | | 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 org.springframework.validation.annotation.Validated; |
| | |
| | | */ |
| | | @Service |
| | | @Validated |
| | | @Slf4j |
| | | public class ErpPurchaseOrderServiceImpl implements ErpPurchaseOrderService { |
| | | |
| | | /** |
| | | * BPM 采购订单审批分类编码 |
| | | */ |
| | | private static final String PURCHASE_ORDER_APPROVE_CATEGORY_CODE = "purchase_request_approve"; |
| | | |
| | | @Resource |
| | | private ErpPurchaseOrderMapper purchaseOrderMapper; |
| | |
| | | private ErpSupplierService supplierService; |
| | | @Resource |
| | | private ErpAccountService accountService; |
| | | |
| | | @Resource |
| | | private BpmProcessInstanceApi processInstanceApi; |
| | | @Resource |
| | | private BpmCategoryService bpmCategoryService; |
| | | @Resource |
| | | private BpmProcessDefinitionService bpmProcessDefinitionService; |
| | | |
| | | @Resource |
| | | @Lazy |
| | | private MesArrivalNoticeApi arrivalNoticeApi; |
| | | |
| | | @Resource |
| | | private ErpPurchaseRequestItemMapper purchaseRequestItemMapper; |
| | | @Resource |
| | | private ErpPurchaseRequestMapper purchaseRequestMapper; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | throw exception(PURCHASE_ORDER_NO_EXISTS); |
| | | } |
| | | |
| | | // 2.1 插入订单(采购订单不需要审批,直接为已审核状态) |
| | | // 2.1 插入订单(采购订单创建后为未审核状态,需要提交审批) |
| | | ErpPurchaseOrderDO purchaseOrder = BeanUtils.toBean(createReqVO, ErpPurchaseOrderDO.class, in -> in |
| | | .setNo(no).setStatus(ErpAuditStatus.APPROVE.getStatus())); |
| | | .setNo(no).setStatus(ErpAuditStatus.PROCESS.getStatus()) |
| | | .setInCount(BigDecimal.ZERO).setInStatus(ErpPurchaseOrderInStatusEnum.NOT_IN.getStatus())); |
| | | calculateTotalPrice(purchaseOrder, purchaseOrderItems); |
| | | purchaseOrderMapper.insert(purchaseOrder); |
| | | // 2.2 插入订单项 |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updatePurchaseOrderStatus(Long id, Integer status) { |
| | | // 采购订单不再支持状态流转(不需要审批) |
| | | // 此方法保留用于特殊情况(如反审核后重新审核) |
| | | boolean approve = ErpAuditStatus.APPROVE.getStatus().equals(status); |
| | | // 1.1 校验存在 |
| | | ErpPurchaseOrderDO purchaseOrder = validatePurchaseOrderExists(id); |
| | |
| | | if (updateCount == 0) { |
| | | 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. 校验存在 + 未审核状态 |
| | | ErpPurchaseOrderDO purchaseOrder = validatePurchaseOrderExists(id); |
| | | if (!ErpAuditStatus.PROCESS.getStatus().equals(purchaseOrder.getStatus())) { |
| | | throw exception(PURCHASE_ORDER_SUBMIT_FAIL_STATUS); |
| | | } |
| | | |
| | | // 2. 创建 BPM 流程实例 |
| | | String processInstanceId = processInstanceApi.createProcessInstance(userId, |
| | | new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(processDefinitionKey) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | |
| | | // 3. 更新采购订单状态为审批中 |
| | | purchaseOrderMapper.updateById(new ErpPurchaseOrderDO() |
| | | .setId(id) |
| | | .setStatus(ErpAuditStatus.APPROVING.getStatus()) |
| | | .setProcessInstanceId(processInstanceId)); |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getPurchaseOrderApproveProcessDefinitionList() { |
| | | // 1. 校验分类和流程定义是否存在 |
| | | validatePurchaseOrderApproveCategoryAndProcessDefinition(); |
| | | |
| | | // 2. 获取分类下的流程定义信息 |
| | | List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService |
| | | .getProcessDefinitionInfoListByCategory(PURCHASE_ORDER_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; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updatePurchaseOrderAuditStatus(Long id, Integer bpmResult) { |
| | | // 1. 校验存在 |
| | | ErpPurchaseOrderDO purchaseOrder = validatePurchaseOrderExists(id); |
| | | if (!ErpAuditStatus.APPROVING.getStatus().equals(purchaseOrder.getStatus())) { |
| | | log.warn("[updatePurchaseOrderAuditStatus] 采购订单({}) 不处于审批中状态", id); |
| | | return; |
| | | } |
| | | |
| | | // 2. 根据审批结果更新状态 |
| | | Integer newStatus = convertBpmResultToAuditStatus(bpmResult); |
| | | if (newStatus != null) { |
| | | purchaseOrderMapper.updateById(new ErpPurchaseOrderDO() |
| | | .setId(id).setStatus(newStatus)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 校验采购订单审批分类和流程定义是否存在 |
| | | */ |
| | | 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 审批结果为审核状态 |
| | | */ |
| | | 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<ErpPurchaseOrderItemDO> validatePurchaseOrderItems(List<ErpPurchaseOrderSaveReqVO.Item> list) { |
| | |
| | | if (CollUtil.isEmpty(items)) { |
| | | return; |
| | | } |
| | | // 3. 计算入库状态 |
| | | // 3. 计算入库状态和总入库数量 |
| | | int inStatus = calculateInStatus(items); |
| | | // 4. 更新采购订单入库状态 |
| | | BigDecimal totalInCount = calculateTotalInCount(items); |
| | | // 4. 更新采购订单入库状态和入库数量 |
| | | purchaseOrderMapper.updateById(new ErpPurchaseOrderDO() |
| | | .setId(orderId).setInStatus(inStatus)); |
| | | .setId(orderId).setInStatus(inStatus).setInCount(totalInCount)); |
| | | |
| | | // 5. 回写采购申请明细的入库数量和采购申请的入库状态 |
| | | updatePurchaseRequestInStatus(items, inStatus); |
| | | } |
| | | |
| | | /** |
| | | * 计算总入库数量 |
| | | */ |
| | | private BigDecimal calculateTotalInCount(List<ErpPurchaseOrderItemDO> items) { |
| | | BigDecimal total = BigDecimal.ZERO; |
| | | for (ErpPurchaseOrderItemDO item : items) { |
| | | if (item.getInCount() != null) { |
| | | total = total.add(item.getInCount()); |
| | | } |
| | | } |
| | | return total; |
| | | } |
| | | |
| | | /** |
| | | * 回写采购申请入库数量和入库状态 |
| | | * |
| | | * @param orderItems 采购订单明细列表 |
| | | * @param orderInStatus 采购订单入库状态 |
| | | */ |
| | | private void updatePurchaseRequestInStatus(List<ErpPurchaseOrderItemDO> orderItems, int orderInStatus) { |
| | | // 获取所有关联的采购申请明细 ID |
| | | Set<Long> requestItemIds = new HashSet<>(); |
| | | for (ErpPurchaseOrderItemDO item : orderItems) { |
| | | if (item.getRequestItemId() != null) { |
| | | requestItemIds.add(item.getRequestItemId()); |
| | | } |
| | | } |
| | | if (requestItemIds.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | // 更新采购申请明细的入库数量 |
| | | for (ErpPurchaseOrderItemDO orderItem : orderItems) { |
| | | if (orderItem.getRequestItemId() != null && orderItem.getInCount() != null) { |
| | | purchaseRequestItemMapper.updateById(new ErpPurchaseRequestItemDO() |
| | | .setId(orderItem.getRequestItemId()) |
| | | .setInCount(orderItem.getInCount())); |
| | | } |
| | | } |
| | | |
| | | // 获取采购申请明细,找到对应的采购申请 ID |
| | | List<ErpPurchaseRequestItemDO> requestItems = purchaseRequestItemMapper.selectBatchIds(requestItemIds); |
| | | if (CollUtil.isEmpty(requestItems)) { |
| | | return; |
| | | } |
| | | Set<Long> requestIds = convertSet(requestItems, ErpPurchaseRequestItemDO::getRequestId); |
| | | |
| | | // 更新采购申请的入库状态 |
| | | for (Long requestId : requestIds) { |
| | | purchaseRequestMapper.updateById(new ErpPurchaseRequestDO() |
| | | .setId(requestId) |
| | | .setInStatus(orderInStatus)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long confirmReceipt(Long id) { |
| | | // 1. 校验采购订单存在且已审核 |
| | | ErpPurchaseOrderDO purchaseOrder = validatePurchaseOrder(id); |
| | | |
| | | // 2. 校验采购订单未全部入库 |
| | | if (ErpPurchaseOrderInStatusEnum.ALL_IN.getStatus().equals(purchaseOrder.getInStatus())) { |
| | | throw exception(PURCHASE_ORDER_ALL_IN); |
| | | } |
| | | |
| | | // 3. 调用 MES 接口生成到货通知单 |
| | | Long arrivalNoticeId = arrivalNoticeApi.generateArrivalNoticeFromPurchaseOrder(id); |
| | | log.info("[confirmReceipt] 采购订单({}) 确认收货,生成到货通知单({})", id, arrivalNoticeId); |
| | | |
| | | return arrivalNoticeId; |
| | | } |
| | | |
| | | } |