Subversion Repositories SmartDukaan

Rev

Rev 4187 | Rev 4327 | 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
32
    else:
4186 varun.gupt 33
        raise PromotionException(111, 'You are not allowed to use this coupon')
34
 
35
    #Allow only first 1000 users to use the coupon
36
    count_coupon_usage = get_coupon_usage_count_by_user(coupon_code, cart.userId)
37
 
38
    if count_coupon_usage > 0:
39
        raise PromotionException(112, 'You have already used the coupon.')
40
 
41
    discounts = []
42
 
43
    if cart.lines:
44
        total_selling_price = 0
45
        total_discounted_price = 0
4187 varun.gupt 46
        has_used_coupon = False
4186 varun.gupt 47
 
48
        for line in cart.lines:
49
            line.discountedPrice = line.actualPrice
50
 
4187 varun.gupt 51
            if has_used_coupon:
52
                break
4186 varun.gupt 53
 
4291 varun.gupt 54
            line.discountedPrice = line.actualPrice - discount_value
4187 varun.gupt 55
 
56
            has_used_coupon = True
57
 
4186 varun.gupt 58
            total_selling_price += line.actualPrice * line.quantity
59
            total_discounted_price += line.discountedPrice * line.quantity
60
 
61
            if discount_value > 0:
62
                discount = Discount()
63
                discount.cart_id = cart.id
64
                discount.item_id = line.itemId
65
                discount.discount = discount_value
66
                discount.quantity = 1
67
 
68
                discounts.append(discount)
69
 
70
            cart.totalPrice = total_selling_price
71
            cart.discountedPrice = total_discounted_price
72
 
73
    return cart, discounts