src/Controller/DashboardController.php line 32
<?php
namespace App\Controller;
use App\Repository\CommunesRepository;
use App\Repository\DemandeExportateurRepository;
use App\Repository\DemandeExporterProduitRepository;
use App\Repository\DemandeInspectionRepository;
use App\Repository\DepartementsRepository;
use App\Repository\EvaluationRepository;
use App\Repository\InspectionRepository;
use App\Repository\RegionsRepository;
use App\Repository\UserRepository;
use Doctrine\Persistence\ManagerRegistry;
use Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonRemove;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
class DashboardController extends AbstractController
{
public function __construct(ManagerRegistry $registry)
{
$this->conn = $registry->getManager()->getConnection();
}
#[Route('/', name: 'index')]
#[IsGranted('ROLE_USER')]
public function index(): Response
{
if (in_array('ROLE_ADMIN', $this->getUser()->getRoles())) {
return $this->redirectToRoute('app_users');
} else if (in_array('ROLE_DPV', $this->getUser()->getRoles())) {
return $this->redirectToRoute('dashboard_dpv');
} else {
return $this->redirectToRoute('app_market_stats');
}
// return $this->render('dashboard/index.html.twig', [
// 'controller_name' => 'DashboardController',
// ]);
}
#[Route('dpv/dashboard', name: 'dashboard_dpv')]
#[IsGranted('ROLE_USER')]
public function dashboard_dpv(
RegionsRepository $repoReg,
DemandeExportateurRepository $repoDExport,
DemandeExporterProduitRepository $repoExpoertProd,
DemandeInspectionRepository $demandeInsRepo,
InspectionRepository $repoInspect,
EvaluationRepository $repoEval,
UserRepository $utilisateursRepo,
) {
$demandeExportateurEnAttente = count($repoDExport->findBy(['isValid' => FALSE]));
$demandeExporterProdEnAttente = count($repoExpoertProd->findBy(['isOk' => FALSE, 'etat_demande' => '0']));
$demandeInspectionEnAttente = count($demandeInsRepo->findBy(['isScheduled' => FALSE]));
$demandeExportateur = count($repoDExport->findAll());
$demandeExporterProd = count($repoExpoertProd->findAll());
$inspections = count($repoInspect->findAll());
$evaluations = count($repoEval->findAll());
return $this->render('dashboard/dpv.html.twig', [
'regions' => $repoReg->findBy([], ['code' => 'ASC']),
'demandeExportateurEnAttente' => $demandeExportateurEnAttente,
'demandeExporterProdEnAttente' => $demandeExporterProdEnAttente,
'demandeInspectionEnAttente' => $demandeInspectionEnAttente,
'inspections' => $inspections,
'evaluations' => $evaluations,
'producteurs' => count($utilisateursRepo->findUsersByRoleIn('ROLE_PRODUCTEUR')),
'exportateurs' => count($utilisateursRepo->findUsersByRoleIn('ROLE_EXPORTATEUR')),
'demandeExportateur' => $demandeExportateur,
'demandeExporterProd' => $demandeExporterProd,
]);
}
#[Route('dpv/calendrier', name: 'planning_calendar')]
#[IsGranted('ROLE_USER')]
public function calendrier_dpv()
{
return $this->render('dashboard/calendrier_dpv.html.twig', []);
}
#[Route('/reports_dash', name: 'reports_dashboard', options: ['expose' => true])]
#[IsGranted('ROLE_USER')]
public function stats_rep(UserRepository $repo): Response
{
$connectedUser = $repo->findOneBy(['id' => $this->getUser()]);
return $this->render('dashboard/stats.html.twig', [
'connected' => $connectedUser
]);
}
/**
* @Route("/getStat/{codArea}", name="getStats",methods={"GET"},options={"expose"=true})
* @IsGranted("ROLE_USER")
*
*/
public function JsonStats(UserRepository $repo, $codArea)
{
$isSupAdmin = FALSE;
if($this->isGranted("ROLE_ADMIN")){
$isSupAdmin = TRUE;
}
$connectedUser = $repo->findOneBy(['id' => $this->getUser()]);
$code_prod = $connectedUser->getCode();
if ($codArea == 'all' && !$isSupAdmin) {
$requete = "
SELECT COALESCE(rec.prod_nbr_unit, 0) AS units,
COALESCE(rec.prod_nbr_active_unit) AS units_exp
FROM \"producteur_cases\" cas JOIN \"producteur_level-1\" lev ON cas.id = lev.\"case-id\"
JOIN \"producteur_prod_var_cal_rec\" rec ON lev.\"level-1-id\" = rec.\"level-1-id\"
WHERE cas.deleted = 0 AND lev.prod_id = '".$code_prod."'
";
$requeteRep = "
SELECT COALESCE(COUNT(verg.*), 0) AS vergers,
COALESCE(SUM(CASE WHEN verg.unit_verger_exploite = 1 THEN 1 ELSE 0 END), 0) AS vergers_exp
FROM \"unite_cases\" cas JOIN \"unite_level-1\" lev ON cas.id = lev.\"case-id\"
JOIN \"unite_unit_verger\" verg ON lev.\"level-1-id\" = verg.\"level-1-id\"
JOIN \"unite_unit_identificatio_reci\" ident ON verg.\"level-1-id\" = ident.\"level-1-id\"
WHERE cas.deleted = 0 AND lev.unit_prod_id = '".$code_prod."'
";
$requeteParc = "
SELECT COALESCE(COUNT(parc.*), 0) AS parcels,
COALESCE(SUM(CASE WHEN parc.unit_is_exploited = 1 THEN 1 ELSE 0 END), 0) AS parcels_exp
FROM \"unite_cases\" cas JOIN \"unite_level-1\" lev ON cas.id = lev.\"case-id\"
JOIN \"unite_unit_parcelle\" parc ON lev.\"level-1-id\" = parc.\"level-1-id\"
JOIN \"unite_unit_identificatio_reci\" ident ON parc.\"level-1-id\" = ident.\"level-1-id\"
WHERE cas.deleted = 0 AND lev.unit_prod_id = '".$code_prod."'
";
} else if($codArea == 'all' && $isSupAdmin){
// var_dump('ICI'); die;
$requete = "
SELECT COALESCE(rec.prod_nbr_unit, 0) AS units,
COALESCE(rec.prod_nbr_active_unit) AS units_exp
FROM \"producteur_cases\" cas JOIN \"producteur_level-1\" lev ON cas.id = lev.\"case-id\"
JOIN \"producteur_prod_var_cal_rec\" rec ON lev.\"level-1-id\" = rec.\"level-1-id\"
WHERE cas.deleted = 0
";
$requeteRep = "
SELECT COALESCE(COUNT(verg.*), 0) AS vergers,
COALESCE(SUM(CASE WHEN verg.unit_verger_exploite = 1 THEN 1 ELSE 0 END), 0) AS vergers_exp
FROM \"unite_cases\" cas JOIN \"unite_level-1\" lev ON cas.id = lev.\"case-id\"
JOIN \"unite_unit_verger\" verg ON lev.\"level-1-id\" = verg.\"level-1-id\"
JOIN \"unite_unit_identificatio_reci\" ident ON verg.\"level-1-id\" = ident.\"level-1-id\"
WHERE cas.deleted = 0
";
$requeteParc = "
SELECT COALESCE(COUNT(parc.*), 0) AS parcels,
COALESCE(SUM(CASE WHEN parc.unit_is_exploited = 1 THEN 1 ELSE 0 END), 0) AS parcels_exp
FROM \"unite_cases\" cas JOIN \"unite_level-1\" lev ON cas.id = lev.\"case-id\"
JOIN \"unite_unit_parcelle\" parc ON lev.\"level-1-id\" = parc.\"level-1-id\"
JOIN \"unite_unit_identificatio_reci\" ident ON parc.\"level-1-id\" = ident.\"level-1-id\"
WHERE cas.deleted = 0
";
} else {
$requete = "
SELECT COALESCE(rec.prod_nbr_unit, 0) AS units,
COALESCE(rec.prod_nbr_active_unit) AS units_exp
FROM \"producteur_cases\" cas JOIN \"producteur_level-1\" lev ON cas.id = lev.\"case-id\"
JOIN \"producteur_prod_var_cal_rec\" rec ON lev.\"level-1-id\" = rec.\"level-1-id\"
WHERE cas.deleted = 0 AND lev.prod_id = '".$code_prod."' AND inf.prod_commune = '".$codArea."'
";
$requeteRep = "
SELECT COALESCE(COUNT(verg.*), 0) AS vergers,
COALESCE(SUM(CASE WHEN verg.unit_verger_exploite = 1 THEN 1 ELSE 0 END), 0) AS vergers_exp
FROM \"unite_cases\" cas JOIN \"unite_level-1\" lev ON cas.id = lev.\"case-id\"
JOIN \"unite_unit_verger\" verg ON lev.\"level-1-id\" = verg.\"level-1-id\"
JOIN \"unite_unit_identificatio_reci\" ident ON verg.\"level-1-id\" = ident.\"level-1-id\"
WHERE cas.deleted = 0 AND lev.unit_prod_id = '".$code_prod."' AND ident.unit_commune = '".$codArea."'
";
$requeteParc = "
SELECT COALESCE(COUNT(parc.*), 0) AS parcels,
COALESCE(SUM(CASE WHEN parc.unit_is_exploited = 1 THEN 1 ELSE 0 END), 0) AS parcels_exp
FROM \"unite_cases\" cas JOIN \"unite_level-1\" lev ON cas.id = lev.\"case-id\"
JOIN \"unite_unit_parcelle\" parc ON lev.\"level-1-id\" = parc.\"level-1-id\"
JOIN \"unite_unit_identificatio_reci\" ident ON parc.\"level-1-id\" = ident.\"level-1-id\"
WHERE cas.deleted = 0 AND lev.unit_prod_id = '".$code_prod."' AND ident.unit_commune = '".$codArea."'
";
}
$stmt = $this->conn->fetchAllAssociative($requete);
$stmtRep = $this->conn->fetchAllAssociative($requeteRep);
$stmtParc = $this->conn->fetchAllAssociative($requeteParc);
$units = 0;
$units_exp = 0;
$vergers = 0;
$vergers_exp = 0;
$parcels = 0;
$parcels_exp = 0;
if(count($stmt) > 0){
$oneRep = $stmt[0];
$units = $oneRep['units'];
$units_exp = $oneRep['units_exp'];
}
if(count($stmtRep) > 0){
$oneRep2 = $stmtRep[0];
$vergers = $oneRep2['vergers'];
$vergers_exp = $oneRep2['vergers_exp'];
}
if(count($stmtParc) > 0){
$oneParcel = $stmtParc[0];
$parcels = $oneParcel['parcels'];
$parcels_exp = $oneParcel['parcels_exp'];
}
return new JsonResponse(array(
'units' => $units, 'units_exp' => $units_exp
, 'vergers' => $vergers, 'vergers_exp' => $vergers_exp
, 'parcels' => $parcels, 'parcels_exp' => $parcels_exp
));
}
}