From 60117d199e9345ac3e5287f1e82d51f74f14dddb Mon Sep 17 00:00:00 2001
From: maven <2163098428@qq.com>
Date: 星期二, 13 一月 2026 15:57:23 +0800
Subject: [PATCH] yys 付款,回款改为绑定台账,产品

---
 src/main/java/com/ruoyi/purchase/service/impl/PaymentRegistrationServiceImpl.java |  163 ++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 120 insertions(+), 43 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 38e0df3..dd3a341 100644
--- a/src/main/java/com/ruoyi/purchase/service/impl/PaymentRegistrationServiceImpl.java
+++ b/src/main/java/com/ruoyi/purchase/service/impl/PaymentRegistrationServiceImpl.java
@@ -22,10 +22,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;
@@ -91,42 +93,33 @@
     /**
      * 鏂板浠樻鐧昏
      *
-     * @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) {
+        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.getSaleLedgerProductId());
+            if(null==salesLedgerProduct) throw new RuntimeException("鏈壘鍒伴噰璐崟浜у搧");
+            if (salesLedgerProduct.getPendingInvoiceTotal().compareTo(paymentRegistration.getCurrentPaymentAmount())<0){
+                throw new RuntimeException("鏈鍥炴閲戦涓嶈兘澶т簬寰呭洖娆鹃噾棰�");
+            }
+            paymentRegistration.setCreateTime(DateUtils.getNowDate());
+            paymentRegistration.setUpdateTime(DateUtils.getNowDate());
+            salesLedgerProduct.setInvoiceTotal(salesLedgerProduct.getInvoiceTotal().add(paymentRegistration.getCurrentPaymentAmount()));
+            salesLedgerProduct.setPendingInvoiceTotal(salesLedgerProduct.getTaxInclusiveTotalPrice().subtract(salesLedgerProduct.getInvoiceTotal()));
+            paymentRegistrationMapper.insert(paymentRegistration);
+            salesLedgerProductMapper.updateById(salesLedgerProduct);
         }
+        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);
     }
 
     /**
@@ -137,7 +130,9 @@
      */
     @Override
     public int updatePaymentRegistration(PaymentRegistration paymentRegistration) {
-        TicketRegistration ticketRegistration = ticketRegistrationMapper.selectById(paymentRegistration.getTicketRegistrationId());
+        PaymentRegistration paymentRegistration1 = paymentRegistrationMapper.selectById(paymentRegistration.getId());
+
+        TicketRegistration ticketRegistration = ticketRegistrationMapper.selectById(paymentRegistration.getTicketRegistrationId()==null?paymentRegistration1.getTicketRegistrationId():paymentRegistration.getTicketRegistrationId());
 
         List<PaymentRegistration> paymentRegistrations = paymentRegistrationMapper.selectList(new QueryWrapper<PaymentRegistration>()
                 .eq("ticket_registration_id", paymentRegistration.getTicketRegistrationId()).ne("id", paymentRegistration.getId()));
@@ -288,6 +283,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);
@@ -363,20 +360,100 @@
     @Override
     public List<PaymentHistoryRecordVo> getPaymentRecordList(Long supplierId) {
         List<PaymentHistoryRecordVo> paymentRecordList = paymentRegistrationMapper.getPaymentRecordList(supplierId);
-        if(!CollectionUtils.isEmpty(paymentRecordList)) {
-            // 搴斾粯閲戦璁$畻
-            BigDecimal amountTotal = BigDecimal.ZERO;
-            for (PaymentHistoryRecordVo paymentHistoryRecordVo : paymentRecordList) {
-                if(paymentHistoryRecordVo.getType() == 0){
-                    amountTotal = amountTotal.subtract(paymentHistoryRecordVo.getCurrentPaymentAmount());
-                }
-                if(paymentHistoryRecordVo.getType() == 1){
-                    amountTotal = amountTotal.add(paymentHistoryRecordVo.getInvoiceAmount());
-                }
+        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 paymentRecordList;
+        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(Long id) {
+//        PaymentRegistration paymentRegistration = paymentRegistrationMapper.selectById(id);
+//        TicketRegistration ticketRegistration = ticketRegistrationMapper.selectById(paymentRegistration.getTicketRegistrationId());
+//        if (ticketRegistration != null) {
+//            ticketRegistration.setPaymentAmountTotal(ticketRegistration.getPaymentAmountTotal().subtract(paymentRegistration.getCurrentPaymentAmount()));
+//            ticketRegistration.setUnPaymentAmountTotal(ticketRegistration.getUnPaymentAmountTotal().add(paymentRegistration.getCurrentPaymentAmount()));
+//            ticketRegistrationMapper.updateById(ticketRegistration);
+//        }
+
+        return paymentRegistrationMapper.deleteById(id);
     }
 
     // 鎵归噺鏌ヨ閲囪喘鍙拌处锛堝綋鏈堬級

--
Gitblit v1.9.3