<?php
namespace App\Controller;
use App\Entity\AppUser;
use App\Entity\DemandeVacation;
use App\Entity\Device;
use App\Entity\Vacation;
use App\Entity\VacationLine;
use App\Utils\Consts;
use App\Utils\Helpers;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Constraints\Date;
/**
* @Route("/api")
*
*/
class ApiController extends AbstractController
{
/**
* @Route("/", name="app_api")
*/
public function index(Request $request): Response
{
$user = $this->getUser();
return $this->json([
'user' => $user->getUsername(),
'token' => "s4",
]);
}
/**
* @Route("/get_poste_infos", name="api_get_poste_infos")
*/
public function getPosteInfos(Request $request, EntityManagerInterface $entityManager, SerializerInterface $serializer): Response
{
$user = new AppUser();// $this->getUser() ;
$requestContent = json_decode($request->getContent());
$deviceUuid = $requestContent->uuid;
$device = $entityManager->getRepository(Device::class)->findOneBy(['uuid' => $deviceUuid]);
if ($device != null){
$deviceJson = json_decode($serializer->serialize(
$device,
'json', ['groups' => [Consts::GROUPS_API]]
));
$today = new \DateTime();
$vacationSeted = true;
$demandesVacation = $entityManager->getRepository(DemandeVacation::class)->createQueryBuilder('v')
->andWhere('v.poste = :poste')
->andWhere('v.demandeDateTime >= :date_start')
->andWhere('v.demandeDateTime <= :date_end')
->setParameter('date_start', $today->format('Y-m-d 00:00:00'))
->setParameter('date_end', $today->format('Y-m-d 23:59:59'))
->setParameter('poste', $device->getPoste())
->getQuery()
->getResult();
foreach ($demandesVacation as $demande) {
$from = \DateTime::createFromFormat('Y-m-d H:i', $today->format("Y-m-d") . ' ' . $demande->getHeureDebut());
$to = \DateTime::createFromFormat('Y-m-d H:i', $today->format("Y-m-d") . ' ' . $demande->getHeureFin());
if (($today >= $from) && ($today <= $to)) {
$demandeVacationCount = $entityManager->getRepository(Vacation::class)->createQueryBuilder('v')
->select('count(v.id)')
->andWhere('v.poste = :poste')
->andWhere('v.vacationDateTime >= :date_start')
->andWhere('v.vacationDateTime <= :date_end')
->setParameter('date_start', $from)
->setParameter('date_end', $to)
->setParameter('poste', $device->getPoste())
->getQuery()
->getSingleScalarResult();
$vacationSeted = ($demandeVacationCount > 0);
$vacationStart = $demande->getHeureDebut();
$vacationEnd = $demande->getHeureFin();
};
}
if ($vacationSeted) {
$toDayVacationCount = $entityManager->getRepository(Vacation::class)->createQueryBuilder('v')
->select('count(v.id)')
->andWhere('v.poste = :poste')
->andWhere('v.vacationDateTime >= :date_start')
->andWhere('v.vacationDateTime <= :date_end')
->setParameter('date_start', $today->format('Y-m-d 00:00:00'))
->setParameter('date_end', $today->format('Y-m-d 23:59:59'))
->setParameter('poste', $device->getPoste())
->getQuery()
->getSingleScalarResult();
$vacationSeted = ($toDayVacationCount > 0);
$vacationStart = '07:00';
$vacationEnd = '09:00';
}
return $this->json([
'vacationSeted' => $vacationSeted,
'vacationStart' => $vacationStart,
'vacationEnd' => $vacationEnd,
'deviceUuid' => $deviceUuid,
'device' => $deviceJson,
]);
}
return $this->json([
'device' => null,
]);
}
/**
* @Route("/set_poste_vacation", name="api_set_poste_vacation")
*/
public function setPosteVacation(Request $request, EntityManagerInterface $entityManager, SerializerInterface $serializer): Response
{
$user = $this->getUser();
$requestContent = json_decode($request->getContent());
$vacation = new Vacation();
$deviceUuid = $requestContent->uuid;
$vacationValues = $requestContent->values;
$device = $entityManager->getRepository(Device::class)->findOneBy(['uuid' => $deviceUuid]);
$poste = $device->getPoste();
$vacation = new Vacation();
$vacation->setPoste($poste);
$vacation->setVacationDateTime(new \DateTime());
$vacation->setCurrentPlace('draft');
foreach ($vacationValues as $field => $value) {
$vacationLine = new VacationLine();
$vacationLine->setVacation($vacation);
$vacationLine->setFieldCode($field);
$vacationLine->setFieldValue($value);
$entityManager->persist($vacationLine);
}
$entityManager->persist($vacation);
$entityManager->flush();
$Json = json_decode($serializer->serialize(
$poste,
'json', ['groups' => [Consts::GROUPS_API]]
));
return $this->json([
'$requestContent' => $requestContent,
'$Json' => $Json,
]);
}
}