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
Spice Blueberry Mini QT-58    20% off
11
Spice Blueberry Exp QT-61    20% off
3497 varun.gupt 12
Nokia (All models, other than C2-03, from Rs.2000 to Rs.5000)  Rs.200 off
3384 varun.gupt 13
Nokia (All models above Rs.5000)    Rs.500 off
3249 varun.gupt 14
'''
15
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
3417 varun.gupt 16
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_user
3249 varun.gupt 17
from shop2020.clients.CatalogClient import CatalogClient
18
from shop2020.thriftpy.model.v1.catalog.ttypes import Item
19
 
20
def execute(cart, coupon_code, args):
21
 
22
    #Allow only first 1000 users to use the coupon
23
    count_coupon_usage = get_coupon_usage_count(coupon_code)
24
 
25
    if count_coupon_usage >= 1000:
26
        raise PromotionException(112, 'This promotion is over.')
27
 
3417 varun.gupt 28
    #Allow a user to use the coupon only 5 times max.
29
    count_users_usage = get_coupon_usage_count_by_user(coupon_code, cart.userId)
30
 
3491 varun.gupt 31
    if count_users_usage > 1:
3417 varun.gupt 32
        raise PromotionException(111, 'This promotion is over.')
33
 
3249 varun.gupt 34
    if cart.lines:
35
        total_selling_price = 0
36
        total_discounted_price = 0
37
 
38
        has_qualified_model = False
39
 
40
        for line in cart.lines:
41
 
42
            line.discountedPrice = line.actualPrice
43
 
44
            if line.itemId in (159, 160, 161, 1557, 1558):  #Spice QT-58/61
45
 
46
                has_qualified_model = True
47
 
48
                if line.actualPrice:
49
                    line.discountedPrice = round(line.actualPrice * 0.8)
50
 
3497 varun.gupt 51
            # Excluded: Nokia C2-03 and Price above Rs.1,999
52
            elif line.itemId not in (1590, 2014) and line.actualPrice > 1999:
3454 varun.gupt 53
 
3249 varun.gupt 54
                catalog_client = CatalogClient().get_client()
55
                item = catalog_client.getItem(line.itemId)
3254 varun.gupt 56
                print 'Item: %d, Brand: *%s*' % (line.itemId, item.brand.lower())
3454 varun.gupt 57
 
3249 varun.gupt 58
                if item.brand.lower() == 'nokia':
3257 varun.gupt 59
                    has_qualified_model = True
60
 
3254 varun.gupt 61
                    print 'Brand matched'
3249 varun.gupt 62
                    discount_value = 200 if line.actualPrice < 5001 else 500
63
                    line.discountedPrice = line.actualPrice - discount_value
3254 varun.gupt 64
                else:
65
                    print 'Brand not matched'
3249 varun.gupt 66
 
67
            total_selling_price += line.actualPrice * line.quantity
3384 varun.gupt 68
            total_discounted_price += line.discountedPrice + (line.actualPrice * (line.quantity - 1))
3249 varun.gupt 69
 
70
        if has_qualified_model is False:
3401 varun.gupt 71
            raise PromotionException(115, 'This coupon is applicable only for selective handsets.')
3249 varun.gupt 72
 
73
        cart.totalPrice = total_selling_price
74
        cart.discountedPrice = total_discounted_price
75
 
3513 chandransh 76
    return cart