| | |
| | | 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.product.ErpProductDO; |
| | | 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.mysql.purchase.ErpPurchaseOrderItemMapper; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpPurchaseOrderMapper; |
| | | 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.erp.service.product.ErpProductService; |
| | | 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; |
| | |
| | | /** |
| | | * ERP 采购订单 Service 实现类 |
| | | * |
| | | * 注意:采购订单不需要 BPM 审批,审批流程在采购申请阶段完成。 |
| | | * 采购订单由采购申请生成,或直接创建(状态为已审核)。 |
| | | * |
| | | * @author 芋道源码 |
| | | */ |
| | | @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 ErpNoRedisDAO noRedisDAO; |
| | | |
| | | @Resource |
| | | private ErpProductService productService; |
| | | @Resource |
| | | private ErpSupplierService supplierService; |
| | | @Resource |
| | | private ErpAccountService accountService; |
| | | |
| | | @Resource |
| | | private BpmProcessInstanceApi processInstanceApi; |
| | | @Resource |
| | | private BpmCategoryService bpmCategoryService; |
| | | @Resource |
| | | private BpmProcessDefinitionService bpmProcessDefinitionService; |
| | | |
| | | @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); |
| | |
| | | } |
| | | } |
| | | |
| | | @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) { |
| | | // 1. 校验产品存在 |
| | | List<ErpProductDO> productList = productService.validProductList( |
| | | convertSet(list, ErpPurchaseOrderSaveReqVO.Item::getProductId)); |
| | | Map<Long, ErpProductDO> productMap = convertMap(productList, ErpProductDO::getId); |
| | | // 2. 转化为 ErpPurchaseOrderItemDO 列表 |
| | | // 转化为 ErpPurchaseOrderItemDO 列表(产品验证已移除) |
| | | return convertList(list, o -> BeanUtils.toBean(o, ErpPurchaseOrderItemDO.class, item -> { |
| | | item.setProductUnitId(productMap.get(item.getProductId()).getUnitId()); |
| | | item.setTotalPrice(MoneyUtils.priceMultiply(item.getProductPrice(), item.getCount())); |
| | | if (item.getTotalPrice() == null) { |
| | | return; |
| | |
| | | } |
| | | if (inCount.compareTo(item.getCount()) > 0) { |
| | | throw exception(PURCHASE_ORDER_ITEM_IN_FAIL_PRODUCT_EXCEED, |
| | | productService.getProduct(item.getProductId()).getName(), item.getCount()); |
| | | "产品ID:" + item.getProductId(), item.getCount()); |
| | | } |
| | | purchaseOrderItemMapper.updateById(new ErpPurchaseOrderItemDO().setId(item.getId()).setInCount(inCount)); |
| | | }); |
| | |
| | | } |
| | | if (returnCount.compareTo(item.getInCount()) > 0) { |
| | | throw exception(PURCHASE_ORDER_ITEM_RETURN_FAIL_IN_EXCEED, |
| | | productService.getProduct(item.getProductId()).getName(), item.getInCount()); |
| | | "产品ID:" + item.getProductId(), item.getInCount()); |
| | | } |
| | | purchaseOrderItemMapper.updateById(new ErpPurchaseOrderItemDO().setId(item.getId()).setReturnCount(returnCount)); |
| | | }); |
| | |
| | | return purchaseOrderMapper.selectByIds(ids); |
| | | } |
| | | |
| | | @Override |
| | | public void updatePurchaseOrderInStatus(Long orderId) { |
| | | // 1. 获取采购订单 |
| | | ErpPurchaseOrderDO order = validatePurchaseOrderExists(orderId); |
| | | // 2. 获取采购订单明细 |
| | | List<ErpPurchaseOrderItemDO> items = purchaseOrderItemMapper.selectListByOrderId(orderId); |
| | | if (CollUtil.isEmpty(items)) { |
| | | return; |
| | | } |
| | | // 3. 计算入库状态 |
| | | int inStatus = calculateInStatus(items); |
| | | // 4. 更新采购订单入库状态 |
| | | purchaseOrderMapper.updateById(new ErpPurchaseOrderDO() |
| | | .setId(orderId).setInStatus(inStatus)); |
| | | } |
| | | |
| | | /** |
| | | * 计算入库状态 |
| | | * |
| | | * @param items 采购订单明细列表 |
| | | * @return 入库状态 |
| | | */ |
| | | private int calculateInStatus(List<ErpPurchaseOrderItemDO> items) { |
| | | boolean allIn = true; |
| | | boolean anyIn = false; |
| | | |
| | | for (ErpPurchaseOrderItemDO item : items) { |
| | | BigDecimal count = item.getCount() != null ? item.getCount() : BigDecimal.ZERO; |
| | | BigDecimal inCount = item.getInCount() != null ? item.getInCount() : BigDecimal.ZERO; |
| | | |
| | | if (inCount.compareTo(BigDecimal.ZERO) > 0) { |
| | | anyIn = true; |
| | | } |
| | | if (inCount.compareTo(count) < 0) { |
| | | allIn = false; |
| | | } |
| | | } |
| | | |
| | | if (allIn) { |
| | | return ErpPurchaseOrderInStatusEnum.ALL_IN.getStatus(); |
| | | } else if (anyIn) { |
| | | return ErpPurchaseOrderInStatusEnum.PART_IN.getStatus(); |
| | | } else { |
| | | return ErpPurchaseOrderInStatusEnum.NOT_IN.getStatus(); |
| | | } |
| | | } |
| | | |
| | | } |