Files
InvoiceShelf/resources/scripts/composables/use-currency.ts
Darko Gjorgjijoski 71388ec6a5 Rename resources/scripts-v2 to resources/scripts and drop @v2 alias
Now that the legacy v1 frontend (commit 064bdf53) is gone, the v2 directory is the only frontend and the v2 suffix is just noise. Renames resources/scripts-v2 to resources/scripts via git mv (so git records the move as renames, preserving blame and log --follow), then bulk-rewrites the 152 files that imported via @v2/... to use @/scripts/... instead. The existing @ alias (resources/) covers the new path with no extra config needed.

Drops the now-unused @v2 alias from vite.config.js and points the laravel-vite-plugin entry at resources/scripts/main.ts. Updates the only blade reference (resources/views/app.blade.php) to match. The package.json test script (eslint ./resources/scripts) automatically targets the right place after the rename without any edit.

Verified: npm run build exits clean and the Vite warning lines now reference resources/scripts/plugins/i18n.ts, confirming every import resolved through the new path. git log --follow on any moved file walks back through its scripts-v2 history.
2026-04-07 12:50:16 +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,
}
}