Fix budget calculation error

Fixes an issue where entering a monthly budget resulted in incorrect daily, weekly, and monthly budget calculations, leading to incorrect display on the spending and analytics screens.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-18 02:31:08 +00:00
parent 139338877a
commit 7f30d08466
3 changed files with 34 additions and 22 deletions

View File

@@ -1,3 +1,4 @@
import React, { useState, useEffect } from 'react';
import { CirclePlus, Save, Check } from 'lucide-react';
import BudgetInputCard from './BudgetInputCard';
@@ -132,13 +133,13 @@ const BudgetTabContent: React.FC<BudgetTabContentProps> = ({
{showBudgetInput && <div className="mt-4">
<div className="neuro-card p-4">
<div>
<h3 className="text-base font-medium mb-3"> </h3>
<p className="text-sm text-gray-500 mb-4"> , .</p>
<h3 className="text-base font-medium mb-3"> </h3>
<p className="text-sm text-gray-500 mb-4"> . , .</p>
<CategoryBudgetInputs categoryBudgets={categoryBudgets} handleCategoryInputChange={handleCategoryInputChange} />
<div className="mt-4 border-t border-gray-300 pt-3">
<div className="flex justify-between items-center mb-4">
<h3 className="font-medium text-sm px-[34px]"> :</h3>
<h3 className="font-medium text-sm px-[34px]"> :</h3>
<p className="font-bold text-neuro-income text-base px-[10px]">{formatCurrency(calculateTotalBudget())}</p>
</div>

View File

@@ -1,3 +1,4 @@
import { BudgetData, BudgetPeriod, CategoryBudget, Transaction } from './types';
import { EXPENSE_CATEGORIES } from '@/constants/categoryIcons';
@@ -52,7 +53,7 @@ export const calculateCategorySpending = (
}));
};
// 예산 데이터 업데이트 계산 - 수정된 함수
// 예산 데이터 업데이트 계산 - 완전히 수정된 함수
export const calculateUpdatedBudgetData = (
prevBudgetData: BudgetData,
type: BudgetPeriod,
@@ -60,17 +61,25 @@ export const calculateUpdatedBudgetData = (
): BudgetData => {
console.log(`예산 업데이트 계산: 타입=${type}, 금액=${amount}`);
// 카테고리 예산은 항상 월간 기준이므로, monthly 계산 방식 사용
// 월간→주간→일간 순서로 변환
const monthlyAmount = type === 'monthly' ? amount :
type === 'weekly' ? Math.round(amount * 4.3) :
Math.round(amount * 30);
// 모든 타입에 대해 월간 예산을 기준으로 계산
let monthlyAmount = amount;
// 월간 금액에서 주간, 일간 계산
const weeklyAmount = Math.round(monthlyAmount / 4.3);
// 선택된 탭이 월간이 아닌 경우, 올바른 월간 값으로 변환
if (type === 'daily') {
// 일일 예산이 입력된 경우: 일일 * 30 = 월간
monthlyAmount = amount * 30;
console.log(`일일 예산 ${amount}원 → 월간 예산 ${monthlyAmount}원으로 변환`);
} else if (type === 'weekly') {
// 주간 예산이 입력된 경우: 주간 * 4.3 = 월간
monthlyAmount = Math.round(amount * 4.3);
console.log(`주간 예산 ${amount}원 → 월간 예산 ${monthlyAmount}원으로 변환`);
}
// 월간 예산을 기준으로 일일, 주간 예산 계산
const dailyAmount = Math.round(monthlyAmount / 30);
const weeklyAmount = Math.round(monthlyAmount / 4.3);
console.log(`예산 변환: 월간=${monthlyAmount}원, 주간=${weeklyAmount}원, 일=${dailyAmount}`);
console.log(`최종 예산 계산: 월간=${monthlyAmount}원, 주간=${weeklyAmount}원, 일=${dailyAmount}`);
return {
daily: {

View File

@@ -65,25 +65,27 @@ export const useBudgetState = () => {
if (newCategoryBudgets) {
console.log('카테고리 예산도 함께 업데이트:', newCategoryBudgets);
// 카테고리 예산의 합계 검증
// 카테고리 예산의 합계 검증 - 가져온 totalBudget과 카테고리 총합이 같아야 함
const categoryTotal = Object.values(newCategoryBudgets).reduce((sum, val) => sum + val, 0);
console.log(`카테고리 예산 합계: ${categoryTotal}, 입력 금액: ${amount}`);
if (Math.abs(categoryTotal - amount) > 10) { // 반올림 오차 허용
console.warn('카테고리 예산 합계와 전체 예산이 일치하지 않음. 전체 예산을 기준으로 조정합니다.');
// 금액이 카테고리 합계와 다르면 로그 기록 (허용 오차 ±10)
if (Math.abs(categoryTotal - amount) > 10) {
console.warn('카테고리 예산 합계와 총 예산이 일치하지 않음 - 카테고리 합계를 사용함');
// 카테고리 합계를 기준으로 예산 설정
amount = categoryTotal;
}
// 카테고리 예산 상태 업데이트
// 카테고리 예산 저장
updateCategoryBudgets(newCategoryBudgets);
// 로컬 스토리지에 직접 저장
saveCategoryBudgetsToStorage(newCategoryBudgets);
console.log('카테고리 예산 저장 완료');
}
// 예산 목표 업데이트 (카테고리 예산이 없는 경우에도 실행)
handleBudgetGoalUpdate(type, amount);
console.log('예산 업데이트 완료');
// 항상 월간 타입으로 예산 업데이트 (BudgetTabContent에서는 항상 월간 예산을 전달)
handleBudgetGoalUpdate('monthly', amount);
console.log('예산 데이터 업데이트 완료');
} catch (error) {
console.error('예산 업데이트 오류:', error);
toast({