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'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.         $labels array_keys($data);
  63.         $valuesp array_values($data);
  64.         $absents =array_values($mesabsents);
  65.         // Évolution sur 7 jours
  66.         $dates = [];
  67.         $values = [];
  68.         for ($i 6$i >= 0$i--) {
  69.             $date = (new \DateTimeImmutable())->modify("-{$i} days");
  70.             $dates[] = $date->format('d/m');
  71.             $values[] = $this->repo->countPresencesByCreator($user$date);
  72.         }
  73.         $month = (new \DateTimeImmutable())->format('Y-m');
  74. $starta = new \DateTime('-7 days'); // par ex : la semaine passée
  75.     $enda   = new \DateTime('today');
  76.     $rowsa $this->repo->getAbsentsParJour($user->getId(), $starta$enda);
  77.     $labelsa array_column($rowsa'jour');
  78.     $dataa   array_map('intval'array_column($rowsa'total_absents'));
  79.         $count $this->userRepository->countByCreator($user);
  80.         $totalToday $this->repo->countByDay($today$user);
  81.         $lastPointages $this->repo->lastPointages();
  82.         $statsWeek $this->repo->countByWeek();
  83.         $heuresParEmploye $this->repo->totalHoursByEmployeeForMonth($month);
  84.         return $this->render('admin/my-dashboard.html.twig', [
  85.             'total_today'    => $totalToday,
  86.             'last_pointages' => $lastPointages,
  87.             'jours'          => array_column($statsWeek'jour'),
  88.             'valeurs'        => array_column($statsWeek'total'),
  89.             'heuresParEmploye' => $heuresParEmploye,
  90.             'userCount'      => $count,
  91.             'presenceCount'  => $presenceCount,
  92.             'absenceCount'   => $absenceCount,
  93.             'dates'          => $dates,
  94.             'values'         => $values,
  95.             'labels'         => json_encode($labels),
  96.             'valuesp'        => json_encode($valuesp),
  97.             'lesabsents'     => $lesabsents,
  98.             'lespresents'    => $lespresents,
  99.             'absents'     => $absents,
  100.             'labelsa' => json_encode($labelsa),
  101.         'dataa'   => json_encode($dataa),
  102.         ]);
  103.     }
  104.     public function configureDashboard(): Dashboard
  105.     {
  106.         return Dashboard::new()->setTitle('Pointage');
  107.     }
  108.     public function configureMenuItems(): iterable
  109.     {
  110.         yield MenuItem::linkToDashboard('Dashboard''fa fa-home');
  111.     }
  112.     public function configureAssets(): Assets
  113.     {
  114.         return parent::configureAssets()
  115.             ->addCssFile('build/admin.css')
  116.             ->addJsFile('build/admin.js');
  117.     }
  118.     #[Route('/admin/employe/{id}/pointages'name'employe_pointages')]
  119.     public function pointages(int $idPointageRepository $repoRequest $request): Response
  120.     {
  121.         $mode $request->query->get('mode''week'); // "week" ou "month"
  122.         $pointages $mode === 'month'
  123.             $repo->findByEmployeGroupedByMonth($id)
  124.             : $repo->findByEmployeGroupedByWeek($id);
  125.         return $this->render('admin/my-dashboard.html.twig', [
  126.             'pointages' => $pointages,
  127.             'mode'      => $mode,
  128.         ]);
  129.     }
  130.     #[Route('/admin/dashboard/presents'name'dashboard_presents')]
  131.     public function presents(PointageRepository $pointageRepository): Response
  132.     {
  133.         /** @var User|null $user */
  134.         $user $this->getUser();
  135.         if (!$user) {
  136.             return $this->redirectToRoute('app_login');
  137.         }
  138.         $employesPresents $pointageRepository->findEmployesPresentsToday($user);
  139.         return $this->render('admin/presents.html.twig', [
  140.             'employesPresents' => $employesPresents,
  141.         ]);
  142.     }
  143. }