Skip to content

Commit

Permalink
changed logic in change_bill_day to truncate and close any open bill …
Browse files Browse the repository at this point in the history
…and unassociate any coworking days past the change date
  • Loading branch information
jsayles committed Aug 11, 2017
1 parent ef16975 commit d93ec3f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
17 changes: 12 additions & 5 deletions nadine/models/membership.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,13 +500,20 @@ def last_change(self):
return last_subscription.created_ts
return None

def change_bill_day(self, day=None, target_date=None):
if not day:
day = localtime(now()).day

def change_bill_day(self, target_date):
try:
with transaction.atomic():
self.bill_day = day
# Find any overlapping open bills
from billing import UserBill
open_bill = UserBill.objects.get_open_bill(self.user, target_date, target_date)
if open_bill:
for d in open_bill.coworking_days().filter(visit_date__gte=target_date):
d.unassociate()
open_bill.period_end = target_date - timedelta(days=1)
open_bill.close()

# Now we can change the bill day
self.bill_day = target_date.day
self.save()
except IntegrityError as e:
print('There was an ERROR: %s' % e.message)
Expand Down
5 changes: 5 additions & 0 deletions nadine/models/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ def mark_waived(self):
self.payment = 'Waive'
self.save()

def unassociate(self):
''' Unassociate this day with any bill it might be added to. '''
if self.bill:
self.line_item.delete()

def __str__(self):
return '%s - %s' % (self.visit_date, self.user)

Expand Down
13 changes: 6 additions & 7 deletions nadine/tests/test_billing_scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,22 +276,21 @@ def test_change_bill_day(self):
self.assertTrue(may_10_bill.is_open)

# Change the bill date to the 1st
membership.change_bill_day(day=1, target_date=date(2010, 5, 27))
membership.change_bill_day(date(2010, 6, 1))
self.assertEqual(1, membership.bill_day)

# Generate bills again
batch = BillingBatch.objects.run(start_date=date(2010, 6, 1), end_date=date(2010, 6, 30))
self.assertEqual(1, membership.bill_day)
# Generate the bills again
batch = BillingBatch.objects.run(start_date=date(2010, 5, 1), end_date=date(2010, 6, 2))
self.assertTrue(batch.successful)
# self.assertEqual(2, batch.bills.count())
self.assertEqual(1, batch.bills.count())
print_all_bills(user)

# Make sure the 6/2 day got on the new June bill
june_1_bill = user.bills.get(period_start=date(2010, 6, 1))
may_bill = user.bills.get(period_start=date(2010, 5, 10))
self.assertFalse(day1 in june_1_bill.coworking_days())
self.assertFalse(day2 in june_1_bill.coworking_days())
self.assertTrue(day3 in june_1_bill.coworking_days())
self.assertTrue(day3 not in may_bill.coworking_days())


def test_start_package(self):
#New user joins and starts a PT5 membership the same day
Expand Down

0 comments on commit d93ec3f

Please sign in to comment.