<?php
namespace App\Controller\Admin;
use App\Entity\User;
use App\Repository\UserRepository;
use App\Repository\PointageRepository;
use App\Repository\EntrepriseRepository;
use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Entreprise;
class DashboardController extends AbstractDashboardController
{
private UserRepository $userRepository;
private PointageRepository $repo;
public function __construct(UserRepository $userRepository, PointageRepository $repo,EntrepriseRepository $entrepriseRepository)
{
$this->userRepository = $userRepository;
$this->repo = $repo;
$this->entrepriseRepository = $entrepriseRepository;
}
#[Route('/admin', name: 'admin')]
public function index(): Response
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
/** @var User $user */
$user = $this->getUser();
if (!$user) {
return $this->redirectToRoute('app_login');
}
// Vérifie abonnement
if (!$user->isOkAbonnement() && in_array('ROLE_ADMIN', $user->getRoles(), true)) {
$this->addFlash('error', 'Vous n\'êtes pas autorisé à accéder à cette page, veuillez vous abonner d\'abord.');
return $this->redirectToRoute('souscription_new');
}
// Nombre total d’employés de l’entreprise créée par ce user
//$entreprise = $user->getEntreprise();
$em = $this->getDoctrine()->getManager();
$entrepriseRepository = $em->getRepository(\App\Entity\Entreprise::class);
// $entreprise = $this->entrepriseRepository->find($user->getid());
$entreprises = $this->entrepriseRepository->findBy(['createdBy' => $user]);
$totalEmployes = 0;
foreach ($entreprises as $entreprise) {
$totalEmployes += count($entreprise->getEmployes());
}
//$totalEmployes = $entreprise ? count($entreprise->getEmployes()) : 0;
// Présence et Absence du jour
$today = new \DateTimeImmutable('today');
$presenceCount = $this->repo->countPresencesByCreator($user, $today);
$absenceCount = $this->repo->countAbsencesByCreator($user, $today, $totalEmployes);
$mesabsents = $this->repo->getEmployesAbsents($user->getId(), $today);
// Présences par jour pour le mois courant
$userId = $user->getId();
$data = $this->repo->getPresencesParJourForUser($userId);
$lesabsents = $this->repo->getEmployesAbsents($userId,$today);
$lespresents = $this->repo->getEmployesPresents($userId,$today);
$labels = array_keys($data);
$valuesp = array_values($data);
$absents =array_values($mesabsents);
// Évolution sur 7 jours
$dates = [];
$values = [];
for ($i = 6; $i >= 0; $i--) {
$date = (new \DateTimeImmutable())->modify("-{$i} days");
$dates[] = $date->format('d/m');
$values[] = $this->repo->countPresencesByCreator($user, $date);
}
$month = (new \DateTimeImmutable())->format('Y-m');
$starta = new \DateTime('-7 days'); // par ex : la semaine passée
$enda = new \DateTime('today');
$rowsa = $this->repo->getAbsentsParJour($user->getId(), $starta, $enda);
$labelsa = array_column($rowsa, 'jour');
$dataa = array_map('intval', array_column($rowsa, 'total_absents'));
$count = $this->userRepository->countByCreator($user);
$totalToday = $this->repo->countByDay($today, $user);
$lastPointages = $this->repo->lastPointages();
$statsWeek = $this->repo->countByWeek();
$heuresParEmploye = $this->repo->totalHoursByEmployeeForMonth($month);
return $this->render('admin/my-dashboard.html.twig', [
'total_today' => $totalToday,
'last_pointages' => $lastPointages,
'jours' => array_column($statsWeek, 'jour'),
'valeurs' => array_column($statsWeek, 'total'),
'heuresParEmploye' => $heuresParEmploye,
'userCount' => $count,
'presenceCount' => $presenceCount,
'absenceCount' => $absenceCount,
'dates' => $dates,
'values' => $values,
'labels' => json_encode($labels),
'valuesp' => json_encode($valuesp),
'lesabsents' => $lesabsents,
'lespresents' => $lespresents,
'absents' => $absents,
'labelsa' => json_encode($labelsa),
'dataa' => json_encode($dataa),
]);
}
public function configureDashboard(): Dashboard
{
return Dashboard::new()->setTitle('Pointage');
}
public function configureMenuItems(): iterable
{
yield MenuItem::linkToDashboard('Dashboard', 'fa fa-home');
}
public function configureAssets(): Assets
{
return parent::configureAssets()
->addCssFile('build/admin.css')
->addJsFile('build/admin.js');
}
#[Route('/admin/employe/{id}/pointages', name: 'employe_pointages')]
public function pointages(int $id, PointageRepository $repo, Request $request): Response
{
$mode = $request->query->get('mode', 'week'); // "week" ou "month"
$pointages = $mode === 'month'
? $repo->findByEmployeGroupedByMonth($id)
: $repo->findByEmployeGroupedByWeek($id);
return $this->render('admin/my-dashboard.html.twig', [
'pointages' => $pointages,
'mode' => $mode,
]);
}
#[Route('/admin/dashboard/presents', name: 'dashboard_presents')]
public function presents(PointageRepository $pointageRepository): Response
{
/** @var User|null $user */
$user = $this->getUser();
if (!$user) {
return $this->redirectToRoute('app_login');
}
$employesPresents = $pointageRepository->findEmployesPresentsToday($user);
return $this->render('admin/presents.html.twig', [
'employesPresents' => $employesPresents,
]);
}
}