diff --git a/src/accounting/report/reports/balance_sheet.py b/src/accounting/report/reports/balance_sheet.py index cad4679..5ea269e 100644 --- a/src/accounting/report/reports/balance_sheet.py +++ b/src/accounting/report/reports/balance_sheet.py @@ -362,12 +362,7 @@ class PageParams(BasePageParams): return url_for("accounting.report.balance-sheet", currency=currency, period=self.period) - in_use: set[str] = set(db.session.scalars( - sa.select(JournalEntry.currency_code) - .group_by(JournalEntry.currency_code)).all()) - return [OptionLink(str(x), get_url(x), x.code == self.currency.code) - for x in Currency.query.filter(Currency.code.in_(in_use)) - .order_by(Currency.code).all()] + return self._get_currency_options(get_url, self.currency) class BalanceSheet(BaseReport): diff --git a/src/accounting/report/reports/income_expenses.py b/src/accounting/report/reports/income_expenses.py index e751385..218c783 100644 --- a/src/accounting/report/reports/income_expenses.py +++ b/src/accounting/report/reports/income_expenses.py @@ -325,12 +325,7 @@ class PageParams(BasePageParams): currency=currency, account=self.account, period=self.period) - in_use: set[str] = set(db.session.scalars( - sa.select(JournalEntry.currency_code) - .group_by(JournalEntry.currency_code)).all()) - return [OptionLink(str(x), get_url(x), x.code == self.currency.code) - for x in Currency.query.filter(Currency.code.in_(in_use)) - .order_by(Currency.code).all()] + return self._get_currency_options(get_url, self.currency) @property def account_options(self) -> list[OptionLink]: diff --git a/src/accounting/report/reports/income_statement.py b/src/accounting/report/reports/income_statement.py index 578151d..5e0bb23 100644 --- a/src/accounting/report/reports/income_statement.py +++ b/src/accounting/report/reports/income_statement.py @@ -193,12 +193,7 @@ class PageParams(BasePageParams): return url_for("accounting.report.income-statement", currency=currency, period=self.period) - in_use: set[str] = set(db.session.scalars( - sa.select(JournalEntry.currency_code) - .group_by(JournalEntry.currency_code)).all()) - return [OptionLink(str(x), get_url(x), x.code == self.currency.code) - for x in Currency.query.filter(Currency.code.in_(in_use)) - .order_by(Currency.code).all()] + return self._get_currency_options(get_url, self.currency) class IncomeStatement(BaseReport): diff --git a/src/accounting/report/reports/ledger.py b/src/accounting/report/reports/ledger.py index 032c6df..9a701ea 100644 --- a/src/accounting/report/reports/ledger.py +++ b/src/accounting/report/reports/ledger.py @@ -297,12 +297,7 @@ class PageParams(BasePageParams): currency=currency, account=self.account, period=self.period) - in_use: set[str] = set(db.session.scalars( - sa.select(JournalEntry.currency_code) - .group_by(JournalEntry.currency_code)).all()) - return [OptionLink(str(x), get_url(x), x.code == self.currency.code) - for x in Currency.query.filter(Currency.code.in_(in_use)) - .order_by(Currency.code).all()] + return self._get_currency_options(get_url, self.currency) @property def account_options(self) -> list[OptionLink]: diff --git a/src/accounting/report/reports/trial_balance.py b/src/accounting/report/reports/trial_balance.py index 7b59592..dedd6d7 100644 --- a/src/accounting/report/reports/trial_balance.py +++ b/src/accounting/report/reports/trial_balance.py @@ -155,12 +155,7 @@ class PageParams(BasePageParams): return url_for("accounting.report.trial-balance", currency=currency, period=self.period) - in_use: set[str] = set(db.session.scalars( - sa.select(JournalEntry.currency_code) - .group_by(JournalEntry.currency_code)).all()) - return [OptionLink(str(x), get_url(x), x.code == self.currency.code) - for x in Currency.query.filter(Currency.code.in_(in_use)) - .order_by(Currency.code).all()] + return self._get_currency_options(get_url, self.currency) class TrialBalance(BaseReport): diff --git a/src/accounting/report/reports/utils/base_page_params.py b/src/accounting/report/reports/utils/base_page_params.py index 60b27df..436942c 100644 --- a/src/accounting/report/reports/utils/base_page_params.py +++ b/src/accounting/report/reports/utils/base_page_params.py @@ -22,9 +22,13 @@ from abc import ABC, abstractmethod from urllib.parse import urlparse, ParseResult, parse_qsl, urlencode, \ urlunparse +import sqlalchemy as sa from flask import request +from accounting import db +from accounting.models import Currency, JournalEntry from accounting.utils.txn_types import TransactionType +from .option_link import OptionLink from .report_chooser import ReportChooser @@ -66,3 +70,19 @@ class BasePageParams(ABC): parts: list[str] = list(uri_p) parts[4] = urlencode(params) return urlunparse(parts) + + @staticmethod + def _get_currency_options(get_url: t.Callable[[Currency], str], + active_currency: Currency) -> list[OptionLink]: + """Returns the currency options. + + :param get_url: The callback to return the URL of a currency. + :param active_currency: The active currency. + :return: The currency options. + """ + in_use: set[str] = set(db.session.scalars( + sa.select(JournalEntry.currency_code) + .group_by(JournalEntry.currency_code)).all()) + return [OptionLink(str(x), get_url(x), x.code == active_currency.code) + for x in Currency.query.filter(Currency.code.in_(in_use)) + .order_by(Currency.code).all()]