Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

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.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
reverse_journal.py · 3.5 KB · 87 lines
1"""Reverse a posted journal: compose and post its mirror entry.
2
3Posted journals are immutable by design (transitions: posted -> []); every
4correction is a new compensating record (docs/logic-decisions.md #3). This
5action is the one-click version of that doctrine -- and the first real
6instance of a schema-derived action (the record's real verb, not an edit).
7
8POST payload: {"journal_id": "..."}. Allowed for the journal's owner or an
9admin (the executing identity arrives in request["_identity"]). Refuses
10non-posted journals and journals already reversed (generated_from scan).
11The mirror swaps every line's debit/credit, stamps kind=reversing +
12generated_from="reversal:{id}", and posts (a mirror of a balanced entry is
13balanced by construction; re-verified anyway).
14"""
15
16import os
17
18import object_finance
19import object_records
20
21ACTOR = "action_reverse_journal"
22
23
24def _base_dir():
25 return os.environ.get("DBBASIC_DATA_DIR", "data")
26
27
28def POST(request):
29 identity = request.get("_identity") or {}
30 user_id = identity.get("user_id") or ""
31 roles = identity.get("roles") or []
32 journal_id = str(request.get("journal_id") or "").strip()
33 if not journal_id:
34 return {"status": 400, "error": "journal_id is required"}
35
36 base = _base_dir()
37 try:
38 journal = object_records.get_collection_record("fin_journals", journal_id, base_dir=base)
39 except Exception:
40 return {"status": 404, "error": f"Journal not found: {journal_id}"}
41
42 is_admin = "admin" in roles
43 if not is_admin and (not user_id or journal.get("owner_id") != user_id):
44 return {"status": 403, "error": "Only the journal's owner (or an admin) may reverse it."}
45 if journal.get("status") != "posted":
46 return {"status": 409, "error": "Only a posted journal can be reversed; edit the draft instead."}
47
48 marker = f"reversal:{journal_id}"
49 existing = object_finance.find_journal_by_provenance(base, marker)
50 if existing is not None:
51 return {"status": 409, "error": f"Already reversed by journal {existing.get('id')}."}
52
53 lines = [
54 l for l in object_records.read_collection_records("fin_journal_lines", base_dir=base)
55 if l.get("journal_id") == journal_id
56 ]
57 if not lines:
58 return {"status": 409, "error": "Journal has no lines to reverse."}
59
60 composed = object_finance.compose_posted_journal(
61 base,
62 generated_from=marker,
63 date=journal.get("date", ""),
64 description=f"Reversal of {journal.get('reference') or journal.get('description') or journal_id}",
65 lines=[
66 {
67 "account_id": line.get("account_id", ""),
68 "debit_cents": line.get("credit_cents", "0"), # swapped
69 "credit_cents": line.get("debit_cents", "0"), # swapped
70 "memo": line.get("memo", ""),
71 "entity_id": line.get("entity_id", ""),
72 }
73 for line in lines
74 ],
75 owner_id=journal.get("owner_id", ""),
76 entity_id=journal.get("entity_id", ""),
77 kind="reversing",
78 actor=ACTOR,
79 )
80 if not composed.get("ok"):
81 return {"status": 409, "error": composed.get("error", "could not compose the mirror")}
82 if composed.get("skipped"):
83 return {"status": 409, "error": f"Already reversed ({composed.get('journal_id', 'unknown')})."}
84 if composed.get("posted"):
85 return {"status": 200, "reversal_id": composed["journal_id"], "posted": True}
86 return {"status": 200, "reversal_id": composed["journal_id"], "posted": False,
87 "note": "left draft: mirror did not balance"}