From 82a6a53dc4a9d8e4eecb012a85e48785bd221176 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Mon, 13 Mar 2023 21:04:39 +0800 Subject: [PATCH] Revised the account selector to find the account codes from the form through the TransactionForm class, but not finding the codes by itself. --- src/accounting/static/js/account-selector.js | 6 +-- src/accounting/static/js/transaction-form.js | 57 ++++++++++++++++++++ 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/src/accounting/static/js/account-selector.js b/src/accounting/static/js/account-selector.js index 98e56ff..d975241 100644 --- a/src/accounting/static/js/account-selector.js +++ b/src/accounting/static/js/account-selector.js @@ -150,14 +150,10 @@ class AccountSelector { * @return {string[]} the account codes that are used in the form */ #getCodesUsedInForm() { - const accountCodes = Array.from(document.getElementsByClassName("accounting-" + this.#prefix + "-account-code")); - const inUse = []; + const inUse = TransactionForm.getAccountCodesUsed(this.#entryType); if (this.#entryEditor.getAccountCode() !== null) { inUse.push(this.#entryEditor.getAccountCode()); } - for (const accountCode of accountCodes) { - inUse.push(accountCode.value); - } return inUse } diff --git a/src/accounting/static/js/transaction-form.js b/src/accounting/static/js/transaction-form.js index 43be1c1..4470b53 100644 --- a/src/accounting/static/js/transaction-form.js +++ b/src/accounting/static/js/transaction-form.js @@ -178,6 +178,20 @@ class TransactionForm { }); } + /** + * Returns the account codes used in the form. + * + * @param entryType {string} the entry type, either "debit" or "credit" + * @return {string[]} the account codes used in the form + */ + #getAccountCodesUsed(entryType) { + let inUse = []; + for (const currency of this.#currencies) { + inUse = inUse.concat(currency.getAccountCodesUsed(entryType)); + } + return inUse; + } + /** * Validates the form. * @@ -266,6 +280,16 @@ class TransactionForm { static initialize() { this.#form = new TransactionForm() } + + /** + * Returns the account codes used in the form. + * + * @param entryType {string} the entry type, either "debit" or "credit" + * @return {string[]} the account codes used in the form + */ + static getAccountCodesUsed(entryType) { + return this.#form.#getAccountCodesUsed(entryType); + } } /** @@ -359,6 +383,21 @@ class CurrencySubForm { }; } + /** + * Returns the account codes used in the form. + * + * @param entryType {string} the entry type, either "debit" or "credit" + * @return {string[]} the account codes used in the form + */ + getAccountCodesUsed(entryType) { + if (entryType === "debit") { + return this.#debit.getAccountCodesUsed(); + } else if (entryType === "credit") { + return this.#credit.getAccountCodesUsed(); + } + return [] + } + /** * Validates the form. * @@ -577,6 +616,15 @@ class DebitCreditSideSubForm { }); } + /** + * Returns the account codes used in the form. + * + * @return {string[]} the account codes used in the form + */ + getAccountCodesUsed() { + return this.#entries.filter((entry) => entry.getAccountCode() !== null).map((entry) => entry.getAccountCode()); + } + /** * Validates the form. * @@ -735,6 +783,15 @@ class JournalEntrySubForm { }; } + /** + * Returns the account code. + * + * @return {string|null} the account code + */ + getAccountCode() { + return this.#accountCode.value === ""? null: this.#accountCode.value; + } + /** * Validates the form. *