-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMohamadHossein_Mahmodian_HW1_Maktab41.py
218 lines (186 loc) · 5.68 KB
/
MohamadHossein_Mahmodian_HW1_Maktab41.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
product_list = [
{
"type": "1",
"name": "shirt",
"price": 30,
"unit": "Dollar",
"commission_groups": ["A", "B"]
},
{
"type": "2",
"name": "pants",
"price": 50,
"unit": "Dollar",
"commission_groups": ["A", "C"]
},
{
"type": "3",
"name": "shoes",
"price": 80,
"unit": "Dollar",
"commission_groups": ["B"]
},
{
"type": "4",
"name": "hat",
"price": 20,
"unit": "Dollar",
"commission_groups": []
}
]
markup_list = [
{
"product_type": "1",
"lower_cost": 10,
"upper_cost": 20,
"unit": "percent",
"lower_count": 10
},
{
"product_type": "2",
"lower_cost": 15,
"upper_cost": 20,
"unit": "percent",
"lower_count": 10
},
{
"product_type": "3",
"lower_cost": 10,
"upper_cost": 15,
"unit": "percent",
"lower_count": 5
},
{
"product_type": "4",
"lower_cost": 10,
"upper_cost": 30,
"unit": "percent",
"lower_count": 20
}
]
commission_list = [
{
"group_name": "A",
"cost": 5,
"unit": "percent",
"users": [1001, 1002, 1003, 1005]
},
{
"group_name": "B",
"cost": 3,
"unit": "Dollar",
"users": [1001, 1003, 1006]
},
{
"group_name": "C",
"cost": 7,
"unit": "percent",
"users": [1001, 1002, 1004]
}
]
user_list = [
{
"userid": 1001,
"first_name": "Mohsen",
"last_name": "Bayat",
},
{
"userid": 1002,
"first_name": "Sobhan",
"last_name": "Taghadosi",
},
{
"userid": 1003,
"first_name": "Javad",
"last_name": "Jafari",
},
{
"userid": 1004,
"first_name": "Masoud",
"last_name": "Hosseini",
},
{
"userid": 1005,
"first_name": "Hassan",
"last_name": "Zand",
},
{
"userid": 1006,
"first_name": "Ali",
"last_name": "Ebadi",
}
]
def markup_calc(product_type,product_count):
for i in markup_list:
product_type_trigger = i['product_type']
if product_type == int(product_type_trigger):
selected_product = i
break
lower_count = int(selected_product['lower_count'])
if product_count == 1:
return selected_product['upper_cost']
elif product_count >= lower_count:
return selected_product['lower_cost']
elif 1 < product_count < lower_count:
slope = (selected_product['upper_cost'] - selected_product['lower_cost']) / (1 - selected_product['lower_count'])
slope_abs = abs(round(slope,3))
B = slope_abs + selected_product['upper_cost']
linear_cost = round(slope,3) * product_count + B
return round(linear_cost,3)
def commission_calc(product_type,product_count,uID = 0):
markup_precent = markup_calc(product_type,product_count)
print(f'\nmarkup_precent {markup_precent}\n')
for i in product_list:
product_type_trigger = int(i['type'])
if product_type == product_type_trigger:
bought_product = i
product_price = bought_product['price']
product_name = bought_product['name']
product_groups = bought_product['commission_groups']
user_subscribe_group = []
for group in product_groups:
for group_name in commission_list:
group_name_trigger = group_name['group_name']
if group_name_trigger == group:
user_subscribe_group.append(group_name)
first_name = ''
last_name = ''
user_subscribe_group_status = ''
for user in user_list:
if user['userid'] == uID:
first_name = user['first_name']
last_name = user['last_name']
user_subscribe_group_status = user_subscribe_group
break
markup_value = round(product_price * markup_precent / 100,3)
total_price = (product_price + markup_value) * product_count
for j in user_subscribe_group:
if uID in j['users']:
print(f'user is in group {j["group_name"]}')
else:
user_subscribe_group.remove(j)
if len(user_subscribe_group_status) != 0:
compare_commition_list = []
for t in user_subscribe_group:
commission_cost_temp = t['cost']
commission_unit_temp = t['unit']
if commission_unit_temp == 'percent':
commission_precent = total_price * commission_cost_temp / 100
total_with_commition = total_price - commission_precent
elif commission_unit_temp == 'Dollar':
total_with_commition = total_price - commission_cost_temp
compare_commition_list.append(total_with_commition)
minimum = compare_commition_list[0]
for w in compare_commition_list:
if w < minimum:
minimum = w
total_with_commition = minimum
result = {"product_name": product_name,"total_price": total_price,"total_with_commission": round(total_with_commition,3),"discount": commission_precent,"username": {"first_name": first_name,"last_name": last_name}}
return result
else:
result = {"product_name": product_name,"total_price": total_price,"total_with_commission": total_price, "discount": 0,"username": {"first_name": first_name,"last_name": last_name}}
return result
print(commission_calc(1,10,1002))
print(commission_calc(1,15,1003))
print(commission_calc(4,20,1005))
print(commission_calc(2,1))