From 23658be49a76f46021a77ff83d3ad6c536b4e46e Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Thu, 15 Sep 2016 09:29:31 +1000 Subject: [PATCH 1/7] Starts test_helpers.py, so we can get credit note testing stuff into its own module. --- registrasion/tests/test_helpers.py | 11 +++++++++++ registrasion/tests/test_invoice.py | 13 ++----------- 2 files changed, 13 insertions(+), 11 deletions(-) create mode 100644 registrasion/tests/test_helpers.py diff --git a/registrasion/tests/test_helpers.py b/registrasion/tests/test_helpers.py new file mode 100644 index 00000000..cdd23de2 --- /dev/null +++ b/registrasion/tests/test_helpers.py @@ -0,0 +1,11 @@ +class TestHelperMixin(object): + + def _invoice_containing_prod_1(self, qty=1): + cart = TestingCartController.for_user(self.USER_1) + cart.add_to_cart(self.PROD_1, qty) + + return TestingInvoiceController.for_cart(self.reget(cart.cart)) + + def _credit_note_for_invoice(self, invoice): + note = commerce.CreditNote.objects.get(invoice=invoice) + return TestingCreditNoteController(note) diff --git a/registrasion/tests/test_invoice.py b/registrasion/tests/test_invoice.py index 4f73bf3c..334db137 100644 --- a/registrasion/tests/test_invoice.py +++ b/registrasion/tests/test_invoice.py @@ -10,23 +10,14 @@ from registrasion.models import inventory from controller_helpers import TestingCartController from controller_helpers import TestingCreditNoteController from controller_helpers import TestingInvoiceController +from test_helpers import TestHelperMixin from test_cart import RegistrationCartTestCase UTC = pytz.timezone('UTC') -class InvoiceTestCase(RegistrationCartTestCase): - - def _invoice_containing_prod_1(self, qty=1): - cart = TestingCartController.for_user(self.USER_1) - cart.add_to_cart(self.PROD_1, qty) - - return TestingInvoiceController.for_cart(self.reget(cart.cart)) - - def _credit_note_for_invoice(self, invoice): - note = commerce.CreditNote.objects.get(invoice=invoice) - return TestingCreditNoteController(note) +class InvoiceTestCase(TestHelperMixin, RegistrationCartTestCase): def test_create_invoice(self): current_cart = TestingCartController.for_user(self.USER_1) From 66f423eafac09d89f7e4586ae019acbaba34aa59 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Thu, 15 Sep 2016 09:38:42 +1000 Subject: [PATCH 2/7] Moves tests for credit note functionality into its own test module --- registrasion/tests/test_credit_note.py | 328 +++++++++++++++++++++++++ registrasion/tests/test_helpers.py | 6 + registrasion/tests/test_invoice.py | 308 ----------------------- 3 files changed, 334 insertions(+), 308 deletions(-) create mode 100644 registrasion/tests/test_credit_note.py diff --git a/registrasion/tests/test_credit_note.py b/registrasion/tests/test_credit_note.py new file mode 100644 index 00000000..c204cfd4 --- /dev/null +++ b/registrasion/tests/test_credit_note.py @@ -0,0 +1,328 @@ +import datetime +import pytz + +from decimal import Decimal +from django.core.exceptions import ValidationError + +from registrasion.models import commerce +from registrasion.models import conditions +from registrasion.models import inventory +from controller_helpers import TestingCartController +from controller_helpers import TestingCreditNoteController +from controller_helpers import TestingInvoiceController +from test_helpers import TestHelperMixin + +from test_cart import RegistrationCartTestCase + +UTC = pytz.timezone('UTC') + + +class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): + + def test_overpaid_invoice_results_in_credit_note(self): + invoice = self._invoice_containing_prod_1(1) + + # Invoice is overpaid by 1 unit + to_pay = invoice.invoice.value + 1 + invoice.pay("Reference", to_pay) + + # The total paid should be equal to the value of the invoice only + self.assertEqual(invoice.invoice.value, invoice.total_payments()) + self.assertTrue(invoice.invoice.is_paid) + + # There should be a credit note generated out of the invoice. + credit_notes = commerce.CreditNote.objects.filter( + invoice=invoice.invoice, + ) + self.assertEqual(1, credit_notes.count()) + self.assertEqual(to_pay - invoice.invoice.value, credit_notes[0].value) + + def test_full_paid_invoice_does_not_generate_credit_note(self): + invoice = self._invoice_containing_prod_1(1) + + # Invoice is paid evenly + invoice.pay("Reference", invoice.invoice.value) + + # The total paid should be equal to the value of the invoice only + self.assertEqual(invoice.invoice.value, invoice.total_payments()) + self.assertTrue(invoice.invoice.is_paid) + + # There should be no credit notes + credit_notes = commerce.CreditNote.objects.filter( + invoice=invoice.invoice, + ) + self.assertEqual(0, credit_notes.count()) + + def test_refund_partially_paid_invoice_generates_correct_credit_note(self): + invoice = self._invoice_containing_prod_1(1) + + # Invoice is underpaid by 1 unit + to_pay = invoice.invoice.value - 1 + invoice.pay("Reference", to_pay) + invoice.refund() + + # The total paid should be zero + self.assertEqual(0, invoice.total_payments()) + self.assertTrue(invoice.invoice.is_void) + + # There should be a credit note generated out of the invoice. + credit_notes = commerce.CreditNote.objects.filter( + invoice=invoice.invoice, + ) + self.assertEqual(1, credit_notes.count()) + self.assertEqual(to_pay, credit_notes[0].value) + + def test_refund_fully_paid_invoice_generates_correct_credit_note(self): + invoice = self._invoice_containing_prod_1(1) + + to_pay = invoice.invoice.value + invoice.pay("Reference", to_pay) + self.assertTrue(invoice.invoice.is_paid) + + invoice.refund() + + # The total paid should be zero + self.assertEqual(0, invoice.total_payments()) + self.assertTrue(invoice.invoice.is_refunded) + + # There should be a credit note generated out of the invoice. + credit_notes = commerce.CreditNote.objects.filter( + invoice=invoice.invoice, + ) + self.assertEqual(1, credit_notes.count()) + self.assertEqual(to_pay, credit_notes[0].value) + + def test_apply_credit_note_pays_invoice(self): + invoice = self._invoice_containing_prod_1(1) + + to_pay = invoice.invoice.value + invoice.pay("Reference", to_pay) + self.assertTrue(invoice.invoice.is_paid) + + invoice.refund() + + # There should be one credit note generated out of the invoice. + cn = self._credit_note_for_invoice(invoice.invoice) + + # That credit note should be in the unclaimed pile + self.assertEquals(1, commerce.CreditNote.unclaimed().count()) + + # Create a new (identical) cart with invoice + cart = TestingCartController.for_user(self.USER_1) + cart.add_to_cart(self.PROD_1, 1) + + invoice2 = TestingInvoiceController.for_cart(self.reget(cart.cart)) + + cn.apply_to_invoice(invoice2.invoice) + self.assertTrue(invoice2.invoice.is_paid) + + # That invoice should not show up as unclaimed any more + self.assertEquals(0, commerce.CreditNote.unclaimed().count()) + + def test_apply_credit_note_generates_new_credit_note_if_overpaying(self): + invoice = self._invoice_containing_prod_1(2) + + to_pay = invoice.invoice.value + invoice.pay("Reference", to_pay) + self.assertTrue(invoice.invoice.is_paid) + + invoice.refund() + + # There should be one credit note generated out of the invoice. + cn = self._credit_note_for_invoice(invoice.invoice) + + self.assertEquals(1, commerce.CreditNote.unclaimed().count()) + + # Create a new cart (of half value of inv 1) and get invoice + cart = TestingCartController.for_user(self.USER_1) + cart.add_to_cart(self.PROD_1, 1) + + invoice2 = TestingInvoiceController.for_cart(self.reget(cart.cart)) + + cn.apply_to_invoice(invoice2.invoice) + self.assertTrue(invoice2.invoice.is_paid) + + # We generated a new credit note, and spent the old one, + # unclaimed should still be 1. + self.assertEquals(1, commerce.CreditNote.unclaimed().count()) + + credit_note2 = commerce.CreditNote.objects.get( + invoice=invoice2.invoice, + ) + + # The new credit note should be the residual of the cost of cart 1 + # minus the cost of cart 2. + self.assertEquals( + invoice.invoice.value - invoice2.invoice.value, + credit_note2.value, + ) + + def test_cannot_apply_credit_note_on_invalid_invoices(self): + invoice = self._invoice_containing_prod_1(1) + + to_pay = invoice.invoice.value + invoice.pay("Reference", to_pay) + self.assertTrue(invoice.invoice.is_paid) + + invoice.refund() + + # There should be one credit note generated out of the invoice. + cn = self._credit_note_for_invoice(invoice.invoice) + + # Create a new cart with invoice, pay it + invoice_2 = self._invoice_containing_prod_1(1) + invoice_2.pay("LOL", invoice_2.invoice.value) + + # Cannot pay paid invoice + with self.assertRaises(ValidationError): + cn.apply_to_invoice(invoice_2.invoice) + + invoice_2.refund() + # Cannot pay refunded invoice + with self.assertRaises(ValidationError): + cn.apply_to_invoice(invoice_2.invoice) + + # Create a new cart with invoice + invoice_2 = self._invoice_containing_prod_1(1) + invoice_2.void() + # Cannot pay void invoice + with self.assertRaises(ValidationError): + cn.apply_to_invoice(invoice_2.invoice) + + def test_cannot_apply_a_refunded_credit_note(self): + invoice = self._invoice_containing_prod_1(1) + + to_pay = invoice.invoice.value + invoice.pay("Reference", to_pay) + self.assertTrue(invoice.invoice.is_paid) + + invoice.refund() + + self.assertEquals(1, commerce.CreditNote.unclaimed().count()) + + cn = self._credit_note_for_invoice(invoice.invoice) + + cn.refund() + + # Refunding a credit note should mark it as claimed + self.assertEquals(0, commerce.CreditNote.unclaimed().count()) + + # Create a new cart with invoice + cart = TestingCartController.for_user(self.USER_1) + cart.add_to_cart(self.PROD_1, 1) + + invoice_2 = TestingInvoiceController.for_cart(self.reget(cart.cart)) + + # Cannot pay with this credit note. + with self.assertRaises(ValidationError): + cn.apply_to_invoice(invoice_2.invoice) + + def test_cannot_refund_an_applied_credit_note(self): + invoice = self._invoice_containing_prod_1(1) + + to_pay = invoice.invoice.value + invoice.pay("Reference", to_pay) + self.assertTrue(invoice.invoice.is_paid) + + invoice.refund() + + self.assertEquals(1, commerce.CreditNote.unclaimed().count()) + + cn = self._credit_note_for_invoice(invoice.invoice) + + # Create a new cart with invoice + cart = TestingCartController.for_user(self.USER_1) + cart.add_to_cart(self.PROD_1, 1) + + invoice_2 = TestingInvoiceController.for_cart(self.reget(cart.cart)) + cn.apply_to_invoice(invoice_2.invoice) + + self.assertEquals(0, commerce.CreditNote.unclaimed().count()) + + # Cannot refund this credit note as it is already applied. + with self.assertRaises(ValidationError): + cn.refund() + + def test_money_into_void_invoice_generates_credit_note(self): + invoice = self._invoice_containing_prod_1(1) + invoice.void() + + val = invoice.invoice.value + + invoice.pay("Paying into the void.", val, pre_validate=False) + cn = self._credit_note_for_invoice(invoice.invoice) + self.assertEqual(val, cn.credit_note.value) + + def test_money_into_refunded_invoice_generates_credit_note(self): + invoice = self._invoice_containing_prod_1(1) + + val = invoice.invoice.value + + invoice.pay("Paying the first time.", val) + invoice.refund() + + cnval = val - 1 + invoice.pay("Paying into the void.", cnval, pre_validate=False) + + notes = commerce.CreditNote.objects.filter(invoice=invoice.invoice) + notes = sorted(notes, key=lambda note: note.value) + + self.assertEqual(cnval, notes[0].value) + self.assertEqual(val, notes[1].value) + + def test_money_into_paid_invoice_generates_credit_note(self): + invoice = self._invoice_containing_prod_1(1) + + val = invoice.invoice.value + + invoice.pay("Paying the first time.", val) + + invoice.pay("Paying into the void.", val, pre_validate=False) + cn = self._credit_note_for_invoice(invoice.invoice) + self.assertEqual(val, cn.credit_note.value) + + 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) + 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_VOID, + invoice.invoice.status, + ) + + # Both credit notes should be for the same amount + self.assertEquals( + cn.credit_note.value, + cn2.credit_note.value, + ) diff --git a/registrasion/tests/test_helpers.py b/registrasion/tests/test_helpers.py index cdd23de2..6ef5c1d2 100644 --- a/registrasion/tests/test_helpers.py +++ b/registrasion/tests/test_helpers.py @@ -1,3 +1,9 @@ +from registrasion.models import commerce + +from controller_helpers import TestingCartController +from controller_helpers import TestingCreditNoteController +from controller_helpers import TestingInvoiceController + class TestHelperMixin(object): def _invoice_containing_prod_1(self, qty=1): diff --git a/registrasion/tests/test_invoice.py b/registrasion/tests/test_invoice.py index 334db137..6ae662a3 100644 --- a/registrasion/tests/test_invoice.py +++ b/registrasion/tests/test_invoice.py @@ -229,268 +229,6 @@ class InvoiceTestCase(TestHelperMixin, RegistrationCartTestCase): with self.assertRaises(ValidationError): invoice.validate_allowed_to_pay() - def test_overpaid_invoice_results_in_credit_note(self): - invoice = self._invoice_containing_prod_1(1) - - # Invoice is overpaid by 1 unit - to_pay = invoice.invoice.value + 1 - invoice.pay("Reference", to_pay) - - # The total paid should be equal to the value of the invoice only - self.assertEqual(invoice.invoice.value, invoice.total_payments()) - self.assertTrue(invoice.invoice.is_paid) - - # There should be a credit note generated out of the invoice. - credit_notes = commerce.CreditNote.objects.filter( - invoice=invoice.invoice, - ) - self.assertEqual(1, credit_notes.count()) - self.assertEqual(to_pay - invoice.invoice.value, credit_notes[0].value) - - def test_full_paid_invoice_does_not_generate_credit_note(self): - invoice = self._invoice_containing_prod_1(1) - - # Invoice is paid evenly - invoice.pay("Reference", invoice.invoice.value) - - # The total paid should be equal to the value of the invoice only - self.assertEqual(invoice.invoice.value, invoice.total_payments()) - self.assertTrue(invoice.invoice.is_paid) - - # There should be no credit notes - credit_notes = commerce.CreditNote.objects.filter( - invoice=invoice.invoice, - ) - self.assertEqual(0, credit_notes.count()) - - def test_refund_partially_paid_invoice_generates_correct_credit_note(self): - invoice = self._invoice_containing_prod_1(1) - - # Invoice is underpaid by 1 unit - to_pay = invoice.invoice.value - 1 - invoice.pay("Reference", to_pay) - invoice.refund() - - # The total paid should be zero - self.assertEqual(0, invoice.total_payments()) - self.assertTrue(invoice.invoice.is_void) - - # There should be a credit note generated out of the invoice. - credit_notes = commerce.CreditNote.objects.filter( - invoice=invoice.invoice, - ) - self.assertEqual(1, credit_notes.count()) - self.assertEqual(to_pay, credit_notes[0].value) - - def test_refund_fully_paid_invoice_generates_correct_credit_note(self): - invoice = self._invoice_containing_prod_1(1) - - to_pay = invoice.invoice.value - invoice.pay("Reference", to_pay) - self.assertTrue(invoice.invoice.is_paid) - - invoice.refund() - - # The total paid should be zero - self.assertEqual(0, invoice.total_payments()) - self.assertTrue(invoice.invoice.is_refunded) - - # There should be a credit note generated out of the invoice. - credit_notes = commerce.CreditNote.objects.filter( - invoice=invoice.invoice, - ) - self.assertEqual(1, credit_notes.count()) - self.assertEqual(to_pay, credit_notes[0].value) - - def test_apply_credit_note_pays_invoice(self): - invoice = self._invoice_containing_prod_1(1) - - to_pay = invoice.invoice.value - invoice.pay("Reference", to_pay) - self.assertTrue(invoice.invoice.is_paid) - - invoice.refund() - - # There should be one credit note generated out of the invoice. - cn = self._credit_note_for_invoice(invoice.invoice) - - # That credit note should be in the unclaimed pile - self.assertEquals(1, commerce.CreditNote.unclaimed().count()) - - # Create a new (identical) cart with invoice - cart = TestingCartController.for_user(self.USER_1) - cart.add_to_cart(self.PROD_1, 1) - - invoice2 = TestingInvoiceController.for_cart(self.reget(cart.cart)) - - cn.apply_to_invoice(invoice2.invoice) - self.assertTrue(invoice2.invoice.is_paid) - - # That invoice should not show up as unclaimed any more - self.assertEquals(0, commerce.CreditNote.unclaimed().count()) - - def test_apply_credit_note_generates_new_credit_note_if_overpaying(self): - invoice = self._invoice_containing_prod_1(2) - - to_pay = invoice.invoice.value - invoice.pay("Reference", to_pay) - self.assertTrue(invoice.invoice.is_paid) - - invoice.refund() - - # There should be one credit note generated out of the invoice. - cn = self._credit_note_for_invoice(invoice.invoice) - - self.assertEquals(1, commerce.CreditNote.unclaimed().count()) - - # Create a new cart (of half value of inv 1) and get invoice - cart = TestingCartController.for_user(self.USER_1) - cart.add_to_cart(self.PROD_1, 1) - - invoice2 = TestingInvoiceController.for_cart(self.reget(cart.cart)) - - cn.apply_to_invoice(invoice2.invoice) - self.assertTrue(invoice2.invoice.is_paid) - - # We generated a new credit note, and spent the old one, - # unclaimed should still be 1. - self.assertEquals(1, commerce.CreditNote.unclaimed().count()) - - credit_note2 = commerce.CreditNote.objects.get( - invoice=invoice2.invoice, - ) - - # The new credit note should be the residual of the cost of cart 1 - # minus the cost of cart 2. - self.assertEquals( - invoice.invoice.value - invoice2.invoice.value, - credit_note2.value, - ) - - def test_cannot_apply_credit_note_on_invalid_invoices(self): - invoice = self._invoice_containing_prod_1(1) - - to_pay = invoice.invoice.value - invoice.pay("Reference", to_pay) - self.assertTrue(invoice.invoice.is_paid) - - invoice.refund() - - # There should be one credit note generated out of the invoice. - cn = self._credit_note_for_invoice(invoice.invoice) - - # Create a new cart with invoice, pay it - invoice_2 = self._invoice_containing_prod_1(1) - invoice_2.pay("LOL", invoice_2.invoice.value) - - # Cannot pay paid invoice - with self.assertRaises(ValidationError): - cn.apply_to_invoice(invoice_2.invoice) - - invoice_2.refund() - # Cannot pay refunded invoice - with self.assertRaises(ValidationError): - cn.apply_to_invoice(invoice_2.invoice) - - # Create a new cart with invoice - invoice_2 = self._invoice_containing_prod_1(1) - invoice_2.void() - # Cannot pay void invoice - with self.assertRaises(ValidationError): - cn.apply_to_invoice(invoice_2.invoice) - - def test_cannot_apply_a_refunded_credit_note(self): - invoice = self._invoice_containing_prod_1(1) - - to_pay = invoice.invoice.value - invoice.pay("Reference", to_pay) - self.assertTrue(invoice.invoice.is_paid) - - invoice.refund() - - self.assertEquals(1, commerce.CreditNote.unclaimed().count()) - - cn = self._credit_note_for_invoice(invoice.invoice) - - cn.refund() - - # Refunding a credit note should mark it as claimed - self.assertEquals(0, commerce.CreditNote.unclaimed().count()) - - # Create a new cart with invoice - cart = TestingCartController.for_user(self.USER_1) - cart.add_to_cart(self.PROD_1, 1) - - invoice_2 = TestingInvoiceController.for_cart(self.reget(cart.cart)) - - # Cannot pay with this credit note. - with self.assertRaises(ValidationError): - cn.apply_to_invoice(invoice_2.invoice) - - def test_cannot_refund_an_applied_credit_note(self): - invoice = self._invoice_containing_prod_1(1) - - to_pay = invoice.invoice.value - invoice.pay("Reference", to_pay) - self.assertTrue(invoice.invoice.is_paid) - - invoice.refund() - - self.assertEquals(1, commerce.CreditNote.unclaimed().count()) - - cn = self._credit_note_for_invoice(invoice.invoice) - - # Create a new cart with invoice - cart = TestingCartController.for_user(self.USER_1) - cart.add_to_cart(self.PROD_1, 1) - - invoice_2 = TestingInvoiceController.for_cart(self.reget(cart.cart)) - cn.apply_to_invoice(invoice_2.invoice) - - self.assertEquals(0, commerce.CreditNote.unclaimed().count()) - - # Cannot refund this credit note as it is already applied. - with self.assertRaises(ValidationError): - cn.refund() - - def test_money_into_void_invoice_generates_credit_note(self): - invoice = self._invoice_containing_prod_1(1) - invoice.void() - - val = invoice.invoice.value - - invoice.pay("Paying into the void.", val, pre_validate=False) - cn = self._credit_note_for_invoice(invoice.invoice) - self.assertEqual(val, cn.credit_note.value) - - def test_money_into_refunded_invoice_generates_credit_note(self): - invoice = self._invoice_containing_prod_1(1) - - val = invoice.invoice.value - - invoice.pay("Paying the first time.", val) - invoice.refund() - - cnval = val - 1 - invoice.pay("Paying into the void.", cnval, pre_validate=False) - - notes = commerce.CreditNote.objects.filter(invoice=invoice.invoice) - notes = sorted(notes, key=lambda note: note.value) - - self.assertEqual(cnval, notes[0].value) - self.assertEqual(val, notes[1].value) - - def test_money_into_paid_invoice_generates_credit_note(self): - invoice = self._invoice_containing_prod_1(1) - - val = invoice.invoice.value - - invoice.pay("Paying the first time.", val) - - invoice.pay("Paying into the void.", val, pre_validate=False) - cn = self._credit_note_for_invoice(invoice.invoice) - self.assertEqual(val, cn.credit_note.value) - def test_required_category_constraints_prevent_invoicing(self): self.CAT_1.required = True self.CAT_1.save() @@ -525,52 +263,6 @@ class InvoiceTestCase(TestHelperMixin, 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) - 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_VOID, - invoice.invoice.status, - ) - - # Both credit notes should be for the same amount - self.assertEquals( - cn.credit_note.value, - cn2.credit_note.value, - ) - def test_can_generate_manual_invoice(self): description_price_pairs = [ From 05c5cfcb4e8e8a188ed1839cd722a0a82d79e52f Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Thu, 15 Sep 2016 10:03:27 +1000 Subject: [PATCH 3/7] Adds first tests for automatic credit note application --- registrasion/tests/test_credit_note.py | 37 +++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/registrasion/tests/test_credit_note.py b/registrasion/tests/test_credit_note.py index c204cfd4..ce704652 100644 --- a/registrasion/tests/test_credit_note.py +++ b/registrasion/tests/test_credit_note.py @@ -288,10 +288,7 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): 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) + invoice = self._invoice_containing_prod_1(1) # Now get a credit note invoice.pay("Lol", invoice.invoice.value) @@ -326,3 +323,35 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): cn.credit_note.value, cn2.credit_note.value, ) + + def test_creating_invoice_automatically_applies_credit_note(self): + ''' Single credit note is automatically applied to new invoices. ''' + + invoice = self._invoice_containing_prod_1(1) + invoice.pay("boop", invoice.invoice.value) + invoice.refund() + + # Generate a new invoice to the same value as first invoice + # Should be paid, because we're applying credit notes automatically + invoice2 = self._invoice_containing_prod_1(1) + self.assertTrue(invoice2.invoice.is_paid) + + def test_mutiple_credit_notes_are_applied_when_generating_invoice_1(self): + ''' Tests (1) that multiple credit notes are applied to new invoice. + + Sum of credit note values will be *LESS* than the new invoice. + ''' + + raise NotImplementedError() + + def test_mutiple_credit_notes_are_applied_when_generating_invoice_2(self): + ''' Tests (2) that multiple credit notes are applied to new invoice. + + Sum of credit note values will be *GREATER* than the new invoice. + ''' + + raise NotImplementedError() + + def test_credit_notes_are_not_applied_if_user_has_multiple_invoices(self): + + raise NotImplementedError() From 82254a7bf513fc41237d4623b4eaf08ca6f5958c Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Thu, 15 Sep 2016 10:22:12 +1000 Subject: [PATCH 4/7] 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. From 04b7a7998c964c9e3793a78a695c4de8f6bcbd0c Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Thu, 15 Sep 2016 10:32:22 +1000 Subject: [PATCH 5/7] Tests correct behaviour when there are multiple credit notes to be applied --- registrasion/controllers/invoice.py | 11 +++- registrasion/tests/test_credit_note.py | 81 +++++++++++++++++++++++++- 2 files changed, 86 insertions(+), 6 deletions(-) diff --git a/registrasion/controllers/invoice.py b/registrasion/controllers/invoice.py index 9fadcd11..09eb02da 100644 --- a/registrasion/controllers/invoice.py +++ b/registrasion/controllers/invoice.py @@ -208,11 +208,16 @@ class InvoiceController(ForId, object): ''' 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: + # We only automatically apply credit notes if this is the *only* + # unpaid invoice for this user. + invoices = commerce.Invoice.objects.filter( + user=invoice.user, + status=commerce.Invoice.STATUS_UNPAID, + ) + if invoices.count() > 1: return + notes = commerce.CreditNote.objects.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 ce704652..d725048b 100644 --- a/registrasion/tests/test_credit_note.py +++ b/registrasion/tests/test_credit_note.py @@ -336,13 +336,37 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): invoice2 = self._invoice_containing_prod_1(1) self.assertTrue(invoice2.invoice.is_paid) + def _generate_multiple_credit_notes(self): + items = [("Item 1", 5), ("Item 2", 6)] + due = datetime.timedelta(hours=1) + inv1 = TestingInvoiceController.manual_invoice(self.USER_1, due, items) + inv2 = TestingInvoiceController.manual_invoice(self.USER_1, due, items) + invoice1 = TestingInvoiceController(inv1) + invoice1.pay("Pay", inv1.value) + invoice1.refund() + invoice2 = TestingInvoiceController(inv2) + invoice2.pay("Pay", inv2.value) + invoice2.refund() + return inv1.value + inv2.value + def test_mutiple_credit_notes_are_applied_when_generating_invoice_1(self): ''' Tests (1) that multiple credit notes are applied to new invoice. Sum of credit note values will be *LESS* than the new invoice. ''' - raise NotImplementedError() + notes_value = self._generate_multiple_credit_notes() + item = [("Item", notes_value + 1)] + due = datetime.timedelta(hours=1) + inv = TestingInvoiceController.manual_invoice(self.USER_1, due, item) + invoice = TestingInvoiceController(inv) + + self.assertEqual(notes_value, invoice.total_payments()) + self.assertTrue(invoice.invoice.is_unpaid) + + user_unclaimed = commerce.CreditNote.unclaimed() + user_unclaimed = user_unclaimed.filter(invoice__user=self.USER_1) + self.assertEqual(0, user_unclaimed.count()) def test_mutiple_credit_notes_are_applied_when_generating_invoice_2(self): ''' Tests (2) that multiple credit notes are applied to new invoice. @@ -350,8 +374,59 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): Sum of credit note values will be *GREATER* than the new invoice. ''' - raise NotImplementedError() + notes_value = self._generate_multiple_credit_notes() + item = [("Item", notes_value - 1)] + due = datetime.timedelta(hours=1) + inv = TestingInvoiceController.manual_invoice(self.USER_1, due, item) + invoice = TestingInvoiceController(inv) + + self.assertEqual(notes_value - 1, invoice.total_payments()) + self.assertTrue(invoice.invoice.is_paid) + + user_unclaimed = commerce.CreditNote.unclaimed().filter( + invoice__user=self.USER_1 + ) + self.assertEqual(1, user_unclaimed.count()) + + excess = self._credit_note_for_invoice(invoice.invoice) + self.assertEqual(excess.credit_note.value, 1) + + def test_credit_notes_are_left_over_if_not_all_are_needed(self): + ''' Tests that excess credit notes are untouched if they're not needed + ''' + + notes_value = self._generate_multiple_credit_notes() + notes_old = commerce.CreditNote.unclaimed().filter( + invoice__user=self.USER_1 + ) + + # Create a manual invoice whose value is smaller than any of the + # credit notes we created + item = [("Item", 1)] + due = datetime.timedelta(hours=1) + inv = TestingInvoiceController.manual_invoice(self.USER_1, due, item) + + notes_new = commerce.CreditNote.unclaimed().filter( + invoice__user=self.USER_1 + ) + + # Item is True if the note was't consumed when generating invoice. + note_was_unused = [(i in notes_old) for i in notes_new] + self.assertIn(True, note_was_unused) def test_credit_notes_are_not_applied_if_user_has_multiple_invoices(self): - raise NotImplementedError() + # Have an invoice pending with no credit notes; no payment will be made + invoice1 = self._invoice_containing_prod_1(1) + # Create some credit notes. + self._generate_multiple_credit_notes() + + item = [("Item", notes_value)] + due = datetime.timedelta(hours=1) + inv = TestingInvoiceController.manual_invoice(self.USER_1, due, item) + invoice = TestingInvoiceController(inv) + + # Because there's already an invoice open for this user + # The credit notes are not automatically applied. + self.assertEqual(0, invoice.total_payments()) + self.assertTrue(invoice.invoice.is_unpaid) From 5fce13d3862fda20ff7d2fda4852e518e34d44b1 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Thu, 15 Sep 2016 10:55:07 +1000 Subject: [PATCH 6/7] Simplifies credit note tests --- registrasion/tests/test_credit_note.py | 37 +++++++++----------------- registrasion/tests/test_helpers.py | 9 +++++++ 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/registrasion/tests/test_credit_note.py b/registrasion/tests/test_credit_note.py index d725048b..6b2c66ac 100644 --- a/registrasion/tests/test_credit_note.py +++ b/registrasion/tests/test_credit_note.py @@ -16,6 +16,8 @@ from test_cart import RegistrationCartTestCase UTC = pytz.timezone('UTC') +HOURS = datetime.timedelta(hours=1) + class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): @@ -337,17 +339,13 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): self.assertTrue(invoice2.invoice.is_paid) def _generate_multiple_credit_notes(self): - items = [("Item 1", 5), ("Item 2", 6)] - due = datetime.timedelta(hours=1) - inv1 = TestingInvoiceController.manual_invoice(self.USER_1, due, items) - inv2 = TestingInvoiceController.manual_invoice(self.USER_1, due, items) - invoice1 = TestingInvoiceController(inv1) - invoice1.pay("Pay", inv1.value) + invoice1 = self._manual_invoice(11) + invoice2 = self._manual_invoice(11) + invoice1.pay("Pay", invoice1.invoice.value) invoice1.refund() - invoice2 = TestingInvoiceController(inv2) - invoice2.pay("Pay", inv2.value) + invoice2.pay("Pay", invoice2.invoice.value) invoice2.refund() - return inv1.value + inv2.value + return invoice1.invoice.value + invoice2.invoice.value def test_mutiple_credit_notes_are_applied_when_generating_invoice_1(self): ''' Tests (1) that multiple credit notes are applied to new invoice. @@ -356,10 +354,7 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): ''' notes_value = self._generate_multiple_credit_notes() - item = [("Item", notes_value + 1)] - due = datetime.timedelta(hours=1) - inv = TestingInvoiceController.manual_invoice(self.USER_1, due, item) - invoice = TestingInvoiceController(inv) + invoice = self._manual_invoice(notes_value + 1) self.assertEqual(notes_value, invoice.total_payments()) self.assertTrue(invoice.invoice.is_unpaid) @@ -375,10 +370,8 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): ''' notes_value = self._generate_multiple_credit_notes() - item = [("Item", notes_value - 1)] - due = datetime.timedelta(hours=1) - inv = TestingInvoiceController.manual_invoice(self.USER_1, due, item) - invoice = TestingInvoiceController(inv) + invoice = self._manual_invoice(notes_value - 1) + self.assertEqual(notes_value - 1, invoice.total_payments()) self.assertTrue(invoice.invoice.is_paid) @@ -402,10 +395,7 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): # Create a manual invoice whose value is smaller than any of the # credit notes we created - item = [("Item", 1)] - due = datetime.timedelta(hours=1) - inv = TestingInvoiceController.manual_invoice(self.USER_1, due, item) - + invoice = self._manual_invoice(1) notes_new = commerce.CreditNote.unclaimed().filter( invoice__user=self.USER_1 ) @@ -421,10 +411,7 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): # Create some credit notes. self._generate_multiple_credit_notes() - item = [("Item", notes_value)] - due = datetime.timedelta(hours=1) - inv = TestingInvoiceController.manual_invoice(self.USER_1, due, item) - invoice = TestingInvoiceController(inv) + invoice = self._manual_invoice(2) # Because there's already an invoice open for this user # The credit notes are not automatically applied. diff --git a/registrasion/tests/test_helpers.py b/registrasion/tests/test_helpers.py index 6ef5c1d2..c656a2d0 100644 --- a/registrasion/tests/test_helpers.py +++ b/registrasion/tests/test_helpers.py @@ -1,3 +1,5 @@ +import datetime + from registrasion.models import commerce from controller_helpers import TestingCartController @@ -12,6 +14,13 @@ class TestHelperMixin(object): return TestingInvoiceController.for_cart(self.reget(cart.cart)) + def _manual_invoice(self, value=1): + items = [("Item", value)] + due = datetime.timedelta(hours=1) + inv = TestingInvoiceController.manual_invoice(self.USER_1, due, items) + + return TestingInvoiceController(inv) + def _credit_note_for_invoice(self, invoice): note = commerce.CreditNote.objects.get(invoice=invoice) return TestingCreditNoteController(note) From 77a7689de50a0b6d8072167c4687377a52a6ed57 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Thu, 15 Sep 2016 11:01:24 +1000 Subject: [PATCH 7/7] Fixes credit note tests that were broken with the old behaviour --- registrasion/tests/test_credit_note.py | 35 +++++++++++++++++--------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/registrasion/tests/test_credit_note.py b/registrasion/tests/test_credit_note.py index 6b2c66ac..3e6d1cd6 100644 --- a/registrasion/tests/test_credit_note.py +++ b/registrasion/tests/test_credit_note.py @@ -95,6 +95,12 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): self.assertEqual(to_pay, credit_notes[0].value) def test_apply_credit_note_pays_invoice(self): + + # Create a manual invoice (stops credit notes from being auto-applied) + self._manual_invoice(1) + + # Begin the test + invoice = self._invoice_containing_prod_1(1) to_pay = invoice.invoice.value @@ -122,10 +128,11 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): self.assertEquals(0, commerce.CreditNote.unclaimed().count()) def test_apply_credit_note_generates_new_credit_note_if_overpaying(self): + + # Create and refund an invoice, generating a credit note. invoice = self._invoice_containing_prod_1(2) - to_pay = invoice.invoice.value - invoice.pay("Reference", to_pay) + invoice.pay("Reference", invoice.invoice.value) self.assertTrue(invoice.invoice.is_paid) invoice.refund() @@ -135,13 +142,9 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): self.assertEquals(1, commerce.CreditNote.unclaimed().count()) - # Create a new cart (of half value of inv 1) and get invoice - cart = TestingCartController.for_user(self.USER_1) - cart.add_to_cart(self.PROD_1, 1) - - invoice2 = TestingInvoiceController.for_cart(self.reget(cart.cart)) - - cn.apply_to_invoice(invoice2.invoice) + # Create a new invoice for a cart of half value of inv 1 + invoice2 = self._invoice_containing_prod_1(1) + # Credit note is automatically applied by generating the new invoice self.assertTrue(invoice2.invoice.is_paid) # We generated a new credit note, and spent the old one, @@ -160,6 +163,12 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): ) def test_cannot_apply_credit_note_on_invalid_invoices(self): + + # Disable auto-application of invoices. + self._manual_invoice(1) + + # And now start the actual test. + invoice = self._invoice_containing_prod_1(1) to_pay = invoice.invoice.value @@ -237,7 +246,9 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): cart.add_to_cart(self.PROD_1, 1) invoice_2 = TestingInvoiceController.for_cart(self.reget(cart.cart)) - cn.apply_to_invoice(invoice_2.invoice) + with self.assertRaises(ValidationError): + # Creating `invoice_2` will automatically apply `cn`. + cn.apply_to_invoice(invoice_2.invoice) self.assertEquals(0, commerce.CreditNote.unclaimed().count()) @@ -301,9 +312,9 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): cart = TestingCartController.for_user(self.USER_1) cart.add_to_cart(self.PROD_1, 2) - # Create a current invoice, and apply partial payments + # Create a current invoice + # This will automatically apply `cn` to the invoice 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.