"""The package an order ships in, as every carrier is quoted for it. The weight comes from the client (a base per box plus a weight per billed metre of film) and has no default: a carrier refuses to start without it, so a guessed weight never prices a customer's freight. A box holds JADLOG_METROS_POR_CAIXA metres; a longer order ships in as many boxes as it needs, each with its own packaging weight. The box's sides (JADLOG_CAIXA_CM) are what the Correios price by, and with the contract's factor (JADLOG_CUBAGEM_KG_M3) what Jadlog's cubed weight is. The settings keep the Jadlog names they had when Jadlog was the only carrier. """ import math import os import re from decimal import Decimal WEIGHT_SETTINGS = ('JADLOG_PESO_BASE_KG', 'JADLOG_PESO_POR_METRO_KG') GRAMS = Decimal('0.001') def box_cm(text): """The box's three sides in cm from "56x14x14" ("56 × 14 × 14", "56,5x14x14").""" sides = [Decimal(side.strip().replace(',', '.')) for side in re.split(r'[x×*]', text.lower())] if len(sides) != 3 or any(side <= 0 for side in sides): raise RuntimeError('JADLOG_CAIXA_CM takes three sides in cm, such as 56x14x14') return sides class Package: def __init__(self): self.base_kg = Decimal(os.environ['JADLOG_PESO_BASE_KG']) self.per_metre_kg = Decimal(os.environ['JADLOG_PESO_POR_METRO_KG']) # A carrier's time counts from collection; the order is printed first. self.production_days = int(os.environ.get('FREIGHT_PRODUCTION_DAYS') or 0) if self.base_kg < 0 or self.per_metre_kg <= 0 or self.production_days < 0: raise RuntimeError('Package weight and production days must be positive') # A box without its factor (or a factor without a box) would price by a guess. box = (os.environ.get('JADLOG_CAIXA_CM') or '').strip() factor = (os.environ.get('JADLOG_CUBAGEM_KG_M3') or '').strip() if bool(box) != bool(factor): raise RuntimeError('Set JADLOG_CAIXA_CM and JADLOG_CUBAGEM_KG_M3 together, or neither') self.box_cm = box_cm(box) if box else None 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.boxes(metres) * self.base_kg + self.per_metre_kg * Decimal(str(metres))).quantize(GRAMS) def box_weights_kg(self, metres): """Each box's real weight: full boxes first, the rest in the last one.""" metres = Decimal(str(metres)) count = self.boxes(metres) if count == 1: return [self.real_kg(metres)] full = (self.base_kg + self.per_metre_kg * self.metres_per_box).quantize(GRAMS) rest = metres - self.metres_per_box * (count - 1) return [full] * (count - 1) + [(self.base_kg + self.per_metre_kg * rest).quantize(GRAMS)] 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 (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(metres) return max(real, cubed) if cubed is not None else real def status(self): """The package rule, as the Kanban's Integrações shows it.""" cubed = self.cubed_kg() return {'base_kg': str(self.base_kg), 'per_metre_kg': str(self.per_metre_kg), 'production_days': self.production_days, 'box_cm': [str(side) for side in self.box_cm] if self.box_cm else None, 'cubage_kg_m3': str(self.cubage_kg_m3) if self.cubage_kg_m3 else None, 'cubed_kg': str(cubed) if cubed is not None else None, 'metres_per_box': str(self.metres_per_box) if self.metres_per_box else None}