Subversion Repositories SmartDukaan

Rev

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

'''
Created on 06-Dec-2011

@author: varungupta
'''

'''
Created on 24-Oct-2011

@author: Varun Gupta

raghvendra.saboo@gmail.com will get Rs.250 off on one transaction.
'''

from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
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 Discount

def execute(cart, coupon_code, args):
    
    user_client = UserClient().get_client()
    user = user_client.getUserById(cart.userId)
    
    if not user.email.lower().strip() in ('raghvendra.saboo@gmail.com'):
        raise PromotionException(111, 'You are not allowed to use this coupon')
    
    #Allow only first 1000 users to use the coupon
    count_coupon_usage = get_coupon_usage_count_by_user(coupon_code, cart.userId)
    
    if count_coupon_usage > 0:
        raise PromotionException(112, 'You have already used the coupon.')
    
    discounts = []
    
    if cart.lines:
        total_selling_price = 0
        total_discounted_price = 0
        has_used_coupon = False
        
        for line in cart.lines:
            line.discountedPrice = line.actualPrice
            discount_value = 0
            
            if has_used_coupon:
                break
            
            if line.actualPrice < 5000:
                line.discountedPrice = line.actualPrice - 200
                discount_value = 200
            else:
                line.discountedPrice = line.actualPrice - 500
                discount_value = 500
            
            has_used_coupon = True
            
            total_selling_price += line.actualPrice * line.quantity
            total_discounted_price += line.discountedPrice * line.quantity
            
            if discount_value > 0:
                discount = Discount()
                discount.cart_id = cart.id
                discount.item_id = line.itemId
                discount.discount = discount_value
                discount.quantity = 1

                discounts.append(discount)
            
            cart.totalPrice = total_selling_price
            cart.discountedPrice = total_discounted_price
    
    return cart, discounts