python 个人所得税计算器

个人所得税计算器

很奇怪,为啥网上搜的各种个人所得税计算器那么难用,还不能精确的算出来,这不就是一个公式吗
下面这个是北京为例的,2024 年的个人所得税计算
刚在站内搜索了下,几乎没有几个能真实的计算个税,都没有考虑五险一金,还有专项抵扣等
下面这个代码是参考了社保的上下限,还有五险一金、专项抵扣等,代码中的缴纳比例填的都是默认值,默认最大的比例。可以精确的计算出员工的个人所得税信息
# -*- coding: utf-8 -*-
def calculate_social_security(social_base, month):
    """
    计算个人应缴纳的五险一金金额。
    """
    # 社保缴费基数上下限(下面根据北京市2024年的标准)
    if month <= 6:
        lower_limit = 6326  # 上半年下限
        upper_limit = 33891  # 上半年上限
    else:
        lower_limit = 6821   # 下半年下限
        upper_limit = 35283  # 下半年上限

    # 确定缴费基数
    if social_base < lower_limit:
        base = lower_limit
    elif social_base > upper_limit:
        base = upper_limit
    else:
        base = social_base

    # 计算各项费用
    pension = base * 0.08  # 养老保险
    medical = base * 0.02 + 3  # 医疗保险个人缴纳 + 大病医疗互助
    unemployment = base * 0.005  # 失业保险(0.5%)
    housing_fund = base * 0.12  # 住房公积金个人缴纳

    # 医保个人账户入账金额(一般为医疗保险费的30%)
    medical_account = medical

    # 住房公积金账户入账金额(个人 + 公司部分)
    housing_fund_total = housing_fund * 2  # 个人和公司各缴纳12%

    total = pension + medical + unemployment + housing_fund
    return {
        'pension': pension,
        'medical': medical,
        'unemployment': unemployment,
        'housing_fund': housing_fund,
        'total': total,
        'base': base,
        'medical_account': medical_account,
        'housing_fund_total': housing_fund_total
    }

def calculate_taxable_income(cumulative_gross, cumulative_deductions, month):
    """
    计算累计应纳税所得额。
    """
    taxable_income = cumulative_gross - cumulative_deductions - 5000 * month
    return taxable_income if taxable_income > 0 else 0

def calculate_income_tax(cumulative_taxable_income):
    """
    根据累计应纳税所得额计算累计应纳税额,并返回税额和适用税率。
    """
    tax_brackets = [
        (0, 36000, 0.03, 0),
        (36000, 144000, 0.10, 2520),
        (144000, 300000, 0.20, 16920),
        (300000, 420000, 0.25, 31920),
        (420000, 660000, 0.30, 52920),
        (660000, 960000, 0.35, 85920),
        (960000, float('inf'), 0.45, 181920),
    ]

    for bracket in tax_brackets:
        if bracket[0] < cumulative_taxable_income <= bracket[1]:
            tax = cumulative_taxable_income * bracket[2] - bracket[3]
            tax_rate = bracket[2]
            return tax, tax_rate
    # 超过960,000元的部分
    tax = cumulative_taxable_income * 0.45 - 181920
    tax_rate = 0.45
    return tax, tax_rate

def calculate_bonus_tax(bonus):
    """
    计算年终奖的个人所得税。
    """
    average_bonus = bonus / 12
    tax_brackets = [
        (0, 3000, 0.03, 0),
        (3000, 12000, 0.10, 210),
        (12000, 25000, 0.20, 1410),
        (25000, 35000, 0.25, 2660),
        (35000, 55000, 0.30, 4410),
        (55000, 80000, 0.35, 7160),
        (80000, float('inf'), 0.45, 15160),
    ]

    for bracket in tax_brackets:
        if bracket[0] < average_bonus <= bracket[1]:
            tax = bonus * bracket[2] - bracket[3]
            tax_rate = bracket[2]
            return tax, tax_rate
    tax = bonus * 0.45 - 15160  # 超过80,000元的部分
    tax_rate = 0.45
    return tax, tax_rate

