From 7f8caf03819eb26aadc09710df09e261d04516f7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laurent=20L=C3=A9cluse?= <laurent.lecluse@unicaen.fr>
Date: Mon, 25 Apr 2022 13:38:01 +0200
Subject: [PATCH] =?UTF-8?q?Correction=20du=20probl=C3=A8me=20de=20MAJ=20de?=
 =?UTF-8?q?s=20valeurs=20des=20plafonds?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../src/Controller/StatutController.php       |  9 +++------
 .../view/intervenant/statut/saisie.phtml      |  1 -
 module/Plafond/src/Entity/Db/Plafond.php      | 20 +++++++++++++++++++
 module/Plafond/src/Form/PlafondConfigForm.php |  7 ++++++-
 module/Plafond/src/Service/PlafondService.php |  5 ++++-
 5 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/module/Intervenant/src/Controller/StatutController.php b/module/Intervenant/src/Controller/StatutController.php
index ceb0db5666..083aab5e2d 100755
--- a/module/Intervenant/src/Controller/StatutController.php
+++ b/module/Intervenant/src/Controller/StatutController.php
@@ -72,17 +72,14 @@ class StatutController extends AbstractController
             $title = $statut->getLibelle();
         }
 
-        $plafonds = $this->getServicePlafond()->getPlafondsConfig($statut);
-
         $canEdit = $this->isAllowed($statut, Privileges::INTERVENANT_STATUT_EDITION);
         if ($canEdit) {
             $request = $this->getRequest();
-            $form->bindRequestSave($statut, $request, function (Statut $si) use ($plafonds, $request) {
+            $form->bindRequestSave($statut, $request, function (Statut $si) use ($request) {
                 $isNew = !$si->getId();
                 $this->getServiceStatut()->save($si);
-                $this->getFormPlafondConfig()->requestSaveConfigs($plafonds, $request);
+                $this->getFormPlafondConfig()->requestSaveConfigs($si, $request);
                 unset($this->getCacheContainer(RoleProvider::class)->statutsInfo);
-                $plafonds = $this->getServicePlafond()->getPlafondsConfig($si);
                 $this->flashMessenger()->addSuccessMessage('Enregistrement effectué');
                 if ($isNew) {
                     $this->redirect()->toRoute('statut/saisie', ['statut' => $si->getId()]);
@@ -95,7 +92,7 @@ class StatutController extends AbstractController
 
         $vm = new ViewModel();
         $vm->setTemplate('intervenant/statut/saisie');
-        $vm->setVariables(compact('typesIntervenants', 'canEdit', 'statut', 'statuts', 'form', 'title', 'plafonds'));
+        $vm->setVariables(compact('typesIntervenants', 'canEdit', 'statut', 'statuts', 'form', 'title'));
 
         return $vm;
     }
diff --git a/module/Intervenant/view/intervenant/statut/saisie.phtml b/module/Intervenant/view/intervenant/statut/saisie.phtml
index 89213f7aed..df67aef438 100755
--- a/module/Intervenant/view/intervenant/statut/saisie.phtml
+++ b/module/Intervenant/view/intervenant/statut/saisie.phtml
@@ -9,7 +9,6 @@ use Application\Provider\Privilege\Privileges;
  * @var $statuts           \Intervenant\Entity\Db\Statut[]
  * @var $statut            \Intervenant\Entity\Db\Statut
  * @var $form              \Intervenant\Form\StatutSaisieForm
- * @var $plafonds          \Plafond\Interfaces\PlafondConfigInterface[]
  * @var $title             string
  */
 
diff --git a/module/Plafond/src/Entity/Db/Plafond.php b/module/Plafond/src/Entity/Db/Plafond.php
index accf3ef64d..cd56c408df 100755
--- a/module/Plafond/src/Entity/Db/Plafond.php
+++ b/module/Plafond/src/Entity/Db/Plafond.php
@@ -4,6 +4,7 @@ namespace Plafond\Entity\Db;
 
 use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\Common\Collections\Collection;
+use Plafond\Interfaces\PlafondConfigInterface;
 
 /**
  * Plafond
@@ -154,6 +155,25 @@ class Plafond
 
 
 
+    public function addConfig(PlafondConfigInterface $plafondConfig): self
+    {
+        if ($plafondConfig->getPlafond() !== $this) {
+            throw new \Exception('Mauvais plafond transmis');
+        }
+
+        if ($plafondConfig instanceof PlafondStatut) {
+            $this->plafondStatut->add($plafondConfig);
+        } elseif ($plafondConfig instanceof PlafondStructure) {
+            $this->plafondStructure->add($plafondConfig);
+        } elseif ($plafondConfig instanceof PlafondReferentiel) {
+            $this->plafondReferentiel->add($plafondConfig);
+        }
+
+        return $this;
+    }
+
+
+
     /**
      * The __toString method allows a class to decide how it will react when it is converted to a string.
      *
diff --git a/module/Plafond/src/Form/PlafondConfigForm.php b/module/Plafond/src/Form/PlafondConfigForm.php
index 20df24fc21..e211adb442 100755
--- a/module/Plafond/src/Form/PlafondConfigForm.php
+++ b/module/Plafond/src/Form/PlafondConfigForm.php
@@ -2,7 +2,10 @@
 
 namespace Plafond\Form;
 
+use Application\Entity\Db\FonctionReferentiel;
+use Application\Entity\Db\Structure;
 use Application\Form\AbstractForm;
+use Intervenant\Entity\Db\Statut;
 use Laminas\Form\Element;
 use Laminas\Http\Request;
 use Plafond\Entity\Db\Plafond;
@@ -126,12 +129,14 @@ class PlafondConfigForm extends AbstractForm
      *
      * @return void
      */
-    public function requestSaveConfigs(array $plafondConfigs, Request $request)
+    public function requestSaveConfigs(Statut|Structure|FonctionReferentiel $entity, Request $request)
     {
         $heures      = $request->getPost('heures', []);
         $etatPrevu   = $request->getPost('plafondEtatPrevu', []);
         $etatRealise = $request->getPost('plafondEtatRealise', []);
 
+        $plafondConfigs = $this->getServicePlafond()->getPlafondsConfig($entity);
+
         foreach ($plafondConfigs as $plafondConfig) {
             if (isset($heures[$plafondConfig->getPlafond()->getId()])) {
                 $v = stringToFloat($heures[$plafondConfig->getPlafond()->getId()]);
diff --git a/module/Plafond/src/Service/PlafondService.php b/module/Plafond/src/Service/PlafondService.php
index 0df9cb206c..bf97a66db6 100755
--- a/module/Plafond/src/Service/PlafondService.php
+++ b/module/Plafond/src/Service/PlafondService.php
@@ -614,7 +614,7 @@ class PlafondService extends AbstractEntityService
         if ($entity instanceof FonctionReferentiel) {
             return PlafondReferentiel::class;
         }
-        throw new \Exception("L'entité fournie ne permet pas e récupérer une configuration de plafond");
+        throw new \Exception("L'entité fournie ne permet pas de récupérer une configuration de plafond");
     }
 
 
@@ -734,6 +734,9 @@ class PlafondService extends AbstractEntityService
             $plafondConfig->setEtatRealise($this->getEtat(PlafondEtat::DESACTIVE));
         }
 
+        if (!$plafondConfig->getId()) {
+            $plafondConfig->getPlafond()->addConfig($plafondConfig);
+        }
         $this->getEntityManager()->persist($plafondConfig);
         $this->getEntityManager()->flush($plafondConfig);
 
-- 
GitLab