sinake
3 天以前 ee089a8ac4a9d99ef0a0491bbf1d80a2d4fbc393
ruoyi-project/src/main/java/com/ruoyi/project/service/impl/ServiceFunddetailServiceImpl.java
@@ -13,6 +13,7 @@
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.*;
@@ -253,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 累加/扣减,避免浮点尾差
            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);
@@ -285,145 +293,228 @@
    }
    /**
     * 批量算税主方法
     * 1. 校验参数
     * 2. 排序明细,保证税时间为空的排在后面
     * 3. 遍历每条明细,分身份证号为空和不为空两种情况分别处理
     * - 身份证号为空:直接按规则赋值税后金额、税金
     * - 身份证号不为空:
     * a. 查询本月第一天,获取最大序号
     * b. 查询本月累计税前、税金、税后金额
     * c. 根据"税前/税后"类型分别计算税金、税后金额
     * d. 设置算税时间、序号等
     * 4. 累计本批次总税前、税后金额
     * 5. 批量更新所有明细
     * 6. 更新主表ServiceFund的总金额、算税时间、状态
     * 批量算税主方法(劳务个税 + 增值税 + 城建税,v2 口径)
     * <p>
     * 金额模型:
     * <pre>
     * 税前录入:V = ceil(P × 1%)、C = ceil(V × 3.5%)、L = P - V - C、
     *          T = 劳务个税(当月累计 L) - 原累计 T、N = L - T
     * 税后录入:N累计 = 原累计 N + N、L累计 = getTaxationBefore(N累计)、
     *          L = L累计 - 原累计 L、T = L - N、P/V/C = solveGross(L)
     * 无身份证:V = C = T = 0,L = N = P
     * </pre>
     *
     * @param serviceFunddetails 费用明细列表
     * @param tempTaxSumMap      本月累计金额缓存(按身份证号)
     * @return 是否算税成功
     */
    @Override
    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("算税条件为空,请检查后,再进行计算");
        }
        // 2. 排序,税时间为空的排后面
        serviceFunddetails.sort(Comparator.comparing(obj -> ObjectUtils.isEmpty(obj.getTaxTime()) ? 1 : 0));
        // 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及主表信息
        // 3. 获取本批次 fundid 及主表信息
        Long fundid = serviceFunddetails.get(0).getFundid();
        log.info("算税的fundid的值为:{}", fundid);
        ServiceFund serviceFund1 = serviceFundMapper.selectById(fundid);
        log.info("fund表的fundtextime值为:{}", serviceFund1.getFundtaxtime());
        // 4. 初始化累计金额、待更新明细列表、序号缓存、累计金额缓存
        BigDecimal pretaxcost = BigDecimal.ZERO;
        BigDecimal taxedcost = BigDecimal.ZERO;
        if (serviceFund1 == null) {
            throw new BaseException("未找到费用申请单,ID=" + fundid);
        }
        // 明细 tax_time 统一使用本次算税时间
        if (taxTime == null) {
            taxTime = new Date();
        }
        //创建一个用于存放“需要更新到数据库的ServiceFunddetail对象”的列表
        // 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;
        List<ServiceFunddetail> updateList = new ArrayList<>();
        // 5. 遍历每条明细,分情况处理
        // 5. 遍历每条明细
        for (ServiceFunddetail detail : serviceFunddetails) {
            // 5.1 身份证号为空,直接赋值(一些人或公司,不需要算税)
            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 身份证号为空:不扣增值税/城建税/劳务个税
            if (StringUtils.isEmpty(detail.getIdcardno())) {
                if (StringUtils.isEmpty(detail.getServicesscopename()) || !detail.getServicesscopename().contains("税后")) {
                    // 普通明细,税后金额=申请金额
                    detail.setTaxedamount(detail.getAmount());
                } else {
                    // 税后明细,申请金额=税后金额
                    detail.setAmount(detail.getTaxedamount());
                }
                // 累计本批次总金额
                pretaxcost = pretaxcost.add(BigDecimal.valueOf(detail.getAmount()));
                taxedcost = taxedcost.add(BigDecimal.valueOf(detail.getTaxedamount()));
                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;
            }
            // 5.2 身份证号不为空,需查历史累计金额、序号
            // 5.2 身份证号不为空:本月累计 + 分档算税
            String idcard = detail.getIdcardno().trim();
            // 5.2.1 查询本月累计金额(缓存避免重复查库)
            TaxMoneySumEO taxSum = tempTaxSumMap.getOrDefault(idcard, null);
            // 5.2.1 取本月累计(缓存避免重复查库)
            TaxMoneySumEO taxSum = tempTaxSumMap.get(idcard);
            if (taxSum == null) {
                taxSum = new TaxMoneySumEO();
                taxSum.setXh(1L);
                detail.setXh(1L);
                taxSum.setXh(0L);
                tempTaxSumMap.put(idcard, taxSum);
                detail.setAllAmount(taxSum.getAmounts());
            }
            long xh = (taxSum.getXh() == null ? 0L : taxSum.getXh()) + 1;
            taxSum.setXh(xh);
            detail.setXh(xh);
            // 累计税前金额(不含本笔),用于页面展示
            detail.setAllAmount(taxSum.getAmounts());
            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));
                    }
                }
                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 {
                long xh = taxSum.getXh() + 1;
                taxSum.setXh(xh);
                tempTaxSumMap.put(idcard, taxSum);
                detail.setXh(xh);
                detail.setAllAmount(taxSum.getAmounts());
                // 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));
            }
            // 5.2.3 算税逻辑
            if (detail.getServicesscopename().contains("税后")) {
                // 税后明细:累计税后金额,反推税前金额和税金
//                if (idcard.equals("362329199103224511")) {
//                    log.info("朱玮安");
//                }
                BigDecimal newTaxedAmounts = BigDecimal.valueOf(taxSum.getTaxedAmounts()).add(BigDecimal.valueOf(detail.getTaxedamount()));
                String taxationBefore = TaxtUtils.getTaxationBefore(newTaxedAmounts);
                BigDecimal amountNow = new BigDecimal(taxationBefore).subtract(BigDecimal.valueOf(taxSum.getAmounts()));
                BigDecimal tax = amountNow.subtract(BigDecimal.valueOf(detail.getTaxedamount()));
                detail.setAmount(amountNow.doubleValue());
                detail.setTaxamount(tax.doubleValue());
                if (ObjectUtils.isEmpty(serviceFund1.getFundtaxtime())) {
                    detail.setTaxTime(new Date());
                } else {
                    detail.setTaxTime(serviceFund1.getFundtaxtime());
                }
                taxSum.setAmounts((BigDecimal.valueOf(taxSum.getAmounts()).add(amountNow)).doubleValue());
                taxSum.setTaxAmounts((BigDecimal.valueOf(taxSum.getTaxAmounts()).add(tax)).doubleValue());
                taxSum.setTaxedAmounts(newTaxedAmounts.doubleValue());
            } else {
                // 税前明细:累计税前金额,算总税金,再算本次税金和税后金额
                BigDecimal newAmounts = BigDecimal.valueOf(taxSum.getAmounts()).add(BigDecimal.valueOf(detail.getAmount()));
//                if (idcard.equals("362329199103224511")) {
//                    log.info("朱玮安");
//                }
                String taxation = TaxtUtils.getTaxation(newAmounts);
                BigDecimal taxAmountNow = new BigDecimal(taxation).subtract(new BigDecimal(String.valueOf(taxSum.getTaxAmounts())));
                BigDecimal texdAfterNow = BigDecimal.valueOf(detail.getAmount()).subtract(taxAmountNow);
                detail.setTaxamount(taxAmountNow.doubleValue());
                detail.setTaxedamount(texdAfterNow.doubleValue());
                if (ObjectUtils.isEmpty(serviceFund1.getFundtaxtime())) {
                    detail.setTaxTime(new Date());
                } else {
                    detail.setTaxTime(serviceFund1.getFundtaxtime());
                }
                taxSum.setAmounts(newAmounts.doubleValue());
                taxSum.setTaxAmounts((BigDecimal.valueOf(taxSum.getTaxAmounts()).add(taxAmountNow).doubleValue()));
                taxSum.setTaxedAmounts((BigDecimal.valueOf(taxSum.getTaxedAmounts()).add(texdAfterNow)).doubleValue());
            }
            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);
            pretaxcost = pretaxcost.add(BigDecimal.valueOf(detail.getAmount()));
            taxedcost = taxedcost.add(BigDecimal.valueOf(detail.getTaxedamount()));
            // 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());
            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. 更新主表ServiceFund
        ServiceFund serviceFund = new ServiceFund();
        serviceFund.setPretaxcost(pretaxcost.doubleValue());
        serviceFund.setTaxedcost(taxedcost.doubleValue());
        serviceFund.setId(serviceFunddetails.get(0).getFundid());
        serviceFund.setFundtaxtime(new Date());
        // 7. 更新主表 service_fund 的 6 个金额、算税时间、状态、版本
        ServiceFund serviceFund = new ServiceFund();
        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
@@ -493,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);