Skip to content

Instantly share code, notes, and snippets.

@ruiwen
Last active December 10, 2015 01:58
Show Gist options
  • Save ruiwen/4363262 to your computer and use it in GitHub Desktop.
Save ruiwen/4363262 to your computer and use it in GitHub Desktop.
class Contract:
def total_paid(self, at=None):
'''Returns total amount paid due to this Contract'''
# Filter
params = {}
if at:
params.update({"date_paid__lte": at})
return self.payments.filter(**params).aggregate(Sum('amount'))['amount__sum'] or 0.0
def balance(self, in_months=False, at=None):
'''Looks at how much has been paid for this Contract and determines if there is any balance owed by (-ve) / owed to (+ve) the Member'''
balance = 0
duration_in_months = 0
if at and self.start > at:
return 0 # There is no balance if this Contract started after given date
# Calculate number of months Contract has been in effect, ie. not Terminated
if at and (self.end and self.end > at):
print "2"
duration_in_months += self.__month_diff(at, self.start)
elif self.end or (self.end and at and self.end < at):
print "1"
duration_in_months += self.__month_diff(self.end, self.start)
else:
print "3"
duration_in_months += self.__month_diff(datetime.date.today(), self.start)
balance = self.total_paid(at=at) - (self.tier.fee * duration_in_months)
if in_months:
return balance / self.tier.fee
else:
return balance
from hado.models import *
import datetime
f = open("prepayments.csv", "w+")
import csv
ppcsv = csv.DictWriter(f, ["user", "email", "balance"])
ppcsv.writeheader()
total = 0
users = User.objects.all().order_by('first_name')
for u in users:
for c in u.contracts.exclude(start__gte=datetime.date(2010, 10, 1)):
balance = c.balance(at=datetime.date(2010, 9, 30))
if balance > 0:
ppcsv.writerow({"user":u.get_full_name(), "email":u.email, "balance": balance})
total += balance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment