#NEWLINE# def _generate_for_user_with_consolidated_billing( self , customer , billing_date , force_generate ) : #NEWLINE# #INDENT# '\n        Generates the billing documents for all the subscriptions of a customer\n        who uses consolidated billing.\n        ' #NEWLINE# subs_to_bill = [ ] #NEWLINE# criteria = { #NEWLINE# #INDENT# 'state__in' : [ Subscription . STATES . ACTIVE , Subscription . STATES . CANCELED ] , #NEWLINE# #UNINDENT# } #NEWLINE# for subscription in customer . subscriptions . filter ( ** criteria ) : #NEWLINE# #INDENT# if subscription . should_be_billed ( billing_date ) : #NEWLINE# #INDENT# subs_to_bill . append ( subscription ) #NEWLINE# if ( args and ( subscription . state == Subscription . STATES . ACTIVE ) ) : #NEWLINE# #INDENT# subscription . cancel ( when = Subscription . CANCEL_OPTIONS . NOW ) #NEWLINE# subscription . save ( ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( force_generate and ( subscription . state == Subscription . STATES . CANCELED ) ) : #NEWLINE# #INDENT# subs_to_bill . append ( subscription ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( force_generate and ( subscription . state == Subscription . STATES . ACTIVE ) ) : #NEWLINE# #INDENT# subscription . cancel ( when = Subscription . CANCEL_OPTIONS . NOW ) #NEWLINE# subscription . save ( ) #NEWLINE# subs_to_bill . append ( subscription ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# cached_documents = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for subscription in subs_to_bill : #NEWLINE# #INDENT# provider = subscription . plan . provider #NEWLINE# if ( provider in cached_documents ) : #NEWLINE# #INDENT# document = cached_documents [ provider ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# document = self . _create_document ( provider , customer , subscription , billing_date ) #NEWLINE# cached_documents [ provider ] = document #NEWLINE# #UNINDENT# args = { #NEWLINE# #INDENT# 'billing_date' : billing_date , #NEWLINE# provider . flow : document , #NEWLINE# #UNINDENT# } #NEWLINE# self . _log_subscription_billing ( document , subscription ) #NEWLINE# subscription . add_total_value_to_document ( ** args ) #NEWLINE# if ( subscription . state == Subscription . STATES . CANCELED ) : #NEWLINE# #INDENT# subscription . end ( ) #NEWLINE# subscription . save ( ) #NEWLINE# #UNINDENT# #UNINDENT# for ( provider , document ) in cached_documents . iteritems ( ) : #NEWLINE# #INDENT# if ( provider . default_document_state == Provider . DEFAULT_DOC_STATE . ISSUED ) : #NEWLINE# #INDENT# document . issue ( ) #NEWLINE# document . save ( )