Rev 6682 | Rev 7475 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on 06-Dec-2011@author: varungupta'''from shop2020.clients.UserClient import UserClientfrom shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \get_coupon_usage_count_by_userfrom shop2020.thriftpy.model.v1.user.ttypes import PromotionException, Discountfrom shop2020.utils.Utils import to_py_dateimport datetimedef execute(cart, coupon_code, args):user_client = UserClient().get_client()user = user_client.getUserByCartId(cart.id)email = user.email.lower().strip()if args['couponType'] is not None and args['couponType'] != 'physical' and args['couponType'] != 'both':raise PromotionException(111, 'This coupon is not valid')if args['emails'] == '*' or email in args['emails']:discount_value = args['discount']else:raise PromotionException(111, 'You are not allowed to use this coupon')todate = datetime.datetime.now()if 'endOn' in args and todate > to_py_date(args['endOn']):raise PromotionException(113, 'This coupon is expired.')count_coupon_usage = get_coupon_usage_count_by_user(coupon_code, user.userId)if count_coupon_usage >= args['usage_limit_for_user']:raise PromotionException(112, 'Your usage limit for this coupon is exhausted.')discounts = []if cart.lines:total_discounts = 0for line in cart.lines:line_total_price = line.actualPrice * line.quantity + line.insuranceAmountline_total_discount = 0while line_total_discount < line_total_price and discount_value > 0:if line.actualPrice < discount_value:discountGiven = line.actualPricequantity = min(line.quantity,int(discount_value/discountGiven))else:discountGiven = discount_valuequantity = 1discount = Discount()discount.cart_id = cart.iddiscount.item_id = line.itemIddiscount.discount = discountGivendiscount.quantity = quantitydiscounts.append(discount)discount_value = discount_value - discountGiven*quantityline_total_discount += discountGiven*quantitytotal_discounts += line_total_discountcart.discountedPrice = cart.totalPrice - total_discountsreturn cart, discountsdef getDiscountOnItem(item):return None