Subversion Repositories SmartDukaan

Rev

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

Rev 6903 Rev 7781
Line 7... Line 7...
7
from shop2020.clients.UserClient import UserClient
7
from shop2020.clients.UserClient import UserClient
8
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \
8
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \
9
    get_coupon_usage_count, get_coupon_usage_count_by_user
9
    get_coupon_usage_count, get_coupon_usage_count_by_user
10
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
10
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
11
import datetime
11
import datetime
-
 
12
from math import floor
12
 
13
 
13
def execute(cart, coupon_code, args):
14
def execute(cart, coupon_code, args):
14
    if 'handset_display_name' not in args:
15
    if 'handset_display_name' not in args:
15
        raise PromotionException(109, 'Error in rule arguments.')
16
        raise PromotionException(109, 'Error in rule arguments.')
16
            
17
            
Line 38... Line 39...
38
 
39
 
39
    min_amount = None
40
    min_amount = None
40
    if 'min_amount' in args:
41
    if 'min_amount' in args:
41
        min_amount  = args ['min_amount']
42
        min_amount  = args ['min_amount']
42
 
43
 
43
    max_discount = None
44
    max_discount = 10000
44
    if 'max_discount' in args:
45
    if 'max_discount' in args:
45
        max_discount  = args ['max_discount']
46
        max_discount  = args ['max_discount']
-
 
47
    
-
 
48
    discount_left = max_discount
46
        
49
        
47
    
50
    
48
    isPercentageDiscount = False
51
    isPercentageDiscount = False
49
    if 'is_percentage_discount' in args:
52
    if 'is_percentage_discount' in args:
50
        isPercentageDiscount = args['is_percentage_discount']
53
        isPercentageDiscount = args['is_percentage_discount']
Line 52... Line 55...
52
    total_discounts = 0
55
    total_discounts = 0
53
    cart_has_specified_item = False
56
    cart_has_specified_item = False
54
    discounts = []
57
    discounts = []
55
 
58
 
56
    for line in cart.lines:
59
    for line in cart.lines:
-
 
60
        if discount_left > 0 :
57
        discount_value = 0
61
            discount_value = 0
58
        
62
            
59
        if min_amount is not None and line.actualPrice < min_amount:
63
            if min_amount is not None and line.actualPrice < min_amount:
60
            continue
-
 
61
 
-
 
62
        if 'items' in args:
-
 
63
            if line.itemId in args['items'] :
-
 
64
                if type(args['items'])==dict: 
-
 
65
                    discount_value = args['items'][line.itemId]
-
 
66
                    cart_has_specified_item = True
-
 
67
                elif discountJ is not None:
-
 
68
                    discount_value = discountJ
-
 
69
                    cart_has_specified_item = True
-
 
70
            else:
-
 
71
                continue
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
                    continue
72
        elif 'categories' in args:
76
            elif 'categories' in args:
73
            catalog_client = CatalogClient().get_client()
77
                catalog_client = CatalogClient().get_client()
74
            category = catalog_client.getItem(line.itemId).category
78
                category = catalog_client.getItem(line.itemId).category
75
            if category in args['categories']:
79
                if category in args['categories']:
76
                if type(args['categories'])==dict: 
80
                    if type(args['categories'])==dict: 
77
                    discount_value = args['categories'][category]
81
                        discount_value = args['categories'][category]
78
                    cart_has_specified_item = True
82
                        cart_has_specified_item = True
79
                elif discountJ is not None:
83
                    elif discountJ is not None:
80
                    discount_value = discountJ 
84
                        discount_value = discountJ 
81
                    cart_has_specified_item = True
85
                        cart_has_specified_item = True
-
 
86
                else:
-
 
87
                    continue
-
 
88
            elif discountJ is not None:
-
 
89
                discount_value = discountJ 
-
 
90
                cart_has_specified_item = True
82
            else:
91
            else :
83
                continue
92
                continue
84
        elif discountJ is not None:
-
 
85
            discount_value = discountJ 
-
 
86
            cart_has_specified_item = True
-
 
87
        else :
93
        
88
            continue
-
 
89
    
-
 
90
        if isPercentageDiscount:
94
            if isPercentageDiscount:
91
            discount_value = round((line.actualPrice*discount_value)/100, 4)
95
                discount_value = round((line.actualPrice*discount_value)/100, 4)
92
        if max_discount is not None:
-
 
93
            discount_value = min(max_discount, discount_value)
-
 
94
            
96
            
95
        line.discountedPrice = round(line.actualPrice - discount_value, 4)
-
 
96
        total_discounts += round(discount_value * line.quantity, 4)
97
            quantity = min(floor(discount_left/discount_value), line.quantity)
97
        if discount_value > 0:
98
            if quantity > 0:
98
            discount = Discount()
99
                discount = Discount()
-
 
100
                discount.cart_id = cart.id
99
            discount.discount = discount_value
101
                discount.item_id = line.itemId
100
            discount.quantity = line.quantity
102
                discount.quantity = quantity
101
            discount.cart_id = cart.id
103
                discount.discount = discount_value
102
            discount.item_id = line.itemId
104
                discounts.append(discount)
-
 
105
                
-
 
106
                discount_left = discount_left - quantity*discount_value
-
 
107
                total_discounts += round(discount_value * quantity, 4)
103
            
108
            
-
 
109
            if discount_left > 0 and line.quantity - quantity > 0:
-
 
110
                discountOne = Discount()
-
 
111
                discountOne.cart_id = cart.id
-
 
112
                discountOne.item_id = line.itemId
-
 
113
                discountOne.quantity = 1
-
 
114
                discountOne.discount = discount_left
104
            discounts.append(discount)
115
                discounts.append(discountOne)
-
 
116
 
-
 
117
                discount_left = 0
-
 
118
                total_discounts = max_discount
-
 
119
                    
-
 
120
        else:
-
 
121
            break
105
    
122
    
106
    if cart_has_specified_item is False:
123
    if cart_has_specified_item is False:
107
        raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
124
        raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
108
    
125
    
109
    cart.discountedPrice = cart.totalPrice - total_discounts
126
    cart.discountedPrice = cart.totalPrice - total_discounts