The budget update logic was modified to ensure that all budget updates are applied to the monthly budget, regardless of the selected tab. This resolves inconsistencies in budget calculations and data storage.
79 lines
2.6 KiB
TypeScript
79 lines
2.6 KiB
TypeScript
|
|
import { BudgetData, BudgetPeriod } from '../types';
|
|
import { getInitialBudgetData } from './constants';
|
|
|
|
// 예산 데이터 업데이트 계산
|
|
export const calculateUpdatedBudgetData = (
|
|
prevBudgetData: BudgetData,
|
|
type: BudgetPeriod,
|
|
amount: number
|
|
): BudgetData => {
|
|
console.log(`예산 업데이트 계산 시작: 타입=${type}, 금액=${amount}`);
|
|
|
|
// 값이 없거나 유효하지 않은 경우 로깅
|
|
if (!prevBudgetData) {
|
|
console.error('이전 예산 데이터가 없습니다. 기본값 사용.');
|
|
prevBudgetData = getInitialBudgetData();
|
|
}
|
|
|
|
// 항상 입력된 금액을 직접 해당 타입의 예산으로 설정
|
|
let monthlyAmount = 0;
|
|
let weeklyAmount = 0;
|
|
let dailyAmount = 0;
|
|
|
|
if (type === 'monthly') {
|
|
// 월간 예산이 직접 입력된 경우
|
|
monthlyAmount = amount;
|
|
weeklyAmount = Math.round(amount / 4.3);
|
|
dailyAmount = Math.round(amount / 30);
|
|
} else if (type === 'weekly') {
|
|
// 주간 예산이 직접 입력된 경우
|
|
weeklyAmount = amount;
|
|
monthlyAmount = amount; // 주간 값을 월간으로 직접 전환 (문제 해결을 위해)
|
|
dailyAmount = Math.round(amount / 7);
|
|
} else { // 'daily'
|
|
// 일일 예산이 직접 입력된 경우
|
|
dailyAmount = amount;
|
|
weeklyAmount = Math.round(amount * 7);
|
|
monthlyAmount = Math.round(amount * 30);
|
|
}
|
|
|
|
// 모든 금액이 최소한 0 이상이 되도록 보장
|
|
monthlyAmount = Math.max(0, monthlyAmount);
|
|
weeklyAmount = Math.max(0, weeklyAmount);
|
|
dailyAmount = Math.max(0, dailyAmount);
|
|
|
|
console.log(`최종 예산 계산 결과: 월간=${monthlyAmount}원, 주간=${weeklyAmount}원, 일일=${dailyAmount}원`);
|
|
|
|
// 로그에 이전 예산 데이터 출력
|
|
console.log("이전 예산 데이터:", JSON.stringify(prevBudgetData));
|
|
|
|
// 이전 지출 데이터 보존
|
|
const dailySpent = prevBudgetData.daily?.spentAmount || 0;
|
|
const weeklySpent = prevBudgetData.weekly?.spentAmount || 0;
|
|
const monthlySpent = prevBudgetData.monthly?.spentAmount || 0;
|
|
|
|
// 새 예산 데이터 생성 (spentAmount는 이전 값 유지)
|
|
const updatedBudgetData = {
|
|
daily: {
|
|
targetAmount: dailyAmount,
|
|
spentAmount: dailySpent,
|
|
remainingAmount: Math.max(0, dailyAmount - dailySpent)
|
|
},
|
|
weekly: {
|
|
targetAmount: weeklyAmount,
|
|
spentAmount: weeklySpent,
|
|
remainingAmount: Math.max(0, weeklyAmount - weeklySpent)
|
|
},
|
|
monthly: {
|
|
targetAmount: monthlyAmount,
|
|
spentAmount: monthlySpent,
|
|
remainingAmount: Math.max(0, monthlyAmount - monthlySpent)
|
|
}
|
|
};
|
|
|
|
console.log("새 예산 데이터:", JSON.stringify(updatedBudgetData));
|
|
|
|
return updatedBudgetData;
|
|
};
|