src/Controller/DemandeServiceController.php line 505

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Controller\Admin\DemandeAffectationCrudController;
  4. use App\Controller\Admin\DemandeNoteCrudController;
  5. use App\Controller\Admin\DemandeServiceCrudController;
  6. use App\Entity\DemandeAffectation;
  7. use App\Entity\DemandeNote;
  8. use App\Entity\DemandeService;
  9. use App\Entity\DemandeServiceCandidat;
  10. use App\Entity\DocDemandeClient;
  11. use App\Entity\FormationsService;
  12. use App\Entity\InterventionEntrepriseService;
  13. use App\Entity\ProgrammeFormationService;
  14. use App\Entity\Service;
  15. use App\Entity\User;
  16. use App\Form\DemandeServiceType;
  17. use App\Form\DocDemandeClientType;
  18. use App\Model\Globals;
  19. use App\Service\FileUploader;
  20. use App\Service\NotificationManager;
  21. use Doctrine\ORM\EntityManagerInterface;
  22. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  23. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  24. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  25. use Symfony\Component\HttpFoundation\RequestStack;
  26. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  27. use ZipArchive;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\HttpFoundation\Response;
  30. use Symfony\Component\Routing\Annotation\Route;
  31. class DemandeServiceController extends AbstractController
  32. {
  33.     /**
  34.      * @var EntityManagerInterface
  35.      */
  36.     protected $em;
  37.     /**
  38.      * @var RequestStack
  39.      */
  40.     private $request;
  41.     /**
  42.      * @var FileUploader
  43.      */
  44.     protected $fileUploader;
  45.     /**
  46.      * @var AdminUrlGenerator
  47.      */
  48.     private $adminUrlGenerator;
  49.     private $notificationManager;
  50.     /**
  51.      * @param EntityManagerInterface $em
  52.      */
  53.     public function __construct(EntityManagerInterface $emFileUploader $fileUploader
  54.     RequestStack $requestAdminUrlGenerator $adminUrlGeneratorNotificationManager $notificationManager)
  55.     {
  56.         $this->em $em;
  57.         $this->request $request;
  58.         $this->fileUploader $fileUploader;
  59.         $this->adminUrlGenerator $adminUrlGenerator;
  60.         $this->notificationManager $notificationManager;
  61.     }
  62.     /**
  63.      * @param int $serviceId
  64.      * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
  65.      */
  66.     #[Route('/new/demande/{serviceId}/{demandeId}'name'new_demande')]
  67.     public function newDemande(int $serviceId,EntityManagerInterface $entityManager,
  68.      int $demandeIdRequest $requestNotificationManager $notificationManager UrlGeneratorInterface $urlGenerator)
  69.     {
  70.         if (null === $this->getUser()) {
  71.             return $this->redirectToRoute('login');
  72.         }
  73.         $service $this->em->getRepository(Service::class)->find($serviceId);
  74.         $dataOptions['attr_translation_parameters']['service'] = $service;
  75.         $dataOptions['attr_translation_parameters']['user'] = $this->getUser();
  76.         $demandeServiceData = ($demandeId === 0) ? new DemandeService() : $this->em->getRepository(DemandeService::class)->find($demandeId) ;
  77.         if ( ( true === $demandeServiceData->isIsVerified() ) && !== $demandeId ) {
  78.             $this->addFlash('error''Votre demande a été bien reçue');
  79.             return $this->redirectToRoute('app_list_demande');
  80.         }
  81.         foreach ($this->getUser()->getAdresses() as $value) {
  82.             $address $value;
  83.         }
  84.         $form $this->createForm(DemandeServiceType::class, $demandeServiceData$dataOptions);
  85.         $form->handleRequest($request);
  86.         if ($form->isSubmitted() && $form->isValid()) {
  87.             $demandeService $form->getData();
  88.             $demandeService->setIsDeleted(false);
  89.             $demandeService->setIsVerified(false);
  90.             $demandeService->setIsValid(false);
  91.             $demandeService->setEnabled(true);
  92.             $demandeService->setAcceptDocuments(false);
  93.             if ($demandeId === 0) {
  94. //                if ($service instanceof InterventionEntrepriseService) {
  95.                 $demandeService->setEtat(Globals::DEMANDE_ETAT_EN_COURS);
  96. //                } else {
  97. //                    $demandeService->setEtat(Globals::DEMANDE_ETAT_ATTENTE_VALIDATION);
  98. //                }
  99.                 $demandeService->setCreatedBy($this->getUser()->getId());
  100.                 $demandeService->setService($service);
  101.                 if( $demandeService->getPrestation() ){
  102.                     $demandeService->setRubrique($demandeService->getPrestation()->getRubrique());
  103.                 }elseif( $service->getRubriques() ){
  104.                     $demandeService->setRubrique$service->getRubriques()[0] );
  105.                     $demandeService->setPrestation$service->getRubriques()[0]->getPrestations()[0] );
  106.                 }
  107.                 $demandeService->setUser($this->getUser());
  108.             } else {
  109.                 $demandeService->setUpdatedBy($this->getUser()->getId());
  110.                 $demandeService->setUpdatedAt(new \DateTime());
  111.             }
  112.             if ($service instanceof FormationsService) {
  113.                 $demandeService->setProcessType(Globals::FORMATION_PROCESS_TYPE);
  114.             }
  115.             $this->em->persist($demandeService);
  116.             $this->em->flush();
  117.             if ($service instanceof FormationsService) {
  118.                 $demandeServiceCandidatData $this->em->getRepository(DemandeServiceCandidat::class)->findBy(['demandeService' => $demandeService]);
  119.                 if (count($demandeServiceCandidatData)) {
  120.                     foreach ($demandeServiceCandidatData as $candidat) {
  121.                         $this->em->getRepository(DemandeServiceCandidat::class)->remove($candidattrue);
  122.                     }
  123.                 }
  124.                 if (true === array_key_exists('candidats'$this->request->getCurrentRequest()->request->all())) {
  125.                
  126.                     $candidats $this->request->getCurrentRequest()->request->all()['candidats'];
  127.                     $demandeServiceCandidat = [];
  128.                     foreach ($candidats['name'] as $i => $name) {
  129.                         $demandeServiceCandidat[$i] = new DemandeServiceCandidat();
  130.                         $demandeServiceCandidat[$i]->setNomPrenom($name);
  131.                         $demandeServiceCandidat[$i]->setDemandeService($demandeService);
  132.                         $demandeServiceCandidat[$i]->setDeleted(false);
  133.                         $demandeServiceCandidat[$i]->setCreatedBy($this->getUser()->getId());
  134.                         $demandeServiceCandidat[$i]->setCreatedAt(new \DateTime());
  135.                     }
  136.                     foreach ($candidats['email'] as $i => $email) {
  137.                         $demandeServiceCandidat[$i]->setEmail($email);
  138.                     }
  139.                     foreach ($candidats['niveauInstruction'] as $i => $niveauInstruction) {
  140.                         $demandeServiceCandidat[$i]->setNiveauInstruction($niveauInstruction);
  141.                     }
  142.                     foreach ($candidats['affectation'] as $i => $aff) {
  143.                         $demandeServiceCandidat[$i]->setAffectation($aff);
  144.                     }
  145.                     foreach ($candidats['anciennete'] as $i => $anciennete) {
  146.                         $demandeServiceCandidat[$i]->setAnciennete($anciennete);
  147.                         $this->em->persist($demandeServiceCandidat[$i]);
  148.                         $this->em->flush();
  149.                     }
  150.                 }
  151.             }
  152.             $this->addFlash('success''Demande enregistrée avec succès');
  153.             // Send Comment
  154.             $url $this->adminUrlGenerator
  155.             ->setController(DemandeServiceCrudController::class)
  156.             ->setAction(Action::INDEX)
  157.             ->generateUrl();
  158.        
  159.             // Variables définies pour l'URL
  160.             $encodedReferrerUrl urlencode($url);
  161.             $crudControllerFqcn 'App\Controller\Admin\DemandeServiceCrudController';
  162.             $entityFqcn 'App\Entity\DemandeService';
  163.             $filterComparison '=';
  164.             $filterValue $demandeService->getId();
  165.             $filterValue2 ''// Assuming this is an empty value
  166.             // Génération de l'URL avec les paramètres supplémentaires
  167.             $edit_demande_url $this->adminUrlGenerator
  168.                 ->setController($crudControllerFqcn)
  169.                 ->setAction(Action::INDEX)
  170.                 ->set('referrer'$encodedReferrerUrl)
  171.                 ->set('crudAction''index')
  172.                 ->set('crudControllerFqcn'$crudControllerFqcn)
  173.                 ->set('entityFqcn'$entityFqcn)
  174.                 ->set('filters[id][comparison]'$filterComparison)
  175.                 ->set('filters[id][value]'$filterValue)
  176.                 ->set('filters[id][value2]'$filterValue2)
  177.                 ->generateUrl();
  178.             $notification_message 'Mise à jour de demande  ';
  179.             $repository $entityManager->getRepository(User::class);
  180.             $queryBuilder $repository->createQueryBuilder('u');
  181.             $daes $entityManager->getRepository(User::class)->findByRole('ROLE_DAE');
  182.             $CAs $entityManager->getRepository(User::class)->findByRole('ROLE_CHARGE_ASSISTANCE');
  183.              foreach($daes as $dae) {
  184.                 $notificationManager->pushMessage($notification_message$notification_message$dae$edit_demande_url);
  185.             }
  186.             foreach($CAs as $ca) {
  187.                  if($ca->getUniteRegional()->getAdresse()->getGouvernorat() == $demandeService->getGouvernorat() ){
  188.                     $notificationManager->pushMessage($notification_message$notification_message$ca$edit_demande_url);
  189.                  }
  190.             }
  191.             
  192.             // End send comment
  193.             return $this->redirectToRoute('app_list_demande');
  194.         }
  195.         return $this->render('service/demande/new.html.twig', [
  196.             'user' => $this->getUser(),
  197.             'userRole' => $this->getUser()->getRoles(),
  198.             'service' => $service,
  199.             'isInstanceofFormation' => ($service instanceof FormationsService),
  200.             'isInstanceOfInterventionEntreprise' => ($service instanceof InterventionEntrepriseService),
  201.             'isInstanceOfProgrammeFormation' => ($service instanceof ProgrammeFormationService),
  202.             'form' => $form->createView(),
  203.             'demandeId' => $demandeId,
  204.             'address' => $address,
  205.             'etatRefus' => Globals::DEMANDE_ETAT_REFUSER,
  206.             'demande' => $demandeServiceData,
  207.             'show' => false
  208.         ]);
  209.     }
  210.    /**
  211.      * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
  212.      */
  213.     #[Route('/comment/demande/{demandeId}'name'comment_demande')]
  214.     public function listCommentDemandeint $demandeIdRequest $request EntityManagerInterface $entityManager)
  215.     {
  216.         
  217.         $listCommentDemande $this->em->getRepository(DemandeNote :: class)->findBy(['demande' => $demandeId]);
  218.         if (null === $this->getUser()) {
  219.             return $this->redirectToRoute('login');
  220.         }
  221.       
  222.         $referrerUrl $this->adminUrlGenerator
  223.     ->setController(DemandeServiceCrudController::class)
  224.     ->setAction('index')
  225.     ->set('entityFqcn''App\\Entity\\DemandeService')
  226.     ->generateUrl();
  227.     $edit_demande_url $this->adminUrlGenerator
  228.         ->setController(DemandeNoteCrudController::class)
  229.         ->setAction('index')
  230.         ->set('demandeServiceId'$demandeId)
  231.         ->set('referrer'$referrerUrl)
  232.         ->generateUrl();
  233.         $notification_message 'Nouvelle Note / Note modifiée !   ';
  234.         $currentUser $this->getUser();
  235.         $admins $entityManager->getRepository(User::class)
  236.             ->createQueryBuilder('u')
  237.             ->where('u != :currentUser')
  238.             ->setParameter('currentUser'$currentUser)
  239.             ->andWhere('u.roles LIKE :role1 OR u.roles LIKE :role2 ')
  240.             ->setParameter('role1''%ROLE_CHARGE_ASSISTANCE%')
  241.             ->setParameter('role2''%ROLE_DAE%')
  242.             ->getQuery()
  243.             ->getResult();
  244.             $listAffectations $entityManager->getRepository(DemandeAffectation::class)->findBy(['demandeService'=> $demandeId]);
  245.         foreach ($admins as $admin) {
  246.             $this->notificationManager->pushMessage('Nouvelle Note / Note modifiée! '$notification_message$admin$edit_demande_url);
  247.         }
  248.             return $this->render('service/demande/note_list.html.twig', [
  249.                 'listCommentDemandes' => $listCommentDemande,
  250.                 'demandeId' => $demandeId
  251.             ]);
  252.         
  253.     }
  254.   
  255.     
  256.     /**
  257.      * @return \Symfony\Component\HttpFoundation\Response
  258.      */
  259.     #[Route('/demande/list'name'app_list_demande')]
  260.     public function listDemande()
  261.     {
  262.         $demandesDates = [];
  263.         $demandes $this->em->getRepository(DemandeService::class)->findBy(['user' => $this->getUser(), 'isDeleted' => false]);
  264.         foreach( $demandes as $demande ){
  265.             $dateDebut null;
  266.             $dateFin null;
  267.             if( $demande->getGroupeDemande() ){
  268.                 $demandesModules $this->em->getRepository(DemandeService::class)->getDemandesModuleOfGroupe$demande->getGroupeDemande()->getId() );
  269.                 dump$demande->getId(), $demandesModules);
  270.                 foreach ( $demandesModules as $demandeModule ){
  271.                     if ( $demandeModule->getTachExecutionDateDebut() and $dateDebut == null){
  272.                         $dateDebut $demandeModule->getTachExecutionDateDebut();
  273.                     }
  274.                     $dateFin $demandeModule->getTachExecutionDateFin();
  275.                 }
  276.             }
  277.             $demandesDates$demande->getId() ] = [
  278.                 'dateDebut' => $dateDebut,
  279.                 'dateFin' => $dateFin,
  280.             ];
  281.         }
  282.         return $this->render('service/demande/list.html.twig', [
  283.             'demandes' => $demandes,
  284.             'demandesDates' => $demandesDates,
  285.             'etatDocValid' => Globals::DOC_ETAT_VALIDE,
  286.             'etatEncours' => Globals::DEMANDE_ETAT_EN_COURS,
  287.             'etatEnAttente' => Globals::DEMANDE_ETAT_ATTENTE_VALIDATION,
  288.             'etatRefuser' => Globals::DEMANDE_ETAT_REFUSER,
  289.             'etatCloturer' => Globals::DEMANDE_ETAT_CLOTURER,
  290.         ]);
  291.     }
  292.     /**
  293.      * @param int $id
  294.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  295.      */
  296.     #[Route('/demande/delete/{id}'name'app_delete_demande')]
  297.     public function deleteDemande(int $id)
  298.     {
  299.         $demande $this->em->getRepository(DemandeService::class)->find($id);
  300.         if ( true === $demande->isIsVerified() ) {
  301.             $this->addFlash('error''Votre demande a été bien reçue');
  302.             return $this->redirectToRoute('app_list_demande');
  303.         }
  304.         $demande->setIsDeleted(true);
  305.         $demande->setDeletedAt(new \DateTime());
  306.         $this->em->persist($demande);
  307.         $this->em->flush();
  308.         $edit_demande_url $this->adminUrlGenerator
  309.         ->setController(DemandeServiceCrudController::class)
  310.         ->setAction(Action::INDEX)
  311.         ->generateUrl();
  312.         $notification_message 'Demande supprimée!   ';
  313.         $currentUser $this->getUser();
  314.         $admins $this->em->getRepository(User::class)
  315.             ->createQueryBuilder('u')
  316.             ->where('u != :currentUser')
  317.             ->setParameter('currentUser'$currentUser)
  318.             ->andWhere('u.roles LIKE :role1 OR u.roles LIKE :role2 ')
  319.             ->setParameter('role1''%ROLE_CHARGE_ASSISTANCE%')
  320.             ->setParameter('role2''%ROLE_DAE%')
  321.             ->getQuery()
  322.             ->getResult();
  323.         foreach ($admins as $admin) {
  324.             $this->notificationManager->pushMessage('Demande supprimée ! '$notification_message$admin$edit_demande_url);
  325.         }
  326.         $this->addFlash('success''Demande supprimé avec succès');
  327.         return $this->redirectToRoute('app_list_demande');
  328.     }
  329.     /**
  330.      * @param int $id
  331.      * @return \Symfony\Component\HttpFoundation\Response
  332.      */
  333.     #[Route('/demande/show/{id}'name'app_show_demande')]
  334.     public function showDemande(int $id)
  335.     {
  336.         $demandeServiceData $this->em->getRepository(DemandeService::class)->find($id);
  337.         $dataOptions['attr_translation_parameters']['service'] = $demandeServiceData->getService();
  338.         $dataOptions['attr_translation_parameters']['user'] = $demandeServiceData->getUser();
  339.         $dataOptions['row_attr']['show'] = true;
  340.         $form $this->createForm(DemandeServiceType::class, $demandeServiceData$dataOptions);
  341.         return $this->render('service/demande/new.html.twig', [
  342.             'user' => $demandeServiceData->getUser(),
  343.             'service' => $demandeServiceData->getService(),
  344.             'isInstanceofFormation' => ($demandeServiceData->getService() instanceof FormationsService),
  345.             'isInstanceOfInterventionEntreprise' => ($demandeServiceData->getService() instanceof InterventionEntrepriseService),
  346.             'isInstanceOfProgrammeFormation' => ($demandeServiceData->getService() instanceof ProgrammeFormationService),
  347.             'form' => $form->createView(),
  348.             'demandeId' => $demandeServiceData->getId(),
  349.             'demande' => $demandeServiceData,
  350.             'show' => true
  351.         ]);
  352.     }
  353.     #[Route('/demande/verif/{id}'name'app_verif_demande')]
  354.     public function verifDemande(int $id)
  355.     {
  356.         $url $this->adminUrlGenerator
  357.             ->setController(DemandeServiceCrudController::class)
  358.             ->setAction(Action::INDEX)
  359.             ->generateUrl();
  360.         $demandeService $this->em->getRepository(DemandeService::class)->find($id);
  361.         if (true === $this->isGranted('ROLE_CHARGE_ASSISTANCE') || true === $this->isGranted('ROLE_DAE')) {
  362.             $demandeService->setIsverified(true);
  363.             $demandeService->setVerifiedBy($this->getUser()->getId());
  364.             $demandeService->setVerifiedAt(new \DateTime());
  365.             $demandeService->setEtat(Globals::DEMANDE_ETAT_ATTENTE_VALIDATION);
  366.             $this->em->persist($demandeService);
  367.             $this->em->flush($demandeService);
  368.             $this->addFlash('success'"Demande vérifié avec succès");
  369.             return $this->redirect($url);
  370.         } else {
  371.             $this->addFlash('error''Accès interdit');
  372.             return $this->redirect($url);
  373.         }
  374.     }
  375.     #[Route('/demande/print/{id}'name'app_print_demande')]
  376.     public function printDemande(int $id)
  377.     {
  378.         $demande $this->em->getRepository(DemandeService::class)->find($id);
  379.         return $this->render('service/demande/print_demande.html.twig', [
  380.             'demande' => $demande
  381.         ]);
  382.     }
  383.     /**
  384.      * @param int $id
  385.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  386.      */
  387.     #[Route('/demande/valider/{id}'name'valid_demande')]
  388.     public function validerDemande(int $id)
  389.     {
  390.         $url $this->adminUrlGenerator
  391.             ->setController(DemandeServiceCrudController::class)
  392.             ->setAction(Action::INDEX)
  393.             ->generateUrl();
  394.         $demande $this->em->getRepository(DemandeService::class)->find($id);
  395.         if (true === $this->isGranted('ROLE_DAE')) {
  396.             $demande->setIsValid(true);
  397.             $demande->setValidBy($this->getUser()->getId());
  398.             $demande->setValidAt(new \DateTime());
  399.             $demande->setEtat(Globals::DEMANDE_ETAT_VALIDE);
  400.             $this->em->persist($demande);
  401.             $this->em->flush($demande);
  402.             $this->addFlash('success'"Demande Validée avec succès");
  403.             return $this->redirect($url);
  404.         } else {
  405.             $this->addFlash('error''Accès interdit');
  406.             return $this->redirect($url);
  407.         }
  408.     }
  409.     /**
  410.      * @param int $id
  411.      * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
  412.      */
  413.     #[Route('/demande/download/{id}'name'download_doc_demande')]
  414.     public function downloadDocDemande(int  $id)
  415.     {
  416.         $demande $this->em->getRepository(DemandeService::class)->find($id);
  417.         $zipArchive = new ZipArchive();
  418.         $zipArchive->open($this->getParameter('uploads_directory').'/affectations/doc_demande_zip_'.$demande->getId().'.zip'ZipArchive::CREATE);
  419.         if (count($demande->getDemandeAffectation())) {
  420.             foreach ($demande->getDemandeAffectation() as $affectation) {
  421.                 if (count($affectation->getDocDemandeService())) {
  422.                     foreach ($affectation->getDocDemandeService() as $file) {
  423.                         if (true === in_array('ROLE_AGENT_INTERNE'$file->getUser()->getRoles()) && true === $file->isExterne() && 'import_conseiller' === $file->getType() && Globals::DOC_ETAT_VALIDE === $file->getEtat()) {
  424.                             $zipArchive->addFile($this->getParameter('uploads_directory') . '/affectations/' $affectation->getId() . '/' $file->getFileName(), $file->getOriginalFileName());
  425.                         }
  426.                     }
  427.                 }
  428.             }
  429.         }
  430.         $zipArchive->close();
  431.         if (true === file_exists($this->getParameter('uploads_directory').'/affectations/doc_demande_zip_'.$demande->getId().'.zip')) {
  432.             return $this->file($this->getParameter('uploads_directory').'/affectations/doc_demande_zip_'.$demande->getId().'.zip')->deleteFileAfterSend();
  433.         }
  434.         $this->addFlash('error''Documents : accès interdit ! ');
  435.         return $this->redirectToRoute('app_list_demande');
  436.     }
  437.     /**
  438.      * @param int $demandeId
  439.      * @return \Symfony\Component\HttpFoundation\Response
  440.      */
  441.     #[Route('/demande/doc/list/{demandeId}'name'list_doc_demande')]
  442.     public function docList(int $demandeId)
  443.     {
  444.         $demande $this->em->getRepository(DemandeService::class)->find($demandeId);
  445.         $docDemandeClient $this->em->getRepository(DocDemandeClient::class)->findBy(['demandeService' => $demande'deleted' => false]);
  446.         return $this->render('service/demande/doc_list.html.twig', [
  447.             'demande' => $demande,
  448.             'docs' => $docDemandeClient
  449.         ]);
  450.     }
  451.     /**
  452.      * @param int $demandeId
  453.      * @return \Symfony\Component\HttpFoundation\Response
  454.      */
  455.     #[Route('/demande/doc/add/{demandeId}/{docId}'name'add_doc_demande')]
  456.     public function addDoc(int $demandeIdint $docId 0Request $request,NotificationManager $notificationManager,)
  457.     {
  458.         $docDemandeClient = ($docId === 0) ? new DocDemandeClient() : $this->em->getRepository(DocDemandeClient::class)->find($docId);
  459.         $oldFileName '';
  460.         if ($docId !== 0) {
  461.             $oldFileName $this->em->getRepository(DocDemandeClient::class)->find($docId)->getFileName();
  462.         }
  463.         $form $this->createForm(DocDemandeClientType::class, $docDemandeClient);
  464.         $form->handleRequest($request);
  465.         if ($form->isSubmitted() && $form->isValid()) {
  466.             $demande $this->em->getRepository(DemandeService::class)->find($demandeId);
  467.             $edit_demande_url "";
  468.             $docDemandeClient $form->getData();
  469.             $docDemandeClient->setDemandeService($demande);
  470.             $docDemandeClient->setCreatedBy($this->getUser()->getId());
  471.             $docDemandeClient->setCreatedAt(new \DateTime());
  472.             $docDemandeClient->setDeleted(false);
  473.             $file $this->request->getCurrentRequest()->files->get('doc_demande_client')['fileName'];
  474.             if (null !== $file) {
  475.                 $docDemandeClient->setOriginalFileName($file->getClientOriginalName());
  476.                 $newFileName $this->fileUploader->upload($file'/demandes/' $demande->getId(), 'docaff' );
  477.                 $docDemandeClient->setFileName($newFileName);
  478.                 if ($docId !== && true === file_exists($this->getParameter('uploads_directory').'/demandes/'.$docDemandeClient->getDemandeService()->getId().'/'.$oldFileName)) {
  479.                     unlink($this->getParameter('uploads_directory').'/demandes/'.$docDemandeClient->getDemandeService()->getId().'/'.$oldFileName);
  480.                 }
  481.             }
  482.             $this->em->persist($docDemandeClient);
  483.             $this->em->flush();
  484.             $edit_demande_url =  $this->adminUrlGenerator
  485.             ->setController(DemandeServiceCrudController::class)
  486.             ->setAction(Action::INDEX)
  487.             ->set('filters[id][comparison]''=')
  488.             ->set('filters[id][value]',  $demande->getId())
  489.             ->generateUrl();         
  490.     
  491.             $notification_message ="Mise à jour de document d'une demande !";
  492.  
  493.             $daes $this->em->getRepository(User::class)->findByRole('ROLE_DAE');
  494.             $CAs $this->em->getRepository(User::class)->findByRole('ROLE_CHARGE_ASSISTANCE');
  495.              foreach($daes as $dae) {
  496.                 $notificationManager->pushMessage($notification_message$notification_message$dae$edit_demande_url);
  497.             }
  498.             foreach($CAs as $ca) {
  499.                  if($ca->getUniteRegional()->getAdresse()->getGouvernorat() == $demande->getGouvernorat() ){
  500.                     $notificationManager->pushMessage($notification_message$notification_message$ca$edit_demande_url);
  501.                  }
  502.             }
  503.             $this->addFlash('success''Documents ajouté avec succès');
  504.             return $this->redirectToRoute('list_doc_demande', ['demandeId' => $demande->getId()]);
  505.         }
  506.         return $this->render('service/demande/add_doc.html.twig', [
  507.             'form' => $form->createView()
  508.         ]);
  509.     }
  510.     /**
  511.      * @param int $id
  512.      * @return \Symfony\Component\HttpFoundation\BinaryFileResponse|\Symfony\Component\HttpFoundation\RedirectResponse
  513.      */
  514.     #[Route('/demande/doc/download/{id}'name'download_doc_client')]
  515.     public function downloadDocClient(int $id)
  516.     {
  517.         $docDemande $this->em->getRepository(DocDemandeClient::class)->find($id);
  518.         if (true === file_exists($this->getParameter('uploads_directory').'/demandes/'.$docDemande->getDemandeService()->getId().'/'.$docDemande->getFileName())) {
  519.             return $this->file($this->getParameter('uploads_directory').'/demandes/'.$docDemande->getDemandeService()->getId().'/'.$docDemande->getFileName());
  520.         }
  521.         $this->addFlash('error''Document introuvable ! ');
  522.         return $this->redirectToRoute('list_doc_demande', [ 'demandeId' =>  $docDemande->getDemandeService()->getId() ]);
  523.     }
  524.     /**
  525.      * @param int $id
  526.      * @return \Symfony\Component\HttpFoundation\BinaryFileResponse|\Symfony\Component\HttpFoundation\RedirectResponse
  527.      */
  528.     #[Route('/demande/doc/delete/{id}'name'app_delete_doc')]
  529.     public function deleteDocClient(int $id)
  530.     {
  531.         $docDemande $this->em->getRepository(DocDemandeClient::class)->find($id);
  532.         $docDemande->setDeleted(true);
  533.         $this->em->persist($docDemande);
  534.         $this->em->flush();
  535.         $this->addFlash('error''Documents : supprimé avec succès ! ');
  536.         return $this->redirectToRoute('list_doc_demande', ['demandeId' => $docDemande->getDemandeService()->getId()]);
  537.     }
  538.     /**
  539.      * @param int $id
  540.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  541.      */
  542.     #[Route('/demande/doc/accept/{id}'name'accept_document')]
  543.     public function acceptDocDeamnde(int $id)
  544.     {
  545.         $demande $this->em->getRepository(DemandeService::class)->find($id);
  546.         $demande->setAcceptDocuments(true);
  547.         $this->em->persist($demande);
  548.         $this->em->flush();
  549.         $this->addFlash('error''Documents : accepté avec succès ! ');
  550.         return $this->redirectToRoute('app_list_demande');
  551.     }
  552.     /**
  553.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  554.      */
  555.     #[Route('/demande/back'name'admin_list')]
  556.     public function backToAdminList()
  557.     {
  558.         $url $this->adminUrlGenerator
  559.             ->setController(DemandeServiceCrudController::class)
  560.             ->setAction(Action::INDEX)
  561.             ->generateUrl();
  562.         if (true === $this->isGranted('ROLE_AGENT_INTERNE')) {
  563.             $url $this->adminUrlGenerator
  564.                 ->setController(DemandeAffectationCrudController::class)
  565.                 ->setAction(Action::INDEX)
  566.                 ->generateUrl();
  567.         }
  568.         return $this->redirect($url);
  569.     }
  570.     /**
  571.      * @param int $id
  572.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  573.      */
  574.     #[Route('/demande/refuser/{id}'name'refuser_demande')]
  575.     public function refuserDemande(int $id)
  576.     {
  577.         $motifRefus $this->request->getCurrentRequest()->request->all()['motifRefus'];
  578.         $demande $this->em->getRepository(DemandeService::class)->find($id);
  579.         $url $this->adminUrlGenerator
  580.             ->setController(DemandeServiceCrudController::class)
  581.             ->setAction(Action::INDEX)
  582.             ->generateUrl();
  583.         if (true === $this->isGranted('ROLE_DAE')) {
  584.             $demande->setRefusedBy($this->getUser()->getId());
  585.             $demande->setRefusedAt(new \DateTime());
  586.             $demande->setMotifRefus($motifRefus);
  587.             $demande->setEtat(Globals::DEMANDE_ETAT_REFUSER);
  588.             $this->em->persist($demande);
  589.             $this->em->flush($demande);
  590.             $this->addFlash('success'"Demande refusée avec succès");
  591.             $edit_demande_url $this->adminUrlGenerator
  592.             ->setController(DemandeServiceCrudController::class)
  593.             ->set('demandeServiceId'$demande)
  594.             ->setAction(Action::INDEX)
  595.             ->generateUrl();
  596.             $notification_message 'Nouvelle Demande a été acceptée !   ';
  597.             $currentUser $this->getUser();
  598.             $admins $this->em->getRepository(User::class)
  599.                 ->createQueryBuilder('u')
  600.                 ->where('u != :currentUser')
  601.                 ->setParameter('currentUser'$currentUser)
  602.                 ->andWhere('u.roles LIKE :role1 OR u.roles LIKE :role2 ')
  603.                 ->setParameter('role1''%ROLE_CHARGE_ASSISTANCE%')
  604.                 ->setParameter('role2''%ROLE_DAE%')
  605.                 ->getQuery()
  606.                 ->getResult();
  607.     
  608.             foreach ($admins as $admin) {
  609.                 $this->notificationManager->pushMessage(' Demande a été refusée! '$notification_message$admin$edit_demande_url);
  610.             }
  611.             $userExp $this->em->getRepository(User::class)->findOneBy(['id'=> $demande->getUser()->getId()]);
  612.            
  613.             $url_user_front '/demande/list';
  614.             $this->notificationManager->pushMessage('Demande a été refusée! '$notification_message$userExp$url_user_front);
  615.             return $this->redirect($url);
  616.         } else {
  617.             $this->addFlash('error''Accès interdit');
  618.             return $this->redirect($url);
  619.         }
  620.     }
  621. /**
  622.  * @Route("/note/add", name="add_note", methods={"POST"})
  623.  */
  624. public function addComment(Request $requestEntityManagerInterface $entityManager): Response
  625. {
  626.     $id = (int)$request->request->get('demandeIdName');
  627.     $demande $entityManager->getRepository(DemandeService::class)->find($id);
  628.     if (!$demande) {
  629.         return $this->json(['error' => 'Commentaire not found.'], Response::HTTP_NOT_FOUND);
  630.     }
  631.     $noteDemande = new DemandeNote;
  632.     // Update the properties of the DemandeNote entity based on the form data
  633.     $noteDemande->setDescription($request->request->get('commentName'));
  634.     $noteDemande->setDemande($demande);
  635.     $noteDemande->setIsPublic(true);
  636.     // Persist and flush the changes to the database
  637.     $entityManager->persist($noteDemande);
  638.     $entityManager->flush();
  639.     $this->addFlash('success''Commentaire a été ajoutée  avec succès');
  640.     return $this->redirectToRoute('comment_demande', ['demandeId' => $id ]);
  641. }
  642.         /**
  643.      * @Route("/note/update/{id}", name="update_note", methods={"POST"})
  644.      */
  645.     public function updateNote(Request $requestint $idEntityManagerInterface $entityManager): Response
  646.     {
  647.         $demandeNote $entityManager->getRepository(DemandeNote::class)->find($id);
  648.         if (!$demandeNote) {
  649.             return $this->json(['error' => 'Commentaire not found.'], Response::HTTP_NOT_FOUND);
  650.         }
  651.         $formData json_decode($request->getContent(), true);
  652.         // Check if 'editComment' key exists in formData
  653.         if (isset($formData['editComment'])) {
  654.             $editComment $formData['editComment'];
  655.             $demandeNote->setDescription($editComment);
  656.         } else {
  657.         
  658.             return $this->json(['error' => 'editComment key is missing in formData.'], Response::HTTP_BAD_REQUEST);
  659.         }
  660.         // Persist and flush the changes to the database
  661.         $entityManager->persist($demandeNote);
  662.         $entityManager->flush();
  663.         $response = [
  664.             'success' => true,
  665.             'messages' => 'Commentaire a été mis à jour avec succès.',
  666.         ];
  667.         return $this->json($response);
  668.     }
  669.     #[Route('/note/delete/{id}/{demandeId}'name'delete_note')]
  670.     public function deleteAction($id,$demandeIdEntityManagerInterface $entityManager)
  671.     {
  672.         $note $entityManager->getRepository(DemandeNote::class);
  673.         $note $note->find($id);
  674.         if (!$note) {
  675.             throw $this->createNotFoundException(
  676.                 'Il n\'y a aucun commentaire avec l\'identifiant suivant : ' $id
  677.             );
  678.         }
  679.         $entityManager->remove($note);
  680.         $entityManager->flush();
  681.    
  682.         $response = [
  683.             'success' => true,
  684.             'messages' => 'suppression effectuée avec succès.',
  685.         ];
  686.         $this->addFlash('success''suppression effectuée avec succès');
  687.         return $this->redirectToRoute('comment_demande', ['demandeId' => $demandeId ]);
  688.     
  689.     }
  690.     /**
  691.      * @Route("/note/fetchData/{id}", name="fetch_note_data", methods={"GET"})
  692.      */
  693.     public function fetchData(int $idEntityManagerInterface $entityManager): Response
  694.     {
  695.         $demandeNote $entityManager->getRepository(DemandeNote::class)->find($id);
  696.         if (!$demandeNote) {
  697.             return $this->json(['error' => 'Subject not found.'], Response::HTTP_NOT_FOUND);
  698.         }
  699.       
  700.         $response = [
  701.             'id' => $demandeNote->getId(),
  702.             'description' => $demandeNote->getDescription()
  703.         ];
  704.         return $this->json($response);
  705.     }
  706.       /**
  707.      * @Route("/get_candidat_form", name="fcandidat_form", methods={"GET"})
  708.      */
  709.     public function getCandidatForm(Request $request): Response
  710.     {
  711.         $index $request->query->get('i');
  712.         $content $this->renderView('include/add_condidat_form.html.twig', ['index' => $index]);
  713.         return new Response($content);
  714.     }
  715. }