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.
expense_report.py · 4.0 KB · 93 lines
| 1 | """action_expense_report -- deductible and non-deductible spending, by period and category. |
| 2 | |
| 3 | GET /objects/action_expense_report?from=2025-01-01&to=2025-12-31&period=month|quarter|year |
| 4 | [&format=csv] |
| 5 | |
| 6 | Why an object and not a declared rollup. A rollup sums one field of a |
| 7 | child into its parent; this is a fold over expenses grouped by two keys |
| 8 | (the period of `incurred_on`, the category), restricted to the rows on the |
| 9 | books, and split three ways. That is a loop, and configuration never gets |
| 10 | loops (docs/package-authoring.md, "What can't be declared"). The |
| 11 | arithmetic is object_expense_rules.deduction_report, pure and tested by |
| 12 | hand; this object only reads and gates. |
| 13 | |
| 14 | What it sums is the STAMP. Each expense carries the deductible share its |
| 15 | category said when it was approved (deductible_bps, deductible_cents), so |
| 16 | this year's report does not change because somebody edited a category |
| 17 | next spring (docs/logic-decisions.md #1). An expense with no category is |
| 18 | reported as `unclassified`: its amount is counted, its deductibility is |
| 19 | not guessed. `gaps` lists every expense on the books that lacks what its |
| 20 | stamps required, an approver, or a category -- the list to clear before |
| 21 | an accountant asks. |
| 22 | |
| 23 | Who sees what: the rows the caller may read through the record API, asked |
| 24 | of the same engine (object_access.Access.visible). A claimant gets a |
| 25 | report of their own spending; role:manager and role:finance, who read |
| 26 | every expense under the shipped rules, get the company's; anonymous is |
| 27 | refused. This object reads storage directly, so it is the only gate in |
| 28 | front of these numbers and it asks. |
| 29 | |
| 30 | Not tax advice, and the answer says so: the percentages are the |
| 31 | operator's data, and whether they are right is a question for an |
| 32 | accountant. |
| 33 | """ |
| 34 | |
| 35 | import os |
| 36 | import re |
| 37 | |
| 38 | import object_access |
| 39 | import object_collections |
| 40 | import object_expense_rules as rules |
| 41 | import object_records |
| 42 | |
| 43 | _DATE = re.compile(r"^\d{4}-\d{2}-\d{2}$") |
| 44 | NOTE = ("Deductible shares are what this box's expense categories said when each " |
| 45 | "expense was approved -- operator data, not a ruling. Check with your accountant; " |
| 46 | "this is not tax advice.") |
| 47 | |
| 48 | |
| 49 | def _base_dir(): |
| 50 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 51 | |
| 52 | |
| 53 | def GET(request): |
| 54 | base = _base_dir() |
| 55 | access = object_access.Access(request.get("_identity"), base_dir=base) |
| 56 | if not (access.signed_in or access.is_admin): |
| 57 | return {"status": 401, "error": "Sign in to see an expense report."} |
| 58 | |
| 59 | start = rules.text(request.get("from")) |
| 60 | end = rules.text(request.get("to")) |
| 61 | for name, value in (("from", start), ("to", end)): |
| 62 | if value and not _DATE.match(value): |
| 63 | return {"status": 400, "error": f"{name} must be YYYY-MM-DD"} |
| 64 | period = rules.text(request.get("period")) or "month" |
| 65 | if period not in rules.PERIODS: |
| 66 | return {"status": 400, "error": f"period must be one of {', '.join(rules.PERIODS)}"} |
| 67 | |
| 68 | try: |
| 69 | expenses = object_records.read_collection_records("expenses", base_dir=base) |
| 70 | except object_collections.CollectionNotFoundError: |
| 71 | expenses = [] |
| 72 | try: |
| 73 | # access: categories are policy every signed-in user reads (app-finance rules); only names are shown |
| 74 | categories = {row.get("id"): row for row in |
| 75 | object_records.read_collection_records("expense_categories", base_dir=base)} |
| 76 | except object_collections.CollectionNotFoundError: |
| 77 | categories = {} |
| 78 | # Anything else raised above is an unreadable ledger: the platform |
| 79 | # answers it as a failed execution, never as an empty report. |
| 80 | |
| 81 | mine = access.visible("expenses", expenses) |
| 82 | try: |
| 83 | report = rules.deduction_report(mine, categories, period=period, start=start, end=end) |
| 84 | except ValueError as exc: |
| 85 | return {"status": 409, "error": f"An expense on the books cannot be read: {exc}"} |
| 86 | |
| 87 | if rules.text(request.get("format")).lower() == "csv": |
| 88 | return {"status": 200, "content_type": "text/csv; charset=utf-8", |
| 89 | "body": rules.report_csv(report)} |
| 90 | return {"ok": True, **report, "note": NOTE} |
| 91 | |
| 92 | |
| 93 | POST = GET |