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
 
8
A user can use the coupon only 5 times.
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
3454 varun.gupt 14
Nokia (All models, other than 1280, upto 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
 
33
    if count_users_usage > 4:
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
 
3256 varun.gupt 53
            elif line.itemId == 1591:  #Blackberry 9900 Bold 4
3249 varun.gupt 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
 
67
            elif line.itemId not in (34, 35):   # NOT Nokia 1280
68
 
3249 varun.gupt 69
                catalog_client = CatalogClient().get_client()
70
                item = catalog_client.getItem(line.itemId)
3254 varun.gupt 71
                print 'Item: %d, Brand: *%s*' % (line.itemId, item.brand.lower())
3454 varun.gupt 72
 
3249 varun.gupt 73
                if item.brand.lower() == 'nokia':
3257 varun.gupt 74
                    has_qualified_model = True
75
 
3254 varun.gupt 76
                    print 'Brand matched'
3249 varun.gupt 77
                    discount_value = 200 if line.actualPrice < 5001 else 500
78
                    line.discountedPrice = line.actualPrice - discount_value
3254 varun.gupt 79
                else:
80
                    print 'Brand not matched'
3249 varun.gupt 81
 
82
            total_selling_price += line.actualPrice * line.quantity
3384 varun.gupt 83
            total_discounted_price += line.discountedPrice + (line.actualPrice * (line.quantity - 1))
3249 varun.gupt 84
 
85
        if has_qualified_model is False:
3401 varun.gupt 86
            raise PromotionException(115, 'This coupon is applicable only for selective handsets.')
3249 varun.gupt 87
 
88
        cart.totalPrice = total_selling_price
89
        cart.discountedPrice = total_discounted_price
90
 
91
    return cart