| 2121 |
varun.gupt |
1 |
'''
|
| 3871 |
varun.gupt |
2 |
Created on 24-Oct-2011
|
| 2121 |
varun.gupt |
3 |
|
| 3871 |
varun.gupt |
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.
|
| 2121 |
varun.gupt |
8 |
'''
|
| 3871 |
varun.gupt |
9 |
|
| 2121 |
varun.gupt |
10 |
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
|
| 3871 |
varun.gupt |
11 |
from shop2020.clients.UserClient import UserClient
|
|
|
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
|
| 2121 |
varun.gupt |
14 |
|
|
|
15 |
def execute(cart, coupon_code, args):
|
|
|
16 |
|
| 3871 |
varun.gupt |
17 |
user_client = UserClient().get_client()
|
|
|
18 |
user = user_client.getUserById(cart.userId)
|
| 2121 |
varun.gupt |
19 |
|
| 3937 |
varun.gupt |
20 |
if not user.email.lower().strip() in ('pankaj.jain@shop2020.in', 'pankaj.kankar@shop2020.in', 'rajveer.singh@shop2020.in', \
|
| 3871 |
varun.gupt |
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'):
|
|
|
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 |
|
| 3877 |
varun.gupt |
31 |
if count_coupon_usage > 0:
|
|
|
32 |
raise PromotionException(112, 'You have already used the voucher.')
|
| 2121 |
varun.gupt |
33 |
|
| 3871 |
varun.gupt |
34 |
discounts = []
|
|
|
35 |
|
| 2121 |
varun.gupt |
36 |
if cart.lines:
|
|
|
37 |
total_selling_price = 0
|
| 3871 |
varun.gupt |
38 |
total_discounted_price = 0
|
|
|
39 |
available_discount = 2000
|
| 2121 |
varun.gupt |
40 |
|
|
|
41 |
for line in cart.lines:
|
| 3871 |
varun.gupt |
42 |
line.discountedPrice = line.actualPrice
|
|
|
43 |
discount_value = 0
|
| 2131 |
varun.gupt |
44 |
|
| 3871 |
varun.gupt |
45 |
if available_discount > 0:
|
|
|
46 |
if line.actualPrice < available_discount:
|
|
|
47 |
line.discountedPrice = 0
|
|
|
48 |
discount_value = line.actualPrice
|
|
|
49 |
available_discount -= line.actualPrice
|
|
|
50 |
else:
|
|
|
51 |
line.discountedPrice = line.actualPrice - available_discount
|
|
|
52 |
discount_value = available_discount
|
|
|
53 |
available_discount = 0
|
|
|
54 |
|
|
|
55 |
total_selling_price += line.actualPrice * line.quantity
|
|
|
56 |
total_discounted_price += line.discountedPrice * line.quantity
|
|
|
57 |
|
|
|
58 |
if discount_value > 0:
|
|
|
59 |
discount = Discount()
|
|
|
60 |
discount.cart_id = cart.id
|
|
|
61 |
discount.item_id = line.itemId
|
|
|
62 |
discount.discount = discount_value
|
|
|
63 |
discount.quantity = 1
|
| 2121 |
varun.gupt |
64 |
|
| 3871 |
varun.gupt |
65 |
discounts.append(discount)
|
|
|
66 |
|
|
|
67 |
cart.totalPrice = total_selling_price
|
|
|
68 |
cart.discountedPrice = total_discounted_price
|
|
|
69 |
|
|
|
70 |
return cart, discounts
|