askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
trial_balance.py · 5.8 KB · 155 lines
| 1 | """Trial balance report page: the one report v1 of this package ships |
| 2 | (reconciled against a private predecessor-system audit, not part of this |
| 3 | repo, whose own reports "filter posted lines" -- profit & loss, balance |
| 4 | sheet, and cash flow are the same fold shape and are DEFERRED, see |
| 5 | dbbasic-package.json's description). |
| 6 | |
| 7 | Unlike every other page in this package, this page computes its data |
| 8 | SERVER-SIDE by calling object_finance.trial_balance() directly in |
| 9 | GET(request), rather than having the browser fetch /collections/* |
| 10 | records and fold them in JS. That is a deliberate choice ("pick simpler, |
| 11 | report which" -- this is simpler): a trial balance is a fold over every |
| 12 | posted journal's lines across the whole owner, not a single record's |
| 13 | detail, so there is no natural single collection endpoint for the |
| 14 | browser to call the way invoice_view.py fetches one invoice and its |
| 15 | lines. Computing it here, once, and rendering plain HTML rows avoids |
| 16 | needing a bespoke API object (a fifth object in this package) just to |
| 17 | re-expose the same fold object_finance.py already provides. |
| 18 | |
| 19 | Trust boundary, stated explicitly: object_finance.trial_balance() reads |
| 20 | fin_journals/fin_journal_lines/fin_accounts directly via object_records, |
| 21 | which is NOT subject to permissions/rules.json's row_filter |
| 22 | owner_id=$user_id the way the HTTP /collections/* API is (see |
| 23 | object_finance.trial_balance's own docstring). This handler passes |
| 24 | owner=user_id explicitly on every call, which is what keeps this report |
| 25 | scoped to the signed-in visitor's own books -- removing that argument |
| 26 | would leak every owner's journals into one report. There is no |
| 27 | anonymous or cross-owner path here at all: an anonymous visitor gets a |
| 28 | sign-in prompt and object_finance is never even imported for that |
| 29 | request. |
| 30 | |
| 31 | Money is formatted for display only; the stored value stays an integer |
| 32 | number of cents everywhere else. |
| 33 | """ |
| 34 | from __future__ import annotations |
| 35 | |
| 36 | import os |
| 37 | |
| 38 | _STYLE = """ |
| 39 | table.tb { width: 100%; border-collapse: collapse; margin: 1rem 0; } |
| 40 | table.tb th, table.tb td { text-align: left; padding: 0.4rem 0.6rem; border-bottom: 1px solid var(--line, #38384a); } |
| 41 | table.tb th { font-weight: 600; } |
| 42 | table.tb td.num, table.tb th.num { text-align: right; } |
| 43 | table.tb tr.totals td { font-weight: 700; border-top: 2px solid var(--line, #38384a); } |
| 44 | """ |
| 45 | |
| 46 | DATA_DIR_ENV = "DBBASIC_DATA_DIR" |
| 47 | |
| 48 | |
| 49 | def _data_dir() -> str: |
| 50 | # Same pattern as packages/app-invoices/objects/system/invoice_totals.py's |
| 51 | # own _data_dir(): standalone, reads os.environ directly rather than |
| 52 | # depending on object_server for a base_dir. object_finance is a plain |
| 53 | # library import (like object_records itself), not a registered |
| 54 | # DBBASIC object, so there is no request payload to read a base_dir |
| 55 | # override from. |
| 56 | import object_records |
| 57 | return os.environ.get(DATA_DIR_ENV, object_records.DEFAULT_DATA_DIR) |
| 58 | |
| 59 | |
| 60 | def _money(cents_value, currency: str = "USD") -> str: |
| 61 | cents = int(cents_value or 0) |
| 62 | sign = "-" if cents < 0 else "" |
| 63 | amount = abs(cents) |
| 64 | return f"{sign}{currency} {amount // 100}.{amount % 100:02d}" |
| 65 | |
| 66 | |
| 67 | def _row_html(row: dict) -> str: |
| 68 | label = row["account_name"] or row["account_id"] |
| 69 | code = f" ({row['account_code']})" if row["account_code"] else "" |
| 70 | return ( |
| 71 | "<tr>" |
| 72 | f"<td>{_esc(label)}{_esc(code)}</td>" |
| 73 | f"<td>{_esc(row['account_type'])}</td>" |
| 74 | f"<td class=\"num\">{_money(row['debit_total_cents'])}</td>" |
| 75 | f"<td class=\"num\">{_money(row['credit_total_cents'])}</td>" |
| 76 | "</tr>" |
| 77 | ) |
| 78 | |
| 79 | |
| 80 | def _esc(value) -> str: |
| 81 | return ( |
| 82 | str(value or "") |
| 83 | .replace("&", "&") |
| 84 | .replace("<", "<") |
| 85 | .replace(">", ">") |
| 86 | .replace('"', """) |
| 87 | .replace("'", "'") |
| 88 | ) |
| 89 | |
| 90 | |
| 91 | def GET(request): |
| 92 | identity = request.get("_identity", {}) |
| 93 | user_id = identity.get("user_id") |
| 94 | _logger.info("site_trial_balance served", user_id=user_id or "anonymous") |
| 95 | |
| 96 | if not user_id: |
| 97 | body = '<p class="hint"><a href="/login?next=/trial-balance">Sign in</a> to see your trial balance.</p>' |
| 98 | else: |
| 99 | import object_finance |
| 100 | |
| 101 | rows = object_finance.trial_balance(base_dir=_data_dir(), owner=user_id) |
| 102 | total_debits_cents = sum(row["debit_total_cents"] for row in rows) |
| 103 | total_credits_cents = sum(row["credit_total_cents"] for row in rows) |
| 104 | |
| 105 | if rows: |
| 106 | rows_html = "".join(_row_html(row) for row in rows) |
| 107 | else: |
| 108 | rows_html = '<tr><td colspan="4" class="hint">No posted journals yet.</td></tr>' |
| 109 | |
| 110 | totals_html = ( |
| 111 | '<tr class="totals">' |
| 112 | "<td colspan=\"2\">Total</td>" |
| 113 | f"<td class=\"num\">{_money(total_debits_cents)}</td>" |
| 114 | f"<td class=\"num\">{_money(total_credits_cents)}</td>" |
| 115 | "</tr>" |
| 116 | ) |
| 117 | |
| 118 | body = f""" |
| 119 | <div class="breadcrumb"><a href="/">Home</a> / Trial Balance</div> |
| 120 | <div class="pagehead"><h1>Trial Balance</h1></div> |
| 121 | <p class="hint">Posted journals only -- draft journals are excluded, matching |
| 122 | the predecessor system's own report semantics (a private predecessor-system |
| 123 | audit, not part of this repo).</p> |
| 124 | <table class="tb"> |
| 125 | <thead><tr><th>Account</th><th>Type</th><th class="num">Debit</th><th class="num">Credit</th></tr></thead> |
| 126 | <tbody> |
| 127 | {rows_html} |
| 128 | {totals_html} |
| 129 | </tbody> |
| 130 | </table> |
| 131 | """ |
| 132 | |
| 133 | who = ( |
| 134 | f"signed in as <strong>{user_id}</strong>" |
| 135 | if user_id |
| 136 | else '<a href="/login?next=/trial-balance">sign in</a>' |
| 137 | ) |
| 138 | html = f"""<!doctype html> |
| 139 | <html lang="en"> |
| 140 | <head> |
| 141 | <meta charset="utf-8"> |
| 142 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 143 | <title>Trial Balance</title> |
| 144 | <link rel="stylesheet" href="/style"> |
| 145 | <style>{_STYLE}</style> |
| 146 | </head> |
| 147 | <body> |
| 148 | <div class="wrap"> |
| 149 | <header class="app"><h1><a href="/">DBBASIC</a></h1><div class="who">{who}</div></header> |
| 150 | {body} |
| 151 | </div> |
| 152 | <script src="/nav"></script> |
| 153 | </body> |
| 154 | </html>""" |
| 155 | return {"content_type": "text/html; charset=utf-8", "body": html} |