From edbfa6b1a3a45e79cb17b06f3798535c3b1ba395 Mon Sep 17 00:00:00 2001
From: yxh <172933527@qq.com>
Date: 星期三, 18 十一月 2026 09:12:39 +0800
Subject: [PATCH] 修改了增值税城建税

---
 ruoyi-project/src/main/java/com/ruoyi/project/service/impl/ServiceFunddetailServiceImpl.java |  208 +++++++++++++++++++++++++++++++++++----------------
 1 files changed, 143 insertions(+), 65 deletions(-)

diff --git a/ruoyi-project/src/main/java/com/ruoyi/project/service/impl/ServiceFunddetailServiceImpl.java b/ruoyi-project/src/main/java/com/ruoyi/project/service/impl/ServiceFunddetailServiceImpl.java
index 6bf6c64..29d2ee9 100644
--- a/ruoyi-project/src/main/java/com/ruoyi/project/service/impl/ServiceFunddetailServiceImpl.java
+++ b/ruoyi-project/src/main/java/com/ruoyi/project/service/impl/ServiceFunddetailServiceImpl.java
@@ -130,84 +130,132 @@
     }
 
     /**
-     * 鏍规嵁鍙楃泭浜鸿韩浠借瘉缂栧彿鑾峰彇绋庤垂閲戦
+     * 鏍规嵁鍙楃泭浜鸿韩浠借瘉缂栧彿鑾峰彇绋庤垂閲戦锛堝綍鍏ユ椂鐨勪釜绋庨瑙堬級
+     * <p>
+     * 2026-11-17锛堢疮璁″彛寰勶級锛氬悓涓�韬唤璇�**褰撴湀**鐨勫鍊肩◣銆佸煄寤虹◣銆佷釜绋庨兘鎸夌疮璁¤绠楋紝鏈瑪鍙栧樊棰濓細
+     * <pre>
+     * P绱� = 危褰撴湀绋庡墠 + 鏈锛沄绱� = calcVat(P绱�)锛堣捣寰佺偣鎸夌疮璁″垽鏂級锛汣绱� = calcCityTax(V绱�)
+     * 鏈瑪 V = V绱� 鈭� 危宸茬畻绋庡鍊肩◣锛涙湰绗� C 鍚岀悊锛涙湰绗� L = 鏈瑪 P 鈭� 鏈瑪 V 鈭� 鏈瑪 C
+     * 鏈瑪涓◣ = 涓◣(L绱) 鈭� 危宸茬畻绋庝釜绋�
+     * </pre>
      *
-     * @param taxMoneyVo
-     * @return
+     * @param taxMoneyVo 韬唤璇佸彿 + 鏈鍚◣閲戦
+     * @return 鏈鍔冲姟涓◣
      */
     @Override
     public Double getTaxByBeneFiciaryNo(TaxMoneyVO taxMoneyVo) {
         // 閲戦绱姞蹇呴』鐢� BigDecimal锛歞ouble 绱姞浼氬紩鍏ユ诞鐐瑰熬宸紝瀵艰嚧绠楀嚭鐨勪釜绋庡嚭鐜板垎浠ヤ笅璇樊
-        BigDecimal sum = BigDecimal.ZERO;
-        BigDecimal totalScore = BigDecimal.ZERO;
+        BigDecimal sumPretax = BigDecimal.ZERO;
+        BigDecimal sumVat = BigDecimal.ZERO;
+        BigDecimal sumCityTax = BigDecimal.ZERO;
+        BigDecimal sumLaborPretax = BigDecimal.ZERO;
+        BigDecimal sumTax = BigDecimal.ZERO;
         List<ServiceFunddetail> taxationByBeneFiciaryNo = serviceFunddetailMapper.getTaxBeforeByBeneFiciaryNo(taxMoneyVo);
         if (CollectionUtils.isNotEmpty(taxationByBeneFiciaryNo)) {
-            //璁$畻鐢宠閲戦鎬诲拰锛屼笉鍖呮嫭褰撳墠浼犺繘鏉ョ殑
-            sum = taxationByBeneFiciaryNo.stream()
-                    .map(ServiceFunddetail::getAmount)
-                    .filter(java.util.Objects::nonNull)
-                    .map(BigDecimal::valueOf)
-                    .reduce(BigDecimal.ZERO, BigDecimal::add);
-            //涓嶅姞鏈钖叕鐨勪釜绋�
-            totalScore = taxationByBeneFiciaryNo.stream()
-                    .map(ServiceFunddetail::getTaxamount)
-                    .filter(java.util.Objects::nonNull)
-                    .map(BigDecimal::valueOf)
-                    .reduce(BigDecimal.ZERO, BigDecimal::add);
+            for (ServiceFunddetail detail : taxationByBeneFiciaryNo) {
+                sumPretax = sumPretax.add(toMoney(detail.getAmount()));
+                sumVat = sumVat.add(toMoney(detail.getVatAmount()));
+                sumCityTax = sumCityTax.add(toMoney(detail.getCityTaxAmount()));
+                sumLaborPretax = sumLaborPretax.add(laborPretaxOf(detail));
+                sumTax = sumTax.add(toMoney(detail.getTaxamount()));
+            }
         }
 
-        sum = sum.add(new BigDecimal(taxMoneyVo.getMoney()));
-        //鍔犱笂鏈钖叕鐨勬�讳釜绋�
-        String taxation = TaxtUtils.getTaxation(sum);
+        // 鏈鍚◣閲戦 + 绱锛氭寜绱鍙e緞绠楀鍊肩◣/鍩庡缓绋庯紝鏈瑪鍙栧樊棰�
+        BigDecimal nowPretax = new BigDecimal(taxMoneyVo.getMoney());
+        BigDecimal cumPretax = sumPretax.add(nowPretax);
+        BigDecimal cumVat = VatTaxUtils.calcVat(cumPretax);
+        BigDecimal cumCityTax = VatTaxUtils.calcCityTax(cumVat);
+        BigDecimal nowVat = VatTaxUtils.roundMoney(cumVat.subtract(sumVat));
+        BigDecimal nowCityTax = VatTaxUtils.roundMoney(cumCityTax.subtract(sumCityTax));
+        if (nowVat.signum() < 0) {
+            nowVat = BigDecimal.ZERO;
+        }
+        if (nowCityTax.signum() < 0) {
+            nowCityTax = BigDecimal.ZERO;
+        }
+        BigDecimal nowLaborPretax = VatTaxUtils.roundMoney(nowPretax.subtract(nowVat).subtract(nowCityTax));
+        if (nowLaborPretax.signum() < 0) {
+            // 涓庣畻绋庝竴鑷达細鏈瑪涓嶆墸绋庛�佸樊棰濋『寤�
+            nowVat = BigDecimal.ZERO;
+            nowCityTax = BigDecimal.ZERO;
+            nowLaborPretax = nowPretax;
+        }
 
-        Double tax = new BigDecimal(taxation).subtract(totalScore).doubleValue();
-        return tax;
+        BigDecimal cumLaborPretax = sumLaborPretax.add(nowLaborPretax);
+        String taxation = TaxtUtils.getTaxation(cumLaborPretax);
+        return new BigDecimal(taxation).subtract(sumTax).doubleValue();
     }
 
     /**
-     * 鏍规嵁绋庡悗宸ヨ祫锛岀畻鍑虹◣鍓嶅伐璧�
+     * 鏄庣粏鐨勫姵鍔$◣鍓嶏細浼樺厛鍙栬惤搴撶殑 labor_pretax_amount锛屼负绌烘椂鍥為��鍒扮◣鍓嶉噾棰濓紙鍏煎鏈洖濉幆澧冿級
+     */
+    private static BigDecimal laborPretaxOf(ServiceFunddetail detail) {
+        if (detail.getLaborPretaxAmount() != null) {
+            return BigDecimal.valueOf(detail.getLaborPretaxAmount());
+        }
+        // 娌℃湁鍔冲姟绋庡墠鏃舵寜鈥滃惈绋庨噾棰濆仛浠风◣鍒嗙鈥濆厹搴曪紝淇濊瘉涓庣畻绋庡彛寰勪竴鑷�
+        return detail.getAmount() == null ? BigDecimal.ZERO
+                : VatTaxUtils.calcLaborPretax(BigDecimal.valueOf(detail.getAmount()));
+    }
+
+    /**
+     * 鏍规嵁绋庡悗宸ヨ祫锛岀畻鍑虹◣鍓嶅伐璧勶紙褰曞叆鏃剁殑鈥滅◣鍚� 鈫� 绋庡墠鈥濋瑙堬級
+     * <p>
+     * 2026-11-17锛堢疮璁″彛寰勶級锛氫笌绠楃◣淇濇寔涓�鑷达紝鍏堢敱鈥滅疮璁$◣鍚庘�濆弽绠楃疮璁″姵鍔$◣鍓� L绱紝
+     * 鍐嶈В绱鍚◣绋庡墠 P绱紙P绱� = L绱� + V绱� + C绱紝璧峰緛鐐规寜绱鍒ゆ柇锛夛紝鏈瑪鍧囧彇宸銆�
      *
-     * @param taxMoneyVo
-     * @return
+     * @param taxMoneyVo 韬唤璇佸彿 + 鏈鍔冲姟绋庡悗閲戦
+     * @return 绋庡墠/涓◣绛夐瑙堝��
      */
     @Override
     public TaxedMoneyVO getTaxBeforeByAfterMoney(TaxMoneyVO taxMoneyVo) {
         TaxedMoneyVO taxedMoneyVO = new TaxedMoneyVO();
         // 閲戦绱姞蹇呴』鐢� BigDecimal锛岄伩鍏嶆诞鐐瑰熬宸奖鍝嶇◣鍓�/绋庨缁撴灉
-        BigDecimal sum = BigDecimal.ZERO;
-        BigDecimal totalScore = BigDecimal.ZERO;
+        BigDecimal sumTaxed = BigDecimal.ZERO;
+        BigDecimal sumPretax = BigDecimal.ZERO;
+        BigDecimal sumVat = BigDecimal.ZERO;
+        BigDecimal sumCityTax = BigDecimal.ZERO;
+        BigDecimal sumLaborPretax = BigDecimal.ZERO;
+        BigDecimal sumTax = BigDecimal.ZERO;
         List<ServiceFunddetail> taxationByBeneFiciaryNo = serviceFunddetailMapper.getTaxBeforeByBeneFiciaryNo(taxMoneyVo);
         if (CollectionUtils.isNotEmpty(taxationByBeneFiciaryNo)) {
-            //璁$畻鍑虹◣鍚庢�婚噾棰濓紙涓嶅寘鍚湰娆★級
-            sum = taxationByBeneFiciaryNo.stream()
-                    .map(ServiceFunddetail::getTaxedamount)
-                    .filter(java.util.Objects::nonNull)
-                    .map(BigDecimal::valueOf)
-                    .reduce(BigDecimal.ZERO, BigDecimal::add);
-            //姹傚嚭涔嬪墠鐨勬�荤殑涓◣
-            totalScore = taxationByBeneFiciaryNo.stream()
-                    .map(ServiceFunddetail::getTaxamount)
-                    .filter(java.util.Objects::nonNull)
-                    .map(BigDecimal::valueOf)
-                    .reduce(BigDecimal.ZERO, BigDecimal::add);
+            for (ServiceFunddetail detail : taxationByBeneFiciaryNo) {
+                sumTaxed = sumTaxed.add(toMoney(detail.getTaxedamount()));
+                sumPretax = sumPretax.add(toMoney(detail.getAmount()));
+                sumVat = sumVat.add(toMoney(detail.getVatAmount()));
+                sumCityTax = sumCityTax.add(toMoney(detail.getCityTaxAmount()));
+                sumLaborPretax = sumLaborPretax.add(laborPretaxOf(detail));
+                sumTax = sumTax.add(toMoney(detail.getTaxamount()));
+            }
         }
-        //璁$畻鍑烘�荤殑绋庡悗钖叕锛堝寘鍚湰娆★級
-        sum = sum.add(new BigDecimal(taxMoneyVo.getMoney()));
-        //绋庡墠鎬婚噾棰�
-        String taxationBefore = TaxtUtils.getTaxationBefore(sum);
-        //鎬讳釜绋�
-        BigDecimal tax = new BigDecimal(taxationBefore).subtract(sum);
 
-        //鏈鐨勭◣鍓嶉噾棰�
-        String nowTaxMoney = TaxtUtils.getTaxationBefore(new BigDecimal(taxMoneyVo.getMoney()));
-        //鏈鐨勭◣璐�
-        BigDecimal subtract = new BigDecimal(nowTaxMoney).subtract(new BigDecimal(taxMoneyVo.getMoney()));
+        // 鍚湰娆$殑绱绋庡悗
+        BigDecimal nowTaxed = new BigDecimal(taxMoneyVo.getMoney());
+        BigDecimal cumTaxed = sumTaxed.add(nowTaxed);
+        // 绱鍔冲姟绋庡墠锛堢敱绱绋庡悗鍙嶇畻锛�
+        BigDecimal cumLaborPretax = new BigDecimal(TaxtUtils.getTaxationBefore(cumTaxed));
+        // 绱涓◣
+        BigDecimal cumTax = VatTaxUtils.roundMoney(cumLaborPretax.subtract(cumTaxed));
+        // 绱鍚◣绋庡墠閲戦锛堝唴閮ㄦ寜绱 P 鍒ゆ柇璧峰緛鐐癸級
+        VatTaxUtils.GrossAmount gross = VatTaxUtils.solveGross(cumLaborPretax);
 
-        taxedMoneyVO.setAllTax(tax.toString());
-        taxedMoneyVO.setAllTaxbeforeMoney(taxationBefore);
-        taxedMoneyVO.setAllAaxAfterMoney(sum.toString());
-        taxedMoneyVO.setNowTaxBeforeMoney(nowTaxMoney);
-        taxedMoneyVO.setNowTax(subtract.toString());
+        // 鏈瑪鍏ㄩ儴鍙栧樊棰濓紝涓� calculateTax 鐨勫垎鎽婂彛寰勪竴鑷�
+        BigDecimal nowPretax = VatTaxUtils.roundMoney(gross.getPretax().subtract(sumPretax));
+        BigDecimal nowLaborPretax = VatTaxUtils.roundMoney(cumLaborPretax.subtract(sumLaborPretax));
+        BigDecimal nowTax = VatTaxUtils.roundMoney(cumTax.subtract(sumTax));
+        if (nowTax.signum() < 0) {
+            nowTax = BigDecimal.ZERO;
+        }
+        if (nowPretax.signum() < 0) {
+            nowPretax = nowLaborPretax.add(gross.getVat().subtract(sumVat)).add(gross.getCityTax().subtract(sumCityTax));
+        }
+
+        taxedMoneyVO.setAllTax(cumTax.toString());
+        taxedMoneyVO.setAllTaxbeforeMoney(gross.getPretax().toString());
+        taxedMoneyVO.setAllAaxAfterMoney(cumTaxed.toString());
+        taxedMoneyVO.setNowTaxBeforeMoney(nowPretax.toString());
+        taxedMoneyVO.setNowTax(nowTax.toString());
 
         return taxedMoneyVO;
     }
