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:
@@ -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>
|
||||
|
||||
@@ -154,4 +155,4 @@ const BudgetTabContent: React.FC<BudgetTabContentProps> = ({
|
||||
</div>}
|
||||
</div>;
|
||||
};
|
||||
export default BudgetTabContent;
|
||||
export default BudgetTabContent;
|
||||
|
||||
@@ -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: {
|
||||
|
||||
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user