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_books.py · 3.0 KB · 74 lines
| 1 | """system_asset_books -- the register reaches the books, on a schedule. |
| 2 | |
| 3 | Scheduled daily (dbbasic-package.json `asset_books_daily`) and also a |
| 4 | HANDLES reaction to writes on the register, so a box running the change |
| 5 | dispatcher books a disposal the moment it is recorded. Both callers land in |
| 6 | object_assets, which does the work and owns idempotency: every journal |
| 7 | carries a generated_from marker (fixed_assets/{id}/capitalization, |
| 8 | asset_work/{id}, fixed_assets/{id}/depreciation/{YYYY-MM}, |
| 9 | asset_disposals/{id}, loss_events/{id}, loss_events/{id}/recovery) that |
| 10 | fin_journals declares unique, so a replayed event, a second click, or the |
| 11 | schedule running after the on-demand action composes nothing twice. |
| 12 | |
| 13 | Why daily and not monthly: a month's depreciation is booked on the first |
| 14 | pass after the month ends, and a daily pass is the one that cannot miss a |
| 15 | month because the box was down on the 1st. Most days it finds nothing to |
| 16 | do and writes nothing. |
| 17 | |
| 18 | The trap from AGENTS.md: the scheduler calls POST and the change |
| 19 | dispatcher calls EVENT. Both names are bound to the same function below. |
| 20 | A scheduled pass (no collection in the request) books the whole register, |
| 21 | every owner, each journal in its asset's owner's books. |
| 22 | """ |
| 23 | |
| 24 | import os |
| 25 | from datetime import date |
| 26 | |
| 27 | import object_assets |
| 28 | import object_records |
| 29 | |
| 30 | HANDLES = [ |
| 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 | |
| 37 | ACTOR = "system_asset_books" |
| 38 | |
| 39 | |
| 40 | def _base_dir(): |
| 41 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 42 | |
| 43 | |
| 44 | def _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 | |
| 54 | def 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 | |
| 74 | POST = EVENT |