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
receiving_sheet.py · 11.1 KB · 285 lines
1"""site_receiving_sheet -- the clipboard. GET
2/purchase-orders/{order_id}/receive.
3
4The sheet exists because the dock is where the paperwork actually happens,
5and a clipboard beats a phone in a cold warehouse. Somebody is standing by
6a roller door with gloves on, a driver waiting, and a pallet that has to be
7counted before it can be signed for. A screen requiring a scroll, a login
8that timed out, or a touch target the size of a pea are not neutral in that
9room: they are the reason the count gets done from memory, at a desk,
10twenty minutes later, from what the delivery note claimed. Printed columns
11for received and rejected -- left BLANK on purpose -- are what make the
12count happen where the goods are, which is the only place it is worth
13anything.
14
15A READ, never a table. It folds the purchase order's lines against every
16prior receipt, so what it shows is what is genuinely still outstanding and
17not what somebody remembered to update. Prior receipts are summarised
18underneath rather than hidden: a second delivery against a partly-received
19PO is exactly when a receiver needs to see that eight of the ten already
20arrived last Tuesday, and its absence is how the same carton gets booked in
21twice.
22
23Rejected has its own column, next to received and never folded into it,
24because they are different arguments with the supplier: a short delivery
25means goods that were never sent and must never be invoiced; a rejection
26means goods that arrived, that we hold, and that we want credit for. One
27column for both is how a supplier gets paid for what they never delivered.
28
29Requires a signed-in identity, the same gate site_pick_list uses (and the
30same shape: a sign-in prompt, not a 403). An unknown or non-purchase order
31is a friendly page in the same shell, never a traceback: a mistyped link is
32somebody who is still trying to receive a delivery.
33
34Signed in is not enough: the purchase order must be one the caller may
35read through the orders API, asked of the permission engine
36(object_access). This page reads storage directly, so it is the only gate,
37and it used to open any purchase order by id -- supplier, number, lines --
38for any signed-in user (bug-reports/2026-11-28-06-14-30). A purchase order
39the caller may not read gets the same "no purchase order with that id" as
40a mistyped one. Its lines and receipts are shown as parts of the order:
41the order is the unit of access, and a sheet that silently dropped a
42receipt would invite booking the same carton in twice.
43"""
44
45import html
46import os
47from decimal import Decimal, InvalidOperation
48
49import object_access
50import object_records
51
52# A receipt abandoned before anything was counted describes no goods.
53NOT_RECEIVED_STATUSES = {"cancelled"}
54
55
56def _base_dir():
57 return os.environ.get("DBBASIC_DATA_DIR", "data")
58
59
60def _text(value):
61 return str(value if value is not None else "").strip()
62
63
64def _esc(value):
65 return html.escape(_text(value))
66
67
68def _quantity(value):
69 try:
70 return Decimal(_text(value) or "0")
71 except InvalidOperation:
72 return Decimal(0)
73
74
75def _number(value):
76 return format(value.normalize(), "f")
77
78
79_STYLE = """
80.wrap { max-width: 820px; margin: 0 auto; padding: 2rem 1.25rem; }
81.pagehead { margin-bottom: 1rem; }
82.pagehead h1 { margin: 0 0 0.25rem; font-size: 1.4rem; }
83.hint { color: var(--muted, #999); font-size: 0.9rem; }
84.supplier { border: 1px solid var(--line, #333); border-radius: 8px; padding: 0.9rem 1.1rem; margin: 1rem 0; }
85table.sheet { width: 100%; border-collapse: collapse; margin: 1rem 0 1.5rem; }
86table.sheet th, table.sheet td { text-align: left; padding: 0.45rem 0.6rem; border-bottom: 1px solid var(--line, #38384a); }
87table.sheet td.num, table.sheet th.num { text-align: right; }
88table.sheet td.blank { border-bottom: 1px solid var(--line, #38384a); min-width: 5rem; }
89.sign { margin-top: 2rem; }
90.sign span { display: inline-block; border-bottom: 1px solid var(--line, #999); min-width: 14rem; }
91.notfound { text-align: center; padding: 3rem 1rem; }
92@media print {
93 nav, header.app, .noprint, .btn { display: none !important; }
94 .wrap { max-width: none; padding: 0; }
95 body { background: #fff; color: #000; }
96 .supplier, table.sheet th, table.sheet td { border-color: #999; }
97}
98"""
99
100
101def _page(body, *, title, status=200):
102 page = {
103 "content_type": "text/html; charset=utf-8",
104 "body": f"""<!doctype html>
105<html lang="en">
106<head>
107<meta charset="utf-8">
108<meta name="viewport" content="width=device-width, initial-scale=1">
109<title>{_esc(title)}</title>
110<link rel="stylesheet" href="/style">
111<style>{_STYLE}</style>
112</head>
113<body>
114<div class="wrap">
115{body}
116</div>
117<script src="/nav"></script>
118</body>
119</html>""",
120 }
121 if status != 200:
122 page["status"] = status
123 return page
124
125
126def _not_found(message):
127 return _page(
128 f'<div class="notfound"><h1>Not found</h1><p class="hint">{message}</p>'
129 '</div>',
130 title="Receiving sheet -- not found", status=404)
131
132
133def _received_by_line(base, order_id):
134 """Cumulative received quantity per order line, plus the receipts and
135 lines it was folded from -- one read of the collections, used by both
136 tables on the page."""
137 try:
138 # access: children of the purchase order read through Access above
139 receipts = [row for row in object_records.read_collection_records(
140 "receipts", base_dir=base)
141 if _text(row.get("order_id")) == order_id
142 and _text(row.get("status")) not in NOT_RECEIVED_STATUSES]
143 except Exception:
144 receipts = []
145 counted = {row["id"]: row for row in receipts}
146 try:
147 # access: children of the purchase order read through Access above
148 lines = [row for row in object_records.read_collection_records(
149 "receipt_lines", base_dir=base)
150 if _text(row.get("receipt_id")) in counted]
151 except Exception:
152 lines = []
153
154 received = {}
155 for line in lines:
156 key = _text(line.get("order_line_id"))
157 received[key] = received.get(key, Decimal(0)) + _quantity(
158 line.get("quantity_received"))
159 return received, list(counted.values()), lines
160
161
162def _lines_html(order_lines, received):
163 if not order_lines:
164 return ('<p class="hint">This purchase order has no lines, so there '
165 'is nothing anybody could be delivering against it.</p>')
166 rows = []
167 for line in order_lines:
168 ordered = _quantity(line.get("quantity"))
169 already = received.get(line["id"], Decimal(0))
170 outstanding = ordered - already
171 rows.append(
172 "<tr>"
173 f"<td>{_esc(line.get('description')) or _esc(line.get('product_id'))}</td>"
174 f"<td class=\"num\">{_esc(_number(ordered))}</td>"
175 f"<td class=\"num\">{_esc(_number(already))}</td>"
176 f"<td class=\"num\">{_esc(_number(outstanding if outstanding > 0 else Decimal(0)))}</td>"
177 "<td class=\"blank\">&nbsp;</td>"
178 "<td class=\"blank\">&nbsp;</td>"
179 "</tr>")
180 return f"""
181<table class="sheet">
182<thead><tr>
183<th>Item</th><th class="num">Ordered</th><th class="num">Already in</th>
184<th class="num">Expected today</th><th>Received</th><th>Rejected</th>
185</tr></thead>
186<tbody>{''.join(rows)}</tbody>
187</table>"""
188
189
190def _prior_html(receipts, lines):
191 if not receipts:
192 return ('<p class="hint">No deliveries have been booked in against '
193 'this purchase order yet.</p>')
194 by_receipt = {}
195 for line in lines:
196 key = _text(line.get("receipt_id"))
197 entry = by_receipt.setdefault(key, [Decimal(0), Decimal(0)])
198 entry[0] += _quantity(line.get("quantity_received"))
199 entry[1] += _quantity(line.get("quantity_rejected"))
200 receipts = sorted(receipts, key=lambda row: _text(row.get("received_on")))
201 rows = "".join(
202 "<tr>"
203 f"<td>{_esc(row.get('received_on')) or 'no date'}</td>"
204 f"<td>{_esc(row.get('supplier_reference')) or '&mdash;'}</td>"
205 f"<td>{_esc(row.get('status'))}</td>"
206 f"<td class=\"num\">{_esc(_number(by_receipt.get(row['id'], [Decimal(0), Decimal(0)])[0]))}</td>"
207 f"<td class=\"num\">{_esc(_number(by_receipt.get(row['id'], [Decimal(0), Decimal(0)])[1]))}</td>"
208 "</tr>"
209 for row in receipts)
210 return f"""
211<h2>Already received</h2>
212<table class="sheet">
213<thead><tr><th>Date</th><th>Their reference</th><th>Status</th>
214<th class="num">Received</th><th class="num">Rejected</th></tr></thead>
215<tbody>{rows}</tbody>
216</table>"""
217
218
219def GET(request):
220 identity = request.get("_identity") or {}
221 user_id = _text(identity.get("user_id"))
222 base = _base_dir()
223 order_id = _text(request.get("order_id"))
224
225 if not user_id:
226 return _page(
227 '<div class="pagehead"><h1>Receiving sheet</h1></div>'
228 f'<p class="hint"><a href="/login?next=/purchase-orders/{_esc(order_id)}/receive">'
229 'Sign in</a> to check a delivery in.</p>',
230 title="Receiving sheet")
231
232 if not order_id:
233 return _not_found("No purchase order was named in that link.")
234 try:
235 order = object_access.Access(identity, base_dir=base).get(
236 "orders", order_id)
237 except Exception:
238 order = None
239 if not order:
240 return _not_found("There is no purchase order with that id. It may "
241 "have been mistyped.")
242 if _text(order.get("doc_type")) != "purchase":
243 return _not_found(
244 "That is a sales order. Goods leave on a shipment and arrive on a "
245 "receipt, so there is no delivery to check in here -- "
246 '<a href="/pick-list">the pick list</a> is the sheet for orders '
247 "going out.")
248
249 try:
250 # access: children of the purchase order read through Access above
251 order_lines = [row for row in object_records.read_collection_records(
252 "order_lines", base_dir=base)
253 if _text(row.get("order_id")) == order_id]
254 except Exception:
255 order_lines = []
256 order_lines.sort(key=lambda row: _text(row.get("description")))
257
258 received, receipts, receipt_lines = _received_by_line(base, order_id)
259
260 number = _text(order.get("number")) or order_id
261 supplier = _text(order.get("customer_name")) or "No supplier on this order"
262 expected = _text(order.get("expected_date"))
263
264 body = f"""
265<div class="breadcrumb noprint"><a href="/">Home</a> / Receiving</div>
266<div class="pagehead">
267<h1>Receiving sheet</h1>
268<p class="hint">Purchase order {_esc(number)}
269&middot; {_esc(order.get('status'))}
270{('&middot; expected ' + _esc(expected)) if expected else ''}</p>
271</div>
272<div class="supplier"><strong>Supplier</strong><br>{_esc(supplier)}</div>
273{_lines_html(order_lines, received)}
274<p class="hint">Count the pallet against "expected today", write what
275actually turned up in the blank columns, and put anything damaged or wrong
276in Rejected -- rejected goods arrived and are going back, which is not the
277same argument as a short delivery and must never be folded into it.</p>
278{_prior_html(receipts, receipt_lines)}
279<div class="sign"><p>Received by <span>&nbsp;</span> &nbsp; Date
280<span>&nbsp;</span></p>
281<p>Their delivery note number <span>&nbsp;</span></p></div>
282<p class="hint noprint">Type the counted sheet in with Receive goods against
283this purchase order.</p>
284"""
285 return _page(body, title=f"Receiving sheet -- {number}")