`;
inputsHtml += `
Buying Scenario Inputs:
Property Price: ${lb_formatCurrency(inputs.buy.propPrice)}
Down Payment: ${inputs.buy.dpType === 'percent' ? inputs.buy.dpValue + '%' : lb_formatCurrency(inputs.buy.dpValue)} (${lb_formatCurrency(inputs.buy.actualDownPayment)})
Closing Costs (Buy): ${inputs.buy.ccType === 'percent' ? inputs.buy.ccValue + '%' : lb_formatCurrency(inputs.buy.ccValue)} (${lb_formatCurrency(inputs.buy.actualClosingCostsBuy)})
${inputs.buy.loanAmount > 0 ? `
Mortgage Rate: ${lb_formatPercentage(inputs.buy.mortgageRate)}
Loan Term: ${inputs.buy.loanTermYrs} years
` : ''}
Property Taxes: ${inputs.buy.taxType === 'percent' ? inputs.buy.taxValue + '%' : lb_formatCurrency(inputs.buy.taxValue)} / year
Home Insurance: ${lb_formatCurrency(inputs.buy.insuranceAnnual)} / year
Maintenance: ${inputs.buy.maintType === 'percent' ? inputs.buy.maintValue + '%' : lb_formatCurrency(inputs.buy.maintValue)} / year
HOA Fees: ${lb_formatCurrency(inputs.buy.hoaAnnual)} / year
Est. Property Appreciation: ${lb_formatPercentage(inputs.buy.appreciationRate)} p.a.
Est. Selling Costs: ${lb_formatPercentage(inputs.buy.sellingCostsPercent)} of sale price
`;
inputsHtml += `
Leasing Scenario Inputs:
Initial Monthly Rent: ${lb_formatCurrency(inputs.lease.monthlyRent)}
Annual Rent Escalation: ${lb_formatPercentage(inputs.lease.rentEscalationRate)}
Renter's Insurance: ${lb_formatCurrency(inputs.lease.rentersInsuranceAnnual)} / year
One-time Leasing Costs: ${lb_formatCurrency(inputs.lease.oneTimeLeaseCosts)}
Return on Invested Upfront Buying Costs: ${lb_formatPercentage(inputs.lease.investmentReturnRate)} p.a.
`;
const comparisonTableHtml = `
| Metric | Buying Scenario | Leasing Scenario |
| Total Upfront Cash Outlay | ${lb_formatCurrency(buyRes.upfrontCash)} | ${lb_formatCurrency(inputs.lease.oneTimeLeaseCosts)} |
| Avg. Monthly Housing Cost (Est.) | ${lb_formatCurrency(buyRes.avgMonthlyCost)} | ${lb_formatCurrency(leaseRes.avgMonthlyCost)} |
| Proj. Property Equity (End) | ${lb_formatCurrency(buyRes.equityAtEnd)} | N/A |
| Proj. Value of Invested Funds (Leasing) | N/A | ${lb_formatCurrency(leaseRes.futureValueOfInvestedFunds)} |
| Est. Change in Net Worth |
${lb_formatCurrency(buyRes.netWorthChange)} |
${lb_formatCurrency(leaseRes.netWorthChange)} |
`;
let conclusionTextPdf = "Based on inputs: ";
if (Math.abs(buyRes.netWorthChange - leaseRes.netWorthChange) < 100) conclusionTextPdf += `Both scenarios result in a similar estimated change in net worth.`;
else if (buyRes.netWorthChange > leaseRes.netWorthChange) conclusionTextPdf += `Buying appears to lead to a better estimated net worth change by approx. ${lb_formatCurrency(buyRes.netWorthChange - leaseRes.netWorthChange)}.`;
else conclusionTextPdf += `Leasing appears to lead to a better estimated net worth change by approx. ${lb_formatCurrency(leaseRes.netWorthChange - buyRes.netWorthChange)}.`;
const interpretationNote_pdf = `This analysis compares estimated financial outcomes. It excludes tax implications and non-financial factors. Estimates are highly dependent on your inputs (e.g., appreciation, investment returns).`;
pdfContentEl.innerHTML = `
Lease vs. Buy Property Decision Analysis
I. Input Parameters
${inputsHtml}
II. Comparison Summary (Over ${inputs.comparisonPeriodYears} Years)
${conclusionTextPdf}
Important Notes: ${interpretationNote_pdf}
`;
document.body.appendChild(pdfContentEl);
html2canvas(pdfContentEl, { scale: 2, useCORS: true, logging:true, windowWidth: pdfContentEl.scrollWidth, windowHeight: pdfContentEl.scrollHeight }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const imgProps = pdf.getImageProperties(imgData);
let numPages = Math.ceil(imgProps.height / ( (pdfHeight - 40) * (imgProps.width / (pdfWidth - 40)) ) );
let pageCanvasHeight = (pdfHeight - 40) * (imgProps.width / (pdfWidth - 40));
for (let i = 0; i < numPages; i++) {
if (i > 0) pdf.addPage();
let sourceY = i * pageCanvasHeight;
let sourceHeight = Math.min(pageCanvasHeight, imgProps.height - sourceY);
const tempCanvas = document.createElement('canvas');
tempCanvas.width = imgProps.width;
tempCanvas.height = sourceHeight;
const ctx = tempCanvas.getContext('2d');
ctx.drawImage(canvas, 0, sourceY, imgProps.width, sourceHeight, 0, 0, imgProps.width, sourceHeight);
const pageImgData = tempCanvas.toDataURL('image/png');
const pageImgHeight = (sourceHeight * (pdfWidth - 40)) / imgProps.width;
pdf.addImage(pageImgData, 'PNG', 20, 20, pdfWidth - 40, pageImgHeight, undefined, 'FAST');
}
pdf.save('Lease_vs_Buy_Analysis.pdf');
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
}).catch(err => {
console.error("Lease vs Buy PDF Error:", err); alert("Error generating PDF. See console.");
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
});
}
document.addEventListener('DOMContentLoaded', function() {
lb_openTab({}, 'lb_tab_common');
// Default some values for easier testing / usability
const propPriceEl = document.getElementById('lb_buy_propertyPrice');
if (propPriceEl && !propPriceEl.value) propPriceEl.value = '300000';
const dpValueEl = document.getElementById('lb_buy_downPaymentValue');
if (dpValueEl && !dpValueEl.value) dpValueEl.value = '20'; // Assuming % type is default
const ccValueEl = document.getElementById('lb_buy_closingCostsValue');
if (ccValueEl && !ccValueEl.value) ccValueEl.value = '3'; // Assuming % type is default
const mortgageRateEl = document.getElementById('lb_buy_mortgageRate');
if (mortgageRateEl && !mortgageRateEl.value) mortgageRateEl.value = '6';
const loanTermEl = document.getElementById('lb_buy_loanTermYears');
if (loanTermEl && !loanTermEl.value) loanTermEl.value = '30';
const monthlyRentEl = document.getElementById('lb_lease_monthlyRent');
if (monthlyRentEl && !monthlyRentEl.value) monthlyRentEl.value = '1800';
const invReturnEl = document.getElementById('lb_lease_investmentReturnRate');
if (invReturnEl && !invReturnEl.value) invReturnEl.value = '5';
if (!document.getElementById('lb_comparisonPeriodYears')) {
console.error("Critical input 'lb_comparisonPeriodYears' not found.");
}
});