Files
ezbookkeeping/src/views/base/accounts/AccountListPageBase.ts
T
2025-08-17 01:55:19 +08:00

107 lines
4.3 KiB
TypeScript

import { ref, computed } from 'vue';
import { useI18n } from '@/locales/helpers.ts';
import { useSettingsStore } from '@/stores/setting.ts';
import { useUserStore } from '@/stores/user.ts';
import { useAccountsStore } from '@/stores/account.ts';
import type { HiddenAmount, NumberWithSuffix } from '@/core/numeral.ts';
import type { WeekDayValue } from '@/core/datetime.ts';
import { type AccountCategory, AccountType } from '@/core/account.ts';
import type { Account, CategorizedAccount } from '@/models/account.ts';
import { isObject, isNumber, isString } from '@/lib/common.ts';
export function useAccountListPageBaseBase() {
const { formatAmountToLocalizedNumeralsWithCurrency } = useI18n();
const settingsStore = useSettingsStore();
const userStore = useUserStore();
const accountsStore = useAccountsStore();
const loading = ref<boolean>(true);
const showHidden = ref<boolean>(false);
const displayOrderModified = ref<boolean>(false);
const showAccountBalance = computed<boolean>({
get: () => settingsStore.appSettings.showAccountBalance,
set: (value) => settingsStore.setShowAccountBalance(value)
});
const firstDayOfWeek = computed<WeekDayValue>(() => userStore.currentUserFirstDayOfWeek);
const fiscalYearStart = computed<number>(() => userStore.currentUserFiscalYearStart);
const defaultCurrency = computed<string>(() => userStore.currentUserDefaultCurrency);
const allAccounts = computed<Account[]>(() => accountsStore.allAccounts);
const allCategorizedAccountsMap = computed<Record<number, CategorizedAccount>>(() => accountsStore.allCategorizedAccountsMap);
const allAccountCount = computed<number>(() => accountsStore.allAvailableAccountsCount);
const netAssets = computed<string>(() => {
const netAssets: number | HiddenAmount | NumberWithSuffix = accountsStore.getNetAssets(showAccountBalance.value);
return formatAmountToLocalizedNumeralsWithCurrency(netAssets, defaultCurrency.value);
});
const totalAssets = computed<string>(() => {
const totalAssets: number | HiddenAmount | NumberWithSuffix = accountsStore.getTotalAssets(showAccountBalance.value);
return formatAmountToLocalizedNumeralsWithCurrency(totalAssets, defaultCurrency.value);
});
const totalLiabilities = computed<string>(() => {
const totalLiabilities: number | HiddenAmount | NumberWithSuffix = accountsStore.getTotalLiabilities(showAccountBalance.value);
return formatAmountToLocalizedNumeralsWithCurrency(totalLiabilities, defaultCurrency.value);
});
function accountCategoryTotalBalance(accountCategory?: AccountCategory): string {
if (!accountCategory) {
return '';
}
const totalBalance: number | HiddenAmount | NumberWithSuffix = accountsStore.getAccountCategoryTotalBalance(showAccountBalance.value, accountCategory);
return formatAmountToLocalizedNumeralsWithCurrency(totalBalance, defaultCurrency.value);
}
function accountBalance(account: Account, currentSubAccountId?: string): string | null {
if (account.type === AccountType.SingleAccount.type) {
const balance: number| HiddenAmount | null = accountsStore.getAccountBalance(showAccountBalance.value, account);
if (!isNumber(balance) && !isString(balance)) {
return '';
}
return formatAmountToLocalizedNumeralsWithCurrency(balance, account.currency);
} else if (account.type === AccountType.MultiSubAccounts.type) {
const balanceResult = accountsStore.getAccountSubAccountBalance(showAccountBalance.value, showHidden.value, account, currentSubAccountId);
if (!isObject(balanceResult)) {
return '';
}
return formatAmountToLocalizedNumeralsWithCurrency(balanceResult.balance, balanceResult.currency);
} else {
return null;
}
}
return {
// states
loading,
showHidden,
displayOrderModified,
// computed states
showAccountBalance,
firstDayOfWeek,
fiscalYearStart,
defaultCurrency,
allAccounts,
allCategorizedAccountsMap,
allAccountCount,
netAssets,
totalAssets,
totalLiabilities,
// functions
accountCategoryTotalBalance,
accountBalance
};
}