| | |
| | | package cn.iocoder.yudao.module.erp.service.sale; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.extra.spring.SpringUtil; |
| | | 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.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.crm.api.customer.CrmCustomerApi; |
| | | import cn.iocoder.yudao.module.crm.api.contract.CrmContractApi; |
| | | import cn.iocoder.yudao.module.crm.api.contract.dto.CrmContractForSaleDTO; |
| | | import cn.iocoder.yudao.module.crm.api.customer.dto.CrmCustomerRespDTO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.sale.vo.order.ErpSaleOrderImportExcelVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.sale.vo.order.ErpSaleOrderImportReqVO; |
| | |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.sale.ErpSaleOrderMapper; |
| | | 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.ErpSaleOrderProductionStatusEnum; |
| | | import cn.iocoder.yudao.module.erp.api.sale.enums.ErpSaleOrderOutStatusEnum; |
| | | import cn.iocoder.yudao.module.erp.service.finance.ErpAccountService; |
| | | import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi; |
| | |
| | | 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 com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import jakarta.annotation.Resource; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | if (createReqVO.getSaleUserId() != null) { |
| | | adminUserApi.validateUser(createReqVO.getSaleUserId()); |
| | | } |
| | | if (createReqVO.getContractId() != null && saleOrderMapper.selectByContractId(createReqVO.getContractId()) != null) { |
| | | throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "该合同已关联销售订单"); |
| | | } |
| | | // 1.5 生成订单号,并校验唯一性 |
| | | String no = noRedisDAO.generate(ErpNoRedisDAO.SALE_ORDER_NO_PREFIX); |
| | | if (saleOrderMapper.selectByNo(no) != null) { |
| | |
| | | public void updateSaleOrder(ErpSaleOrderSaveReqVO updateReqVO) { |
| | | // 1.1 校验存在 |
| | | ErpSaleOrderDO saleOrder = validateSaleOrderExists(updateReqVO.getId()); |
| | | if (ErpAuditStatus.APPROVE.getStatus().equals(saleOrder.getStatus())) { |
| | | if (!ErpAuditStatus.PROCESS.getStatus().equals(saleOrder.getStatus())) { |
| | | throw exception(SALE_ORDER_UPDATE_FAIL_APPROVE, saleOrder.getNo()); |
| | | } |
| | | // 1.2 校验客户(使用 CRM 客户 API) |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateSaleOrderStatus(Long id, Integer status) { |
| | | boolean approve = ErpAuditStatus.APPROVE.getStatus().equals(status); |
| | | // 1.1 校验存在 |
| | | ErpSaleOrderDO saleOrder = validateSaleOrderExists(id); |
| | | // 1.2 校验状态 |
| | | if (saleOrder.getStatus().equals(status)) { |
| | | ErpSaleOrderDO order = validateSaleOrderExists(id); |
| | | if (approve && !ErpAuditStatus.PROCESS.getStatus().equals(order.getStatus()) |
| | | || !approve && (!ErpAuditStatus.PROCESS.getStatus().equals(status) |
| | | || !ErpAuditStatus.APPROVE.getStatus().equals(order.getStatus()))) { |
| | | throw exception(approve ? SALE_ORDER_APPROVE_FAIL : SALE_ORDER_PROCESS_FAIL); |
| | | } |
| | | // 1.3 存在销售出库单,无法反审核 |
| | | BigDecimal outCount = saleOrder.getOutCount() != null ? saleOrder.getOutCount() : BigDecimal.ZERO; |
| | | if (!approve && outCount.compareTo(BigDecimal.ZERO) > 0) { |
| | | throw exception(SALE_ORDER_PROCESS_FAIL_EXISTS_OUT); |
| | | if (!approve) { |
| | | BigDecimal outCount = order.getOutCount() == null ? BigDecimal.ZERO : order.getOutCount(); |
| | | if (outCount.signum() > 0) throw exception(SALE_ORDER_PROCESS_FAIL_EXISTS_OUT); |
| | | validateNoActiveMps(id); |
| | | } |
| | | |
| | | // 2. 更新状态 |
| | | int updateCount = saleOrderMapper.updateByIdAndStatus(id, saleOrder.getStatus(), |
| | | new ErpSaleOrderDO().setStatus(status)); |
| | | if (updateCount == 0) { |
| | | if (saleOrderMapper.updateByIdAndStatus(id, order.getStatus(), new ErpSaleOrderDO().setStatus(status)) != 1) { |
| | | throw exception(approve ? SALE_ORDER_APPROVE_FAIL : SALE_ORDER_PROCESS_FAIL); |
| | | } |
| | | |
| | | // 3. 审核通过时,为需要生产的明细创建 MPS |
| | | if (approve) { |
| | | createMpsForSaleOrder(id, saleOrder); |
| | | } |
| | | if (approve) createMpsForSaleOrder(id, order); |
| | | } |
| | | |
| | | /** |
| | |
| | | .setId(item.getId()) |
| | | .setMpsId(mpsId)); |
| | | createdCount++; |
| | | } |
| | | // 重新生成生产计划后,订单生产状态回到未完成(覆盖此前的“已取消”) |
| | | if (createdCount > 0) { |
| | | saleOrderMapper.update(null, new LambdaUpdateWrapper<ErpSaleOrderDO>() |
| | | .eq(ErpSaleOrderDO::getId, orderId) |
| | | .set(ErpSaleOrderDO::getProductionStatus, ErpSaleOrderProductionStatusEnum.NOT_FINISHED.getStatus()) |
| | | .set(ErpSaleOrderDO::getProductionFinishTime, null)); |
| | | } |
| | | log.info("[createMpsForSaleOrder] 销售订单({}) 审核完成,创建 MPS {} 条,复用 MPS {} 条", |
| | | orderId, createdCount, reusedCount); |
| | |
| | | throw exception(SALE_ORDER_CANCEL_FAIL_EXISTS_OUT); |
| | | } |
| | | |
| | | validateNoActiveMps(id); |
| | | |
| | | // 2. 更新状态为已作废 |
| | | int updateCount = saleOrderMapper.updateByIdAndStatus(id, saleOrder.getStatus(), |
| | | new ErpSaleOrderDO().setStatus(ErpAuditStatus.CANCEL.getStatus())); |
| | |
| | | throw exception(SALE_ORDER_CANCEL_FAIL); |
| | | } |
| | | |
| | | // 3. 同步更新关联合同状态为已作废 |
| | | if (saleOrder.getContractId() != null) { |
| | | contractApi.updateContractStatus(saleOrder.getContractId(), 50).getCheckedData(); // 50 = 已作废 |
| | | } |
| | | // 合同审批状态由合同及其 BPM 流程维护,订单作废不改变合同审批结果。 |
| | | } |
| | | |
| | | @Override |
| | |
| | | throw exception(SALE_ORDER_ENABLE_FAIL); |
| | | } |
| | | |
| | | // 3. 同步更新关联合同状态为审核通过 |
| | | if (saleOrder.getContractId() != null) { |
| | | contractApi.updateContractStatus(saleOrder.getContractId(), 20).getCheckedData(); // 20 = 审核通过 |
| | | } |
| | | // 订单启用不改变关联合同的审批结果。 |
| | | } |
| | | |
| | | private void validateNoActiveMps(Long orderId) { |
| | | for (ErpSaleOrderItemDO item : saleOrderItemMapper.selectListByOrderId(orderId)) { |
| | | MesProMpsRespDTO mps = mpsApi.getMpsBySaleOrderItemId(item.getId()); |
| | | if (mps != null && ObjectUtil.notEqual(mps.getStatus(), MPS_STATUS_CANCELED)) { |
| | | throw exception(SALE_ORDER_HAS_ACTIVE_MPS); |
| | | } |
| | | } |
| | | } |
| | | private List<ErpSaleOrderItemDO> validateSaleOrderItems(List<ErpSaleOrderSaveReqVO.Item> list) { |
| | | // 转化为 ErpSaleOrderItemDO 列表(产品验证已移除) |
| | | return convertList(list, o -> BeanUtils.toBean(o, ErpSaleOrderItemDO.class, item -> { |
| | |
| | | return; |
| | | } |
| | | saleOrders.forEach(saleOrder -> { |
| | | if (ErpAuditStatus.APPROVE.getStatus().equals(saleOrder.getStatus())) { |
| | | if (!ErpAuditStatus.PROCESS.getStatus().equals(saleOrder.getStatus())) { |
| | | throw exception(SALE_ORDER_DELETE_FAIL_APPROVE, saleOrder.getNo()); |
| | | } |
| | | }); |
| | |
| | | @Override |
| | | public ErpSaleOrderImportRespVO importSaleOrderList(List<ErpSaleOrderImportExcelVO> importOrders, |
| | | ErpSaleOrderImportReqVO importReqVO) { |
| | | if (Boolean.TRUE.equals(importReqVO.getUpdateSupport())) { |
| | | throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "销售订单导入仅支持新增,不支持覆盖"); |
| | | } |
| | | // 1. 校验非空(过滤完全空白的行) |
| | | importOrders = filterList(importOrders, item -> StrUtil.isNotBlank(item.getProductCode())); |
| | | importOrders = filterList(importOrders, item -> item != null && (StrUtil.isNotBlank(item.getNo()) || StrUtil.isNotBlank(item.getCustomerName()) || StrUtil.isNotBlank(item.getProductCode()) || item.getCount() != null || item.getProductPrice() != null)); |
| | | if (CollUtil.isEmpty(importOrders)) { |
| | | throw exception(SALE_ORDER_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | // 2. 按「单号」分组:相同单号的多行合并成一张销售订单;单号为空时每行各自成一张订单 |
| | | List<List<ErpSaleOrderImportExcelVO>> groups = new ArrayList<>(); |
| | | Map<String, Integer> groupIndexMap = new LinkedHashMap<>(); |
| | | int rowIndex = 0; |
| | | for (ErpSaleOrderImportExcelVO row : importOrders) { |
| | | rowIndex++; |
| | | String groupKey = StrUtil.isNotBlank(row.getNo()) ? row.getNo() : ("__row_" + rowIndex); |
| | | if (StrUtil.isBlank(row.getNo())) { |
| | | groups.add(new ArrayList<>(Collections.singletonList(row))); |
| | | continue; |
| | | } |
| | | String groupKey = row.getNo().trim(); |
| | | Integer index = groupIndexMap.get(groupKey); |
| | | if (index == null) { |
| | | index = groups.size(); |
| | |
| | | String groupKey = StrUtil.isNotBlank(group.get(0).getNo()) ? group.get(0).getNo() |
| | | : "第" + (importOrders.indexOf(group.get(0)) + 1) + "行"; |
| | | try { |
| | | ErpSaleOrderSaveReqVO createReqVO = buildSaleOrderImportSaveReqVO(group); |
| | | Long orderId = createSaleOrder(createReqVO); |
| | | Long orderId = SpringUtil.getBean(ErpSaleOrderServiceImpl.class).createImportedOrder(group); |
| | | respVO.getCreateSaleOrderNos().add(saleOrderMapper.selectById(orderId).getNo()); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailureSaleOrderNos().put(groupKey, ex.getMessage()); |
| | |
| | | return respVO; |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createImportedOrder(List<ErpSaleOrderImportExcelVO> group) { |
| | | return createSaleOrder(buildSaleOrderImportSaveReqVO(group)); |
| | | } |
| | | /** |
| | | * 将同一张销售订单的多行明细转换成创建 Request VO |
| | | */ |
| | | private ErpSaleOrderSaveReqVO buildSaleOrderImportSaveReqVO(List<ErpSaleOrderImportExcelVO> group) { |
| | | ErpSaleOrderImportExcelVO first = group.get(0); |
| | | for (ErpSaleOrderImportExcelVO row : group) { |
| | | if (StrUtil.isNotBlank(row.getCustomerName()) && !StrUtil.equals(first.getCustomerName(), row.getCustomerName()) |
| | | || row.getOrderTime() != null && !row.getOrderTime().equals(first.getOrderTime()) |
| | | || StrUtil.isNotBlank(row.getSaleUserName()) && !StrUtil.equals(first.getSaleUserName(), row.getSaleUserName()) |
| | | || StrUtil.isNotBlank(row.getAccountName()) && !StrUtil.equals(first.getAccountName(), row.getAccountName()) |
| | | || StrUtil.isNotBlank(row.getContractNo()) && !StrUtil.equals(first.getContractNo(), row.getContractNo())) { |
| | | throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "同一订单的客户、下单时间、销售员和结算账户必须一致"); |
| | | } |
| | | } |
| | | // 1. 校验并解析客户 |
| | | if (StrUtil.isBlank(first.getCustomerName())) { |
| | | throw exception(SALE_ORDER_IMPORT_CUSTOMER_NOT_EXISTS, ""); |
| | |
| | | } |
| | | if (first.getOrderTime() == null) { |
| | | throw exception(SALE_ORDER_IMPORT_ORDER_TIME_REQUIRED); |
| | | } |
| | | if (first.getDiscountPercent() != null && (first.getDiscountPercent().signum() < 0 || first.getDiscountPercent().compareTo(new BigDecimal("100")) > 0)) { |
| | | throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "优惠率必须在 0 到 100 之间"); |
| | | } |
| | | if (first.getDepositPrice() != null && first.getDepositPrice().signum() < 0) { |
| | | throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "定金不能为负数"); |
| | | } |
| | | // 2. 解析结算账户(可选) |
| | | Long accountId = null; |
| | |
| | | // 4. 组装明细 |
| | | List<ErpSaleOrderSaveReqVO.Item> items = new ArrayList<>(); |
| | | for (ErpSaleOrderImportExcelVO row : group) { |
| | | if (StrUtil.isBlank(row.getProductCode()) || StrUtil.isBlank(row.getProductUnitCode())) { |
| | | throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "物料编码和单位编码不能为空"); |
| | | } |
| | | if (row.getCount() == null || row.getCount().compareTo(BigDecimal.ZERO) <= 0) { |
| | | throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "物料 " + row.getProductCode() + " 的数量必须大于零"); |
| | | } |
| | | if (row.getProductPrice() == null || row.getProductPrice().compareTo(BigDecimal.ZERO) < 0) { |
| | | throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "物料 " + row.getProductCode() + " 的单价不能为空或为负数"); |
| | | } |
| | | if (row.getNeedProduction() != null && row.getNeedProduction() != 0 && row.getNeedProduction() != 1) { |
| | | throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "是否需要生产只能填写 0 或 1"); |
| | | } |
| | | if (row.getTaxPercent() != null && (row.getTaxPercent().signum() < 0 || row.getTaxPercent().compareTo(new BigDecimal("100")) > 0)) { |
| | | throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "税率必须在 0 到 100 之间"); |
| | | } |
| | | MdmItemRespDTO product = mdmItemApi.getItemByCode(row.getProductCode()).getCheckedData(); |
| | | if (product == null) { |
| | | throw exception(SALE_ORDER_IMPORT_PRODUCT_NOT_EXISTS, row.getProductCode()); |
| | |
| | | .setRemark(row.getItemRemark()); |
| | | items.add(item); |
| | | } |
| | | CrmContractForSaleDTO contract = null; |
| | | if (StrUtil.isNotBlank(first.getContractNo())) { |
| | | contract = contractApi.getContractForSaleByNo(first.getContractNo().trim()).getCheckedData(); |
| | | if (contract == null || !Integer.valueOf(20).equals(contract.getAuditStatus())) { |
| | | throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "合同不存在或尚未审批通过"); |
| | | } |
| | | if (!customer.getId().equals(contract.getCustomerId())) { |
| | | throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "合同客户与订单客户不一致"); |
| | | } |
| | | if (saleOrderMapper.selectByContractId(contract.getId()) != null) { |
| | | throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "该合同已关联销售订单"); |
| | | } |
| | | Map<String, BigDecimal> expected = new LinkedHashMap<>(); |
| | | if (contract.getItems() != null) { |
| | | for (CrmContractForSaleDTO.Item item : contract.getItems()) { |
| | | if (item.getItemId() == null || item.getItemUnitId() == null || item.getContractPrice() == null || item.getCount() == null) { |
| | | throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "合同明细不完整,无法关联订单"); |
| | | } |
| | | String key = item.getItemId() + ":" + item.getItemUnitId() + ":" + item.getContractPrice().stripTrailingZeros().toPlainString(); |
| | | expected.merge(key, item.getCount(), BigDecimal::add); |
| | | } |
| | | } |
| | | Map<String, BigDecimal> actual = new LinkedHashMap<>(); |
| | | for (ErpSaleOrderSaveReqVO.Item item : items) { |
| | | String key = item.getProductId() + ":" + item.getProductUnitId() + ":" + item.getProductPrice().stripTrailingZeros().toPlainString(); |
| | | actual.merge(key, item.getCount(), BigDecimal::add); |
| | | } |
| | | if (expected.isEmpty() || expected.size() != actual.size() || expected.entrySet().stream() |
| | | .anyMatch(entry -> !actual.containsKey(entry.getKey()) || entry.getValue().compareTo(actual.get(entry.getKey())) != 0)) { |
| | | throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "订单物料、单位、单价及数量须与合同明细一致"); |
| | | } |
| | | BigDecimal expectedDiscount = ObjectUtil.defaultIfNull(contract.getDiscountPercent(), BigDecimal.ZERO); |
| | | if (expectedDiscount.compareTo(ObjectUtil.defaultIfNull(first.getDiscountPercent(), BigDecimal.ZERO)) != 0) { |
| | | throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "订单优惠率须与合同一致"); |
| | | } |
| | | } |
| | | // 5. 组装主表 |
| | | ErpSaleOrderSaveReqVO createReqVO = new ErpSaleOrderSaveReqVO() |
| | | .setCustomerId(customer.getId()) |
| | | .setContractId(contract == null ? null : contract.getId()) |
| | | .setContractNo(contract == null ? null : first.getContractNo().trim()) |
| | | .setOrderTime(first.getOrderTime()) |
| | | .setSaleUserId(saleUserId) |
| | | .setAccountId(accountId) |