def apply(self, basket, condition, offer, discount_amount=None, max_total_discount=None):
    if (discount_amount is None):
        discount_amount = self.value
    line_tuples = self.get_applicable_lines(offer, basket)
    max_affected_items = self._effective_max_affected_items()
    num_affected_items = 0
    affected_items_total = D('0.00')
    lines_to_discount = []
    for (price, line) in line_tuples:
        if (num_affected_items >= max_affected_items):
            break
        qty = min(line.quantity_without_offer_discount(offer), (max_affected_items - num_affected_items))
        lines_to_discount.append((line, price, qty))
        num_affected_items += qty
        affected_items_total += (qty * price)
    discount = min(discount_amount, affected_items_total)
    if (max_total_discount is not None):
        discount = min(discount, max_total_discount)
    if (discount == 0):
        return ZERO_DISCOUNT
    affected_lines = []
    applied_discount = D('0.00')
    for (i, (line, price, qty)) in enumerate(lines_to_discount):
        if (i == (len(lines_to_discount) - 1)):
            line_discount = (discount - applied_discount)
        else:
            line_discount = self.round((((price * qty) / affected_items_total) * discount))
        apply_discount(line, line_discount, qty, offer)
        affected_lines.append((line, line_discount, qty))
        applied_discount += line_discount
    condition.consume_items(offer, basket, affected_lines)
    return BasketDiscount(discount)
def apply(self, basket, condition, offer, discount_amount=None, max_total_discount=None):
    if (discount_amount is None):
        discount_amount = self.value
    line_tuples = self.get_applicable_lines(offer, basket)
    max_affected_items = self._effective_max_affected_items()
    num_affected_items = 0
    affected_items_total = D('0.00')
    lines_to_discount = []
    for (price, line) in line_tuples:
        if (num_affected_items >= max_affected_items):
            break
        qty = min(line.quantity_without_offer_discount(offer), (max_affected_items - num_affected_items))
        lines_to_discount.append((line, price, qty))
        num_affected_items += qty
        affected_items_total += (qty * price)
    discount = min(discount_amount, affected_items_total)
    if (max_total_discount is not None):
        discount = min(discount, max_total_discount)
    if (discount == 0):
        return ZERO_DISCOUNT
    affected_lines = []
    applied_discount = D('0.00')
    for (i, (line, price, qty)) in enumerate(lines_to_discount):
        if (i == (len(lines_to_discount) - 1)):
            line_discount = (discount - applied_discount)
        else:
            line_discount = self.round((((price * qty) / affected_items_total) * discount))
        apply_discount(line, line_discount, qty, offer)
        affected_lines.append((line, line_discount, qty))
        applied_discount += line_discount
    condition.consume_items(offer, basket, affected_lines)
    return BasketDiscount(discount)