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 · 4.5 KB · 107 lines
| 1 | """action_asset_books -- book the register now, or read one asset's schedule. |
| 2 | |
| 3 | POST {asset_id?, loss_event_id?, as_of?} |
| 4 | Runs the same pass the daily schedule runs (object_assets), for the |
| 5 | asset or loss event named, or for every asset and approved loss event |
| 6 | the caller may UPDATE. as_of (default today) is the last day whose |
| 7 | ended months are booked; a date after today is refused -- depreciation |
| 8 | is booked for months that have ended, never ahead of time. Running it |
| 9 | twice books nothing twice (generated_from markers). |
| 10 | |
| 11 | GET {asset_id} |
| 12 | The asset's book schedule (month by month, with what is already on the |
| 13 | books beside it), its tax schedule (shown, never booked), its |
| 14 | improvements and repairs, and its disposal. |
| 15 | |
| 16 | Who: only somebody the record API would let update the asset (or the loss |
| 17 | event) -- object_access asks the same engine with the same policy. A |
| 18 | missing record and a refused one are the same 404, so this is no oracle for |
| 19 | ids. The pass then writes as a trusted writer (object_records), every |
| 20 | schema rule, lock and locked check still applying. |
| 21 | """ |
| 22 | |
| 23 | import os |
| 24 | from datetime import date |
| 25 | |
| 26 | import object_access |
| 27 | import object_assets |
| 28 | import object_records |
| 29 | |
| 30 | ACTOR = "action_asset_books" |
| 31 | |
| 32 | |
| 33 | def _base_dir(): |
| 34 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 35 | |
| 36 | |
| 37 | def _text(value): |
| 38 | return str(value if value is not None else "").strip() |
| 39 | |
| 40 | |
| 41 | def _as_of(request): |
| 42 | today = date.today().isoformat() |
| 43 | as_of = _text(request.get("as_of"))[:10] or today |
| 44 | try: |
| 45 | date.fromisoformat(as_of) |
| 46 | except ValueError: |
| 47 | return None, {"status": 400, "error": "as_of must be a date (YYYY-MM-DD)."} |
| 48 | if as_of > today: |
| 49 | return None, {"status": 400, "error": ("Depreciation is booked for months that have " |
| 50 | "ended, never ahead of time: as_of cannot be after today.")} |
| 51 | return as_of, None |
| 52 | |
| 53 | |
| 54 | def POST(request): |
| 55 | base = _base_dir() |
| 56 | access = object_access.Access(request.get("_identity") or {}, base_dir=base) |
| 57 | if not access.user_id and not access.is_admin: |
| 58 | return {"status": 403, "error": "Sign in to book your fixed assets."} |
| 59 | as_of, refusal = _as_of(request) |
| 60 | if refusal: |
| 61 | return refusal |
| 62 | |
| 63 | asset_id = _text(request.get("asset_id")) |
| 64 | event_id = _text(request.get("loss_event_id")) |
| 65 | try: |
| 66 | if asset_id: |
| 67 | if not access.get("fixed_assets", asset_id, action=object_access.UPDATE): |
| 68 | return {"status": 404, "error": f"Asset not found: {asset_id}"} |
| 69 | summary = object_assets.run_register(base, as_of=as_of, asset_ids=[asset_id], |
| 70 | loss_event_ids=[], actor=ACTOR) |
| 71 | elif event_id: |
| 72 | if not access.get("loss_events", event_id, action=object_access.UPDATE): |
| 73 | return {"status": 404, "error": f"Loss event not found: {event_id}"} |
| 74 | summary = object_assets.run_register(base, as_of=as_of, asset_ids=[], |
| 75 | loss_event_ids=[event_id], actor=ACTOR) |
| 76 | else: |
| 77 | assets = access.visible("fixed_assets", object_records.read_collection_records( |
| 78 | "fixed_assets", base_dir=base), action=object_access.UPDATE) |
| 79 | try: |
| 80 | events = object_records.read_collection_records("loss_events", base_dir=base) |
| 81 | except Exception: |
| 82 | events = [] |
| 83 | events = access.visible("loss_events", events, action=object_access.UPDATE) |
| 84 | summary = object_assets.run_register( |
| 85 | base, as_of=as_of, |
| 86 | asset_ids=[a["id"] for a in assets |
| 87 | if _text(a.get("status")) in ("in_service", "disposed")], |
| 88 | loss_event_ids=[e["id"] for e in events if _text(e.get("status")) == "approved"], |
| 89 | actor=ACTOR) |
| 90 | except Exception as exc: |
| 91 | return {"status": 503, "error": f"The register or the books could not be read: {str(exc)[:200]}"} |
| 92 | ok = not summary.get("errors") and not any( |
| 93 | r.get("errors") for r in summary["assets"] + summary["loss_events"]) |
| 94 | return {"status": 200, "ok": ok, **summary} |
| 95 | |
| 96 | |
| 97 | def GET(request): |
| 98 | base = _base_dir() |
| 99 | access = object_access.Access(request.get("_identity") or {}, base_dir=base) |
| 100 | asset_id = _text(request.get("asset_id")) |
| 101 | asset = access.get("fixed_assets", asset_id) if asset_id else None |
| 102 | if not asset: |
| 103 | return {"status": 404, "error": f"Asset not found: {asset_id}"} |
| 104 | try: |
| 105 | return {"status": 200, **object_assets.asset_overview(base, asset)} |
| 106 | except object_assets.BooksUnavailable as exc: |
| 107 | return {"status": 503, "error": str(exc)} |