Fix budget calculation and storage

Correct budget calculation and storage issues for 교통 and 기타 categories, and ensure daily/weekly budgets are displayed correctly.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-22 07:42:46 +00:00
parent 09e29d57f3
commit 0d716d79d2
6 changed files with 35 additions and 17 deletions

View File

@@ -1,8 +1,11 @@
import React, { useState, useEffect } from 'react';
import { CirclePlus, Save, Check } from 'lucide-react';
import BudgetInputCard from './BudgetInputCard';
import { Button } from '@/components/ui/button';
import CategoryBudgetInputs from './CategoryBudgetInputs';
import { EXPENSE_CATEGORIES } from '@/constants/categoryIcons';
interface BudgetData {
targetAmount: number;
spentAmount: number;
@@ -42,6 +45,7 @@ const BudgetTabContent: React.FC<BudgetTabContentProps> = ({
// 남은 예산 또는 초과 예산 텍스트 및 금액
const budgetStatusText = isOverBudget ? '예산 초과: ' : '남은 예산: ';
const budgetAmount = isOverBudget ? formatCurrency(Math.abs(targetAmount - spentAmount)) : formatCurrency(Math.max(0, targetAmount - spentAmount));
const handleCategoryInputChange = (value: string, category: string) => {
const numValue = parseInt(value, 10) || 0;
setCategoryBudgets(prev => ({
@@ -52,18 +56,29 @@ const BudgetTabContent: React.FC<BudgetTabContentProps> = ({
// 카테고리별 예산 합계 계산
const calculateTotalBudget = () => {
const total = Object.values(categoryBudgets).reduce((sum, value) => sum + value, 0);
// 모든 EXPENSE_CATEGORIES에 있는 카테고리 포함해서 합계 계산
let total = 0;
EXPENSE_CATEGORIES.forEach(category => {
total += categoryBudgets[category] || 0;
});
console.log('카테고리 예산 총합:', total, categoryBudgets);
return total;
};
// 카테고리 예산 저장
const handleSaveCategoryBudgets = () => {
// 카테고리 예산 기본값 설정 - 모든 카테고리 포함
const updatedCategoryBudgets: Record<string, number> = {};
EXPENSE_CATEGORIES.forEach(category => {
updatedCategoryBudgets[category] = categoryBudgets[category] || 0;
});
const totalBudget = calculateTotalBudget();
console.log('카테고리 예산 저장 및 총 예산 설정:', totalBudget, categoryBudgets);
console.log('카테고리 예산 저장 및 총 예산 설정:', totalBudget, updatedCategoryBudgets);
// 총액이 0이 아닐 때만 저장 처리
if (totalBudget > 0) {
onSaveBudget(totalBudget, categoryBudgets);
onSaveBudget(totalBudget, updatedCategoryBudgets);
setShowBudgetInput(false);
} else {
alert('예산을 입력해주세요.');

View File

@@ -32,9 +32,9 @@ const CategoryBudgetInputs: React.FC<CategoryBudgetInputsProps> = ({
try {
const amount = parseInt(numericValue, 10) || 0;
markSingleCategoryBudgetAsModified(category, amount);
console.log(`[예산 추적] 카테고리 '${category}' 예산 변경 추적: ${amount}`);
console.log(`카테고리 '${category}' 예산 ${amount}으로 수정 완료, 타임스탬프: ${new Date().toISOString()}`);
} catch (error) {
console.error(`[예산 추적] 카테고리 '${category}' 예산 변경 추적 실패:`, error);
console.error(`카테고리 '${category}' 예산 변경 추적 실패:`, error);
}
// 사용자에게 시각적 피드백 제공