diff --git a/app/api/operator.py b/app/api/operator.py index 9ba3d57..1cbb160 100644 --- a/app/api/operator.py +++ b/app/api/operator.py @@ -69,7 +69,8 @@ def freight_status(): 'production_days': freight.production_days, 'box_cm': [str(side) for side in freight.box_cm] if freight.box_cm else None, 'cubage_kg_m3': str(freight.cubage_kg_m3) if freight.cubage_kg_m3 else None, - 'cubed_kg': str(cubed) if cubed is not None else None} + 'cubed_kg': str(cubed) if cubed is not None else None, + 'metres_per_box': str(freight.metres_per_box) if freight.metres_per_box else None} def backup_status(c): diff --git a/app/jadlog.py b/app/jadlog.py index 7779699..b33142c 100644 --- a/app/jadlog.py +++ b/app/jadlog.py @@ -12,8 +12,11 @@ plus a weight per billed metre) and has no default: FREIGHT_ADAPTER=jadlog refuses to start without it, so a guessed weight never prices a customer's freight. The cubed weight is the box's volume times the contract's factor (JADLOG_CAIXA_CM, JADLOG_CUBAGEM_KG_M3); set both or neither, and with neither -only the real weight is sent. +only the real weight is sent. A box holds JADLOG_METROS_POR_CAIXA metres of +film; a longer order ships in as many boxes as it needs, each with its own +packaging weight and volume. """ +import math import os import re from decimal import ROUND_HALF_UP, Decimal @@ -117,20 +120,31 @@ class JadlogFreight: self.cubage_kg_m3 = Decimal(factor.replace(',', '.')) if factor else None if self.cubage_kg_m3 is not None and self.cubage_kg_m3 <= 0: raise RuntimeError('JADLOG_CUBAGEM_KG_M3 must be positive') + per_box = (os.environ.get('JADLOG_METROS_POR_CAIXA') or '').strip() + self.metres_per_box = Decimal(per_box.replace(',', '.')) if per_box else None + if self.metres_per_box is not None and self.metres_per_box <= 0: + raise RuntimeError('JADLOG_METROS_POR_CAIXA must be positive') + + def boxes(self, metres=None): + """How many boxes the order fills; one when the capacity is not set.""" + if metres is None or self.metres_per_box is None: + return 1 + return max(1, math.ceil(Decimal(str(metres)) / self.metres_per_box)) def real_kg(self, metres): - return (self.base_kg + self.per_metre_kg * Decimal(str(metres))).quantize(GRAMS) + return (self.boxes(metres) * self.base_kg + self.per_metre_kg * Decimal(str(metres))).quantize(GRAMS) - def cubed_kg(self): - """The box's volume in m³ times the contract's factor; None without a box.""" + def cubed_kg(self, metres=None): + """The boxes' volume in m³ times the contract's factor (one box without a + length); None without a box.""" if not self.box_cm: return None a, b, c = self.box_cm - return (a * b * c / Decimal(1_000_000) * self.cubage_kg_m3).quantize(GRAMS) + return (self.boxes(metres) * a * b * c / Decimal(1_000_000) * self.cubage_kg_m3).quantize(GRAMS) def weight_kg(self, metres): """What Jadlog charges by: the larger of the real and the cubed weight.""" - real, cubed = self.real_kg(metres), self.cubed_kg() + real, cubed = self.real_kg(metres), self.cubed_kg(metres) return max(real, cubed) if cubed is not None else real def quote(self, service, postal_code, metres=None, declared_cents=0): @@ -148,5 +162,6 @@ class JadlogFreight: days = result['days'] return {'provider': self.name, 'service': SERVICE, 'postal_code': postal_code, 'total_cents': result['total_cents'], 'weight_kg': str(weight), 'real_kg': str(real), + 'boxes': self.boxes(metres), 'days': None if days is None else int(days) + self.production_days, 'description': 'Jadlog .PACKAGE'} diff --git a/compose.local.yaml b/compose.local.yaml index e2a5e39..4d25d3f 100644 --- a/compose.local.yaml +++ b/compose.local.yaml @@ -45,6 +45,7 @@ x-app: &app JADLOG_PESO_POR_METRO_KG: ${JADLOG_PESO_POR_METRO_KG:-} JADLOG_CAIXA_CM: ${JADLOG_CAIXA_CM:-} JADLOG_CUBAGEM_KG_M3: ${JADLOG_CUBAGEM_KG_M3:-} + JADLOG_METROS_POR_CAIXA: ${JADLOG_METROS_POR_CAIXA:-} FREIGHT_PRODUCTION_DAYS: ${FREIGHT_PRODUCTION_DAYS:-0} # Carts the Site priced are approved at checkout; larger ones wait for review. QUOTE_AUTO_APPROVE: ${QUOTE_AUTO_APPROVE:-true} diff --git a/docker-compose.yml b/docker-compose.yml index 4cb8870..53ce1e0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -51,6 +51,8 @@ x-app-environment: &app-environment # charges the larger of the real and the cubed weight. Both or neither. JADLOG_CAIXA_CM: ${JADLOG_CAIXA_CM:-} JADLOG_CUBAGEM_KG_M3: ${JADLOG_CUBAGEM_KG_M3:-} + # Metres of film one box holds; a longer order ships in more boxes. + JADLOG_METROS_POR_CAIXA: ${JADLOG_METROS_POR_CAIXA:-} # Working days of production added to Jadlog's delivery time. FREIGHT_PRODUCTION_DAYS: ${FREIGHT_PRODUCTION_DAYS:-0} # A cart the Site priced is approved at checkout and can be paid at once; diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 8dd2830..b8706d3 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -281,6 +281,11 @@ From the report already sent. These are dated promises, not backlog. `JADLOG_CUBAGEM_KG_M3` make the quote send the larger of the real and the cubed weight. The contract's factor is still unknown; until it is set the Kanban's Frete card says the quote uses the real weight only. + **Corrected (2026-10-02):** the box is 63 × 15.5 × 15.5 cm (0.30 kg) and holds + about 60 m; UV ships in the same box at the same weight. + `JADLOG_METROS_POR_CAIXA` quotes a longer order as several boxes. Jadlog's + public .PACKAGE factor is 300 kg/m³ between states (167 within SP): 300 is + set, so in-state orders are quoted a little high, never low. - `[~]` 1.3 — Idempotent Tiny/Olist order creation with order-number traceability. Confirm endpoints, tag behaviour and rate limits first. **Groundwork (2026-09-24), API v3 by decision:** OAuth2 against Tiny's diff --git a/tests/test_jadlog.py b/tests/test_jadlog.py index 0c28fa5..066ef45 100644 --- a/tests/test_jadlog.py +++ b/tests/test_jadlog.py @@ -124,10 +124,27 @@ class JadlogFreightTest(unittest.TestCase): freight.quote('jadlog', '01310100', '60', 1490) self.assertEqual(seen[1], 4.8) + def test_a_long_order_ships_in_more_boxes(self): + seen = [] + + def handler(request): + seen.append(json.loads(request.content)['frete'][0]['peso']) + return httpx.Response(200, json={'frete': [{'vltotal': 30, 'prazo': 3}]}) + + freight = self.freight(handler, JADLOG_PESO_BASE_KG='0.3', JADLOG_PESO_POR_METRO_KG='0.075', + JADLOG_CAIXA_CM='63x15,5x15,5', JADLOG_CUBAGEM_KG_M3='300', + JADLOG_METROS_POR_CAIXA='60') + # 63 x 15.5 x 15.5 cm is 0.01513575 m³: 4.541 kg cubed for one box. + self.assertEqual((freight.boxes('60'), freight.boxes('60.01'), freight.boxes('121')), (1, 2, 3)) + quote = freight.quote('jadlog', '01310100', '61', 1490) + # Two boxes: 0.6 kg of packaging plus 4.575 kg of film, against 9.081 kg cubed. + self.assertEqual((quote['boxes'], quote['real_kg'], seen[0]), (2, '5.175', 9.081)) + def test_a_box_needs_its_factor(self): for env in ({'JADLOG_CAIXA_CM': '56x14x14'}, {'JADLOG_CUBAGEM_KG_M3': '300'}, {'JADLOG_CAIXA_CM': '56x14', 'JADLOG_CUBAGEM_KG_M3': '300'}, - {'JADLOG_CAIXA_CM': '56x14x14', 'JADLOG_CUBAGEM_KG_M3': '0'}): + {'JADLOG_CAIXA_CM': '56x14x14', 'JADLOG_CUBAGEM_KG_M3': '0'}, + {'JADLOG_METROS_POR_CAIXA': '0'}): with self.subTest(env=env), self.assertRaises(RuntimeError): self.freight(lambda request: httpx.Response(200), **env) # Neither: the real weight only, as before the box was known. diff --git a/web/kanban.js b/web/kanban.js index 3354e1b..61d4da7 100644 --- a/web/kanban.js +++ b/web/kanban.js @@ -774,7 +774,8 @@ function renderIntegrations(){ // Jadlog charges the larger of the real and the cubed weight: without the // box and the contract's factor, a light big box is quoted below its price. const caixa=fr.box_cm?' Caixa '+fr.box_cm.map(n=>String(n).replace('.',',')).join(' × ')+' cm a '+ - String(fr.cubage_kg_m3).replace('.',',')+' kg/m³ = '+kg(fr.cubed_kg)+' cubados; vale o maior dos dois pesos.' + String(fr.cubage_kg_m3).replace('.',',')+' kg/m³ = '+kg(fr.cubed_kg)+' cubados'+ + (fr.metres_per_box?', até '+String(fr.metres_per_box).replace('.',',')+' m por caixa':'')+'; vale o maior dos dois pesos.' :' Cubagem não configurada: a cotação usa só o peso real, e a Jadlog pode cobrar pelo tamanho da caixa.'; cards.push(fr.provider==='jadlog' ? integration('Frete',fr.box_cm?'Ativo':'Verificar',fr.box_cm?'ok':'warn','Entrega pela Jadlog e retirada em Franca. Pacote: '+kg(fr.base_kg)+