Subversion Repositories SmartDukaan

Rev

Rev 11841 | Rev 13169 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 11841 Rev 13155
Line 1... Line 1...
1
'''
1
'''
2
Created on 06-Jul-2011
2
Created on 06-Jul-2011
3
 
3
 
4
@author: Varun Gupta
4
@author: Varun Gupta
5
'''
5
'''
-
 
6
from math import floor
6
from shop2020.clients.CatalogClient import CatalogClient
7
from shop2020.clients.CatalogClient import CatalogClient
7
from shop2020.clients.UserClient import UserClient
8
from shop2020.model.v1.user.impl import UserDataAccessors
8
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \
9
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \
9
    get_coupon_usage_count, get_coupon_usage_count_by_user
10
    get_coupon_usage_count, get_coupon_usage_count_by_user
10
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
11
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
11
import datetime
12
import datetime
12
from math import floor
-
 
13
 
13
 
14
def execute(cart, coupon_code, args):
14
def execute(cart, coupon_code, args):
15
    
15
    
16
    appliedOnce = False
-
 
17
    
16
    
18
    if 'handset_display_name' not in args:
17
    if 'handset_display_name' not in args:
19
        raise PromotionException(109, 'Error in rule arguments.')
18
        raise PromotionException(109, 'Error in rule arguments.')
20
            
19
            
21
    if 'end_on' in args and datetime.datetime.strptime(args['end_on'], '%Y-%m-%d') <= datetime.datetime.now():
20
    if 'end_on' in args and datetime.datetime.strptime(args['end_on'], '%Y-%m-%d') <= datetime.datetime.now():
Line 23... Line 22...
23
         
22
         
24
    if 'usage_limit' in args and args['usage_limit'] is not None:
23
    if 'usage_limit' in args and args['usage_limit'] is not None:
25
        if args['usage_limit'] <= get_coupon_usage_count(coupon_code):
24
        if args['usage_limit'] <= get_coupon_usage_count(coupon_code):
26
            raise PromotionException(112, 'This promotion is over.')
25
            raise PromotionException(112, 'This promotion is over.')
27
    
26
    
28
    user_client = UserClient().get_client()
-
 
29
    user = user_client.getUserByCartId(cart.id)
27
    user = UserDataAccessors.get_user_by_cart_id(cart.id)
-
 
28
        
-
 
29
 
30
 
30
 
31
    if 'usage_limit_for_user' in args and args['usage_limit_for_user'] is not None:
31
    if 'usage_limit_for_user' in args and args['usage_limit_for_user'] is not None:
32
        if args['usage_limit_for_user'] <= get_coupon_usage_count_by_user(coupon_code, user.userId):
32
        if args['usage_limit_for_user'] <= get_coupon_usage_count_by_user(coupon_code, user.id):
33
            raise PromotionException(111, 'You have already used this coupon maximum possible times.')
33
            raise PromotionException(111, 'You have already used this coupon maximum possible times.')
34
    
34
    
35
    if not cart.lines:
35
    if not cart.lines:
36
        return cart
36
        return cart
-
 
37
    
-
 
38
    appliedOnce = args['appliedOnce'] if 'dealer_flag' in args else True
37
 
39
 
-
 
40
    dealItems = []
-
 
41
    if not (args['dealer_flag'] if 'dealer_flag' in args else False):
-
 
42
        privateDealUser = UserDataAccessors.get_private_deal_user(user.id)
-
 
43
        if privateDealUser is not None and privateDealUser.isActive:
-
 
44
            catalog_client = CatalogClient().get_client() 
-
 
45
            items = [line.itemId for line in cart.lines]
-
 
46
            dealItems = catalog_client.getAllActivePrivateDeals(items, 0)
-
 
47
            
-
 
48
        
38
 
49
 
39
    discountJ = None
50
    discountJ = None
40
    if 'discount' in args:
51
    if 'discount' in args:
41
        discountJ  = args ['discount']
52
        discountJ  = args ['discount']
42
 
53
 
Line 55... Line 66...
55
        isPercentageDiscount = args['is_percentage_discount']
66
        isPercentageDiscount = args['is_percentage_discount']
56
 
67
 
57
    total_discounts = 0
68
    total_discounts = 0
58
    cart_has_specified_item = False
69
    cart_has_specified_item = False
59
    discounts = []
70
    discounts = []
60
 
71
    
61
    for line in cart.lines:
72
    for line in cart.lines:
-
 
73
        if line.itemId in dealItems:
-
 
74
            print "continued  line.itemId",  line.itemId 
-
 
75
            continue
62
        if discount_left > 0 :
76
        if discount_left > 0 :
63
            discount_value = 0
77
            discount_value = 0
64
            
78
            
65
            if min_amount is not None and line.actualPrice < min_amount:
79
            if min_amount is not None and line.actualPrice < min_amount:
66
                continue
80
                continue
Line 86... Line 100...
86
                        discount_value = discountJ 
100
                        discount_value = discountJ 
87
                        cart_has_specified_item = True
101
                        cart_has_specified_item = True
88
                else:
102
                else:
89
                    continue
103
                    continue
90
 
104
 
-
 
105
            elif 'categoriesexcept' in args:
-
 
106
                catalog_client = CatalogClient().get_client()
-
 
107
                category = catalog_client.getItem(line.itemId).category
-
 
108
                if category in args['categoriesexcept']:
-
 
109
                    if type(args['categoriesexcept'])==dict:
-
 
110
                        discount_value = discountJ 
-
 
111
                        cart_has_specified_item = True
-
 
112
                else:
-
 
113
                    continue
-
 
114
 
91
            elif 'brands' in args:
115
            elif 'brands' in args:
92
                catalog_client = CatalogClient().get_client()
116
                catalog_client = CatalogClient().get_client()
93
                brand = catalog_client.getItem(line.itemId).brand
117
                brand = catalog_client.getItem(line.itemId).brand
94
                if brand in args['brands']:
118
                if brand in args['brands']:
95
                    if type(args['brands'])==dict: 
119
                    if type(args['brands'])==dict: 
Line 108... Line 132...
108
                continue
132
                continue
109
        
133
        
110
            if isPercentageDiscount:
134
            if isPercentageDiscount:
111
                discount_value = round((line.actualPrice*discount_value)/100, 4)
135
                discount_value = round((line.actualPrice*discount_value)/100, 4)
112
                    
136
                    
113
            if 'appliedOnce' in args and args['appliedOnce']:
137
            if appliedOnce:
114
                quantity = 1
138
                quantity = 1
115
                appliedOnce = True
-
 
116
            else:
139
            else:
117
                quantity = min(floor(discount_left/discount_value), line.quantity)
140
                quantity = min(floor(discount_left/discount_value), line.quantity)
118
            
141
            
119
            if quantity > 0:
142
            if quantity > 0:
120
                discount = Discount()
143
                discount = Discount()
Line 147... Line 170...
147
        
170
        
148
    if cart_has_specified_item is False:
171
    if cart_has_specified_item is False:
149
        raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
172
        raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
150
    
173
    
151
    cart.discountedPrice = cart.totalPrice - round(total_discounts, 0)
174
    cart.discountedPrice = cart.totalPrice - round(total_discounts, 0)
-
 
175
    print "cart.discountedPrice----",  cart.discountedPrice
152
    return cart, discounts
176
    return cart, discounts
153
 
177
 
154
def getDiscountOnItem(item):
178
def getDiscountOnItem(item):
155
    return None
179
    return None
156
180