fix: Change transaction_type to type in controllers and models

- FinancialHealthController: Fix column name in queries
- ReportController: Fix column name in queries
- Budget model: Fix getSpentAmountAttribute query
This commit is contained in:
marcoitaloesp-ai 2025-12-14 16:36:31 +00:00 committed by GitHub
parent 854e90e23c
commit 604302ada4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 29 deletions

View File

@ -87,8 +87,8 @@ private function calculateSavingsCapacity($userId)
$data = Transaction::where('user_id', $userId)
->where('transaction_date', '>=', now()->subMonths(3)->startOfMonth())
->selectRaw("
SUM(CASE WHEN transaction_type = 'credit' THEN amount ELSE 0 END) as income,
SUM(CASE WHEN transaction_type = 'debit' THEN ABS(amount) ELSE 0 END) as expense
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
")
->first();
@ -136,7 +136,7 @@ private function calculateDebtControl($userId)
// Ingresos mensuales promedio
$monthlyIncome = Transaction::where('user_id', $userId)
->where('transaction_type', 'credit')
->where('type', 'credit')
->where('transaction_date', '>=', now()->subMonths(3)->startOfMonth())
->sum('amount') / 3;
@ -288,13 +288,13 @@ private function calculateEmergencyFund($userId)
// Gastos mensuales promedio
$monthlyExpenses = Transaction::where('user_id', $userId)
->where('transaction_type', 'debit')
->where('type', 'debit')
->where('transaction_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('transaction_type', 'debit')
->where('type', 'debit')
->where('transaction_date', '>=', now()->subMonths(3)->startOfMonth())
->sum('amount')) / 3;

View File

@ -29,8 +29,8 @@ public function summary(Request $request)
$yearData = Transaction::where('user_id', $userId)
->whereYear('transaction_date', $year)
->selectRaw("
SUM(CASE WHEN transaction_type = 'credit' THEN amount ELSE 0 END) as income,
SUM(CASE WHEN transaction_type = 'debit' THEN ABS(amount) ELSE 0 END) as expense
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
")
->first();
@ -38,8 +38,8 @@ public function summary(Request $request)
$lastYearData = Transaction::where('user_id', $userId)
->whereYear('transaction_date', $year - 1)
->selectRaw("
SUM(CASE WHEN transaction_type = 'credit' THEN amount ELSE 0 END) as income,
SUM(CASE WHEN transaction_type = 'debit' THEN ABS(amount) ELSE 0 END) as expense
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
")
->first();
@ -78,7 +78,7 @@ public function byCategory(Request $request)
$data = Transaction::where('user_id', $userId)
->whereBetween('transaction_date', [$startDate, $endDate])
->where('transaction_type', $type)
->where('type', $type)
->whereNotNull('category_id')
->join('categories', 'transactions.category_id', '=', 'categories.id')
->selectRaw("
@ -134,8 +134,8 @@ public function monthlyEvolution(Request $request)
->where('transaction_date', '>=', now()->subMonths($months)->startOfMonth())
->selectRaw("
DATE_FORMAT(transaction_date, '%Y-%m') as month,
SUM(CASE WHEN transaction_type = 'credit' THEN amount ELSE 0 END) as income,
SUM(CASE WHEN transaction_type = 'debit' THEN ABS(amount) ELSE 0 END) as expense
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
")
->groupBy('month')
->orderBy('month')
@ -176,7 +176,7 @@ public function byDayOfWeek(Request $request)
$months = $request->get('months', 6);
$data = Transaction::where('user_id', $userId)
->where('transaction_type', 'debit')
->where('type', 'debit')
->where('transaction_date', '>=', now()->subMonths($months))
->selectRaw("
DAYOFWEEK(transaction_date) as day_num,
@ -214,7 +214,7 @@ public function topExpenses(Request $request)
$limit = $request->get('limit', 20);
$transactions = Transaction::where('user_id', $userId)
->where('transaction_type', 'debit')
->where('type', 'debit')
->whereBetween('transaction_date', [$startDate, $endDate])
->with(['category', 'account'])
->orderByRaw('ABS(amount) DESC')
@ -259,8 +259,8 @@ public function comparePeriods(Request $request)
return Transaction::where('user_id', $userId)
->whereBetween('transaction_date', [$start, $end])
->selectRaw("
SUM(CASE WHEN transaction_type = 'credit' THEN amount ELSE 0 END) as income,
SUM(CASE WHEN transaction_type = 'debit' THEN ABS(amount) ELSE 0 END) as expense,
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,
COUNT(*) as transactions
")
->first();
@ -315,15 +315,15 @@ public function accountsReport(Request $request)
$recentActivity = Transaction::where('account_id', $account->id)
->orderBy('transaction_date', 'desc')
->limit(5)
->get(['id', 'description', 'amount', 'transaction_type', 'transaction_date']);
->get(['id', 'description', 'amount', 'type', 'transaction_date']);
// Movimientos del mes
$monthStats = Transaction::where('account_id', $account->id)
->whereMonth('transaction_date', now()->month)
->whereYear('transaction_date', now()->year)
->selectRaw("
SUM(CASE WHEN transaction_type = 'credit' THEN amount ELSE 0 END) as income,
SUM(CASE WHEN transaction_type = 'debit' THEN ABS(amount) ELSE 0 END) as expense
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
")
->first();
@ -362,10 +362,10 @@ public function projection(Request $request)
->where('transaction_date', '>=', now()->subMonths($months)->startOfMonth())
->where('transaction_date', '<', now()->startOfMonth())
->selectRaw("
AVG(CASE WHEN transaction_type = 'credit' THEN amount ELSE NULL END) as avg_income,
AVG(CASE WHEN transaction_type = 'debit' THEN ABS(amount) ELSE NULL END) as avg_expense,
SUM(CASE WHEN transaction_type = 'credit' THEN amount ELSE 0 END) / ? as monthly_income,
SUM(CASE WHEN transaction_type = 'debit' THEN ABS(amount) ELSE 0 END) / ? as monthly_expense
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,
SUM(CASE WHEN type = 'credit' THEN amount ELSE 0 END) / ? as monthly_income,
SUM(CASE WHEN type = 'debit' THEN ABS(amount) ELSE 0 END) / ? as monthly_expense
", [$months, $months])
->first();
@ -374,8 +374,8 @@ public function projection(Request $request)
->whereMonth('transaction_date', now()->month)
->whereYear('transaction_date', now()->year)
->selectRaw("
SUM(CASE WHEN transaction_type = 'credit' THEN amount ELSE 0 END) as income,
SUM(CASE WHEN transaction_type = 'debit' THEN ABS(amount) ELSE 0 END) as expense
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
")
->first();
@ -427,8 +427,8 @@ public function recurringReport(Request $request)
->with(['category', 'account'])
->get();
$monthlyIncome = $templates->where('transaction_type', 'credit')->sum('amount');
$monthlyExpense = $templates->where('transaction_type', 'debit')->sum(fn($t) => abs($t->amount));
$monthlyIncome = $templates->where('type', 'credit')->sum('amount');
$monthlyExpense = $templates->where('type', 'debit')->sum(fn($t) => abs($t->amount));
return response()->json([
'templates' => $templates->map(function($t) {
@ -436,7 +436,7 @@ public function recurringReport(Request $request)
'id' => $t->id,
'description' => $t->description,
'amount' => abs($t->amount),
'type' => $t->transaction_type,
'type' => $t->type,
'frequency' => $t->frequency,
'category' => $t->category ? $t->category->name : null,
'next_date' => $t->next_occurrence_date,

View File

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