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.
assets.py · 6.4 KB · 136 lines
| 1 | """check_assets -- the locked checks that keep the register consistent. |
| 2 | |
| 3 | Three checks, declared by the three register schemas (`locked_checks`), |
| 4 | decided by object_records inside the write lock for EVERY writer -- the |
| 5 | record API, MCP, imports, the books pass -- because each one is a fact |
| 6 | about other rows that a pre-write hook (advisory, HTTP only) could not |
| 7 | hold still: |
| 8 | |
| 9 | ASSET_CHECK (fixed_assets.asset_state): an asset becomes `disposed` only |
| 10 | when its disposal is on the books. Status is an enum with transitions, |
| 11 | and `in_service -> disposed` has to be a legal move for the books pass to |
| 12 | make it -- which, without this check, would let anybody with update on |
| 13 | the row declare a van gone with no disposal, no gain or loss and no |
| 14 | evidence, and stop its depreciation. |
| 15 | |
| 16 | WORK_CHECK (asset_work.work_fits_asset): work is recorded against an asset |
| 17 | that is in service, dated inside its service life (not before it went |
| 18 | into service, not after it was disposed of, not in the future), and the |
| 19 | rule of thumb named agrees with the treatment -- an "improvement" resting |
| 20 | on routine maintenance, or a "repair" resting on a betterment, is a |
| 21 | decision that contradicts its own reason, and the books would carry |
| 22 | whichever half was wrong. |
| 23 | |
| 24 | DISPOSAL_CHECK (asset_disposals.disposal_fits_asset): an asset is disposed |
| 25 | of once it is in service, on a date inside its life (not in the future: |
| 26 | a disposal is a fact, not a plan), and not before work already recorded |
| 27 | against it. |
| 28 | |
| 29 | All three share the `assets` lock (they read each other's collections). |
| 30 | A read that fails raises, which the platform answers 503 and writes |
| 31 | nothing: an unreadable register is not an empty one. |
| 32 | """ |
| 33 | |
| 34 | from datetime import date |
| 35 | |
| 36 | import object_records |
| 37 | |
| 38 | IMPROVEMENT_BASES = {"betterment", "restoration", "adaptation", "other"} |
| 39 | REPAIR_BASES = {"routine_maintenance", "de_minimis", "other"} |
| 40 | |
| 41 | |
| 42 | def _text(value): |
| 43 | return str(value if value is not None else "").strip() |
| 44 | |
| 45 | |
| 46 | def _refuse(message): |
| 47 | return {"error": message, "status": 409} |
| 48 | |
| 49 | |
| 50 | def _base(request): |
| 51 | return request.get("base_dir") or "data" |
| 52 | |
| 53 | |
| 54 | def ASSET_CHECK(request): |
| 55 | action = request.get("action") |
| 56 | if action not in ("create", "update"): |
| 57 | return None |
| 58 | record = request.get("record") or {} |
| 59 | existing = request.get("existing") or {} |
| 60 | if _text(record.get("status")) != "disposed": |
| 61 | return None |
| 62 | if action == "create": |
| 63 | return _refuse("An asset is registered as draft or in_service. It becomes disposed " |
| 64 | "when its disposal is posted.") |
| 65 | if _text(existing.get("status")) == "disposed": |
| 66 | return None |
| 67 | asset_id = _text(record.get("id")) |
| 68 | rows = object_records.read_collection_records("asset_disposals", base_dir=_base(request)) |
| 69 | if any(_text(row.get("asset_id")) == asset_id and _text(row.get("status")) == "posted" |
| 70 | for row in rows): |
| 71 | return None |
| 72 | return _refuse("An asset becomes disposed when its disposal is posted: record the sale, " |
| 73 | "scrapping, theft or casualty as an asset disposal and run the books.") |
| 74 | |
| 75 | |
| 76 | def _asset(request, asset_id): |
| 77 | return object_records.get_collection_record("fixed_assets", asset_id, base_dir=_base(request)) |
| 78 | |
| 79 | |
| 80 | def WORK_CHECK(request): |
| 81 | action = request.get("action") |
| 82 | if action not in ("create", "update"): |
| 83 | return None |
| 84 | record = request.get("record") or {} |
| 85 | treatment, basis = _text(record.get("treatment")), _text(record.get("basis")) |
| 86 | if treatment == "improvement" and basis not in IMPROVEMENT_BASES: |
| 87 | return _refuse(f"An improvement rests on a betterment, a restoration or an adaptation " |
| 88 | f"(or 'other', with the reason); '{basis}' points to a repair.") |
| 89 | if treatment == "repair" and basis not in REPAIR_BASES: |
| 90 | return _refuse(f"A repair rests on routine maintenance or the de minimis safe harbor " |
| 91 | f"(or 'other', with the reason); '{basis}' points to an improvement.") |
| 92 | |
| 93 | asset = _asset(request, _text(record.get("asset_id"))) |
| 94 | status = _text(asset.get("status")) |
| 95 | if status == "draft": |
| 96 | return _refuse("This asset is not in service yet. Money spent getting it ready for use " |
| 97 | "is part of its cost: add it to the cost basis instead.") |
| 98 | if status == "disposed": |
| 99 | return _refuse("This asset has been disposed of; nothing more is spent on it.") |
| 100 | performed_on = _text(record.get("performed_on"))[:10] |
| 101 | in_service = _text(asset.get("placed_in_service_on"))[:10] |
| 102 | if in_service and performed_on < in_service: |
| 103 | return _refuse(f"The work is dated {performed_on}, before the asset went into service " |
| 104 | f"on {in_service}.") |
| 105 | if performed_on > date.today().isoformat(): |
| 106 | return _refuse("Work is recorded once it has been done, not before.") |
| 107 | for row in object_records.read_collection_records("asset_disposals", base_dir=_base(request)): |
| 108 | if _text(row.get("asset_id")) == _text(asset.get("id")): |
| 109 | disposed_on = _text(row.get("disposed_on"))[:10] |
| 110 | if disposed_on and performed_on > disposed_on: |
| 111 | return _refuse(f"The asset was disposed of on {disposed_on}; this work is dated after it.") |
| 112 | return None |
| 113 | |
| 114 | |
| 115 | def DISPOSAL_CHECK(request): |
| 116 | action = request.get("action") |
| 117 | if action not in ("create", "update"): |
| 118 | return None |
| 119 | record = request.get("record") or {} |
| 120 | asset = _asset(request, _text(record.get("asset_id"))) |
| 121 | if _text(asset.get("status")) != "in_service": |
| 122 | return _refuse("Only an asset in service can be disposed of (a draft was never on the " |
| 123 | "books; a disposed one already left them).") |
| 124 | disposed_on = _text(record.get("disposed_on"))[:10] |
| 125 | in_service = _text(asset.get("placed_in_service_on"))[:10] |
| 126 | if in_service and disposed_on < in_service: |
| 127 | return _refuse(f"The disposal is dated {disposed_on}, before the asset went into service " |
| 128 | f"on {in_service}.") |
| 129 | if disposed_on > date.today().isoformat(): |
| 130 | return _refuse("A disposal is recorded once it has happened, not before.") |
| 131 | for row in object_records.read_collection_records("asset_work", base_dir=_base(request)): |
| 132 | if (_text(row.get("asset_id")) == _text(asset.get("id")) |
| 133 | and _text(row.get("performed_on"))[:10] > disposed_on): |
| 134 | return _refuse(f"Work on this asset is recorded on {_text(row.get('performed_on'))[:10]}, " |
| 135 | f"after the disposal date {disposed_on}.") |
| 136 | return None |