计算税前工资
累加计税,指定税后(到手)工资,支持五险一金和附加扣除每月定制、中间离职再入职等情况,误差控制在1元内。
代码:
import openpyxl
def calculate_tax(taxable_income, year_tax):
result = 0.00
if taxable_income <= 36000.00:
result = taxable_income * 0.03 - year_tax
elif taxable_income <= 144000.00:
result = taxable_income * 0.10 - 2520.00 - year_tax
elif taxable_income <= 300000.00:
result = taxable_income * 0.20 - 16920.00 - year_tax
elif taxable_income <= 420000.00:
result = taxable_income * 0.25 - 31920.0 - year_tax
elif taxable_income <= 660000.00:
result = taxable_income * 0.30 - 52920.0 - year_tax
elif taxable_income <= 960000.00:
result = taxable_income * 0.35 - 85920.0 - year_tax
return max(result-0.10, 0.00)
# keep origin data(include formula)
origin_workbook = openpyxl.load_workbook('/home/zyd/file/excel/税前工资计算.xlsx')
origin_sheet = origin_workbook['05']
# read numerical value
workbook = openpyxl.load_workbook('/home/zyd/file/excel/税前工资计算.xlsx', data_only=True)
sheet = workbook['05']
for row in sheet.iter_rows():
row_index = row[0].row
# skip headline
if row_index < 2 or row[1].value is None:
continue
# read necessary data
month_salary_after_tax = row[2].value
year_salary_before_tax = row[3].value
month_count = row[4].value
year_social_insurance = row[5].value
month_social_insurance = row[6].value
year_special_deduction = row[7].value
month_special_deduction = row[8].value
year_tax = row[9].value
# start from 5000.00
month_salary_before_tax = 5000.00
basic_salary = 5000.00
year_taxable_income = year_salary_before_tax - basic_salary*month_count - year_social_insurance - month_social_insurance - year_special_deduction - month_special_deduction
tax = calculate_tax(year_taxable_income+month_salary_before_tax, year_tax)
# check until reach the condition
while month_salary_before_tax - tax - month_social_insurance - month_salary_after_tax < -1.00:
month_salary_before_tax += 2.00
tax = calculate_tax(year_taxable_income+month_salary_before_tax, year_tax)
# set the tax
origin_sheet.cell(row_index, 11, tax)
# save the result
origin_workbook.save('/home/zyd/file/excel/税前工资计算.xlsx')
origin_workbook.close()
workbook.close()
表格数据示例(五月开始入职):
excel文件下载地址:
https://download.csdn.net/download/chunzhenzyd/87817773
验证: