使用Python计算个人所得税(累计预扣法)

一、引言

在中国,个人所得税的计算遵循累计预扣法,即根据纳税人一个纳税年度内截至当前月份累计取得的工资薪金所得收入额,减去累计免税收入、累计减除费用(5000元/月)、累计专项扣除、累计专项附加扣除和依法确定的其他扣除后的余额为应纳税所得额,对照综合所得税率表,计算出累计应预扣预缴税额,减除已预扣预缴税额后的余额,作为本期应预扣预缴税额。本文将通过一个Python函数来演示如何计算给定每个月度收入的个人所得税,同时考虑五险一金固定缴纳和专项扣除费用的变化(从10月份开始)。

二、代码解释

以下是一个Python函数calculate_monthly_personal_income_tax_cumulative,它接收每个月的收入、五险一金固定缴纳金额、住房公积金固定缴纳金额、10月份前后的专项扣除费用以及一个具体月份N作为参数,返回每个月应缴个人所得税的列表、累计应纳税所得额和累计已缴税额。

def calculate_monthly_personal_income_tax_cumulative(monthly_incomes, social_insurance_fixed, housing_fund_fixed,
                                                     special_deductions_before, special_deductions_after, N):
    # 定义税率表和速算扣除数
    tax_brackets = [
        (36000, 0.03, 0),
        (144000, 0.10, 2520),
        (300000, 0.20, 16920),
        (420000, 0.25, 31920),
        (660000, 0.30, 52920),
        (960000, 0.35, 85920),
        (float('inf'), 0.45, 181920)
    ]

    # 初始化变量
    monthly_taxes = []
    cumulative_taxable_income = 0
    cumulative_tax_paid = 0

    # 遍历每个月的收入并计算税额
    for month, monthly_income in enumerate(monthly_incomes, start=1):
        # 根据月份选择专项扣除费用
        special_deductions = special_deductions_before if month < N else special_deductions_after

        # 计算应纳税所得额
        taxable_income = monthly_income - social_insurance_fixed - housing_fund_fixed - special_deductions - 5000

        # 累计应纳税所得额
        cumulative_taxable_income += taxable_income

        # 计算累计应纳税额
        cumulative_tax = 0
        previous_limit = 0
        for upper_limit, rate, quick_deduction in tax_brackets:
            if cumulative_taxable_income <= upper_limit:
                cumulative_tax = cumulative_taxable_income * rate - quick_deduction
                break
            else:
                tax_for_bracket = (min(cumulative_taxable_income, upper_limit) - previous_limit) * rate
                cumulative_tax += tax_for_bracket
                previous_limit = upper_limit

        # 计算当前月份应缴税额(累计应纳税额减去之前月份已缴税额)
        current_tax = cumulative_tax - cumulative_tax_paid

        # 更新累计已缴税额
        cumulative_tax_paid = cumulative_tax

        # 确保税额不为负数,并添加到列表中
        monthly_taxes.append(max(current_tax, 0))

    return monthly_taxes, cumulative_taxable_income, cumulative_tax_paid

三、使用示例

下面是如何使用这个函数的一个示例,包括每个月的收入列表、五险一金固定缴纳金额、住房公积金固定缴纳金额、10月份前后的专项扣除费用以及具体月份N。

if __name__ == '__main__':
    monthly_incomes = [30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000]
    social_insurance_fixed = 1566
    housing_fund_fixed = 400
    N = 13
    special_deductions_before = 1500
    special_deductions_after = 4500

    monthly_taxes, cumulative_taxable_income, cumulative_tax_paid = calculate_monthly_personal_income_tax_cumulative(
        monthly_incomes, social_insurance_fixed, housing_fund_fixed, special_deductions_before,
        special_deductions_after, N)

    total = 0
    for i, tax in enumerate(monthly_taxes):
        print(f"月度收入为{monthly_incomes[i]}元时,累计应纳税所得额{cumulative_taxable_income:.2f}元,累计已缴税额{cumulative_tax_paid:.2f}元,当前月份应缴个人所得税为{tax:.2f}元;"
              +(f"(注:从第{N}月开始,专项扣除费用由{special_deductions_before}元变为{special_deductions_after}元。)" if i >= N - 1 else ""))
        total = total + tax

    total_income = sum(monthly_incomes)
    print(f"\033[34m年度总收入为{total_income}元,年缴纳个人所得税为{round(total,2)}元,实际年到手收入为{total_income-12*(social_insurance_fixed+housing_fund_fixed)-round(total,2)}元。\033[0m")

运行结果:

C:\Users\LTCH\AppData\Local\Programs\Python\Python39\python.exe D:/python_demo/24、个税/个税.py
月度收入为30000元时,累计应纳税所得额258408.00元,累计已缴税额34761.60元,当前月份应缴个人所得税为646.02元;
月度收入为30000元时,累计应纳税所得额258408.00元,累计已缴税额34761.60元,当前月份应缴个人所得税为1140.78元;
月度收入为30000元时,累计应纳税所得额258408.00元,累计已缴税额34761.60元,当前月份应缴个人所得税为2153.40元;
月度收入为30000元时,累计应纳税所得额258408.00元,累计已缴税额34761.60元,当前月份应缴个人所得税为2153.40元;
月度收入为30000元时,累计应纳税所得额258408.00元,累计已缴税额34761.60元,当前月份应缴个人所得税为2153.40元;
月度收入为30000元时,累计应纳税所得额258408.00元,累计已缴税额34761.60元,当前月份应缴个人所得税为2153.40元;
月度收入为30000元时,累计应纳税所得额258408.00元,累计已缴税额34761.60元,当前月份应缴个人所得税为2827.20元;
月度收入为30000元时,累计应纳税所得额258408.00元,累计已缴税额34761.60元,当前月份应缴个人所得税为4306.80元;
月度收入为30000元时,累计应纳税所得额258408.00元,累计已缴税额34761.60元,当前月份应缴个人所得税为4306.80元;
月度收入为30000元时,累计应纳税所得额258408.00元,累计已缴税额34761.60元,当前月份应缴个人所得税为4306.80元;
月度收入为30000元时,累计应纳税所得额258408.00元,累计已缴税额34761.60元,当前月份应缴个人所得税为4306.80元;
月度收入为30000元时,累计应纳税所得额258408.00元,累计已缴税额34761.60元,当前月份应缴个人所得税为4306.80元;
年度总收入为360000元,年缴纳个人所得税为34761.6元,实际年到手收入为301646.4元。

