| Line 4... |
Line 4... |
| 4 |
'''
|
4 |
'''
|
| 5 |
from shop2020.clients.UserClient import UserClient
|
5 |
from shop2020.clients.UserClient import UserClient
|
| 6 |
from shop2020.clients.InventoryClient import InventoryClient
|
6 |
from shop2020.clients.InventoryClient import InventoryClient
|
| 7 |
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
|
7 |
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
|
| 8 |
from shop2020.model.v1.user.impl import Dataservice
|
8 |
from shop2020.model.v1.user.impl import Dataservice
|
| 9 |
from shop2020.model.v1.user.impl.Dataservice import Promotion, Coupon,\
|
9 |
from shop2020.model.v1.user.impl.Dataservice import Promotion, Coupon, PromotionTracker
|
| 10 |
PromotionTracker
|
- |
|
| 11 |
from shop2020.utils.Utils import to_py_date
|
10 |
from shop2020.utils.Utils import to_py_date
|
| - |
|
11 |
from shop2020.model.v1.user.promotionrules import rule_for_5perc_discount_on_5k_purchase
|
| 12 |
|
12 |
|
| 13 |
from elixir import session
|
13 |
from elixir import session
|
| 14 |
import datetime
|
14 |
import datetime
|
| 15 |
|
15 |
|
| 16 |
def initialize(dbname = 'user'):
|
16 |
def initialize(dbname = 'user'):
|
| Line 36... |
Line 36... |
| 36 |
coupon.coupon_code = coupon_code
|
36 |
coupon.coupon_code = coupon_code
|
| 37 |
coupon.arguments = ""
|
37 |
coupon.arguments = ""
|
| 38 |
session.commit()
|
38 |
session.commit()
|
| 39 |
|
39 |
|
| 40 |
def apply_coupon(coupon_code, cart_id):
|
40 |
def apply_coupon(coupon_code, cart_id):
|
| 41 |
print coupon_code, cart_id
|
- |
|
| 42 |
coupon = Coupon.get_by(coupon_code = coupon_code)
|
41 |
coupon = Coupon.get_by(coupon_code = coupon_code)
|
| 43 |
|
42 |
|
| 44 |
if coupon:
|
43 |
if coupon:
|
| 45 |
user_client = UserClient().get_client()
|
44 |
user_client = UserClient().get_client()
|
| 46 |
cart = user_client.getCart(cart_id)
|
45 |
cart = user_client.getCart(cart_id)
|
| 47 |
|
46 |
|
| 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, coupon_code, {})
|
47 |
updated_cart = eval(coupon.promotion.rule_execution_src.strip() + ".execute(cart, coupon_code, {})")
|
| 51 |
user_client.applyCouponToCart(cart_id, coupon_code, updated_cart.totalPrice, updated_cart.discountedPrice)
|
48 |
user_client.applyCouponToCart(cart_id, coupon_code, updated_cart.totalPrice, updated_cart.discountedPrice)
|
| 52 |
return updated_cart
|
49 |
return updated_cart
|
| 53 |
else:
|
50 |
else:
|
| 54 |
print 'Invalid Coupon Code'
|
51 |
print 'Invalid Coupon Code'
|
| 55 |
raise PromotionException(101, 'Invalid Coupon Code')
|
52 |
raise PromotionException(101, 'Invalid Coupon Code')
|
| 56 |
|
53 |
|
| 57 |
def rule_for_5perc_discount_on_5k_purchase(cart, coupon_code, args):
|
- |
|
| 58 |
|
- |
|
| 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:
|
- |
|
| 62 |
raise PromotionException(111, 'You have already used this Coupon.')
|
- |
|
| 63 |
|
- |
|
| 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:
|
- |
|
| 67 |
raise PromotionException(112, 'This promotion is over.')
|
- |
|
| 68 |
|
- |
|
| 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 |
|
- |
|
| 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:
|
- |
|
| 80 |
discount_perc = 0.05
|
- |
|
| 81 |
else:
|
- |
|
| 82 |
discount_perc = 0.03
|
- |
|
| 83 |
|
- |
|
| 84 |
cart.totalPrice = round(total_selling_price, 2)
|
- |
|
| 85 |
cart.discountedPrice = round((1 - discount_perc) * total_selling_price, 2)
|
- |
|
| 86 |
|
- |
|
| 87 |
for line in cart.lines:
|
- |
|
| 88 |
if line.actualPrice:
|
- |
|
| 89 |
line.discountedPrice = round((1 - discount_perc) * line.actualPrice, 2)
|
- |
|
| 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):
|
54 |
def track_coupon_usage(coupon_code, transaction_id, user_id):
|
| 94 |
promotion_tracker = PromotionTracker()
|
55 |
promotion_tracker = PromotionTracker()
|
| 95 |
promotion_tracker.coupon_code = coupon_code
|
56 |
promotion_tracker.coupon_code = coupon_code
|
| 96 |
promotion_tracker.transaction_id = transaction_id
|
57 |
promotion_tracker.transaction_id = transaction_id
|
| 97 |
promotion_tracker.user_id = user_id
|
58 |
promotion_tracker.user_id = user_id
|
| 98 |
promotion_tracker.applied_on = datetime.datetime.now()
|
59 |
promotion_tracker.applied_on = datetime.datetime.now()
|
| 99 |
session.commit()
|
60 |
session.commit()
|
| 100 |
|
61 |
|
| 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 |
|
- |
|
| 104 |
def get_coupon_usage_count(coupon_code):
|
- |
|
| 105 |
return PromotionTracker.query.filter_by(coupon_code = coupon_code).count()
|
- |
|
| 106 |
|
- |
|
| 107 |
def close_session():
|
62 |
def close_session():
|
| 108 |
if session.is_active:
|
63 |
if session.is_active:
|
| 109 |
session.close()
|
64 |
session.close()
|
| 110 |
|
65 |
|