Rev 6471 | Rev 6903 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on 06-Jul-2011@author: Varun Gupta'''from shop2020.clients.CatalogClient import CatalogClientfrom shop2020.clients.UserClient import UserClientfrom shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \get_coupon_usage_count, get_coupon_usage_count_by_userfrom shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discountimport datetimedef execute(cart, coupon_code, args):if 'handset_display_name' not in args:raise PromotionException(109, 'Error in rule arguments.')if 'end_on' in args and datetime.datetime.strptime(args['end_on'], '%Y-%m-%d') <= datetime.datetime.now():raise PromotionException(112, 'This promotion is expired.')if 'usage_limit' in args and args['usage_limit'] is not None:if args['usage_limit'] <= get_coupon_usage_count(coupon_code):raise PromotionException(112, 'This promotion is over.')user_client = UserClient().get_client()user = user_client.getUserByCartId(cart.id)if 'usage_limit_for_user' in args and args['usage_limit_for_user'] is not None:if args['usage_limit_for_user'] <= get_coupon_usage_count_by_user(coupon_code, user.userId):raise PromotionException(111, 'You have already used this coupon maximum possible times.')if not cart.lines:return cartdiscountJ = Noneif 'discount' in args:discountJ = args ['discount']min_amount = Noneif 'min_amount' in args:min_amount = args ['min_amount']max_discount = Noneif 'max_discount' in args:max_discount = args ['max_discount']isPercentageDiscount = Falseif 'is_percentage_discount' in args:isPercentageDiscount = args['is_percentage_discount']total_selling_price = 0total_discounted_price = 0cart_has_specified_item = Falsediscounts = []for line in cart.lines:total_selling_price += line.actualPrice * line.quantitydiscount_value = 0if min_amount is not None and line.actualPrice < min_amount:total_discounted_price += round(line.actualPrice * line.quantity, 4)continueif 'items' in args:if line.itemId in args['items'] :if type(args['items'])==dict:discount_value = args['items'][line.itemId]cart_has_specified_item = Trueelif discountJ is not None:discount_value = discountJcart_has_specified_item = Trueelse:total_discounted_price += round(line.actualPrice * line.quantity, 4)continueelif 'categories' in args:catalog_client = CatalogClient().get_client()category = catalog_client.getItem(line.itemId).categoryif category in args['categories']:if type(args['categories'])==dict:discount_value = args['categories'][category]cart_has_specified_item = Trueelif discountJ is not None:discount_value = discountJcart_has_specified_item = Trueelse:total_discounted_price += round(line.actualPrice * line.quantity, 4)continueelif discountJ is not None:discount_value = discountJcart_has_specified_item = Trueelse :total_discounted_price += round(line.actualPrice * line.quantity, 4)continueif isPercentageDiscount:discount_value = round((line.actualPrice*discount_value)/100, 4)if max_discount is not None:discount_value = min(max_discount, discount_value)line.discountedPrice = round(line.actualPrice - discount_value, 4)total_discounted_price += round(line.discountedPrice * line.quantity, 4)if discount_value > 0:discount = Discount()discount.discount = discount_valuediscount.quantity = line.quantitydiscount.cart_id = cart.iddiscount.item_id = line.itemIddiscounts.append(discount)if cart_has_specified_item is False:raise PromotionException(115, 'This coupon is applicable only for ' + args['handset_display_name'])cart.totalPrice = round(total_selling_price, 4)cart.discountedPrice = total_discounted_pricereturn cart, discountsdef getDiscountOnItem(item):return None