From ee089a8ac4a9d99ef0a0491bbf1d80a2d4fbc393 Mon Sep 17 00:00:00 2001
From: sinake <sinake1@qq.com>
Date: 星期二, 10 十一月 2026 09:15:54 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 ruoyi-project/src/main/java/com/ruoyi/project/service/impl/ServiceFunddetailServiceImpl.java |  425 +++++++++++++++++++++++++++++++++++------------------
 1 files changed, 281 insertions(+), 144 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 079fc2e..6bf6c64 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
@@ -1,6 +1,7 @@
 package com.ruoyi.project.service.impl;
 
 import java.math.BigDecimal;
+import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.time.LocalDate;
 import java.time.ZoneId;
@@ -12,10 +13,12 @@
 import com.ruoyi.common.exception.base.BaseException;
 import com.ruoyi.common.tax.PerformanceTaxtUtils;
 import com.ruoyi.common.tax.TaxtUtils;
+import com.ruoyi.common.tax.VatTaxUtils;
 import com.ruoyi.common.utils.bean.DtoConversionUtils;
 import com.ruoyi.project.domain.*;
 import com.ruoyi.project.domain.vo.*;
 import com.ruoyi.project.mapper.ServiceFundMapper;
+import com.ruoyi.project.mapper.SpStatBonusMapper;
 import lombok.extern.flogger.Flogger;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
@@ -46,6 +49,9 @@
 
     @Autowired
     ServiceFundMapper serviceFundMapper;
+
+    @Autowired
+    SpStatBonusMapper spStatBonusMapper;
 
 
     /**
@@ -99,6 +105,12 @@
         if (serviceFunddetail.getDel_flag() != null) {
             wrappers.eq(ServiceFunddetail::getDel_flag, serviceFunddetail.getDel_flag());
         }
+        if (serviceFunddetail.getDeptId() != null) {
+            wrappers.eq(ServiceFunddetail::getDeptId, serviceFunddetail.getDeptId());
+        }
+        if (serviceFunddetail.getDeptName() != null) {
+            wrappers.eq(ServiceFunddetail::getDeptName, serviceFunddetail.getDeptName());
+        }
         return this.list(wrappers);
     }
 
@@ -125,21 +137,30 @@
      */
     @Override
     public Double getTaxByBeneFiciaryNo(TaxMoneyVO taxMoneyVo) {
-        Double sum = 0.00;
-        Double totalScore = 0.00;
+        // 閲戦绱姞蹇呴』鐢� BigDecimal锛歞ouble 绱姞浼氬紩鍏ユ诞鐐瑰熬宸紝瀵艰嚧绠楀嚭鐨勪釜绋庡嚭鐜板垎浠ヤ笅璇樊
+        BigDecimal sum = BigDecimal.ZERO;
+        BigDecimal totalScore = BigDecimal.ZERO;
         List<ServiceFunddetail> taxationByBeneFiciaryNo = serviceFunddetailMapper.getTaxBeforeByBeneFiciaryNo(taxMoneyVo);
         if (CollectionUtils.isNotEmpty(taxationByBeneFiciaryNo)) {
             //璁$畻鐢宠閲戦鎬诲拰锛屼笉鍖呮嫭褰撳墠浼犺繘鏉ョ殑
-            sum = taxationByBeneFiciaryNo.stream().mapToDouble(ServiceFunddetail::getAmount).sum();
+            sum = taxationByBeneFiciaryNo.stream()
+                    .map(ServiceFunddetail::getAmount)
+                    .filter(java.util.Objects::nonNull)
+                    .map(BigDecimal::valueOf)
+                    .reduce(BigDecimal.ZERO, BigDecimal::add);
             //涓嶅姞鏈钖叕鐨勪釜绋�
-            totalScore = taxationByBeneFiciaryNo.stream().mapToDouble(ServiceFunddetail::getTaxamount).sum();
+            totalScore = taxationByBeneFiciaryNo.stream()
+                    .map(ServiceFunddetail::getTaxamount)
+                    .filter(java.util.Objects::nonNull)
+                    .map(BigDecimal::valueOf)
+                    .reduce(BigDecimal.ZERO, BigDecimal::add);
         }
 
-        sum = sum + new Double(taxMoneyVo.getMoney());
+        sum = sum.add(new BigDecimal(taxMoneyVo.getMoney()));
         //鍔犱笂鏈钖叕鐨勬�讳釜绋�
-        String taxation = TaxtUtils.getTaxation(new BigDecimal(sum));
+        String taxation = TaxtUtils.getTaxation(sum);
 
-        Double tax = Double.valueOf(taxation) - totalScore;
+        Double tax = new BigDecimal(taxation).subtract(totalScore).doubleValue();
         return tax;
     }
 
