<?php
namespace App\Controller;
use App\Entity\Vehicule;
use App\Entity\Conducteur;
use App\Form\VehiculeType;
use App\Form\VehiculeConducteurType;
use App\Entity\Photo;
use App\Service\VehiculesTypesBySpotHelper;
use App\Repository\VehiculeRepository;
use App\Repository\ConducteurRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\String\Slugger\SluggerInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
/**
* @Route("/vehicule")
*/
class VehiculeController extends AbstractController
{
public function __construct() {
$this->styleHeader = [
'font' => [
'bold' => true,
'size' => 8,
'name' => 'Arial'
],
];
$this->styleBody = [
'font' => [
'size' => 8,
'name' => 'Arial'
],
];
}
/**
* @Route("/_/{tri}/{asc}", name="app_vehicule_index", methods={"GET"})
*/
public function index(VehiculeRepository $vehiculeRepository, SessionInterface $session, string $tri = NULL, string $asc = NULL): Response
{
$fbbStructures = $this->getParameter('fbb_structures');
$typesVehicules = $this->getParameter('types_vehicules');
$session->set('idf', 0);
$session->set('slug', '');
$request = Request::createFromGlobals();
$url = $request->getPathInfo();
$url = substr($url, 0, 11);
if ($tri == "") {
$orderby = 'marque';
} else {
$orderby = $tri;
}
switch ($asc) {
case "ASC":
$asc = "DESC";
break;
case "DESC":
$asc = "ASC";
break;
}
if ($asc == "") {
$asc = "ASC";
}
return $this->render('vehicule/index.html.twig', [
'vehicules' => $vehiculeRepository->findBy(
['destruction' => 0],
[$orderby => $asc]
),
'sidebar_title' => 'VEHICULES',
'fbbStructures' => $fbbStructures,
'typesVehicules' => $typesVehicules,
'url' => $url,
'asc' => $asc,
'title' => 'GESTION DES VEHICULES',
]);
}
/**
* @Route("/filtres/{slug}/{idf}/{tri}/{asc}", name="app_vehicules_by_filtres", methods={"GET"})
*/
public function vehiculesByFiltres(VehiculeRepository $vehiculeRepository, SessionInterface $session, int $idf = NULL, string $slug = NULL, string $tri = NULL, string $asc = NULL): Response
{
$session->set('idf', $idf);
$session->set('slug', $slug);
$fbbStructures = $this->getParameter('fbb_structures');
$typesVehicules = $this->getParameter('types_vehicules');
$request = Request::createFromGlobals();
$url = $request->getPathInfo();
if (str_contains($url, 'type_vehicule')) {
$titre = $typesVehicules[$idf];
$url = substr($url, 0, 33);
} elseif (str_contains($url, 'lieu_affectation')) {
$titre = $fbbStructures[$idf];
$url = substr($url, 0, 36);
}
if ($tri == "") {
$orderby = 'type_vehicule';
} else {
$orderby = $tri;
}
switch ($asc) {
case "":
$asc = "DESC";
break;
case "ASC":
$asc = "DESC";
break;
case "DESC":
$asc = "ASC";
break;
}
return $this->render('vehicule/index.html.twig', [
'vehicules' => $vehiculeRepository->findBy(
[$slug => $idf, 'destruction' => 0],
[$orderby => $asc]
),
'sidebar_title' => 'VEHICULES',
'typesVehicules' => $typesVehicules,
'fbbStructures' => $fbbStructures,
'url' => $url,
'asc' => $asc,
'title' => strtoupper($titre),
]);
}
/**
* @Route("/destruction/{tri}/{asc}", name="app_vehicule_destruction", methods={"GET"})
*/
public function destruction(VehiculeRepository $vehiculeRepository, string $tri = NULL, string $asc = NULL): Response
{
$fbbStructures = $this->getParameter('fbb_structures');
$typesVehicules = $this->getParameter('types_vehicules');
$request = Request::createFromGlobals();
$url = $request->getPathInfo();
$url = "/vehicule/destruction";
if ($tri == "") {
$orderby = 'marque';
} else {
$orderby = $tri;
}
switch ($asc) {
case "ASC":
$asc = "DESC";
break;
case "DESC":
$asc = "ASC";
break;
}
if ($asc == "") {
$asc = "ASC";
}
return $this->render('vehicule/destruction.html.twig', [
'vehicules' => $vehiculeRepository->findBy(
['destruction' => 1],
[$orderby => $asc]
),
'sidebar_title' => 'VEHICULES DETRUITS',
'typesVehicules' => $typesVehicules,
'fbbStructures' => $fbbStructures,
'title' => 'VEHICULES SORTIS OU DETRUITS',
'url' => $url,
'asc' => $asc,
]);
}
/**
* @Route("/controle_technique/{tri}/{asc}", name="app_controle_technique", methods={"GET"})
*/
public function controleTechnique(VehiculeRepository $vehiculeRepository, string $tri = NULL, string $asc = NULL): Response
{
$fbbStructures = $this->getParameter('fbb_structures');
$typesVehicules = $this->getParameter('types_vehicules');
$request = Request::createFromGlobals();
$url = $request->getPathInfo();
$url = substr($url, 0, 28);
if ($tri == "") {
$tri = 'v.date_prochain_ct';
}
switch ($asc) {
case "ASC":
$asc = "DESC";
break;
case "DESC":
$asc = "ASC";
break;
}
if ($asc == "") {
$asc = "ASC";
}
$vehicules = $vehiculeRepository->findVehiculesByControleTechnique($tri, $asc);
return $this->render('vehicule/controles.html.twig', [
'sidebar_title' => 'CONTROLES TECHNIQUES',
'typesVehicules' => $typesVehicules,
'fbbStructures' => $fbbStructures,
'vehicules' => $vehicules,
'url' => $url,
'asc' => $asc,
'title' => 'GESTION DES VEHICULES',
]);
}
/**
* @Route("/carte_essence/{tri}/{asc}", name="app_carte_essence", methods={"GET"})
*/
public function carteEssence(VehiculeRepository $vehiculeRepository, SessionInterface $session, string $tri = NULL, string $asc = NULL): Response
{
$session->set('slug', 'carte_essence');
$fbbStructures = $this->getParameter('fbb_structures');
$typesVehicules = $this->getParameter('types_vehicules');
$request = Request::createFromGlobals();
$url = $request->getPathInfo();
//$url = substr($url, 0, 28);
if ($tri == "") {
$tri = 'v.type';
}
$url = "/vehicule/carte_essence";
switch ($asc) {
case "ASC":
$asc = "DESC";
break;
case "DESC":
$asc = "ASC";
break;
}
if ($asc == "") {
$asc = "ASC";
}
$vehicules = $vehiculeRepository->findVehiculesByCarteEssence($tri, $asc);
return $this->render('vehicule/carburant.html.twig', [
'sidebar_title' => 'CARTE CARBURANT',
'typesVehicules' => $typesVehicules,
'fbbStructures' => $fbbStructures,
'vehicules' => $vehicules,
'url' => $url,
'asc' => $asc,
'title' => 'VEHICULES AVEC CARTE CARBURANT',
]);
}
/**
* @Route("/echeance_assurance/{tri}/{asc}", name="app_echeance_assurance", methods={"GET"})
*/
public function echeanceAssurance(VehiculeRepository $vehiculeRepository, string $tri = NULL, string $asc = NULL): Response
{
$fbbStructures = $this->getParameter('fbb_structures');
$typesVehicules = $this->getParameter('types_vehicules');
$request = Request::createFromGlobals();
$url = $request->getPathInfo();
$url = substr($url, 0, 28);
if ($tri == "") {
$tri = 'v.echeance_assurance';
}
switch ($asc) {
case "ASC":
$asc = "DESC";
break;
case "DESC":
$asc = "ASC";
break;
}
if ($asc == "") {
$asc = "ASC";
}
$vehicules = $vehiculeRepository->findVehiculesByEcheanceAssurance($tri, $asc);
return $this->render('vehicule/assurance.html.twig', [
'sidebar_title' => 'ASSURANCE',
'typesVehicules' => $typesVehicules,
'fbbStructures' => $fbbStructures,
'vehicules' => $vehicules,
'url' => $url,
'asc' => $asc,
'title' => 'GESTION DES VEHICULES',
]);
}
/**
* @Route("/new", name="app_vehicule_new", methods={"GET", "POST"})
*/
public function new(Request $request, VehiculeRepository $vehiculeRepository, SessionInterface $session): Response
{
$idf = $session->get('idf');
$slug = $session->get('slug');
$vehicule = new Vehicule();
$fbbStructures = $this->getParameter('fbb_structures');
$structures = array_flip($fbbStructures);
array_splice($structures, 0, 1);
$typesVehicules = $this->getParameter('types_vehicules');
$vehicules = array_flip($typesVehicules);
array_splice($vehicules, 0, 1);
$form = $this->createForm(VehiculeType::class, $vehicule, [
'fbbStructures' => $structures,
'typesVehicules' => $vehicules,
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$vehicule->setDateInsert(new \DateTime());
$vehicule->setUser($this->getUser());
$vehiculeRepository->add($vehicule, true);
//$this->addFlash('success', 'Ajout réussi');
} catch (\Exception $e) {
$this->addFlash('warning', 'Echec : ' . $e->getMessage());
}
return $this->redirectToRoute('app_vehicule_show', ['id' => $vehicule->getId()], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('vehicule/new.html.twig', [
'vehicule' => $vehicule,
'form' => $form,
'title' => 'Ajout d\'un nouveau véhicule',
'idf' => $idf,
'slug' => $slug,
'title' => 'GESTION DES VEHICULES',
]);
}
/**
* @Route("/show/{id}", name="app_vehicule_show", methods={"GET"})
*/
public function show(Vehicule $vehicule, SessionInterface $session): Response
{
$idf = $session->get('idf');
$slug = $session->get('slug');
$fbbStructures = $this->getParameter('fbb_structures');
$typesVehicules = $this->getParameter('types_vehicules');
return $this->render('vehicule/show.html.twig', [
'vehicule' => $vehicule,
'typesVehicules' => $typesVehicules,
'fbbStructures' => $fbbStructures,
'idf' => $idf,
'slug' => $slug,
'vehicule_id' => $vehicule->getId(),
'title' => 'GESTION DES VEHICULES',
]);
}
/**
* @Route("/conducteur_associe/{id}", name="app_conducteur_associe", methods={"GET", "POST"})
*/
public function conduteur(Request $request, Vehicule $vehicule, SessionInterface $session, ConducteurRepository $conducteurRepository): Response
{
$idf = $session->get('idf');
$slug = $session->get('slug');
$fbbStructures = $this->getParameter('fbb_structures');
$typesVehicules = $this->getParameter('types_vehicules');
// liste de conducteurs associés
$conducteurs = $vehicule->getConducteurs();
$form = $this->createForm(VehiculeConducteurType::class, $vehicule);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//foreach($conducteurs as $conducteur) {
$conducteurRepository->remove_vehicule_conducteur($vehicule->getId());
//}
$entityManager = $this->getDoctrine()->getManager();
$datas = $form->getData();
//dd($datas);
foreach ($datas->getConducteurs() as $conducteur) {
$conducteur->addVehicule($vehicule);
}
$entityManager->flush();
//return $this->redirectToRoute('app_vehicule_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('vehicule/vehicule_conducteurs.html.twig', [
'vehicule' => $vehicule,
'form' => $form->createView(),
'conducteurs' => $conducteurs,
'typesVehicules' => $typesVehicules,
'fbbStructures' => $fbbStructures,
'idf' => $idf,
'slug' => $slug,
'title' => 'Conducteurs du véhicule',
'vehicule_id' => $vehicule->getId(),
]);
}
/**
* @Route("/statistique/", name="app_vehicule_statistique", methods={"GET"})
*/
public function statistique(VehiculeRepository $vehiculeRepository, VehiculesTypesBySpotHelper $vehiculesTypesBySpotHelper): Response
{
$fbbStructures = $this->getParameter('fbb_structures');
$typesVehicules = $this->getParameter('types_vehicules');
// par types de véhicules
$ville = $vehiculeRepository->findVehiculesByType(1);
$petits_utilitaires = $vehiculeRepository->findVehiculesByType(2);
$grands_utilitaires = $vehiculeRepository->findVehiculesByType(3);
$agricoles = $vehiculeRepository->findVehiculesByType(4);
$speciaux = $vehiculeRepository->findVehiculesByType(5);
$atteles = $vehiculeRepository->findVehiculesByType(6);
$moyens_utilitaires = $vehiculeRepository->findVehiculesByType(7);
$aeronefs = $vehiculeRepository->findVehiculesByType(8);
$deux_roues = $vehiculeRepository->findVehiculesByType(9);
$bazoches = $vehiculeRepository->findVehiculesByLieuAffectation(1);
$bazoches_detail = $vehiculeRepository->findTypesVehiculesByLieuAffectation(1);
$bazoches_type = $vehiculesTypesBySpotHelper->vehiculesTypesBySpot($typesVehicules, $bazoches_detail);
//print_r($bazoches_type);
//die;
$daviere = $vehiculeRepository->findVehiculesByLieuAffectation(2);
$daviere_detail = $vehiculeRepository->findTypesVehiculesByLieuAffectation(2);
$daviere_type = $vehiculesTypesBySpotHelper->vehiculesTypesBySpot($typesVehicules, $daviere_detail);
$mareauzou = $vehiculeRepository->findVehiculesByLieuAffectation(3);
$mareauzou_detail = $vehiculeRepository->findTypesVehiculesByLieuAffectation(3);
$mareauzou_type = $vehiculesTypesBySpotHelper->vehiculesTypesBySpot($typesVehicules, $mareauzou_detail);
$montpon = $vehiculeRepository->findVehiculesByLieuAffectation(4);
$montpon_detail = $vehiculeRepository->findTypesVehiculesByLieuAffectation(4);
$montpon_type = $vehiculesTypesBySpotHelper->vehiculesTypesBySpot($typesVehicules, $montpon_detail);
$siege = $vehiculeRepository->findVehiculesByLieuAffectation(5);
$siege_detail = $vehiculeRepository->findTypesVehiculesByLieuAffectation(5);
$siege_type = $vehiculesTypesBySpotHelper->vehiculesTypesBySpot($typesVehicules, $siege_detail);
$bernay = $vehiculeRepository->findVehiculesByLieuAffectation(6);
$bernay_detail = $vehiculeRepository->findTypesVehiculesByLieuAffectation(6);
$bernay_type = $vehiculesTypesBySpotHelper->vehiculesTypesBySpot($typesVehicules, $bernay_detail);
$tropez = $vehiculeRepository->findVehiculesByLieuAffectation(7);
$tropez_detail = $vehiculeRepository->findTypesVehiculesByLieuAffectation(7);
$tropez_type = $vehiculesTypesBySpotHelper->vehiculesTypesBySpot($typesVehicules, $tropez_detail);
$total = $bazoches + $daviere + $mareauzou + $montpon + $siege + $bernay + $tropez;
return $this->render('vehicule/statistique.html.twig', [
'typesVehicules' => $typesVehicules,
'fbbStructures' => $fbbStructures,
'ville' => $ville,
'petits_utilitaires' => $petits_utilitaires,
'grands_utilitaires' => $grands_utilitaires,
'agricoles' => $agricoles,
'speciaux' => $speciaux,
'atteles' => $atteles,
'moyens_utilitaires' => $moyens_utilitaires,
'aeronefs' => $aeronefs,
'deux_roues' => $deux_roues,
'bazoches' => $bazoches,
'daviere' => $daviere,
'mareauzou' => $mareauzou,
'montpon' => $montpon,
'bernay' => $bernay,
'siege' => $siege,
'tropez' => $tropez,
'total' => $total,
'bazoches_type' => $bazoches_type,
'daviere_type' => $daviere_type,
'mareauzou_type' => $mareauzou_type,
'montpon_type' => $montpon_type,
'siege_type' => $siege_type,
'bernay_type' => $bernay_type,
'tropez_type' => $tropez_type,
'title' => 'GESTION DES VEHICULES',
]);
}
/**
* @Route("/edit/{id}/edit", name="app_vehicule_edit", methods={"GET", "POST"})
*/
public function edit(Request $request, Vehicule $vehicule, VehiculeRepository $vehiculeRepository, SessionInterface $session): Response
{
$idf = $session->get('idf');
$slug = $session->get('slug');
if ($idf == 0) {
$flag = false;
} else {
$flag = true;
}
$fbbStructures = $this->getParameter('fbb_structures');
$structures = array_flip($fbbStructures);
$typesVehicules = $this->getParameter('types_vehicules');
$vehicules = array_flip($typesVehicules);
$form = $this->createForm(VehiculeType::class, $vehicule, [
'fbbStructures' => $structures,
'typesVehicules' => $vehicules,
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$vehicule->setDateUpdate(new \DateTime());
$vehicule->setUser($this->getUser());
$vehiculeRepository->add($vehicule, true);
//$this->addFlash('success', 'Ajout réussi');
} catch (\Exception $e) {
$this->addFlash('warning', 'Echec : ' . $e->getMessage());
}
if ($flag) {
if ($slug != 'carte_essence') {
return $this->redirectToRoute('app_vehicules_by_filtres', ['slug'=>$slug, 'idf'=>$idf], Response::HTTP_SEE_OTHER);
} elseif ($slug == 'carte_essence') {
return $this->redirectToRoute('app_carte_essence', [], Response::HTTP_SEE_OTHER);
}
} else {
if ($slug != 'carte_essence') {
return $this->redirectToRoute('app_vehicule_index', [], Response::HTTP_SEE_OTHER);
} elseif ($slug == 'carte_essence') {
return $this->redirectToRoute('app_carte_essence', [], Response::HTTP_SEE_OTHER);
}
}
}
return $this->renderForm('vehicule/new.html.twig', [
'vehicule' => $vehicule,
'form' => $form,
'title' => 'Mise à jour d\'une fiche véhicule',
'idf' => $idf,
'slug' => $slug,
'title' => 'GESTION DES VEHICULES',
]);
}
/**
* @Route("/delete/{id}", name="app_vehicule_delete", methods={"POST"})
*/
public function delete(Request $request, Vehicule $vehicule, VehiculeRepository $vehiculeRepository): Response
{
if ($this->isCsrfTokenValid('delete'.$vehicule->getId(), $request->request->get('_token'))) {
$vehiculeRepository->remove($vehicule, true);
}
return $this->redirectToRoute('app_vehicule_index', [], Response::HTTP_SEE_OTHER);
}
/**
* @Route("/controle_technique_xls", name="app_controle_technique_xls", methods={"GET"})
*/
public function controleTechniqueXLS(VehiculeRepository $vehiculeRepository, string $tri = NULL, string $asc = NULL)
{
$fbbStructures = $this->getParameter('fbb_structures');
$typesVehicules = $this->getParameter('types_vehicules');
$tri = 'v.date_prochain_ct';
$asc = 'ASC';
$vehicules = $vehiculeRepository->findVehiculesByControleTechnique($tri, $asc);
foreach ($vehicules as $v) {
if ($v->getDateControlePollution() != NULL) {
$cp = $v->getDateControlePollution()->format('d/m/Y');
} else {
$cp = '';
}
$list[] = [
$v->getDateProchainCt()->format('d/m/Y'),
$cp,
$v->getMarque(),
$v->getType(),
$v->getImmatriculation(),
$fbbStructures[$v->getLieuAffectation()],
];
}
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Contrôles techniques');
$lignes = count($list)+1;
$sheet->getStyle('A1:F1')->applyFromArray($this->styleHeader);
//$sheet->getStyle('B2:F'.$lignes)->applyFromArray($this->styleBody);
//$sheet->getStyle('A2:A'.$lignes)->getNumberFormat()->setFormatCode('00 000');
$sheet->getCell('A1')->setValue('Date CT');
$sheet->getCell('B1')->setValue('Contrôle pollution');
$sheet->getCell('C1')->setValue('Marque');
$sheet->getCell('D1')->setValue('Type');
$sheet->getCell('E1')->setValue('Immatriculation');
$sheet->getCell('F1')->setValue('Structure Fbb');
// Increase row cursor after header write
$sheet->fromArray($list, null, 'A2', true);
$date = date('d-m-Y_hms', time());
$writer = new Xlsx($spreadsheet);
// Create a Temporary file in the system
$fileName = 'controles_techniques-' . $date . '.xlsx';
$temp_file = tempnam(sys_get_temp_dir(), $fileName);
// Create the excel file in the tmp directory of the system
$writer->save($temp_file);
// Return the excel file as an attachment
return $this->file($temp_file, $fileName, ResponseHeaderBag::DISPOSITION_INLINE);
}
/**
* @Route("/echeance_assurance_xls", name="app_echeance_assurance_xls", methods={"GET"})
*/
public function echeanceAssuranceXLS(VehiculeRepository $vehiculeRepository, string $tri = NULL, string $asc = NULL)
{
$fbbStructures = $this->getParameter('fbb_structures');
$typesVehicules = $this->getParameter('types_vehicules');
$tri = 'v.echeance_assurance';
$asc = 'ASC';
$vehicules = $vehiculeRepository->findVehiculesByEcheanceAssurance($tri, $asc);
foreach ($vehicules as $v) {
$list[] = [
$v->getEcheanceAssurance()->format('d/m/Y'),
$v->getMarque(),
$v->getType(),
$v->getImmatriculation(),
$fbbStructures[$v->getLieuAffectation()],
];
}
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Echéances assurances');
$lignes = count($list)+1;
$sheet->getStyle('A1:E1')->applyFromArray($this->styleHeader);
$sheet->getStyle('B2:E'.$lignes)->applyFromArray($this->styleBody);
$sheet->getStyle('A2:A'.$lignes)->getNumberFormat()->setFormatCode('00 000');
$sheet->getCell('A1')->setValue('Echéance assurance');
$sheet->getCell('B1')->setValue('Marque');
$sheet->getCell('C1')->setValue('Type');
$sheet->getCell('D1')->setValue('Immatriculation');
$sheet->getCell('E1')->setValue('Structure Fbb');
// Increase row cursor after header write
$sheet->fromArray($list, null, 'A2', true);
$date = date('d-m-Y_hms', time());
$writer = new Xlsx($spreadsheet);
// Create a Temporary file in the system
$fileName = 'controle_assurances-' . $date . '.xlsx';
$temp_file = tempnam(sys_get_temp_dir(), $fileName);
// Create the excel file in the tmp directory of the system
$writer->save($temp_file);
// Return the excel file as an attachment
return $this->file($temp_file, $fileName, ResponseHeaderBag::DISPOSITION_INLINE);
}
#[Route("/vehicules/reparations", name: "app_vehicule_en_reparation", methods: ['GET'])]
public function vehicule_en_reparation(VehiculeRepository $vehiculeRepository): Response
{
$reparations = $vehiculeRepository->findVehiculesEnReparation();
$fbbStructures = $this->getParameter('fbb_structures');
$typesVehicules = $this->getParameter('types_vehicules');
//foreach($reparations as $val) {
//print_r($val);
//}
return $this->render('vehicule/vehicules_en_reparation.html.twig', [
'reparations' => $reparations,
'typesVehicules' => $typesVehicules,
'fbbStructures' => $fbbStructures,
'title' => 'Véhicules en réparation',
]);
}
}