def main():
    # 输入信息
    month = int(input("请输入当前月份(1-12):"))
    is_fixed_salary = int(input("是否固定工资?固定输入1,不固定输入0:"))
    if is_fixed_salary:
        gross_salary = float(input("请输入每月税前工资(元):"))
        salary_list = [gross_salary] * month
    else:
        salary_list = []
        for m in range(1, month + 1):
            salary = float(input(f"请输入第 {m} 月的税前工资(元):"))
            salary_list.append(salary)

    # 是否固定社保基数
    is_fixed_social_base = int(input("社保缴费基数是否固定?(社保基数一般是去年全年收入/12,但不超过当地社保上限)固定输入1,不固定输入0:"))
    if is_fixed_social_base:
        social_base = float(input("请输入社保缴费基数(元):"))
        social_base_list = [social_base] * month
    else:
        social_base_list = []
        for m in range(1, month + 1):
            sb = float(input(f"请输入第 {m} 月的社保缴费基数(元):"))
            social_base_list.append(sb)

    # 输入专项附加扣除
    child_education = int(input("子女教育(有输入1,无输入0):")) * 1000
    continuing_education = int(input("继续教育(有输入1,无输入0):")) * 400
    housing_loan_interest = int(input("住房贷款利息(有输入1,无输入0):")) * 1000
    housing_rent = int(input("住房租金(有输入1,无输入0):")) * 1500

    is_only_child = int(input("是否为独生子女(是输入1,否输入0):"))
    if is_only_child:
        elderly_support = 3000
    else:
        elderly_support = float(input("请输入您分摊的赡养老人金额(每月不超过1500元):"))

    # 计算每月专项附加扣除总额
    monthly_special_deductions = (child_education + continuing_education +
                                  housing_loan_interest + housing_rent + elderly_support)

    # 初始化累计变量
    cumulative_gross = 0
    cumulative_social_security = 0
    cumulative_deductions = 0
    previous_cumulative_tax = 0
    total_net_salary = 0  # 总税后工资
    total_medical_account = 0  # 医保账户总收入
    total_housing_fund_account = 0  # 公积金账户总收入
    total_medical_fund = 0  # 医疗保险个人缴纳部分

    print("\n---- 计算结果 ----")
    # 循环计算每个月的税额
    for m in range(1, month + 1):
        gross_salary = salary_list[m - 1]
        social_base = social_base_list[m - 1]

        # 累计收入
        cumulative_gross += gross_salary

        # 计算本月五险一金
        social_security = calculate_social_security(social_base, m)

        # 累计五险一金
        cumulative_social_security += social_security['total']

        # 累计专项附加扣除
        cumulative_deductions += monthly_special_deductions

        # 计算累计应纳税所得额
        cumulative_taxable_income = calculate_taxable_income(
            cumulative_gross,
            cumulative_social_security + cumulative_deductions,
            m
        )

        # 计算累计应纳税额和适用税率
        cumulative_tax, tax_rate = calculate_income_tax(cumulative_taxable_income)

        # 计算本期应预扣缴税额
        current_tax = cumulative_tax - previous_cumulative_tax

        # 计算税后工资
        net_salary = gross_salary - social_security['total'] - current_tax

        # 累计税后工资
        total_net_salary += net_salary

        # 累计医保账户入账金额
        total_medical_account += social_security['medical_account']

        # 累计公积金账户入账金额
        total_housing_fund_account += social_security['housing_fund_total']

        # 保存当前累计税额,供下个月使用
        previous_cumulative_tax = cumulative_tax

        # 输出每个月的结果
        print(f"\n第 {m} 月:")
        print(f"税前工资:{gross_salary:.2f} 元")
        print(f"五险一金个人缴纳:{social_security['total']:.2f} 元")
        print(f"专项附加扣除:{monthly_special_deductions:.2f} 元")
        print(f"累计应纳税所得额:{cumulative_taxable_income:.2f} 元")
        print(f"适用税率:{tax_rate*100:.0f}%")
        print(f"本期应预扣缴税额:{current_tax:.2f} 元")
        print(f"公积金账户本月入账(个人+公司):{social_security['housing_fund_total']:.2f} 元")
        print(f"医疗保险个人账户本月入账:{social_security['medical_account']:.2f} 元")
        print(f"税后工资:{net_salary:.2f} 元")
    print("\n注:每年的 7 月更新社保基数一般不体现在 7 月,会在 8 月完成调整。")

    # 年终奖计算
    bonus = float(input("\n请输入年终奖金额(元),如果没有请输入0:"))
    if bonus > 0:
        bonus_tax, bonus_tax_rate = calculate_bonus_tax(bonus)
        net_bonus = bonus - bonus_tax
        print(f"\n年终奖税额:{bonus_tax:.2f} 元")
        print(f"年终奖税后金额:{net_bonus:.2f} 元")
    else:
        bonus_tax = 0
        net_bonus = 0

    # 计算全年总税后收入
    total_net_income = total_net_salary + net_bonus + total_medical_account + total_housing_fund_account
    print(f"\n全年医保账户总收入:{total_medical_account:.2f} 元")
    print(f"全年公积金账户总收入:{total_housing_fund_account:.2f} 元")
    print(f"\n全年税后收入总计:{total_net_salary:.2f} 元")
    print(f"\n全年税后收入总计(含医保和公积金):{total_net_salary + total_medical_account + total_housing_fund_account:.2f} 元")
    print(f"\n全年税后收入总计(含年终奖,医保和公积金):{total_net_income:.2f} 元")

if __name__ == "__main__":
    main()





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值