Subversion Repositories SmartDukaan

Rev

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

'''
Created on 06-Sept-2011

@author: Varun Gupta

Max Uses: 1000

A user can use the coupon only 2 times.

Spice Blueberry Mini QT-58    20% off
Spice Blueberry Exp QT-61    20% off
Nokia (All models, other than C2-03, from Rs.2000 to Rs.5000)  Rs.200 off
Nokia (All models above Rs.5000)    Rs.500 off
'''
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_user
from shop2020.clients.CatalogClient import CatalogClient
from shop2020.thriftpy.model.v1.catalog.ttypes import Item

def execute(cart, coupon_code, args):
    
    #Allow only first 1000 users to use the coupon
    count_coupon_usage = get_coupon_usage_count(coupon_code)
    
    if count_coupon_usage >= 1000:
        raise PromotionException(112, 'This promotion is over.')
    
    #Allow a user to use the coupon only 5 times max.
    count_users_usage = get_coupon_usage_count_by_user(coupon_code, cart.userId)
    
    if count_users_usage > 1:
        raise PromotionException(111, 'This promotion is over.')
    
    if cart.lines:
        total_selling_price = 0
        total_discounted_price = 0
        
        has_qualified_model = False
        
        for line in cart.lines:
            
            line.discountedPrice = line.actualPrice
            
            if line.itemId in (159, 160, 161, 1557, 1558):  #Spice QT-58/61
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = round(line.actualPrice * 0.8)
            
            # Excluded: Nokia C2-03 and Price above Rs.1,999
            elif line.itemId not in (1590, 2014) and line.actualPrice > 1999:
                
                catalog_client = CatalogClient().get_client()
                item = catalog_client.getItem(line.itemId)
                print 'Item: %d, Brand: *%s*' % (line.itemId, item.brand.lower())
                
                if item.brand.lower() == 'nokia':
                    has_qualified_model = True
                    
                    print 'Brand matched'
                    discount_value = 200 if line.actualPrice < 5001 else 500
                    line.discountedPrice = line.actualPrice - discount_value
                else:
                    print 'Brand not matched'
            
            total_selling_price += line.actualPrice * line.quantity
            total_discounted_price += line.discountedPrice + (line.actualPrice * (line.quantity - 1))
        
        if has_qualified_model is False:
            raise PromotionException(115, 'This coupon is applicable only for selective handsets.')
        
        cart.totalPrice = total_selling_price
        cart.discountedPrice = total_discounted_price
    
    return cart