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
 
2331 varun.gupt 16
    #Allow only first 50 users to use the coupon
2121 varun.gupt 17
    count_coupon_usage = get_coupon_usage_count(coupon_code)
2165 varun.gupt 18
    if count_coupon_usage >= 50:
2121 varun.gupt 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')
2331 varun.gupt 29
            if line.itemId == 1491 or line.itemId == 1521:
2151 rajveer 30
                raise PromotionException(115, 'This coupon is not applicable for HTC Desire A8181')
2165 varun.gupt 31
            if line.itemId == 13:
32
                raise PromotionException(115, 'This coupon is not applicable for HTC Desire Z A7272')
2121 varun.gupt 33
            if line.actualPrice:
34
                total_selling_price +=  line.actualPrice * line.quantity
35
 
36
        #MAX Discount 1500
37
        if total_selling_price * 0.05 >= 1500:
38
            discount_perc = 1500 / total_selling_price
39
        elif total_selling_price >= 5000:
40
            discount_perc = 0.05
41
        else:
42
            discount_perc = 0.03
43
 
44
        cart.totalPrice = round(total_selling_price, 2)
45
        cart.discountedPrice = round((1 - discount_perc) * total_selling_price, 2)
46
 
47
        for line in cart.lines:
48
            if line.actualPrice:
49
                line.discountedPrice = round((1 - discount_perc) * line.actualPrice, 2)
50
                print "The final price, after discount is " + str(cart.discountedPrice)
2151 rajveer 51
    return cart