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
stock_moves.py · 4.2 KB · 101 lines
| 1 | """Pre-write hook for stock_moves: loss-shape gates + cost stamping. |
| 2 | |
| 3 | Two jobs (plan/inventory-adjustments-spec.md): |
| 4 | |
| 5 | - GATES the schema can't express: quantity must be positive (direction |
| 6 | comes from from/to locations, corrections are new compensating moves -- |
| 7 | never negative quantities; the numeric-range repeat counter ticks again, |
| 8 | see docs/logic-decisions.md #4), and a LOSS move (waste/breakage/theft/ |
| 9 | expiry/damage/disaster) must leave a real location and have no |
| 10 | destination -- you cannot waste goods INTO a shelf. |
| 11 | |
| 12 | - STAMPS unit_cost_cents when the client omitted it (docs/logic-decisions.md |
| 13 | #1: the cost at the moment of movement; editing the product's cost |
| 14 | tomorrow must not reprice yesterday's loss). A client-supplied cost is |
| 15 | kept -- purchases carry their invoice cost. A SALE is stamped with the |
| 16 | moving weighted average of stock actually on hand; everything else, and |
| 17 | a sale with no priced acquisitions behind it, falls back to |
| 18 | products.cost_cents. |
| 19 | |
| 20 | Create-only: stock_moves is an append collection, so updates never carry |
| 21 | business meaning here. |
| 22 | """ |
| 23 | |
| 24 | import os |
| 25 | from decimal import Decimal, InvalidOperation |
| 26 | |
| 27 | import object_records |
| 28 | import object_stock |
| 29 | |
| 30 | LOSS_REASONS = {"waste", "breakage", "theft", "expiry", "damage", "disaster"} |
| 31 | |
| 32 | |
| 33 | def _base_dir(): |
| 34 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 35 | |
| 36 | |
| 37 | def BEFORE_WRITE(request): |
| 38 | if request.get("action") != "create": |
| 39 | return None |
| 40 | record = request.get("record") or {} |
| 41 | |
| 42 | try: |
| 43 | quantity = Decimal(str(record.get("quantity") or "0")) |
| 44 | except InvalidOperation: |
| 45 | return None # schema validation reports the type error properly |
| 46 | if quantity <= 0: |
| 47 | return {"error": "Stock move quantity must be positive -- direction " |
| 48 | "comes from the from/to locations, and corrections " |
| 49 | "are new compensating moves, never negative rows.", |
| 50 | "status": 400} |
| 51 | |
| 52 | reason = (record.get("reason") or "").strip() |
| 53 | if reason in LOSS_REASONS: |
| 54 | if not (record.get("from_location_id") or "").strip(): |
| 55 | return {"error": f"A {reason} move must say which location the " |
| 56 | "goods left (from_location_id is required for " |
| 57 | "loss reasons).", |
| 58 | "status": 400} |
| 59 | if (record.get("to_location_id") or "").strip(): |
| 60 | return {"error": f"A {reason} move cannot have a destination -- " |
| 61 | "lost goods leave the system. Clear " |
| 62 | "to_location_id, or use reason=transfer.", |
| 63 | "status": 400} |
| 64 | |
| 65 | if not str(record.get("unit_cost_cents") or "").strip(): |
| 66 | product_id = record.get("product_id") or "" |
| 67 | |
| 68 | # A SALE is valued, not priced. The cost that leaves with the goods |
| 69 | # is the moving weighted average of what is actually on the shelf |
| 70 | # (object_stock.weighted_average_cost_cents) -- computed here, |
| 71 | # BEFORE this move joins the log, so it is the average at the |
| 72 | # moment of sale and excludes the sale itself. |
| 73 | # |
| 74 | # products.cost_cents is a standard cost: a planning figure someone |
| 75 | # typed, not what was paid. It stays the fallback for a product |
| 76 | # with no priced acquisitions yet, and it stays the primary for |
| 77 | # losses -- writing off a broken mug at standard cost is fine, |
| 78 | # while reporting cost of SALES at a typed-in number is how gross |
| 79 | # margin quietly becomes fiction. |
| 80 | if reason == "sale": |
| 81 | try: |
| 82 | average = object_stock.average_cost_for_product( |
| 83 | product_id, base_dir=_base_dir()) |
| 84 | except Exception: |
| 85 | average = None |
| 86 | if average: |
| 87 | stamped = dict(record) |
| 88 | stamped["unit_cost_cents"] = str(average) |
| 89 | return {"record": stamped} |
| 90 | |
| 91 | try: |
| 92 | product = object_records.get_collection_record( |
| 93 | "products", product_id, base_dir=_base_dir()) |
| 94 | except Exception: |
| 95 | return None # relation validation owns a missing product |
| 96 | cost = str(product.get("cost_cents") or "").strip() |
| 97 | if cost: |
| 98 | stamped = dict(record) |
| 99 | stamped["unit_cost_cents"] = cost |
| 100 | return {"record": stamped} |
| 101 | return None |