Loading transactions...
Error loading transactions
'; } } // Format card number function formatCardNumber(cardNumber) { if (!cardNumber) return 'N/A'; return cardNumber.replace(/(.{3})(.{4})(.{4})(.{4})/, '$1-$2-$3-$4'); } // Handle date range change function handleDateRangeChange() { const dateRange = document.getElementById('dateRange').value; const customStart = document.getElementById('customDateStart'); const customEnd = document.getElementById('customDateEnd'); if (dateRange === 'custom') { customStart.style.display = 'block'; customEnd.style.display = 'block'; } else { customStart.style.display = 'none'; customEnd.style.display = 'none'; currentFilters.dateRange = parseInt(dateRange); } } // Apply filters function applyFilters(event) { event.preventDefault(); const dateRange = document.getElementById('dateRange').value; const cardType = document.getElementById('cardType').value; if (dateRange === 'custom') { // Handle custom date range const startDate = document.getElementById('startDate').value; const endDate = document.getElementById('endDate').value; if (!startDate || !endDate) { alert('Please select both start and end dates'); return; } // Calculate days difference const start = new Date(startDate); const end = new Date(endDate); const days = Math.ceil((end - start) / (1000 * 60 * 60 * 24)); currentFilters.dateRange = days; } else { currentFilters.dateRange = parseInt(dateRange); } currentFilters.cardType = cardType; // Reload data with new filters loadDashboard(); } // Export chart function exportChart(chartId) { const chart = chartId === 'salesChart' ? salesChart : usageChart; const url = chart.toBase64Image(); const a = document.createElement('a'); a.href = url; a.download = `${chartId}_${new Date().toISOString().split('T')[0]}.png`; document.body.appendChild(a); a.click(); document.body.removeChild(a); } // Export transactions to CSV async function exportTransactions() { try { // Fetch transactions via GCP report endpoint (all time) const today = new Date().toISOString().split('T')[0]; const report = await _gcrFetchReport('2000-01-01', today); const transactions = Array.isArray(report.recent_transactions) ? report.recent_transactions : (report.transactions || []); // Create CSV const headers = ['Date', 'Card Number', 'Type', 'Amount', 'Balance After', 'Notes']; const rows = transactions.map(tx => [ new Date(tx.created_at).toLocaleString(), tx.card_number || 'N/A', tx.transaction_type, parseFloat(tx.amount || 0).toFixed(2), parseFloat(tx.balance_after || 0).toFixed(2), tx.notes || '' ]); const csvContent = [ headers.join(','), ...rows.map(row => row.map(cell => `"${cell}"`).join(',')) ].join('\n'); // Download CSV const blob = new Blob([csvContent], { type: 'text/csv' }); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `gift_card_transactions_${new Date().toISOString().split('T')[0]}.csv`; document.body.appendChild(a); a.click(); document.body.removeChild(a); window.URL.revokeObjectURL(url); } catch (error) { console.error('Error exporting transactions:', error); alert('Failed to export transactions. Please try again.'); } } // Initialize dashboard loadDashboard(); // Add Font Awesome icons if (!document.querySelector('link[href*="font-awesome"]')) { const link = document.createElement('link'); link.rel = 'stylesheet'; link.href = 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css'; document.head.appendChild(link); }