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
pick_list.py · 8.7 KB · 233 lines
| 1 | """site_pick_list -- everything that is owed and not yet in a box. GET |
| 2 | /pick-list. |
| 3 | |
| 4 | A READ, never a table. There is no pick_list collection and there must |
| 5 | never be one: the list is ordered quantity minus shipped quantity, folded |
| 6 | live, exactly the way stock levels are folded from stock_moves |
| 7 | (object_stock.py) instead of stored. Storing it would mean two answers to |
| 8 | "what is left to pick" that can disagree, and the stored one is always the |
| 9 | one somebody trusts at the wrong moment. |
| 10 | |
| 11 | GROUPED BY PRODUCT, because a picker walks the room once. Two orders each |
| 12 | wanting a mug is one trip to the mug shelf carrying two, not two trips -- |
| 13 | and a per-order list is what makes small warehouses walk the same aisle |
| 14 | five times a morning. The orders each row is for are listed alongside, so |
| 15 | the packing bench can split the pile back out again. |
| 16 | |
| 17 | Oldest first: the order that has waited longest is picked first, which is |
| 18 | the only fair rule that needs no judgement and the one a customer would |
| 19 | choose if asked. |
| 20 | |
| 21 | Sorting by LOCATION walk order -- the bin-typed locations app-catalog |
| 22 | already models -- is the obvious next improvement and is deliberately not |
| 23 | here: nothing in this repo yet records which product lives in which bin, |
| 24 | and a walk order derived from data that does not exist would be a |
| 25 | confident wrong answer. Grouping by product is the honest half of that |
| 26 | idea and is most of its value. |
| 27 | |
| 28 | Requires a signed-in identity, the same gate site_stock uses (and the same |
| 29 | shape: a sign-in prompt, not a 403). Orders are included when they belong |
| 30 | to the signed-in user OR carry no owner at all -- guest web checkout |
| 31 | leaves owner_id blank by design (a shopper is not required to have an |
| 32 | account), so filtering strictly by owner would show an empty warehouse to |
| 33 | the one person who has parcels to send. |
| 34 | """ |
| 35 | |
| 36 | import html |
| 37 | import os |
| 38 | from decimal import Decimal, InvalidOperation |
| 39 | |
| 40 | import object_records |
| 41 | |
| 42 | # What a picker is asked to pick against: an order that has been committed |
| 43 | # and is not yet fully out the door. draft is not a commitment; shipped and |
| 44 | # delivered have nothing left; cancelled must never be picked. |
| 45 | PICKABLE_ORDER_STATUSES = {"confirmed", "processing", "partial"} |
| 46 | |
| 47 | # Shipments in these never reached anybody, so their lines do not reduce |
| 48 | # what is still owed -- the goods have to be picked again. |
| 49 | NOT_DELIVERED_STATUSES = {"lost", "returned_to_sender"} |
| 50 | |
| 51 | |
| 52 | def _base_dir(): |
| 53 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 54 | |
| 55 | |
| 56 | def _text(value): |
| 57 | return str(value if value is not None else "").strip() |
| 58 | |
| 59 | |
| 60 | def _esc(value): |
| 61 | return html.escape(_text(value)) |
| 62 | |
| 63 | |
| 64 | def _quantity(value): |
| 65 | try: |
| 66 | return Decimal(_text(value) or "0") |
| 67 | except InvalidOperation: |
| 68 | return Decimal(0) |
| 69 | |
| 70 | |
| 71 | def _number(value): |
| 72 | return format(value.normalize(), "f") |
| 73 | |
| 74 | |
| 75 | _STYLE = """ |
| 76 | .pick-table { width: 100%; border-collapse: collapse; margin: 0.5rem 0 1.5rem; } |
| 77 | .pick-table th, .pick-table td { text-align: left; padding: 0.4rem 0.6rem; border-bottom: 1px solid var(--line, #38384a); } |
| 78 | .pick-table th { font-weight: 600; } |
| 79 | .pick-table td.num, .pick-table th.num { text-align: right; } |
| 80 | .hint { color: var(--muted, #999); font-size: 0.9rem; } |
| 81 | @media print { |
| 82 | nav, header.app, .noprint, .btn { display: none !important; } |
| 83 | body { background: #fff; color: #000; } |
| 84 | } |
| 85 | """ |
| 86 | |
| 87 | |
| 88 | def _rows_for(base, user_id): |
| 89 | """Fold orders x order lines x shipment lines into one row per product. |
| 90 | |
| 91 | Full scans of four collections: the right cost at this scale (a shop |
| 92 | with a warehouse this size has hundreds of open lines, not millions), |
| 93 | and the same trade site_stock's own summary makes. |
| 94 | """ |
| 95 | try: |
| 96 | orders = object_records.read_collection_records("orders", base_dir=base) |
| 97 | except Exception: |
| 98 | return [] |
| 99 | open_orders = { |
| 100 | order["id"]: order for order in orders |
| 101 | if _text(order.get("status")) in PICKABLE_ORDER_STATUSES |
| 102 | and _text(order.get("doc_type") or "sale") == "sale" |
| 103 | and _text(order.get("owner_id")) in ("", user_id) |
| 104 | } |
| 105 | if not open_orders: |
| 106 | return [] |
| 107 | |
| 108 | try: |
| 109 | order_lines = [line for line in object_records.read_collection_records( |
| 110 | "order_lines", base_dir=base) |
| 111 | if _text(line.get("order_id")) in open_orders] |
| 112 | except Exception: |
| 113 | return [] |
| 114 | |
| 115 | try: |
| 116 | shipments = object_records.read_collection_records("shipments", |
| 117 | base_dir=base) |
| 118 | except Exception: |
| 119 | shipments = [] |
| 120 | counted = {row["id"] for row in shipments |
| 121 | if _text(row.get("status")) not in NOT_DELIVERED_STATUSES} |
| 122 | try: |
| 123 | shipment_lines = object_records.read_collection_records( |
| 124 | "shipment_lines", base_dir=base) |
| 125 | except Exception: |
| 126 | shipment_lines = [] |
| 127 | |
| 128 | shipped = {} |
| 129 | for line in shipment_lines: |
| 130 | if _text(line.get("shipment_id")) not in counted: |
| 131 | continue |
| 132 | key = _text(line.get("order_line_id")) |
| 133 | shipped[key] = shipped.get(key, Decimal(0)) + _quantity(line.get("quantity")) |
| 134 | |
| 135 | try: |
| 136 | products = {row["id"]: row for row in |
| 137 | object_records.read_collection_records("products", |
| 138 | base_dir=base)} |
| 139 | except Exception: |
| 140 | products = {} |
| 141 | |
| 142 | grouped = {} |
| 143 | for line in order_lines: |
| 144 | remaining = _quantity(line.get("quantity")) - shipped.get(line["id"], Decimal(0)) |
| 145 | if remaining <= 0: |
| 146 | continue |
| 147 | order = open_orders[_text(line.get("order_id"))] |
| 148 | product_id = _text(line.get("product_id")) |
| 149 | description = _text(line.get("description")) |
| 150 | # Group by product where there is one; a free-text line groups by |
| 151 | # what it says, which is the only identity it has. |
| 152 | key = product_id or f"text:{description.lower()}" |
| 153 | row = grouped.setdefault(key, { |
| 154 | "product_id": product_id, |
| 155 | "name": (_text(products.get(product_id, {}).get("name")) |
| 156 | or description or product_id), |
| 157 | "sku": _text(products.get(product_id, {}).get("sku")), |
| 158 | "quantity": Decimal(0), |
| 159 | "orders": [], |
| 160 | "oldest": "", |
| 161 | }) |
| 162 | row["quantity"] += remaining |
| 163 | number = _text(order.get("number")) or order["id"] |
| 164 | if number not in row["orders"]: |
| 165 | row["orders"].append(number) |
| 166 | placed = _text(order.get("order_date")) or _text(order.get("created_at"))[:10] |
| 167 | if placed and (not row["oldest"] or placed < row["oldest"]): |
| 168 | row["oldest"] = placed |
| 169 | |
| 170 | rows = list(grouped.values()) |
| 171 | # Oldest waiting first; a row with no date sorts last rather than first, |
| 172 | # because "we do not know when this was ordered" is not a claim to |
| 173 | # urgency. |
| 174 | rows.sort(key=lambda row: (row["oldest"] or "9999-12-31", row["name"])) |
| 175 | return rows |
| 176 | |
| 177 | |
| 178 | def GET(request): |
| 179 | identity = request.get("_identity") or {} |
| 180 | user_id = _text(identity.get("user_id")) |
| 181 | |
| 182 | if not user_id: |
| 183 | body = ('<div class="pagehead"><h1>Pick list</h1></div>' |
| 184 | '<p class="hint"><a href="/login?next=/pick-list">Sign in</a> ' |
| 185 | 'to see what is waiting to be picked.</p>') |
| 186 | else: |
| 187 | rows = _rows_for(_base_dir(), user_id) |
| 188 | table_rows = "".join( |
| 189 | "<tr>" |
| 190 | f"<td>{_esc(row['name'])}</td>" |
| 191 | f"<td>{_esc(row['sku'])}</td>" |
| 192 | f"<td class=\"num\">{_esc(_number(row['quantity']))}</td>" |
| 193 | f"<td>{_esc(', '.join(row['orders']))}</td>" |
| 194 | f"<td>{_esc(row['oldest'])}</td>" |
| 195 | "</tr>" |
| 196 | for row in rows |
| 197 | ) or ('<tr><td colspan="5" class="hint">Nothing is waiting to be ' |
| 198 | 'picked.</td></tr>') |
| 199 | |
| 200 | body = f""" |
| 201 | <div class="breadcrumb noprint"><a href="/">Home</a> / Pick list</div> |
| 202 | <div class="pagehead"><h1>Pick list</h1></div> |
| 203 | <p class="hint">Everything ordered and not yet on a shipment, grouped by |
| 204 | product so each shelf is visited once. Oldest orders first. This is a read |
| 205 | of the order and shipment records, never a stored list.</p> |
| 206 | <table class="pick-table"> |
| 207 | <thead><tr><th>Product</th><th>SKU</th><th class="num">To pick</th><th>Orders</th><th>Oldest</th></tr></thead> |
| 208 | <tbody>{table_rows}</tbody> |
| 209 | </table> |
| 210 | <p class="hint noprint">Pack a shipment from an order to take lines off this |
| 211 | list.</p> |
| 212 | """ |
| 213 | |
| 214 | who = (f"signed in as <strong>{_esc(user_id)}</strong>" if user_id |
| 215 | else '<a href="/login?next=/pick-list">sign in</a>') |
| 216 | page = f"""<!doctype html> |
| 217 | <html lang="en"> |
| 218 | <head> |
| 219 | <meta charset="utf-8"> |
| 220 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 221 | <title>Pick list</title> |
| 222 | <link rel="stylesheet" href="/style"> |
| 223 | <style>{_STYLE}</style> |
| 224 | </head> |
| 225 | <body> |
| 226 | <div class="wrap"> |
| 227 | <header class="app"><h1><a href="/">DBBASIC</a></h1><div class="who">{who}</div></header> |
| 228 | {body} |
| 229 | </div> |
| 230 | <script src="/nav"></script> |
| 231 | </body> |
| 232 | </html>""" |
| 233 | return {"content_type": "text/html; charset=utf-8", "body": page} |