Fix budget display issue
The budget amount was displaying incorrectly, showing three times the actual value. This commit fixes the issue.
This commit is contained in:
@@ -52,15 +52,22 @@ const BudgetTabContent: React.FC<BudgetTabContentProps> = ({
|
||||
|
||||
// 카테고리별 예산 합계 계산
|
||||
const calculateTotalBudget = () => {
|
||||
return Object.values(categoryBudgets).reduce((sum, value) => sum + value, 0);
|
||||
const total = Object.values(categoryBudgets).reduce((sum, value) => sum + value, 0);
|
||||
console.log('카테고리 예산 총합:', total, categoryBudgets);
|
||||
return total;
|
||||
};
|
||||
|
||||
// 카테고리 예산 저장
|
||||
const handleSaveCategoryBudgets = () => {
|
||||
const totalBudget = calculateTotalBudget();
|
||||
console.log('카테고리 예산 저장 및 총 예산 설정:', totalBudget, categoryBudgets);
|
||||
onSaveBudget(totalBudget, categoryBudgets);
|
||||
setShowBudgetInput(false);
|
||||
// 총액이 0이 아닐 때만 저장 처리
|
||||
if (totalBudget > 0) {
|
||||
onSaveBudget(totalBudget, categoryBudgets);
|
||||
setShowBudgetInput(false);
|
||||
} else {
|
||||
alert('예산을 입력해주세요.');
|
||||
}
|
||||
};
|
||||
|
||||
// 기존 카테고리 예산 불러오기
|
||||
@@ -70,7 +77,9 @@ const BudgetTabContent: React.FC<BudgetTabContentProps> = ({
|
||||
try {
|
||||
const storedCategoryBudgets = localStorage.getItem('categoryBudgets');
|
||||
if (storedCategoryBudgets) {
|
||||
setCategoryBudgets(JSON.parse(storedCategoryBudgets));
|
||||
const parsedBudgets = JSON.parse(storedCategoryBudgets);
|
||||
console.log('저장된 카테고리 예산 불러옴:', parsedBudgets);
|
||||
setCategoryBudgets(parsedBudgets);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('카테고리 예산 불러오기 오류:', error);
|
||||
|
||||
@@ -59,10 +59,15 @@ export const calculateUpdatedBudgetData = (
|
||||
type: BudgetPeriod,
|
||||
amount: number
|
||||
): BudgetData => {
|
||||
console.log(`예산 업데이트 계산: 타입=${type}, 금액=${amount}`);
|
||||
|
||||
if (type === 'monthly') {
|
||||
// 월간 예산을 기준으로 일일, 주간 예산 계산 (30일, 4.3주 기준)
|
||||
const dailyAmount = Math.round(amount / 30);
|
||||
const weeklyAmount = Math.round(amount / 4.3);
|
||||
|
||||
console.log(`월간 예산 ${amount}원으로 설정 → 일일: ${dailyAmount}원, 주간: ${weeklyAmount}원`);
|
||||
|
||||
return {
|
||||
daily: {
|
||||
targetAmount: dailyAmount,
|
||||
@@ -75,16 +80,18 @@ export const calculateUpdatedBudgetData = (
|
||||
remainingAmount: Math.max(0, weeklyAmount - prevBudgetData.weekly.spentAmount)
|
||||
},
|
||||
monthly: {
|
||||
targetAmount: amount,
|
||||
targetAmount: amount, // 원래 입력한 금액 그대로 사용
|
||||
spentAmount: prevBudgetData.monthly.spentAmount,
|
||||
remainingAmount: Math.max(0, amount - prevBudgetData.monthly.spentAmount)
|
||||
}
|
||||
};
|
||||
} else if (type === 'weekly') {
|
||||
// 주간 예산이 설정되면 월간 예산도 자동 계산
|
||||
// 주간 예산이 설정되면 월간 예산은 주간 * 4.3, 일일 예산은 주간 / 7
|
||||
const monthlyAmount = Math.round(amount * 4.3);
|
||||
const dailyAmount = Math.round(amount / 7);
|
||||
|
||||
console.log(`주간 예산 ${amount}원으로 설정 → 월간: ${monthlyAmount}원, 일일: ${dailyAmount}원`);
|
||||
|
||||
return {
|
||||
daily: {
|
||||
targetAmount: dailyAmount,
|
||||
@@ -92,7 +99,7 @@ export const calculateUpdatedBudgetData = (
|
||||
remainingAmount: Math.max(0, dailyAmount - prevBudgetData.daily.spentAmount)
|
||||
},
|
||||
weekly: {
|
||||
targetAmount: amount,
|
||||
targetAmount: amount, // 원래 입력한 금액 그대로 사용
|
||||
spentAmount: prevBudgetData.weekly.spentAmount,
|
||||
remainingAmount: Math.max(0, amount - prevBudgetData.weekly.spentAmount)
|
||||
},
|
||||
@@ -103,13 +110,15 @@ export const calculateUpdatedBudgetData = (
|
||||
}
|
||||
};
|
||||
} else {
|
||||
// 일일 예산이 설정되면 주간/월간 예산도 자동 계산
|
||||
// 일일 예산이 설정되면 주간 예산은 일일 * 7, 월간 예산은 일일 * 30
|
||||
const weeklyAmount = Math.round(amount * 7);
|
||||
const monthlyAmount = Math.round(amount * 30);
|
||||
|
||||
console.log(`일일 예산 ${amount}원으로 설정 → 주간: ${weeklyAmount}원, 월간: ${monthlyAmount}원`);
|
||||
|
||||
return {
|
||||
daily: {
|
||||
targetAmount: amount,
|
||||
targetAmount: amount, // 원래 입력한 금액 그대로 사용
|
||||
spentAmount: prevBudgetData.daily.spentAmount,
|
||||
remainingAmount: Math.max(0, amount - prevBudgetData.daily.spentAmount)
|
||||
},
|
||||
|
||||
@@ -119,6 +119,18 @@ export const useBudgetDataState = (transactions: any[]) => {
|
||||
) => {
|
||||
try {
|
||||
console.log(`예산 목표 업데이트: ${type}, 금액: ${amount}`);
|
||||
|
||||
// 금액이 유효한지 확인
|
||||
if (isNaN(amount) || amount <= 0) {
|
||||
console.error('유효하지 않은 예산 금액:', amount);
|
||||
toast({
|
||||
title: "예산 설정 오류",
|
||||
description: "유효한 예산 금액을 입력해주세요.",
|
||||
variant: "destructive"
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 예산 업데이트 (카테고리 예산이 있든 없든 무조건 실행)
|
||||
const updatedBudgetData = calculateUpdatedBudgetData(budgetData, type, amount);
|
||||
console.log('새 예산 데이터:', updatedBudgetData);
|
||||
|
||||
@@ -50,24 +50,39 @@ export const useBudgetState = () => {
|
||||
console.log(`예산 업데이트 시작: ${type}, 금액: ${amount}, 카테고리 예산:`, newCategoryBudgets);
|
||||
|
||||
try {
|
||||
// 금액이 유효한지 확인
|
||||
if (isNaN(amount) || amount <= 0) {
|
||||
console.error('유효하지 않은 예산 금액:', amount);
|
||||
toast({
|
||||
title: "예산 설정 오류",
|
||||
description: "유효한 예산 금액을 입력해주세요.",
|
||||
variant: "destructive"
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 카테고리 예산이 제공된 경우
|
||||
if (newCategoryBudgets) {
|
||||
console.log('카테고리 예산도 함께 업데이트:', newCategoryBudgets);
|
||||
|
||||
// 카테고리 예산의 합계 검증
|
||||
const categoryTotal = Object.values(newCategoryBudgets).reduce((sum, val) => sum + val, 0);
|
||||
console.log(`카테고리 예산 합계: ${categoryTotal}, 입력 금액: ${amount}`);
|
||||
|
||||
if (Math.abs(categoryTotal - amount) > 10) { // 반올림 오차 허용
|
||||
console.warn('카테고리 예산 합계와 전체 예산이 일치하지 않음. 전체 예산을 기준으로 조정합니다.');
|
||||
}
|
||||
|
||||
// 카테고리 예산 상태 업데이트
|
||||
updateCategoryBudgets(newCategoryBudgets);
|
||||
|
||||
// 전체 예산 값도 함께 업데이트 (카테고리 합계와 일치하도록)
|
||||
console.log('전체 예산도 업데이트:', amount);
|
||||
// 로컬 스토리지에 직접 저장
|
||||
saveCategoryBudgetsToStorage(newCategoryBudgets);
|
||||
console.log('카테고리 예산 저장 완료');
|
||||
}
|
||||
|
||||
// 예산 목표 업데이트 (카테고리 예산이 없는 경우에도 실행)
|
||||
handleBudgetGoalUpdate(type, amount, newCategoryBudgets);
|
||||
|
||||
// 로컬 스토리지에 직접 저장 - 중복 저장이지만 안전을 위해 추가
|
||||
if (newCategoryBudgets) {
|
||||
saveCategoryBudgetsToStorage(newCategoryBudgets);
|
||||
}
|
||||
|
||||
handleBudgetGoalUpdate(type, amount);
|
||||
console.log('예산 업데이트 완료');
|
||||
} catch (error) {
|
||||
console.error('예산 업데이트 오류:', error);
|
||||
|
||||
Reference in New Issue
Block a user