diff --git a/src/frontend/src/components/Invoices/InvoiceList.js b/src/frontend/src/components/Invoices/InvoiceList.js index 02a95ff..ff815c5 100644 --- a/src/frontend/src/components/Invoices/InvoiceList.js +++ b/src/frontend/src/components/Invoices/InvoiceList.js @@ -9,7 +9,8 @@ import { Trash2, Calendar, Search, - Filter + Filter, + Download } from 'lucide-react'; import { format } from 'date-fns'; @@ -66,6 +67,47 @@ const InvoiceList = () => { return matchesSearch && matchesStatus; }); + const handleExportCSV = () => { + try { + if (filteredInvoices.length === 0) { + toast.warning('No invoices to export'); + return; + } + + const headers = ['Invoice Number', 'Customer Name', 'Customer Email', 'Issue Date', 'Due Date', 'Amount', 'Status']; + const csvRows = [headers.join(',')]; + + filteredInvoices.forEach(invoice => { + const row = [ + invoice.invoice_number, + `"${invoice.customer_name}"`, + invoice.customer_email || '', + format(new Date(invoice.issue_date), 'yyyy-MM-dd'), + format(new Date(invoice.due_date), 'yyyy-MM-dd'), + invoice.total_amount.toFixed(2), + invoice.status + ]; + csvRows.push(row.join(',')); + }); + + const csvContent = csvRows.join('\n'); + const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' }); + const link = document.createElement('a'); + const url = URL.createObjectURL(blob); + + link.setAttribute('href', url); + link.setAttribute('download', `invoices_${format(new Date(), 'yyyy-MM-dd')}.csv`); + link.style.visibility = 'hidden'; + document.body.appendChild(link); + link.click(); + document.body.removeChild(link); + + toast.success(`Exported ${filteredInvoices.length} invoice(s)`); + } catch (error) { + toast.error('Failed to export invoices'); + } + }; + if (loading) { return (
@@ -85,10 +127,16 @@ const InvoiceList = () => {

Invoices

- - - New Invoice - +
+ + + + New Invoice + +
{/* Filters */}