src/Controller/Admin/DashboardController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Entity\User;
  4. use App\Repository\UserRepository;
  5. use App\Repository\PointageRepository;
  6. use App\Repository\EntrepriseRepository;
  7. use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
  8. use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
  9. use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
  10. use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use App\Entity\Entreprise;
  15. class DashboardController extends AbstractDashboardController
  16. {
  17.     private UserRepository $userRepository;
  18.     private PointageRepository $repo;
  19.     public function __construct(UserRepository $userRepositoryPointageRepository $repo,EntrepriseRepository $entrepriseRepository)
  20.     {
  21.         $this->userRepository $userRepository;
  22.         $this->repo $repo;
  23.         $this->entrepriseRepository $entrepriseRepository;
  24.     }
  25.     #[Route('/admin'name'app_admin')]
  26.     public function index(): Response
  27.     {
  28.         $this->denyAccessUnlessGranted('ROLE_ADMIN');
  29.         /** @var User $user */
  30.         $user $this->getUser();
  31.         if (!$user) {
  32.             return $this->redirectToRoute('app_login');
  33.         }
  34.         // Vérifie abonnement
  35.         if (!$user->isOkAbonnement() && in_array('ROLE_ADMIN'$user->getRoles(), true)) {
  36.             $this->addFlash('error''Vous n\'êtes pas autorisé à accéder à cette page, veuillez vous abonner d\'abord.');
  37.             return $this->redirectToRoute('souscription_new');
  38.         }
  39.         // Nombre total d’employés de l’entreprise créée par ce user
  40.         //$entreprise = $user->getEntreprise();
  41.         $em $this->getDoctrine()->getManager();
  42.         $entrepriseRepository $em->getRepository(\App\Entity\Entreprise::class);
  43.        // $entreprise = $this->entrepriseRepository->find($user->getid());
  44.         $entreprises $this->entrepriseRepository->findBy(['createdBy' => $user]);
  45.         $totalEmployes 0;
  46. foreach ($entreprises as $entreprise) {
  47.     $totalEmployes += count($entreprise->getEmployes());
  48. }
  49.        
  50.         //$totalEmployes = $entreprise ? count($entreprise->getEmployes()) : 0;
  51.         // Présence et Absence du jour
  52.         $today = new \DateTimeImmutable('today');
  53.         $presenceCount $this->repo->countPresencesByCreator($user$today);
  54.         $absenceCount  $this->repo->countAbsencesByCreator($user$today$totalEmployes);
  55.         $mesabsents $this->repo->getEmployesAbsents($user->getId(), $today);
  56.         
  57.         // Présences par jour pour le mois courant
  58.         $userId $user->getId();
  59.         $data $this->repo->getPresencesParJourForUser($userId);
  60.         $lesabsents  $this->repo->getEmployesAbsents($userId,$today);
  61.         $lespresents  $this->repo->getEmployesPresents($userId,$today);
  62.          $resultats $this->repo->findRetardatairesSemaine(new \DateTimeImmutable(), $userId);
  63.         $labels array_keys($data);
  64.         $valuesp array_values($data);
  65.         $absents =array_values($mesabsents);
  66.         // Évolution sur 7 jours
  67.         $dates = [];
  68.         $values = [];
  69.         for ($i 6$i >= 0$i--) {
  70.             $date = (new \DateTimeImmutable())->modify("-{$i} days");
  71.             $dates[] = $date->format('d/m');
  72.             $values[] = $this->repo->countPresencesByCreator($user$date);
  73.         }
  74.         $month = (new \DateTimeImmutable())->format('Y-m');
  75.     $starta = new \DateTime('-10 days'); // par ex : la semaine passée
  76.     $enda   = new \DateTime('today');
  77.     $rowsa $this->repo->getAbsentsParJour($user->getId(), $starta$enda);
  78.     $labelsa array_column($rowsa'jour');
  79.     $dataa   array_map('intval'array_column($rowsa'total_absents'));
  80.  // Remplacer employe_id par l'objet Employe
  81.     $retardataires = [];
  82.     foreach ($resultats as $r) {
  83.         $employe $this->userRepository->find($r['employe_id']);
  84.         $retardataires[] = [
  85.             'employe' => $employe,
  86.             'totalRetard' => (int) $r['total_retard'],
  87.         ];
  88.     }
  89.         $count $this->userRepository->countByCreator($user);
  90.         $totalToday $this->repo->countByDay($today$user);
  91.         $lastPointages $this->repo->lastPointages();
  92.         $statsWeek $this->repo->countByWeek();
  93.         $heuresParEmploye $this->repo->totalHoursByEmployeeForMonth($month);
  94.         return $this->render('admin/my-dashboard.html.twig', [
  95.             'total_today'    => $totalToday,
  96.             'last_pointages' => $lastPointages,
  97.             'jours'          => array_column($statsWeek'jour'),
  98.             'valeurs'        => array_column($statsWeek'total'),
  99.             'heuresParEmploye' => $heuresParEmploye,
  100.             'userCount'      => $count,
  101.             'presenceCount'  => $presenceCount,
  102.             'absenceCount'   => $absenceCount,
  103.             'dates'          => $dates,
  104.             'values'         => $values,
  105.             'labels'         => json_encode($labels),
  106.             'valuesp'        => json_encode($valuesp),
  107.             'lesabsents'     => $lesabsents,
  108.             'lespresents'    => $lespresents,
  109.             'absents'     => $absents,
  110.             'labelsa' => json_encode($labelsa),
  111.             'dataa'   => json_encode($dataa),
  112.             'retardataires' => $retardataires,
  113.         ]);
  114.     }
  115.     public function configureDashboard(): Dashboard
  116.     {
  117.         return Dashboard::new()->setTitle('Pointage');
  118.     }
  119.     public function configureMenuItems(): iterable
  120.     {
  121.         yield MenuItem::linkToDashboard('Dashboard''fa fa-home');
  122.     }
  123.     public function configureAssets(): Assets
  124.     {
  125.         return parent::configureAssets()
  126.             ->addCssFile('build/admin.css')
  127.             ->addJsFile('build/admin.js');
  128.     }
  129.     #[Route('/admin/employe/{id}/pointages'name'employe_pointages')]
  130.     public function pointages(int $idPointageRepository $repoRequest $request): Response
  131.     {
  132.         $mode $request->query->get('mode''week'); // "week" ou "month"
  133.         $pointages $mode === 'month'
  134.             $repo->findByEmployeGroupedByMonth($id)
  135.             : $repo->findByEmployeGroupedByWeek($id);
  136.         return $this->render('admin/my-dashboard.html.twig', [
  137.             'pointages' => $pointages,
  138.             'mode'      => $mode,
  139.         ]);
  140.     }
  141.     #[Route('/admin/dashboard/presents'name'dashboard_presents')]
  142.     public function presents(PointageRepository $pointageRepository): Response
  143.     {
  144.         /** @var User|null $user */
  145.         $user $this->getUser();
  146.         if (!$user) {
  147.             return $this->redirectToRoute('app_login');
  148.         }
  149.         $employesPresents $pointageRepository->findEmployesPresentsToday($user);
  150.         return $this->render('admin/presents.html.twig', [
  151.             'employesPresents' => $employesPresents,
  152.         ]);
  153.     }
  154.     #[Route('/admin/dashboard/absents'name'dashboard_absents')]
  155.   public function absents(PointageRepository $pointageRepository): Response
  156. {
  157.     /** @var User|null $user */
  158.     $user $this->getUser();
  159.     if (!$user) {
  160.         return $this->redirectToRoute('app_login');
  161.     }
  162.     $today = new \DateTimeImmutable('today');
  163.     $employesAbsents $pointageRepository->getEmployesAbsents($user->getId(), $today);
  164.     return $this->render('admin/absents.html.twig', [
  165.         'employesAbsents' => $employesAbsents,
  166.     ]);
  167. }
  168. }