Subversion Repositories SmartDukaan

Rev

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

'''
Created on May 24, 2010
@author: Varun Gupta
'''
from shop2020.clients.UserClient import UserClient
from shop2020.clients.InventoryClient import InventoryClient
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
from shop2020.model.v1.user.impl import Dataservice
from shop2020.model.v1.user.impl.Dataservice import Promotion, Coupon,\
    PromotionTracker
from shop2020.utils.Utils import to_py_date

from elixir import session
import datetime

def initialize(dbname = 'user'):
    Dataservice.initialize(dbname)

def create_promotion(name, rule_execution_src, start_on, end_on):
    promotion = Promotion()
    promotion.name = name
    promotion.rule_execution_src = rule_execution_src
    promotion.start_on = to_py_date(start_on)
    promotion.end_on = to_py_date(end_on)
    promotion.created_on = datetime.datetime.now()
    session.commit()

def get_all_promotions():
    return Promotion.query.all()

def generate_coupons_for_promotion(promotion_id, coupon_code):
    promotion = Promotion.get_by(id = promotion_id)
    
    coupon = Coupon()
    coupon.promotion = promotion
    coupon.coupon_code = coupon_code
    coupon.arguments = ""
    session.commit()

def apply_coupon(coupon_code, cart_id):
    print  coupon_code, cart_id
    coupon = Coupon.get_by(coupon_code = coupon_code)
    
    if coupon:
        user_client = UserClient().get_client()
        cart = user_client.getCart(cart_id)

        #TODO: Use rule_name to updates the prices in the cart.
        rule_name = coupon.promotion.rule_execution_src
        updated_cart = rule_for_5perc_discount_on_5k_purchase(cart, coupon_code, {})
        user_client.applyCouponToCart(cart_id, coupon_code, updated_cart.totalPrice, updated_cart.discountedPrice)
        return updated_cart
    else:
        print 'Invalid Coupon Code'
        raise PromotionException(101, 'Invalid Coupon Code')

def rule_for_5perc_discount_on_5k_purchase(cart, coupon_code, args):
    
    #Allow a user to use the coupon only once.
    count_users_usage = get_coupon_usage_count_by_user(coupon_code, cart.userId)
    if count_users_usage > 0:
        return cart
    
    #Allow only first 100 users to use the coupon
    count_coupon_usage = get_coupon_usage_count(coupon_code)
    if count_coupon_usage >= 100:
        return cart
    
    if cart.lines:
        total_selling_price = 0
        
        for line in cart.lines:
            if line.actualPrice:
                total_selling_price +=  line.actualPrice * line.quantity
        
        #MAX Discount 1500
        if total_selling_price * 0.05 >= 1500:
            discount_perc = 1500 / total_selling_price
        elif total_selling_price >= 5000:
            discount_perc = 0.05
        else:
            discount_perc = 0.03
        
        cart.totalPrice = total_selling_price
        cart.discountedPrice = (1 - discount_perc) * total_selling_price

        for line in cart.lines:
            if line.actualPrice:
                line.discountedPrice = (1 - discount_perc) * line.actualPrice
                print "The final price, after discount is " + str(cart.discountedPrice)
    return cart

def track_coupon_usage(coupon_code, transaction_id, user_id):
    promotion_tracker = PromotionTracker()
    promotion_tracker.coupon_code = coupon_code
    promotion_tracker.transaction_id = transaction_id
    promotion_tracker.user_id = user_id
    promotion_tracker.applied_on = datetime.datetime.now()
    session.commit()

def get_coupon_usage_count_by_user(coupon_code, user_id):
    return PromotionTracker.query.filter_by(coupon_code = coupon_code).filter_by(user_id = user_id).count()

def get_coupon_usage_count(coupon_code):
    return PromotionTracker.query.filter_by(coupon_code = coupon_code).count()

def close_session():
    if session.is_active:
        session.close()