All checks were successful
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Successful in 8m24s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Successful in 1m14s
Marcus: "o pedido mínimo de um metro deve ser somente para o primeiro metro, após isso deve se somar a metragem de cada arquivo", "Soma tudo e arredonda uma vez só", and for different products "Pode ser sim": each product apart at its price, the minimum for the whole order. Order #246 (three ready sheets of 3,74, 0,24 and 0,22 m) was 5,80 m with 1 m per item; it is now 4,20 m. pricing.bill_order replaces the per-item minimum and bill_splits: per product the exact lengths (a ready sheet without its gaps) are rounded once with the 8 mm grace, the minimum's difference goes to the first item's product, and each product's total is shared back to its items in 10 cm steps (each its length rounded down, the missing steps to the largest fractions, the minimum to the product's first item), for Tiny's 10 cm SKUs and the cards. A share of 0 m adds no Tiny line. The print file's guard allows an item one 10 cm step under its length. The Site cart shares the same way (cobrarPedido), split sheets round once, the buy box says the items of a product are summed and rounded once, and the Montador's estimate rounds its sum once. Contract: output/revisao/contrato-minimo-pedido.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
167 lines
7.9 KiB
Python
167 lines
7.9 KiB
Python
"""Dropstar's single price per metre for each product; integer BRL cents at the boundary.
|
|
|
|
One price per product (Dropstar, 2026-10-23). The grade is still recorded with
|
|
each item, for the customer and the print room, but it no longer changes the
|
|
price. This module does not inspect artwork or perform pre-flight.
|
|
"""
|
|
import os
|
|
import re
|
|
from decimal import Decimal, ROUND_CEILING
|
|
|
|
# Billed in steps of 10 cm, with 8 mm of grace for the customer (Marcus,
|
|
# 2026-11-27): a length that passes a step by up to 8 mm is billed at that
|
|
# step, by more at the next one. The Site rounds the same (web/site-config.js).
|
|
STEP_GRACE_M = Decimal('0.008')
|
|
# The film between a ready sheet's copies (app/core/models.py SHEET_GAP_CM).
|
|
SHEET_GAP_CM = Decimal('0.8')
|
|
|
|
|
|
def unbilled_gaps_cm(production):
|
|
"""The film between a ready sheet's copies and sheets (8 mm each, Marcus
|
|
26/11): laid out, but not billed since Marcus's 2026-12-03 answer ("por
|
|
hora pode tirar a cobrança"). The layout's height less its pieces; zero
|
|
for loose art, whose spacing is part of the packing."""
|
|
production = production or {}
|
|
sources = production.get('sources') or []
|
|
if not sources or any(source.get('kind') != 'sheet' for source in sources):
|
|
return Decimal('0')
|
|
placements = production.get('placements') or []
|
|
if len(placements) < 2:
|
|
return Decimal('0')
|
|
pieces = sum(Decimal(str(p['length_cm'])) for p in placements)
|
|
# Never more than one gap of SHEET_GAP_CM between each two pieces.
|
|
most = SHEET_GAP_CM * (len(placements) - 1)
|
|
return min(most, max(Decimal('0'), Decimal(str(production['height_cm'])) - pieces))
|
|
|
|
|
|
def billable_metres(item):
|
|
"""The metres an item is billed on: its length less the unbilled gaps."""
|
|
return Decimal(str(item['metres'])) - unbilled_gaps_cm(item.get('production')) / 100
|
|
|
|
|
|
def billed_length(length):
|
|
"""The metres billed for `length` metres, before any minimum; never 0 for
|
|
a sheet that has a length."""
|
|
steps = ((Decimal(str(length)) - STEP_GRACE_M) * 10).to_integral_value(rounding=ROUND_CEILING)
|
|
return max(Decimal('0.1'), steps / 10)
|
|
|
|
|
|
PRICES = {'file': 1990, 'avulsa': 2190, 'uvfile': 7990, 'uv': 8990, 'premium': 3990}
|
|
|
|
# A finished sheet the Site cannot open (CDR, AI, PSD, TIFF) is opened by a
|
|
# person before printing. It pays this per metre on top of the product's price
|
|
# when the business sets it, to make the formats the robot reads the cheaper
|
|
# path. Zero adds nothing.
|
|
MANUAL_FORMATS = re.compile(r'\.(cdr|ai|psd|psb|tiff?)$', re.I)
|
|
|
|
|
|
def manual_extra_cents() -> int:
|
|
value = int(os.environ.get('MANUAL_FILE_EXTRA_CENTS', '0') or 0)
|
|
if not 0 <= value <= 100000:
|
|
raise ValueError('MANUAL_FILE_EXTRA_CENTS must be between 0 and 100000')
|
|
return value
|
|
|
|
|
|
def treatment_cents() -> int:
|
|
"""Ajuste Fino, a fixed amount per art (front 1a).
|
|
|
|
Zero keeps the option hidden: the price is the client's decision, and the
|
|
server and the Site read the same `TREATMENT_CENTS`.
|
|
"""
|
|
value = int(os.environ.get('TREATMENT_CENTS', '0') or 0)
|
|
if not 0 <= value <= 1000000:
|
|
raise ValueError('TREATMENT_CENTS must be between 0 and 1000000')
|
|
return value
|
|
|
|
|
|
def is_manual(item, names) -> bool:
|
|
"""Whether a finished-sheet item has a file someone must open by hand: one
|
|
the customer measured, or one in a format the Site cannot read."""
|
|
return any(source['kind'] == 'sheet' and (source['measurement'] == 'customer' or
|
|
MANUAL_FORMATS.search(names.get(str(source['upload_id']), '')))
|
|
for source in item['production']['sources'])
|
|
|
|
|
|
def price(mode: str, metres: str, grade: int, extra_cents: int = 0, minimum: Decimal = Decimal('1'),
|
|
unit_cents: int | None = None) -> dict:
|
|
"""The item's charge. The grade is validated and kept with the item; the
|
|
price does not depend on it. A split sheet has no minimum of its own: its
|
|
group has one (bill_splits). `unit_cents` is the current price from the
|
|
database (app/catalog.py); without it, the table above."""
|
|
length = Decimal(str(metres))
|
|
if mode not in PRICES or not length.is_finite() or not 0 < length <= 12000:
|
|
raise ValueError('Invalid product or length')
|
|
if isinstance(grade, bool) or not isinstance(grade, int) or not 0 <= grade <= 100:
|
|
raise ValueError('Grade must be an integer between 0 and 100')
|
|
billed = max(minimum, billed_length(length))
|
|
if isinstance(extra_cents, bool) or not isinstance(extra_cents, int) or extra_cents < 0:
|
|
raise ValueError('Invalid extra charge')
|
|
unit = (PRICES[mode] if unit_cents is None else unit_cents) + extra_cents
|
|
priced = {'mode': mode, 'metres': str(length), 'grade': grade,
|
|
'billed_metres': str(billed), 'unit_cents': unit,
|
|
'total_cents': int(billed * unit)}
|
|
if extra_cents:
|
|
priced['manual_extra_cents'] = extra_cents
|
|
return priced
|
|
|
|
|
|
def bill_order(priced: list, lengths: list) -> list:
|
|
"""The order's metres (Marcus, 2026-12-03): each product's exact lengths
|
|
summed and rounded once (billed_length), the 1 m minimum once for the
|
|
order, on the first item's product. Each product's total is then shared
|
|
back to its items in 10 cm steps, for Tiny's SKUs and the cards: every
|
|
item its length rounded down, the missing steps to the largest fractions
|
|
(ties to the first item), the minimum's difference to the product's first
|
|
item (output/revisao/contrato-minimo-pedido.md)."""
|
|
if not priced:
|
|
return priced
|
|
steps = [Decimal(str(length)) * 10 for length in lengths]
|
|
modes = list(dict.fromkeys(item['mode'] for item in priced))
|
|
totals = {mode: billed_length(sum(Decimal(str(lengths[k])) for k, item in enumerate(priced)
|
|
if item['mode'] == mode)) for mode in modes}
|
|
short = Decimal('1') - sum(totals.values())
|
|
for mode in modes:
|
|
indexes = [k for k, item in enumerate(priced) if item['mode'] == mode]
|
|
shares = {k: int(steps[k].to_integral_value(rounding='ROUND_FLOOR')) for k in indexes}
|
|
missing = int(totals[mode] * 10) - sum(shares.values())
|
|
by_fraction = sorted(indexes, key=lambda k: (-(steps[k] - shares[k]), k))
|
|
for k in by_fraction[:missing]:
|
|
shares[k] += 1
|
|
if mode == priced[0]['mode'] and short > 0:
|
|
shares[indexes[0]] += int(short * 10)
|
|
for k in indexes:
|
|
billed = Decimal(shares[k]) / 10
|
|
priced[k].update(billed_metres=str(billed), total_cents=int(billed * priced[k]['unit_cents']))
|
|
return priced
|
|
|
|
|
|
def bill_splits(priced: list) -> list:
|
|
"""The order minimum of 1 m once per split group, on its first sheet: the
|
|
customer did not choose the split (item 17), so it never costs a metre more."""
|
|
for group in dict.fromkeys(i['split']['group'] for i in priced if i.get('split')):
|
|
parts = [i for i in priced if (i.get('split') or {}).get('group') == group]
|
|
short = Decimal('1') - sum(Decimal(i['billed_metres']) for i in parts)
|
|
if short > 0:
|
|
first = min(parts, key=lambda i: i['split']['part'])
|
|
billed = Decimal(first['billed_metres']) + short
|
|
first.update(billed_metres=str(billed), total_cents=int(billed * first['unit_cents']))
|
|
return priced
|
|
|
|
|
|
def apply_bands(priced: list, unit_by_mode: dict) -> list:
|
|
"""Rewrite the items of each mode to the band price for the order's total
|
|
metres of that product (front 8.2). The manual-file extra stays on top of
|
|
each item; a mode absent from `unit_by_mode` is untouched."""
|
|
for item in priced:
|
|
if item['mode'] in unit_by_mode:
|
|
unit = unit_by_mode[item['mode']] + item.get('manual_extra_cents', 0)
|
|
item['unit_cents'] = unit
|
|
item['total_cents'] = int(Decimal(str(item['billed_metres'])) * unit)
|
|
return priced
|
|
|
|
|
|
def sheet_label(item: dict) -> str:
|
|
"""' · folha 1 de 2' on a split sheet (item 17), for the customer's lists."""
|
|
split = item.get('split')
|
|
return f" · folha {split['part']} de {split['of']}" if split else ''
|