From 4b50f90bb065c26d45f0561f89c07de19436cc47 Mon Sep 17 00:00:00 2001
From: Jean-Philippe Metivier <jean-philippe.metivier@unicaen.fr>
Date: Tue, 5 Jul 2022 18:24:14 +0200
Subject: [PATCH] =?UTF-8?q?Correction=20structure=20des=20agents=20forc?=
 =?UTF-8?q?=C3=A9s?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../Controller/StructureController.php        |  3 +-
 .../Service/Structure/StructureService.php    | 37 +++++++++++++++++++
 2 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/module/Structure/src/Structure/Controller/StructureController.php b/module/Structure/src/Structure/Controller/StructureController.php
index 013449cd0..82df1a6f6 100644
--- a/module/Structure/src/Structure/Controller/StructureController.php
+++ b/module/Structure/src/Structure/Controller/StructureController.php
@@ -93,7 +93,8 @@ class StructureController extends AbstractActionController {
 
         /** Récupération des agents et postes liés aux structures */
         $agents = $this->getAgentService()->getAgentsByStructures($structures);
-        $agentsForces = array_map(function (StructureAgentForce $a) { return $a->getAgent(); }, $structure->getAgentsForces());
+        $agentsForces = $this->getStructureService()->getAgentsForces($structure);
+        $agentsForces = array_map(function (StructureAgentForce $a) { return $a->getAgent(); }, $agentsForces);
         $allAgents = array_merge($agents, $agentsForces);
 
         $fichesRecrutements = $this->getStructureService()->getFichesPostesRecrutementsByStructures($structures);
diff --git a/module/Structure/src/Structure/Service/Structure/StructureService.php b/module/Structure/src/Structure/Service/Structure/StructureService.php
index 7646899b6..9b427ea9e 100644
--- a/module/Structure/src/Structure/Service/Structure/StructureService.php
+++ b/module/Structure/src/Structure/Service/Structure/StructureService.php
@@ -12,6 +12,7 @@ use Doctrine\ORM\NonUniqueResultException;
 use Doctrine\ORM\ORMException;
 use Doctrine\ORM\QueryBuilder;
 use Structure\Entity\Db\Structure;
+use Structure\Entity\Db\StructureAgentForce;
 use Structure\Entity\Db\StructureGestionnaire;
 use Structure\Entity\Db\StructureResponsable;
 use Structure\Provider\RoleProvider;
@@ -572,4 +573,40 @@ EOS;
         $result = $qb->getQuery()->getResult();
         return $result;
     }
+
+    /** GESTION DES AGENTS FORCES *************************************************************************************/
+
+    /**
+     * @param Structure $structure
+     * @param DateTime|null $date
+     * @return StructureAgentForce[]
+     */
+    public function getAgentsForces(Structure $structure, ?DateTime $date = null) : array
+    {
+        if ($date === null) $date = new DateTime();
+
+        $qb = $this->getEntityManager()->getRepository(StructureAgentForce::class)->createQueryBuilder('af')
+            ->join('af.agent', 'af_agent')->addSelect('af_agent')
+            ->join('af.structure', 'af_structure')->addSelect('af_structure')
+            ->leftjoin('af_agent.affectations', 'af_affectation')->addSelect('af_affectation')
+            ->leftjoin('af_affectation.structure', 'affectation_structure')->addSelect('affectation_structure')
+//            ->leftJoin('affectation_structure.responsables', 'structure_responsable')->addSelect('structure_responsable')
+//            ->leftJoin('structure_responsable.agent', 'structure_responsable_agent')->addSelect('structure_responsable_agent')
+            ->leftjoin('affectation_structure.niv2', 'affectation_niv2')->addSelect('affectation_niv2')
+//            ->leftJoin('affectation_niv2.responsables', 'niv2_responsable')->addSelect('niv2_responsable')
+//            ->leftJoin('niv2_responsable.agent', 'niv2_responsable_agent')->addSelect('niv2_responsable_agent')
+
+            ->andWhere('af.structure = :structure')
+            ->setParameter('structure', $structure)
+            ->andWhere('af_affectation.dateDebut IS NULL or af_affectation.dateDebut <= :date')
+            ->andWhere('af_affectation.dateFin IS NULL or af_affectation.dateFin >= :date')
+//            ->andWhere('structure_responsable.dateDebut IS NULL or structure_responsable.dateDebut <= :date')
+//            ->andWhere('structure_responsable.dateFin IS NULL or structure_responsable.dateFin >= :date')
+//            ->andWhere('niv2_responsable.dateDebut IS NULL or niv2_responsable.dateDebut <= :date')
+//            ->andWhere('niv2_responsable.dateFin IS NULL or niv2_responsable.dateFin >= :date')
+            ->setParameter('date', $date)
+        ;
+        $result = $qb->getQuery()->getResult();
+        return $result;
+    }
 }
-- 
GitLab