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.
counter_pay.py · 5.1 KB · 107 lines
| 1 | """action_counter_pay -- a cashier takes payment for a sale at the counter. |
| 2 | |
| 3 | POST {"invoice_id": "..." | "order_id": "...", "amount_cents": "650", "method": "cash"} |
| 4 | |
| 5 | Payments are recorded by role:manager over the records API (app-payments |
| 6 | 0.8.0 took the grant away from customers, who owned the invoices they could |
| 7 | mark paid). A cashier is the person who actually takes the money at a |
| 8 | counter, and Corner Cup's cashiers had no way to record it except the |
| 9 | owner's login. Granting cashiers create on payments would not do: a |
| 10 | payment's invoice_id declares relation.requires update, so they would also |
| 11 | need update on invoices -- the power to reprice and void bills. |
| 12 | |
| 13 | So this action records it, in process, for a bill the caller may read and |
| 14 | that is issued (not a draft, a void, a quote or a credit note) with money |
| 15 | still open on it; anything else gets the answer a missing id gets, before |
| 16 | anything is written. The amount defaults to the open balance and may not |
| 17 | exceed it, and the ledger's locked check (hook_payments.LOCKED_CHECK, every |
| 18 | writer) still refuses anything over it. Only the roles app-counter's rules |
| 19 | give execute on this action run it (object_corrections.staff_refusal), |
| 20 | whatever the server's enforcement setting. |
| 21 | The payment is filed under the bill's owner, as checkout and the Stripe |
| 22 | webhook file theirs, and attributed to the cashier in the change log |
| 23 | (actor "action_counter_pay:<user>"). |
| 24 | """ |
| 25 | |
| 26 | from __future__ import annotations |
| 27 | |
| 28 | import os |
| 29 | from datetime import date |
| 30 | |
| 31 | import object_access |
| 32 | import object_corrections |
| 33 | import object_ids |
| 34 | import object_record_changes |
| 35 | import object_records |
| 36 | |
| 37 | ACTOR = "action_counter_pay" |
| 38 | METHODS = ("cash", "card", "check", "transfer", "other") |
| 39 | |
| 40 | |
| 41 | def _base_dir(): |
| 42 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 43 | |
| 44 | |
| 45 | def _post(request): |
| 46 | text = object_corrections.text |
| 47 | base = _base_dir() |
| 48 | access = object_access.Access(request.get("_identity"), base_dir=base) |
| 49 | if not access.signed_in: |
| 50 | return {"status": 401, "error": "Sign in to take a payment."} |
| 51 | staff = object_corrections.staff_refusal(access, "action_counter_pay", doing="take a payment here") |
| 52 | if staff: |
| 53 | return staff |
| 54 | invoice = None |
| 55 | if text(request.get("invoice_id")): |
| 56 | invoice = access.get("invoices", text(request.get("invoice_id"))) |
| 57 | elif text(request.get("order_id")): |
| 58 | order = access.get("orders", text(request.get("order_id"))) |
| 59 | if order is not None: |
| 60 | # access: filtered to the order read through access.get above, and each bill asked about with access.can |
| 61 | bills = object_corrections.bills_for_order( |
| 62 | order, object_records.read_collection_records("invoices", base_dir=base)) |
| 63 | bills = [b for b in bills if object_corrections.issued(b) |
| 64 | and access.can("read", "invoices", b)] |
| 65 | # The one still owing first; a settled one answers "nothing is owed". |
| 66 | bills.sort(key=lambda b: object_corrections.cents(b.get("balance_due_cents")) <= 0) |
| 67 | invoice = bills[0] if bills else None |
| 68 | # Only an issued bill takes a payment here: not a draft, a void, a quote |
| 69 | # or a credit note, whichever way it was named. All of those get the |
| 70 | # answer a missing or unreadable id gets, before anything is written. |
| 71 | if not object_corrections.issued(invoice): |
| 72 | return {"status": 404, "error": "No bill to take payment for."} |
| 73 | method = text(request.get("method") or "cash") |
| 74 | if method not in METHODS: |
| 75 | return {"status": 400, "error": f"method is one of {', '.join(METHODS)}."} |
| 76 | balance = object_corrections.cents(invoice.get("balance_due_cents")) |
| 77 | if balance <= 0: |
| 78 | return {"status": 409, "error": "Nothing is owed on this bill."} |
| 79 | amount = object_corrections.cents(request.get("amount_cents") or balance) |
| 80 | if amount <= 0: |
| 81 | return {"status": 400, "error": "amount_cents is a whole number of cents above zero."} |
| 82 | if amount > balance: |
| 83 | return {"status": 409, "error": f"This bill has {balance} open; nothing was recorded."} |
| 84 | row = {"id": object_ids.new_uuid4(), "invoice_id": text(invoice.get("id")), |
| 85 | "amount_cents": str(amount), "method": method, |
| 86 | "received_on": date.today().isoformat(), "status": "received", |
| 87 | "reference": text(request.get("reference"))[:120], |
| 88 | "owner_id": text(invoice.get("owner_id"))} |
| 89 | try: |
| 90 | # access: system write after access check above (access.get on the bill) |
| 91 | stored = object_records.create_collection_record( |
| 92 | "payments", row, base_dir=base, actor=f"{ACTOR}:{access.user_id}") |
| 93 | except object_records.CheckRefusedError as exc: |
| 94 | return {"status": exc.status, "error": str(exc)} |
| 95 | return {"status": 200, "ok": True, "payment_id": stored["id"], |
| 96 | "invoice_id": row["invoice_id"], "amount_cents": amount} |
| 97 | |
| 98 | |
| 99 | def _t(value): |
| 100 | return str(value if value is not None else "").strip() |
| 101 | |
| 102 | |
| 103 | def POST(request): |
| 104 | """Every record change this action writes carries why, as the history |
| 105 | entry's message (object_record_changes.change_reason).""" |
| 106 | with object_record_changes.change_reason(f"payment taken at the counter ({_t(request.get('method')) or 'cash'})"): |
| 107 | return _post(request) |