def apply(self, basket, condition, offer, discount_percent=None, max_total_discount=None):
    if (discount_percent is None):
        discount_percent = self.value
    discount_amount_available = max_total_discount
    line_tuples = self.get_applicable_lines(offer, basket)
    discount_percent = min(discount_percent, D('100.0'))
    discount = D('0.00')
    affected_items = 0
    max_affected_items = self._effective_max_affected_items()
    affected_lines = []
    for (price, line) in line_tuples:
        if (affected_items >= max_affected_items):
            break
        if (discount_amount_available == 0):
            break
        quantity_affected = min(line.quantity_without_offer_discount(offer), (max_affected_items - affected_items))
        line_discount = self.round((((discount_percent / D('100.0')) * price) * int(quantity_affected)))
        if (discount_amount_available is not None):
            line_discount = min(line_discount, discount_amount_available)
            discount_amount_available -= line_discount
        apply_discount(line, line_discount, quantity_affected, offer)
        affected_lines.append((line, line_discount, quantity_affected))
        affected_items += quantity_affected
        discount += line_discount
    if (discount > 0):
        condition.consume_items(offer, basket, affected_lines)
    return BasketDiscount(discount)
def apply(self, basket, condition, offer, discount_percent=None, max_total_discount=None):
    if (discount_percent is None):
        discount_percent = self.value
    discount_amount_available = max_total_discount
    line_tuples = self.get_applicable_lines(offer, basket)
    discount_percent = min(discount_percent, D('100.0'))
    discount = D('0.00')
    affected_items = 0
    max_affected_items = self._effective_max_affected_items()
    affected_lines = []
    for (price, line) in line_tuples:
        if (affected_items >= max_affected_items):
            break
        if (discount_amount_available == 0):
            break
        quantity_affected = min(line.quantity_without_offer_discount(offer), (max_affected_items - affected_items))
        line_discount = self.round((((discount_percent / D('100.0')) * price) * int(quantity_affected)))
        if (discount_amount_available is not None):
            line_discount = min(line_discount, discount_amount_available)
            discount_amount_available -= line_discount
        apply_discount(line, line_discount, quantity_affected, offer)
        affected_lines.append((line, line_discount, quantity_affected))
        affected_items += quantity_affected
        discount += line_discount
    if (discount > 0):
        condition.consume_items(offer, basket, affected_lines)
    return BasketDiscount(discount)