Commit 88ec7ef5 authored by Antony Le Courtes's avatar Antony Le Courtes
Browse files

Correction V_TBL_AGREMENT.sql / Ajout de structure_id sur agrement conseil...

Correction V_TBL_AGREMENT.sql / Ajout de structure_id sur agrement conseil restreint. / Script de migration pour remettre les structure_id manquant sur les agrements
parent 1ff0a0e6
Loading
Loading
Loading
Loading
+182
Changes for admin/migration/AgrementStructure.php: 182 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php





class AgrementStructure extends AbstractMigration
{
    protected $contexte = self::CONTEXTE_ALL;



    public function description(): string
    {
        return "Correction des structures manquantes sur les agréments";
    }



    public function utile(): bool
    {
        return true;
    }



    public function action(string $contexte)
    {
        if ($contexte == self::CONTEXTE_PRE) {
            $this->before();
        } else {
            $this->after();
        }
    }



    protected function before()
    {
        $this->manager->sauvegarderTable('AGREMENT', 'SAVE_AGREMENT');
    }



    protected function after()
    {
        $bdd     = $this->manager->getBdd();
        $console = $this->manager->getOseAdmin()->getConsole();
        $console->println("Récupération des agréments conseil restreint sans structure");
        //La vue TBL_AGREMENT a changé, donc il faut d'abord recharger la vue agrement pour le traitement
        $console->println("------------------------------------------------------------------");
        $console->println("Nouveau calcul du tableau de bord agrement");
        $bdd->exec('BEGIN unicaen_tbl.calculer(\'agrement\'); END;');

        //On récupère les agrements de type conseil restreint sans structure
        $agrementsToRecreate = [];
        $sql                 = 'SELECT 
                    a.id               agrement_id,
                    i.id               intervenant_id,
                    i.annee_id         annee_id,
                    i.nom_usuel        nom_usuel,
                    i.prenom           prenom,
                    i.code             code_intervenant,
                    a.type_agrement_id type_agrement_id
                FROM AGREMENT a
                JOIN INTERVENANT i ON i.id = a.intervenant_id
                WHERE
                a.type_agrement_id = 1
                AND a.structure_id IS NULL
                AND a.histo_destruction IS NULL
                AND i.annee_id = 2019';

        $agrements = $bdd->select($sql);
        //Nombre d'agréments concernés
        $nbAgrement = count($agrements);
        $console->println("Il y a $nbAgrement a corrigé");
        $nbAgrementCorrige = 0;
        //On commence le traitement de chaque agrément qui pose problème
        foreach ($agrements as $agrement) {
            try {
                $structureId    = false;
                $nomIntervenant = $agrement['NOM_USUEL'];
                $intervenantId  = $agrement['INTERVENANT_ID'];
                $agrementId     = $agrement['AGREMENT_ID'];
                $console->println("------------------------------------------------------------------");
                $console->println("Traitement de l'agrement de $nomIntervenant / Agrement_id : $agrementId");
                $console->println("Recherche d'un structure id dans la table TBL_AGREMENT");
                //On recherche d'abord dans la table TBL_AGREMENT pour trouver la structure possible
                $sqlAgrement = "SELECT
                                     ta.structure_id   structure_id,
                                     ta.intervenant_id intervenant_id
                                 FROM 
                                    TBL_AGREMENT ta
                                 WHERE
                                 ta.intervenant_id = $intervenantId
                                 AND ta.type_agrement_id = 1
                                 AND ta.agrement_id IS NULL";

                $agrements = $bdd->select($sqlAgrement);

                $nbStructuresPossibles = count($agrements);
                //Cas où une seule structure est trouvée dans la table TBL_AGREMENT
                if ($nbStructuresPossibles == 1) {
                    $agrement = current($agrements);
                    //Je set la variable $structureId pour l'update
                    $structureId = $agrement['STRUCTURE_ID'];
                    $console->println("Une seule structure trouvée pour cet intervenant / Structure_id : " . $structureId);
                } else {
                    //Cas où j'ai plusieurs structures possibles.
                    $console->println("Plusieurs structure_id possible pour cet agrément : $nbStructuresPossibles");
                    $console->println("Recherche des structures possibles via les contrats");
                    //Il faut donc remonter au niveau des contrats pour pouvoir essayer de trouver le bon structure_id de l'agrement
                    $sqlContrats = "SELECT 
                                        c.structure_id   structure_id,
                                        c.intervenant_id intervenant_id   
                                    FROM
                                        CONTRAT c 
                                    WHERE 
                                        c.intervenant_id = $intervenantId
                                        AND c.type_contrat_id = 1
                                        AND c.histo_destruction IS NULL";

                    $contrats = $bdd->select($sqlContrats);

                    $nbContrats = count($contrats);
                    //Cas où j'ai un contrat unique valide, je peux donc isoler la structure_id pour la mettre à jour dans l'agrément
                    if ($nbContrats == 1) {
                        $contrat = current($contrats);
                        //Je set la variable $structureId pour l'update
                        $structureId = $contrat['STRUCTURE_ID'];
                        $console->println("Un contrat unique trouvé et validé avec la structure id : " . $structureId);
                    }
                }
                //Si j'ai réussi à identifier une structure unique alors je peux mettre à jour la table agrement
                if ($structureId !== false) {
                    $console->println("Mise à jour de l'agrement id : $agrementId avec le structure id : $structureId", $console::COLOR_LIGHT_GREEN);
                    $sqlUpdate = "UPDATE AGREMENT SET STRUCTURE_ID = $structureId WHERE ID = $agrementId";
                    $nbAgrementCorrige++;
                    $bdd->exec($sqlUpdate);
                } else {
                    //Sinon je fais un soft delete de l'agrement
                    //on met une date précise pour retrouver facilement
                    $console->println("Suppression de l'agrément id : $agrementId car aucune structure n'a pu être identifié", $console::COLOR_LIGHT_RED);
                    $agrementsToRecreate [] = $agrement;
                    $sqlDelete              = "UPDATE 
                                AGREMENT
                              SET 
                                HISTO_DESTRUCTION = TO_DATE('2020 - 01 - 01', 'YYYY - MM - DD'),
                                HISTO_DESTRUCTEUR_ID = HISTO_CREATEUR_ID 
                              WHERE ID = $agrementId";
                    $bdd->exec($sqlDelete);
                }
            } catch (Exception $e) {
                $console->println($e->getMessage());
            }
        }

        if (!empty($agrementsToRecreate)) {
            $console->println("Seulement $nbAgrementCorrige sur $nbAgrement on été corrigé", $console::COLOR_LIGHT_RED);
            $console->println("Liste des agréments restreints qu'il faut refaire manuellement : ", $console::COLOR_LIGHT_RED);
            foreach ($agrementsToRecreate as $value) {
                $console->println($value['PRENOM'] . " " . $value['NOM_USUEL'] . " (id : " . $value['INTERVENANT_ID'] . " / code : " . $value['CODE_INTERVENANT'] . ")", $console::COLOR_LIGHT_RED);
            }
        } else {
            $console->println("Les $nbAgrement agrements restreints sans structure on été corrigé.", $console::COLOR_LIGHT_GREEN);
        }

        //On recalcule les tableaux de bord agrement et workflow pour tout remettre d'équerre
        $console->println("------------------------------------------------------------------");
        $console->println("Nouveau calcul du tableau de bord agrement");
        $bdd->exec('BEGIN unicaen_tbl.calculer(\'agrement\'); END;');

        $console->println("Nouveau calcul du tableau de bord workflow");
        $bdd->exec('BEGIN unicaen_tbl.calculer(\'workflow\'); END;');

        //On supprimme la table de sauvegarde temporaire.
        //$this->manager->supprimerSauvegarde('SAVE_AGREMENT');

        $console->println("Terminé");
    }

}
 No newline at end of file
