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.
expenses.json · 11.2 KB · 116 lines
| 1 | { |
| 2 | "name": "expenses", |
| 3 | "title": "Expenses", |
| 4 | "version": 5, |
| 5 | "description": "Money the business spent, and sometimes money a client owes back. An expense is an accounting fact first and a billable item second, which is why it lives with the books rather than with the invoices: an unbillable expense still costs the company and still belongs in the ledger. It walks the same approval ladder as time (draft -> submitted -> approved -> billed) and for the same reason -- somebody other than the spender says yes before it becomes either a journal entry or a line on a client's bill. paid_by is the field most expense systems get wrong by omitting: a card the company holds and a card an employee holds produce DIFFERENT journals (credit the clearing account, or credit a liability owed back to the person), and guessing from the amount is not a strategy. markup_bps is in basis points to match invoice_lines' tax_rate_bps; it is stamped at approval alongside the billable amount, so re-running a bill can never apply this quarter's markup policy to last quarter's approved costs (docs/logic-decisions.md #1). Approved expenses are frozen: a mistaken one is corrected by a compensating entry or a credit, never by editing the record of what was spent (#5, #8). v4: relation.requires: update on the ledger account, the invoice it is rebilled on and the entity, read on the project. v5 (substantiation): category_id (expense_categories) and the business_purpose, attendees and location an auditor asks for, and receipt_scan_id, a real link to the scanned receipt (requires update on the scan: only somebody who may act on that evidence may cite it; unique, so one receipt substantiates one expense). receipt_ref stays, readable, for what was recorded before. What the category requires is STAMPED onto the expense (receipt_required, purpose_required, attendees_required, deductible_bps, deductible_cents) by the hook, and the LOCKED_CHECK category_stamps refuses, for every writer, a stamp that disagrees with the category at the moment of submitting or approving; required_when then makes the stamped requirements binding while submitted, approved or billed. Rows from before v5 have no category and no stamps and are unaffected.", |
| 6 | "fields": [ |
| 7 | {"name": "id"}, |
| 8 | {"name": "description", "type": "text", "required": true, "label": "Description", |
| 9 | "help": "What was bought. This is what a client reads on the invoice line, so write it for them."}, |
| 10 | {"name": "incurred_on", "type": "date", "required": true, "label": "Incurred", |
| 11 | "help": "The day the money was spent, which is the day the books belong to -- not the day somebody got round to entering it."}, |
| 12 | {"name": "amount_cents", "type": "integer", "required": true, "label": "Amount", |
| 13 | "validation": {"min": 1}, |
| 14 | "help": "What it cost, in integer minor units (docs/logic-decisions.md #10). The cost, not the price to the client."}, |
| 15 | {"name": "currency", "type": "text", "default": "USD", "label": "Currency"}, |
| 16 | {"name": "project_id", "label": "Project", |
| 17 | "relation": {"collection": "projects", "display_field": "name", "requires": "read"}, |
| 18 | "help": "What the spend was for. Required in practice to bill it: an expense with no project has no invoice to land on."}, |
| 19 | {"name": "account_id", "label": "Expense Account", |
| 20 | "relation": {"collection": "fin_accounts", "display_field": "name", "requires": "update"}, |
| 21 | "help": "Which account to debit. Falls back to the expenses.journal.expense_account setting, so a business that has not built out a chart of accounts still gets a balanced entry."}, |
| 22 | {"name": "paid_by", "type": "enum", "default": "company", "label": "Paid By", |
| 23 | "enum": ["company", "personal"], |
| 24 | "help": "company credits the card/cash clearing account. personal credits a liability owed back to the person who paid -- a reimbursement the business owes, which is a real debt and not a footnote."}, |
| 25 | {"name": "billable", "type": "boolean", "default": "true", "label": "Billable", |
| 26 | "help": "Whether the client pays it back. Unbillable expenses still post to the books; they simply never reach an invoice."}, |
| 27 | {"name": "markup_bps", "type": "integer", "label": "Markup", |
| 28 | "help": "Basis points added when billing it on: 1500 is 15%. Deliberately has NO default, so that blank and zero can mean different things: blank takes the house policy (the billing.expense_markup_bps setting) at approval, while an explicit 0 says this one goes through at cost no matter what the house does. A default of 0 here would have made the two indistinguishable and quietly disabled the setting."}, |
| 29 | {"name": "billable_amount_cents", "type": "integer", "default": "0", |
| 30 | "label": "Billable Amount", |
| 31 | "help": "Stamped at approval: amount plus markup, rounded half-up once. What actually reaches the invoice."}, |
| 32 | {"name": "status", "type": "enum", "default": "draft", "label": "Status", |
| 33 | "enum": ["draft", "submitted", "approved", "rejected", "billed"], |
| 34 | "transitions": { |
| 35 | "draft": [{"to": "submitted", "when": {"owner_id": "$user_id"}}], |
| 36 | "submitted": ["approved", "rejected"], |
| 37 | "rejected": [{"to": "draft", "when": {"owner_id": "$user_id"}}], |
| 38 | "approved": ["billed", "rejected"], |
| 39 | "billed": [] |
| 40 | }, |
| 41 | "help": "Only the person who spent the money may submit it, and somebody else approves it. Approval is what composes the journal and what makes it billable. billed is terminal -- a disputed expense is a credit on the invoice, not a rewind."}, |
| 42 | {"name": "invoice_id", "label": "Invoice", |
| 43 | "relation": {"collection": "invoices", "display_field": "number", "requires": "update"}, |
| 44 | "help": "Stamped when billed. An expense already carrying one is never billed again."}, |
| 45 | {"name": "receipt_ref", "type": "text", "label": "Receipt (old reference)", |
| 46 | "help": "Free text from before receipts were linked: a file id, a scan id, a vendor reference. Kept and readable; new expenses link the scan itself in receipt_scan_id, which is what the receipt requirement reads."}, |
| 47 | {"name": "generated_from", "type": "text", "read_only": true, "label": "Generated From", |
| 48 | "help": "Provenance for an expense a machine wrote: scans/{id} when a person confirmed a scanned receipt. Unique, so one scan books one expense however many times confirmation is clicked or retried (v2, 2026-11-28). Blank on an expense typed in by hand; no form or API client can set it."}, |
| 49 | {"name": "approved_by", "type": "text", "label": "Approved By"}, |
| 50 | {"name": "approved_at", "type": "datetime", "label": "Approved"}, |
| 51 | {"name": "notes", "type": "textarea", "label": "Notes"}, |
| 52 | {"name": "owner_id", "type": "text", "label": "Owner"}, |
| 53 | {"name": "entity_id", "type": "text", "label": "Entity", |
| 54 | "relation": {"collection": "entities", "display_field": "name", "requires": "update"}}, |
| 55 | {"name": "created_at", "type": "datetime", "read_only": true, "label": "Created"}, |
| 56 | {"name": "category_id", "label": "Category", |
| 57 | "relation": {"collection": "expense_categories", "display_field": "name", "requires": "read"}, |
| 58 | "help": "What kind of spending this is. The category decides what has to be attached before it can be submitted, and how much of it the books treat as deductible."}, |
| 59 | {"name": "business_purpose", "type": "textarea", "label": "Business purpose", |
| 60 | "validation": {"required_when": {"purpose_required": "true", "status": ["submitted", "approved", "billed"]}}, |
| 61 | "help": "What business this was for, in a sentence somebody reading it in three years will understand: who, what was discussed or done, why it was needed."}, |
| 62 | {"name": "attendees", "type": "textarea", "label": "Attendees", |
| 63 | "validation": {"required_when": {"attendees_required": "true", "status": ["submitted", "approved", "billed"]}}, |
| 64 | "help": "Who was there and their business relationship (\"Jo Park, CFO, Acme -- prospect\"). Required for meals and entertainment."}, |
| 65 | {"name": "location", "type": "text", "label": "Place", |
| 66 | "help": "Where: the restaurant, the city, the venue."}, |
| 67 | {"name": "receipt_scan_id", "label": "Receipt", |
| 68 | "relation": {"collection": "scans", "display_field": "filename", "requires": "update"}, |
| 69 | "validation": {"required_when": {"receipt_required": "true", "status": ["submitted", "approved", "billed"]}}, |
| 70 | "help": "The scanned receipt (app-intake). One receipt substantiates one expense."}, |
| 71 | {"name": "receipt_required", "type": "boolean", "label": "Receipt required", |
| 72 | "help": "Stamped from the category: the amount is over its receipt threshold. Not typed in; the server sets it and refuses one that disagrees with the category."}, |
| 73 | {"name": "purpose_required", "type": "boolean", "label": "Purpose required", |
| 74 | "help": "Stamped from the category."}, |
| 75 | {"name": "attendees_required", "type": "boolean", "label": "Attendees required", |
| 76 | "help": "Stamped from the category."}, |
| 77 | {"name": "deductible_bps", "type": "integer", "label": "Deductible (basis points)", |
| 78 | "validation": {"min": 0, "max": 10000}, |
| 79 | "help": "Stamped from the category when submitted and approved: the share the books treat as deductible. Editing the category later does not change it."}, |
| 80 | {"name": "deductible_cents", "type": "integer", "label": "Deductible amount", |
| 81 | "validation": {"min": 0}, |
| 82 | "help": "amount_cents times deductible_bps, rounded half-up once. Blank without a category: the report calls that unclassified rather than guessing."} |
| 83 | ], |
| 84 | "unique": [ |
| 85 | {"fields": ["generated_from"], |
| 86 | "message": "That scan has already been confirmed into an expense."}, |
| 87 | {"fields": ["receipt_scan_id"], |
| 88 | "message": "That receipt already substantiates another expense. One receipt, one claim."} |
| 89 | ], |
| 90 | "locked_when": [ |
| 91 | {"when": {"status": ["approved", "billed"]}, |
| 92 | "except": ["status", "invoice_id", "notes", "receipt_ref"], |
| 93 | "message": "This expense is approved or billed: what was spent is settled and its journal is already posted. Only status, invoice_id, notes and receipt_ref may change, and it cannot be deleted. Correct it with a compensating entry or a credit."} |
| 94 | ], |
| 95 | "locked_checks": [ |
| 96 | {"name": "category_stamps", "object": "hook_expenses", "function": "LOCKED_CHECK", |
| 97 | "reads": ["expense_categories"], |
| 98 | "on": {"create": true, |
| 99 | "update": ["category_id", "amount_cents", "status", "receipt_required", |
| 100 | "purpose_required", "attendees_required", "deductible_bps", |
| 101 | "deductible_cents"], |
| 102 | "delete": false}} |
| 103 | ], |
| 104 | "hooks": {"before_write": "hook_expenses"}, |
| 105 | "search": {"fields": ["description", "notes", "receipt_ref", "business_purpose", "attendees", "location"]}, |
| 106 | "forms": {"default": {"fields": ["description", "incurred_on", "amount_cents", |
| 107 | "currency", "category_id", "business_purpose", |
| 108 | "attendees", "location", "receipt_scan_id", |
| 109 | "project_id", "account_id", |
| 110 | "paid_by", "billable", "markup_bps", |
| 111 | "receipt_ref", "notes"]}}, |
| 112 | "views": {"list_mode": "table", |
| 113 | "list_fields": ["incurred_on", "description", "amount_cents", |
| 114 | "paid_by", "billable", "status"], |
| 115 | "filter_fields": ["status", "billable", "paid_by", "project_id", "category_id"]} |
| 116 | } |