function invImportFromQuote() { const savedQuotes = JSON.parse(localStorage.getItem('jobbud_quotes') || '[]'); if(savedQuotes.length === 0) { alert('No saved quotes found. Build a quote first by going to the Quotes tab and clicking Preview.'); return; } // Build a selection modal const existing = document.getElementById('inv-quote-modal'); if(existing) existing.remove(); const fmt = n => '$' + Number(n).toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ','); const modal = document.createElement('div'); modal.id = 'inv-quote-modal'; modal.style.cssText = 'position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,0.8);z-index:99998;display:flex;align-items:center;justify-content:center;padding:20px;'; modal.innerHTML = `

Import from quote

Select a quote to import its line items into this invoice.

${savedQuotes.slice().reverse().map((q, i) => `
${q.client || 'No client'} — ${q.number}
${q.date} · ${q.lines?.length || 0} line items
${q.bizName || ''}
${fmt(q.total || 0)}
${(q.lines||[]).slice(0,3).map(l => l.desc).join(' · ')}${q.lines?.length > 3 ? ' ...' : ''}
`).join('')}
`; document.body.appendChild(modal); modal.onclick = e => { if(e.target === modal) modal.remove(); }; } function invApplyQuote(idx) { const savedQuotes = JSON.parse(localStorage.getItem('jobbud_quotes') || '[]'); const quote = savedQuotes[idx]; if(!quote) return; // Ask: replace or append? const action = invLines.length > 0 ? confirm('Replace existing line items with quote lines?\n\nOK = Replace\nCancel = Add to existing') : true; if(action) invLines = []; // Add quote lines (quote.lines || []).forEach(l => { invLines.push({ desc: l.desc || '', qty: l.qty || 1, price: l.price || 0, }); }); // Auto-fill client details if(quote.client) { const clientEl = document.getElementById('inv-client-name'); if(clientEl && !clientEl.value) clientEl.value = quote.client; } if(quote.clientEmail) { const emailEl = document.getElementById('inv-client-email'); if(emailEl && !emailEl.value) emailEl.value = quote.clientEmail; } // Auto-fill reference const refEl = document.getElementById('inv-ref'); if(refEl && !refEl.value) refEl.value = quote.number; invRenderLines(); invCalcTotals(); document.getElementById('inv-quote-modal').remove(); // Show success const btn = document.querySelector('[onclick="invImportFromQuote()"]'); if(btn) { const orig = btn.innerHTML; btn.innerHTML = ' Imported!'; btn.style.color = '#4ade80'; setTimeout(() => { btn.innerHTML = orig; btn.style.color = ''; }, 2000); } }