Adds cancellation fee implementation and tests
This commit is contained in:
parent
66dd3d9152
commit
d4f4312178
2 changed files with 51 additions and 0 deletions
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue