testing new features
This commit is contained in:
+24
-21
@@ -17,17 +17,33 @@ class ForecastService:
|
||||
|
||||
results = []
|
||||
for account in self.model.accounts:
|
||||
balance = account.balance
|
||||
monthly = self._project_account(account, months)
|
||||
for m in range(months):
|
||||
balance = monthly[m]["balance"]
|
||||
results.append({
|
||||
"account": account.name,
|
||||
"month": m + 1,
|
||||
"balance": round(balance, 2),
|
||||
"income": round(monthly[m]["income"], 2),
|
||||
"expenses": round(monthly[m]["expenses"], 2),
|
||||
"balance": monthly[m]["balance"],
|
||||
"income": monthly[m]["income"],
|
||||
"expenses": monthly[m]["expenses"],
|
||||
})
|
||||
|
||||
# Asset growth — once per month, distributed across accounts proportionally
|
||||
for m in range(months):
|
||||
total_growth = sum(
|
||||
a.value * a.growth_rate / 100 / 12
|
||||
for a in self.model.assets
|
||||
)
|
||||
month_rows = [r for r in results if r["month"] == m + 1]
|
||||
total_bal = sum(r["balance"] for r in month_rows) or 1
|
||||
for r in month_rows:
|
||||
share = r["balance"] / total_bal
|
||||
r["income"] = round(r["income"] + total_growth * share, 2)
|
||||
r["balance"] = round(r["balance"] + total_growth * share, 2)
|
||||
|
||||
# Compound asset values for next month
|
||||
for a in self.model.assets:
|
||||
a.value += a.value * a.growth_rate / 100 / 12
|
||||
|
||||
return results
|
||||
|
||||
def _project_account(self, account: Account, months: int) -> list[dict]:
|
||||
@@ -50,30 +66,17 @@ class ForecastService:
|
||||
else:
|
||||
expenses += abs(r.amount)
|
||||
|
||||
income += self._asset_income(account)
|
||||
expenses += self._liability_cost(account)
|
||||
|
||||
balance += income - expenses
|
||||
|
||||
asset_growth = sum(
|
||||
a.value * a.growth_rate / 12
|
||||
for a in self.model.assets
|
||||
)
|
||||
balance += asset_growth
|
||||
|
||||
monthly.append({
|
||||
"balance": balance,
|
||||
"income": income,
|
||||
"expenses": expenses,
|
||||
"balance": round(balance, 2),
|
||||
"income": round(income, 2),
|
||||
"expenses": round(expenses, 2),
|
||||
})
|
||||
return monthly
|
||||
|
||||
def _asset_income(self, account: Account) -> float:
|
||||
return sum(
|
||||
a.value * a.growth_rate / 12
|
||||
for a in self.model.assets
|
||||
)
|
||||
|
||||
def _liability_cost(self, account: Account) -> float:
|
||||
total = 0.0
|
||||
for liability in self.model.liabilities:
|
||||
|
||||
Reference in New Issue
Block a user