| | |
| | | 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.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.service.finance.ErpAccountService; |
| | | import cn.iocoder.yudao.module.erp.service.product.ErpProductService; |
| | | import jakarta.annotation.Resource; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*; |
| | |
| | | /** |
| | | * ERP 采购订单 Service 实现类 |
| | | * |
| | | * 注意:采购订单不需要 BPM 审批,审批流程在采购申请阶段完成。 |
| | | * 采购订单由采购申请生成,或直接创建(状态为已审核)。 |
| | | * |
| | | * @author 芋道源码 |
| | | */ |
| | | @Service |
| | | @Validated |
| | | @Slf4j |
| | | public class ErpPurchaseOrderServiceImpl implements ErpPurchaseOrderService { |
| | | |
| | | /** |
| | | * BPM 采购订单审批流程标识 |
| | | */ |
| | | public static final String BPM_PROCESS_DEFINITION_KEY = "erp-purchase-order-audit"; |
| | | |
| | | @Resource |
| | | private ErpPurchaseOrderMapper purchaseOrderMapper; |
| | |
| | | private ErpSupplierService supplierService; |
| | | @Resource |
| | | private ErpAccountService accountService; |
| | | |
| | | @Resource |
| | | private BpmProcessInstanceApi bpmProcessInstanceApi; |
| | | |
| | | @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.DRAFT.getStatus())); |
| | | .setNo(no).setStatus(ErpAuditStatus.APPROVE.getStatus())); |
| | | calculateTotalPrice(purchaseOrder, purchaseOrderItems); |
| | | purchaseOrderMapper.insert(purchaseOrder); |
| | | // 2.2 插入订单项 |
| | |
| | | public void updatePurchaseOrder(ErpPurchaseOrderSaveReqVO updateReqVO) { |
| | | // 1.1 校验存在 |
| | | ErpPurchaseOrderDO purchaseOrder = validatePurchaseOrderExists(updateReqVO.getId()); |
| | | // 采购订单不允许修改(已审核状态) |
| | | if (ErpAuditStatus.APPROVE.getStatus().equals(purchaseOrder.getStatus())) { |
| | | throw exception(PURCHASE_ORDER_UPDATE_FAIL_APPROVE, purchaseOrder.getNo()); |
| | | } |
| | |
| | | @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 (purchaseOrder.getStatus().equals(status)) { |
| | | throw exception(approve ? PURCHASE_ORDER_APPROVE_FAIL : PURCHASE_ORDER_PROCESS_FAIL); |
| | | } |
| | | // 1.3 存在采购入单,无法反审核 |
| | | if (!approve && purchaseOrder.getInCount().compareTo(BigDecimal.ZERO) > 0) { |
| | | // 1.3 存在采购入库单,无法反审核 |
| | | BigDecimal inCount = purchaseOrder.getInCount(); |
| | | if (!approve && inCount != null && inCount.compareTo(BigDecimal.ZERO) > 0) { |
| | | throw exception(PURCHASE_ORDER_PROCESS_FAIL_EXISTS_IN); |
| | | } |
| | | // 1.4 存在采购退货单,无法反审核 |
| | | if (!approve && purchaseOrder.getReturnCount().compareTo(BigDecimal.ZERO) > 0) { |
| | | BigDecimal returnCount = purchaseOrder.getReturnCount(); |
| | | if (!approve && returnCount != null && returnCount.compareTo(BigDecimal.ZERO) > 0) { |
| | | throw exception(PURCHASE_ORDER_PROCESS_FAIL_EXISTS_RETURN); |
| | | } |
| | | |
| | |
| | | private void updatePurchaseOrderItemList(Long id, List<ErpPurchaseOrderItemDO> newList) { |
| | | // 第一步,对比新老数据,获得添加、修改、删除的列表 |
| | | List<ErpPurchaseOrderItemDO> oldList = purchaseOrderItemMapper.selectListByOrderId(id); |
| | | List<List<ErpPurchaseOrderItemDO>> diffList = diffList(oldList, newList, // id 不同,就认为是不同的记录 |
| | | List<List<ErpPurchaseOrderItemDO>> diffList = diffList(oldList, newList, |
| | | (oldVal, newVal) -> oldVal.getId().equals(newVal.getId())); |
| | | |
| | | // 第二步,批量添加、修改、删除 |
| | |
| | | } |
| | | }); |
| | | |
| | | // 2. 遍历删除,并记录操作日志 |
| | | // 2. 遍历删除 |
| | | purchaseOrders.forEach(purchaseOrder -> { |
| | | // 2.1 删除订单 |
| | | purchaseOrderMapper.deleteById(purchaseOrder.getId()); |
| | | // 2.2 删除订单项 |
| | | purchaseOrderItemMapper.deleteByOrderId(purchaseOrder.getId()); |
| | | }); |
| | | } |
| | |
| | | return purchaseOrderItemMapper.selectListByOrderIds(orderIds); |
| | | } |
| | | |
| | | // ==================== BPM 流程审批 ==================== |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void submitPurchaseOrder(Long id, Long userId) { |
| | | // 1. 校验采购订单处于草稿状态 |
| | | ErpPurchaseOrderDO purchaseOrder = validatePurchaseOrderExists(id); |
| | | if (ObjectUtil.notEqual(purchaseOrder.getStatus(), ErpAuditStatus.DRAFT.getStatus())) { |
| | | throw exception(PURCHASE_ORDER_SUBMIT_FAIL_NOT_DRAFT); |
| | | } |
| | | |
| | | // 2. 创建采购订单审批流程实例 |
| | | String processInstanceId = bpmProcessInstanceApi.createProcessInstance(userId, new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(BPM_PROCESS_DEFINITION_KEY) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | |
| | | // 3. 更新采购订单工作流编号、状态 |
| | | purchaseOrderMapper.updateById(new ErpPurchaseOrderDO() |
| | | .setId(id) |
| | | .setProcessInstanceId(processInstanceId) |
| | | .setStatus(ErpAuditStatus.PROCESS.getStatus())); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updatePurchaseOrderAuditStatus(Long id, Integer bpmResult) { |
| | | // 1. 校验采购订单存在 |
| | | ErpPurchaseOrderDO purchaseOrder = validatePurchaseOrderExists(id); |
| | | |
| | | // 2. 校验采购订单处于审批中 |
| | | if (ObjectUtil.notEqual(purchaseOrder.getStatus(), ErpAuditStatus.PROCESS.getStatus())) { |
| | | log.error("[updatePurchaseOrderAuditStatus][purchaseOrder({}) 不处于审批中,无法更新审批结果({})]", |
| | | purchaseOrder.getId(), bpmResult); |
| | | throw exception(PURCHASE_ORDER_UPDATE_AUDIT_STATUS_FAIL_NOT_PROCESS); |
| | | } |
| | | |
| | | // 3. 更新采购订单审批结果 |
| | | Integer auditStatus = convertBpmResultToAuditStatus(bpmResult); |
| | | purchaseOrderMapper.updateById(new ErpPurchaseOrderDO() |
| | | .setId(id) |
| | | .setStatus(auditStatus)); |
| | | } |
| | | |
| | | /** |
| | | * 将 BPM 审批结果转换为 ERP 审批状态 |
| | | * |
| | | * @param bpmResult BPM 审批结果 |
| | | * @return ERP 审批状态 |
| | | */ |
| | | private Integer convertBpmResultToAuditStatus(Integer bpmResult) { |
| | | // BPM 审批结果:2=通过,3=不通过,4=已取消 |
| | | if (bpmResult == 2) { |
| | | return ErpAuditStatus.APPROVE.getStatus(); |
| | | } else if (bpmResult == 3) { |
| | | return ErpAuditStatus.REJECT.getStatus(); |
| | | } else if (bpmResult == 4) { |
| | | return ErpAuditStatus.DRAFT.getStatus(); |
| | | } else { |
| | | log.warn("[convertBpmResultToAuditStatus][未知的 BPM 审批结果({})]", bpmResult); |
| | | return ErpAuditStatus.PROCESS.getStatus(); |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |