Rev 3401 | Rev 3454 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on 06-Sept-2011@author: Varun GuptaMax Uses: 1000A user can use the coupon only 5 times.Blackberry 9900 Bold 4 Rs.2000 offSamsung Galaxy S II i9100 Rs.1000 offSpice Blueberry Mini QT-58 20% offSpice Blueberry Exp QT-61 20% offNokia (All models upto Rs.5000) Rs.200 offNokia (All models 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.catalog.ttypes import Itemdef execute(cart, coupon_code, args):#Allow only first 1000 users to use the couponcount_coupon_usage = get_coupon_usage_count(coupon_code)if count_coupon_usage >= 1000: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, cart.userId)if count_users_usage > 4:raise PromotionException(111, 'This promotion is over.')if cart.lines:total_selling_price = 0total_discounted_price = 0has_qualified_model = Falsefor line in cart.lines:line.discountedPrice = line.actualPriceif line.itemId in (159, 160, 161, 1557, 1558): #Spice QT-58/61has_qualified_model = Trueif line.actualPrice:line.discountedPrice = round(line.actualPrice * 0.8)elif line.itemId == 1591: #Blackberry 9900 Bold 4has_qualified_model = Trueif line.actualPrice:line.discountedPrice = line.actualPrice - 2000elif line.itemId == 1502: #Galaxy SIIhas_qualified_model = Trueif line.actualPrice:line.discountedPrice = line.actualPrice - 1000else:print 'Can be Nokia'catalog_client = CatalogClient().get_client()item = catalog_client.getItem(line.itemId)print 'Item: %d, Brand: *%s*' % (line.itemId, item.brand.lower())if item.brand.lower() == 'nokia':has_qualified_model = Trueprint 'Brand matched'discount_value = 200 if line.actualPrice < 5001 else 500line.discountedPrice = line.actualPrice - discount_valueelse:print 'Brand not matched'total_selling_price += line.actualPrice * line.quantitytotal_discounted_price += line.discountedPrice + (line.actualPrice * (line.quantity - 1))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