Subversion Repositories SmartDukaan

Rev

Rev 5331 | Rev 5500 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3249 varun.gupt 1
'''
5499 varun.gupt 2
Created on 27-June-2012
3249 varun.gupt 3
@author: Varun Gupta
3384 varun.gupt 4
 
5499 varun.gupt 5
Max Uses: 200
3417 varun.gupt 6
 
5499 varun.gupt 7
A user can use the coupon only 1 times.
3417 varun.gupt 8
 
5499 varun.gupt 9
Samsung Galaxy Ace S5830i    Rs.401 off
10
Samsung Galaxy Ace Duos S6802    Rs.401 off
11
HTC One V T320e    Rs.512 off
12
HTC Desire C A320e    Rs.762 off
3249 varun.gupt 13
'''
14
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
3417 varun.gupt 15
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_user
3249 varun.gupt 16
from shop2020.clients.CatalogClient import CatalogClient
3554 varun.gupt 17
from shop2020.thriftpy.model.v1.user.ttypes import Discount
5331 rajveer 18
from shop2020.clients.UserClient import UserClient
3249 varun.gupt 19
 
20
def execute(cart, coupon_code, args):
5331 rajveer 21
    user_client = UserClient().get_client()
22
    user = user_client.getUserByCartId(cart.id)
3249 varun.gupt 23
 
4505 varun.gupt 24
    #Allow only first 2000 users to use the coupon
3249 varun.gupt 25
    count_coupon_usage = get_coupon_usage_count(coupon_code)
26
 
5499 varun.gupt 27
    if count_coupon_usage >= 200:
3249 varun.gupt 28
        raise PromotionException(112, 'This promotion is over.')
29
 
3417 varun.gupt 30
    #Allow a user to use the coupon only 5 times max.
5331 rajveer 31
    count_users_usage = get_coupon_usage_count_by_user(coupon_code, user.id)
3417 varun.gupt 32
 
5499 varun.gupt 33
    if count_users_usage > 0:
3417 varun.gupt 34
        raise PromotionException(111, 'This promotion is over.')
35
 
3554 varun.gupt 36
    discounts = []
37
 
3249 varun.gupt 38
    if cart.lines:
39
        total_selling_price = 0
40
        total_discounted_price = 0
41
 
42
        has_qualified_model = False
43
 
44
        for line in cart.lines:
45
 
46
            line.discountedPrice = line.actualPrice
3554 varun.gupt 47
            discount_value = 0
3249 varun.gupt 48
 
5499 varun.gupt 49
            if line.itemId in (6829, 7243, 7298):  #Samsung Galaxy Ace Duos S6802 & Galaxy Ace S5830i
3249 varun.gupt 50
 
4799 varun.gupt 51
                has_qualified_model = True
52
 
53
                if line.actualPrice:
5499 varun.gupt 54
                    line.discountedPrice = round(line.actualPrice - 401)
55
                    discount_value = 401
4799 varun.gupt 56
 
5499 varun.gupt 57
            elif line.itemId == 4645:  #HTC One V T320e
4799 varun.gupt 58
 
5499 varun.gupt 59
                has_qualified_model = True
3454 varun.gupt 60
 
5499 varun.gupt 61
                if line.actualPrice:
62
                    line.discountedPrice = round(line.actualPrice - 512)
63
                    discount_value = 512
3249 varun.gupt 64
 
5499 varun.gupt 65
            elif line.itemId in (6747, 7127):  #HTC Desire C A320e
66
 
67
                has_qualified_model = True
68
 
69
                if line.actualPrice:
70
                    line.discountedPrice = round(line.actualPrice - 762)
71
                    discount_value = 762
72
 
3249 varun.gupt 73
            total_selling_price += line.actualPrice * line.quantity
5499 varun.gupt 74
            total_discounted_price += line.discountedPrice * line.quantity
3554 varun.gupt 75
 
76
            if discount_value > 0:
77
                discount = Discount()
78
                discount.cart_id = cart.id
79
                discount.item_id = line.itemId
80
                discount.discount = discount_value
5499 varun.gupt 81
                discount.quantity = line.quantity
3554 varun.gupt 82
 
83
                discounts.append(discount)
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
 
4189 varun.gupt 91
    return cart, discounts
92
 
4799 varun.gupt 93
 
4189 varun.gupt 94
def getDiscountOnItem(item):
95
    discount_expressions = [
4812 varun.gupt 96
            '150 if item.id in (159, 160, 161) else None',
4228 varun.gupt 97
            '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',
4857 varun.gupt 98
            '500 if item.brand == "Nokia" and item.sellingPrice > 5000 and item.id not in (2565, 2602, 2210, 2478) else None'
4189 varun.gupt 99
        ]
100
    for expression in discount_expressions:
101
        discount = eval(expression)
102
 
103
        if discount is not None:
104
            return discount
105
 
106
    return None