| | |
| | | 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.exception.ServiceException; |
| | | 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.ErpPurchaseRequestImportExcelVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.request.ErpPurchaseRequestImportReqVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.request.ErpPurchaseRequestImportRespVO; |
| | | 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.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.dataobject.purchase.ErpSupplierDO; |
| | | 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.mysql.purchase.ErpSupplierMapper; |
| | | 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.ErpPurchaseOrderStatusEnum; |
| | | import cn.iocoder.yudao.module.erp.enums.ErpPurchaseRequestStatusEnum; |
| | | import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi; |
| | | import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO; |
| | | import cn.iocoder.yudao.module.mdm.api.unit.MdmUnitMeasureApi; |
| | | import cn.iocoder.yudao.module.mdm.api.unit.dto.MdmUnitMeasureRespDTO; |
| | | 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"; |
| | | |
| | | /** |
| | | * 采购申请审批的 BPM 流程定义 Key |
| | | */ |
| | | private static final String PURCHASE_REQUEST_PROCESS_KEY = "purchase_request_approve"; |
| | | |
| | | @Resource |
| | | private ErpPurchaseRequestMapper purchaseRequestMapper; |
| | |
| | | private ErpSupplierService supplierService; |
| | | |
| | | @Resource |
| | | private BpmProcessInstanceApi processInstanceApi; |
| | | private ErpSupplierMapper supplierMapper; |
| | | |
| | | @Resource |
| | | private BpmCategoryService bpmCategoryService; |
| | | private MdmItemApi mdmItemApi; |
| | | @Resource |
| | | private BpmProcessDefinitionService bpmProcessDefinitionService; |
| | | private MdmUnitMeasureApi mdmUnitMeasureApi; |
| | | |
| | | @Resource |
| | | private ApprovalConfigApi approvalConfigApi; |
| | | |
| | | @Resource |
| | | private BpmProcessInstanceApi bpmProcessInstanceApi; |
| | | |
| | | @Resource |
| | | private AdminUserApi adminUserApi; |
| | |
| | | .setStatus(ErpPurchaseRequestStatusEnum.DRAFT.getStatus()) |
| | | .setRequestUserId(userId) |
| | | .setRequestDeptId(deptId)); |
| | | // 兼容旧客户端未传定金的情况,数据库与业务层统一按 0 处理 |
| | | if (purchaseRequest.getDepositPrice() == null) { |
| | | purchaseRequest.setDepositPrice(BigDecimal.ZERO); |
| | | } |
| | | calculateTotalPrice(purchaseRequest, requestItems); |
| | | purchaseRequestMapper.insert(purchaseRequest); |
| | | |
| | |
| | | @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()); |
| | |
| | | |
| | | // 4. 更新申请单 |
| | | ErpPurchaseRequestDO updateObj = BeanUtils.toBean(updateReqVO, ErpPurchaseRequestDO.class); |
| | | if (updateObj.getDepositPrice() == null) { |
| | | updateObj.setDepositPrice(BigDecimal.ZERO); |
| | | } |
| | | calculateTotalPrice(updateObj, requestItems); |
| | | purchaseRequestMapper.updateById(updateObj); |
| | | |
| | |
| | | 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, |
| | | // 3. 校验审批配置已启用且配置了有效审批人 |
| | | approvalConfigApi.validateApprovalEnabledAndGetApprovers(PURCHASE_REQUEST_APPROVE_BIZ_TYPE); |
| | | // 4. 状态置为审批中,并清空上一轮审核结果 |
| | | if (purchaseRequestMapper.submitAndResetAuditInfo(id, ErpPurchaseRequestStatusEnum.PROCESS.getStatus()) != 1) { |
| | | throw exception(PURCHASE_REQUEST_SUBMIT_FAIL_NOT_ALLOWED); |
| | | } |
| | | // 5. 创建 BPM 流程实例,使审批出现在「协同办公 - 我的流程」 |
| | | bpmProcessInstanceApi.createProcessInstance(getLoginUserId(), |
| | | new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(processDefinitionKey) |
| | | .setProcessDefinitionKey(PURCHASE_REQUEST_PROCESS_KEY) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | |
| | | // 4. 更新采购申请状态为审批中 |
| | | purchaseRequestMapper.updateById(new ErpPurchaseRequestDO() |
| | | .setId(id) |
| | | .setStatus(ErpPurchaseRequestStatusEnum.PROCESS.getStatus()) |
| | | .setProcessInstanceId(processInstanceId)); |
| | | } |
| | | |
| | | @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); |
| | | } |
| | | if (purchaseRequestMapper.auditPurchaseRequest(id, ErpPurchaseRequestStatusEnum.REJECT.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark) != 1) { |
| | | throw exception(PURCHASE_REQUEST_AUDIT_FAIL_NOT_PROCESS); |
| | | } |
| | | return; |
| | | } |
| | | |
| | | // 2. 根据审批结果更新状态 |
| | | Integer newStatus = convertBpmResultToStatus(bpmResult); |
| | | if (newStatus != null) { |
| | | purchaseRequestMapper.updateById(new ErpPurchaseRequestDO() |
| | | .setId(id).setStatus(newStatus)); |
| | | // 5. 审核通过:记录审核人与审核意见 |
| | | if (purchaseRequestMapper.auditPurchaseRequest(id, ErpPurchaseRequestStatusEnum.APPROVE.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark) != 1) { |
| | | throw exception(PURCHASE_REQUEST_AUDIT_FAIL_NOT_PROCESS); |
| | | } |
| | | |
| | | // 3. 审批通过且有供应商,自动生成采购订单 |
| | | if (ErpPurchaseRequestStatusEnum.APPROVE.getStatus().equals(newStatus) |
| | | && purchaseRequest.getSupplierId() != null |
| | | && purchaseRequest.getOrderId() == null) { |
| | | // 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 |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updatePurchaseRequestAuditStatus(Long id, Integer bpmStatus, String reason) { |
| | | // 1. BPM 审批结果映射为采购申请状态:通过=20 / 不通过=30 / 取消=40 |
| | | Integer newStatus = convertBpmResultToStatus(bpmStatus); |
| | | if (newStatus == null) { |
| | | return; |
| | | } |
| | | // 2. 仅当仍处于「审批中」时回写,避免重复覆盖 |
| | | if (purchaseRequestMapper.updateAuditStatusByBpm(id, newStatus, reason) != 1) { |
| | | log.warn("[updatePurchaseRequestAuditStatus] 采购申请({}) 不处于审批中状态,忽略 BPM 结果({})", id, bpmStatus); |
| | | return; |
| | | } |
| | | // 3. 审批通过且有供应商、未生成订单时,自动生成采购订单 |
| | | if (!ErpPurchaseRequestStatusEnum.APPROVE.getStatus().equals(newStatus)) { |
| | | return; |
| | | } |
| | | ErpPurchaseRequestDO purchaseRequest = purchaseRequestMapper.selectById(id); |
| | | if (purchaseRequest == null || purchaseRequest.getSupplierId() == null |
| | | || purchaseRequest.getOrderId() != null) { |
| | | return; |
| | | } |
| | | try { |
| | | Long orderId = doGeneratePurchaseOrder(purchaseRequest); |
| | | log.info("[updatePurchaseRequestAuditStatus] 采购申请({}) 自动生成采购订单({})", id, orderId); |
| | | } catch (Exception e) { |
| | | // 不抛出异常,避免影响审批结果落库;生成失败可由用户手动触发 |
| | | log.error("[updatePurchaseRequestAuditStatus] 采购申请({}) 自动生成采购订单失败", id, e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转换 BPM 审批结果为采购申请状态 |
| | | */ |
| | | private Integer convertBpmResultToStatus(Integer bpmStatus) { |
| | | if (BpmTaskStatusEnum.APPROVE.getStatus().equals(bpmStatus)) { |
| | | return ErpPurchaseRequestStatusEnum.APPROVE.getStatus(); |
| | | } |
| | | if (BpmTaskStatusEnum.REJECT.getStatus().equals(bpmStatus)) { |
| | | return ErpPurchaseRequestStatusEnum.REJECT.getStatus(); |
| | | } |
| | | if (BpmTaskStatusEnum.CANCEL.getStatus().equals(bpmStatus)) { |
| | | return ErpPurchaseRequestStatusEnum.CANCEL.getStatus(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @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); |
| | |
| | | // 3. 创建采购订单 |
| | | ErpPurchaseOrderDO purchaseOrder = new ErpPurchaseOrderDO(); |
| | | purchaseOrder.setNo(orderNo); |
| | | purchaseOrder.setStatus(ErpAuditStatus.APPROVE.getStatus()); // 采购订单直接为已审核状态 |
| | | // 自动生成的采购订单仍为草稿,必须走订单自身的提交/审批流程,避免绕过审批 |
| | | purchaseOrder.setStatus(ErpPurchaseOrderStatusEnum.DRAFT.getStatus()); |
| | | purchaseOrder.setSupplierId(purchaseRequest.getSupplierId()); |
| | | purchaseOrder.setOrderTime(purchaseRequest.getRequestTime()); |
| | | purchaseOrder.setTotalCount(purchaseRequest.getTotalCount()); |
| | |
| | | }); |
| | | purchaseOrderItemMapper.insertBatch(orderItems); |
| | | |
| | | // 5. 更新采购申请的订单信息 |
| | | // 5. 更新采购申请的订单信息,并回写最终使用的供应商 |
| | | purchaseRequestMapper.updateById(new ErpPurchaseRequestDO() |
| | | .setId(id) |
| | | .setSupplierId(purchaseRequest.getSupplierId()) |
| | | .setOrderId(purchaseOrder.getId()) |
| | | .setOrderNo(orderNo)); |
| | | |
| | | 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) { |
| | |
| | | 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; |
| | |
| | | return cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId().toString(); |
| | | } |
| | | |
| | | } |
| | | @Override |
| | | public ErpPurchaseRequestImportRespVO importPurchaseRequestList(List<ErpPurchaseRequestImportExcelVO> importRequests, |
| | | ErpPurchaseRequestImportReqVO importReqVO) { |
| | | // 1. 校验非空(过滤完全空白的行) |
| | | importRequests = filterList(importRequests, item -> StrUtil.isNotBlank(item.getProductCode())); |
| | | if (CollUtil.isEmpty(importRequests)) { |
| | | throw exception(PURCHASE_REQUEST_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | // 2. 按「单号」分组:相同单号的多行合并成一张采购申请;单号为空时每行各自成一张申请 |
| | | List<List<ErpPurchaseRequestImportExcelVO>> groups = new ArrayList<>(); |
| | | Map<String, Integer> groupIndexMap = new LinkedHashMap<>(); |
| | | int rowIndex = 0; |
| | | for (ErpPurchaseRequestImportExcelVO row : importRequests) { |
| | | rowIndex++; |
| | | String groupKey = StrUtil.isNotBlank(row.getNo()) ? row.getNo() : ("__row_" + rowIndex); |
| | | Integer index = groupIndexMap.get(groupKey); |
| | | if (index == null) { |
| | | index = groups.size(); |
| | | groupIndexMap.put(groupKey, index); |
| | | groups.add(new ArrayList<>()); |
| | | } |
| | | groups.get(index).add(row); |
| | | } |
| | | // 3. 逐组合并创建采购申请 |
| | | ErpPurchaseRequestImportRespVO respVO = ErpPurchaseRequestImportRespVO.builder() |
| | | .createPurchaseRequestNos(new ArrayList<>()) |
| | | .failurePurchaseRequestNos(new LinkedHashMap<>()) |
| | | .build(); |
| | | for (List<ErpPurchaseRequestImportExcelVO> group : groups) { |
| | | String groupKey = StrUtil.isNotBlank(group.get(0).getNo()) ? group.get(0).getNo() |
| | | : "第" + (importRequests.indexOf(group.get(0)) + 1) + "行"; |
| | | try { |
| | | ErpPurchaseRequestSaveReqVO createReqVO = buildPurchaseRequestImportSaveReqVO(group); |
| | | Long requestId = createPurchaseRequest(createReqVO); |
| | | respVO.getCreatePurchaseRequestNos().add(purchaseRequestMapper.selectById(requestId).getNo()); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailurePurchaseRequestNos().put(groupKey, ex.getMessage()); |
| | | } |
| | | } |
| | | return respVO; |
| | | } |
| | | |
| | | /** |
| | | * 将同一张采购申请的多行明细转换成创建 Request VO |
| | | */ |
| | | private ErpPurchaseRequestSaveReqVO buildPurchaseRequestImportSaveReqVO(List<ErpPurchaseRequestImportExcelVO> group) { |
| | | ErpPurchaseRequestImportExcelVO first = group.get(0); |
| | | // 1. 校验申请时间 |
| | | if (first.getRequestTime() == null) { |
| | | throw exception(PURCHASE_REQUEST_IMPORT_REQUEST_TIME_REQUIRED); |
| | | } |
| | | // 2. 解析供应商(可选) |
| | | Long supplierId = null; |
| | | if (StrUtil.isNotBlank(first.getSupplierName())) { |
| | | ErpSupplierDO supplier = supplierMapper.selectByName(first.getSupplierName()); |
| | | if (supplier == null) { |
| | | throw exception(PURCHASE_REQUEST_IMPORT_SUPPLIER_NOT_EXISTS, first.getSupplierName()); |
| | | } |
| | | supplierId = supplier.getId(); |
| | | } |
| | | // 3. 组装明细 |
| | | List<ErpPurchaseRequestSaveReqVO.Item> items = new ArrayList<>(); |
| | | for (ErpPurchaseRequestImportExcelVO row : group) { |
| | | MdmItemRespDTO product = mdmItemApi.getItemByCode(row.getProductCode()).getCheckedData(); |
| | | if (product == null) { |
| | | throw exception(PURCHASE_REQUEST_IMPORT_PRODUCT_NOT_EXISTS, row.getProductCode()); |
| | | } |
| | | MdmUnitMeasureRespDTO unit = mdmUnitMeasureApi.getUnitMeasureByCode(row.getProductUnitCode()).getCheckedData(); |
| | | if (unit == null) { |
| | | throw exception(PURCHASE_REQUEST_IMPORT_UNIT_NOT_EXISTS, row.getProductUnitCode()); |
| | | } |
| | | ErpPurchaseRequestSaveReqVO.Item item = new ErpPurchaseRequestSaveReqVO.Item() |
| | | .setProductId(product.getId()) |
| | | .setProductUnitId(unit.getId()) |
| | | .setProductPrice(row.getProductPrice()) |
| | | .setCount(row.getCount()) |
| | | .setTaxPercent(row.getTaxPercent()) |
| | | .setDemandTime(row.getDemandTime()) |
| | | .setQcCheckFlag(row.getQcCheckFlag()) |
| | | .setRemark(row.getItemRemark()); |
| | | items.add(item); |
| | | } |
| | | // 4. 组装主表 |
| | | return new ErpPurchaseRequestSaveReqVO() |
| | | .setRequestTime(first.getRequestTime()) |
| | | .setRequestReason(first.getRequestReason()) |
| | | .setSupplierId(supplierId) |
| | | .setDiscountPercent(first.getDiscountPercent()) |
| | | .setDepositPrice(first.getDepositPrice()) |
| | | .setRemark(first.getRemark()) |
| | | .setItems(items); |
| | | } |
| | | |
| | | } |