simplified when we calc totals and hence growth, applied monthly growth annually

This commit is contained in:
2025-08-21 16:52:37 +10:00
parent c469f6d281
commit cd7eca0c6e

View File

@@ -35,11 +35,13 @@ def add_missing_monthly_bills_in_yr( bill_type, bill_info, yr ):
# really perfectly the same each month, but its only for an estimate so should be ok # really perfectly the same each month, but its only for an estimate so should be ok
dd = bill_info[bill_type]['first_bill']['bill_date'][8:] dd = bill_info[bill_type]['first_bill']['bill_date'][8:]
mm = bill_info[bill_type]['first_bill']['bill_date'][5:7] 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 # choose last bill from last amount to grow from as its most relevant
amt = bill_info[bill_type]['last_bill']['amount'] 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']
growth=0
#okay add monthly bills for the rest of this year if its the first year #okay add monthly bills for the rest of this year if its the first year
if bill_info[bill_type]['first_bill_year'] == yr: if bill_info[bill_type]['first_bill_year'] == yr:
start_m=int(mm) start_m=int(mm)
@@ -59,7 +61,12 @@ def add_missing_monthly_bills_in_yr( bill_type, bill_info, yr ):
bill_found=True bill_found=True
break break
if not bill_found: if not bill_found:
amt += amt * growth/100 # 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):
print(f"its month: {i} - time to add growth: {bill_info[bill_type]['growth']}" )
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) # last param is estimated (and this is an estimate for a future bill / not real)
new_bill( bill_type, amt, new_date, 1 ) new_bill( bill_type, amt, new_date, 1 )
return return
@@ -94,6 +101,9 @@ def process_bill_data(bd, bt, bf):
# due to sql sorting, this first instance is the last bill # due to sql sorting, this first instance is the last bill
bill_info[bill_type]['last_bill']=bill bill_info[bill_type]['last_bill']=bill
bill_info[bill_type]['last_bill_year']=int(bill['bill_date'][:4]) bill_info[bill_type]['last_bill_year']=int(bill['bill_date'][:4])
if not bill['estimated']:
print( f"this bill is real, its the first so consider it the last bill paid - date is: {int(bill['bill_date'][:4])}" )
bill_info[bill_type]['last_real_bill_year']=int(bill['bill_date'][:4])
bill_info[bill_type]['year']={} bill_info[bill_type]['year']={}
bill_info[bill_type]['year_real']={} bill_info[bill_type]['year_real']={}
if not yr in bill_info[bill_type]['year']: if not yr in bill_info[bill_type]['year']:
@@ -103,6 +113,9 @@ def process_bill_data(bd, bt, bf):
# keep updating last to this matching bill # keep updating last to this matching bill
bill_info[bill_type]['first_bill']=bill bill_info[bill_type]['first_bill']=bill
bill_info[bill_type]['first_bill_year']=int(bill['bill_date'][:4]) 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']:
print( f"{bill_type}: seems we dont have a last_real_bill_year, set it to: {int(bill['bill_date'][:4])} ")
bill_info[bill_type]['last_real_bill_year']=int(bill['bill_date'][:4])
# add this bill to list for this year # add this bill to list for this year
bill_info[bill_type]['year'][yr].append(bill) bill_info[bill_type]['year'][yr].append(bill)
if not bill['estimated']: if not bill['estimated']:
@@ -142,11 +155,20 @@ def add_missing_bills_for_yr( bill_type, bill_info, yr ):
return return
def derive_ann_growth( bill_type, bill_info ): def derive_ann_growth( bill_type, bill_info ):
print(f"{bill_type}: Derive annual growth on bill_type: {bill_type} " ) print(f"{bill_type}: Derive annual growth on bill_type: {bill_type} - fby={bill_info[bill_type]['first_bill_year']}, lby={bill_info[bill_type]['last_real_bill_year']} " )
total={} total={}
for yr in range( bill_info[bill_type]['first_bill_year'], bill_info[bill_type]['last_bill_year']+1): for yr in range( bill_info[bill_type]['first_bill_year'], bill_info[bill_type]['last_real_bill_year']+1):
if len(bill_info[bill_type]['year_real'][yr]) != bill_info[bill_type]['num_ann_bills']: # for monthly bills, 1 or 12 bills is enough to work with
if bill_info[bill_type]['num_ann_bills'] == 12:
if len(bill_info[bill_type]['year_real'][yr]) != 1 and len(bill_info[bill_type]['year_real'][yr]) != 12:
continue;
# okay annual or quarterly bills, only total them if we have all of them for the year
elif len(bill_info[bill_type]['year_real'][yr]) != bill_info[bill_type]['num_ann_bills']:
continue;
# for monthlys add totals regardless (we only process total if there is 12 or 1 further below). Other bill_types, only total if there is all bills for year
if bill_info[bill_type]['num_ann_bills'] != 12 and len(bill_info[bill_type]['year_real'][yr]) != bill_info[bill_type]['num_ann_bills']:
continue continue
total[yr] = 0 total[yr] = 0
for b in bill_info[bill_type]['year'][yr]: for b in bill_info[bill_type]['year'][yr]:
@@ -161,15 +183,9 @@ def derive_ann_growth( bill_type, bill_info ):
avg_growth = 0 avg_growth = 0
max_growth = 0 max_growth = 0
count = 0 count = 0
for yr in range( bill_info[bill_type]['first_bill_year'], bill_info[bill_type]['last_bill_year']+1): # start from year after first bill, so we can see annual growth from the following year onwards
# less than {num_ann_bills} bills in yr: {yr-1}, so can't use data for yr in range( bill_info[bill_type]['first_bill_year']+1, bill_info[bill_type]['last_bill_year']+1):
if yr-1 in bill_info[bill_type]['year'] and len(bill_info[bill_type]['year'][yr-1]) != bill_info[bill_type]['num_ann_bills']: # if full data sets for consecutive years, work out annual growth stats
continue
# less than {num_ann_bills} bills in yr: {yr-1}, so can't use data
if yr in bill_info[bill_type]['year'] and len(bill_info[bill_type]['year'][yr]) != bill_info[bill_type]['num_ann_bills']:
continue
# we full data sets for consecutive years, work out annual growth stats
if yr-1 in total and yr in total: if yr-1 in total and yr in total:
growth = (total[yr] - total[yr-1]) / total[yr-1] * 100 growth = (total[yr] - total[yr-1]) / total[yr-1] * 100
avg_growth += growth avg_growth += growth