src/Controller/ClientController.php line 116

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use finfo;
  4. use App\Entity\Clients;
  5. use App\Service\Securizer;
  6. use App\Library\GraphOneDrive;
  7. use App\Repository\ClientsRepository;
  8. use App\Repository\ContactsRepository;
  9. use App\Repository\ContratsRepository;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Serializer\SerializerInterface;
  14. use Symfony\Component\Validator\Validator\ValidatorInterface;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  18. class ClientController extends AbstractController
  19. {
  20.     function calculateHotlineCompteur($clientContratsRepository $repoContrats)
  21.     {
  22.         $contratEnCours $repoContrats->getContratsActifBy($client->getId(), "Télémaintenance");
  23.         $sum 0;
  24.         foreach ($contratEnCours as $key => $contrat) {
  25.             $lignes $contrat->getLigneDeContrats();
  26.             for ($i 0$i count($lignes); $i++) {
  27.                 if ($lignes[$i]->getIdTypeIntervention()->gettype() === "Télémaintenance")
  28.                     $sum += $lignes[$i]->getQuantitePrevus() - $lignes[$i]->getQuantiteConsome();
  29.             }
  30.         }
  31.         $client->setHotlineCompteur($sum $client->getHotlineCompteur());
  32.         return $client;
  33.     }
  34.     /**
  35.      * @IsGranted("ROLE_CLIENT")
  36.      */
  37.     public function index(ClientsRepository $repoClientContratsRepository $repoContratsAccessDecisionManagerInterface $accessDecisionManager): Response
  38.     {
  39.         // Retourne tous les clients if->role_tech or seulement le meme client if->role_client
  40.         $securizer = new Securizer($accessDecisionManager);
  41.         $clients $repoClient->findClientTous($this->getUser(), $securizer);
  42.         foreach ($clients as $client) {
  43.             $client $this->calculateHotlineCompteur($client$repoContrats);
  44.         }
  45.         return $this->json($clients200, [], ['groups' => 'affichageClient']);
  46.     }
  47.     /**
  48.      * @IsGranted("ROLE_CLIENT")
  49.      */
  50.     public function actif(ClientsRepository $repoClientContratsRepository $repoContratsAccessDecisionManagerInterface $accessDecisionManager): Response
  51.     {
  52.         //voir la liste des clients actif
  53.         $securizer = new Securizer($accessDecisionManager);
  54.         $clients $repoClient->findClientActif($this->getUser(), $securizer);
  55.         foreach ($clients as $client) {
  56.             $client $this->calculateHotlineCompteur($client$repoContrats);
  57.         }
  58.         return $this->json($clients200, [], ['groups' => 'affichageClient']);
  59.     }
  60.     /**
  61.      * @IsGranted("ROLE_COMMERCIAL")
  62.      */
  63.     public function archiver(ClientsRepository $repoClientContratsRepository $repoContratsAccessDecisionManagerInterface $accessDecisionManager): Response
  64.     {
  65.         //voir la liste des clients archivé
  66.         $securizer = new Securizer($accessDecisionManager);
  67.         $clients $repoClient->findClientArchiver($this->getUser(), $securizer);
  68.         foreach ($clients as $client) {
  69.             $client $this->calculateHotlineCompteur($client$repoContrats);
  70.         }
  71.         return $this->json($clients200, [], ['groups' => 'affichageClient']);
  72.     }
  73.     /**
  74.      * @IsGranted("ROLE_CLIENT")
  75.      */
  76.     public function voir($idClientsRepository $repoClientContratsRepository $repoContratsAccessDecisionManagerInterface $accessDecisionManager): Response
  77.     {
  78.         //voir un client par $id
  79.         $securizer = new Securizer($accessDecisionManager);
  80.         //recupere l'id du ou des client qu'il est possible d'afficher
  81.         $clients $repoClient->findClientTous($this->getUser(), $securizer);
  82.         //verifi que le client que l'on cherche fait bien partie de la liste de ceux qu'il est possible d'afficher
  83.         if (in_array($repoClient->find($id), $clients)) {
  84.             $client $repoClient->find($id);
  85.             $client $this->calculateHotlineCompteur($client$repoContrats);
  86.         } else {
  87.             return $this->json([
  88.                 'status' => 400,
  89.                 'message' => "Vous ne pouvez pas consulter ce client"
  90.             ], 400);
  91.         }
  92.         return $this->json($client200, [], ['groups' => ['affichageClient''affichageInfoClient']]);
  93.     }
  94.     /**
  95.      * @IsGranted("ROLE_TECH")
  96.      */
  97.     public function searchClient($searchClientsRepository $repoClientContratsRepository $repoContratsAccessDecisionManagerInterface $accessDecisionManager): Response
  98.     {
  99.         $securizer = new Securizer($accessDecisionManager);
  100.         $clients $repoClient->globalSearch($search$this->getUser(), $securizer);
  101.         return $this->json($clients200, [], ['groups' => 'affichageClient']);
  102.     }
  103.     /**
  104.      * @IsGranted("ROLE_CLIENT")
  105.      */
  106.     public function logoClient(Request $request$idClientsRepository $repoClientContactsRepository $repoContactsEntityManagerInterface $managerAccessDecisionManagerInterface $accessDecisionManager): Response
  107.     {
  108.         $securizer = new Securizer($accessDecisionManager);
  109.         $client $repoClient->find($id);
  110.         if (!$securizer->isGranted($this->getUser(), "ROLE_TECH") &&  $client->getId() !== $repoContacts->findOneBy(["mail" => $this->getUser()->getUserIdentifier()])->getIdClient()->getId())
  111.             return $this->json([
  112.                 'status' => 400,
  113.                 'message' => "Vous ne pouvez modifier le logo de ce client"
  114.             ], 400);
  115.         //le dossier de destination du fichier telecharger
  116.         $destination $this->getParameter('kernel.project_dir') . '/public/img/logo_client';
  117.         //le fichier a telecharger
  118.         $img $request->files->get("myFile");
  119.         //tableau des extention acceptées
  120.         $extensions = array('.png''.gif''.jpg''.jpeg');
  121.         $extension strrchr($_FILES['myFile']['name'], '.');
  122.         if (!in_array($extension$extensions)) {
  123.             return $this->json([
  124.                 'status' => 400,
  125.                 'message' => "Vous ne pouvez uploder que des fichiers de type .png, .gif, .jpeg, .jpg"
  126.             ], 400);
  127.         }
  128.         //tableau des type MINE accepter
  129.         $mineTypes = array('image/png''image/gif''image/jpg''image/jpeg');
  130.         $finfo = new finfo(FILEINFO_MIME_TYPENULL);
  131.         $fileMineType $finfo->file($_FILES['myFile']['tmp_name']);
  132.         //renvoie un json d'erreur si le MINE type n'est pas dans le tableau
  133.         if (!in_array($fileMineType$mineTypes)) {
  134.             return $this->json([
  135.                 'status' => 400,
  136.                 'message' => "Vous ne pouvez uploder que des fichiers de type .png, .gif, .jpeg, .jpg"
  137.             ], 400);
  138.         }
  139.         //renomage du fichier pour qu'il soit unique sur la serveur
  140.         $newFilename uniqid() . '-' $img->getClientOriginalName();
  141.         //enregistrement du fichier dans le dossier de destination
  142.         $img->move($destination$newFilename);
  143.         $client->setLogo("img/logo_client/" $newFilename);
  144.         $manager->persist($client);
  145.         $manager->flush();
  146.         //json de retour pour envoi de l'url en base
  147.         return $this->json([
  148.             'status' => 200,
  149.             'url' => 'img' '/' $newFilename
  150.         ], 200);
  151.     }
  152.     /**
  153.      * @IsGranted("ROLE_COMMERCIAL")
  154.      */
  155.     public function creer(Request $requestValidatorInterface $validatorSerializerInterface $serializerEntityManagerInterface $manager): Response
  156.     {
  157.         //creer un nouveau client, seul les roles commercial et admin peuvent accer a cette url
  158.         $jsonRecu $request->getContent();
  159.         try {
  160.             $client $serializer->deserialize($jsonRecuClients::class, 'json');
  161.             $graph = new GraphOneDrive();
  162.             $idDrive "b!0B8l_7Yi_U2bP-TRWI8AGy-WjI76ekZLrXGgrRJbi4d9LH8_adGXT6XMhZMdfqIi";
  163.             $folderClient $graph->createFolderInDrive($idDrive$client->getNom());
  164.             $folderPhotos $graph->createDriveFolder($idDrive$folderClient->getId(), "photos");
  165.             $client->setGraphFolderId($folderPhotos->getId());
  166.             $errors $validator->validate($client);
  167.             if (count($errors) > 0)
  168.                 return $this->json($errors400);
  169.             $manager->persist($client);
  170.             $manager->flush();
  171.         } catch (\throwable $e) {
  172.             return $this->json([
  173.                 'status' => 400,
  174.                 'message' => $e->getMessage()
  175.             ], 400);
  176.         }
  177.         return $this->json($client201, [], ['groups' => 'affichageClient']);
  178.     }
  179.     /**
  180.      * @IsGranted("ROLE_COMMERCIAL")
  181.      */
  182.     public function modif($idRequest $requestClientsRepository $repoClientSerializerInterface $serializerEntityManagerInterface $manager): Response
  183.     {
  184.         //modifi un client existant, seul les roles commercial et admin peuvent accer a cette url
  185.         $client $repoClient->find($id);
  186.         if ($client == null) {
  187.             return $this->json([
  188.                 'status' => 400,
  189.                 'message' => "le client à modifier n'existe pas"
  190.             ], 400);
  191.         }
  192.         $jsonRecu json_decode($request->getContent(), true);
  193.         // effacer vars = null
  194.         foreach ($jsonRecu as $key => $value) {
  195.             if ($value === null) unset($jsonRecu[$key]);
  196.         }
  197.         unset($jsonRecu["commercialReferent"]);
  198.         unset($jsonRecu["contactReferent"]);
  199.         unset($jsonRecu["contacts"]);
  200.         if (isset($jsonRecu["hotlineCompteur"])) unset($jsonRecu["hotlineCompteur"]);
  201.         $jsonRecu json_encode($jsonRecu);
  202.         try {
  203.             $serializer->deserialize($jsonRecuClients::class, 'json', ['object_to_populate' => $client]);
  204.             $manager->persist($client);
  205.             $manager->flush();
  206.         } catch (\throwable $e) {
  207.             return $this->json([
  208.                 'status' => 400,
  209.                 'message' => $e->getMessage()
  210.             ], 400);
  211.         }
  212.         return $this->json($client201, [], ['groups' => ['affichageClient''affichageInfoClient']]);
  213.     }
  214.     /**
  215.      * @IsGranted("ROLE_COMMERCIAL")
  216.      */
  217.     public function setContactReferent($idRequest $requestClientsRepository $repoClientContactsRepository $repoContactsEntityManagerInterface $manager): Response
  218.     {
  219.         //modifi le contact referent de un client existant, seul les roles commercial et admin peuvent accer a cette url
  220.         $client $repoClient->find($id);
  221.         if ($client == null)
  222.             return $this->json([
  223.                 'status' => 400,
  224.                 'message' => "le client à modifier n'existe pas"
  225.             ], 400);
  226.         $jsonRecu json_decode($request->getContent());
  227.         $contact $repoContacts->findOneBy(["id" => $jsonRecu->contactId]);
  228.         try {
  229.             $client->setContactReferent($contact);
  230.             $manager->persist($client);
  231.             $manager->flush();
  232.         } catch (\throwable $e) {
  233.             return $this->json([
  234.                 'status' => 400,
  235.                 'message' => $e->getMessage()
  236.             ], 400);
  237.         }
  238.         return $this->json($client201, [], ['groups' => ['affichageClient''affichageInfoClient']]);
  239.     }
  240.     /**
  241.      * @IsGranted("ROLE_COMMERCIAL")
  242.      */
  243.     public function setCommercialRef($idRequest $requestClientsRepository $repoClientContactsRepository $repoContactsEntityManagerInterface $manager): Response
  244.     {
  245.         //modifi le comercial referent de un client existant, seul les roles commercial et admin peuvent accer a cette url
  246.         $client $repoClient->find($id);
  247.         if ($client == null)
  248.             return $this->json([
  249.                 'status' => 400,
  250.                 'message' => "le client à modifier n'existe pas"
  251.             ], 400);
  252.         $jsonRecu json_decode($request->getContent());
  253.         $commercial $repoContacts->findOneBy(["id" => $jsonRecu->commercialId]);
  254.         if ($commercial != null && !in_array("ROLE_COMMERCIAL"$commercial->getRoles())) {
  255.             return $this->json([
  256.                 'status' => 400,
  257.                 'message' => "Le contact à ajouter n’a pas de rôle commercial"
  258.             ], 400);
  259.         }
  260.         try {
  261.             $client->setCommercialReferent($commercial);
  262.             $manager->persist($client);
  263.             $manager->flush();
  264.         } catch (\throwable $e) {
  265.             return $this->json([
  266.                 'status' => 400,
  267.                 'message' => $e->getMessage()
  268.             ], 400);
  269.         }
  270.         return $this->json($client201, [], ['groups' => ['affichageClient''affichageInfoClient']]);
  271.     }
  272.     /**
  273.      * @IsGranted("ROLE_COMMERCIAL")
  274.      */
  275.     public function setTechnicienRef($idRequest $requestClientsRepository $repoClientContactsRepository $repoContactsEntityManagerInterface $manager): Response
  276.     {
  277.         //modifi le technicien referent de un client existant, seul les roles commercial et admin peuvent accer a cette url
  278.         $client $repoClient->find($id);
  279.         if ($client == null)
  280.             return $this->json([
  281.                 'status' => 400,
  282.                 'message' => "le client à modifier n'existe pas"
  283.             ], 400);
  284.         $jsonRecu json_decode($request->getContent());
  285.         $technicien $repoContacts->findOneBy(["id" => $jsonRecu->technicienId]);
  286.         if ($technicien != null && !in_array("ROLE_TECH"$technicien->getRoles())) {
  287.             return $this->json([
  288.                 'status' => 400,
  289.                 'message' => "Le contact à ajouter n’a pas de rôle technicien"
  290.             ], 400);
  291.         }
  292.         try {
  293.             $client->setTechnicienReferent($technicien);
  294.             $manager->persist($client);
  295.             $manager->flush();
  296.         } catch (\throwable $e) {
  297.             return $this->json([
  298.                 'status' => 400,
  299.                 'message' => $e->getMessage()
  300.             ], 400);
  301.         }
  302.         return $this->json($client201, [], ['groups' => ['affichageClient''affichageInfoClient']]);
  303.     }
  304.     /**
  305.      * @IsGranted("ROLE_COMMERCIAL")
  306.      */
  307.     public function hotlineCompteur(ClientsRepository $repoClientContratsRepository $repoContratsEntityManagerInterface $managerAccessDecisionManagerInterface $accessDecisionManager): Response
  308.     {
  309.         // permet de mettre à jour les valeurs de hotlineCompteur sur les contracts de Télémaintenance
  310.         $securizer = new Securizer($accessDecisionManager);
  311.         $clients $repoClient->findClientTous($this->getUser(), $securizer);
  312.         foreach ($clients as $client) {
  313.             $contratEnCours $repoContrats->getContratsActifBy($client->getId(), "Télémaintenance");
  314.             if (count($contratEnCours) > 0) {
  315.                 foreach ($contratEnCours[0]->getLigneDeContrats() as $ligne) {
  316.                     $compteur $client->getHotlineCompteur();
  317.                     $prevus $ligne->getQuantitePrevus();
  318.                     $consome $ligne->getQuantiteConsome();
  319.                     if ($compteur 0  && $ligne->getIdTypeIntervention()->getType() === "Télémaintenance" && $consome $prevus) {
  320.                         $ligne->setQuantiteConsome($consome $compteur  $prevus $prevus $consome $compteur);
  321.                         $client->setHotlineCompteur($consome $compteur  $prevus $consome $compteur $prevus 0);
  322.                         $manager->persist($ligne);
  323.                         $manager->persist($client);
  324.                     }
  325.                 }
  326.             }
  327.         }
  328.         $manager->flush();
  329.         return $this->json(true200, [], ['groups' => 'affichageClient']);
  330.     }
  331.     /**
  332.      * @IsGranted("ROLE_ADMIN")
  333.      */
  334.     public function setIdOneDrive(Request $requestClientsRepository $repoClientEntityManagerInterface $managerAccessDecisionManagerInterface $accessDecisionManager): Response
  335.     {
  336.         $securizer = new Securizer($accessDecisionManager);
  337.         if (!$securizer->isGranted($this->getUser(), "ROLE_ADMIN"))
  338.             return $this->json([
  339.                 'status' => 400,
  340.                 'message' => "Vous ne pouvez pas consulter cet élément"
  341.             ], 400);
  342.         $jsonRecu json_decode($request->getContent());
  343.         $client $repoClient->find($jsonRecu->idClient);
  344.         $idOneDrive $jsonRecu->idOneDrive;
  345.         if (!isset($client) || !isset($idOneDrive)) {
  346.             return $this->json([
  347.                 'status' => 400,
  348.                 'message' => "L'id du client ou l'id one drive ne sont pas correctes"
  349.             ], 400);
  350.         }
  351.         try {
  352.             $client->setGraphFolderId($idOneDrive);
  353.             $manager->persist($client);
  354.             $manager->flush();
  355.         } catch (\throwable $e) {
  356.             return $this->json([
  357.                 'status' => 400,
  358.                 'message' => $e->getMessage()
  359.             ], 400);
  360.         }
  361.         return $this->json(true200, []);
  362.     }
  363. }