| | |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | 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.order.ErpPurchaseOrderImportExcelVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.order.ErpPurchaseOrderImportReqVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.order.ErpPurchaseOrderImportRespVO; |
| | | 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.finance.ErpAccountDO; |
| | | 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.finance.ErpAccountMapper; |
| | | 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.ErpPurchaseOrderInStatusEnum; |
| | | import cn.iocoder.yudao.module.erp.enums.ErpPurchaseOrderStatusEnum; |
| | | import cn.iocoder.yudao.module.erp.service.finance.ErpAccountService; |
| | | 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.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.*; |
| | | |
| | | /** |
| | | * ERP 采购订单 Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | |
| | | 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"; |
| | | |
| | | /** |
| | | * 采购订单审批的 BPM 流程定义 Key |
| | | */ |
| | | private static final String PURCHASE_ORDER_PROCESS_KEY = "purchase_approve"; |
| | | |
| | | @Resource |
| | | private ErpPurchaseOrderMapper purchaseOrderMapper; |
| | |
| | | private ErpSupplierService supplierService; |
| | | @Resource |
| | | private ErpAccountService accountService; |
| | | @Resource |
| | | private ErpSupplierMapper supplierMapper; |
| | | @Resource |
| | | private ErpAccountMapper accountMapper; |
| | | @Resource |
| | | private MdmItemApi mdmItemApi; |
| | | @Resource |
| | | private MdmUnitMeasureApi mdmUnitMeasureApi; |
| | | |
| | | @Resource |
| | | private BpmProcessInstanceApi processInstanceApi; |
| | | private ApprovalConfigApi approvalConfigApi; |
| | | @Resource |
| | | private BpmCategoryService bpmCategoryService; |
| | | private BpmProcessInstanceApi bpmProcessInstanceApi; |
| | | @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. 置为审批中,并清空上一轮审核信息,避免驳回原因残留 |
| | | if (purchaseOrderMapper.submitAndResetAuditInfo(id, ErpPurchaseOrderStatusEnum.PROCESS.getStatus()) != 1) { |
| | | throw exception(PURCHASE_ORDER_SUBMIT_FAIL_NOT_ALLOWED); |
| | | } |
| | | |
| | | // 4. 创建 BPM 流程实例,使审批出现在「协同办公 - 我的流程」 |
| | | bpmProcessInstanceApi.createProcessInstance(getLoginUserId(), |
| | | new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(PURCHASE_ORDER_PROCESS_KEY) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | } |
| | | |
| | | @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); |
| | | if (purchaseOrderMapper.auditPurchaseOrder(id, ErpPurchaseOrderStatusEnum.REJECT.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark) != 1) { |
| | | throw exception(PURCHASE_ORDER_AUDIT_FAIL_NOT_PROCESS); |
| | | } |
| | | 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. 审核通过 |
| | | if (purchaseOrderMapper.auditPurchaseOrder(id, ErpPurchaseOrderStatusEnum.APPROVE.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark) != 1) { |
| | | throw exception(PURCHASE_ORDER_AUDIT_FAIL_NOT_PROCESS); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 获取用户昵称,用于冗余存储审核人姓名 |
| | | */ |
| | | private String getUserNickname(Long userId) { |
| | | if (userId == null) { |
| | | return null; |
| | | } |
| | | 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); |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updatePurchaseOrderAuditStatus(Long id, Integer bpmStatus, String reason) { |
| | | // 1. BPM 审批结果映射为采购订单状态:通过=20 / 不通过=30 / 取消=40 |
| | | Integer newStatus = convertBpmResultToAuditStatus(bpmStatus); |
| | | if (newStatus == null) { |
| | | return; |
| | | } |
| | | // 2. 仅当仍处于「审批中」时回写,避免重复覆盖 |
| | | if (purchaseOrderMapper.updateAuditStatusByBpm(id, newStatus, reason) != 1) { |
| | | log.warn("[updatePurchaseOrderAuditStatus] 采购订单({}) 不处于审批中状态,忽略 BPM 结果({})", id, bpmStatus); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | 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; |
| | | } |
| | |
| | | return; |
| | | } |
| | | purchaseOrders.forEach(purchaseOrder -> { |
| | | if (ErpAuditStatus.APPROVE.getStatus().equals(purchaseOrder.getStatus())) { |
| | | if (ErpPurchaseOrderStatusEnum.APPROVE.getStatus().equals(purchaseOrder.getStatus())) { |
| | | throw exception(PURCHASE_ORDER_DELETE_FAIL_APPROVE, purchaseOrder.getNo()); |
| | | } |
| | | // 审批中禁止删除,需先由审批人「审核不通过」退回 |
| | | if (ErpPurchaseOrderStatusEnum.PROCESS.getStatus().equals(purchaseOrder.getStatus())) { |
| | | throw exception(PURCHASE_ORDER_UPDATE_FAIL_PROCESS, purchaseOrder.getNo()); |
| | | } |
| | | }); |
| | | |
| | |
| | | return arrivalNoticeId; |
| | | } |
| | | |
| | | @Override |
| | | public ErpPurchaseOrderImportRespVO importPurchaseOrderList(List<ErpPurchaseOrderImportExcelVO> rows, |
| | | ErpPurchaseOrderImportReqVO req) { |
| | | if (CollUtil.isEmpty(rows)) throw exception(PURCHASE_ORDER_IMPORT_LIST_IS_EMPTY); |
| | | if (Boolean.TRUE.equals(req.getUpdateSupport())) throw exception(PURCHASE_ORDER_IMPORT_INVALID, "仅支持新增,不支持覆盖"); |
| | | ErpPurchaseOrderImportRespVO result = ErpPurchaseOrderImportRespVO.builder() |
| | | .createPurchaseOrderNos(new ArrayList<>()).failurePurchaseOrderNos(new LinkedHashMap<>()).build(); |
| | | List<List<ErpPurchaseOrderImportExcelVO>> groups = new ArrayList<>(); |
| | | List<String> labels = new ArrayList<>(); |
| | | Map<String, Integer> indexes = new LinkedHashMap<>(); |
| | | for (int i = 0; i < rows.size(); i++) { |
| | | ErpPurchaseOrderImportExcelVO row = rows.get(i); |
| | | if (row == null || cn.hutool.core.bean.BeanUtil.beanToMap(row).values().stream() |
| | | .noneMatch(value -> value != null && (!(value instanceof String) || StrUtil.isNotBlank((String) value)))) continue; |
| | | if (StrUtil.isBlank(row.getNo())) { |
| | | groups.add(new ArrayList<>(Collections.singletonList(row))); |
| | | labels.add("第" + (i + 2) + "行"); |
| | | continue; |
| | | } |
| | | String key = row.getNo().trim(); |
| | | Integer index = indexes.get(key); |
| | | if (index == null) { |
| | | index = groups.size(); indexes.put(key, index); |
| | | groups.add(new ArrayList<>()); labels.add(key); |
| | | } |
| | | groups.get(index).add(row); |
| | | } |
| | | if (groups.isEmpty()) throw exception(PURCHASE_ORDER_IMPORT_LIST_IS_EMPTY); |
| | | for (int i = 0; i < groups.size(); i++) { |
| | | try { |
| | | Long id = cn.hutool.extra.spring.SpringUtil.getBean(ErpPurchaseOrderServiceImpl.class) |
| | | .createImportedPurchaseOrder(groups.get(i)); |
| | | result.getCreatePurchaseOrderNos().add(purchaseOrderMapper.selectById(id).getNo()); |
| | | } catch (ServiceException ex) { |
| | | result.getFailurePurchaseOrderNos().put(labels.get(i), ex.getMessage()); |
| | | } catch (RuntimeException ex) { |
| | | log.error("[importPurchaseOrderList] 采购订单导入失败,分组={}", labels.get(i), ex); |
| | | result.getFailurePurchaseOrderNos().put(labels.get(i), "导入失败,请检查基础资料和订单明细"); |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createImportedPurchaseOrder(List<ErpPurchaseOrderImportExcelVO> group) { |
| | | return createPurchaseOrder(buildPurchaseOrderImportSaveReqVO(group)); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 将同一张采购订单的多行明细转换成创建 Request VO |
| | | */ |
| | | private ErpPurchaseOrderSaveReqVO buildPurchaseOrderImportSaveReqVO(List<ErpPurchaseOrderImportExcelVO> group) { |
| | | ErpPurchaseOrderImportExcelVO first = group.get(0); |
| | | // 1. 校验并解析供应商 |
| | | if (StrUtil.isBlank(first.getSupplierName())) { |
| | | throw exception(PURCHASE_ORDER_IMPORT_SUPPLIER_NOT_EXISTS, ""); |
| | | } |
| | | ErpSupplierDO supplier = supplierMapper.selectByName(first.getSupplierName()); |
| | | if (supplier == null) { |
| | | throw exception(PURCHASE_ORDER_IMPORT_SUPPLIER_NOT_EXISTS, first.getSupplierName()); |
| | | } |
| | | if (first.getOrderTime() == null) { |
| | | throw exception(PURCHASE_ORDER_IMPORT_ORDER_TIME_REQUIRED); |
| | | } |
| | | if (first.getDiscountPercent() != null && (first.getDiscountPercent().signum() < 0 || first.getDiscountPercent().compareTo(new BigDecimal("100")) > 0) |
| | | || first.getDepositPrice() != null && first.getDepositPrice().signum() < 0) { |
| | | throw exception(PURCHASE_ORDER_IMPORT_INVALID, "优惠率须在 0 至 100 之间,定金不能为负数"); |
| | | } |
| | | for (ErpPurchaseOrderImportExcelVO row : group) { |
| | | if (StrUtil.isNotBlank(row.getSupplierName()) && !StrUtil.equals(first.getSupplierName(), row.getSupplierName()) |
| | | || row.getOrderTime() != null && !row.getOrderTime().equals(first.getOrderTime()) |
| | | || StrUtil.isNotBlank(row.getAccountName()) && !StrUtil.equals(first.getAccountName(), row.getAccountName()) |
| | | || row.getDiscountPercent() != null && (first.getDiscountPercent() == null || row.getDiscountPercent().compareTo(first.getDiscountPercent()) != 0) |
| | | || row.getDepositPrice() != null && (first.getDepositPrice() == null || row.getDepositPrice().compareTo(first.getDepositPrice()) != 0) |
| | | || StrUtil.isNotBlank(row.getRemark()) && !StrUtil.equals(first.getRemark(), row.getRemark())) { |
| | | throw exception(PURCHASE_ORDER_IMPORT_INVALID, "同一采购订单的主表信息不一致"); |
| | | } |
| | | if (StrUtil.isBlank(row.getProductCode()) || StrUtil.isBlank(row.getProductUnitCode()) || row.getCount() == null || row.getCount().signum() <= 0 |
| | | || row.getProductPrice() == null || row.getProductPrice().signum() < 0 |
| | | || row.getTaxPercent() != null && (row.getTaxPercent().signum() < 0 || row.getTaxPercent().compareTo(new BigDecimal("100")) > 0)) { |
| | | throw exception(PURCHASE_ORDER_IMPORT_INVALID, "每行须填写物料、单位、正数数量、非负单价;税率须在 0 至 100 之间"); |
| | | } |
| | | } |
| | | // 2. 解析结算账户(可选) |
| | | Long accountId = null; |
| | | if (StrUtil.isNotBlank(first.getAccountName())) { |
| | | ErpAccountDO account = accountMapper.selectByName(first.getAccountName()); |
| | | if (account == null) { |
| | | throw exception(PURCHASE_ORDER_IMPORT_ACCOUNT_NOT_EXISTS, first.getAccountName()); |
| | | } |
| | | accountId = account.getId(); |
| | | } |
| | | // 3. 组装明细 |
| | | List<ErpPurchaseOrderSaveReqVO.Item> items = new ArrayList<>(); |
| | | for (ErpPurchaseOrderImportExcelVO row : group) { |
| | | MdmItemRespDTO product = mdmItemApi.getItemByCode(row.getProductCode().trim()).getCheckedData(); |
| | | if (product == null) { |
| | | throw exception(PURCHASE_ORDER_IMPORT_PRODUCT_NOT_EXISTS, row.getProductCode()); |
| | | } |
| | | MdmUnitMeasureRespDTO unit = mdmUnitMeasureApi.getUnitMeasureByCode(row.getProductUnitCode().trim()).getCheckedData(); |
| | | if (unit == null) { |
| | | throw exception(PURCHASE_ORDER_IMPORT_UNIT_NOT_EXISTS, row.getProductUnitCode()); |
| | | } |
| | | if (product.getStatus() != null && product.getStatus() != 0 || unit.getStatus() != null && unit.getStatus() != 0) { |
| | | throw exception(PURCHASE_ORDER_IMPORT_INVALID, "物料或计量单位已停用"); |
| | | } |
| | | ErpPurchaseOrderSaveReqVO.Item item = new ErpPurchaseOrderSaveReqVO.Item() |
| | | .setProductId(product.getId()) |
| | | .setProductUnitId(unit.getId()) |
| | | .setProductPrice(row.getProductPrice()) |
| | | .setCount(row.getCount()) |
| | | .setTaxPercent(row.getTaxPercent()) |
| | | .setQcCheckFlag(row.getQcCheckFlag()) |
| | | .setRemark(row.getItemRemark()); |
| | | items.add(item); |
| | | } |
| | | // 4. 组装主表 |
| | | ErpPurchaseOrderSaveReqVO createReqVO = new ErpPurchaseOrderSaveReqVO() |
| | | .setSupplierId(supplier.getId()) |
| | | .setAccountId(accountId) |
| | | .setOrderTime(first.getOrderTime()) |
| | | .setDiscountPercent(ObjectUtil.defaultIfNull(first.getDiscountPercent(), BigDecimal.ZERO)) |
| | | .setDepositPrice(ObjectUtil.defaultIfNull(first.getDepositPrice(), BigDecimal.ZERO)) |
| | | .setRemark(first.getRemark()) |
| | | .setItems(items); |
| | | return createReqVO; |
| | | } |
| | | |
| | | } |