Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2121 varun.gupt 1
'''
2
Created on 08-Jun-2011
3
 
4
@author: varungupta
5
'''
6
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
7
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_user
8
 
9
def execute(cart, coupon_code, args):
10
    #Allow a user to use the coupon only once.
11
    count_users_usage = get_coupon_usage_count_by_user(coupon_code, cart.userId)
12
 
13
    if count_users_usage > 0:
14
        raise PromotionException(111, 'You have already used this Coupon.')
15
 
16
    #Allow only first 100 users to use the coupon
17
    count_coupon_usage = get_coupon_usage_count(coupon_code)
18
    if count_coupon_usage >= 100:
19
        raise PromotionException(112, 'This promotion is over.')
20
 
21
    if cart.lines:
22
        total_selling_price = 0
23
 
24
        for line in cart.lines:
2131 varun.gupt 25
            #Disabling Coupon for Samsung Galaxy SII 
26
 
27
            if line.itemId == 1502:
28
                raise PromotionException(115, 'This coupon is not applicable for Samsung Galaxy S II')
2121 varun.gupt 29
            if line.actualPrice:
30
                total_selling_price +=  line.actualPrice * line.quantity
31
 
32
        #MAX Discount 1500
33
        if total_selling_price * 0.05 >= 1500:
34
            discount_perc = 1500 / total_selling_price
35
        elif total_selling_price >= 5000:
36
            discount_perc = 0.05
37
        else:
38
            discount_perc = 0.03
39
 
40
        cart.totalPrice = round(total_selling_price, 2)
41
        cart.discountedPrice = round((1 - discount_perc) * total_selling_price, 2)
42
 
43
        for line in cart.lines:
44
            if line.actualPrice:
45
                line.discountedPrice = round((1 - discount_perc) * line.actualPrice, 2)
46
                print "The final price, after discount is " + str(cart.discountedPrice)
47
    return cart