Subversion Repositories SmartDukaan

Rev

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

'''
Created on 30-Apr-2012

@author: Varun Gupta

When SAccessory, coupon code is applied to cart, containing phone(s) as well as accessories,
a discount, equal to 5% of the selling price of phone/tablet, will be awarded on accessories. 
'''
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import 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
from shop2020.utils.Utils import to_py_date
import datetime

def execute(cart, coupon_code, args):
    user_client = UserClient().get_client()
    user = user_client.getUserByCartId(cart.id)
    #Allow a user to use the coupon only 1 time
    count_users_usage = get_coupon_usage_count_by_user(coupon_code, user.id)
    
    if count_users_usage > 0:
        raise PromotionException(111, 'This promotion is over.')
    
    discounts = []
    
    if cart.lines:
        active_since = to_py_date(user.activeSince)
        
        if datetime.date(2012, 5, 7) < datetime.date(active_since.year, active_since.month, active_since.day):
            raise PromotionException(111, 'You are not eligible for this discount')
        
        catalog_client = CatalogClient().get_client()
        
        category_map = {}
        for category in catalog_client.getAllCategories():
            if category_map.has_key(category.parent_category_id):
                category_map[category.parent_category_id].append(category.id)
            else:
                category_map[category.parent_category_id] = [category.id]
        
        total_selling_price = 0
        total_discounted_price = 0
        available_discount = 0
        
        has_phone = False
        has_accessory = False
        
        items = {}
        #Computing Discount Value
        for line in cart.lines:
            item = catalog_client.getItem(line.itemId)
            
            items[line.itemId] = item
            print item.modelName, ', category: ', item.category
            if item.category == 10010 or item.category in category_map[10001]:
                
                if available_discount < round(item.sellingPrice * 0.05): 
                    available_discount = round(item.sellingPrice * 0.05) 
                has_phone = True
        
        if not has_phone:
            raise PromotionException(115, 'This coupon is valid when a phone/tablet is included in the purchase')
        
        print 'Available Discount: ', available_discount
        
        #Applying discount on accessories
        for line in cart.lines:
            item = items[line.itemId]
            
            line.discountedPrice = line.actualPrice
            quantity_with_discount = 0
            total_discount_value = 0
            
            if item.category in category_map[10011]:
                print 'Accessory found: ', line.itemId, ' ', item.modelName
                discount_map = {}
                has_accessory = True
                
                for i in range(int(line.quantity)):
                    print 'For quantity ', i
                    print 'Available Discount: ', available_discount
                    discount_value = 0
                    
                    if available_discount > 0:
                        quantity_with_discount += 1
                        
                        if line.actualPrice < available_discount:
                            line.discountedPrice = 0
                            discount_value = line.actualPrice
                            available_discount -= line.actualPrice
                        else:
                            line.discountedPrice = line.actualPrice - available_discount
                            discount_value = available_discount
                            available_discount = 0
                    
                    if discount_map.has_key(discount_value):
                        discount_map[discount_value] += 1
                    elif discount_value > 0:
                        discount_map[discount_value] = 1
                    total_discount_value += discount_value
                
                for discount_value, quantity in discount_map.iteritems():
                    discount = Discount()
                    discount.cart_id = cart.id
                    discount.item_id = line.itemId
                    discount.discount = discount_value
                    discount.quantity = quantity
                    print discount
                    discounts.append(discount)
            
            print 'quantity_with_discount: ', quantity_with_discount
            
            total_discounted_price += line.quantity * line.actualPrice -total_discount_value
            total_selling_price += line.actualPrice * line.quantity
            print 'Total discounted price: ', total_discounted_price
            
            cart.totalPrice = total_selling_price
            cart.discountedPrice = total_discounted_price
        
        if not has_accessory:
            raise PromotionException(115, 'This coupon is valid when an accessory is included in the purchase')
    
    return cart, discounts

def getDiscountOnItem(item):
    return None