Subversion Repositories SmartDukaan

Rev

Rev 4187 | Go to most recent revision | Details | 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
 
12
raghvendra.saboo@gmail.com will get Rs.250 off on one transaction.
13
'''
14
 
15
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
16
from shop2020.clients.UserClient import UserClient
17
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count_by_user
18
from shop2020.thriftpy.model.v1.user.ttypes import Discount
19
 
20
def execute(cart, coupon_code, args):
21
 
22
    user_client = UserClient().get_client()
23
    user = user_client.getUserById(cart.userId)
24
 
25
    if not user.email.lower().strip() in ('raghvendra.saboo@gmail.com'):
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
39
        available_discount = 250
40
 
41
        for line in cart.lines:
42
            line.discountedPrice = line.actualPrice
43
            discount_value = 0
44
 
45
            if available_discount > 0:
46
                if line.actualPrice < available_discount:
47
                    line.discountedPrice = 0
48
                    discount_value = line.actualPrice
49
                    available_discount -= line.actualPrice
50
                else:
51
                    line.discountedPrice = line.actualPrice - available_discount
52
                    discount_value = available_discount
53
                    available_discount = 0
54
 
55
            total_selling_price += line.actualPrice * line.quantity
56
            total_discounted_price += line.discountedPrice * line.quantity
57
 
58
            if discount_value > 0:
59
                discount = Discount()
60
                discount.cart_id = cart.id
61
                discount.item_id = line.itemId
62
                discount.discount = discount_value
63
                discount.quantity = 1
64
 
65
                discounts.append(discount)
66
 
67
            cart.totalPrice = total_selling_price
68
            cart.discountedPrice = total_discounted_price
69
 
70
    return cart, discounts