| | |
| | | import com.ruoyi.sales.dto.InvoiceLedgerDto; |
| | | import com.ruoyi.sales.dto.SalesLedgerDto; |
| | | import com.ruoyi.sales.mapper.InvoiceLedgerMapper; |
| | | import com.ruoyi.sales.mapper.InvoiceRegistrationProductMapper; |
| | | import com.ruoyi.sales.mapper.ReceiptPaymentMapper; |
| | | import com.ruoyi.sales.pojo.InvoiceLedger; |
| | | import com.ruoyi.sales.pojo.InvoiceRegistrationProduct; |
| | | import com.ruoyi.sales.pojo.ReceiptPayment; |
| | | import com.ruoyi.sales.pojo.SalesLedger; |
| | | import com.ruoyi.sales.service.ICommonFileService; |
| | |
| | | import java.io.OutputStream; |
| | | import java.math.BigDecimal; |
| | | import java.net.URLEncoder; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | |
| | | @Autowired |
| | | private InvoiceLedgerMapper invoiceLedgerMapper; |
| | | |
| | | @Autowired |
| | | private InvoiceRegistrationProductMapper invoiceRegistrationProductMapper; |
| | | |
| | | @Autowired |
| | | private ReceiptPaymentMapper receiptPaymentMapper; |
| | |
| | | @Log(title = "销售台账", businessType = BusinessType.EXPORT) |
| | | @PostMapping("/export") |
| | | public void export(HttpServletResponse response, SalesLedgerDto salesLedgerDto) { |
| | | List<SalesLedger> list = salesLedgerService.selectSalesLedgerList(salesLedgerDto); |
| | | Page page = new Page(-1,-1); |
| | | IPage<SalesLedger> salesLedgerIPage = listPage(page, salesLedgerDto); |
| | | ExcelUtil<SalesLedger> util = new ExcelUtil<SalesLedger>(SalesLedger.class); |
| | | if(salesLedgerIPage == null){ |
| | | util.exportExcel(response, new ArrayList<>(), "销售台账数据"); |
| | | return; |
| | | } |
| | | List<SalesLedger> list = salesLedgerIPage.getRecords(); |
| | | util.exportExcel(response, list, "销售台账数据"); |
| | | } |
| | | |
| | |
| | | */ |
| | | @GetMapping("/listPage") |
| | | public IPage<SalesLedger> listPage(Page page, SalesLedgerDto salesLedgerDto) { |
| | | IPage<SalesLedger> iPage = salesLedgerService.selectSalesLedgerListPage(page,salesLedgerDto); |
| | | // 计算已开票金额/未开票金额(已填写发票金额为准) |
| | | if(CollectionUtils.isEmpty(iPage.getRecords())){ |
| | | return iPage; |
| | | } |
| | | List<Long> salesLedgerIds = iPage.getRecords().stream().map(SalesLedger::getId).collect(Collectors.toList()); |
| | | List<InvoiceLedgerDto> invoiceLedgerDtoList = invoiceLedgerMapper.invoicedTotal(salesLedgerIds); |
| | | if(CollectionUtils.isEmpty(invoiceLedgerDtoList)){ |
| | | return iPage; |
| | | } |
| | | // 计算回款金额,待回款金额 |
| | | List<InvoiceRegistrationProduct> invoiceRegistrationProducts = invoiceRegistrationProductMapper.selectList(new LambdaQueryWrapper<InvoiceRegistrationProduct>() |
| | | .in(InvoiceRegistrationProduct::getSalesLedgerId, salesLedgerIds)); |
| | | IPage<SalesLedger> iPage = salesLedgerService.selectSalesLedgerListPage(page, salesLedgerDto); |
| | | |
| | | List<InvoiceLedger> invoiceLedgers = invoiceLedgerMapper.selectList(new LambdaQueryWrapper<InvoiceLedger>() |
| | | .in(InvoiceLedger::getInvoiceRegistrationProductId, invoiceRegistrationProducts.stream().map(InvoiceRegistrationProduct::getId).collect(Collectors.toList()))); |
| | | List<ReceiptPayment> receiptPayments = new ArrayList<>(); |
| | | if(!CollectionUtils.isEmpty(invoiceLedgers)){ |
| | | receiptPayments = receiptPaymentMapper.selectList(new LambdaQueryWrapper<ReceiptPayment>() |
| | | .in(ReceiptPayment::getInvoiceLedgerId, invoiceLedgers.stream().map(InvoiceLedger::getId).collect(Collectors.toList()))); |
| | | // 查询结果为空,直接返回 |
| | | if (CollectionUtils.isEmpty(iPage.getRecords())) { |
| | | return iPage; |
| | | } |
| | | for (SalesLedger salesLedger : iPage.getRecords()) { |
| | | boolean existFlag = false; |
| | | BigDecimal noInvoiceAmountTotal = BigDecimal.ZERO; |
| | | BigDecimal invoiceTotal = BigDecimal.ZERO; |
| | | for (InvoiceLedgerDto invoiceLedgerDto : invoiceLedgerDtoList) { |
| | | if (salesLedger.getId().intValue() == invoiceLedgerDto.getSalesLedgerId()) { |
| | | noInvoiceAmountTotal = salesLedger.getContractAmount().subtract(invoiceLedgerDto.getInvoiceTotal()); |
| | | invoiceTotal = invoiceLedgerDto.getInvoiceTotal(); |
| | | existFlag = true; |
| | | if(!CollectionUtils.isEmpty(receiptPayments)){ |
| | | List<InvoiceRegistrationProduct> collect = invoiceRegistrationProducts.stream() |
| | | .filter(item -> salesLedger.getId().equals(Long.parseLong(item.getSalesLedgerId().toString()))) |
| | | .collect(Collectors.toList()); |
| | | List<Integer> collect1 = collect.stream() |
| | | .map(InvoiceRegistrationProduct::getId).collect(Collectors.toList()); |
| | | List<InvoiceLedger> collect2 = invoiceLedgers.stream() |
| | | .filter(item -> collect1.contains(item.getInvoiceRegistrationProductId())) |
| | | .collect(Collectors.toList()); |
| | | // 获取已回款金额 |
| | | List<ReceiptPayment> collect3 = receiptPayments.stream() |
| | | .filter(item -> collect2.stream().anyMatch(item1 -> item1.getId().equals(item.getInvoiceLedgerId()))) |
| | | .collect(Collectors.toList()); |
| | | BigDecimal receiptPaymentAmountTotal = collect3.stream().map(ReceiptPayment::getReceiptPaymentAmount) |
| | | .filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | // 获取待回款金额 |
| | | BigDecimal noReceiptPaymentAmountTotal = invoiceLedgerDto.getInvoiceTotal().subtract(receiptPaymentAmountTotal); |
| | | salesLedger.setReceiptPaymentAmountTotal(receiptPaymentAmountTotal); |
| | | salesLedger.setNoReceiptAmount(noReceiptPaymentAmountTotal); |
| | | } |
| | | break; |
| | | |
| | | // 获取当前页所有台账记录的 ID 集合 |
| | | List<Long> salesLedgerIds = iPage.getRecords().stream().map(SalesLedger::getId).collect(Collectors.toList()); |
| | | |
| | | // 查询发票信息的已开票金额 |
| | | List<InvoiceLedgerDto> invoiceLedgerDtoList = invoiceLedgerMapper.invoicedTotal(salesLedgerIds); |
| | | if (CollectionUtils.isEmpty(invoiceLedgerDtoList)) { |
| | | invoiceLedgerDtoList = Collections.emptyList(); |
| | | } |
| | | |
| | | // 转换发票数据, key 为台账ID, value 为该台账的总开票金额 |
| | | Map<Long, BigDecimal> invoiceTotals = invoiceLedgerDtoList.stream() |
| | | .filter(dto -> dto.getSalesLedgerId() != null && dto.getInvoiceTotal() != null) |
| | | .collect(Collectors.toMap( |
| | | dto -> dto.getSalesLedgerId().longValue(), |
| | | InvoiceLedgerDto::getInvoiceTotal, |
| | | BigDecimal::add // 存在重复ID执行累加 |
| | | )); |
| | | |
| | | // 查询回款/付款记录 |
| | | List<ReceiptPayment> receiptPayments = Collections.emptyList(); |
| | | if (!CollectionUtils.isEmpty(salesLedgerIds)) { |
| | | receiptPayments = receiptPaymentMapper.selectList(new LambdaQueryWrapper<ReceiptPayment>() |
| | | .in(ReceiptPayment::getSalesLedgerId, salesLedgerIds)); |
| | | } |
| | | |
| | | // 转换回款数据, key 为台账ID, value 为该台账的总回款金额 |
| | | Map<Long, BigDecimal> receiptTotals = new HashMap<>(); |
| | | if (!CollectionUtils.isEmpty(receiptPayments)) { |
| | | for (ReceiptPayment receiptPayment : receiptPayments) { |
| | | if (receiptPayment.getSalesLedgerId() != null && receiptPayment.getReceiptPaymentAmount() != null) { |
| | | // 如果 key 存在则相加,不存在则放入 |
| | | receiptTotals.merge(receiptPayment.getSalesLedgerId(), receiptPayment.getReceiptPaymentAmount(), BigDecimal::add); |
| | | } |
| | | } |
| | | if(existFlag){ |
| | | salesLedger.setNoInvoiceAmountTotal(noInvoiceAmountTotal); |
| | | }else { |
| | | salesLedger.setNoInvoiceAmountTotal(salesLedger.getContractAmount()); |
| | | } |
| | | salesLedger.setInvoiceTotal(invoiceTotal); |
| | | } |
| | | |
| | | for (SalesLedger salesLedger : iPage.getRecords()) { |
| | | Long ledgerId = salesLedger.getId(); |
| | | // 合同总金额 |
| | | BigDecimal contractAmount = salesLedger.getContractAmount() == null ? BigDecimal.ZERO : salesLedger.getContractAmount(); |
| | | // 开票总额和回款总额 |
| | | BigDecimal invoiceTotal = invoiceTotals.getOrDefault(ledgerId, BigDecimal.ZERO); |
| | | BigDecimal receiptPaymentAmountTotal = receiptTotals.getOrDefault(ledgerId, BigDecimal.ZERO); |
| | | |
| | | // 未开票金额 = 合同金额 - 已开票金额 |
| | | BigDecimal noInvoiceAmountTotal = contractAmount.subtract(invoiceTotal); |
| | | if (noInvoiceAmountTotal.compareTo(BigDecimal.ZERO) < 0) { |
| | | noInvoiceAmountTotal = BigDecimal.ZERO; |
| | | } |
| | | |
| | | // 待回款金额 = 已开票金额 - 已回款金额 |
| | | BigDecimal noReceiptPaymentAmountTotal = invoiceTotal.subtract(receiptPaymentAmountTotal); |
| | | if (noReceiptPaymentAmountTotal.compareTo(BigDecimal.ZERO) < 0) { |
| | | noReceiptPaymentAmountTotal = BigDecimal.ZERO; |
| | | } |
| | | |
| | | salesLedger.setNoInvoiceAmountTotal(noInvoiceAmountTotal); |
| | | salesLedger.setInvoiceTotal(invoiceTotal); |
| | | salesLedger.setReceiptPaymentAmountTotal(receiptPaymentAmountTotal); |
| | | salesLedger.setNoReceiptAmount(noReceiptPaymentAmountTotal); |
| | | |
| | | // 如果已经有过开票或回款操作,则不允许编辑 |
| | | boolean hasInvoiceOperation = invoiceTotal.compareTo(BigDecimal.ZERO) > 0; |
| | | boolean hasReceiptOperation = receiptPaymentAmountTotal.compareTo(BigDecimal.ZERO) > 0; |
| | | salesLedger.setIsEdit(!(hasInvoiceOperation || hasReceiptOperation)); |
| | | } |
| | | |
| | | if (ObjectUtils.isNotEmpty(salesLedgerDto.getStatus())) { |
| | | if (salesLedgerDto.getStatus()) { |
| | | iPage.getRecords().removeIf(salesLedger -> Objects.equals(salesLedger.getNoInvoiceAmountTotal(), new BigDecimal("0.00"))); |
| | | // 清除所有“未开票金额”为 0 的记录 |
| | | iPage.getRecords().removeIf(salesLedger -> |
| | | Objects.equals(salesLedger.getNoInvoiceAmountTotal(), new BigDecimal("0.00"))); |
| | | iPage.setTotal(iPage.getRecords().size()); |
| | | } |
| | | } |