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.
refunds.json · 7.2 KB · 50 lines
| 1 | { |
| 2 | "name": "refunds", |
| 3 | "title": "Refunds", |
| 4 | "version": 7, |
| 5 | "storage": "append", |
| 6 | "description": "A refund is a new compensating record pointing at its payment -- never an edit (docs/logic-decisions.md #3). invoice_id is STAMPED by the hook from the payment (stamp doctrine #1) so invoices can roll refunds up directly; the hook also enforces refund <= payment minus prior refunds. v2: the hook gated create only while the owner could PUT and DELETE, so a refund could be raised past its payment, edited negative, re-pointed at another invoice, or deleted and issued again. hook_refunds now refuses an edit of anything but `reason`, and any delete, for anybody who is not an admin (an admin's edit passes the same gate as a create), and amount_cents carries validation.min 1 so no writer can store a refund of zero or less. v3 adds payment_seq, unique per payment: the order in which refunds landed on their payment. The refundable amount is checked in a hook, before the write lock, so two refunds submitted at once each passed without seeing the other; the hook now stamps the next number from the same read it summed, and the constraint (inside the lock) refuses whichever write no longer holds the next number. v4 retires it: refund <= payment minus the other refunds, not against a bounced payment, and invoice_id equal to the payment's, are a locked check (locked_checks.refundable -> hook_refunds.LOCKED_CHECK) that object_records calls inside the write lock for EVERY writer, holding the ledger lock every write to payments also takes -- so a refund written below the hook, an admin's edit, and a payment bounced or lowered while a refund is in flight are all decided against the ledger as it is when the refund lands. The unique constraint is gone; payment_seq stays as a column (no data dropped), no longer stamped, and blanked when a client sends one. v5: payment_id declares relation.requires update. A registered user's refund needed only its own owner_id to be theirs, so anybody signed in could refund anybody's payment up to its full headroom (the ceiling held; whose payment it was did not). Over the API a refund now needs a writer the engine would let update the payment it refunds -- the person who recorded the payment, an admin, or a role granted it. invoice_id is not checked separately: the hook stamps it from the payment and the locked check refuses any other value. v6 cites what it corrects (Corner Cup, a Haiku simulation of a counter cafe, found refunds carrying no line, only reason text): order_line_id, invoice_line_id and quantity say which item and how many; reason_code is a correction_reasons code; approval_id and approved_by say who allowed it; goodwill_id names the goodwill record a kindness pays out. The locked check also refuses a cited line that is not on the refunded invoice's order and a quantity that would correct more of the line than was sold (refunds and credit-note lines counted together). Like every refund field they are frozen after the write. v7: credit_note_id, the credit note whose money a refund returns (the owner's decision of 2026-11-30: a debt is cancelled only by a credit note, so refunding a sale is a credit note plus a refund and the invoice reads refunded). The locked check refuses a credit note that does not credit the refund's invoice and refunds that would come to more than it is worth; the units are counted once, on the credit note's line.", |
| 7 | "fields": [ |
| 8 | {"name": "id"}, |
| 9 | {"name": "payment_id", "subject": "relation", "required": true, "label": "Payment", |
| 10 | "relation": {"collection": "payments", "display_field": "reference", "requires": "update"}}, |
| 11 | {"name": "invoice_id", "label": "Invoice", |
| 12 | "relation": {"collection": "invoices", "display_field": "number"}}, |
| 13 | {"name": "amount_cents", "type": "integer", "required": true, "label": "Amount (cents)", |
| 14 | "validation": {"min": 1}, |
| 15 | "help": "A positive whole number of cents. Fixed once recorded: if too little was refunded, record another refund."}, |
| 16 | {"name": "reason", "type": "textarea", "label": "Reason"}, |
| 17 | {"name": "refunded_on", "type": "date", "required": true, "label": "Refunded"}, |
| 18 | {"name": "payment_seq", "type": "integer", "label": "Sequence on payment", |
| 19 | "help": "Retired in v4: refunds recorded under v3 carry the order they landed on their payment; newer ones are blank. Nothing reads it, and nobody edits it."}, |
| 20 | {"name": "order_line_id", "type": "text", "label": "Order line", "validation": {"max_length": 64}, |
| 21 | "help": "The order line this refund corrects (a line refund). Refused when the line is not on the refunded invoice's order, or when this quantity, with the refunds and credit notes already citing the line, would correct more than was sold. Blank for a refund that corrects no line (goodwill, a whole bill)."}, |
| 22 | {"name": "invoice_line_id", "type": "text", "label": "Invoice line", "validation": {"max_length": 64}, |
| 23 | "help": "The invoice line that billed it, when there is one."}, |
| 24 | {"name": "quantity", "type": "number", "label": "Quantity", |
| 25 | "help": "How many of the line this refund gives back. Required with order_line_id."}, |
| 26 | {"name": "reason_code", "type": "text", "label": "Reason code", "validation": {"max_length": 64}, |
| 27 | "help": "A correction_reasons code (app-counter): wrong_item_by_staff, quality, goodwill... `reason` stays the free text."}, |
| 28 | {"name": "approval_id", "type": "text", "label": "Approval", "validation": {"max_length": 64}, |
| 29 | "help": "The counter_approvals row a manager gave for this refund, when the person refunding needed one."}, |
| 30 | {"name": "approved_by", "type": "text", "label": "Approved by", "validation": {"max_length": 120}, |
| 31 | "help": "Who approved it: the manager who gave the approval, or the manager who refunded."}, |
| 32 | {"name": "goodwill_id", "type": "text", "label": "Goodwill", "validation": {"max_length": 64}, |
| 33 | "help": "The goodwill record this refund pays out, when the money back is a kindness and the charge was right (app-counter)."}, |
| 34 | {"name": "credit_note_id", "type": "text", "label": "Credit note", "validation": {"max_length": 64}, |
| 35 | "help": "The credit note whose money this refund returns. Refunding a sale is a credit note (which cancels the debt) plus a refund (which returns the money), so the invoice reads refunded with nothing due; a refund with no credit note leaves the debt standing and the invoice reopens. Refused when the credit note does not credit this refund's invoice, or the refunds citing it would come to more than it is worth."}, |
| 36 | {"name": "owner_id", "type": "text"}, |
| 37 | {"name": "created_at", "type": "datetime", "read_only": true, "label": "Created"} |
| 38 | ], |
| 39 | "forms": {"default": {"fields": ["payment_id", "amount_cents", "reason", "refunded_on"]}}, |
| 40 | "views": {"list_mode": "table", "list_fields": ["payment_id", "invoice_id", "amount_cents", "refunded_on"]}, |
| 41 | "locked_checks": [ |
| 42 | {"name": "refundable", |
| 43 | "object": "hook_refunds", |
| 44 | "function": "LOCKED_CHECK", |
| 45 | "reads": ["payments", "order_lines", "orders", "invoices", "invoice_lines", "goodwill"], |
| 46 | "lock": "ledger", |
| 47 | "on": {"create": true, "update": ["amount_cents", "invoice_id", "payment_id", "order_line_id", "quantity", "goodwill_id", "credit_note_id"], "delete": false}} |
| 48 | ], |
| 49 | "hooks": {"before_write": "hook_refunds"} |
| 50 | } |