"""One pricing module for every way a cart is priced (Critical Corrections 3). The Site's pre-check and the approval used to repeat the same chain and had already drifted: the pre-check ignored the manual-file extra. Everything that turns items into money lives here now. The catalog is read once, the arithmetic is pure, and a new price rule only has to be added in one place. """ from decimal import Decimal from . import catalog, coupons, grade_rules from .core import pricing def price_items(items, names, catalog_now, manual_extra): """Price the items without a database: price() per item with the product's base price and the manual extra, bill_splits, then the band of each mode's total, then apply_bands. Exactly as an order is charged.""" priced = [] for item in items: extra = manual_extra if pricing.is_manual(item, names) else 0 product = catalog_now[item['mode']] priced.append({**pricing.price(item['mode'], str(item['metres']), item['grade'], extra, minimum=Decimal('0') if item.get('split') else Decimal('1'), unit_cents=product['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 price_cart(c, items, names, *, coupon_code=None, customer_document=None, wants_treatment=False, treatment_rule=True): """Price a cart exactly as an order is charged. Returns {'items': [priced item dicts], 'items_total': int, 'coupon': dict|None, 'treatment_cents': int, 'grade_flags': dict}. Raises HTTPException(422) for a product switched off, a required treatment the cart did not ask for, or a refused coupon (the same messages as today). `treatment_rule=False` is for the montador, which assembles the sheet.""" from fastapi import HTTPException catalog_now = catalog.current(c) for item in items: if not catalog_now[item['mode']]['enabled']: raise HTTPException(422, 'Este produto não está à venda agora.') priced = price_items(items, names, catalog_now, pricing.manual_extra_cents()) offer = catalog_now['tratamento'] offered = bool(offer['enabled'] and offer['unit_cents'] > 0) flags = grade_rules.triggered(grade_rules.current_rules(c), priced, offered) if not treatment_rule: flags.pop('tratamento', None) if 'tratamento' in flags and not wants_treatment: raise HTTPException(422, grade_rules.treatment_message(flags['tratamento'])) coupon = None if coupon_code: coupon = coupons.check(c, coupon_code, customer_document, priced) treatment = offer['unit_cents'] if wants_treatment and offer['enabled'] else 0 return {'items': priced, 'items_total': sum(i['total_cents'] for i in priced), 'coupon': coupon, 'treatment_cents': treatment, 'grade_flags': flags}