src/Controller/ApiController.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\AppUser;
  4. use App\Entity\DemandeVacation;
  5. use App\Entity\Device;
  6. use App\Entity\Vacation;
  7. use App\Entity\VacationLine;
  8. use App\Utils\Consts;
  9. use App\Utils\Helpers;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Serializer\SerializerInterface;
  16. use Symfony\Component\Validator\Constraints\Date;
  17. /**
  18.  * @Route("/api")
  19.  *
  20.  */
  21. class ApiController extends AbstractController
  22. {
  23.     /**
  24.      * @Route("/", name="app_api")
  25.      */
  26.     public function index(Request $request): Response
  27.     {
  28.         $user $this->getUser();
  29.         return $this->json([
  30.             'user' => $user->getUsername(),
  31.             'token' => "s4",
  32.         ]);
  33.     }
  34.     /**
  35.      * @Route("/get_poste_infos", name="api_get_poste_infos")
  36.      */
  37.     public function getPosteInfos(Request $requestEntityManagerInterface $entityManagerSerializerInterface $serializer): Response
  38.     {
  39.         $user = new AppUser();//  $this->getUser()  ;
  40.         $requestContent json_decode($request->getContent());
  41.         $deviceUuid $requestContent->uuid;
  42.         $device $entityManager->getRepository(Device::class)->findOneBy(['uuid' => $deviceUuid]);
  43.         if ($device != null){
  44.             $deviceJson json_decode($serializer->serialize(
  45.                 $device,
  46.                 'json', ['groups' => [Consts::GROUPS_API]]
  47.             ));
  48.             $today = new \DateTime();
  49.             $vacationSeted true;
  50.             $demandesVacation $entityManager->getRepository(DemandeVacation::class)->createQueryBuilder('v')
  51.                 ->andWhere('v.poste = :poste')
  52.                 ->andWhere('v.demandeDateTime >= :date_start')
  53.                 ->andWhere('v.demandeDateTime <= :date_end')
  54.                 ->setParameter('date_start'$today->format('Y-m-d 00:00:00'))
  55.                 ->setParameter('date_end'$today->format('Y-m-d 23:59:59'))
  56.                 ->setParameter('poste'$device->getPoste())
  57.                 ->getQuery()
  58.                 ->getResult();
  59.             foreach ($demandesVacation as $demande) {
  60.                 $from = \DateTime::createFromFormat('Y-m-d H:i'$today->format("Y-m-d") . ' ' $demande->getHeureDebut());
  61.                 $to = \DateTime::createFromFormat('Y-m-d H:i'$today->format("Y-m-d") . ' ' $demande->getHeureFin());
  62.                 if (($today >= $from) && ($today <= $to)) {
  63.                     $demandeVacationCount $entityManager->getRepository(Vacation::class)->createQueryBuilder('v')
  64.                         ->select('count(v.id)')
  65.                         ->andWhere('v.poste = :poste')
  66.                         ->andWhere('v.vacationDateTime >= :date_start')
  67.                         ->andWhere('v.vacationDateTime <= :date_end')
  68.                         ->setParameter('date_start'$from)
  69.                         ->setParameter('date_end'$to)
  70.                         ->setParameter('poste'$device->getPoste())
  71.                         ->getQuery()
  72.                         ->getSingleScalarResult();
  73.                     $vacationSeted = ($demandeVacationCount 0);
  74.                     $vacationStart $demande->getHeureDebut();
  75.                     $vacationEnd $demande->getHeureFin();
  76.                 };
  77.             }
  78.             if ($vacationSeted) {
  79.                 $toDayVacationCount $entityManager->getRepository(Vacation::class)->createQueryBuilder('v')
  80.                     ->select('count(v.id)')
  81.                     ->andWhere('v.poste = :poste')
  82.                     ->andWhere('v.vacationDateTime >= :date_start')
  83.                     ->andWhere('v.vacationDateTime <= :date_end')
  84.                     ->setParameter('date_start'$today->format('Y-m-d 00:00:00'))
  85.                     ->setParameter('date_end'$today->format('Y-m-d 23:59:59'))
  86.                     ->setParameter('poste'$device->getPoste())
  87.                     ->getQuery()
  88.                     ->getSingleScalarResult();
  89.                 $vacationSeted = ($toDayVacationCount 0);
  90.                 $vacationStart '07:00';
  91.                 $vacationEnd '09:00';
  92.             }
  93.             return $this->json([
  94.                 'vacationSeted' => $vacationSeted,
  95.                 'vacationStart' => $vacationStart,
  96.                 'vacationEnd' => $vacationEnd,
  97.                 'deviceUuid' => $deviceUuid,
  98.                 'device' => $deviceJson,
  99.             ]);
  100.         }
  101.         return $this->json([
  102.             'device' => null,
  103.         ]);
  104.     }
  105.     /**
  106.      * @Route("/set_poste_vacation", name="api_set_poste_vacation")
  107.      */
  108.     public function setPosteVacation(Request $requestEntityManagerInterface $entityManagerSerializerInterface $serializer): Response
  109.     {
  110.         $user $this->getUser();
  111.         $requestContent json_decode($request->getContent());
  112.         $vacation = new Vacation();
  113.         $deviceUuid $requestContent->uuid;
  114.         $vacationValues $requestContent->values;
  115.         $device $entityManager->getRepository(Device::class)->findOneBy(['uuid' => $deviceUuid]);
  116.         $poste $device->getPoste();
  117.         $vacation = new Vacation();
  118.         $vacation->setPoste($poste);
  119.         $vacation->setVacationDateTime(new \DateTime());
  120.         $vacation->setCurrentPlace('draft');
  121.         foreach ($vacationValues as $field => $value) {
  122.             $vacationLine = new VacationLine();
  123.             $vacationLine->setVacation($vacation);
  124.             $vacationLine->setFieldCode($field);
  125.             $vacationLine->setFieldValue($value);
  126.             $entityManager->persist($vacationLine);
  127.         }
  128.         $entityManager->persist($vacation);
  129.         $entityManager->flush();
  130.         $Json json_decode($serializer->serialize(
  131.             $poste,
  132.             'json', ['groups' => [Consts::GROUPS_API]]
  133.         ));
  134.         return $this->json([
  135.             '$requestContent' => $requestContent,
  136.             '$Json' => $Json,
  137.         ]);
  138.     }
  139. }