From d5da6ffa7443b5d0b0cfd127cf96ed2db4e2babd Mon Sep 17 00:00:00 2001
From: Jean-Philippe Metivier <jean-philippe.metivier@unicaen.fr>
Date: Fri, 16 Dec 2022 10:29:10 +0100
Subject: [PATCH] =?UTF-8?q?[FIX]=20correction=20du=20choix=20de=20l'=C3=A9?=
 =?UTF-8?q?chelon=20(si=20plusieurs=20=C3=A9chelons=20actifs=20alors=20sel?=
 =?UTF-8?q?ection=20du=20plus=20r=C3=A9cent=20en=20terme=20de=20date=20de?=
 =?UTF-8?q?=20passage)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 documentation/release/3.1.2.md                |  1 +
 .../src/Application/Entity/Db/Agent.php       | 15 +++--
 .../Application/Entity/Db/AgentEchelon.php    | 60 ++-----------------
 .../Db/MacroContent/AgentMacroTrait.php       |  2 +-
 ...Application.Entity.Db.AgentEchelon.dcm.xml |  3 +-
 .../Entity/Db/Traits/HasPeriodeTrait.php      | 26 ++------
 .../View/Helper/partial/agent-grade.phtml     |  2 +-
 .../view/application/verification/index.phtml |  2 +-
 8 files changed, 24 insertions(+), 87 deletions(-)

diff --git a/documentation/release/3.1.2.md b/documentation/release/3.1.2.md
index be1dbad0f..40d5d8193 100644
--- a/documentation/release/3.1.2.md
+++ b/documentation/release/3.1.2.md
@@ -5,6 +5,7 @@
 **Changements**
 
 * Ajout d'un parametre indiquant le chamin d'installation pour la partie Vérification
+* [FIX] correction du choix de l'échelon (si plusieurs échelons actifs alors selection du plus récent "en terme de date de passage")
 * [FIX] propagation d'un renommage de variable sur l'interface des supérieurs hiérarchique
 
 **Modifications de la base de donnée**
diff --git a/module/Application/src/Application/Entity/Db/Agent.php b/module/Application/src/Application/Entity/Db/Agent.php
index fdb27b1b5..c13fa4699 100644
--- a/module/Application/src/Application/Entity/Db/Agent.php
+++ b/module/Application/src/Application/Entity/Db/Agent.php
@@ -416,18 +416,21 @@ class Agent implements
         $echelons = $this->echelons->toArray();
         $grades = array_filter($echelons, function (AgentEchelon $ag) { return !$ag->isDeleted();});
         usort($grades, function (AgentEchelon $a, AgentEchelon $b) {
-            return $a->getDate() > $b->getDate();
+            return $a->getDateDebut() > $b->getDateDebut();
         });
         return $grades;
     }
 
-    /**
-     * @return AgentEchelon|null
-     */
-    public function getEchelonActif() : ?AgentEchelon
+    public function getEchelonActif(?DateTime $date = null) : ?AgentEchelon
     {
+        if ($date === null) $date = new DateTime();
         $echelons = $this->getEchelons();
-        $echelon = (!empty($echelons))?$echelons[0]:null;
+        $echelons = array_filter($echelons, function (AgentEchelon $a) use ($date) {return $a->estEnCours($date);});
+
+        $echelon = null;
+        foreach ($echelons as $echelon_) {
+            if ($echelon === null OR $echelon_->getDateDebut() > $echelon->getDateDebut()) $echelon = $echelon_;
+        }
         return $echelon;
     }
 
diff --git a/module/Application/src/Application/Entity/Db/AgentEchelon.php b/module/Application/src/Application/Entity/Db/AgentEchelon.php
index 9575e0a41..8c25981b4 100644
--- a/module/Application/src/Application/Entity/Db/AgentEchelon.php
+++ b/module/Application/src/Application/Entity/Db/AgentEchelon.php
@@ -15,77 +15,27 @@ use Structure\Entity\Db\Structure;
  * Données synchronisées depuis Octopus :
  * - pas de setter sur les données ainsi remontées
  */
-class AgentEchelon {
+class AgentEchelon implements HasPeriodeInterface {
     use DbImportableAwareTrait;
+    use HasPeriodeTrait;
 
-    /** @var int */
-    private $id;
-    /** @var Agent */
-    private $agent;
-    /** @var int */
-    private  $echelon;
-    /** @var DateTime */
-    private $date;
+    private ?int $id = -1;
+    private ?Agent $agent = null;
+    private ?int $echelon = null;
 
-    /**
-     * @return int|null
-     */
     public function getId() : ?int
     {
         return $this->id;
     }
 
-    /**
-     * @return Agent|null
-     */
     public function getAgent(): ?Agent
     {
         return $this->agent;
     }
 
-    /**
-     * @param Agent $agent
-     * @return AgentEchelon
-     */
-    public function setAgent(Agent $agent): AgentEchelon
-    {
-        $this->agent = $agent;
-        return $this;
-    }
-
-    /**
-     * @return int|null
-     */
     public function getEchelon(): ?int
     {
         return $this->echelon;
     }
 
-    /**
-     * @param int $echelon
-     * @return AgentEchelon
-     */
-    public function setEchelon(int $echelon): AgentEchelon
-    {
-        $this->echelon = $echelon;
-        return $this;
-    }
-
-    /**
-     * @return DateTime|null
-     */
-    public function getDate(): ?DateTime
-    {
-        return $this->date;
-    }
-
-    /**
-     * @param DateTime $date
-     * @return AgentEchelon
-     */
-    public function setDate(DateTime $date): AgentEchelon
-    {
-        $this->date = $date;
-        return $this;
-    }
 }
\ No newline at end of file
diff --git a/module/Application/src/Application/Entity/Db/MacroContent/AgentMacroTrait.php b/module/Application/src/Application/Entity/Db/MacroContent/AgentMacroTrait.php
index ff6de5954..d94c5d854 100644
--- a/module/Application/src/Application/Entity/Db/MacroContent/AgentMacroTrait.php
+++ b/module/Application/src/Application/Entity/Db/MacroContent/AgentMacroTrait.php
@@ -343,7 +343,7 @@ trait AgentMacroTrait
         $echelon = $agent->getEchelonActif();
 
         if ($echelon) {
-            return $echelon->getDate()->format('d/m/Y');
+            return $echelon->getDateDebut()->format('d/m/Y');
         }
         return "";
     }
