Skip to content

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

fix: join accounts in previous balance query (#593)

Chris Lopezbfd122d66ca0
diff --git a/backend/app/services/account_service.py b/backend/app/services/account_service.py
index 959d8bc5..c53dfa65 100644
--- a/backend/app/services/account_service.py
+++ b/backend/app/services/account_service.py
@@ -90,6 +90,7 @@ async def get_accounts(session: AsyncSession, workspace_id: uuid.UUID, include_c
Transaction.account_id,
func.coalesce(func.sum(signed_amount), 0).label("previous_balance"),
)
+ .join(Account, Transaction.account_id == Account.id)
.outerjoin(Category, Transaction.category_id == Category.id)
.where(
Transaction.date <= prev_month_end,
diff --git a/backend/tests/test_account_service.py b/backend/tests/test_account_service.py
index c6e9115d..ecc42064 100644
--- a/backend/tests/test_account_service.py
+++ b/backend/tests/test_account_service.py
@@ -647,6 +647,33 @@ async def test_get_accounts_returns_list(session: AsyncSession, test_user, test_
assert "previous_balance" in acc
+async def test_get_accounts_previous_balance_uses_each_account_currency(
+ session: AsyncSession, test_user, test_workspace
+):
+ """Previous balances convert each transaction with its own account currency."""
+ previous_month = date.today().replace(day=1) - timedelta(days=1)
+ brl_account = await _make_account(session, test_user.id, "BRL Account", currency="BRL")
+ usd_account = await _make_account(session, test_user.id, "USD Account", currency="USD")
+
+ brl_txn = await _add_txn(
+ session, test_user.id, brl_account.id, 100, "credit", previous_month
+ )
+ usd_txn = await _add_txn(
+ session, test_user.id, usd_account.id, 20, "credit", previous_month
+ )
+ usd_txn.currency = "USD"
+ brl_txn.amount_primary = Decimal("500.00")
+ usd_txn.amount_primary = Decimal("100.00")
+ await session.commit()
+
+ accounts = await get_accounts(session, test_workspace.id)
+ by_id = {account["id"]: account for account in accounts}
+
+ assert by_id[brl_account.id]["previous_balance"] == pytest.approx(100.0)
+ assert by_id[usd_account.id]["previous_balance"] == pytest.approx(20.0)
+
+
@pytest.mark.asyncio
async def test_get_accounts_excludes_closed(session: AsyncSession, test_user, test_workspace):
"""get_accounts excludes closed accounts by default."""