@receiver(request_discounts)<EOL>def getBestDiscount(sender,**kwargs):
if not getConstant('<STR_LIT>'):<EOL><INDENT>return<EOL><DEDENT>logger.debug('<STR_LIT>')<EOL>reg = kwargs.pop('<STR_LIT>',None)<EOL>if not reg:<EOL><INDENT>logger.warning('<STR_LIT>')<EOL>return<EOL><DEDENT>payAtDoor = reg.payAtDoor<EOL>newCustomer = True<EOL>customer = Customer.objects.filter(email=reg.email,first_name=reg.firstName,last_name=reg.lastName).first()<EOL>if (customer and customer.numClassSeries > <NUM_LIT:0>) or sender != RegistrationSummaryView:<EOL><INDENT>newCustomer = False<EOL><DEDENT>eligible_filter = (<EOL>Q(event__series__pricingTier__isnull=False) |<EOL>Q(event__publicevent__pricingTier__isnull=False)<EOL>)<EOL>ineligible_filter = (<EOL>(Q(event__series__isnull=False) & Q(event__series__pricingTier__isnull=True)) |<EOL>(Q(event__publicevent__isnull=False) & Q(event__publicevent__pricingTier__isnull=True)) |<EOL>Q(dropIn=True)<EOL>)<EOL>if apps.is_installed('<STR_LIT>'):<EOL><INDENT>eligible_filter = eligible_filter | Q(event__privatelessonevent__pricingTier__isnull=False)<EOL>ineligible_filter = ineligible_filter | (<EOL>Q(event__privatelessonevent__isnull=False) &<EOL>Q(event__privatelessonevent__pricingTier__isnull=True)<EOL>)<EOL><DEDENT>eventregs_list = reg.temporaryeventregistration_set.all()<EOL>eligible_list = eventregs_list.filter(dropIn=False).filter(eligible_filter)<EOL>ineligible_list = eventregs_list.filter(ineligible_filter)<EOL>ineligible_total = sum(<EOL>[x.event.getBasePrice(payAtDoor=payAtDoor) for x in ineligible_list.exclude(dropIn=True)] +<EOL>[x.price for x in ineligible_list.filter(dropIn=True)]<EOL>)<EOL>discountCodesApplicable = getApplicableDiscountCombos(eligible_list, newCustomer, reg.student, customer=customer, addOn=False, cannotCombine=False, dateTime=reg.dateTime)<EOL>discountCodesApplicable.sort(key=lambda x: x.code.category.order)<EOL>best_discounts = OrderedDict()<EOL>initial_prices = [x.event.getBasePrice(payAtDoor=payAtDoor) for x in eligible_list]<EOL>initial_total = sum(initial_prices)<EOL>if discountCodesApplicable:<EOL><INDENT>net_allocated_prices = initial_prices<EOL>net_precategory_price = initial_total<EOL>last_category = discountCodesApplicable[<NUM_LIT:0>].code.category<EOL><DEDENT>for discount in discountCodesApplicable:<EOL><INDENT>if (discount.code.category != last_category):<EOL><INDENT>last_category = discount.code.category<EOL>if best_discounts:<EOL><INDENT>last_discount = best_discounts.get(next(reversed(best_discounts)))<EOL>net_allocated_prices = last_discount.net_allocated_prices<EOL>net_precategory_price = last_discount.net_price<EOL><DEDENT><DEDENT>tieredTuples = [(x,<NUM_LIT:1>) for x in eligible_list[:]]<EOL>for itemTuple in discount.itemTuples:<EOL><INDENT>tieredTuples = [(p,q) if p != itemTuple[<NUM_LIT:0>] else (p,q - itemTuple[<NUM_LIT:1>]) for (p,q) in tieredTuples]<EOL><DEDENT>response = discount.code.applyAndAllocate(net_allocated_prices,tieredTuples,payAtDoor)<EOL>current_code = best_discounts.get(discount.code.category.name, None)<EOL>if (<EOL>response and (<EOL>(not current_code and response.net_price < net_precategory_price) or<EOL>(current_code and response.net_price < current_code.net_price)<EOL>)<EOL>):<EOL><INDENT>best_discounts[discount.code.category.name] = response<EOL><DEDENT><DEDENT>uncombinedCodesApplicable = getApplicableDiscountCombos(<EOL>eligible_list, newCustomer, reg.student, customer=customer, addOn=False, cannotCombine=True, dateTime=reg.dateTime<EOL>)<EOL>for discount in uncombinedCodesApplicable:<EOL><INDENT>tieredTuples = [(x,<NUM_LIT:1>) for x in eligible_list[:]]<EOL>for itemTuple in discount.itemTuples:<EOL><INDENT>tieredTuples = [(p,q) if p != itemTuple[<NUM_LIT:0>] else (p,q - itemTuple[<NUM_LIT:1>]) for (p,q) in tieredTuples]<EOL><DEDENT>response = discount.code.applyAndAllocate(initial_prices,tieredTuples,payAtDoor)<EOL>if (<EOL>response and<EOL>response.net_price < min([x.net_price for x in best_discounts.values()] + [initial_total])<EOL>):<EOL><INDENT>best_discounts = OrderedDict({discount.code.category.name: response})<EOL><DEDENT><DEDENT>if not best_discounts:<EOL><INDENT>logger.debug('<STR_LIT>')<EOL><DEDENT>return DiscountCombo.DiscountApplication([x for x in best_discounts.values()], ineligible_total)<EOL>
When a customer registers for events, discounts may need to be
automatically applied.  A given shopping cart may, in fact,
be eligible for multiple different types of discounts (e.g. hours-based
discounts for increasing numbers of class hours), but typically, only one
discount should be applied.  Therefore, this handler loops through all potential
discounts, finds the ones that are applicable to the passed registration or set
of items, and returns the code and discounted price of the best available discount,
in a tuple of the form (code, discounted_price).