document.addEventListener('DOMContentLoaded', function() { let itemCount = 0; const itemsTable = document.getElementById('itemsTable'); // Load saved "From" details if they exist const savedFromDetails = JSON.parse(localStorage.getItem('fromDetails')) || {}; if (savedFromDetails.businessName) { document.querySelector('input[placeholder="Your Business Name"]').value = savedFromDetails.businessName; } if (savedFromDetails.address) { document.querySelector('input[placeholder="Address"]').value = savedFromDetails.address; } if (savedFromDetails.cityStateZip) { document.querySelector('input[placeholder="City, State, ZIP"]').value = savedFromDetails.cityStateZip; } if (savedFromDetails.email) { document.querySelector('input[placeholder="Email"]').value = savedFromDetails.email; } if (savedFromDetails.phone) { document.querySelector('input[placeholder="Phone"]').value = savedFromDetails.phone; } // Save "From" details when they change const fromInputs = document.querySelectorAll('input[placeholder^="Your"]'); fromInputs.forEach(input => { input.addEventListener('change', () => { const fromDetails = { businessName: document.querySelector('input[placeholder="Your Business Name"]').value, address: document.querySelector('input[placeholder="Address"]').value, cityStateZip: document.querySelector('input[placeholder="City, State, ZIP"]').value, email: document.querySelector('input[placeholder="Email"]').value, phone: document.querySelector('input[placeholder="Phone"]').value }; localStorage.setItem('fromDetails', JSON.stringify(fromDetails)); }); }); const addItemBtn = document.getElementById('addItemBtn'); const generateBtn = document.getElementById('generateBtn'); const closeModalBtn = document.getElementById('closeModalBtn'); const printBtn = document.getElementById('printBtn'); const downloadBtn = document.getElementById('downloadBtn'); const invoiceModal = document.getElementById('invoiceModal'); const invoiceContent = document.getElementById('invoiceContent'); const taxRateInput = document.getElementById('taxRate'); // Add new item row addItemBtn.addEventListener('click', addItemRow); // Generate invoice generateBtn.addEventListener('click', generateInvoice); // Close modal closeModalBtn.addEventListener('click', () => { invoiceModal.classList.add('hidden'); }); // Print invoice printBtn.addEventListener('click', () => { window.print(); }); // Download as PDF (placeholder functionality) downloadBtn.addEventListener('click', () => { alert('PDF download functionality would be implemented here with a library like jsPDF'); }); // Recalculate totals when tax rate changes taxRateInput.addEventListener('change', calculateTotals); // Add initial item row addItemRow(); function addItemRow() { itemCount++; const row = document.createElement('tr'); row.className = 'item-row'; row.innerHTML = ` $0.00 `; itemsTable.appendChild(row); feather.replace(); // Add event listeners for the new row const quantityInput = row.querySelector('.quantity'); const priceInput = row.querySelector('.price'); const deleteBtn = row.querySelector('.delete-item'); quantityInput.addEventListener('input', calculateRowTotal); priceInput.addEventListener('input', calculateRowTotal); deleteBtn.addEventListener('click', () => { row.remove(); calculateTotals(); }); function calculateRowTotal() { const quantity = parseFloat(quantityInput.value) || 0; const price = parseFloat(priceInput.value) || 0; const total = quantity * price; row.querySelector('.item-total').textContent = `$${total.toFixed(2)}`; calculateTotals(); } } function calculateTotals() { const rows = document.querySelectorAll('.item-row'); let subtotal = 0; rows.forEach(row => { const totalText = row.querySelector('.item-total').textContent; subtotal += parseFloat(totalText.replace('$', '')) || 0; }); const taxRate = parseFloat(taxRateInput.value) || 0; const taxAmount = subtotal * (taxRate / 100); const total = subtotal + taxAmount; document.getElementById('subtotal').textContent = `$${subtotal.toFixed(2)}`; document.getElementById('taxAmount').textContent = `$${taxAmount.toFixed(2)}`; document.getElementById('totalAmount').textContent = `$${total.toFixed(2)}`; } function generateInvoice() { const fromName = document.querySelector('input[placeholder="Your Business Name"]').value || 'Your Business Name'; const fromAddress = document.querySelector('input[placeholder="Address"]').value || 'Your Address'; const fromCityStateZip = document.querySelector('input[placeholder="City, State, ZIP"]').value || 'City, State, ZIP'; const fromEmail = document.querySelector('input[placeholder="Email"]').value || 'your@email.com'; const fromPhone = document.querySelector('input[placeholder="Phone"]').value || '(123) 456-7890'; const toName = document.querySelector('input[placeholder="Client Name"]').value || 'Client Name'; const toAddress = document.querySelector('input[placeholder="Client Address"]').value || 'Client Address'; const toCityStateZip = document.querySelector('input[placeholder="City, State, ZIP"]').value || 'City, State, ZIP'; const toEmail = document.querySelector('input[placeholder="Client Email"]').value || 'client@email.com'; const toPhone = document.querySelector('input[placeholder="Client Phone"]').value || '(987) 654-3210'; const invoiceDate = new Date().toLocaleDateString(); const invoiceNumber = 'INV-' + Math.floor(Math.random() * 10000); const subtotal = document.getElementById('subtotal').textContent; const taxAmount = document.getElementById('taxAmount').textContent; const totalAmount = document.getElementById('totalAmount').textContent; const taxRate = taxRateInput.value; let itemsHTML = ''; const rows = document.querySelectorAll('.item-row'); rows.forEach(row => { const description = row.querySelector('.description').value || 'Item description'; const quantity = row.querySelector('.quantity').value || '1'; const price = row.querySelector('.price').value || '0.00'; const total = row.querySelector('.item-total').textContent; itemsHTML += ` ${description} ${quantity} $${parseFloat(price).toFixed(2)} ${total} `; }); invoiceContent.innerHTML = `

${fromName}

${fromAddress}

${fromCityStateZip}

${fromEmail}

${fromPhone}

INVOICE

Date: ${invoiceDate}

Invoice #: ${invoiceNumber}

Bill To:

${toName}

${toAddress}

${toCityStateZip}

${toEmail}

${toPhone}

${itemsHTML}
Description Qty Unit Price Amount
Subtotal: ${subtotal}
Tax (${taxRate}%): ${taxAmount}
Total: ${totalAmount}

Thank you for your business!

Please make payment within 30 days

`; invoiceModal.classList.remove('hidden'); } });