From 01bf24b901327aaf34e03359af689446c19e9284 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laurent=20L=C3=A9cluse?= <laurent.lecluse@unicaen.fr>
Date: Wed, 7 Jun 2023 12:14:52 +0200
Subject: [PATCH] =?UTF-8?q?Correction=20bug=20saisie=20r=C3=A9f=C3=A9renti?=
 =?UTF-8?q?el?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../src/Service/ServiceReferentielService.php | 105 +++++++-----------
 .../VolumeHoraireReferentielService.php       |  25 +++--
 2 files changed, 57 insertions(+), 73 deletions(-)

diff --git a/module/Referentiel/src/Service/ServiceReferentielService.php b/module/Referentiel/src/Service/ServiceReferentielService.php
index 4cea07e121..0309beffe7 100755
--- a/module/Referentiel/src/Service/ServiceReferentielService.php
+++ b/module/Referentiel/src/Service/ServiceReferentielService.php
@@ -71,8 +71,8 @@ class ServiceReferentielService extends AbstractEntityService
      * Permet de retourner des valeurs par défaut ou de les forcer en cas de besoin
      * Format de sortie : array( $qb, $alias ).
      *
-     * @param QueryBuilder|null $qb    Générateur de requêtes
-     * @param string|null       $alias Alias d'entité
+     * @param QueryBuilder|null $qb Générateur de requêtes
+     * @param string|null $alias Alias d'entité
      *
      * @return array
      */
@@ -119,8 +119,8 @@ class ServiceReferentielService extends AbstractEntityService
     /**
      *
      * @param TypeVolumeHoraire $typeVolumeHoraire
-     * @param QueryBuilder      $qb
-     * @param string            $alias
+     * @param QueryBuilder $qb
+     * @param string $alias
      *
      * @return QueryBuilder
      */
