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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
| import json import pandas as pd from datetime import datetime from typing import Dict import os
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) ]
class SpecialDeductionManager: """专项附加扣除管理""" DEDUCTION_STANDARDS = { "子女教育": 2000, "继续教育": 400, "大病医疗": 0, "住房贷款利息": 1000, "住房租金": 1500, "赡养老人": 3000, "3岁以下婴幼儿照护": 2000 } def __init__(self, data_file="deductions.json"): self.data_file = data_file self.deductions = self.load_deductions() def load_deductions(self): if os.path.exists(self.data_file): try: with open(self.data_file, 'r', encoding='utf-8') as f: return json.load(f) except: pass return {} def save_deductions(self): with open(self.data_file, 'w', encoding='utf-8') as f: json.dump(self.deductions, f, ensure_ascii=False, indent=2) def add_deduction(self, category: str, details: str, amount: float = None): if category not in self.DEDUCTION_STANDARDS: print(f"❌ 无效类别: {category}") return False if amount is None: amount = self.DEDUCTION_STANDARDS[category] self.deductions[category] = { "金额": amount, "详情": details, "添加时间": datetime.now().strftime('%Y-%m-%d') } self.save_deductions() print(f"✅ 已添加 {category}: ¥{amount}/月") return True def remove_deduction(self, category: str): if category in self.deductions: del self.deductions[category] self.save_deductions() print(f"✅ 已删除 {category}") else: print(f"⚠️ 未找到 {category}") def list_deductions(self): if not self.deductions: print("📭 暂无扣除项") return {} total = 0 print("\n" + "=" * 45) print("专项附加扣除清单") print("=" * 45) for cat, info in self.deductions.items(): print(f"\n🏷️ {cat}:") print(f" 金额: ¥{info['金额']}/月") print(f" 详情: {info['详情']}") total += info['金额'] print(f"\n💡 合计: ¥{total}/月 (年度¥{total*12})") return self.deductions
class TaxYearCalculator: """年度个税计算器""" def __init__(self, deduction_manager: SpecialDeductionManager): self.dm = deduction_manager self.records = [] def add_monthly_record(self, month: int, income: float, insurance: float, tax_paid: float): self.records.append({ "月份": month, "收入": income, "五险一金": insurance, "已缴个税": tax_paid }) def generate_report(self): if not self.records: print("⚠️ 暂无数据") return print("\n" + "=" * 55) print("年度个税汇算清缴报告") print("=" * 55) df = pd.DataFrame(self.records) total_income = df['收入'].sum() total_insurance = df['五险一金'].sum() total_tax_paid = df['已缴个税'].sum() special_deductions = self.dm.list_deductions() total_special = sum(v['金额'] for v in special_deductions.values()) * 12 taxable_income = total_income - total_insurance - 60000 - total_special print(f"\n💰 收入情况:") print(f" 年度总收入: ¥{total_income:,.2f}") print(f" 年度五险一金: ¥{total_insurance:,.2f}") print(f" 专项附加扣除: ¥{total_special:,.2f}") print(f" 应纳税所得额: ¥{taxable_income:,.2f}") if taxable_income <= 0: print("🎉 无需缴纳个税") return for low, high, rate, deduction in TAX_BRACKETS: if low < taxable_income <= high: annual_tax = taxable_income * rate - deduction break print(f"\n💸 年度应缴个税: ¥{annual_tax:,.2f}") print(f"💵 年度已缴个税: ¥{total_tax_paid:,.2f}") diff = annual_tax - total_tax_paid if diff > 0: print(f"\n⚠️ 需要补税: ¥{diff:,.2f}") elif diff < 0: print(f"\n🎉 可以退税: ¥{abs(diff):,.2f}") else: print("\n✅ 无需补退") report_data = { "项目": ["年度总收入", "五险一金", "专项扣除", "基本减除", "应纳税所得额", "应缴个税", "已缴个税", "补税/退税"], "金额": [total_income, total_insurance, total_special, 60000, taxable_income, annual_tax, total_tax_paid, diff], "说明": ["", "", "", "", "", "", "", "正数补税,负数退税"] } report_df = pd.DataFrame(report_data) filename = f"个税汇算报告_{datetime.now().strftime('%Y%m%d')}.xlsx" report_df.to_excel(filename, index=False) print(f"\n✅ 报告已保存: {filename}")
def calculate_single_month(): """单月个税计算""" print("\n" + "=" * 40) print("📅 单月个税计算器") print("=" * 40) try: income = float(input("本月收入: ").strip()) insurance = float(input("本月五险一金: ").strip()) dm = SpecialDeductionManager() special = dm.list_deductions() months_passed = int(input("已过月份数: ").strip()) previous_tax = float(input("之前累计已缴个税: ").strip()) result = calculate_monthly_tax( income, insurance, special, months_passed, previous_tax ) print("\n" + "=" * 40) for key, value in result.items(): print(f"{key}: {value}") except Exception as e: print(f"❌ 输入错误: {e}")
def main(): dm = SpecialDeductionManager() while True: print("\n" + "=" * 50) print("个人税务助手 v1.0") print("=" * 50) print("1. 管理专项附加扣除") print("2. 计算单月个税") print("3. 录入年度工资数据") print("4. 生成年度汇算报告") print("5. 退出") print("=" * 50) choice = input("请选择功能: ").strip() if choice == "1": while True: print("\n" + "=" * 40) print("专项附加扣除管理") print("=" * 40) print("1. 添加扣除项") print("2. 删除扣除项") print("3. 查看扣除项") print("4. 返回上级") c = input("请选择: ").strip() if c == "1": cats = list(dm.DEDUCTION_STANDARDS.keys()) print("\n可选类别:") for i, cat in enumerate(cats, 1): print(f" {i}. {cat}") cat_name = input("\n类别名称: ").strip() if cat_name not in cats: print("❌ 类别错误!") continue details = input("详情: ").strip() custom = input("金额(回车使用标准): ").strip() if custom: try: amt = float(custom) dm.add_deduction(cat_name, details, amt) except: print("❌ 金额格式错误") else: dm.add_deduction(cat_name, details) elif c == "2": dm.list_deductions() cat = input("\n删除的类别: ").strip() dm.remove_deduction(cat) elif c == "3": dm.list_deductions() input("\n按回车继续...") elif c == "4": break elif choice == "2": calculate_single_month() input("\n按回车继续...") elif choice == "3": calc = TaxYearCalculator(dm) while True: print("\n录入月度数据(q退出)") month = input("月份: ").strip() if month == 'q': break try: m = int(month) inc = float(input("收入: ").strip()) ins = float(input("五险一金: ").strip()) tax = float(input("已缴个税: ").strip()) calc.add_monthly_record(m, inc, ins, tax) except Exception as e: print(f"❌ 错误: {e}") elif choice == "4": calc = TaxYearCalculator(dm) for record in calc.records: calc.add_monthly_record(record['月份'], record['收入'], record['五险一金'], record['已缴个税']) calc.generate_report() input("\n按回车继续...") elif choice == "5": print("👋 再见!") break else: print("请输入1-5!")
if __name__ == "__main__": main()
|