File size: 5,803 Bytes
a1fb95c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
from src.core.tax_interface import BaseTaxEngine
from src.logger import logger

class UniversalTaxEngine(BaseTaxEngine):
    """
    A schema-driven tax engine that can represent any country's logic 
    based on a provided JSON configuration.
    """
    
    def __init__(self, country_name, currency_symbol, rules_path):
        self._country_name = country_name
        self._currency_symbol = currency_symbol
        self.rules_path = rules_path
        self.rules = {}
        self.current_year = "Universal"
        self.load_rules()

    def load_rules(self):
        if os.path.exists(self.rules_path):
            with open(self.rules_path, "r") as f:
                self.rules = json.load(f)
                self.current_year = self.rules.get("year", "Universal")
        else:
            logger.error(f"Rules not found at {self.rules_path}")

    @property
    def country_name(self) -> str:
        return self._country_name

    @property
    def currency_symbol(self) -> str:
        return self._currency_symbol

    def get_supported_years(self) -> list[str]:
        return [self.current_year]

    def set_tax_year(self, year: str) -> None:
        # For universal engines, we typically load a specific file
        pass

    def get_ui_tabs(self):
        """Generates UI tabs dynamically from deduction rules."""
        tabs = []
        deductions = self.rules.get("deduction_limits", {})
        
        # Categorize deductions into logical tabs
        investment_comps = []
        health_comps = []
        other_comps = []

        for key, value in deductions.items():
            comp = {"type": "number", "id": key, "label": f"Section {key}"}
            if "80C" in key: investment_comps.append(comp)
            elif "80D" in key: health_comps.append(comp)
            else: other_comps.append(comp)

        if investment_comps: tabs.append({"tab_name": "💰 Investments", "components": investment_comps})
        if health_comps: tabs.append({"tab_name": "🏥 Health", "components": health_comps})
        if other_comps: tabs.append({"tab_name": "🔍 Other", "components": other_comps})

        return tabs

    def calculate_slabs(self, income, regime="NEW"):
        tax = 0
        slabs = self.rules.get("tax_slabs", {}).get(regime.lower(), [])
        
        prev_limit = 0
        for slab in slabs:
            limit = slab.get("limit")
            rate = slab.get("rate", 0.0)
            
            if limit is None: # Top-most slab
                if income > prev_limit:
                    tax += (income - prev_limit) * rate
                break
            
            if income > prev_limit:
                tax_range = min(income, limit) - prev_limit
                tax += tax_range * rate
                prev_limit = limit
            else:
                break
        return tax

    def compute_tax_liability(self, financial_summary: dict, age: int, cash_inc: float = 0.0, hra_exemption: float = 0.0, user_inputs: dict = None) -> dict:
        user_inputs = user_inputs or {}
        
        # 1. Gross Income
        gross_income = sum(financial_summary.values()) + cash_inc
        
        # 2. Deductions
        total_deductions = 0
        deductions_breakdown = {}
        
        limits = self.rules.get("deduction_limits", {})
        for key, val in user_inputs.items():
            if key in limits:
                limit = limits[key]
                # Handle nested limits (like 80D) if they exist, else simple number
                if isinstance(limit, dict):
                    limit_val = limit.get("self", 25000) # Fallback
                else:
                    limit_val = limit
                
                actual_deduction = min(val, limit_val)
                total_deductions += actual_deduction
                deductions_breakdown[key] = actual_deduction

        # 3. Standard Deduction
        std_ded_old = self.rules.get("standard_deduction", {}).get("old_regime", 0)
        std_ded_new = self.rules.get("standard_deduction", {}).get("new_regime", 0)

        # 4. Net Taxable
        old_net = max(0, gross_income - total_deductions - std_ded_old - hra_exemption)
        new_net = max(0, gross_income - std_ded_new) # New regime usually has fewer deductions

        # 5. Slab Calculation
        old_tax = self.calculate_slabs(old_net, "OLD")
        new_tax = self.calculate_slabs(new_net, "NEW")

        # 6. Rebates, Surcharge, Cess
        rebate_rule = self.rules.get("rebate_87a", {})
        if old_net <= rebate_rule.get("old_regime", {}).get("limit", 0):
            old_tax = max(0, old_tax - rebate_rule.get("old_regime", {}).get("max_rebate", 0))
        if new_net <= rebate_rule.get("new_regime", {}).get("limit", 0):
            new_tax = max(0, new_tax - rebate_rule.get("new_regime", {}).get("max_rebate", 0))

        surcharge_rate = self.rules.get("surcharge", {}).get("rate", 0)
        surcharge_threshold = self.rules.get("surcharge", {}).get("threshold", 0)
        
        if old_net > surcharge_threshold: old_tax *= (1 + surcharge_rate)
        if new_net > surcharge_threshold: new_tax *= (1 + surcharge_rate)

        cess_rate = self.rules.get("cess_rate", 0)
        old_final = old_tax * (1 + cess_rate)
        new_final = new_tax * (1 + cess_rate)

        return {
            "gross_income": gross_income,
            "old_taxable": old_net,
            "new_taxable": new_net,
            "old_final_tax": old_final,
            "new_final_tax": new_final,
            "recommendations": [f"Standard optimization applied for {self.country_name}."],
            "deductions_breakdown": deductions_breakdown
        }

    def run_deduction_interview(self, age: int):
        # Interactive CLI mode (optional)
        return {}