四、结论

通过上述Python函数,我们可以方便地计算每个月应缴的个人所得税,同时考虑五险一金固定缴纳和专项扣除费用的变化。这不仅有助于个人合理规划财务,还能提高税务计算的准确性和效率。希望这篇文章和代码示例对你有所帮助!

五、附件(完整代码)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024-10-28 16:51
# @Author : Leuanghing Chen
# @Blog : https://blog.csdn.net/weixin_46153372?spm=1010.2135.3001.5421
# @File : 个税.py
# @Software : PyCharm

def calculate_monthly_personal_income_tax_cumulative(monthly_incomes, social_insurance_fixed, housing_fund_fixed,
                                                     special_deductions_before, special_deductions_after, N):
    """
    计算给定每个月度收入的个人所得税(采用累计预扣法),考虑五险一金固定缴纳和专项扣除费用的变化(从10月份开始)。

    :param monthly_incomes: 包含每个月收入的列表(元)
    :param social_insurance_fixed: 五险一金固定缴纳金额(元)
    :param housing_fund_fixed: 住房公积金固定缴纳金额(元)
    :param special_deductions_before: 10月份之前的专项扣除费用(元)
    :param special_deductions_after: 10月份及之后的专项扣除费用(元)
    :param N: 具体月份
    :return: 包含每个月应缴个人所得税的列表(元)
    """
    # 定义税率表和速算扣除数
    tax_brackets = [
        (36000, 0.03, 0),
        (144000, 0.10, 2520),
        (300000, 0.20, 16920),
        (420000, 0.25, 31920),
        (660000, 0.30, 52920),
        (960000, 0.35, 85920),
        (float('inf'), 0.45, 181920)
    ]

    # 初始化变量
    monthly_taxes = []
    cumulative_taxable_income = 0
    cumulative_tax_paid = 0

    # 遍历每个月的收入并计算税额
    for month, monthly_income in enumerate(monthly_incomes, start=1):
        # 根据月份选择专项扣除费用
        special_deductions = special_deductions_before if month < N else special_deductions_after

        # 计算应纳税所得额
        taxable_income = monthly_income - social_insurance_fixed - housing_fund_fixed - special_deductions - 5000  # 5000为起征点

        # 累计应纳税所得额
        cumulative_taxable_income += taxable_income

        # 计算累计应纳税额
        cumulative_tax = 0
        previous_limit = 0
        for upper_limit, rate, quick_deduction in tax_brackets:
            if cumulative_taxable_income <= upper_limit:
                cumulative_tax = cumulative_taxable_income * rate - quick_deduction
                break
            else:
                tax_for_bracket = (min(cumulative_taxable_income, upper_limit) - previous_limit) * rate
                cumulative_tax += tax_for_bracket
                previous_limit = upper_limit

                # 计算当前月份应缴税额(累计应纳税额减去之前月份已缴税额)
        current_tax = cumulative_tax - cumulative_tax_paid

        # 更新累计已缴税额
        cumulative_tax_paid = cumulative_tax

        # 确保税额不为负数,并添加到列表中
        monthly_taxes.append(max(current_tax, 0))

    return monthly_taxes, cumulative_taxable_income, cumulative_tax_paid


if __name__ == '__main__':
    monthly_incomes = [30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000,30000]  # 每个月的收入列表
    social_insurance_fixed = 1566  # 五险一金固定缴纳金额
    housing_fund_fixed = 400  # 住房公积金固定缴纳金额
    N = 13  # N=13时,专项扣除费用不变更
    special_deductions_before = 1500  # N月份之前的专项扣除费用
    special_deductions_after = 4500  # N月份及之后的专项扣除费用

    monthly_taxes, cumulative_taxable_income, cumulative_tax_paid = calculate_monthly_personal_income_tax_cumulative(
        monthly_incomes, social_insurance_fixed, housing_fund_fixed, special_deductions_before,
        special_deductions_after, N)

    total = 0
    # 打印信息
    for i, tax in enumerate(monthly_taxes):
        print(f"月度收入为{monthly_incomes[i]}元时,累计应纳税所得额{cumulative_taxable_income:.2f}元,累计已缴税额{cumulative_tax_paid:.2f}元,当前月份应缴个人所得税为{tax:.2f}元;"
              +(f"(注:从第{N}月开始,专项扣除费用由{special_deductions_before}元变为{special_deductions_after}元。)" if i >= N - 1 else ""))
        total = total + tax

    # 年度总收入
    total_income = sum(monthly_incomes)
    print(f"\033[34m年度总收入为{total_income}元,年缴纳个人所得税为{round(total,2)}元,实际年到手收入为{total_income-12*(social_insurance_fixed+housing_fund_fixed)-round(total,2)}元。\033[0m")
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值