Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 49 minutes ago History
invoice_line_freeze.py · 3.8 KB · 94 lines
1"""system_invoice_line_freeze -- an invoice line on an issued or paid bill
2is not rewritten in place.
3
4The locked check `settled_line` on invoice_lines (lock `ledger`). The rule
5and its reasons live in object_corrections, shared with app-orders'
6system_line_freeze so an order line and the invoice line billing it are
7frozen by the same sentence. Corner Cup edited a paid invoice line from soup
8to salad and left a "paid" invoice with 150 due; the order said something
9else again.
10
11A line may still be ADDED to an issued, unpaid invoice (checkout raises the
12invoice as sent and then writes its lines), never to a paid one. Fails
13closed on an unreadable invoice or payments.
14"""
15
16import os
17
18import object_collections
19import object_corrections
20import object_records
21
22
23def _base_dir():
24 return os.environ.get("DBBASIC_DATA_DIR", "data")
25
26
27def _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
37def _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
44def _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
60def 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