| 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
|
|
|
50 |
updated_cart = rule_for_5perc_discount_on_5k_purchase(cart)
|
|
|
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 |
|
|
|
57 |
def rule_for_5perc_discount_on_5k_purchase(cart):
|
|
|
58 |
#TODO: Allow only first 100 users to use the coupon
|
|
|
59 |
|
|
|
60 |
#TODO: Allow a user to use the coupon only once.
|
|
|
61 |
|
|
|
62 |
if cart.lines:
|
|
|
63 |
total_selling_price = 0
|
|
|
64 |
|
|
|
65 |
for line in cart.lines:
|
|
|
66 |
if line.actualPrice:
|
|
|
67 |
total_selling_price += line.actualPrice * line.quantity
|
|
|
68 |
|
|
|
69 |
cart.totalPrice = total_selling_price
|
|
|
70 |
|
|
|
71 |
#if cart.totalPrice == 0.0:
|
|
|
72 |
# cart.discountedPrice = 0.0
|
|
|
73 |
|
|
|
74 |
if cart.totalPrice >= 5000:
|
|
|
75 |
discount_perc = 0.05
|
|
|
76 |
else:
|
|
|
77 |
discount_perc = 0.03
|
|
|
78 |
|
|
|
79 |
cart.discountedPrice = (1 - discount_perc) * total_selling_price
|
|
|
80 |
|
|
|
81 |
for line in cart.lines:
|
|
|
82 |
if line.actualPrice:
|
|
|
83 |
line.discountedPrice = (1 - discount_perc) * line.actualPrice
|
|
|
84 |
print "The final price, after discount is " + str(cart.discountedPrice)
|
|
|
85 |
return cart
|
|
|
86 |
|
|
|
87 |
def track_coupon_usage(coupon_code, transaction_id, user_id):
|
|
|
88 |
promotion_tracker = PromotionTracker()
|
|
|
89 |
promotion_tracker.coupon_code = coupon_code
|
|
|
90 |
promotion_tracker.transaction_id = transaction_id
|
|
|
91 |
promotion_tracker.user_id = user_id
|
|
|
92 |
promotion_tracker.applied_on = datetime.datetime.now()
|
|
|
93 |
session.commit()
|
|
|
94 |
|
|
|
95 |
def get_coupon_usage_count_by_user(coupon_code, user_id):
|
|
|
96 |
return PromotionTracker.query.filter_by(coupon_code = coupon_code).filter_by(user_id = user_id).count()
|
|
|
97 |
|
|
|
98 |
def close_session():
|
|
|
99 |
if session.is_active:
|
|
|
100 |
session.close()
|