Subversion Repositories SmartDukaan

Rev

Rev 4857 | Rev 5499 | 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: 2000

A user can use the coupon only 2 times.

Spice Blueberry Mini QT-58    Rs.150 off
Nokia (All models, other than C2-03 & C2-06, from Rs.3000 to Rs.5000)  Rs.200 off
Nokia (All models, other than Lumia 710, 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.user.ttypes import Discount
from shop2020.clients.UserClient import UserClient

def execute(cart, coupon_code, args):
    user_client = UserClient().get_client()
    user = user_client.getUserByCartId(cart.id)
    
    #Allow only first 2000 users to use the coupon
    count_coupon_usage = get_coupon_usage_count(coupon_code)
    
    if count_coupon_usage >= 2000:
        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, user.id)
    
    if count_users_usage > 1:
        raise PromotionException(111, '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 (159, 160, 161) and not has_qualified_model:  #Spice QT-58
                
                has_qualified_model = True
                
                if line.actualPrice:
                    line.discountedPrice = round(line.actualPrice - 150)
                    discount_value = 150
            
            # Excluded: Nokia C2-03, C2-06, Lumia 710 and Price above Rs.1,999
            elif line.itemId not in (1590, 2014, 2063, 2035, 2036, 2565, 2602, 2210, 2478) and line.actualPrice > 2999 and not has_qualified_model:
                
                catalog_client = CatalogClient().get_client()
                item = catalog_client.getItem(line.itemId)
                
                if item.brand.lower() == 'nokia':
                    has_qualified_model = True
                    
                    discount_value = 200 if line.actualPrice < 5001 else 500
                    line.discountedPrice = line.actualPrice - discount_value
            
            total_selling_price += line.actualPrice * line.quantity
            total_discounted_price += line.discountedPrice + (line.actualPrice * (line.quantity - 1))
            
            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)
        
        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, discounts


def getDiscountOnItem(item):
    discount_expressions = [
            '150 if item.id in (159, 160, 161) else None',
            '200 if item.brand == "Nokia" and item.sellingPrice > 2999 and item.sellingPrice < 5001 and item.id not in (1590, 2014, 2063, 2035, 2036) else None',
            '500 if item.brand == "Nokia" and item.sellingPrice > 5000 and item.id not in (2565, 2602, 2210, 2478) else None'
        ]
    for expression in discount_expressions:
        discount = eval(expression)
        
        if discount is not None:
            return discount
    
    return None