Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 49 minutes ago History
expense_report.py · 4.0 KB · 93 lines
1"""action_expense_report -- deductible and non-deductible spending, by period and category.
2
3GET /objects/action_expense_report?from=2025-01-01&to=2025-12-31&period=month|quarter|year
4 [&format=csv]
5
6Why an object and not a declared rollup. A rollup sums one field of a
7child 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
9books, and split three ways. That is a loop, and configuration never gets
10loops (docs/package-authoring.md, "What can't be declared"). The
11arithmetic is object_expense_rules.deduction_report, pure and tested by
12hand; this object only reads and gates.
13
14What it sums is the STAMP. Each expense carries the deductible share its
15category said when it was approved (deductible_bps, deductible_cents), so
16this year's report does not change because somebody edited a category
17next spring (docs/logic-decisions.md #1). An expense with no category is
18reported as `unclassified`: its amount is counted, its deductibility is
19not guessed. `gaps` lists every expense on the books that lacks what its
20stamps required, an approver, or a category -- the list to clear before
21an accountant asks.
22
23Who sees what: the rows the caller may read through the record API, asked
24of the same engine (object_access.Access.visible). A claimant gets a
25report of their own spending; role:manager and role:finance, who read
26every expense under the shipped rules, get the company's; anonymous is
27refused. This object reads storage directly, so it is the only gate in
28front of these numbers and it asks.
29
30Not tax advice, and the answer says so: the percentages are the
31operator's data, and whether they are right is a question for an
32accountant.
33"""
34
35import os
36import re
37
38import object_access
39import object_collections
40import object_expense_rules as rules
41import object_records
42
43_DATE = re.compile(r"^\d{4}-\d{2}-\d{2}$")
44NOTE = ("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
49def _base_dir():
50 return os.environ.get("DBBASIC_DATA_DIR", "data")
51
52
53def 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
93POST = GET