Skip to content

Pricing

Dmitri Chudinov edited this page Aug 28, 2026 · 1 revision

Pricing

The most carefully guarded module in the system. Pure, deterministic, versioned, no I/O — enforced by an import contract, not by good intentions.

def price(spec: PriceSpec, rates: RateSnapshot, tier: CustomerTier | None = None) -> Breakdown

contexts/pricing may import only core. No sqlalchemy, no datetime.now, no floats. That restriction is what buys everything on this page.


1. Why purity is the whole design

What the product needs How purity delivers it
A transparent price structure The Breakdown is the structure. The UI renders line items and their bases; it computes nothing
"+120 ₽ labour, −260 ₽ material" per option change diff(price(spec_a, r), price(spec_b, r)) grouped by code. Any other approach ends with the UI re-implementing the arithmetic and drifting
A quote reproducible years later The order stores rate_snapshot_id and engine_version; the exact quote can be recomputed
Storefront and console agreeing There is one function. There cannot be a second
Testability Property tests: monotonic in quantity, the discount ladder never inverts, total equals the sum of the items, no float drift

2. The three types

PriceSpec — everything that affects price: the geometry estimate (or exact values from a PreparedPlate), material spec and colours, scale, finishes, quantity, rush, delivery method, customer tier code.

RateSnapshot — an immutable, content-hashed bundle of every rate used for one calculation. Its primary key is its own content hash, so identical rates are one row by construction. Defaults as shipped:

Field Default
labor_rate_per_hour 600 ₽
labor_hours_per_print_hour 0.05 supervision
labor_hours_per_job 0.25 setup handling
engineering_hours_per_resize 0.5
postprocess_rate_per_hour 500 ₽
electricity_rate_per_kwh 6.50 ₽
printer_power_kw 0.35
depreciation_per_printer_hour 35 ₽
material_procurement_flat 500 ₽ charged once per order, not per unit
packaging_per_unit 40 ₽
shipping_flat 400 ₽
overhead_per_print_hour 25 ₽
failure_buffer_percent 7 %
rush_surcharge_percent 25 %
margin_percent 30 %
multicolor_purge_grams_per_extra_color 12 g
guard_tier_cliffs true see §5

Breakdown — typed line items, each carrying its basis so the UI can explain itself:

LineItem(code="labor.supervision",
         label_ru="Труд · надзор за печатью",
         amount=Money("1_680.00", RUB),
         basis="4.2 ч × 400 ₽/ч",
         category=COST)

3. The line codes

These are the contract between the engine and the clients' message catalogues, so they are stable identifiers. Renaming one silently blanks a row in the customer's price breakdown — treat a change as a breaking API change. A coverage test proves no line can reach a customer without both an RU and an EN label.

Code What it is
material.filament The filament the part consumes
material.purge Purge waste for multicolour changes
material.procurement Buying in a filament the farm does not hold — once per order, because one procurement covers the whole plate however many copies it makes
machine.electricity Power
machine.depreciation Machine wear
labor.supervision Watching the print
labor.setup Setup handling
labor.engineering A resize
postprocess.* One per chosen finish
logistics.packaging
logistics.shipping Omitted entirely for pickup — not printed as a zero
overhead.general Per print hour
risk.failure_buffer
adjustment.rush
adjustment.volume_discount
adjustment.customer_discount The loyalty or negotiated tier
margin.profit Applied last

4. What scales with quantity, and what does not

Stated once, explicitly, because this is exactly where an earlier version of this system had two calculators silently disagreeing — its web copy multiplied packaging, shipping and flat labour by quantity and its desktop copy did not.

Per unit, times quantity material, machine time, supervision labour, post-processing, packaging
Once per job setup handling, engineering (resize), shipping, material procurement
Derived overhead (per print hour), failure buffer, rush, discount, margin

Order of operations is fixed and tested: production cost → burden → commercial adjustments → margin last.

5. The discount ladder, and the cliff guard

A plain step ladder has a cliff at every threshold. With a 5 % tier at 10 units and a 12 % tier at 50, ordering 50 units costs less than ordering 49 — so the farm prints one more part, spends more material and machine time, and is paid less for it. The customer is not being clever; the pricing rule is simply wrong at that point.

The guard caps the discount at each threshold so the total never decreases as quantity grows. The customer still gets a genuine volume discount; it is just never so large that 50 undercuts 49. Within a tier the total always rises with quantity, so checking each threshold against the unit below it makes the whole curve non-decreasing.

guard_tier_cliffs = false returns to raw step tiers. The behaviour is a commercial choice, so it is configuration rather than a constant.

6. The loyalty ladder

Tier From lifetime spend Discount
standard 0 ₽ 0 %
silver 100 000 ₽ 4 %
gold 300 000 ₽ 8 %

A tier may also carry a margin_percent_override. The engine takes a CustomerTier and knows nothing about ladders, which is what keeps a negotiated tier — a contract with one customer — expressible in exactly the same terms as an earned one.

7. Where prices come from at runtime

console settings  →  settings context  →  RateSnapshot  →  price()  →  Breakdown
                                              │
                                              └─ persisted (ADR-0020), referenced by the order

Editing a rate in the console changes what future quotes cost. It does not touch an order already placed: the order holds a RESTRICT foreign key to the snapshot it was quoted under, so the row cannot be deleted out from under it. Every settings save is audited with a «было · стало» record.

Until somebody changes them, every rate is the default compiled into the settings catalogue. Those are sensible numbers but not this farm's — setting the real rates is step one of first boot.

8. Money handling

Decimal and an explicit Money(amount, currency) throughout. Floats are banned in pricing by a lint rule, because mixing decimal and binary floating point through a cost stack loses money by rounding, silently, in the farm's disfavour about half the time.

Rounding is centralised: each line is rounded once, and the total is the sum of the rounded lines, so the breakdown a customer adds up by hand matches the figure charged.

9. Money is walled off from production

VIEW_FINANCIALS is deliberately separate from every production permission. An operator entitled to walk the fleet screen is not thereby entitled to the farm's finances — which is why the dashboard needs view_all_orders while the post-production board needs only view_production, and why payment.settled has its amount stripped on the event relay.

A response carrying seconds must not quietly start carrying rubles or kilowatt-hours.

Related

Clone this wiki locally