| | |
| | | package cn.iocoder.yudao.module.crm.service.quotation; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.lang.Assert; |
| | | 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.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.crm.controller.admin.contract.vo.contract.CrmContractSaveReqVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.quotation.vo.CrmSaleQuotationPageReqVO; |
| | | import cn.iocoder.yudao.module.crm.controller.admin.quotation.vo.CrmSaleQuotationSaveReqVO; |
| | |
| | | import cn.iocoder.yudao.module.crm.dal.mysql.quotation.CrmSaleQuotationMapper; |
| | | import cn.iocoder.yudao.module.crm.dal.mysql.quotation.CrmSaleQuotationProductMapper; |
| | | import cn.iocoder.yudao.module.crm.dal.redis.no.CrmNoRedisDAO; |
| | | import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum; |
| | | import cn.iocoder.yudao.module.crm.enums.permission.CrmPermissionLevelEnum; |
| | | import cn.iocoder.yudao.module.crm.enums.quotation.CrmSaleQuotationStatusEnum; |
| | | import cn.iocoder.yudao.module.crm.service.business.CrmBusinessService; |
| | | import cn.iocoder.yudao.module.crm.service.contact.CrmContactService; |
| | | import cn.iocoder.yudao.module.crm.service.contract.CrmContractService; |
| | | import cn.iocoder.yudao.module.crm.service.customer.CrmCustomerService; |
| | | import cn.iocoder.yudao.module.crm.service.permission.CrmPermissionService; |
| | | import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionCreateReqBO; |
| | | import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi; |
| | | import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO; |
| | | 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; |
| | |
| | | @Validated |
| | | @Slf4j |
| | | public class CrmSaleQuotationServiceImpl implements CrmSaleQuotationService { |
| | | |
| | | /** |
| | | * 销售报价 BPM 分类编码 |
| | | */ |
| | | private static final String SALE_QUOTATION_APPROVE_CATEGORY_CODE = "sale_quotation_approve"; |
| | | |
| | | @Resource |
| | | private CrmSaleQuotationMapper saleQuotationMapper; |
| | |
| | | private CrmPermissionService permissionService; |
| | | @Resource |
| | | private CrmContractService contractService; |
| | | |
| | | @Resource |
| | | private BpmCategoryService bpmCategoryService; |
| | | @Resource |
| | | private BpmProcessDefinitionService bpmProcessDefinitionService; |
| | | @Resource |
| | | private BpmProcessInstanceApi bpmProcessInstanceApi; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | quotationProducts.forEach(p -> p.setQuotationId(saleQuotation.getId())); |
| | | saleQuotationProductMapper.insertBatch(quotationProducts); |
| | | // 2.3 创建数据权限 |
| | | permissionService.createPermission(saleQuotation.getId(), userId); |
| | | permissionService.createPermission(new CrmPermissionCreateReqBO() |
| | | .setUserId(userId) |
| | | .setBizType(CrmBizTypeEnum.CRM_SALE_QUOTATION.getType()) |
| | | .setBizId(saleQuotation.getId()) |
| | | .setLevel(CrmPermissionLevelEnum.OWNER.getLevel())); |
| | | return saleQuotation.getId(); |
| | | } |
| | | |
| | |
| | | // 2.2 删除报价明细 |
| | | saleQuotationProductMapper.deleteByQuotationId(saleQuotation.getId()); |
| | | // 2.3 删除数据权限 |
| | | permissionService.deletePermission(saleQuotation.getId()); |
| | | permissionService.deletePermission(CrmBizTypeEnum.CRM_SALE_QUOTATION.getType(), saleQuotation.getId()); |
| | | }); |
| | | } |
| | | |
| | |
| | | |
| | | private List<CrmSaleQuotationProductDO> validateQuotationItems(List<CrmSaleQuotationSaveReqVO.Item> list) { |
| | | // 1. 校验物料存在且启用 |
| | | List<Long> itemIds = convertSet(list, CrmSaleQuotationSaveReqVO.Item::getItemId); |
| | | Set<Long> itemIds = convertSet(list, CrmSaleQuotationSaveReqVO.Item::getItemId); |
| | | Map<Long, MdmItemRespDTO> itemMap = mdmItemApi.getItemMap(itemIds); |
| | | // 2. 转换为 CrmSaleQuotationProductDO 列表 |
| | | return convertList(list, o -> BeanUtils.toBean(o, CrmSaleQuotationProductDO.class, item -> { |
| | |
| | | throw exception(SALE_QUOTATION_ITEM_NOT_EXISTS); |
| | | } |
| | | item.setItemUnitId(mdmItem.getUnitMeasureId()); |
| | | item.setItemPrice(mdmItem.getSalesPrice()); |
| | | // 物料原价,如果 MDM 中没有则默认为 0 |
| | | item.setItemPrice(mdmItem.getSalesPrice() != null ? mdmItem.getSalesPrice() : BigDecimal.ZERO); |
| | | if (item.getQuotationPrice() != null && item.getCount() != null) { |
| | | item.setTotalPrice(MoneyUtils.priceMultiply(item.getQuotationPrice(), item.getCount())); |
| | | if (item.getTaxPercent() != null) { |
| | |
| | | |
| | | private void calculateTotalPrice(CrmSaleQuotationDO saleQuotation, List<CrmSaleQuotationProductDO> items) { |
| | | saleQuotation.setTotalProductPrice(getSumValue(items, CrmSaleQuotationProductDO::getTotalPrice, BigDecimal::add, BigDecimal.ZERO)); |
| | | saleQuotation.setTotalTaxPrice(getSumValue(items, CrmSaleQuotationProductDO::getTaxPrice, BigDecimal::add, BigDecimal.ZERO)); |
| | | BigDecimal totalTaxPrice = getSumValue(items, CrmSaleQuotationProductDO::getTaxPrice, BigDecimal::add, BigDecimal.ZERO); |
| | | saleQuotation.setTaxPrice(totalTaxPrice); |
| | | // 计算优惠价格 |
| | | if (saleQuotation.getDiscountPercent() == null) { |
| | | saleQuotation.setDiscountPercent(BigDecimal.ZERO); |
| | | } |
| | | BigDecimal basePrice = saleQuotation.getTotalProductPrice().add(saleQuotation.getTotalTaxPrice()); |
| | | BigDecimal basePrice = saleQuotation.getTotalProductPrice().add(totalTaxPrice); |
| | | saleQuotation.setDiscountPrice(MoneyUtils.priceMultiplyPercent(basePrice, saleQuotation.getDiscountPercent())); |
| | | saleQuotation.setTotalPrice(basePrice.subtract(saleQuotation.getDiscountPrice())); |
| | | } |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void submitSaleQuotation(Long id, String processDefinitionKey, Long userId) { |
| | | public Long convertToContract(Long id, Long userId) { |
| | | // 1. 校验报价单存在且为草稿状态 |
| | | CrmSaleQuotationDO saleQuotation = validateSaleQuotationExists(id); |
| | | if (!CrmSaleQuotationStatusEnum.DRAFT.getStatus().equals(saleQuotation.getStatus())) { |
| | | throw exception(SALE_QUOTATION_SUBMIT_FAIL_NOT_DRAFT); |
| | | } |
| | | |
| | | // 2. 校验分类和流程定义是否存在 |
| | | validateSaleQuotationApproveCategoryAndProcessDefinition(); |
| | | |
| | | // 3. 校验传入的流程定义 Key 是否属于该分类且处于激活状态 |
| | | ProcessDefinition processDefinition = bpmProcessDefinitionService.getActiveProcessDefinition(processDefinitionKey); |
| | | if (processDefinition == null) { |
| | | throw exception(SALE_QUOTATION_BPM_PROCESS_DEFINITION_NOT_EXISTS); |
| | | } |
| | | |
| | | // 4. 创建流程实例 |
| | | String processInstanceId = bpmProcessInstanceApi.createProcessInstance(userId, |
| | | new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(processDefinitionKey) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | |
| | | // 5. 更新报价单状态 |
| | | saleQuotationMapper.updateById(new CrmSaleQuotationDO() |
| | | .setId(id) |
| | | .setProcessInstanceId(processInstanceId) |
| | | .setStatus(CrmSaleQuotationStatusEnum.PROCESS.getStatus())); |
| | | } |
| | | |
| | | /** |
| | | * 校验 BPM 销售报价审批分类和流程定义是否存在 |
| | | */ |
| | | private void validateSaleQuotationApproveCategoryAndProcessDefinition() { |
| | | // 1. 校验分类是否存在 |
| | | List<String> codes = Collections.singletonList(SALE_QUOTATION_APPROVE_CATEGORY_CODE); |
| | | Map<String, ?> categoryMap = bpmCategoryService.getCategoryMap(codes); |
| | | if (!categoryMap.containsKey(SALE_QUOTATION_APPROVE_CATEGORY_CODE)) { |
| | | throw exception(SALE_QUOTATION_BPM_CATEGORY_NOT_EXISTS); |
| | | } |
| | | |
| | | // 2. 校验分类下是否有可用的流程定义 |
| | | List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService |
| | | .getProcessDefinitionInfoListByCategory(SALE_QUOTATION_APPROVE_CATEGORY_CODE); |
| | | if (CollUtil.isEmpty(definitionInfoList)) { |
| | | throw exception(SALE_QUOTATION_BPM_PROCESS_DEFINITION_NOT_EXISTS); |
| | | } |
| | | |
| | | // 3. 校验是否有激活状态的流程定义 |
| | | Set<String> processDefinitionIds = convertSet(definitionInfoList, BpmProcessDefinitionInfoDO::getProcessDefinitionId); |
| | | List<ProcessDefinition> processDefinitions = bpmProcessDefinitionService.getProcessDefinitionList(processDefinitionIds); |
| | | boolean hasActiveDefinition = processDefinitions.stream() |
| | | .anyMatch(pd -> !pd.isSuspended()); |
| | | if (!hasActiveDefinition) { |
| | | throw exception(SALE_QUOTATION_BPM_PROCESS_DEFINITION_NOT_EXISTS); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void updateSaleQuotationAuditStatus(Long id, Integer bpmResult) { |
| | | // 1. 校验报价单存在且为审批中状态 |
| | | CrmSaleQuotationDO saleQuotation = validateSaleQuotationExists(id); |
| | | if (!CrmSaleQuotationStatusEnum.PROCESS.getStatus().equals(saleQuotation.getStatus())) { |
| | | log.error("[updateSaleQuotationAuditStatus] 报价单({}) 不处于审批中", id); |
| | | throw exception(SALE_QUOTATION_UPDATE_AUDIT_STATUS_FAIL_NOT_PROCESS); |
| | | } |
| | | |
| | | // 2. 转换审批结果 |
| | | Integer auditStatus = convertBpmResultToAuditStatus(bpmResult); |
| | | saleQuotationMapper.updateById(new CrmSaleQuotationDO() |
| | | .setId(id) |
| | | .setStatus(auditStatus)); |
| | | } |
| | | |
| | | /** |
| | | * BPM 审批结果转换 |
| | | */ |
| | | private Integer convertBpmResultToAuditStatus(Integer bpmResult) { |
| | | Integer auditStatus = BpmTaskStatusEnum.APPROVE.getStatus().equals(bpmResult) |
| | | ? CrmSaleQuotationStatusEnum.APPROVE.getStatus() |
| | | : BpmTaskStatusEnum.REJECT.getStatus().equals(bpmResult) |
| | | ? CrmSaleQuotationStatusEnum.REJECT.getStatus() |
| | | : BpmTaskStatusEnum.CANCEL.getStatus().equals(bpmResult) |
| | | ? CrmSaleQuotationStatusEnum.CANCEL.getStatus() : null; |
| | | Assert.notNull(auditStatus, "BPM 审批结果({}) 转换失败", bpmResult); |
| | | return auditStatus; |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getSaleQuotationApproveProcessDefinitionList() { |
| | | // 1. 校验分类和流程定义是否存在 |
| | | validateSaleQuotationApproveCategoryAndProcessDefinition(); |
| | | |
| | | // 2. 获取分类下的流程定义信息 |
| | | List<BpmProcessDefinitionInfoDO> definitionInfoList = bpmProcessDefinitionService |
| | | .getProcessDefinitionInfoListByCategory(SALE_QUOTATION_APPROVE_CATEGORY_CODE); |
| | | |
| | | // 3. 获取流程定义详情 |
| | | Set<String> processDefinitionIds = convertSet(definitionInfoList, BpmProcessDefinitionInfoDO::getProcessDefinitionId); |
| | | List<ProcessDefinition> processDefinitions = bpmProcessDefinitionService.getProcessDefinitionList(processDefinitionIds); |
| | | |
| | | // 4. 过滤:只保留激活状态,且相同 key 只保留最新版本 |
| | | Map<String, ProcessDefinition> latestVersionMap = new HashMap<>(); |
| | | for (ProcessDefinition pd : processDefinitions) { |
| | | if (pd.isSuspended()) { |
| | | continue; |
| | | } |
| | | ProcessDefinition existing = latestVersionMap.get(pd.getKey()); |
| | | if (existing == null || pd.getVersion() > existing.getVersion()) { |
| | | latestVersionMap.put(pd.getKey(), pd); |
| | | } |
| | | } |
| | | |
| | | // 5. 返回流程定义列表 |
| | | 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()); |
| | | item.put("version", pd.getVersion()); |
| | | result.add(item); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long convertToContract(Long id, Long userId) { |
| | | // 1. 校验报价单存在且已审批通过 |
| | | CrmSaleQuotationDO saleQuotation = validateSaleQuotationExists(id); |
| | | if (!CrmSaleQuotationStatusEnum.APPROVE.getStatus().equals(saleQuotation.getStatus())) { |
| | | throw exception(SALE_QUOTATION_NOT_APPROVE); |
| | | throw exception(SALE_QUOTATION_CONVERT_FAIL_NOT_DRAFT, saleQuotation.getNo()); |
| | | } |
| | | // 校验是否已转合同 |
| | | if (saleQuotation.getContractId() != null) { |
| | |
| | | }); |
| | | contractVO.setProducts(contractProducts); |
| | | |
| | | // 4. 创建合同 |
| | | // 4. 创建合同(会自动检测合同审批流程并提交审批) |
| | | Long contractId = contractService.createContract(contractVO, userId); |
| | | |
| | | // 5. 更新报价单关联信息 |