Subversion Repositories SmartDukaan

Rev

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

'''
Created on 14-Sept-2011

@author: Varun Gupta

Curve 8520    Rs.250 off
Curve 3G 9300    Rs.250 off
Torch 2 9810    Rs.600 off
Playbook 16 GB    Rs.1390 off
Playbook 32 GB    Rs.1690 off
'''
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discount
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count

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 (5, 6, 7, 8, 9, 1499):  #Curve 8520
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = round(line.actualPrice  - 250)
                    discount_value = 250
            
            elif line.itemId in (10, 1560, 1561):  #Curve 3G 9300
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 250
                    discount_value = 250
            
            elif line.itemId == 2016:  #Torch 2 9810
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 600
                    discount_value = 600
             
            elif line.itemId == 1543:  #Playbook 16 GB
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 1390
                    discount_value = 1390
            
            elif line.itemId == 1550:  #Playbook 32 GB
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = line.actualPrice - 1690
                    discount_value = 1690
            
            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 Blackberry handsets.')
        
        cart.totalPrice = total_selling_price
        cart.discountedPrice = total_discounted_price
    
    return cart, discounts

def getDiscountOnItem(item):
    discount_expressions = [
            '250 if item.id in (5, 6, 7, 8, 9, 1499, 10, 1560, 1561) else None',
            '600 if item.id == 2016 else None',
            '1390 if item.id == 1543 else None',
            '1690 if item.id == 1550 else None'
        ]
    for expression in discount_expressions:
        discount = eval(expression)
        
        if discount is not None:
            return discount
    
    return None