`;
overviewContent.innerHTML = html;
msat_renderCategoryChart(data.categorySpending);
}
function msat_renderCategoryChart(categoryData) {
const chartContainer = document.getElementById('msatCategoryChartContainer');
const canvas = document.getElementById('msatCategoryChart');
if (Object.keys(categoryData).length === 0) {
chartContainer.style.display = 'none';
return;
}
chartContainer.style.display = 'block';
const labels = Object.keys(categoryData);
const data = Object.values(categoryData);
// Generate colors dynamically for the chart
const chartColors = labels.map((_, index) => {
// Simple color generation, can be improved
const hue = (index * (360 / labels.length)) % 360;
return `hsl(${hue}, 70%, 60%)`;
});
if (msat_chartInstance) {
msat_chartInstance.destroy();
}
msat_chartInstance = new Chart(canvas, {
type: 'pie', // or 'doughnut'
data: {
labels: labels,
datasets: [{
label: 'Spending by Category',
data: data,
backgroundColor: chartColors,
borderColor: '#fff',
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: true, // Try true or false based on layout needs
plugins: {
legend: {
position: 'top',
},
title: {
display: true,
text: 'Monthly Spending by Category ($)'
}
}
}
});
}
function msat_updateReportPreview() {
const data = msat_getCalculatedData();
const reportPreview = document.getElementById('msatReportPreview');
let html = `
Subscription Audit Summary
Total Monthly Subscription Cost: $${data.totalMonthlyCost.toFixed(2)}
Total Annual Subscription Cost: $${data.totalAnnualCost.toFixed(2)}
Potential Monthly Savings: $${data.potentialMonthlySavings.toFixed(2)}
Potential Annual Savings: $${data.potentialAnnualSavings.toFixed(2)}
Spending by Category (Monthly):
- `;
for (const category in data.categorySpending) {
html += `
- ${category}: $${data.categorySpending[category].toFixed(2)} `; } html += `
Full details will be in the PDF.
`; reportPreview.innerHTML = html; } function msat_generatePdf() { const data = msat_getCalculatedData(); const { jsPDF } = window.jspdf; const doc = new jsPDF(); const primaryColor = getComputedStyle(document.documentElement).getPropertyValue('--primary-color').trim(); const accentColor = getComputedStyle(document.documentElement).getPropertyValue('--accent-color').trim(); const reviewColor = getComputedStyle(document.documentElement).getPropertyValue('--review-color').trim(); const textColor = getComputedStyle(document.documentElement).getPropertyValue('--text-color').trim(); let yPos = 20; doc.setFontSize(20); doc.setTextColor(primaryColor); doc.text("Monthly Subscription Audit Report", doc.internal.pageSize.getWidth() / 2, yPos, { align: 'center' }); yPos += 15; // Overall Summary doc.setFontSize(14); doc.setTextColor(primaryColor); doc.text("Overall Financial Summary", 14, yPos); yPos += 8; doc.setFontSize(11); doc.setTextColor(textColor); doc.text(`Total Monthly Subscription Cost: $${data.totalMonthlyCost.toFixed(2)}`, 14, yPos); yPos += 7; doc.text(`Total Annual Subscription Cost: $${data.totalAnnualCost.toFixed(2)}`, 14, yPos); yPos += 7; doc.text(`Potential Monthly Savings (from 'Review' items): `, 14, yPos); doc.setTextColor(accentColor); doc.text(`$${data.potentialMonthlySavings.toFixed(2)}`, 100, yPos); yPos += 7; doc.setTextColor(textColor); doc.text(`Potential Annual Savings: `, 14, yPos); doc.setTextColor(accentColor); doc.text(`$${data.potentialAnnualSavings.toFixed(2)}`, 100, yPos); yPos += 10; doc.setTextColor(textColor); // All Subscriptions Table if (msat_subscriptions.length > 0) { doc.setFontSize(14); doc.setTextColor(primaryColor); doc.text("All Subscriptions List", 14, yPos); yPos += 8; const tableBody = msat_subscriptions.map(sub => [ sub.name, sub.category, `$${sub.cost.toFixed(2)}`, sub.status === 'review' ? 'Review' : 'Keep' ]); doc.autoTable({ startY: yPos, head: [['Name', 'Category', 'Monthly Cost', 'Status']], body: tableBody, theme: 'grid', headStyles: { fillColor: primaryColor, textColor: '#ffffff' }, styles: { font: 'Arial', fontSize: 10 }, didParseCell: function (data) { // Color 'Review' status rows if (data.column.index === 3 && data.cell.section === 'body' && data.cell.text[0] === 'Review') { data.cell.styles.fillColor = reviewColor; // light yellow/orange data.cell.styles.textColor = '#504400'; } }, didDrawPage: function(dataHook) { yPos = dataHook.cursor.y; } }); yPos = doc.lastAutoTable.finalY + 10; } else { doc.setFontSize(11); doc.text("No subscriptions were added to the audit.", 14, yPos); yPos += 10; } // Category Spending Summary if (Object.keys(data.categorySpending).length > 0) { doc.setFontSize(14); doc.setTextColor(primaryColor); doc.text("Monthly Spending by Category", 14, yPos); yPos += 8; doc.setFontSize(10); doc.setTextColor(textColor); let catY = yPos; const categoryEntries = Object.entries(data.categorySpending).sort((a,b) => b[1] - a[1]); // Sort by amount categoryEntries.forEach(([category, amount]) => { if (catY > doc.internal.pageSize.getHeight() - 20) { // Check for page break doc.addPage(); catY = 20; } doc.text(`${category}: `, 14, catY); doc.text(`$${amount.toFixed(2)}`, 80, catY, {align: 'left'}); catY += 6; }); yPos = catY + 4; // Space after category list } // Optional: List subscriptions marked for review const reviewSubs = msat_subscriptions.filter(s => s.status === 'review'); if (reviewSubs.length > 0) { if (yPos > doc.internal.pageSize.getHeight() - 40) { doc.addPage(); yPos = 20; } // Check for page break doc.setFontSize(14); doc.setTextColor(reviewColor); // Use a distinct color doc.text("Subscriptions Marked for Review/Cancellation", 14, yPos); yPos += 8; const reviewTableBody = reviewSubs.map(sub => [ sub.name, sub.category, `$${sub.cost.toFixed(2)}` ]); doc.autoTable({ startY: yPos, head: [['Name', 'Category', 'Monthly Cost (Potential Saving)']], body: reviewTableBody, theme: 'grid', headStyles: { fillColor: reviewColor, textColor: '#504400' }, styles: { font: 'Arial', fontSize: 10 }, didDrawPage: function(dataHook) { yPos = dataHook.cursor.y; } }); yPos = doc.lastAutoTable.finalY + 10; } doc.save('monthly_subscription_audit.pdf'); } // Initialize document.addEventListener('DOMContentLoaded', () => { msat_showTab(0); msat_renderSubscriptionList(); // Initial render for empty state // Add a few default subscriptions for demo/testing msat_subscriptions.push({id: 'sub_001', name: 'Streaming Service A', category: 'Streaming', cost: 12.99, status: 'keep'}); msat_subscriptions.push({id: 'sub_002', name: 'Music Platform B', category: 'Music', cost: 9.99, status: 'keep'}); msat_subscriptions.push({id: 'sub_003', name: 'Old Magazine Sub', category: 'News & Learning', cost: 5.00, status: 'review'}); msat_renderSubscriptionList(); });