diff --git a/registrasion/controllers/invoice.py b/registrasion/controllers/invoice.py index 09eb02da..d4be50b1 100644 --- a/registrasion/controllers/invoice.py +++ b/registrasion/controllers/invoice.py @@ -217,7 +217,9 @@ class InvoiceController(ForId, object): if invoices.count() > 1: return - notes = commerce.CreditNote.objects.filter(invoice__user=invoice.user) + notes = commerce.CreditNote.unclaimed().filter( + invoice__user=invoice.user + ) for note in notes: try: CreditNoteController(note).apply_to_invoice(invoice) diff --git a/registrasion/tests/test_credit_note.py b/registrasion/tests/test_credit_note.py index 3e6d1cd6..2857ce7f 100644 --- a/registrasion/tests/test_credit_note.py +++ b/registrasion/tests/test_credit_note.py @@ -428,3 +428,15 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): # The credit notes are not automatically applied. self.assertEqual(0, invoice.total_payments()) self.assertTrue(invoice.invoice.is_unpaid) + + def test_credit_notes_are_applied_even_if_some_notes_are_claimed(self): + + for i in xrange(10): + # Generate credit note + invoice1 = self._manual_invoice(1) + invoice1.pay("Pay", invoice1.invoice.value) + invoice1.refund() + + # Generate invoice that should be automatically paid + invoice2 = self._manual_invoice(1) + self.assertTrue(invoice2.invoice.is_paid)