Changeset - 4a50d699365b
[Not reviewed]
0 5 0
Christopher Neugebauer - 8 years ago 2016-09-15 23:35:12
chrisjrn@gmail.com
Moves total_payments() to Invoice model; adds balance_due()
5 files changed with 43 insertions and 40 deletions:
0 comments (0 inline, 0 general)
registrasion/controllers/invoice.py
Show inline comments
...
 
@@ -271,9 +271,2 @@ class InvoiceController(ForId, object):
 

	
 
    def total_payments(self):
 
        ''' Returns the total amount paid towards this invoice. '''
 

	
 
        payments = commerce.PaymentBase.objects.filter(invoice=self.invoice)
 
        total_paid = payments.aggregate(Sum("amount"))["amount__sum"] or 0
 
        return total_paid
 

	
 
    def update_status(self):
...
 
@@ -283,3 +276,3 @@ class InvoiceController(ForId, object):
 
        old_status = self.invoice.status
 
        total_paid = self.total_payments()
 
        total_paid = self.invoice.total_payments()
 
        num_payments = commerce.PaymentBase.objects.filter(
...
 
@@ -368,3 +361,3 @@ class InvoiceController(ForId, object):
 
        if not self._invoice_matches_cart():
 
            if self.total_payments() > 0:
 
            if self.invoice.total_payments() > 0:
 
                # Free up the payments made to this invoice
...
 
@@ -376,3 +369,3 @@ class InvoiceController(ForId, object):
 
        ''' Voids the invoice if it is valid to do so. '''
 
        if self.total_payments() > 0:
 
        if self.invoice.total_payments() > 0:
 
            raise ValidationError("Invoices with payments must be refunded.")
...
 
@@ -396,3 +389,3 @@ class InvoiceController(ForId, object):
 
        # Raises a credit note fot the value of the invoice.
 
        amount = self.total_payments()
 
        amount = self.invoice.total_payments()
 

	
registrasion/models/commerce.py
Show inline comments
...
 
@@ -6,3 +6,3 @@ from django.core.exceptions import ValidationError
 
from django.db import models
 
from django.db.models import F, Q
 
from django.db.models import F, Q, Sum
 
from django.utils import timezone
...
 
@@ -177,2 +177,13 @@ class Invoice(models.Model):
 

	
 
    def total_payments(self):
 
        ''' Returns the total amount paid towards this invoice. '''
 

	
 
        payments = PaymentBase.objects.filter(invoice=self)
 
        total_paid = payments.aggregate(Sum("amount"))["amount__sum"] or 0
 
        return total_paid
 

	
 
    def balance_due(self):
 
        ''' Returns the total balance remaining towards this invoice. '''
 
        return self.value - self.total_payments()
 

	
 
    # Invoice Number
...
 
@@ -226,2 +237,7 @@ class LineItem(models.Model):
 

	
 
    @property
 
    def total_price(self):
 
        ''' price * quantity '''
 
        return self.price * self.quantity
 

	
 
    invoice = models.ForeignKey(Invoice)
registrasion/templatetags/registrasion_tags.py
Show inline comments
...
 
@@ -76,22 +76 @@ def items_purchased(context, category=None):
 
    )
 

	
 

	
 
@register.filter
 
def multiply(value, arg):
 
    ''' Multiplies value by arg.
 

	
 
    This is useful when displaying invoices, as it lets you multiply the
 
    quantity by the unit value.
 

	
 
    Arguments:
 

	
 
        value (number)
 

	
 
        arg (number)
 

	
 
    Returns:
 
        number: value * arg
 

	
 
    '''
 

	
 
    return value * arg
registrasion/tests/test_credit_note.py
Show inline comments
...
 
@@ -31,3 +31,5 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase):
 
        # The total paid should be equal to the value of the invoice only
 
        self.assertEqual(invoice.invoice.value, invoice.total_payments())
 
        self.assertEqual(
 
            invoice.invoice.value, invoice.invoice.total_payments()
 
        )
 
        self.assertTrue(invoice.invoice.is_paid)
...
 
@@ -48,3 +50,5 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase):
 
        # The total paid should be equal to the value of the invoice only
 
        self.assertEqual(invoice.invoice.value, invoice.total_payments())
 
        self.assertEqual(
 
            invoice.invoice.value, invoice.invoice.total_payments()
 
        )
 
        self.assertTrue(invoice.invoice.is_paid)
...
 
@@ -66,3 +70,3 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase):
 
        # The total paid should be zero
 
        self.assertEqual(0, invoice.total_payments())
 
        self.assertEqual(0, invoice.invoice.total_payments())
 
        self.assertTrue(invoice.invoice.is_void)
...
 
@@ -86,3 +90,3 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase):
 
        # The total paid should be zero
 
        self.assertEqual(0, invoice.total_payments())
 
        self.assertEqual(0, invoice.invoice.total_payments())
 
        self.assertTrue(invoice.invoice.is_refunded)
...
 
@@ -369,3 +373,3 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase):
 

	
 
        self.assertEqual(notes_value, invoice.total_payments())
 
        self.assertEqual(notes_value, invoice.invoice.total_payments())
 
        self.assertTrue(invoice.invoice.is_unpaid)
...
 
@@ -386,3 +390,3 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase):
 

	
 
        self.assertEqual(notes_value - 1, invoice.total_payments())
 
        self.assertEqual(notes_value - 1, invoice.invoice.total_payments())
 
        self.assertTrue(invoice.invoice.is_paid)
...
 
@@ -428,3 +432,3 @@ class CreditNoteTestCase(TestHelperMixin, RegistrationCartTestCase):
 
        # The credit notes are not automatically applied.
 
        self.assertEqual(0, invoice.total_payments())
 
        self.assertEqual(0, invoice.invoice.total_payments())
 
        self.assertTrue(invoice.invoice.is_unpaid)
registrasion/tests/test_invoice.py
Show inline comments
...
 
@@ -99,2 +99,13 @@ class InvoiceTestCase(TestHelperMixin, RegistrationCartTestCase):
 

	
 
    def test_total_payments_balance_due(self):
 
        invoice = self._invoice_containing_prod_1(2)
 
        for i in xrange(0, invoice.invoice.value):
 
            self.assertTrue(
 
                i + 1, invoice.invoice.total_payments()
 
            )
 
            self.assertTrue(
 
                invoice.invoice.value - i, invoice.invoice.balance_due()
 
            )
 
            invoice.pay("Pay 1", 1)
 

	
 
    def test_invoice_includes_discounts(self):
0 comments (0 inline, 0 general)