diff --git a/module/Application/src/Application/Entity/Db/Mapping/Application.Entity.Db.AgentEchelon.dcm.xml b/module/Application/src/Application/Entity/Db/Mapping/Application.Entity.Db.AgentEchelon.dcm.xml
index abff70457..e73893664 100644
--- a/module/Application/src/Application/Entity/Db/Mapping/Application.Entity.Db.AgentEchelon.dcm.xml
+++ b/module/Application/src/Application/Entity/Db/Mapping/Application.Entity.Db.AgentEchelon.dcm.xml
@@ -8,8 +8,9 @@
             <join-column name="agent_id" referenced-column-name="c_individu"/>
         </many-to-one>
 
-        <field name="date"          type="datetime"                 column="d_debut"        nullable="false"/>
         <field name="echelon"       type="integer"                  column="echelon"             nullable="false"/>
+        <field name="dateDebut"     type="datetime"                 column="d_debut"        nullable="false"/>
+        <field name="dateFin"       type="datetime"                 column="d_fin"        nullable="true"/>
 
         <!-- DB IMPORT #############################  -->
         <field name="created_on"               column="created_on"       type="datetime"/>
diff --git a/module/Application/src/Application/Entity/Db/Traits/HasPeriodeTrait.php b/module/Application/src/Application/Entity/Db/Traits/HasPeriodeTrait.php
index 8e0f3b414..a87bed5b4 100644
--- a/module/Application/src/Application/Entity/Db/Traits/HasPeriodeTrait.php
+++ b/module/Application/src/Application/Entity/Db/Traits/HasPeriodeTrait.php
@@ -7,45 +7,27 @@ use Doctrine\ORM\QueryBuilder;
 
 trait HasPeriodeTrait {
 
-    /** @var DateTime|null */
-    private $dateDebut;
-    /** @var DateTime|null */
-    private $dateFin;
+    private ?DateTime $dateDebut = null;
+    private ?DateTime $dateFin = null;
 
-    /**
-     * @return DateTime|null
-     */
     public function getDateDebut() : ?DateTime
     {
         return $this->dateDebut;
     }
 
-    /**
-     * @param DateTime|null $date
-     * @return self
-     */
-    public function setDateDebut(?DateTime $date) : self
+    public function setDateDebut(?DateTime $date) : void
     {
         $this->dateDebut = $date;
-        return $this;
     }
 
-    /**
-     * @return DateTime|null
-     */
     public function getDateFin() : ?DateTime
     {
         return $this->dateFin;
     }
 
-    /**
-     * @param DateTime|null $date
-     * @return self
-     */
-    public function setDateFin(?DateTime $date) : self
+    public function setDateFin(?DateTime $date) : void
     {
         $this->dateFin = $date;
-        return $this;
     }
 
     /**
diff --git a/module/Application/src/Application/View/Helper/partial/agent-grade.phtml b/module/Application/src/Application/View/Helper/partial/agent-grade.phtml
index 976d187dd..acab514bf 100644
--- a/module/Application/src/Application/View/Helper/partial/agent-grade.phtml
+++ b/module/Application/src/Application/View/Helper/partial/agent-grade.phtml
@@ -103,7 +103,7 @@ $displayCorrespondance = (isset($options['correspondance']) and $options['corres
             <?php if ($echelon and $echelon->getEchelon() !== 0) : ?>
                 <dt class="col-md-3"> Échelon</dt>
                 <dd class="col-md-9"> <?php echo $echelon->getEchelon(); ?> (date de passage
-                    : <?php echo $echelon->getDate()->format('d/m/Y'); ?>)
+                    : <?php echo $echelon->getDateDebut()->format('d/m/Y'); ?>)
                 </dd>
             <?php endif; ?>
         </dl>
diff --git a/module/Application/view/application/verification/index.phtml b/module/Application/view/application/verification/index.phtml
index a8cbbe2e4..c1cd3b03c 100644
--- a/module/Application/view/application/verification/index.phtml
+++ b/module/Application/view/application/verification/index.phtml
@@ -97,7 +97,7 @@ $this->headTitle("Vérification de l'installation");
     </div>
 
     <!-- PARAMETRE ---------------------------------------------------------------------------------------------------->
-    <?php $path = file_exists($installation_path . '/module/' . $module . '/src/' . $module . '/Provider/Parametre'); ?>
+    <?php $path = $installation_path . '/module/' . $module . '/src/' . $module . '/Provider/Parametre'; ?>
     <?php $exist =  (file_exists($installation_path . '/module/' . $module . '/src/' . $module . '/Provider/Parametre')); ?>
     <div class="card bg-default ">
         <div class="card-header">
-- 
GitLab