| 4186 |
varun.gupt |
1 |
'''
|
|
|
2 |
Created on 06-Dec-2011
|
|
|
3 |
|
|
|
4 |
@author: varungupta
|
|
|
5 |
'''
|
|
|
6 |
|
|
|
7 |
'''
|
|
|
8 |
Created on 24-Oct-2011
|
|
|
9 |
|
|
|
10 |
@author: Varun Gupta
|
|
|
11 |
|
|
|
12 |
raghvendra.saboo@gmail.com will get Rs.250 off on one transaction.
|
|
|
13 |
'''
|
|
|
14 |
|
|
|
15 |
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
|
|
|
16 |
from shop2020.clients.UserClient import UserClient
|
|
|
17 |
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count_by_user
|
|
|
18 |
from shop2020.thriftpy.model.v1.user.ttypes import Discount
|
|
|
19 |
|
|
|
20 |
def execute(cart, coupon_code, args):
|
|
|
21 |
|
|
|
22 |
user_client = UserClient().get_client()
|
|
|
23 |
user = user_client.getUserById(cart.userId)
|
|
|
24 |
|
|
|
25 |
if not user.email.lower().strip() in ('raghvendra.saboo@gmail.com'):
|
|
|
26 |
raise PromotionException(111, 'You are not allowed to use this coupon')
|
|
|
27 |
|
|
|
28 |
#Allow only first 1000 users to use the coupon
|
|
|
29 |
count_coupon_usage = get_coupon_usage_count_by_user(coupon_code, cart.userId)
|
|
|
30 |
|
|
|
31 |
if count_coupon_usage > 0:
|
|
|
32 |
raise PromotionException(112, 'You have already used the coupon.')
|
|
|
33 |
|
|
|
34 |
discounts = []
|
|
|
35 |
|
|
|
36 |
if cart.lines:
|
|
|
37 |
total_selling_price = 0
|
|
|
38 |
total_discounted_price = 0
|
| 4187 |
varun.gupt |
39 |
has_used_coupon = False
|
| 4186 |
varun.gupt |
40 |
|
|
|
41 |
for line in cart.lines:
|
|
|
42 |
line.discountedPrice = line.actualPrice
|
|
|
43 |
discount_value = 0
|
|
|
44 |
|
| 4187 |
varun.gupt |
45 |
if has_used_coupon:
|
|
|
46 |
break
|
| 4186 |
varun.gupt |
47 |
|
| 4187 |
varun.gupt |
48 |
if line.actualPrice < 5000:
|
|
|
49 |
line.discountedPrice = line.actualPrice - 200
|
|
|
50 |
discount_value = 200
|
|
|
51 |
else:
|
|
|
52 |
line.discountedPrice = line.actualPrice - 500
|
|
|
53 |
discount_value = 500
|
|
|
54 |
|
|
|
55 |
has_used_coupon = True
|
|
|
56 |
|
| 4186 |
varun.gupt |
57 |
total_selling_price += line.actualPrice * line.quantity
|
|
|
58 |
total_discounted_price += line.discountedPrice * line.quantity
|
|
|
59 |
|
|
|
60 |
if discount_value > 0:
|
|
|
61 |
discount = Discount()
|
|
|
62 |
discount.cart_id = cart.id
|
|
|
63 |
discount.item_id = line.itemId
|
|
|
64 |
discount.discount = discount_value
|
|
|
65 |
discount.quantity = 1
|
|
|
66 |
|
|
|
67 |
discounts.append(discount)
|
|
|
68 |
|
|
|
69 |
cart.totalPrice = total_selling_price
|
|
|
70 |
cart.discountedPrice = total_discounted_price
|
|
|
71 |
|
|
|
72 |
return cart, discounts
|