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>
294 lines
16 KiB
Python
294 lines
16 KiB
Python
"""One pricing module: the same arithmetic for every cart (plan 003).
|
|
|
|
Run from repository root: python3 -m unittest tests.test_cart_pricing -v.
|
|
old_way() reproduces today's approval arithmetic with the functions that were
|
|
already there; the matrix pins price, split minimums, bands and the manual
|
|
extra. app/cart_pricing.price_items must return exactly the same list.
|
|
"""
|
|
import unittest
|
|
from decimal import Decimal
|
|
|
|
from uuid import uuid4
|
|
|
|
from app import catalog
|
|
from app.cart_pricing import (designer_hours, needs_designer, price_items,
|
|
redesign_price, treatment_price)
|
|
from app.core import pricing
|
|
|
|
try:
|
|
from pydantic import ValidationError
|
|
from app.core.models import QuoteRequest
|
|
except ImportError:
|
|
# The validate job runs this file on the host, without the runtime image's
|
|
# dependencies; the runtime suites exercise the model there.
|
|
QuoteRequest = ValidationError = None
|
|
|
|
|
|
BASE = {'file': 1990, 'avulsa': 2190, 'uvfile': 7990, 'uv': 8990}
|
|
UV_BANDS = [{'from_metres': '5', 'unit_cents': 8000},
|
|
{'from_metres': '20', 'unit_cents': 7000}]
|
|
|
|
|
|
def catalog_now(bands=None, prices=None):
|
|
prices = prices or BASE
|
|
bands = bands or {}
|
|
now = {mode: {'enabled': True, 'unit_cents': prices[mode],
|
|
'bands': bands.get(mode, [])} for mode in BASE}
|
|
now['tratamento'] = {'enabled': True, 'unit_cents': 1000, 'bands': []}
|
|
now['redesenho'] = {'enabled': True, 'unit_cents': 2990, 'bands': []}
|
|
return now
|
|
|
|
|
|
def item(mode, metres, grade=90, uid='u1', measurement='file', split=None):
|
|
kind = 'sheet' if mode in ('file', 'uvfile') else 'artwork'
|
|
data = {'mode': mode, 'metres': str(metres), 'grade': grade, 'uploads': [uid],
|
|
'production': {
|
|
'version': 3,
|
|
'film_width_cm': 27 if mode in ('uvfile', 'uv') else 57,
|
|
'height_cm': float(Decimal(str(metres)) * 100),
|
|
'sources': [{'upload_id': uid, 'kind': kind, 'width_cm': 10,
|
|
'length_cm': 10, 'copies': 1, 'rotation_degrees': 0,
|
|
'mirrored': False, 'measurement': measurement}],
|
|
'placements': []},
|
|
'quality_status': 'ok', 'quality_acknowledged': False}
|
|
if split:
|
|
data['split'] = split
|
|
return data
|
|
|
|
|
|
def old_way(items, names, catalog_now, manual_extra):
|
|
"""Today's approve arithmetic (app/quote_review.py before plan 003)."""
|
|
priced = []
|
|
for item in items:
|
|
extra = manual_extra if pricing.is_manual(item, names) else 0
|
|
priced.append({**pricing.price(item['mode'], str(item['metres']), item['grade'], extra,
|
|
minimum=Decimal('0') if item.get('split') else Decimal('1'),
|
|
unit_cents=catalog_now[item['mode']]['unit_cents']),
|
|
**({'split': item['split']} if item.get('split') else {})})
|
|
pricing.bill_splits(priced)
|
|
totals = {}
|
|
for item in priced:
|
|
totals[item['mode']] = totals.get(item['mode'], Decimal('0')) + Decimal(item['billed_metres'])
|
|
banded = {}
|
|
for mode, total in totals.items():
|
|
product = catalog_now[mode]
|
|
price_for_total = catalog.band_price(product['unit_cents'], product['bands'], total)
|
|
if price_for_total != product['unit_cents']:
|
|
banded[mode] = price_for_total
|
|
pricing.apply_bands(priced, banded)
|
|
return priced
|
|
|
|
|
|
def carts():
|
|
plain = {'u1': 'ARTE.PNG'}
|
|
manual = {'u1': 'ARTE.CDR'}
|
|
split = {'group': 'g', 'part': 1, 'of': 2}
|
|
split2 = {'group': 'g', 'part': 2, 'of': 2}
|
|
return {
|
|
'file-one': {'items': [item('file', '1')], 'names': plain,
|
|
'catalog': catalog_now(), 'manual': 0},
|
|
'avulsa-one': {'items': [item('avulsa', '2.75')], 'names': plain,
|
|
'catalog': catalog_now(), 'manual': 0},
|
|
'uvfile-one': {'items': [item('uvfile', '1.01')], 'names': plain,
|
|
'catalog': catalog_now(), 'manual': 0},
|
|
'uv-one': {'items': [item('uv', '1')], 'names': plain,
|
|
'catalog': catalog_now(), 'manual': 0},
|
|
'same-mode-band-together': {
|
|
'items': [item('uv', '2.5', uid='u1'), item('uv', '2.5', uid='u2')],
|
|
'names': {'u1': 'A.PNG', 'u2': 'B.PNG'},
|
|
'catalog': catalog_now(bands={'uv': UV_BANDS}), 'manual': 0},
|
|
'same-mode-below-band': {
|
|
'items': [item('uv', '2.4', uid='u1'), item('uv', '2.5', uid='u2')],
|
|
'names': {'u1': 'A.PNG', 'u2': 'B.PNG'},
|
|
'catalog': catalog_now(bands={'uv': UV_BANDS}), 'manual': 0},
|
|
'split-under-one-metre': {
|
|
'items': [item('avulsa', '0.3', uid='u1', split=split),
|
|
item('avulsa', '0.2', uid='u2', split=split2)],
|
|
'names': {'u1': 'A.PNG', 'u2': 'B.PNG'},
|
|
'catalog': catalog_now(), 'manual': 0},
|
|
'split-and-band': {
|
|
'items': [item('avulsa', '0.3', uid='u1', split=split),
|
|
item('avulsa', '0.2', uid='u2', split=split2)],
|
|
'names': {'u1': 'A.PNG', 'u2': 'B.PNG'},
|
|
'catalog': catalog_now(bands={'avulsa': [{'from_metres': '1',
|
|
'unit_cents': 1990}]}),
|
|
'manual': 0},
|
|
'manual-cdr-extra': {'items': [item('file', '1', split=None)],
|
|
'names': manual, 'catalog': catalog_now(), 'manual': 200},
|
|
'manual-customer-measure': {
|
|
'items': [item('file', '1.01', measurement='customer')],
|
|
'names': plain, 'catalog': catalog_now(), 'manual': 200},
|
|
'mixed-modes': {
|
|
'items': [item('file', '1', uid='u1'), item('avulsa', '2', uid='u2'),
|
|
item('uvfile', '0.5', uid='u3'), item('uv', '3', uid='u4')],
|
|
'names': {'u1': 'A.PNG', 'u2': 'B.PNG', 'u3': 'C.PNG', 'u4': 'D.PNG'},
|
|
'catalog': catalog_now(), 'manual': 0},
|
|
'no-bands': {'items': [item('avulsa', '1.2'), item('avulsa', '1.3')],
|
|
'names': plain, 'catalog': catalog_now(bands={}), 'manual': 0},
|
|
'band-exact-boundary': {
|
|
'items': [item('uv', '5')], 'names': plain,
|
|
'catalog': catalog_now(bands={'uv': UV_BANDS}), 'manual': 0},
|
|
'band-floating-metres': {
|
|
'items': [item('uv', '2.8000000000000003')], 'names': plain,
|
|
'catalog': catalog_now(bands={'uv': [{'from_metres': '2.8', 'unit_cents': 8500}]}),
|
|
'manual': 0},
|
|
'manual-plus-band': {
|
|
'items': [item('file', '2.5')], 'names': manual,
|
|
'catalog': catalog_now(bands={'file': [{'from_metres': '2', 'unit_cents': 1890}]}),
|
|
'manual': 200},
|
|
'mixed-modes-with-bands': {
|
|
'items': [item('file', '2', uid='u1'), item('file', '3', uid='u2'),
|
|
item('uv', '2', uid='u3')],
|
|
'names': {'u1': 'A.PNG', 'u2': 'B.PNG', 'u3': 'C.PNG'},
|
|
'catalog': catalog_now(bands={'file': [{'from_metres': '5', 'unit_cents': 1790}],
|
|
'uv': UV_BANDS}),
|
|
'manual': 0},
|
|
}
|
|
|
|
|
|
class CartPricingCharacterizationTest(unittest.TestCase):
|
|
def test_price_items_matches_todays_approval(self):
|
|
# Since 2026-12-03 the 1 m minimum is once per order, not per item:
|
|
# only a cart with an item under 1 m beside others bills differently.
|
|
for name, cart in carts().items():
|
|
if name == 'mixed-modes':
|
|
continue
|
|
with self.subTest(cart=name):
|
|
self.assertEqual(
|
|
price_items(cart['items'], cart['names'], cart['catalog'], cart['manual']),
|
|
old_way(cart['items'], cart['names'], cart['catalog'], cart['manual']))
|
|
|
|
def test_the_matrix_is_anchored_to_the_table_prices(self):
|
|
def priced(name):
|
|
cart = carts()[name]
|
|
return price_items(cart['items'], cart['names'], cart['catalog'], cart['manual'])
|
|
self.assertEqual(priced('file-one')[0]['total_cents'], 1990)
|
|
self.assertEqual(priced('uv-one')[0]['total_cents'], 8990)
|
|
self.assertEqual([p['unit_cents'] for p in priced('same-mode-band-together')],
|
|
[8000, 8000])
|
|
split = priced('split-under-one-metre')
|
|
self.assertEqual([(p['billed_metres'], p['total_cents']) for p in split],
|
|
[('0.8', 1752), ('0.2', 438)])
|
|
# A float's noise past 2,80 m is inside the 8 mm grace (Marcus,
|
|
# 2026-11-27): billed 2,80 m at the band from 2,8 m, no longer 2,90 m.
|
|
floating = priced('band-floating-metres')[0]
|
|
self.assertEqual((floating['billed_metres'], floating['total_cents']), ('2.8', 23800))
|
|
|
|
def test_one_rounding_per_product_and_one_minimum_per_order(self):
|
|
# Marcus, 2026-12-03: "Soma tudo e arredonda uma vez só"; the minimum
|
|
# once for the order; products apart, each at its price ("Pode ser sim").
|
|
lengths = ['3.7364', '0.2447', '0.2164'] # order #246, three ready sheets
|
|
priced = price_items([item('file', m, uid=f'u{i}') for i, m in enumerate(lengths)],
|
|
{}, catalog_now(), 0)
|
|
self.assertEqual([p['billed_metres'] for p in priced], ['3.7', '0.3', '0.2'])
|
|
self.assertEqual(sum(p['total_cents'] for p in priced), 8358) # 4,20 m, not 5,80 m
|
|
# An item under 1 m beside others is not raised to 1 m any more.
|
|
mixed = carts()['mixed-modes']
|
|
priced = price_items(mixed['items'], mixed['names'], mixed['catalog'], 0)
|
|
self.assertEqual([p['billed_metres'] for p in priced], ['1', '2', '0.5', '3'])
|
|
# Alone under 1 m: the order minimum, on the first item.
|
|
self.assertEqual(price_items([item('file', '0.3')], {}, catalog_now(), 0)[0]['billed_metres'], '1')
|
|
# Two products under 1 m together: the difference on the first one's product.
|
|
priced = price_items([item('file', '0.25', uid='u1'), item('avulsa', '0.31', uid='u2')],
|
|
{'u1': 'A.PNG', 'u2': 'B.PNG'}, catalog_now(), 0)
|
|
self.assertEqual([p['billed_metres'] for p in priced], ['0.6', '0.4'])
|
|
self.assertEqual(sum(p['total_cents'] for p in priced), 6 * 199 + 4 * 219)
|
|
|
|
def test_a_ready_sheet_is_billed_without_its_gaps(self):
|
|
# Marcus, 2026-12-03 ("por hora pode tirar a cobrança"): the Lais case,
|
|
# one 100 cm sheet and another with 4 copies, 4 gaps of 8 mm = 503,2 cm.
|
|
sheet = item('file', '5.032')
|
|
sheet['production']['placements'] = [
|
|
{'source_index': 0, 'copy_index': i, 'x_cm': 0, 'y_cm': i * 100.8, 'width_cm': 57,
|
|
'length_cm': 100, 'rotation_degrees': 0, 'mirrored': False} for i in range(5)]
|
|
priced = price_items([sheet], {}, catalog_now(), 0)[0]
|
|
self.assertEqual(priced['billed_metres'], '5')
|
|
self.assertEqual(priced['total_cents'], 9950)
|
|
self.assertEqual(priced['metres'], '5.032') # the film stays as laid out
|
|
self.assertEqual(pricing.unbilled_gaps_cm(sheet['production']), Decimal('3.2'))
|
|
# Loose art keeps its packing; one piece has no gap.
|
|
loose = item('avulsa', '1.5')
|
|
loose['production']['placements'] = sheet['production']['placements']
|
|
self.assertEqual(pricing.unbilled_gaps_cm(loose['production']), 0)
|
|
self.assertEqual(pricing.unbilled_gaps_cm(item('file', '1')['production']), 0)
|
|
|
|
def test_ajuste_fino_is_per_art_with_the_band(self):
|
|
# Front 1a: 1 to 9 arts at R$ 9,90, from 10 at R$ 6,90, all arts charged
|
|
# at the band. `true` keeps meaning every art of the cart.
|
|
offer = {'enabled': True, 'unit_cents': 990,
|
|
'bands': [{'from_metres': '10', 'unit_cents': 690}]}
|
|
for arts, unit in ((1, 990), (9, 990), (10, 690), (12, 690)):
|
|
items = [item('avulsa', '1', uid=f'u{index}') for index in range(arts)]
|
|
self.assertEqual(treatment_price(items, True, offer),
|
|
{'cents': unit * arts, 'hours': 24, 'arts': arts, 'unit_cents': unit})
|
|
items = [item('file', '1', uid='u1'), item('file', '1', uid='u2')]
|
|
self.assertEqual(treatment_price(items, [[1, 0]], offer),
|
|
{'cents': 990, 'hours': 24, 'arts': 1, 'unit_cents': 990})
|
|
self.assertEqual(treatment_price(items, [], offer), {})
|
|
self.assertEqual(treatment_price(items, True, {**offer, 'enabled': False}), {})
|
|
|
|
def test_redesenho_is_per_art_never_a_band(self):
|
|
# Front 1b: R$ 29,90 per art, and the basic batch band never applies.
|
|
offer = {'enabled': True, 'unit_cents': 2990,
|
|
'bands': [{'from_metres': '10', 'unit_cents': 690}]}
|
|
items = [item('file', '1', uid='u1'), item('file', '1', uid='u2')]
|
|
self.assertEqual(redesign_price(items, [[0, 0], [1, 0]], offer),
|
|
{'cents': 5980, 'hours': 48, 'arts': 2, 'unit_cents': 2990})
|
|
self.assertEqual(redesign_price(items, [], offer), {})
|
|
self.assertEqual(redesign_price(items, [[0, 0]], {**offer, 'enabled': False}), {})
|
|
self.assertEqual(redesign_price(items, [[0, 0]], {**offer, 'unit_cents': 0}), {})
|
|
|
|
def test_the_basic_band_counts_only_basic_arts(self):
|
|
offer = {'enabled': True, 'unit_cents': 990,
|
|
'bands': [{'from_metres': '10', 'unit_cents': 690}]}
|
|
items = [item('avulsa', '1', uid=f'u{index}') for index in range(10)]
|
|
basic = [[index, 0] for index in range(9)]
|
|
self.assertEqual(treatment_price(items, basic, offer),
|
|
{'cents': 8910, 'hours': 24, 'arts': 9, 'unit_cents': 990})
|
|
redesign = redesign_price(items, [[9, 0]], offer={**offer, 'unit_cents': 2990})
|
|
self.assertEqual(redesign['arts'], 1)
|
|
|
|
def test_designer_helpers_read_both_services(self):
|
|
self.assertEqual(designer_hours({}), 0)
|
|
self.assertEqual(designer_hours({'treatment': {'cents': 990}}), 24)
|
|
self.assertEqual(designer_hours({'redesign': {'cents': 2990}}), 48)
|
|
self.assertEqual(designer_hours({'treatment': {'cents': 990},
|
|
'redesign': {'cents': 2990}}), 48)
|
|
self.assertFalse(needs_designer({}))
|
|
self.assertTrue(needs_designer({'treatment': {'cents': 990}}))
|
|
self.assertTrue(needs_designer({'redesign': {'cents': 2990}}))
|
|
|
|
@unittest.skipUnless(QuoteRequest, 'pydantic is only in the runtime image')
|
|
def test_a_pair_cannot_be_basic_and_redesenho(self):
|
|
def quote(treatment=None, redesign=None, items=1):
|
|
entries = []
|
|
for _ in range(items):
|
|
uid = str(uuid4())
|
|
data = item('avulsa', '1', uid=uid)
|
|
data['production']['placements'] = [{
|
|
'source_index': 0, 'copy_index': 0, 'x_cm': 0, 'y_cm': 0,
|
|
'width_cm': 10, 'length_cm': 10, 'rotation_degrees': 0, 'mirrored': False}]
|
|
entries.append(data)
|
|
payload = {'request_key': str(uuid4()),
|
|
'customer': {'cnpj': '11222333000181', 'zap': '11999999999',
|
|
'mail': 'arte@example.test'},
|
|
'items': entries, 'freight': {'service': 'pickup'}}
|
|
if treatment is not None:
|
|
payload['treatment'] = treatment
|
|
if redesign is not None:
|
|
payload['redesign'] = redesign
|
|
return payload
|
|
QuoteRequest.model_validate(quote(treatment=[[0, 0]], redesign=[[1, 0]], items=2))
|
|
with self.assertRaises(ValidationError):
|
|
QuoteRequest.model_validate(quote(treatment=[[0, 0]], redesign=[[0, 0]]))
|
|
with self.assertRaises(ValidationError):
|
|
QuoteRequest.model_validate(quote(treatment=True, redesign=[[0, 0]]))
|
|
with self.assertRaises(ValidationError):
|
|
QuoteRequest.model_validate(quote(redesign=[[0, 0], [0, 0]]))
|
|
with self.assertRaises(ValidationError):
|
|
QuoteRequest.model_validate(quote(redesign=[[1, 0]]))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|