Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
3249 varun.gupt 1
'''
2
Created on 06-Sept-2011
3
 
4
@author: Varun Gupta
3384 varun.gupt 5
 
3417 varun.gupt 6
Max Uses: 1000
7
 
3491 varun.gupt 8
A user can use the coupon only 2 times.
3417 varun.gupt 9
 
3384 varun.gupt 10
Blackberry 9900 Bold 4    Rs.2000 off
11
Samsung Galaxy S II i9100    Rs.1000 off
12
Spice Blueberry Mini QT-58    20% off
13
Spice Blueberry Exp QT-61    20% off
3497 varun.gupt 14
Nokia (All models, other than C2-03, from Rs.2000 to Rs.5000)  Rs.200 off
3384 varun.gupt 15
Nokia (All models above Rs.5000)    Rs.500 off
3249 varun.gupt 16
'''
17
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
3417 varun.gupt 18
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_user
3249 varun.gupt 19
from shop2020.clients.CatalogClient import CatalogClient
20
from shop2020.thriftpy.model.v1.catalog.ttypes import Item
21
 
22
def execute(cart, coupon_code, args):
23
 
24
    #Allow only first 1000 users to use the coupon
25
    count_coupon_usage = get_coupon_usage_count(coupon_code)
26
 
27
    if count_coupon_usage >= 1000:
28
        raise PromotionException(112, 'This promotion is over.')
29
 
3417 varun.gupt 30
    #Allow a user to use the coupon only 5 times max.
31
    count_users_usage = get_coupon_usage_count_by_user(coupon_code, cart.userId)
32
 
3491 varun.gupt 33
    if count_users_usage > 1:
3417 varun.gupt 34
        raise PromotionException(111, 'This promotion is over.')
35
 
3249 varun.gupt 36
    if cart.lines:
37
        total_selling_price = 0
38
        total_discounted_price = 0
39
 
40
        has_qualified_model = False
41
 
42
        for line in cart.lines:
43
 
44
            line.discountedPrice = line.actualPrice
45
 
46
            if line.itemId in (159, 160, 161, 1557, 1558):  #Spice QT-58/61
47
 
48
                has_qualified_model = True
49
 
50
                if line.actualPrice:
51
                    line.discountedPrice = round(line.actualPrice * 0.8)
52
 
3513 chandransh 53
#            elif line.itemId == 1591:  #Blackberry 9900 Bold 4
54
#                
55
#                has_qualified_model = True
56
#                
57
#                if line.actualPrice:
58
#                    line.discountedPrice = line.actualPrice - 2000
59
#            
3256 varun.gupt 60
            elif line.itemId == 1502:  #Galaxy SII
3249 varun.gupt 61
 
62
                has_qualified_model = True
63
 
64
                if line.actualPrice:
65
                    line.discountedPrice = line.actualPrice - 1000
3454 varun.gupt 66
 
3497 varun.gupt 67
            # Excluded: Nokia C2-03 and Price above Rs.1,999
68
            elif line.itemId not in (1590, 2014) and line.actualPrice > 1999:
3454 varun.gupt 69
 
3249 varun.gupt 70
                catalog_client = CatalogClient().get_client()
71
                item = catalog_client.getItem(line.itemId)
3254 varun.gupt 72
                print 'Item: %d, Brand: *%s*' % (line.itemId, item.brand.lower())
3454 varun.gupt 73
 
3249 varun.gupt 74
                if item.brand.lower() == 'nokia':
3257 varun.gupt 75
                    has_qualified_model = True
76
 
3254 varun.gupt 77
                    print 'Brand matched'
3249 varun.gupt 78
                    discount_value = 200 if line.actualPrice < 5001 else 500
79
                    line.discountedPrice = line.actualPrice - discount_value
3254 varun.gupt 80
                else:
81
                    print 'Brand not matched'
3249 varun.gupt 82
 
83
            total_selling_price += line.actualPrice * line.quantity
3384 varun.gupt 84
            total_discounted_price += line.discountedPrice + (line.actualPrice * (line.quantity - 1))
3249 varun.gupt 85
 
86
        if has_qualified_model is False:
3401 varun.gupt 87
            raise PromotionException(115, 'This coupon is applicable only for selective handsets.')
3249 varun.gupt 88
 
89
        cart.totalPrice = total_selling_price
90
        cart.discountedPrice = total_discounted_price
91
 
3513 chandransh 92
    return cart