Refactor budgetUtils.ts
Refactor budgetUtils.ts to improve code organization and maintainability by splitting the logic into multiple files. The functionality remains the same.
This commit is contained in:
78
src/contexts/budget/utils/budgetCalculation.ts
Normal file
78
src/contexts/budget/utils/budgetCalculation.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
|
||||
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: number, weeklyAmount: number, dailyAmount: number;
|
||||
|
||||
if (type === 'monthly') {
|
||||
// 월간 예산이 직접 입력된 경우
|
||||
monthlyAmount = amount;
|
||||
// 월 30일 기준 (실제 사용 시 값 확인)
|
||||
dailyAmount = Math.round(monthlyAmount / 30);
|
||||
// 월 4.3주 기준 (실제 사용 시 값 확인)
|
||||
weeklyAmount = Math.round(monthlyAmount / 4.3);
|
||||
} else if (type === 'weekly') {
|
||||
// 주간 예산이 직접 입력된 경우
|
||||
weeklyAmount = amount;
|
||||
monthlyAmount = Math.round(weeklyAmount * 4.3);
|
||||
dailyAmount = Math.round(weeklyAmount / 7);
|
||||
} else { // 'daily'
|
||||
// 일일 예산이 직접 입력된 경우
|
||||
dailyAmount = amount;
|
||||
weeklyAmount = Math.round(dailyAmount * 7);
|
||||
monthlyAmount = Math.round(dailyAmount * 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;
|
||||
};
|
||||
Reference in New Issue
Block a user