Subversion Repositories SmartDukaan

Rev

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

Rev 6471 Rev 6538
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
6
from shop2020.clients.UserClient import UserClient
7
from shop2020.clients.UserClient import UserClient
7
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \
8
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \
8
    get_coupon_usage_count, get_coupon_usage_count_by_user
9
    get_coupon_usage_count, get_coupon_usage_count_by_user
9
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
10
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
10
import datetime
11
import datetime
11
 
12
 
12
def execute(cart, coupon_code, args):
13
def execute(cart, coupon_code, args):
13
    if 'items' not in args or 'handset_display_name' not in args:
14
    if 'handset_display_name' not in args:
14
        raise PromotionException(109, 'Error in rule arguments.')
15
        raise PromotionException(109, 'Error in rule arguments.')
15
        
16
            
16
    if 'end_on' in args and datetime.datetime.strptime(args['end_on'], '%Y-%m-%d') <= datetime.datetime.now():
17
    if 'end_on' in args and datetime.datetime.strptime(args['end_on'], '%Y-%m-%d') <= datetime.datetime.now():
17
        raise PromotionException(112, 'This promotion is expired.')
18
        raise PromotionException(112, 'This promotion is expired.')
18
         
19
         
19
    if 'usage_limit' in args and args['usage_limit'] is not None:
20
    if 'usage_limit' in args and args['usage_limit'] is not None:
20
        if args['usage_limit'] <= get_coupon_usage_count(coupon_code):
21
        if args['usage_limit'] <= get_coupon_usage_count(coupon_code):
Line 27... Line 28...
27
        if args['usage_limit_for_user'] <= get_coupon_usage_count_by_user(coupon_code, user.userId):
28
        if args['usage_limit_for_user'] <= get_coupon_usage_count_by_user(coupon_code, user.userId):
28
            raise PromotionException(111, 'You have already used this coupon maximum possible times.')
29
            raise PromotionException(111, 'You have already used this coupon maximum possible times.')
29
    
30
    
30
    if not cart.lines:
31
    if not cart.lines:
31
        return cart
32
        return cart
-
 
33
 
-
 
34
 
-
 
35
    discountJ = None
-
 
36
    if 'discount' in args:
-
 
37
        discountJ  = args ['discount']
-
 
38
 
-
 
39
    min_amount = None
-
 
40
    if 'min_amount' in args:
-
 
41
        min_amount  = args ['min_amount']
-
 
42
 
-
 
43
    max_discount = None
-
 
44
    if 'max_discount' in args:
-
 
45
        max_discount  = args ['max_discount']
-
 
46
        
-
 
47
    
32
    isPercentageDiscount = False
48
    isPercentageDiscount = False
-
 
49
    if 'is_percentage_discount' in args:
-
 
50
        isPercentageDiscount = args['is_percentage_discount']
-
 
51
    
-
 
52
 
33
    total_selling_price = 0
53
    total_selling_price = 0
34
    total_discounted_price = 0
54
    total_discounted_price = 0
35
    cart_has_specified_item = False
55
    cart_has_specified_item = False
36
    discounts = []
56
    discounts = []
37
    
57
 
38
    if 'is_percentage_discount' in args:
-
 
39
        isPercentageDiscount = args['is_percentage_discount']
-
 
40
    for line in cart.lines:
58
    for line in cart.lines:
41
        total_selling_price += line.actualPrice * line.quantity
59
        total_selling_price += line.actualPrice * line.quantity
42
        discount_value = 0
60
        discount_value = 0
43
        
61
        
44
        if line.itemId in args['items'] or (len(args['items'])==0 and args['min_amount'] <= line.actualPrice):
-
 
45
            cart_has_specified_item = True
-
 
46
            if(len(args['items'])==0):
-
 
47
                discount_value = args['discount']
-
 
48
            else: 
-
 
49
                discount_value = args['items'][line.itemId]
-
 
50
            if isPercentageDiscount:
-
 
51
                discount_value = round((line.actualPrice*discount_value)/100, 4)
-
 
52
                
-
 
53
            line.discountedPrice = round(line.actualPrice - discount_value, 4)
62
        if min_amount is not None and line.actualPrice < min_amount:
54
            total_discounted_price += round(line.discountedPrice * line.quantity, 4)
-
 
55
        else:
-
 
56
            total_discounted_price += round(line.actualPrice * line.quantity, 4)
63
            total_discounted_price += round(line.actualPrice * line.quantity, 4)
-
 
64
            continue
-
 
65
 
-
 
66
        if 'items' in args:
-
 
67
            if line.itemId in args['items'] :
-
 
68
                if type(args['items'])==dict: 
-
 
69
                    discount_value = args['items'][line.itemId]
-
 
70
                    cart_has_specified_item = True
-
 
71
                elif discountJ is not None:
-
 
72
                    discount_value = discountJ
-
 
73
                    cart_has_specified_item = True
-
 
74
            else:
-
 
75
                total_discounted_price += round(line.actualPrice * line.quantity, 4)
-
 
76
                continue
-
 
77
        elif 'categories' in args:
-
 
78
            catalog_client = CatalogClient().get_client()
-
 
79
            category = catalog_client.getItem(line.itemId).category
-
 
80
            if category in args['categories']:
-
 
81
                if type(args['categories'])==dict: 
-
 
82
                    discount_value = args['categories'][category]
-
 
83
                    cart_has_specified_item = True
-
 
84
                elif discountJ is not None:
-
 
85
                    discount_value = discountJ 
-
 
86
                    cart_has_specified_item = True
-
 
87
            else:
-
 
88
                total_discounted_price += round(line.actualPrice * line.quantity, 4)
-
 
89
                continue
-
 
90
        elif discountJ is not None:
-
 
91
            discount_value = discountJ 
-
 
92
            cart_has_specified_item = True
-
 
93
        else :
-
 
94
            total_discounted_price += round(line.actualPrice * line.quantity, 4) 
-
 
95
            continue
-
 
96
    
-
 
97
        if isPercentageDiscount:
-
 
98
            discount_value = round((line.actualPrice*discount_value)/100, 4)
-
 
99
        if max_discount is not None:
-
 
100
            discount_value = min(max_discount, discount_value)
57
        
101
            
-
 
102
        line.discountedPrice = round(line.actualPrice - discount_value, 4)
-
 
103
        total_discounted_price += round(line.discountedPrice * line.quantity, 4)
58
        if discount_value > 0:
104
        if discount_value > 0:
59
            discount = Discount()
105
            discount = Discount()
60
            discount.discount = discount_value
106
            discount.discount = discount_value
61
            discount.quantity = line.quantity
107
            discount.quantity = line.quantity
62
            discount.cart_id = cart.id
108
            discount.cart_id = cart.id