#NEWLINE# def update_credits( self , payment_record ) : #NEWLINE# #INDENT# amount = payment_record . amount #NEWLINE# for invoice in self . invoices : #NEWLINE# #INDENT# deduct_amount = min ( amount , invoice . balance ) #NEWLINE# amount -= deduct_amount #NEWLINE# if ( deduct_amount > 0 ) : #NEWLINE# #INDENT# CreditLine . add_credit ( deduct_amount , account = invoice . subscription . account , payment_record = payment_record ) #NEWLINE# CreditLine . add_credit ( ( - deduct_amount ) , account = invoice . subscription . account , invoice = invoice ) #NEWLINE# invoice . update_balance ( ) #NEWLINE# invoice . save ( ) #NEWLINE# #UNINDENT# #UNINDENT# if amount : #NEWLINE# #INDENT# account = BillingAccount . get_or_create_account_by_domain ( self . domain ) #NEWLINE# CreditLine . add_credit ( amount , account = account , payment_record = payment_record )