Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
1976 varun.gupt 1
'''
2
Created on May 24, 2010
3
@author: Varun Gupta
4
'''
5
from shop2020.clients.UserClient import UserClient
6
from shop2020.clients.InventoryClient import InventoryClient
7
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
8
from shop2020.model.v1.user.impl import Dataservice
9
from shop2020.model.v1.user.impl.Dataservice import Promotion, Coupon,\
10
    PromotionTracker
11
from shop2020.utils.Utils import to_py_date
12
 
13
from elixir import session
14
import datetime
15
 
16
def initialize(dbname = 'user'):
17
    Dataservice.initialize(dbname)
18
 
19
def create_promotion(name, rule_execution_src, start_on, end_on):
20
    promotion = Promotion()
21
    promotion.name = name
22
    promotion.rule_execution_src = rule_execution_src
23
    promotion.start_on = to_py_date(start_on)
24
    promotion.end_on = to_py_date(end_on)
25
    promotion.created_on = datetime.datetime.now()
26
    session.commit()
27
 
28
def get_all_promotions():
29
    return Promotion.query.all()
30
 
31
def generate_coupons_for_promotion(promotion_id, coupon_code):
32
    promotion = Promotion.get_by(id = promotion_id)
33
 
34
    coupon = Coupon()
35
    coupon.promotion = promotion
36
    coupon.coupon_code = coupon_code
37
    coupon.arguments = ""
38
    session.commit()
39
 
40
def apply_coupon(coupon_code, cart_id):
41
    print  coupon_code, cart_id
42
    coupon = Coupon.get_by(coupon_code = coupon_code)
43
 
44
    if coupon:
45
        user_client = UserClient().get_client()
46
        cart = user_client.getCart(cart_id)
47
 
48
        #TODO: Use rule_name to updates the prices in the cart.
49
        rule_name = coupon.promotion.rule_execution_src
2003 varun.gupt 50
        updated_cart = rule_for_5perc_discount_on_5k_purchase(cart, coupon_code, {})
1976 varun.gupt 51
        user_client.applyCouponToCart(cart_id, coupon_code, updated_cart.totalPrice, updated_cart.discountedPrice)
52
        return updated_cart
53
    else:
54
        print 'Invalid Coupon Code'
55
        raise PromotionException(101, 'Invalid Coupon Code')
56
 
2003 varun.gupt 57
def rule_for_5perc_discount_on_5k_purchase(cart, coupon_code, args):
1976 varun.gupt 58
 
2003 varun.gupt 59
    #Allow a user to use the coupon only once.
60
    count_users_usage = get_coupon_usage_count_by_user(coupon_code, cart.userId)
61
    if count_users_usage > 0:
2007 varun.gupt 62
        raise PromotionException(111, 'You have already used this Coupon.')
1976 varun.gupt 63
 
2003 varun.gupt 64
    #Allow only first 100 users to use the coupon
65
    count_coupon_usage = get_coupon_usage_count(coupon_code)
66
    if count_coupon_usage >= 100:
2006 varun.gupt 67
        raise PromotionException(112, 'This Coupon is now exhausted.')
2003 varun.gupt 68
 
1976 varun.gupt 69
    if cart.lines:
70
        total_selling_price = 0
71
 
72
        for line in cart.lines:
73
            if line.actualPrice:
74
                total_selling_price +=  line.actualPrice * line.quantity
75
 
2003 varun.gupt 76
        #MAX Discount 1500
77
        if total_selling_price * 0.05 >= 1500:
78
            discount_perc = 1500 / total_selling_price
79
        elif total_selling_price >= 5000:
1976 varun.gupt 80
            discount_perc = 0.05
81
        else:
82
            discount_perc = 0.03
83
 
2003 varun.gupt 84
        cart.totalPrice = total_selling_price
1976 varun.gupt 85
        cart.discountedPrice = (1 - discount_perc) * total_selling_price
86
 
87
        for line in cart.lines:
88
            if line.actualPrice:
89
                line.discountedPrice = (1 - discount_perc) * line.actualPrice
90
                print "The final price, after discount is " + str(cart.discountedPrice)
91
    return cart
92
 
93
def track_coupon_usage(coupon_code, transaction_id, user_id):
94
    promotion_tracker = PromotionTracker()
95
    promotion_tracker.coupon_code = coupon_code
96
    promotion_tracker.transaction_id = transaction_id
97
    promotion_tracker.user_id = user_id
98
    promotion_tracker.applied_on = datetime.datetime.now()
99
    session.commit()
100
 
101
def get_coupon_usage_count_by_user(coupon_code, user_id):
102
    return PromotionTracker.query.filter_by(coupon_code = coupon_code).filter_by(user_id = user_id).count()
103
 
2003 varun.gupt 104
def get_coupon_usage_count(coupon_code):
105
    return PromotionTracker.query.filter_by(coupon_code = coupon_code).count()
106
 
1976 varun.gupt 107
def close_session():
108
    if session.is_active:
109
        session.close()