From 1e492d8724957c461adfd7c1afb7ba925f9129fb Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 27 Jul 2025 16:59:31 +0800 Subject: [PATCH] code refactor --- .../ReconciliationStatementPageBase.ts | 57 ++++++------------- .../dialogs/ReconciliationStatementDialog.vue | 4 +- 2 files changed, 20 insertions(+), 41 deletions(-) diff --git a/src/views/base/transactions/ReconciliationStatementPageBase.ts b/src/views/base/transactions/ReconciliationStatementPageBase.ts index 6a0299c6..d247e7ff 100644 --- a/src/views/base/transactions/ReconciliationStatementPageBase.ts +++ b/src/views/base/transactions/ReconciliationStatementPageBase.ts @@ -51,6 +51,10 @@ export function useReconciliationStatementPageBase() { const currentTimezoneOffsetMinutes = computed(() => getTimezoneOffsetMinutes(settingsStore.appSettings.timeZone)); const defaultCurrency = computed(() => userStore.currentUserDefaultCurrency); + const currentAccount = computed(() => allAccountsMap.value[accountId.value]); + const currentAccountCurrency = computed(() => currentAccount.value?.currency ?? defaultCurrency.value); + const isCurrentLiabilityAccount = computed(() => currentAccount.value?.isLiability ?? false); + const exportFileName = computed(() => { const nickname = userStore.currentUserNickname; @@ -66,16 +70,6 @@ export function useReconciliationStatementPageBase() { const allAccountsMap = computed>(() => accountsStore.allAccountsMap); const allCategoriesMap = computed>(() => transactionCategoriesStore.allTransactionCategoriesMap); - const accountCurrency = computed(() => { - let currency = defaultCurrency.value; - - if (allAccountsMap.value[accountId.value]) { - currency = allAccountsMap.value[accountId.value].currency; - } - - return currency; - }); - const totalOutflows = computed(() => { let totalOutflows = 0; @@ -117,42 +111,30 @@ export function useReconciliationStatementPageBase() { }); const displayTotalOutflows = computed(() => { - return formatAmountWithCurrency(totalOutflows.value, accountCurrency.value); + return formatAmountWithCurrency(totalOutflows.value, currentAccountCurrency.value); }); const displayTotalInflows = computed(() => { - return formatAmountWithCurrency(totalInflows.value, accountCurrency.value); + return formatAmountWithCurrency(totalInflows.value, currentAccountCurrency.value); }); const displayTotalBalance = computed(() => { - return formatAmountWithCurrency(totalInflows.value - totalOutflows.value, accountCurrency.value); + return formatAmountWithCurrency(totalInflows.value - totalOutflows.value, currentAccountCurrency.value); }); const displayOpeningBalance = computed(() => { - let isLiabilityAccount = false; - - if (allAccountsMap.value[accountId.value]) { - isLiabilityAccount = allAccountsMap.value[accountId.value].isLiability; - } - - if (isLiabilityAccount) { - return formatAmountWithCurrency(-openingBalance.value, accountCurrency.value); + if (isCurrentLiabilityAccount.value) { + return formatAmountWithCurrency(-openingBalance.value, currentAccountCurrency.value); } else { - return formatAmountWithCurrency(openingBalance.value, accountCurrency.value); + return formatAmountWithCurrency(openingBalance.value, currentAccountCurrency.value); } }); const displayClosingBalance = computed(() => { - let isLiabilityAccount = false; - - if (allAccountsMap.value[accountId.value]) { - isLiabilityAccount = allAccountsMap.value[accountId.value].isLiability; - } - - if (isLiabilityAccount) { - return formatAmountWithCurrency(-closingBalance.value, accountCurrency.value); + if (isCurrentLiabilityAccount.value) { + return formatAmountWithCurrency(-closingBalance.value, currentAccountCurrency.value); } else { - return formatAmountWithCurrency(closingBalance.value, accountCurrency.value); + return formatAmountWithCurrency(closingBalance.value, currentAccountCurrency.value); } }); @@ -213,14 +195,8 @@ export function useReconciliationStatementPageBase() { separator = '\t'; } - let isLiabilityAccount = false; - - if (allAccountsMap.value[accountId.value]) { - isLiabilityAccount = allAccountsMap.value[accountId.value].isLiability; - } - const digitGroupingSymbol = getCurrentDigitGroupingSymbol(); - const accountBalanceName = isLiabilityAccount ? 'Account Outstanding Balance' : 'Account Balance'; + const accountBalanceName = isCurrentLiabilityAccount.value ? 'Account Outstanding Balance' : 'Account Balance'; const header = [ tt('Transaction Time'), @@ -263,7 +239,7 @@ export function useReconciliationStatementPageBase() { let displayAccountBalance = ''; - if (isLiabilityAccount) { + if (isCurrentLiabilityAccount.value) { displayAccountBalance = removeAll(formatAmount(-transaction.accountBalance), digitGroupingSymbol); } else { displayAccountBalance = removeAll(formatAmount(transaction.accountBalance), digitGroupingSymbol); @@ -302,6 +278,9 @@ export function useReconciliationStatementPageBase() { // computed states currentTimezoneOffsetMinutes, defaultCurrency, + currentAccount, + currentAccountCurrency, + isCurrentLiabilityAccount, exportFileName, allAccountsMap, allCategoriesMap, diff --git a/src/views/desktop/accounts/list/dialogs/ReconciliationStatementDialog.vue b/src/views/desktop/accounts/list/dialogs/ReconciliationStatementDialog.vue index f5bd80c5..0d2894ba 100644 --- a/src/views/desktop/accounts/list/dialogs/ReconciliationStatementDialog.vue +++ b/src/views/desktop/accounts/list/dialogs/ReconciliationStatementDialog.vue @@ -244,6 +244,7 @@ const { currentTimezoneOffsetMinutes, allAccountsMap, allCategoriesMap, + isCurrentLiabilityAccount, exportFileName, displayStartDateTime, displayEndDateTime, @@ -274,7 +275,6 @@ const countPerPage = ref(10); let rejectFunc: ((reason?: unknown) => void) | null = null; -const account = computed(() => allAccountsMap.value[accountId.value]); const reconciliationStatementsTablePageOptions = computed(() => getTablePageOptions(reconciliationStatements.value?.length)); const totalPageCount = computed(() => { @@ -293,7 +293,7 @@ const totalPageCount = computed(() => { const dataTableHeaders = computed(() => { const headers: object[] = []; - const accountBalanceName = account.value?.isLiability ? 'Account Outstanding Balance' : 'Account Balance'; + const accountBalanceName = isCurrentLiabilityAccount.value ? 'Account Outstanding Balance' : 'Account Balance'; headers.push({ key: 'time', value: 'time', title: tt('Transaction Time'), sortable: true, nowrap: true }); headers.push({ key: 'type', value: 'type', title: tt('Type'), sortable: true, nowrap: true });