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.
invoices.json · 19.3 KB · 539 lines
| 1 | { |
| 2 | "name": "invoices", |
| 3 | "title": "Invoices", |
| 4 | "version": 14, |
| 5 | "description": "Line-item invoices, quotes, and credit notes. Lines live in invoice_lines; subtotal/tax/total/balance are stamped by the invoice_totals handler, never typed by hand. See plan/vocabulary/20-invoice-spec.md. v8: customer_id declares relation.requires read. v9: the invoice's lifecycle and money are not its owner's to set. Machine-raised invoices (checkout, the subscription biller) are OWNED BY THE CUSTOMER they bill, so the owner is the payer, and an owner who could PUT status=paid stopped their own dunning, and a shop's fulfillment handler -- which trusts status=paid -- would ship. status, doc_type, currency, the dates, the stamped totals, the dunning stamps, paid_at, the tokens, the portal counters and source_order_id are now field permissions {edit: role:manager, role:admin; default: read}; reactions (invoice totals, payment status, aging, the portal) write in process and are untouched. The status transitions lost their owner_id guards: who may move the status is the field permission now, and a guard naming the owner would have refused the staff who must move a customer's invoice. v10: credit notes net. credits_invoice_id says which invoice a credit note credits; an applied credit is a row in invoice_credits, and two rollups read those rows: credited_cents (credits applied TO this invoice) and credit_applied_cents (how much of THIS credit note has been applied, to an invoice or on account). balance_due_cents is total less paid less both, so an invoice owes less once credited and an applied credit note owes nothing -- it used to read its own total as a balance due. amount_paid_cents and the status rules that follow it are untouched: a credit is not a payment. v11: exchange_status, stamped by app-exchange when the customer's box refuses the invoice or credit note (\"Refused by <partner>: <reason>\"); the invoice is otherwise unchanged -- whether a refused invoice is void is a person's decision. v12: a credit note also says why and who agreed -- reason_code (a correction_reasons code, app-counter) and approved_by -- and its lines cite what they correct (invoice_lines.order_line_id, and credits_line_id for the credited invoice's line), so a correction after payment is a new document pointing at the old one instead of an edit to it. One credit-note model, however it is issued: it is applied by an invoice_credits row, so the credited invoice's balance_due_cents falls by the credit and the credit note's own balance is 0. app-counter's action_counter_correct applies it in the act that issues it (source counter); one a person makes is applied when it is sent through the exchange (source exchange), or by hand; sending a counter credit note does not apply it twice. v13: status follows the money. An issued invoice's total never changes; a debt is cancelled only by a credit note, and the status is read off received, refunded and credited every time one moves (object_corrections.money_status): three new states, credit_owed (balance below zero, the shop owes the customer), refunded (credited away and the money given back: refunding a sale is a credit note plus a refund) and credited (credited away, nothing paid), and paid is no longer terminal -- a bounced or deleted payment, or a refund with no credit note, reopens the invoice and the aging pass duns it again. v14: tax is stamped per line at issue and computed once per rate group, half-up (decision 2); invoice_totals folds drafts only.", |
| 6 | "fields": [ |
| 7 | { |
| 8 | "name": "id" |
| 9 | }, |
| 10 | { |
| 11 | "name": "doc_type", |
| 12 | "type": "enum", |
| 13 | "label": "Document Type", |
| 14 | "default": "invoice", |
| 15 | "enum": [ |
| 16 | "invoice", |
| 17 | "credit_note", |
| 18 | "quote" |
| 19 | ], |
| 20 | "help": "invoice/credit_note/quote share this schema. Real predecessor numbers each type separately (INV-####/CN-####/QT-####); auto-numbering is deferred here (see the number field) but the type is captured now so nothing is lost. Staff-only (v9): turning an invoice into a quote takes it out of aging and dunning.", |
| 21 | "permissions": { |
| 22 | "edit": [ |
| 23 | "role:manager", |
| 24 | "role:admin" |
| 25 | ], |
| 26 | "default": "read" |
| 27 | } |
| 28 | }, |
| 29 | { |
| 30 | "name": "credits_invoice_id", |
| 31 | "label": "Credits Invoice", |
| 32 | "relation": { |
| 33 | "collection": "invoices", |
| 34 | "display_field": "number", |
| 35 | "requires": "read" |
| 36 | }, |
| 37 | "help": "On a credit note: the invoice it credits. Sending the credit note to a partner's box applies it to that invoice (invoice_credits); blank means it stands on the customer's account. Its lines cite what they correct: invoice_lines.order_line_id (the order line) and credits_line_id (the credited invoice's line).", |
| 38 | "permissions": { |
| 39 | "edit": [ |
| 40 | "role:manager", |
| 41 | "role:admin" |
| 42 | ], |
| 43 | "default": "read" |
| 44 | } |
| 45 | }, |
| 46 | { |
| 47 | "name": "number", |
| 48 | "type": "text", |
| 49 | "required": true, |
| 50 | "label": "Number", |
| 51 | "help": "Human document number, e.g. INV-0001. v1 is typed by the owner, not auto-assigned -- a sequenced invoice_counters collection + closed create route is a later slice (plan/vocabulary/20-invoice-spec.md), not built here." |
| 52 | }, |
| 53 | { |
| 54 | "name": "customer_id", |
| 55 | "subject": "relation", |
| 56 | "label": "Customer (contact)", |
| 57 | "relation": { |
| 58 | "collection": "contacts", |
| 59 | "display_field": "first_name", |
| 60 | "requires": "read" |
| 61 | }, |
| 62 | "help": "Optional cross-reference only. The snapshot fields below are what every surface renders -- editing the contact later never rewrites an already-issued document." |
| 63 | }, |
| 64 | { |
| 65 | "name": "customer_name", |
| 66 | "type": "text", |
| 67 | "required": true, |
| 68 | "label": "Bill To", |
| 69 | "validation": { |
| 70 | "min_length": 1, |
| 71 | "max_length": 200 |
| 72 | } |
| 73 | }, |
| 74 | { |
| 75 | "name": "customer_email", |
| 76 | "subject": "email", |
| 77 | "type": "text", |
| 78 | "label": "Customer Email", |
| 79 | "validation": { |
| 80 | "max_length": 254 |
| 81 | } |
| 82 | }, |
| 83 | { |
| 84 | "name": "customer_address", |
| 85 | "type": "textarea", |
| 86 | "label": "Billing Address" |
| 87 | }, |
| 88 | { |
| 89 | "name": "currency", |
| 90 | "type": "text", |
| 91 | "default": "USD", |
| 92 | "label": "Currency (ISO 4217)", |
| 93 | "validation": { |
| 94 | "pattern": "^[A-Z]{3}$" |
| 95 | }, |
| 96 | "permissions": { |
| 97 | "edit": [ |
| 98 | "role:manager", |
| 99 | "role:admin" |
| 100 | ], |
| 101 | "default": "read" |
| 102 | } |
| 103 | }, |
| 104 | { |
| 105 | "name": "status", |
| 106 | "type": "enum", |
| 107 | "label": "Status", |
| 108 | "default": "draft", |
| 109 | "enum": [ |
| 110 | "draft", |
| 111 | "sent", |
| 112 | "paid", |
| 113 | "partial", |
| 114 | "overdue", |
| 115 | "credit_owed", |
| 116 | "refunded", |
| 117 | "credited", |
| 118 | "void" |
| 119 | ], |
| 120 | "transitions": { |
| 121 | "draft": [ |
| 122 | "sent", |
| 123 | "void" |
| 124 | ], |
| 125 | "sent": [ |
| 126 | "partial", |
| 127 | "overdue", |
| 128 | "paid", |
| 129 | "credit_owed", |
| 130 | "refunded", |
| 131 | "credited", |
| 132 | "void" |
| 133 | ], |
| 134 | "partial": [ |
| 135 | "sent", |
| 136 | "overdue", |
| 137 | "paid", |
| 138 | "credit_owed", |
| 139 | "refunded", |
| 140 | "credited", |
| 141 | "void" |
| 142 | ], |
| 143 | "overdue": [ |
| 144 | "sent", |
| 145 | "partial", |
| 146 | "paid", |
| 147 | "credit_owed", |
| 148 | "refunded", |
| 149 | "credited", |
| 150 | "void" |
| 151 | ], |
| 152 | "paid": [ |
| 153 | "sent", |
| 154 | "partial", |
| 155 | "overdue", |
| 156 | "credit_owed", |
| 157 | "refunded", |
| 158 | "credited" |
| 159 | ], |
| 160 | "credit_owed": [ |
| 161 | "sent", |
| 162 | "partial", |
| 163 | "overdue", |
| 164 | "paid", |
| 165 | "refunded", |
| 166 | "credited" |
| 167 | ], |
| 168 | "refunded": [ |
| 169 | "sent", |
| 170 | "partial", |
| 171 | "overdue", |
| 172 | "paid", |
| 173 | "credit_owed", |
| 174 | "credited" |
| 175 | ], |
| 176 | "credited": [ |
| 177 | "sent", |
| 178 | "partial", |
| 179 | "overdue", |
| 180 | "paid", |
| 181 | "credit_owed", |
| 182 | "refunded" |
| 183 | ] |
| 184 | }, |
| 185 | "help": "Status follows the money (v13, the owner's decision of 2026-11-30): once issued, an invoice's total never changes, and system_invoice_status reads its status off what was received, refunded and credited (object_corrections.money_status) -- paid, partial, sent (overdue is the aging pass's), credit_owed (the shop owes the customer), refunded (credited away and the money given back) or credited (credited away, nothing paid). Any money state may follow any other, because money can move back: a bounced or deleted payment, or a refund with no credit note, reopens a paid invoice and dunning resumes. void stays terminal and is reached only from an unpaid state. Moved by staff (role:manager, role:admin) and by the platform's reactions (app-payments' invoice status handler, the aging pass) -- never by the invoice's owner, who on a machine-raised invoice is the customer being billed: v8 let that owner PUT status=paid, which stopped dunning with the balance unchanged and is what app-shop's fulfillment trusts before shipping. The field permission is the gate; the map below only says which moves exist.", |
| 186 | "permissions": { |
| 187 | "edit": [ |
| 188 | "role:manager", |
| 189 | "role:admin" |
| 190 | ], |
| 191 | "default": "read" |
| 192 | } |
| 193 | }, |
| 194 | { |
| 195 | "name": "issue_date", |
| 196 | "type": "date", |
| 197 | "label": "Issue Date", |
| 198 | "permissions": { |
| 199 | "edit": [ |
| 200 | "role:manager", |
| 201 | "role:admin" |
| 202 | ], |
| 203 | "default": "read" |
| 204 | } |
| 205 | }, |
| 206 | { |
| 207 | "name": "due_date", |
| 208 | "type": "date", |
| 209 | "label": "Due Date", |
| 210 | "permissions": { |
| 211 | "edit": [ |
| 212 | "role:manager", |
| 213 | "role:admin" |
| 214 | ], |
| 215 | "default": "read" |
| 216 | }, |
| 217 | "help": "Staff-only (v9): a due date pushed out by the payer is dunning switched off." |
| 218 | }, |
| 219 | { |
| 220 | "name": "subtotal_cents", |
| 221 | "type": "integer", |
| 222 | "default": "0", |
| 223 | "label": "Subtotal (cents)", |
| 224 | "help": "Stamped by the invoice_totals handler: sum of every line's line_total_cents. Never user-entered -- see package README-equivalent (dbbasic-package.json description) for why this is not schema read_only.", |
| 225 | "permissions": { |
| 226 | "edit": [ |
| 227 | "role:manager", |
| 228 | "role:admin" |
| 229 | ], |
| 230 | "default": "read" |
| 231 | } |
| 232 | }, |
| 233 | { |
| 234 | "name": "tax_cents", |
| 235 | "type": "integer", |
| 236 | "default": "0", |
| 237 | "label": "Tax (cents)", |
| 238 | "help": "The invoice's tax, stamped at issue: computed once per rate group, rounded half-up, and shared out to the lines that state a rate (decision 2, 2026-11-30). invoice_totals folds a draft; an issued invoice's tax is never recalculated, and a credit note gives tax back at the original rate.", |
| 239 | "permissions": { |
| 240 | "edit": [ |
| 241 | "role:manager", |
| 242 | "role:admin" |
| 243 | ], |
| 244 | "default": "read" |
| 245 | } |
| 246 | }, |
| 247 | { |
| 248 | "name": "total_cents", |
| 249 | "type": "integer", |
| 250 | "default": "0", |
| 251 | "label": "Total (cents)", |
| 252 | "help": "Stamped by invoice_totals: subtotal_cents + tax_cents.", |
| 253 | "permissions": { |
| 254 | "edit": [ |
| 255 | "role:manager", |
| 256 | "role:admin" |
| 257 | ], |
| 258 | "default": "read" |
| 259 | } |
| 260 | }, |
| 261 | { |
| 262 | "name": "payments_received_cents", |
| 263 | "type": "computed", |
| 264 | "label": "Payments (cents)", |
| 265 | "rollup": { |
| 266 | "collection": "payments", |
| 267 | "fk_field": "invoice_id", |
| 268 | "op": "sum", |
| 269 | "field": "amount_cents", |
| 270 | "where": { |
| 271 | "status": "received" |
| 272 | } |
| 273 | } |
| 274 | }, |
| 275 | { |
| 276 | "name": "refunded_cents", |
| 277 | "type": "computed", |
| 278 | "label": "Refunded (cents)", |
| 279 | "rollup": { |
| 280 | "collection": "refunds", |
| 281 | "fk_field": "invoice_id", |
| 282 | "op": "sum", |
| 283 | "field": "amount_cents" |
| 284 | } |
| 285 | }, |
| 286 | { |
| 287 | "name": "amount_paid_cents", |
| 288 | "type": "computed", |
| 289 | "label": "Paid (cents)", |
| 290 | "formula": "payments_received_cents - refunded_cents" |
| 291 | }, |
| 292 | { |
| 293 | "name": "credited_cents", |
| 294 | "type": "computed", |
| 295 | "label": "Credited (cents)", |
| 296 | "rollup": { |
| 297 | "collection": "invoice_credits", |
| 298 | "fk_field": "invoice_id", |
| 299 | "op": "sum", |
| 300 | "field": "amount_cents" |
| 301 | } |
| 302 | }, |
| 303 | { |
| 304 | "name": "credit_applied_cents", |
| 305 | "type": "computed", |
| 306 | "label": "Credit Applied (cents)", |
| 307 | "rollup": { |
| 308 | "collection": "invoice_credits", |
| 309 | "fk_field": "credit_note_id", |
| 310 | "op": "sum", |
| 311 | "field": "amount_cents" |
| 312 | } |
| 313 | }, |
| 314 | { |
| 315 | "name": "balance_due_cents", |
| 316 | "type": "computed", |
| 317 | "label": "Balance due (cents)", |
| 318 | "formula": "total_cents - amount_paid_cents - credited_cents - credit_applied_cents" |
| 319 | }, |
| 320 | { |
| 321 | "name": "dunning_level", |
| 322 | "type": "integer", |
| 323 | "default": "0", |
| 324 | "label": "Dunning level", |
| 325 | "permissions": { |
| 326 | "edit": [ |
| 327 | "role:manager", |
| 328 | "role:admin" |
| 329 | ], |
| 330 | "default": "read" |
| 331 | } |
| 332 | }, |
| 333 | { |
| 334 | "name": "last_dunned_on", |
| 335 | "type": "date", |
| 336 | "label": "Last dunned", |
| 337 | "permissions": { |
| 338 | "edit": [ |
| 339 | "role:manager", |
| 340 | "role:admin" |
| 341 | ], |
| 342 | "default": "read" |
| 343 | } |
| 344 | }, |
| 345 | { |
| 346 | "name": "exchange_status", |
| 347 | "type": "text", |
| 348 | "read_only": true, |
| 349 | "label": "Partner Said", |
| 350 | "help": "What a trading partner's box said back about this record when it was sent through the exchange and refused: \"Refused by <partner>: <reason>\"." |
| 351 | }, |
| 352 | { |
| 353 | "name": "notes", |
| 354 | "type": "textarea", |
| 355 | "label": "Notes" |
| 356 | }, |
| 357 | { |
| 358 | "name": "payment_link", |
| 359 | "type": "text", |
| 360 | "label": "Payment Link" |
| 361 | }, |
| 362 | { |
| 363 | "name": "paid_at", |
| 364 | "type": "datetime", |
| 365 | "label": "Paid At", |
| 366 | "permissions": { |
| 367 | "edit": [ |
| 368 | "role:manager", |
| 369 | "role:admin" |
| 370 | ], |
| 371 | "default": "read" |
| 372 | } |
| 373 | }, |
| 374 | { |
| 375 | "name": "public_token", |
| 376 | "type": "text", |
| 377 | "label": "Public Link Token", |
| 378 | "help": "Separate rotatable token for a future public/printable view (plan/vocabulary/20-invoice-spec.md's /i/{token} route). The public page itself is a later slice -- not built here.", |
| 379 | "permissions": { |
| 380 | "edit": [ |
| 381 | "role:manager", |
| 382 | "role:admin" |
| 383 | ], |
| 384 | "default": "read" |
| 385 | } |
| 386 | }, |
| 387 | { |
| 388 | "name": "portal_token", |
| 389 | "type": "text", |
| 390 | "read_only": true, |
| 391 | "label": "Portal Link Token", |
| 392 | "help": "secrets.token_urlsafe(32), minted server-side (action_regenerate_portal_link, or lazily by system_invoice_aging the first time a dunning email needs a working link). Distinct from public_token on purpose -- this token grants a capability (view this one invoice AND see its live balance/payment-instructions state), not merely a read, so it gets its own rotatable credential rather than overloading the printable-view token with a different trust level. read_only because it is never client-submitted; a server-side write uses preserve_read_only, same posture invoice_totals.py documents for stamped totals. Deliberately absent from forms.default.fields, views.list_fields, and search.fields below so the generic form/list/search generators never round-trip it -- and it is further shielded from ever crossing an owner boundary because plain row_filter scoping (permissions/rules.json: {\"owner_id\": \"$user_id\"}) already means a non-owner's list/search call returns zero rows for someone else's invoice, token included." |
| 393 | }, |
| 394 | { |
| 395 | "name": "portal_views", |
| 396 | "type": "integer", |
| 397 | "default": "0", |
| 398 | "label": "Portal Views", |
| 399 | "help": "Stamped by site_invoice_portal on every /pay/{token} view (best-effort -- a counter write must never break the page the customer is trying to read). Dunning intelligence: 'viewed 3x, still unpaid' is a materially different follow-up than 'never opened', per plan/customer-payment-portal-spec.md.", |
| 400 | "permissions": { |
| 401 | "edit": [ |
| 402 | "role:manager", |
| 403 | "role:admin" |
| 404 | ], |
| 405 | "default": "read" |
| 406 | } |
| 407 | }, |
| 408 | { |
| 409 | "name": "last_viewed_at", |
| 410 | "type": "datetime", |
| 411 | "label": "Portal Last Viewed", |
| 412 | "help": "Stamped alongside portal_views on every portal page view. Blank means never opened.", |
| 413 | "permissions": { |
| 414 | "edit": [ |
| 415 | "role:manager", |
| 416 | "role:admin" |
| 417 | ], |
| 418 | "default": "read" |
| 419 | } |
| 420 | }, |
| 421 | { |
| 422 | "name": "source_order_id", |
| 423 | "type": "text", |
| 424 | "label": "Source Order", |
| 425 | "help": "Provenance link to the order this invoice was converted from (real predecessor's convert_to_invoice). No orders/catalog package exists yet in this codebase -- the field is kept so a future app-orders package has somewhere to record it without a backfill.", |
| 426 | "permissions": { |
| 427 | "edit": [ |
| 428 | "role:manager", |
| 429 | "role:admin" |
| 430 | ], |
| 431 | "default": "read" |
| 432 | } |
| 433 | }, |
| 434 | { |
| 435 | "name": "generated_from", |
| 436 | "type": "text", |
| 437 | "read_only": true, |
| 438 | "label": "Generated From", |
| 439 | "help": "Provenance of a machine-raised invoice, e.g. subscriptions/{id}:{period_start} from system_billing_runner. It is what makes a re-run of the biller a no-op, so it is read_only: the marker used to live only inside the editable notes field, and an ordinary edit of the note let the same period be billed a second time. Blank on a hand-entered invoice. Written server-side with preserve_read_only, the same posture portal_token documents." |
| 440 | }, |
| 441 | { |
| 442 | "name": "number_key", |
| 443 | "type": "text", |
| 444 | "read_only": true, |
| 445 | "label": "Number Claim", |
| 446 | "help": "The number again, on an invoice a generator numbered (action_generate_tm_invoice today). Declared unique, so two generated invoices can never share a number however the runs overlap: the header write IS the claim, inside the write lock, and a taken number moves the generator on to the next suffix. Blank on a hand-numbered invoice, which the constraint therefore does not touch -- typed numbers stay the owner's business (v7, 2026-11-28)." |
| 447 | }, |
| 448 | { |
| 449 | "name": "reason_code", |
| 450 | "type": "text", |
| 451 | "label": "Reason code", |
| 452 | "validation": { |
| 453 | "max_length": 64 |
| 454 | }, |
| 455 | "permissions": { |
| 456 | "edit": [ |
| 457 | "role:manager", |
| 458 | "role:admin" |
| 459 | ], |
| 460 | "default": "read" |
| 461 | }, |
| 462 | "description": "On a credit note: why (a correction_reasons code, app-counter)." |
| 463 | }, |
| 464 | { |
| 465 | "name": "approved_by", |
| 466 | "type": "text", |
| 467 | "label": "Approved by", |
| 468 | "validation": { |
| 469 | "max_length": 120 |
| 470 | }, |
| 471 | "permissions": { |
| 472 | "edit": [ |
| 473 | "role:manager", |
| 474 | "role:admin" |
| 475 | ], |
| 476 | "default": "read" |
| 477 | }, |
| 478 | "description": "On a credit note: who approved it." |
| 479 | }, |
| 480 | { |
| 481 | "name": "owner_id", |
| 482 | "type": "text", |
| 483 | "label": "Owner" |
| 484 | }, |
| 485 | { |
| 486 | "name": "created_at", |
| 487 | "type": "datetime", |
| 488 | "label": "Created", |
| 489 | "read_only": true |
| 490 | } |
| 491 | ], |
| 492 | "forms": { |
| 493 | "default": { |
| 494 | "fields": [ |
| 495 | "doc_type", |
| 496 | "credits_invoice_id", |
| 497 | "number", |
| 498 | "customer_id", |
| 499 | "customer_name", |
| 500 | "customer_email", |
| 501 | "customer_address", |
| 502 | "currency", |
| 503 | "issue_date", |
| 504 | "due_date", |
| 505 | "status", |
| 506 | "notes", |
| 507 | "payment_link" |
| 508 | ] |
| 509 | } |
| 510 | }, |
| 511 | "views": { |
| 512 | "list_mode": "table", |
| 513 | "list_fields": [ |
| 514 | "number", |
| 515 | "customer_name", |
| 516 | "status", |
| 517 | "total_cents", |
| 518 | "due_date" |
| 519 | ] |
| 520 | }, |
| 521 | "unique": [ |
| 522 | { |
| 523 | "fields": [ |
| 524 | "number_key" |
| 525 | ], |
| 526 | "message": "That invoice number has already been issued." |
| 527 | } |
| 528 | ], |
| 529 | "search": { |
| 530 | "fields": [ |
| 531 | "number", |
| 532 | "customer_name" |
| 533 | ] |
| 534 | }, |
| 535 | "capabilities": { |
| 536 | "comments": true, |
| 537 | "attachments": true |
| 538 | } |
| 539 | } |