import { getReceipts, getSettings, saveSettings, clearAllReceipts } from './utils/storage.js'; import { getCategoryLabel } from './utils/categories.js'; import { exportToCsv, downloadBlob } from './utils/export.js'; import type { Receipt, AppSettings } from './types.js'; const nameInput = document.getElementById('s-name') as HTMLInputElement; const currencySelect = document.getElementById('s-currency') as HTMLSelectElement; const yearInput = document.getElementById('s-year') as HTMLInputElement; const saveBtn = document.getElementById('save-settings') as HTMLButtonElement; const sumCount = document.getElementById('sum-count') as HTMLSpanElement; const sumTotal = document.getElementById('sum-total') as HTMLSpanElement; const tableWrap = document.getElementById('receipts-table-wrap') as HTMLDivElement; const toast = document.getElementById('toast') as HTMLDivElement; let currentReceipts: Receipt[] = []; async function init() { const settings = await getSettings(); nameInput.value = settings.ownerName || ''; currencySelect.value = settings.defaultCurrency || 'USD'; yearInput.value = settings.taxYear || new Date().getFullYear().toString(); await loadData(); saveBtn.addEventListener('click', async () => { const settings: AppSettings = { ownerName: nameInput.value.trim(), defaultCurrency: currencySelect.value, taxYear: yearInput.value.trim(), }; await saveSettings(settings); showToast('Settings saved'); }); document.getElementById('export-csv')!.addEventListener('click', async () => { if (currentReceipts.length === 0) { showToast('No receipts to export'); return; } const csv = exportToCsv(currentReceipts); const year = yearInput.value || new Date().getFullYear(); downloadBlob(csv, `simplescan-receipts-${year}.csv`, 'text/csv'); showToast('CSV downloaded'); }); document.getElementById('export-pdf')!.addEventListener('click', async () => { if (currentReceipts.length === 0) { showToast('No receipts to export'); return; } exportPdf(currentReceipts); }); document.getElementById('clear-all')!.addEventListener('click', async () => { if (!confirm('Are you sure? This will delete ALL receipts forever.')) return; await clearAllReceipts(); await loadData(); showToast('All receipts deleted'); }); } async function loadData() { currentReceipts = await getReceipts(); sumCount.textContent = String(currentReceipts.length); const total = currentReceipts.reduce((s, r) => s + r.amount, 0); const currency = currentReceipts[0]?.currency || 'USD'; sumTotal.textContent = formatMoney(total, currency); if (currentReceipts.length === 0) { tableWrap.innerHTML = '

No receipts yet. Add some from the extension popup.

'; return; } tableWrap.innerHTML = ` ${currentReceipts .map( (r) => ` ` ) .join('')}
Date Vendor Description Category Amount
${escapeHtml(r.date)} ${escapeHtml(r.vendor)} ${escapeHtml(r.title)} ${getCategoryLabel(r.category)} ${formatMoney(r.amount, r.currency)}
`; } function exportPdf(receipts: Receipt[]) { // Simple PDF generation using browser print to PDF approach const currency = receipts[0]?.currency || 'USD'; const total = receipts.reduce((s, r) => s + r.amount, 0); const year = yearInput.value || new Date().getFullYear(); const owner = nameInput.value.trim() || 'Freelancer'; const html = ` Receipt Report ${year}

Receipt Report — ${year}

${escapeHtml(owner)} · ${receipts.length} receipts · ${formatMoney(total, currency)} total
${receipts .map( (r) => ` ` ) .join('')}
Date Vendor Description Category Amount Notes
${escapeHtml(r.date)} ${escapeHtml(r.vendor)} ${escapeHtml(r.title)} ${getCategoryLabel(r.category)} ${formatMoney(r.amount, r.currency)} ${escapeHtml(r.notes)}
Total ${formatMoney(total, currency)}
`; const printWindow = window.open('', '_blank'); if (!printWindow) { showToast('Popup blocked. Allow popups for PDF export.'); return; } printWindow.document.write(html); printWindow.document.close(); printWindow.focus(); setTimeout(() => { printWindow.print(); }, 300); showToast('PDF print dialog opened'); } function formatMoney(amount: number, currency: string): string { const map: Record = { USD: '$', EUR: '€', GBP: '£', CAD: 'C$', AUD: 'A$' }; const symbol = map[currency] || currency + ' '; return symbol + amount.toFixed(2); } function escapeHtml(text: string): string { const div = document.createElement('div'); div.textContent = text; return div.innerHTML; } function showToast(message: string) { toast.textContent = message; toast.classList.remove('hidden'); setTimeout(() => toast.classList.add('hidden'), 2500); } init();