- ČNB exchange rate service with date-specific rates and caching - Invoice/received invoice stats convert foreign currencies to CZK - Dashboard revenue converts all currencies to CZK - Invoice PDF: VAT recap table always in CZK with CNB rate footer - Inline styles replaced with utility classes (step 4 cleanup) - Spinner animation exempt from prefers-reduced-motion Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
/**
|
|
* Czech National Bank (ČNB) exchange rate service.
|
|
* Fetches daily rates and caches them.
|
|
* API: https://api.cnb.cz/cnbapi/exrates/daily
|
|
*/
|
|
|
|
interface CnbRate {
|
|
currencyCode: string;
|
|
rate: number;
|
|
amount: number;
|
|
}
|
|
|
|
const rateCache: Record<string, Record<string, number>> = {};
|
|
|
|
async function fetchRatesForDate(
|
|
date?: string,
|
|
): Promise<Record<string, number>> {
|
|
const key = date || "today";
|
|
if (rateCache[key]) return rateCache[key];
|
|
|
|
try {
|
|
let url = "https://api.cnb.cz/cnbapi/exrates/daily?lang=EN";
|
|
if (date) url += `&date=${date}`;
|
|
|
|
const response = await fetch(url);
|
|
if (!response.ok) throw new Error(`CNB API: ${response.status}`);
|
|
|
|
const data = (await response.json()) as { rates: CnbRate[] };
|
|
const rates: Record<string, number> = { CZK: 1 };
|
|
|
|
for (const r of data.rates) {
|
|
rates[r.currencyCode] = r.rate / r.amount;
|
|
}
|
|
|
|
rateCache[key] = rates;
|
|
return rates;
|
|
} catch (err) {
|
|
console.error("Failed to fetch CNB exchange rates:", err);
|
|
if (rateCache["today"]) return rateCache["today"];
|
|
return { CZK: 1, EUR: 25, USD: 22, GBP: 28 };
|
|
}
|
|
}
|
|
|
|
/** Convert an amount from a given currency to CZK using CNB rates */
|
|
export async function toCzk(
|
|
amount: number,
|
|
currency: string,
|
|
date?: string,
|
|
): Promise<number> {
|
|
if (currency === "CZK") return amount;
|
|
const rates = await fetchRatesForDate(date);
|
|
const rate = rates[currency];
|
|
if (!rate) return amount;
|
|
return Math.round(amount * rate * 100) / 100;
|
|
}
|
|
|
|
/** Get CNB rate for a currency (CZK per 1 unit), optionally for a specific date */
|
|
export async function getRate(
|
|
currency: string,
|
|
date?: string,
|
|
): Promise<number> {
|
|
if (currency === "CZK") return 1;
|
|
const rates = await fetchRatesForDate(date);
|
|
return rates[currency] || 1;
|
|
}
|