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