Fix budget calculation and display

Ensure daily and weekly budgets are calculated correctly based on the monthly budget, and that the monthly budget is consistent across the app.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-22 12:02:41 +00:00
parent cbee1f4fb9
commit 0bc53208b5
3 changed files with 45 additions and 13 deletions

View File

@@ -8,8 +8,11 @@ import { safelyLoadBudgetData } from '../budgetUtils';
*/ */
export const useBudgetState = () => { export const useBudgetState = () => {
// 초기 데이터 로드 시 safelyLoadBudgetData 함수 사용 // 초기 데이터 로드 시 safelyLoadBudgetData 함수 사용
const [budgetData, setBudgetData] = useState<BudgetData>(safelyLoadBudgetData()); const initialBudgetData = safelyLoadBudgetData();
const [selectedTab, setSelectedTab] = useState<BudgetPeriod>("daily"); // 초기값을 daily로 변경 console.log('초기 예산 데이터 로드:', initialBudgetData);
const [budgetData, setBudgetData] = useState<BudgetData>(initialBudgetData);
const [selectedTab, setSelectedTab] = useState<BudgetPeriod>("daily"); // 초기값은 daily
const [isInitialized, setIsInitialized] = useState(false); const [isInitialized, setIsInitialized] = useState(false);
const [lastUpdateTime, setLastUpdateTime] = useState(0); const [lastUpdateTime, setLastUpdateTime] = useState(0);

View File

@@ -13,19 +13,26 @@ export const calculateSpentAmounts = (
// 오늘 지출 계산 // 오늘 지출 계산
const todayExpenses = expenseTransactions.filter(t => { const todayExpenses = expenseTransactions.filter(t => {
if (t.date.includes('오늘')) return true; if (t.date && (t.date.includes('오늘') || t.date.includes('today'))) return true;
return false; return false;
}); });
const dailySpent = todayExpenses.reduce((sum, t) => sum + t.amount, 0); const dailySpent = todayExpenses.reduce((sum, t) => sum + t.amount, 0);
// 이번 주 지출 계산 (단순화된 버전) // 이번 주 지출 계산 (단순화된 버전)
const weeklyExpenses = expenseTransactions.filter(t => { const weeklyExpenses = expenseTransactions.filter(t => {
if (t.date.includes('오늘') || t.date.includes('어제') || t.date.includes('이번주')) return true; if (t.date && (
return true; t.date.includes('오늘') ||
t.date.includes('today') ||
t.date.includes('어제') ||
t.date.includes('yesterday') ||
t.date.includes('이번주') ||
t.date.includes('this week')
)) return true;
return false;
}); });
const weeklySpent = weeklyExpenses.reduce((sum, t) => sum + t.amount, 0); const weeklySpent = weeklyExpenses.reduce((sum, t) => sum + t.amount, 0);
// 이번 달 총 지출 계산 // 이번 달 총 지출 계산 (모든 지출 트랜잭션)
const monthlySpent = expenseTransactions.reduce((sum, t) => sum + t.amount, 0); const monthlySpent = expenseTransactions.reduce((sum, t) => sum + t.amount, 0);
// 기존 예산 목표 유지 (없으면 기본값 0) // 기존 예산 목표 유지 (없으면 기본값 0)
@@ -33,6 +40,12 @@ export const calculateSpentAmounts = (
const weeklyTarget = prevBudgetData?.weekly?.targetAmount || 0; const weeklyTarget = prevBudgetData?.weekly?.targetAmount || 0;
const monthlyTarget = prevBudgetData?.monthly?.targetAmount || 0; const monthlyTarget = prevBudgetData?.monthly?.targetAmount || 0;
console.log("예산 목표 확인:", {
일일: dailyTarget,
주간: weeklyTarget,
월간: monthlyTarget
});
// 예산 데이터 업데이트 // 예산 데이터 업데이트
const updatedBudget = { const updatedBudget = {
daily: { daily: {

View File

@@ -2,8 +2,7 @@
import { useCallback } from 'react'; import { useCallback } from 'react';
import { toast } from '@/hooks/useToast.wrapper'; import { toast } from '@/hooks/useToast.wrapper';
import { import {
loadTransactionsFromStorage, loadTransactionsFromStorage
loadBudgetFromStorage
} from './storageUtils'; } from './storageUtils';
/** /**
@@ -25,11 +24,28 @@ export const useTransactionsLoader = (
const localTransactions = loadTransactionsFromStorage(); const localTransactions = loadTransactionsFromStorage();
setTransactions(localTransactions); setTransactions(localTransactions);
// 예산 가져오기 (월간 예산만 설정) // 예산 데이터에서 직접 월간 예산 값을 가져옴
const budgetAmount = loadBudgetFromStorage(); try {
setTotalBudget(budgetAmount); const budgetDataStr = localStorage.getItem('budgetData');
if (budgetDataStr) {
console.log('로드된 예산 금액:', budgetAmount); const budgetData = JSON.parse(budgetDataStr);
// 월간 예산 값만 사용
if (budgetData && budgetData.monthly && typeof budgetData.monthly.targetAmount === 'number') {
const monthlyBudget = budgetData.monthly.targetAmount;
setTotalBudget(monthlyBudget);
console.log('월간 예산 설정:', monthlyBudget);
} else {
console.log('유효한 월간 예산 데이터가 없습니다. 기본값 0 사용');
setTotalBudget(0);
}
} else {
console.log('예산 데이터가 없습니다. 기본값 0 사용');
setTotalBudget(0);
}
} catch (budgetErr) {
console.error('예산 데이터 파싱 오류:', budgetErr);
setTotalBudget(0);
}
} catch (err) { } catch (err) {
console.error('트랜잭션 로드 중 오류:', err); console.error('트랜잭션 로드 중 오류:', err);
setError('데이터를 불러오는 중 문제가 발생했습니다.'); setError('데이터를 불러오는 중 문제가 발생했습니다.');