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

View File

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

View File

@ -58,7 +58,7 @@ 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('transaction_type', 'debit') ->where('type', 'debit')
->whereYear('transaction_date', $this->year); ->whereYear('transaction_date', $this->year);
if ($this->period_type === 'monthly' && $this->month) { if ($this->period_type === 'monthly' && $this->month) {