-deat 修复逻辑
This commit is contained in:
@@ -68,6 +68,14 @@ function Home() {
|
|||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
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
|
// Calculate dates for today, yesterday, and last week same day
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
const yesterday = new Date(today);
|
const yesterday = new Date(today);
|
||||||
@@ -75,9 +83,9 @@ function Home() {
|
|||||||
const lastWeek = new Date(today);
|
const lastWeek = new Date(today);
|
||||||
lastWeek.setDate(lastWeek.getDate() - 7);
|
lastWeek.setDate(lastWeek.getDate() - 7);
|
||||||
|
|
||||||
const todayStr = today.toISOString().split('T')[0];
|
const todayStr = toLocalDate(today);
|
||||||
const yesterdayStr = yesterday.toISOString().split('T')[0];
|
const yesterdayStr = toLocalDate(yesterday);
|
||||||
const lastWeekStr = lastWeek.toISOString().split('T')[0];
|
const lastWeekStr = toLocalDate(lastWeek);
|
||||||
|
|
||||||
// Load accounts, recent transactions, today/yesterday stats... AND last week
|
// Load accounts, recent transactions, today/yesterday stats... AND last week
|
||||||
const [accountsData, transactionsData, categoriesData, ledgersData, settingsData, budgetsData, todayData, yesterdayData, lastWeekData, streakData] = await Promise.all([
|
const [accountsData, transactionsData, categoriesData, ledgersData, settingsData, budgetsData, todayData, yesterdayData, lastWeekData, streakData] = await Promise.all([
|
||||||
@@ -103,17 +111,22 @@ function Home() {
|
|||||||
setTodaySpend(calculateTotalExpense(todayData.items));
|
setTodaySpend(calculateTotalExpense(todayData.items));
|
||||||
setTodayTransactions(todayData.items || []);
|
setTodayTransactions(todayData.items || []);
|
||||||
setYesterdaySpend(calculateTotalExpense(yesterdayData.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 mTotal = 0;
|
||||||
let mSpent = 0;
|
let mSpent = 0;
|
||||||
// ...
|
|
||||||
(budgetsData || []).forEach((b: any) => {
|
(budgetsData || []).forEach((b: any) => {
|
||||||
if (b.periodType === 'monthly') {
|
let multiplier = 1;
|
||||||
mTotal += b.amount;
|
if (b.periodType === 'daily') multiplier = 30;
|
||||||
mSpent += (b.spent || 0);
|
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);
|
setMonthlyBudgetTotal(mTotal);
|
||||||
setMonthlyBudgetSpentTotal(mSpent);
|
setMonthlyBudgetSpentTotal(mSpent);
|
||||||
|
|||||||
Reference in New Issue
Block a user