src/Controller/ResourcesController.php line 26
<?php
namespace App\Controller;
use App\Entity\Block;
use App\Entity\General;
use App\Entity\User;
use App\Entity\Status;
use App\Entity\Resource;
use App\Entity\Reservation;
use App\Entity\OccupationPerHour;
use App\Entity\GroupAncestorDetails;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class ResourcesController extends AbstractController
{
#[Route('/', name: 'app_default')]
public function index(ManagerRegistry $doctrine, SerializerInterface $serializer, Request $request): Response
{
$localisations = $doctrine->getRepository(GroupAncestorDetails::class)->findBy(['groupTypeName' => 'Localisation'], ['groupLevelId' => 'ASC']);
$typeResources = $doctrine->getRepository(GroupAncestorDetails::class)->findBy(['groupTypeName' => 'Type'], ['groupLevelId' => 'ASC']);
$status = $doctrine->getRepository(Status::class)->findAllStatus($request->getLocale());
$users = $doctrine->getRepository(User::class)->findUsers();
$general = $doctrine->getRepository(General::class)->find(1);
$resources = $doctrine->getRepository(Resource::class)->findGroupped();
$localisationsTree = [];
foreach (Json_decode($serializer->serialize($localisations, 'json'), true) as $item) {
if ($item['ancestorGroupId'] == $item['groupId']) {
$localisationsTree[] = array(
'text' => $item['groupName'],
'type' => 'localisation',
'checked' => false,
'id' => $item['ancestorGroupId'],
'children' => array()
);
} else {
$this->buildTree($localisationsTree,$item,'localisation');
}
}
$typeResourcesTree = [];
foreach (Json_decode($serializer->serialize($typeResources, 'json'), true) as $item) {
if ($item['ancestorGroupId'] == $item['groupId']) {
$typeResourcesTree[] = array(
'text' => $item['groupName'],
'type' => 'resourceType',
'checked' => true,
'id' => $item['ancestorGroupId'],
'children' => array()
);
} else {
$this->buildTree($typeResourcesTree,$item,'resourceType',true);
}
}
return $this->render('statistiques/resources.html.twig', [
'localisationsTree' => $localisationsTree,
'typeResourcesTree' => $typeResourcesTree,
'resources' => $resources,
'users' => $users,
'status' => $status,
'general' => $general,
]);
}
// Fonction récursive pour construire l'arbre
function buildTree(&$parents, $item, $type, $checked = false)
{
foreach ($parents as &$parent) {
if ($parent['id'] == $item['groupId']) {
// Ajouter l'objet enfant au tableau children de l'objet principal correspondant
$parent['children'][] = array(
'text' => $item['groupName'],
'type' => $type,
'checked' => $checked,
'id' => $item['ancestorGroupId'],
'children' => array()
);
} else {
$this->buildTree($parent['children'],$item,$type);
}
}
}
#[Route('/get-reservation', name: 'getReservation')]
public function getReservation(ManagerRegistry $doctrine, Request $request, TranslatorInterface $translator): Response
{
$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());
return new JsonResponse([
'totalReservations' => $reservation[0]['count'],
]);
}
#[Route('/get-status', name: 'getStatus')]
public function getStatus(ManagerRegistry $doctrine, Request $request, TranslatorInterface $translator): Response
{
$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');
return new JsonResponse([
'reservationStatus' => $status,
]);
}
#[Route('/get-origin', name: 'getOrigin')]
public function getOrigin(ManagerRegistry $doctrine, Request $request, TranslatorInterface $translator): Response
{
$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');
return new JsonResponse([
'reservationOrigin' => $origins,
]);
}
#[Route('/get-immediate', name: 'getImmediate')]
public function getImmediate(ManagerRegistry $doctrine, Request $request, TranslatorInterface $translator): Response
{
$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');
//Translate immadiateUse
foreach ($immediate as $key => $value) {
$immediate[$key]['item'] = $translator->trans($value['item']);
}
return new JsonResponse([
'immediateReservation' => $immediate,
]);
}
#[Route('/get-type-site', name: 'getTypeSite')]
public function getTypeSite(ManagerRegistry $doctrine, Request $request, TranslatorInterface $translator): Response
{
$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');
$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');
$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');
$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');
$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');
$resourceByStackingType=[];
$resourceWithoutSite=[];
foreach ($resourceByStackingDiagram as $item) {
$site = $item['site'];
$value = $item['value'];
$itemName = $item['item'];
if(!isset($resourceByStackingType[$site])){
$resourceByStackingType[$site]['site'] = $site;
}
$resourceByStackingType[$site][$itemName] = $value;
$resourceWithoutSite[$itemName] = $value;
}
$resourceByStackingType = array_values($resourceByStackingType);
// $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');
// $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');
return new JsonResponse([
'resourceByType' => $resourceByType,
'resourceBySite' => $resourceBySite,
'resourceByStage' => $resourceByStage,
'resourceByStackingDiagram' => $resourceByStackingType,
'resourceWithoutSite' => $resourceWithoutSite,
'resourceByZone' => $resourceByZone
]);
}
#[Route('/get-creneau', name: 'getCreneau')]
public function getCreneau(ManagerRegistry $doctrine, Request $request): Response
{
$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);
$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'));
$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'));
$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'));
$tauxOccupation = [];
$resourceDispoTotal = 0;
$totalReservation = 0;
$interval = new \DateInterval('PT30M');
foreach ($findResourceByDates as $item) {
$currentHour = new \DateTime($item['open']);
$endHour = new \DateTime($item['close']);
while ($currentHour < $endHour) {
if (in_array($currentHour->format('H:i'),array_column($tauxOccupation,'hour'))) {
$index = array_search($currentHour->format('H:i'),array_column($tauxOccupation,'hour'));
$tauxOccupation[$index]['resourceDispo'] += $item['count'];
}else {
$tauxOccupation[] = [
'hour' => $currentHour->format('H:i'),
'reservation' => 0,
'reservation_reel' => 0,
'resourceDispo' => $item['count'],
'tauxOccupation' => 0,
'tauxOccupationReal' => 0
];
}
$resourceDispoTotal += $item['count'];
$currentHour->add($interval);
}
}
foreach ($creneau as $item) {
$index = array_search($item['hour'],array_column($tauxOccupation,'hour'));
$tauxOccupation[$index]['reservation'] += $item['value'];
$tauxOccupation[$index]['reservation_reel'] += $item['value_reel'];
$tauxOccupation[$index]['tauxOccupation'] = round(($tauxOccupation[$index]['reservation'] * 100) / $tauxOccupation[$index]['resourceDispo'],2);
$tauxOccupation[$index]['tauxOccupationReal'] = round(($tauxOccupation[$index]['reservation_reel'] * 100) / $tauxOccupation[$index]['resourceDispo'],2);
$totalReservation += $item['value'];
}
$tauxOccupationTotal = 0;
if ($resourceDispoTotal > 0)
$tauxOccupationTotal = round(($totalReservation * 100) / $resourceDispoTotal,2);
array_multisort(array_column($tauxOccupation, 'hour'), SORT_ASC, $tauxOccupation);
return new JsonResponse([
'reservationByDay' => $reservationByDay,
'topResources' => $topResources,
'tauxOccupation' => $tauxOccupation,
'tauxOccupationTotal' => round($tauxOccupationTotal, 2)
]);
}
public function getBlock() {
//Blocages
$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'));
$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');
$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');
$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());
$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'));
return new JsonResponse([
'totalBlocks' => sizeof($blocages),
'blockBySite' => $blockBySite,
'blockByTypeResource' => $blockByTypeResource,
'blockByType' => $blockByType,
'blockByDates' => $blockByDates
]);
}
#[Route('/getResourcesByFilters', name: 'getResourcesByFilters')]
public function getResourcesByFilters(ManagerRegistry $doctrine, Request $request): Response
{
$resources = $doctrine->getRepository(Resource::class)->findGroupped($request->request->get('localisation'),$request->request->get('resourceType'));
return $this->render('components/resources-options.html.twig', [
'resources' => $resources,
]);
}
#[Route('/getResourceByDates', name: 'getResourceByDates')]
public function getResourceByDates(ManagerRegistry $doctrine, Request $request): Response
{
$resources = $doctrine->getRepository(Resource::class)->getResourceByDates($request->request->get('localisation'),$request->request->get('resourceType'));
return $this->render('components/resources-options.html.twig', [
'resources' => $resources,
]);
}
}