Net Cash Flow
$${netCashFlow.toLocaleString(undefined, {maximumFractionDigits:0})}
`;
const tableRows = schedule.map(p => `
| ${p.period} | $${p.fixed.toLocaleString(undefined,{maximumFractionDigits:0})} | $${p.floating.toLocaleString(undefined,{maximumFractionDigits:0})} | $${p.net.toLocaleString(undefined,{maximumFractionDigits:0})} |
`).join('');
document.getElementById('schedule-table-container').innerHTML = `
| Period | Fixed Paid | Floating Rcvd | Net |
${tableRows}
`;
const ctx = document.getElementById('flow-chart').getContext('2d');
if (flowChart) flowChart.destroy();
flowChart = new Chart(ctx, { type: 'bar', data: {
labels: schedule.map(p => `P${p.period}`),
datasets: [{ label: 'Net Cash Flow', data: schedule.map(p => p.net), backgroundColor: schedule.map(p => p.net >= 0 ? '#16a34a' : '#dc2626') }]
}, options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { display: false } } } });
}
// --- PDF GENERATION ---
async function generatePdfReport() {
downloadPdfBtn.disabled = true;
downloadPdfBtn.textContent = 'Generating...';
const { totalFixed, totalFloating, netCashFlow, schedule } = analysisResults;
const tableRows = schedule.map(p => `
| ${p.period} | $${p.fixed.toLocaleString()} | $${p.floating.toLocaleString()} | $${p.net.toLocaleString()} |
`).join('');
const reportHtml = `
TOTAL FIXED PAID
$${totalFixed.toLocaleString(undefined, {maximumFractionDigits:0})}
TOTAL FLOATING RECEIVED
$${totalFloating.toLocaleString(undefined, {maximumFractionDigits:0})}
NET CASH FLOW
$${netCashFlow.toLocaleString(undefined, {maximumFractionDigits:0})}
Cash Flow by Period
Detailed Payment Schedule
| Period | Fixed Paid | Floating Received | Net Payment |
${tableRows}
`;
const pdfTemplate = document.getElementById('pdf-template');
pdfTemplate.innerHTML = reportHtml;
pdfTemplate.classList.remove('invisible');
new Chart(document.getElementById('pdf-flow-chart'), { type: 'bar', data: flowChart.data, options: { ...flowChart.options, animation: { duration: 0 }, maintainAspectRatio: false } });
setTimeout(async () => {
try {
const { jsPDF } = window.jspdf;
const canvas = await html2canvas(pdfTemplate.querySelector('.pdf-report-container'), { scale: 2 });
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth(), pdfHeight = (canvas.height * pdfWidth) / canvas.width;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
pdf.save('Interest_Rate_Swap_Analysis.pdf');
} catch (e) { console.error('PDF Generation Error:', e); } finally {
downloadPdfBtn.disabled = false;
downloadPdfBtn.textContent = 'Download PDF Report';
pdfTemplate.classList.add('invisible');
pdfTemplate.innerHTML = '';
}
}, 500);
}
// --- EVENT LISTENERS ---
downloadPdfBtn.addEventListener('click', generatePdfReport);
// --- INITIALIZATION ---
switchTab(0);
});