From a484951645eb311c81c5a73733fdb493f58e7eff Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 16 一月 2026 09:53:56 +0800
Subject: [PATCH] fix: 删除来票登记及对应台账后本次来票金额未正确回滚

---
 src/main/java/com/ruoyi/sales/service/impl/InvoiceLedgerServiceImpl.java |  155 ++++++++++++++++++++++++++++++---------------------
 1 files changed, 92 insertions(+), 63 deletions(-)

diff --git a/src/main/java/com/ruoyi/sales/service/impl/InvoiceLedgerServiceImpl.java b/src/main/java/com/ruoyi/sales/service/impl/InvoiceLedgerServiceImpl.java
index 4722ac4..614fc20 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/InvoiceLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/InvoiceLedgerServiceImpl.java
@@ -381,55 +381,71 @@
 
     /**
      * 澶勭悊褰撴湀寮�绁ㄥ彴璐﹀垹闄�
+     *
      * @param invoiceRegistrationProductId
      * @param invoiceRegistrationProduct
      */
-    private void dealCurrentMonthDel(Integer invoiceRegistrationProductId,InvoiceRegistrationProduct invoiceRegistrationProduct){
+
+    private void dealCurrentMonthDel(Integer invoiceRegistrationProductId, InvoiceRegistrationProduct invoiceRegistrationProduct) {
+
         // 鍒犻櫎寮�绁ㄥ彴璐�
-        QueryWrapper<InvoiceLedger> delMapper = new QueryWrapper<>();
-        delMapper.eq("invoice_registration_product_id", invoiceRegistrationProductId);
-        invoiceLedgerMapper.delete(delMapper);
-        // 鍒犻櫎寮�绁ㄧ櫥璁板苟鍥炴粴
+        QueryWrapper<InvoiceLedger> delLedgerWrapper = new QueryWrapper<>();
+        delLedgerWrapper.eq("invoice_registration_product_id", invoiceRegistrationProductId);
+        invoiceLedgerMapper.delete(delLedgerWrapper);
+
+        // 鏌ヨ褰撳墠浜у搧涓嬬殑鎵�鏈夊紑绁ㄧ櫥璁�
         QueryWrapper<InvoiceRegistrationProduct> queryWrapper = new QueryWrapper<>();
         queryWrapper.eq("sales_ledger_id", invoiceRegistrationProduct.getSalesLedgerId());
         queryWrapper.eq("sales_ledger_product_id", invoiceRegistrationProduct.getSalesLedgerProductId());
         queryWrapper.orderByAsc("create_time");
-        List<InvoiceRegistrationProduct> invoiceRegistrationProductList = invoiceRegistrationProductMapper.selectList(queryWrapper);
-//        InvoiceRegistrationProduct invoiceRegistrationProduct = invoiceRegistrationProductMapper.selectById(invoiceRegistrationProductId);
-        int index =  -1;
-        for (int i = 0; i < invoiceRegistrationProductList.size(); i++) {
-            InvoiceRegistrationProduct currentInvoiceRegProduct = invoiceRegistrationProductList.get(i);
-            if(invoiceRegistrationProduct.getId().equals(currentInvoiceRegProduct.getId())){
+        List<InvoiceRegistrationProduct> allList = invoiceRegistrationProductMapper.selectList(queryWrapper);
+
+        int index = -1;
+        for (int i = 0; i < allList.size(); i++) {
+            if (invoiceRegistrationProductId.equals(allList.get(i).getId())) {
                 index = i;
+                break;
             }
         }
-        if(index == -1){
+        if (index == -1) {
             return;
         }
-        for (int i = index + 1; i < invoiceRegistrationProductList.size(); i++) {
-            InvoiceRegistrationProduct currentInvoiceRegProduct = invoiceRegistrationProductList.get(i);
-            // 鍥炴粴鏈紑绁ㄦ暟/鏈紑绁ㄩ噾棰�
-            BigDecimal noInvoiceAmount = currentInvoiceRegProduct.getNoInvoiceAmount().add(invoiceRegistrationProduct.getInvoiceAmount());
-            BigDecimal noInvoiceNum = currentInvoiceRegProduct.getNoInvoiceNum().add(invoiceRegistrationProduct.getInvoiceNum());
-            currentInvoiceRegProduct.setNoInvoiceAmount(noInvoiceAmount);
-            currentInvoiceRegProduct.setNoInvoiceNum(noInvoiceNum);
-            invoiceRegistrationProductMapper.updateById(currentInvoiceRegProduct);
+
+        for (int i = index + 1; i < allList.size(); i++) {
+            InvoiceRegistrationProduct current = allList.get(i);
+
+            current.setNoInvoiceNum(current.getNoInvoiceNum().add(invoiceRegistrationProduct.getInvoiceNum()));
+            current.setNoInvoiceAmount(current.getNoInvoiceAmount().add(invoiceRegistrationProduct.getInvoiceAmount()));
+
+            invoiceRegistrationProductMapper.updateById(current);
         }
+
+        // 鍒犻櫎褰撳墠寮�绁ㄧ櫥璁�
         invoiceRegistrationProductMapper.deleteById(invoiceRegistrationProductId);
-        // 淇敼sale_product鏁版嵁
-        SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(invoiceRegistrationProduct.getSalesLedgerProductId());
+
+        // 鏌ヨ鍒犻櫎鍚庣殑鍓╀綑寮�绁ㄧ櫥璁�
         QueryWrapper<InvoiceRegistrationProduct> newQueryWrapper = new QueryWrapper<>();
-        queryWrapper.eq("sales_ledger_id", invoiceRegistrationProduct.getSalesLedgerId());
-        queryWrapper.eq("sales_ledger_product_id", invoiceRegistrationProduct.getSalesLedgerProductId());
-        queryWrapper.orderByAsc("create_time");
-        List<InvoiceRegistrationProduct> newInvoiceRegistrationProductList = invoiceRegistrationProductMapper.selectList(newQueryWrapper);
-        if(CollectionUtils.isEmpty(newInvoiceRegistrationProductList)){
+        newQueryWrapper.eq("sales_ledger_id", invoiceRegistrationProduct.getSalesLedgerId());
+        newQueryWrapper.eq("sales_ledger_product_id", invoiceRegistrationProduct.getSalesLedgerProductId());
+        newQueryWrapper.orderByAsc("create_time");
+        List<InvoiceRegistrationProduct> remainList = invoiceRegistrationProductMapper.selectList(newQueryWrapper);
+
+        // 鏌ヨ閿�鍞彴璐︿骇鍝�
+        SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(invoiceRegistrationProduct.getSalesLedgerProductId());
+
+        if (CollectionUtils.isEmpty(remainList)) {
+            // 娌℃湁浠讳綍寮�绁ㄨ褰曪紝鎭㈠鍒濆鐘舵��
+            salesLedgerProduct.setInvoiceNum(BigDecimal.ZERO);
+            salesLedgerProduct.setInvoiceAmount(BigDecimal.ZERO);
             salesLedgerProduct.setNoInvoiceNum(salesLedgerProduct.getQuantity());
             salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxInclusiveTotalPrice());
-        }else {
+        } else {
+            salesLedgerProduct.setInvoiceNum(salesLedgerProduct.getInvoiceNum().subtract(invoiceRegistrationProduct.getInvoiceNum()));
+            salesLedgerProduct.setInvoiceAmount(salesLedgerProduct.getInvoiceAmount().subtract(invoiceRegistrationProduct.getInvoiceAmount()));
             salesLedgerProduct.setNoInvoiceNum(salesLedgerProduct.getNoInvoiceNum().add(invoiceRegistrationProduct.getInvoiceNum()));
             salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getNoInvoiceAmount().add(invoiceRegistrationProduct.getInvoiceAmount()));
         }
+
         salesLedgerProductMapper.updateById(salesLedgerProduct);
     }
 
@@ -438,46 +454,59 @@
      * @param invoiceRegistrationProduct
      * @param invoiceLedger
      */
-    private void dealOtherMonthDel(InvoiceRegistrationProduct invoiceRegistrationProduct,InvoiceLedger invoiceLedger ){
-        InvoiceRegistrationProduct copyRegProduct = new InvoiceRegistrationProduct();
-        BeanUtils.copyProperties(invoiceRegistrationProduct, copyRegProduct);
-        BigDecimal invoiceNum = copyRegProduct.getInvoiceNum().negate();
-        BigDecimal invoiceAmount = copyRegProduct.getInvoiceAmount().negate();
-        // invoice_registration_product
-        // 鍒犻櫎寮�绁ㄧ櫥璁板苟鍥炴粴
+    private void dealOtherMonthDel(InvoiceRegistrationProduct invoiceRegistrationProduct, InvoiceLedger invoiceLedger) {
+        //鏌ヨ鍘嗗彶寮�绁ㄧ櫥璁�
         QueryWrapper<InvoiceRegistrationProduct> queryWrapper = new QueryWrapper<>();
         queryWrapper.eq("sales_ledger_id", invoiceRegistrationProduct.getSalesLedgerId());
         queryWrapper.eq("sales_ledger_product_id", invoiceRegistrationProduct.getSalesLedgerProductId());
         queryWrapper.orderByAsc("create_time");
-        List<InvoiceRegistrationProduct> invoiceRegistrationProductList = invoiceRegistrationProductMapper.selectList(queryWrapper);
-        InvoiceRegistrationProduct lastInvoiceRegistrationProduct = invoiceRegistrationProductList.get(invoiceRegistrationProductList.size() - 1);
-        BigDecimal noInvoiceNum = lastInvoiceRegistrationProduct.getNoInvoiceNum().subtract(invoiceNum);
-        BigDecimal noInvoiceAmount = lastInvoiceRegistrationProduct.getNoInvoiceAmount().subtract(invoiceAmount);
-        copyRegProduct.setInvoiceNum(invoiceNum);
-        copyRegProduct.setInvoiceAmount(invoiceAmount);
-        copyRegProduct.setNoInvoiceNum(noInvoiceNum);
-        copyRegProduct.setNoInvoiceAmount(noInvoiceAmount);
-        copyRegProduct.setId(null);
-        invoiceRegistrationProductMapper.insert(copyRegProduct);
+        List<InvoiceRegistrationProduct> list = invoiceRegistrationProductMapper.selectList(queryWrapper);
+
+        if (CollectionUtils.isEmpty(list)) {
+            return;
+        }
+
+        InvoiceRegistrationProduct last = list.get(list.size() - 1);
+
+        BigDecimal negateInvoiceNum = invoiceRegistrationProduct.getInvoiceNum().negate();
+        BigDecimal negateInvoiceAmount = invoiceRegistrationProduct.getInvoiceAmount().negate();
+
+        InvoiceRegistrationProduct copy = new InvoiceRegistrationProduct();
+        BeanUtils.copyProperties(invoiceRegistrationProduct, copy);
+
+        copy.setId(null);
+        copy.setInvoiceNum(negateInvoiceNum);
+        copy.setInvoiceAmount(negateInvoiceAmount);
+
+        // 鏈紑绁� = 涓婁竴鏉� + 鏈鍐插洖鐨勬暟閲�
+        copy.setNoInvoiceNum(last.getNoInvoiceNum().add(invoiceRegistrationProduct.getInvoiceNum()));
+        copy.setNoInvoiceAmount(last.getNoInvoiceAmount().add(invoiceRegistrationProduct.getInvoiceAmount()));
+
+        invoiceRegistrationProductMapper.insert(copy);
+
         // 澶嶅埗寮�绁ㄥ彴璐�
-        InvoiceLedger invoiceLedgerCopy = new InvoiceLedger();
-        BeanUtils.copyProperties(invoiceLedger, invoiceLedgerCopy);
-        invoiceLedgerCopy.setInvoiceRegistrationProductId(copyRegProduct.getId());
-        BigDecimal invoiceTotal = invoiceLedgerCopy.getInvoiceTotal().negate();
-        invoiceLedgerCopy.setInvoiceTotal(invoiceTotal);
-        invoiceLedgerCopy.setId(null);
-        invoiceLedgerCopy.setInvoiceDate(LocalDate.now());
-        invoiceLedgerMapper.insert(invoiceLedgerCopy);
-        // 鏇存柊sales_leger_product
-        QueryWrapper<SalesLedgerProduct> salesLedgerProductQueryWrapper = new QueryWrapper<>();
-        salesLedgerProductQueryWrapper.eq("id", invoiceRegistrationProduct.getSalesLedgerProductId());
-        SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectOne(salesLedgerProductQueryWrapper);
-        salesLedgerProduct.setNoInvoiceNum(noInvoiceNum);
-        salesLedgerProduct.setNoInvoiceAmount(noInvoiceAmount);
-        BigDecimal newInvoiceNum = salesLedgerProduct.getInvoiceNum().add(invoiceNum);
-        BigDecimal newInvocieAmount = salesLedgerProduct.getInvoiceAmount().add(invoiceAmount);
-        salesLedgerProduct.setInvoiceNum(newInvoiceNum);
-        salesLedgerProduct.setInvoiceAmount(newInvocieAmount);
+        InvoiceLedger ledgerCopy = new InvoiceLedger();
+        BeanUtils.copyProperties(invoiceLedger, ledgerCopy);
+
+        ledgerCopy.setId(null);
+        ledgerCopy.setInvoiceRegistrationProductId(copy.getId());
+        ledgerCopy.setInvoiceTotal(invoiceLedger.getInvoiceTotal().negate());
+        ledgerCopy.setInvoiceDate(LocalDate.now());
+
+        invoiceLedgerMapper.insert(ledgerCopy);
+
+        // 鍥炴粴 sales_ledger_product
+        SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(invoiceRegistrationProduct.getSalesLedgerProductId());
+
+        // 宸插紑绁�
+        salesLedgerProduct.setInvoiceNum(salesLedgerProduct.getInvoiceNum().add(negateInvoiceNum));
+        salesLedgerProduct.setInvoiceAmount(salesLedgerProduct.getInvoiceAmount().add(negateInvoiceAmount));
+
+        // 鏈紑绁�
+        salesLedgerProduct.setNoInvoiceNum(salesLedgerProduct.getNoInvoiceNum().add(invoiceRegistrationProduct.getInvoiceNum()));
+        salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getNoInvoiceAmount().add(invoiceRegistrationProduct.getInvoiceAmount()));
+
         salesLedgerProductMapper.updateById(salesLedgerProduct);
     }
+
 }

--
Gitblit v1.9.3