yxh
13 小时以前 a6df1cdd4b0ff3e35eaff8f313d125d4847dc93b
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package com.ruoyi.common.tax;
 
import lombok.extern.slf4j.Slf4j;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
 
/**
 * 增值税 / 城建税 / 劳务税前 计算规则
 * <p>
 * 2026-11-17 口径调整:增值税由“价外税”改为“<b>价内税</b>”,税前金额 P 视为<b>含增值税</b>的金额:
 * <pre>
 * 起征点:P &lt; 1010 元时 V = C = 0(L = P)
 * V = round(P × 1% / (1 + 1%), 2)   增值税(从含税金额中价税分离,四舍五入到分)
 * C = round(V × 3.5%, 2)            城建税(四舍五入到分)
 * L = P - V - C                    劳务税前(不含增值税、城建税)
 * </pre>
 * 个税 T 仍按当月累计劳务税前 L 计算;劳务税后 N = L - T。
 * <p>
 * 录入劳务税后时,劳务税前 L 已知,需要反解 P/V/C:{@code P = L + V + C},
 * 而 V、C 又取决于 P,因此用 {@link #solveGross(BigDecimal)} 迭代求解(通常 2~3 次收敛)。
 * <p>
 * 取整说明:增值税、城建税、劳务个税统一<b>四舍五入到分</b>(2026-11-17 由向上取整调整),
 * 因此“P → N → 反算 P”仍可能因取整相差 0.01,属既定口径(业务已确认可接受)。
 * <p>
 * 起征点说明:由于 P &lt; 1010 不征增值税,而 P 略大于 1010 时 V+C 不为 0,L 在起征点附近不是单射——
 * 反算时若 L &lt; 1010 会得到 P = L(不扣税分支),这是规则的固有边界,实际单据金额远大于该区间。
 *
 * @author ruoyi
 */
@Slf4j
public class VatTaxUtils {
 
    /**
     * 增值税税率 1%
     */
    public static final BigDecimal VAT_RATE = new BigDecimal("0.01");
 
    /**
     * 价税分离分母 1 + 1%
     */
    public static final BigDecimal ONE_PLUS_VAT_RATE = new BigDecimal("1.01");
 
    /**
     * 城建税税率 3.5%(以增值税为基数)
     */
    public static final BigDecimal CITY_TAX_RATE = new BigDecimal("0.035");
 
    /**
     * 税口径版本:
     * <ul>
     *   <li>v1 = 历史口径(无增值税/城建税)</li>
     *   <li>v2 = 价外税口径(增值税 = 税前金额 × 1%,2026-11 短暂使用)</li>
     *   <li>v3 = 价内税口径(增值税 = 税前金额 × 1% / (1+1%),2026-11-17 起)</li>
     * </ul>
     */
    public static final String TAX_VERSION_V1 = "v1";
 
    public static final String TAX_VERSION_V2 = "v2";
 
    public static final String TAX_VERSION_V3 = "v3";
 
    /**
     * 当前口径版本(算税时写入)
     */
    public static final String TAX_VERSION_CURRENT = TAX_VERSION_V3;
 
    /**
     * 增值税起征金额(含税税前金额 P 达到该值才计征增值税,等于也计征)
     * <p>
     * 2026-11-17 业务确认:P &lt; 1010 元不征增值税(城建税随之为 0),P ≥ 1010 元按价内税公式计征。
     */
    public static final BigDecimal VAT_THRESHOLD = new BigDecimal("1010");
 
    /**
     * 反解循环上限(正常 2~3 次即稳定)
     */
    private static final int SOLVE_MAX_LOOP = 20;
 
    private static final int MONEY_SCALE = 2;
 
    /**
     * 中间除法保留位数(避免除不尽时提前截断影响“进一法”)
     */
    private static final int DIVIDE_SCALE = 12;
 
    private VatTaxUtils() {
    }
 
    /**
     * 金额四舍五入到分(算税统一取整方式,2026-11-17 由“向上取整”改为“四舍五入”)
     */
    public static BigDecimal roundMoney(BigDecimal money) {
        if (money == null) {
            return BigDecimal.ZERO.setScale(MONEY_SCALE, RoundingMode.HALF_UP);
        }
        return money.setScale(MONEY_SCALE, RoundingMode.HALF_UP);
    }
 
    /**
     * 增值税 = round(含税金额 × 1% / (1 + 1%), 2)
     * <p>
     * 起征点:税前金额 P &lt; {@link #VAT_THRESHOLD}(1010 元)时不计征,返回 0;
     * P ≥ 1010 时按价内税公式计算(城建税随之为 0 / 正常计征)。
     * <p>
     * 例:P=10,000 → round(10000 × 0.01 / 1.01, 2) = round(99.009900…, 2) = 99.01;
     * P=1,009.99 → 0;P=1,010 → 10.00
     *
     * @param pretax 税前金额 P(含增值税)
     */
    public static BigDecimal calcVat(BigDecimal pretax) {
        if (pretax == null || pretax.signum() <= 0) {
            return BigDecimal.ZERO.setScale(MONEY_SCALE, RoundingMode.HALF_UP);
        }
        if (pretax.compareTo(VAT_THRESHOLD) < 0) {
            // 未达起征点:不计征增值税(城建税随之为 0,劳务税前 = 税前金额)
            return BigDecimal.ZERO.setScale(MONEY_SCALE, RoundingMode.HALF_UP);
        }
        return pretax.multiply(VAT_RATE)
                .divide(ONE_PLUS_VAT_RATE, DIVIDE_SCALE, RoundingMode.HALF_UP)
                .setScale(MONEY_SCALE, RoundingMode.HALF_UP);
    }
 
