Business Loan Balloon Payment Estimator
Loan & Rate Details
Loan Structure
Calculated Regular Periodic Payment:
Balloon Payment Estimate & Summary
Please complete the previous tabs to see the estimate.
Please complete the previous tabs to see the estimate.
';
}
blbpe_domElements.pdfDownloadBtn.classList.add('blbpe-hidden');
if (tabId !== 'blbpe-tab2') { // Hide regular payment if not on tab2 or tab3 calculation pending
blbpe_domElements.regularPaymentResult.classList.add('blbpe-hidden');
}
}
}
function blbpe_navigateToTab(targetTabId) {
const currentActiveTabContent = document.querySelector('.blbpe-tab-content.blbpe-active');
let proceed = false;
if (currentActiveTabContent.id === 'blbpe-tab1') {
if (targetTabId === 'blbpe-tab2' || targetTabId === 'blbpe-tab3') {
proceed = blbpe_validateTab1();
} else { proceed = true; }
} else if (currentActiveTabContent.id === 'blbpe-tab2') {
if (targetTabId === 'blbpe-tab3') {
if (blbpe_validateTab1()) { // First ensure tab 1 is valid
proceed = blbpe_validateTab2AndCalcRegularPayment(); // Validates tab 2 and calculates payment
} else { // If tab 1 is not valid, switch to tab 1
const tab1Button = document.querySelector('.blbpe-tab-link[data-tab="blbpe-tab1"]');
if (tab1Button) tab1Button.click();
return;
}
} else { proceed = true; }
} else {
proceed = true;
}
if (proceed) {
const targetTabButton = document.querySelector(`.blbpe-tab-link[data-tab="${targetTabId}"]`);
if (targetTabButton) targetTabButton.click();
}
}
function blbpe_performFinalCalculationsAndDisplay() {
if (!blbpe_validateTab1()) {
blbpe_domElements.resultsContainer.innerHTML = '
Please correct errors in Tab 1: Loan Details.
';
blbpe_domElements.pdfDownloadBtn.classList.add('blbpe-hidden');
return;
}
if (!blbpe_validateTab2AndCalcRegularPayment()) {
blbpe_domElements.resultsContainer.innerHTML = '
Please correct errors in Tab 2: Loan Structure.
';
blbpe_domElements.pdfDownloadBtn.classList.add('blbpe-hidden');
return;
}
// All data should now be in blbpe_calculatedData, including regularPeriodicPayment
blbpe_calculatedData.balloonPayment = blbpe_calculateBalloonPaymentB(
blbpe_calculatedData.loanAmount,
blbpe_calculatedData.annualInterestRate,
blbpe_calculatedData.loanTermYears,
blbpe_calculatedData.regularPeriodicPayment,
blbpe_calculatedData.paymentsPerYear
);
blbpe_calculatedData.actualNumPayments = blbpe_calculatedData.loanTermYears * blbpe_calculatedData.paymentsPerYear;
blbpe_calculatedData.totalRegularPaymentsMade = blbpe_calculatedData.regularPeriodicPayment * blbpe_calculatedData.actualNumPayments;
const totalRepaid = blbpe_calculatedData.totalRegularPaymentsMade + blbpe_calculatedData.balloonPayment;
blbpe_calculatedData.totalInterestPaid = totalRepaid - blbpe_calculatedData.loanAmount;
blbpe_calculatedData.periodicInterestRate = (blbpe_calculatedData.annualInterestRate / 100) / blbpe_calculatedData.paymentsPerYear;
blbpe_domElements.resultsContainer.innerHTML = `
Loan Input Summary
Loan Amount: ${blbpe_formatCurrency(blbpe_calculatedData.loanAmount)}
Annual Interest Rate: ${blbpe_calculatedData.annualInterestRate.toFixed(2)}%
Payments Per Year: ${blbpe_calculatedData.paymentsPerYear}
Loan Term (until balloon): ${blbpe_calculatedData.loanTermYears} years
Amortization Period (for payment calc): ${blbpe_calculatedData.amortizationPeriodYears} years
Payment Details
Periodic Interest Rate: ${(blbpe_calculatedData.periodicInterestRate * 100).toFixed(4)}%
Calculated Regular Periodic Payment: ${blbpe_formatCurrency(blbpe_calculatedData.regularPeriodicPayment)}
Total Number of Regular Payments: ${blbpe_calculatedData.actualNumPayments}
Total Paid via Regular Payments: ${blbpe_formatCurrency(blbpe_calculatedData.totalRegularPaymentsMade)}
Balloon Payment Estimate
Estimated Balloon Payment: ${blbpe_formatCurrency(blbpe_calculatedData.balloonPayment)}
Overall Loan Cost
Total Repaid (Regular Payments + Balloon): ${blbpe_formatCurrency(totalRepaid)}
Total Interest Paid: ${blbpe_formatCurrency(blbpe_calculatedData.totalInterestPaid)}
`;
blbpe_domElements.pdfDownloadBtn.classList.remove('blbpe-hidden');
}
function blbpe_hexToRgb(hex) {
if (!hex || typeof hex !== 'string') return null;
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) { return r + r + g + g + b + b; });
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? { r: parseInt(result[1], 16), g: parseInt(result[2], 16), b: parseInt(result[3], 16) } : null;
}
function blbpe_generatePdf() {
if (typeof window.jspdf === 'undefined' || typeof window.jspdf.jsPDF === 'undefined') {
console.error("jsPDF library not loaded."); alert("PDF generation unavailable: jsPDF library not loaded."); return;
}
if (typeof window.jspdf.jsPDF.API === 'undefined' || typeof window.jspdf.jsPDF.API.autoTable === 'undefined') {
console.error("jsPDF-AutoTable plugin not loaded."); alert("PDF generation unavailable: AutoTable plugin not loaded."); return;
}
// Ensure data is calculated and valid before PDF generation
if (!blbpe_validateTab1() || !blbpe_validateTab2AndCalcRegularPayment()) {
alert("Please ensure all inputs are correctly filled and calculations are up to date before generating PDF.");
if (!blbpe_validateTab1()) {
const tab1Button = document.querySelector('.blbpe-tab-link[data-tab="blbpe-tab1"]');
if (tab1Button) tab1Button.click();
} else if (!blbpe_validateTab2AndCalcRegularPayment()) {
const tab2Button = document.querySelector('.blbpe-tab-link[data-tab="blbpe-tab2"]');
if (tab2Button) tab2Button.click();
}
return;
}
// Re-run final calc to ensure blbpe_calculatedData is fresh for PDF
blbpe_performFinalCalculationsAndDisplay();
// Check if resultsContainer shows an error message after trying to calculate
if (blbpe_domElements.resultsContainer.innerHTML.includes("blbpe-error-message")) {
alert("Cannot generate PDF due to errors in input. Please correct them.");
return;
}
const ActualJsPDF = window.jspdf.jsPDF;
const doc = new ActualJsPDF();
const { loanAmount, annualInterestRate, paymentsPerYear, loanTermYears, amortizationPeriodYears,
regularPeriodicPayment, balloonPayment, totalRegularPaymentsMade, totalInterestPaid,
periodicInterestRate, actualNumPayments } = blbpe_calculatedData;
const totalRepaid = totalRegularPaymentsMade + balloonPayment;
doc.setFontSize(18);
const primaryColorRGB = blbpe_hexToRgb(getComputedStyle(document.documentElement).getPropertyValue('--blbpe-primary-color').trim());
const secondaryColorRGB = blbpe_hexToRgb(getComputedStyle(document.documentElement).getPropertyValue('--blbpe-secondary-color').trim());
if (primaryColorRGB) doc.setTextColor(primaryColorRGB.r, primaryColorRGB.g, primaryColorRGB.b); else doc.setTextColor(0,90,156);
doc.text("Business Loan Balloon Payment Estimate", 105, 20, null, null, "center");
doc.setFontSize(12);
doc.setTextColor(51, 51, 51);
let startY = 35;
const tableHeadFillColor = secondaryColorRGB ? [secondaryColorRGB.r, secondaryColorRGB.g, secondaryColorRGB.b] : [0,51,102];
// Section 1: Loan Inputs
doc.setFontSize(14);
if (secondaryColorRGB) doc.setTextColor(secondaryColorRGB.r, secondaryColorRGB.g, secondaryColorRGB.b); else doc.setTextColor(0,51,102);
doc.text("Loan Input Summary", 14, startY); startY += 7;
doc.autoTable({
startY: startY, theme: 'grid', headStyles: { fillColor: tableHeadFillColor },
body: [
["Loan Amount:", blbpe_formatCurrency(loanAmount)],
["Annual Interest Rate:", `${annualInterestRate.toFixed(2)}%`],
["Payments Per Year:", `${paymentsPerYear}`],
["Loan Term (until balloon):", `${loanTermYears} years`],
["Amortization Period:", `${amortizationPeriodYears} years`],
], margin: { left: 14, right: 14 }
});
startY = doc.autoTable.previous.finalY + 10;
// Section 2: Payment Details
doc.setFontSize(14);
if (secondaryColorRGB) doc.setTextColor(secondaryColorRGB.r, secondaryColorRGB.g, secondaryColorRGB.b); else doc.setTextColor(0,51,102);
doc.text("Payment Details", 14, startY); startY += 7;
doc.autoTable({
startY: startY, theme: 'grid', headStyles: { fillColor: tableHeadFillColor },
body: [
["Periodic Interest Rate:", `${(periodicInterestRate * 100).toFixed(4)}%`],
["Regular Periodic Payment:", blbpe_formatCurrency(regularPeriodicPayment)],
["Total Number of Regular Payments:", `${actualNumPayments}`],
["Total Paid via Regular Payments:", blbpe_formatCurrency(totalRegularPaymentsMade)],
], margin: { left: 14, right: 14 }
});
startY = doc.autoTable.previous.finalY + 10;
// Section 3: Balloon Payment
doc.setFontSize(14);
if (secondaryColorRGB) doc.setTextColor(secondaryColorRGB.r, secondaryColorRGB.g, secondaryColorRGB.b); else doc.setTextColor(0,51,102);
doc.text("Estimated Balloon Payment", 14, startY); startY += 7;
doc.autoTable({
startY: startY, theme: 'grid', headStyles: { fillColor: tableHeadFillColor },
body: [
[{content: "Estimated Balloon Payment Due:", styles: {fontStyle: 'bold'}},
{content: blbpe_formatCurrency(balloonPayment), styles: {fontStyle: 'bold', fontSize: 12, textColor: primaryColorRGB ? [primaryColorRGB.r, primaryColorRGB.g, primaryColorRGB.b] : [0,90,156] }}],
], margin: { left: 14, right: 14 }
});
startY = doc.autoTable.previous.finalY + 10;
// Section 4: Overall Cost
doc.setFontSize(14);
if (secondaryColorRGB) doc.setTextColor(secondaryColorRGB.r, secondaryColorRGB.g, secondaryColorRGB.b); else doc.setTextColor(0,51,102);
doc.text("Overall Loan Cost", 14, startY); startY += 7;
doc.autoTable({
startY: startY, theme: 'grid', headStyles: { fillColor: tableHeadFillColor },
body: [
["Total Repaid (Regular Payments + Balloon):", blbpe_formatCurrency(totalRepaid)],
["Total Interest Paid:", blbpe_formatCurrency(totalInterestPaid)],
], margin: { left: 14, right: 14 }
});
startY = doc.autoTable.previous.finalY + 10;
doc.setFontSize(10);
doc.setTextColor(150);
doc.text(`Report generated on: ${new Date().toLocaleDateString()} ${new Date().toLocaleTimeString()}`, 14, doc.internal.pageSize.height - 10);
doc.save("Loan_Balloon_Payment_Estimate.pdf");
}
window.blbpe_switchTab = blbpe_switchTab;
window.blbpe_navigateToTab = blbpe_navigateToTab;
window.blbpe_generatePdf = blbpe_generatePdf;