diff --git a/registrasion/controllers/credit_note.py b/registrasion/controllers/credit_note.py index 182c10e9..e4784c8b 100644 --- a/registrasion/controllers/credit_note.py +++ b/registrasion/controllers/credit_note.py @@ -1,3 +1,5 @@ +import datetime + from django.db import transaction from registrasion.models import commerce @@ -53,3 +55,27 @@ class CreditNoteController(ForId, object): inv.update_status() # TODO: Add administration fee generator. + @transaction.atomic + def cancellation_fee(self, percentage): + ''' Generates an invoice with a cancellation fee, and applies + credit to the invoice. + + percentage (Decimal): The percentage of the credit note to turn into + a cancellation fee. Must be 0 <= percentage <= 100. + ''' + + from invoice import InvoiceController # Circular imports bleh. + + assert(percentage >= 0 and percentage <= 100) + + cancellation_fee = self.credit_note.value * percentage / 100 + due = datetime.timedelta(days=1) + item = [("Cancellation fee", cancellation_fee)] + invoice = InvoiceController.manual_invoice( + self.credit_note.invoice.user, due, item + ) + + if not invoice.is_paid: + self.apply_to_invoice(invoice) + + return InvoiceController(invoice) diff --git a/registrasion/tests/test_credit_note.py b/registrasion/tests/test_credit_note.py index 2857ce7f..c4be1041 100644 --- a/registrasion/tests/test_credit_note.py +++ b/registrasion/tests/test_credit_note.py @@ -440,3 +440,28 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase): # Generate invoice that should be automatically paid invoice2 = self._manual_invoice(1) self.assertTrue(invoice2.invoice.is_paid) + + def test_cancellation_fee_is_applied(self): + + invoice1 = self._manual_invoice(1) + invoice1.pay("Pay", invoice1.invoice.value) + invoice1.refund() + + percentage = 15 + + cn = self._credit_note_for_invoice(invoice1.invoice) + canc = cn.cancellation_fee(15) + + # Cancellation fee exceeds the amount for the invoice. + self.assertTrue(canc.invoice.is_paid) + + # Cancellation fee is equal to 15% of credit note's value + self.assertEqual( + canc.invoice.value, + cn.credit_note.value * percentage / 100 + ) + + def test_cancellation_fee_is_applied_when_another_invoice_is_unpaid(self): + + extra_invoice = self._manual_invoice(23) + self.test_cancellation_fee_is_applied()