Subversion Repositories SmartDukaan

Rev

Rev 6018 | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on 06-Jul-2011

@author: Varun Gupta
'''
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_user
from shop2020.clients.UserClient import UserClient

def execute(cart, coupon_code, args):
    itemTuple = (2091,2527,3882,6829,7243,5834,7227,2629,5839,3358,4643,7298,2215,2481,4630,5731,1493,1502,2073,2621,5840,5841,5842,4961,7509,7510,7511,7335,7508,5,6,7,8,9,2094,2274,2280,6823,7517,7565,7332,2026,2074,2020,2021,1584,2058,1595,3361,4627,2039,7295,4645,7126,7503,4646,4859,2082,2083,3880,7297,7548,4647)
    if 'discount' not in args or 'handset_display_name' not in args:
        raise PromotionException(109, 'Error in rule arguments.')
        
    if 'usage_limit' in args and args['usage_limit'] is not None:
        if args['usage_limit'] <= get_coupon_usage_count(coupon_code):
            raise PromotionException(112, 'This promotion is over.')
    
    user_client = UserClient().get_client()
    user = user_client.getUserByCartId(cart.id)

    if 'usage_limit_for_user' in args and args['usage_limit_for_user'] is not None:
        if args['usage_limit_for_user'] <= get_coupon_usage_count_by_user(coupon_code, user.userId):
            raise PromotionException(111, 'You have already used this coupon maximum possible times.')
    
    if not cart.lines:
        return cart
    
    total_selling_price = 0
    total_discounted_price = 0
    cart_has_specified_item = False
    discounts = []
    for line in cart.lines:
        total_selling_price += line.actualPrice * line.quantity
        discount_value = 0
        
        if line.itemId in itemTuple:
            discount = args['discount']
            cart_has_specified_item = True
            line.discountedPrice = round(line.actualPrice - discount, 4)
            total_discounted_price += round(line.discountedPrice * line.quantity, 4)
            discount_value = discount
        else:
            total_discounted_price += round(line.actualPrice * line.quantity, 4)
        
        if discount_value > 0:
            discount = Discount()
            discount.discount = discount_value
            discount.quantity = line.quantity
            discount.cart_id = cart.id
            discount.item_id = line.itemId
            
            discounts.append(discount)
    
    if cart_has_specified_item is False:
        raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])
    
    cart.totalPrice = round(total_selling_price, 4)
    cart.discountedPrice = total_discounted_price
    return cart, discounts

def getDiscountOnItem(item):
    return None