@@ -152,21 +173,30 @@
     @Override
     public TaxedMoneyVO getTaxBeforeByAfterMoney(TaxMoneyVO taxMoneyVo) {
         TaxedMoneyVO taxedMoneyVO = new TaxedMoneyVO();
-        Double sum = 0.00;
-        Double totalScore = 0.00;
+        // 閲戦绱姞蹇呴』鐢� BigDecimal锛岄伩鍏嶆诞鐐瑰熬宸奖鍝嶇◣鍓�/绋庨缁撴灉
+        BigDecimal sum = BigDecimal.ZERO;
+        BigDecimal totalScore = BigDecimal.ZERO;
         List<ServiceFunddetail> taxationByBeneFiciaryNo = serviceFunddetailMapper.getTaxBeforeByBeneFiciaryNo(taxMoneyVo);
         if (CollectionUtils.isNotEmpty(taxationByBeneFiciaryNo)) {
             //璁$畻鍑虹◣鍚庢�婚噾棰濓紙涓嶅寘鍚湰娆★級
-            sum = taxationByBeneFiciaryNo.stream().mapToDouble(ServiceFunddetail::getTaxedamount).sum();
+            sum = taxationByBeneFiciaryNo.stream()
+                    .map(ServiceFunddetail::getTaxedamount)
+                    .filter(java.util.Objects::nonNull)
+                    .map(BigDecimal::valueOf)
+                    .reduce(BigDecimal.ZERO, BigDecimal::add);
             //姹傚嚭涔嬪墠鐨勬�荤殑涓◣
-            totalScore = taxationByBeneFiciaryNo.stream().mapToDouble(ServiceFunddetail::getTaxamount).sum();
+            totalScore = taxationByBeneFiciaryNo.stream()
+                    .map(ServiceFunddetail::getTaxamount)
+                    .filter(java.util.Objects::nonNull)
+                    .map(BigDecimal::valueOf)
+                    .reduce(BigDecimal.ZERO, BigDecimal::add);
         }
         //璁$畻鍑烘�荤殑绋庡悗钖叕锛堝寘鍚湰娆★級
-        sum = sum + Double.valueOf(taxMoneyVo.getMoney());
+        sum = sum.add(new BigDecimal(taxMoneyVo.getMoney()));
         //绋庡墠鎬婚噾棰�
-        String taxationBefore = TaxtUtils.getTaxationBefore(new BigDecimal(sum));
+        String taxationBefore = TaxtUtils.getTaxationBefore(sum);
         //鎬讳釜绋�
-        BigDecimal tax = new BigDecimal(taxationBefore).subtract(new BigDecimal(sum));
+        BigDecimal tax = new BigDecimal(taxationBefore).subtract(sum);
 
         //鏈鐨勭◣鍓嶉噾棰�
         String nowTaxMoney = TaxtUtils.getTaxationBefore(new BigDecimal(taxMoneyVo.getMoney()));
@@ -224,15 +254,22 @@
         Long fundid = serviceFunddetail.getFundid();
         ServiceFund serviceFund = serviceFundMapper.selectById(fundid);
         if (ObjectUtils.isNotEmpty(serviceFund)) {
-            Double pretax = serviceFund.getPretaxcost();
-            Double aftertax = serviceFund.getTaxedcost();
-            pretax = pretax - serviceFunddetail.getAmount();
-            aftertax = aftertax - serviceFunddetail.getTaxedamount();
-            Double amount = pretax + aftertax;
+            // 閲戦涓�寰嬬敤 BigDecimal 绱姞/鎵e噺锛岄伩鍏嶆诞鐐瑰熬宸�
+            BigDecimal pretax = toMoney(serviceFund.getPretaxcost()).subtract(toMoney(serviceFunddetail.getAmount()));
+            BigDecimal aftertax = toMoney(serviceFund.getTaxedcost()).subtract(toMoney(serviceFunddetail.getTaxedamount()));
+            BigDecimal vat = toMoney(serviceFund.getVatCost()).subtract(toMoney(serviceFunddetail.getVatAmount()));
+            BigDecimal cityTax = toMoney(serviceFund.getCityTaxCost()).subtract(toMoney(serviceFunddetail.getCityTaxAmount()));
+            BigDecimal laborPretax = toMoney(serviceFund.getLaborPretaxCost()).subtract(toMoney(serviceFunddetail.getLaborPretaxAmount()));
+            BigDecimal tax = toMoney(serviceFund.getTaxcost()).subtract(toMoney(serviceFunddetail.getTaxamount()));
+            BigDecimal amount = pretax.add(aftertax);
 
-            serviceFund.setPretaxcost(pretax);
-            serviceFund.setTaxedcost(aftertax);
-            serviceFund.setAmountrequested(amount);
+            serviceFund.setPretaxcost(pretax.doubleValue());
+            serviceFund.setTaxedcost(aftertax.doubleValue());
+            serviceFund.setVatCost(vat.doubleValue());
+            serviceFund.setCityTaxCost(cityTax.doubleValue());
+            serviceFund.setLaborPretaxCost(laborPretax.doubleValue());
+            serviceFund.setTaxcost(tax.doubleValue());
+            serviceFund.setAmountrequested(amount.doubleValue());
             int updateAmount = serviceFundMapper.updateById(serviceFund);
         }
         return serviceFunddetailMapper.deleteById(id);
@@ -256,157 +293,248 @@
     }
 
     /**
-     * 鏍规嵁鏉′欢绠楃◣
+     * 鎵归噺绠楃◣涓绘柟娉曪紙鍔冲姟涓◣ + 澧炲�肩◣ + 鍩庡缓绋庯紝v2 鍙e緞锛�
+     * <p>
+     * 閲戦妯″瀷锛�
+     * <pre>
+     * 绋庡墠褰曞叆锛歏 = ceil(P 脳 1%)銆丆 = ceil(V 脳 3.5%)銆丩 = P - V - C銆�
+     *          T = 鍔冲姟涓◣(褰撴湀绱 L) - 鍘熺疮璁� T銆丯 = L - T
+     * 绋庡悗褰曞叆锛歂绱 = 鍘熺疮璁� N + N銆丩绱 = getTaxationBefore(N绱)銆�
+     *          L = L绱 - 鍘熺疮璁� L銆乀 = L - N銆丳/V/C = solveGross(L)
+     * 鏃犺韩浠借瘉锛歏 = C = T = 0锛孡 = N = P
+     * </pre>
      *
-     * @param serviceFunddetails
+     * @param serviceFunddetails 璐圭敤鏄庣粏鍒楄〃
+     * @param tempTaxSumMap      鏈湀绱閲戦缂撳瓨锛堟寜韬唤璇佸彿锛�
+     * @return 鏄惁绠楃◣鎴愬姛
      */
     @Override
