104 lines
3.6 KiB
PHP
104 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Commande;
|
|
use App\Models\Service;
|
|
use App\Models\Contrat;
|
|
use App\Models\Domaine;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function index(Request $request): Response
|
|
{
|
|
$user = $request->user();
|
|
$stats = [
|
|
'total' => Commande::count(),
|
|
'en_cours' => Commande::enCours()->count(),
|
|
'en_retard' => Commande::enRetard()->count(),
|
|
'brouillons' => Commande::parStatut('brouillon')->count(),
|
|
'en_attente_validation' => Commande::parStatut('en_attente_validation')->count(),
|
|
'validees' => Commande::parStatut('validee')->count(),
|
|
'commandees' => Commande::parStatut('commandee')->count(),
|
|
'partiellement_recues' => Commande::parStatut('partiellement_recue')->count(),
|
|
'recues_complete' => Commande::parStatut('recue_complete')->count(),
|
|
];
|
|
|
|
$commandesRecentes = Commande::with(['service', 'fournisseur', 'demandeur'])
|
|
->latest()
|
|
->limit(8)
|
|
->get();
|
|
|
|
$commandesEnRetard = Commande::enRetard()
|
|
->with(['service', 'fournisseur', 'demandeur'])
|
|
->orderBy('date_souhaitee')
|
|
->limit(10)
|
|
->get();
|
|
|
|
$commandesUrgentes = Commande::urgentes()
|
|
->enCours()
|
|
->with(['service', 'fournisseur', 'demandeur'])
|
|
->latest()
|
|
->limit(5)
|
|
->get();
|
|
|
|
$statsParStatut = Commande::select('statut', DB::raw('count(*) as total'))
|
|
->groupBy('statut')
|
|
->get()
|
|
->keyBy('statut');
|
|
|
|
$statsParService = Service::withCount([
|
|
'commandes',
|
|
'commandes as commandes_en_cours_count' => fn ($q) => $q->enCours(),
|
|
])->get();
|
|
|
|
$montantParMois = Commande::select(
|
|
DB::raw('YEAR(date_demande) as annee'),
|
|
DB::raw('MONTH(date_demande) as mois'),
|
|
DB::raw('SUM(montant_ttc) as total_ttc'),
|
|
DB::raw('COUNT(*) as nb_commandes')
|
|
)
|
|
->whereYear('date_demande', now()->year)
|
|
->whereNotIn('statut', ['annulee'])
|
|
->groupBy('annee', 'mois')
|
|
->orderBy('mois')
|
|
->get();
|
|
|
|
// Stats Contrats
|
|
$contratsQuery = Contrat::query();
|
|
if (!$user->hasRole('admin')) {
|
|
$contratsQuery->where('service_id', $user->service_id);
|
|
}
|
|
$tousContrats = $contratsQuery->get();
|
|
$statsContrats = [
|
|
'total' => $tousContrats->count(),
|
|
'proches' => $tousContrats->filter(fn($c) => $c->est_proche_echeance && !$c->est_en_retard)->count(),
|
|
'en_retard' => $tousContrats->filter(fn($c) => $c->est_en_retard)->count(),
|
|
];
|
|
|
|
// Stats Domaines (Visibles par tous)
|
|
$tousDomaines = Domaine::all();
|
|
$statsDomaines = [
|
|
'total' => $tousDomaines->count(),
|
|
'proches' => $tousDomaines->filter(fn($d) => $d->est_proche_echeance && !$d->est_en_retard)->count(),
|
|
'en_retard' => $tousDomaines->filter(fn($d) => $d->est_en_retard)->count(),
|
|
];
|
|
|
|
return Inertia::render('Dashboard/Index', compact(
|
|
'stats',
|
|
'commandesRecentes',
|
|
'commandesEnRetard',
|
|
'commandesUrgentes',
|
|
'statsParStatut',
|
|
'statsParService',
|
|
'montantParMois',
|
|
'statsContrats',
|
|
'statsDomaines',
|
|
));
|
|
}
|
|
}
|