feat: progressive price bands per product (front 8.2)
A price may carry up to five bands, "from N m, R$ Y": the order's total billed metres of that product chooses one price for its whole quantity, the manual-file extra still on top. Bands start and become history with their price row. The Site charges the same band for the cart, shows "Faltam X m para R$ Y o metro" and lists the bands under the price table; the Kanban's Site tab edits them. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -9,8 +9,13 @@ and an approved quote keeps the price it was approved with.
|
||||
|
||||
`tratamento` is the designer's treatment (front 5), a fixed amount per order:
|
||||
it counts as offered only when it is switched on and priced above zero.
|
||||
|
||||
A price row may carry bands by the order's total metres of that product (front
|
||||
8.2): below the first band the base price, then the highest band reached wins
|
||||
and the whole quantity is charged at that one price. The bands travel with the
|
||||
row, so they start and become history with it.
|
||||
"""
|
||||
from fastapi import HTTPException
|
||||
from decimal import Decimal
|
||||
|
||||
from .core import pricing
|
||||
|
||||
@@ -34,30 +39,58 @@ def fallback_cents(product: str) -> int:
|
||||
return pricing.treatment_cents() if product == 'tratamento' else pricing.PRICES[product]
|
||||
|
||||
|
||||
def band_price(base_cents: int, bands: list, metres: Decimal) -> int:
|
||||
"""The per-metre price for this quantity: the base, or the highest band
|
||||
reached. The whole quantity is charged at one price, not in steps."""
|
||||
metres = Decimal(str(metres))
|
||||
price = base_cents
|
||||
best = None
|
||||
for band in bands or []:
|
||||
start = Decimal(str(band['from_metres']))
|
||||
if metres >= start and (best is None or start >= best):
|
||||
best = start
|
||||
price = band['unit_cents']
|
||||
return price
|
||||
|
||||
|
||||
def current(c) -> dict:
|
||||
"""{'file': {'enabled': bool, 'unit_cents': int}, ...}: what is sold now."""
|
||||
"""{'file': {'enabled': bool, 'unit_cents': int, 'bands': [...]}, ...}."""
|
||||
enabled = {row['id']: row['enabled'] for row in
|
||||
c.execute('SELECT id, enabled FROM dtf_local.products').fetchall()}
|
||||
prices = {row['product']: row['unit_cents'] for row in c.execute('''
|
||||
SELECT DISTINCT ON (product) product, unit_cents
|
||||
prices = {row['product']: row for row in c.execute('''
|
||||
SELECT DISTINCT ON (product) product, unit_cents, bands
|
||||
FROM dtf_local.product_prices
|
||||
WHERE starts_at <= now()
|
||||
ORDER BY product, starts_at DESC, id DESC''').fetchall()}
|
||||
return {product: {'enabled': bool(enabled.get(product, True)),
|
||||
'unit_cents': prices.get(product, fallback_cents(product))}
|
||||
'unit_cents': prices[product]['unit_cents'] if product in prices else fallback_cents(product),
|
||||
'bands': prices[product]['bands'] if product in prices else []}
|
||||
for product in PRODUCTS}
|
||||
|
||||
|
||||
def unit_cents(c, product: str) -> int:
|
||||
"""The current price of one product, or the fallback."""
|
||||
"""The current base price of one product, or the fallback."""
|
||||
row = c.execute('''SELECT unit_cents FROM dtf_local.product_prices
|
||||
WHERE product=%s AND starts_at <= now()
|
||||
ORDER BY starts_at DESC, id DESC LIMIT 1''', (product,)).fetchone()
|
||||
return row['unit_cents'] if row else fallback_cents(product)
|
||||
|
||||
|
||||
def unit_cents_for(c, product: str, metres: Decimal) -> int:
|
||||
"""The current price of one product for this quantity, bands included."""
|
||||
row = c.execute('''SELECT unit_cents, bands FROM dtf_local.product_prices
|
||||
WHERE product=%s AND starts_at <= now()
|
||||
ORDER BY starts_at DESC, id DESC LIMIT 1''', (product,)).fetchone()
|
||||
if not row:
|
||||
return fallback_cents(product)
|
||||
return band_price(row['unit_cents'], row['bands'], metres)
|
||||
|
||||
|
||||
def require_enabled(c, product: str) -> None:
|
||||
"""Refuse a product someone switched off in the Kanban's Site tab."""
|
||||
# Imported here so the pure helpers above load without the web framework
|
||||
# (tests/test_pricing.py runs on the host, where FastAPI is not installed).
|
||||
from fastapi import HTTPException
|
||||
row = c.execute('SELECT enabled FROM dtf_local.products WHERE id=%s', (product,)).fetchone()
|
||||
if row and not row['enabled']:
|
||||
raise HTTPException(422, 'Este produto não está à venda agora.')
|
||||
|
||||
Reference in New Issue
Block a user