| Line 1... |
Line 1... |
| 1 |
'''
|
1 |
'''
|
| 2 |
Created on 08-Jun-2011
|
2 |
Created on 24-Oct-2011
|
| 3 |
|
3 |
|
| 4 |
@author: varungupta
|
4 |
@author: Varun Gupta
|
| - |
|
5 |
|
| - |
|
6 |
A user with email on shop2020.in domain can avail a discount
|
| - |
|
7 |
of Rs.2000. The user can use this coupon only once.
|
| 5 |
'''
|
8 |
'''
|
| - |
|
9 |
|
| 6 |
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
|
10 |
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
|
| - |
|
11 |
from shop2020.clients.UserClient import UserClient
|
| 7 |
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_user
|
12 |
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count_by_user
|
| - |
|
13 |
from shop2020.thriftpy.model.v1.user.ttypes import Discount
|
| 8 |
|
14 |
|
| 9 |
def execute(cart, coupon_code, args):
|
15 |
def execute(cart, coupon_code, args):
|
| 10 |
#Allow a user to use the coupon only once.
|
- |
|
| 11 |
count_users_usage = get_coupon_usage_count_by_user(coupon_code, cart.userId)
|
- |
|
| 12 |
|
16 |
|
| 13 |
if count_users_usage > 0:
|
17 |
user_client = UserClient().get_client()
|
| - |
|
18 |
user = user_client.getUserById(cart.userId)
|
| - |
|
19 |
|
| - |
|
20 |
if not user.email.strip() in ('pankaj.jain@shop2020.in', 'pankaj.kankar@shop2020.in', 'rajveer.singh@shop2020.in', \
|
| - |
|
21 |
'chandranshu.s@shop2020.in', 'vikas.malik@shop2020.in', 'varun.gupta@shop2020.in', \
|
| - |
|
22 |
'mandeep.dhir@shop2020.in', 'vrinda.k@shop2020.in', 'smriti.r@shop2020.in', \
|
| - |
|
23 |
'tarita.kulkani@shop2020.in', 'manmohan.singh@shop2020.in', 'anand.sinha@shop2020.in', \
|
| - |
|
24 |
'parmod.kumar@shop2020.in', 'satya.mahapatra@shop2020.in', 'abhishek.mathur@shop2020.in', \
|
| - |
|
25 |
'chaitnaya.vats@shop2020.in', 'j.p.gupta@shop2020.in', 'zaffar.kar@shop2020.in'):
|
| 14 |
raise PromotionException(111, 'You have already used this Coupon.')
|
26 |
raise PromotionException(111, 'You are not allowed to use this coupon')
|
| 15 |
|
27 |
|
| 16 |
#Allow only first 50 users to use the coupon
|
28 |
#Allow only first 1000 users to use the coupon
|
| 17 |
count_coupon_usage = get_coupon_usage_count(coupon_code)
|
29 |
count_coupon_usage = get_coupon_usage_count_by_user(coupon_code, cart.userId)
|
| - |
|
30 |
|
| 18 |
if count_coupon_usage >= 50:
|
31 |
if count_coupon_usage >= 1000:
|
| 19 |
raise PromotionException(112, 'This promotion is over.')
|
32 |
raise PromotionException(112, 'This promotion is over.')
|
| 20 |
|
33 |
|
| - |
|
34 |
discounts = []
|
| - |
|
35 |
|
| 21 |
if cart.lines:
|
36 |
if cart.lines:
|
| 22 |
total_selling_price = 0
|
37 |
total_selling_price = 0
|
| - |
|
38 |
total_discounted_price = 0
|
| - |
|
39 |
available_discount = 2000
|
| 23 |
|
40 |
|
| 24 |
for line in cart.lines:
|
41 |
for line in cart.lines:
|
| 25 |
#Disabling Coupon for Samsung Galaxy SII
|
42 |
line.discountedPrice = line.actualPrice
|
| - |
|
43 |
discount_value = 0
|
| 26 |
|
44 |
|
| 27 |
if line.itemId == 1502:
|
45 |
if available_discount > 0:
|
| 28 |
raise PromotionException(115, 'This coupon is not applicable for Samsung Galaxy S II')
|
46 |
if line.actualPrice < available_discount:
|
| - |
|
47 |
line.discountedPrice = 0
|
| 29 |
if line.itemId == 1491 or line.itemId == 1521:
|
48 |
discount_value = line.actualPrice
|
| 30 |
raise PromotionException(115, 'This coupon is not applicable for HTC Desire A8181')
|
49 |
available_discount -= line.actualPrice
|
| 31 |
if line.itemId == 13:
|
50 |
else:
|
| 32 |
raise PromotionException(115, 'This coupon is not applicable for HTC Desire Z A7272')
|
51 |
line.discountedPrice = line.actualPrice - available_discount
|
| 33 |
if line.actualPrice:
|
52 |
discount_value = available_discount
|
| 34 |
total_selling_price += line.actualPrice * line.quantity
|
53 |
available_discount = 0
|
| 35 |
|
54 |
|
| 36 |
#MAX Discount 1500
|
- |
|
| 37 |
if total_selling_price * 0.05 >= 1500:
|
55 |
total_selling_price += line.actualPrice * line.quantity
|
| 38 |
discount_perc = 1500 / total_selling_price
|
56 |
total_discounted_price += line.discountedPrice * line.quantity
|
| 39 |
elif total_selling_price >= 5000:
|
57 |
|
| 40 |
discount_perc = 0.05
|
58 |
if discount_value > 0:
|
| 41 |
else:
|
59 |
discount = Discount()
|
| 42 |
discount_perc = 0.03
|
60 |
discount.cart_id = cart.id
|
| 43 |
|
61 |
discount.item_id = line.itemId
|
| 44 |
cart.totalPrice = round(total_selling_price, 2)
|
62 |
discount.discount = discount_value
|
| 45 |
cart.discountedPrice = round((1 - discount_perc) * total_selling_price, 2)
|
63 |
discount.quantity = 1
|
| 46 |
|
64 |
|
| 47 |
for line in cart.lines:
|
65 |
discounts.append(discount)
|
| 48 |
if line.actualPrice:
|
66 |
|
| 49 |
line.discountedPrice = round((1 - discount_perc) * line.actualPrice, 2)
|
67 |
cart.totalPrice = total_selling_price
|
| 50 |
print "The final price, after discount is " + str(cart.discountedPrice)
|
68 |
cart.discountedPrice = total_discounted_price
|
| - |
|
69 |
|
| 51 |
return cart
|
70 |
return cart, discounts
|
| 52 |
|
71 |
|