Subversion Repositories SmartDukaan

Rev

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

'''
Created on 01-Oct-2011

@author: Varun Gupta

Max uses: 100

Apple 16GB iPad 2 Wi-Fi+3G Rs.900 off
Apple 32GB iPad 2 Wi-Fi+3G Rs.900 off
Apple 64GB iPad 2 Wi-Fi+3G Rs.900 off
BlackBerry playbook 32gb Rs.250 off
BlackBerry playbook 64gb Rs.2500 off
HTC P510e Flyer Rs.500 off
Motorola MZ601 Xoom Rs.2000 off
Samsung N7000 Galaxy Note Rs.2500 off
Samsung P7300 Tablet 8.9" Rs.1500 off
Samsung P7500 Galaxy Tab 750 Rs.1800 off
Spice Mi-720 Tab Rs.1200 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

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 in [2177, 2178, 2179, 2180, 2181, 2182]:  #iPad 16GB, iPad 32GB, iPad 64GB
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 900
                    discount_value = 900
            
            elif line.itemId == 2199:   #BlackBerry PlayBook 32GB
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 250
                    discount_value = 250
            
            elif line.itemId == 1550:   #BlackBerry PlayBook 64GB
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 2500
                    discount_value = 2500
            
            elif line.itemId == 1551:   #HTC P510e Flyer
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 500
                    discount_value = 500
            
            elif line.itemId == 1552:   #Motorola MZ601 Xoom
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 2000
                    discount_value = 2000
            
            elif line.itemId == 2215:   #Samsung N7000 Galaxy Note
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 2500
                    discount_value = 2500
            
            elif line.itemId == 2230:   #Samsung P7300 Galaxy Tab
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 1500
                    discount_value = 1500
            
            elif line.itemId in (2005, 2194):   #Samsung P7500 Galaxy Tab
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 1700
                    discount_value = 1700
            
            elif line.itemId == 2022:   #Spice Mi-720 Tab
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 1200
                    discount_value = 1200
            
            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


def getDiscountOnItem(item):
    discount_expressions = [
            '900 if item.id in [2177, 2178, 2179, 2180, 2181, 2182] else None',
            '250 if item.id == 2199 else None',
            '2500 if item.id == 1550 else None',
            '500 if item.id == 1551 else None',
            '2000 if item.id == 1552 else None',
            '2500 if item.id == 2215 else None',
            '1500 if item.id == 2230 else None',
            '1700 if item.id == 2005 else None',
            '1200 if item.id == 2022 else None'
        ]
    for expression in discount_expressions:
        discount = eval(expression)
        
        if discount is not None:
            return discount
    
    return None