/** * API Client para Exchange Rate */ import { api } from '$lib/api'; export interface ExchangeRate { id: number; date: string; value: number; local_currency: string | null; foreign_currency: string | null; company_id: number; tenant_id: number; } export interface ExchangeRateListResponse { items: ExchangeRate[]; total: number; page: number; size: number; pages: number; } /** * Get exchange rate by date */ export async function getExchangeRateByDate(date: string, companyId: number): Promise { try { console.log('📡 [API] Solicitando tipos de cambio para company_id:', companyId); // Get all exchange rates and filter by date on client side const response = await api.get(`/v1/a76/exchange-rate/?company_id=${companyId}`); console.log('📡 [API] Respuesta recibida:', response.data?.total, 'tipos de cambio'); if (response.data && response.data.items && response.data.items.length > 0) { // Filter by date and find USD exchange rate const dateOnly = date.split('T')[0]; // Get YYYY-MM-DD part console.log('🔍 [API] Buscando fecha:', dateOnly, 'en', response.data.items.length, 'registros'); const matchingRates = response.data.items.filter(rate => { const rateDate = rate.date.split('T')[0]; const matches = rateDate === dateOnly && rate.foreign_currency === 'USD'; console.log(' - Comparando:', rateDate, '===', dateOnly, '&& USD ===', rate.foreign_currency, '→', matches); return matches; }); console.log('✅ [API] Encontrados', matchingRates.length, 'tipos de cambio que coinciden'); return matchingRates.length > 0 ? matchingRates[0] : null; } console.log('⚠️ [API] No hay datos en la respuesta'); return null; } catch (error) { console.error('❌ [API] Error fetching exchange rate:', error); return null; } }