Skip to content

Commit

Permalink
Also update the cached bill totals when saving or deleting a BillLine…
Browse files Browse the repository at this point in the history
…Item
  • Loading branch information
jsayles committed Jan 3, 2019
1 parent ca20772 commit ecde6af
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
3 changes: 2 additions & 1 deletion nadine/models/billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

logger = logging.getLogger(__name__)


class BatchManager(models.Manager):

def run(self, start_date=None, end_date=None, created_by=None):
Expand Down Expand Up @@ -886,4 +887,4 @@ def __str__(self):
@cached_property
def calculate_tax_rate(self):
# Figure out the tax rate from amount and line_item.amount
pass
pass
32 changes: 30 additions & 2 deletions nadine/signals.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
from __future__ import unicode_literals
import logging

from django.dispatch import receiver
from django.db.models.signals import post_save, post_delete
from django.core.exceptions import ObjectDoesNotExist

from nadine.models import Payment, BillLineItem

logger = logging.getLogger(__name__)


@receiver(post_save, sender=BillLineItem)
def lineitem_post_save(**kwargs):
"""
Update cached totals on UserBill.
"""
lineitem = kwargs['instance']
bill = lineitem.bill
bill.update_cached_totals()


@receiver(post_delete, sender=BillLineItem)
def lineitem_post_delete(**kwargs):
"""
Update cached totals on UserBill.
"""
lineitem = kwargs['instance']
try:
bill = lineitem.bill
bill.update_cached_totals()
except ObjectDoesNotExist:
logger.warn("Deleting a BillLineItem that does not have a Bill!")

from nadine.models import Payment

@receiver(post_save, sender=Payment)
def payment_post_save(**kwargs):
Expand All @@ -14,6 +41,7 @@ def payment_post_save(**kwargs):
bill = payment.bill
bill.update_cached_totals()


@receiver(post_delete, sender=Payment)
def payment_post_delete(**kwargs):
"""
Expand Down
14 changes: 11 additions & 3 deletions nadine/tests/test_userbill.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,21 @@ def test_outstanding(self):
self.assertEqual(0, bill.total_owed)
self.assertFalse(bill in UserBill.objects.outstanding())
# Create a line item
BillLineItem.objects.create(bill=bill, amount=10)
lineitem = BillLineItem.objects.create(bill=bill, amount=10)
self.assertEqual(10, bill.amount)
self.assertEqual(10, bill.total_owed)
self.assertTrue(bill in UserBill.objects.outstanding())
# Pay the bill
Payment.objects.create(bill=bill, user=self.user1, amount=10)
payment = Payment.objects.create(bill=bill, user=self.user1, amount=10)
self.assertEqual(0, bill.total_owed)
self.assertFalse(bill in UserBill.objects.outstanding())
# Delete the payment
payment.delete()
self.assertEqual(10, bill.amount)
self.assertEqual(10, bill.total_owed)
self.assertTrue(bill in UserBill.objects.outstanding())
# Delete the line item
lineitem.delete()
self.assertEqual(0, bill.total_owed)
self.assertFalse(bill in UserBill.objects.outstanding())

Expand Down Expand Up @@ -195,7 +204,6 @@ def test_resource_allowance(self):
bill.add_subscription(subscription2)
self.assertEqual(18, bill.resource_allowance(Resource.objects.day_resource))


def test_recalculate(self):
user = User.objects.create(username='test_user', first_name='Test', last_name='User')
membership = Membership.objects.for_user(user)
Expand Down

0 comments on commit ecde6af

Please sign in to comment.