| | |
| | | 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; |
| | |
| | | public static final String SEPARATOR = ","; |
| | | |
| | | /** |
| | | * 获取税金 |
| | | * 获取税金(不含城建税,兼容旧调用) |
| | | * |
| | | * @param money 报酬 (税前) |
| | | * @param money 报酬 (劳务税前) |
| | | */ |
| | | public static String getTaxation(BigDecimal money) { |
| | | return getTaxation(money, BigDecimal.ZERO); |
| | | } |
| | | |
| | | /** |
| | | * 获取劳务个税(2026-11-30 口径:档位公式内再扣城建税) |
| | | * <p> |
| | | * <pre> |
| | | * 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 |
| | | * </pre> |
| | | * 档位判断仍按劳务税前 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 = new BigDecimal(0.0); |
| | | BigDecimal taxMoney = BigDecimal.valueOf(0.0); |
| | | |
| | | //当800<X<=4000时,个税T=(X-800)*20% |
| | | if (money.doubleValue() > 800 && money.doubleValue() <= 4000) { |
| | | BigDecimal subtract = money.subtract(new BigDecimal(800)); |
| | | taxMoney = subtract.multiply(new BigDecimal(0.2)); |
| | | //当800<X<=4000时,个税T=(X-800-C)*20% |
| | | if (l.doubleValue() > 800 && l.doubleValue() <= 4000) { |
| | | BigDecimal subtract = l.subtract(new BigDecimal(800)).subtract(c); |
| | | taxMoney = subtract.multiply(BigDecimal.valueOf(0.2)); |
| | | } |
| | | |
| | | BigDecimal multiply = money.multiply(new BigDecimal(0.8)); |
| | | BigDecimal multiply = l.multiply(BigDecimal.valueOf(0.8)); |
| | | // 当X>4000时 |
| | | //(1)当X*(1-20%)<=20000时,个税T=X*(1-20%)*20% |
| | | if (money.doubleValue() > 4000 && multiply.doubleValue() <= 20000) { |
| | | taxMoney = multiply.multiply(new BigDecimal(0.2)); |
| | | //(1)当X*(1-20%)<=20000时,个税T=(X*(1-20%)-C)*20% |
| | | if (l.doubleValue() > 4000 && multiply.doubleValue() <= 20000) { |
| | | taxMoney = multiply.subtract(c).multiply(BigDecimal.valueOf(0.2)); |
| | | } |
| | | |
| | | //(2)当20000<X*(1-20%)<=50000时,个税T=X*(1-20%)*30%-2000 |
| | | //(2)当20000<X*(1-20%)<=50000时,个税T=(X*(1-20%)-C)*30%-2000 |
| | | if (multiply.doubleValue() > 20000 && multiply.doubleValue() <= 50000) { |
| | | taxMoney = multiply.multiply(new BigDecimal(0.3)).subtract(new BigDecimal(2000)); |
| | | taxMoney = multiply.subtract(c).multiply(BigDecimal.valueOf(0.3)).subtract(BigDecimal.valueOf(2000)); |
| | | } |
| | | |
| | | //X*(1-20%)>50000时,个税T=X*(1-20%)*40%-7000 |
| | | //X*(1-20%)>50000时,个税T=(X*(1-20%)-C)*40%-7000 |
| | | if (multiply.doubleValue() > 50000) { |
| | | taxMoney = multiply.multiply(new BigDecimal(0.4)).subtract(new BigDecimal(7000)); |
| | | taxMoney = multiply.subtract(c).multiply(BigDecimal.valueOf(0.4)).subtract(BigDecimal.valueOf(7000)); |
| | | } |
| | | DecimalFormat decimalFormat = new DecimalFormat("#.00"); |
| | | if (taxMoney.signum() < 0) { |
| | | taxMoney = BigDecimal.ZERO; |
| | | } |
| | | DecimalFormat decimalFormat = new DecimalFormat("#0.00"); |
| | | // 算税统一四舍五入到分(2026-11-17 由“进一法”调整为四舍五入) |
| | | decimalFormat.setRoundingMode(RoundingMode.HALF_UP); |
| | | String format = decimalFormat.format(taxMoney); |
| | | log.info("税金为:{}", format); |
| | | return format; |
| | | } |
| | | |
| | | /** |
| | | * 通过税后工资,算出税前工资 |
| | | * <p> |
| | | * 注意:除数必须用字符串/常量构造的精确值。原先写成 {@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 口径) |
| | | * <p> |
| | | * 关系:{@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) { |
| | | //当X<=800时,个税T=0 |
| | | BigDecimal taxMoney = new BigDecimal(0.0); |
| | | if (money.doubleValue() <= 800) { |
| | | taxMoney = money; |
| | | BigDecimal n = money == null ? BigDecimal.ZERO : money; |
| | | if (n.signum() <= 0) { |
| | | return "0.00"; |
| | | } |
| | | //第一个参数是除数,第二个参数代表保留几位小数,第三个代表的是使用的模式 |
| | | if (money.doubleValue() > 800 && money.doubleValue() <= 3360) { |
| | | taxMoney = money.subtract(new BigDecimal(160)).divide(new BigDecimal(0.8), 2, BigDecimal.ROUND_UP); |
| | | 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); |
| | | |
| | | if (money.doubleValue() > 3360 && money.doubleValue() <= 21000) { |
| | | taxMoney = money.divide(new BigDecimal(0.84), 2, BigDecimal.ROUND_DOWN); |
| | | // 校正:在 ±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; |
| | | |
| | | if (money.doubleValue() > 21000 && money.doubleValue() <= 40000) { |
| | | money = money.subtract(new BigDecimal(2000)); |
| | | taxMoney = money.divide(new BigDecimal(0.76), 2, BigDecimal.ROUND_DOWN); |
| | | } |
| | | |
| | | if (money.doubleValue() > 40000) { |
| | | money = money.subtract(new BigDecimal(7000)); |
| | | taxMoney = money.divide(new BigDecimal(0.68), 2, BigDecimal.ROUND_DOWN); |
| | | } |
| | | |
| | | |
| | | DecimalFormat decimalFormat = new DecimalFormat("#.00"); |
| | | String format = decimalFormat.format(taxMoney); |
| | | 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(new BigDecimal(400)); |
| | | String taxationBefore = getTaxationBefore(new BigDecimal(400)); |
| | | |
| | | |
| | | System.out.println(taxation + " " + taxationBefore); |
| | | String taxation = getTaxation(BigDecimal.valueOf(43000)); |
| | | String taxationBefore = getTaxationBefore(BigDecimal.valueOf(205892.36)); |
| | | System.out.println("taxation:" + taxation + "============taxationBefore: " + taxationBefore); |
| | | } |
| | | |
| | | } |