Subversion Repositories SmartDukaan

Rev

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