Smart Rebate & Cashback Calculator

Smart Rebate & Cashback Calculator

Calculate your total savings and final cost after rebates and cashback.

${formatCurrency(finalCost)}

Cost vs. Savings Breakdown

`; // Render Chart const chartCtx = document.getElementById('savingsBreakdownChart').getContext('2d'); if (savingsChart) savingsChart.destroy(); savingsChart = new Chart(chartCtx, { type: 'bar', data: { labels: ['Breakdown'], datasets: [ { label: 'Final Cost', data: [finalCost], backgroundColor: '#22c55e' /* green-500 */ }, { label: 'Rebates', data: [totalRebates], backgroundColor: '#f59e0b' /* amber-500 */ }, { label: 'Cashback', data: [totalCashback], backgroundColor: '#f97316' /* orange-500 */ }, ] }, options: { responsive: true, maintainAspectRatio: false, indexAxis: 'y', scales: { x: { stacked: true, ticks: { callback: value => formatCurrency(value) } }, y: { stacked: true } }, plugins: { tooltip: { callbacks: { label: (context) => `${context.dataset.label}: ${formatCurrency(context.raw)}` } } } } }); document.getElementById('download-pdf-btn').addEventListener('click', generatePdf); }; const renderItemConfig = () => { const configContent = document.getElementById('content-item-config'); if (!configContent) return; let itemsHtml = appData.items.map((item, index) => `
`).join(''); configContent.innerHTML = `

Enter Purchase Details

${itemsHtml}
`; attachItemConfigListeners(); }; const attachItemConfigListeners = () => { document.getElementById('add-item-btn').addEventListener('click', () => { appData.items.push({ name: '', price: 0, rebateType: 'fixed', rebateValue: 0, cashbackRate: 0 }); renderItemConfig(); lucide.createIcons(); }); document.querySelectorAll('.remove-item-btn').forEach(btn => { btn.addEventListener('click', (e) => { const index = parseInt(e.currentTarget.dataset.index); appData.items.splice(index, 1); renderItemConfig(); lucide.createIcons(); }); }); document.getElementById('calculate-btn').addEventListener('click', handleCalculationUpdate); }; const handleCalculationUpdate = () => { const newItems = []; document.querySelectorAll('.item-row').forEach(row => { const index = parseInt(row.querySelector('input').dataset.index); const item = {}; row.querySelectorAll('input, select').forEach(input => { const field = input.dataset.field; const value = input.type === 'number' ? parseFloat(input.value) : input.value; item[field] = value; }); newItems.push(item); }); appData.items = newItems; calculationResult = null; // Force recalculation renderDashboard(); switchTab(0); alert('Savings calculation updated!'); }; const generatePdf = () => { loadingOverlay.style.display = 'flex'; const { jsPDF } = window.jspdf; const pdfContent = document.getElementById('pdf-content-area'); const pdfHeader = document.getElementById('pdf-header'); document.getElementById('pdf-generated-date').textContent = new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }); pdfHeader.classList.remove('hidden'); html2canvas(pdfContent, { scale: 2, useCORS: true, logging: false }) .then(canvas => { pdfHeader.classList.add('hidden'); const imgData = canvas.toDataURL('image/jpeg', 0.95); const pdf = new jsPDF({ orientation: 'landscape', unit: 'px', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const imgProps = pdf.getImageProperties(imgData); const imgHeight = (imgProps.height * pdfWidth) / imgProps.width; pdf.addImage(imgData, 'JPEG', 0, 0, pdfWidth, imgHeight); pdf.save('Smart-Savings-Report.pdf'); loadingOverlay.style.display = 'none'; }).catch(err => { console.error("PDF generation failed:", err); pdfHeader.classList.add('hidden'); loadingOverlay.style.display = 'none'; alert('An error occurred generating the PDF.'); }); }; // --- TAB NAVIGATION & INITIALIZATION --- const switchTab = (tabIndex) => { activeTabIndex = tabIndex; document.querySelectorAll('.tab-btn').forEach((btn, i) => btn.classList.toggle('active', i === tabIndex)); document.querySelectorAll('.tab-content').forEach((content, i) => content.classList.toggle('hidden', i !== tabIndex)); updateNavButtons(); }; const updateNavButtons = () => { prevTabBtn.disabled = activeTabIndex === 0; nextTabBtn.disabled = activeTabIndex === tabIdentifiers.length - 1; }; const initializeUI = () => { const tabs = [ { name: 'Savings Dashboard', id: 'dashboard' }, { name: 'Item & Offer Configuration', id: 'item-config' } ]; tabIdentifiers = tabs.map(t => t.id); tabsContainer.innerHTML = tabs.map(tab => ``).join(''); mainContent.innerHTML = tabs.map(tab => `
`).join(''); tabs.forEach((tab, index) => { document.getElementById(`tab-${tab.id}`).addEventListener('click', () => switchTab(index)); }); renderDashboard(); renderItemConfig(); switchTab(0); }; initializeUI(); lucide.createIcons(); prevTabBtn.addEventListener('click', () => { if (activeTabIndex > 0) switchTab(activeTabIndex - 1); }); nextTabBtn.addEventListener('click', () => { if (activeTabIndex < tabIdentifiers.length - 1) switchTab(activeTabIndex + 1); }); });
Scroll to Top