-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorder_processing.py
More file actions
107 lines (82 loc) · 3.06 KB
/
order_processing.py
File metadata and controls
107 lines (82 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
TAX_RATE = 0.21
SAVE10_DISCOUNT_RATE = 0.10
SAVE20_FULL_RATE = 0.20
SAVE20_MIN_RATE = 0.05
SAVE20_THRESHOLD = 200
VIP_FULL_DISCOUNT = 50
VIP_MIN_DISCOUNT = 10
VIP_THRESHOLD = 100
DEFAULT_CURRENCY = "USD"
def parse_request(request: dict):
"""Извлекает данные из запроса."""
user_id = request.get("user_id")
items = request.get("items")
coupon = request.get("coupon")
currency = request.get("currency")
return user_id, items, coupon, currency
def validate_request(user_id, items, currency):
"""Проверяет обязательные поля и структуру данных."""
if user_id is None:
raise ValueError("user_id is required")
if items is None:
raise ValueError("items is required")
if not isinstance(items, list):
raise ValueError("items must be a list")
if len(items) == 0:
raise ValueError("items must not be empty")
for item in items:
if "price" not in item or "qty" not in item:
raise ValueError("item must have price and qty")
if item["price"] <= 0:
raise ValueError("price must be positive")
if item["qty"] <= 0:
raise ValueError("qty must be positive")
if currency is None:
return DEFAULT_CURRENCY
return currency
def calculate_subtotal(items):
"""Считает сумму без скидки."""
subtotal = 0
for item in items:
subtotal += item["price"] * item["qty"]
return subtotal
def calculate_discount(coupon, subtotal):
"""Определяет размер скидки в зависимости от купона."""
if coupon is None or coupon == "":
return 0
if coupon == "SAVE10":
return int(subtotal * SAVE10_DISCOUNT_RATE)
if coupon == "SAVE20":
if subtotal >= SAVE20_THRESHOLD:
return int(subtotal * SAVE20_FULL_RATE)
else:
return int(subtotal * SAVE20_MIN_RATE)
if coupon == "VIP":
discount = VIP_FULL_DISCOUNT
if subtotal < VIP_THRESHOLD:
discount = VIP_MIN_DISCOUNT
return discount
raise ValueError("unknown coupon")
def generate_order_id(user_id, items_count):
"""Генерирует идентификатор заказа."""
return f"{user_id}-{items_count}-X"
def process_checkout(request: dict) -> dict:
"""Основная функция обработки заказа."""
user_id, items, coupon, currency = parse_request(request)
currency = validate_request(user_id, items, currency)
subtotal = calculate_subtotal(items)
discount = calculate_discount(coupon, subtotal)
total_after_discount = max(subtotal - discount, 0)
tax = int(total_after_discount * TAX_RATE)
total = total_after_discount + tax
order_id = generate_order_id(user_id, len(items))
return {
"order_id": order_id,
"user_id": user_id,
"currency": currency,
"subtotal": subtotal,
"discount": discount,
"tax": tax,
"total": total,
"items_count": len(items),
}