Subversion Repositories SmartDukaan

Rev

Rev 6250 | Rev 6367 | Go to most recent revision | 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 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 args['endOn'] 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:
        total_selling_price = 0
        productFound = False
        for line in cart.lines:
            line_total_price = line.actualPrice * line.quantity
            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
            total_selling_price += line_total_price
            
            
        if productFound:
            cart.totalPrice = total_selling_price
            cart.discountedPrice = total_selling_price - 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