Subversion Repositories SmartDukaan

Rev

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

Rev 1976 Rev 2003
Line 45... Line 45...
45
        user_client = UserClient().get_client()
45
        user_client = UserClient().get_client()
46
        cart = user_client.getCart(cart_id)
46
        cart = user_client.getCart(cart_id)
47
 
47
 
48
        #TODO: Use rule_name to updates the prices in the cart.
48
        #TODO: Use rule_name to updates the prices in the cart.
49
        rule_name = coupon.promotion.rule_execution_src
49
        rule_name = coupon.promotion.rule_execution_src
50
        updated_cart = rule_for_5perc_discount_on_5k_purchase(cart)
50
        updated_cart = rule_for_5perc_discount_on_5k_purchase(cart, coupon_code, {})
51
        user_client.applyCouponToCart(cart_id, coupon_code, updated_cart.totalPrice, updated_cart.discountedPrice)
51
        user_client.applyCouponToCart(cart_id, coupon_code, updated_cart.totalPrice, updated_cart.discountedPrice)
52
        return updated_cart
52
        return updated_cart
53
    else:
53
    else:
54
        print 'Invalid Coupon Code'
54
        print 'Invalid Coupon Code'
55
        raise PromotionException(101, 'Invalid Coupon Code')
55
        raise PromotionException(101, 'Invalid Coupon Code')
56
 
56
 
57
def rule_for_5perc_discount_on_5k_purchase(cart):
57
def rule_for_5perc_discount_on_5k_purchase(cart, coupon_code, args):
58
    #TODO: Allow only first 100 users to use the coupon
-
 
59
    
58
    
60
    #TODO: Allow a user to use the coupon only once.
59
    #Allow a user to use the coupon only once.
-
 
60
    count_users_usage = get_coupon_usage_count_by_user(coupon_code, cart.userId)
-
 
61
    if count_users_usage > 0:
-
 
62
        return cart
-
 
63
    
-
 
64
    #Allow only first 100 users to use the coupon
-
 
65
    count_coupon_usage = get_coupon_usage_count(coupon_code)
-
 
66
    if count_coupon_usage >= 100:
-
 
67
        return cart
61
    
68
    
62
    if cart.lines:
69
    if cart.lines:
63
        total_selling_price = 0
70
        total_selling_price = 0
64
        
71
        
65
        for line in cart.lines:
72
        for line in cart.lines:
66
            if line.actualPrice:
73
            if line.actualPrice:
67
                total_selling_price +=  line.actualPrice * line.quantity
74
                total_selling_price +=  line.actualPrice * line.quantity
68
        
75
        
69
        cart.totalPrice = total_selling_price
76
        #MAX Discount 1500
70
        
-
 
71
        #if cart.totalPrice == 0.0:
77
        if total_selling_price * 0.05 >= 1500:
72
        #    cart.discountedPrice = 0.0
78
            discount_perc = 1500 / total_selling_price
73
        
-
 
74
        if cart.totalPrice >= 5000:
79
        elif total_selling_price >= 5000:
75
            discount_perc = 0.05
80
            discount_perc = 0.05
76
        else:
81
        else:
77
            discount_perc = 0.03
82
            discount_perc = 0.03
78
        
83
        
-
 
84
        cart.totalPrice = total_selling_price
79
        cart.discountedPrice = (1 - discount_perc) * total_selling_price
85
        cart.discountedPrice = (1 - discount_perc) * total_selling_price
80
 
86
 
81
        for line in cart.lines:
87
        for line in cart.lines:
82
            if line.actualPrice:
88
            if line.actualPrice:
83
                line.discountedPrice = (1 - discount_perc) * line.actualPrice
89
                line.discountedPrice = (1 - discount_perc) * line.actualPrice
Line 93... Line 99...
93
    session.commit()
99
    session.commit()
94
 
100
 
95
def get_coupon_usage_count_by_user(coupon_code, user_id):
101
def get_coupon_usage_count_by_user(coupon_code, user_id):
96
    return PromotionTracker.query.filter_by(coupon_code = coupon_code).filter_by(user_id = user_id).count()
102
    return PromotionTracker.query.filter_by(coupon_code = coupon_code).filter_by(user_id = user_id).count()
97
 
103
 
-
 
104
def get_coupon_usage_count(coupon_code):
-
 
105
    return PromotionTracker.query.filter_by(coupon_code = coupon_code).count()
-
 
106
 
98
def close_session():
107
def close_session():
99
    if session.is_active:
108
    if session.is_active:
100
        session.close()
109
        session.close()
101
110