From 1ab612fa454477bfaec03b0fe9133f8881ce29cc Mon Sep 17 00:00:00 2001
From: maven <2163098428@qq.com>
Date: 星期五, 16 一月 2026 16:16:02 +0800
Subject: [PATCH] yys 回款---财务收入,付款---财务支出

---
 src/main/java/com/ruoyi/purchase/service/impl/PaymentRegistrationServiceImpl.java |  244 ++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 200 insertions(+), 44 deletions(-)

diff --git a/src/main/java/com/ruoyi/purchase/service/impl/PaymentRegistrationServiceImpl.java b/src/main/java/com/ruoyi/purchase/service/impl/PaymentRegistrationServiceImpl.java
index ec355bd..a32b6f6 100644
--- a/src/main/java/com/ruoyi/purchase/service/impl/PaymentRegistrationServiceImpl.java
+++ b/src/main/java/com/ruoyi/purchase/service/impl/PaymentRegistrationServiceImpl.java
@@ -6,11 +6,15 @@
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.account.mapper.AccountExpenseMapper;
+import com.ruoyi.account.pojo.AccountExpense;
+import com.ruoyi.account.service.AccountExpenseService;
 import com.ruoyi.basic.mapper.SupplierManageMapper;
 import com.ruoyi.basic.pojo.SupplierManage;
 import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.framework.security.LoginUser;
+import com.ruoyi.purchase.dto.PaymentHistoryRecordVo;
 import com.ruoyi.purchase.dto.PaymentLedgerDto;
 import com.ruoyi.purchase.dto.PaymentRegistrationDto;
 import com.ruoyi.purchase.mapper.*;
@@ -21,10 +25,12 @@
 import com.ruoyi.purchase.service.IPaymentRegistrationService;
 import com.ruoyi.sales.mapper.SalesLedgerMapper;
 import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
+import com.ruoyi.sales.pojo.ReceiptPayment;
 import com.ruoyi.sales.pojo.SalesLedger;
 import com.ruoyi.sales.pojo.SalesLedgerProduct;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
+import org.springframework.util.ObjectUtils;
 import org.springframework.util.StringUtils;
 
 import java.math.BigDecimal;
@@ -47,8 +53,6 @@
 
     private PurchaseLedgerMapper purchaseLedgerMapper;
 
-    private InvoicePurchaseMapper invoicePurchaseMapper;
-
     private SalesLedgerMapper salesLedgerMapper;
 
     private SupplierManageMapper supplierManageMapper;
@@ -58,6 +62,8 @@
     private TicketRegistrationMapper ticketRegistrationMapper;
 
     private ProductRecordMapper productRecordMapper;
+
+    private AccountExpenseService accountExpenseService;
 
     /**
      * 鏌ヨ浠樻鐧昏
@@ -92,42 +98,48 @@
     /**
      * 鏂板浠樻鐧昏
      *
-     * @param paymentRegistration 浠樻鐧昏
+     * @param paymentRegistrationList 浠樻鐧昏
      * @return 缁撴灉
      */
     @Override
