defcalculate_line_price(product_id:str,quantity:int)->LinePriceResult:"""Calculate the price for a single order line. Fetches the product from the catalog and applies quantity-based pricing. """product=FetchProductCatalog().call(product_id)unit_price=product.base_price# Volume discount tiersifquantity>=100:discount=0.15elifquantity>=50:discount=0.10elifquantity>=10:discount=0.05else:discount=0.0discounted_price=round(unit_price*(1-discount),2)returnLinePriceResult(product=product.name,unit_price=unit_price,quantity=quantity,volume_discount_pct=discount*100,line_total=round(discounted_price*quantity,2),)
defcalculate_order_total(customer_id:str,items:list,)->OrderTotal:"""Calculate the total price for an order with loyalty discounts. Applies volume discounts per line, then the customer's loyalty discount on top. """customer=FetchCustomerTier().call(customer_id)loyalty_discount=customer.discount_pct/100subtotal=0.0lines=[]foriteminitems:line=calculate_line_price(item["product_id"],item["quantity"])lines.append(line)subtotal+=line.line_totalloyalty_savings=round(subtotal*loyalty_discount,2)total=round(subtotal-loyalty_savings,2)returnOrderTotal(customer_tier=customer.tier,lines=lines,subtotal=subtotal,loyalty_discount_pct=customer.discount_pct,loyalty_savings=loyalty_savings,total=total,)