Subversion Repositories SmartDukaan

Rev

Rev 2010 | Rev 2278 | 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
2121 varun.gupt 9
from shop2020.model.v1.user.impl.Dataservice import Promotion, Coupon, PromotionTracker
1976 varun.gupt 10
from shop2020.utils.Utils import to_py_date
2121 varun.gupt 11
from shop2020.model.v1.user.promotionrules import rule_for_5perc_discount_on_5k_purchase
1976 varun.gupt 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
    coupon = Coupon.get_by(coupon_code = coupon_code)
42
 
43
    if coupon:
44
        user_client = UserClient().get_client()
45
        cart = user_client.getCart(cart_id)
46
 
2121 varun.gupt 47
        updated_cart = eval(coupon.promotion.rule_execution_src.strip() + ".execute(cart, coupon_code, {})")
1976 varun.gupt 48
        user_client.applyCouponToCart(cart_id, coupon_code, updated_cart.totalPrice, updated_cart.discountedPrice)
49
        return updated_cart
50
    else:
51
        print 'Invalid Coupon Code'
52
        raise PromotionException(101, 'Invalid Coupon Code')
53
 
54
def track_coupon_usage(coupon_code, transaction_id, user_id):
55
    promotion_tracker = PromotionTracker()
56
    promotion_tracker.coupon_code = coupon_code
57
    promotion_tracker.transaction_id = transaction_id
58
    promotion_tracker.user_id = user_id
59
    promotion_tracker.applied_on = datetime.datetime.now()
60
    session.commit()
61
 
62
def close_session():
63
    if session.is_active:
64
        session.close()