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
 
4510 varun.gupt 6
Max Uses: 2000
3417 varun.gupt 7
 
3491 varun.gupt 8
A user can use the coupon only 2 times.
3417 varun.gupt 9
 
4195 varun.gupt 10
Nokia (All models, other than C2-03 & C2-06, from Rs.3000 to Rs.5000)  Rs.200 off
4379 varun.gupt 11
Nokia (All models above Rs.5000)    Rs.500 off
3249 varun.gupt 12
'''
13
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
3417 varun.gupt 14
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_user
3249 varun.gupt 15
from shop2020.clients.CatalogClient import CatalogClient
3554 varun.gupt 16
from shop2020.thriftpy.model.v1.user.ttypes import Discount
3249 varun.gupt 17
 
18
def execute(cart, coupon_code, args):
19
 
4505 varun.gupt 20
    #Allow only first 2000 users to use the coupon
3249 varun.gupt 21
    count_coupon_usage = get_coupon_usage_count(coupon_code)
22
 
4505 varun.gupt 23
    if count_coupon_usage >= 2000:
3249 varun.gupt 24
        raise PromotionException(112, 'This promotion is over.')
25
 
3417 varun.gupt 26
    #Allow a user to use the coupon only 5 times max.
27
    count_users_usage = get_coupon_usage_count_by_user(coupon_code, cart.userId)
28
 
3491 varun.gupt 29
    if count_users_usage > 1:
3417 varun.gupt 30
        raise PromotionException(111, 'This promotion is over.')
31
 
3554 varun.gupt 32
    discounts = []
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
3554 varun.gupt 43
            discount_value = 0
3249 varun.gupt 44
 
4562 varun.gupt 45
            if line.itemId not in (1590, 2014, 2063, 2035, 2036) and line.actualPrice > 2999 and not has_qualified_model:
3249 varun.gupt 46
 
47
                catalog_client = CatalogClient().get_client()
48
                item = catalog_client.getItem(line.itemId)
3454 varun.gupt 49
 
3249 varun.gupt 50
                if item.brand.lower() == 'nokia':
3257 varun.gupt 51
                    has_qualified_model = True
52
 
3249 varun.gupt 53
                    discount_value = 200 if line.actualPrice < 5001 else 500
54
                    line.discountedPrice = line.actualPrice - discount_value
55
 
56
            total_selling_price += line.actualPrice * line.quantity
3384 varun.gupt 57
            total_discounted_price += line.discountedPrice + (line.actualPrice * (line.quantity - 1))
3554 varun.gupt 58
 
59
            if discount_value > 0:
60
                discount = Discount()
61
                discount.cart_id = cart.id
62
                discount.item_id = line.itemId
63
                discount.discount = discount_value
64
                discount.quantity = 1
65
 
66
                discounts.append(discount)
3249 varun.gupt 67
 
68
        if has_qualified_model is False:
3401 varun.gupt 69
            raise PromotionException(115, 'This coupon is applicable only for selective handsets.')
3249 varun.gupt 70
 
71
        cart.totalPrice = total_selling_price
72
        cart.discountedPrice = total_discounted_price
73
 
4189 varun.gupt 74
    return cart, discounts
75
 
76
def getDiscountOnItem(item):
77
    discount_expressions = [
4228 varun.gupt 78
            '200 if item.brand == "Nokia" and item.sellingPrice > 2999 and item.sellingPrice < 5001 and item.id not in (1590, 2014, 2063, 2035, 2036) else None',
4379 varun.gupt 79
            '500 if item.brand == "Nokia" and item.sellingPrice > 5000 else None'
4189 varun.gupt 80
        ]
81
    for expression in discount_expressions:
82
        discount = eval(expression)
83
 
84
        if discount is not None:
85
            return discount
86
 
87
    return None