<?php
namespace App\Controller;
use App\Entity\Configuration\CompanyEntity;
use App\Repository\ActionPlanRepository;
use App\Entity\ActionPlan;
use App\Entity\bilan\Entity;
use App\Entity\Comments;
use App\Entity\Domain;
use App\Entity\Dysfonctionnement\Constat;
use App\Entity\Indicator;
use App\Entity\IndicatorValues;
use App\Entity\Processus;
use App\Entity\Source;
use App\Entity\Type;
use App\Entity\User;
use App\Enum\CategoryEnum;
use App\Enum\StatusEnum;
use App\Form\ActionPlanType;
use App\Form\IndicatorType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Utils\Utility;
use Doctrine\Common\CommonException;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Notification\SendNotification;
use App\Security\ActionVoter;
/**
* @Route("/action-plan")
*/
class ActionPlanController extends AbstractController
{
/**
* @var SendNotification
*/
private $notifyActionPlanValidator;
private $notifyActionPlanValid;
private $notifyActionPlanRefused;
private $notifyActionPlanRefusedResponsible;
public function __construct(
SendNotification $notifyActionPlanValidator,
SendNotification $notifyActionPlanValid,
SendNotification $notifyActionPlanRefused,
SendNotification $notifyActionPlanRefusedResponsible
)
{
$this->notifyActionPlanValidator = $notifyActionPlanValidator;
$this->notifyActionPlanValid = $notifyActionPlanValid;
$this->notifyActionPlanRefused = $notifyActionPlanRefused;
$this->notifyActionPlanRefusedResponsible = $notifyActionPlanRefusedResponsible;
}
/**
* @Route("/", name="action_plan_list", methods={"GET"})
*/
public function index(): Response
{
$processus_list = $this->getDoctrine()->getRepository(Processus::class)->findAll();
$entities = $this->getDoctrine()->getRepository(CompanyEntity::class)->findBy([],['nom'=>'ASC']);
$domains = $this->getDoctrine()->getRepository(Domain::class)->findAll();
//menu
$menu="action";
$left_menu="pilotage_actionPlans";
return $this->render('action_plan/index.html.twig',[
'menu'=>$menu,
'left_menu'=>$left_menu,
'domains'=>$domains,
'processus_list'=>$processus_list,
'entities'=>$entities
]);
}
/**
* @Route("/list", name="actionplan_list", methods={"GET"})
*/
public function list(Request $request, ActionPlanRepository $actionPlanRepository)
{
$startDate=$request->query->get('startDate','');
$endDate=$request->query->get('endDate','');
$user = $this->getUser();
$actionPlansData = $actionPlanRepository->transformAll($user,$startDate,$endDate);
return new JsonResponse($actionPlansData);
}
/**
* @Route("/new", name="action_plan_new", methods={"GET","POST"})
*/
public function new(Request $request): Response
{
$this->denyAccessUnlessGranted(ActionVoter::ACTION_ACTIONPLAN_CRUD);
$actionPlan = new ActionPlan();
$user= $this->getUser();
$options = [];
$constat_id = $request->query->get('constat','');
$type_plan = $request->query->get('type','');
$actionPlan_type= ActionPlan::TYPE_PA_PROGRES;
$prefix=ActionPlan::PREFIX;
if($constat_id) {
$constat = $this->getDoctrine()->getRepository(Constat::class)->find($constat_id);
switch ($constat->getType()) {
case Constat::TYPE_DYS:
$actionPlan_type=ActionPlan::TYPE_PA_DYS;
$prefix = ActionPlan::PREFIX_DYS;
break;
case Constat::TYPE_REC:
$actionPlan_type=ActionPlan::TYPE_PA_REC;
$prefix = ActionPlan::PREFIX_REC;
break;
case Constat::TYPE_ACC:
$actionPlan_type=ActionPlan::TYPE_PA_ACC;
$prefix = ActionPlan::PREFIX_ACC;
break;
default:
$actionPlan_type=ActionPlan::TYPE_PA_PROGRES;
$prefix = ActionPlan::PREFIX;
break;
}
$actionPlan->setConstat($constat);
$domaine = $constat->getDomain();
$actionPlan->setDomain($domaine);
$options['disabled_domain']=true;
if($type_plan) {
$type = $this->getDoctrine()->getRepository(Type::class)->findOneBy(['title'=>'Risque-Préventif']);
$actionPlan->setType($type);
$options['disabled_type']=true;
}
$source = $constat->getSource();
$actionPlan->setSource($source);
$options['disabled_source']=true;
$processus = $constat->getProcessus();
$actionPlan->setProcessus($processus);
$options['disabled_processus']=true;
}
$actionPlan->setTypePa($actionPlan_type);
$lastId=0;
$lastActionPlan = $this->getDoctrine()->getRepository(ActionPlan::class)->findOneBy([], ['id' => 'desc']);
if(is_object($lastActionPlan))
{
$lastId = $lastActionPlan->getId();
}
$utility = new Utility;
$ref = $utility->generateRef($lastId,$prefix);
$actionPlan->setRef($ref);
$form = $this->createForm(ActionPlanType::class, $actionPlan, $options);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$actionPlanData= $request->request->get('action_plan');
$sign= $actionPlanData['sign'];
$cible= $actionPlanData['cible'];
$indicator_id= $actionPlanData['indicator'];
$indicator = $this->getDoctrine()->getRepository(Indicator::class)->find($indicator_id);
$indicator->setResponsible($user);
$indicator->setSign($sign);
$indicator->setCible($cible);
$indicator->setType(Indicator::TYPE_OBJECTIF);
$indicator->setStatus(Indicator::STATUS_VALIDATED);
$indicator->setActivity(NULL);
$indicator->setProcessus($actionPlan->getProcessus());
$actionPlan->setIndicator($indicator);
/* set status*/
$actionPlan->setStatus(StatusEnum::STATUS_ACPL_PLANNED);
// if($actionPlan->getDateStartEstimated() > $actionPlan->getCreatedAt())
// {
// $actionPlan->setStatus(StatusEnum::STATUS_PLANNED);
// }
/* createdBy */
$actionPlan->setCreatedBy($user);
$actionPlan->addParticipant($user);
$actionPlan->addParticipant($actionPlan->getResponsible());
/* set resources */
if(!empty($actionPlan->getResourceHum()))
{
foreach($actionPlan->getResourceHum() as $resource)
{
$actionPlan->addResource($resource);
}
}
if(!empty($actionPlan->getResourceMat()))
{
foreach($actionPlan->getResourceMat() as $resource)
{
$actionPlan->addResource($resource);
}
}
/* set validation */
if(!empty($actionPlan->getValidator()))
{
$actionPlan->setStatus(StatusEnum::STATUS_ACPL_PLANNED);
$actionPlan->addParticipant($actionPlan->getValidator());
$actionPlan->setValidated(0);
// $this->notifyActionPlanValidator->notifyActionPlanValidator(
// $actionPlan->getCreatedBy()->getEmail(),
// $actionPlan->getValidator()->getEmail(),
// $actionPlan->getRef(),
// $actionPlan->getId()
// );
}
// add comment dashboard
$comment = new Comments();
$comment->setCreatedBy($actionPlan->getCreatedBy());
$actionPlan->setCommentDashboard($comment);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($actionPlan);
$entityManager->flush();
if($actionPlan->getValidator())
{
$this->notifyActionPlanValidator->notifyActionPlanValidator(
$actionPlan->getCreatedBy()->getEmail(),
$actionPlan->getValidator()->getEmail(),
$actionPlan->getRef(),
$actionPlan->getId()
);
}
$this->addFlash("success","Le plan d'action a été créé avec succès");
return $this->redirectToRoute('action_plan_list');
}
//dd($request);
//menu
$menu="action";
$left_menu="new_actionPlan";
return $this->render('action_plan/new.html.twig', [
'action_plan' => $actionPlan,
'form' => $form->createView(),
'menu' => $menu,
'left_menu'=>$left_menu
]);
}
/**
* @Route("/{id}", name="action_plan_show", methods={"GET"})
*/
public function show(ActionPlan $actionPlan): Response
{
if(empty($actionPlan->getCommentDashboard()))
{
$entityManager = $this->getDoctrine()->getManager();
$comment = new Comments();
$comment->setCreatedBy($actionPlan->getCreatedBy());
$actionPlan->setCommentDashboard($comment);
$entityManager->flush();
}
//menu
$menu="action";
$left_menu="pilotage_actionPlans";
return $this->render('action_plan/show.html.twig', [
'actionPlan' => $actionPlan,
'menu' => $menu,
'left_menu' => $left_menu
]);
}
/**
* @Route("/{id}/edit", name="action_plan_edit", methods={"GET","POST"})
*/
public function edit(Request $request, ActionPlan $actionPlan): Response
{
$user= $this->getUser();
$options['actionplan_sign'] = $actionPlan->getIndicator()->getSign();
$form = $this->createForm(ActionPlanType::class, $actionPlan,$options);
$form->handleRequest($request);
if ($form->isSubmitted())
{
$this->denyAccessUnlessGranted(ActionVoter::ACTION_ACTIONPLAN_CRUD);
$actionPlanData= $request->request->get('action_plan');
$sign= $actionPlanData['sign'];
$cible= $actionPlanData['cible'];
$new_indicator_id= $actionPlanData['indicator'];
$oldIndicator = $actionPlan->getIndicator();
if($oldIndicator->getId() != $new_indicator_id) {
$this->getDoctrine()->getManager()->remove($oldIndicator);
$indicator = $this->getDoctrine()->getRepository(Indicator::class)->find($new_indicator_id);
$indicator->setStatus(Indicator::STATUS_VALIDATED);
}else {
$indicator = $oldIndicator;
}
$indicator->setResponsible($user);
$indicator->setSign($sign);
$indicator->setCible($cible);
$indicator->setType(Indicator::TYPE_OBJECTIF);
$indicator->setActivity(NULL);
$indicator->setProcessus($actionPlan->getProcessus());
$actionPlan->setIndicator($indicator);
if(!empty($actionPlan->getValidator()))
{
$actionPlan->addParticipant($actionPlan->getValidator());
}
/* set resources */
$array_new_resources_hum=[];
if(!empty($actionPlan->getResourceHum()))
{
foreach($actionPlan->getResourceHum() as $resource)
{
$array_new_resources_hum[]=$resource;
$actionPlan->addResource($resource);
}
}
$array_new_resources_mat=[];
if(!empty($actionPlan->getResourceMat()))
{
foreach($actionPlan->getResourceMat() as $resource)
{
$array_new_resources_mat[]=$resource;
$actionPlan->addResource($resource);
}
}
$removed_resources_hum=array_diff($actionPlan->getResource('res_hum'),$array_new_resources_hum);
$removed_resources_mat=array_diff($actionPlan->getResource('res_mat'),$array_new_resources_mat);
$removed_resources = array_merge($removed_resources_hum, $removed_resources_mat);
foreach($removed_resources as $resource)
{
$actionPlan->removeResource($resource);
}
$actionPlan->addParticipant($actionPlan->getResponsible());
$this->getDoctrine()->getManager()->flush();
$this->addFlash("success","Le plan d'action a été modifié avec succès");
return $this->redirectToRoute('action_plan_list');
//return $this->redirectToRoute('action_plan_show',['id'=>$actionPlan->getId(),'section'=>'view']);
}
return $this->render('action_plan/edit.html.twig', [
'action_plan' => $actionPlan,
'form' => $form->createView()
]);
}
/**
* @Route("/delete/{id}", name="action_plan_delete", methods={"GET"})
*/
public function delete(Request $request, ActionPlan $actionPlan): Response
{
$this->denyAccessUnlessGranted(ActionVoter::ACTION_ACTIONPLAN_CRUD);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($actionPlan);
$entityManager->flush();
return $this->redirect($request->headers->get('referer'));
}
/**
* @Route("/{id}/planification", name="action_plan_planification", methods={"GET"})
*/
public function planification(ActionPlan $actionPlan): Response
{
$users = $this->getDoctrine()->getRepository(User::class)->findAll();
$processus = $this->getDoctrine()->getRepository(Processus::class)->findAll();
$sources = $this->getDoctrine()->getRepository(Source::class)->findAll();
$domains = $this->getDoctrine()->getRepository(Domain::class)->findAll();
$types = $this->getDoctrine()->getRepository(Type::class)->findAll();
$categories = CategoryEnum::getAvailableCategories();
//menu
$menu="action";
return $this->render('action_plan/planification.html.twig',[
'actionPlan'=>$actionPlan,
'users'=>$users,
'processus'=>$processus,
'sources'=>$sources,
'domains'=>$domains,
'types'=>$types,
'categories'=>$categories,
'menu'=>$menu,
]);
}
/**
* @Route("/{id}/validation", name="action_plan_validation", methods={"GET"})
*/
public function validation(ActionPlan $actionPlan): Response
{
$this->denyAccessUnlessGranted(ActionVoter::ACTION_ACTIONPLAN_CRUD);
$actionPlan->setStatus(StatusEnum::STATUS_ACPL_IN_PROGRESS);
$actionPlan->setValidated(1);
$this->getDoctrine()->getManager()->flush();
$this->addFlash("success","Le plan d'action a été validé avec succès");
$this->notifyActionPlanValid->notifyActionPlanValid(
$actionPlan->getCreatedBy()->getEmail(),
$actionPlan->getValidator()->getEmail(),
$actionPlan->getRef(),
$actionPlan->getId()
);
return $this->redirectToRoute('action_plan_list');
}
/**
* @Route("/{id}/refused", name="action_plan_refused", methods={"GET"})
*/
public function refused(ActionPlan $actionPlan): Response
{
$this->denyAccessUnlessGranted(ActionVoter::ACTION_ACTIONPLAN_CRUD);
$actionPlan->setStatus(StatusEnum::STATUS_ACPL_ANNULE);
$actionPlan->setValidated(2);
$this->getDoctrine()->getManager()->flush();
$this->addFlash("success","Le plan d'action a été refusé");
$this->notifyActionPlanRefused->notifyActionPlanRefused(
$actionPlan->getCreatedBy()->getEmail(),
$actionPlan->getValidator()->getEmail(),
$actionPlan->getRef(),
$actionPlan->getId()
);
$this->notifyActionPlanRefusedResponsible->notifyActionPlanRefusedResponsible(
$actionPlan->getCreatedBy()->getEmail(),
$actionPlan->getResponsible()->getEmail(),
$actionPlan->getRef(),
$actionPlan->getId()
);
return $this->redirectToRoute('action_plan_list');
}
/**
* @Route("/annuler/{id}", name="action_plan_annuler", methods={"GET","POST"})
*/
public function annulerPa(Request $request, ActionPlan $actionPlan): Response
{
$this->denyAccessUnlessGranted(ActionVoter::ACTION_ACTIONPLAN_CRUD);
$actionPlan->setStatus(StatusEnum::STATUS_ACPL_ANNULE);
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('action_plan_list');
}
/**
* @Route("/external/new", name="new_pa_external", methods={"GET","POST"})
*/
public function new_pa(Request $request): Response
{
$this->denyAccessUnlessGranted(ActionVoter::ACTION_ACTIONPLAN_CRUD);
$actionPlan = new ActionPlan();
$user= $this->getUser();
/* genretate ref */
$lastId=0;
$lastActionPlan = $this->getDoctrine()->getRepository(ActionPlan::class)->findOneBy([], ['id' => 'desc']);
if(is_object($lastActionPlan))
{
$lastId = $lastActionPlan->getId();
}
$utility = new Utility;
$ref = $utility->generateRef($lastId,ActionPlan::PREFIX_DYS);
$actionPlan->setRef($ref);
/* end genretate ref*/
$options=[];
$constat='';
$constat_id = $request->query->get('constat','');
$temp = $request->query->get('temp','');
if($constat_id) {
$constat = $this->getDoctrine()->getRepository(Constat::class)->find($constat_id);
$domaine = $constat->getDomain();
$actionPlan->setDomain($domaine);
$options['disabled_domain']=true;
if($temp) {
$type = $this->getDoctrine()->getRepository(Type::class)->findOneBy(['title'=>'Correctif temporaire']);
}else {
$type = $this->getDoctrine()->getRepository(Type::class)->findOneBy(['title'=>'Correctif définitif']);
}
$actionPlan->setType($type);
$options['disabled_type']=true;
$source = $constat->getSource();
$actionPlan->setSource($source);
$options['disabled_source']=true;
$processus = $constat->getProcessus();
$actionPlan->setProcessus($processus);
$options['disabled_processus']=true;
}
// $options['disabled_domain']=true;
// $options['disabled_type']=true;
// $options['disabled_source']=true;
$form = $this->createForm(ActionPlanType::class, $actionPlan, $options);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$actionPlan->setStatus(StatusEnum::STATUS_ACPL_PLANNED);
/* createdBy */
$actionPlan->setCreatedBy($user);
$actionPlan->addParticipant($user);
$actionPlan->addParticipant($actionPlan->getResponsible());
if($constat) {
$constat->addActionPlan($actionPlan);
}
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($actionPlan);
$entityManager->flush();
if($actionPlan->getValidator())
{
$this->notifyActionPlanValidator->notifyActionPlanValidator(
$actionPlan->getCreatedBy()->getEmail(),
$actionPlan->getValidator()->getEmail(),
$actionPlan->getRef(),
$actionPlan->getId()
);
}
$this->addFlash("success","Le plan d'action a été créé avec succès");
return $this->redirectToRoute('action_plan_show',['id'=>$actionPlan->getId()]);
}
$users_list = $this->getDoctrine()->getRepository(User::class)->findAll();
$users_list_array = [];
foreach($users_list as $user) {
$fonction = str_replace('"', "", $user->getFonction());
$users_list_array[$user->getId()] = $fonction;
}
//menu
$menu="action";
$left_menu="pilotage_actionPlans";
return $this->render('action_plan/new_pa.html.twig',[
'menu'=>$menu,
'left_menu'=>$left_menu,
'form'=>$form->createView(),
'users_list_array'=>$users_list_array
]);
}
/**
* @Route("/terminer_planification/{id}", name="terminer_planification", methods={"GET"})
*/
public function terminer_planification(ActionPlan $actionPlan)
{
$this->denyAccessUnlessGranted(ActionVoter::ACTION_ACTIONPLAN_CRUD);
$actionPlan->setPlanificationClosed(true);
$this->getDoctrine()->getManager()->flush();
if($actionPlan->getConstat()) {
return $this->redirectToRoute('analyse',['id'=>$actionPlan->getConstat()->getId()]);
}
return $this->redirectToRoute('action_plan_show',['id'=>$actionPlan->getId()]);
}
}