Rev 5132 | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on 30-Apr-2012@author: Varun GuptaWhen SAccessory, coupon code is applied to cart, containing phone(s) as well as accessories,a discount, equal to 5% of the selling price of phone/tablet, will be awarded on accessories.'''from shop2020.thriftpy.model.v1.user.ttypes import PromotionExceptionfrom shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count_by_userfrom shop2020.clients.CatalogClient import CatalogClientfrom shop2020.thriftpy.model.v1.user.ttypes import Discountfrom shop2020.clients.UserClient import UserClientfrom shop2020.utils.Utils import to_py_dateimport datetimedef execute(cart, coupon_code, args):user_client = UserClient().get_client()user = user_client.getUserByCartId(cart.id)#Allow a user to use the coupon only 1 timecount_users_usage = get_coupon_usage_count_by_user(coupon_code, user.id)if count_users_usage > 0:raise PromotionException(111, 'This promotion is over.')discounts = []if cart.lines:active_since = to_py_date(user.activeSince)if datetime.date(2012, 5, 7) < datetime.date(active_since.year, active_since.month, active_since.day):raise PromotionException(111, 'You are not eligible for this discount')catalog_client = CatalogClient().get_client()category_map = {}for category in catalog_client.getAllCategories():if category_map.has_key(category.parent_category_id):category_map[category.parent_category_id].append(category.id)else:category_map[category.parent_category_id] = [category.id]total_selling_price = 0total_discounted_price = 0available_discount = 0has_phone = Falsehas_accessory = Falseitems = {}#Computing Discount Valuefor line in cart.lines:item = catalog_client.getItem(line.itemId)items[line.itemId] = itemprint item.modelName, ', category: ', item.categoryif item.category == 10010 or item.category in category_map[10001]:if available_discount < round(item.sellingPrice * 0.05):available_discount = round(item.sellingPrice * 0.05)has_phone = Trueif not has_phone:raise PromotionException(115, 'This coupon is valid when a phone/tablet is included in the purchase')print 'Available Discount: ', available_discount#Applying discount on accessoriesfor line in cart.lines:item = items[line.itemId]line.discountedPrice = line.actualPricequantity_with_discount = 0total_discount_value = 0if item.category in category_map[10011]:print 'Accessory found: ', line.itemId, ' ', item.modelNamediscount_map = {}has_accessory = Truefor i in range(int(line.quantity)):print 'For quantity ', iprint 'Available Discount: ', available_discountdiscount_value = 0if available_discount > 0:quantity_with_discount += 1if line.actualPrice < available_discount:line.discountedPrice = 0discount_value = line.actualPriceavailable_discount -= line.actualPriceelse:line.discountedPrice = line.actualPrice - available_discountdiscount_value = available_discountavailable_discount = 0if discount_map.has_key(discount_value):discount_map[discount_value] += 1elif discount_value > 0:discount_map[discount_value] = 1total_discount_value += discount_valuefor discount_value, quantity in discount_map.iteritems():discount = Discount()discount.cart_id = cart.iddiscount.item_id = line.itemIddiscount.discount = discount_valuediscount.quantity = quantityprint discountdiscounts.append(discount)print 'quantity_with_discount: ', quantity_with_discounttotal_discounted_price += line.quantity * line.actualPrice -total_discount_valuetotal_selling_price += line.actualPrice * line.quantityprint 'Total discounted price: ', total_discounted_pricecart.totalPrice = total_selling_pricecart.discountedPrice = total_discounted_priceif not has_accessory:raise PromotionException(115, 'This coupon is valid when an accessory is included in the purchase')return cart, discountsdef getDiscountOnItem(item):return None