In the case that we calculate the rent for a tenancy based on the begin/end of a tenancy (AND independently of the rentrevision dates), I think we need to introduce the parameters tenancy_begin_date, tenancy_end_date into the function revision_to_cashflows
def revision_to_cashflows(rev, end_date, tenancy_begin_date = date(2015, 6, 1), tenancy_end_date = date(2015, 12, 13)):
"""Converts a revision to a list of cashflows
end_date -- the first month we do not want to take into account
"""
end_date = rev.end_date or end_date
if not end_date:
end_date = next_month(date.today(),1)
start_date = rev.start_date
if not tenancy_end_date:
tenancy_end_date = next_month(date.today(),1)
delta_t = (tenancy_end_date - tenancy_begin_date).days
#list of dates from d1 to d2
tenancy_date_list = [tenancy_begin_date + timedelta(days=x) for x in range(0, delta_t+1)]
delta_r = (end_date - start_date).days
#list of dates from d1 to d2
revision_date_list = [start_date + timedelta(days=x) for x in range(0, delta_r+1)]
intersect_date_list = list(set(revision_date_list).intersection(set(tenancy_date_list)))
result = []
month_year_list = [(d.year, d.month) for d in intersect_date_list]
#generate-list-of-tuples (year,month, days-in-month, full-month) from-list-of-dates
year_month_ndays_full = [(k[0],k[1],v , True if monthrange(k[0], k[1])[1] == v else False) for k,v in Counter(month_year_list).iteritems()]
for d in year_month_ndays_full:
date_info = date(d[0],d[1],1)
#if full month
if d[3] == True:
result.append(Cashflow(date_info, - rev.rent, _('RENT ') ))
if rev.provision != 0:
result.append(Cashflow(date_info, -rev.provision, _('PROVISION ') ))
#if partial month, divide by days of month rented
else:
daysinmonth = monthrange(date_info.year, date_info.month)[1]
rented_days = d[2]
result.append(Cashflow(date_info, -(rev.rent/daysinmonth*rented_days),\
ungettext("RENT for %(day)s day in partial month",\
"RENT for %(day)s days in in partial month", rented_days) % {'day': str(rented_days)}))
if rev.provision != 0:
result.append(Cashflow(date_info, -(rev.provision/daysinmonth*rented_days), \
ungettext("PROVISION for %(day)s day in partial month",\
"PROVISION for %(day)s days in in partial month", rented_days) % {'day': str(rented_days)}))
return result