@@ -140,20 +140,20 @@ class ServiceReferentielService extends AbstractEntityService
     /**
      * Retourne un service unique selon ses critères précis
      *
-     * @param Intervenant         $intervenant
+     * @param Intervenant $intervenant
      * @param FonctionReferentiel $fonction
-     * @param Structure           $structure
-     * @param string              $commentaires
+     * @param Structure $structure
+     * @param string $commentaires
      *
      * @return null|ServiceReferentiel
      */
     public function getBy(
-        Intervenant $intervenant,
+        Intervenant         $intervenant,
         FonctionReferentiel $fonction,
-        Structure $structure,
-        ?Tag $tag,
-        ?MotifNonPaiement $motifNonPaiement,
-        $commentaires = null
+        Structure           $structure,
+        ?Tag                $tag,
+        ?MotifNonPaiement   $motifNonPaiement,
+                            $commentaires = null
     )
     {
         $result = $this->getRepo()->findBy([
@@ -186,7 +186,7 @@ class ServiceReferentielService extends AbstractEntityService
     /**
      *
      * @param QueryBuilder|null $qb
-     * @param string|null       $alias
+     * @param string|null $alias
      */
     public function orderBy(QueryBuilder $qb = null, $alias = null)
     {
@@ -205,7 +205,7 @@ class ServiceReferentielService extends AbstractEntityService
     /**
      *
      * @param ServiceReferentiel[] $servicesReferentiels
-     * @param TypeVolumeHoraire    $typeVolumeHoraire
+     * @param TypeVolumeHoraire $typeVolumeHoraire
      */
     public function setTypeVolumeHoraire($servicesReferentiels, TypeVolumeHoraire $typeVolumeHoraire)
     {
@@ -224,11 +224,14 @@ class ServiceReferentielService extends AbstractEntityService
     public function newEntity()
     {
         $entity = parent::newEntity();
-        $role   = $this->getServiceContext()->getSelectedIdentityRole();
+        $role = $this->getServiceContext()->getSelectedIdentityRole();
         if ($intervenant = $role->getIntervenant()) {
             $entity->setIntervenant($intervenant);
         }
 
+        $entity->setSource($this->getServiceSource()->getOse());
+        $entity->setSourceCode(uniqid('ose-'));
+
         return $entity;
     }
 
@@ -244,17 +247,16 @@ class ServiceReferentielService extends AbstractEntityService
     public function save($entity)
     {
         $role = $this->getServiceContext()->getSelectedIdentityRole();
-        $this->getEntityManager()->beginTransaction();
-        try {
-            if (!$entity->getIntervenant() && $intervenant = $role->getIntervenant()) {
-                $entity->setIntervenant($intervenant);
-            }
-            if (!$this->getAuthorize()->isAllowed($entity, $entity->getTypeVolumeHoraire()->getPrivilegeReferentielEdition())) {
-                throw new \BjyAuthorize\Exception\UnAuthorizedException('Saisie interdite');
-            }
 
-            $serviceAllreadyExists = null;
+        if (!$entity->getIntervenant() && $intervenant = $role->getIntervenant()) {
+            $entity->setIntervenant($intervenant);
+        }
+        if (!$this->getAuthorize()->isAllowed($entity, $entity->getTypeVolumeHoraire()->getPrivilegeReferentielEdition())) {
+            throw new \BjyAuthorize\Exception\UnAuthorizedException('Saisie interdite');
+        }
 
+        $serviceAllreadyExists = null;
+        if (!$entity->getId()) { // uniquement pour les nouveaux services!!
             $serviceAllreadyExists = $this->getBy(
                 $entity->getIntervenant(),
                 $entity->getFonctionReferentiel(),
@@ -263,57 +265,36 @@ class ServiceReferentielService extends AbstractEntityService
                 $entity->getMotifNonPaiement(),
                 $entity->getCommentaires()
             );
+        }
 
-            //@TODO probleme de supression
-            //On regarde dans le cas d'un modification d'un service reférentiel
-
-
+        // Enregistrement en BDD
+        $this->getEntityManager()->beginTransaction();
+        try {
             if ($serviceAllreadyExists) {
-                $result = $serviceAllreadyExists;
-
-                if ($result->getId() != $entity->getId()) {
-                    //on remove l'ancien service puisque les volumes horaires vont être accrochés sur un service déjà existant
-                    parent::delete($entity);
-                } else {
-                    $result = parent::save($entity);
-                }
-            } else {
-                $sourceOse = $this->getServiceSource()->getOse();
-                if (!$entity->getSource()) {
-                    $entity->setSource($sourceOse);
-                }
-                if (!$entity->getSourceCode()) {
-                    $entity->setSourceCode(uniqid('ose-'));
-                }
+                // on déplace les nouveaux volumes horaires sur l'ancien
                 foreach ($entity->getVolumeHoraireReferentiel() as $vhr) {
-                    if (!$vhr->getSource()) {
-                        $vhr->setSource($sourceOse);
-                    }
-                    if (!$vhr->getSourceCode()) {
-                        $vhr->setSourceCode(uniqid('ose-'));
-                    }
+                    $vhr->setServiceReferentiel($serviceAllreadyExists);
+                    $this->getEntityManager()->persist($vhr);
                 }
-                $result = parent::save($entity);
+                // l'ancien remplace le nouveau
+                $entity = $serviceAllreadyExists;
             }
 
-            /* Sauvegarde automatique des volumes horaires associés */
-            $serviceVolumeHoraire = $this->getServiceVolumeHoraireReferentiel();
-
+            $entity = parent::save($entity);
             foreach ($entity->getVolumeHoraireReferentiel() as $volumeHoraire) {
-                if ($result !== $entity) $volumeHoraire->setServiceReferentiel($result);
                 if ($volumeHoraire->getRemove()) {
-                    $serviceVolumeHoraire->delete($volumeHoraire);
+                    $this->getServiceVolumeHoraireReferentiel()->delete($volumeHoraire);
                 } else {
-                    $serviceVolumeHoraire->save($volumeHoraire);
+                    $this->getServiceVolumeHoraireReferentiel()->save($volumeHoraire);
                 }
             }
             $this->getEntityManager()->commit();
-        } catch (Exception $e) {
+        } catch (\Exception $e) {
             $this->getEntityManager()->rollBack();
             throw $e;
         }
 
-        return $result;
+        return $entity;
     }
 
 
@@ -322,7 +303,7 @@ class ServiceReferentielService extends AbstractEntityService
      * Supprime (historise par défaut) le service spécifié.
      *
      * @param ServiceReferentiel $entity Entité à détruire
-     * @param bool               $softDelete
+     * @param bool $softDelete
      *
      * @return self
      */
@@ -330,7 +311,7 @@ class ServiceReferentielService extends AbstractEntityService
     {
         if ($softDelete) {
             $vhListe = $entity->getVolumeHoraireReferentielListe();
-            $listes  = $vhListe->getSousListes([$vhListe::FILTRE_HORAIRE_DEBUT, $vhListe::FILTRE_HORAIRE_FIN]);
+            $listes = $vhListe->getSousListes([$vhListe::FILTRE_HORAIRE_DEBUT, $vhListe::FILTRE_HORAIRE_FIN]);
             foreach ($listes as $liste) {
                 $liste->setHeures(0);
             }
@@ -399,7 +380,7 @@ class ServiceReferentielService extends AbstractEntityService
 
     public function getPrevusFromPrevusData(Intervenant $intervenant)
     {
-        $tvhPrevu  = $this->getServiceTypeVolumeHoraire()->getPrevu();
+        $tvhPrevu = $this->getServiceTypeVolumeHoraire()->getPrevu();
         $tvhSource = $this->getServiceTypeVolumeHoraire()->getByCode($this->getServiceParametres()->get('report_service'));
         $evhValide = $this->getServiceEtatVolumeHoraire()->getSaisi();
 
diff --git a/module/Referentiel/src/Service/VolumeHoraireReferentielService.php b/module/Referentiel/src/Service/VolumeHoraireReferentielService.php
index 9404d47d11..17fd8114fa 100755
--- a/module/Referentiel/src/Service/VolumeHoraireReferentielService.php
+++ b/module/Referentiel/src/Service/VolumeHoraireReferentielService.php
@@ -58,11 +58,24 @@ class VolumeHoraireReferentielService extends AbstractEntityService
         $entity = parent::newEntity();
         $entity->setTypeVolumeHoraire($this->getServiceTypeVolumeHoraire()->getPrevu());
 
+        $entity->setSource($this->getServiceSource()->getOse());
+        $entity->setSourceCode(uniqid('ose-'));
+
         return $entity;
     }
 
 
 
+    public function populateAutoValidation(VolumeHoraireReferentiel $entity)
+    {
+        $typeVolumeHoraire = $entity->getTypeVolumeHoraire();
+        $canAutoValidate   = $this->getAuthorize()->isAllowed($entity, $typeVolumeHoraire->getPrivilegeReferentielAutoValidation());
+
+        if ($canAutoValidate) $entity->setAutoValidation(true);
+    }
+
+
+
     /**
      * Sauvegarde une entité
      *
@@ -73,17 +86,7 @@ class VolumeHoraireReferentielService extends AbstractEntityService
      */
     public function save($entity)
     {
-        if (!$entity->getSource()) {
-            $entity->setSource($this->getServiceSource()->getOse());
-        }
-        if (!$entity->getSourceCode()) {
-            $entity->setSourceCode(uniqid('ose-'));
-        }
-
-        $typeVolumeHoraire = $entity->getTypeVolumeHoraire();
-        $canAutoValidate   = $this->getAuthorize()->isAllowed($entity, $typeVolumeHoraire->getPrivilegeReferentielAutoValidation());
-
-        if ($canAutoValidate) $entity->setAutoValidation(true);
+        $this->populateAutoValidation($entity);
 
         return parent::save($entity);
     }
-- 
GitLab