diff --git a/src/accounting/models.py b/src/accounting/models.py
index 7cd8339..d0c88b4 100644
--- a/src/accounting/models.py
+++ b/src/accounting/models.py
@@ -203,25 +203,6 @@ class Account(db.Model):
return
self.l10n.append(AccountL10n(locale=current_locale, title=value))
- @property
- def is_in_use(self) -> bool:
- """Returns whether the account is in use.
-
- :return: True if the account is in use, or False otherwise.
- """
- if not hasattr(self, "__is_in_use"):
- setattr(self, "__is_in_use", len(self.entries) > 0)
- return getattr(self, "__is_in_use")
-
- @is_in_use.setter
- def is_in_use(self, is_in_use: bool) -> None:
- """Sets whether the account is in use.
-
- :param is_in_use: True if the account is in use, or False otherwise.
- :return: None.
- """
- setattr(self, "__is_in_use", is_in_use)
-
@classmethod
def find_by_code(cls, code: str) -> t.Self | None:
"""Finds an account by its code.
diff --git a/src/accounting/static/js/account-selector.js b/src/accounting/static/js/account-selector.js
index 96e2edd..69f13ad 100644
--- a/src/accounting/static/js/account-selector.js
+++ b/src/accounting/static/js/account-selector.js
@@ -140,7 +140,7 @@ class AccountSelector {
* @return {string[]} the account codes that are used in the form
*/
#getAccountCodeUsedInForm() {
- const accountCodes = Array.from(document.getElementsByClassName("accounting-account-code"));
+ const accountCodes = Array.from(document.getElementsByClassName("accounting-" + this.#prefix + "-account-code"));
const formAccount = document.getElementById("accounting-entry-form-account");
const inUse = [formAccount.dataset.code];
for (const accountCode of accountCodes) {
diff --git a/src/accounting/templates/accounting/transaction/include/form-entry-item.html b/src/accounting/templates/accounting/transaction/include/form-entry-item.html
index 427cf7e..8f593a2 100644
--- a/src/accounting/templates/accounting/transaction/include/form-entry-item.html
+++ b/src/accounting/templates/accounting/transaction/include/form-entry-item.html
@@ -25,7 +25,7 @@ First written: 2023/2/25
{% endif %}
-
+
diff --git a/src/accounting/transaction/forms.py b/src/accounting/transaction/forms.py
index 5faafea..184a416 100644
--- a/src/accounting/transaction/forms.py
+++ b/src/accounting/transaction/forms.py
@@ -115,6 +115,35 @@ class IsDebitAccount:
"This account is not for debit entries."))
+class AccountOption:
+ """An account option."""
+
+ def __init__(self, account: Account):
+ """Constructs an account option.
+
+ :param account: The account.
+ """
+ self.__account: Account = account
+ self.id: str = account.id
+ self.code: str = account.code
+ self.is_in_use: bool = False
+
+ def __str__(self) -> str:
+ """Returns the string representation of the account option.
+
+ :return: The string representation of the account option.
+ """
+ return str(self.__account)
+
+ @property
+ def query_values(self) -> list[str]:
+ """Returns the values to be queried.
+
+ :return: The values to be queried.
+ """
+ return self.__account.query_values
+
+
class JournalEntryForm(FlaskForm):
"""The base form to create or edit a journal entry."""
eid = IntegerField()
@@ -321,40 +350,37 @@ class TransactionForm(FlaskForm):
obj.no = count + 1
@property
- def debit_account_options(self) -> list[Account]:
+ def debit_account_options(self) -> list[AccountOption]:
"""The selectable debit accounts.
:return: The selectable debit accounts.
"""
- accounts: list[Account] = Account.debit()
- in_use: set[int] = self.__get_in_use_account_id()
+ accounts: list[AccountOption] \
+ = [AccountOption(x) for x in Account.debit()]
+ in_use: set[int] = set(db.session.scalars(
+ sa.select(JournalEntry.account_id)
+ .filter(JournalEntry.is_debit)
+ .group_by(JournalEntry.account_id)).all())
for account in accounts:
account.is_in_use = account.id in in_use
return accounts
@property
- def credit_account_options(self) -> list[Account]:
+ def credit_account_options(self) -> list[AccountOption]:
"""The selectable credit accounts.
:return: The selectable credit accounts.
"""
- accounts: list[Account] = Account.credit()
- in_use: set[int] = self.__get_in_use_account_id()
+ accounts: list[AccountOption] \
+ = [AccountOption(x) for x in Account.credit()]
+ in_use: set[int] = set(db.session.scalars(
+ sa.select(JournalEntry.account_id)
+ .filter(sa.not_(JournalEntry.is_debit))
+ .group_by(JournalEntry.account_id)).all())
for account in accounts:
account.is_in_use = account.id in in_use
return accounts
- def __get_in_use_account_id(self) -> set[int]:
- """Returns the ID of the accounts that are in use.
-
- :return: The ID of the accounts that are in use.
- """
- if self.__in_use_account_id is None:
- self.__in_use_account_id = set(db.session.scalars(
- sa.select(JournalEntry.account_id)
- .group_by(JournalEntry.account_id)).all())
- return self.__in_use_account_id
-
@property
def currencies_errors(self) -> list[str | LazyString]:
"""Returns the currency errors, without the errors in their sub-forms.