src/Controller/ActionPlanController.php line 86

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Configuration\CompanyEntity;
  4. use App\Repository\ActionPlanRepository;
  5. use App\Entity\ActionPlan;
  6. use App\Entity\bilan\Entity;
  7. use App\Entity\Comments;
  8. use App\Entity\Domain;
  9. use App\Entity\Dysfonctionnement\Constat;
  10. use App\Entity\Indicator;
  11. use App\Entity\IndicatorValues;
  12. use App\Entity\Processus;
  13. use App\Entity\Source;
  14. use App\Entity\Type;
  15. use App\Entity\User;
  16. use App\Enum\CategoryEnum;
  17. use App\Enum\StatusEnum;
  18. use App\Form\ActionPlanType;
  19. use App\Form\IndicatorType;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. use App\Utils\Utility;
  25. use Doctrine\Common\CommonException;
  26. use Symfony\Component\HttpFoundation\JsonResponse;
  27. use App\Notification\SendNotification;
  28. use App\Security\ActionVoter;
  29. /**
  30.  * @Route("/action-plan")
  31.  */
  32. class ActionPlanController extends AbstractController
  33. {
  34.     /**
  35.      * @var SendNotification
  36.      */
  37.     private $notifyActionPlanValidator;
  38.     private $notifyActionPlanValid;
  39.     private $notifyActionPlanRefused;
  40.     private $notifyActionPlanRefusedResponsible;
  41.     public function __construct
  42.         SendNotification $notifyActionPlanValidator
  43.         SendNotification $notifyActionPlanValid
  44.         SendNotification $notifyActionPlanRefused,
  45.         SendNotification $notifyActionPlanRefusedResponsible
  46.         
  47.         )
  48.     {
  49.         $this->notifyActionPlanValidator $notifyActionPlanValidator;
  50.         $this->notifyActionPlanValid $notifyActionPlanValid;
  51.         $this->notifyActionPlanRefused $notifyActionPlanRefused;
  52.         $this->notifyActionPlanRefusedResponsible $notifyActionPlanRefusedResponsible;
  53.     }
  54.     
  55.     /**
  56.      * @Route("/", name="action_plan_list", methods={"GET"})
  57.      */
  58.     public function index(): Response
  59.     {
  60.         
  61.         $processus_list $this->getDoctrine()->getRepository(Processus::class)->findAll();
  62.         $entities $this->getDoctrine()->getRepository(CompanyEntity::class)->findBy([],['nom'=>'ASC']);
  63.         $domains $this->getDoctrine()->getRepository(Domain::class)->findAll();
  64.         //menu 
  65.         $menu="action";
  66.         $left_menu="pilotage_actionPlans";
  67.         return $this->render('action_plan/index.html.twig',[
  68.             'menu'=>$menu,
  69.             'left_menu'=>$left_menu,
  70.             'domains'=>$domains,
  71.             'processus_list'=>$processus_list,
  72.             'entities'=>$entities
  73.             
  74.         ]);
  75.     }
  76.     /**
  77.      * @Route("/list", name="actionplan_list", methods={"GET"})
  78.      */
  79.     public function list(Request $requestActionPlanRepository $actionPlanRepository)
  80.     {
  81.         $startDate=$request->query->get('startDate','');
  82.         $endDate=$request->query->get('endDate','');
  83.         $user $this->getUser();
  84.         $actionPlansData $actionPlanRepository->transformAll($user,$startDate,$endDate);
  85.         return new JsonResponse($actionPlansData);
  86.     }
  87.     /**
  88.      * @Route("/new", name="action_plan_new", methods={"GET","POST"})
  89.      */
  90.     public function new(Request $request): Response
  91.     {
  92.         $this->denyAccessUnlessGranted(ActionVoter::ACTION_ACTIONPLAN_CRUD);
  93.         $actionPlan = new ActionPlan();
  94.         $user$this->getUser();
  95.         
  96.         $options = [];
  97.         $constat_id $request->query->get('constat','');
  98.         $type_plan $request->query->get('type','');
  99.         $actionPlan_typeActionPlan::TYPE_PA_PROGRES;
  100.         $prefix=ActionPlan::PREFIX;
  101.         if($constat_id) {
  102.             $constat $this->getDoctrine()->getRepository(Constat::class)->find($constat_id);
  103.             switch ($constat->getType()) {
  104.                 case Constat::TYPE_DYS:
  105.                     $actionPlan_type=ActionPlan::TYPE_PA_DYS;
  106.                     $prefix ActionPlan::PREFIX_DYS;
  107.                     break;
  108.                 case Constat::TYPE_REC:
  109.                     $actionPlan_type=ActionPlan::TYPE_PA_REC;
  110.                     $prefix ActionPlan::PREFIX_REC;
  111.                     break;
  112.                 case Constat::TYPE_ACC:
  113.                     $actionPlan_type=ActionPlan::TYPE_PA_ACC;
  114.                     $prefix ActionPlan::PREFIX_ACC;
  115.                     break;
  116.                 
  117.                 default:
  118.                     $actionPlan_type=ActionPlan::TYPE_PA_PROGRES;
  119.                     $prefix ActionPlan::PREFIX;
  120.                     break;
  121.             }
  122.             
  123.             $actionPlan->setConstat($constat);
  124.             $domaine $constat->getDomain();
  125.             $actionPlan->setDomain($domaine);
  126.             $options['disabled_domain']=true;
  127.             if($type_plan) {
  128.                 $type $this->getDoctrine()->getRepository(Type::class)->findOneBy(['title'=>'Risque-Préventif']);
  129.                 $actionPlan->setType($type);
  130.                 $options['disabled_type']=true;
  131.             }
  132.             $source $constat->getSource();
  133.             $actionPlan->setSource($source);
  134.             $options['disabled_source']=true;
  135.             $processus $constat->getProcessus();
  136.             $actionPlan->setProcessus($processus);
  137.             $options['disabled_processus']=true;
  138.         }
  139.         $actionPlan->setTypePa($actionPlan_type);
  140.         $lastId=0;
  141.         $lastActionPlan $this->getDoctrine()->getRepository(ActionPlan::class)->findOneBy([], ['id' => 'desc']);
  142.         if(is_object($lastActionPlan))
  143.         {
  144.             $lastId $lastActionPlan->getId();
  145.         }
  146.         $utility = new Utility;
  147.         $ref $utility->generateRef($lastId,$prefix);
  148.         $actionPlan->setRef($ref);
  149.         
  150.         $form $this->createForm(ActionPlanType::class, $actionPlan$options);
  151.         $form->handleRequest($request);
  152.         
  153.         
  154.         if ($form->isSubmitted()) {
  155.             
  156.             $actionPlanData$request->request->get('action_plan');
  157.             
  158.             $sign$actionPlanData['sign'];
  159.             $cible$actionPlanData['cible'];
  160.             $indicator_id$actionPlanData['indicator'];
  161.             $indicator $this->getDoctrine()->getRepository(Indicator::class)->find($indicator_id);
  162.             
  163.            
  164.             $indicator->setResponsible($user);
  165.             $indicator->setSign($sign);
  166.             $indicator->setCible($cible);
  167.             $indicator->setType(Indicator::TYPE_OBJECTIF);
  168.             $indicator->setStatus(Indicator::STATUS_VALIDATED);
  169.             $indicator->setActivity(NULL);
  170.             $indicator->setProcessus($actionPlan->getProcessus());
  171.             $actionPlan->setIndicator($indicator);
  172.            
  173.             
  174.             /* set status*/
  175.             $actionPlan->setStatus(StatusEnum::STATUS_ACPL_PLANNED);
  176.             // if($actionPlan->getDateStartEstimated() > $actionPlan->getCreatedAt())
  177.             // {
  178.             //     $actionPlan->setStatus(StatusEnum::STATUS_PLANNED);
  179.             // }
  180.          
  181.             /* createdBy */
  182.             $actionPlan->setCreatedBy($user);
  183.             $actionPlan->addParticipant($user);
  184.             $actionPlan->addParticipant($actionPlan->getResponsible());
  185.             /* set resources */
  186.             if(!empty($actionPlan->getResourceHum()))
  187.             {
  188.                 foreach($actionPlan->getResourceHum() as $resource)
  189.                 {
  190.                     $actionPlan->addResource($resource);
  191.                 }
  192.             }
  193.             if(!empty($actionPlan->getResourceMat()))
  194.             {
  195.                 foreach($actionPlan->getResourceMat() as $resource)
  196.                 {
  197.                     $actionPlan->addResource($resource);
  198.                 }
  199.             }
  200.             /* set validation */
  201.             if(!empty($actionPlan->getValidator()))
  202.             {
  203.                 $actionPlan->setStatus(StatusEnum::STATUS_ACPL_PLANNED);
  204.                 $actionPlan->addParticipant($actionPlan->getValidator());
  205.                 $actionPlan->setValidated(0);
  206.             //     $this->notifyActionPlanValidator->notifyActionPlanValidator(
  207.             //     $actionPlan->getCreatedBy()->getEmail(),
  208.             //     $actionPlan->getValidator()->getEmail(),
  209.             //     $actionPlan->getRef(),
  210.             //     $actionPlan->getId()
  211.             // );
  212.             }
  213.             // add comment dashboard
  214.             $comment =  new Comments();
  215.             $comment->setCreatedBy($actionPlan->getCreatedBy());
  216.             $actionPlan->setCommentDashboard($comment);
  217.             
  218.             $entityManager $this->getDoctrine()->getManager();
  219.             $entityManager->persist($actionPlan);
  220.             $entityManager->flush();
  221.             
  222.             if($actionPlan->getValidator())
  223.             {
  224.                 
  225.                 $this->notifyActionPlanValidator->notifyActionPlanValidator(
  226.                 $actionPlan->getCreatedBy()->getEmail(),
  227.                 $actionPlan->getValidator()->getEmail(),
  228.                 $actionPlan->getRef(),
  229.                 $actionPlan->getId()
  230.             );
  231.             }
  232.             $this->addFlash("success","Le plan d'action a été créé avec succès");
  233.             return $this->redirectToRoute('action_plan_list');
  234.         }
  235.         //dd($request);
  236.         //menu 
  237.         $menu="action";
  238.         $left_menu="new_actionPlan";
  239.         return $this->render('action_plan/new.html.twig', [
  240.             'action_plan' => $actionPlan,
  241.             'form' => $form->createView(),
  242.             'menu' => $menu,
  243.             'left_menu'=>$left_menu
  244.         ]);
  245.     }
  246.     /**
  247.      * @Route("/{id}", name="action_plan_show", methods={"GET"})
  248.      */
  249.     public function show(ActionPlan $actionPlan): Response
  250.     {
  251.         if(empty($actionPlan->getCommentDashboard()))
  252.         {
  253.             $entityManager $this->getDoctrine()->getManager();
  254.             $comment =  new Comments();
  255.             $comment->setCreatedBy($actionPlan->getCreatedBy());
  256.             $actionPlan->setCommentDashboard($comment);
  257.             $entityManager->flush();
  258.         }
  259.         
  260.         //menu 
  261.         $menu="action";
  262.         $left_menu="pilotage_actionPlans";
  263.         return $this->render('action_plan/show.html.twig', [
  264.             'actionPlan' => $actionPlan,
  265.             'menu' => $menu,
  266.             'left_menu' => $left_menu
  267.         ]);
  268.     }
  269.     /**
  270.      * @Route("/{id}/edit", name="action_plan_edit", methods={"GET","POST"})
  271.      */
  272.     public function edit(Request $requestActionPlan $actionPlan): Response
  273.     {
  274.         $user$this->getUser();
  275.         $options['actionplan_sign'] = $actionPlan->getIndicator()->getSign();
  276.         $form $this->createForm(ActionPlanType::class, $actionPlan,$options);
  277.         
  278.         $form->handleRequest($request);
  279.         
  280.         
  281.         if ($form->isSubmitted()) 
  282.         {
  283.             $this->denyAccessUnlessGranted(ActionVoter::ACTION_ACTIONPLAN_CRUD);
  284.             $actionPlanData$request->request->get('action_plan');
  285.             $sign$actionPlanData['sign'];
  286.             $cible$actionPlanData['cible'];
  287.             $new_indicator_id$actionPlanData['indicator'];
  288.             $oldIndicator $actionPlan->getIndicator();
  289.             if($oldIndicator->getId() != $new_indicator_id) {
  290.                 $this->getDoctrine()->getManager()->remove($oldIndicator);
  291.                 $indicator $this->getDoctrine()->getRepository(Indicator::class)->find($new_indicator_id);
  292.                 
  293.                 $indicator->setStatus(Indicator::STATUS_VALIDATED);
  294.             }else {
  295.                 $indicator $oldIndicator;
  296.             }
  297.             
  298.             
  299.            
  300.             $indicator->setResponsible($user);
  301.             $indicator->setSign($sign);
  302.             $indicator->setCible($cible);
  303.             $indicator->setType(Indicator::TYPE_OBJECTIF);
  304.             $indicator->setActivity(NULL);
  305.             $indicator->setProcessus($actionPlan->getProcessus());
  306.             $actionPlan->setIndicator($indicator);
  307.             if(!empty($actionPlan->getValidator()))
  308.             {
  309.                 $actionPlan->addParticipant($actionPlan->getValidator());
  310.             }
  311.             /* set resources */
  312.             $array_new_resources_hum=[];
  313.             if(!empty($actionPlan->getResourceHum()))
  314.             {
  315.                 foreach($actionPlan->getResourceHum() as $resource)
  316.                 {
  317.                     $array_new_resources_hum[]=$resource;
  318.                     $actionPlan->addResource($resource);
  319.                 }
  320.             }
  321.             
  322.             $array_new_resources_mat=[];
  323.             if(!empty($actionPlan->getResourceMat()))
  324.             {
  325.                 foreach($actionPlan->getResourceMat() as $resource)
  326.                 {
  327.                     $array_new_resources_mat[]=$resource;
  328.                     $actionPlan->addResource($resource);
  329.                 }
  330.             }
  331.             $removed_resources_hum=array_diff($actionPlan->getResource('res_hum'),$array_new_resources_hum);
  332.             $removed_resources_mat=array_diff($actionPlan->getResource('res_mat'),$array_new_resources_mat);
  333.             $removed_resources array_merge($removed_resources_hum$removed_resources_mat);
  334.             foreach($removed_resources as $resource)
  335.             {
  336.                 $actionPlan->removeResource($resource);
  337.             }
  338.             $actionPlan->addParticipant($actionPlan->getResponsible());
  339.             $this->getDoctrine()->getManager()->flush();
  340.             
  341.             $this->addFlash("success","Le plan d'action a été modifié avec succès");
  342.             return $this->redirectToRoute('action_plan_list');
  343.             //return $this->redirectToRoute('action_plan_show',['id'=>$actionPlan->getId(),'section'=>'view']);
  344.         }
  345.         
  346.         return $this->render('action_plan/edit.html.twig', [
  347.             'action_plan' => $actionPlan,
  348.             'form' => $form->createView()
  349.             
  350.         ]);
  351.     }
  352.     /**
  353.      * @Route("/delete/{id}", name="action_plan_delete", methods={"GET"})
  354.      */
  355.     public function delete(Request $requestActionPlan $actionPlan): Response
  356.     {
  357.         $this->denyAccessUnlessGranted(ActionVoter::ACTION_ACTIONPLAN_CRUD);
  358.         $entityManager $this->getDoctrine()->getManager();
  359.         $entityManager->remove($actionPlan);
  360.         $entityManager->flush();
  361.        
  362.         return $this->redirect($request->headers->get('referer'));
  363.     }
  364.     /**
  365.      * @Route("/{id}/planification", name="action_plan_planification", methods={"GET"})
  366.      */
  367.     public function planification(ActionPlan $actionPlan): Response
  368.     {
  369.         $users $this->getDoctrine()->getRepository(User::class)->findAll();
  370.         $processus $this->getDoctrine()->getRepository(Processus::class)->findAll();
  371.         $sources $this->getDoctrine()->getRepository(Source::class)->findAll();
  372.         $domains $this->getDoctrine()->getRepository(Domain::class)->findAll();
  373.         $types $this->getDoctrine()->getRepository(Type::class)->findAll();
  374.         $categories CategoryEnum::getAvailableCategories();
  375.         
  376.         //menu 
  377.         $menu="action";
  378.         return $this->render('action_plan/planification.html.twig',[
  379.             'actionPlan'=>$actionPlan,
  380.             'users'=>$users,
  381.             'processus'=>$processus,
  382.             'sources'=>$sources,
  383.             'domains'=>$domains,
  384.             'types'=>$types,
  385.             'categories'=>$categories,
  386.             'menu'=>$menu,
  387.         ]);
  388.     }
  389.     /**
  390.      * @Route("/{id}/validation", name="action_plan_validation", methods={"GET"})
  391.      */
  392.     public function validation(ActionPlan $actionPlan): Response
  393.     {
  394.         $this->denyAccessUnlessGranted(ActionVoter::ACTION_ACTIONPLAN_CRUD);
  395.         $actionPlan->setStatus(StatusEnum::STATUS_ACPL_IN_PROGRESS);
  396.         $actionPlan->setValidated(1);
  397.         $this->getDoctrine()->getManager()->flush();
  398.         $this->addFlash("success","Le plan d'action a été validé avec succès");
  399.         $this->notifyActionPlanValid->notifyActionPlanValid(
  400.             $actionPlan->getCreatedBy()->getEmail(),
  401.             $actionPlan->getValidator()->getEmail(),
  402.             $actionPlan->getRef(),
  403.             $actionPlan->getId()
  404.         );
  405.         return $this->redirectToRoute('action_plan_list');
  406.     }
  407.     /**
  408.      * @Route("/{id}/refused", name="action_plan_refused", methods={"GET"})
  409.      */
  410.     public function refused(ActionPlan $actionPlan): Response
  411.     {
  412.         $this->denyAccessUnlessGranted(ActionVoter::ACTION_ACTIONPLAN_CRUD);
  413.         $actionPlan->setStatus(StatusEnum::STATUS_ACPL_ANNULE);
  414.         $actionPlan->setValidated(2);
  415.         $this->getDoctrine()->getManager()->flush();
  416.         $this->addFlash("success","Le plan d'action a été refusé");
  417.         $this->notifyActionPlanRefused->notifyActionPlanRefused(
  418.             $actionPlan->getCreatedBy()->getEmail(),
  419.             $actionPlan->getValidator()->getEmail(),
  420.             $actionPlan->getRef(),
  421.             $actionPlan->getId()
  422.         );
  423.         $this->notifyActionPlanRefusedResponsible->notifyActionPlanRefusedResponsible(
  424.             $actionPlan->getCreatedBy()->getEmail(),
  425.             $actionPlan->getResponsible()->getEmail(),
  426.             $actionPlan->getRef(),
  427.             $actionPlan->getId()
  428.         );
  429.         return $this->redirectToRoute('action_plan_list');
  430.     }
  431.     /**
  432.      * @Route("/annuler/{id}", name="action_plan_annuler", methods={"GET","POST"})
  433.      */
  434.     public function annulerPa(Request $requestActionPlan $actionPlan): Response
  435.     {
  436.         $this->denyAccessUnlessGranted(ActionVoter::ACTION_ACTIONPLAN_CRUD);
  437.         $actionPlan->setStatus(StatusEnum::STATUS_ACPL_ANNULE);
  438.         $this->getDoctrine()->getManager()->flush();
  439.         
  440.         return $this->redirectToRoute('action_plan_list');
  441.     }
  442.     /**
  443.      * @Route("/external/new", name="new_pa_external", methods={"GET","POST"})
  444.      */
  445.     public function new_pa(Request $request): Response
  446.     {
  447.         $this->denyAccessUnlessGranted(ActionVoter::ACTION_ACTIONPLAN_CRUD);
  448.         $actionPlan = new ActionPlan();
  449.         $user$this->getUser();
  450.         
  451.         /* genretate ref */
  452.         $lastId=0;
  453.         $lastActionPlan $this->getDoctrine()->getRepository(ActionPlan::class)->findOneBy([], ['id' => 'desc']);
  454.         if(is_object($lastActionPlan))
  455.         {
  456.             $lastId $lastActionPlan->getId();
  457.         }
  458.         $utility = new Utility;
  459.         $ref $utility->generateRef($lastId,ActionPlan::PREFIX_DYS);
  460.         $actionPlan->setRef($ref);
  461.         /* end genretate ref*/
  462.         $options=[];
  463.         $constat='';
  464.         $constat_id $request->query->get('constat','');
  465.         $temp $request->query->get('temp','');
  466.         if($constat_id) {
  467.             $constat $this->getDoctrine()->getRepository(Constat::class)->find($constat_id);
  468.             $domaine $constat->getDomain();
  469.             $actionPlan->setDomain($domaine);
  470.             $options['disabled_domain']=true;
  471.             if($temp) {
  472.                 $type $this->getDoctrine()->getRepository(Type::class)->findOneBy(['title'=>'Correctif temporaire']);
  473.             }else {
  474.                 $type $this->getDoctrine()->getRepository(Type::class)->findOneBy(['title'=>'Correctif définitif']);
  475.             }
  476.             $actionPlan->setType($type);
  477.             $options['disabled_type']=true;
  478.             $source $constat->getSource();
  479.             $actionPlan->setSource($source);
  480.             $options['disabled_source']=true;
  481.             $processus $constat->getProcessus();
  482.             $actionPlan->setProcessus($processus);
  483.             $options['disabled_processus']=true;
  484.         }
  485.         // $options['disabled_domain']=true;
  486.         // $options['disabled_type']=true;
  487.         // $options['disabled_source']=true;
  488.         
  489.         $form $this->createForm(ActionPlanType::class, $actionPlan$options);
  490.         $form->handleRequest($request);
  491.         if($form->isSubmitted() && $form->isValid()) {
  492.             $actionPlan->setStatus(StatusEnum::STATUS_ACPL_PLANNED);
  493.             
  494.             /* createdBy */
  495.             $actionPlan->setCreatedBy($user);
  496.             $actionPlan->addParticipant($user);
  497.             $actionPlan->addParticipant($actionPlan->getResponsible());
  498.             if($constat) {
  499.                 $constat->addActionPlan($actionPlan);
  500.             }
  501.             $entityManager $this->getDoctrine()->getManager();
  502.             $entityManager->persist($actionPlan);
  503.             $entityManager->flush();
  504.             
  505.             if($actionPlan->getValidator())
  506.             {
  507.                 
  508.                 $this->notifyActionPlanValidator->notifyActionPlanValidator(
  509.                     $actionPlan->getCreatedBy()->getEmail(),
  510.                     $actionPlan->getValidator()->getEmail(),
  511.                     $actionPlan->getRef(),
  512.                     $actionPlan->getId()
  513.                 );
  514.             }
  515.             $this->addFlash("success","Le plan d'action a été créé avec succès");
  516.             return $this->redirectToRoute('action_plan_show',['id'=>$actionPlan->getId()]);
  517.         }
  518.         $users_list $this->getDoctrine()->getRepository(User::class)->findAll();
  519.         $users_list_array = [];
  520.         foreach($users_list as $user) {
  521.             $fonction =  str_replace('"'""$user->getFonction());
  522.             $users_list_array[$user->getId()] = $fonction;
  523.         }
  524.         
  525.         //menu 
  526.         $menu="action";
  527.         $left_menu="pilotage_actionPlans";
  528.         return $this->render('action_plan/new_pa.html.twig',[
  529.             'menu'=>$menu,
  530.             'left_menu'=>$left_menu,
  531.             'form'=>$form->createView(),
  532.             'users_list_array'=>$users_list_array
  533.         ]);
  534.     }
  535.     /**
  536.      * @Route("/terminer_planification/{id}", name="terminer_planification", methods={"GET"})
  537.      */
  538.     public function terminer_planification(ActionPlan $actionPlan)
  539.     {
  540.         $this->denyAccessUnlessGranted(ActionVoter::ACTION_ACTIONPLAN_CRUD);
  541.         $actionPlan->setPlanificationClosed(true);
  542.         $this->getDoctrine()->getManager()->flush();
  543.         if($actionPlan->getConstat()) {
  544.             return $this->redirectToRoute('analyse',['id'=>$actionPlan->getConstat()->getId()]);
  545.         }
  546.         return $this->redirectToRoute('action_plan_show',['id'=>$actionPlan->getId()]);
  547.     }
  548.     
  549. }