Gestão and the administrators switch each product on and off and set its price from a start date; a price that has started is history and cannot change, a scheduled one can be cancelled. The Site and the server read the current price; with no row the code's prices and TREATMENT_CENTS still apply, so nothing changes until someone sets one. A switched-off product disappears from the Site and new quotes with it are refused. Approved quotes and paid orders keep their price. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
64 lines
2.7 KiB
Python
64 lines
2.7 KiB
Python
"""What the Site sells and at what price, edited in the Kanban's Site tab.
|
|
|
|
Dropstar, 2026-11-05 (Fase 2, front 8): the products and their prices leave the
|
|
code and live in the database, with a start date and a history. `PRICES` in
|
|
app/core/pricing.py stays as the seed and the fallback for the four modes;
|
|
`treatment_cents()` stays as the fallback for `tratamento`. A price row is never
|
|
changed or deleted once its start date has passed (the table is the history),
|
|
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.
|
|
"""
|
|
from fastapi import HTTPException
|
|
|
|
from .core import pricing
|
|
|
|
# The four modes sold by the metre, and the treatment sold per order.
|
|
MODES = ('file', 'avulsa', 'uvfile', 'uv')
|
|
PRODUCTS = (*MODES, 'tratamento')
|
|
|
|
TITLES = {
|
|
'file': 'Têxtil folha pronta',
|
|
'avulsa': 'Têxtil artes avulsas',
|
|
'uvfile': 'UV folha pronta',
|
|
'uv': 'UV artes avulsas',
|
|
'tratamento': 'Tratamento pelo designer',
|
|
}
|
|
UNITS = {'file': 'metro', 'avulsa': 'metro', 'uvfile': 'metro', 'uv': 'metro',
|
|
'tratamento': 'pedido'}
|
|
|
|
|
|
def fallback_cents(product: str) -> int:
|
|
"""The price in the code, used until a row in the database takes over."""
|
|
return pricing.treatment_cents() if product == 'tratamento' else pricing.PRICES[product]
|
|
|
|
|
|
def current(c) -> dict:
|
|
"""{'file': {'enabled': bool, 'unit_cents': int}, ...}: what is sold now."""
|
|
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
|
|
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))}
|
|
for product in PRODUCTS}
|
|
|
|
|
|
def unit_cents(c, product: str) -> int:
|
|
"""The current 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 require_enabled(c, product: str) -> None:
|
|
"""Refuse a product someone switched off in the Kanban's Site tab."""
|
|
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.')
|