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
asset_books.py · 3.0 KB · 74 lines
1"""system_asset_books -- the register reaches the books, on a schedule.
2
3Scheduled daily (dbbasic-package.json `asset_books_daily`) and also a
4HANDLES reaction to writes on the register, so a box running the change
5dispatcher books a disposal the moment it is recorded. Both callers land in
6object_assets, which does the work and owns idempotency: every journal
7carries a generated_from marker (fixed_assets/{id}/capitalization,
8asset_work/{id}, fixed_assets/{id}/depreciation/{YYYY-MM},
9asset_disposals/{id}, loss_events/{id}, loss_events/{id}/recovery) that
10fin_journals declares unique, so a replayed event, a second click, or the
11schedule running after the on-demand action composes nothing twice.
12
13Why daily and not monthly: a month's depreciation is booked on the first
14pass after the month ends, and a daily pass is the one that cannot miss a
15month because the box was down on the 1st. Most days it finds nothing to
16do and writes nothing.
17
18The trap from AGENTS.md: the scheduler calls POST and the change
19dispatcher calls EVENT. Both names are bound to the same function below.
20A scheduled pass (no collection in the request) books the whole register,
21every owner, each journal in its asset's owner's books.
22"""
23
24import os
25from datetime import date
26
27import object_assets
28import object_records
29
30HANDLES = [
31 "fixed_assets.record.created", "fixed_assets.record.updated",
32 "asset_work.record.created", "asset_work.record.updated",
33 "asset_disposals.record.created", "asset_disposals.record.updated",
34 "loss_events.record.created", "loss_events.record.updated",
35]
36
37ACTOR = "system_asset_books"
38
39
40def _base_dir():
41 return os.environ.get("DBBASIC_DATA_DIR", "data")
42
43
44def _asset_for(base, collection, record_id):
45 if collection == "fixed_assets":
46 return record_id
47 try:
48 row = object_records.get_collection_record(collection, record_id, base_dir=base)
49 except Exception:
50 return ""
51 return str(row.get("asset_id") or "")
52
53
54def EVENT(request):
55 base = _base_dir()
56 today = date.today().isoformat()
57 collection = str(request.get("collection") or "")
58 record_id = str(request.get("record_id") or request.get("id") or "")
59 try:
60 if collection == "loss_events" and record_id:
61 return {"ok": True, **object_assets.run_register(
62 base, as_of=today, asset_ids=[], loss_event_ids=[record_id], actor=ACTOR)}
63 if collection in ("fixed_assets", "asset_work", "asset_disposals") and record_id:
64 asset_id = _asset_for(base, collection, record_id)
65 if not asset_id:
66 return {"ok": True, "skipped": "no asset for this record"}
67 return {"ok": True, **object_assets.run_register(
68 base, as_of=today, asset_ids=[asset_id], loss_event_ids=[], actor=ACTOR)}
69 return {"ok": True, **object_assets.run_register(base, as_of=today, actor=ACTOR)}
70 except Exception as exc: # a reaction never breaks the dispatcher
71 return {"ok": False, "error": str(exc)[:200]}
72
73
74POST = EVENT