Rev 4857 | Rev 5499 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on 06-Sept-2011@author: Varun GuptaMax Uses: 2000A user can use the coupon only 2 times.Spice Blueberry Mini QT-58 Rs.150 offNokia (All models, other than C2-03 & C2-06, from Rs.3000 to Rs.5000) Rs.200 offNokia (All models, other than Lumia 710, above Rs.5000) Rs.500 off'''from shop2020.thriftpy.model.v1.user.ttypes import PromotionExceptionfrom shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count, get_coupon_usage_count_by_userfrom shop2020.clients.CatalogClient import CatalogClientfrom shop2020.thriftpy.model.v1.user.ttypes import Discountfrom shop2020.clients.UserClient import UserClientdef execute(cart, coupon_code, args):user_client = UserClient().get_client()user = user_client.getUserByCartId(cart.id)#Allow only first 2000 users to use the couponcount_coupon_usage = get_coupon_usage_count(coupon_code)if count_coupon_usage >= 2000:raise PromotionException(112, 'This promotion is over.')#Allow a user to use the coupon only 5 times max.count_users_usage = get_coupon_usage_count_by_user(coupon_code, user.id)if count_users_usage > 1:raise PromotionException(111, 'This promotion is over.')discounts = []if cart.lines:total_selling_price = 0total_discounted_price = 0has_qualified_model = Falsefor line in cart.lines:line.discountedPrice = line.actualPricediscount_value = 0if line.itemId in (159, 160, 161) and not has_qualified_model: #Spice QT-58has_qualified_model = Trueif line.actualPrice:line.discountedPrice = round(line.actualPrice - 150)discount_value = 150# Excluded: Nokia C2-03, C2-06, Lumia 710 and Price above Rs.1,999elif line.itemId not in (1590, 2014, 2063, 2035, 2036, 2565, 2602, 2210, 2478) and line.actualPrice > 2999 and not has_qualified_model:catalog_client = CatalogClient().get_client()item = catalog_client.getItem(line.itemId)if item.brand.lower() == 'nokia':has_qualified_model = Truediscount_value = 200 if line.actualPrice < 5001 else 500line.discountedPrice = line.actualPrice - discount_valuetotal_selling_price += line.actualPrice * line.quantitytotal_discounted_price += line.discountedPrice + (line.actualPrice * (line.quantity - 1))if discount_value > 0:discount = Discount()discount.cart_id = cart.iddiscount.item_id = line.itemIddiscount.discount = discount_valuediscount.quantity = 1discounts.append(discount)if has_qualified_model is False:raise PromotionException(115, 'This coupon is applicable only for selective handsets.')cart.totalPrice = total_selling_pricecart.discountedPrice = total_discounted_pricereturn cart, discountsdef getDiscountOnItem(item):discount_expressions = ['150 if item.id in (159, 160, 161) else None','200 if item.brand == "Nokia" and item.sellingPrice > 2999 and item.sellingPrice < 5001 and item.id not in (1590, 2014, 2063, 2035, 2036) else None','500 if item.brand == "Nokia" and item.sellingPrice > 5000 and item.id not in (2565, 2602, 2210, 2478) else None']for expression in discount_expressions:discount = eval(expression)if discount is not None:return discountreturn None