"""Exact approved Site DTF price ladders; integer BRL cents at the boundary. Quantity/grade must be supplied by an authenticated commercial reviewer. This module does not inspect artwork or perform pre-flight. """ import os import re from decimal import Decimal, ROUND_CEILING TIERS = { 'file': [(90,1490),(75,1620),(60,1750),(40,1870),(0,1990)], 'avulsa': [(90,2490),(75,2620),(60,2750),(40,2870),(0,2990)], 'uvfile': [(90,6990),(75,7390),(60,7790),(40,8190),(0,8590)], 'uv': [(90,8390),(75,8790),(60,9190),(40,9590),(0,9990)], } # A finished sheet the Site cannot open (CDR, AI, PSD, TIFF) is opened by a # person before printing. It is priced at the top of the ladder, plus this per # metre when the business sets it, to make the formats the robot reads the # cheaper path. Zero (the default) 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 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) -> dict: length = Decimal(str(metres)) if mode not in TIERS 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(Decimal('1'), (length * 10).to_integral_value(rounding=ROUND_CEILING) / 10) if isinstance(extra_cents, bool) or not isinstance(extra_cents, int) or extra_cents < 0: raise ValueError('Invalid extra charge') tier = next(cents for minimum, cents in TIERS[mode] if grade >= minimum) unit = tier + extra_cents priced = {'mode': mode, 'metres': str(length), 'grade': grade, 'billed_metres': str(billed), 'unit_cents': unit, 'total_cents': int(billed * unit), 'discount_cents': int(billed * (TIERS[mode][-1][1] - tier))} if extra_cents: priced['manual_extra_cents'] = extra_cents return priced