diff --git a/backend/app/Http/Controllers/Api/FinancialHealthController.php b/backend/app/Http/Controllers/Api/FinancialHealthController.php index f148ca7..8595ced 100644 --- a/backend/app/Http/Controllers/Api/FinancialHealthController.php +++ b/backend/app/Http/Controllers/Api/FinancialHealthController.php @@ -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; diff --git a/backend/app/Http/Controllers/Api/ReportController.php b/backend/app/Http/Controllers/Api/ReportController.php index c399561..37139bc 100644 --- a/backend/app/Http/Controllers/Api/ReportController.php +++ b/backend/app/Http/Controllers/Api/ReportController.php @@ -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, diff --git a/backend/app/Models/Budget.php b/backend/app/Models/Budget.php index 2e4a9ab..a5bffa5 100644 --- a/backend/app/Models/Budget.php +++ b/backend/app/Models/Budget.php @@ -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) {