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:
@@ -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('예산을 입력해주세요.');
|
||||
|
||||
Reference in New Issue
Block a user