From cd7eca0c6ec96098f155494b7a217285a6727ddd Mon Sep 17 00:00:00 2001 From: Damien De Paoli Date: Thu, 21 Aug 2025 16:52:37 +1000 Subject: [PATCH] simplified when we calc totals and hence growth, applied monthly growth annually --- bills.py | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/bills.py b/bills.py index 902de72..f5314c2 100644 --- a/bills.py +++ b/bills.py @@ -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 dd = bill_info[bill_type]['first_bill']['bill_date'][8:] 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 - 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 if bill_info[bill_type]['first_bill_year'] == yr: start_m=int(mm) @@ -59,7 +61,12 @@ def add_missing_monthly_bills_in_yr( bill_type, bill_info, yr ): bill_found=True break 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) new_bill( bill_type, amt, new_date, 1 ) return @@ -94,6 +101,9 @@ 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']: + 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_real']={} 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 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']: + 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 bill_info[bill_type]['year'][yr].append(bill) if not bill['estimated']: @@ -142,11 +155,20 @@ def add_missing_bills_for_yr( bill_type, bill_info, yr ): return 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={} - for yr in range( bill_info[bill_type]['first_bill_year'], bill_info[bill_type]['last_bill_year']+1): - if len(bill_info[bill_type]['year_real'][yr]) != bill_info[bill_type]['num_ann_bills']: + for yr in range( bill_info[bill_type]['first_bill_year'], bill_info[bill_type]['last_real_bill_year']+1): + # 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 total[yr] = 0 for b in bill_info[bill_type]['year'][yr]: @@ -161,15 +183,9 @@ def derive_ann_growth( bill_type, bill_info ): avg_growth = 0 max_growth = 0 count = 0 - for yr in range( bill_info[bill_type]['first_bill_year'], bill_info[bill_type]['last_bill_year']+1): - # less than {num_ann_bills} bills in yr: {yr-1}, so can't use data - 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']: - 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 + # start from year after first bill, so we can see annual growth from the following year onwards + for yr in range( bill_info[bill_type]['first_bill_year']+1, bill_info[bill_type]['last_bill_year']+1): + # if full data sets for consecutive years, work out annual growth stats if yr-1 in total and yr in total: growth = (total[yr] - total[yr-1]) / total[yr-1] * 100 avg_growth += growth