| 11653 |
amit.gupta |
1 |
'''
|
|
|
2 |
Created on 06-Dec-2011
|
|
|
3 |
|
|
|
4 |
@author: varungupta
|
|
|
5 |
'''
|
|
|
6 |
|
|
|
7 |
from shop2020.clients.CatalogClient import CatalogClient
|
|
|
8 |
from shop2020.clients.UserClient import UserClient
|
|
|
9 |
from shop2020.model.v1.user.impl.Dataservice import PrivateDealUser
|
|
|
10 |
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
def execute(cart, coupon_code, args):
|
|
|
14 |
user_client = UserClient().get_client()
|
|
|
15 |
user = user_client.getUserByCartId(cart.id)
|
| 11663 |
amit.gupta |
16 |
if user.isAnonymous:
|
|
|
17 |
raise PromotionException(115, 'Please try logging in')
|
|
|
18 |
|
| 11653 |
amit.gupta |
19 |
#email = user.email.lower().strip()
|
| 11658 |
amit.gupta |
20 |
privateDealUser = PrivateDealUser.get_by(id=user.userId)
|
| 11666 |
amit.gupta |
21 |
print "privateDealUser.isActive" + `privateDealUser.isActive`
|
| 11664 |
amit.gupta |
22 |
if privateDealUser is not None and privateDealUser.isActive:
|
| 11665 |
amit.gupta |
23 |
if cart.lines is not None:
|
| 11653 |
amit.gupta |
24 |
discounts = []
|
|
|
25 |
itemIds = []
|
|
|
26 |
[itemIds.append(line.itemId) for line in cart.lines]
|
| 11659 |
amit.gupta |
27 |
catalog_client = CatalogClient().get_client()
|
| 11660 |
amit.gupta |
28 |
privateDeals= catalog_client.getAllActivePrivateDeals(itemIds,0)
|
| 11653 |
amit.gupta |
29 |
if(len(privateDeals) == 0):
|
|
|
30 |
raise PromotionException(115, 'None of the products have valid Private Deal')
|
|
|
31 |
dealItems = privateDeals.keys()
|
|
|
32 |
total_discounts = 0
|
|
|
33 |
for line in cart.lines:
|
|
|
34 |
if line.itemId in dealItems:
|
| 11662 |
amit.gupta |
35 |
discountAmount = line.actualPrice - privateDeals[line.itemId].dealPrice
|
|
|
36 |
if discountAmount > 0:
|
|
|
37 |
discount = Discount()
|
|
|
38 |
discount.cart_id = cart.id
|
|
|
39 |
discount.item_id = line.itemId
|
|
|
40 |
discount.quantity = line.quantity
|
|
|
41 |
discount.discount = discountAmount
|
|
|
42 |
discounts.append(discount)
|
| 11669 |
amit.gupta |
43 |
total_discounts = total_discounts + discountAmount*line.quantity
|
| 11653 |
amit.gupta |
44 |
if privateDeals[line.itemId].dealTextOption==0:
|
|
|
45 |
line.dealText = ''
|
| 11868 |
amit.gupta |
46 |
cart.message = 'Deal text has changed'
|
| 11653 |
amit.gupta |
47 |
if privateDeals[line.itemId].dealTextOption==2:
|
|
|
48 |
line.dealText = privateDeals[line.itemId].dealText
|
| 11848 |
amit.gupta |
49 |
cart.message = 'Deal text has changed'
|
| 11653 |
amit.gupta |
50 |
|
|
|
51 |
if privateDeals[line.itemId].dealFreebieOption==0:
|
|
|
52 |
line.freebieId = 0
|
|
|
53 |
if privateDeals[line.itemId].dealFreebieOption==2:
|
|
|
54 |
line.freebieId = privateDeals[line.itemId].dealFreebieItemId
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
cart.discountedPrice = cart.totalPrice - round(total_discounts, 0)
|
|
|
58 |
return cart, discounts
|
| 11663 |
amit.gupta |
59 |
else:
|
|
|
60 |
raise PromotionException(115, 'Invalid Coupon code')
|
|
|
61 |
|