import { BudgetData, BudgetPeriod, CategoryBudget, Transaction } from './types'; import { EXPENSE_CATEGORIES } from '@/constants/categoryIcons'; // 기본 데이터 상수 (기본값을 0으로 설정) export const DEFAULT_CATEGORY_BUDGETS: Record = { 식비: 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 => { // 3개 카테고리만 유지 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 { // 지원되지 않는 카테고리는 '생활비'로 집계 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 => { if (type === 'monthly') { const dailyAmount = Math.round(amount / 30); const weeklyAmount = Math.round(amount / 4.3); 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: amount, spentAmount: prevBudgetData.monthly.spentAmount, remainingAmount: Math.max(0, amount - prevBudgetData.monthly.spentAmount) } }; } else if (type === 'weekly') { // 주간 예산이 설정되면 월간 예산도 자동 계산 const monthlyAmount = Math.round(amount * 4.3); const dailyAmount = Math.round(amount / 7); return { daily: { targetAmount: dailyAmount, spentAmount: prevBudgetData.daily.spentAmount, remainingAmount: Math.max(0, dailyAmount - prevBudgetData.daily.spentAmount) }, weekly: { targetAmount: amount, spentAmount: prevBudgetData.weekly.spentAmount, remainingAmount: Math.max(0, amount - prevBudgetData.weekly.spentAmount) }, monthly: { targetAmount: monthlyAmount, spentAmount: prevBudgetData.monthly.spentAmount, remainingAmount: Math.max(0, monthlyAmount - prevBudgetData.monthly.spentAmount) } }; } else { // 일일 예산이 설정되면 주간/월간 예산도 자동 계산 const weeklyAmount = Math.round(amount * 7); const monthlyAmount = Math.round(amount * 30); return { daily: { targetAmount: amount, spentAmount: prevBudgetData.daily.spentAmount, remainingAmount: Math.max(0, amount - 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 } }; };