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.
asset_schedule.py · 7.2 KB · 144 lines
| 1 | """site_asset_schedule -- one fixed asset and its depreciation schedule. |
| 2 | |
| 3 | GET /assets/{asset_id}: the register entry, the book schedule month by |
| 4 | month (what the schedule says beside what is already on the books), the |
| 5 | improvements and repairs with the decision recorded on each, the disposal |
| 6 | with its gain or loss, and the tax schedule labelled for what it is (shown, |
| 7 | never booked; not MACRS; ask your accountant). |
| 8 | |
| 9 | Computed server-side for the same reason site_trial_balance is: a schedule |
| 10 | is a fold over several collections and the books, not one record's detail. |
| 11 | The read is gated by object_access exactly as GET /collections/fixed_assets |
| 12 | /records/{id} would be (missing and refused are one 404). The rows are |
| 13 | read through object_records (object_assets.asset_overview), so the gate |
| 14 | above is the only thing standing between a viewer and somebody else's |
| 15 | register -- do not remove it. |
| 16 | |
| 17 | Money is formatted for display only; every stored amount is integer cents. |
| 18 | """ |
| 19 | |
| 20 | import os |
| 21 | |
| 22 | import object_access |
| 23 | import object_assets |
| 24 | |
| 25 | |
| 26 | def _base_dir(): |
| 27 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 28 | |
| 29 | |
| 30 | def _esc(value): |
| 31 | return (str(value if value is not None else "").replace("&", "&").replace("<", "<") |
| 32 | .replace(">", ">").replace('"', """).replace("'", "'")) |
| 33 | |
| 34 | |
| 35 | def _money(cents): |
| 36 | cents = int(cents or 0) |
| 37 | sign = "-" if cents < 0 else "" |
| 38 | return f"{sign}{abs(cents) // 100:,}.{abs(cents) % 100:02d}" |
| 39 | |
| 40 | |
| 41 | _STYLE = """ |
| 42 | table.sch { width: 100%; border-collapse: collapse; margin: .5rem 0 1.5rem; } |
| 43 | table.sch th, table.sch td { text-align: left; padding: .3rem .5rem; border-bottom: 1px solid var(--line, #38384a); } |
| 44 | table.sch td.num, table.sch th.num { text-align: right; font-variant-numeric: tabular-nums; } |
| 45 | .note { opacity: .8; font-size: .9em; } |
| 46 | dl.facts { display: grid; grid-template-columns: max-content 1fr; gap: .2rem 1rem; } |
| 47 | """ |
| 48 | |
| 49 | |
| 50 | def _page(title, body, user_id): |
| 51 | who = f"signed in as <strong>{_esc(user_id)}</strong>" if user_id else '<a href="/login">sign in</a>' |
| 52 | return {"content_type": "text/html; charset=utf-8", "body": f"""<!doctype html> |
| 53 | <html lang="en"><head><meta charset="utf-8"> |
| 54 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 55 | <title>{_esc(title)}</title><link rel="stylesheet" href="/style"><style>{_STYLE}</style></head> |
| 56 | <body><div class="wrap"> |
| 57 | <header class="app"><h1><a href="/">DBBASIC</a></h1><div class="who">{who}</div></header> |
| 58 | {body} |
| 59 | </div><script src="/nav"></script></body></html>"""} |
| 60 | |
| 61 | |
| 62 | def GET(request): |
| 63 | identity = request.get("_identity") or {} |
| 64 | user_id = identity.get("user_id") or "" |
| 65 | asset_id = str(request.get("asset_id") or "").strip() |
| 66 | if not user_id: |
| 67 | return _page("Fixed asset", f'<p class="hint"><a href="/login?next=/assets/{_esc(asset_id)}">Sign in</a> ' |
| 68 | "to see this asset.</p>", "") |
| 69 | base = _base_dir() |
| 70 | access = object_access.Access(identity, base_dir=base) |
| 71 | asset = access.get("fixed_assets", asset_id) |
| 72 | if not asset: |
| 73 | return {"status": 404, "content_type": "text/html; charset=utf-8", |
| 74 | "body": "<p>Asset not found.</p>"} |
| 75 | try: |
| 76 | view = object_assets.asset_overview(base, asset) |
| 77 | except object_assets.BooksUnavailable as exc: |
| 78 | return _page("Fixed asset", f"<p>The books could not be read: {_esc(exc)}</p>", user_id) |
| 79 | |
| 80 | book = view["book"] |
| 81 | facts = "".join(f"<dt>{_esc(label)}</dt><dd>{_esc(value)}</dd>" for label, value in ( |
| 82 | ("Status", asset.get("status")), ("Cost basis", _money(asset.get("cost_cents"))), |
| 83 | ("Placed in service", asset.get("placed_in_service_on")), |
| 84 | ("Location", asset.get("location")), ("Custodian", asset.get("custodian")), |
| 85 | ("Book method", f"{asset.get('book_method')} over {asset.get('book_life_months') or '-'} months, " |
| 86 | f"salvage {_money(asset.get('book_salvage_cents'))}"), |
| 87 | ("Booked depreciation", _money(view["booked_total_cents"])))) |
| 88 | |
| 89 | book_rows = "".join( |
| 90 | f"<tr><td>{_esc(r['month'])}</td><td class=num>{_money(r['amount_cents'])}</td>" |
| 91 | f"<td class=num>{_money(r['accumulated_cents'])}</td><td class=num>{_money(r['cost_basis_cents'])}</td>" |
| 92 | f"<td class=num>{_money(r['net_book_value_cents'])}</td>" |
| 93 | f"<td class=num>{_money(r['booked_cents']) if r['booked_cents'] else ''}</td></tr>" |
| 94 | for r in book["rows"]) or '<tr><td colspan="6" class="hint">No depreciation (method none, or not in service).</td></tr>' |
| 95 | |
| 96 | work_rows = "".join( |
| 97 | f"<tr><td>{_esc(w.get('performed_on'))}</td><td>{_esc(w.get('treatment'))}</td>" |
| 98 | f"<td>{_esc(w.get('basis'))}</td><td>{_esc(w.get('description'))}</td>" |
| 99 | f"<td>{_esc(w.get('reason'))}</td><td class=num>{_money(w.get('amount_cents'))}</td>" |
| 100 | f"<td>{_esc(w.get('status') or 'not yet booked')}</td></tr>" |
| 101 | for w in view["work"]) or '<tr><td colspan="7" class="hint">None recorded.</td></tr>' |
| 102 | |
| 103 | disposal = view["disposal"] |
| 104 | if disposal: |
| 105 | disposal_html = "<dl class=facts>" + "".join( |
| 106 | f"<dt>{_esc(label)}</dt><dd>{_esc(value)}</dd>" for label, value in ( |
| 107 | ("Kind", disposal.get("kind")), ("Date", disposal.get("disposed_on")), |
| 108 | ("Proceeds", _money(disposal.get("proceeds_cents"))), |
| 109 | ("Insurance recovery", _money(disposal.get("insurance_recovery_cents"))), |
| 110 | ("Net book value", _money(disposal.get("net_book_value_cents")) |
| 111 | if disposal.get("status") == "posted" else "computed when booked"), |
| 112 | ("Gain (loss)", _money(disposal.get("gain_loss_cents")) |
| 113 | if disposal.get("status") == "posted" else "computed when booked"), |
| 114 | ("Evidence", disposal.get("evidence_refs")))) + "</dl>" |
| 115 | else: |
| 116 | disposal_html = '<p class="hint">In service.</p>' |
| 117 | |
| 118 | tax = view["tax"] |
| 119 | tax_rows = "".join(f"<tr><td>{_esc(r['year'])}</td><td class=num>{_money(r['amount_cents'])}</td>" |
| 120 | f"<td>{_esc(r['basis_note'])}</td></tr>" for r in tax["rows"]) \ |
| 121 | or '<tr><td colspan="3" class="hint">Nothing entered.</td></tr>' |
| 122 | |
| 123 | name = asset.get("name") or asset_id |
| 124 | body = f""" |
| 125 | <div class="breadcrumb"><a href="/">Home</a> / <a href="/assets">Fixed Assets</a> / {_esc(name)}</div> |
| 126 | <div class="pagehead"><h1>{_esc(name)}</h1> <a href="/assets/{_esc(asset_id)}/edit">Edit</a></div> |
| 127 | <dl class="facts">{facts}</dl> |
| 128 | <h2>Book depreciation</h2> |
| 129 | <p class="note">Full-month convention: the month placed in service counts, the month of disposal does not. |
| 130 | "On the books" is what has been posted; a month is booked after it ends.</p> |
| 131 | <table class="sch"><thead><tr><th>Month</th><th class=num>Depreciation</th><th class=num>Accumulated</th> |
| 132 | <th class=num>Cost basis</th><th class=num>Net book value</th><th class=num>On the books</th></tr></thead> |
| 133 | <tbody>{book_rows}</tbody></table> |
| 134 | <h2>Improvements and repairs</h2> |
| 135 | <table class="sch"><thead><tr><th>Date</th><th>Treatment</th><th>Rule of thumb</th><th>What</th> |
| 136 | <th>Reason</th><th class=num>Amount</th><th>Booked</th></tr></thead><tbody>{work_rows}</tbody></table> |
| 137 | <h2>Disposal</h2> |
| 138 | {disposal_html} |
| 139 | <h2>Tax depreciation ({_esc(tax['method'])})</h2> |
| 140 | <p class="note">{_esc(tax['note'])}</p> |
| 141 | <table class="sch"><thead><tr><th>Year</th><th class=num>Amount</th><th>Basis</th></tr></thead> |
| 142 | <tbody>{tax_rows}</tbody></table> |
| 143 | """ |
| 144 | return _page(f"{name} -- fixed asset", body, user_id) |