Creada nueva pagina "Reportes"
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Sale;
|
||||
use App\Models\Expense;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class ReportController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
// 1. Totales Generales
|
||||
$totalIncome = Sale::sum('total');
|
||||
$totalOutcome = Expense::where('status', 'Pagado')->sum('total_amount');
|
||||
$balance = $totalIncome - $totalOutcome;
|
||||
|
||||
// 2. Gastos por Categoría (Para Pie Chart)
|
||||
$expensesByCategory = Expense::where('status', 'Pagado')
|
||||
->select('expense_category', DB::raw('SUM(total_amount) as total'))
|
||||
->groupBy('expense_category')
|
||||
->get();
|
||||
|
||||
// 3. Ingresos y Egresos por Mes (Últimos 6 meses)
|
||||
$months = [];
|
||||
$monthlyIncomeData = [];
|
||||
$monthlyOutcomeData = [];
|
||||
|
||||
for ($i = 5; $i >= 0; $i--) {
|
||||
$date = now()->subMonths($i);
|
||||
$monthName = $date->translatedFormat('M Y'); // ej: may 2026
|
||||
|
||||
$months[] = ucfirst($monthName);
|
||||
|
||||
// Ingresos del mes
|
||||
$inc = Sale::whereYear('created_at', $date->year)
|
||||
->whereMonth('created_at', $date->month)
|
||||
->sum('total');
|
||||
$monthlyIncomeData[] = $inc;
|
||||
|
||||
// Egresos del mes
|
||||
$out = Expense::where('status', 'Pagado')
|
||||
->whereYear('expense_date', $date->year)
|
||||
->whereMonth('expense_date', $date->month)
|
||||
->sum('total_amount');
|
||||
$monthlyOutcomeData[] = $out;
|
||||
}
|
||||
|
||||
return view('reports.index', compact(
|
||||
'totalIncome',
|
||||
'totalOutcome',
|
||||
'balance',
|
||||
'expensesByCategory',
|
||||
'months',
|
||||
'monthlyIncomeData',
|
||||
'monthlyOutcomeData'
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user