|
|
|
@ -60,9 +60,9 @@ async def get_charts(
|
|
|
|
|
|
|
|
|
|
|
|
chart_data = results.scalars().all()
|
|
|
|
chart_data = results.scalars().all()
|
|
|
|
bep_year = None
|
|
|
|
bep_year = None
|
|
|
|
previous_year = 0
|
|
|
|
previous_year = None
|
|
|
|
previous_total_cost = 0
|
|
|
|
previous_total_cost = None
|
|
|
|
previous_revenue = 0
|
|
|
|
previous_revenue = None
|
|
|
|
bep_total_lcc = 0
|
|
|
|
bep_total_lcc = 0
|
|
|
|
|
|
|
|
|
|
|
|
for idx, item in enumerate(chart_data):
|
|
|
|
for idx, item in enumerate(chart_data):
|
|
|
|
@ -73,16 +73,36 @@ async def get_charts(
|
|
|
|
)
|
|
|
|
)
|
|
|
|
revenue = item.chart_revenue_annualized
|
|
|
|
revenue = item.chart_revenue_annualized
|
|
|
|
|
|
|
|
|
|
|
|
if previous_total_cost and previous_revenue:
|
|
|
|
if previous_total_cost is not None and previous_revenue is not None:
|
|
|
|
if (previous_total_cost > previous_revenue and total_cost < revenue) or (
|
|
|
|
prev_diff = previous_total_cost - previous_revenue
|
|
|
|
previous_total_cost < previous_revenue and total_cost > revenue
|
|
|
|
curr_diff = total_cost - revenue
|
|
|
|
):
|
|
|
|
|
|
|
|
if total_cost < revenue:
|
|
|
|
# If signs differ there's a crossing between previous and current point
|
|
|
|
bep_total_lcc = previous_total_cost
|
|
|
|
if prev_diff == 0:
|
|
|
|
bep_year = previous_year
|
|
|
|
bep_year = previous_year
|
|
|
|
|
|
|
|
bep_total_lcc = previous_total_cost
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if prev_diff * curr_diff < 0:
|
|
|
|
|
|
|
|
# Interpolate linearly between the two years to estimate BEP year
|
|
|
|
|
|
|
|
denom = ( (total_cost - previous_total_cost) - (revenue - previous_revenue) )
|
|
|
|
|
|
|
|
if denom != 0:
|
|
|
|
|
|
|
|
t = (previous_revenue - previous_total_cost) / denom
|
|
|
|
|
|
|
|
# clamp t to [0,1]
|
|
|
|
|
|
|
|
t = max(0.0, min(1.0, t))
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
bep_year = previous_year + t * (item.tahun - previous_year)
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
|
|
bep_year = previous_year
|
|
|
|
|
|
|
|
bep_total_lcc = previous_total_cost + t * (total_cost - previous_total_cost)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
bep_total_lcc = total_cost
|
|
|
|
# fallback if interpolation is not possible
|
|
|
|
bep_year = item.tahun
|
|
|
|
if total_cost < revenue:
|
|
|
|
|
|
|
|
bep_total_lcc = previous_total_cost
|
|
|
|
|
|
|
|
bep_year = previous_year
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
bep_total_lcc = total_cost
|
|
|
|
|
|
|
|
bep_year = item.tahun
|
|
|
|
break
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
previous_total_cost = total_cost
|
|
|
|
previous_total_cost = total_cost
|
|
|
|
|