Credit note is automatically applied if you have a single invoice

This commit is contained in:
Christopher Neugebauer 2016-09-15 10:22:12 +10:00
parent 05c5cfcb4e
commit 82254a7bf5

View file

@ -198,10 +198,30 @@ class InvoiceController(ForId, object):
commerce.LineItem.objects.bulk_create(line_items) commerce.LineItem.objects.bulk_create(line_items)
cls._apply_credit_notes(invoice)
cls.email_on_invoice_creation(invoice) cls.email_on_invoice_creation(invoice)
return invoice return invoice
@classmethod
def _apply_credit_notes(cls, invoice):
''' Applies the user's credit notes to the given invoice on creation.
'''
notes = commerce.CreditNote.objects.filter(invoice__user=invoice.user)
if len(notes) == 0:
return
for note in notes:
try:
CreditNoteController(note).apply_to_invoice(invoice)
except ValidationError:
# ValidationError will get raised once we're overpaying.
break
invoice.refresh_from_db()
def can_view(self, user=None, access_code=None): def can_view(self, user=None, access_code=None):
''' Returns true if the accessing user is allowed to view this invoice, ''' Returns true if the accessing user is allowed to view this invoice,
or if the given access code matches this invoice's user's access code. or if the given access code matches this invoice's user's access code.