Skip to content

Commit

Permalink
Merge pull request #73 from KilledWhale/master
Browse files Browse the repository at this point in the history
Updated billing to send month's bills on first day of month when billing...
  • Loading branch information
annttu committed Feb 27, 2014
2 parents 4c4fdcd + c5fa366 commit 8df59b9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 4 additions & 1 deletion 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
Expand Down Expand Up @@ -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
Expand All @@ -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)))
Expand Down
8 changes: 5 additions & 3 deletions membership/tests.py
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 8df59b9

Please sign in to comment.