def _generate_for_user_with_consolidated_billing(self, customer,
                                                     billing_date,
                                                     force_generate):

        # Select all the active or canceled subscriptions
        subs_to_bill = []
        criteria = {'state__in': [Subscription.STATES.ACTIVE,
                                  Subscription.STATES.CANCELED]}
        for subscription in customer.subscriptions.filter(**criteria):
            if subscription.should_be_billed(billing_date):
                subs_to_bill.append(subscription)
                if (force_generate and
                    subscription.state == Subscription.STATES.ACTIVE
                ):
                    subscription.cancel(when=Subscription.CANCEL_OPTIONS.NOW)
                    subscription.save()
            elif force_generate and subscription.state == Subscription.STATES.CANCELED:
                subs_to_bill.append(subscription)
            elif force_generate and subscription.state == Subscription.STATES.ACTIVE:
                subscription.cancel(when=Subscription.CANCEL_OPTIONS.NOW)
                subscription.save()
                subs_to_bill.append(subscription)

        # For each provider there will be one invoice or proforma. The cache
        # is necessary as a certain customer might have more than one
        # subscription => all the subscriptions that belong to the same
        # provider will be added on the same invoice/proforma
        # => they are "cached".
        cached_documents = {}
        for subscription in subs_to_bill:
            provider = subscription.plan.provider
            if provider in cached_documents:
                # The BillingDocument was created beforehand, now just extract it
                # and add the new entries to the document.
                document = cached_documents[provider]
            else:
                # A BillingDocument instance does not exist for this provider
                # => create one
                document = self._create_document(provider, customer,
                                                 subscription, billing_date)
                cached_documents[provider] = document

            args = {
                'billing_date': billing_date,
                provider.flow: document,
            }

            self._log_subscription_billing(document, subscription)

            subscription.add_total_value_to_document(**args)

            if subscription.state == Subscription.STATES.CANCELED:
                subscription.end()
                subscription.save()

        for provider, document in cached_documents.iteritems():
            if provider.default_document_state == Provider.DEFAULT_DOC_STATE.ISSUED:
                document.issue()
                document.save()
Generates the billing documents for all the subscriptions of a customer
dataset/ETHPy150Open PressLabs/silver/silver/documents_generator.py/DocumentsGenerator._generate_for_user_with_consolidated_billing