| | |
| | | } |
| | | |
| | | /** |
| | | * 根据受益人身份证编号获取税费金额 |
| | | * 根据受益人身份证编号获取税费金额(录入时的个税预览) |
| | | * <p> |
| | | * 2026-11-17(累计口径):同一身份证**当月**的增值税、城建税、个税都按累计计算,本笔取差额: |
| | | * <pre> |
| | | * P累 = Σ当月税前 + 本次;V累 = calcVat(P累)(起征点按累计判断);C累 = calcCityTax(V累) |
| | | * 本笔 V = V累 − Σ已算税增值税;本笔 C 同理;本笔 L = 本笔 P − 本笔 V − 本笔 C |
| | | * 本笔个税 = 个税(L累计) − Σ已算税个税 |
| | | * </pre> |
| | | * |
| | | * @param taxMoneyVo |
| | | * @return |
| | | * @param taxMoneyVo 身份证号 + 本次含税金额 |
| | | * @return 本次劳务个税 |
| | | */ |
| | | @Override |
| | | public Double getTaxByBeneFiciaryNo(TaxMoneyVO taxMoneyVo) { |
| | | // 金额累加必须用 BigDecimal:double 累加会引入浮点尾差,导致算出的个税出现分以下误差 |
| | | 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); |
| | | // 本次含税金额 + 累计:按累计口径算增值税/城建税,本笔取差额 |
| | | 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; |
| | | } |
| | |
| | | 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); |
| | | |
| | |
| | | BigDecimal taxedAmount; |
| | | |
| | | if (afterTax) { |
| | | // 5.2.2 税后录入:先反算劳务税前,再循环反解税前/增值税/城建税 |
| | | // 5.2.2 税后录入:先由“累计税后”反算累计劳务税前,再反解累计含税税前,最后按差额得出本笔 V/C/P |
| | | taxedAmount = toMoney(detail.getTaxedamount()); |
| | | BigDecimal sumTaxed = prevTaxed.add(taxedAmount); |
| | | BigDecimal sumLaborPretax = new BigDecimal(TaxtUtils.getTaxationBefore(sumTaxed)); |
| | |
| | | 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(); |
| | | // 累计口径:P累 = 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("累计跨起征点导致本笔劳务税前为负,本笔不扣税、差额顺延。人员:{},本笔税前={},本笔增值税={}," |
| | | + "本笔城建税={},累计税前={}", |
| | | 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)); |
| | |
| | | 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); |
| | | |
| | |
| | | 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); |