wrapped new_bill in new_estimated_bill func, that adds to DB and to local bill_info, use this better to fill in quarterly future bills. Also exclude in growth calculations the final year of real bill if it includes estimates and real figures. For now, this is usable

This commit is contained in:
2025-08-22 18:10:09 +10:00
parent 6bccfade2b
commit 1719032ebf
2 changed files with 63 additions and 26 deletions

View File

@@ -60,6 +60,21 @@ def find_previous_bill( bill_type, bill_info, bill_date ):
return None
def new_estimated_bill( bill_info, yr, bill_type, amt, new_date ):
# add to DB
new_bill( bill_type, amt, new_date, 1 )
# patch this data back into bill_info so growth works in future
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 3 fields
bill_info[bill_type]['year'][yr].append(bill)
return
# 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
@@ -72,13 +87,31 @@ def add_missing_annual_bill_in_yr( bill_type, bill_info, yr ):
amt += amt * bill_info[bill_type]['growth']/100
# last param is estimated (and this is an estimate for a future bill / not real)
new_bill( bill_type, amt, f'{yr}-{mm_dd}', 1 )
new_estimated_bill( bill_info, yr, bill_type, amt, f'{yr}-{mm_dd}' )
return
# missing quarterly bill, find date based on MM-DD and ??? - can have missing bilsl in first year
# add growth (based on drop-down) for each future year
def add_missing_quarter_bills_in_yr( bill_type, bill_info, yr ):
print( f"*** add_missing_quarter_bills_in_yr( {bill_type}, bill_info, {yr} ): NOT YET" )
# okay we have data for this year but some missing (wouldnt be here otherwise)
# and data from previous year... lets fill in gaps
if yr in bill_info[bill_type]['year'] and yr-1 in bill_info[bill_type]['year']:
# per if above, ONLY get here if we have first few bills of {yr}, cannot be last few
have_q = qtr( bill_info[bill_type]['year'][yr][0]['bill_date'] )
for q in range(have_q+1,5):
# use 5-q, as bills are in descending order in bill_info, e.g. q4 is 1st,
bill=bill_info[bill_type]['year'][yr-1][4-q]
mm_dd= bill['bill_date'][5:]
amt = bill['amount']*(1+bill_info[bill_type]['growth']/100)
new_date = f'{yr}-{mm_dd}'
new_estimated_bill( bill_info, yr, bill_type, amt, new_date )
# for now only add full new years based on last year with ann_growth (seasonal)
if yr not in bill_info[bill_type]['year'] and yr-1 in bill_info[bill_type]['year']:
for bill in bill_info[bill_type]['year'][yr-1]:
mm_dd= bill['bill_date'][5:]
amt = bill['amount']*(1+bill_info[bill_type]['growth']/100)
new_estimated_bill( bill_info, yr, bill_type, amt, f'{yr}-{mm_dd}' )
return
# missing monthly bills, find date based on DD and put in each missing month
@@ -127,16 +160,7 @@ def add_missing_monthly_bills_in_yr( bill_type, bill_info, yr ):
amt += amt * bill_info[bill_type]['growth']/100
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)
new_estimated_bill( bill_info, yr, bill_type, amt, new_date )
return
# given the bill_type has a which_growth contain min/avg/max, return the corresponding growth number
@@ -183,6 +207,8 @@ def process_bill_data(bd, bt, bf):
# due to sql sorting, this first instance is the last bill
bill_info[bill_type]['last_bill']=bill
bill_info[bill_type]['last_bill_year']=int(bill['bill_date'][:4])
if not bill['estimated']:
bill_info[bill_type]['last_real_bill_year']=int(bill['bill_date'][:4])
bill_info[bill_type]['year']={}
if not yr in bill_info[bill_type]['year']:
bill_info[bill_type]['year'][yr]=[]
@@ -190,6 +216,8 @@ def process_bill_data(bd, bt, bf):
# keep updating last to this matching bill
bill_info[bill_type]['first_bill']=bill
bill_info[bill_type]['first_bill_year']=int(bill['bill_date'][:4])
if not 'last_real_bill_year' in bill_info[bill_type] and not bill['estimated']:
bill_info[bill_type]['last_real_bill_year']=int(bill['bill_date'][:4])
# add this bill to list for this year
bill_info[bill_type]['year'][yr].append(bill)
@@ -234,6 +262,15 @@ def derive_ann_growth( bill_type, bill_info ):
if yr not in bill_info[bill_type]['year'] or len(bill_info[bill_type]['year'][yr]) != bill_info[bill_type]['num_ann_bills']:
continue;
# just going to make sure we dont use estimated data in the last year of real data - can skew growths
if yr == bill_info[bill_type]['last_real_bill_year']:
skip_yr=False
for b in bill_info[bill_type]['year'][yr]:
if b['estimated']:
skip_yr=True
if skip_yr:
continue
total[yr] = 0
for b in bill_info[bill_type]['year'][yr]:
total[yr] += b['amount']