Skip to content

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

fix(dashboard): keep opening balances out of spending by category (#1175)

Albert Dolkun0bce6f7eb856
diff --git a/backend/app/services/dashboard_service.py b/backend/app/services/dashboard_service.py
index f3538b76..bf4e4cc8 100644
--- a/backend/app/services/dashboard_service.py
+++ b/backend/app/services/dashboard_service.py
@@ -751,6 +751,7 @@ async def get_spending_by_category(
report_date >= month_start,
report_date < month_end,
report_date <= today,
+ Transaction.source != "opening_balance",
Transaction.status == "posted",
counts_as_user_pnl(),
*acct_filter,
diff --git a/backend/tests/test_dashboard_service.py b/backend/tests/test_dashboard_service.py
index 97969922..7b5a786a 100644
--- a/backend/tests/test_dashboard_service.py
+++ b/backend/tests/test_dashboard_service.py
@@ -569,6 +569,25 @@ async def test_spending_excludes_transfers(session: AsyncSession, test_user, tes
assert 500 not in [s.total for s in spending] or total == 0
+async def test_spending_excludes_opening_balance(session: AsyncSession, test_user, test_workspace):
+ """A debt carried in as an opening balance is not spending, so the
+ breakdown still adds up to the expenses card."""
+ cat = await _make_category(session, test_user.id, "Groceries")
+ account = await _make_account(session, test_user.id, "OB Card", acc_type="credit_card")
+ today = date.today()
+
+ await _add_txn(session, test_user.id, account.id, 2500, "debit", today, source="opening_balance")
+ await _add_txn(session, test_user.id, account.id, 40, "debit", today, category_id=cat.id)
+
+ spending = await get_spending_by_category(session, test_workspace.id, test_user.id)
+ assert next((s for s in spending if s.category_id is None), None) is None
+ assert sum(s.total for s in spending) == pytest.approx(40.0)
+
+ summary = await get_summary(session, test_workspace.id, test_user.id)
+ assert sum(s.total for s in spending) == pytest.approx(summary.monthly_expenses)
+
+
@pytest.mark.asyncio
async def test_spending_percentage(session: AsyncSession, test_user, test_workspace):
"""Percentages sum to approximately 100%."""