src/Controller/ResourcesController.php line 26

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Block;
  4. use App\Entity\General;
  5. use App\Entity\User;
  6. use App\Entity\Status;
  7. use App\Entity\Resource;
  8. use App\Entity\Reservation;
  9. use App\Entity\OccupationPerHour;
  10. use App\Entity\GroupAncestorDetails;
  11. use Doctrine\Persistence\ManagerRegistry;
  12. use Symfony\Component\Serializer\Serializer;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\Serializer\SerializerInterface;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  20. class ResourcesController extends AbstractController
  21. {
  22.     #[Route('/'name'app_default')]
  23.     public function index(ManagerRegistry $doctrineSerializerInterface $serializerRequest $request): Response
  24.     {
  25.         $localisations $doctrine->getRepository(GroupAncestorDetails::class)->findBy(['groupTypeName' => 'Localisation'], ['groupLevelId' => 'ASC']);
  26.         $typeResources $doctrine->getRepository(GroupAncestorDetails::class)->findBy(['groupTypeName' => 'Type'], ['groupLevelId' => 'ASC']);
  27.         $status $doctrine->getRepository(Status::class)->findAllStatus($request->getLocale());
  28.         $users $doctrine->getRepository(User::class)->findUsers();
  29.         $general $doctrine->getRepository(General::class)->find(1);
  30.         $resources $doctrine->getRepository(Resource::class)->findGroupped();
  31.         $localisationsTree = [];
  32.         foreach (Json_decode($serializer->serialize($localisations'json'), true) as $item) {
  33.             if ($item['ancestorGroupId'] == $item['groupId']) {
  34.                 $localisationsTree[] = array(
  35.                     'text' => $item['groupName'],
  36.                     'type' => 'localisation',
  37.                     'checked' => false,
  38.                     'id' => $item['ancestorGroupId'],
  39.                     'children' => array()
  40.                 );
  41.             } else {
  42.                 $this->buildTree($localisationsTree,$item,'localisation');
  43.             }
  44.         }
  45.         $typeResourcesTree = [];
  46.         foreach (Json_decode($serializer->serialize($typeResources'json'), true) as $item) {
  47.             if ($item['ancestorGroupId'] == $item['groupId']) {
  48.                 $typeResourcesTree[] = array(
  49.                     'text' => $item['groupName'],
  50.                     'type' => 'resourceType',
  51.                     'checked' => true,
  52.                     'id' => $item['ancestorGroupId'],
  53.                     'children' => array()
  54.                 );
  55.             } else {
  56.                 $this->buildTree($typeResourcesTree,$item,'resourceType',true);
  57.             }
  58.         }
  59.         return $this->render('statistiques/resources.html.twig', [
  60.             'localisationsTree' => $localisationsTree,
  61.             'typeResourcesTree' => $typeResourcesTree,
  62.             'resources' => $resources,
  63.             'users' => $users,
  64.             'status' => $status,
  65.             'general' => $general,
  66.         ]);
  67.     }
  68.         
  69.     // Fonction récursive pour construire l'arbre
  70.     function buildTree(&$parents$item$type$checked false)
  71.     {
  72.         foreach ($parents as &$parent) {
  73.             if ($parent['id'] == $item['groupId']) {
  74.                 // Ajouter l'objet enfant au tableau children de l'objet principal correspondant
  75.                 $parent['children'][] = array(
  76.                     'text' => $item['groupName'],
  77.                     'type' => $type,
  78.                     'checked' => $checked,
  79.                     'id' => $item['ancestorGroupId'],
  80.                     'children' => array()
  81.                 );
  82.             } else {
  83.                 $this->buildTree($parent['children'],$item,$type);
  84.             }
  85.         }
  86.     }
  87.     
  88.     #[Route('/get-reservation'name'getReservation')]
  89.     public function getReservation(ManagerRegistry $doctrineRequest $requestTranslatorInterface $translator): Response
  90.     {
  91.         $reservation $doctrine->getRepository(Reservation::class)->findReservationByFilters($request->request->get('resource'),$request->request->get('resourceType'), $request->request->get('localisation'), $request->request->get('user'), $request->request->get('startDate'), $request->request->get('endDate'),$request->request->get('status'), $request->getLocale());
  92.         return new JsonResponse([
  93.             'totalReservations' => $reservation[0]['count'],
  94.         ]);
  95.     } 
  96.     #[Route('/get-status'name'getStatus')]
  97.     public function getStatus(ManagerRegistry $doctrineRequest $requestTranslatorInterface $translator): Response
  98.     {
  99.         $status $doctrine->getRepository(Reservation::class)->findReservationByFilters($request->request->get('resource'),$request->request->get('resourceType'), $request->request->get('localisation'), $request->request->get('user'), $request->request->get('startDate'), $request->request->get('endDate'),$request->request->get('status'), $request->getLocale(),'status');
  100.         return new JsonResponse([
  101.             'reservationStatus' => $status,
  102.         ]);
  103.     }
  104.     #[Route('/get-origin'name'getOrigin')]
  105.     public function getOrigin(ManagerRegistry $doctrineRequest $requestTranslatorInterface $translator): Response
  106.     {
  107.         $origins $doctrine->getRepository(Reservation::class)->findReservationByFilters($request->request->get('resource'),$request->request->get('resourceType'), $request->request->get('localisation'), $request->request->get('user'), $request->request->get('startDate'), $request->request->get('endDate'),$request->request->get('status'), $request->getLocale(),'origin');
  108.         return new JsonResponse([
  109.             'reservationOrigin' => $origins,
  110.         ]);
  111.     }
  112.     #[Route('/get-immediate'name'getImmediate')]
  113.     public function getImmediate(ManagerRegistry $doctrineRequest $requestTranslatorInterface $translator): Response
  114.     {
  115.         $immediate $doctrine->getRepository(Reservation::class)->findReservationByFilters($request->request->get('resource'),$request->request->get('resourceType'), $request->request->get('localisation'), $request->request->get('user'), $request->request->get('startDate'), $request->request->get('endDate'),$request->request->get('status'), $request->getLocale(),'immediat_use');
  116.         //Translate immadiateUse
  117.         foreach ($immediate as $key => $value) {
  118.             $immediate[$key]['item'] =  $translator->trans($value['item']);
  119.         }
  120.         return new JsonResponse([
  121.             'immediateReservation' => $immediate,
  122.         ]);
  123.     }
  124.     #[Route('/get-type-site'name'getTypeSite')]
  125.     public function getTypeSite(ManagerRegistry $doctrineRequest $requestTranslatorInterface $translator): Response
  126.     {
  127.         $resourceByType $doctrine->getRepository(Reservation::class)->findReservationByFilters($request->request->get('resource'),$request->request->get('resourceType'), $request->request->get('localisation'), $request->request->get('user'), $request->request->get('startDate'), $request->request->get('endDate'),$request->request->get('status'), $request->getLocale(),'resourceByType');
  128.         $resourceBySite $doctrine->getRepository(OccupationPerHour::class)->findByFilter$request->request->get('localisation'), $request->request->get('user'), $request->request->get('startDate'), $request->request->get('endDate'),$request->request->get('status'), $request->getLocale(),$request->request->get('resource'),$request->request->get('resourceType'),'resourceBySite');
  129.         $resourceByStage $doctrine->getRepository(OccupationPerHour::class)->findByFilter$request->request->get('localisation'), $request->request->get('user'), $request->request->get('startDate'), $request->request->get('endDate'),$request->request->get('status'), $request->getLocale(),$request->request->get('resource'),$request->request->get('resourceType'),'resourceByStage');
  130.         $resourceByZone $doctrine->getRepository(OccupationPerHour::class)->findByFilter$request->request->get('localisation'), $request->request->get('user'), $request->request->get('startDate'), $request->request->get('endDate'),$request->request->get('status'), $request->getLocale(),$request->request->get('resource'),$request->request->get('resourceType'),'resourceByZone');
  131.         $resourceByStackingDiagram $doctrine->getRepository(OccupationPerHour::class)->findByFilter$request->request->get('localisation'), $request->request->get('user'), $request->request->get('startDate'), $request->request->get('endDate'),$request->request->get('status'), $request->getLocale(),$request->request->get('resource'),$request->request->get('resourceType'),'resourceByType');
  132.         $resourceByStackingType=[];
  133.         $resourceWithoutSite=[];
  134.         foreach ($resourceByStackingDiagram as $item) {
  135.             $site $item['site'];
  136.             $value $item['value'];
  137.             $itemName $item['item'];
  138.             if(!isset($resourceByStackingType[$site])){
  139.                 $resourceByStackingType[$site]['site'] = $site;
  140.             }
  141.             $resourceByStackingType[$site][$itemName] = $value;
  142.             $resourceWithoutSite[$itemName] = $value;
  143.         }
  144.         $resourceByStackingType array_values($resourceByStackingType);
  145.        // $resourceByBuilding = $doctrine->getRepository(OccupationPerHour::class)->findByFilter( $request->request->get('localisation'), $request->request->get('user'), $request->request->get('startDate'), $request->request->get('endDate'),$request->request->get('status'), $request->getLocale(),$request->request->get('resource'),$request->request->get('resourceType'),'resourceByBuilding');
  146.       //  $resourceHasNotChildren = $doctrine->getRepository(OccupationPerHour::class)->findByFilter( $request->request->get('localisation'), $request->request->get('user'), $request->request->get('startDate'), $request->request->get('endDate'),$request->request->get('status'), $request->getLocale(),$request->request->get('resource'),$request->request->get('resourceType'),'hasNotChildren');
  147.         return new JsonResponse([
  148.             'resourceByType' => $resourceByType,
  149.             'resourceBySite' => $resourceBySite,
  150.             'resourceByStage' => $resourceByStage,
  151.             'resourceByStackingDiagram' => $resourceByStackingType,
  152.             'resourceWithoutSite' => $resourceWithoutSite,
  153.             'resourceByZone' => $resourceByZone
  154.         ]);
  155.     }
  156.     #[Route('/get-creneau'name'getCreneau')]
  157.     public function getCreneau(ManagerRegistry $doctrineRequest $request): Response
  158.     {
  159.         $topResources $doctrine->getRepository(Reservation::class)->findReservationByFilters($request->request->get('resource'),$request->request->get('resourceType'), $request->request->get('localisation'), $request->request->get('user'), $request->request->get('startDate'), $request->request->get('endDate'),$request->request->get('status'), $request->getLocale(),'ressource',5);
  160.         $creneau $doctrine->getRepository(OccupationPerHour::class)->findCreneau($request->request->get('resource'),$request->request->get('resourceType'), $request->request->get('localisation'), $request->request->get('startDate'), $request->request->get('endDate'), $request->request->get('user'),$request->request->get('status'));
  161.         $reservationByDay $doctrine->getRepository(OccupationPerHour::class)->findReservationByDay($request->request->get('resource'),$request->request->get('resourceType'), $request->request->get('localisation'), $request->request->get('startDate'), $request->request->get('endDate'), $request->request->get('user'),$request->request->get('status'));
  162.         $findResourceByDates $doctrine->getRepository(Resource::class)->findResourceByDates($request->request->get('startDate'), $request->request->get('endDate'),$request->request->get('localisation'), $request->request->get('resourceType'),$request->request->get('resource'));
  163.         $tauxOccupation = [];
  164.         $resourceDispoTotal 0;
  165.         $totalReservation 0;
  166.         $interval = new \DateInterval('PT30M');
  167.         foreach ($findResourceByDates as $item) {
  168.             
  169.             $currentHour = new \DateTime($item['open']);
  170.             $endHour = new \DateTime($item['close']);
  171.             while ($currentHour $endHour) {
  172.                 if (in_array($currentHour->format('H:i'),array_column($tauxOccupation,'hour'))) {
  173.                     $index array_search($currentHour->format('H:i'),array_column($tauxOccupation,'hour'));
  174.                     $tauxOccupation[$index]['resourceDispo'] += $item['count'];
  175.                 }else {
  176.                     $tauxOccupation[] = [
  177.                         'hour' => $currentHour->format('H:i'),
  178.                         'reservation' => 0,
  179.                         'reservation_reel' => 0,
  180.                         'resourceDispo' => $item['count'],
  181.                         'tauxOccupation' => 0,
  182.                         'tauxOccupationReal' => 0
  183.                     ];
  184.                 }
  185.                 $resourceDispoTotal += $item['count'];
  186.                 $currentHour->add($interval);
  187.             }
  188.         }
  189.         foreach ($creneau as $item) {
  190.             $index array_search($item['hour'],array_column($tauxOccupation,'hour'));
  191.             
  192.             $tauxOccupation[$index]['reservation'] += $item['value'];
  193.             $tauxOccupation[$index]['reservation_reel'] += $item['value_reel'];
  194.             $tauxOccupation[$index]['tauxOccupation'] =  round(($tauxOccupation[$index]['reservation'] * 100) / $tauxOccupation[$index]['resourceDispo'],2);
  195.             $tauxOccupation[$index]['tauxOccupationReal'] =  round(($tauxOccupation[$index]['reservation_reel'] * 100) / $tauxOccupation[$index]['resourceDispo'],2);
  196.             $totalReservation += $item['value'];
  197.         }
  198.         $tauxOccupationTotal 0;
  199.         if ($resourceDispoTotal 0)
  200.             $tauxOccupationTotal round(($totalReservation 100) / $resourceDispoTotal,2);
  201.         array_multisort(array_column($tauxOccupation'hour'), SORT_ASC$tauxOccupation);
  202.         return new JsonResponse([
  203.             'reservationByDay' => $reservationByDay,
  204.             'topResources' => $topResources,
  205.             'tauxOccupation' => $tauxOccupation,
  206.             'tauxOccupationTotal' => round($tauxOccupationTotal2)
  207.         ]);
  208.     }
  209.     public function getBlock() {
  210.         
  211.         //Blocages
  212.         $blocages = []; // $doctrine->getRepository(Block::class)->getBlockageByFilters($request->request->get('resource'),$request->request->get('resourceType'), $request->request->get('localisation'), $request->request->get('startDate'), $request->request->get('endDate'));
  213.         $blockBySite = []; // $doctrine->getRepository(Block::class)->getBlockagesGroppedByResource($request->request->get('resource'),$request->request->get('resourceType'), $request->request->get('localisation'), $request->request->get('startDate'), $request->request->get('endDate'),'Localisation');
  214.         $blockByTypeResource = []; // $doctrine->getRepository(Block::class)->getBlockagesGroppedByResource($request->request->get('resource'),$request->request->get('resourceType'), $request->request->get('localisation'), $request->request->get('startDate'), $request->request->get('endDate'),'Type');
  215.         $blockByType =  []; //$doctrine->getRepository(Block::class)->getBlockageByType($request->request->get('resource'),$request->request->get('resourceType'), $request->request->get('localisation'), $request->request->get('startDate'), $request->request->get('endDate'), $request->getLocale());
  216.         $blockByDates = []; // $doctrine->getRepository(Block::class)->getBlockByDates($request->request->get('resource'),$request->request->get('resourceType'), $request->request->get('localisation'), $request->request->get('startDate'), $request->request->get('endDate'));
  217.         return new JsonResponse([
  218.             'totalBlocks' => sizeof($blocages),
  219.             'blockBySite' => $blockBySite,
  220.             'blockByTypeResource' => $blockByTypeResource,
  221.             'blockByType' => $blockByType,
  222.             'blockByDates' => $blockByDates
  223.         ]);
  224.     }
  225.     #[Route('/getResourcesByFilters'name'getResourcesByFilters')]
  226.     public function getResourcesByFilters(ManagerRegistry $doctrineRequest $request): Response
  227.     {
  228.         $resources $doctrine->getRepository(Resource::class)->findGroupped($request->request->get('localisation'),$request->request->get('resourceType'));
  229.         return $this->render('components/resources-options.html.twig', [
  230.             'resources' => $resources,
  231.         ]);
  232.     }
  233.     #[Route('/getResourceByDates'name'getResourceByDates')]
  234.     public function getResourceByDates(ManagerRegistry $doctrineRequest $request): Response
  235.     {
  236.         $resources $doctrine->getRepository(Resource::class)->getResourceByDates($request->request->get('localisation'),$request->request->get('resourceType'));
  237.         return $this->render('components/resources-options.html.twig', [
  238.             'resources' => $resources,
  239.         ]);
  240.     }
  241. }