@@ -375,7 +423,7 @@
                 detail.setLaborPretaxAmount(p.doubleValue());
                 detail.setTaxamount(0.0);
                 detail.setTaxedamount(p.doubleValue());
-                detail.setTaxVersion(VatTaxUtils.TAX_VERSION_V2);
+                detail.setTaxVersion(VatTaxUtils.TAX_VERSION_CURRENT);
                 detail.setTaxTime(taxTime);
                 updateList.add(detail);
 
@@ -416,7 +464,7 @@
             BigDecimal taxedAmount;
 
             if (afterTax) {
-                // 5.2.2 绋庡悗褰曞叆锛氬厛鍙嶇畻鍔冲姟绋庡墠锛屽啀寰幆鍙嶈В绋庡墠/澧炲�肩◣/鍩庡缓绋�
+                // 5.2.2 绋庡悗褰曞叆锛氬厛鐢扁�滅疮璁$◣鍚庘�濆弽绠楃疮璁″姵鍔$◣鍓嶏紝鍐嶅弽瑙g疮璁″惈绋庣◣鍓嶏紝鏈�鍚庢寜宸寰楀嚭鏈瑪 V/C/P
                 taxedAmount = toMoney(detail.getTaxedamount());
                 BigDecimal sumTaxed = prevTaxed.add(taxedAmount);
                 BigDecimal sumLaborPretax = new BigDecimal(TaxtUtils.getTaxationBefore(sumTaxed));
@@ -434,17 +482,47 @@
                 taxAmount = VatTaxUtils.roundMoney(laborPretax.subtract(taxedAmount));
                 if (taxAmount.signum() < 0) {
                     taxAmount = BigDecimal.ZERO;
+                    taxedAmount = laborPretax;
                 }
-                VatTaxUtils.GrossAmount gross = VatTaxUtils.solveGross(laborPretax);
-                pretax = gross.getPretax();
-                vat = gross.getVat();
-                cityTax = gross.getCityTax();
+                // 绱鍙e緞锛歅绱� = L绱� + V绱� + C绱紙V绱� 鐨勮捣寰佺偣鎸夌疮璁$◣鍓嶅垽鏂級锛屾湰绗斿彇宸
+                VatTaxUtils.GrossAmount gross = VatTaxUtils.solveGross(sumLaborPretax);
+                pretax = VatTaxUtils.roundMoney(gross.getPretax().subtract(prevAmount));
+                vat = VatTaxUtils.roundMoney(gross.getVat().subtract(prevVat));
+                cityTax = VatTaxUtils.roundMoney(gross.getCityTax().subtract(prevCityTax));
+                if (pretax.signum() < 0) {
+                    pretax = laborPretax;
+                }
+                if (vat.signum() < 0) {
+                    vat = BigDecimal.ZERO;
+                }
+                if (cityTax.signum() < 0) {
+                    cityTax = BigDecimal.ZERO;
+                }
             } else {
-                // 5.2.3 绋庡墠褰曞叆锛氬厛绠楀鍊肩◣/鍩庡缓绋�/鍔冲姟绋庡墠锛屽啀鎸夌疮璁″姵鍔$◣鍓嶇畻鍔冲姟涓◣
+                // 5.2.3 绋庡墠褰曞叆锛氬厛鎸夌疮璁$◣鍓嶇畻绱澧炲�肩◣/鍩庡缓绋庯紝鍐嶆寜宸寰楁湰绗旓紝鏈�鍚庢寜绱鍔冲姟绋庡墠绠椾釜绋�
                 pretax = toMoney(detail.getAmount());
-                vat = VatTaxUtils.calcVat(pretax);
-                cityTax = VatTaxUtils.calcCityTax(vat);
+                BigDecimal cumPretax = prevAmount.add(pretax);
+                BigDecimal cumVat = VatTaxUtils.calcVat(cumPretax);
+                BigDecimal cumCityTax = VatTaxUtils.calcCityTax(cumVat);
+                vat = VatTaxUtils.roundMoney(cumVat.subtract(prevVat));
+                cityTax = VatTaxUtils.roundMoney(cumCityTax.subtract(prevCityTax));
+                if (vat.signum() < 0) {
+                    vat = BigDecimal.ZERO;
+                }
+                if (cityTax.signum() < 0) {
+                    cityTax = BigDecimal.ZERO;
+                }
                 laborPretax = VatTaxUtils.roundMoney(pretax.subtract(vat).subtract(cityTax));
+                if (laborPretax.signum() < 0) {
+                    // 鏋佺杈圭晫锛氱疮璁″垰璺ㄨ繃璧峰緛鐐广�佹湰绗旈噾棰濆皬浜庤烦鍙橀噺銆�
+                    // 鏈瑪涓嶆墸绋庛�佸樊棰濋『寤跺埌鍚庣画绗旓紙绱鍙帹杩涘疄闄呭垎鎽婂�硷紝涓嬩竴绗斿樊鍒嗘椂鑷姩琛ュ洖锛�
+                    log.warn("绱璺ㄨ捣寰佺偣瀵艰嚧鏈瑪鍔冲姟绋庡墠涓鸿礋锛屾湰绗斾笉鎵g◣銆佸樊棰濋『寤躲�備汉鍛橈細{}锛屾湰绗旂◣鍓�={}锛屾湰绗斿鍊肩◣={}锛�"
+                                    + "鏈瑪鍩庡缓绋�={}锛岀疮璁$◣鍓�={}",
+                            safeName(detail), pretax, vat, cityTax, cumPretax);
+                    vat = BigDecimal.ZERO;
+                    cityTax = BigDecimal.ZERO;
+                    laborPretax = pretax;
+                }
                 BigDecimal sumLaborPretax = prevLaborPretax.add(laborPretax);
                 BigDecimal sumTax = new BigDecimal(TaxtUtils.getTaxation(sumLaborPretax));
                 taxAmount = VatTaxUtils.roundMoney(sumTax.subtract(prevTax));
@@ -460,7 +538,7 @@
             detail.setLaborPretaxAmount(laborPretax.doubleValue());
             detail.setTaxamount(taxAmount.doubleValue());
             detail.setTaxedamount(taxedAmount.doubleValue());
-            detail.setTaxVersion(VatTaxUtils.TAX_VERSION_V2);
+            detail.setTaxVersion(VatTaxUtils.TAX_VERSION_CURRENT);
             detail.setTaxTime(taxTime);
             updateList.add(detail);
 
@@ -494,7 +572,7 @@
         serviceFund.setLaborPretaxCost(laborPretaxTotal.doubleValue());
         serviceFund.setTaxcost(taxTotal.doubleValue());
         serviceFund.setTaxedcost(taxedTotal.doubleValue());
-        serviceFund.setTaxVersion(VatTaxUtils.TAX_VERSION_V2);
+        serviceFund.setTaxVersion(VatTaxUtils.TAX_VERSION_CURRENT);
         serviceFund.setFundtaxtime(taxTime);
         serviceFund.setIstax(1);
         serviceFundMapper.updateById(serviceFund);

--
Gitblit v1.9.3