src/Controller/ActionController.php line 691

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Action;
  4. use App\Entity\ActionPlan;
  5. use App\Entity\bilan\ConditionSubdomain;
  6. use App\Entity\bilan\Entity;
  7. use App\Entity\bilan\SituationRisque;
  8. use App\Entity\Configuration\CompanyEntity;
  9. use App\Entity\ConformityArticle;
  10. use App\Entity\Domain;
  11. use App\Entity\Dysfonctionnement\Cause5p;
  12. use App\Entity\Dysfonctionnement\Cause6m;
  13. use App\Entity\inspection\DomaineQuestionnaire;
  14. use App\Entity\inspection\InspectionQuestion;
  15. use App\Entity\Processus;
  16. use App\Entity\ReunionSubject;
  17. use App\Entity\Source;
  18. use App\Entity\User;
  19. use App\Entity\Subject;
  20. use App\Entity\Type;
  21. use App\Entity\VGP\Observation;
  22. use App\Enum\CategoryEnum;
  23. use App\Enum\PeriodActionEnum;
  24. use App\Enum\PriorityEnum;
  25. use App\Enum\StatusEnum;
  26. use App\Form\ActionType;
  27. use App\Repository\Configuration\CompanyEntityRepository;
  28. use App\Service\SelectedEntityService;
  29. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  30. use Symfony\Component\HttpFoundation\Request;
  31. use Symfony\Component\HttpFoundation\RequestStack;
  32. use Symfony\Component\HttpFoundation\Response;
  33. use Symfony\Component\Routing\Annotation\Route;
  34. use App\Utils\Utility;
  35. use App\Repository\ActionRepository;
  36. use DateTimeInterface;
  37. use Symfony\Component\HttpFoundation\JsonResponse;
  38. use Symfony\Component\Security\Core\Security;
  39. use Symfony\Component\Validator\Constraints\DateTime;
  40. use Symfony\Component\Validator\Constraints\Json;
  41. use App\Notification\SendNotification;
  42. use App\Security\ActionVoter;
  43. use Proxies\__CG__\App\Entity\inspection\InspectionQuestion as InspectionInspectionQuestion;
  44. /**
  45.  * @Route("/action")
  46.  */
  47. class ActionController extends AbstractController
  48. {
  49.     /**
  50.      * @var SendNotification
  51.      */
  52.     private $notify;
  53.     private $notifyAction;
  54.     /**
  55.      * @var Security
  56.      */
  57.     private $security;
  58.     private $requestStack;
  59.     public function __construct(
  60.         SendNotification $notify,
  61.         SendNotification $notifyAction,
  62.         SendNotification $notifyActionValidator,
  63.         Security $security,
  64.         RequestStack $requestStack
  65.     )
  66.     {
  67.         $this->notify $notify;
  68.         $this->notifyAction $notifyAction;
  69.         $this->notifyActionValidator $notifyActionValidator;
  70.         $this->security $security;
  71.         $this->requestStack $requestStack;
  72.     }
  73.     /**
  74.      * @Route("/", name="action_index", methods={"GET"})
  75.      */
  76.     public function index(Request $requestSelectedEntityService $selectedEntityService): Response
  77.     {
  78.         $users $this->getDoctrine()->getRepository(User::class)->findAll();
  79.         $processus $this->getDoctrine()->getRepository(Processus::class)->findAll();
  80.         $sources $this->getDoctrine()->getRepository(Source::class)->findAll();
  81.         $domains $this->getDoctrine()->getRepository(Domain::class)->findAll();
  82.         $types $this->getDoctrine()->getRepository(Type::class)->findAll();
  83.         $categories CategoryEnum::getAvailableCategories();
  84.         $entities $this->getDoctrine()->getRepository(CompanyEntity::class)->findAll();
  85.         $ref_action $request->query->get('ref');
  86.         $selectedEntity $selectedEntityService->getSelectedEntity();
  87.         
  88.         //menu 
  89.         $menu="action";
  90.         $left_menu="pilotage_actions";
  91.         return $this->render('action/index.html.twig',[
  92.             'users'=>$users,
  93.             'processus'=>$processus,
  94.             'sources'=>$sources,
  95.             'domains'=>$domains,
  96.             'types'=>$types,
  97.             'categories'=>$categories,
  98.             'entities'=>$entities,
  99.             'ref_action'=>$ref_action,
  100.             'menu'=>$menu,
  101.             'left_menu'=>$left_menu,
  102.             'selectedEntity'=>$selectedEntity,
  103.         ]);
  104.     }
  105.     /**
  106.      * @Route("/list/{actionplanId}", name="action_list", defaults={"actionplanId": null}, methods={"GET"})
  107.      */
  108.     public function list(ActionRepository $actionRepository,Request $request,$actionplanId='')
  109.     {
  110.         $startDate=$request->query->get('startDate','');
  111.         $endDate=$request->query->get('endDate','');
  112.         $remove_link$request->query->get('remove_link','');
  113.         $actionPlan='';
  114.         if($actionplanId>0)
  115.         {
  116.             $actionPlan$this->getDoctrine()->getRepository(ActionPlan::class)->find($actionplanId);
  117.             
  118.         }
  119.         
  120.         $user $this->getUser();
  121.         $actionsData $actionRepository->transformAll($user,$actionPlan,$remove_link,$startDate,$endDate);
  122.         return new JsonResponse($actionsData);
  123.     }
  124.     /**
  125.      * @Route("/new", name="action_new", methods={"GET","POST"})
  126.      */
  127.     public function new(Request $requestActionRepository $actionRepositoryCompanyEntityRepository $companyEntityRepository): Response
  128.     {
  129.         $action = new Action();
  130.         $this->denyAccessUnlessGranted(ActionVoter::ACTION_CRUD$action);
  131.         $user $this->security->getUser();
  132.         $session $this->requestStack->getCurrentRequest()->getSession();
  133.         $selectedEntityId $session->get('selected_entity_id');
  134.         $selectedEntity null;
  135.         if ($selectedEntityId && ($this->security->isGranted('ROLE_ADMIN') || $this->security->isGranted('ROLE_DIRECTION'))) {
  136.             $selectedEntity $companyEntityRepository->find($selectedEntityId);
  137.         } elseif (count($user->getEntities()) === 1) {
  138.             $selectedEntity $user->getEntities()->first();
  139.         }
  140.         if ($selectedEntity) {
  141.             $action->addCompanyEntity($selectedEntity);
  142.         }
  143.         $utility = new Utility;
  144.         
  145.         /* genretate ref */
  146.         
  147.         $oldRef$actionRepository->findLastRef(Action::PREFIX_AC);
  148.         $newRef $utility->generateNewRef($oldRef,Action::PREFIX_AC);
  149.         $actionplan_id=$request->query->get('actionplan');
  150.         if($actionplan_id 0)
  151.         {
  152.             $oldRef$actionRepository->findLastRef(Action::PREFIX_PA_AC);
  153.             $newRef $utility->generateNewRef($oldRef,Action::PREFIX_PA_AC);
  154.         }
  155.         
  156.         $action->setRef($newRef);
  157.         /* end genretate ref*/
  158.         /* genretate ref for actionEE */
  159.         $oldRefEE$actionRepository->findLastRef(Action::PREFIX_ACEE);
  160.         $newRefEE $utility->generateNewRef($oldRefEE,Action::PREFIX_ACEE);
  161.         $action->setRefEE($newRefEE);
  162.         /* end genretate ref*/
  163.         $options=[];
  164.         /* get actionPlan carecteristics */
  165.         $domainPa='';
  166.         $typePa='';
  167.         $sourcePa='';
  168.         $actionplan_id $request->query->get('actionplan');
  169.         if($actionplan_id )
  170.         {   
  171.             $actionPlan$this->getDoctrine()->getRepository(ActionPlan::class)->find($actionplan_id);
  172.             if($actionPlan)
  173.             {
  174.                 $domainPa=$actionPlan->getDomain();
  175.                 $action->addDomaine($domainPa);
  176.                 $action->setDomainEE([$domainPa]);
  177.                 $options['disabled_domain']=true;
  178.                 $typePa=$actionPlan->getType();
  179.                 $action->setType($typePa);
  180.                 $action->setTypeEE($typePa);
  181.                 $options['disabled_type']=true;
  182.                 $sourcePa=$actionPlan->getSource();
  183.                 $action->setSource($sourcePa);
  184.                 $options['disabled_source']=true;
  185.             }
  186.         }
  187.         /* end actionPlan */
  188.         /* situation risque */
  189.         $situationRisque_id $request->query->get('sr');
  190.         if($situationRisque_id 0) {
  191.             $domain $this->getDoctrine()->getRepository(Domain::class)->findOneBy(['title'=>'Santé & sécurité au travail']);
  192.             $action->addDomaine($domain);
  193.             $action->setDomainEE([$domain]);
  194.             $options['disabled_domain']=true;
  195.             $type$this->getDoctrine()->getRepository(Type::class)->findOneBy(['title'=>'Risque-Préventif']);
  196.             $action->setType($type);
  197.             $action->setTypeEE($type);
  198.             $source$this->getDoctrine()->getRepository(Source::class)->findOneBy(['title'=>'Evrp (doc unique)']);
  199.             $action->setSource($source);
  200.             $options['disabled_source']=true;
  201.             $processus_id$request->query->get('pr');
  202.             $processus $this->getDoctrine()->getRepository(Processus::class)->find($processus_id);
  203.             $action->addProcessus($processus);
  204.         }
  205.         /* end situation risque */
  206.         
  207.         /* condition subdomain */
  208.         $conditionSubdomain_id $request->query->get('cs');
  209.         if($conditionSubdomain_id 0) {
  210.             $domain $this->getDoctrine()->getRepository(Domain::class)->findOneBy(['title'=>'Environnement']);
  211.             $action->addDomaine($domain);
  212.             $action->setDomainEE([$domain]);
  213.             $options['disabled_domain']=true;
  214.             $type$this->getDoctrine()->getRepository(Type::class)->findOneBy(['title'=>'Risque-Préventif']);
  215.             $action->setType($type);
  216.             $action->setTypeEE($type);
  217.             $source$this->getDoctrine()->getRepository(Source::class)->findOneBy(['title'=>'Analyse environnementale']);
  218.             $action->setSource($source);
  219.             $options['disabled_source']=true;
  220.             $processus_id$request->query->get('pr');
  221.             $processus $this->getDoctrine()->getRepository(Processus::class)->find($processus_id);
  222.             $action->addProcessus($processus);
  223.         }
  224.         /* condition inspection */
  225.          $inspectionQuestion_id $request->query->get('iq');
  226.         if($inspectionQuestion_id 0) {
  227.            $inspectionQuestion $this->getDoctrine()->getRepository(InspectionQuestion::class)->find($inspectionQuestion_id);
  228.            $questionnaire=$inspectionQuestion->getInspection()->getQuestionnaire();
  229.           
  230.            $domainesQ$this->getDoctrine()->getRepository(DomaineQuestionnaire::class)->findBy(['questionnaire'=>$questionnaire]);
  231.            
  232.            foreach( $domainesQ as $domainQ)
  233.                 {
  234.                     
  235.                     $action->addDomaine($domainQ->getDomaine());
  236.                 }
  237.            
  238.             $options['disabled_domain']=true;
  239.             $type$this->getDoctrine()->getRepository(Type::class)->findOneBy(['title'=>'correctif']);
  240.             $action->setType($type);
  241.             $action->setTypeEE($type);
  242.             $source$this->getDoctrine()->getRepository(Source::class)->findOneBy(['title'=>'Vérification TIK TOP']);
  243.             $action->setSource($source);
  244.             $options['disabled_source']=true;
  245.         }
  246.         /* end condition subdomain */
  247.         //add action realted to article 
  248.         $article_id $request->query->get('article_id','');
  249.         if($article_id) {
  250.             $article $this->getDoctrine()->getRepository(ConformityArticle::class)->find($article_id);
  251.             $conformityText $article->getConformityText();
  252.             $domaines $conformityText->getDomains();
  253.             foreach( $domaines as $domaine)
  254.             {
  255.                 $action->addDomaine($domaine);
  256.             }
  257.            
  258.             $options['disabled_domain']=true;
  259.             //Action corrective définitive
  260.             $type$this->getDoctrine()->getRepository(Type::class)->findOneBy(['title'=>'Action corrective définitive']);
  261.             if($type) {
  262.                 $action->setType($type);
  263.                 $action->setTypeEE($type);
  264.                 $options['disabled_type']=true;
  265.             }
  266.             
  267.             //Veille règlementaire
  268.             $source$this->getDoctrine()->getRepository(Source::class)->findOneBy(['title'=>'Veille règlementaire']);
  269.             if($source) {
  270.                 $action->setSource($source);
  271.                 $options['disabled_source']=true;
  272.             }
  273.             
  274.         }
  275.         
  276.         //end artilce
  277.         $form $this->createForm(ActionType::class, $action,$options);
  278.         $form->handleRequest($request);
  279.         if ($form->isSubmitted() && $form->isValid()) {
  280.             
  281.             /* set typePa */
  282.             $action->setTypePa('Sans PA');
  283.             /* set status*/
  284.             $action->setStatus(StatusEnum::STATUS_PLANNED);
  285.             // $action->setStatus(StatusEnum::STATUS_IN_PROGRESS);
  286.             // if(!empty($action->getValidator()) || $action->getDateStartEstimated() > $action->getCreatedAt())
  287.             // {
  288.             //     $action->setStatus(StatusEnum::STATUS_PLANNED);
  289.             // }
  290.             /* set validation */
  291.             if(!empty($action->getValidator()))
  292.             {
  293.                 $action->setValidated(0);
  294.             }
  295.             /* createdBy */
  296.             $createdBy $this->getUser();
  297.             $action->setCreatedBy($createdBy);
  298.             /* set type Action */
  299.             $action->setTypeAction(Action::PREFIX_AC);
  300.             /* set progress */
  301.             $action->setProgress(0);
  302.             /* set resources */
  303.             if(!empty($action->getResourceHum()))
  304.             {
  305.                 foreach($action->getResourceHum() as $resource)
  306.                 {
  307.                     $action->addResource($resource);
  308.                 }
  309.             }
  310.             if(!empty($action->getResourceMat()))
  311.             {
  312.                 foreach($action->getResourceMat() as $resource)
  313.                 {
  314.                     $action->addResource($resource);
  315.                 }
  316.             }
  317.             
  318.             
  319.             
  320.             /** add actionEE */
  321.             if(!empty($_POST['efficacite']))
  322.             {
  323.                 $user $this->getUser();
  324.                 $actionEE= new Action();
  325.                 $actionEE->setCompanyEntities($action->getCompanyEntities());
  326.                 $actionEE->setRef($action->getRefEE());
  327.                 $actionEE->setTitle($action->getTitleEE());
  328.                 // $actionEE->setDomain($_POST['action']['domain']);
  329.                 // $actionEE->setType($_POST['action']['type']);
  330.                 foreach($action->getDomaines() as $domain)
  331.                 {
  332.                     $actionEE->addDomaine($domain);
  333.                 }
  334.                 
  335.                 $actionEE->setType($action->getType());
  336.                 $actionEE->setDateStartEstimated($action->getDateStartEstimatedEE());
  337.                 $actionEE->setDateEndEstimated($action->getDateEndEstimatedEE());
  338.                 $actionEE->setDescription($action->getDescriptionEE());
  339.                 $actionEE->setTypeAction(Action::PREFIX_ACEE);
  340.                 $actionEE->setStatus(StatusEnum::STATUS_PLANNED);
  341.                 $actionEE->setProgress(0);
  342.                 $actionEE->setResponsible($action->getResponsibleEE());
  343.                 $actionEE->setDurationEstimated($action->getDurationEstimatedEE());
  344.                 $actionEE->setBudgetEstimated($action->getBudgetEstimatedEE());
  345.                 $actionEE->setCreatedBy($user);
  346.                 $actionEE->setPriority(PriorityEnum::PRIORITY_HIGH);
  347.                 
  348.                 foreach($action->getProcessusEE() as $processus)
  349.                 {
  350.                     $actionEE->addProcessus($processus);
  351.                 }
  352.                 $action->setAction($actionEE);
  353.                 /* set typePa */
  354.                 $actionEE->setTypePa('Sans PA');
  355.             }
  356.             $entityManager $this->getDoctrine()->getManager();
  357.             
  358.             /** set subjet */
  359.             $reunion_subject='';
  360.             $id_reunion_subject=$request->query->get('reunion_subject','');
  361.             if($id_reunion_subject 0)
  362.             {
  363.                 $reunion_subject $entityManager->getRepository(ReunionSubject::class)->find($id_reunion_subject);
  364.                 if(is_object($reunion_subject))
  365.                 {
  366.                     $action->setReunionSubject($reunion_subject);
  367.                     $action->setReunion($reunion_subject->getReunion());
  368.                     $action->setStatus(StatusEnum::STATUS_DRAFT);
  369.                     if(!empty($_POST['efficacite']))
  370.                     {
  371.                         $actionEE->setStatus(StatusEnum::STATUS_DRAFT);
  372.                     }
  373.                 }
  374.                 
  375.             }
  376.             //$path='action_index';
  377.             /** set ActionPlan */
  378.             $actionplan='';
  379.             if($_POST['actionplan'] > 0)
  380.             {
  381.                 $id_actionplan $_POST['actionplan'];
  382.                 $actionplan $entityManager->getRepository(ActionPlan::class)->find($id_actionplan);
  383.                 if(is_object($actionplan))
  384.                 {
  385.                     
  386.                     $action->setActionPlan($actionplan);
  387.                     /* set typePa */
  388.                     $action->setTypePa($actionplan->getTypePa());
  389.                     
  390.                     $actionplan->addParticipant($createdBy);
  391.                     $actionplan->addParticipant($action->getResponsible());
  392.                     /* set domain, type, source*/
  393.                     $action->addDomaine($actionplan->getDomain());
  394.                     $action->setType($actionplan->getType());
  395.                     $action->setSource($actionplan->getSource());
  396.                     
  397.                     if(is_object($action->getValidator()) )
  398.                     {
  399.                         $actionplan->addParticipant($action->getValidator());
  400.                     }
  401.                     if(is_object($action->getAction()))
  402.                     {
  403.                         $action->getAction()->setActionPlan($actionplan);
  404.                         /* set typePa */
  405.                         $action->getAction()->setTypePa('Plan de progrès');
  406.                          /* set domain, type, source*/
  407.                         $action->getAction()->addDomaine($actionplan->getDomain());
  408.                         $action->getAction()->setType($actionplan->getType());
  409.                         $actionplan->addParticipant($action->getAction()->getResponsible());
  410.                         if(is_object($action->getAction()->getValidator()) )
  411.                         {
  412.                             $actionplan->addParticipant($action->getAction()->getValidator());
  413.                         }   
  414.                     }
  415.                     /** set status of actionPlan */
  416.                     if($action->getStatus()==StatusEnum::STATUS_IN_PROGRESS)
  417.                     {
  418.                         if($actionplan->getStatus()==StatusEnum::STATUS_ACPL_PLANNED && empty($actionplan->getBudgetEstimated()))
  419.                         {
  420.                             $actionplan->setStatus(StatusEnum::STATUS_ACPL_IN_PROGRESS);
  421.                         }
  422.                     }
  423.                     
  424.                 }
  425.                 
  426.             }
  427.             
  428.             
  429.             
  430.             $entityManager->persist($action);
  431.             //check if situation risque est liée a cette action
  432.             $situationRisque='';
  433.             if($_POST['sr'] > 0){
  434.                 $id_situationRisque $_POST['sr'];
  435.                 $situationRisque $entityManager->getRepository(SituationRisque::class)->find($id_situationRisque);
  436.                 $action->setSituationRisque($situationRisque);
  437.                 $situationRisque->addAction($action);
  438.             }
  439.             //check if situation risque est liée a cette action
  440.             $conditionSubdomain='';
  441.             if($_POST['cs'] > 0){
  442.                 $id_conditionSubdomain $_POST['cs'];
  443.                 $conditionSubdomain $entityManager->getRepository(ConditionSubdomain::class)->find($id_conditionSubdomain);
  444.                 $conditionSubdomain->setAction($action);
  445.             }
  446.             //check if inspection question est liée a cette action
  447.             $inspectionQuestion='';
  448.             if($request->query->get('iq') > 0){
  449.                 $id_inspectionQ $request->query->get('iq');
  450.                 $inspectionQuestion $entityManager->getRepository(InspectionQuestion::class)->find($id_inspectionQ);
  451.                 $inspectionQuestion->setAction($action);
  452.             }
  453.             //check if article est liée a cette action
  454.             $article='';
  455.             $conformityText='';
  456.             if($request->query->get('article_id') > 0){
  457.                 $article_id $request->query->get('article_id');
  458.                 $article $entityManager->getRepository(ConformityArticle::class)->find($article_id);
  459.                 $conformityText=$article->getConformityText();
  460.                 //$article->addAction($action);
  461.                 $action->setConformityArticle($article);
  462.                
  463.             }
  464.             //cause 5p
  465.             $cause5p_id=$request->query->get('cause5p','');
  466.             
  467.             if($cause5p_id) {
  468.                 $cause5p $this->getDoctrine()->getRepository(Cause5p::class)->find($cause5p_id);
  469.                 $cause5p->setAction($action);
  470.                 
  471.             }
  472.             $cause6m_id=$request->query->get('cause6m','');
  473.             
  474.             if($cause6m_id) {
  475.                 $cause6m $this->getDoctrine()->getRepository(Cause6m::class)->find($cause6m_id);
  476.                 $cause6m->setAction($action);
  477.                 
  478.             }
  479.             $observation_id=$request->query->get('observation_id','');
  480.             $observation='';
  481.             if($observation_id) {
  482.                 $observation $this->getDoctrine()->getRepository(Observation::class)->find($observation_id);
  483.                 $observation->setAction($action);
  484.             }
  485.             // if($action->getPeriodicity()) {
  486.             //     $this->createPeriodActions($action);
  487.             // }else {
  488.             //     dd('nothing');
  489.             // }
  490.             
  491.             $entityManager->flush();
  492.             
  493.             // if($_POST['actionplan'] > 0)
  494.             // {
  495.             //     //notification action with actionplan
  496.             //     if(!empty($action->getValidator()))
  497.             //     {
  498.             //         $this->notifyActionValidator->notifyActionValidator(
  499.             //             $action->getCreatedBy()->getEmail(),
  500.             //             $action->getValidator()->getEmail(),
  501.             //             $action->getRef()
  502.             //         );
  503.             //     }
  504.             //     if(!empty($_POST['efficacite']))
  505.             //     {
  506.             //         $this->notify->notify(
  507.             //             $action->getCreatedBy()->getEmail(),
  508.             //             $action->getResponsibleEE()->getEmail(),
  509.             //             $action->getResponsibleEE()->getFullName(),
  510.             //             $action->getRefEE(),
  511.             //         );
  512.             //     }
  513.             //     $this->notify->notify(
  514.             //         $action->getCreatedBy()->getEmail(),
  515.             //         $action->getResponsible()->getEmail(),
  516.             //         $action->getResponsible()->getFullName(),
  517.             //         $action->getRef()
  518.             //     );
  519.             // }else{
  520.             //     //notification action without actionplan
  521.             //     if(!empty($action->getValidator()))
  522.             //     {
  523.             //         $this->notifyActionValidator->notifyActionValidator(
  524.             //             $action->getCreatedBy()->getEmail(),
  525.             //             $action->getValidator()->getEmail(),
  526.             //             $action->getRef()
  527.             //         );
  528.             //     }
  529.             //     if(!empty($_POST['efficacite']))
  530.             //     {
  531.             //         $this->notifyAction->notifyAction(
  532.             //             $action->getCreatedBy()->getEmail(),
  533.             //             $action->getResponsibleEE()->getEmail(),
  534.             //             $action->getResponsibleEE()->getFullName(),
  535.             //             $action->getRefEE(),
  536.             //             $action->getCreatedBy()->getFonction()
  537.             //         );
  538.             //     }
  539.             //     $this->notifyAction->notifyAction(
  540.             //         $action->getCreatedBy()->getEmail(),
  541.             //         $action->getResponsible()->getEmail(),
  542.             //         $action->getResponsible()->getFullName(),
  543.             //         $action->getRef(),
  544.             //         $action->getCreatedBy()->getFonction()
  545.             //     );
  546.             // }
  547.             $this->addFlash("success","L'action a été créée avec succès");
  548.             if($_POST['add_new_action']){
  549.                 $menu="action";
  550.                 $left_menu="new_action";
  551.                 return $this->render('action/new.html.twig', [
  552.                     'action' => $action,
  553.                     'form' => $form->createView(),
  554.                     'menu' => $menu,
  555.                     'left_menu'=>$left_menu,
  556.                     'domainPa' => $domainPa,
  557.                     'typePa' => $typePa,
  558.                     'sourcePa' => $sourcePa,
  559.                 ]);
  560.             }
  561.             $constat_id=$request->query->get('constat','');
  562.             $reunion_id $request->query->get('reunion','');
  563.             if($observation) {
  564.                 $groupMajeur_id $request->query->get('groupMajeur_id','');
  565.                 return $this->redirectToRoute('vgp_surveillance',['id'=>$groupMajeur_id]);
  566.             }
  567.             if($reunion_id) {
  568.                 if($cause5p_id) {
  569.                     return $this->redirectToRoute('analyse_5p',['id'=>$reunion_id]);
  570.                 }else if ($cause6m_id) {
  571.                     return $this->redirectToRoute('analyse_6m',['id'=>$reunion_id]);
  572.                 }else {
  573.                     return $this->redirectToRoute('reunion_show',['id'=>$reunion_id]);
  574.                 }
  575.             }
  576.             if($constat_id) {
  577.                 return $this->redirectToRoute('analyse',['id'=>$constat_id]);
  578.             }
  579.             if(is_object($actionplan) && !$constat_id
  580.             {
  581.                 return $this->redirectToRoute('action_plan_show',['id'=>$actionplan->getId(),'section'=>'planification']);
  582.             }else if(is_object($situationRisque)) {
  583.                 return $this->redirectToRoute('analyseSST');
  584.             }else if(is_object($conditionSubdomain)){
  585.                 return $this->redirectToRoute('analyseENV');
  586.             }else if(is_object($inspectionQuestion)){
  587.                return $this->redirectToRoute('inspection_resultat_inspection',array('id'=>$inspectionQuestion->getInspection()->getId()));
  588.             }else if(is_object($conformityText)){
  589.                 return $this->redirectToRoute('conformite_analyse',array('id'=>$conformityText->getId()));
  590.             }else if(is_object($reunion_subject)) {
  591.                 return $this->redirectToRoute('reunion_show',array('id'=>$reunion_subject->getReunion()->getId()));
  592.             }else {
  593.                 return $this->redirectToRoute('action_index');
  594.             }
  595.         }
  596.         //menu 
  597.         
  598.         $menu="action";
  599.         $left_menu="new_action";
  600.         return $this->render('action/new.html.twig', [
  601.             'action' => $action,
  602.             'form' => $form->createView(),
  603.             'menu' => $menu,
  604.             'left_menu'=>$left_menu,
  605.             'domainPa' => $domainPa,
  606.             'typePa' => $typePa,
  607.             'sourcePa' => $sourcePa,
  608.         ]);
  609.     }
  610.     
  611.     /**
  612.      * @Route("/{id}/edit", name="action_edit", methods={"GET","POST"})
  613.      */
  614.     public function edit(Request $requestAction $actionActionRepository $actionRepositorySelectedEntityService $selectedEntityService): Response
  615.     {
  616.         $selectedEntity $selectedEntityService->getSelectedEntity();
  617.         if ($selectedEntity) {
  618.             $action->addCompanyEntity($selectedEntity);
  619.         }
  620.         $utility = new Utility;
  621.         $options = [];
  622.         //previous page
  623.         $goback $request->headers->get('referer');
  624.         $goback_array explode('section=',$goback);
  625.         
  626.         if(count($goback_array) > 1)
  627.         {
  628.             $goback_array[1] = 'planification';
  629.         }
  630.         $goback implode('section=',$goback_array);
  631.         
  632.         
  633.         
  634.         $actionEE  = new Action();
  635.         $procEE_ids=[];
  636.         if($action->hasActionEE()==1)
  637.         {
  638.             $actionEE $action->getAction();
  639.             foreach($actionEE->getProcessus() as $processus)
  640.             {
  641.                 $procEE_ids[]=$processus->getId();
  642.             }
  643.         }
  644.         else
  645.         {
  646.             /* genretate ref for actionEE */
  647.             $oldRefEE$actionRepository->findLastRef(Action::PREFIX_ACEE);
  648.             $newRefEE $utility->generateNewRef($oldRefEE,Action::PREFIX_ACEE);
  649.             $actionEE->setRef($newRefEE);
  650.             /* end genretate ref*/
  651.         }
  652.         $date_fin_old=$action->getDateEndEstimated();
  653.         $actionPlan="";
  654.         if($action->getActionPlan())
  655.         {
  656.             $actionPlan $action->getActionPlan()->getId();
  657.             $options['disabled_domain']=true;
  658.             $options['disabled_type']=true;
  659.             $options['disabled_source']=true;
  660.         }
  661.         
  662.         $reunion_subject="";
  663.         if($action->getReunionSubject())
  664.         {
  665.             $reunion_subject $action->getReunionSubject()->getId();
  666.             
  667.         }
  668.         
  669.         $responsibleIdOfMotherAction='0';
  670.         if($action->getTypeAction()=="AE/AC")
  671.         {
  672.             $actionMother $this->getDoctrine()->getRepository(Action::class)->findOneBy(array('action'=>$action));
  673.             if($actionMother)
  674.             {
  675.                 $responsibleIdOfMotherAction $actionMother->getResponsible()->getId();
  676.             }
  677.             
  678.         }
  679.         $situationRisque $this->getDoctrine()->getRepository(SituationRisque::class)->findOneBy(['action'=>$action]);
  680.         if($situationRisque){
  681.             $options['disabled_domain']=true;
  682.             $options['disabled_source']=true;
  683.         }
  684.         $form $this->createForm(ActionType::class, $action$options);
  685.         $form->handleRequest($request);
  686.         if ($form->isSubmitted() && $form->isValid()) 
  687.         {
  688.             $this->denyAccessUnlessGranted(ActionVoter::ACTION_CRUD,$action);
  689.             $entityManager $this->getDoctrine()->getManager();
  690.             /* clear all domaines */
  691.             
  692.             $domaines = [];
  693.             if(array_key_exists('domaines',$_POST['action'])){
  694.                 $action->removeAllDomaines();
  695.                 foreach($_POST['action']['domaines'] as $domain_id)
  696.                 {
  697.                     $domain $this->getDoctrine()->getRepository(Domain::class)->find($domain_id);
  698.                     $action->addDomaine($domain);
  699.                     $domaines[]=$domain;
  700.                 }
  701.             }
  702.             
  703.             /* set status */
  704.             $action->setStatus(StatusEnum::STATUS_PLANNED);
  705.             /* set validation */
  706.             if(!empty($action->getValidator()))
  707.             {
  708.                 $action->setValidated(0);
  709.             }
  710.             /* set resources */
  711.             if(!empty($action->getResourceHum()))
  712.             {
  713.                 foreach($action->getResourceHum() as $resource)
  714.                 {
  715.                     $action->addResource($resource);
  716.                 }
  717.             }
  718.             if(!empty($action->getResourceMat()))
  719.             {
  720.                 foreach($action->getResourceMat() as $resource)
  721.                 {
  722.                     $action->addResource($resource);
  723.                 }
  724.             }
  725.             /** nbr report */
  726.             
  727.             if(isset($_POST['date_fin_old']))
  728.             {
  729.                 $date_fin_old=$_POST['date_fin_old'];
  730.                 $date_fin_old.=" 00:00:00";
  731.                 $date_fin_old = \DateTime::createFromFormat("d/m/Y H:i:s",$date_fin_old);
  732.                 if($action->getDateEndEstimated() > $date_fin_old)
  733.                 {
  734.                     $nbr_report intval($action->getReportNbr()) + 1;
  735.                     $action->setReportNbr($nbr_report);
  736.                 }
  737.             }
  738.              /** actionEE */
  739.             if(isset($_POST['efficacite']))
  740.             {
  741.             
  742.                 $efficacite $_POST['efficacite'];
  743.                 if($efficacite=='1')
  744.                 {
  745.                     if($action->hasActionEE()==1)
  746.                     {
  747.                         $actionEE $action->getAction();
  748.                     }
  749.                     else
  750.                     {
  751.                         $actionEE->setCreatedBy($this->getUser());
  752.                         $action->setAction($actionEE);
  753.                         
  754.                     }
  755.                     $actionEE->setCompanyEntities($action->getCompanyEntities());
  756.                     
  757.                     
  758.                     $actionEE->setTitle($action->getTitleEE());
  759.                     /* clear all domaines */
  760.                     $actionEE->removeAllDomaines();
  761.                     foreach($action->getDomaines() as $domain)
  762.                     {
  763.                         $actionEE->addDomaine($domain);
  764.                     }
  765.                     // $type_id =$_POST['action']['type'];
  766.                     // $type= $this->getDoctrine()->getRepository(Type::class)->find($type_id);
  767.                     $actionEE->setType($action->getType());
  768.                     $actionEE->setDateStartEstimated($action->getDateStartEstimatedEE());
  769.                     $actionEE->setDateEndEstimated($action->getDateEndEstimatedEE());
  770.                     $actionEE->setDescription($action->getDescriptionEE());
  771.                     $actionEE->setTypeAction(Action::PREFIX_ACEE);
  772.                     $actionEE->setStatus(StatusEnum::STATUS_PLANNED);
  773.                     $actionEE->setPriority(PriorityEnum::PRIORITY_HIGH);
  774.                     
  775.                     
  776.                     $actionEE->setResponsible($action->getResponsibleEE());
  777.                     $actionEE->setDurationEstimated($action->getDurationEstimatedEE());
  778.                     $actionEE->setBudgetEstimated($action->getBudgetEstimatedEE());
  779.                     
  780.                     $actionEE->removeAllProcessus();
  781.                     foreach($action->getProcessusEE() as $processus)
  782.                     {
  783.                         $actionEE->addProcessus($processus);
  784.                     }
  785.                 }
  786.                 else
  787.                 {
  788.                     if($action->hasActionEE()==1)
  789.                     {
  790.                         
  791.                         
  792.                         $entityManager->remove($action->getAction());
  793.                         $action->setAction(null);
  794.                     }
  795.                     
  796.                 }
  797.             }
  798.              /** set ActionPlan */
  799.             $actionplan='';
  800.             if($_POST['actionplan'] > 0)
  801.             {
  802.                 $id_actionplan $_POST['actionplan'];
  803.                 $actionplan $entityManager->getRepository(ActionPlan::class)->find($id_actionplan);
  804.                 if(is_object($actionplan))
  805.                 {
  806.                     $action->setActionPlan($actionplan);
  807.                     $actionplan->addParticipant($action->getResponsible());
  808.                     if(is_object($action->getValidator()) )
  809.                     {
  810.                         $actionplan->addParticipant($action->getValidator());
  811.                     }
  812.                 }
  813.                 
  814.             }
  815.             /** set subjet */
  816.             if($_POST['reunion_subject'] > 0)
  817.             {
  818.                 $id_reunion_subject $_POST['reunion_subject'];
  819.                 $reunion_subject $entityManager->getRepository(ReunionSubject::class)->find($id_reunion_subject);
  820.                 if(is_object($reunion_subject))
  821.                 {
  822.                     $action->setReunionSubject($reunion_subject);
  823.                 }
  824.                 
  825.             }
  826.             $action->setUpdatedBy($this->getUser());
  827.             $action->setUpdatedAt(new \DateTime('now'));
  828.             
  829.             //find previous page
  830.             $previous_page $_POST['goback'];
  831.             
  832.             
  833.             $entityManager->flush();
  834.             
  835.             $this->addFlash("success","L'action a été modifiée avec succès");
  836.             
  837.             return $this->redirect($previous_page);
  838.             /*if(is_object($actionplan))
  839.             {
  840.                 return $this->redirectToRoute('action_plan_show',['id'=>$actionplan->getId(),'section'=>'planification']);
  841.             }
  842.             return $this->redirectToRoute('action_index');*/
  843.         }
  844.         //menu 
  845.         $menu="action";
  846.         $left_menu="pilotage_actions";
  847.         return $this->render('action/edit.html.twig', [
  848.             'action' => $action,
  849.             'actionEE' => $actionEE,
  850.             'procEE_ids' => $procEE_ids,
  851.             'form' => $form->createView(),
  852.             'menu' => $menu,
  853.             'left_menu' => $left_menu,
  854.             'date_fin_old'=>$date_fin_old,
  855.             'actionPlan'=>$actionPlan,
  856.             'reunion_subject'=>$reunion_subject,
  857.             'goback'=>$goback,
  858.             'responsibleIdOfMotherAction'=>$responsibleIdOfMotherAction
  859.         ]);
  860.     }
  861.     /**
  862.      * @Route("/delete/{id}", name="action_delete", methods={"GET"})
  863.      */
  864.     public function delete(Request $requestAction $action): Response
  865.     {
  866.        // if ($this->isCsrfTokenValid('delete'.$action->getId(), $request->request->get('_token'))) {
  867.         $this->denyAccessUnlessGranted(ActionVoter::ACTION_CRUD,$action);
  868.            $entityManager $this->getDoctrine()->getManager();
  869.             if($action->hasActionEE()){
  870.                 $actionEE $action->getAction();
  871.                 $entityManager->remove($actionEE);
  872.                 $action->setAction(null);
  873.                 $entityManager->flush();
  874.             }else{
  875.                 $actionMotherArray $this->getDoctrine()->getRepository(Action::class)->findBy(["action"=>$action]);
  876.                 if(!empty($actionMotherArray)){
  877.                     $actionMother $actionMotherArray[0];
  878.                     $actionMother->setAction(null); 
  879.                 }
  880.                 
  881.             }
  882.             $entityManager->remove($action);
  883.             $entityManager->flush();
  884.        // }
  885.         return $this->redirect($request->headers->get('referer'));
  886.         //return $this->redirectToRoute('action_index');
  887.     }
  888.     /**
  889.      * @Route("/annuler/{id}", name="action_annuler", methods={"GET","POST"})
  890.      */
  891.     public function annuler(Request $requestAction $action): Response
  892.     
  893.         $this->denyAccessUnlessGranted(ActionVoter::ACTION_CRUD,$action);
  894.         $action->setStatus(StatusEnum::STATUS_ANNULE);
  895.         $action->setReportNbr($action->getReportNbr()+1);
  896.         $this->getDoctrine()->getManager()->flush();
  897.         // return $this->redirectToRoute('reunion_index');
  898.         return $this->redirect($request->headers->get('referer'));
  899.     }
  900.     /**
  901.      * @Route("/activate/{id}", name="action_activate", methods={"GET","POST"})
  902.      */
  903.     public function activate(Request $requestAction $action): Response
  904.     {
  905.         $form $this->createForm(ActionType::class, $action, []);
  906.         if($request->getMethod() ==='POST') {
  907.             $em $this->getDoctrine()->getManager();
  908.             $responsibleId $request->request->get('responsible');
  909.             $responsible $em->getRepository(User::class)->find($responsibleId);
  910.             $action->setResponsible($responsible);
  911.             $dateEndEstimatedText $request->request->get('dateEndEstimated');
  912.             $dateEndEstimated = \DateTime::createFromFormat('d/m/Y'$dateEndEstimatedText);
  913.             $action->setDateEndEstimated($dateEndEstimated);
  914.             $durationEstimated $request->request->get('durationEstimated');
  915.             $action->setDurationEstimated($durationEstimated);
  916.             $budgetEstimated $request->request->get('budgetEstimated');
  917.             $action->setBudgetEstimated($budgetEstimated);
  918.             $action->setStatus(StatusEnum::STATUS_PLANNED);
  919.             $em->flush();
  920.             return new JsonResponse(['saved'=>1]);
  921.         }
  922.         return $this->render('action/activate.html.twig',[
  923.             'action'=>$action,
  924.             'form'=>$form->createView()
  925.         ]);
  926.     }
  927.     /**
  928.      * @Route("/updateCell", name="update_cell", methods={"POST"})
  929.      */
  930.     public function updateCell(Request $request)
  931.     {
  932.         if (!$request->isXmlHttpRequest()) {
  933.             return new JsonResponse(array('status' => 'error','message' => 'Access forbidden!'), 400);
  934.         }
  935.         if(isset($request->request))
  936.         {
  937.             $entityManager $this->getDoctrine()->getManager();
  938.             $id $request->request->get('id');
  939.             $columnName $request->request->get('columnName');
  940.             $columnValue $request->request->get('columnValue');
  941.             if($columnValue ==="") {
  942.                 return new JsonResponse(array('status' => 'not updated'), 201);
  943.             }
  944.             $id =intval($id);
  945.             $action $entityManager->getRepository(Action::class)->find($id);
  946.             //$old_status = $action->getStatus();
  947.             $this->denyAccessUnlessGranted(ActionVoter::ACTION_INSERT,$action);
  948.             $statutEE "";
  949.             $idEE="";
  950.             $efficaciteActionMother="";
  951.             $statutActionMother="";
  952.             $idActionMother="";
  953.             /*if($old_status !=StatusEnum::STATUS_IN_PROGRESS)
  954.             {
  955.                 return new JsonResponse(array('status' => 'error','message' => 'status forbidden'), 200);
  956.             }*/
  957.             $actionMotherNotClosed="";
  958.             $actionMother $entityManager->getRepository(Action::class)->findOneBy(array('action'=>$action));
  959.             if($actionMother)
  960.             {
  961.                 if(!in_array($actionMother->getStatus(),[StatusEnum::STATUS_CLOSED,StatusEnum::STATUS_ATTENTE_EE]))
  962.                 {
  963.                     $actionMotherNotClosed=$actionMother->getRef();
  964.                     return new JsonResponse([ 'actionMotherNotClosed'=>$actionMotherNotClosed],200);
  965.                 }
  966.             }
  967.             
  968.             if($columnName=="progress")
  969.             {
  970.                 $progress intval($columnValue);
  971.                 $action->setProgress($progress);
  972.             }
  973.             elseif($columnName=="dateStartReal")
  974.             {
  975.                 
  976.                 $date str_replace('/''-'$columnValue);
  977.                 $dateStartReal date('Y-m-d'strtotime($date));
  978.                 $dateStartReal =\DateTime::createFromFormat('Y-m-d'$dateStartReal);
  979.                 $action->setDateStartReal($dateStartReal);
  980.                 if($action->getStatus() == StatusEnum::STATUS_PLANNED)
  981.                 {
  982.                     $action->setStatus(StatusEnum::STATUS_IN_PROGRESS);
  983.                     if($action->getActionPlan())
  984.                     {
  985.                         $action->getActionPlan()->setStatus(StatusEnum::STATUS_ACPL_IN_PROGRESS);
  986.                     }
  987.                 }
  988.             }
  989.             elseif($columnName=="dateEndReal")
  990.             {
  991.                 $date str_replace('/''-'$columnValue);
  992.                 $dateEndReal date('Y-m-d'strtotime($date));
  993.                 $dateEndReal =\DateTime::createFromFormat('Y-m-d'$dateEndReal);
  994.                 $action->setDateEndReal($dateEndReal);
  995.                 $action->setProgress(100);
  996.                 if($action->getAction())
  997.                 {
  998.                     $action->setStatus(StatusEnum::STATUS_ATTENTE_EE);
  999.                     //$action->getAction()->setStatus(StatusEnum::STATUS_IN_PROGRESS);
  1000.                     $statutEE $action->getAction()->getStatus();
  1001.                     $idEE=$action->getAction()->getId();
  1002.                 }
  1003.                 else
  1004.                 {
  1005.                     $action->setStatus(StatusEnum::STATUS_CLOSED);
  1006.                     
  1007.                 }
  1008.             }
  1009.             elseif($columnName=="durationReal")
  1010.             {
  1011.                 $durationRealfloatval($columnValue);
  1012.                 $action->setDurationReal($durationReal);
  1013.             }
  1014.             elseif($columnName=="budgetReal")
  1015.             {
  1016.                 $budgetRealfloatval($columnValue);
  1017.                 $action->setBudgetReal($budgetReal);
  1018.             }
  1019.             elseif($columnName=="efficacite")
  1020.             {
  1021.                 $efficacite intval($columnValue);
  1022.                 $action->setDateEndReal(new \DateTime());
  1023.                 $action->setStatus(StatusEnum::STATUS_CLOSED);
  1024.                 $action->setProgress(100);
  1025.                 if($actionMother)
  1026.                 {
  1027.                     $actionMother->setStatus(StatusEnum::STATUS_CLOSED);
  1028.                     $actionMother->setEfficient($efficacite);
  1029.                     $entityManager->persist($actionMother);
  1030.                     
  1031.                     $idActionMother=$actionMother->getId();
  1032.                     
  1033.                     $efficaciteActionMother=strval($efficacite);
  1034.                     
  1035.                     
  1036.                     $statutActionMother=$actionMother->getStatus();
  1037.                 }
  1038.             }
  1039.             $entityManager->persist($action);
  1040.             try 
  1041.             {
  1042.                 $entityManager->flush();
  1043.                 
  1044.                 $respectBudget ="OK";
  1045.                 if($action->getBudgetEstimated()<$action->getBudgetReal()) $respectBudget ="NOK";
  1046.                 $respectDuree "OK";
  1047.                 if($action->getDurationEstimated()<$action->getDurationReal()) $respectDuree ="NOK";
  1048.                 $respectDelais "OK";
  1049.                 $joursRetard="0";
  1050.                 if($action->getDateEndEstimated()<$action->getDateEndReal()) 
  1051.                 {
  1052.                     $respectDelais ="NOK";
  1053.                     $joursRetard $action->getDateEndEstimated()->diff($action->getDateEndReal())->format("%a");
  1054.                 }
  1055.                 $statut $action->getStatus();
  1056.                 
  1057.                 
  1058.                 $data = [
  1059.                     'statut'=> $statut,
  1060.                     'respectDuree'=> $respectDuree,
  1061.                     'joursRetard'=> $joursRetard,
  1062.                     'respectDelais'=> $respectDelais,
  1063.                     'respectBudget'=> $respectBudget,
  1064.                     'statutEE'=> $statutEE,
  1065.                     'idEE'=>$idEE,
  1066.                     'efficaciteActionMother'=>$efficaciteActionMother,
  1067.                     'statutActionMother'=>$statutActionMother,
  1068.                     'idActionMother'=>$idActionMother,
  1069.                     'actionMotherNotClosed'=>$actionMotherNotClosed
  1070.                 ];
  1071.                 $response =$data;
  1072.             } catch (\Exception $e) {
  1073.                 $response = array('status' => 'error''message' => $e->getMessage());
  1074.             }
  1075.         }
  1076.         return new JsonResponse($response,200);
  1077.     }
  1078.     /**
  1079.      * @Route("/{id}/validation", name="action_validation", methods={"GET"})
  1080.      */
  1081.     public function validation(Action $actionRequest $request): Response
  1082.     {
  1083.         $this->denyAccessUnlessGranted(ActionVoter::ACTION_INSERT,$action);
  1084.         $action->setStatus(StatusEnum::STATUS_IN_PROGRESS);
  1085.         //$action->setValidated(0);
  1086.         $action->setValidated(1);
  1087.         $this->getDoctrine()->getManager()->flush();
  1088.         $this->addFlash("success","L'action a été validé avec succès");
  1089.         return $this->redirect($request->headers->get('referer'));
  1090.     }
  1091.     
  1092.     /**
  1093.      * @Route("/{id}/comment", name="action_comment", methods={"GET"})
  1094.      */
  1095.     public function action_comment(Action $actionRequest $request) : Response
  1096.     {
  1097.         
  1098.         return $this->render('action/comment.html.twig', [
  1099.             'action'=>$action
  1100.         ]);
  1101.     }
  1102.     /**
  1103.      * @Route("/{id}/comment/save", name="action_comment_save", methods={"POST"})
  1104.      */
  1105.     public function action_comment_save(Action $actionRequest $request) : JsonResponse
  1106.     {
  1107.         $comment $request->request->get('comment','');
  1108.         $action->setComment($comment);
  1109.         $entityManager $this->getDoctrine()->getManager();
  1110.         $entityManager->flush();
  1111.         return new JsonResponse(['saved'=>1]);
  1112.     }
  1113.     public function createPeriodActions(Action $action)
  1114.     {
  1115.         $nextAction = clone $action;
  1116.         $increment $action->hasActionEE() ? 21;
  1117.         
  1118.         $ref Utility::incrementRef($action->getRef(),$increment);
  1119.         $action->setRef($ref);
  1120.         $numberOfDays PeriodActionEnum::getPeriodValue($action->getPeriodicity());
  1121.         $dateEndEstimated =  $action->getDateEndEstimated();
  1122.         $dateEndEstimated->add(new \DateInterval('P'.$numberOfDays.'D'));
  1123.         $action->setDateEndEstimated($dateEndEstimated);
  1124.         
  1125.     }
  1126. }