Subversion Repositories SmartDukaan

Rev

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

'''
Created on 24-Oct-2011

@author: Varun Gupta

A user with email on shop2020.in domain can avail a discount 
of Rs.2000. The user can use this coupon only once.
'''

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.getUserByCartId(cart.id)
    
    if not user.email.lower().strip() in ('pankaj.jain@shop2020.in', 'pankaj.kankar@shop2020.in', 'rajveer.singh@shop2020.in', \
                          'chandranshu.s@shop2020.in', 'vikas.malik@shop2020.in', 'varun.gupta@shop2020.in', \
                          'mandeep.dhir@shop2020.in', 'vrinda.k@shop2020.in', 'smriti.r@shop2020.in', \
                          'tarita.kulkani@shop2020.in', 'manmohan.singh@shop2020.in', 'anand.sinha@shop2020.in', \
                          'parmod.kumar@shop2020.in', 'satya.mahapatra@shop2020.in', 'abhishek.mathur@shop2020.in', \
                          'chaitnaya.vats@shop2020.in', 'j.p.gupta@shop2020.in', 'zaffar.kar@shop2020.in'):
        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, user.id)
    
    if count_coupon_usage > 0:
        raise PromotionException(112, 'You have already used the voucher.')
    
    discounts = []
    
    if cart.lines:
        total_selling_price = 0
        total_discounted_price = 0
        available_discount = 2000
        
        for line in cart.lines:
            line.discountedPrice = line.actualPrice
            discount_value = 0
            
            if available_discount > 0:
                if line.actualPrice < available_discount:
                    line.discountedPrice = 0
                    discount_value = line.actualPrice
                    available_discount -= line.actualPrice
                else:
                    line.discountedPrice = line.actualPrice - available_discount
                    discount_value = available_discount
                    available_discount = 0
            
            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