<?php
namespace App\Controller\Dysfonctionnement;
use App\Entity\Dysfonctionnement\CategoryConstat;
use App\Entity\Dysfonctionnement\Constat;
use App\Entity\Processus;
use App\Entity\Reunion;
use App\Entity\User;
use App\EventListener\ConstatUpdated;
use App\Repository\Dysfonctionnement\ConstatRepository;
use Doctrine\Persistence\ManagerRegistry;
use Dompdf\Dompdf;
use Dompdf\Options;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/dysfonctionnement/analyse")
*/
class AnalyseController extends AbstractController
{
/**
* @Route("/{id}", name="analyse")
*/
public function index(Constat $constat ,Request $request): Response
{
$menu="ameliorer";
$left_menu="enregistrer_constat";
return $this->render('dysfonctionnementTemplate/analyse/index.html.twig', [
"constat" => $constat,
"menu" => $menu,
"left_menu" => $left_menu
]);
}
/**
* @Route("/modal/decision/{id}", name="prise_decision_modal", methods={"GET","POST"})
*/
public function priseDecisionModal(Request $request, Constat $constat): Response
{
$processus_list = $this->getDoctrine()->getRepository(Processus::class)->findAll();
$users_list = $this->getDoctrine()->getRepository(User::class)->findAll();
$categoryConstat = $this->getDoctrine()->getRepository(CategoryConstat::class)->findAll();
$users_list_array = [];
foreach($users_list as $user) {
$fonction = str_replace('"', "", $user->getFonction());
$users_list_array[$user->getId()] = $fonction;
}
return $this->render('dysfonctionnementTemplate/analyse/new.html.twig', [
'processus_list' => $processus_list,
'users_list' => $users_list,
'categoryConstat' => $categoryConstat,
'users_list_array'=>$users_list_array,
'constat' => $constat
]);
}
/**
* @Route("/prise_decision/{id}", name="prise_decision_new", methods={"GET","POST"})
*/
public function priseDecision(Request $request, Constat $constat): Response
{
$doctrine = $this->getDoctrine();
$entityManager = $this->getDoctrine()->getManager();
if (!$constat) {
throw $this->createNotFoundException(
'There are no constat with the following id: ' . $constat->getId()
);
}
$processus_id = $request->request->get('processus');
$processus = $doctrine->getRepository(Processus::class)->find($processus_id);
$category_id = $request->request->get('categoryConstat');
$categoryConstat = $doctrine->getRepository(CategoryConstat::class)->find($category_id);
$responsable_suivi_id = $request->request->get('responsable_suivi');
$responsable_suivi = $doctrine->getRepository(User::class)->find($responsable_suivi_id);
$cout = $request->request->get('cout');
$commentaireDecision = $request->request->get('commentaireDecision');
$is_investigation = $request->request->get('is_investigation');
$action_temp = $request->request->get('is_action_temp');
$cause_planifier = $request->request->get('cause_planifier');
$methode_analyse = $request->request->get('methode_analyse');
$priority = $request->request->get('priority');
if ($is_investigation == "true" ) {
$constat->setNiveauTraitement("Sans suite");
$constat->setIsInvestigation(true);
$constat->setResponsableSuivie(Null);
$constat->setPriorite(Null);
}else {
$constat->setIsInvestigation(false);
$constat->setResponsableSuivie($responsable_suivi);
$constat->setPriorite($priority);
}
if ($action_temp == "true" ) {
$constat->setNiveauTraitement("Temporaire");
$constat->setIsActionTemp(true);
}else {
$constat->setIsActionTemp(false);
}
if ($cause_planifier == "true" ) {
$constat->setNiveauTraitement("Définitif");
$constat->setMethodeAnalyse($methode_analyse);
$constat->setCausePlanifier(true);
}
$constat->setProcessus($processus);
$constat->setCout(floatval($cout));
$constat->setCategoryConstat($categoryConstat);
$constat->setCommentaireDecision($commentaireDecision);
$constat->setDateCloture( new \DateTime('now'));
$entityManager->flush();
return $this->redirectToRoute('analyse',['id'=>$constat->getId()]);
// $menu="ameliorer";
// $left_menu="suivi_general";
// return $this->forward('App\Controller\Dysfonctionnement\AnalyseController::index',[
// "constat" => $constat,
// "menu" => $menu,
// "left_menu" => $left_menu
// ]);
}
/**
* @Route("/list_pa_preventif/{id}", name="list_pa_preventif", methods={"GET"})
*/
public function list_pa_preventif(Constat $constat)
{
$actionPlansPreventif = $constat->getActionPlanPreventif();
$actionPlansPreventif_data=['data'=>[]];
foreach($actionPlansPreventif->getActions() as $action) {
$ref=$action->getRef();
$actionPlansPreventif_data['data'][]=[
'pa_ref'=>$ref,
'pa_echeance'=>$action->getDateEndEstimated(true),
'pa_responsable'=>$action->getCreatedBy()->getFullName(),
'pa_title'=>$action->getTitle()
];
}
return new JsonResponse($actionPlansPreventif_data);
}
/**
* @Route("/constat/generate_pdf/{id}", name="constat_generate_pdf", methods={"GET"})
*/
public function constat_generate_pdf(Constat $constat)
{
$path_logo = $this->getParameter('kernel.project_dir').'/public/img/persyst-logo.png';
$type_logo = pathinfo($path_logo, PATHINFO_EXTENSION);
$data_logo = file_get_contents($path_logo);
$base64_logo = 'data:image/' . $type_logo . ';base64,' . base64_encode($data_logo);
// Configure Dompdf according to your needs
$pdfOptions = new Options();
$pdfOptions->set('defaultFont', 'Arial');
$pdfOptions->setIsRemoteEnabled(true);
// Instantiate Dompdf with our options
$dompdf = new Dompdf($pdfOptions);
// return $this->render('pdf/constat_analyse/index.html.twig',[
// 'base64_logo' => $base64_logo,
// ''fontawesomecss_path'=>$fontawesomecss_path
// 'constat'=>$constat
// ]);
$html = $this->renderView('pdf/constat_analyse/index.html.twig', [
'base64_logo' => $base64_logo,
'constat'=>$constat
]);
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
$doc = 'constat_'.$constat->getReference().'.pdf';
$dir = $this->getParameter('kernel.project_dir').'/public/uploads/pdf/constat/'.$constat->getId().'/';
if (!file_exists($dir)) {
$fs=new Filesystem();
$fs->mkdir($dir);
}
$output = $dompdf->output();
$pdfFilepath = $dir.$doc;
file_put_contents($pdfFilepath, $output);
$constat->setPdfFile($pdfFilepath);
$constat->setEnableListener(false);
$em= $this->getDoctrine()->getManager();
$em->flush();
$dompdf->stream($doc, [
"Attachment" => true
]);
}
}