@@ -56,7 +56,7 @@ def generate(self, subscription=None, billing_date=None, customers=None,
5656 :param customers: the customers for which one wants to generate the
5757 proformas/invoices.
5858 :param force_generate: if True, invoices are generated at the date
59- indicated by `billing_date` instead of the normal end of billing
59+ indicated by `billing_date` instead of after the normal end of billing
6060 cycle.
6161
6262 :note
@@ -114,7 +114,16 @@ def get_subscriptions_prepared_for_billing(self, customer, billing_date, force_g
114114 criteria = {'state__in' : [Subscription .STATES .ACTIVE ,
115115 Subscription .STATES .CANCELED ]}
116116 for subscription in customer .subscriptions .filter (** criteria ):
117- if subscription .should_be_billed (billing_date ) or force_generate :
117+ to_bill = subscription .should_be_billed (billing_date ) or force_generate
118+
119+ if not to_bill and subscription .cancel_date :
120+ billing_up_to_dates = subscription .billed_up_to_dates
121+ to_bill = (
122+ subscription .cancel_date < billing_up_to_dates ["metered_features_billed_up_to" ] and
123+ subscription .cancel_date < billing_up_to_dates ["plan_billed_up_to" ]
124+ )
125+
126+ if to_bill :
118127 subs_to_bill .append (subscription )
119128
120129 return subs_to_bill
@@ -135,7 +144,6 @@ def _bill_subscription_into_document(self, subscription, billing_date, document=
135144 })
136145
137146 billing_log , entries_info = self .add_subscription_cycles_to_document (** kwargs )
138-
139147 if subscription .state == Subscription .STATES .CANCELED :
140148 subscription .end ()
141149 subscription .save ()
@@ -382,6 +390,12 @@ def _generate_for_user_with_consolidated_billing(self, customer, billing_date, f
382390
383391 self ._create_discount_entries (** kwargs )
384392
393+ # TODO: Creating and then deleting the document in the DB is not ideal and this whole logic
394+ # should be refactored.
395+ if not document .entries .exists ():
396+ document .delete ()
397+ continue
398+
385399 if provider .default_document_state == Provider .DEFAULT_DOC_STATE .ISSUED :
386400 document .issue ()
387401
@@ -405,6 +419,12 @@ def _generate_for_user_without_consolidated_billing(self, customer, billing_date
405419
406420 self ._create_discount_entries (** kwargs )
407421
422+ # TODO: Creating and then deleting the document in the DB is not ideal and this whole logic
423+ # should be refactored.
424+ if not document .entries .exists ():
425+ document .delete ()
426+ continue
427+
408428 if provider .default_document_state == Provider .DEFAULT_DOC_STATE .ISSUED :
409429 document .issue ()
410430
@@ -419,14 +439,29 @@ def _generate_for_single_subscription(self, subscription=None, billing_date=None
419439
420440 provider = subscription .provider
421441
422- if not subscription .should_be_billed (billing_date ) or force_generate :
442+ to_bill = subscription .should_be_billed (billing_date ) or force_generate
443+
444+ if not to_bill and subscription .cancel_date :
445+ billing_up_to_dates = subscription .billed_up_to_dates
446+ to_bill = (
447+ subscription .cancel_date < billing_up_to_dates ["metered_features_billed_up_to" ] and
448+ subscription .cancel_date < billing_up_to_dates ["plan_billed_up_to" ]
449+ )
450+
451+ if not to_bill :
423452 return
424453
425454 document , discount_amounts = self ._bill_subscription_into_document (subscription , billing_date )
426455
427456 kwargs = {'entries_info' : discount_amounts ,
428457 provider .flow : document }
429458
459+ # TODO: Creating and then deleting the document in the DB is not ideal and this whole logic
460+ # should be refactored.
461+ if not document .entries .exists ():
462+ document .delete ()
463+ return
464+
430465 self ._create_discount_entries (** kwargs )
431466
432467 if provider .default_document_state == Provider .DEFAULT_DOC_STATE .ISSUED :
0 commit comments