fix: Change transaction_date to effective_date in controllers and models

This commit is contained in:
marcoitaloesp-ai 2025-12-14 16:38:32 +00:00 committed by GitHub
parent 604302ada4
commit 39de07bf96
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 27 additions and 27 deletions

View File

@ -97,8 +97,8 @@ public function show($id)
// Obtener transacciones del período // Obtener transacciones del período
$query = Transaction::where('user_id', Auth::id()) $query = Transaction::where('user_id', Auth::id())
->where('transaction_type', 'debit') ->where('transaction_type', 'debit')
->whereYear('transaction_date', $budget->year) ->whereYear('effective_date', $budget->year)
->whereMonth('transaction_date', $budget->month); ->whereMonth('effective_date', $budget->month);
if ($budget->category_id) { if ($budget->category_id) {
$categoryIds = [$budget->category_id]; $categoryIds = [$budget->category_id];
@ -108,7 +108,7 @@ public function show($id)
} }
$transactions = $query->with('category') $transactions = $query->with('category')
->orderBy('transaction_date', 'desc') ->orderBy('effective_date', 'desc')
->limit(50) ->limit(50)
->get(); ->get();

View File

@ -85,7 +85,7 @@ private function calculateSavingsCapacity($userId)
{ {
// Últimos 3 meses // Últimos 3 meses
$data = Transaction::where('user_id', $userId) $data = Transaction::where('user_id', $userId)
->where('transaction_date', '>=', now()->subMonths(3)->startOfMonth()) ->where('effective_date', '>=', now()->subMonths(3)->startOfMonth())
->selectRaw(" ->selectRaw("
SUM(CASE WHEN type = 'credit' THEN amount ELSE 0 END) as income, SUM(CASE WHEN type = 'credit' THEN amount ELSE 0 END) as income,
SUM(CASE WHEN type = 'debit' THEN ABS(amount) ELSE 0 END) as expense SUM(CASE WHEN type = 'debit' THEN ABS(amount) ELSE 0 END) as expense
@ -137,7 +137,7 @@ private function calculateDebtControl($userId)
// Ingresos mensuales promedio // Ingresos mensuales promedio
$monthlyIncome = Transaction::where('user_id', $userId) $monthlyIncome = Transaction::where('user_id', $userId)
->where('type', 'credit') ->where('type', 'credit')
->where('transaction_date', '>=', now()->subMonths(3)->startOfMonth()) ->where('effective_date', '>=', now()->subMonths(3)->startOfMonth())
->sum('amount') / 3; ->sum('amount') / 3;
// Ratio deuda/ingresos // Ratio deuda/ingresos
@ -289,13 +289,13 @@ private function calculateEmergencyFund($userId)
// Gastos mensuales promedio // Gastos mensuales promedio
$monthlyExpenses = Transaction::where('user_id', $userId) $monthlyExpenses = Transaction::where('user_id', $userId)
->where('type', 'debit') ->where('type', 'debit')
->where('transaction_date', '>=', now()->subMonths(3)->startOfMonth()) ->where('effective_date', '>=', now()->subMonths(3)->startOfMonth())
->sum(fn($t) => abs($t->amount)) / 3; ->sum(fn($t) => abs($t->amount)) / 3;
// Usar consulta directa para mejor rendimiento // Usar consulta directa para mejor rendimiento
$monthlyExpenses = abs(Transaction::where('user_id', $userId) $monthlyExpenses = abs(Transaction::where('user_id', $userId)
->where('type', 'debit') ->where('type', 'debit')
->where('transaction_date', '>=', now()->subMonths(3)->startOfMonth()) ->where('effective_date', '>=', now()->subMonths(3)->startOfMonth())
->sum('amount')) / 3; ->sum('amount')) / 3;
// Meses cubiertos // Meses cubiertos

View File

@ -27,7 +27,7 @@ public function summary(Request $request)
// Ingresos y gastos del año // Ingresos y gastos del año
$yearData = Transaction::where('user_id', $userId) $yearData = Transaction::where('user_id', $userId)
->whereYear('transaction_date', $year) ->whereYear('effective_date', $year)
->selectRaw(" ->selectRaw("
SUM(CASE WHEN type = 'credit' THEN amount ELSE 0 END) as income, SUM(CASE WHEN type = 'credit' THEN amount ELSE 0 END) as income,
SUM(CASE WHEN type = 'debit' THEN ABS(amount) ELSE 0 END) as expense SUM(CASE WHEN type = 'debit' THEN ABS(amount) ELSE 0 END) as expense
@ -36,7 +36,7 @@ public function summary(Request $request)
// Comparar con año anterior // Comparar con año anterior
$lastYearData = Transaction::where('user_id', $userId) $lastYearData = Transaction::where('user_id', $userId)
->whereYear('transaction_date', $year - 1) ->whereYear('effective_date', $year - 1)
->selectRaw(" ->selectRaw("
SUM(CASE WHEN type = 'credit' THEN amount ELSE 0 END) as income, SUM(CASE WHEN type = 'credit' THEN amount ELSE 0 END) as income,
SUM(CASE WHEN type = 'debit' THEN ABS(amount) ELSE 0 END) as expense SUM(CASE WHEN type = 'debit' THEN ABS(amount) ELSE 0 END) as expense
@ -77,7 +77,7 @@ public function byCategory(Request $request)
$type = $request->get('type', 'debit'); // debit o credit $type = $request->get('type', 'debit'); // debit o credit
$data = Transaction::where('user_id', $userId) $data = Transaction::where('user_id', $userId)
->whereBetween('transaction_date', [$startDate, $endDate]) ->whereBetween('effective_date', [$startDate, $endDate])
->where('type', $type) ->where('type', $type)
->whereNotNull('category_id') ->whereNotNull('category_id')
->join('categories', 'transactions.category_id', '=', 'categories.id') ->join('categories', 'transactions.category_id', '=', 'categories.id')
@ -131,9 +131,9 @@ public function monthlyEvolution(Request $request)
$months = $request->get('months', 12); $months = $request->get('months', 12);
$data = Transaction::where('user_id', $userId) $data = Transaction::where('user_id', $userId)
->where('transaction_date', '>=', now()->subMonths($months)->startOfMonth()) ->where('effective_date', '>=', now()->subMonths($months)->startOfMonth())
->selectRaw(" ->selectRaw("
DATE_FORMAT(transaction_date, '%Y-%m') as month, DATE_FORMAT(effective_date, '%Y-%m') as month,
SUM(CASE WHEN type = 'credit' THEN amount ELSE 0 END) as income, SUM(CASE WHEN type = 'credit' THEN amount ELSE 0 END) as income,
SUM(CASE WHEN type = 'debit' THEN ABS(amount) ELSE 0 END) as expense SUM(CASE WHEN type = 'debit' THEN ABS(amount) ELSE 0 END) as expense
") ")
@ -177,9 +177,9 @@ public function byDayOfWeek(Request $request)
$data = Transaction::where('user_id', $userId) $data = Transaction::where('user_id', $userId)
->where('type', 'debit') ->where('type', 'debit')
->where('transaction_date', '>=', now()->subMonths($months)) ->where('effective_date', '>=', now()->subMonths($months))
->selectRaw(" ->selectRaw("
DAYOFWEEK(transaction_date) as day_num, DAYOFWEEK(effective_date) as day_num,
COUNT(*) as count, COUNT(*) as count,
SUM(ABS(amount)) as total, SUM(ABS(amount)) as total,
AVG(ABS(amount)) as average AVG(ABS(amount)) as average
@ -215,7 +215,7 @@ public function topExpenses(Request $request)
$transactions = Transaction::where('user_id', $userId) $transactions = Transaction::where('user_id', $userId)
->where('type', 'debit') ->where('type', 'debit')
->whereBetween('transaction_date', [$startDate, $endDate]) ->whereBetween('effective_date', [$startDate, $endDate])
->with(['category', 'account']) ->with(['category', 'account'])
->orderByRaw('ABS(amount) DESC') ->orderByRaw('ABS(amount) DESC')
->limit($limit) ->limit($limit)
@ -227,7 +227,7 @@ public function topExpenses(Request $request)
'id' => $t->id, 'id' => $t->id,
'description' => $t->description, 'description' => $t->description,
'amount' => abs($t->amount), 'amount' => abs($t->amount),
'date' => $t->transaction_date->format('Y-m-d'), 'date' => $t->effective_date->format('Y-m-d'),
'category' => $t->category ? $t->category->name : null, 'category' => $t->category ? $t->category->name : null,
'account' => $t->account ? $t->account->name : null, 'account' => $t->account ? $t->account->name : null,
]; ];
@ -257,7 +257,7 @@ public function comparePeriods(Request $request)
$getPeriodData = function($start, $end) use ($userId) { $getPeriodData = function($start, $end) use ($userId) {
return Transaction::where('user_id', $userId) return Transaction::where('user_id', $userId)
->whereBetween('transaction_date', [$start, $end]) ->whereBetween('effective_date', [$start, $end])
->selectRaw(" ->selectRaw("
SUM(CASE WHEN type = 'credit' THEN amount ELSE 0 END) as income, SUM(CASE WHEN type = 'credit' THEN amount ELSE 0 END) as income,
SUM(CASE WHEN type = 'debit' THEN ABS(amount) ELSE 0 END) as expense, SUM(CASE WHEN type = 'debit' THEN ABS(amount) ELSE 0 END) as expense,
@ -313,14 +313,14 @@ public function accountsReport(Request $request)
$result = $accounts->map(function($account) { $result = $accounts->map(function($account) {
// Últimas transacciones // Últimas transacciones
$recentActivity = Transaction::where('account_id', $account->id) $recentActivity = Transaction::where('account_id', $account->id)
->orderBy('transaction_date', 'desc') ->orderBy('effective_date', 'desc')
->limit(5) ->limit(5)
->get(['id', 'description', 'amount', 'type', 'transaction_date']); ->get(['id', 'description', 'amount', 'type', 'effective_date']);
// Movimientos del mes // Movimientos del mes
$monthStats = Transaction::where('account_id', $account->id) $monthStats = Transaction::where('account_id', $account->id)
->whereMonth('transaction_date', now()->month) ->whereMonth('effective_date', now()->month)
->whereYear('transaction_date', now()->year) ->whereYear('effective_date', now()->year)
->selectRaw(" ->selectRaw("
SUM(CASE WHEN type = 'credit' THEN amount ELSE 0 END) as income, SUM(CASE WHEN type = 'credit' THEN amount ELSE 0 END) as income,
SUM(CASE WHEN type = 'debit' THEN ABS(amount) ELSE 0 END) as expense SUM(CASE WHEN type = 'debit' THEN ABS(amount) ELSE 0 END) as expense
@ -359,8 +359,8 @@ public function projection(Request $request)
// Obtener promedio de los últimos meses // Obtener promedio de los últimos meses
$historical = Transaction::where('user_id', $userId) $historical = Transaction::where('user_id', $userId)
->where('transaction_date', '>=', now()->subMonths($months)->startOfMonth()) ->where('effective_date', '>=', now()->subMonths($months)->startOfMonth())
->where('transaction_date', '<', now()->startOfMonth()) ->where('effective_date', '<', now()->startOfMonth())
->selectRaw(" ->selectRaw("
AVG(CASE WHEN type = 'credit' THEN amount ELSE NULL END) as avg_income, AVG(CASE WHEN type = 'credit' THEN amount ELSE NULL END) as avg_income,
AVG(CASE WHEN type = 'debit' THEN ABS(amount) ELSE NULL END) as avg_expense, AVG(CASE WHEN type = 'debit' THEN ABS(amount) ELSE NULL END) as avg_expense,
@ -371,8 +371,8 @@ public function projection(Request $request)
// Gastos del mes actual // Gastos del mes actual
$currentMonth = Transaction::where('user_id', $userId) $currentMonth = Transaction::where('user_id', $userId)
->whereMonth('transaction_date', now()->month) ->whereMonth('effective_date', now()->month)
->whereYear('transaction_date', now()->year) ->whereYear('effective_date', now()->year)
->selectRaw(" ->selectRaw("
SUM(CASE WHEN type = 'credit' THEN amount ELSE 0 END) as income, SUM(CASE WHEN type = 'credit' THEN amount ELSE 0 END) as income,
SUM(CASE WHEN type = 'debit' THEN ABS(amount) ELSE 0 END) as expense SUM(CASE WHEN type = 'debit' THEN ABS(amount) ELSE 0 END) as expense

View File

@ -59,10 +59,10 @@ public function getSpentAmountAttribute()
// Calcular el gasto real de las transacciones // Calcular el gasto real de las transacciones
$query = Transaction::where('user_id', $this->user_id) $query = Transaction::where('user_id', $this->user_id)
->where('type', 'debit') ->where('type', 'debit')
->whereYear('transaction_date', $this->year); ->whereYear('effective_date', $this->year);
if ($this->period_type === 'monthly' && $this->month) { if ($this->period_type === 'monthly' && $this->month) {
$query->whereMonth('transaction_date', $this->month); $query->whereMonth('effective_date', $this->month);
} }
if ($this->category_id) { if ($this->category_id) {