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,TranslatorInterface $translator): Response
  24.     {
  25.         $localisations $doctrine->getRepository(GroupAncestorDetails::class)->findBy(['groupTypeName' => 'Place'], ['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.         $allSites[] = [
  33.             'groupName' => $translator->trans("All sites"),
  34.             "groupId" => -999,
  35.             "ancestorGroupId" => -999,
  36.             "groupLevelId" => 0,
  37.             "groupLevelName" => "Site",
  38.             "groupTypeName" => "Localisation"
  39.         ];
  40.         array_push($allSites,...array_map(function($item) {
  41.             if ($item['ancestorGroupId'] == $item['groupId']) {
  42.                 $item['groupId'] = -999;
  43.             }
  44.             return $item;
  45.         },Json_decode($serializer->serialize($localisations'json'), true)));
  46.         foreach ($allSites as $item) {
  47.             if ($item['ancestorGroupId'] == $item['groupId']) {
  48.                 $localisationsTree[] = array(
  49.                     'name' => $item['groupName'],
  50.                     'value' => $item['ancestorGroupId'],
  51.                     'children' => array(),
  52.                 );
  53.             } else {
  54.                 $this->buildTree($localisationsTree,$item,'localisation');
  55.             }
  56.         }
  57.         $typeResourcesTree = [];
  58.         $allTypeResources[] = [
  59.             'groupName' => $translator->trans("All resources"),
  60.             "groupId" => -999,
  61.             "ancestorGroupId" => -999,
  62.             "groupLevelId" => 0,
  63.             "groupLevelName" => "Site",
  64.             "groupTypeName" => "Localisation"
  65.         ];
  66.         
  67.         array_push($allTypeResources,...array_map(function($item) {
  68.             if ($item['ancestorGroupId'] == $item['groupId']) {
  69.                 $item['groupId'] = -999;
  70.             }
  71.             return $item;
  72.         },Json_decode($serializer->serialize($typeResources'json'), true)));
  73.         foreach ($allTypeResources as $item) {
  74.             if ($item['ancestorGroupId'] == $item['groupId']) {
  75.                 $typeResourcesTree[] = array(
  76.                     'name' => $item['groupName'],
  77.                     'value' => $item['ancestorGroupId'],
  78.                     'children' => array(),
  79.                 );
  80.             } else {
  81.                 $this->buildTree($typeResourcesTree,$item,'resourceType',true);
  82.             }
  83.         }
  84.         return $this->render('statistiques/resources.html.twig', [
  85.             'localisationsTree' => $localisationsTree,
  86.             'typeResourcesTree' => $typeResourcesTree,
  87.             'typeResources' => $typeResources,
  88.             'localisations' => $localisations,
  89.             'resources' => $resources,
  90.             'users' => $users,
  91.             'status' => $status,
  92.             'general' => $general,
  93.         ]);
  94.     }
  95.         
  96.     // Fonction récursive pour construire l'arbre
  97.     function buildTree(&$parents$item$type$checked false)
  98.     {
  99.         foreach ($parents as &$parent) {
  100.             if (isset($parent['value']) && $parent['value'] == $item['groupId']) {
  101.                 // Ajouter l'objet enfant au tableau children de l'objet principal correspondant
  102.                 $parent['children'][] = array(
  103.                     'name' => $item['groupName'],
  104.                     'value' => $item['ancestorGroupId'],
  105.                     'children' => array(),
  106.                 );
  107.             } else {
  108.                 $this->buildTree($parent['children'],$item,$type);
  109.             }
  110.         }
  111.     }
  112.     
  113.     #[Route('/get-reservation'name'getReservation')]
  114.     public function getReservation(ManagerRegistry $doctrineRequest $requestTranslatorInterface $translator): Response
  115.     {
  116.         $reservation $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'));
  117.         return new JsonResponse([
  118.             'totalReservations' => $reservation[0]['value'],
  119.         ]);
  120.     } 
  121.     #[Route('/get-status'name'getStatus')]
  122.     public function getStatus(ManagerRegistry $doctrineRequest $requestTranslatorInterface $translator): Response
  123.     {
  124.         $status $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'),'status');
  125.         foreach ($status as $key => $value) {
  126.             $status[$key]['item'] =  $translator->trans($value['item']);
  127.         }
  128.         return new JsonResponse([
  129.             'reservationStatus' => $status,
  130.         ]);
  131.     }
  132.     #[Route('/get-origin'name'getOrigin')]
  133.     public function getOrigin(ManagerRegistry $doctrineRequest $requestTranslatorInterface $translator): Response
  134.     {
  135.         $origins $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'),'origin');
  136.         foreach ($origins as $key => $value) {
  137.             $origins[$key]['item'] =  $translator->trans($value['item']);
  138.         }
  139.         return new JsonResponse([
  140.             'reservationOrigin' => $origins,
  141.         ]);
  142.     }
  143.     #[Route('/get-immediate'name'getImmediate')]
  144.     public function getImmediate(ManagerRegistry $doctrineRequest $requestTranslatorInterface $translator): Response
  145.     {
  146.         $immediate $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'),'immediat_use');
  147.         //Translate immadiateUse
  148.         foreach ($immediate as $key => $value) {
  149.             $immediate[$key]['item'] =  $translator->trans($value['item']);
  150.         }
  151.         return new JsonResponse([
  152.             'immediateReservation' => $immediate,
  153.         ]);
  154.     }
  155.     #[Route('/get-type-site'name'getTypeSite')]
  156.     public function getTypeSite(ManagerRegistry $doctrineRequest $requestTranslatorInterface $translator): Response
  157.     {
  158.         $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');
  159.         return new JsonResponse([
  160.             'resourceBySite' => $resourceBySite,
  161.         ]);
  162.     }
  163.     #[Route('/get-by-stage'name'getByStage')]
  164.     public function getByStage(ManagerRegistry $doctrineRequest $requestTranslatorInterface $translator): Response
  165.     {
  166.         $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');
  167.         return new JsonResponse([
  168.             'resourceByStage' => $resourceByStage,
  169.         ]);
  170.     }
  171.     #[Route('/get-without-site'name'getWithoutSite')]
  172.     public function getWithoutSite(ManagerRegistry $doctrineRequest $requestTranslatorInterface $translator): Response
  173.     {
  174.         $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');
  175.         $resourceByStackingType=[];
  176.         $resourceWithoutSite=[];
  177.         foreach ($resourceByStackingDiagram as $item) {
  178.             $site $item['site'];
  179.             $value $item['value'];
  180.             $itemName $item['item'];
  181.             if(!isset($resourceByStackingType[$site])){
  182.                 $resourceByStackingType[$site]['site'] = $site;
  183.             }
  184.             $resourceByStackingType[$site][$itemName] = $value;
  185.             $resourceWithoutSite[$itemName] = $value;
  186.         }
  187.         $resourceByStackingType array_values($resourceByStackingType);
  188.         return new JsonResponse([
  189.             'resourceByStackingDiagram' => $resourceByStackingType,
  190.             'resourceWithoutSite' => $resourceWithoutSite,
  191.         ]);
  192.     }
  193.     #[Route('/get-type-zone'name'getByZone')]
  194.     public function getTypeZone(ManagerRegistry $doctrineRequest $requestTranslatorInterface $translator): Response
  195.     {
  196.         $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');
  197.         return new JsonResponse([
  198.             'resourceByZone' => $resourceByZone,
  199.         ]);
  200.     }
  201.     #[Route('/get-by-type'name'getByType')]
  202.     public function getByType(ManagerRegistry $doctrineRequest $requestTranslatorInterface $translator): Response
  203.     {
  204.         $resourceByType $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');
  205.         return new JsonResponse([
  206.             'resourceByType' => $resourceByType,
  207.         ]);
  208.     }
  209.     #[Route('/get-creneau'name'getCreneau')]
  210.     public function getCreneau(ManagerRegistry $doctrineRequest $request): Response
  211.     {
  212.         $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);
  213.         $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'));
  214.         $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'));
  215.         $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'));
  216.         $tauxOccupation = [];
  217.         $resourceDispoTotal 0;
  218.         $totalReservation 0;
  219.         $interval = new \DateInterval('PT30M');
  220.         foreach ($findResourceByDates as $item) {
  221.             
  222.             $currentHour = new \DateTime($item['open']);
  223.             $endHour = new \DateTime($item['close']);
  224.             while ($currentHour $endHour) {
  225.                 if (in_array($currentHour->format('H:i'),array_column($tauxOccupation,'hour'))) {
  226.                     $index array_search($currentHour->format('H:i'),array_column($tauxOccupation,'hour'));
  227.                     $tauxOccupation[$index]['resourceDispo'] += $item['count'];
  228.                 }else {
  229.                     $tauxOccupation[] = [
  230.                         'hour' => $currentHour->format('H:i'),
  231.                         'reservation' => 0,
  232.                         'reservation_reel' => 0,
  233.                         'resourceDispo' => $item['count'],
  234.                         'tauxOccupation' => 0,
  235.                         'tauxOccupationReal' => 0
  236.                     ];
  237.                 }
  238.                 $resourceDispoTotal += $item['count'];
  239.                 $currentHour->add($interval);
  240.             }
  241.         }
  242.         foreach ($creneau as $item) {
  243.             $index array_search($item['hour'],array_column($tauxOccupation,'hour'));
  244.             
  245.             $tauxOccupation[$index]['reservation'] += $item['value'];
  246.             $tauxOccupation[$index]['reservation_reel'] += $item['value_reel'];
  247.             $tauxOccupation[$index]['tauxOccupation'] =  round(($tauxOccupation[$index]['reservation'] * 100) / $tauxOccupation[$index]['resourceDispo'],2);
  248.             $tauxOccupation[$index]['tauxOccupationReal'] =  round(($tauxOccupation[$index]['reservation_reel'] * 100) / $tauxOccupation[$index]['resourceDispo'],2);
  249.             $totalReservation += $item['value'];
  250.         }
  251.         $tauxOccupationTotal 0;
  252.         if ($resourceDispoTotal 0)
  253.             $tauxOccupationTotal round(($totalReservation 100) / $resourceDispoTotal,2);
  254.         array_multisort(array_column($tauxOccupation'hour'), SORT_ASC$tauxOccupation);
  255.         return new JsonResponse([
  256.             'reservationByDay' => $reservationByDay,
  257.             'topResources' => $topResources,
  258.             'tauxOccupation' => $tauxOccupation,
  259.             'tauxOccupationTotal' => round($tauxOccupationTotal2)
  260.         ]);
  261.     }
  262.     public function getBlock() {
  263.         
  264.         //Blocages
  265.         $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'));
  266.         $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');
  267.         $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');
  268.         $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());
  269.         $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'));
  270.         return new JsonResponse([
  271.             'totalBlocks' => sizeof($blocages),
  272.             'blockBySite' => $blockBySite,
  273.             'blockByTypeResource' => $blockByTypeResource,
  274.             'blockByType' => $blockByType,
  275.             'blockByDates' => $blockByDates
  276.         ]);
  277.     }
  278.     #[Route('/getResourcesByFilters'name'getResourcesByFilters')]
  279.     public function getResourcesByFilters(ManagerRegistry $doctrineRequest $request): Response
  280.     {
  281.         $resources $doctrine->getRepository(Resource::class)->findGroupped($request->request->get('localisation'),$request->request->get('resourceType'));
  282.         return $this->render('components/resources-options.html.twig', [
  283.             'resources' => $resources,
  284.         ]);
  285.     }
  286.     #[Route('/getResourceByDates'name'getResourceByDates')]
  287.     public function getResourceByDates(ManagerRegistry $doctrineRequest $request): Response
  288.     {
  289.         $resources $doctrine->getRepository(Resource::class)->getResourceByDates($request->request->get('localisation'),$request->request->get('resourceType'));
  290.         return $this->render('components/resources-options.html.twig', [
  291.             'resources' => $resources,
  292.         ]);
  293.     }
  294. }