From 82254a7bf513fc41237d4623b4eaf08ca6f5958c Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Thu, 15 Sep 2016 10:22:12 +1000 Subject: [PATCH] Credit note is automatically applied if you have a single invoice --- registrasion/controllers/invoice.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/registrasion/controllers/invoice.py b/registrasion/controllers/invoice.py index a2adca4e..9fadcd11 100644 --- a/registrasion/controllers/invoice.py +++ b/registrasion/controllers/invoice.py @@ -198,10 +198,30 @@ class InvoiceController(ForId, object): commerce.LineItem.objects.bulk_create(line_items) + cls._apply_credit_notes(invoice) cls.email_on_invoice_creation(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): ''' 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.