created find_previous_bill and use it to help work out gaps in bills - e.g. like with internet where I added only the new costs

This commit is contained in:
2025-08-22 13:24:25 +10:00
parent 3bfeb30640
commit 0cf3d9897f

View File

@@ -6,6 +6,44 @@ def qtr(d):
m = int(d[5:7])
return ( (m-1)//3 + 1 )
def find_previous_bill( bill_type, bill_info, bill_date ):
# print( f"{bill_type}: find_previous_bill -> {bill_date}" )
wanted_year = int(bill_date[:4])
wanted_mm = int(bill_date[5:7])
# if we don't have a bill before this date, no way to set price
if int(wanted_year) < int(bill_info[bill_type]['first_bill_year']):
# print(f"{bill_type}: find_previous_bill - failed. wanted_year={wanted_year} is older than our first_bill={bill_info[bill_type]['first_bill_year']}" )
return None
# start loop from bill_date, go backwards and find which one it is (same year, should be month-based)
# earlier year, then just last one from the year.
yr_range=range( wanted_year, bill_info[bill_type]['first_bill_year'], -1 )
if wanted_year == int(bill_info[bill_type]['first_bill_year']):
# range of this year with -1, does not return anything, so force this year.
yr_range=[ wanted_year ]
for yr in yr_range:
# start with bills in the year wanted (if any)
# must include 'estimated' bills to deal with growth of future years
if yr in bill_info[bill_type]['year']:
# okay, we have the previous billing year, and we wanted one for a year in the future,
# just return the last one in this year as its the most recent
if wanted_year > yr:
# print("should be return last of {yr} - date={bill_info[bill_type]['year'][yr][-1]['bill_date']}" )
return bill_info[bill_type]['year'][yr][-1]
else:
# lets go through the newest to oldest of these bills
for bill in bill_info[bill_type]['year'][wanted_year][::-1]:
bill_mm = int(bill['bill_date'][5:7])
# reversing the bills, means we start with the 'most recent' in this year to the oldest
# if the month we want is after the bill, we are done
if wanted_mm > bill_mm:
return bill
# print(f"{bill_type}: find_previous_bill - failed. Seems our first bill = {bill_info[bill_type]['first_bill']['bill_date']} is in same year, but after wanted month={wanted_mm}, so no base to rely on" )
return None
# missing annual bill, find date based on MM-DD and add new year - given we start with first_bill anyway, will only be used for future bill predictions
# future only, so add ann_growth (based on drop-down) for each future year
# NOTE: only ever called when there is a need to add a new bill
@@ -37,11 +75,6 @@ def add_missing_monthly_bills_in_yr( bill_type, bill_info, yr ):
mm = bill_info[bill_type]['first_bill']['bill_date'][5:7]
lb_mm = bill_info[bill_type]['last_bill']['bill_date'][5:7]
# choose last bill from last amount to grow from as its most relevant
if not 'last_bill_amount' in bill_info[bill_type]:
bill_info[bill_type]['last_bill_amount']=bill_info[bill_type]['last_bill']['amount']
amt = bill_info[bill_type]['last_bill_amount']
#okay add monthly bills for the rest of this year if its the first year
if bill_info[bill_type]['first_bill_year'] == yr:
start_m=int(mm)
@@ -61,6 +94,12 @@ def add_missing_monthly_bills_in_yr( bill_type, bill_info, yr ):
bill_found=True
break
if not bill_found:
pb=find_previous_bill( bill_type, bill_info, new_date )
if not pb:
print("Failed to find previous_bill, can't calculate missing bill - returning" )
return
amt = pb['amount']
# if this month is the same as the last bill month and as per above
# we don't have a bill for this date, then add annual grotwh
if i == int(lb_mm):
@@ -69,6 +108,15 @@ def add_missing_monthly_bills_in_yr( bill_type, bill_info, yr ):
bill_info[bill_type]['last_bill_amount']=amt
# last param is estimated (and this is an estimate for a future bill / not real)
new_bill( bill_type, amt, new_date, 1 )
if not yr in bill_info[bill_type]['year']:
bill_info[bill_type]['year'][yr]=[]
bill={}
bill['bill_date']=new_date
bill['amount']=amt
bill['estimated']=1
# need this for find_previous_bill to work but only need the above 2 fields?
bill_info[bill_type]['year'][yr].append(bill)
return
# given the bill_type has a which_growth contain min/avg/max, return the corresponding growth number