Subversion Repositories SmartDukaan

Rev

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

'''
Created on 06-Dec-2011

@author: varungupta
'''

from shop2020.clients.UserClient import UserClient
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \
    get_coupon_usage_count_by_user
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
from shop2020.utils.Utils import to_py_date
import datetime

def execute(cart, coupon_code, args):
    minPrice = 5000
    user_client = UserClient().get_client()
    user = user_client.getUserByCartId(cart.id)
    
    email = user.email.lower().strip()
    
    if args['couponType'] is not None and args['couponType'] != 'physical' and args['couponType'] != 'both':
        raise PromotionException(111, 'This coupon is not valid')
    
    if args['emails'] == '*' or email in args['emails']:
        discount_value = args['discount']
    else:
        raise PromotionException(111, 'You are not entitled to use this Gift Voucher')

    todate = datetime.datetime.now()      
    
    if 'endOn' in args and todate > to_py_date(args['endOn']):
        raise PromotionException(113, 'This voucher is expired.')
    
    count_coupon_usage = get_coupon_usage_count_by_user(coupon_code, user.userId)
    if count_coupon_usage >= args['usage_limit_for_user']:
        raise PromotionException(112, 'This Gift Voucher is used.')
    
    
    discounts = []
    
    if cart.lines:
        productFound = False
        for line in cart.lines:
            if (not productFound and line.actualPrice >= minPrice):
                discount = Discount()
                discount.cart_id = cart.id
                discount.item_id = line.itemId
                discount.quantity = 1
                discount.discount = discount_value
                discounts.append(discount)
                productFound = True
            
            
        if productFound:
            cart.discountedPrice = cart.totalPrice - discount_value
        else:
            raise PromotionException(114, 'Your cart should have at least one item with selling price above Rs.' + str(minPrice)+ '.')
            
            
    return cart, discounts

def getDiscountOnItem(item):
    return None