Subversion Repositories SmartDukaan

Rev

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

Rev 9141 Rev 9168
Line 10... Line 10...
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
from math import floor
13
 
13
 
14
def execute(cart, coupon_code, args):
14
def execute(cart, coupon_code, args):
-
 
15
    
-
 
16
    appliedOnce = False
-
 
17
    
15
    if 'handset_display_name' not in args:
18
    if 'handset_display_name' not in args:
16
        raise PromotionException(109, 'Error in rule arguments.')
19
        raise PromotionException(109, 'Error in rule arguments.')
17
            
20
            
18
    if 'end_on' in args and datetime.datetime.strptime(args['end_on'], '%Y-%m-%d') <= datetime.datetime.now():
21
    if 'end_on' in args and datetime.datetime.strptime(args['end_on'], '%Y-%m-%d') <= datetime.datetime.now():
19
        raise PromotionException(112, 'This promotion is expired.')
22
        raise PromotionException(112, 'This promotion is expired.')
Line 104... Line 107...
104
            else :
107
            else :
105
                continue
108
                continue
106
        
109
        
107
            if isPercentageDiscount:
110
            if isPercentageDiscount:
108
                discount_value = round((line.actualPrice*discount_value)/100, 4)
111
                discount_value = round((line.actualPrice*discount_value)/100, 4)
-
 
112
                    
-
 
113
            if 'appliedOnce' in args and args['appliedOnce']:
-
 
114
                quantity = 1
-
 
115
                appliedOnce = True
-
 
116
            else:
-
 
117
                quantity = min(floor(discount_left/discount_value), line.quantity)
109
            
118
            
110
            quantity = min(floor(discount_left/discount_value), line.quantity)
-
 
111
            if quantity > 0:
119
            if quantity > 0:
112
                discount = Discount()
120
                discount = Discount()
113
                discount.cart_id = cart.id
121
                discount.cart_id = cart.id
114
                discount.item_id = line.itemId
122
                discount.item_id = line.itemId
115
                discount.quantity = quantity
123
                discount.quantity = quantity
Line 117... Line 125...
117
                discounts.append(discount)
125
                discounts.append(discount)
118
                
126
                
119
                discount_left = discount_left - quantity*discount_value
127
                discount_left = discount_left - quantity*discount_value
120
                total_discounts += round(discount_value * quantity, 4)
128
                total_discounts += round(discount_value * quantity, 4)
121
            
129
            
-
 
130
            if appliedOnce :
-
 
131
                break
-
 
132
            
122
            if discount_left > 0 and line.quantity - quantity > 0:
133
            if discount_left > 0 and line.quantity - quantity > 0:
123
                discountOne = Discount()
134
                discountOne = Discount()
124
                discountOne.cart_id = cart.id
135
                discountOne.cart_id = cart.id
125
                discountOne.item_id = line.itemId
136
                discountOne.item_id = line.itemId
126
                discountOne.quantity = 1
137
                discountOne.quantity = 1
127
                discountOne.discount = discount_left
138
                discountOne.discount = discount_left
128
                discounts.append(discountOne)
139
                discounts.append(discountOne)
129
 
140
 
130
                discount_left = 0
141
                discount_left = 0
131
                total_discounts = max_discount
142
                total_discounts = max_discount
132
                    
143
        
133
        else:
144
        else:
134
            break
145
            break
-
 
146
            
135
    
147
        
136
    if cart_has_specified_item is False:
148
    if cart_has_specified_item is False:
137
        raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
149
        raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
138
    
150
    
139
    cart.discountedPrice = cart.totalPrice - round(total_discounts, 0)
151
    cart.discountedPrice = cart.totalPrice - round(total_discounts, 0)
140
    return cart, discounts
152
    return cart, discounts