From e244ff70e6c74ce826f23937c84b52dc4286772c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Sun, 5 Mar 2023 22:34:52 +0800 Subject: [PATCH] Simplified the SQL query in the currency_options and account_options pseudo properties in the Ledger report and the account_options pseudo property in the IncomeExpenses report. --- src/accounting/report/reports.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/accounting/report/reports.py b/src/accounting/report/reports.py index 72d05bc..d4e2fda 100644 --- a/src/accounting/report/reports.py +++ b/src/accounting/report/reports.py @@ -389,9 +389,8 @@ class Ledger(JournalEntryReport[LedgerRow]): 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()) + in_use: sa.Select = sa.Select(JournalEntry.currency_code)\ + .group_by(JournalEntry.currency_code) 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()] @@ -410,10 +409,9 @@ class Ledger(JournalEntryReport[LedgerRow]): currency=self.currency, account=account, period=self.period) - in_use: set[int] = set(db.session.scalars( - sa.select(JournalEntry.account_id) - .filter(JournalEntry.currency_code == self.currency.code) - .group_by(JournalEntry.account_id)).all()) + in_use: sa.Select = sa.Select(JournalEntry.account_id)\ + .filter(JournalEntry.currency_code == self.currency.code)\ + .group_by(JournalEntry.account_id) return [OptionLink(str(x), get_url(x), x.id == self.account.id) for x in Account.query.filter(Account.id.in_(in_use)) .order_by(Account.base_code, Account.no).all()] @@ -621,15 +619,14 @@ class IncomeExpenses(JournalEntryReport[IncomeExpensesRow]): currency=self.currency, account=account, period=self.period) - in_use: set[int] = set(db.session.scalars( - sa.Select(JournalEntry.account_id) - .join(Account) + in_use: sa.Select = sa.Select(JournalEntry.account_id)\ + .join(Account)\ .filter(JournalEntry.currency_code == self.currency.code, sa.or_(Account.base_code.startswith("11"), Account.base_code.startswith("12"), Account.base_code.startswith("21"), - Account.base_code.startswith("22"))) - .group_by(JournalEntry.account_id)).all()) + Account.base_code.startswith("22")))\ + .group_by(JournalEntry.account_id) return [OptionLink(str(x), get_url(x), x.id == self.account.id) for x in Account.query.filter(Account.id.in_(in_use)) .order_by(Account.base_code, Account.no).all()]