| 6081 |
amit.gupta |
1 |
'''
|
|
|
2 |
Created on 06-Jul-2011
|
|
|
3 |
|
|
|
4 |
@author: Varun Gupta
|
|
|
5 |
'''
|
|
|
6 |
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
|
|
|
7 |
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_user
|
|
|
8 |
from shop2020.clients.UserClient import UserClient
|
|
|
9 |
|
|
|
10 |
itemTuple = (6737)
|
|
|
11 |
def execute(cart, coupon_code, args):
|
|
|
12 |
|
|
|
13 |
if 2 <= get_coupon_usage_count(coupon_code):
|
|
|
14 |
raise PromotionException(112, 'This promotion is over.')
|
|
|
15 |
|
|
|
16 |
user_client = UserClient().get_client()
|
|
|
17 |
user = user_client.getUserByCartId(cart.id)
|
|
|
18 |
|
|
|
19 |
if 2 <= get_coupon_usage_count_by_user(coupon_code, user.userId):
|
|
|
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
|
|
|
28 |
discount = 2000
|
|
|
29 |
discounts = []
|
|
|
30 |
for line in cart.lines:
|
|
|
31 |
total_selling_price += line.actualPrice * line.quantity
|
|
|
32 |
discount_value = 0
|
|
|
33 |
|
|
|
34 |
if line.itemId == itemTuple:
|
|
|
35 |
cart_has_specified_item = True
|
|
|
36 |
line.discountedPrice = round(line.actualPrice - discount, 4)
|
|
|
37 |
total_discounted_price += round(line.discountedPrice * line.quantity, 4)
|
|
|
38 |
discount_value = discount
|
|
|
39 |
else:
|
|
|
40 |
total_discounted_price += round(line.actualPrice * line.quantity, 4)
|
|
|
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)
|
|
|
50 |
|
|
|
51 |
if cart_has_specified_item is False:
|
|
|
52 |
raise PromotionException(115, 'This coupon is applicable only for Samsung Note II N7100 16GB')
|
|
|
53 |
|
|
|
54 |
cart.totalPrice = round(total_selling_price, 4)
|
|
|
55 |
cart.discountedPrice = total_discounted_price
|
|
|
56 |
return cart, discounts
|
|
|
57 |
|
|
|
58 |
def getDiscountOnItem(item):
|
|
|
59 |
if item.id in itemTuple:
|
|
|
60 |
return None
|
|
|
61 |
return None
|