Files
InvoiceShelf/resources/scripts-v2/composables/use-currency.ts
Darko Gjorgjijoski 991b716b33 Phase 1: TypeScript foundation in scripts-v2/
Create the complete TypeScript foundation for the Vue 3 migration
in a parallel scripts-v2/ directory. 72 files, 5430 lines, zero
any types, strict mode.

- types/ (21 files): Domain interfaces for all 17 entities derived
  from actual Laravel models and API resources. Enums for all
  statuses. Generic API response wrappers.
- api/ (29 files): Typed axios client with interceptors, endpoint
  constants from routes/api.php, 25 typed service classes covering
  every API endpoint.
- composables/ (14 files): Vue 3 composition functions for auth,
  notifications, dialogs, modals, pagination, filters, currency,
  dates, theme, sidebar, company context, and permissions.
- utils/ (5 files): Pure typed utilities for money formatting,
  date formatting (date-fns), localStorage, and error handling.
- config/ (3 files): Typed ability constants, app constants.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 05:00:00 +02:00

106 lines
2.7 KiB
TypeScript

import { ref } from 'vue'
import type { Ref } from 'vue'
import { formatMoney as formatMoneyUtil } from '../utils/format-money'
import type { CurrencyConfig } from '../utils/format-money'
export interface Currency {
id: number
code: string
name: string
precision: number
thousand_separator: string
decimal_separator: string
symbol: string
swap_currency_symbol?: boolean
exchange_rate?: number
[key: string]: unknown
}
export interface UseCurrencyReturn {
currencies: Ref<Currency[]>
setCurrencies: (data: Currency[]) => void
formatMoney: (amountInCents: number, currency?: CurrencyConfig) => string
convertCurrency: (
amountInCents: number,
fromRate: number,
toRate: number
) => number
findCurrencyByCode: (code: string) => Currency | undefined
}
const currencies = ref<Currency[]>([])
/**
* Default currency configuration matching the v1 behavior.
*/
const DEFAULT_CURRENCY_CONFIG: CurrencyConfig = {
precision: 2,
thousand_separator: ',',
decimal_separator: '.',
symbol: '$',
swap_currency_symbol: false,
}
/**
* Composable for currency formatting and conversion.
* Maintains a shared list of available currencies.
*/
export function useCurrency(): UseCurrencyReturn {
function setCurrencies(data: Currency[]): void {
currencies.value = data
}
/**
* Format an amount in cents using the provided or default currency config.
*
* @param amountInCents - Amount in cents
* @param currency - Optional currency config override
* @returns Formatted currency string
*/
function formatMoney(
amountInCents: number,
currency?: CurrencyConfig
): string {
return formatMoneyUtil(amountInCents, currency ?? DEFAULT_CURRENCY_CONFIG)
}
/**
* Convert an amount from one currency to another using exchange rates.
*
* @param amountInCents - Amount in cents in the source currency
* @param fromRate - Exchange rate of the source currency
* @param toRate - Exchange rate of the target currency
* @returns Converted amount in cents
*/
function convertCurrency(
amountInCents: number,
fromRate: number,
toRate: number
): number {
if (fromRate === 0) {
return 0
}
return Math.round((amountInCents / fromRate) * toRate)
}
/**
* Find a currency by its ISO code.
*
* @param code - The ISO 4217 currency code (e.g. "USD")
* @returns The matching Currency, or undefined
*/
function findCurrencyByCode(code: string): Currency | undefined {
return currencies.value.find(
(c) => c.code.toUpperCase() === code.toUpperCase()
)
}
return {
currencies,
setCurrencies,
formatMoney,
convertCurrency,
findCurrencyByCode,
}
}