Rev 13247 | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on May 24, 2010@author: Varun Gupta'''from elixir import sessionfrom shop2020.clients.CatalogClient import CatalogClientfrom shop2020.clients.UserClient import UserClientfrom shop2020.model.v1.user.impl import Dataservicefrom shop2020.model.v1.user.impl.Dataservice import Promotion, Coupon, \PromotionTracker, RechargeVoucherfrom shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \get_coupon_usage_count, get_coupon_usage_count_by_userfrom shop2020.thriftpy.model.v1.user.ttypes import PromotionExceptionfrom shop2020.utils.Utils import to_py_datefrom sqlalchemy.sql.expression import existsfrom string import Templateimport datetimeimport tracebackimport uuidfrom shop2020.model.v1.user.impl.Converters import to_t_couponfrom shop2020.model.v1.user.promotionrules import rule_specific_emi_discount_on_specific_itemsimport astdef initialize(dbname = 'user', db_hostname="localhost"):Dataservice.initialize(dbname, db_hostname)def create_promotion(name, rule_execution_src, start_on, end_on):promotion = Promotion()promotion.name = namepromotion.rule_execution_src = rule_execution_srcpromotion.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 = promotioncoupon.coupon_code = coupon_codecoupon.arguments = ""session.commit()def remove_all_coupons_by_promotion_id(promotion_id):promotion = Promotion.get_by(id=promotion_id)if(promotion is None):raise PromotionException(101, 'Could not find Promotion for id' + str(promotion_id))elif(promotion.end_on > datetime.datetime.now()):raise PromotionException(111, 'This promotion is still running. Promotion id' + str(promotion_id))deletecount = Coupon.query.filter_by(promotion_id = promotion_id).delete()session.commit()return deletecountdef create_coupon(promotionId, couponCategory, couponCode, arguments, isCod, prefix=None):if promotionId not in (26,27,28):raise PromotionException(101, 'Only promotion ids 26 and 27 are expected')promotion = Promotion.get_by(id = promotionId)if promotion:if couponCode == '':coupon_code = uuid.uuid4().hex[:10] if prefix is None else prefix + uuid.uuid4().hex[:6]else:coupon_code = couponCodecoupon = Coupon.get_by(coupon_code = coupon_code)while coupon is not None:coupon_code = uuid.uuid4().hex[:10] if prefix is None else prefix + uuid.uuid4().hex[:6]coupon = Coupon.get_by(coupon_code = coupon_code)arguments_map= ast.literal_eval(arguments)if arguments_map.has_key('orderId'):coupon_code = coupon_code + 'Id' +str(arguments_map['orderId'])coupon = Coupon()coupon.promotion = promotioncoupon.coupon_code = coupon_codecoupon.arguments = argumentscoupon.coupon_category = couponCategorysession.commit()return coupon_codeelse:raise PromotionException(101, 'Could not find Promotion for id' + str(promotionId))def get_coupon(coupon_code):return Coupon.get_by(coupon_code = coupon_code)def apply_recharge_coupon(coupon_code, total_amount, user_id):coupon = Coupon.get_by(coupon_code = coupon_code)discount = 0if coupon:args = eval(coupon.arguments) if coupon.arguments is not None else {}todate = datetime.datetime.now()if 'endOn' in args and todate > to_py_date(args['endOn']) :return {0:'This coupon has expired'}if 'minDiscountableVal' in args and total_amount < int(args['minDiscountableVal']):return {0:'This coupon is valid for purchases equal to or more than Rs.' + str((args['minDiscountableVal']))}if 'couponType' not in args or args['couponType'] not in ('recharge','both'):return {0:'Invalid Coupon'}isUserSpecific = Falseif args['emails'] != '*':user_client = UserClient().get_client()user = user_client.getUserById(user_id)isUserSpecific = Trueif user.email not in args['emails']:return {0:'Invalid coupon'}if 'startHour' in args and 'startMinute' in args and 'endHour' in args and 'endMinute' in args :startHour = int(args['startHour'])startMinute = int(args['startMinute'])endHour = int(args['endHour'])endMinute = int(args['endMinute'])now = datetime.datetime.now()curtime = datetime.time(now.hour, now.minute, now.second)starttime = datetime.time(startHour, startMinute, 0)endtime = datetime.time(endHour, endMinute, 0)if (curtime < starttime or curtime > endtime):return {0:'Currently this coupon is unavailable'}if 'usage_limit_for_user' in args :userLimit = args['usage_limit_for_user']else :userLimit = 1count_coupon_usage_by_user = get_coupon_usage_count_by_user(coupon_code, user_id)if count_coupon_usage_by_user >= userLimit:return {0:'Invalid Coupon'}if 'globalLimit' in args :globalLimit = args['globalLimit']count_coupon_usage = get_coupon_usage_count(coupon_code)if count_coupon_usage >= globalLimit:return {0:'This promotion is over.'}if 'discountType' in args:discountType = args['discountType']else :discountType = 'absolute' #Default is absoluteif discountType == 'percent' :discount = round(total_amount * (args['discount']/float(100)))elif discountType == 'absolute' :discount = min(args['discount'], total_amount)else :return {0 : 'Invalid coupon'}if 'maxDiscount' in args and discount > int(args['maxDiscount']):return {int(args['maxDiscount']):"Coupon Applied"}if isUserSpecific :return {discount:'login'}return {discount:'Coupon Applied'}else:return {0:'Invalid Coupon'}def apply_coupon(coupon_code, cart_id):coupon = Coupon.get_by(coupon_code = coupon_code)if coupon:todate = datetime.datetime.now()if not coupon.promotion.start_on <= todate:raise PromotionException(101, 'Invalid coupon')if not coupon.promotion.end_on >= todate:raise PromotionException(101, 'Promotion has expired')user_client = UserClient().get_client()cart = user_client.getCart(cart_id)coupon_module = coupon.promotion.rule_execution_src.strip()try:user_client.deleteDiscountsFromCart(cart_id)imported_coupon_module = __import__("shop2020.model.v1.user.promotionrules", globals(), locals(), [coupon_module])rule = eval("imported_coupon_module." + coupon_module)if coupon.promotion.id == 37:count = PromotionTracker.get_by(promotion_id = 37).count()if count >= 2013:raise PromotionException(112, 'This promotion is over.')args = eval(coupon.arguments) if coupon.arguments is not None else {}'''Processing in case of EMI'''if coupon.promotion.type == 2:updated_cart = rule.execute(cart, coupon_code, args)else:updated_cart, discounts = rule.execute(cart, coupon_code, args)if discounts and len(discounts) > 0:user_client.saveDiscounts(discounts)user_client.applyCouponToCart(updated_cart, coupon_code)return updated_cartexcept ImportError as e:traceback.print_stack()#TODO: Better messageraise PromotionException(100, 'Internal error occurred.')else:print 'Invalid Coupon Code'raise PromotionException(101, 'Invalid Coupon Code')def get_emi_discount(cart_id):try:user_client = UserClient().get_client()cart = user_client.getCart(cart_id)if not (cart.couponCode is None or cart.couponCode == ""):coupon = Coupon.get_by(coupon_code = cart.couponCode)args = eval(coupon.arguments) if coupon.arguments is not None else {}if coupon.promotion.type != 2:return {}else:return rule_specific_emi_discount_on_specific_items.getEmiDiscounts(cart, coupon.coupon_code, args)else:return {}except ImportError as e:traceback.print_stack()return {}def track_coupon_usage(coupon_code, transaction_id, user_id, amount, is_digital):coupon = Coupon.query.filter_by(coupon_code=coupon_code).one()promotion_tracker = PromotionTracker()promotion_tracker.coupon_code = coupon_codepromotion_tracker.transaction_id = transaction_idpromotion_tracker.user_id = user_idpromotion_tracker.promotion_id = coupon.promotion.idpromotion_tracker.applied_on = datetime.datetime.now()promotion_tracker.amount = amountpromotion_tracker.is_digital = is_digitalsession.commit()def get_active_coupons():return Coupon.query.all()def get_successful_payment_count_for_coupon(coupon_code):return PromotionTracker.query.filter_by(coupon_code = coupon_code).count()def get_rule_doc_string(rule_name):imported_coupon_module = __import__("shop2020.model.v1.user.promotionrules", globals(), locals(), [rule_name])rule = eval("imported_coupon_module." + rule_name)return rule.__doc__def close_session():if session.is_active:session.close()def is_alive():try:session.query(Promotion.id).limit(1).one()return Trueexcept:return Falsedef get_discounts_for_entity(entity_id):catalog_client = CatalogClient().get_client()items = catalog_client.getItemsByCatalogId(entity_id)discount_finder = ItemDiscountFinder()discounts = discount_finder.getCouponsAndDiscountsOnItem(items[0])if len(discounts) > 0:return {discounts[0][0]: discounts[0][1]}else:return {}def get_item_discount_map(item_ids):discount_finder = ItemDiscountFinder()item_coupons_discounts = []catalog_client = CatalogClient().get_client()for item_id in item_ids:item = catalog_client.getItem(item_id)discounts = discount_finder.getCouponsAndDiscountsOnItem(item)for discount in discounts:item_coupons_discounts.append((item_id, discount[0], discount[1]))breakreturn item_coupons_discountsdef get_active_gvs(promotion_id):all_coupons = session.query(Coupon).filter_by(promotion_id=promotion_id).filter(~exists().where(Coupon.coupon_code==PromotionTracker.coupon_code)).all()return [to_t_coupon(coupon) for coupon in all_coupons]def delete_coupon(coupon_code):Coupon.query.filter_by(coupon_code = coupon_code).delete()session.commit()def get_all_coupons_by_promotion_id(promotion_id):all_coupons = Coupon.query.filter_by(promotion_id = promotion_id).all()return [to_t_coupon(coupon) for coupon in all_coupons]def add_voucher(t_voucher):voucher = RechargeVoucher()voucher.available = Truevoucher.amount = t_voucher.amountvoucher.voucherCode = t_voucher.voucherCodevoucher.voucherType = t_voucher.voucherTypeif t_voucher.issuedOn:voucher.issuedOn = to_py_date(t_voucher.issuedOn)if t_voucher.expiredOn:voucher.expiredOn = to_py_date(t_voucher.expiredOn)session.commit()def assign_voucher(userId, userEmail, voucherType, amount):voucher = RechargeVoucher.query.filter_by(voucherType = voucherType, amount = amount, available = True).first()if not voucher:raise PromotionException(501, 'Voucher of this type is not available.')voucher.userId = userIdvoucher.available = Falsevoucher.issuedOn = datetime.datetime.now()voucher.email = userEmailsession.commit()return voucherdef mark_voucher_as_redeemed(voucherCode, redeemedOn):voucher = RechargeVoucher.query.filter_by(voucherCode = voucherCode).first()voucher.redeemedOn = to_py_date(redeemedOn)voucher.redeemed = Truesession.commit()return Trueclass ItemDiscountFinder:def __init__(self):self.coupon_modules = {}coupons = get_active_coupons()for coupon in coupons:if coupon.coupon_code not in ('SAHOLIC4RS', 'SAndroid', 'SAHOLIC4MJ', 'SAHOLIC4SS', 'SJunglee'):coupon_rule = coupon.promotion.rule_execution_src.strip()imported_coupon_module = __import__("shop2020.model.v1.user.promotionrules", globals(), locals(), [coupon_rule])self.coupon_modules[coupon.coupon_code] = eval("imported_coupon_module." + coupon_rule)def getCouponsAndDiscountsOnItem(self, item):discounts = []for coupon_code, coupon_rule_module in self.coupon_modules.iteritems():discount = coupon_rule_module.getDiscountOnItem(item)if discount is not None:discounts.append((coupon_code, discount))return discounts