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.
invoice_line_freeze.py · 3.8 KB · 94 lines
| 1 | """system_invoice_line_freeze -- an invoice line on an issued or paid bill |
| 2 | is not rewritten in place. |
| 3 | |
| 4 | The locked check `settled_line` on invoice_lines (lock `ledger`). The rule |
| 5 | and its reasons live in object_corrections, shared with app-orders' |
| 6 | system_line_freeze so an order line and the invoice line billing it are |
| 7 | frozen by the same sentence. Corner Cup edited a paid invoice line from soup |
| 8 | to salad and left a "paid" invoice with 150 due; the order said something |
| 9 | else again. |
| 10 | |
| 11 | A line may still be ADDED to an issued, unpaid invoice (checkout raises the |
| 12 | invoice as sent and then writes its lines), never to a paid one. Fails |
| 13 | closed on an unreadable invoice or payments. |
| 14 | """ |
| 15 | |
| 16 | import os |
| 17 | |
| 18 | import object_collections |
| 19 | import object_corrections |
| 20 | import object_records |
| 21 | |
| 22 | |
| 23 | def _base_dir(): |
| 24 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 25 | |
| 26 | |
| 27 | def _invoice(invoice_id, base): |
| 28 | if not invoice_id: |
| 29 | return None |
| 30 | try: |
| 31 | return object_records.get_collection_record("invoices", invoice_id, base_dir=base) |
| 32 | except (object_records.RecordNotFoundError, object_records.InvalidRecordIdError, |
| 33 | object_collections.CollectionNotFoundError): |
| 34 | return None |
| 35 | |
| 36 | |
| 37 | def _rows(collection, base): |
| 38 | try: |
| 39 | return object_records.read_collection_records(collection, base_dir=base) |
| 40 | except object_collections.CollectionNotFoundError: |
| 41 | return [] |
| 42 | |
| 43 | |
| 44 | def _credit_ceiling(record, base): |
| 45 | """A credit-note line citing an order line must not, with the refunds |
| 46 | and other credit-note lines citing it, correct more than was sold: a |
| 47 | refund and a credit note for the same soup are one correction made |
| 48 | twice. Raises on an unreadable collection (the caller answers 503).""" |
| 49 | line_id = object_corrections.text(record.get("order_line_id")) |
| 50 | if not line_id: |
| 51 | return None |
| 52 | line = next((row for row in _rows("order_lines", base) |
| 53 | if object_corrections.text(row.get("id")) == line_id), None) |
| 54 | already = object_corrections.corrected_quantity( |
| 55 | line_id, refunds=_rows("refunds", base), invoices=_rows("invoices", base), |
| 56 | invoice_lines=_rows("invoice_lines", base)) |
| 57 | return object_corrections.ceiling_refusal(line, record.get("quantity"), already) |
| 58 | |
| 59 | |
| 60 | def LOCKED_CHECK(request): |
| 61 | action = request.get("action") |
| 62 | record = request.get("record") or {} |
| 63 | existing = request.get("existing") |
| 64 | base = request.get("base_dir") or _base_dir() |
| 65 | text = object_corrections.text |
| 66 | invoice_ids = {text(record.get("invoice_id"))} |
| 67 | if existing: |
| 68 | invoice_ids.add(text(existing.get("invoice_id"))) |
| 69 | try: |
| 70 | try: |
| 71 | payments = object_records.read_collection_records("payments", base_dir=base) |
| 72 | except object_collections.CollectionNotFoundError: |
| 73 | payments = [] |
| 74 | worst = None |
| 75 | for invoice_id in sorted(i for i in invoice_ids if i): |
| 76 | invoice = _invoice(invoice_id, base) |
| 77 | if invoice and text(invoice.get("doc_type")) == "credit_note" and action == "create": |
| 78 | worst = worst or _credit_ceiling(record, base) |
| 79 | continue |
| 80 | if not object_corrections.is_bill(invoice): |
| 81 | continue |
| 82 | refusal = object_corrections.line_refusal( |
| 83 | kind="invoice", action=action, existing=existing, record=record, |
| 84 | issued_bill=object_corrections.issued(invoice), |
| 85 | received=object_corrections.received_cents([invoice_id], payments)) |
| 86 | worst = worst or refusal |
| 87 | except Exception as exc: # noqa: BLE001 |
| 88 | return {"status": 503, |
| 89 | "error": (f"Whether this invoice has been issued or paid cannot be read " |
| 90 | f"right now ({str(exc)[:120]}), so the line was not changed. " |
| 91 | f"Try again in a moment.")} |
| 92 | if worst: |
| 93 | return {"status": 409, "error": worst} |
| 94 | return None |