+24 −38
Changes for data/ddl/package/UNICAEN_TBL/body.sql: 24 added lines, 38 removed lines.
Original line number Diff line number Diff line
@@ -323,7 +323,7 @@ CREATE OR REPLACE PACKAGE BODY "UNICAEN_TBL" AS
        (WITH i_s AS (
  SELECT
    fr.intervenant_id,
            ep.structure_id ep_structure_id
    ep.structure_id structure_id
  FROM
    formule_resultat fr
    JOIN type_volume_horaire  tvh ON tvh.code = ''PREVU'' AND tvh.id = fr.type_volume_horaire_id
@@ -337,21 +337,21 @@ CREATE OR REPLACE PACKAGE BODY "UNICAEN_TBL" AS
),
avi AS (
    SELECT
                i.code                code_intervenant,
        i.code                intervenant_code,
        i.annee_id            annee_id,
                a.type_agrement_id    type_agrement,
        a.type_agrement_id    type_agrement_id,
        a.id                  agrement_id,
                tas.duree_vie         duree_vie,
                i.annee_id+duree_vie date_validite
        a.structure_id        structure_id
    FROM intervenant i
            JOIN type_agrement_statut tas ON tas.statut_intervenant_id = i.statut_id
            JOIN agrement a ON a.intervenant_id = i.id AND tas.type_agrement_id = a.type_agrement_id AND a.histo_destruction IS NULL
    	JOIN agrement a ON a.intervenant_id = i.id
    WHERE
    	a.histo_destruction IS NULL
)
        SELECT DISTINCT "ANNEE_ID","ANNEE_AGREMENT","TYPE_AGREMENT_ID","INTERVENANT_ID","CODE_INTERVENANT","STRUCTURE_ID","OBLIGATOIRE","AGREMENT_ID","DUREE_VIE","RANK" FROM (
SELECT DISTINCT "ANNEE_ID","ANNEE_AGREMENT","TYPE_AGREMENT_ID","INTERVENANT_ID","CODE_INTERVENANT","STRUCTURE_ID","OBLIGATOIRE","AGREMENT_ID","DUREE_VIE" FROM (
    SELECT
      i.annee_id                     annee_id,
      CASE
                WHEN NVL(NVL(a.id, avi.agrement_id),0) = 0
        WHEN COALESCE (avi.agrement_id,0) = 0
        THEN NULL
        ELSE NVL(avi.annee_id, i.annee_id) END   annee_agrement,
      tas.type_agrement_id                       type_agrement_id,
@@ -359,12 +359,12 @@ CREATE OR REPLACE PACKAGE BODY "UNICAEN_TBL" AS
      i.code                                     code_intervenant,
      null                                       structure_id,
      tas.obligatoire                            obligatoire,
              NVL(a.id, avi.agrement_id)                 agrement_id,
      avi.agrement_id			                 agrement_id,
      tas.duree_vie                              duree_vie,
      RANK() OVER(
        PARTITION BY i.code,i.annee_id ORDER BY
        CASE
                WHEN NVL(NVL(a.id, avi.agrement_id),0) = 0
        WHEN COALESCE (avi.agrement_id,0) = 0
        THEN NULL
        ELSE NVL(avi.annee_id, i.annee_id) END DESC
      ) rank
@@ -374,20 +374,13 @@ CREATE OR REPLACE PACKAGE BODY "UNICAEN_TBL" AS
                                        AND tas.histo_destruction IS NULL

      JOIN intervenant                 i ON i.histo_destruction IS NULL
                                               -- AND (tas.premier_recrutement IS NULL OR NVL(i.premier_recrutement,0) = tas.premier_recrutement)
                                        AND i.statut_id = tas.statut_intervenant_id

      JOIN                           i_s ON i_s.intervenant_id = i.id


              LEFT JOIN agrement               a ON a.type_agrement_id = ta.id
                                                AND a.intervenant_id = i.id
                                                AND a.histo_destruction IS NULL

              LEFT JOIN                      avi ON i.code = avi.code_intervenant
                                                AND tas.type_agrement_id = avi.type_agrement
                                                AND i.annee_id < avi.date_validite
                                                AND i.annee_id >= avi.annee_id
      JOIN                      avi ON i.code = avi.intervenant_code
      									AND avi.type_agrement_id = tas.type_agrement_id
                                        AND i.annee_id < avi.annee_id + tas.duree_vie

    WHERE
      ta.code = ''CONSEIL_ACADEMIQUE'')
@@ -395,24 +388,24 @@ CREATE OR REPLACE PACKAGE BODY "UNICAEN_TBL" AS
  rank = 1

UNION ALL
        SELECT DISTINCT "ANNEE_ID","ANNEE_AGREMENT","TYPE_AGREMENT_ID","INTERVENANT_ID","CODE_INTERVENANT","STRUCTURE_ID","OBLIGATOIRE","AGREMENT_ID","DUREE_VIE","RANK" FROM (
SELECT DISTINCT "ANNEE_ID","ANNEE_AGREMENT","TYPE_AGREMENT_ID","INTERVENANT_ID","CODE_INTERVENANT","STRUCTURE_ID","OBLIGATOIRE","AGREMENT_ID","DUREE_VIE" FROM (
    SELECT
      i.annee_id                                  annee_id,
      CASE
                WHEN NVL(NVL(a.id, avi.agrement_id),0) = 0
        WHEN COALESCE (avi.agrement_id,0) = 0
        THEN NULL
        ELSE NVL(avi.annee_id, i.annee_id) END    annee_agrement,
      tas.type_agrement_id                        type_agrement_id,
      i.id                                        intervenant_id,
      i.code                                      code_intervenant,
              a.structure_id                            structure_id,
      i_s.structure_id							  structure_id,
      tas.obligatoire                             obligatoire,
              NVL(a.id, avi.agrement_id)                  agrement_id,
      avi.agrement_id 			                  agrement_id,
      tas.duree_vie                               duree_vie,
      RANK() OVER(
                PARTITION BY i.code,i.annee_id,i_s.ep_structure_id ORDER BY
        PARTITION BY i.code,i.annee_id,i_s.structure_id ORDER BY
        CASE
                WHEN NVL(NVL(a.id, avi.agrement_id),0) = 0
        WHEN COALESCE (avi.agrement_id,0) = 0
        THEN NULL
        ELSE NVL(avi.annee_id, i.annee_id) END DESC
      ) rank
@@ -426,18 +419,11 @@ CREATE OR REPLACE PACKAGE BODY "UNICAEN_TBL" AS

      JOIN                           i_s ON i_s.intervenant_id = i.id

              LEFT JOIN agrement               a ON a.type_agrement_id = ta.id
                                                AND a.structure_id = i_s.ep_structure_id
                                                AND a.intervenant_id = i.id
                                                AND a.histo_destruction IS NULL

              LEFT JOIN                      avi ON i.code = avi.code_intervenant
                                                AND tas.type_agrement_id = avi.type_agrement
                                                AND a.id = avi.agrement_id
                                                AND i.annee_id < avi.date_validite
      LEFT JOIN                      avi ON i.code = avi.intervenant_code
        								AND avi.type_agrement_id = tas.type_agrement_id
										AND COALESCE(avi.structure_id,0) = COALESCE(i_s.structure_id,0)
                                        AND i.annee_id < avi.annee_id + tas.duree_vie
                                        AND i.annee_id >= avi.annee_id


    WHERE
      ta.code = ''CONSEIL_RESTREINT'')
WHERE
+24 −36
Changes for data/ddl/view/V_TBL_AGREMENT.sql: 24 added lines, 36 removed lines.
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@ CREATE OR REPLACE FORCE VIEW V_TBL_AGREMENT AS
WITH i_s AS (
  SELECT
    fr.intervenant_id,
    ep.structure_id ep_structure_id
    ep.structure_id structure_id
  FROM
    formule_resultat fr
    JOIN type_volume_horaire  tvh ON tvh.code = 'PREVU' AND tvh.id = fr.type_volume_horaire_id
@@ -16,21 +16,21 @@ WITH i_s AS (
),
avi AS (
    SELECT
        i.code                code_intervenant,
        i.code                intervenant_code,
        i.annee_id            annee_id,
        a.type_agrement_id    type_agrement,
        a.type_agrement_id    type_agrement_id,
        a.id                  agrement_id,
        tas.duree_vie         duree_vie,
        i.annee_id+duree_vie date_validite
        a.structure_id        structure_id
    FROM intervenant i
    JOIN type_agrement_statut tas ON tas.statut_intervenant_id = i.statut_id
    JOIN agrement a ON a.intervenant_id = i.id AND tas.type_agrement_id = a.type_agrement_id AND a.histo_destruction IS NULL
    	JOIN agrement a ON a.intervenant_id = i.id
    WHERE
    	a.histo_destruction IS NULL
)
SELECT DISTINCT "ANNEE_ID","ANNEE_AGREMENT","TYPE_AGREMENT_ID","INTERVENANT_ID","CODE_INTERVENANT","STRUCTURE_ID","OBLIGATOIRE","AGREMENT_ID","DUREE_VIE","RANK" FROM (
SELECT DISTINCT "ANNEE_ID","ANNEE_AGREMENT","TYPE_AGREMENT_ID","INTERVENANT_ID","CODE_INTERVENANT","STRUCTURE_ID","OBLIGATOIRE","AGREMENT_ID","DUREE_VIE" FROM (
    SELECT
      i.annee_id                     annee_id,
      CASE
        WHEN NVL(NVL(a.id, avi.agrement_id),0) = 0
        WHEN COALESCE (avi.agrement_id,0) = 0
        THEN NULL
        ELSE NVL(avi.annee_id, i.annee_id) END   annee_agrement,
      tas.type_agrement_id                       type_agrement_id,
@@ -38,12 +38,12 @@ SELECT DISTINCT "ANNEE_ID","ANNEE_AGREMENT","TYPE_AGREMENT_ID","INTERVENANT_ID",
      i.code                                     code_intervenant,
      null                                       structure_id,
      tas.obligatoire                            obligatoire,
      NVL(a.id, avi.agrement_id)                 agrement_id,
      avi.agrement_id			                 agrement_id,
      tas.duree_vie                              duree_vie,
      RANK() OVER(
        PARTITION BY i.code,i.annee_id ORDER BY
        CASE
        WHEN NVL(NVL(a.id, avi.agrement_id),0) = 0
        WHEN COALESCE (avi.agrement_id,0) = 0
        THEN NULL
        ELSE NVL(avi.annee_id, i.annee_id) END DESC
      ) rank
@@ -53,20 +53,13 @@ SELECT DISTINCT "ANNEE_ID","ANNEE_AGREMENT","TYPE_AGREMENT_ID","INTERVENANT_ID",
                                        AND tas.histo_destruction IS NULL

      JOIN intervenant                 i ON i.histo_destruction IS NULL
                                       -- AND (tas.premier_recrutement IS NULL OR NVL(i.premier_recrutement,0) = tas.premier_recrutement)
                                        AND i.statut_id = tas.statut_intervenant_id

      JOIN                           i_s ON i_s.intervenant_id = i.id


      LEFT JOIN agrement               a ON a.type_agrement_id = ta.id
                                        AND a.intervenant_id = i.id
                                        AND a.histo_destruction IS NULL

      LEFT JOIN                      avi ON i.code = avi.code_intervenant
                                        AND tas.type_agrement_id = avi.type_agrement
                                        AND i.annee_id < avi.date_validite
                                        AND i.annee_id >= avi.annee_id
      JOIN                      avi ON i.code = avi.intervenant_code
      									AND avi.type_agrement_id = tas.type_agrement_id
                                        AND i.annee_id < avi.annee_id + tas.duree_vie

    WHERE
      ta.code = 'CONSEIL_ACADEMIQUE')
@@ -74,24 +67,24 @@ WHERE
  rank = 1

UNION ALL
SELECT DISTINCT "ANNEE_ID","ANNEE_AGREMENT","TYPE_AGREMENT_ID","INTERVENANT_ID","CODE_INTERVENANT","STRUCTURE_ID","OBLIGATOIRE","AGREMENT_ID","DUREE_VIE","RANK" FROM (
SELECT DISTINCT "ANNEE_ID","ANNEE_AGREMENT","TYPE_AGREMENT_ID","INTERVENANT_ID","CODE_INTERVENANT","STRUCTURE_ID","OBLIGATOIRE","AGREMENT_ID","DUREE_VIE" FROM (
    SELECT
      i.annee_id                                  annee_id,
      CASE
        WHEN NVL(NVL(a.id, avi.agrement_id),0) = 0
        WHEN COALESCE (avi.agrement_id,0) = 0
        THEN NULL
        ELSE NVL(avi.annee_id, i.annee_id) END    annee_agrement,
      tas.type_agrement_id                        type_agrement_id,
      i.id                                        intervenant_id,
      i.code                                      code_intervenant,
      a.structure_id                            structure_id,
      i_s.structure_id							  structure_id,
      tas.obligatoire                             obligatoire,
      NVL(a.id, avi.agrement_id)                  agrement_id,
      avi.agrement_id 			                  agrement_id,
      tas.duree_vie                               duree_vie,
      RANK() OVER(
        PARTITION BY i.code,i.annee_id,i_s.ep_structure_id ORDER BY
        PARTITION BY i.code,i.annee_id,i_s.structure_id ORDER BY
        CASE
        WHEN NVL(NVL(a.id, avi.agrement_id),0) = 0
        WHEN COALESCE (avi.agrement_id,0) = 0
        THEN NULL
        ELSE NVL(avi.annee_id, i.annee_id) END DESC
      ) rank
@@ -105,15 +98,10 @@ SELECT DISTINCT "ANNEE_ID","ANNEE_AGREMENT","TYPE_AGREMENT_ID","INTERVENANT_ID",

      JOIN                           i_s ON i_s.intervenant_id = i.id

      LEFT JOIN agrement               a ON a.type_agrement_id = ta.id
                                        AND a.structure_id = i_s.ep_structure_id
                                        AND a.intervenant_id = i.id
                                        AND a.histo_destruction IS NULL

      LEFT JOIN                      avi ON i.code = avi.code_intervenant
                                        AND tas.type_agrement_id = avi.type_agrement
                                        AND a.id = avi.agrement_id
                                        AND i.annee_id < avi.date_validite
      LEFT JOIN                           avi ON i.code = avi.intervenant_code
        								AND avi.type_agrement_id = tas.type_agrement_id
										AND COALESCE(avi.structure_id,0) = COALESCE(i_s.structure_id,0)
                                        AND i.annee_id < avi.annee_id + tas.duree_vie
                                        AND i.annee_id >= avi.annee_id