Net Cash Flow
$${netCashFlow.toLocaleString()}
Total Inflow
$${totalInflow.toLocaleString()}
`;
const rows = calculationResults.monthlyData.map(d => `
| ${d.month} | $${d.inflow.toLocaleString()} | $${d.outflow.toLocaleString()} | $${d.end.toLocaleString()} |
`).join('');
document.getElementById('projection-table-container').innerHTML = `
| Month | Inflow | Outflow | End Bal. |
${rows}
`;
const ctx = document.getElementById('projection-chart').getContext('2d');
if (projectionChart) projectionChart.destroy();
projectionChart = new Chart(ctx, { type: 'line', data: {
labels: calculationResults.monthlyData.map(d => `Month ${d.month}`),
datasets: [{ label: 'Ending Cash Balance', data: calculationResults.monthlyData.map(d => d.end), backgroundColor: '#05966930', borderColor: '#059669', tension: 0.1, fill: true }]
}, options: { responsive: true, maintainAspectRatio: false, scales: { y: { ticks: { callback: v => `$${(v/1000)}k` } } } } });
}
// --- PDF GENERATION ---
async function generatePdfReport() {
downloadPdfBtn.disabled = true;
downloadPdfBtn.textContent = 'Generating...';
const { endingBalance, netCashFlow, totalInflow, monthlyData } = calculationResults;
const tableRows = monthlyData.map(d => `
| ${d.month} | $${d.start.toLocaleString()} | $${d.inflow.toLocaleString()} | $${d.outflow.toLocaleString()} | $${d.netFlow.toLocaleString()} | $${d.end.toLocaleString()} |
`).join('');
const reportHtml = `
ENDING BALANCE
$${endingBalance.toLocaleString()}
NET CASH FLOW
$${netCashFlow.toLocaleString()}
TOTAL INFLOW
$${totalInflow.toLocaleString()}
Cash Balance Over Time
Month-by-Month Breakdown
| Month | Start Bal. | Inflow | Outflow | Net Flow | End Bal. |
${tableRows}
`;
const pdfTemplate = document.getElementById('pdf-template');
pdfTemplate.innerHTML = reportHtml;
pdfTemplate.classList.remove('invisible');
new Chart(document.getElementById('pdf-projection-chart'), { type: 'line', data: projectionChart.data, 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('Cash_Flow_Projection.pdf');
} catch (e) { console.error('PDF Generation Error:', e); } finally {
downloadPdfBtn.disabled = false;
downloadPdfBtn.textContent = 'Download Projection PDF';
pdfTemplate.classList.add('invisible');
pdfTemplate.innerHTML = '';
}
}, 500);
}
downloadPdfBtn.addEventListener('click', generatePdfReport);
// --- INITIALIZATION ---
switchTab(0);
renderList('income');
renderList('expense');
renderEventsTable();
});