83 lines
2.3 KiB
PHP
83 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Cotizacion;
|
|
use App\Models\Client;
|
|
use App\Models\Producto;
|
|
use Illuminate\Http\Request;
|
|
|
|
class HomeController extends Controller
|
|
{
|
|
/**
|
|
* Create a new controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
}
|
|
|
|
/**
|
|
* Show the application dashboard.
|
|
*/
|
|
public function index()
|
|
{
|
|
$user = auth()->user();
|
|
|
|
// ================== FILTRO MULTIEMPRESA ==================
|
|
$cotizacionesQuery = Cotizacion::query();
|
|
$clientesQuery = Client::query();
|
|
$productosQuery = Producto::query();
|
|
|
|
if (!$user->hasRole('super-admin')) {
|
|
$cotizacionesQuery->where('company_id', $user->company_id);
|
|
$clientesQuery->where('company_id', $user->company_id);
|
|
$productosQuery->where('company_id', $user->company_id);
|
|
}
|
|
|
|
// ================== MÉTRICAS ==================
|
|
$totalCotizaciones = $cotizacionesQuery->count();
|
|
$totalClientes = $clientesQuery->count();
|
|
$totalProductos = $productosQuery->count();
|
|
|
|
$ingresosTotales = (clone $cotizacionesQuery)
|
|
->where('estado', 'aprobada')
|
|
->sum('total');
|
|
|
|
// ================== LISTADOS ==================
|
|
$cotizaciones = (clone $cotizacionesQuery)
|
|
->with('cliente')
|
|
->latest()
|
|
->limit(5)
|
|
->get();
|
|
|
|
$clientesRecientes = (clone $clientesQuery)
|
|
->latest()
|
|
->limit(5)
|
|
->get();
|
|
|
|
// ================== RESUMEN DEL MES ==================
|
|
$cotizacionesMes = (clone $cotizacionesQuery)
|
|
->whereMonth('created_at', now()->month)
|
|
->whereYear('created_at', now()->year)
|
|
->count();
|
|
|
|
$clientesMes = (clone $clientesQuery)
|
|
->whereMonth('created_at', now()->month)
|
|
->whereYear('created_at', now()->year)
|
|
->count();
|
|
|
|
// ================== VISTA CORRECTA ==================
|
|
return view('home', compact(
|
|
'totalCotizaciones',
|
|
'totalClientes',
|
|
'totalProductos',
|
|
'ingresosTotales',
|
|
'cotizaciones',
|
|
'clientesRecientes',
|
|
'cotizacionesMes',
|
|
'clientesMes'
|
|
));
|
|
}
|
|
}
|