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:
@@ -1,11 +1,13 @@
|
||||
"""Run from repository root: python3 -m unittest tests.test_pricing -v."""
|
||||
import json
|
||||
import os
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
import subprocess
|
||||
import unittest
|
||||
from app.core.pricing import is_manual, price, PRICES, treatment_cents
|
||||
from app.catalog import band_price
|
||||
from app.core.pricing import apply_bands, bill_splits, is_manual, price, PRICES, treatment_cents
|
||||
|
||||
class PricingTests(unittest.TestCase):
|
||||
def test_every_length_against_actual_site_javascript(self):
|
||||
@@ -69,6 +71,67 @@ class PricingTests(unittest.TestCase):
|
||||
self.assertEqual(extra['billed_metres'], '1.1')
|
||||
self.assertEqual(extra['total_cents'], 1907)
|
||||
|
||||
def test_band_price_below_at_between_and_above(self):
|
||||
# Front 8.2: the highest band reached wins, and it charges the whole
|
||||
# quantity, not a staircase.
|
||||
bands = [{'from_metres': '5', 'unit_cents': 1990},
|
||||
{'from_metres': '20', 'unit_cents': 1890}]
|
||||
self.assertEqual(band_price(2190, bands, Decimal('4.9')), 2190)
|
||||
self.assertEqual(band_price(2190, bands, Decimal('5')), 1990)
|
||||
self.assertEqual(band_price(2190, bands, Decimal('19.9')), 1990)
|
||||
self.assertEqual(band_price(2190, bands, Decimal('20')), 1890)
|
||||
self.assertEqual(band_price(2190, bands, Decimal('12000')), 1890)
|
||||
# No bands, or a number from the session, is the base price.
|
||||
self.assertEqual(band_price(2190, [], Decimal('5')), 2190)
|
||||
self.assertEqual(band_price(2190, [{'from_metres': 5, 'unit_cents': 1990}], Decimal('5')), 1990)
|
||||
|
||||
def test_apply_bands_prices_the_whole_product_together(self):
|
||||
# Two items of the same mode reach the band together: both pay it.
|
||||
priced = [{'mode': 'file', 'billed_metres': '2.5', 'unit_cents': 2190, 'total_cents': 5475},
|
||||
{'mode': 'file', 'billed_metres': '2.5', 'unit_cents': 2190, 'total_cents': 5475}]
|
||||
apply_bands(priced, {'file': 1990})
|
||||
self.assertEqual([item['unit_cents'] for item in priced], [1990, 1990])
|
||||
self.assertEqual([item['total_cents'] for item in priced], [4975, 4975])
|
||||
# A split group counts its billed metres once, after bill_splits.
|
||||
priced = [{'mode': 'avulsa', 'billed_metres': '0.2', 'unit_cents': 2190, 'total_cents': 438,
|
||||
'split': {'group': 'g', 'part': 1, 'of': 2}},
|
||||
{'mode': 'avulsa', 'billed_metres': '0.3', 'unit_cents': 2190, 'total_cents': 657,
|
||||
'split': {'group': 'g', 'part': 2, 'of': 2}}]
|
||||
bill_splits(priced)
|
||||
self.assertEqual(priced[0]['billed_metres'], '0.7')
|
||||
apply_bands(priced, {'avulsa': 1890})
|
||||
self.assertEqual(priced[0]['total_cents'], 1323)
|
||||
self.assertEqual(priced[1]['total_cents'], 567)
|
||||
# The manual-file extra stays on top of the band price.
|
||||
priced = [{'mode': 'file', 'billed_metres': '2', 'unit_cents': 2190,
|
||||
'manual_extra_cents': 200, 'total_cents': 4380}]
|
||||
apply_bands(priced, {'file': 1890})
|
||||
self.assertEqual(priced[0]['unit_cents'], 2090)
|
||||
self.assertEqual(priced[0]['total_cents'], 4180)
|
||||
# Another mode is untouched.
|
||||
uv = {'mode': 'uv', 'billed_metres': '1', 'unit_cents': 8990, 'total_cents': 8990}
|
||||
priced = [{'mode': 'file', 'billed_metres': '1', 'unit_cents': 2190, 'total_cents': 2190}, uv]
|
||||
apply_bands(priced, {'file': 1990})
|
||||
self.assertEqual((uv['unit_cents'], uv['total_cents']), (8990, 8990))
|
||||
|
||||
def test_band_prices_against_actual_site_javascript(self):
|
||||
# The Site must show the same band price the server charges: run its
|
||||
# own precoNaMetragem for the same table and totals.
|
||||
source = Path('web/site-config.js').read_text()
|
||||
prices = source.split('const PRECO=')[1].split(';', 1)[0]
|
||||
body = source.split('function precoNaMetragem', 1)[1].split('\n}', 1)[0]
|
||||
bands = [{'from_metres': 5, 'unit_cents': 1990},
|
||||
{'from_metres': 20, 'unit_cents': 1890}]
|
||||
js = ('const PRECO=' + prices + ';const FAIXAS=' + json.dumps({'file': bands}) + ';'
|
||||
'function precoNaMetragem' + body + '\n}'
|
||||
';const results=[];for(const m of ["4.9","5","19.9","20","60"])'
|
||||
'results.push([m,Math.round(precoNaMetragem("file",Number(m))*100)]);'
|
||||
'process.stdout.write(JSON.stringify(results));')
|
||||
cases = json.loads(subprocess.check_output(['node', '-e', js], text=True))
|
||||
for metres, cents in cases:
|
||||
with self.subTest(metres=metres):
|
||||
self.assertEqual(cents, band_price(PRICES['file'], bands, Decimal(metres)))
|
||||
|
||||
def test_which_items_are_opened_by_hand(self):
|
||||
def item(kind, measurement, uid='u1'):
|
||||
return {'production': {'sources': [{'kind': kind, 'measurement': measurement, 'upload_id': uid}]}}
|
||||
|
||||
Reference in New Issue
Block a user