VIP Customer Discount Level Calculator

VIP Customer Discount Level Calculator

Determine customer discount tiers based on their value and loyalty.

Fill out the form and click calculate.

`; document.getElementById('calculator-form').addEventListener('submit', handleCalculation); }; const renderConfig = () => { const configContent = document.getElementById('content-config'); if (!configContent) return; const tiersHtml = appData.tiers.map((tier, index) => `
`).join(''); configContent.innerHTML = `

Configure Discount Tiers

Tier Name Min. Spend ($) Min. Purchases Min. Loyalty (Yrs) Discount (%)
${tiersHtml}
`; document.getElementById('config-form').addEventListener('submit', handleConfigUpdate); }; const handleCalculation = (e) => { e.preventDefault(); const spend = parseFloat(document.getElementById('total-spending').value); const purchases = parseInt(document.getElementById('total-purchases').value); const sinceDate = new Date(document.getElementById('customer-since').value); const customerName = document.getElementById('customer-name').value; if (isNaN(spend) || isNaN(purchases) || !sinceDate) { alert('Please fill in all fields with valid numbers and a date.'); return; } const today = new Date(); const loyaltyYears = (today - sinceDate) / (1000 * 60 * 60 * 24 * 365.25); let assignedTier = { name: 'Standard', discount: 0 }; // Tiers are pre-sorted from highest to lowest in appData for (const tier of appData.tiers) { if (spend >= tier.minSpend && purchases >= tier.minPurchases && loyaltyYears >= tier.minLoyaltyYears) { assignedTier = tier; break; // Stop at the first (highest) tier they qualify for } } renderResults({ customerName, spend, purchases, loyaltyYears, assignedTier }); }; const renderResults = (data) => { const resultsContainer = document.getElementById('results-container'); resultsContainer.innerHTML = `

Status for ${data.customerName}

${data.assignedTier.name}

${data.assignedTier.discount}%

Discount Level

Customer Metrics

  • Lifetime Spending $${data.spend.toLocaleString()}
  • Total Purchases ${data.purchases}
  • Customer Loyalty ${data.loyaltyYears.toFixed(1)} Years
`; lucide.createIcons(); document.getElementById('download-pdf-btn').addEventListener('click', () => generatePdf(data.customerName)); }; const handleConfigUpdate = (e) => { e.preventDefault(); const newTiers = []; const form = document.getElementById('config-form'); const tierCount = form.querySelectorAll('input[data-field="name"]').length; for (let i = 0; i < tierCount; i++) { const tier = { name: form.querySelector(`input[data-index="${i}"][data-field="name"]`).value, minSpend: parseFloat(form.querySelector(`input[data-index="${i}"][data-field="minSpend"]`).value), minPurchases: parseInt(form.querySelector(`input[data-index="${i}"][data-field="minPurchases"]`).value), minLoyaltyYears: parseFloat(form.querySelector(`input[data-index="${i}"][data-field="minLoyaltyYears"]`).value), discount: parseFloat(form.querySelector(`input[data-index="${i}"][data-field="discount"]`).value) }; newTiers.push(tier); } // Sort by highest requirement to lowest for correct calculation logic appData.tiers = newTiers.sort((a, b) => b.minSpend - a.minSpend); const statusEl = document.getElementById('config-status'); statusEl.textContent = 'Configuration saved!'; setTimeout(() => { statusEl.textContent = ''; }, 3000); }; const generatePdf = (customerName) => { loadingOverlay.style.display = 'flex'; const { jsPDF } = window.jspdf; const pdfContent = document.getElementById('pdf-content-area'); const pdfHeader = document.getElementById('pdf-header'); document.getElementById('pdf-customer-name').textContent = customerName; document.getElementById('pdf-generated-date').textContent = new Date().toLocaleDateString('en-US'); pdfHeader.classList.remove('hidden'); html2canvas(pdfContent, { scale: 2, useCORS: true }) .then(canvas => { pdfHeader.classList.add('hidden'); const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF({ orientation: 'portrait', unit: 'px', format: [canvas.width, canvas.height] }); pdf.addImage(imgData, 'PNG', 0, 0, canvas.width, canvas.height); pdf.save(`VIP-Report-${customerName.replace(/\s/g, '_')}.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: 'Discount Calculator', id: 'calculator' }, { name: 'Configure Tiers', id: '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)); }); renderCalculator(); renderConfig(); 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