| 2465 |
varun.gupt |
1 |
'''
|
|
|
2 |
Created on 06-Jul-2011
|
|
|
3 |
|
|
|
4 |
@author: Varun Gupta
|
|
|
5 |
'''
|
|
|
6 |
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
|
|
|
7 |
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_user
|
| 4859 |
varun.gupt |
8 |
from shop2020.model.v1.user.impl.Dataservice import Discount
|
| 2465 |
varun.gupt |
9 |
|
|
|
10 |
def execute(cart, coupon_code, args):
|
|
|
11 |
if 'items' not in args or 'discount' not in args or 'handset_display_name' not in args:
|
|
|
12 |
raise PromotionException(109, 'Error in rule arguments.')
|
|
|
13 |
|
|
|
14 |
if 'usage_limit' in args and args['usage_limit'] is not None:
|
|
|
15 |
if args['usage_limit'] <= get_coupon_usage_count(coupon_code):
|
|
|
16 |
raise PromotionException(112, 'This promotion is over.')
|
|
|
17 |
|
|
|
18 |
if 'usage_limit_for_user' in args and args['usage_limit_for_user'] is not None:
|
| 4856 |
varun.gupt |
19 |
if args['usage_limit_for_user'] <= get_coupon_usage_count_by_user(coupon_code, cart.userId):
|
| 2465 |
varun.gupt |
20 |
raise PromotionException(111, 'You have already used this coupon maximum possible times.')
|
|
|
21 |
|
|
|
22 |
if not cart.lines:
|
|
|
23 |
return cart
|
|
|
24 |
|
|
|
25 |
total_selling_price = 0
|
|
|
26 |
total_discounted_price = 0
|
|
|
27 |
cart_has_specified_item = False
|
| 4859 |
varun.gupt |
28 |
discounts = []
|
| 2465 |
varun.gupt |
29 |
|
|
|
30 |
for line in cart.lines:
|
|
|
31 |
total_selling_price += line.actualPrice * line.quantity
|
| 4859 |
varun.gupt |
32 |
discount_value = 0
|
| 2465 |
varun.gupt |
33 |
|
|
|
34 |
if line.itemId in args['items']:
|
|
|
35 |
cart_has_specified_item = True
|
|
|
36 |
line.discountedPrice = round(line.actualPrice - args['discount'], 4)
|
|
|
37 |
total_discounted_price += round(line.discountedPrice * line.quantity, 4)
|
| 4859 |
varun.gupt |
38 |
discount_value = args['discount']
|
| 2465 |
varun.gupt |
39 |
else:
|
|
|
40 |
total_discounted_price += round(line.actualPrice * line.quantity, 4)
|
| 4859 |
varun.gupt |
41 |
|
|
|
42 |
if discount_value > 0:
|
|
|
43 |
discount = Discount()
|
|
|
44 |
discount.discount = discount_value
|
|
|
45 |
discount.quantity = line.quantity
|
|
|
46 |
discount.cart_id = cart.id
|
|
|
47 |
discount.item_id = line.itemId
|
|
|
48 |
|
|
|
49 |
discounts.append(discount)
|
| 2465 |
varun.gupt |
50 |
|
|
|
51 |
if cart_has_specified_item is False:
|
|
|
52 |
raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
|
|
|
53 |
|
|
|
54 |
cart.totalPrice = round(total_selling_price, 4)
|
|
|
55 |
cart.discountedPrice = total_discounted_price
|
| 4861 |
varun.gupt |
56 |
return cart, discounts
|