yxh
7 小时以前 e61966a89350cf11940d5bd538a5dda06cb6b437
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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 口径:档位公式内再扣城建税)
     * <p>
     * <pre>
     * L ≤ 800                      → 0
     * 800 &lt; L ≤ 4,000              → (L − 800 − C) × 20%
     * L &gt; 4,000 且 L×80% ≤ 20,000  → (L×80% − C) × 20%
     * 20,000 &lt; L×80% ≤ 50,000      → (L×80% − C) × 30% − 2,000
     * L×80% &gt; 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 = BigDecimal.valueOf(0.0);
 
        //当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 = l.multiply(BigDecimal.valueOf(0.8));
        // 当X>4000时
        //(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%)-C)*30%-2000
        if (multiply.doubleValue() > 20000 && multiply.doubleValue() <= 50000) {
            taxMoney = multiply.subtract(c).multiply(BigDecimal.valueOf(0.3)).subtract(BigDecimal.valueOf(2000));
        }
 
        //X*(1-20%)>50000时,个税T=(X*(1-20%)-C)*40%-7000
        if (multiply.doubleValue() > 50000) {
            taxMoney = multiply.subtract(c).multiply(BigDecimal.valueOf(0.4)).subtract(BigDecimal.valueOf(7000));
        }
        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) {
        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);
    }
 
}