Browse files

Updated billing to send month's bills on first day of month when bill…

…ing cycle is due to end instead of 14 days before end of billing cycle
  • Loading branch information...
1 parent 4c4fdcd commit c5fa366c69882270ad2c030c923dae9980107823 @KilledWhale KilledWhale committed Feb 27, 2014
Showing with 9 additions and 4 deletions.
  1. +4 −1 membership/management/commands/makebills.py
  2. +5 −3 membership/tests.py
View
5 membership/management/commands/makebills.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import logging
from datetime import datetime, timedelta
+import calendar
from django.core.exceptions import ObjectDoesNotExist
from django.core.management.base import NoArgsCommand
@@ -90,6 +91,8 @@ def makebills():
logger.info("Running makebills...")
latest_recorded_payment = Payment.latest_payment_date()
+ dt = datetime.now()
+ last_of_month = datetime(dt.year, dt.month, calendar.monthrange(dt.year, dt.month)[1], 23, 59, 59)
for member in Membership.objects.filter(status='A').filter(id__gt=0):
# Billing cycles and bills
cycles = member.billingcycle_set
@@ -100,7 +103,7 @@ def makebills():
latest_cycle = cycles.latest("end")
if latest_cycle.end < datetime.now():
logger.warning("no new billing cycle created for %s after an expired one!" % repr(member))
- if latest_cycle.end < datetime.now() + timedelta(days=settings.BILL_DAYS_BEFORE_CYCLE):
+ if latest_cycle.end <= last_of_month:
cycle = create_billingcycle(member)
logger.info("Created billing cycle %s for %s" %
(repr(cycle), repr(member)))
View
8 membership/tests.py
@@ -9,6 +9,7 @@
from decimal import Decimal
from datetime import datetime, timedelta
from random import randint
+import calendar
import json
from django.contrib.auth.models import User
@@ -481,18 +482,19 @@ def test_new_billing_cycle_with_previous_paid(self):
self.assertEqual(len(m.billingcycle_set.all()), 1)
self.assertEqual(len(mail.outbox), 1)
+ dt = datetime.now()
c = m.billingcycle_set.all()[0]
- c.end = datetime.now() + timedelta(days=5)
+ c.end = datetime(dt.year, dt.month, calendar.monthrange(dt.year, dt.month)[1], 23, 59, 59)
c.save()
b = c.last_bill()
- b.due_date = datetime.now() + timedelta(days=9)
+ b.due_date = datetime(dt.year, dt.month, 1) + timedelta(days = 7)
b.save()
b.billingcycle.is_paid = True
b.billingcycle.save()
makebills()
- self.assertTrue(len(m.billingcycle_set.all()), 2)
+ self.assertEqual(len(m.billingcycle_set.all()), 2)
self.assertEqual(len(mail.outbox), 2)
class SingleMemberBillingModelsTest(TestCase):

0 comments on commit c5fa366

Please sign in to comment.