Motorcycle Loan EMI Calculator
Step 1: Loan Details
Step 2: EMI & Amortization
EMI Calculation Summary
Amortization Schedule
Month
EMI
Principal Paid
Interest Paid
Balance
Download EMI Details as PDF
Previous
Calculate EMI
Total Interest Payable: ${mec_formatCurrency(totalInterestPayable)}
Total Amount Payable (Principal + Interest): ${mec_formatCurrency(totalAmountPayable)}
`;
}
let amortizationHTML = "";
let balance = principal;
const monthlyRate = (annualRate / 100) / 12;
for (let month = 1; month <= termMonths; month++) {
let interestForMonth = balance * monthlyRate;
if (monthlyRate === 0) interestForMonth = 0; // Handle 0% APR correctly
let principalPaidThisMonth = emi - interestForMonth;
// Adjust last payment to clear balance exactly
if (month === termMonths) {
principalPaidThisMonth = balance; // Last principal payment should be the remaining balance
const lastEmi = principalPaidThisMonth + interestForMonth; // This could be slightly different from calculated EMI due to rounding.
// For simplicity, we usually show the calculated EMI and let the balance clear.
// Or, show the adjusted EMI for the last month. Let's stick to consistent EMI.
// Balance might be a few cents off, which is acceptable.
}
balance -= principalPaidThisMonth;
if (Math.abs(balance) < 0.01) balance = 0; // Clean up small residuals
amortizationHTML += `
${month}
${mec_formatCurrency(emi)}
${mec_formatCurrency(principalPaidThisMonth)}
${mec_formatCurrency(interestForMonth)}
${mec_formatCurrency(balance)}
`;
}
if(mec_el.amortizationTableBody) mec_el.amortizationTableBody.innerHTML = amortizationHTML;
if(mec_el.pdfDownloadBtn && mec_el.pdfDownloadBtn.style) mec_el.pdfDownloadBtn.style.display = 'block';
}
function mec_resetForm() {
if(mec_el.loanAmount) mec_el.loanAmount.value = "2000";
if(mec_el.annualInterestRate) mec_el.annualInterestRate.value = "9.0";
if(mec_el.loanTenureMonths) mec_el.loanTenureMonths.value = "24";
document.querySelectorAll('#motorcycleEmiCalculator .mec-error-message').forEach(el => el.textContent = '');
if(mec_el.resultsSummary) mec_el.resultsSummary.innerHTML = "";
if(mec_el.amortizationTableBody) mec_el.amortizationTableBody.innerHTML = "";
if(mec_el.pdfDownloadBtn && mec_el.pdfDownloadBtn.style) mec_el.pdfDownloadBtn.style.display = 'none';
if(mec_el.resultsTabButton) mec_el.resultsTabButton.disabled = true;
mec_currentTab = 0;
mec_openTab(null, 'mec-inputTab');
}
function mec_downloadPDF() {
if (typeof window.jspdf === 'undefined' || typeof window.jspdf.jsPDF !== 'function' ||
typeof window.jspdf.jsPDF.API.autoTable !== 'function') {
alert("PDF generation library not loaded. Check CDN links & internet connection."); return;
}
const jsPDFConstructor = window.jspdf.jsPDF;
const resultsSummaryContent = mec_el.resultsSummary;
const amortizationTableElement = document.getElementById('mec-amortizationTable');
if (!resultsSummaryContent || !resultsSummaryContent.textContent.trim() || !amortizationTableElement) {
alert("No results to download. Please calculate EMI first."); return;
}
try {
const pdf = new jsPDFConstructor('p', 'pt', 'a4');
const toolTitle = "Motorcycle Loan EMI Report";
const margins = { top: 40, bottom: 40, left: 30, right: 30 };
let yPos = margins.top;
pdf.setFontSize(18);
pdf.text(toolTitle, pdf.internal.pageSize.getWidth() / 2, yPos, { align: 'center' });
yPos += 25;
pdf.setFontSize(11);
pdf.text("Loan Inputs:", margins.left, yPos); yPos += 15;
pdf.setFontSize(9);
pdf.text(`Loan Amount: ${mec_formatCurrency(parseFloat(mec_el.loanAmount.value))}`, margins.left + 10, yPos); yPos += 13;
pdf.text(`Annual Interest Rate: ${mec_el.annualInterestRate.value}%`, margins.left + 10, yPos); yPos += 13;
pdf.text(`Loan Tenure: ${mec_el.loanTenureMonths.value} months`, margins.left + 10, yPos); yPos += 20;
pdf.setFontSize(11);
pdf.text("EMI Summary:", margins.left, yPos); yPos += 15;
pdf.setFontSize(9);
const summaryLines = pdf.splitTextToSize(resultsSummaryContent.innerText.trim(), pdf.internal.pageSize.getWidth() - margins.left - margins.right - 10);
pdf.text(summaryLines, margins.left + 10, yPos);
yPos += (summaryLines.length * 12) + 10; // Adjust yPos based on number of lines in summary
if (yPos > pdf.internal.pageSize.getHeight() - margins.bottom - 60) { // Check space for table header
pdf.addPage(); yPos = margins.top;
}
pdf.setFontSize(11);
pdf.text("Amortization Schedule:", margins.left, yPos);
pdf.autoTable({
html: '#mec-amortizationTable',
startY: yPos + 15,
theme: 'grid',
headStyles: { fillColor: [0, 123, 255], textColor: 255, fontSize: 9 },
styles: { fontSize: 8, cellPadding: 3 },
columnStyles: { 0: { halign: 'center' } }, // Month column centered
tableWidth: 'auto',
margin: { left: margins.left, right: margins.right }
});
pdf.save('Motorcycle_EMI_Report.pdf');
} catch (e) {
alert("An error occurred during PDF generation: " + e.message + ". Check console.");
console.error("PDF Generation Error:", e);
}
}