Subversion Repositories SmartDukaan

Rev

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

Rev 6250 Rev 6282
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
from shop2020.utils.Utils import to_py_date
11
from shop2020.utils.Utils import to_py_date
12
import datetime
12
import datetime
13
 
13
 
14
def execute(cart, coupon_code, args):
14
def execute(cart, coupon_code, args):
15
    
15
    minPrice = 5000
16
    user_client = UserClient().get_client()
16
    user_client = UserClient().get_client()
17
    user = user_client.getUserByCartId(cart.id)
17
    user = user_client.getUserByCartId(cart.id)
18
    
18
    
19
    email = user.email.lower().strip()
19
    email = user.email.lower().strip()
20
    
20
    
Line 29... Line 29...
29
        raise PromotionException(113, 'This voucher is expired.')
29
        raise PromotionException(113, 'This voucher is expired.')
30
    
30
    
31
    count_coupon_usage = get_coupon_usage_count_by_user(coupon_code, user.userId)
31
    count_coupon_usage = get_coupon_usage_count_by_user(coupon_code, user.userId)
32
    if count_coupon_usage >= args['usage_limit_for_user']:
32
    if count_coupon_usage >= args['usage_limit_for_user']:
33
        raise PromotionException(112, 'This Gift Voucher is used.')
33
        raise PromotionException(112, 'This Gift Voucher is used.')
34
 
34
    
35
    
35
    
36
    discounts = []
36
    discounts = []
37
    
37
    
38
    if cart.lines:
38
    if cart.lines:
39
        total_selling_price = 0
39
        total_selling_price = 0
40
        total_discounted_price = 0
40
        productFound = False
41
        
-
 
42
        for line in cart.lines:
41
        for line in cart.lines:
43
            line_total_price = line.actualPrice * line.quantity
42
            line_total_price = line.actualPrice * line.quantity
44
            line_total_discount = 0
-
 
45
            while line_total_discount < line_total_price and discount_value > 0:
-
 
46
                if line.actualPrice < discount_value:
-
 
47
                    discountGiven = line.actualPrice
43
            if (not productFound and line.actualPrice >= minPrice):
48
                    quantity = min(line.quantity,int(discount_value/discountGiven))
-
 
49
                else:
-
 
50
                    discountGiven = discount_value
-
 
51
                    quantity = 1
-
 
52
                    
-
 
53
                
-
 
54
                discount = Discount()
44
                discount = Discount()
55
                discount.cart_id = cart.id
45
                discount.cart_id = cart.id
56
                discount.item_id = line.itemId
46
                discount.item_id = line.itemId
57
                discount.discount = discountGiven
47
                discount.quantity = 1
58
                discount.quantity = quantity
48
                discount.discount = discount_value
59
                discounts.append(discount)
49
                discounts.append(discount)
60
 
-
 
61
                discount_value = discount_value - discountGiven*quantity 
-
 
62
                line_total_discount += discountGiven*quantity
50
                productFound = True
63
            
-
 
64
            total_selling_price += line_total_price
51
            total_selling_price += line_total_price
65
            total_discounted_price += line_total_price - line_total_discount
-
 
66
            
52
            
67
            
53
            
-
 
54
        if productFound:
68
        cart.totalPrice = total_selling_price
55
            cart.totalPrice = total_selling_price
69
        cart.discountedPrice = total_discounted_price
56
            cart.discountedPrice = total_selling_price - discount_value
-
 
57
        else:
-
 
58
            raise PromotionException(114, 'Your cart should have at least one item with selling price above Rs.' + str(minPrice)+ '.')
-
 
59
            
70
    
60
            
71
    return cart, discounts
61
    return cart, discounts
72
 
62
 
73
def getDiscountOnItem(item):
63
def getDiscountOnItem(item):
74
    return None
64
    return None
75
65