Skip to content

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

fix: align dashboard drilldown totals (#379)

Albert Dolkunbcd27e422a62
diff --git a/backend/app/api/transactions.py b/backend/app/api/transactions.py
index 0f5fff1b..a33bacf0 100644
--- a/backend/app/api/transactions.py
+++ b/backend/app/api/transactions.py
@@ -94,6 +94,7 @@ async def list_transactions(
limit: int = Query(50, ge=1, le=500),
include_opening_balance: bool = Query(False),
exclude_transfers: bool = Query(False),
+ user_pnl_only: bool = Query(False, description="Return only rows that count toward dashboard/user income/expense totals"),
tags: Optional[List[str]] = Query(None),
min_amount: Optional[float] = Query(None, ge=0, description="Filter to transactions with absolute amount >= this value (primary currency)."),
max_amount: Optional[float] = Query(None, ge=0, description="Filter to transactions with absolute amount <= this value (primary currency)."),
@@ -110,6 +111,7 @@ async def list_transactions(
payee_id=payee_id, from_date=from_date, to_date=to_date, page=page, limit=limit,
include_opening_balance=include_opening_balance, search=q, uncategorized=uncategorized,
txn_type=type, exclude_transfers=exclude_transfers,
+ user_pnl_only=user_pnl_only,
accounting_mode=accounting_mode,
tags=tags,
bill_id=bill_id,
diff --git a/backend/app/services/dashboard_service.py b/backend/app/services/dashboard_service.py
index ca493332..5f1e534a 100644
--- a/backend/app/services/dashboard_service.py
+++ b/backend/app/services/dashboard_service.py
@@ -54,10 +54,19 @@ async def _get_recurring_projections(
return []
stmt = (
select(RecurringTransaction)
+ .outerjoin(Category, RecurringTransaction.category_id == Category.id)
.where(
RecurringTransaction.workspace_id == workspace_id,
RecurringTransaction.is_active == True,
RecurringTransaction.start_date < month_end,
+ or_(
+ RecurringTransaction.category_id.is_(None),
+ Category.treat_as_transfer.is_not(True),
+ ),
+ or_(
+ RecurringTransaction.category_id.is_(None),
+ Category.is_ignored.is_not(True),
+ ),
)
)
if account_ids:
@@ -731,10 +740,19 @@ async def get_projected_transactions(
result = await session.execute(
select(RecurringTransaction)
+ .outerjoin(Category, RecurringTransaction.category_id == Category.id)
.where(
RecurringTransaction.workspace_id == workspace_id,
RecurringTransaction.is_active == True,
RecurringTransaction.start_date < month_end,
+ or_(
+ RecurringTransaction.category_id.is_(None),
+ Category.treat_as_transfer.is_not(True),
+ ),
+ or_(
+ RecurringTransaction.category_id.is_(None),
+ Category.is_ignored.is_not(True),
+ ),
)
)
recurring_list = list(result.scalars().all())
diff --git a/backend/app/services/transaction_service.py b/backend/app/services/transaction_service.py
index 23e7abb0..e368e7b5 100644
--- a/backend/app/services/transaction_service.py
+++ b/backend/app/services/transaction_service.py
@@ -21,7 +21,7 @@ from app.services import split_service
from app.services.credit_card_service import apply_effective_date
from app.services.rule_service import apply_rules_to_transaction
from app.services.fx_rate_service import stamp_primary_amount, convert as fx_convert
-from app.services._query_filters import counts_as_pnl, reporting_date_col
+from app.services._query_filters import counts_as_pnl, counts_as_user_pnl, reporting_date_col
def _apply_fx_override(transaction, amount, amount_primary=None, fx_rate_used=None):
@@ -82,6 +82,7 @@ async def get_transactions(
max_amount: Optional[float] = None,
account_types: Optional[list[str]] = None,
include_summary: bool = False,
+ user_pnl_only: bool = False,
) -> tuple[list[Transaction], int, Optional[dict]]:
"""List transactions for a workspace.
@@ -214,6 +215,8 @@ async def get_transactions(
)
if exclude_transfers:
base_query = base_query.where(Transaction.transfer_pair_id.is_(None))
+ if user_pnl_only:
+ base_query = base_query.where(Account.is_closed == False, counts_as_user_pnl())
if txn_type:
base_query = base_query.where(Transaction.type == txn_type)
if currency:
diff --git a/backend/tests/test_dashboard_service.py b/backend/tests/test_dashboard_service.py
index d6d60bbc..433a7315 100644
--- a/backend/tests/test_dashboard_service.py
+++ b/backend/tests/test_dashboard_service.py
@@ -22,6 +22,7 @@ from app.services.dashboard_service import (
get_spending_by_category,
get_projected_transactions,
)
+from app.services.transaction_service import get_transactions
# ---------------------------------------------------------------------------
@@ -193,6 +194,9 @@ async def test_account_balance_at_credit_card_connected(session: AsyncSession, t
# ---------------------------------------------------------------------------
+
+
+
@pytest.mark.asyncio
async def test_total_balance_by_currency(session: AsyncSession, test_user, test_workspace):
"""Total balance groups by currency."""
@@ -229,6 +233,42 @@ async def test_get_summary_basic(session: AsyncSession, test_user, test_workspac
assert summary.accounts_count >= 1
+async def test_summary_matches_drilldown_and_excludes_closed_accounts(
+ session: AsyncSession, test_user, test_workspace
+):
+ month = (date.today().replace(day=1) - timedelta(days=1)).replace(day=1)
+ month_start, month_end = _month_range(month)
+ open_account = await _make_account(session, test_user.id, "Open")
+ closed_account = await _make_account(
+ session, test_user.id, "Closed", is_closed=True
+ )
+
+ await _add_txn(session, test_user.id, open_account.id, 100, "credit", month_start)
+ await _add_txn(session, test_user.id, open_account.id, 40, "debit", month_start)
+ await _add_txn(session, test_user.id, closed_account.id, 900, "credit", month_start)
+ await _add_txn(session, test_user.id, closed_account.id, 800, "debit", month_start)
+
+ summary = await get_summary(session, test_workspace.id, test_user.id, month=month)
+ rows, _, _ = await get_transactions(
+ session,
+ test_workspace.id,
+ test_user.id,
+ from_date=month_start,
+ to_date=month_end - timedelta(days=1),
+ skip_pagination=True,
+ user_pnl_only=True,
+ )
+
+ assert {row.account_id for row in rows} == {open_account.id}
+ assert summary.monthly_income == pytest.approx(
+ sum(float(row.amount) for row in rows if row.type == "credit")
+ )
+ assert summary.monthly_expenses == pytest.approx(
+ sum(float(row.amount) for row in rows if row.type == "debit")
+ )
+
+
@pytest.mark.asyncio
async def test_get_summary_excludes_opening_balance_from_income(session: AsyncSession, test_user, test_workspace):
"""Opening balance does not count as monthly income."""
@@ -588,6 +628,60 @@ async def test_get_recurring_projections(session, test_user, test_workspace):
assert projections[0]["amount"] == 2000.0
+async def test_get_recurring_projections_excludes_ignored_categories(
+ session, test_user, test_workspace
+):
+ month_start = date.today().replace(day=1)
+ month_end = (
+ date(month_start.year + 1, 1, 1)
+ if month_start.month == 12
+ else date(month_start.year, month_start.month + 1, 1)
+ )
+ ignored = Category(
+ id=uuid.uuid4(),
+ user_id=test_user.id,
+ workspace_id=test_workspace.id,
+ name="Ignored",
+ is_ignored=True,
+ )
+ transfer_like = Category(
+ id=uuid.uuid4(),
+ user_id=test_user.id,
+ workspace_id=test_workspace.id,
+ name="Transfer",
+ treat_as_transfer=True,
+ )
+ session.add_all([ignored, transfer_like])
+ await session.commit()
+
+ for description, amount, category_id in (
+ ("Counted", "100", None),
+ ("Ignored", "200", ignored.id),
+ ("Transfer-like", "300", transfer_like.id),
+ ):
+ session.add(RecurringTransaction(
+ id=uuid.uuid4(),
+ user_id=test_user.id,
+ workspace_id=test_workspace.id,
+ description=description,
+ amount=Decimal(amount),
+ type="debit",
+ frequency="monthly",
+ start_date=month_start,
+ next_occurrence=month_start,
+ currency="BRL",
+ category_id=category_id,
+ ))
+ await session.commit()
+
+ projections = await _get_recurring_projections(
+ session, test_workspace.id, month_start, month_end
+ )
+
+ assert [p["amount"] for p in projections] == [100.0]
+
+
@pytest.mark.asyncio
async def test_balance_history_basic(session, test_user, test_workspace):
acct = Account(
@@ -758,5 +852,3 @@ async def test_balance_at_multi_currency(session, test_user, test_workspace):
# ---------------------------------------------------------------------------
# _total_balance_by_currency
# ---------------------------------------------------------------------------
-
-
diff --git a/backend/tests/test_transaction_service_coverage.py b/backend/tests/test_transaction_service_coverage.py
index 02680cbc..199a3399 100644
--- a/backend/tests/test_transaction_service_coverage.py
+++ b/backend/tests/test_transaction_service_coverage.py
@@ -290,6 +290,52 @@ async def test_get_transactions_summary_excludes_transfers_and_treat_as_transfer
assert summary["excluded"] == Decimal("1300")
+async def test_get_transactions_user_pnl_only_returns_only_dashboard_reportable_rows(
+ session, test_user, test_workspace, acct
+):
+ from app.models.category import Category
+
+ ignored = Category(
+ id=uuid.uuid4(),
+ user_id=test_user.id,
+ workspace_id=test_workspace.id,
+ name="Ignored",
+ is_ignored=True,
+ )
+ transfer_like = Category(
+ id=uuid.uuid4(),
+ user_id=test_user.id,
+ workspace_id=test_workspace.id,
+ name="Investments",
+ treat_as_transfer=True,
+ )
+ session.add_all([ignored, transfer_like])
+ await session.commit()
+
+ pair = uuid.uuid4()
+ await _mk_txn(session, test_user, acct, description="Salary", amount=Decimal("1000"), type="credit")
+ await _mk_txn(session, test_user, acct, description="Rent", amount=Decimal("300"), type="debit")
+ await _mk_txn(session, test_user, acct, description="Hidden", is_ignored=True)
+ await _mk_txn(session, test_user, acct, description="Hidden category", category_id=ignored.id)
+ await _mk_txn(session, test_user, acct, description="Investment", category_id=transfer_like.id)
+ await _mk_txn(session, test_user, acct, description="Transfer", transfer_pair_id=pair)
+ await _mk_txn(session, test_user, acct, description="Settlement credit", type="credit", source="settlement")
+
+ rows, total, summary = await get_transactions(
+ session,
+ test_workspace.id,
+ test_user.id,
+ user_pnl_only=True,
+ include_summary=True,
+ )
+
+ assert {tx.description for tx in rows} == {"Salary", "Rent"}
+ assert total == 2
+ assert summary["income"] == Decimal("1000")
+ assert summary["expense"] == Decimal("300")
+ assert summary["excluded"] == Decimal("0")
+
+
async def test_get_transactions_sorting(session, test_user, test_workspace, acct):
await _mk_txn(session, test_user, acct, description="Aaa", amount=Decimal("30"))
await _mk_txn(session, test_user, acct, description="Zzz", amount=Decimal("10"))
diff --git a/backend/tests/test_transactions_api.py b/backend/tests/test_transactions_api.py
index bd3ea0a9..8832e247 100644
--- a/backend/tests/test_transactions_api.py
+++ b/backend/tests/test_transactions_api.py
@@ -612,6 +612,39 @@ async def test_list_transactions_exclude_transfers(
assert "Transfer in" not in descriptions
+async def test_list_transactions_user_pnl_only(
+ client: AsyncClient,
+ auth_headers,
+ session: AsyncSession,
+ test_transactions_with_transfers,
+):
+ """user_pnl_only returns only rows that count toward dashboard/user P&L."""
+ for txn in test_transactions_with_transfers:
+ if txn.description == "GROCERIES":
+ txn.is_ignored = True
+ settlement = Transaction(
+ id=uuid.uuid4(),
+ user_id=test_transactions_with_transfers[0].user_id,
+ account_id=test_transactions_with_transfers[0].account_id,
+ description="Settlement credit",
+ amount=Decimal("75.00"),
+ date=date.today(),
+ type="credit",
+ source="settlement",
+ created_at=datetime.now(timezone.utc),
+ )
+ session.add(settlement)
+ await session.commit()
+
+ response = await client.get("/api/transactions?user_pnl_only=true", headers=auth_headers)
+
+ assert response.status_code == 200
+ data = response.json()
+ assert data["total"] == 1
+ assert [item["description"] for item in data["items"]] == ["SALARY"]
+
+
@pytest.mark.asyncio
async def test_exclude_transfers_false_includes_all(
client: AsyncClient, auth_headers, test_transactions_with_transfers,
diff --git a/frontend/src/components/transaction-drill-down.tsx b/frontend/src/components/transaction-drill-down.tsx
index 0fa06dd1..26f76039 100644
--- a/frontend/src/components/transaction-drill-down.tsx
+++ b/frontend/src/components/transaction-drill-down.tsx
@@ -70,7 +70,7 @@ export function TransactionDrillDown({
from: filter?.from,
to: filter?.to,
limit: 200,
- exclude_transfers: true,
+ user_pnl_only: true,
}),
enabled: !!filter,
})
diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts
index 439685dd..6188cbf4 100644
--- a/frontend/src/lib/api.ts
+++ b/frontend/src/lib/api.ts
@@ -439,6 +439,7 @@ export const transactions = {
limit?: number
include_opening_balance?: boolean
exclude_transfers?: boolean
+ user_pnl_only?: boolean
tags?: string[]
min_amount?: number
max_amount?: number
diff --git a/frontend/src/pages/dashboard.tsx b/frontend/src/pages/dashboard.tsx
index d1ec54a5..da43b3cf 100644
--- a/frontend/src/pages/dashboard.tsx
+++ b/frontend/src/pages/dashboard.tsx
@@ -490,7 +490,7 @@ export default function DashboardPage() {
groupId: null,
parentOwnerName: null,
groupName: null,
- isIgnored: pt.is_ignored
+ isIgnored: false
})
}
rows.sort((a, b) => txSortDesc ? b.date.localeCompare(a.date) : a.date.localeCompare(b.date))
diff --git a/frontend/src/types/index.ts b/frontend/src/types/index.ts
index 9155604b..61369055 100644
--- a/frontend/src/types/index.ts
+++ b/frontend/src/types/index.ts
@@ -451,7 +451,6 @@ export interface ProjectedTransaction {
category_name: string | null
category_icon: string | null
category_color: string | null
- is_ignored: boolean
}
export interface DashboardSummary {