Subversion Repositories SmartDukaan

Rev

Rev 4405 | Rev 4941 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4186 varun.gupt 1
'''
2
Created on 06-Dec-2011
3
 
4
@author: varungupta
5
 
4405 varun.gupt 6
raghvendra.saboo@gmail.com will get Rs.200 off on one transaction
7
michaelananth.ravichandran@tcs.com will get Rs.200 off on one transaction
4475 varun.gupt 8
nc41@indiatimes.com will get Rs.200 off on one transaction
4186 varun.gupt 9
'''
10
 
11
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
12
from shop2020.clients.UserClient import UserClient
13
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count_by_user
14
from shop2020.thriftpy.model.v1.user.ttypes import Discount
15
 
16
def execute(cart, coupon_code, args):
17
 
18
    user_client = UserClient().get_client()
19
    user = user_client.getUserById(cart.userId)
20
 
4291 varun.gupt 21
    email = user.email.lower().strip()
22
 
4475 varun.gupt 23
    if email in ('raghvendra.saboo@gmail.com', 'michaelananth.ravichandran@tcs.com', 'nc41@indiatimes.com'):
4291 varun.gupt 24
        discount_value = 200
25
    else:
4186 varun.gupt 26
        raise PromotionException(111, 'You are not allowed to use this coupon')
27
 
28
    #Allow only first 1000 users to use the coupon
29
    count_coupon_usage = get_coupon_usage_count_by_user(coupon_code, cart.userId)
30
 
31
    if count_coupon_usage > 0:
32
        raise PromotionException(112, 'You have already used the coupon.')
33
 
34
    discounts = []
35
 
36
    if cart.lines:
37
        total_selling_price = 0
38
        total_discounted_price = 0
4187 varun.gupt 39
        has_used_coupon = False
4186 varun.gupt 40
 
41
        for line in cart.lines:
42
            line.discountedPrice = line.actualPrice
43
 
4187 varun.gupt 44
            if has_used_coupon:
45
                break
4186 varun.gupt 46
 
4291 varun.gupt 47
            line.discountedPrice = line.actualPrice - discount_value
4187 varun.gupt 48
 
49
            has_used_coupon = True
50
 
4186 varun.gupt 51
            total_selling_price += line.actualPrice * line.quantity
52
            total_discounted_price += line.discountedPrice * line.quantity
53
 
54
            if discount_value > 0:
55
                discount = Discount()
56
                discount.cart_id = cart.id
57
                discount.item_id = line.itemId
58
                discount.discount = discount_value
59
                discount.quantity = 1
60
 
61
                discounts.append(discount)
62
 
63
            cart.totalPrice = total_selling_price
64
            cart.discountedPrice = total_discounted_price
65
 
66
    return cart, discounts