yxh
6 天以前 89bb0237ead5fb632fcc14c2c52276496e19843f
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
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) {
        //当X<=800时,个税T=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(BigDecimal.valueOf(0.2));
        }
 
        BigDecimal multiply = money.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(BigDecimal.valueOf(0.2));
        }
 
        //(2)当20000<X*(1-20%)<=50000时,个税T=X*(1-20%)*30%-2000
        if (multiply.doubleValue() > 20000 && multiply.doubleValue() <= 50000) {
            taxMoney = multiply.multiply(BigDecimal.valueOf(0.3)).subtract(BigDecimal.valueOf(2000));
        }
 
        //X*(1-20%)>50000时,个税T=X*(1-20%)*40%-7000
        if (multiply.doubleValue() > 50000) {
            taxMoney = multiply.multiply(BigDecimal.valueOf(0.4)).subtract(BigDecimal.valueOf(7000));
        }
        DecimalFormat decimalFormat = new DecimalFormat("#0.00");
        //进一法
        decimalFormat.setRoundingMode(RoundingMode.CEILING);
        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
     */
    public static String getTaxationBefore(BigDecimal money) {
        //当X<=800时,个税T=0
        BigDecimal taxMoney = BigDecimal.valueOf(0.00);
        if (money.doubleValue() <= 800) {
            taxMoney = money;
        }
//第一个参数是除数,第二个参数代表保留几位小数,第三个代表的是使用的模式
        if (money.doubleValue() > 800 && money.doubleValue() <= 3360) {
            taxMoney = money.subtract(new BigDecimal(160)).divide(new BigDecimal("0.8"), 2, BigDecimal.ROUND_CEILING);
        }
 
        if (money.doubleValue() > 3360 && money.doubleValue() <= 21000) {
            taxMoney = money.divide(new BigDecimal("0.84"), 2, BigDecimal.ROUND_CEILING);
        }
 
        if (money.doubleValue() > 21000 && money.doubleValue() <= 49500) {
            money = money.subtract(new BigDecimal(2000));
            taxMoney = money.divide(new BigDecimal("0.76"), 2, BigDecimal.ROUND_CEILING);
        }
 
        if (money.doubleValue() > 49500) {
            money = money.subtract(new BigDecimal(7000));
            taxMoney = money.divide(new BigDecimal("0.68"), 2, BigDecimal.ROUND_CEILING);
        }
 
 
        DecimalFormat decimalFormat = new DecimalFormat("#0.00");
        //进一法
        decimalFormat.setRoundingMode(RoundingMode.CEILING);
        String format = decimalFormat.format(taxMoney);
        log.info("税前金额为:{}", format);
        return format;
    }
 
 
    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);
    }
 
}