src/Controller/ActionsController.php line 77

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use DateTime;
  4. use App\Entity\Actions;
  5. use App\Service\Securizer;
  6. use App\Entity\Intervention;
  7. use App\Repository\StatusRepository;
  8. use App\Repository\ActionsRepository;
  9. use App\Repository\ClientsRepository;
  10. use App\Repository\TicketsRepository;
  11. use App\Repository\ContactsRepository;
  12. use App\Repository\ContratsRepository;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use App\Repository\InterventionRepository;
  15. use App\Repository\LigneDeContratRepository;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Serializer\SerializerInterface;
  19. use Symfony\Component\Validator\Validator\ValidatorInterface;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  23. class ActionsController extends AbstractController
  24. {
  25.     /**
  26.      * @IsGranted("ROLE_USER")
  27.      */
  28.     public function index(AccessDecisionManagerInterface $accessDecisionManagerActionsRepository $repoActionContactsRepository $repoContactTicketsRepository $repoTicketInterventionRepository $repoInterventionContratsRepository $repoContratLigneDeContratRepository $repoLigneContrat): Response
  29.     {
  30.         //retourne tous les tickets
  31.         $securizer = new Securizer($accessDecisionManager);
  32.         $actions $repoAction->findActionId($this->getUser(), $securizer$repoTicket$repoContact$repoIntervention$repoContrat$repoLigneContrat);
  33.         return $this->json($actions200, [], ['groups' => 'affichageAction']);
  34.     }
  35.     /**
  36.      * @IsGranted("ROLE_USER")
  37.      */
  38.     public function voir($idActionsRepository $repoActionContactsRepository $repoContactAccessDecisionManagerInterface $accessDecisionManagerTicketsRepository $repoTicketInterventionRepository $repoInterventionContratsRepository $repoContratLigneDeContratRepository $repoLigneContrat): Response
  39.     {
  40.         //retourne une action
  41.         $securizer = new Securizer($accessDecisionManager);
  42.         $actions $repoAction->findActionId($this->getUser(), $securizer$repoTicket$repoContact$repoIntervention$repoContrat$repoLigneContrat);
  43.         //verifi que l'intervention que l'on cherche fait bien partie de la liste de ceux qu'il est possible d'afficher
  44.         if (in_array($repoAction->find($id), $actions)) {
  45.             $action $repoAction->find($id);
  46.         } else {
  47.             return $this->json([
  48.                 'status' => 400,
  49.                 'message' => "Vous ne pouvez pas consulter cette action"
  50.             ], 400);
  51.         }
  52.         return $this->json($action200, [], ['groups' => 'affichageAction']);
  53.     }
  54.     /**
  55.      * @IsGranted("ROLE_TECH")
  56.      */
  57.     public function voirActionsIntervention($idActionsRepository $repoAction): Response
  58.     {
  59.         //retourne actions par rapport a une intervention
  60.         $actions $repoAction->findBy(["intervention" => $id]);
  61.         return $this->json($actions200, [], ['groups' => 'affichageAction']);
  62.     }
  63.     /**
  64.      * @IsGranted("ROLE_CLIENT")
  65.      */
  66.     public function voirActionsTicket($idActionsRepository $repoAction): Response
  67.     {
  68.         //retourne actions par rapport a une intervention
  69.         $actions $repoAction->findBy(["ticket" => $id]);
  70.         return $this->json($actions200, [], ['groups' => 'affichageAction']);
  71.     }
  72.     /**
  73.      * @IsGranted("ROLE_TECH")
  74.      */
  75.     public function creer(Request $requestValidatorInterface $validatorSerializerInterface $serializerEntityManagerInterface $managerStatusRepository $repoStatus): Response
  76.     {
  77.         //creer une nouvelle action, seul les role Tech ou superieur peuvent acceder a cette page
  78.         $jsonRecu $request->getContent();
  79.         try {
  80.             //transforme le json reçu en entity
  81.             $action $serializer->deserialize($jsonRecuActions::class, 'json');
  82.             if ($action->getTicket() == null && $action->getIntervention() == null) {
  83.                 return $this->json([
  84.                     'status' => 400,
  85.                     'message' => "Vous devez choisir un ticket ou une intervention"
  86.                 ], 400);
  87.             } else {
  88.                 $jsonRecu json_decode($jsonRecutrue);
  89.                 $action->setIntervenant($this->getUser());
  90.                 //validation des données reçus
  91.                 $errors $validator->validate($action);
  92.                 if (count($errors) > 0) {
  93.                     return $this->json($errors400);
  94.                 }
  95.                 $manager->persist($action);
  96.                 $manager->flush();
  97.                 $ticket $action->getTicket();
  98.                 /** @var Intervention $intervention */
  99.                 $intervention $action->getIntervention();
  100.                 if ($ticket != null) {
  101.                     if ($jsonRecu["idStatus"] !== $ticket->getIdStatus()->getId()) {
  102.                         $status $repoStatus->findOneBy(['id' => (int) $jsonRecu["idStatus"]]);
  103.                         $ticket->setIdStatus($status);
  104.                         $action->setStatusChanged(true);
  105.                         if ($status->getStatus() == "Termine") {
  106.                             $client $ticket->getBeneficiaire()->getIdClient();
  107.                             $client->setHotlineCompteur($client->getHotlineCompteur() + $ticket->getDureeTotale());
  108.                             $manager->persist($client);
  109.                         }
  110.                         $manager->persist($ticket);
  111.                     } else if ($ticket->getIdStatus()->getStatus() == "Nouveau") {
  112.                         $ticket->setIdStatus($repoStatus->findOneBy(['status' => 'En Cours']));
  113.                         $manager->persist($ticket);
  114.                     }
  115.                 }
  116.                 if ($intervention != null && ($intervention->getIdStatus()->getStatus() == "Programmer" || $jsonRecu["cloturer"])) {
  117.                     $status = ($jsonRecu["cloturer"] ? 'Cloture' 'En Cours');
  118.                     $intervention->setIdStatus($repoStatus->findOneBy(['status' => $status]));
  119.                     $action->setStatusChanged(true);
  120.                     $manager->persist($intervention);
  121.                     if ($status == "Cloture") {
  122.                         // Si l'intervention est cloturée, on cloture tous les tickets associés
  123.                         foreach ($intervention->getActions() as $action) {
  124.                             $ticket $action->getTicket();
  125.                             if ($ticket != null) {
  126.                                 $ticket->setIdStatus($repoStatus->findOneBy(['status' => 'Cloture']));
  127.                                 $manager->persist($ticket);
  128.                             }
  129.                         }
  130.                     }
  131.                 }
  132.                 $manager->flush();
  133.             }
  134.         } catch (\throwable $e) {
  135.             return $this->json([
  136.                 'status' => 400,
  137.                 'message' => $e->getMessage()
  138.             ], 400);
  139.         }
  140.         return $this->json($action201, [], ['groups' => 'affichageAction']);
  141.     }
  142.     /**
  143.      * @IsGranted("ROLE_COMMERCIAL")
  144.      */
  145.     public function modif($idRequest $requestActionsRepository $repoActionValidatorInterface $validatorSerializerInterface $serializerEntityManagerInterface $manager): Response
  146.     {
  147.         //modifier une action, seul les role Commercial ou superieur peuvent acceder a cette page
  148.         $action $repoAction->find($id);
  149.         $interventionDepart $action->getIntervention();
  150.         $ticketDepart $action->getTicket();
  151.         if ($action == null) {
  152.             return $this->json([
  153.                 'status' => 400,
  154.                 'message' => "l'action à modifier n'existe pas"
  155.             ], 400);
  156.         }
  157.         $jsonRecu $request->getContent();
  158.         try {
  159.             //transforme le json reçu en entity
  160.             $serializer->deserialize($jsonRecuActions::class, 'json', ['object_to_populate' => $action]);
  161.             if ($action->getTicket() == $ticketDepart && $action->getIntervention() == $interventionDepart) {
  162.                 $errors $validator->validate($action);
  163.                 if (count($errors) > 0) {
  164.                     return $this->json($errors400);
  165.                 }
  166.                 $manager->persist($action);
  167.                 $manager->flush();
  168.             } else {
  169.                 return $this->json([
  170.                     'status' => 400,
  171.                     'message' => "Vous ne pouvez pas modifier le ticket ou l'intervention d'une action"
  172.                 ], 400);
  173.             }
  174.         } catch (\throwable $e) {
  175.             return $this->json([
  176.                 'status' => 400,
  177.                 'message' => $e->getMessage()
  178.             ], 400);
  179.         }
  180.         return $this->json($action201, [], ['groups' => 'affichageAction']);
  181.     }
  182.     /**
  183.      * @IsGranted("ROLE_TECH")
  184.      */
  185.     public function delete($idActionsRepository $repoActionStatusRepository $repoStatusAccessDecisionManagerInterface $accessDecisionManagerTicketsRepository $repoTicketInterventionRepository $repoInterventionContratsRepository $repoContratLigneDeContratRepository $repoLigneContrat): Response
  186.     {
  187.         //Permets de supprimer une intervention
  188.         $action $repoAction->find($id);
  189.         if (!isset($action)) {
  190.             return $this->json([
  191.                 'status' => 400,
  192.                 'message' => "l'action à supprimer n'existe pas"
  193.             ], 400);
  194.         }
  195.         $ticket $action->getTicket();
  196.         $intervention $action->getIntervention();
  197.         if (isset($ticket) || isset($intervention)) {
  198.             $status $repoStatus->findBy(["status" => ["Termine""Cloture"]]);
  199.             if (in_array((isset($ticket) ? $ticket $intervention)->getIdStatus(), $status))
  200.                 return $this->json([
  201.                     'status' => 400,
  202.                     'message' => "l'action ne peut pas être supprimé car l'intervention ou le ticket n'est pas 'En Cours'"
  203.                 ], 400);
  204.         }
  205.         $repoAction->remove($action);
  206.         return $this->json(true200, [], ['groups' => 'affichageAction']);
  207.     }
  208. }