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
$query = Transaction::where('user_id', Auth::id())
->where('transaction_type', 'debit')
->whereYear('transaction_date', $budget->year)
->whereMonth('transaction_date', $budget->month);
->whereYear('effective_date', $budget->year)
->whereMonth('effective_date', $budget->month);
if ($budget->category_id) {
$categoryIds = [$budget->category_id];
@ -108,7 +108,7 @@ public function show($id)
}
$transactions = $query->with('category')
->orderBy('transaction_date', 'desc')
->orderBy('effective_date', 'desc')
->limit(50)
->get();

View File

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

View File

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

View File

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