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.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
fin_journal_lines.py · 2.2 KB · 59 lines
| 1 | """Pre-write hook for fin_journal_lines: a settled journal's lines are |
| 2 | settled too. |
| 3 | |
| 4 | The closed-period gate on fin_journals stops the journal record itself |
| 5 | from being touched -- but a journal's AMOUNTS live in its lines, and |
| 6 | without this hook a line added to (or edited on) a journal dated inside a |
| 7 | closed period would change settled books while the journal record sat |
| 8 | innocently untouched. Same period, same rule, checked from the line's |
| 9 | side: look up the parent journal, and if its date falls in a closed |
| 10 | period, refuse. |
| 11 | |
| 12 | Rejection-only. Lines on journals in open periods pass untouched -- |
| 13 | including the ordinary compose flow of drafting a journal and adding its |
| 14 | lines. A line whose journal_id does not resolve is left to relation |
| 15 | validation, which owns missing parents. |
| 16 | """ |
| 17 | |
| 18 | import os |
| 19 | |
| 20 | import object_finance |
| 21 | import object_records |
| 22 | |
| 23 | |
| 24 | def _base_dir(): |
| 25 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 26 | |
| 27 | |
| 28 | def BEFORE_WRITE(request): |
| 29 | if request.get("action") not in ("create", "update"): |
| 30 | return None |
| 31 | record = request.get("record") or {} |
| 32 | existing = request.get("existing") or {} |
| 33 | journal_id = str(record.get("journal_id") or existing.get("journal_id") or "").strip() |
| 34 | if not journal_id: |
| 35 | return None # required/relation validation owns this |
| 36 | |
| 37 | base = _base_dir() |
| 38 | try: |
| 39 | journal = object_records.get_collection_record("fin_journals", journal_id, base_dir=base) |
| 40 | except Exception: |
| 41 | return None # relation validation owns a missing journal |
| 42 | |
| 43 | period = object_finance.closed_period_for( |
| 44 | journal.get("date"), base_dir=base, |
| 45 | owner_id=journal.get("owner_id") or "", |
| 46 | entity_id=journal.get("entity_id") or "") |
| 47 | if period is None: |
| 48 | return None |
| 49 | |
| 50 | reason = str(period.get("reason") or "").strip() |
| 51 | label = f" ({reason})" if reason else "" |
| 52 | return { |
| 53 | "error": (f"This line belongs to a journal dated {journal.get('date')}, inside " |
| 54 | f"the closed period {period.get('start_date')} to " |
| 55 | f"{period.get('end_date')}{label}. Settled amounts are corrected by a " |
| 56 | "new journal in the current period, never by editing the old one's " |
| 57 | "lines."), |
| 58 | "status": 409, |
| 59 | } |