// Invoice Generator Utility // Generates invoice data and creates downloadable PDF export const generateInvoiceNumber = (count) => { return `INV-2026-${String(count + 1).padStart(6, '0')}`; }; export const createInvoiceData = (booking, photographer, customer) => { const subtotal = booking.totalPrice || 0; const taxRate = 18; // 18% GST const tax = (subtotal * taxRate) / 100; const totalAmount = subtotal + tax; const items = [ { description: `${photographer.firstName} ${photographer.lastName} - Photography Session`, quantity: 1, unitPrice: subtotal, total: subtotal, }, ]; return { issueDate: new Date(), dueDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), items, subtotal, tax, taxRate, totalAmount, photographerId: photographer._id, customerId: customer._id, bookingId: booking._id, }; }; export const formatInvoiceForDisplay = (invoice) => { return { ...invoice, issueDate: new Date(invoice.issueDate).toLocaleDateString('en-IN', { year: 'numeric', month: 'long', day: 'numeric', }), dueDate: new Date(invoice.dueDate).toLocaleDateString('en-IN', { year: 'numeric', month: 'long', day: 'numeric', }), }; }; // Simple text-based invoice template (can be extended with jsPDF for actual PDF) export const generateInvoiceHTML = (invoice, photographer, customer) => { const formatted = formatInvoiceForDisplay(invoice); return `

INVOICE

SnapLocal - Professional Photography Services

Invoice Details

Invoice #: ${invoice.invoiceNumber}

Issue Date: ${formatted.issueDate}

Due Date: ${formatted.dueDate}

From

${photographer.firstName} ${photographer.lastName}

${photographer.bio || 'Professional Photographer'}

Email: ${photographer.email || 'N/A'}

Bill To

${customer.firstName} ${customer.lastName}

Email: ${customer.email}

${invoice.items .map( item => `` ) .join('')} ${ invoice.discount > 0 ? `` : '' }
Description Quantity Unit Price Total
${item.description} ${item.quantity} ₹${item.unitPrice.toFixed(2)} ₹${item.total.toFixed(2)}
Subtotal: ₹${invoice.subtotal.toFixed(2)}
Tax (${invoice.taxRate}%): ₹${invoice.tax.toFixed(2)}
Discount: -₹${invoice.discount.toFixed(2)}
TOTAL AMOUNT DUE: ₹${invoice.totalAmount.toFixed(2)}
Payment Status: ${invoice.paymentStatus.toUpperCase()} ${ invoice.paidAt ? `

Paid on ${new Date(invoice.paidAt).toLocaleDateString()}

` : '' }
`; }; export const downloadInvoiceAsPDF = async (invoice, photographer, customer) => { // Simple approach: Generate HTML and open in new window for manual PDF save const htmlContent = generateInvoiceHTML(invoice, photographer, customer); const newWindow = window.open('', '', 'width=900,height=600'); newWindow.document.write(htmlContent); newWindow.document.close(); newWindow.print(); }; export const downloadInvoiceAsJSON = (invoice) => { const dataStr = JSON.stringify(invoice, null, 2); const dataBlob = new Blob([dataStr], { type: 'application/json' }); const url = URL.createObjectURL(dataBlob); const link = document.createElement('a'); link.href = url; link.download = `${invoice.invoiceNumber}.json`; link.click(); };