`;
if (currentHustle.cogsItems.length > 0) {
pdfHtml += `
Itemized Direct Costs (COGS)
| Description | Amount ($) |
`;
currentHustle.cogsItems.forEach(item => {
pdfHtml += `| ${item.description} | ${item.amount.toFixed(2)} |
`;
});
pdfHtml += `| Total COGS | $${currentHustle.totalCogs.toFixed(2)} |
`;
}
if (currentHustle.opexItems.length > 0) {
pdfHtml += `
Itemized Operating Expenses
| Description | Amount ($) |
`;
currentHustle.opexItems.forEach(item => {
pdfHtml += `| ${item.description} | ${item.amount.toFixed(2)} |
`;
});
pdfHtml += `| Total OpEx | $${currentHustle.totalOpex.toFixed(2)} |
`;
}
pdfHtml += `
`; // End Actual Performance Section
if (currentHustle.projections) {
pdfHtml += `
"What-If" Projection
`;
pdfHtml += `
`;
pdfHtml += `
Projected Revenue: $${currentHustle.projections.revenue.toFixed(2)}
Projected Direct Costs (COGS): $${currentHustle.projections.cogs.toFixed(2)}
Projected Gross Profit: $${currentHustle.projections.grossProfit.toFixed(2)}
Projected Gross Profit Margin: ${currentHustle.projections.grossProfitMargin.toFixed(1)}%
Projected Operating Expenses: $${currentHustle.projections.opex.toFixed(2)}
Projected Net Profit: $${currentHustle.projections.netProfit.toFixed(2)}
Projected Net Profit Margin: ${currentHustle.projections.netProfitMargin.toFixed(1)}%
`;
}
pdfHtml += `
`; // Close shpa-pdf-output
pdfOutputEl.innerHTML = pdfHtml;
const opt = {
margin: [0.5, 0.5, 0.5, 0.5],
filename: `Profit_Margin_Report_${(currentHustle.name || "SideHustle").replace(/[^a-zA-Z0-9]/g, '_')}.pdf`,
image: { type: 'jpeg', quality: 0.98 },
html2canvas: { scale: 2, useCORS: true, logging: false },
jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
};
if (html2pdf && pdfOutputEl) {
html2pdf().from(pdfOutputEl).set(opt).save()
.catch(err => { console.error("PDF generation failed:", err); alert("PDF generation error. See console."); });
} else {
alert("PDF library not loaded or output element missing.");
}
});
// --- Tab Navigation ---
window.shpa_openTab = function(evt, tabName) {
tabs.forEach(tab => { if(tab) tab.style.display = 'none'; });
tabLinks.forEach(link => { if(link) link.classList.remove('active'); });
const activeTabContent = toolContainer.querySelector('#' + tabName);
if(activeTabContent) activeTabContent.style.display = 'block';
else { console.error(`Tab content for ${tabName} not found.`); return; }
let clickedIndex = -1;
if (evt && evt.currentTarget) {
evt.currentTarget.classList.add('active');
clickedIndex = tabLinks.indexOf(evt.currentTarget);
} else {
clickedIndex = tabLinks.findIndex(link => {
const onclickAttr = link.getAttribute('onclick');
return onclickAttr && onclickAttr.includes(tabName);
});
if (clickedIndex !== -1 && tabLinks[clickedIndex]) tabLinks[clickedIndex].classList.add('active');
}
currentTabIndex = clickedIndex !== -1 ? clickedIndex : 0;
shpa_updateNavButtons();
// If opening analysis tab, ensure calculations are fresh
if (tabName === 'shpa-tab3') {
shpa_performFullAnalysis();
}
}
window.shpa_navigateTab = function(direction) {
let newIndex = currentTabIndex;
if (direction === 'next' && currentTabIndex < tabs.length - 1) newIndex++;
else if (direction === 'prev' && currentTabIndex > 0) newIndex--;
if (tabLinks[newIndex] && newIndex !== currentTabIndex) tabLinks[newIndex].click();
}
function shpa_updateNavButtons() {
if(prevButton) prevButton.disabled = currentTabIndex === 0;
if(nextButton) nextButton.disabled = currentTabIndex >= tabs.length - 1;
}
// --- Initialization ---
function initializeTool() {
shpa_renderCostTablesAndTotals(); // Initial render for empty tables
shpa_openTab(null, 'shpa-tab1'); // Open first tab
// Load any persisted helper data if implemented (e.g., last hustle name)
// const lastHustleName = localStorage.getItem(`${toolId}_lastHustleName`);
// if (lastHustleName && hustleNameEl) hustleNameEl.value = lastHustleName;
}
let attempts = 0;
function checkLibrariesAndInit_shpa() {
if (typeof html2pdf !== 'undefined') {
initializeTool();
} else if (attempts < 20) {
attempts++;
setTimeout(checkLibrariesAndInit_shpa, 100);
} else {
alert("Error: PDF generation library (html2pdf.js) could not be loaded. PDF export will not work.");
initializeTool();
}
}
checkLibrariesAndInit_shpa();
})();