example-code-2e/10-dp-1class-func/promotions.py

26 lines
834 B
Python
Raw Normal View History

2021-08-07 05:44:01 +02:00
from decimal import Decimal
from strategy import Order
def fidelity_promo(order: Order) -> Decimal: # <3>
2020-06-11 19:58:15 +02:00
"""5% discount for customers with 1000 or more fidelity points"""
2021-08-07 05:44:01 +02:00
if order.customer.fidelity >= 1000:
return order.total() * Decimal('0.05')
return Decimal(0)
2020-06-11 19:58:15 +02:00
2021-08-07 05:44:01 +02:00
def bulk_item_promo(order: Order) -> Decimal:
2020-06-11 19:58:15 +02:00
"""10% discount for each LineItem with 20 or more units"""
2021-08-07 05:44:01 +02:00
discount = Decimal(0)
2020-06-11 19:58:15 +02:00
for item in order.cart:
if item.quantity >= 20:
2021-08-07 05:44:01 +02:00
discount += item.total() * Decimal('0.1')
2020-06-11 19:58:15 +02:00
return discount
2021-08-07 05:44:01 +02:00
def large_order_promo(order: Order) -> Decimal:
2020-06-11 19:58:15 +02:00
"""7% discount for orders with 10 or more distinct items"""
distinct_items = {item.product for item in order.cart}
if len(distinct_items) >= 10:
2021-08-07 05:44:01 +02:00
return order.total() * Decimal('0.07')
return Decimal(0)