根据不同价格段税率范围计算最终应缴纳税款范围

问题描述

  • 最近遇到一个计算税款的问题,记录一下,以免下次忘记了

代码实现

/*
 * @Description:计算收费金额
 * @param1: { number } amount 标的额
 * @param2: { number } piece 件数(收费额不足5000元时,需按件数收费500元/件)
 * @return: { array } 收费金额范围
 * @Author: mengbing
 * @Date: 2022-07-25 18:46:16
 */
function calculateCharge(amount, piece) {
    // 分段计算方式(upper:当前税级上限,percent:当前税级收费比例范围)
    const brackets = [
        {
            upper: 10 * 10000,
            percent: [8, 10],
        },
        {
            upper: 50 * 10000,
            percent: [7, 9],
        },
        {
            upper: 100 * 10000,
            percent: [6, 8],
        },
        {
            upper: 500 * 10000,
            percent: [5, 7],
        },
        {
            upper: 1000 * 10000,
            percent: [4, 6],
        },
        {
            upper: 2000 * 10000,
            percent: [3, 5],
        },
        {
            upper: 5000 * 10000,
            percent: [2, 4],
        },
        {
            upper: Infinity,
            percent: [1, 3],
        },
    ]
    // 按件计费标准额
    const STANDERD_PIECE = 500

    let res = [0, 0] // 最后收费额范围
    for (let i = 0; i < brackets.length; i++) {
        if (amount >= brackets[i].upper) {
            let base =
                i == 0 ? brackets[i].upper : brackets[i].upper - brackets[i - 1].upper

            let min = base * brackets[i].percent[0] * 0.01
            let max = base * brackets[i].percent[1] * 0.01

            res = [res[0] + min, res[1] + max]
        } else {
            let base = i == 0 ? amount : amount - brackets[i - 1].upper
            let min = base * brackets[i].percent[0] * 0.01
            let max = base * brackets[i].percent[1] * 0.01

            if (i == 0) {
                let chargeByPiece = STANDERD_PIECE * piece // 按件计费
                if (max < STANDERD_PIECE) {
                    res = [res[0] + chargeByPiece, res[1] + chargeByPiece]
                } else if (min < STANDERD_PIECE && max >= STANDERD_PIECE) {
                    res = [res[0] + chargeByPiece, res[1] + max]
                } else if (min >= STANDERD_PIECE) {
                    res = [res[0] + min, res[1] + max]
                }
            } else {
                res = [res[0] + min, res[1] + max]
            }

            break
        }
    }

    return res
}

测试

// console.log(calculateCharge(10 * 10000, 4)) //[8000, 10000]
// console.log(calculateCharge(5 * 10000, 4)) //[2000, 5000]
// console.log(calculateCharge(2 * 10000, 4)) //[1600, 2000]
// console.log(calculateCharge(12 * 10000, 4)) //[9400, 11800]
console.log(calculateCharge(50 * 10000, 4)) //[36000, 46000]
posted @ 2022-07-27 09:34  孟冰er  阅读(40)  评论(0编辑  收藏  举报