Commit adca01bd authored by Antony Le Courtes's avatar Antony Le Courtes
Browse files

Refactoring pour moins de requête update

parent cac2c76d
Loading
Loading
Loading
Loading
+13 −25
Changes for admin/actions/maj-code-intervenant-octopus.php: 13 added lines, 25 removed lines.
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ $bdd = $this->getBdd();
$console = $this->getConsole();
$console->printMainTitle("Maj des codes intervenants de harpège vers Octopus", $console::BG_BLUE);
$console->println("Récupération source id 'Octopus'", $console::COLOR_GREEN);
$notFound = [];
//On récupére la source octopus
$sqlSource = "SELECT id FROM source WHERE code = 'Octopus'";
$result    = $bdd->select($sqlSource, [], ['fetch' => $bdd::FETCH_ONE]);
@@ -29,33 +30,19 @@ if ($result) {
}

//On récupére tous les intervenants de OSE provenant de la source Harpege
$sql = "SELECT 
           id       intervenant_id,
           code     code_harpege,
           source_id   source_harpege,
           annee_id
$sql = "SELECT DISTINCT
           code     code_harpege
       FROM intervenant 
       WHERE source_id =" . $sourceIdHarpege;

$resultIntervenantOse = $bdd->select($sql);

//On récupére de octopus le mapping des codes harpeges vs code octopus
$sql = " SELECT DISTINCT
   			FIRST_VALUE(c_individu_chaine) OVER (PARTITION BY c_individu_chaine ORDER BY ordre_source) code_octopus,
			FIRST_VALUE(c_source) OVER (PARTITION BY c_individu_chaine ORDER BY ordre_source) c_source,
			FIRST_VALUE(c_src_individu) OVER (PARTITION BY c_individu_chaine ORDER BY ordre_source) code_harpege FROM (
				SELECT DISTINCT
			   		u.c_individu_chaine,
			   		u.c_source,
			   		u.c_src_individu,
			   		CASE WHEN u.c_source = 'SIHAM' THEN 1
			            WHEN u.c_source = 'HARP'  THEN 2
			   		    WHEN u.c_source = 'OCTO'  THEN 3
			    	    WHEN u.c_source = 'APO'   THEN 4
			       END                  ordre_source
				FROM individu_unique u
	 		)
";
$sql = "SELECT
            c_individu_chaine        code_octopus,
            c_src_individu           code_harpege            
        FROM individu_unique
        WHERE c_source = 'HARP'";

$resultIntervenantOctopus  = $bdd->select($sql);
$mappingCodeOctopusHarpege = [];
@@ -71,14 +58,16 @@ $totalIntervenant = count($resultIntervenantOse);
//On commence la migration des codes intervenants et on change la source en octopus
$bdd->trigger()->disable('INTERVENANT_CK');
foreach ($resultIntervenantOse as $intervenantOse) {

    $i++;
    $pourcent = round(($i * 100) / $totalIntervenant);
    if (array_key_exists($intervenantOse['CODE_HARPEGE'], $mappingCodeOctopusHarpege)) {

        $console->msg($pourcent . " % Migration code intervenant  / code harpege : " . $intervenantOse['CODE_HARPEGE'] . " vers code octopus " . $mappingCodeOctopusHarpege[$intervenantOse['CODE_HARPEGE']], true);
        $sql = "UPDATE intervenant SET code = '" . $mappingCodeOctopusHarpege[$intervenantOse['CODE_HARPEGE']] . "', source_id =" . $sourceIdOctopus . " WHERE source_id=" . $sourceIdHarpege . " AND code = '" . $intervenantOse['CODE_HARPEGE'] . "'";
        $bdd->exec($sql);
    } else {
        $notFound [$intervenantOse['ANNEE_ID']][] = $intervenantOse['CODE_HARPEGE'];
        $notFound[] = $intervenantOse['CODE_HARPEGE'];
    }
}
$bdd->trigger()->enable('INTERVENANT_CK');
@@ -87,10 +76,9 @@ ksort($notFound);
$console->end("Début migration des codes intervenant OSE de Harpège vers Octopus");
$console->println("==================================");
$console->println("Intervenants non trouvés dans Octopus", $console::BG_RED);
foreach ($notFound as $annee => $intervenants) {
    $console->println("Année " . $annee . " : " . implode(',', $intervenants));
$console->println(implode(',', $notFound));
$console->println("==================================");
}


//Calcul des tableaux pour mettre à jour suite changement des codes intervenants
$oa->exec('calcul-tableaux-bord');