diff --git a/src/pages/Home/Home.tsx b/src/pages/Home/Home.tsx index fd0ee56..0f68fab 100644 --- a/src/pages/Home/Home.tsx +++ b/src/pages/Home/Home.tsx @@ -68,6 +68,14 @@ function Home() { setLoading(true); setError(null); + // Helper for local date string YYYY-MM-DD to fix timezone issues + const toLocalDate = (d: Date) => { + const year = d.getFullYear(); + const month = String(d.getMonth() + 1).padStart(2, '0'); + const day = String(d.getDate()).padStart(2, '0'); + return `${year}-${month}-${day}`; + }; + // Calculate dates for today, yesterday, and last week same day const today = new Date(); const yesterday = new Date(today); @@ -75,9 +83,9 @@ function Home() { const lastWeek = new Date(today); lastWeek.setDate(lastWeek.getDate() - 7); - const todayStr = today.toISOString().split('T')[0]; - const yesterdayStr = yesterday.toISOString().split('T')[0]; - const lastWeekStr = lastWeek.toISOString().split('T')[0]; + const todayStr = toLocalDate(today); + const yesterdayStr = toLocalDate(yesterday); + const lastWeekStr = toLocalDate(lastWeek); // Load accounts, recent transactions, today/yesterday stats... AND last week const [accountsData, transactionsData, categoriesData, ledgersData, settingsData, budgetsData, todayData, yesterdayData, lastWeekData, streakData] = await Promise.all([ @@ -103,17 +111,22 @@ function Home() { setTodaySpend(calculateTotalExpense(todayData.items)); setTodayTransactions(todayData.items || []); setYesterdaySpend(calculateTotalExpense(yesterdayData.items)); - setLastWeekSpend(calculateTotalExpense(lastWeekData.items)); // Store this in new state + setLastWeekSpend(calculateTotalExpense(lastWeekData.items)); - // Calculate monthly budget stats + // Calculate monthly budget stats (Normalized) let mTotal = 0; let mSpent = 0; - // ... (budgetsData || []).forEach((b: any) => { - if (b.periodType === 'monthly') { - mTotal += b.amount; - mSpent += (b.spent || 0); - } + let multiplier = 1; + if (b.periodType === 'daily') multiplier = 30; + else if (b.periodType === 'weekly') multiplier = 4.3; + else if (b.periodType === 'yearly') multiplier = 1 / 12; + + mTotal += b.amount * multiplier; + // We add the actual spent amount regardless of period, as it contributes to "Monthly Spending" in a general sense + // But strictly speaking, Daily spent resets daily. + // For the purpose of "Monthly Overview", we just want to know if there IS a budget. + mSpent += (b.spent || 0); }); setMonthlyBudgetTotal(mTotal); setMonthlyBudgetSpentTotal(mSpent);