145 lines
4.1 KiB
TypeScript
145 lines
4.1 KiB
TypeScript
|
|
import { BudgetData, BudgetPeriod, CategoryBudget, Transaction } from './types';
|
|
import { EXPENSE_CATEGORIES } from '@/constants/categoryIcons';
|
|
|
|
// 기본 데이터 상수 (기본값을 0으로 변경)
|
|
export const DEFAULT_CATEGORY_BUDGETS: Record<string, number> = {
|
|
식비: 0,
|
|
생활비: 0,
|
|
교통비: 0
|
|
};
|
|
|
|
export const DEFAULT_MONTHLY_BUDGET = 0;
|
|
|
|
// 카테고리별 지출 계산
|
|
export const calculateCategorySpending = (
|
|
transactions: Transaction[],
|
|
categoryBudgets: Record<string, number>
|
|
): CategoryBudget[] => {
|
|
const expenseTransactions = transactions.filter(t => t.type === 'expense');
|
|
const categorySpending: Record<string, number> = {};
|
|
|
|
// 모든 카테고리에 대해 초기값 0 설정
|
|
Object.keys(categoryBudgets).forEach(category => {
|
|
categorySpending[category] = 0;
|
|
});
|
|
|
|
expenseTransactions.forEach(t => {
|
|
if (t.category in categorySpending) {
|
|
categorySpending[t.category] += t.amount;
|
|
}
|
|
});
|
|
|
|
return Object.keys(categoryBudgets).map(category => ({
|
|
title: category,
|
|
current: categorySpending[category] || 0,
|
|
total: categoryBudgets[category]
|
|
}));
|
|
};
|
|
|
|
// 예산 데이터 업데이트 계산
|
|
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 {
|
|
const remainingAmount = Math.max(0, amount - prevBudgetData[type].spentAmount);
|
|
return {
|
|
...prevBudgetData,
|
|
[type]: {
|
|
...prevBudgetData[type],
|
|
targetAmount: amount,
|
|
remainingAmount: remainingAmount
|
|
}
|
|
};
|
|
}
|
|
};
|
|
|
|
// 지출액 계산 (일일, 주간, 월간)
|
|
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('어제')) 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
|
|
}
|
|
};
|
|
};
|