下载地址:http://lanzou.com.cn/i92b53338

📁 output/zhinengaijisuanxitong/
├── 📄 README.md198 B
├── 📄 pom.xml1.6 KB
├── 📄 package.json696 B
├── 📄 src/main/java/Worker.java7.8 KB
├── 📄 contracts/Parser.py5.1 KB
├── 📄 router/Scheduler.js4 KB
├── 📄 table/Util.py4.6 KB
├── 📄 table/Adapter.cpp1.4 KB
├── 📄 src/main/java/Validator.java4.7 KB
├── 📄 config/Wrapper.xml1.5 KB
├── 📄 embedding/Controller.py4.5 KB
├── 📄 config/Converter.json696 B
├── 📄 auth/Handler.cpp1.5 KB
├── 📄 src/main/java/Service.java6.9 KB
├── 📄 router/Helper.js4 KB
├── 📄 src/main/java/Provider.java6.2 KB
├── 📄 embedding/Engine.js2.7 KB
├── 📄 lib/Queue.jar656 B
├── 📄 src/main/java/Client.java6.3 KB
├── 📄 router/Listener.py5.5 KB
├── 📄 embedding/Server.js4.5 KB
├── 📄 embedding/Resolver.py6.2 KB
├── 📄 auth/Executor.cpp1.5 KB
├── 📄 router/Builder.java6.4 KB
├── 📄 config/Buffer.xml1.3 KB
项目编译入口:
Project Structure
Project : 所得税智能AI计算系统
Folder : zhinengaijisuanxitong
Files : 26
Size : 90.4 KB
Generated: 2026-03-22 19:29:37
zhinengaijisuanxitong/
├── README.md [198 B]
├── auth/
│ ├── Executor.cpp [1.5 KB]
│ └── Handler.cpp [1.5 KB]
├── config/
│ ├── Buffer.xml [1.3 KB]
│ ├── Converter.json [696 B]
│ └── Wrapper.xml [1.5 KB]
├── contracts/
│ └── Parser.py [5.1 KB]
├── embedding/
│ ├── Controller.py [4.5 KB]
│ ├── Engine.js [2.7 KB]
│ ├── Resolver.py [6.2 KB]
│ └── Server.js [4.5 KB]
├── lib/
│ └── Queue.jar [656 B]
├── package.json [696 B]
├── pom.xml [1.6 KB]
├── router/
│ ├── Builder.java [6.4 KB]
│ ├── Helper.js [4 KB]
│ ├── Listener.py [5.5 KB]
│ └── Scheduler.js [4 KB]
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ ├── Client.java [6.3 KB]
│ │ │ ├── Provider.java [6.2 KB]
│ │ │ ├── Service.java [6.9 KB]
│ │ │ ├── Validator.java [4.7 KB]
│ │ │ └── Worker.java [7.8 KB]
│ │ └── resources/
│ └── test/
│ └── java/
└── table/
├── Adapter.cpp [1.4 KB]
└── Util.py [4.6 KB]
============================================================================
个人所得税智能计算引擎 - 基于Julia的完整实现
模块: 税率表定义、应纳税所得额计算、专项附加扣除、智能优化建议、API接口
版本: 2.0.0
作者: AI Assistant
描述: 本代码为所得税APP提供完整的后端计算逻辑,支持综合所得、年终奖、
股权激励等多种收入类型的智能计税与优化建议。
============================================================================
module TaxEngine
using Dates
using JSON
using HTTP
using LinearAlgebra
using Printf
using DataFrames
using Statistics
------------------------------ 1. 税率表定义 (2024年度) ---------------------------------
综合所得年度税率表 (累进税率)
const COMPREHENSIVE_TAX_TABLE = [
(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, typemax(Int64), 0.45, 181920)
]
年终奖单独计税税率表 (按月换算)
const BONUS_TAX_TABLE = [
(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, typemax(Int64), 0.45, 15160)
]
------------------------------ 2. 数据结构定义 ---------------------------------
"""
IndividualTaxPayer
个人所得税纳税人信息结构
"""
struct IndividualTaxPayer
id::String # 纳税人ID
name::String # 姓名
birth_date::Date # 出生日期
is_resident::Bool # 是否居民个人
basic_deduction::Float64 # 基本减除费用 (年度累计)
social_security_base::Float64 # 社保公积金基数
social_security_rate::Float64 # 社保公积金总费率
end
"""
IncomeDetail
收入明细结构
"""
struct IncomeDetail
income_type::String # 收入类型: "salary", "bonus", "equity", "labor", "royalty", "interest"
amount::Float64 # 金额
month::Int # 月份 (1-12)
year::Int # 年份
is_tax_free::Bool # 是否免税
tax_rate::Float64 # 预扣税率 (如果已知)
end
"""
SpecialDeduction
专项附加扣除结构
"""
struct SpecialDeduction
children_education::Float64 # 子女教育 (每月)
continuing_education::Float64 # 继续教育 (每月)
housing_loan::Float64 # 住房贷款利息 (每月)
housing_rent::Float64 # 住房租金 (每月)
elderly_support::Float64 # 赡养老人 (每月)
critical_illness::Float64 # 大病医疗 (年度)
end
"""
TaxCalculationResult
税务计算结果结构
"""
struct TaxCalculationResult
total_income::Float64 # 总收入
total_deductions::Float64 # 总扣除额
taxable_income::Float64 # 应纳税所得额
tax_liability::Float64 # 应纳税额
effective_tax_rate::Float64 # 有效税率
after_tax_income::Float64 # 税后收入
details::Dict{String, Any} # 详细计算过程
end
------------------------------ 3. 核心计算函数 ---------------------------------
"""
calculate_comprehensive_tax(income::Float64, deductions::Float64) -> Float64
计算综合所得个人所得税
"""
function calculate_comprehensive_tax(income::Float64, deductions::Float64)::Float64
taxable = max(0.0, income - deductions)
for (lower, upper, rate, quick_deduction) in COMPREHENSIVE_TAX_TABLE
if taxable > lower && taxable <= upper
return taxable * rate - quick_deduction
end
end
return 0.0
end
"""
calculate_bonus_tax(bonus_amount::Float64) -> Float64
计算年终奖单独计税 (除以12找税率)
"""
function calculate_bonus_tax(bonus_amount::Float64)::Float64
monthly = bonus_amount / 12.0
for (lower, upper, rate, quick_deduction) in BONUS_TAX_TABLE
if monthly > lower && monthly <= upper
return bonus_amount * rate - quick_deduction
end
end
return 0.0
end
"""
calculate_optimal_bonus_tax(salary_income::Float64, bonus::Float64,
total_deductions::Float64) -> Dict
智能优化: 比较年终奖单独计税与并入综合所得两种方式的税负,返回最优方案
"""
function calculate_optimal_bonus_tax(salary_income::Float64, bonus::Float64,
total_deductions::Float64)::Dict
# 方案1: 年终奖单独计税
salary_tax = calculate_comprehensive_tax(salary_income, total_deductions)
bonus_tax = calculate_bonus_tax(bonus)
total_tax_separate = salary_tax + bonus_tax
# 方案2: 并入综合所得
total_income = salary_income + bonus
total_tax_combined = calculate_comprehensive_tax(total_income, total_deductions)
if total_tax_separate <= total_tax_combined
return Dict(
"optimal_method" => "separate",
"tax_amount" => total_tax_separate,
"salary_tax" => salary_tax,
"bonus_tax" => bonus_tax,
"savings" => total_tax_combined - total_tax_separate
)
else
return Dict(
"optimal_method" => "combined",
"tax_amount" => total_tax_combined,
"combined_tax" => total_tax_combined,
"savings" => total_tax_separate - total_tax_combined
)
end
end
"""
calculate_special_deductions_annual(special::SpecialDeduction) -> Float64
计算年度专项附加扣除总额
"""
function calculate_special_deductions_annual(special::SpecialDeduction)::Float64
monthly_total = special.children_education +
special.continuing_education +
special.housing_loan +
special.housing_rent +
special.elderly_support
annual = monthly_total * 12 + special.critical_illness
return annual
end
"""
compute_individual_tax(payer::IndividualTaxPayer,
incomes::Vector{IncomeDetail},
special::SpecialDeduction) -> TaxCalculationResult
主计算函数: 根据纳税人信息和收入明细,计算最终税负
"""
function compute_individual_tax(payer::IndividualTaxPayer,
incomes::Vector{IncomeDetail},
special::SpecialDeduction)::TaxCalculationResult
# 分类汇总收入
salary_total = 0.0
bonus_total = 0.0
equity_total = 0.0
labor_total = 0.0
royalty_total = 0.0
other_total = 0.0
for inc in incomes
if inc.is_tax_free
continue
end
if inc.income_type == "salary"
salary_total += inc.amount
elseif inc.income_type == "bonus"
bonus_total += inc.amount
elseif inc.income_type == "equity"
equity_total += inc.amount
elseif inc.income_type == "labor"
# 劳务报酬按80%计入综合所得
labor_total += inc.amount * 0.8
elseif inc.income_type == "royalty"
# 稿酬所得按56%计入综合所得 (80% * 70%)
royalty_total += inc.amount * 0.56
else
other_total += inc.amount
end
end
# 综合所得总收入 (不含年终奖单独计税部分)
comprehensive_income = salary_total + labor_total + royalty_total +
equity_total + other_total
# 计算专项扣除 (社保公积金)
social_security_deduction = payer.social_security_base *
payer.social_security_rate * 12 # 年度
# 专项附加扣除
special_deduction_total = calculate_special_deductions_annual(special)
# 基本减除费用
basic_deduction = payer.basic_deduction
# 总扣除额
total_deductions = basic_deduction + social_security_deduction +
special_deduction_total
# 最优年终奖计税处理
bonus_opt = calculate_optimal_bonus_tax(comprehensive_income,
bonus_total,
total_deductions)
# 最终应纳税额
if bonus_opt["optimal_method"] == "separate"
final_tax = bonus_opt["tax_amount"]
# 此时综合所得部分已不含年终奖
taxable_income = max(0.0, comprehensive_income - total_deductions)
comprehensive_tax_part = calculate_comprehensive_tax(comprehensive_income,
total_deductions)
else
final_tax = bonus_opt["tax_amount"]
taxable_income = max(0.0, comprehensive_income + bonus_total - total_deductions)
comprehensive_tax_part = final_tax
end
total_income_all = comprehensive_income + bonus_total
after_tax_income = total_income_all - final_tax
effective_rate = total_income_all > 0 ? final_tax / total_income_all : 0.0
details = Dict(
"comprehensive_income" => comprehensive_income,
"bonus_amount" => bonus_total,
"basic_deduction" => basic_deduction,
"social_security_deduction" => social_security_deduction,
"special_deduction_total" => special_deduction_total,
"total_deductions" => total_deductions,
"taxable_income" => taxable_income,
"bonus_optimization" => bonus_opt,
"calculation_date" => Dates.format(now(), "yyyy-mm-dd HH:MM:SS")
)
return TaxCalculationResult(total_income_all, total_deductions,
taxable_income, final_tax,
effective_rate, after_tax_income, details)
end
------------------------------ 4. 智能优化与规划模块 ---------------------------------
"""
marginal_tax_rate_analysis(income::Float64, deductions::Float64) -> Dict
边际税率分析: 计算每增加一元收入所对应的边际税率
"""
function marginal_tax_rate_analysis(income::Float64, deductions::Float64)::Dict
taxable = max(0.0, income - deductions)
current_tax = calculate_comprehensive_tax(income, deductions)
# 计算增加1000元收入后的税负
increment = 1000.0
new_tax = calculate_comprehensive_tax(income + increment, deductions)
marginal_rate = (new_tax - current_tax) / increment
# 找到当前所在的税率档位
current_bracket = nothing
for (lower, upper, rate, _) in COMPREHENSIVE_TAX_TABLE
if taxable > lower && taxable <= upper
current_bracket = (lower, upper, rate)
break
end
end
return Dict(
"current_taxable_income" => taxable,
"current_tax" => current_tax,
"marginal_tax_rate" => marginal_rate,
"current_bracket_rate" => current_bracket !== nothing ? current_bracket[3] : 0.0,
"suggestion" => marginal_rate > 0.3 ? "建议考虑税收递延型养老或公益捐赠" : "收入增长税负合理"
)
end
"""
tax_planning_suggestions(payer::IndividualTaxPayer,
incomes::Vector{IncomeDetail},
special::SpecialDeduction) -> Vector{String}
生成个性化税务规划建议
"""
function tax_planning_suggestions(payer::IndividualTaxPayer,
incomes::Vector{IncomeDetail},
special::SpecialDeduction)::Vector{String}
result = compute_individual_tax(payer, incomes, special)
suggestions = String[]
# 建议1: 专项附加扣除优化
special_total = calculate_special_deductions_annual(special)
if special_total < 12000
push!(suggestions, "您的专项附加扣除较低,请检查是否有未申报的子女教育、房贷利息或赡养老人等项目。")
end
# 建议2: 年终奖筹划
bonus_opt = result.details["bonus_optimization"]
if bonus_opt["optimal_method"] == "combined"
push!(suggestions, "建议将年终奖单独计税,可节省税款约 $(round(bonus_opt["savings"], digits=2)) 元。")
end
# 建议3: 边际税率过高提醒
marginal = marginal_tax_rate_analysis(result.total_income, result.total_deductions)
if marginal["marginal_tax_rate"] > 0.35
push!(suggestions, "您的边际税率高达 $(round(marginal["marginal_tax_rate"]*100, digits=1))%,可考虑增加个人养老金缴存(每年上限12000元)实现税前抵扣。")
end
# 建议4: 社保基数合理性
if payer.social_security_base * 12 > result.total_income * 0.8
push!(suggestions, "您的社保公积金基数较高,虽然增加税前扣除,但需确认是否超出当地上限。")
end
if isempty(suggestions)
push!(suggestions, "您的税负结构较为合理,继续保持。")
end
return suggestions
end
------------------------------ 5. 批量计算与统计分析 ---------------------------------
"""
batch_tax_calculation(payers::Vector{IndividualTaxPayer},
incomes_list::Vector{Vector{IncomeDetail}},
specials::Vector{SpecialDeduction}) -> DataFrame
批量计算多个纳税人的税负,返回数据框用于分析
"""
function batch_tax_calculation(payers::Vector{IndividualTaxPayer},
incomes_list::Vector{Vector{IncomeDetail}},
specials::Vector{SpecialDeduction})::DataFrame
@assert length(payers) == length(incomes_list) == length(specials)
results = []
for i in 1:length(payers)
res = compute_individual_tax(payers[i], incomes_list[i], specials[i])
push!(results, (id=payers[i].id,
name=payers[i].name,
total_income=res.total_income,
total_deductions=res.total_deductions,
taxable_income=res.taxable_income,
tax_liability=res.tax_liability,
effective_rate=res.effective_tax_rate,
after_tax=res.after_tax_income))
end
return DataFrame(results)
end
"""
sensitivity_analysis(base_income::Float64, base_deductions::Float64,
income_range::Vector{Float64}) -> DataFrame
敏感性分析: 测试不同收入水平下的税负变化
"""
function sensitivity_analysis(base_income::Float64, base_deductions::Float64,
income_range::Vector{Float64})::DataFrame
results = []
for inc in income_range
tax = calculate_comprehensive_tax(inc, base_deductions)
after_tax = inc - tax
effective_rate = inc > 0 ? tax / inc : 0.0
push!(results, (income=inc, tax=tax, after_tax=after_tax,
effective_rate=effective_rate))
end
return DataFrame(results)
end
------------------------------ 6. API 服务层 (HTTP 接口) ---------------------------------
"""
start_api_server(port::Int=8080) -> Nothing
启动HTTP服务,提供RESTful API接口
"""
function start_api_server(port::Int=8080)
println("启动税务计算API服务,监听端口: $port")
# 路由定义
router = HTTP.Router()
# GET /health 健康检查
HTTP.@register(router, "GET", "/health", function(req)
return HTTP.Response(200, "OK", Dict("Content-Type" => "text/plain"),
body="Tax Engine is running")
end)
# POST /api/compute 单次计算
HTTP.@register(router, "POST", "/api/compute", function(req)
body = String(req.body)
data = JSON.parse(body)
# 解析请求JSON
payer_data = data["payer"]
payer = IndividualTaxPayer(
payer_data["id"],
payer_data["name"],
Date(payer_data["birth_date"]),
payer_data["is_resident"],
payer_data["basic_deduction"],
payer_data["social_security_base"],
payer_data["social_security_rate"]
)
incomes = []
for inc in data["incomes"]
push!(incomes, IncomeDetail(
inc["income_type"],
inc["amount"],
inc["month"],
inc["year"],
inc["is_tax_free"],
get(inc, "tax_rate", 0.0)
))
end
special = SpecialDeduction(
data["special"]["children_education"],
data["special"]["continuing_education"],
data["special"]["housing_loan"],
data["special"]["housing_rent"],
data["special"]["elderly_support"],
data["special"]["critical_illness"]
)
result = compute_individual_tax(payer, incomes, special)
suggestions = tax_planning_suggestions(payer, incomes, special)
response = Dict(
"status" => "success",
"result" => Dict(
"total_income" => result.total_income,
"total_deductions" => result.total_deductions,
"taxable_income" => result.taxable_income,
"tax_liability" => result.tax_liability,
"effective_tax_rate" => result.effective_tax_rate,
"after_tax_income" => result.after_tax_income,
"details" => result.details,
"suggestions" => suggestions
)
)
return HTTP.Response(200, JSON.json(response),
Dict("Content-Type" => "application/json"))
end)
# POST /api/batch 批量计算
HTTP.@register(router, "POST", "/api/batch", function(req)
body = String(req.body)
data = JSON.parse(body)
payers = []
incomes_list = []
specials = []
for item in data["batch"]
p = IndividualTaxPayer(
item["payer"]["id"],
item["payer"]["name"],
Date(item["payer"]["birth_date"]),
item["payer"]["is_resident"],
item["payer"]["basic_deduction"],
item["payer"]["social_security_base"],
item["payer"]["social_security_rate"]
)
push!(payers, p)
incs = []
for inc in item["incomes"]
push!(incs, IncomeDetail(
inc["income_type"],
inc["amount"],
inc["month"],
inc["year"],
inc["is_tax_free"],
0.0
))
end
push!(incomes_list, incs)
s = SpecialDeduction(
item["special"]["children_education"],
item["special"]["continuing_education"],
item["special"]["housing_loan"],
item["special"]["housing_rent"],
item["special"]["elderly_support"],
item["special"]["critical_illness"]
)
push!(specials, s)
end
df = batch_tax_calculation(payers, incomes_list, specials)
# 转换DataFrame为JSON
json_result = Dict(
"status" => "success",
"batch_results" => [Dict(col => row[col] for col in propertynames(row))
for row in eachrow(df)]
)
return HTTP.Response(200, JSON.json(json_result),
Dict("Content-Type" => "application/json"))
end)
# POST /api/optimize 优化建议
HTTP.@register(router, "POST", "/api/optimize", function(req)
body = String(req.body)
data = JSON.parse(body)
payer = IndividualTaxPayer(
data["payer"]["id"],
data["payer"]["name"],
Date(data["payer"]["birth_date"]),
data["payer"]["is_resident"],
data["payer"]["basic_deduction"],
data["payer"]["social_security_base"],
data["payer"]["social_security_rate"]
)
incomes = []
for inc in data["incomes"]
push!(incomes, IncomeDetail(
inc["income_type"],
inc["amount"],
inc["month"],
inc["year"],
inc["is_tax_free"],
0.0
))
end
special = SpecialDeduction(
data["special"]["children_education"],
data["special"]["continuing_education"],
data["special"]["housing_loan"],
data["special"]["housing_rent"],
data["special"]["elderly_support"],
data["special"]["critical_illness"]
)
suggestions = tax_planning_suggestions(payer, incomes, special)
return HTTP.Response(200, JSON.json(Dict("suggestions" => suggestions)),
Dict("Content-Type" => "application/json"))
end)
# 启动服务器
HTTP.serve(router, "0.0.0.0", port)
end
------------------------------ 7. 单元测试与示例 ---------------------------------
"""
run_tests() -> Nothing
运行内置测试用例验证核心计算逻辑
"""
function run_tests()
println("========== 运行税务计算引擎测试 ==========")
# 测试1: 综合所得计税
println("测试1: 综合所得计税")
tax1 = calculate_comprehensive_tax(150000, 60000)
@assert round(tax1, digits=2) == 150000.0 - 60000.0 == 90000.0 ?
(90000.0 * 0.10 - 2520) : 0.0
println("✓ 综合所得计税通过, 税额: $(tax1)")
# 测试2: 年终奖单独计税
println("测试2: 年终奖单独计税")
tax2 = calculate_bonus_tax(50000)
monthly = 50000/12 ≈ 4166.67
expected = 50000 * 0.10 - 210
@assert abs(tax2 - expected) < 0.01
println("✓ 年终奖计税通过, 税额: $(tax2)")
# 测试3: 完整纳税人计算
println("测试3: 完整纳税人计算")
payer = IndividualTaxPayer("001", "张三", Date(1990, 1, 1), true, 60000, 20000, 0.18)
incomes = [
IncomeDetail("salary", 25000, 1, 2024, false, 0.0),
IncomeDetail("salary", 25000, 2, 2024, false, 0.0),
IncomeDetail("salary", 25000, 3, 2024, false, 0.0),
IncomeDetail("salary", 25000, 4, 2024, false, 0.0),
IncomeDetail("salary", 25000, 5, 2024, false, 0.0),
IncomeDetail("salary", 25000, 6, 2024, false, 0.0),
IncomeDetail("salary", 25000, 7, 2024, false, 0.0),
IncomeDetail("salary", 25000, 8, 2024, false, 0.0),
IncomeDetail("salary", 25000, 9, 2024, false, 0.0),
IncomeDetail("salary", 25000, 10, 2024, false, 0.0),
IncomeDetail("salary", 25000, 11, 2024, false, 0.0),
IncomeDetail("salary", 25000, 12, 2024, false, 0.0),
IncomeDetail("bonus", 80000, 12, 2024, false, 0.0)
]
special = SpecialDeduction(1000, 400, 1000, 0, 2000, 0)
result = compute_individual_tax(payer, incomes, special)
println("总收入: $(result.total_income)")
println("应纳税额: $(result.tax_liability)")
println("税后收入: $(result.after_tax_income)")
println("有效税率: $(round(result.effective_tax_rate*100, digits=2))%")
# 测试4: 优化建议
println("\n测试4: 税务优化建议")
suggestions = tax_planning_suggestions(payer, incomes, special)
for (i, s) in enumerate(suggestions)
println("建议$i: $s")
end
# 测试5: 批量计算
println("\n测试5: 批量计算")
payers_batch = [payer, payer]
incomes_batch = [incomes, incomes]
specials_batch = [special, special]
df = batch_tax_calculation(payers_batch, incomes_batch, specials_batch)
println(df)
println("========== 所有测试通过 ==========")
end
------------------------------ 8. 命令行交互入口 ---------------------------------
function main()
println("="^60)
println("个人所得税智能计算引擎 (Julia版)")
println("="^60)
println("请选择功能:")
println("1. 运行内置测试")
println("2. 启动API服务器 (端口8080)")
println("3. 交互式计算")
println("4. 敏感性分析演示")
print("请输入选项 (1-4): ")
choice = readline()
if choice == "1"
run_tests()
elseif choice == "2"
start_api_server()
elseif choice == "3"
# 交互式输入简化示例
println("\n--- 交互式计算 ---")
print("年综合收入 (不含年终奖): ")
income = parse(Float64, readline())
print("年度专项附加扣除总额: ")
deductions = parse(Float64, readline())
tax = calculate_comprehensive_tax(income, deductions)
println("应纳税额: $tax")
println("税后收入: $(income - tax)")
elseif choice == "4"
println("\n--- 敏感性分析: 收入变化对税负的影响 ---")
base_ded = 60000
incomes_range = 50000:20000:500000
df = sensitivity_analysis(0.0, base_ded, collect(incomes_range))
println(df)
else
println("无效选项")
end
end
如果直接运行此文件,则启动主函数
if abspath(PROGRAM_FILE) == @FILE
main()
end
end # module TaxEngine