-    @Transactional(rollbackFor = Exception.class)
-    public Boolean calculateTax(List<ServiceFunddetail> serviceFunddetails) {
+    public Boolean calculateTax(List<ServiceFunddetail> serviceFunddetails, Map<String, TaxMoneySumEO> tempTaxSumMap) {
+        return calculateTax(serviceFunddetails, tempTaxSumMap, new Date());
+    }
+
+    /**
+     * 鎵归噺绠楃◣锛堟寚瀹氭湰鎵圭畻绋庢椂闂达紝淇濊瘉鍚屾壒鏄庣粏 tax_time 涓�鑷达級
+     *
+     * @param serviceFunddetails 璐圭敤鏄庣粏鍒楄〃
+     * @param tempTaxSumMap      鏈湀绱閲戦缂撳瓨锛堟寜韬唤璇佸彿锛�
+     * @param taxTime            鏈绠楃◣鏃堕棿
+     * @return 鏄惁绠楃◣鎴愬姛
+     */
+    @Override
+    public Boolean calculateTax(List<ServiceFunddetail> serviceFunddetails, Map<String, TaxMoneySumEO> tempTaxSumMap, Date taxTime) {
+        // 1. 鏍¢獙鍙傛暟
         if (CollectionUtils.isEmpty(serviceFunddetails)) {
             throw new BaseException("绠楃◣鏉′欢涓虹┖锛岃妫�鏌ュ悗锛屽啀杩涜璁$畻");
         }
-        //杩欎釜鏄负浜嗚幏鍙杅und_tax_time
+        // 2. 鎺掑簭锛氱◣鏃堕棿涓虹┖鐨勬帓鍚庨潰锛屽啀鎸� id 淇濊瘉鍚屾壒椤哄簭绋冲畾
+        serviceFunddetails.sort(Comparator
+                .comparing((ServiceFunddetail obj) -> ObjectUtils.isEmpty(obj.getTaxTime()) ? 1 : 0)
+                .thenComparing(obj -> obj.getId() == null ? Long.MAX_VALUE : obj.getId()));
+
+        // 3. 鑾峰彇鏈壒娆� fundid 鍙婁富琛ㄤ俊鎭�
         Long fundid = serviceFunddetails.get(0).getFundid();
         log.info("绠楃◣鐨刦undid鐨勫�间负锛歿}", fundid);
         ServiceFund serviceFund1 = serviceFundMapper.selectById(fundid);
-        log.info("fund琛ㄧ殑fundtextime鍊间负锛歿}", serviceFund1.getFundtaxtime());
+        if (serviceFund1 == null) {
+            throw new BaseException("鏈壘鍒拌垂鐢ㄧ敵璇峰崟锛孖D=" + fundid);
+        }
+        // 鏄庣粏 tax_time 缁熶竴浣跨敤鏈绠楃◣鏃堕棿
+        if (taxTime == null) {
+            taxTime = new Date();
+        }
 
-        //鐢ㄤ簬涓存椂淇濆瓨宸茬粡绠楀ソ鐨�"璐圭敤璇︽儏鏁版嵁"
-        List<ServiceFunddetail> temporarySave = new ArrayList<>();
+        // 4. 鏈壒娆″悎璁★紙鍏ㄩ儴鐢� BigDecimal锛岀姝� double 绱姞锛�
+        BigDecimal pretaxTotal = BigDecimal.ZERO;
+        BigDecimal vatTotal = BigDecimal.ZERO;
+        BigDecimal cityTaxTotal = BigDecimal.ZERO;
+        BigDecimal laborPretaxTotal = BigDecimal.ZERO;
+        BigDecimal taxTotal = BigDecimal.ZERO;
+        BigDecimal taxedTotal = BigDecimal.ZERO;
 
-        //淇濆瓨鎬荤◣鍓嶉噾棰濓紙鐢ㄤ簬鏇存柊fund琛ㄩ噷鐨勶級
-        BigDecimal pretaxcost = BigDecimal.valueOf(0.00);
-        for (ServiceFunddetail serviceFunddetail : serviceFunddetails) {
-            if (StringUtils.isEmpty(serviceFunddetail.getIdcardno())) {
-                logger.info("serviceFunddetail鏁版嵁鍏ュ弬 锛� {}", serviceFunddetail);
-                //濡傛灉韬唤璇佸彿涓虹┖锛屽氨涓嶇敤鏌ヤ簡锛屽彲鑳芥槸鍖婚櫌鐨勮褰�
-                if (StringUtils.isEmpty(serviceFunddetail.getServicesscopename()) || !serviceFunddetail.getServicesscopename().contains("绋庡悗")) {
-                    serviceFunddetail.setTaxedamount(serviceFunddetail.getAmount());
-                } else {
-                    serviceFunddetail.setAmount(serviceFunddetail.getTaxedamount());
-                }
-                serviceFunddetail.setTaxamount(0.0);
-                logger.info("serviceFunddetail鏇存柊鍚庣殑鏁版嵁 锛� {}", serviceFunddetail);
-                boolean b = this.updateById(serviceFunddetail);
-                logger.info("serviceFunddetail淇敼缁撴灉 锛� {}", b);
+        List<ServiceFunddetail> updateList = new ArrayList<>();
+
+        // 5. 閬嶅巻姣忔潯鏄庣粏
+        for (ServiceFunddetail detail : serviceFunddetails) {
+            if (detail.getAmount() == null) {
+                detail.setAmount(0.0);
+            }
+            if (detail.getTaxedamount() == null) {
+                detail.setTaxedamount(0.0);
+            }
+            // 鏈嶅姟璇存槑涓虹┖鎸夆�滈潪绋庡悗鈥濆鐞�
+            String scopeName = detail.getServicesscopename() == null ? "" : detail.getServicesscopename().trim();
+            boolean afterTax = scopeName.contains("绋庡悗");
+
+            // 5.1 韬唤璇佸彿涓虹┖锛氫笉鎵e鍊肩◣/鍩庡缓绋�/鍔冲姟涓◣
+            if (StringUtils.isEmpty(detail.getIdcardno())) {
+                BigDecimal p = afterTax ? toMoney(detail.getTaxedamount()) : toMoney(detail.getAmount());
+                detail.setAmount(p.doubleValue());
+                detail.setVatAmount(0.0);
+                detail.setCityTaxAmount(0.0);
+                detail.setLaborPretaxAmount(p.doubleValue());
+                detail.setTaxamount(0.0);
+                detail.setTaxedamount(p.doubleValue());
+                detail.setTaxVersion(VatTaxUtils.TAX_VERSION_V2);
+                detail.setTaxTime(taxTime);
+                updateList.add(detail);
+
+                pretaxTotal = pretaxTotal.add(p);
+                laborPretaxTotal = laborPretaxTotal.add(p);
+                taxedTotal = taxedTotal.add(p);
                 continue;
             }
 
-            //鑾峰彇褰撴湀鐨勭涓�澶�
-            Date firstDay = Date.from(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay(ZoneId.systemDefault()).toInstant());
-//            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-//            String firstDatStr = dateFormat.format(firstDay);
-            TaxMoneyByItemEO taxMoneyVO = new TaxMoneyByItemEO();
-            if (serviceFund1.getFundtaxtime() != null) {
-                taxMoneyVO.setTaxTime(serviceFund1.getFundtaxtime());
-            } else {
-                taxMoneyVO.setTaxTime(new Date());
-            }
-            taxMoneyVO.setFirstDay(firstDay);
-            taxMoneyVO.setFundID(serviceFunddetail.getFundid());
-            taxMoneyVO.setIDCard(serviceFunddetail.getIdcardno());
-            //鏌ュ嚭绋庡墠銆佺◣銆佺◣鍚庣殑鎬婚锛堜笉鍖呭惈鏈锛�
-            TaxMoneySumEO taxSum = serviceFunddetailMapper.getTaxSum(taxMoneyVO);
-            logger.info("鏌ヨ鐨則axSum鐨勫�� 锛� {}", taxSum);
-            if (ObjectUtils.isEmpty(taxSum)) {
+            // 5.2 韬唤璇佸彿涓嶄负绌猴細鏈湀绱 + 鍒嗘。绠楃◣
+            String idcard = detail.getIdcardno().trim();
+
+            // 5.2.1 鍙栨湰鏈堢疮璁★紙缂撳瓨閬垮厤閲嶅鏌ュ簱锛�
+            TaxMoneySumEO taxSum = tempTaxSumMap.get(idcard);
+            if (taxSum == null) {
                 taxSum = new TaxMoneySumEO();
+                taxSum.setXh(0L);
+                tempTaxSumMap.put(idcard, taxSum);
             }
+            long xh = (taxSum.getXh() == null ? 0L : taxSum.getXh()) + 1;
+            taxSum.setXh(xh);
+            detail.setXh(xh);
+            // 绱绋庡墠閲戦锛堜笉鍚湰绗旓級锛岀敤浜庨〉闈㈠睍绀�
+            detail.setAllAmount(taxSum.getAmounts());
 
-            //鍒ゆ柇涓存椂闆嗗悎temporarySave涓紝鏈夋病鏈夊凡缁忕畻杩囩殑鏁版嵁
-            if (CollectionUtils.isNotEmpty(temporarySave)) {
-                for (ServiceFunddetail serviceFunddetail1 : temporarySave) {
-                    //濡傛灉鏈鐨勨�濅汉鍛樼被鍨嬧�溿�佲�濊韩浠借瘉鍙封�滃湪涓存椂闆嗗悎涓瓨鍦紝鍒欏皢闆嗗悎鏁版嵁涓殑璇ュ彈鐩婁汉鐨勶紝绋庡墠銆佺◣閲戙�佺◣鍚庯紝鍜屾�荤殑鐩稿姞锛屽苟灏嗚�佺殑瑕嗙洊
-                    // if (serviceFunddetail1.getApplytype().equals(serviceFunddetail.getApplytype()) && serviceFunddetail1.getBeneficiaryno().equals(serviceFunddetail.getBeneficiaryno())) {
-                    if (serviceFunddetail1.getIdcardno().trim().equals(serviceFunddetail.getIdcardno().trim())) {
-                        //灏嗛泦鍚堟暟鎹腑鐨勮鍙楃泭浜虹殑锛岀◣鍓嶃�佺◣閲戙�佺◣鍚庯紝鍜屾�荤殑鐩稿姞
-                        BigDecimal amounts = BigDecimal.valueOf(taxSum.getAmounts()).add(new BigDecimal(String.valueOf(serviceFunddetail1.getAmount())));
-                        BigDecimal taxAmounts = BigDecimal.valueOf(taxSum.getTaxAmounts()).add(new BigDecimal(String.valueOf(serviceFunddetail1.getTaxamount())));
-                        BigDecimal TaxedAmounts = BigDecimal.valueOf(taxSum.getTaxedAmounts()).add(new BigDecimal(String.valueOf(serviceFunddetail1.getTaxedamount())));
-                        //灏嗚�佺殑瑕嗙洊
-                        taxSum.setAmounts(amounts.doubleValue());
-                        taxSum.setTaxAmounts(taxAmounts.doubleValue());
-                        taxSum.setTaxedAmounts(TaxedAmounts.doubleValue());
+            BigDecimal prevAmount = toMoney(taxSum.getAmounts());
+            BigDecimal prevLaborPretax = toMoney(taxSum.getLaborPretaxAmounts());
+            BigDecimal prevTax = toMoney(taxSum.getTaxAmounts());
+            BigDecimal prevTaxed = toMoney(taxSum.getTaxedAmounts());
+            BigDecimal prevVat = toMoney(taxSum.getVatAmounts());
+            BigDecimal prevCityTax = toMoney(taxSum.getCityTaxAmounts());
+
+            BigDecimal pretax;
+            BigDecimal vat;
+            BigDecimal cityTax;
+            BigDecimal laborPretax;
+            BigDecimal taxAmount;
+            BigDecimal taxedAmount;
+
+            if (afterTax) {
+                // 5.2.2 绋庡悗褰曞叆锛氬厛鍙嶇畻鍔冲姟绋庡墠锛屽啀寰幆鍙嶈В绋庡墠/澧炲�肩◣/鍩庡缓绋�
+                taxedAmount = toMoney(detail.getTaxedamount());
+                BigDecimal sumTaxed = prevTaxed.add(taxedAmount);
+                BigDecimal sumLaborPretax = new BigDecimal(TaxtUtils.getTaxationBefore(sumTaxed));
+                laborPretax = VatTaxUtils.roundMoney(sumLaborPretax.subtract(prevLaborPretax));
+                if (laborPretax.signum() < 0) {
+                    // 閫愮瑪鍚戜笂鍙栨暣鍙兘甯︽潵鍑犲垎閽辩殑鍙嶇畻璇樊锛屽宸唴鎸� 0 澶勭悊锛涜秴鍑哄宸鏄庢暟鎹紓甯�
+                    if (laborPretax.abs().compareTo(new BigDecimal("0.05")) <= 0) {
+                        log.warn("绋庡悗閲戦鍙嶇畻鍔冲姟绋庡墠鍑虹幇璐熷熬宸紝宸叉寜 0 澶勭悊銆備汉鍛橈細{}锛屽弽绠楀姵鍔$◣鍓�={}锛屽師绱鍔冲姟绋庡墠={}",
+                                safeName(detail), sumLaborPretax, prevLaborPretax);
+                        laborPretax = BigDecimal.ZERO;
+                    } else {
+                        throw new BaseException("绋庡悗閲戦鍙嶇畻鍔冲姟绋庡墠澶辫触锛屼汉鍛橈細" + safeName(detail));
                     }
-                    continue;
                 }
+                taxAmount = VatTaxUtils.roundMoney(laborPretax.subtract(taxedAmount));
+                if (taxAmount.signum() < 0) {
+                    taxAmount = BigDecimal.ZERO;
+                }
+                VatTaxUtils.GrossAmount gross = VatTaxUtils.solveGross(laborPretax);
+                pretax = gross.getPretax();
+                vat = gross.getVat();
+                cityTax = gross.getCityTax();
+            } else {
+                // 5.2.3 绋庡墠褰曞叆锛氬厛绠楀鍊肩◣/鍩庡缓绋�/鍔冲姟绋庡墠锛屽啀鎸夌疮璁″姵鍔$◣鍓嶇畻鍔冲姟涓◣
+                pretax = toMoney(detail.getAmount());
+                vat = VatTaxUtils.calcVat(pretax);
+                cityTax = VatTaxUtils.calcCityTax(vat);
+                laborPretax = VatTaxUtils.roundMoney(pretax.subtract(vat).subtract(cityTax));
+                BigDecimal sumLaborPretax = prevLaborPretax.add(laborPretax);
+                BigDecimal sumTax = new BigDecimal(TaxtUtils.getTaxation(sumLaborPretax));
+                taxAmount = VatTaxUtils.roundMoney(sumTax.subtract(prevTax));
+                if (taxAmount.signum() < 0) {
+                    taxAmount = BigDecimal.ZERO;
+                }
+                taxedAmount = VatTaxUtils.roundMoney(laborPretax.subtract(taxAmount));
             }
 
-            if (StringUtils.isEmpty(serviceFunddetail.getServicesscopename()) || !serviceFunddetail.getServicesscopename().contains("绋庡悗")) {
-                logger.info("璁$畻绋庡墠淇℃伅寮�濮嬶細{}", serviceFunddetail);
-                logger.info("taxSum鍏ュ弬鏁版嵁锛歿}", taxSum);
-                // 濡備綍绋庡墠閲戦涓嶄负绌猴紝鐢ㄤ箣鍓嶇殑鈥濇�荤◣鍓嶉噾棰濃�滐紝鍔犱笂褰撳墠鈥濈◣鍓嶉噾棰濃��
-                BigDecimal newAmounts = BigDecimal.valueOf(taxSum.getAmounts()).add(BigDecimal.valueOf(serviceFunddetail.getAmount()));
+            detail.setAmount(pretax.doubleValue());
+            detail.setVatAmount(vat.doubleValue());
+            detail.setCityTaxAmount(cityTax.doubleValue());
+            detail.setLaborPretaxAmount(laborPretax.doubleValue());
+            detail.setTaxamount(taxAmount.doubleValue());
+            detail.setTaxedamount(taxedAmount.doubleValue());
+            detail.setTaxVersion(VatTaxUtils.TAX_VERSION_V2);
+            detail.setTaxTime(taxTime);
+            updateList.add(detail);
 
-                // 璁$畻鎬荤◣閲戯紙鍖呭惈鏈锛�
-                String taxation = TaxtUtils.getTaxation(newAmounts);
-                //璁$畻鏈绋庨噾锛堟�荤◣閲戯紙鍖呭惈鏈锛� - 鎬荤◣閲戯紙涓嶅寘鍚湰娆★級锛�
-                BigDecimal taxAmountNow = new BigDecimal(taxation).subtract(new BigDecimal(String.valueOf(taxSum.getTaxAmounts())));
+            // 5.2.4 鏇存柊鏈湀绱
+            taxSum.setAmounts(prevAmount.add(pretax).doubleValue());
+            taxSum.setVatAmounts(prevVat.add(vat).doubleValue());
+            taxSum.setCityTaxAmounts(prevCityTax.add(cityTax).doubleValue());
+            taxSum.setLaborPretaxAmounts(prevLaborPretax.add(laborPretax).doubleValue());
+            taxSum.setTaxAmounts(prevTax.add(taxAmount).doubleValue());
+            taxSum.setTaxedAmounts(prevTaxed.add(taxedAmount).doubleValue());
 
-                //璁$畻鏈鐨勭◣鍚庨噾棰�(鏈鐨勭◣鍓嶉噾棰� - 绋庨噾)
-                BigDecimal texdAfterNow = BigDecimal.valueOf(serviceFunddetail.getAmount()).subtract(taxAmountNow);
-                logger.info("鎵撳嵃newAmounts锛歿},taxation:{},taxAmountNow:{},texdAfterNow:{}", newAmounts, taxation, taxAmountNow, texdAfterNow);
-
-                //灏嗗惊鐜殑鏁版嵁閲嶆柊鏀惧埌涓�涓柊瀵硅薄涓�
-                ServiceFunddetail serviceFunddetail3 = DtoConversionUtils.sourceToTarget(serviceFunddetail, ServiceFunddetail.class);
-                serviceFunddetail3.setTaxamount(taxAmountNow.doubleValue());
-                serviceFunddetail3.setTaxedamount(texdAfterNow.doubleValue());
-                if (ObjectUtils.isEmpty(serviceFund1.getFundtaxtime())) {
-                    serviceFunddetail3.setTaxTime(new Date());
-                } else {
-                    serviceFunddetail3.setTaxTime(serviceFund1.getFundtaxtime());
-                }
-                logger.info("鎵撳嵃绋庡墠serviceFunddetail3鏁版嵁 :{}", serviceFunddetail3);
-
-                // 灏嗚鏉℃暟鎹洿鏂�
-                boolean b = this.updateById(serviceFunddetail3);
-                pretaxcost = pretaxcost.add(BigDecimal.valueOf(serviceFunddetail.getAmount()));
-                // 鎶婅鏁版嵁锛屾斁鍒颁复鏃剁殑闆嗗悎涓�
-                temporarySave.add(serviceFunddetail3);
-            } else if (serviceFunddetail.getServicesscopename().contains("绋庡悗")) {
-                logger.info("璁$畻绋庡悗淇℃伅寮�濮嬶細{}", serviceFunddetail);
-                logger.info("taxSum鍏ュ弬鏁版嵁锛歿}", taxSum);
-                //绋庡悗涓嶄负绌�, 鐢ㄤ箣鍓嶇殑鈥濇�荤◣鍚庨噾棰濃�滐紝鍔犱笂褰撳墠鈥濈◣鍚庨噾棰濃��
-                BigDecimal newTaxedAmounts = BigDecimal.valueOf(taxSum.getTaxedAmounts()).add(BigDecimal.valueOf(serviceFunddetail.getTaxedamount()));
-
-                //閫氳繃鎬荤◣鍚庯紙鍖呭惈鏈锛夌畻鍑烘�荤◣鍓�
-                String taxationBefore = TaxtUtils.getTaxationBefore(newTaxedAmounts);
-                //绠楀嚭鏈绋庡墠锛堟�荤◣鍓嶏紙鍖呭惈鏈锛� - 鎬荤◣鍓嶏紙涓嶅寘鍚湰娆★級锛�
-                BigDecimal amountNow = new BigDecimal(taxationBefore).subtract(BigDecimal.valueOf(taxSum.getAmounts()));
-
-                //绠楀嚭鏈绋庨噾(鏈绋庡墠 - 鏈绋庡悗)
-                BigDecimal tax = amountNow.subtract(BigDecimal.valueOf(serviceFunddetail.getTaxedamount()));
-                logger.info("鎵撳嵃newTaxedAmounts锛歿},taxationBefore:{},amountNow:{},tax:{}", newTaxedAmounts, taxationBefore, amountNow, tax);
-
-                //灏嗗惊鐜殑鏁版嵁閲嶆柊鏀惧埌涓�涓柊瀵硅薄涓�
-                ServiceFunddetail serviceFunddetail3 = DtoConversionUtils.sourceToTarget(serviceFunddetail, ServiceFunddetail.class);
-                serviceFunddetail3.setAmount(amountNow.doubleValue());
-                serviceFunddetail3.setTaxamount(tax.doubleValue());
-                if (ObjectUtils.isEmpty(serviceFund1.getFundtaxtime())) {
-                    serviceFunddetail3.setTaxTime(new Date());
-                } else {
-                    serviceFunddetail3.setTaxTime(serviceFund1.getFundtaxtime());
-                }
-                logger.info("鎵撳嵃serviceFunddetail3:{}", serviceFunddetail3);
-                // 灏嗚鏉℃暟鎹洿鏂�
-                updateById(serviceFunddetail3);
-                pretaxcost = pretaxcost.add(BigDecimal.valueOf(serviceFunddetail3.getAmount()));
-                // 鎶婅鏁版嵁锛屾斁鍒颁复鏃剁殑闆嗗悎涓�
-                temporarySave.add(serviceFunddetail3);
-            }
+            pretaxTotal = pretaxTotal.add(pretax);
+            vatTotal = vatTotal.add(vat);
+            cityTaxTotal = cityTaxTotal.add(cityTax);
+            laborPretaxTotal = laborPretaxTotal.add(laborPretax);
+            taxTotal = taxTotal.add(taxAmount);
+            taxedTotal = taxedTotal.add(taxedAmount);
         }
+
+        // 6. 鎵归噺鏇存柊鎵�鏈夋槑缁�
+        for (ServiceFunddetail d : updateList) {
+            this.updateById(d);
+        }
+
+        // 7. 鏇存柊涓昏〃 service_fund 鐨� 6 涓噾棰濄�佺畻绋庢椂闂淬�佺姸鎬併�佺増鏈�
         ServiceFund serviceFund = new ServiceFund();
-        serviceFund.setPretaxcost(pretaxcost.doubleValue());
-        serviceFund.setId(serviceFunddetails.get(0).getFundid());
-        if (ObjectUtils.isEmpty(serviceFund1.getFundtaxtime())) {
-            serviceFund.setFundtaxtime(new Date());
-        }
-        // 1 宸茬畻绋�
+        serviceFund.setId(fundid);
+        serviceFund.setPretaxcost(pretaxTotal.doubleValue());
+        serviceFund.setVatCost(vatTotal.doubleValue());
+        serviceFund.setCityTaxCost(cityTaxTotal.doubleValue());
+        serviceFund.setLaborPretaxCost(laborPretaxTotal.doubleValue());
+        serviceFund.setTaxcost(taxTotal.doubleValue());
+        serviceFund.setTaxedcost(taxedTotal.doubleValue());
+        serviceFund.setTaxVersion(VatTaxUtils.TAX_VERSION_V2);
+        serviceFund.setFundtaxtime(taxTime);
         serviceFund.setIstax(1);
         serviceFundMapper.updateById(serviceFund);
-
         return true;
     }
+
+    /**
+     * null 瀹夊叏鐨勯噾棰濊浆鎹�
+     */
+    private static BigDecimal toMoney(Double value) {
+        return value == null ? BigDecimal.ZERO : BigDecimal.valueOf(value);
+    }
+
+    /**
+     * 鎶ラ敊鏃惰兘瀹氫綅鍒板叿浣撲汉鍛�
+     */
+    private static String safeName(ServiceFunddetail detail) {
+        String name = detail.getBeneficiaryname() == null ? "" : detail.getBeneficiaryname();
+        String idcard = detail.getIdcardno() == null ? "" : detail.getIdcardno();
+        return name + "(" + idcard + ")";
+    }
+
+//    @Override
+//    public Boolean calculateTax2(List<ServiceFunddetail> serviceFunddetails) {
+//        //鐢ㄤ簬缂撳瓨姣忎釜韬唤璇佸彿锛坕dcardno锛夋湰鏈堢疮璁$殑绋庡墠銆佺◣閲戙�佺◣鍚庨噾棰濓紙TaxMoneySumEO瀵硅薄锛�
+//        Map<String, TaxMoneySumEO> tempTaxSumMap = new HashMap<>();
+//
+//        TaxMoneyByItemEO taxMoneyVO = new TaxMoneyByItemEO();
+//        Date firstDay = Date.from(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay(ZoneId.systemDefault()).toInstant());
+//        taxMoneyVO.setFirstDay(firstDay);
+//        taxMoneyVO.setTaxTime(new Date());
+//
+//        List<TaxMoneySumEO> taxSum = serviceFunddetailMapper.getTaxSum(taxMoneyVO);
+//        for (TaxMoneySumEO taxMoneySumEO : taxSum) {
+//            tempTaxSumMap.put(taxMoneySumEO.getIDCardNo(), taxMoneySumEO);
+//        }
+//
+//        return calculateTax(serviceFunddetails, tempTaxSumMap);
+//    }
+
 
     @Override
     public Map<String, Double> performance(ServiceFunddetail serviceFunddetail) {
@@ -456,6 +584,10 @@
         //灏嗚绠楀嚭鏉ョ殑鏁版嵁鏀惧埌serviceFunddetail涓�
         serviceFunddetail.setTaxamount(nowSingleTax.doubleValue());
         serviceFunddetail.setTaxedamount(taxAfter.doubleValue());
+        // 缁╂晥涓嶅睘浜庝笓瀹惰垂锛氫笉璁$畻澧炲�肩◣/鍩庡缓绋庯紝鍔冲姟绋庡墠 = 绋庡墠閲戦
+        serviceFunddetail.setVatAmount(0.0);
+        serviceFunddetail.setCityTaxAmount(0.0);
+        serviceFunddetail.setLaborPretaxAmount(serviceFunddetail.getAmount());
         serviceFunddetail.setJxrq(nowMonth);
 
         save(serviceFunddetail);
@@ -465,4 +597,9 @@
         return map;
     }
 
+
+    @Override
+    public List<SpStatBonus> getListBySpStatBonus(SpStatBonusReq spStatBonusReq) {
+        return spStatBonusMapper.getListBySpStatBonus(spStatBonusReq.getPabegtime(), spStatBonusReq.getPaendtime(), spStatBonusReq.getPadeptno(), spStatBonusReq.getPausername());
+    }
 }

--
Gitblit v1.9.3