| 4186 |
varun.gupt |
1 |
'''
|
|
|
2 |
Created on 06-Dec-2011
|
|
|
3 |
|
|
|
4 |
@author: varungupta
|
|
|
5 |
'''
|
|
|
6 |
|
| 6250 |
amit.gupta |
7 |
from shop2020.clients.UserClient import UserClient
|
|
|
8 |
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \
|
|
|
9 |
get_coupon_usage_count_by_user
|
| 4941 |
varun.gupt |
10 |
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
|
| 6250 |
amit.gupta |
11 |
from shop2020.utils.Utils import to_py_date
|
|
|
12 |
import datetime
|
| 4186 |
varun.gupt |
13 |
|
|
|
14 |
def execute(cart, coupon_code, args):
|
|
|
15 |
|
|
|
16 |
user_client = UserClient().get_client()
|
| 5331 |
rajveer |
17 |
user = user_client.getUserByCartId(cart.id)
|
| 4186 |
varun.gupt |
18 |
|
| 4291 |
varun.gupt |
19 |
email = user.email.lower().strip()
|
|
|
20 |
|
| 6680 |
anupam.sin |
21 |
if args['couponType'] not in ('physical','both'):
|
| 6679 |
anupam.sin |
22 |
raise PromotionException(111, 'This coupon is not valid')
|
|
|
23 |
|
|
|
24 |
if args['emails'] == '*' or email in args['emails']:
|
| 4941 |
varun.gupt |
25 |
discount_value = args['discount']
|
| 4291 |
varun.gupt |
26 |
else:
|
| 4186 |
varun.gupt |
27 |
raise PromotionException(111, 'You are not allowed to use this coupon')
|
|
|
28 |
|
| 6250 |
amit.gupta |
29 |
todate = datetime.datetime.now()
|
| 6367 |
amit.gupta |
30 |
if 'endOn' in args and todate > to_py_date(args['endOn']):
|
| 6250 |
amit.gupta |
31 |
raise PromotionException(113, 'This coupon is expired.')
|
|
|
32 |
|
| 5353 |
varun.gupt |
33 |
count_coupon_usage = get_coupon_usage_count_by_user(coupon_code, user.userId)
|
| 4186 |
varun.gupt |
34 |
|
| 4941 |
varun.gupt |
35 |
if count_coupon_usage >= args['usage_limit_for_user']:
|
|
|
36 |
raise PromotionException(112, 'Your usage limit for this coupon is exhausted.')
|
| 4186 |
varun.gupt |
37 |
|
|
|
38 |
discounts = []
|
|
|
39 |
|
|
|
40 |
if cart.lines:
|
|
|
41 |
total_selling_price = 0
|
|
|
42 |
total_discounted_price = 0
|
|
|
43 |
|
|
|
44 |
for line in cart.lines:
|
| 6009 |
amit.gupta |
45 |
line_total_price = line.actualPrice * line.quantity
|
|
|
46 |
line_total_discount = 0
|
|
|
47 |
while line_total_discount < line_total_price and discount_value > 0:
|
|
|
48 |
if line.actualPrice < discount_value:
|
|
|
49 |
discountGiven = line.actualPrice
|
|
|
50 |
quantity = min(line.quantity,int(discount_value/discountGiven))
|
|
|
51 |
else:
|
|
|
52 |
discountGiven = discount_value
|
|
|
53 |
quantity = 1
|
|
|
54 |
|
|
|
55 |
|
| 4186 |
varun.gupt |
56 |
discount = Discount()
|
|
|
57 |
discount.cart_id = cart.id
|
|
|
58 |
discount.item_id = line.itemId
|
| 6009 |
amit.gupta |
59 |
discount.discount = discountGiven
|
|
|
60 |
discount.quantity = quantity
|
|
|
61 |
discounts.append(discount)
|
| 4186 |
varun.gupt |
62 |
|
| 6009 |
amit.gupta |
63 |
discount_value = discount_value - discountGiven*quantity
|
|
|
64 |
line_total_discount += discountGiven*quantity
|
| 4186 |
varun.gupt |
65 |
|
| 6009 |
amit.gupta |
66 |
total_selling_price += line_total_price
|
|
|
67 |
total_discounted_price += line_total_price - line_total_discount
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
cart.totalPrice = total_selling_price
|
|
|
71 |
cart.discountedPrice = total_discounted_price
|
| 4186 |
varun.gupt |
72 |
|
| 5044 |
varun.gupt |
73 |
return cart, discounts
|
|
|
74 |
|
|
|
75 |
def getDiscountOnItem(item):
|
|
|
76 |
return None
|