"""SA-006 UK tax tools (owner YES 28 Nov 2026 20:14 UK). Plain code with the rates read from tax_rates.json, so a Budget change means editing that file, not retraining. Covers VAT, stamp duty (England & NI), income tax (not Scotland), National Insurance, corporation tax, and a rates lookup.""" import json, os RATES = json.load(open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "tax_rates.json"))) def _m(x): return f"£{x:,.2f}" def _num(x): if isinstance(x, (int, float)): return float(x) s = str(x).lower().replace("£", "").replace(",", "").strip() mult = 1 if s.endswith("k"): mult, s = 1000, s[:-1] elif s.endswith("m"): mult, s = 1_000_000, s[:-1] return float(s) * mult def _slices(amount, bands, extra=0.0): out, lo, tot = [], 0.0, 0.0 for top, rate in bands: hi = amount if top is None else min(amount, top) if hi > lo: t = (hi - lo) * (rate + extra); tot += t; out.append(f"{rate + extra:.0%} on {_m(hi - lo)} = {_m(t)}") if top is None or amount <= top: break lo = top return tot, out TAX_TOOLS = [ {"type": "function", "function": {"name": "vat", "description": "UK VAT on an amount. mode 'add' (amount is before VAT) or 'remove' (amount already includes VAT).", "parameters": {"type": "object", "properties": {"amount": {"type": "number"}, "mode": {"type": "string", "enum": ["add", "remove"]}, "rate": {"type": "string", "enum": ["standard", "reduced", "zero"]}}, "required": ["amount"]}}}, {"type": "function", "function": {"name": "stamp_duty", "description": "Stamp Duty Land Tax on a home in England or Northern Ireland.", "parameters": {"type": "object", "properties": {"price": {"type": "number"}, "buyer": {"type": "string", "enum": ["standard", "first_time", "additional"]}, "non_resident": {"type": "boolean"}}, "required": ["price"]}}}, {"type": "function", "function": {"name": "income_tax", "description": "UK income tax for a year (England, Wales, Northern Ireland) on total taxable income before the Personal Allowance.", "parameters": {"type": "object", "properties": {"income": {"type": "number"}}, "required": ["income"]}}}, {"type": "function", "function": {"name": "national_insurance", "description": "Yearly National Insurance: 'employee' (Class 1 on salary) or 'self_employed' (Class 4 on profits).", "parameters": {"type": "object", "properties": {"income": {"type": "number"}, "kind": {"type": "string", "enum": ["employee", "self_employed"]}}, "required": ["income"]}}}, {"type": "function", "function": {"name": "corporation_tax", "description": "UK corporation tax on a company's yearly taxable profit (includes marginal relief).", "parameters": {"type": "object", "properties": {"profit": {"type": "number"}}, "required": ["profit"]}}}, {"type": "function", "function": {"name": "tax_rates", "description": "Current UK rates and thresholds. topic: vat, income_tax, national_insurance, stamp_duty, corporation_tax, dividends, sole_trader_vs_limited.", "parameters": {"type": "object", "properties": {"topic": {"type": "string"}}, "required": ["topic"]}}}, ] def vat(amount, mode="add", rate="standard"): a = _num(amount); r = RATES["vat"][rate or "standard"] if (mode or "add") == "remove": net = round(a / (1 + r), 2); v = round(a - net, 2) return f"VAT at {r:.0%} included in {_m(a)}: VAT {_m(v)}, price before VAT {_m(net)}" v = round(a * r, 2) return f"VAT at {r:.0%} on {_m(a)}: VAT {_m(v)}, total {_m(a + v)}" def stamp_duty(price, buyer="standard", non_resident=False): p = _num(price); S = RATES["stamp_duty"]; buyer = buyer or "standard" extra = S["non_resident_surcharge"] if non_resident else 0.0 note = "" if buyer == "first_time" and p <= S["first_time"]["max_price"]: bands = S["first_time"]["bands"] + [[None, 0.0]] else: bands = S["standard"] if buyer == "first_time": note = " (first-time buyer relief does not apply over £500,000)" if buyer == "additional" and p >= S["additional_min_price"]: extra += S["additional_surcharge"] tot, parts = _slices(p, bands, extra) tot = float(int(tot + 1e-9)) # SDLT is rounded down to whole pounds return f"Stamp duty on {_m(p)} ({buyer.replace('_', ' ')} buyer{', non-UK resident' if non_resident else ''}){note}: total {_m(tot)}. " + "; ".join(parts) def income_tax(income): i = _num(income); T = RATES["income_tax"] pa = max(0.0, T["personal_allowance"] - max(0.0, (i - T["allowance_taper_start"]) // 2)) taxable = max(0.0, i - pa); tot, parts = _slices(taxable, T["bands"]); tot = round(tot, 2) return f"Income tax on {_m(i)}: Personal Allowance {_m(pa)}, taxable {_m(taxable)}, total tax {_m(tot)}. " + "; ".join(parts) def national_insurance(income, kind="employee"): i = _num(income); kind = kind or "employee"; N = RATES["national_insurance"]["employee" if kind == "employee" else "self_employed_class4"] main = max(0.0, min(i, N["upper"]) - N["lower"]) * N["main_rate"]; up = max(0.0, i - N["upper"]) * N["upper_rate"]; tot = round(main + up, 2) label = "Class 1 employee" if kind == "employee" else "Class 4 self-employed" return f"{label} National Insurance on {_m(i)}: total {_m(tot)} ({N['main_rate']:.0%} between {_m(N['lower'])} and {_m(N['upper'])} = {_m(main)}; {N['upper_rate']:.0%} above = {_m(up)})" def corporation_tax(profit): p = _num(profit); C = RATES["corporation_tax"] if p <= C["lower_limit"]: tot = p * C["small_profits_rate"]; how = f"small profits rate {C['small_profits_rate']:.0%}" elif p >= C["upper_limit"]: tot = p * C["main_rate"]; how = f"main rate {C['main_rate']:.0%}" else: a, b = C["marginal_fraction"]; mr = (C["upper_limit"] - p) * a / b; tot = p * C["main_rate"] - mr how = f"main rate {C['main_rate']:.0%} ({_m(p * C['main_rate'])}) less marginal relief {_m(mr)}" return f"Corporation tax on profit of {_m(p)}: total {_m(round(tot, 2))} ({how})" def tax_rates(topic): t = str(topic).lower().replace(" ", "_") R = RATES; y = R["tax_year"] if "vat" in t: v = R["vat"]; return f"VAT {y}: standard {v['standard']:.0%}, reduced {v['reduced']:.0%}, zero 0%. Must register when taxable turnover over any rolling 12 months goes over {_m(v['registration_threshold'])}; can deregister below {_m(v['deregistration_threshold'])}." if "income" in t: return f"Income tax {y} (England, Wales, NI): Personal Allowance £12,570 (reduced by £1 for every £2 over £100,000, gone at £125,140); basic 20% on the first £37,700 of taxable income, higher 40% up to £125,140, additional 45% above. Scotland has its own rates." if "national" in t or t == "ni": return f"National Insurance {y}: employees 8% on earnings £12,570 to £50,270 and 2% above; self-employed Class 4 6% on profits £12,570 to £50,270 and 2% above; employers 15% above £5,000." if "stamp" in t or "sdlt" in t: return f"Stamp duty {y} (England and NI): 0% to £125,000, 2% to £250,000, 5% to £925,000, 10% to £1.5m, 12% above. First-time buyers: 0% to £300,000, 5% to £500,000, no relief over £500,000. Additional homes +5% on every band; non-UK residents +2%. Scotland has LBTT and Wales has LTT." if "corp" in t: return f"Corporation tax {y}: 19% on profits up to £50,000, 25% over £250,000, marginal relief in between (fraction 3/200)." if "divid" in t: return f"Dividends {y}: £500 allowance, then 10.75% (basic), 35.75% (higher), 39.35% (additional)." if "sole" in t or "limited" in t or "ltd" in t: return (f"Sole trader vs limited company {y}: a sole trader pays income tax (20/40/45%) and Class 4 NI (6% then 2%) on profits. " "A limited company pays corporation tax (19% to 25%) on profits; owners then pay themselves salary (income tax + NI) and/or dividends (£500 allowance, then 10.75/35.75/39.35%). Companies have more admin and accounts to file.") return "error: topic must be one of vat, income_tax, national_insurance, stamp_duty, corporation_tax, dividends, sole_trader_vs_limited" FUNCS = {"vat": vat, "stamp_duty": stamp_duty, "income_tax": income_tax, "national_insurance": national_insurance, "corporation_tax": corporation_tax, "tax_rates": tax_rates}