Subversion Repositories SmartDukaan

Rev

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

'''
Created on 01-Oct-2011

@author: Varun Gupta

Max uses: 100

BB Playbook 16GB     1090 off
BB Playbook 64GB     1690 off
HTC P510e Flyer     500 off
Motorola Xoom WiFi 3G     1700 off
Samsung P7500 Galaxy Tab     2500 off
Spice Mi-720 Tab     1500 off
'''
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_user
from shop2020.clients.CatalogClient import CatalogClient

def execute(cart, coupon_code, args):
    
    #Allow only first 100 users to use the coupon
    count_coupon_usage = get_coupon_usage_count(coupon_code)
    
    if count_coupon_usage >= 100:
        raise PromotionException(112, 'This promotion is over.')
    
    discounts = []
    
    if cart.lines:
        total_selling_price = 0
        total_discounted_price = 0
        
        has_qualified_model = False
        
        for line in cart.lines:
            
            line.discountedPrice = line.actualPrice
            discount_value = 0
            
            if line.itemId == 1543:  #Playbook 16GB
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 1090
                    discount_value = 1090
            
            elif line.itemId == 1550:   #Playbook 64GB
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 1690
                    discount_value = 1690
            
            elif line.itemId == 1551:   #HTC Flyer P510e
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 500
                    discount_value = 500
            
            elif line.itemId == 1552:   #Motorola Xoom WiFi 3G
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 1700
                    discount_value = 1700
            
            elif line.itemId == 2005:   #Samsung P7500 Galaxy Tab
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 2500
                    discount_value = 2500
            
            elif line.itemId == 2022:   #Spice Mi-720 Tab
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 1500
                    discount_value = 1500
            
            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 = line.quantity
                
                discounts.append(discount)
        
        if has_qualified_model is False:
            raise PromotionException(115, 'This coupon is applicable only for selective tablets.')
        
        cart.totalPrice = total_selling_price
        cart.discountedPrice = total_discounted_price
    
    return cart, discounts