acupedia-api/app/features/payments/pricing.py

85 lines
3.6 KiB
Python

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")
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) -> 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:
coupon_text = f"Code: {coupon.code} - {coupon.description or ''}".rstrip(" -")
if coupon.discount_type == "flat":
coupon_discount = min(coupon.value, after_bundle)
elif coupon.discount_type == "percentage":
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)
fee_rate = Decimal("0.02") if curr == "INR" else Decimal("0.03")
fee = q(net * fee_rate)
gst = q(fee * Decimal("0.18")) if curr == "INR" else Decimal("0")
grand = q(net + fee + gst)
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)),
"total_discounts": float(q(bundle + coupon_discount)),
"convenience_fee_rate": float(fee_rate * 100),
"convenience_fee": float(fee),
"taxable_amount": float(fee),
"gst_amount": float(gst),
"grand_total": float(grand),
"cart_items": [{"code": i.code, "name": i.name, "category": i.category, "price": float(q(i.price))} for i in items],
}