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