Payday Loan Interest & Fee Calculator
Loan Cost Summary
Total Repayment Amount:
$0.00
Total Loan Cost (Fee):
$0.00
Equivalent Annual Percentage Rate (APR):
0.00%
Error: Calculator components failed to load.
"; } }); function formatCurrencyPDL(value) { const num = Number(value); if (isNaN(num)) return "0.00"; return num.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }); } function formatPercentagePDL(value) { const num = Number(value); if (isNaN(num) || !isFinite(num)) return "0.00"; // Handle NaN or Infinity return num.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }); } function calculatePaydayLoanCostsPDL() { if (!loanAmountInPDL || !loanFeeInPDL || !loanTermDaysInPDL) { alert("Initialization error. Please refresh."); return; } const principal = parseFloat(loanAmountInPDL.value); const fee = parseFloat(loanFeeInPDL.value); const termDays = parseInt(loanTermDaysInPDL.value); if (isNaN(principal) || principal <= 0) { alert("Please enter a valid Loan Amount greater than 0."); return; } if (isNaN(fee) || fee < 0) { // Fee can be 0, but not negative alert("Please enter a valid Loan Fee (0 or positive)."); return; } if (isNaN(termDays) || termDays <= 0) { alert("Please enter a valid Loan Term in days (greater than 0)."); return; } const totalRepayment = principal + fee; const totalCost = fee; let apr = 0; if (principal > 0 && termDays > 0) { // Avoid division by zero for APR // APR = ((Fee / Principal) / Term in Days) * 365 * 100 apr = (fee / principal) / termDays * 365 * 100; } if (!isFinite(apr)) { // Check for Infinity if termDays or principal was somehow 0 after checks apr = 0; // Or handle as an error/NA, but 0 is safer for display } totalRepaymentOutPDL.textContent = `$${formatCurrencyPDL(totalRepayment)}`; totalFeeOutPDL.textContent = `$${formatCurrencyPDL(totalCost)}`; equivalentAprOutPDL.textContent = `${formatPercentagePDL(apr)}%`; calculatedValuesPDL = { principal, fee, termDays, totalRepayment, totalCost, apr }; resultsSectionPDL.style.display = "block"; } function generatePdfPDL() { if (Object.keys(calculatedValuesPDL).length === 0 || !resultsSectionPDL || resultsSectionPDL.style.display === 'none') { alert('Please calculate the costs first to generate a PDF summary.'); return; } if (typeof window.jspdf === 'undefined' || typeof window.jspdf.jsPDF === 'undefined') { alert('PDF generation library (jsPDF) is not loaded.'); return; } const { jsPDF } = window.jspdf; const doc = new jsPDF(); if (typeof doc.autoTable !== 'function') { alert('PDF generation plugin (jsPDF-AutoTable) is not loaded.'); return; } const data = calculatedValuesPDL; try { doc.setFontSize(18); doc.setTextColor(52, 58, 64); // #343a40 Dark Gray doc.text("Payday Loan Interest & Fee Calculation", 14, 22); doc.setFontSize(10); doc.setTextColor(100); doc.text(`Report Generated: ${new Date().toLocaleDateString()}`, 14, 28); let startY = 38; const tableTheme = 'grid'; const headFillColor = [108, 117, 125]; // #6c757d Gray const headTextColor = [255, 255, 255]; // White doc.setFontSize(12); doc.setTextColor(73, 80, 87); // #495057 doc.text("Loan Inputs", 14, startY); startY += 7; doc.autoTable({ head: [['Parameter', 'Value']], body: [ ['Loan Amount', `$${formatCurrencyPDL(data.principal)}`], ['Total Loan Fee', `$${formatCurrencyPDL(data.fee)}`], ['Loan Term', `${data.termDays} days`] ], startY: startY, theme: tableTheme, headStyles: { fillColor: headFillColor, textColor: headTextColor }, styles: { fontSize: 10 } }); startY = doc.lastAutoTable.finalY + 12; doc.setFontSize(12); doc.setTextColor(73, 80, 87); doc.text("Calculated Costs", 14, startY); startY += 7; doc.autoTable({ head: [['Description', 'Amount / Rate']], body: [ ['Total Repayment Amount', `$${formatCurrencyPDL(data.totalRepayment)}`], ['Total Loan Cost (Fee)', `$${formatCurrencyPDL(data.totalCost)}`], ['Equivalent APR', `${formatPercentagePDL(data.apr)}%`] ], startY: startY, theme: tableTheme, headStyles: { fillColor: headFillColor, textColor: headTextColor }, styles: { fontSize: 10 }, columnStyles: { 1: { fontStyle: 'bold' } }, // Highlight APR row if possible, or ensure it's clear didDrawCell: function(data) { if (data.section === 'body' && data.row.index === 2) { // APR row doc.setTextColor(220, 53, 69); // Red color for APR text in PDF doc.setFont(undefined, 'bold'); } }, willDrawCell: function(data) { // Reset for other cells if (!(data.section === 'body' && data.row.index === 2)) { doc.setTextColor(40); // Default text color doc.setFont(undefined, 'normal'); } } }); doc.save("Payday_Loan_Cost_Summary.pdf"); } catch (error) { console.error("PDL PDF Error:", error); alert("An error occurred while generating the PDF: " + error.message); } }