<?php
namespace App\Controller;
use App\Entity\Action;
use App\Entity\ActionPlan;
use App\Entity\bilan\ConditionSubdomain;
use App\Entity\bilan\Entity;
use App\Entity\bilan\SituationRisque;
use App\Entity\Configuration\CompanyEntity;
use App\Entity\ConformityArticle;
use App\Entity\Domain;
use App\Entity\Dysfonctionnement\Cause5p;
use App\Entity\Dysfonctionnement\Cause6m;
use App\Entity\inspection\DomaineQuestionnaire;
use App\Entity\inspection\InspectionQuestion;
use App\Entity\Processus;
use App\Entity\ReunionSubject;
use App\Entity\Source;
use App\Entity\User;
use App\Entity\Subject;
use App\Entity\Type;
use App\Entity\VGP\Observation;
use App\Enum\CategoryEnum;
use App\Enum\PeriodActionEnum;
use App\Enum\PriorityEnum;
use App\Enum\StatusEnum;
use App\Form\ActionType;
use App\Repository\Configuration\CompanyEntityRepository;
use App\Service\SelectedEntityService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Utils\Utility;
use App\Repository\ActionRepository;
use DateTimeInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Validator\Constraints\DateTime;
use Symfony\Component\Validator\Constraints\Json;
use App\Notification\SendNotification;
use App\Security\ActionVoter;
use Proxies\__CG__\App\Entity\inspection\InspectionQuestion as InspectionInspectionQuestion;
/**
* @Route("/action")
*/
class ActionController extends AbstractController
{
/**
* @var SendNotification
*/
private $notify;
private $notifyAction;
/**
* @var Security
*/
private $security;
private $requestStack;
public function __construct(
SendNotification $notify,
SendNotification $notifyAction,
SendNotification $notifyActionValidator,
Security $security,
RequestStack $requestStack
)
{
$this->notify = $notify;
$this->notifyAction = $notifyAction;
$this->notifyActionValidator = $notifyActionValidator;
$this->security = $security;
$this->requestStack = $requestStack;
}
/**
* @Route("/", name="action_index", methods={"GET"})
*/
public function index(Request $request, SelectedEntityService $selectedEntityService): 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();
$entities = $this->getDoctrine()->getRepository(CompanyEntity::class)->findAll();
$ref_action = $request->query->get('ref');
$selectedEntity = $selectedEntityService->getSelectedEntity();
//menu
$menu="action";
$left_menu="pilotage_actions";
return $this->render('action/index.html.twig',[
'users'=>$users,
'processus'=>$processus,
'sources'=>$sources,
'domains'=>$domains,
'types'=>$types,
'categories'=>$categories,
'entities'=>$entities,
'ref_action'=>$ref_action,
'menu'=>$menu,
'left_menu'=>$left_menu,
'selectedEntity'=>$selectedEntity,
]);
}
/**
* @Route("/list/{actionplanId}", name="action_list", defaults={"actionplanId": null}, methods={"GET"})
*/
public function list(ActionRepository $actionRepository,Request $request,$actionplanId='')
{
$startDate=$request->query->get('startDate','');
$endDate=$request->query->get('endDate','');
$remove_link= $request->query->get('remove_link','');
$actionPlan='';
if($actionplanId>0)
{
$actionPlan= $this->getDoctrine()->getRepository(ActionPlan::class)->find($actionplanId);
}
$user = $this->getUser();
$actionsData = $actionRepository->transformAll($user,$actionPlan,$remove_link,$startDate,$endDate);
return new JsonResponse($actionsData);
}
/**
* @Route("/new", name="action_new", methods={"GET","POST"})
*/
public function new(Request $request, ActionRepository $actionRepository, CompanyEntityRepository $companyEntityRepository): Response
{
$action = new Action();
$this->denyAccessUnlessGranted(ActionVoter::ACTION_CRUD, $action);
$user = $this->security->getUser();
$session = $this->requestStack->getCurrentRequest()->getSession();
$selectedEntityId = $session->get('selected_entity_id');
$selectedEntity = null;
if ($selectedEntityId && ($this->security->isGranted('ROLE_ADMIN') || $this->security->isGranted('ROLE_DIRECTION'))) {
$selectedEntity = $companyEntityRepository->find($selectedEntityId);
} elseif (count($user->getEntities()) === 1) {
$selectedEntity = $user->getEntities()->first();
}
if ($selectedEntity) {
$action->addCompanyEntity($selectedEntity);
}
$utility = new Utility;
/* genretate ref */
$oldRef= $actionRepository->findLastRef(Action::PREFIX_AC);
$newRef = $utility->generateNewRef($oldRef,Action::PREFIX_AC);
$actionplan_id=$request->query->get('actionplan');
if($actionplan_id > 0)
{
$oldRef= $actionRepository->findLastRef(Action::PREFIX_PA_AC);
$newRef = $utility->generateNewRef($oldRef,Action::PREFIX_PA_AC);
}
$action->setRef($newRef);
/* end genretate ref*/
/* genretate ref for actionEE */
$oldRefEE= $actionRepository->findLastRef(Action::PREFIX_ACEE);
$newRefEE = $utility->generateNewRef($oldRefEE,Action::PREFIX_ACEE);
$action->setRefEE($newRefEE);
/* end genretate ref*/
$options=[];
/* get actionPlan carecteristics */
$domainPa='';
$typePa='';
$sourcePa='';
$actionplan_id = $request->query->get('actionplan');
if($actionplan_id > 0 )
{
$actionPlan= $this->getDoctrine()->getRepository(ActionPlan::class)->find($actionplan_id);
if($actionPlan)
{
$domainPa=$actionPlan->getDomain();
$action->addDomaine($domainPa);
$action->setDomainEE([$domainPa]);
$options['disabled_domain']=true;
$typePa=$actionPlan->getType();
$action->setType($typePa);
$action->setTypeEE($typePa);
$options['disabled_type']=true;
$sourcePa=$actionPlan->getSource();
$action->setSource($sourcePa);
$options['disabled_source']=true;
}
}
/* end actionPlan */
/* situation risque */
$situationRisque_id = $request->query->get('sr');
if($situationRisque_id > 0) {
$domain = $this->getDoctrine()->getRepository(Domain::class)->findOneBy(['title'=>'Santé & sécurité au travail']);
$action->addDomaine($domain);
$action->setDomainEE([$domain]);
$options['disabled_domain']=true;
$type= $this->getDoctrine()->getRepository(Type::class)->findOneBy(['title'=>'Risque-Préventif']);
$action->setType($type);
$action->setTypeEE($type);
$source= $this->getDoctrine()->getRepository(Source::class)->findOneBy(['title'=>'Evrp (doc unique)']);
$action->setSource($source);
$options['disabled_source']=true;
$processus_id= $request->query->get('pr');
$processus = $this->getDoctrine()->getRepository(Processus::class)->find($processus_id);
$action->addProcessus($processus);
}
/* end situation risque */
/* condition subdomain */
$conditionSubdomain_id = $request->query->get('cs');
if($conditionSubdomain_id > 0) {
$domain = $this->getDoctrine()->getRepository(Domain::class)->findOneBy(['title'=>'Environnement']);
$action->addDomaine($domain);
$action->setDomainEE([$domain]);
$options['disabled_domain']=true;
$type= $this->getDoctrine()->getRepository(Type::class)->findOneBy(['title'=>'Risque-Préventif']);
$action->setType($type);
$action->setTypeEE($type);
$source= $this->getDoctrine()->getRepository(Source::class)->findOneBy(['title'=>'Analyse environnementale']);
$action->setSource($source);
$options['disabled_source']=true;
$processus_id= $request->query->get('pr');
$processus = $this->getDoctrine()->getRepository(Processus::class)->find($processus_id);
$action->addProcessus($processus);
}
/* condition inspection */
$inspectionQuestion_id = $request->query->get('iq');
if($inspectionQuestion_id > 0) {
$inspectionQuestion = $this->getDoctrine()->getRepository(InspectionQuestion::class)->find($inspectionQuestion_id);
$questionnaire=$inspectionQuestion->getInspection()->getQuestionnaire();
$domainesQ= $this->getDoctrine()->getRepository(DomaineQuestionnaire::class)->findBy(['questionnaire'=>$questionnaire]);
foreach( $domainesQ as $domainQ)
{
$action->addDomaine($domainQ->getDomaine());
}
$options['disabled_domain']=true;
$type= $this->getDoctrine()->getRepository(Type::class)->findOneBy(['title'=>'correctif']);
$action->setType($type);
$action->setTypeEE($type);
$source= $this->getDoctrine()->getRepository(Source::class)->findOneBy(['title'=>'Vérification TIK TOP']);
$action->setSource($source);
$options['disabled_source']=true;
}
/* end condition subdomain */
//add action realted to article
$article_id = $request->query->get('article_id','');
if($article_id) {
$article = $this->getDoctrine()->getRepository(ConformityArticle::class)->find($article_id);
$conformityText = $article->getConformityText();
$domaines = $conformityText->getDomains();
foreach( $domaines as $domaine)
{
$action->addDomaine($domaine);
}
$options['disabled_domain']=true;
//Action corrective définitive
$type= $this->getDoctrine()->getRepository(Type::class)->findOneBy(['title'=>'Action corrective définitive']);
if($type) {
$action->setType($type);
$action->setTypeEE($type);
$options['disabled_type']=true;
}
//Veille règlementaire
$source= $this->getDoctrine()->getRepository(Source::class)->findOneBy(['title'=>'Veille règlementaire']);
if($source) {
$action->setSource($source);
$options['disabled_source']=true;
}
}
//end artilce
$form = $this->createForm(ActionType::class, $action,$options);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/* set typePa */
$action->setTypePa('Sans PA');
/* set status*/
$action->setStatus(StatusEnum::STATUS_PLANNED);
// $action->setStatus(StatusEnum::STATUS_IN_PROGRESS);
// if(!empty($action->getValidator()) || $action->getDateStartEstimated() > $action->getCreatedAt())
// {
// $action->setStatus(StatusEnum::STATUS_PLANNED);
// }
/* set validation */
if(!empty($action->getValidator()))
{
$action->setValidated(0);
}
/* createdBy */
$createdBy = $this->getUser();
$action->setCreatedBy($createdBy);
/* set type Action */
$action->setTypeAction(Action::PREFIX_AC);
/* set progress */
$action->setProgress(0);
/* set resources */
if(!empty($action->getResourceHum()))
{
foreach($action->getResourceHum() as $resource)
{
$action->addResource($resource);
}
}
if(!empty($action->getResourceMat()))
{
foreach($action->getResourceMat() as $resource)
{
$action->addResource($resource);
}
}
/** add actionEE */
if(!empty($_POST['efficacite']))
{
$user = $this->getUser();
$actionEE= new Action();
$actionEE->setCompanyEntities($action->getCompanyEntities());
$actionEE->setRef($action->getRefEE());
$actionEE->setTitle($action->getTitleEE());
// $actionEE->setDomain($_POST['action']['domain']);
// $actionEE->setType($_POST['action']['type']);
foreach($action->getDomaines() as $domain)
{
$actionEE->addDomaine($domain);
}
$actionEE->setType($action->getType());
$actionEE->setDateStartEstimated($action->getDateStartEstimatedEE());
$actionEE->setDateEndEstimated($action->getDateEndEstimatedEE());
$actionEE->setDescription($action->getDescriptionEE());
$actionEE->setTypeAction(Action::PREFIX_ACEE);
$actionEE->setStatus(StatusEnum::STATUS_PLANNED);
$actionEE->setProgress(0);
$actionEE->setResponsible($action->getResponsibleEE());
$actionEE->setDurationEstimated($action->getDurationEstimatedEE());
$actionEE->setBudgetEstimated($action->getBudgetEstimatedEE());
$actionEE->setCreatedBy($user);
$actionEE->setPriority(PriorityEnum::PRIORITY_HIGH);
foreach($action->getProcessusEE() as $processus)
{
$actionEE->addProcessus($processus);
}
$action->setAction($actionEE);
/* set typePa */
$actionEE->setTypePa('Sans PA');
}
$entityManager = $this->getDoctrine()->getManager();
/** set subjet */
$reunion_subject='';
$id_reunion_subject=$request->query->get('reunion_subject','');
if($id_reunion_subject > 0)
{
$reunion_subject = $entityManager->getRepository(ReunionSubject::class)->find($id_reunion_subject);
if(is_object($reunion_subject))
{
$action->setReunionSubject($reunion_subject);
$action->setReunion($reunion_subject->getReunion());
$action->setStatus(StatusEnum::STATUS_DRAFT);
if(!empty($_POST['efficacite']))
{
$actionEE->setStatus(StatusEnum::STATUS_DRAFT);
}
}
}
//$path='action_index';
/** set ActionPlan */
$actionplan='';
if($_POST['actionplan'] > 0)
{
$id_actionplan = $_POST['actionplan'];
$actionplan = $entityManager->getRepository(ActionPlan::class)->find($id_actionplan);
if(is_object($actionplan))
{
$action->setActionPlan($actionplan);
/* set typePa */
$action->setTypePa($actionplan->getTypePa());
$actionplan->addParticipant($createdBy);
$actionplan->addParticipant($action->getResponsible());
/* set domain, type, source*/
$action->addDomaine($actionplan->getDomain());
$action->setType($actionplan->getType());
$action->setSource($actionplan->getSource());
if(is_object($action->getValidator()) )
{
$actionplan->addParticipant($action->getValidator());
}
if(is_object($action->getAction()))
{
$action->getAction()->setActionPlan($actionplan);
/* set typePa */
$action->getAction()->setTypePa('Plan de progrès');
/* set domain, type, source*/
$action->getAction()->addDomaine($actionplan->getDomain());
$action->getAction()->setType($actionplan->getType());
$actionplan->addParticipant($action->getAction()->getResponsible());
if(is_object($action->getAction()->getValidator()) )
{
$actionplan->addParticipant($action->getAction()->getValidator());
}
}
/** set status of actionPlan */
if($action->getStatus()==StatusEnum::STATUS_IN_PROGRESS)
{
if($actionplan->getStatus()==StatusEnum::STATUS_ACPL_PLANNED && empty($actionplan->getBudgetEstimated()))
{
$actionplan->setStatus(StatusEnum::STATUS_ACPL_IN_PROGRESS);
}
}
}
}
$entityManager->persist($action);
//check if situation risque est liée a cette action
$situationRisque='';
if($_POST['sr'] > 0){
$id_situationRisque = $_POST['sr'];
$situationRisque = $entityManager->getRepository(SituationRisque::class)->find($id_situationRisque);
$action->setSituationRisque($situationRisque);
$situationRisque->addAction($action);
}
//check if situation risque est liée a cette action
$conditionSubdomain='';
if($_POST['cs'] > 0){
$id_conditionSubdomain = $_POST['cs'];
$conditionSubdomain = $entityManager->getRepository(ConditionSubdomain::class)->find($id_conditionSubdomain);
$conditionSubdomain->setAction($action);
}
//check if inspection question est liée a cette action
$inspectionQuestion='';
if($request->query->get('iq') > 0){
$id_inspectionQ = $request->query->get('iq');
$inspectionQuestion = $entityManager->getRepository(InspectionQuestion::class)->find($id_inspectionQ);
$inspectionQuestion->setAction($action);
}
//check if article est liée a cette action
$article='';
$conformityText='';
if($request->query->get('article_id') > 0){
$article_id = $request->query->get('article_id');
$article = $entityManager->getRepository(ConformityArticle::class)->find($article_id);
$conformityText=$article->getConformityText();
//$article->addAction($action);
$action->setConformityArticle($article);
}
//cause 5p
$cause5p_id=$request->query->get('cause5p','');
if($cause5p_id) {
$cause5p = $this->getDoctrine()->getRepository(Cause5p::class)->find($cause5p_id);
$cause5p->setAction($action);
}
$cause6m_id=$request->query->get('cause6m','');
if($cause6m_id) {
$cause6m = $this->getDoctrine()->getRepository(Cause6m::class)->find($cause6m_id);
$cause6m->setAction($action);
}
$observation_id=$request->query->get('observation_id','');
$observation='';
if($observation_id) {
$observation = $this->getDoctrine()->getRepository(Observation::class)->find($observation_id);
$observation->setAction($action);
}
// if($action->getPeriodicity()) {
// $this->createPeriodActions($action);
// }else {
// dd('nothing');
// }
$entityManager->flush();
// if($_POST['actionplan'] > 0)
// {
// //notification action with actionplan
// if(!empty($action->getValidator()))
// {
// $this->notifyActionValidator->notifyActionValidator(
// $action->getCreatedBy()->getEmail(),
// $action->getValidator()->getEmail(),
// $action->getRef()
// );
// }
// if(!empty($_POST['efficacite']))
// {
// $this->notify->notify(
// $action->getCreatedBy()->getEmail(),
// $action->getResponsibleEE()->getEmail(),
// $action->getResponsibleEE()->getFullName(),
// $action->getRefEE(),
// );
// }
// $this->notify->notify(
// $action->getCreatedBy()->getEmail(),
// $action->getResponsible()->getEmail(),
// $action->getResponsible()->getFullName(),
// $action->getRef()
// );
// }else{
// //notification action without actionplan
// if(!empty($action->getValidator()))
// {
// $this->notifyActionValidator->notifyActionValidator(
// $action->getCreatedBy()->getEmail(),
// $action->getValidator()->getEmail(),
// $action->getRef()
// );
// }
// if(!empty($_POST['efficacite']))
// {
// $this->notifyAction->notifyAction(
// $action->getCreatedBy()->getEmail(),
// $action->getResponsibleEE()->getEmail(),
// $action->getResponsibleEE()->getFullName(),
// $action->getRefEE(),
// $action->getCreatedBy()->getFonction()
// );
// }
// $this->notifyAction->notifyAction(
// $action->getCreatedBy()->getEmail(),
// $action->getResponsible()->getEmail(),
// $action->getResponsible()->getFullName(),
// $action->getRef(),
// $action->getCreatedBy()->getFonction()
// );
// }
$this->addFlash("success","L'action a été créée avec succès");
if($_POST['add_new_action']){
$menu="action";
$left_menu="new_action";
return $this->render('action/new.html.twig', [
'action' => $action,
'form' => $form->createView(),
'menu' => $menu,
'left_menu'=>$left_menu,
'domainPa' => $domainPa,
'typePa' => $typePa,
'sourcePa' => $sourcePa,
]);
}
$constat_id=$request->query->get('constat','');
$reunion_id = $request->query->get('reunion','');
if($observation) {
$groupMajeur_id = $request->query->get('groupMajeur_id','');
return $this->redirectToRoute('vgp_surveillance',['id'=>$groupMajeur_id]);
}
if($reunion_id) {
if($cause5p_id) {
return $this->redirectToRoute('analyse_5p',['id'=>$reunion_id]);
}else if ($cause6m_id) {
return $this->redirectToRoute('analyse_6m',['id'=>$reunion_id]);
}else {
return $this->redirectToRoute('reunion_show',['id'=>$reunion_id]);
}
}
if($constat_id) {
return $this->redirectToRoute('analyse',['id'=>$constat_id]);
}
if(is_object($actionplan) && !$constat_id)
{
return $this->redirectToRoute('action_plan_show',['id'=>$actionplan->getId(),'section'=>'planification']);
}else if(is_object($situationRisque)) {
return $this->redirectToRoute('analyseSST');
}else if(is_object($conditionSubdomain)){
return $this->redirectToRoute('analyseENV');
}else if(is_object($inspectionQuestion)){
return $this->redirectToRoute('inspection_resultat_inspection',array('id'=>$inspectionQuestion->getInspection()->getId()));
}else if(is_object($conformityText)){
return $this->redirectToRoute('conformite_analyse',array('id'=>$conformityText->getId()));
}else if(is_object($reunion_subject)) {
return $this->redirectToRoute('reunion_show',array('id'=>$reunion_subject->getReunion()->getId()));
}else {
return $this->redirectToRoute('action_index');
}
}
//menu
$menu="action";
$left_menu="new_action";
return $this->render('action/new.html.twig', [
'action' => $action,
'form' => $form->createView(),
'menu' => $menu,
'left_menu'=>$left_menu,
'domainPa' => $domainPa,
'typePa' => $typePa,
'sourcePa' => $sourcePa,
]);
}
/**
* @Route("/{id}/edit", name="action_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Action $action, ActionRepository $actionRepository, SelectedEntityService $selectedEntityService): Response
{
$selectedEntity = $selectedEntityService->getSelectedEntity();
if ($selectedEntity) {
$action->addCompanyEntity($selectedEntity);
}
$utility = new Utility;
$options = [];
//previous page
$goback = $request->headers->get('referer');
$goback_array = explode('section=',$goback);
if(count($goback_array) > 1)
{
$goback_array[1] = 'planification';
}
$goback = implode('section=',$goback_array);
$actionEE = new Action();
$procEE_ids=[];
if($action->hasActionEE()==1)
{
$actionEE = $action->getAction();
foreach($actionEE->getProcessus() as $processus)
{
$procEE_ids[]=$processus->getId();
}
}
else
{
/* genretate ref for actionEE */
$oldRefEE= $actionRepository->findLastRef(Action::PREFIX_ACEE);
$newRefEE = $utility->generateNewRef($oldRefEE,Action::PREFIX_ACEE);
$actionEE->setRef($newRefEE);
/* end genretate ref*/
}
$date_fin_old=$action->getDateEndEstimated();
$actionPlan="";
if($action->getActionPlan())
{
$actionPlan = $action->getActionPlan()->getId();
$options['disabled_domain']=true;
$options['disabled_type']=true;
$options['disabled_source']=true;
}
$reunion_subject="";
if($action->getReunionSubject())
{
$reunion_subject = $action->getReunionSubject()->getId();
}
$responsibleIdOfMotherAction='0';
if($action->getTypeAction()=="AE/AC")
{
$actionMother = $this->getDoctrine()->getRepository(Action::class)->findOneBy(array('action'=>$action));
if($actionMother)
{
$responsibleIdOfMotherAction = $actionMother->getResponsible()->getId();
}
}
$situationRisque = $this->getDoctrine()->getRepository(SituationRisque::class)->findOneBy(['action'=>$action]);
if($situationRisque){
$options['disabled_domain']=true;
$options['disabled_source']=true;
}
$form = $this->createForm(ActionType::class, $action, $options);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$this->denyAccessUnlessGranted(ActionVoter::ACTION_CRUD,$action);
$entityManager = $this->getDoctrine()->getManager();
/* clear all domaines */
$domaines = [];
if(array_key_exists('domaines',$_POST['action'])){
$action->removeAllDomaines();
foreach($_POST['action']['domaines'] as $domain_id)
{
$domain = $this->getDoctrine()->getRepository(Domain::class)->find($domain_id);
$action->addDomaine($domain);
$domaines[]=$domain;
}
}
/* set status */
$action->setStatus(StatusEnum::STATUS_PLANNED);
/* set validation */
if(!empty($action->getValidator()))
{
$action->setValidated(0);
}
/* set resources */
if(!empty($action->getResourceHum()))
{
foreach($action->getResourceHum() as $resource)
{
$action->addResource($resource);
}
}
if(!empty($action->getResourceMat()))
{
foreach($action->getResourceMat() as $resource)
{
$action->addResource($resource);
}
}
/** nbr report */
if(isset($_POST['date_fin_old']))
{
$date_fin_old=$_POST['date_fin_old'];
$date_fin_old.=" 00:00:00";
$date_fin_old = \DateTime::createFromFormat("d/m/Y H:i:s",$date_fin_old);
if($action->getDateEndEstimated() > $date_fin_old)
{
$nbr_report = intval($action->getReportNbr()) + 1;
$action->setReportNbr($nbr_report);
}
}
/** actionEE */
if(isset($_POST['efficacite']))
{
$efficacite = $_POST['efficacite'];
if($efficacite=='1')
{
if($action->hasActionEE()==1)
{
$actionEE = $action->getAction();
}
else
{
$actionEE->setCreatedBy($this->getUser());
$action->setAction($actionEE);
}
$actionEE->setCompanyEntities($action->getCompanyEntities());
$actionEE->setTitle($action->getTitleEE());
/* clear all domaines */
$actionEE->removeAllDomaines();
foreach($action->getDomaines() as $domain)
{
$actionEE->addDomaine($domain);
}
// $type_id =$_POST['action']['type'];
// $type= $this->getDoctrine()->getRepository(Type::class)->find($type_id);
$actionEE->setType($action->getType());
$actionEE->setDateStartEstimated($action->getDateStartEstimatedEE());
$actionEE->setDateEndEstimated($action->getDateEndEstimatedEE());
$actionEE->setDescription($action->getDescriptionEE());
$actionEE->setTypeAction(Action::PREFIX_ACEE);
$actionEE->setStatus(StatusEnum::STATUS_PLANNED);
$actionEE->setPriority(PriorityEnum::PRIORITY_HIGH);
$actionEE->setResponsible($action->getResponsibleEE());
$actionEE->setDurationEstimated($action->getDurationEstimatedEE());
$actionEE->setBudgetEstimated($action->getBudgetEstimatedEE());
$actionEE->removeAllProcessus();
foreach($action->getProcessusEE() as $processus)
{
$actionEE->addProcessus($processus);
}
}
else
{
if($action->hasActionEE()==1)
{
$entityManager->remove($action->getAction());
$action->setAction(null);
}
}
}
/** set ActionPlan */
$actionplan='';
if($_POST['actionplan'] > 0)
{
$id_actionplan = $_POST['actionplan'];
$actionplan = $entityManager->getRepository(ActionPlan::class)->find($id_actionplan);
if(is_object($actionplan))
{
$action->setActionPlan($actionplan);
$actionplan->addParticipant($action->getResponsible());
if(is_object($action->getValidator()) )
{
$actionplan->addParticipant($action->getValidator());
}
}
}
/** set subjet */
if($_POST['reunion_subject'] > 0)
{
$id_reunion_subject = $_POST['reunion_subject'];
$reunion_subject = $entityManager->getRepository(ReunionSubject::class)->find($id_reunion_subject);
if(is_object($reunion_subject))
{
$action->setReunionSubject($reunion_subject);
}
}
$action->setUpdatedBy($this->getUser());
$action->setUpdatedAt(new \DateTime('now'));
//find previous page
$previous_page = $_POST['goback'];
$entityManager->flush();
$this->addFlash("success","L'action a été modifiée avec succès");
return $this->redirect($previous_page);
/*if(is_object($actionplan))
{
return $this->redirectToRoute('action_plan_show',['id'=>$actionplan->getId(),'section'=>'planification']);
}
return $this->redirectToRoute('action_index');*/
}
//menu
$menu="action";
$left_menu="pilotage_actions";
return $this->render('action/edit.html.twig', [
'action' => $action,
'actionEE' => $actionEE,
'procEE_ids' => $procEE_ids,
'form' => $form->createView(),
'menu' => $menu,
'left_menu' => $left_menu,
'date_fin_old'=>$date_fin_old,
'actionPlan'=>$actionPlan,
'reunion_subject'=>$reunion_subject,
'goback'=>$goback,
'responsibleIdOfMotherAction'=>$responsibleIdOfMotherAction
]);
}
/**
* @Route("/delete/{id}", name="action_delete", methods={"GET"})
*/
public function delete(Request $request, Action $action): Response
{
// if ($this->isCsrfTokenValid('delete'.$action->getId(), $request->request->get('_token'))) {
$this->denyAccessUnlessGranted(ActionVoter::ACTION_CRUD,$action);
$entityManager = $this->getDoctrine()->getManager();
if($action->hasActionEE()){
$actionEE = $action->getAction();
$entityManager->remove($actionEE);
$action->setAction(null);
$entityManager->flush();
}else{
$actionMotherArray = $this->getDoctrine()->getRepository(Action::class)->findBy(["action"=>$action]);
if(!empty($actionMotherArray)){
$actionMother = $actionMotherArray[0];
$actionMother->setAction(null);
}
}
$entityManager->remove($action);
$entityManager->flush();
// }
return $this->redirect($request->headers->get('referer'));
//return $this->redirectToRoute('action_index');
}
/**
* @Route("/annuler/{id}", name="action_annuler", methods={"GET","POST"})
*/
public function annuler(Request $request, Action $action): Response
{
$this->denyAccessUnlessGranted(ActionVoter::ACTION_CRUD,$action);
$action->setStatus(StatusEnum::STATUS_ANNULE);
$action->setReportNbr($action->getReportNbr()+1);
$this->getDoctrine()->getManager()->flush();
// return $this->redirectToRoute('reunion_index');
return $this->redirect($request->headers->get('referer'));
}
/**
* @Route("/activate/{id}", name="action_activate", methods={"GET","POST"})
*/
public function activate(Request $request, Action $action): Response
{
$form = $this->createForm(ActionType::class, $action, []);
if($request->getMethod() ==='POST') {
$em = $this->getDoctrine()->getManager();
$responsibleId = $request->request->get('responsible');
$responsible = $em->getRepository(User::class)->find($responsibleId);
$action->setResponsible($responsible);
$dateEndEstimatedText = $request->request->get('dateEndEstimated');
$dateEndEstimated = \DateTime::createFromFormat('d/m/Y', $dateEndEstimatedText);
$action->setDateEndEstimated($dateEndEstimated);
$durationEstimated = $request->request->get('durationEstimated');
$action->setDurationEstimated($durationEstimated);
$budgetEstimated = $request->request->get('budgetEstimated');
$action->setBudgetEstimated($budgetEstimated);
$action->setStatus(StatusEnum::STATUS_PLANNED);
$em->flush();
return new JsonResponse(['saved'=>1]);
}
return $this->render('action/activate.html.twig',[
'action'=>$action,
'form'=>$form->createView()
]);
}
/**
* @Route("/updateCell", name="update_cell", methods={"POST"})
*/
public function updateCell(Request $request)
{
if (!$request->isXmlHttpRequest()) {
return new JsonResponse(array('status' => 'error','message' => 'Access forbidden!'), 400);
}
if(isset($request->request))
{
$entityManager = $this->getDoctrine()->getManager();
$id = $request->request->get('id');
$columnName = $request->request->get('columnName');
$columnValue = $request->request->get('columnValue');
if($columnValue ==="") {
return new JsonResponse(array('status' => 'not updated'), 201);
}
$id =intval($id);
$action = $entityManager->getRepository(Action::class)->find($id);
//$old_status = $action->getStatus();
$this->denyAccessUnlessGranted(ActionVoter::ACTION_INSERT,$action);
$statutEE = "";
$idEE="";
$efficaciteActionMother="";
$statutActionMother="";
$idActionMother="";
/*if($old_status !=StatusEnum::STATUS_IN_PROGRESS)
{
return new JsonResponse(array('status' => 'error','message' => 'status forbidden'), 200);
}*/
$actionMotherNotClosed="";
$actionMother = $entityManager->getRepository(Action::class)->findOneBy(array('action'=>$action));
if($actionMother)
{
if(!in_array($actionMother->getStatus(),[StatusEnum::STATUS_CLOSED,StatusEnum::STATUS_ATTENTE_EE]))
{
$actionMotherNotClosed=$actionMother->getRef();
return new JsonResponse([ 'actionMotherNotClosed'=>$actionMotherNotClosed],200);
}
}
if($columnName=="progress")
{
$progress = intval($columnValue);
$action->setProgress($progress);
}
elseif($columnName=="dateStartReal")
{
$date = str_replace('/', '-', $columnValue);
$dateStartReal = date('Y-m-d', strtotime($date));
$dateStartReal =\DateTime::createFromFormat('Y-m-d', $dateStartReal);
$action->setDateStartReal($dateStartReal);
if($action->getStatus() == StatusEnum::STATUS_PLANNED)
{
$action->setStatus(StatusEnum::STATUS_IN_PROGRESS);
if($action->getActionPlan())
{
$action->getActionPlan()->setStatus(StatusEnum::STATUS_ACPL_IN_PROGRESS);
}
}
}
elseif($columnName=="dateEndReal")
{
$date = str_replace('/', '-', $columnValue);
$dateEndReal = date('Y-m-d', strtotime($date));
$dateEndReal =\DateTime::createFromFormat('Y-m-d', $dateEndReal);
$action->setDateEndReal($dateEndReal);
$action->setProgress(100);
if($action->getAction())
{
$action->setStatus(StatusEnum::STATUS_ATTENTE_EE);
//$action->getAction()->setStatus(StatusEnum::STATUS_IN_PROGRESS);
$statutEE = $action->getAction()->getStatus();
$idEE=$action->getAction()->getId();
}
else
{
$action->setStatus(StatusEnum::STATUS_CLOSED);
}
}
elseif($columnName=="durationReal")
{
$durationReal= floatval($columnValue);
$action->setDurationReal($durationReal);
}
elseif($columnName=="budgetReal")
{
$budgetReal= floatval($columnValue);
$action->setBudgetReal($budgetReal);
}
elseif($columnName=="efficacite")
{
$efficacite = intval($columnValue);
$action->setDateEndReal(new \DateTime());
$action->setStatus(StatusEnum::STATUS_CLOSED);
$action->setProgress(100);
if($actionMother)
{
$actionMother->setStatus(StatusEnum::STATUS_CLOSED);
$actionMother->setEfficient($efficacite);
$entityManager->persist($actionMother);
$idActionMother=$actionMother->getId();
$efficaciteActionMother=strval($efficacite);
$statutActionMother=$actionMother->getStatus();
}
}
$entityManager->persist($action);
try
{
$entityManager->flush();
$respectBudget ="OK";
if($action->getBudgetEstimated()<$action->getBudgetReal()) $respectBudget ="NOK";
$respectDuree = "OK";
if($action->getDurationEstimated()<$action->getDurationReal()) $respectDuree ="NOK";
$respectDelais = "OK";
$joursRetard="0";
if($action->getDateEndEstimated()<$action->getDateEndReal())
{
$respectDelais ="NOK";
$joursRetard = $action->getDateEndEstimated()->diff($action->getDateEndReal())->format("%a");
}
$statut = $action->getStatus();
$data = [
'statut'=> $statut,
'respectDuree'=> $respectDuree,
'joursRetard'=> $joursRetard,
'respectDelais'=> $respectDelais,
'respectBudget'=> $respectBudget,
'statutEE'=> $statutEE,
'idEE'=>$idEE,
'efficaciteActionMother'=>$efficaciteActionMother,
'statutActionMother'=>$statutActionMother,
'idActionMother'=>$idActionMother,
'actionMotherNotClosed'=>$actionMotherNotClosed
];
$response =$data;
} catch (\Exception $e) {
$response = array('status' => 'error', 'message' => $e->getMessage());
}
}
return new JsonResponse($response,200);
}
/**
* @Route("/{id}/validation", name="action_validation", methods={"GET"})
*/
public function validation(Action $action, Request $request): Response
{
$this->denyAccessUnlessGranted(ActionVoter::ACTION_INSERT,$action);
$action->setStatus(StatusEnum::STATUS_IN_PROGRESS);
//$action->setValidated(0);
$action->setValidated(1);
$this->getDoctrine()->getManager()->flush();
$this->addFlash("success","L'action a été validé avec succès");
return $this->redirect($request->headers->get('referer'));
}
/**
* @Route("/{id}/comment", name="action_comment", methods={"GET"})
*/
public function action_comment(Action $action, Request $request) : Response
{
return $this->render('action/comment.html.twig', [
'action'=>$action
]);
}
/**
* @Route("/{id}/comment/save", name="action_comment_save", methods={"POST"})
*/
public function action_comment_save(Action $action, Request $request) : JsonResponse
{
$comment = $request->request->get('comment','');
$action->setComment($comment);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->flush();
return new JsonResponse(['saved'=>1]);
}
public function createPeriodActions(Action $action)
{
$nextAction = clone $action;
$increment = $action->hasActionEE() ? 2: 1;
$ref = Utility::incrementRef($action->getRef(),$increment);
$action->setRef($ref);
$numberOfDays = PeriodActionEnum::getPeriodValue($action->getPeriodicity());
$dateEndEstimated = $action->getDateEndEstimated();
$dateEndEstimated->add(new \DateInterval('P'.$numberOfDays.'D'));
$action->setDateEndEstimated($dateEndEstimated);
}
}