"""End-to-end: book a freelancer's month into the ledger, then let the engine read the real aggregates and compute what's owed. No model — just storage + math. Run from the project root: python -m scripts.demo_ledger """ import sys try: sys.stdout.reconfigure(encoding="utf-8") except (AttributeError, ValueError): pass from src.engine import ( isr_provisional_monthly, iva_monthly, profit_margin, resico_isr_monthly, taxable_base, ) from src.ledger import Ledger def rule(title): print("\n" + "═" * 64 + f"\n{title}\n" + "═" * 64) def main(): lg = Ledger(":memory:") # in real use: data/user/ledger.db # --- A month of activity for a freelance designer (May 2024) ---------- lg.record_income("2024-05-03", "Branding — Café Luna", 18000, iva_rate="0.16") lg.record_income("2024-05-17", "Sitio web — Dental MX", 27000, iva_rate="0.16", isr_retenido="2700", iva_retenido="2880") # invoiced a persona moral lg.record_expense("2024-05-05", "Adobe Creative Cloud", 1300, iva_rate="0.16") lg.record_expense("2024-05-12", "Renta de oficina (parte)", 4000, iva_rate="0.16") lg.record_expense("2024-05-22", "Comida no deducible", 600, iva_rate="0.16", deductible=False) rule("LEDGER · May 2024 — what got booked") m = lg.month_totals(2024, 5) print(f"Income (CFDI-backed): {m.income}") print(f"Deductible expenses: {m.deductible_expenses}") print(f"Non-deductible expenses: {m.nondeductible_expenses}") print(f"IVA trasladado (cobrado): {m.iva_trasladado}") print(f"IVA acreditable (pagado): {m.iva_acreditable}") print(f"IVA retenido by clients: {m.iva_retenido}") print(f"ISR retenido by clients: {m.isr_retenido}") rule("TAXES · computed from the real ledger (RESICO vs general)") print(resico_isr_monthly(m.income).explain()) print() base = taxable_base(m.income, m.deductible_expenses) print(f"General-regime taxable base = {m.income} − {m.deductible_expenses} = {base}") print(isr_provisional_monthly(base).explain()) print() print(iva_monthly(m.iva_trasladado, m.iva_acreditable, m.iva_retenido).explain()) rule("STATEMENTS") s = lg.income_statement(2024, 5) print(f"Income statement {s.period}: revenue {s.revenue} − expenses {s.expenses} " f"= net {s.net_profit}") print(profit_margin(s.net_profit, s.revenue).explain()) bs = lg.balance_sheet("2024-05-31") print(f"Balance sheet @ {bs.as_of}: assets {bs.assets} = liabilities " f"{bs.liabilities} + equity {bs.equity}") lg.close() print("\nThe books balance, and every tax figure was derived from booked " "transactions — not typed in by hand.") if __name__ == "__main__": main()