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,TranslatorInterface $translator): Response
{
$localisations = Json_decode($serializer->serialize($doctrine->getRepository(GroupAncestorDetails::class)->findBy(['groupTypeName' => 'Localisation'], ['groupLevelId' => 'ASC']), 'json',['groups' => 'groupAncestor']),true);
$typeResources = Json_decode($serializer->serialize($doctrine->getRepository(GroupAncestorDetails::class)->findBy(['groupTypeName' => 'Type'], ['groupLevelId' => 'ASC']), 'json',['groups' => 'groupAncestor']),true);
$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 = [];
$allSites[] = [
"id" => -999,
"groupAncestorId" => -999,
"groupName" => $translator->trans("All sites"),
"groupLevelId" => 0,
"groupLevelName" => "Site",
"groupTypeName" => "Localisation"
];
array_push($allSites,...array_map(function($item) {
if ($item['id'] == $item['groupAncestorId']) {
$item['groupAncestorId'] = -999;
}
return $item;
},$localisations));
foreach ($allSites as $item) {
if ($item['id'] == $item['groupAncestorId']) {
$localisationsTree[] = array(
'name' => $item['groupName'],
'value' => $item['id'],
'children' => array(),
);
} else {
$this->buildTree($localisationsTree,$item,'localisation');
}
}
$typeResourcesTree = [];
$allTypeResources[] = [
"id" => -999,
"groupAncestorId" => -999,
"groupName" => $translator->trans("All resources"),
"groupLevelId" => 0,
"groupLevelName" => "Type",
"groupTypeName" => "Type"
];
array_push($allTypeResources,...array_map(function($item) {
if ($item['id'] == $item['groupAncestorId']) {
$item['groupAncestorId'] = -999;
}
return $item;
},$typeResources));
foreach ($allTypeResources as $item) {
if ($item['id'] == $item['groupAncestorId']) {
$typeResourcesTree[] = array(
'name' => $item['groupName'],
'value' => $item['id'],
'children' => array(),
);
} else {
$this->buildTree($typeResourcesTree,$item,'resourceType',true);
}
}
return $this->render('statistiques/resources.html.twig', [
'localisationsTree' => $localisationsTree,
'typeResourcesTree' => $typeResourcesTree,
'typeResources' => $typeResources,
'localisations' => $localisations,
'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 (isset($parent['value']) && $parent['value'] == $item['groupAncestorId']) {
// Ajouter l'objet enfant au tableau children de l'objet principal correspondant
$parent['children'][] = array(
'name' => $item['groupName'],
'value' => $item['id'],
'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)->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'));
return new JsonResponse([
'totalReservations' => $reservation[0]['value'],
]);
}
#[Route('/get-status', name: 'getStatus')]
public function getStatus(ManagerRegistry $doctrine, Request $request, TranslatorInterface $translator): Response
{
$status = $doctrine->getRepository(Reservation::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');
foreach ($status as $key => $value) {
$status[$key]['item'] = $translator->trans($value['item']);
}
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)->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');
foreach ($origins as $key => $value) {
$origins[$key]['item'] = $translator->trans($value['item']);
}
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)->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');
//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
{
$resourceBySite = $doctrine->getRepository(Reservation::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',10);
return new JsonResponse([
'resourceBySite' => $resourceBySite,
]);
}
#[Route('/get-by-stage', name: 'getByStage')]
public function getByStage(ManagerRegistry $doctrine, Request $request, TranslatorInterface $translator): Response
{
$resourceByStage = $doctrine->getRepository(Reservation::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');
$resourceByStage = array_reduce($resourceByStage, function ($carry, $item) use ($translator) {
// Vérifiez si l'élément existe déjà dans le tableau de regroupement
$key = $item["item"];
if (!array_key_exists($key, $carry)) {
$carry[$key] = ["item" => $key, $translator->trans('Refused') => 0, $translator->trans("Cancelled") => 0, $translator->trans("Pending") => 0, $translator->trans("Confirmed") => 0];
}
// Incrémentez le total du statut correspondant
if ($item['status'] == -1)
$carry[$key][$translator->trans("Refused")] += $item["value"];
else if ($item['status'] == 0)
$carry[$key][$translator->trans("Cancelled")] += $item["value"];
else if ($item['status'] == 1)
$carry[$key][$translator->trans("Pending")] += $item["value"];
else
$carry[$key][$translator->trans("Confirmed")] += $item["value"];
return $carry;
}, []);
return new JsonResponse([
'resourceByStage' => array_values($resourceByStage),
]);
}
#[Route('/get-without-site', name: 'getWithoutSite')]
public function getWithoutSite(ManagerRegistry $doctrine, Request $request, TranslatorInterface $translator): Response
{
$resourceByStackingDiagram = $doctrine->getRepository(Reservation::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]['item'] = $site;
}
$resourceByStackingType[$site][$itemName] = $value;
$resourceWithoutSite[$itemName] = $value;
}
$resourceByStackingType = array_values($resourceByStackingType);
return new JsonResponse([
'resourceByStackingDiagram' => $resourceByStackingType,
'resourceWithoutSite' => $resourceWithoutSite,
]);
}
#[Route('/get-type-zone', name: 'getByZone')]
public function getTypeZone(ManagerRegistry $doctrine, Request $request, TranslatorInterface $translator): Response
{
$resourceByZone = $doctrine->getRepository(Reservation::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');
return new JsonResponse([
'resourceByZone' => $resourceByZone,
]);
}
#[Route('/get-by-type', name: 'getByType')]
public function getByType(ManagerRegistry $doctrine, Request $request, TranslatorInterface $translator): Response
{
$resourceByType = $doctrine->getRepository(Reservation::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');
return new JsonResponse([
'resourceByType' => $resourceByType,
]);
}
#[Route('/get-creneau', name: 'getCreneau')]
public function getCreneau(ManagerRegistry $doctrine, Request $request,TranslatorInterface $translator): Response
{
$weekDays = [
1 => 'Lundi',
2 => 'Mardi',
3 => 'Mercredi',
4 => 'Jeudi',
5 => 'Vendredi',
6 => 'Samedi',
7 => 'Dimanche',
];
$topResources = $doctrine->getRepository(Reservation::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'),'resources',5);
$topResources = array_reduce($topResources, function ($carry, $item) use ($translator) {
// Vérifiez si l'élément existe déjà dans le tableau de regroupement
$key = $item["item"];
if (!array_key_exists($key, $carry)) {
$carry[$key] = ["item" => $key, $translator->trans('Refused') => 0, $translator->trans("Cancelled") => 0, $translator->trans("Pending") => 0, $translator->trans("Confirmed") => 0];
}
// Incrémentez le total du statut correspondant
if ($item['status'] == -1)
$carry[$key][$translator->trans("Refused")] += $item["value"];
else if ($item['status'] == 0)
$carry[$key][$translator->trans("Cancelled")] += $item["value"];
else if ($item['status'] == 1)
$carry[$key][$translator->trans("Pending")] += $item["value"];
else
$carry[$key][$translator->trans("Confirmed")] += $item["value"];
return $carry;
}, []);
asort($topResources);
$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'),2);
$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'),2);
$findResourceByDatesTime = $doctrine->getRepository(Resource::class)->findResourceByDatesTimes($request->request->get('startDate'), $request->request->get('endDate'),$request->request->get('localisation'), $request->request->get('resourceType'),$request->request->get('resource'));
$tauxOccupation = [];
$tauxOccupationByDate = [];
$resourceDispoTotal = 0;
$totalReservation = 0;
$interval = new \DateInterval('PT30M');
foreach ($findResourceByDatesTime 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
];
}
$indexDay = null;
foreach ($tauxOccupationByDate as $key => $value) {
if (
$value['date'] === $item['date'] &&
$value['hour'] === $currentHour->format('H:i')) {
$indexDay = $key;
break;
}
}
if ($indexDay) {
$tauxOccupationByDate[$indexDay]['resourceDispo'] += $item['count'];
}else {
$tauxOccupationByDate[] = [
'date' => $item['date'],
'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) {
$indexDay = null;
foreach ($tauxOccupationByDate as $key => $value) {
if ($value['date'] === $item['date'] && $value['hour'] === $item['hour']) {
$indexDay = $key;
break;
}
}
if ($indexDay != null) {
$tauxOccupationByDate[$indexDay]['reservation'] += $item['value'];
$tauxOccupationByDate[$indexDay]['reservation_reel'] += $item['value_reel'];
$tauxOccupationByDate[$indexDay]['tauxOccupation'] = round(($tauxOccupationByDate[$indexDay]['reservation'] * 100) / $tauxOccupationByDate[$indexDay]['resourceDispo'],2);
$tauxOccupationByDate[$indexDay]['tauxOccupationReal'] = round(($tauxOccupationByDate[$indexDay]['reservation_reel'] * 100) / $tauxOccupationByDate[$indexDay]['resourceDispo'],2);
}
$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'];
}
$tauxOccupationDesPickByDate = [];
foreach ($tauxOccupationByDate as $item) {
$date = (new \DateTime($item['date']))->format('Y-m-d');
if (in_array($date,array_column($tauxOccupationDesPickByDate,'date'))) {
$index = array_search($date,array_column($tauxOccupationDesPickByDate,'date'));
if ($tauxOccupationDesPickByDate[$index]['tauxOccupation'] < $item['tauxOccupation']) {
$tauxOccupationDesPickByDate[$index]['tauxOccupation'] = $item['tauxOccupation'];
$tauxOccupationDesPickByDate[$index]['tauxOccupationReal'] = $item['tauxOccupationReal'];
};
}else {
$tauxOccupationDesPickByDate[] = [
'date' => $date,
'tauxOccupation' => $item['tauxOccupation'],
'tauxOccupationReal' => $item['tauxOccupationReal'],
];
}
}
$tauxOccupationDesPickByDateTotal = 0;
foreach ($tauxOccupationDesPickByDate as $item) {
$tauxOccupationDesPickByDateTotal += $item['tauxOccupation'];
}
$tauxOccupationDesPickByDateTotal = round($tauxOccupationDesPickByDateTotal / sizeof($tauxOccupationDesPickByDate),2);
$tauxOccupationByWeekDay = [];
foreach ($tauxOccupationByDate as $item) {
$dayIndex = (new \DateTime($item['date']))->format('N');
$day = $weekDays[$dayIndex];
if (in_array($day,array_column($tauxOccupationByWeekDay,'day'))) {
$index = array_search($day,array_column($tauxOccupationByWeekDay,'day'));
$tauxOccupationByWeekDay[$index]['reservation'] += $item['reservation'];
$tauxOccupationByWeekDay[$index]['reservation_reel'] += $item['reservation_reel'];
$tauxOccupationByWeekDay[$index]['resourceDispo'] += $item['resourceDispo'];
}else {
$tauxOccupationByWeekDay[] = [
'dayIndex' => $dayIndex,
'day' => $day,
'reservation' => $item['reservation'],
'reservation_reel' => $item['reservation_reel'],
'resourceDispo' => $item['resourceDispo'],
'tauxOccupation' => 0,
'tauxOccupationReal' => 0
];
}
}
foreach ($tauxOccupationByWeekDay as $key => $value) {
$tauxOccupationByWeekDay[$key]['tauxOccupation'] = round(($value['reservation'] * 100) / $value['resourceDispo'],2);
$tauxOccupationByWeekDay[$key]['tauxOccupationReal'] = round(($value['reservation_reel'] * 100) / $value['resourceDispo'],2);
}
$openMatin = new \DateTime('09:00');
$closeMatin = new \DateTime('12:00');
$openAprem = new \DateTime('14:00');
$closeAprem = new \DateTime('17:00');
$tauxOccupationReal = array_filter($tauxOccupation,function($item) use ($openMatin, $closeMatin, $openAprem, $closeAprem) {
$hour = new \DateTime($item['hour']);
return ($hour >= $openMatin && $hour <= $closeMatin) || ($hour >= $openAprem && $hour <= $closeAprem);
});
$tauxOccupationTotal = 0;
$tauxOccupationRealTotal = 0;
$totalResourceDispo = 0;
$totalReservationDispo = 0;
foreach ($tauxOccupationReal as $obj) {
$totalReservationDispo += $obj['reservation'];
$totalResourceDispo += $obj['resourceDispo'];
}
$tauxOccupationRealTotal = round(($totalReservationDispo * 100) / $totalResourceDispo,2);
if ($resourceDispoTotal > 0)
$tauxOccupationTotal = round(($totalReservation * 100) / $resourceDispoTotal,2);
array_multisort(array_column($tauxOccupation, 'hour'), SORT_ASC, $tauxOccupation);
array_multisort(array_column($tauxOccupationReal, 'hour'), SORT_ASC, $tauxOccupationReal);
array_multisort(array_column($tauxOccupationByWeekDay, 'dayIndex'), SORT_ASC, $tauxOccupationByWeekDay);
array_multisort(array_column($tauxOccupationDesPickByDate, 'date'), SORT_ASC, $tauxOccupationDesPickByDate);
return new JsonResponse([
'reservationByDay' => $reservationByDay,
'topResources' => array_values($topResources),
'tauxOccupation' => $tauxOccupation,
'tauxOccupationByWeekDay' => $tauxOccupationByWeekDay,
'tauxOccupationDesPickByDate' => $tauxOccupationDesPickByDate,
'tauxOccupationDesPickByDateTotal' => $tauxOccupationDesPickByDateTotal,
'tauxOccupationReal' => $tauxOccupationReal,
'tauxOccupationRealTotal' => round($tauxOccupationRealTotal, 2),
'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,
]);
}
}