Skip to content

Commit

Permalink
fix: do not generate multiple invoices (#28216)
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Nov 8, 2021
1 parent 5d46f76 commit 40b5bb1
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions erpnext/accounts/doctype/subscription/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,11 @@ def is_prepaid_to_invoice(self):
# Check invoice dates and make sure it doesn't have outstanding invoices
return getdate() >= getdate(self.current_invoice_start)

def is_current_invoice_generated(self):
def is_current_invoice_generated(self, _current_start_date=None, _current_end_date=None):
invoice = self.get_current_invoice()
_current_start_date, _current_end_date = self.update_subscription_period(date=add_days(self.current_invoice_end, 1), return_date=True)

if not (_current_start_date and _current_end_date):
_current_start_date, _current_end_date = self.update_subscription_period(date=add_days(self.current_invoice_end, 1), return_date=True)

if invoice and getdate(_current_start_date) <= getdate(invoice.posting_date) <= getdate(_current_end_date):
return True
Expand All @@ -516,13 +518,16 @@ def process_for_active(self):
2. Change the `Subscription` status to 'Past Due Date'
3. Change the `Subscription` status to 'Cancelled'
"""
if getdate() > getdate(self.current_invoice_end) and self.is_prepaid_to_invoice():
self.update_subscription_period(add_days(self.current_invoice_end, 1))

if not self.is_current_invoice_generated() and (self.is_postpaid_to_invoice() or self.is_prepaid_to_invoice()):
if not self.is_current_invoice_generated(self.current_invoice_start, self.current_invoice_end) \
and (self.is_postpaid_to_invoice() or self.is_prepaid_to_invoice()):

prorate = frappe.db.get_single_value('Subscription Settings', 'prorate')
self.generate_invoice(prorate)

if getdate() > getdate(self.current_invoice_end) and self.is_prepaid_to_invoice():
self.update_subscription_period(add_days(self.current_invoice_end, 1))

if self.cancel_at_period_end and getdate() > getdate(self.current_invoice_end):
self.cancel_subscription_at_period_end()

Expand Down Expand Up @@ -556,8 +561,10 @@ def process_for_past_due_date(self):
self.set_status_grace_period()

# Generate invoices periodically even if current invoice are unpaid
if self.generate_new_invoices_past_due_date and not self.is_current_invoice_generated() and (self.is_postpaid_to_invoice()
or self.is_prepaid_to_invoice()):
if self.generate_new_invoices_past_due_date and not \
self.is_current_invoice_generated(self.current_invoice_start, self.current_invoice_end) \
and (self.is_postpaid_to_invoice() or self.is_prepaid_to_invoice()):

prorate = frappe.db.get_single_value('Subscription Settings', 'prorate')
self.generate_invoice(prorate)

Expand Down

0 comments on commit 40b5bb1

Please sign in to comment.