| | |
| | | import cn.iocoder.yudao.module.crm.dal.dataobject.contract.CrmContractDO; |
| | | import cn.iocoder.yudao.module.crm.dal.dataobject.contract.CrmContractProductDO; |
| | | import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO; |
| | | import cn.iocoder.yudao.module.crm.dal.dataobject.product.CrmProductDO; |
| | | import cn.iocoder.yudao.module.crm.enums.common.CrmAuditStatusEnum; |
| | | import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi; |
| | | import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO; |
| | | 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.product.CrmProductService; |
| | | import cn.iocoder.yudao.module.crm.service.invoice.CrmInvoiceService; |
| | | import cn.iocoder.yudao.module.crm.service.receivable.CrmReceivableService; |
| | | import cn.iocoder.yudao.module.system.api.dept.DeptApi; |
| | | import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; |
| | |
| | | @Resource |
| | | private CrmBusinessService businessService; |
| | | @Resource |
| | | private CrmProductService productService; |
| | | @Resource |
| | | private CrmReceivableService receivableService; |
| | | @Resource |
| | | private CrmInvoiceService invoiceService; |
| | | |
| | | @Resource |
| | | private MdmItemApi mdmItemApi; |
| | | |
| | | @Resource |
| | | private AdminUserApi adminUserApi; |
| | |
| | | CrmContractRespVO contractVO = buildContractDetailList(singletonList(contract)).get(0); |
| | | // 拼接产品项 |
| | | List<CrmContractProductDO> businessProducts = contractService.getContractProductListByContractId(contractVO.getId()); |
| | | Map<Long, CrmProductDO> productMap = productService.getProductMap( |
| | | convertSet(businessProducts, CrmContractProductDO::getProductId)); |
| | | Map<Long, MdmItemRespDTO> itemMap = mdmItemApi.getItemMap( |
| | | convertSet(businessProducts, CrmContractProductDO::getItemId)); |
| | | contractVO.setProducts(BeanUtils.toBean(businessProducts, CrmContractRespVO.Product.class, businessProductVO -> |
| | | MapUtils.findAndThen(productMap, businessProductVO.getProductId(), |
| | | product -> businessProductVO.setProductName(product.getName()) |
| | | .setProductNo(product.getNo()).setProductUnit(product.getUnit())))); |
| | | MapUtils.findAndThen(itemMap, businessProductVO.getItemId(), |
| | | item -> businessProductVO.setItemCode(item.getCode()) |
| | | .setItemName(item.getName()).setItemBarCode(item.getBarCode())))); |
| | | return contractVO; |
| | | } |
| | | |
| | |
| | | public void exportContractExcel(@Valid CrmContractPageReqVO exportReqVO, |
| | | HttpServletResponse response) throws IOException { |
| | | PageResult<CrmContractDO> pageResult = contractService.getContractPage(exportReqVO, getLoginUserId()); |
| | | List<CrmContractRespVO> list = BeanUtils.toBean(pageResult.getList(), CrmContractRespVO.class); |
| | | // 计算未回款金额、转换审批状态为中文 |
| | | list.forEach(vo -> { |
| | | if (vo.getTotalPrice() != null && vo.getTotalReceivablePrice() != null) { |
| | | vo.setUnReceivablePrice(vo.getTotalPrice().subtract(vo.getTotalReceivablePrice())); |
| | | } |
| | | if (vo.getAuditStatus() != null) { |
| | | vo.setAuditStatusName(CrmAuditStatusEnum.getNameByStatus(vo.getAuditStatus())); |
| | | } |
| | | }); |
| | | // 导出 Excel |
| | | ExcelUtils.write(response, "合同.xls", "数据", CrmContractRespVO.class, |
| | | BeanUtils.toBean(pageResult.getList(), CrmContractRespVO.class)); |
| | | ExcelUtils.write(response, "合同.xls", "数据", CrmContractRespVO.class, list); |
| | | } |
| | | |
| | | @PutMapping("/transfer") |
| | |
| | | @PutMapping("/submit") |
| | | @Operation(summary = "提交合同审批") |
| | | @PreAuthorize("@ss.hasPermission('crm:contract:update')") |
| | | public CommonResult<Boolean> submitContract(@RequestParam("id") Long id) { |
| | | contractService.submitContract(id, getLoginUserId()); |
| | | public CommonResult<Boolean> submitContract(@RequestParam("id") Long id, |
| | | @RequestParam("processDefinitionKey") String processDefinitionKey) { |
| | | contractService.submitContract(id, processDefinitionKey, getLoginUserId()); |
| | | return success(true); |
| | | } |
| | | |
| | | @GetMapping("/approve-process-list") |
| | | @Operation(summary = "获取合同审批流程列表") |
| | | @PreAuthorize("@ss.hasPermission('crm:contract:query')") |
| | | public CommonResult<List<Map<String, Object>>> getContractApproveProcessList() { |
| | | return success(contractService.getContractApproveProcessDefinitionList()); |
| | | } |
| | | |
| | | private List<CrmContractRespVO> buildContractDetailList(List<CrmContractDO> contractList) { |
| | |
| | | // 1.5 获得已回款金额 |
| | | Map<Long, BigDecimal> receivablePriceMap = receivableService.getReceivablePriceMapByContractId( |
| | | convertSet(contractList, CrmContractDO::getId)); |
| | | // 1.6 获得已开票金额(审批通过 + 审批中,与开票防超校验口径一致) |
| | | Map<Long, BigDecimal> invoicePriceMap = invoiceService.getInvoicePriceMapByContractId( |
| | | convertSet(contractList, CrmContractDO::getId)); |
| | | // 2. 拼接数据 |
| | | return BeanUtils.toBean(contractList, CrmContractRespVO.class, contractVO -> { |
| | | // 2.1 设置客户信息 |
| | |
| | | findAndThen(businessMap, contractVO.getBusinessId(), business -> contractVO.setBusinessName(business.getName())); |
| | | // 2.5 设置已回款金额 |
| | | contractVO.setTotalReceivablePrice(receivablePriceMap.getOrDefault(contractVO.getId(), BigDecimal.ZERO)); |
| | | // 2.6 设置已开票金额 |
| | | contractVO.setTotalInvoicePrice(invoicePriceMap.getOrDefault(contractVO.getId(), BigDecimal.ZERO)); |
| | | // 2.7 设置未开票金额 |
| | | if (contractVO.getTotalPrice() != null && contractVO.getTotalInvoicePrice() != null) { |
| | | contractVO.setUnInvoicePrice(contractVO.getTotalPrice().subtract(contractVO.getTotalInvoicePrice())); |
| | | } |
| | | }); |
| | | } |
| | | |
| | |
| | | // 拼接数据 |
| | | Map<Long, BigDecimal> receivablePriceMap = receivableService.getReceivablePriceMapByContractId( |
| | | convertSet(pageResult.getList(), CrmContractDO::getId)); |
| | | Map<Long, BigDecimal> invoicePriceMap = invoiceService.getInvoicePriceMapByContractId( |
| | | convertSet(pageResult.getList(), CrmContractDO::getId)); |
| | | return success(convertList(pageResult.getList(), contract -> new CrmContractRespVO() // 只返回 id、name 等精简字段 |
| | | .setId(contract.getId()).setName(contract.getName()).setAuditStatus(contract.getAuditStatus()) |
| | | .setTotalPrice(contract.getTotalPrice()) |
| | | .setTotalReceivablePrice(receivablePriceMap.getOrDefault(contract.getId(), BigDecimal.ZERO)))); |
| | | .setTotalReceivablePrice(receivablePriceMap.getOrDefault(contract.getId(), BigDecimal.ZERO)) |
| | | .setTotalInvoicePrice(invoicePriceMap.getOrDefault(contract.getId(), BigDecimal.ZERO)))); |
| | | } |
| | | |
| | | @PostMapping("/generate-sale-order") |
| | | @Operation(summary = "根据合同生成销售订单") |
| | | @Parameter(name = "id", description = "合同编号", required = true) |
| | | @PreAuthorize("@ss.hasPermission('crm:contract:update')") |
| | | public CommonResult<Long> generateSaleOrder(@RequestParam("id") Long id) { |
| | | return success(contractService.generateSaleOrder(id, getLoginUserId())); |
| | | } |
| | | |
| | | } |