| 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 |
|
| 6682 |
anupam.sin |
21 |
if args['couponType'] is not None and args['couponType'] != 'physical' and args['couponType'] != 'both':
|
| 6679 |
anupam.sin |
22 |
raise PromotionException(111, 'This coupon is not valid')
|
|
|
23 |
|
|
|
24 |
if args['emails'] == '*' or email in args['emails']:
|
| 7475 |
anupam.sin |
25 |
discount_value_left = 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:
|
| 7475 |
anupam.sin |
41 |
total_discount_given = 0
|
| 4186 |
varun.gupt |
42 |
|
|
|
43 |
for line in cart.lines:
|
| 6903 |
anupam.sin |
44 |
line_total_price = line.actualPrice * line.quantity + line.insuranceAmount
|
| 7475 |
anupam.sin |
45 |
discountGivenForOneLine = 0
|
|
|
46 |
while discountGivenForOneLine < line_total_price and discount_value_left > 0:
|
|
|
47 |
priceOfOneItemIncludingInsurance = line.actualPrice + line.insuranceAmount/line.quantity
|
|
|
48 |
if priceOfOneItemIncludingInsurance < discount_value_left:
|
|
|
49 |
discountGiven = priceOfOneItemIncludingInsurance
|
|
|
50 |
quantity = min(line.quantity,int(discount_value_left/discountGiven))
|
| 6009 |
amit.gupta |
51 |
else:
|
| 7475 |
anupam.sin |
52 |
discountGiven = discount_value_left
|
| 6009 |
amit.gupta |
53 |
quantity = 1
|
|
|
54 |
|
| 4186 |
varun.gupt |
55 |
discount = Discount()
|
|
|
56 |
discount.cart_id = cart.id
|
|
|
57 |
discount.item_id = line.itemId
|
| 6009 |
amit.gupta |
58 |
discount.discount = discountGiven
|
|
|
59 |
discount.quantity = quantity
|
|
|
60 |
discounts.append(discount)
|
| 4186 |
varun.gupt |
61 |
|
| 7475 |
anupam.sin |
62 |
discount_value_left = discount_value_left - discountGiven*quantity
|
|
|
63 |
discountGivenForOneLine += discountGiven*quantity
|
| 4186 |
varun.gupt |
64 |
|
| 7475 |
anupam.sin |
65 |
total_discount_given += discountGivenForOneLine
|
| 6009 |
amit.gupta |
66 |
|
| 7475 |
anupam.sin |
67 |
cart.discountedPrice = cart.totalPrice - total_discount_given
|
| 4186 |
varun.gupt |
68 |
|
| 5044 |
varun.gupt |
69 |
return cart, discounts
|
|
|
70 |
|
|
|
71 |
def getDiscountOnItem(item):
|
|
|
72 |
return None
|