Subversion Repositories SmartDukaan

Rev

Rev 4291 | Rev 4405 | 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
'''
6
 
7
'''
8
Created on 24-Oct-2011
9
 
10
@author: Varun Gupta
11
 
4291 varun.gupt 12
raghvendra.saboo@gmail.com will get Rs.200 off on one transaction.
13
nishantgandhi@yahoo.com will get Rs.500 off on one transaction.
4186 varun.gupt 14
'''
15
 
16
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
17
from shop2020.clients.UserClient import UserClient
18
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count_by_user
19
from shop2020.thriftpy.model.v1.user.ttypes import Discount
20
 
21
def execute(cart, coupon_code, args):
22
 
23
    user_client = UserClient().get_client()
24
    user = user_client.getUserById(cart.userId)
25
 
4291 varun.gupt 26
    email = user.email.lower().strip()
27
 
28
    if email == 'raghvendra.saboo@gmail.com':
29
        discount_value = 200
30
    elif email == 'nishantgandhi@yahoo.com':
31
        discount_value = 500
4327 varun.gupt 32
    elif email == 'akshay.karandikar@rocketmail.com':
33
        discount_value = 250
34
    elif email == 'mis@birla.ac.in':
35
        discount_value = 1889
4291 varun.gupt 36
    else:
4186 varun.gupt 37
        raise PromotionException(111, 'You are not allowed to use this coupon')
38
 
39
    #Allow only first 1000 users to use the coupon
40
    count_coupon_usage = get_coupon_usage_count_by_user(coupon_code, cart.userId)
41
 
42
    if count_coupon_usage > 0:
43
        raise PromotionException(112, 'You have already used the coupon.')
44
 
45
    discounts = []
46
 
47
    if cart.lines:
48
        total_selling_price = 0
49
        total_discounted_price = 0
4187 varun.gupt 50
        has_used_coupon = False
4186 varun.gupt 51
 
52
        for line in cart.lines:
53
            line.discountedPrice = line.actualPrice
54
 
4187 varun.gupt 55
            if has_used_coupon:
56
                break
4186 varun.gupt 57
 
4291 varun.gupt 58
            line.discountedPrice = line.actualPrice - discount_value
4187 varun.gupt 59
 
60
            has_used_coupon = True
61
 
4186 varun.gupt 62
            total_selling_price += line.actualPrice * line.quantity
63
            total_discounted_price += line.discountedPrice * line.quantity
64
 
65
            if discount_value > 0:
66
                discount = Discount()
67
                discount.cart_id = cart.id
68
                discount.item_id = line.itemId
69
                discount.discount = discount_value
70
                discount.quantity = 1
71
 
72
                discounts.append(discount)
73
 
74
            cart.totalPrice = total_selling_price
75
            cart.discountedPrice = total_discounted_price
76
 
77
    return cart, discounts