Affiliate Commission Calculator
Calculate earnings based on different commission models and sales data.
Commission Tiers
Total Sales Value
$0.00
Commission Earned
$0.00
Calculation Breakdown
Flat Rate per Sale: $${rate.toFixed(2)}
Total Commission: $${commission.toFixed(2)}
`; } else if (model === 'tiered') { const salesVolume = parseFloat(document.getElementById('tiered-sales-volume').value) || 0; sales = salesVolume; breakdownHtml = `Model: Tiered Percentage
Total Sales Volume: $${sales.toFixed(2)}
Applied Tiers:
`; const tiers = Array.from(document.querySelectorAll('.tier-row')).map(row => ({ from: parseFloat(row.querySelector('[data-tier-part="from"]').value) || 0, to: parseFloat(row.querySelector('[data-tier-part="to"]').value) || Infinity, rate: parseFloat(row.querySelector('[data-tier-part="rate"]').value) || 0 })).sort((a, b) => a.from - b.from); let applicableTier = tiers.find(tier => salesVolume >= tier.from && salesVolume <= tier.to); if (applicableTier) { commission = salesVolume * (applicableTier.rate / 100); breakdownHtml += `Tier Range: $${applicableTier.from.toFixed(2)} - $${applicableTier.to === Infinity ? 'And up' : applicableTier.to.toFixed(2)}
Applied Rate: ${applicableTier.rate}%
No applicable tier found for the sales volume.
`; } breakdownHtml += `Total Commission: $${commission.toFixed(2)}
`; } document.getElementById('summary-sales').textContent = `$${(model === 'flat' ? 'N/A' : sales.toFixed(2))}`; document.getElementById('summary-commission').textContent = `$${commission.toFixed(2)}`; document.getElementById('calculation-breakdown').innerHTML = breakdownHtml; }; const generatePDF = () => { const { jsPDF } = window.jspdf; const pdfContent = document.getElementById('pdf-content'); const pdfBtnContainer = document.getElementById('pdf-button-container'); if (!pdfContent || !pdfBtnContainer) return; pdfBtnContainer.style.display = 'none'; html2canvas(pdfContent, { scale: 2, useCORS: true }).then(canvas => { const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const imgWidth = pdfWidth - 20; const imgHeight = (canvas.height * imgWidth) / canvas.width; pdf.setFontSize(22); pdf.setFont('helvetica', 'bold'); pdf.text('Affiliate Commission Report', pdfWidth / 2, 15, { align: 'center' }); pdf.addImage(imgData, 'PNG', 10, 25, imgWidth, imgHeight); pdf.save('affiliate-commission-report.pdf'); pdfBtnContainer.style.display = 'block'; }).catch(err => { console.error("Error generating PDF:", err); pdfBtnContainer.style.display = 'block'; }); }; // --- Event Listeners --- tabBtnConfig.addEventListener('click', () => showTab('config')); tabBtnDashboard.addEventListener('click', () => showTab('dashboard')); prevBtn.addEventListener('click', () => showTab('config')); nextBtn.addEventListener('click', () => showTab('dashboard')); commissionModelSelect.addEventListener('change', updateModelVisibility); addTierBtn.addEventListener('click', () => addTier(0,0,0)); downloadPdfBtn.addEventListener('click', generatePDF); // --- Initial Setup --- updateModelVisibility(); addTier(0, 10000, 5); addTier(10001, 50000, 8); addTier(50001, 100000, 12); showTab('config'); });