import { BudgetData, BudgetPeriod, CategoryBudget, Transaction } from './types'; import { EXPENSE_CATEGORIES } from '@/constants/categoryIcons'; // 기본 데이터 상수 (기본값을 0으로 설정) export const DEFAULT_CATEGORY_BUDGETS: Record = { 음식: 0, 쇼핑: 0, 교통: 0, 기타: 0 }; export const DEFAULT_MONTHLY_BUDGET = 0; // 카테고리별 지출 계산 export const calculateCategorySpending = ( transactions: Transaction[], categoryBudgets: Record ): CategoryBudget[] => { const expenseTransactions = transactions.filter(t => t.type === 'expense'); const categorySpending: Record = {}; // 모든 카테고리에 대해 초기값 0 설정 Object.keys(categoryBudgets).forEach(category => { // 정의된 카테고리만 유지 if (EXPENSE_CATEGORIES.includes(category)) { categorySpending[category] = 0; } }); // 지원되는 카테고리가 없을 경우 기본값 설정 if (Object.keys(categorySpending).length === 0) { EXPENSE_CATEGORIES.forEach(category => { categorySpending[category] = 0; }); } expenseTransactions.forEach(t => { if (t.category in categorySpending) { categorySpending[t.category] += t.amount; } else if (EXPENSE_CATEGORIES.includes(t.category)) { // 지원되는 카테고리이지만 초기화되지 않은 경우 categorySpending[t.category] = t.amount; } else if (t.category === '교통비') { // 예전 카테고리명 '교통비'를 '교통'으로 매핑 categorySpending['교통'] = (categorySpending['교통'] || 0) + t.amount; } else { // 지원되지 않는 카테고리는 '기타'로 집계 categorySpending['기타'] = (categorySpending['기타'] || 0) + t.amount; } }); return EXPENSE_CATEGORIES.map(category => ({ title: category, current: categorySpending[category] || 0, total: categoryBudgets[category] || 0 })); }; // 예산 데이터 업데이트 계산 - 완전히 수정된 함수 export const calculateUpdatedBudgetData = ( prevBudgetData: BudgetData, type: BudgetPeriod, amount: number ): BudgetData => { console.log(`예산 업데이트 계산: 타입=${type}, 금액=${amount}`); // 모든 타입에 대해 월간 예산을 기준으로 계산 let monthlyAmount = amount; // 선택된 탭이 월간이 아닌 경우, 올바른 월간 값으로 변환 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}원`); return { daily: { targetAmount: dailyAmount, spentAmount: prevBudgetData.daily.spentAmount, remainingAmount: Math.max(0, dailyAmount - prevBudgetData.daily.spentAmount) }, weekly: { targetAmount: weeklyAmount, spentAmount: prevBudgetData.weekly.spentAmount, remainingAmount: Math.max(0, weeklyAmount - prevBudgetData.weekly.spentAmount) }, monthly: { targetAmount: monthlyAmount, spentAmount: prevBudgetData.monthly.spentAmount, remainingAmount: Math.max(0, monthlyAmount - prevBudgetData.monthly.spentAmount) } }; }; // 지출액 계산 (일일, 주간, 월간) export const calculateSpentAmounts = ( transactions: Transaction[], prevBudgetData: BudgetData ): BudgetData => { // 지출 거래 필터링 const expenseTransactions = transactions.filter(t => t.type === 'expense'); // 오늘 지출 계산 const todayExpenses = expenseTransactions.filter(t => { if (t.date.includes('오늘')) return true; return false; }); const dailySpent = todayExpenses.reduce((sum, t) => sum + t.amount, 0); // 이번 주 지출 계산 (단순화된 버전) const weeklyExpenses = expenseTransactions.filter(t => { if (t.date.includes('오늘') || t.date.includes('어제') || t.date.includes('이번주')) return true; return true; }); const weeklySpent = weeklyExpenses.reduce((sum, t) => sum + t.amount, 0); // 이번 달 총 지출 계산 const monthlySpent = expenseTransactions.reduce((sum, t) => sum + t.amount, 0); // 예산 데이터 업데이트 return { daily: { ...prevBudgetData.daily, spentAmount: dailySpent, remainingAmount: Math.max(0, prevBudgetData.daily.targetAmount - dailySpent) }, weekly: { ...prevBudgetData.weekly, spentAmount: weeklySpent, remainingAmount: Math.max(0, prevBudgetData.weekly.targetAmount - weeklySpent) }, monthly: { ...prevBudgetData.monthly, spentAmount: monthlySpent, remainingAmount: Math.max(0, prevBudgetData.monthly.targetAmount - monthlySpent) } }; }; // 초기 예산 데이터 생성 export const getInitialBudgetData = (): BudgetData => { return { daily: { targetAmount: 0, spentAmount: 0, remainingAmount: 0 }, weekly: { targetAmount: 0, spentAmount: 0, remainingAmount: 0 }, monthly: { targetAmount: 0, spentAmount: 0, remainingAmount: 0 } }; };