-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromotion.py
187 lines (152 loc) · 6.38 KB
/
promotion.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from datetime import date
from itertools import count
class Promotion:
# due_date format = YYYY-MM-DD
def __init__(self, due_date:str, minimum_price:int, title:str="", description:str=""):
self.__due_date = due_date
self.__minimum_price = minimum_price
self.__description = description
self.__title = title
def is_available_date(self):
today = date.today()
y, m, d = [int(x) for x in self.__due_date.split('-')]
due_date = date(y, m, d)
if today > due_date:
return False
return True
def is_available_price(self, price):
if price < self.__minimum_price:
return False
return True
def get_description(self):
return self.__description
def get_minimum_price(self):
return self.__minimum_price
def get_minimum_price_str(self):
return str(self.__minimum_price) + '.-'
def get_due_date_str(self):
try:
month = ["ม.ค.", 'ก.พ.', 'มี.ค.', 'เม.ย.', 'พ.ค.', 'มิ.ย.', 'ก.ค.', 'ส.ค.', 'ก.ย.', 'ต.ค.', 'พ.ย.', 'ธ.ค.']
dmy = self.__due_date.split('-')
return dmy[2] + ' ' + month[int(dmy[1]) - 1] + ' ' + dmy[0]
except:
return ''
def get_title(self):
return self.__title
class FlatDiscount(Promotion):
def __init__(self, due_date:str, minimum_price:int, discount:int, title:str="", description:str=""):
Promotion.__init__(self, due_date, minimum_price, title, description)
self.__discount = discount
def get_discount(self, price):
if self.is_available_price(price):
return self.__discount
return 0
def get_discount_str(self):
return str(self.__discount) + '.-'
class PercentageDiscount(Promotion):
def __init__(self, due_date:str, minimum_price:int, discount:int, max_discount:int, title:str="", description:str=""):
Promotion.__init__(self, due_date, minimum_price, title, description)
self.__discount = discount
self.__max_discount = max_discount
def get_discount(self, price):
if self.is_available_price(price):
discount = self.__discount * price / 100
if discount > self.__max_discount:
return self.__max_discount
return discount
return 0
def get_discount_str(self):
return str(self.__discount) + '%'
coupon_id_gen = count()
class Coupon(Promotion):
def __init__(self, quantity:int, coupon_type:str, ban_products:list=[], ban_types:list=[], types:list=[], brands:list=[]):
self.__quantity = quantity
self.__code_id = None
self.__ban_products = ban_products
self.__ban_types = ban_types
self.__types = types
self.__brands = brands
self.__coupon_type = coupon_type
def set_id(self, id):
self.__code_id = id
def use(self):
if self.__quantity > 0:
self.__quantity -= 1
return True
def get_id(self):
return str(self.__code_id)
def get_coupon_type(self):
return self.__coupon_type
def get_types(self):
if(self.__types == []):
return ["ทั้งหมด"]
return self.__types
def use_coupon(self):
self.__quantity -= 1
return self.__quantity
def is_available_type(self, data):
if self.__quantity < 1:
return False
# data = product.get_type_brand_id()
if data["type"] in self.__ban_types:
return False
if data["id"] in self.__ban_products:
return False
if self.__types == []:
if self.__brands == []:
return True
if data["brand"] in self.__brands:
return True
if data["type"] in self.__types:
if self.__brands == []:
return True
if data["brand"] in self.__brands:
return True
return False
def is_available(self, price, data):
if self.is_available_price(price) and self.is_available_type(data) and self.is_available_date():
return True
return False
class FlatCoupon(FlatDiscount, Coupon):
def __init__(self, due_date:str, minimum_price:int, discount:int, quantity:int, coupon_type:str, title:str="", description:str="", ban_products:list=[], ban_types:list=[], types:list=[], brands:list=[]):
FlatDiscount.__init__(self, due_date, minimum_price, discount, title, description)
Coupon.__init__(self, quantity, coupon_type, ban_products, ban_types, types, brands)
class PercentageCoupon(PercentageDiscount, Coupon):
def __init__(self, due_date:str, minimum_price:int, discount:int, max_discount:int, quantity:int, coupon_type:str, title:str="", description:str="", ban_products:list=[], ban_types:list=[], types:list=[], brands:list=[]):
PercentageDiscount.__init__(self, due_date, minimum_price, discount, max_discount, title, description)
Coupon.__init__(self, quantity, coupon_type, ban_products, ban_types, types, brands)
class CouponCatalog:
def __init__(self):
self.__coupons = []
def get_available_coupon(self, price, data):
available_coupon = []
for coupon in self.__coupons:
if coupon.is_available(price, data):
available_coupon.append(coupon)
return available_coupon
def get_coupons(self):
return self.__coupons
def add_coupon(self, coupon):
coupon.set_id(str(next(coupon_id_gen)))
self.__coupons.append(coupon)
return True
def edit_coupon(self, id, coupon):
id = int(id)
if id < len(self.__coupons):
self.__coupons[id] = coupon
return True
return False
def delete_coupon(self, id):
if self.__coupons[int(id)]:
self.__coupons[int(id)] = None
return True
return False
def get_coupons_sorted_by_coupon_type(self):
sorted_coupon = {'exclusive':[], 'speaker':[], 'gaming':[], 'computer':[], 'table':[]}
for coupon in self.__coupons:
coupon_type = coupon.get_coupon_type()
if coupon_type in sorted_coupon:
sorted_coupon[coupon_type].append(coupon)
return sorted_coupon
def get_coupon_by_id(self, id):
return self.__coupons[int(id)]