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; } else if (type === 'weekly') { // 주간 예산이 입력된 경우 - 이를 월간으로 환산 (4.345주/월 기준) monthlyAmount = Math.round(amount * 4.345); } else if (type === 'daily') { // 일일 예산이 입력된 경우 - 이를 월간으로 환산 (30일/월 기준) monthlyAmount = Math.round(amount * 30); } // 월간 예산을 기준으로 주간/일일 예산 계산 weeklyAmount = Math.round(monthlyAmount / 4.345); // 한 달 평균 4.345주 dailyAmount = Math.round(monthlyAmount / 30); // 한 달 평균 30일 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; };