-    public int insertPaymentRegistration(PaymentRegistration paymentRegistration) {
-        PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(paymentRegistration.getPurchaseLedgerId());
-        SalesLedger salesLedger = salesLedgerMapper.selectOne(new QueryWrapper<SalesLedger>().
-                eq("sales_contract_no", purchaseLedger.getSalesContractNo()));
-        if (salesLedger == null) {
-            throw new RuntimeException("鍏宠仈閿�鍞悎鍚屽彿涓嶅瓨鍦�");
+    public int insertPaymentRegistration(List<PaymentRegistration> paymentRegistrationList) {
+        LoginUser userId = SecurityUtils.getLoginUser();
+        for (PaymentRegistration paymentRegistration : paymentRegistrationList) {
+            PaymentRegistration byId = paymentRegistration;
+            if (!ObjectUtils.isEmpty(paymentRegistration.getId())){
+                paymentRegistration = this.getById(paymentRegistration.getId());
+            }
+            PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(paymentRegistration.getPurchaseLedgerId());
+            if(null==purchaseLedger) throw new RuntimeException("鏈壘鍒伴噰璐崟");
+            // 宸插洖娆鹃噾棰�
+            SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(paymentRegistration.getSalesLedgerProductId());
+            if(null==salesLedgerProduct) throw new RuntimeException("鏈壘鍒伴噰璐崟浜у搧");
+            if (salesLedgerProduct.getPendingTicketsTotal().compareTo(paymentRegistration.getCurrentPaymentAmount())<0){
+                throw new RuntimeException("鏈鍥炴閲戦涓嶈兘澶т簬寰呭洖娆鹃噾棰�");
+            }
+            paymentRegistration.setCreateTime(DateUtils.getNowDate());
+            paymentRegistration.setUpdateTime(DateUtils.getNowDate());
+            paymentRegistration.setRegistrantId(userId.getUserId());
+            salesLedgerProduct.setTicketsTotal(salesLedgerProduct.getTicketsTotal().add(paymentRegistration.getCurrentPaymentAmount()));
+            salesLedgerProduct.setPendingTicketsTotal(salesLedgerProduct.getTaxInclusiveTotalPrice().subtract(salesLedgerProduct.getTicketsTotal()));
+            paymentRegistrationMapper.insert(paymentRegistration);
+            salesLedgerProductMapper.updateById(salesLedgerProduct);
+            // 2. 澶勭悊璐︽埛鏀嚭
+            AccountExpense accountExpense = new AccountExpense();
+            accountExpense.setExpenseDate(purchaseLedger.getEntryDate());
+            accountExpense.setExpenseType("0");
+            accountExpense.setSupplierName(purchaseLedger.getSupplierName());
+            accountExpense.setExpenseMoney(paymentRegistration.getCurrentPaymentAmount());
+            accountExpense.setExpenseDescribed("浠樻鏀嚭");
+            accountExpense.setExpenseMethod("0");
+            accountExpense.setBusinessId(paymentRegistration.getId());
+            accountExpense.setBusinessType(1);
+            accountExpense.setInputTime(new Date());
+            accountExpense.setInputUser(userId.getNickName());
+            accountExpenseService.save(accountExpense);
         }
+        return 1;
 
-        paymentRegistration.setSaleLedgerId(salesLedger.getId());
-        paymentRegistration.setSupplierId(purchaseLedger.getSupplierId());
-
-        TicketRegistration tr = ticketRegistrationMapper.selectOne(new LambdaQueryWrapper<TicketRegistration>().eq(TicketRegistration::getId, paymentRegistration.getTicketRegistrationId()));
-
-        if (tr == null) {
-            throw new RuntimeException("鍏宠仈鍙戠エ涓嶅瓨鍦�");
-        }
-
-        List<PaymentRegistration> paymentRegistrations = paymentRegistrationMapper.selectList(new QueryWrapper<PaymentRegistration>()
-                .eq("ticket_registration_id", tr.getId()));
-        BigDecimal total = paymentRegistrations.stream().map(PaymentRegistration::getCurrentPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
-
-        if (total.add(paymentRegistration.getCurrentPaymentAmount()).compareTo(tr.getInvoiceAmount()) > 0) {
-            throw new RuntimeException("浠樻閲戦瓒呭嚭鍙戠エ閲戦");
-        }
-
-        LoginUser loginUser = SecurityUtils.getLoginUser();
-        Integer tenantId = loginUser.getTenantId();
-        paymentRegistration.setTenantId(tenantId.longValue());
-        paymentRegistration.setRegistrantId(loginUser.getUserId());
-        paymentRegistration.setCreateTime(DateUtils.getNowDate());
-        paymentRegistration.setUpdateTime(DateUtils.getNowDate());
-        return paymentRegistrationMapper.insert(paymentRegistration);
     }
 
     /**
@@ -138,17 +150,30 @@
      */
     @Override
     public int updatePaymentRegistration(PaymentRegistration paymentRegistration) {
-        TicketRegistration ticketRegistration = ticketRegistrationMapper.selectById(paymentRegistration.getTicketRegistrationId());
-
-        List<PaymentRegistration> paymentRegistrations = paymentRegistrationMapper.selectList(new QueryWrapper<PaymentRegistration>()
-                .eq("ticket_registration_id", paymentRegistration.getTicketRegistrationId()).ne("id", paymentRegistration.getId()));
-        BigDecimal total = paymentRegistrations.stream().map(PaymentRegistration::getCurrentPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
-
-        if (total.add(paymentRegistration.getCurrentPaymentAmount()).compareTo(ticketRegistration.getInvoiceAmount()) > 0) {
-            throw new RuntimeException("浠樻閲戦瓒呭嚭鍙戠エ閲戦");
+        PaymentRegistration paymentRegistration1 = paymentRegistrationMapper.selectById(paymentRegistration.getId());
+        if(null==paymentRegistration1) throw new RuntimeException("鏈壘鍒颁粯娆剧櫥璁�");
+        SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(paymentRegistration1.getSalesLedgerProductId());
+        if(null==salesLedgerProduct) throw new RuntimeException("鏈壘鍒伴噰璐崟浜у搧");
+        // 鍒ゆ柇浠樻閲戦涓嶅彲澶т簬寰呬粯娆鹃噾棰�
+        BigDecimal subtract = paymentRegistration.getCurrentPaymentAmount().subtract(paymentRegistration1.getCurrentPaymentAmount());
+        if (subtract.compareTo(salesLedgerProduct.getPendingTicketsTotal()) > 0) {
+            throw new RuntimeException("浠樻閲戦瓒呭嚭寰呬粯娆鹃噾棰�");
         }
-
         paymentRegistration.setUpdateTime(DateUtils.getNowDate());
+        // 鍚屾淇敼璐︽埛鏀嚭
+        LambdaQueryWrapper<AccountExpense> accountExpenseLambdaQueryWrapper = new LambdaQueryWrapper<>();
+        accountExpenseLambdaQueryWrapper.eq(AccountExpense::getBusinessId, paymentRegistration.getId())
+                .eq(AccountExpense::getBusinessType, 1)
+                .last("limit 1");
+        AccountExpense accountExpense = accountExpenseService.getOne(accountExpenseLambdaQueryWrapper);
+        if(null!=accountExpense){
+            accountExpense.setExpenseMoney(paymentRegistration.getCurrentPaymentAmount());
+            accountExpenseService.updateById(accountExpense);
+        }
+        // 淇敼閲囪喘浜у搧浠樻閲戦
+        salesLedgerProduct.setTicketsTotal(salesLedgerProduct.getTicketsTotal().add(subtract));
+        salesLedgerProduct.setPendingTicketsTotal(salesLedgerProduct.getTaxInclusiveTotalPrice().subtract(salesLedgerProduct.getTicketsTotal()));
+        salesLedgerProductMapper.updateById(salesLedgerProduct);
         return paymentRegistrationMapper.updateById(paymentRegistration);
     }
 
@@ -203,6 +228,7 @@
         for (SupplierManage supplierManage : supplierManages) {
             Map<String, Object> res = new HashMap<>();
             res.put("supplierName", supplierManage.getSupplierName());
+            res.put("supplierId", supplierManage.getId());
 
             // 搴斾粯閲戦璁$畻
             BigDecimal payableAmount = BigDecimal.ZERO;
@@ -214,7 +240,7 @@
                     .map(PurchaseLedger::getId)
                     .filter(Objects::nonNull)
                     .flatMap(id -> salesLedgerProductMapper.selectList(
-                            new QueryWrapper<SalesLedgerProduct>().eq("sales_ledger_id", id)
+                            new QueryWrapper<SalesLedgerProduct>().eq("sales_ledger_id", id).eq("type",2)
                     ).stream())
                     .collect(Collectors.toList());
             payableAmount = salesLedgerProducts.stream()
@@ -268,7 +294,6 @@
                                             new LambdaQueryWrapper<ProductRecord>().eq(ProductRecord::getTicketRegistrationId, ticketRegistration.getId())
                                     ).stream()
                                     .map(ProductRecord::getTicketsNum)
-                                    .map(BigDecimal::new)
                                     .filter(Objects::nonNull)
                                     .reduce(BigDecimal.ZERO, BigDecimal::add);
                             detail.put("voteCount", voteCount);
@@ -289,6 +314,8 @@
             detailPagination.put("pageSize", detailPageSize);
             detailPagination.put("pages", (int) Math.ceil((double) totalDetails / detailPageSize));
 
+            // 搴斾粯閲戦閫昏緫涓嶉噰鐢ㄥ悎鍚岄噾棰濇敼鎴�  鍙戠エ閲戦鍑忎粯娆鹃噾棰�
+            payableAmount = invoiceAmount.subtract(paymentAmount);
             res.put("invoiceAmount", invoiceAmount);
             res.put("payableAmount", payableAmount);
             res.put("paymentAmount", paymentAmount);
@@ -345,6 +372,135 @@
         return result;
     }
 
+    /**
+     * 鏌ヨ浠樻鐧昏鍒楄〃
+     *
+     * @param paymentRegistrationDto 浠樻鐧昏
+     * @return 浠樻鐧昏闆嗗悎
+     */
+    @Override
+    public List<PaymentRegistrationDto> paymentHistoryList(PaymentRegistrationDto paymentRegistrationDto) {
+        return paymentRegistrationMapper.paymentHistoryList(paymentRegistrationDto);
+    }
+
+    /**
+     * 鏌ヨ渚涘簲鍟嗗線鏉ヨ褰�
+     * @param supplierId
+     * @return
+     */
+    @Override
+    public List<PaymentHistoryRecordVo> getPaymentRecordList(Long supplierId) {
+        List<PaymentHistoryRecordVo> paymentRecordList = paymentRegistrationMapper.getPaymentRecordList(supplierId);
+        List<PaymentHistoryRecordVo> result = new ArrayList<>();
+        List<PaymentHistoryRecordVo> newResult = new ArrayList<>();
+        // 搴斾粯鎬婚噾棰濋噾棰濊绠�
+        BigDecimal amountTotal = BigDecimal.ZERO;
+        if(CollectionUtils.isNotEmpty(paymentRecordList)) {
+            Map<LocalDate, List<PaymentHistoryRecordVo>> dateListMap = paymentRecordList.stream().collect(
+                                                                        Collectors.groupingBy(
+                                                                                PaymentHistoryRecordVo::getHappenTime,
+                                                                                LinkedHashMap::new,
+                                                                                Collectors.toList()
+                                                                        )
+                                                                        );
+            for (LocalDate localDate : dateListMap.keySet()) {
+                BigDecimal currentPaymentAmount = BigDecimal.ZERO;
+                BigDecimal invoiceAmount = BigDecimal.ZERO;
+                BigDecimal currentDateTotal = BigDecimal.ZERO;
+                List<PaymentHistoryRecordVo> paymentHistoryRecordVos = dateListMap.get(localDate);
+                // 璁$畻褰撳ぉ姹囨鏁�
+                currentPaymentAmount = paymentHistoryRecordVos.stream()
+                                            .filter(item ->item.getType() == 0)
+                                            .map(PaymentHistoryRecordVo::getCurrentPaymentAmount)
+                                            .reduce(BigDecimal.ZERO,BigDecimal::add);
+                // 璁$畻浠婂ぉ鏉ョエ鏁�
+                invoiceAmount = paymentHistoryRecordVos.stream()
+                        .filter(item ->item.getType() == 1)
+                        .map(PaymentHistoryRecordVo::getInvoiceAmount)
+                        .reduce(BigDecimal.ZERO,BigDecimal::add);
+                // 璁$畻褰撴棩姹囨��
+                currentDateTotal = currentDateTotal.add(invoiceAmount).subtract(currentPaymentAmount);
+                PaymentHistoryRecordVo paymentHistoryRecordVo = new PaymentHistoryRecordVo();
+                paymentHistoryRecordVo.setHappenTime(localDate);
+                paymentHistoryRecordVo.setCurrentPaymentAmount(currentPaymentAmount);
+                paymentHistoryRecordVo.setInvoiceAmount(invoiceAmount);
+                amountTotal = paymentHistoryRecordVo.getInvoiceAmount().subtract(paymentHistoryRecordVo.getCurrentPaymentAmount());
+                paymentHistoryRecordVo.setPayableAmount(amountTotal);
+                result.add(paymentHistoryRecordVo);
+            }
+
+
+            for (int i = 0; i < result.size(); i++) {
+                PaymentHistoryRecordVo paymentHistoryRecordVo = result.get(i);
+                if (i == 0) {
+                    paymentHistoryRecordVo.setPayableAmount(paymentHistoryRecordVo.getInvoiceAmount().subtract(paymentHistoryRecordVo.getCurrentPaymentAmount()));
+                }else {
+                    PaymentHistoryRecordVo paymentHistoryRecordVo1 = result.get(i-1);
+                    paymentHistoryRecordVo.setPayableAmount(paymentHistoryRecordVo1.getPayableAmount()
+                            .add(paymentHistoryRecordVo.getInvoiceAmount()).subtract(paymentHistoryRecordVo.getCurrentPaymentAmount()));
+                }
+                newResult.add(paymentHistoryRecordVo);
+            }
+        }
+        return newResult;
+    }
+
+    /**
+     * 鏌ヨ浠樻鐧昏鍒楄〃鍒嗛〉
+     *
+     * @param paymentRegistrationDto 浠樻鐧昏
+     * @return 浠樻鐧昏闆嗗悎
+     */
+    @Override
+    public IPage<PaymentRegistrationDto> paymentHistoryListPage(Page page, PaymentRegistrationDto paymentRegistrationDto) {
+        return paymentRegistrationMapper.paymentHistoryListPage(page, paymentRegistrationDto);
+    }
+
+    @Override
+    public Boolean delete(Long[] ids) {
+        for (Long id : ids) {
+            List<ProductRecord> productRecords = productRecordMapper.selectList(new QueryWrapper<ProductRecord>().lambda().eq(ProductRecord::getTicketRegistrationId, id));
+            for (ProductRecord productRecord : productRecords) {
+                SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(productRecord.getSaleLedgerProjectId());
+                salesLedgerProduct.setTicketsNum(salesLedgerProduct.getTicketsNum().subtract(productRecord.getTicketsNum()));
+                salesLedgerProduct.setTicketsAmount(salesLedgerProduct.getTicketsAmount().subtract(productRecord.getTicketsAmount()));
+                salesLedgerProduct.setFutureTickets(salesLedgerProduct.getFutureTickets().add(productRecord.getTicketsNum()));
+                salesLedgerProduct.setFutureTicketsAmount(salesLedgerProduct.getFutureTicketsAmount().add(productRecord.getTicketsAmount()));
+                salesLedgerProductMapper.updateById(salesLedgerProduct);
+            }
+            ticketRegistrationMapper.delete(new QueryWrapper<TicketRegistration>().lambda().eq(TicketRegistration::getId, id));
+            productRecordMapper.delete(new QueryWrapper<ProductRecord>().lambda().eq(ProductRecord::getTicketRegistrationId, id));
+        }
+        return true;
+    }
+
+    @Override
+    public int delPaymentRegistration(List<Long> ids) {
+        LambdaQueryWrapper<AccountExpense> accountExpenseLambdaQueryWrapper = new LambdaQueryWrapper<>();
+        accountExpenseLambdaQueryWrapper.in(AccountExpense::getBusinessId, ids)
+                .eq(AccountExpense::getBusinessType, 1);
+        accountExpenseService.remove(accountExpenseLambdaQueryWrapper);
+        // 淇敼閲囪喘浜у搧鐨勫凡浠樻閲戦,寰呬粯娆鹃噾棰�
+        List<PaymentRegistration> paymentRegistrations = paymentRegistrationMapper.selectBatchIds(ids);
+        for (PaymentRegistration paymentRegistration : paymentRegistrations) {
+            SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(paymentRegistration.getSalesLedgerProductId());
+            salesLedgerProduct.setTicketsTotal(salesLedgerProduct.getTicketsTotal().subtract(paymentRegistration.getCurrentPaymentAmount()));
+            salesLedgerProduct.setPendingTicketsTotal(salesLedgerProduct.getPendingTicketsTotal().add(paymentRegistration.getCurrentPaymentAmount()));
+            salesLedgerProductMapper.updateById(salesLedgerProduct);
+        }
+        return paymentRegistrationMapper.deleteBatchIds(ids);
+    }
+
+    @Override
+    public IPage<PaymentRegistrationDto> supplierNameListPage(Page page, PaymentLedgerDto paymentLedgerDto) {
+        return paymentRegistrationMapper.supplierNameListPage(page, paymentLedgerDto);
+    }
+
+    @Override
+    public List<PaymentRegistrationDto> supplierNameListPageDetails(PaymentLedgerDto paymentLedgerDto) {
+        return paymentRegistrationMapper.supplierNameListPageDetails(paymentLedgerDto);
+    }
+
     // 鎵归噺鏌ヨ閲囪喘鍙拌处锛堝綋鏈堬級
     private Map<Long, List<PurchaseLedger>> batchQueryPurchaseLedgers(List<Long> supplierIds, LocalDate startDate, LocalDate endDate) {
         LambdaQueryWrapper<PurchaseLedger> query = new LambdaQueryWrapper<>();

--
Gitblit v1.9.3