Subversion Repositories SmartDukaan

Rev

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

Rev 5970 Rev 5972
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 shop2020.clients.CatalogClient import CatalogClient
-
 
7
from shop2020.clients.UserClient import UserClient
-
 
8
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \
-
 
9
    get_coupon_usage_count, get_coupon_usage_count_by_user
-
 
10
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
6
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
-
 
7
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_user
11
from shop2020.model.v1.user.impl import PromotionDataAccessors
8
from shop2020.clients.UserClient import UserClient
12
 
9
 
13
def execute(cart, coupon):
10
def execute(cart, coupon_code, args):
-
 
11
    if 'items' not in args or 'discount' not in args or 'handset_display_name' not in args:
-
 
12
        raise PromotionException(109, 'Error in rule arguments.')
14
        
13
        
15
    if coupon.usageLimit is not None:
14
    if 'usage_limit' in args and args['usage_limit'] is not None:
16
        if coupon.usageLimit <= get_coupon_usage_count(coupon.couponCode):
15
        if args['usage_limit'] <= get_coupon_usage_count(coupon_code):
17
            raise PromotionException(112, 'This promotion is over.')
16
            raise PromotionException(112, 'This promotion is over.')
18
    
17
    
19
    user_client = UserClient().get_client()
18
    user_client = UserClient().get_client()
20
    user = user_client.getUserByCartId(cart.id)
19
    user = user_client.getUserByCartId(cart.id)
21
 
20
 
22
    if coupon.usageLimitForUser is not None:
21
    if 'usage_limit_for_user' in args and args['usage_limit_for_user'] is not None:
23
        if coupon.usageLimitForUser <= get_coupon_usage_count_by_user(coupon.couponCode, user.userId):
22
        if args['usage_limit_for_user'] <= get_coupon_usage_count_by_user(coupon_code, user.userId):
24
            raise PromotionException(111, 'You have already used this coupon maximum possible times.')
23
            raise PromotionException(111, 'You have already used this coupon maximum possible times.')
25
    
24
    
26
    if not cart.lines:
25
    if not cart.lines:
27
        return cart
26
        return cart
28
    
27
    
29
    total_selling_price = 0
28
    total_selling_price = 0
30
    total_discounted_price = 0
29
    total_discounted_price = 0
31
    cart_has_specified_item = False
30
    cart_has_specified_item = False
32
    discounts = []
31
    discounts = []
33
    
32
    
34
    
-
 
35
    couponMappings = PromotionDataAccessors.coupon_item_mapping_cache.get(coupon.couponCode)
-
 
36
 
-
 
37
    for line in cart.lines:
33
    for line in cart.lines:
38
        total_selling_price += line.actualPrice * line.quantity
34
        total_selling_price += line.actualPrice * line.quantity
39
        discount_value = 0
35
        discount_value = 0
40
        
36
        
41
        if couponMappings.has_key(line.itemId) :
37
        if line.itemId in args['items']:
42
            cart_has_specified_item = True
38
            cart_has_specified_item = True
43
            line.discountedPrice = round(line.actualPrice - couponMappings[line.itemId], 4)
39
            line.discountedPrice = round(line.actualPrice - args['discount'], 4)
44
            total_discounted_price += round(line.discountedPrice * line.quantity, 4)
40
            total_discounted_price += round(line.discountedPrice * line.quantity, 4)
45
            discount_value = couponMappings[line.itemId]
41
            discount_value = args['discount']
46
        else:
42
        else:
47
            total_discounted_price += round(line.actualPrice * line.quantity, 4)
43
            total_discounted_price += round(line.actualPrice * line.quantity, 4)
48
        
44
        
49
        if discount_value > 0:
45
        if discount_value > 0:
50
            discount = Discount()
46
            discount = Discount()
Line 54... Line 50...
54
            discount.item_id = line.itemId
50
            discount.item_id = line.itemId
55
            
51
            
56
            discounts.append(discount)
52
            discounts.append(discount)
57
    
53
    
58
    if cart_has_specified_item is False:
54
    if cart_has_specified_item is False:
59
        raise PromotionException(115, 'This coupon is not applicable current items in cart')
55
        raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
60
    
56
    
61
    cart.totalPrice = round(total_selling_price, 4)
57
    cart.totalPrice = round(total_selling_price, 4)
62
    cart.discountedPrice = total_discounted_price
58
    cart.discountedPrice = total_discounted_price
63
    return cart, discounts
59
    return cart, discounts
64
 
60