package com.ruoyi.common.tax; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.core.domain.entity.SysDictData; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.spring.SpringUtils; import lombok.extern.slf4j.Slf4j; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.Collection; import java.util.List; /** * 劳务报酬个税计算规则 (可能不适合公共用法,用前先看看是否适合) * * @author ls */ @Slf4j public class TaxtUtils { /** * 分隔符 */ public static final String SEPARATOR = ","; /** * 获取税金(不含城建税,兼容旧调用) * * @param money 报酬 (劳务税前) */ public static String getTaxation(BigDecimal money) { return getTaxation(money, BigDecimal.ZERO); } /** * 获取劳务个税(2026-11-30 口径:档位公式内再扣城建税) *
*
* L ≤ 800 → 0
* 800 < L ≤ 4,000 → (L − 800 − C) × 20%
* L > 4,000 且 L×80% ≤ 20,000 → (L×80% − C) × 20%
* 20,000 < L×80% ≤ 50,000 → (L×80% − C) × 30% − 2,000
* L×80% > 50,000 → (L×80% − C) × 40% − 7,000
*
* 档位判断仍按劳务税前 L;结果四舍五入到分,且不小于 0。
*
* @param money 劳务税前 L
* @param cityTax 城建税 C(可为 null)
*/
public static String getTaxation(BigDecimal money, BigDecimal cityTax) {
BigDecimal l = money == null ? BigDecimal.ZERO : money;
BigDecimal c = cityTax == null ? BigDecimal.ZERO : cityTax;
//当X<=800时,个税T=0
BigDecimal taxMoney = BigDecimal.valueOf(0.0);
//当800* 注意:除数必须用字符串/常量构造的精确值。原先写成 {@code new BigDecimal(0.84)} 等, * 会把 double 的二进制误差带进除法(如 8313.06 / 0.84 变成 9896.500000…001), * 再叠加取整就多出 1 分钱,导致“税后反算税前”无法还原(8300.00 类金额反解出 10000.03)。 * * @param money * @return */ /** * 由劳务税后 N 反算劳务税前 L(2026-11-30 口径) *
* 关系:{@code N = L − C − T(L, C)},且 {@code L = P − V}、{@code V = calcVat(P)}、{@code C = calcCityTax(V)}。 * 令 {@code Y = L − C},各档位可解析求 Y(个税公式里的城建税修正项),再迭代求出 C,最后做 ±0.01 校正, * 保证「L → T/C → N」能还原输入的 N。 * * @param money 劳务税后 N * @return 劳务税前 L */ public static String getTaxationBefore(BigDecimal money) { BigDecimal n = money == null ? BigDecimal.ZERO : money; if (n.signum() <= 0) { return "0.00"; } BigDecimal c = BigDecimal.ZERO; BigDecimal y = inverseLaborValue(n, c); for (int i = 0; i < 20; i++) { BigDecimal l = y.add(c); BigDecimal p = VatTaxUtils.solveGross(l).getPretax(); BigDecimal cNew = VatTaxUtils.calcCityTax(VatTaxUtils.calcVat(p)); boolean stable = cNew.compareTo(c) == 0; c = cNew; y = inverseLaborValue(n, c); if (stable) { break; } } BigDecimal laborPretax = y.add(c).setScale(2, RoundingMode.HALF_UP); // 校正:在 ±0.01 内找使 L − C − T 等于 N 的解(取整导致的 1 分差修正) BigDecimal best = null; for (int delta = -1; delta <= 1; delta++) { BigDecimal candidate = laborPretax.add(BigDecimal.valueOf(delta).movePointLeft(2)); if (candidate.signum() <= 0) { continue; } BigDecimal pretax = VatTaxUtils.solveGross(candidate).getPretax(); BigDecimal cityTax = VatTaxUtils.calcCityTax(VatTaxUtils.calcVat(pretax)); BigDecimal tax = new BigDecimal(getTaxation(candidate, cityTax)); BigDecimal taxed = candidate.subtract(cityTax).subtract(tax); if (taxed.subtract(n).abs().compareTo(new BigDecimal("0.005")) <= 0) { best = candidate; break; } } BigDecimal result = best != null ? best : laborPretax; DecimalFormat decimalFormat = new DecimalFormat("#0.00"); decimalFormat.setRoundingMode(RoundingMode.HALF_UP); String format = decimalFormat.format(result); log.info("税前金额为:{}", format); return format; } /** * 已知 N 与 C,解 Y = L − C(各档位解析式,含城建税修正项) */ private static BigDecimal inverseLaborValue(BigDecimal n, BigDecimal c) { double nd = n.doubleValue(); if (nd <= 800) { // T = 0,N = L − C = Y return n; } if (nd <= 3360) { // N = 0.8Y + 160 return n.subtract(new BigDecimal(160)).divide(new BigDecimal("0.8"), 2, RoundingMode.HALF_UP); } if (nd <= 21000) { // N = 0.84Y + 0.04C return n.subtract(c.multiply(new BigDecimal("0.04"))).divide(new BigDecimal("0.84"), 2, RoundingMode.HALF_UP); } if (nd <= 49500) { // N = 0.76Y + 2000 + 0.06C return n.subtract(new BigDecimal(2000)).subtract(c.multiply(new BigDecimal("0.06"))) .divide(new BigDecimal("0.76"), 2, RoundingMode.HALF_UP); } // N = 0.68Y + 7000 + 0.08C return n.subtract(new BigDecimal(7000)).subtract(c.multiply(new BigDecimal("0.08"))) .divide(new BigDecimal("0.68"), 2, RoundingMode.HALF_UP); } public static void main(String[] args) { String taxation = getTaxation(BigDecimal.valueOf(43000)); String taxationBefore = getTaxationBefore(BigDecimal.valueOf(205892.36)); System.out.println("taxation:" + taxation + "============taxationBefore: " + taxationBefore); } }