From 5a7819b0d725413b3a2e06382207ac12e04d198b Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Sat, 3 Sep 2016 11:07:46 +1000 Subject: [PATCH 1/4] Test for issue 64 --- registrasion/tests/test_invoice.py | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/registrasion/tests/test_invoice.py b/registrasion/tests/test_invoice.py index bd8c4340..4b971f73 100644 --- a/registrasion/tests/test_invoice.py +++ b/registrasion/tests/test_invoice.py @@ -534,6 +534,44 @@ class InvoiceTestCase(RegistrationCartTestCase): with self.assertRaises(ValidationError): invoice = TestingInvoiceController.for_cart(cart.cart) + def test_invoice_with_credit_note_applied_is_refunded(self): + ''' Invoices with partial payments should void when cart is updated. + + Test for issue #64 -- applying a credit note to an invoice + means that invoice cannot be voided, and new invoices cannot be + created. ''' + + cart = TestingCartController.for_user(self.USER_1) + + cart.add_to_cart(self.PROD_1, 1) + invoice = TestingInvoiceController.for_cart(cart.cart) + + # Now get a credit note + invoice.pay("Lol", invoice.invoice.value) + invoice.refund() + cn = self._credit_note_for_invoice(invoice.invoice) + + # Create a cart of higher value than the credit note + cart = TestingCartController.for_user(self.USER_1) + cart.add_to_cart(self.PROD_1, 2) + + # Create a current invoice, and apply partial payments + invoice = TestingInvoiceController.for_cart(cart.cart) + cn.apply_to_invoice(invoice.invoice) + + # Adding to cart will mean that the old invoice for this cart + # will be invalidated. A new invoice should be generated. + cart.add_to_cart(self.PROD_1, 1) + invoice2 = TestingInvoiceController.for_cart(cart.cart) + + invoice.invoice.refresh_from_db() + self.assertEquals( + commerce.invoice.STATUS_REFUNDED, + invoice.invoice.status, + ) + + self.assertEquals(cn.credit_note.value, invoice.total_payments()) + def test_sends_email_on_invoice_creation(self): invoice = self._invoice_containing_prod_1(1) self.assertEquals(1, len(self.emails)) From 0329ee7bb2c9d63c258dd7a7e7ddf23a829802e7 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Sat, 3 Sep 2016 11:11:44 +1000 Subject: [PATCH 2/4] Amends test to test *both* paths for validating invoices. --- registrasion/tests/test_invoice.py | 1 + 1 file changed, 1 insertion(+) diff --git a/registrasion/tests/test_invoice.py b/registrasion/tests/test_invoice.py index 4b971f73..8528a708 100644 --- a/registrasion/tests/test_invoice.py +++ b/registrasion/tests/test_invoice.py @@ -562,6 +562,7 @@ class InvoiceTestCase(RegistrationCartTestCase): # Adding to cart will mean that the old invoice for this cart # will be invalidated. A new invoice should be generated. cart.add_to_cart(self.PROD_1, 1) + invoice = TestingInvoiceController.for_id(invoice.invoice.id) invoice2 = TestingInvoiceController.for_cart(cart.cart) invoice.invoice.refresh_from_db() From cdc6e229dc6529d9b03cb38aa086292ca763688d Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Sat, 3 Sep 2016 11:31:39 +1000 Subject: [PATCH 3/4] Etc (squash. srsly) --- registrasion/tests/test_invoice.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/registrasion/tests/test_invoice.py b/registrasion/tests/test_invoice.py index 8528a708..57e51adc 100644 --- a/registrasion/tests/test_invoice.py +++ b/registrasion/tests/test_invoice.py @@ -565,13 +565,19 @@ class InvoiceTestCase(RegistrationCartTestCase): invoice = TestingInvoiceController.for_id(invoice.invoice.id) invoice2 = TestingInvoiceController.for_cart(cart.cart) - invoice.invoice.refresh_from_db() + invoice._refresh() + + # The first invoice should be refunded self.assertEquals( commerce.invoice.STATUS_REFUNDED, invoice.invoice.status, ) - self.assertEquals(cn.credit_note.value, invoice.total_payments()) + # The credit note should be equal to the payments value of first inv + self.assertEquals( + cn.credit_note.value, + invoice.total_payments(), + ) def test_sends_email_on_invoice_creation(self): invoice = self._invoice_containing_prod_1(1) From 1e6c90163dd06cec9348e49174d33e4d751a8f62 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Sat, 3 Sep 2016 11:46:24 +1000 Subject: [PATCH 4/4] Fixes #64 --- registrasion/controllers/invoice.py | 12 ++++++++---- registrasion/tests/test_invoice.py | 7 ++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/registrasion/controllers/invoice.py b/registrasion/controllers/invoice.py index 616f418d..2c69faed 100644 --- a/registrasion/controllers/invoice.py +++ b/registrasion/controllers/invoice.py @@ -43,16 +43,16 @@ class InvoiceController(ForId, object): cart_controller = CartController(cart) cart_controller.validate_cart() # Raises ValidationError on fail. - cls.void_all_invoices(cart) + cls.update_old_invoices(cart) invoice = cls._generate(cart) return cls(invoice) @classmethod - def void_all_invoices(cls, cart): + def update_old_invoices(cls, cart): invoices = commerce.Invoice.objects.filter(cart=cart).all() for invoice in invoices: - cls(invoice).void() + cls(invoice).update_status() @classmethod def resolve_discount_value(cls, item): @@ -299,7 +299,11 @@ class InvoiceController(ForId, object): def update_validity(self): ''' Voids this invoice if the cart it is attached to has updated. ''' if not self._invoice_matches_cart(): - self.void() + if self.total_payments() > 0: + # Free up the payments made to this invoice + self.refund() + else: + self.void() def void(self): ''' Voids the invoice if it is valid to do so. ''' diff --git a/registrasion/tests/test_invoice.py b/registrasion/tests/test_invoice.py index 57e51adc..6d36d082 100644 --- a/registrasion/tests/test_invoice.py +++ b/registrasion/tests/test_invoice.py @@ -564,19 +564,20 @@ class InvoiceTestCase(RegistrationCartTestCase): cart.add_to_cart(self.PROD_1, 1) invoice = TestingInvoiceController.for_id(invoice.invoice.id) invoice2 = TestingInvoiceController.for_cart(cart.cart) + cn2 = self._credit_note_for_invoice(invoice.invoice) invoice._refresh() # The first invoice should be refunded self.assertEquals( - commerce.invoice.STATUS_REFUNDED, + commerce.Invoice.STATUS_VOID, invoice.invoice.status, ) - # The credit note should be equal to the payments value of first inv + # Both credit notes should be for the same amount self.assertEquals( cn.credit_note.value, - invoice.total_payments(), + cn2.credit_note.value, ) def test_sends_email_on_invoice_creation(self):