from __future__ import annotations from dataclasses import dataclass from decimal import Decimal, ROUND_HALF_UP from typing import Any def q(value: Decimal) -> Decimal: return value.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP) @dataclass(frozen=True) class PricingItem: code: str name: str category: str price: Decimal @dataclass(frozen=True) class PricingCoupon: code: str category: str discount_type: str value: Decimal description: str | None = None max_discount_limit: Decimal = Decimal("0") min_purchase_amount: Decimal = Decimal("0") class PricingEngine: """Preserves the legacy bundle/loyalty/fee rules while sourcing prices from commerce.access_plans.""" def calculate(self, *, items: list[PricingItem], owned_categories: list[str], coupon: PricingCoupon | None, currency: str, is_first_purchase: bool, coupon_error: str | None = None) -> dict[str, Any]: curr = currency.upper() base = sum((item.price for item in items), Decimal("0")) categories = {item.category for item in items} has_lifetime = any(i.category.lower() == "lifetime" or i.code == "LIFETIME_ALL" for i in items) bundle = Decimal("0") policy = "None" disable_unit = bool(is_first_purchase and coupon and coupon.category == "introductory" and coupon.discount_type == "flat") if has_lifetime: policy = "Lifetime Bulk Package" elif disable_unit: policy = "None (Overridden by New User Event Coupon)" elif is_first_purchase: n = len(categories) rate = Decimal("0.10") if n == 2 else Decimal("0.20") if n == 3 else Decimal("0.30") if n >= 4 else Decimal("0") bundle = base * rate if rate: policy = f"First-time Buy: {n if n < 4 else '4+'} Units ({int(rate * 100)}% Off)" else: n = len(set(owned_categories)) if n and items: rate = min(Decimal("0.05") * n, Decimal("0.25")) bundle = base * rate policy = f"Staggered Buy: {n} owned categories ({int(rate * 100)}% Off)" after_bundle = max(Decimal("0"), base - bundle) coupon_discount = Decimal("0") coupon_text = "None" if coupon: if coupon.min_purchase_amount > 0 and after_bundle < coupon.min_purchase_amount: coupon_error = f"Minimum cart spend of {curr} {coupon.min_purchase_amount} required to use coupon {coupon.code}." coupon_text = "None" coupon_discount = Decimal("0") else: coupon_text = f"Code: {coupon.code} - {coupon.description or ''}".rstrip(" -") if coupon.discount_type in {"flat", "flat_amount"}: coupon_discount = min(coupon.value, after_bundle) elif coupon.discount_type in {"percentage", "percent"}: coupon_discount = after_bundle * (coupon.value / Decimal("100")) if coupon.max_discount_limit > 0: coupon_discount = min(coupon_discount, coupon.max_discount_limit) net = max(Decimal("0"), after_bundle - coupon_discount) grand = q(net) return { "currency": curr, "base_subtotal": float(q(base)), "discount_policy": policy, "bundle_discount_amount": float(q(bundle)), "coupon_applied": coupon_text, "coupon_discount_amount": float(q(coupon_discount)), "coupon_error": coupon_error, "total_discounts": float(q(bundle + coupon_discount)), "convenience_fee_rate": 0.0, "convenience_fee": 0.0, "taxable_amount": 0.0, "gst_amount": 0.0, "grand_total": float(grand), "cart_items": [{"code": i.code, "name": i.name, "category": i.category, "price": float(q(i.price))} for i in items], }