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(true); const showHidden = ref(false); const displayOrderModified = ref(false); const showAccountBalance = computed({ get: () => settingsStore.appSettings.showAccountBalance, set: (value) => settingsStore.setShowAccountBalance(value) }); const firstDayOfWeek = computed(() => userStore.currentUserFirstDayOfWeek); const fiscalYearStart = computed(() => userStore.currentUserFiscalYearStart); const defaultCurrency = computed(() => userStore.currentUserDefaultCurrency); const allAccounts = computed(() => accountsStore.allAccounts); const allCategorizedAccountsMap = computed>(() => accountsStore.allCategorizedAccountsMap); const allAccountCount = computed(() => accountsStore.allAvailableAccountsCount); const netAssets = computed(() => { const netAssets: number | HiddenAmount | NumberWithSuffix = accountsStore.getNetAssets(showAccountBalance.value); return formatAmountToLocalizedNumeralsWithCurrency(netAssets, defaultCurrency.value); }); const totalAssets = computed(() => { const totalAssets: number | HiddenAmount | NumberWithSuffix = accountsStore.getTotalAssets(showAccountBalance.value); return formatAmountToLocalizedNumeralsWithCurrency(totalAssets, defaultCurrency.value); }); const totalLiabilities = computed(() => { 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 }; }