| 2465 |
varun.gupt |
1 |
'''
|
|
|
2 |
Created on 06-Jul-2011
|
|
|
3 |
|
|
|
4 |
@author: Varun Gupta
|
|
|
5 |
'''
|
| 6538 |
amit.gupta |
6 |
from shop2020.clients.CatalogClient import CatalogClient
|
| 6470 |
amit.gupta |
7 |
from shop2020.clients.UserClient import UserClient
|
|
|
8 |
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \
|
|
|
9 |
get_coupon_usage_count, get_coupon_usage_count_by_user
|
| 4862 |
varun.gupt |
10 |
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
|
| 6470 |
amit.gupta |
11 |
import datetime
|
| 7781 |
amit.gupta |
12 |
from math import floor
|
| 2465 |
varun.gupt |
13 |
|
|
|
14 |
def execute(cart, coupon_code, args):
|
| 9168 |
amit.gupta |
15 |
|
|
|
16 |
appliedOnce = False
|
|
|
17 |
|
| 6538 |
amit.gupta |
18 |
if 'handset_display_name' not in args:
|
| 2465 |
varun.gupt |
19 |
raise PromotionException(109, 'Error in rule arguments.')
|
| 6538 |
amit.gupta |
20 |
|
| 6471 |
amit.gupta |
21 |
if 'end_on' in args and datetime.datetime.strptime(args['end_on'], '%Y-%m-%d') <= datetime.datetime.now():
|
| 6470 |
amit.gupta |
22 |
raise PromotionException(112, 'This promotion is expired.')
|
|
|
23 |
|
| 2465 |
varun.gupt |
24 |
if 'usage_limit' in args and args['usage_limit'] is not None:
|
|
|
25 |
if args['usage_limit'] <= get_coupon_usage_count(coupon_code):
|
|
|
26 |
raise PromotionException(112, 'This promotion is over.')
|
|
|
27 |
|
| 5331 |
rajveer |
28 |
user_client = UserClient().get_client()
|
|
|
29 |
user = user_client.getUserByCartId(cart.id)
|
|
|
30 |
|
| 2465 |
varun.gupt |
31 |
if 'usage_limit_for_user' in args and args['usage_limit_for_user'] is not None:
|
| 5353 |
varun.gupt |
32 |
if args['usage_limit_for_user'] <= get_coupon_usage_count_by_user(coupon_code, user.userId):
|
| 2465 |
varun.gupt |
33 |
raise PromotionException(111, 'You have already used this coupon maximum possible times.')
|
|
|
34 |
|
|
|
35 |
if not cart.lines:
|
|
|
36 |
return cart
|
| 6538 |
amit.gupta |
37 |
|
|
|
38 |
|
|
|
39 |
discountJ = None
|
|
|
40 |
if 'discount' in args:
|
|
|
41 |
discountJ = args ['discount']
|
|
|
42 |
|
|
|
43 |
min_amount = None
|
|
|
44 |
if 'min_amount' in args:
|
|
|
45 |
min_amount = args ['min_amount']
|
|
|
46 |
|
| 7781 |
amit.gupta |
47 |
max_discount = 10000
|
| 6538 |
amit.gupta |
48 |
if 'max_discount' in args:
|
|
|
49 |
max_discount = args ['max_discount']
|
| 7781 |
amit.gupta |
50 |
|
|
|
51 |
discount_left = max_discount
|
| 6538 |
amit.gupta |
52 |
|
| 6450 |
amit.gupta |
53 |
isPercentageDiscount = False
|
| 6538 |
amit.gupta |
54 |
if 'is_percentage_discount' in args:
|
|
|
55 |
isPercentageDiscount = args['is_percentage_discount']
|
|
|
56 |
|
| 6903 |
anupam.sin |
57 |
total_discounts = 0
|
| 2465 |
varun.gupt |
58 |
cart_has_specified_item = False
|
| 4859 |
varun.gupt |
59 |
discounts = []
|
| 6538 |
amit.gupta |
60 |
|
| 2465 |
varun.gupt |
61 |
for line in cart.lines:
|
| 7781 |
amit.gupta |
62 |
if discount_left > 0 :
|
|
|
63 |
discount_value = 0
|
|
|
64 |
|
|
|
65 |
if min_amount is not None and line.actualPrice < min_amount:
|
| 6538 |
amit.gupta |
66 |
continue
|
| 7781 |
amit.gupta |
67 |
|
|
|
68 |
if 'items' in args:
|
|
|
69 |
if line.itemId in args['items'] :
|
|
|
70 |
if type(args['items'])==dict:
|
|
|
71 |
discount_value = args['items'][line.itemId]
|
|
|
72 |
cart_has_specified_item = True
|
|
|
73 |
elif discountJ is not None:
|
|
|
74 |
discount_value = discountJ
|
|
|
75 |
cart_has_specified_item = True
|
|
|
76 |
else:
|
|
|
77 |
continue
|
|
|
78 |
elif 'categories' in args:
|
|
|
79 |
catalog_client = CatalogClient().get_client()
|
|
|
80 |
category = catalog_client.getItem(line.itemId).category
|
|
|
81 |
if category in args['categories']:
|
|
|
82 |
if type(args['categories'])==dict:
|
|
|
83 |
discount_value = args['categories'][category]
|
|
|
84 |
cart_has_specified_item = True
|
|
|
85 |
elif discountJ is not None:
|
|
|
86 |
discount_value = discountJ
|
|
|
87 |
cart_has_specified_item = True
|
|
|
88 |
else:
|
|
|
89 |
continue
|
| 9140 |
amit.gupta |
90 |
|
|
|
91 |
elif 'brands' in args:
|
|
|
92 |
catalog_client = CatalogClient().get_client()
|
|
|
93 |
brand = catalog_client.getItem(line.itemId).brand
|
|
|
94 |
if brand in args['brands']:
|
|
|
95 |
if type(args['brands'])==dict:
|
| 9141 |
amit.gupta |
96 |
discount_value = args['brands'][brand]
|
| 9140 |
amit.gupta |
97 |
cart_has_specified_item = True
|
|
|
98 |
elif discountJ is not None:
|
|
|
99 |
discount_value = discountJ
|
|
|
100 |
cart_has_specified_item = True
|
|
|
101 |
else:
|
|
|
102 |
continue
|
|
|
103 |
|
| 7781 |
amit.gupta |
104 |
elif discountJ is not None:
|
|
|
105 |
discount_value = discountJ
|
|
|
106 |
cart_has_specified_item = True
|
|
|
107 |
else :
|
| 6538 |
amit.gupta |
108 |
continue
|
| 7781 |
amit.gupta |
109 |
|
|
|
110 |
if isPercentageDiscount:
|
|
|
111 |
discount_value = round((line.actualPrice*discount_value)/100, 4)
|
| 9168 |
amit.gupta |
112 |
|
|
|
113 |
if 'appliedOnce' in args and args['appliedOnce']:
|
|
|
114 |
quantity = 1
|
|
|
115 |
appliedOnce = True
|
|
|
116 |
else:
|
|
|
117 |
quantity = min(floor(discount_left/discount_value), line.quantity)
|
| 6538 |
amit.gupta |
118 |
|
| 7781 |
amit.gupta |
119 |
if quantity > 0:
|
|
|
120 |
discount = Discount()
|
|
|
121 |
discount.cart_id = cart.id
|
|
|
122 |
discount.item_id = line.itemId
|
|
|
123 |
discount.quantity = quantity
|
|
|
124 |
discount.discount = discount_value
|
|
|
125 |
discounts.append(discount)
|
|
|
126 |
|
|
|
127 |
discount_left = discount_left - quantity*discount_value
|
|
|
128 |
total_discounts += round(discount_value * quantity, 4)
|
| 4859 |
varun.gupt |
129 |
|
| 9168 |
amit.gupta |
130 |
if appliedOnce :
|
|
|
131 |
break
|
|
|
132 |
|
| 7781 |
amit.gupta |
133 |
if discount_left > 0 and line.quantity - quantity > 0:
|
|
|
134 |
discountOne = Discount()
|
|
|
135 |
discountOne.cart_id = cart.id
|
|
|
136 |
discountOne.item_id = line.itemId
|
|
|
137 |
discountOne.quantity = 1
|
|
|
138 |
discountOne.discount = discount_left
|
|
|
139 |
discounts.append(discountOne)
|
|
|
140 |
|
|
|
141 |
discount_left = 0
|
|
|
142 |
total_discounts = max_discount
|
| 9168 |
amit.gupta |
143 |
|
| 7781 |
amit.gupta |
144 |
else:
|
|
|
145 |
break
|
| 9168 |
amit.gupta |
146 |
|
|
|
147 |
|
| 2465 |
varun.gupt |
148 |
if cart_has_specified_item is False:
|
|
|
149 |
raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
|
|
|
150 |
|
| 8456 |
amit.gupta |
151 |
cart.discountedPrice = cart.totalPrice - round(total_discounts, 0)
|
| 5044 |
varun.gupt |
152 |
return cart, discounts
|
|
|
153 |
|
|
|
154 |
def getDiscountOnItem(item):
|
|
|
155 |
return None
|