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
setup_accounts.py · 5.3 KB · 119 lines
1"""setup_accounts -- 65 multi-entity, slice 4: seed an entity's chart of
2accounts from its mode.
3
4POST /finance/setup-accounts {entity_id}. A faithful port of the predecessor's
5Entity.create_default_accounts / the setup_finance_accounts MCP tool: given one
6of the caller's entities, create its fin_accounts based on the entity's `mode`
7(simple = income + expenses; standard = a 15-account chart; double_entry = a
8full 29-account chart). Idempotent -- an account already present for the entity
9(matched by name, the predecessor's get_or_create key) is skipped, so running
10it twice, or after adding a few accounts by hand, never duplicates.
11
12Owner-gated in the handler (like app-timers' timer_actions): the entity must
13belong to the caller, and every account is created owned by the caller and
14scoped to that entity_id. No public/cross-owner path -- a caller can only set
15up accounts for their own set of books.
16"""
17
18import json
19import os
20
21import object_records
22
23DATA_DIR_ENV = "DBBASIC_DATA_DIR"
24ACCOUNTS_COLLECTION = "fin_accounts"
25ENTITIES_COLLECTION = "entities"
26
27# (account_type, name, code) per mode -- carried verbatim from the predecessor's
28# Entity.STANDARD_ACCOUNTS / DOUBLE_ENTRY_ACCOUNTS (private source audit).
29_SIMPLE = [
30 ("income", "Income", "4000"),
31 ("expense", "Expenses", "5000"),
32]
33_STANDARD = [
34 ("asset", "Cash", "1010"), ("asset", "Checking Account", "1020"),
35 ("asset", "Savings Account", "1030"), ("asset", "Accounts Receivable", "1100"),
36 ("liability", "Accounts Payable", "2100"), ("income", "Income", "4000"),
37 ("expense", "Cost of Goods Sold", "5000"), ("expense", "Rent", "5100"),
38 ("expense", "Utilities", "5200"), ("expense", "Software & Subscriptions", "5300"),
39 ("expense", "Marketing", "5400"), ("expense", "Travel", "5500"),
40 ("expense", "Meals & Entertainment", "5600"), ("expense", "Payroll", "5700"),
41 ("expense", "Taxes", "5800"),
42]
43_DOUBLE_ENTRY = [
44 ("asset", "Cash", "1010"), ("asset", "Checking Account", "1020"),
45 ("asset", "Savings Account", "1030"), ("asset", "Accounts Receivable", "1100"),
46 ("asset", "Inventory", "1200"), ("asset", "Equipment", "1500"),
47 ("asset", "Prepaid Expenses", "1510"), ("liability", "Accounts Payable", "2100"),
48 ("liability", "Credit Card", "2200"), ("liability", "Sales Tax Payable", "2300"),
49 ("liability", "Loans Payable", "2400"), ("equity", "Owner's Equity", "3100"),
50 ("equity", "Retained Earnings", "3200"), ("equity", "Owner's Draw", "3300"),
51 ("income", "Sales Revenue", "4100"), ("income", "Service Revenue", "4200"),
52 ("income", "Other Income", "4900"), ("expense", "Cost of Goods Sold", "5000"),
53 ("expense", "Rent", "5100"), ("expense", "Utilities", "5200"),
54 ("expense", "Software & Subscriptions", "5300"), ("expense", "Marketing", "5400"),
55 ("expense", "Travel", "5500"), ("expense", "Meals & Entertainment", "5600"),
56 ("expense", "Payroll", "5700"), ("expense", "Insurance", "5800"),
57 ("expense", "Depreciation", "5810"), ("expense", "Interest Expense", "5820"),
58 ("expense", "Taxes", "5830"),
59]
60CHART_BY_MODE = {"simple": _SIMPLE, "standard": _STANDARD, "double_entry": _DOUBLE_ENTRY}
61
62
63def _data_dir():
64 return os.environ.get(DATA_DIR_ENV, object_records.DEFAULT_DATA_DIR)
65
66
67def _error(status, message):
68 return {"content_type": "application/json", "status": status,
69 "body": json.dumps({"status": "error", "error": message})}
70
71
72def POST(request):
73 identity = request.get("_identity") or {}
74 user_id = identity.get("user_id")
75 if not user_id:
76 return _error(401, "Sign in to set up accounts.")
77
78 entity_id = str(request.get("entity_id") or "").strip()
79 if not entity_id:
80 return _error(400, "entity_id is required.")
81
82 base_dir = _data_dir()
83 try:
84 entity = object_records.get_collection_record(ENTITIES_COLLECTION, entity_id, base_dir=base_dir)
85 except (object_records.RecordNotFoundError, object_records.InvalidRecordIdError):
86 return _error(404, f"Entity not found: {entity_id}")
87 if entity.get("owner_id") != user_id:
88 return _error(403, "That entity belongs to another owner.")
89
90 mode = (entity.get("mode") or "simple").strip()
91 chart = CHART_BY_MODE.get(mode)
92 if chart is None:
93 return _error(400, f"Unknown entity mode: {mode!r}")
94
95 # Idempotent: match the predecessor's get_or_create-by-name. Only this
96 # entity's existing accounts count -- two entities may share account names.
97 try:
98 all_accounts = object_records.read_collection_records(ACCOUNTS_COLLECTION, base_dir=base_dir)
99 except (LookupError, OSError, ValueError):
100 all_accounts = []
101 have = {a.get("name") for a in all_accounts if a.get("entity_id") == entity_id}
102
103 created, skipped = 0, 0
104 for account_type, name, code in chart:
105 if name in have:
106 skipped += 1
107 continue
108 object_records.create_collection_record(
109 ACCOUNTS_COLLECTION,
110 {"name": name, "code": code, "account_type": account_type,
111 "entity_id": entity_id, "owner_id": user_id, "is_active": "true"},
112 base_dir=base_dir, actor="setup_accounts",
113 )
114 created += 1
115
116 _logger.info("setup_accounts", entity_id=entity_id, mode=mode, created=created, skipped=skipped)
117 return {"content_type": "application/json",
118 "body": json.dumps({"status": "ok", "mode": mode,
119 "created": created, "skipped": skipped})}