Subversion Repositories SmartDukaan

Rev

Rev 2131 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on 08-Jun-2011

@author: varungupta
'''
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_user

def execute(cart, coupon_code, args):
    #Allow a user to use the coupon only once.
    count_users_usage = get_coupon_usage_count_by_user(coupon_code, cart.userId)
    
    if count_users_usage > 0:
        raise PromotionException(111, 'You have already used this Coupon.')
    
    #Allow only first 100 users to use the coupon
    count_coupon_usage = get_coupon_usage_count(coupon_code)
    if count_coupon_usage >= 100:
        raise PromotionException(112, 'This promotion is over.')
    
    if cart.lines:
        total_selling_price = 0
        
        for line in cart.lines:
            if line.actualPrice:
                total_selling_price +=  line.actualPrice * line.quantity
        
        #MAX Discount 1500
        if total_selling_price * 0.05 >= 1500:
            discount_perc = 1500 / total_selling_price
        elif total_selling_price >= 5000:
            discount_perc = 0.05
        else:
            discount_perc = 0.03
        
        cart.totalPrice = round(total_selling_price, 2)
        cart.discountedPrice = round((1 - discount_perc) * total_selling_price, 2)

        for line in cart.lines:
            if line.actualPrice:
                line.discountedPrice = round((1 - discount_perc) * line.actualPrice, 2)
                print "The final price, after discount is " + str(cart.discountedPrice)
    return cart