    /**
     * 城建税 = round(增值税 × 3.5%, 2)
     *
     * @param vat 增值税 V
     */
    public static BigDecimal calcCityTax(BigDecimal vat) {
        if (vat == null || vat.signum() <= 0) {
            return BigDecimal.ZERO.setScale(MONEY_SCALE, RoundingMode.HALF_UP);
        }
        return vat.multiply(CITY_TAX_RATE).setScale(MONEY_SCALE, RoundingMode.HALF_UP);
    }
 
    /**
     * 劳务税前 = 税前金额 - 增值税 - 城建税
     * <p>
     * 金额过小(取整后 增值税+城建税 ≥ 税前金额)时按“不扣税”处理,劳务税前 = 税前金额,保证不出现负数。
     */
    public static BigDecimal calcLaborPretax(BigDecimal pretax) {
        GrossAmount gross = splitRate(pretax);
        return roundMoney(gross.getPretax().subtract(gross.getVat()).subtract(gross.getCityTax()));
    }
 
    /**
     * 由税前金额 P 做价税分离,返回 P / V / C。
     * <p>
     * 小额保护:当取整后的 V + C ≥ P 时,视为金额过小无法扣税,返回 V = C = 0、劳务税前 = P,
     * 避免出现“劳务税前为负”的异常数据。
     *
     * @param pretax 税前金额 P(含增值税)
     */
    public static GrossAmount splitRate(BigDecimal pretax) {
        BigDecimal p = roundMoney(pretax);
        if (p.signum() <= 0) {
            return new GrossAmount(p, zero(), zero());
        }
        BigDecimal v = calcVat(p);
        BigDecimal c = calcCityTax(v);
        if (v.add(c).compareTo(p) >= 0) {
            log.warn("金额过小,价税分离后税费不小于税前金额,按不扣税处理:P={},V={},C={}", p, v, c);
            return new GrossAmount(p, zero(), zero());
        }
        return new GrossAmount(p, v, c);
    }
 
    /**
     * 已知劳务税前 L,反解税前金额 P、增值税 V、城建税 C
     * <p>
     * 循环:P 初值取 L,反复用 P 求 V、C,再用 P = L + V + C 修正,直到稳定。
     * 由于 V、C 四舍五入,迭代可能停在相邻的两个有效解之一(差 0.01),属既定口径。
     *
     * @param laborPretax 劳务税前 L
     * @return 满足 P = L + V + C、V = round(P × 1% / 1.01)、C = round(V × 3.5%) 的解
     */
    public static GrossAmount solveGross(BigDecimal laborPretax) {
        BigDecimal l = roundMoney(laborPretax);
        if (l.signum() <= 0) {
            return new GrossAmount(zero(), zero(), zero());
        }
        BigDecimal p = l;
        BigDecimal v = calcVat(p);
        BigDecimal c = calcCityTax(v);
        for (int i = 0; i < SOLVE_MAX_LOOP; i++) {
            BigDecimal newP = roundMoney(l.add(v).add(c));
            BigDecimal newV = calcVat(newP);
            BigDecimal newC = calcCityTax(newV);
            boolean stable = newP.compareTo(p) == 0 && newV.compareTo(v) == 0 && newC.compareTo(c) == 0;
            p = newP;
            v = newV;
            c = newC;
            if (stable) {
                break;
            }
        }
        log.debug("反解税前:L={} -> P={}, V={}, C={}", l, p, v, c);
        return new GrossAmount(p, v, c);
    }
 
    private static BigDecimal zero() {
        return BigDecimal.ZERO.setScale(MONEY_SCALE, RoundingMode.HALF_UP);
    }
 
    /**
     * 价税分离结果:税前金额 / 增值税 / 城建税
     */
    public static class GrossAmount {
        private final BigDecimal pretax;
        private final BigDecimal vat;
        private final BigDecimal cityTax;
 
        public GrossAmount(BigDecimal pretax, BigDecimal vat, BigDecimal cityTax) {
            this.pretax = pretax;
            this.vat = vat;
            this.cityTax = cityTax;
        }
 
        public BigDecimal getPretax() {
            return pretax;
        }
 
        public BigDecimal getVat() {
            return vat;
        }
 
        public BigDecimal getCityTax() {
            return cityTax;
        }
 
        /**
         * 劳务税前 = 税前金额 - 增值税 - 城建税
         */
        public BigDecimal getLaborPretax() {
            return roundMoney(pretax.subtract(vat).subtract(cityTax));
        }
    }
}