Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

fix(report): handle negative balance accounts in net worth calculations as liabilities, stop ignoring them in the networth report composition (#284)

Geovane Pereira5fafa1bd3b54
diff --git a/backend/app/services/report_service.py b/backend/app/services/report_service.py
index 42e759a9..7ef28bea 100644
--- a/backend/app/services/report_service.py
+++ b/backend/app/services/report_service.py
@@ -74,13 +74,10 @@ async def _net_worth_at(
session, Decimal(str(abs(bal))), account.currency, primary_currency, cutoff
)
converted_val = float(converted)
- if account.type == "credit_card":
+ if account.type == "credit_card" or bal < 0:
liabilities_total += converted_val
else:
- if bal < 0:
- accounts_total -= converted_val
- else:
- accounts_total += converted_val
+ accounts_total += converted_val
assets_total = await _asset_value_at(session, workspace_id, cutoff, primary_currency)
net_worth = accounts_total + assets_total - liabilities_total
@@ -269,6 +266,14 @@ async def get_net_worth_report(
color=account_type_colors.get(account.type, "#6B7280"),
group="accounts",
))
+ elif bal < 0:
+ composition.append(ReportCompositionItem(
+ key=str(account.id),
+ label=get_account_name(account),
+ value=round(converted_val, 2),
+ color="#F43F5E",
+ group="liabilities",
+ ))
# Assets — scoped to workspace
asset_result = await session.execute(
diff --git a/backend/tests/test_report_service.py b/backend/tests/test_report_service.py
index 69aed013..17e5eb3b 100644
--- a/backend/tests/test_report_service.py
+++ b/backend/tests/test_report_service.py
@@ -851,7 +851,20 @@ async def test_net_worth_negative_manual_balance(session: AsyncSession, test_use
await _add_txn(session, test_user.id, acct.id, 1000, "debit", date.today())
dp = await _net_worth_at(session, test_workspace.id, date.today(), "BRL")
- assert dp.breakdowns["accounts"] == -1000.0
+ assert dp.breakdowns["accounts"] == 0.0
+ assert dp.breakdowns["liabilities"] == 1000.0
+ assert dp.value == -1000.0
+
+
+async def test_net_worth_negative_account_in_composition(session: AsyncSession, test_user, test_workspace: User):
+ acct = await _make_manual_account(session, test_user.id, "Overdrawn Acct")
+ await _add_txn(session, test_user.id, acct.id, 500, "debit", date.today())
+
+ report = await get_net_worth_report(session, test_workspace.id, test_user.id, months=1, interval="monthly")
+ liability_items = [c for c in report.composition if c.group == "liabilities"]
+ labels = [c.label for c in liability_items]
+ assert "Overdrawn Acct" in labels
# ---------------------------------------------------------------------------