<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>

	<NODE name="INIT" label="INITIALISATIONS ENVIRONNEMENT [FINESS] [FINESS_TEXTE]">
	</NODE>

	<NODE name="PAIE" label="RÉCUPÉRATION DE LA PAIE">
		<NODE label="Salariés">
			<sqlcmd><![CDATA[
			
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.p_profils
					', 'temp p_profils')
                ;

				INSERT INTO rh.p_profils (
					oid,
					type_contrat_id,
					type_contrat_code_original,
					type_contrat_code,
					type_contrat_texte,
					motif_debut_id,
					motif_debut_code_original,
					motif_debut_code,
					motif_debut_texte,
					motif_fin_id,
					motif_fin_code_original,
					motif_fin_code,
					motif_fin_texte,
					qualification_id,
					qualification_code_original,
					qualification_code,
					qualification_texte,
					service_id,
					service_code_original,
					service_code,
					service_texte,
					specialite_id,
					specialite_code_original,
					specialite_code,
					specialite_texte,
					type_temps_travail_id,
					type_temps_travail_code_original,
					type_temps_travail_code,
					type_temps_travail_texte,
					categorie_socio_professionnelle_id,
					categorie_socio_professionnelle_code_original,
					categorie_socio_professionnelle_code,
					categorie_socio_professionnelle_texte,
					statut_id,
					statut_code_original,
					statut_code,
					statut_texte,
					code_emploi_id,
					code_emploi_code_original,
					code_emploi_code,
					code_emploi_texte,
					societe_interim_id,
					societe_interim_code_original,
					societe_interim_code,
					societe_interim_texte,
					section_analytique_paie_id,
					section_analytique_paie_code_original,
					section_analytique_paie_code,
					section_analytique_paie_texte,
					section_analytique_id,
					section_analytique_code_original,
					section_analytique_code,
					section_analytique_texte,
					grille_id,
					grille_code_original,
					grille_code,
					grille_texte,
					grille_groupe_id,
					grille_groupe_code_original,
					grille_groupe_code,
					grille_groupe_texte,
					type_horaire_id,
					type_horaire_code_original,
					type_horaire_code,
					type_horaire_texte,
					groupe_cotisant_id,
					groupe_cotisant_code_original,
					groupe_cotisant_code,
					groupe_cotisant_texte,
					cadre_emploi_id,
					cadre_emploi_code_original,
					cadre_emploi_code,
					cadre_emploi_texte)
    				SELECT 
    					p_profils.oid + [IPX] AS oid,
    					COALESCE(c_types_contrat.to_oid,0) AS type_contrat_id,
    					COALESCE(c_types_contrat.to_oid::text,'') AS type_contrat_code_original,
    					COALESCE(c_types_contrat.to_code,'') AS type_contrat_code,
    					COALESCE(c_types_contrat.to_texte,'') AS type_contrat_texte,
    					COALESCE(c_motifs_debut_contrat.to_oid,0) AS motif_debut_id,
    					COALESCE(c_motifs_debut_contrat.to_oid::text,'') AS motif_debut_code_original,
    					COALESCE(c_motifs_debut_contrat.to_code,'') AS motif_debut_code,
    					COALESCE(c_motifs_debut_contrat.to_texte,'') AS motif_debut_texte,
    					COALESCE(c_motifs_fin_contrat.to_oid,0) AS motif_fin_id,
    					COALESCE(c_motifs_fin_contrat.to_oid::text,'') AS motif_fin_code_original,
    					COALESCE(c_motifs_fin_contrat.to_code,'') AS motif_fin_code,
    					COALESCE(c_motifs_fin_contrat.to_texte,'') AS motif_fin_texte,
    					COALESCE(c_qualifications.to_oid,0) AS qualification_id,
    					COALESCE(c_qualifications.to_oid::text,'') AS qualification_code_original,
    					COALESCE(c_qualifications.to_code,'') AS qualification_code,
    					COALESCE(c_qualifications.to_texte,'') AS qualification_texte,
    					CASE WHEN service_id <> 0 THEN service_id + [IPX] ELSE 0 END AS service_id,
    					CASE WHEN service_id <> 0 THEN (service_id + [IPX])::text ELSE '' END AS service_code_original,
    					CASE WHEN service_id <> 0 THEN '[CPX]' || service_code ELSE '' END AS service_code,
    					service_texte,
    					COALESCE(c_specialites.to_oid,0) AS specialite_id,
    					COALESCE(c_specialites.to_oid::text,'') AS specialite_code_original,
    					COALESCE(c_specialites.to_code,'') AS specialite_code,
    					COALESCE(c_specialites.to_texte,'') AS specialite_texte,
                        COALESCE(c_types_temps_travail.to_oid, 0) AS type_temps_travail_id,
                        COALESCE(c_types_temps_travail.to_oid::text, '') AS type_temps_travail_code_original,
                        COALESCE(c_types_temps_travail.to_code, '') AS type_temps_travail_code,
                        COALESCE(c_types_temps_travail.to_texte, '') AS type_temps_travail_texte,
    					COALESCE(c_categories_socio_professionnelle.to_oid,0) AS categorie_socio_professionnelle_id,
    					COALESCE(c_categories_socio_professionnelle.to_oid::text,'') AS categorie_socio_professionnelle_code_original,
    					COALESCE(c_categories_socio_professionnelle.to_code,'') AS categorie_socio_professionnelle_code,
    					COALESCE(c_categories_socio_professionnelle.to_texte,'') AS categorie_socio_professionnelle_texte,
    					COALESCE(c_statuts.to_oid, 0) AS statut_id,
                        COALESCE(c_statuts.to_oid::text, '') AS statut_code_original,
                        COALESCE(c_statuts.to_code, '') AS statut_code,
                        COALESCE(c_statuts.to_texte, '') AS statut_texte,
    					COALESCE(c_codes_emploi.to_oid,0) AS code_emploi_id,
    					COALESCE(c_codes_emploi.to_oid::text,'') AS code_emploi_code_original,
    					COALESCE(c_codes_emploi.to_code,'') AS code_emploi_code,
    					COALESCE(c_codes_emploi.to_texte,'') AS code_emploi_texte,
    					0 AS societe_interim_id,
    					'' AS societe_interim_code_original,
    					'' AS societe_interim_code,
    					'' AS societe_interim_texte,
						COALESCE(c_sections_analytiques_paie.to_oid,0) AS section_analytique_paie_id,
    					COALESCE(c_sections_analytiques_paie.to_oid::text,'') AS section_analytique_paie_code_original,
    					COALESCE(c_sections_analytiques_paie.to_code,'') AS section_analytique_paie_code,
    					COALESCE(c_sections_analytiques_paie.to_texte,'') AS section_analytique_paie_texte,
    					COALESCE(c_sections_analytiques.to_oid,0) AS section_analytique_id,
    					COALESCE(c_sections_analytiques.to_oid::text,'') AS section_analytique_code_original,
    					COALESCE(c_sections_analytiques.to_code,'') AS section_analytique_code,
    					COALESCE(c_sections_analytiques.to_texte,'') AS section_analytique_texte,
                        COALESCE(c_grilles.to_oid,0) AS grille_id,
    					COALESCE(c_grilles.to_oid::text,'') AS grille_code_original,
    					COALESCE(c_grilles.to_code,'') AS grille_code,
    					COALESCE(c_grilles.to_texte,'') AS grille_texte,
                        COALESCE(c_grilles_groupes.to_oid,0) AS grille_groupe_id,
    					COALESCE(c_grilles_groupes.to_oid::text,'') AS grille_groupe_code_original,
    					COALESCE(c_grilles_groupes.to_code,'') AS grille_groupe_code,
    					COALESCE(c_grilles_groupes.to_texte,'') AS grille_groupe_texte,
    					COALESCE(c_types_horaire.to_oid,0) AS type_horaire_id,
    					COALESCE(c_types_horaire.to_oid::text,'') AS type_horaire_code_original,
    					COALESCE(c_types_horaire.to_code,'') AS type_horaire_code,
    					COALESCE(c_types_horaire.to_texte,'') AS type_horaire_texte,
    					0 AS groupe_cotisant_id,
    					'' AS groupe_cotisant_code_original,
    					'' AS groupe_cotisant_code,
    					'' AS groupe_cotisant_texte,
    					COALESCE(c_cadre_emploi.to_oid,0) AS cadre_emploi_id,
    					COALESCE(c_cadre_emploi.to_oid::text,'') AS cadre_emploi_code_original,
    					COALESCE(c_cadre_emploi.to_code,'') AS cadre_emploi_code,
    					COALESCE(c_cadre_emploi.to_texte,'') AS cadre_emploi_texte
    				FROM p_profils
    					LEFT JOIN c_types_contrat ON p_profils.type_contrat_id = c_types_contrat.oid
    					LEFT JOIN c_motifs_debut_contrat ON p_profils.motif_debut_id = c_motifs_debut_contrat.oid
    					LEFT JOIN c_motifs_fin_contrat ON p_profils.motif_fin_id = c_motifs_fin_contrat.oid
    					LEFT JOIN c_qualifications ON p_profils.qualification_id = c_qualifications.oid
    					LEFT JOIN c_specialites ON p_profils.specialite_id = c_specialites.oid
    					LEFT JOIN c_codes_emploi ON p_profils.code_emploi_id = c_codes_emploi.oid
    					LEFT JOIN c_categories_socio_professionnelle ON p_profils.categorie_socio_professionnelle_id = c_categories_socio_professionnelle.oid
    					LEFT JOIN c_types_horaire ON p_profils.type_horaire_id = c_types_horaire.oid
                        LEFT JOIN c_types_temps_travail ON p_profils.type_temps_travail_id = c_types_temps_travail.oid
                        LEFT JOIN c_statuts ON p_profils.statut_id = c_statuts.oid
    					LEFT JOIN c_sections_analytiques ON p_profils.section_analytique_id = c_sections_analytiques.oid
    					LEFT JOIN c_sections_analytiques_paie ON p_profils.section_analytique_paie_id = c_sections_analytiques_paie.oid
    					LEFT JOIN c_cadre_emploi ON p_profils.cadre_emploi_id = c_cadre_emploi.oid
                        LEFT JOIN c_grilles ON p_profils.grille_id = c_grilles.oid
                        LEFT JOIN c_grilles_groupes ON p_profils.grille_groupe_id = c_grilles_groupes.oid
				;
				
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.p_salaries
					', 'temp p_salaries'
				);

				INSERT INTO rh.p_salaries(
					oid,
                    code,
					nom,
					prenom,
					date_naissance,
					sexe,
					matricule,
					code_original,
                    nir,
					entreprise_id,
					nationalite_id,
					code_postal_id,
					nom_naissance,
					situation_famille_id,
					adresse1,
					adresse2,
					profil_id,
					date_debut,
					date_fin,
                    finess,
                    date_entree_ets,
                    date_sortie_ets,
                    date_entree_fp,
                    date_entree_fph,
                    no_adeli,
                    code_cotisation_id,
                    matricule_retraite,
                    salarie_fusionne_id,
                    -- personne_id,
                    date_anciennete)
    				SELECT
    					p_salaries.oid + [IPX] AS oid,
                        p_salaries.code,
    					p_salaries.nom,
    					p_salaries.prenom,
    					p_salaries.date_naissance,
    					p_salaries.sexe,
    					p_salaries.matricule || CASE WHEN [IPX] <> 0 THEN ' ([PX])' ELSE '' END AS matricule,
    					p_salaries.oid + [IPX] AS code_original,
                        p_salaries.nir,
    					CASE WHEN p_salaries.entreprise_id <> 0 THEN p_salaries.entreprise_id + [IPX] ELSE 0 END AS entreprise_id,
    					COALESCE(c_nationalites.to_oid, 0) AS nationalite_id,
    					COALESCE(t_codes_postaux.oid, 0) AS code_postal_id,
    					p_salaries.nom_naissance,
    					COALESCE(c_situations_famille.to_oid, 0) AS situation_famille_id,
						p_salaries.adresse1,
						p_salaries.adresse2,
    					CASE WHEN p_salaries.profil_id <> 0 THEN p_salaries.profil_id + [IPX] ELSE 0 END AS profil_id,
    					p_salaries.date_debut,
    					p_salaries.date_fin,
                        p_salaries.finess,
                        p_salaries.date_entree_ets,
                        p_salaries.date_sortie_ets,
                        p_salaries.date_entree_fp,
                        p_salaries.date_entree_fph,
                        p_salaries.no_adeli,
                        --p_salaries.code_cotisation_id + [IPX] AS code_cotisation_id,
                        0 as code_cotisation_id,
                        p_salaries.matricule_retraite,
                        p_salaries.salarie_fusionne_id + [IPX] AS salarie_fusionne_id,
                        -- p_salaries.personne_id + [IPX],
                        p_salaries.date_anciennete
    				FROM p_salaries
    					LEFT JOIN c_nationalites ON p_salaries.nationalite_id = c_nationalites.oid
    					LEFT JOIN c_situations_famille ON p_salaries.situation_famille_id = c_situations_famille.oid
						LEFT JOIN base.t_codes_postaux ON p_salaries.code_postal_id = t_codes_postaux.oid
                        LEFT JOIN rh.p_salaries AS rh_sal ON rh_sal.oid = p_salaries.oid + [IPX]
                    WHERE rh_sal.oid IS NULL
                ;

			]]></sqlcmd>
		</NODE>
		<NODE label="Contrats">
			<sqlcmd><![CDATA[
			
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.p_contrats
					', 'temp p_contrats'
				);

				INSERT INTO rh.p_contrats (
					oid,
					salarie_id,
					date_debut,
					date_fin,
					numero_contrat,
					etablissement_id,
					code_original,
					profil_id,
					anciennete_anterieure_mois,
					anciennete_anterieure_calculee_mois,
					salarie_remplace_id,
					anciennete_anterieure_jours,
                    anciennete_anterieure_calculee_jours)
    				SELECT 
    					p_contrats.oid + [IPX] AS oid,
    					salarie_id + [IPX] AS salarie_id,
    					date_debut,
    					date_fin,
    					numero_contrat || CASE WHEN [IPX] <> 0 THEN ' ([PX])' ELSE '' END AS numero_contrat,
    					CASE WHEN etablissement_id <> 0 THEN etablissement_id + [IPX] ELSE 0 END AS etablissement_id,
    					p_contrats.oid + [IPX] AS code_original,
    					CASE WHEN profil_id <> 0 THEN profil_id + [IPX] ELSE 0 END AS profil_id,
    					anciennete_anterieure_mois,
    					anciennete_anterieure_calculee_mois,
    					CASE WHEN salarie_remplace_id <> 0 THEN salarie_remplace_id + [IPX] ELSE 0 END AS salarie_remplace_id,
    					anciennete_anterieure_jours,
                        anciennete_anterieure_calculee_jours
    				FROM p_contrats
				;

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.p_contrats_mois
					', 'temp p_contrats_mois'
				);

				INSERT INTO rh.p_contrats_mois (
					oid,
					salarie_id,
					contrat_id,
					mois_activite,
					profil_id,
					nombre_debut_contrat,
					nombre_fin_contrat,
					equivalent_temps_plein,
					date_debut,
					date_fin,
					age_id,
					age_jours,
					est_hors_periode,
					present_fin_mois,
					anciennete_mois,
					anciennete_annee_id,
					nombre_departs,
                    anciennete_grade_mois,
                    anciennete_grade_annee_id,
                    anciennete_fp_mois,
                    anciennete_fp_annee_id,
                    anciennete_fph_mois,
                    anciennete_fph_annee_id,
                    fictif,
                    nombre_entrees,
                    present_debut_mois,
                    anciennete_jours,
                    etablissement_id,
                    nombre_heures)
    				SELECT
    					p_contrats_mois.oid + [IPX] AS oid,
    					CASE WHEN p_contrats_mois.salarie_id <> 0 THEN p_contrats_mois.salarie_id + [IPX] ELSE 0 END AS salarie_id,
    					contrat_id + [IPX] AS contrat_id,
    					mois_activite,
    					CASE WHEN profil_id <> 0 THEN profil_id + [IPX] ELSE 0 END AS profil_id,
    					nombre_debut_contrat,
    					nombre_fin_contrat,
    					equivalent_temps_plein,
    					date_debut,
    					date_fin,
    					age_id,
    					age_jours,
    					est_hors_periode,
    					present_fin_mois,
    					anciennete_mois,
    					anciennete_annee_id,
    					nombre_departs,
                        anciennete_grade_mois,
                        anciennete_grade_annee_id,
                        anciennete_fp_mois,
                        anciennete_fp_annee_id,
                        anciennete_fph_mois,
                        anciennete_fph_annee_id,
                        fictif,
                        nombre_entrees,
                        present_debut_mois,
                        anciennete_jours,
                        CASE WHEN etablissement_id <> 0 THEN etablissement_id + [IPX] ELSE 0 END AS etablissement_id,
                        nombre_heures
    				FROM p_contrats_mois
				;

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.p_profil_contrat_mois
					', 'temp p_profil_contrat_mois')
                ;

				INSERT INTO rh.p_profil_contrat_mois (
                    -- profil_avant_transformation_id,
                    oid,
					profil_id, 
					contrat_mois_id, 
					ratio, 
					mois, 
					salarie_id)
    				SELECT
                        -- profil_avant_transformation_id,
                        oid + [IPX],
    					CASE WHEN profil_id <> 0 THEN profil_id + [IPX] ELSE 0 END AS profil_id, 
    					CASE WHEN contrat_mois_id <> 0 THEN contrat_mois_id + [IPX] ELSE 0 END AS contrat_mois_id, 
    					ratio, 
    					mois, 
    					CASE WHEN salarie_id <> 0 THEN salarie_id + [IPX] ELSE 0 END AS salarie_id
    				FROM p_profil_contrat_mois
                ;

			]]></sqlcmd>
		</NODE>
		<NODE label="Historique de paie">
			<sqlcmd><![CDATA[
			
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.p_historique_paie
					', 'temp p_historique_paie'
				);

				INSERT INTO rh.p_historique_paie (
                    oid,
                    etablissement_id,
					contrat_id,
					age_id,
					mois_activite,
					date_debut,
					date_fin,
					mois_paie,
					date_paie,
					rubrique_id,
					compte_id,
					organisme_cotisation_id,
					base,
					montant_brut,
					taux_cotisation_salarie,
					montant_cotisation_salarie,
					taux_cotisation_patronale,
					montant_cotisation_patronale,
					montant_net_imposable_salarie,
					montant_net_a_payer_salarie,
					salarie_id,
					contrat_mois_id,
					profil_id,
					montant_avantage_nature,
					montant_frais_imposables,
					montant_od_net_salarie,
                    montant_od_net_patronale,
                    -- ventilation_1_id,
                    -- ventilation_2_id,
                    -- ventilation_3_id,
                    nombre,
                    montant_masse_salariale,
                    montant_salarial,
                    montant_patronal,
                    heure_contrat,
                    heure_payee,
                    heure_travaillee)
    				SELECT
                        p_historique_paie.oid + [IPX],
                        CASE WHEN etablissement_id <> 0 THEN etablissement_id + [IPX] ELSE 0 END AS etablissement_id,
    					contrat_id + [IPX] AS contrat_id,
    					age_id,
    					mois_activite,
    					date_debut,
    					date_fin,
    					mois_paie,
    					date_paie,
    					COALESCE(c_rubriques.to_oid,0) AS rubrique_id,
    					COALESCE(c_compte.to_oid,0) AS compte_id,
    					0 AS organisme_cotisation_id,
    					base,
    					montant_brut,
    					taux_cotisation_salarie,
    					montant_cotisation_salarie,
    					taux_cotisation_patronale,
    					montant_cotisation_patronale,
    					montant_net_imposable_salarie,
    					montant_net_a_payer_salarie,
    					salarie_id + [IPX] AS salarie_id,
    					contrat_mois_id + [IPX] AS contrat_mois_id,
    					CASE WHEN profil_id <> 0 THEN profil_id + [IPX] ELSE 0 END AS profil_id,
    					montant_avantage_nature,
    					montant_frais_imposables,
    					montant_od_net_salarie,
                        montant_od_net_patronale,
                        -- ventilation_1_id,
                        -- ventilation_2_id,
                        -- ventilation_3_id,
                        nombre,
                        montant_masse_salariale,
                        montant_salarial,
                        montant_patronal,
                        heure_contrat,
                        heure_payee,
                        heure_travaillee
    				FROM p_historique_paie
    					LEFT JOIN c_rubriques ON p_historique_paie.rubrique_id = c_rubriques.oid
    					LEFT JOIN c_compte ON p_historique_paie.compte_id = c_compte.oid
				;
				
			]]></sqlcmd>
		</NODE>
        <NODE label="Mise à jour de l'historique de paie (classique/paramétrée)">
            <condition><![CDATA[
                
            -- Option utilisée lors d'une consolidation avec un mélange paie classique / paramétrée (ex: SHS / CEGI).
            SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_divers
					', 'temp t_divers'
				);

            select valeur = 0
            from t_divers
            where code = 'OPT_STD_HP'
            ;

            ]]></condition>
            <sqlcmd><![CDATA[

            -- Màj basée sur la vue SQL rh.v_historique_paie_0 et pour l'ancien prestataire uniquement (IDENTIQUE A LA FUSION DE 2 PRESTATAIRES).
            with update_hp as (
                SELECT
                    p_historique_paie.ctid,
                    coalesce(p_historique_paie.base, 0) * CASE
                        WHEN (@t_rubriques.coefficient_base) = 1 THEN 1
                        ELSE p_contrats_mois.ratio_temps_travail
                    END * t_rubriques.coefficient_base as base,
                    coalesce(p_historique_paie.nombre, 0) * t_rubriques.coefficient_nombre as nombre,
                    p_historique_paie.montant_brut
                        + p_historique_paie.montant_avantage_nature
                        + p_historique_paie.montant_frais_imposables
                        + p_historique_paie.montant_cotisation_patronale
                        + p_historique_paie.montant_od_net_patronale as montant_masse_salariale,
                    CASE
                        WHEN t_rubriques.code_calcul::text = 10::text THEN
                            CASE
                                WHEN t_rubriques.unite::text = 0::text THEN 1::numeric
                                ELSE t_rubriques.coefficient
                            END * p_historique_paie.base
                        ELSE 0::numeric
                    END AS heures_contrat,
                    CASE
                        WHEN t_rubriques.code_calcul::text = 1::text THEN
                            CASE
                                WHEN t_rubriques.unite::text = 0::text THEN 1::numeric
                                ELSE t_rubriques.coefficient
                            END * p_historique_paie.base
                        ELSE 0::numeric
                    END as heures_payees,
                    CASE
                      WHEN t_divers.valeur = '0'::text THEN
                      CASE
                          WHEN t_rubriques.code_calcul::text = 1::text THEN
                          CASE
                              WHEN t_rubriques.unite::text = 0::text THEN 1::numeric
                              ELSE t_rubriques.coefficient
                          END * p_historique_paie.base
                          ELSE 0::numeric
                      END -
                      CASE
                          WHEN t_rubriques.code_calcul::text = 2::text THEN
                          CASE
                              WHEN t_rubriques.unite::text = 0::text THEN 1::numeric
                              ELSE t_rubriques.coefficient
                          END * p_historique_paie.base
                          ELSE 0::numeric
                      END
                      ELSE
                      CASE
                          WHEN t_rubriques.code_calcul::text = 2::text THEN
                          CASE
                              WHEN t_rubriques.unite::text = 0::text THEN 1::numeric
                              ELSE t_rubriques.coefficient
                          END * p_historique_paie.base
                          ELSE 0::numeric
                      END
                    END heures_travaillees
                FROM rh.p_historique_paie
                    JOIN rh.t_divers ON t_divers.code = 'HEURES_W'::text
                    JOIN rh.p_contrats_mois ON p_contrats_mois.oid = p_historique_paie.contrat_mois_id
                    JOIN rh.t_rubriques ON t_rubriques.oid = p_historique_paie.rubrique_id)
            UPDATE rh.p_historique_paie SET
                base = update_hp.base,
                nombre = update_hp.nombre,
                montant_masse_salariale = update_hp.montant_masse_salariale,
                heure_contrat = update_hp.heures_contrat,
                heure_payee = update_hp.heures_payees,
                heure_travaillee = update_hp.heures_travaillees
            FROM update_hp
            WHERE true
                AND p_historique_paie.ctid = update_hp.ctid
                AND p_historique_paie.oid between [IPX] AND p_historique_paie.oid + [IPX] -- uniquement la plage de l'env. concerné.
                AND (false -- et uniquement pour les lignes où c'est nécessaire.
                    OR p_historique_paie.base IS DISTINCT FROM update_hp.base
                    OR p_historique_paie.nombre IS DISTINCT FROM update_hp.nombre
                    OR p_historique_paie.montant_masse_salariale IS DISTINCT FROM update_hp.montant_masse_salariale
                    OR p_historique_paie.heure_contrat IS DISTINCT FROM update_hp.heures_contrat
                    OR p_historique_paie.heure_payee IS DISTINCT FROM update_hp.heures_payees
                    OR p_historique_paie.heure_travaillee IS DISTINCT FROM update_hp.heures_travaillees)
            ;

            ]]></sqlcmd>
        </NODE>
		<NODE label="Arrets">
			<sqlcmd><![CDATA[
			
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.p_arrets_travail
					', 'temp p_arrets_travail'
				);

				INSERT INTO rh.p_arrets_travail (
                    oid,
					salarie_id,
					contrat_id,
					mois_activite,
					date_debut,
					date_fin,
					date_reprise,
					motif_arret_id,
					precision_motif_arret_id,
					nb_jours,
                    nb_heures)
    				SELECT
                        p_arrets_travail.oid + [IPX] as oid,
    					salarie_id + [IPX] as salarie_id,
    					contrat_id + [IPX] AS contrat_id,
    					mois_activite,
    					date_debut,
    					date_fin,
    					date_reprise,
    					coalesce(c_motifs_arret.to_oid, 0) AS motif_arret_id,
    					0 as precision_motif_arret_id,
    					nb_jours,
                        nb_heures
    				FROM p_arrets_travail
                        LEFT JOIN c_motifs_arret ON p_arrets_travail.motif_arret_id = c_motifs_arret.oid
				;

                SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
                    SELECT *
                    FROM rh.p_arrets_travail_mois
                    ', 'temp p_arrets_travail_mois'
                );

    			INSERT INTO rh.p_arrets_travail_mois(
    				arret_travail_id,
    				salarie_id,
    				contrat_id,
    				contrat_mois_id,
    				mois_activite,
    				nb_debut_arret,
    				nb_fin_arret,
    				nb_reprise_apres_arret,
    				date_debut,
    				date_fin,
    				nb_jours,
    				nb_arret,
                    nb_heures)
    				SELECT
    					arret_travail_id + [IPX] AS arret_travail_id,
    					salarie_id + [IPX] AS salarie_id,
    					contrat_id + [IPX] AS contrat_id,
    					contrat_mois_id + [IPX] AS contrat_mois_id,
    					mois_activite,
    					nb_debut_arret,
    					nb_fin_arret,
    					nb_reprise_apres_arret,
    					date_debut,
    					date_fin,
    					nb_jours,
    					nb_arret,
                        nb_heures
    				FROM p_arrets_travail_mois
    			;
    			
			]]></sqlcmd>
		</NODE>
		<NODE label="Chiffrier">
			<sqlcmd><![CDATA[
			
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.p_chiffrier_production
					', 'temp p_chiffrier_production'
				);

				INSERT INTO rh.p_chiffrier_production (
					entreprise_id,
					etablissement_id,
					mois,
					nombre_salaries,
					montant_brut,
					nombre_heures
				)
				SELECT
					entreprise_id + [IPX] AS entreprise_id,
					etablissement_id + [IPX] AS etablissement_id,
					mois,
					nombre_salaries,
					montant_brut,
					nombre_heures
				FROM p_chiffrier_production
				;
				
										
			]]></sqlcmd>
		</NODE>
	</NODE>

	<NODE name="PARAM" label="RÉCUPÉRATION DES PARAMÈTRES">
		<NODE label="Récupération des tables">
			<sqlcmd><![CDATA[
			
				-- Paramétrage pour fusion paramètres
				INSERT INTO rh.t_divers (code, texte, valeur, description)
				SELECT
					'CONSO_FUSIONPARAM',
					'Fusion des paramètres en consolidation',
					'1',
					'0 Pas de fusion, 1 fusion selon code ou texte'
				WHERE 'CONSO_FUSIONPARAM' NOT IN (SELECT code FROM rh.t_divers)
				;

                SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM base.t_finess
					', 'temp i_finess'
				);
                
				INSERT INTO base.t_finess(
					code,
					texte,
					texte_court,
                    type_etablissement,
                    secondaire)
                    SELECT
                        code,
                        texte,
                        texte_court,
                        type_etablissement,
                        '1' AS secondaire
                    FROM i_finess
                    WHERE 1=1
                        AND oid != 0
                        AND i_finess.code NOT IN (SELECT code FROM base.t_finess)
                ;

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_entreprises
					WHERE oid IN (SELECT entreprise_id FROM rh.p_contrats JOIN rh.t_etablissements ON etablissement_id = t_etablissements.oid GROUP BY 1)
					', 'temp t_entreprises'
				);

				INSERT INTO rh.t_entreprises(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					oid + [IPX],
					oid + [IPX] AS code_original, 
					'[PX]' || code, 
					texte, 
					texte_court
				FROM t_entreprises
				WHERE oid <> 0 AND oid + [IPX] NOT IN (SELECT oid FROM rh.t_entreprises);
				
				UPDATE rh.t_entreprises SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_entreprises SET code_original = oid WHERE code_original <> oid::text;
				
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_etablissements
					WHERE oid IN (SELECT etablissement_id FROM rh.p_contrats)
					', 'temp t_etablissements'
				);


				INSERT INTO rh.t_etablissements(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court,
					entreprise_id,
					base_calcul_etp) 
				SELECT 
					oid + [IPX],
					oid + [IPX] AS code_original, 
					'[CPX]' || code, 
					texte, 
					texte_court,
					CASE WHEN entreprise_id <> 0 THEN entreprise_id + [IPX] ELSE 0 END,
					base_calcul_etp
				FROM t_etablissements
				WHERE oid <> 0 AND oid + [IPX] NOT IN (SELECT oid FROM rh.t_etablissements);
				
				UPDATE rh.t_etablissements SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_etablissements SET code_original = oid WHERE code_original <> oid::text;
				
				-- rubriques
				
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_rubriques
					WHERE oid <> 0
					', 'temp t_rubriques'
				);


				DROP TABLE IF EXISTS c_rubriques;
				CREATE TEMP TABLE c_rubriques AS
				SELECT
					*,
					lower(texte) AS texte_adapte,
					''::text AS texte_soundex,
					''::text AS to_code,
					0::bigint AS to_oid
				FROM t_rubriques;

				UPDATE c_rubriques SET texte_adapte = replace(texte_adapte, 'apprenti', 'app');
				UPDATE c_rubriques SET texte_adapte = replace(texte_adapte, 'absence', 'abs');
				UPDATE c_rubriques SET texte_adapte = replace(texte_adapte, 'vieillesse', 'vieil');
				UPDATE c_rubriques SET texte_adapte = replace(texte_adapte, 'provisions', 'prov');
				UPDATE c_rubriques SET texte_adapte = replace(texte_adapte, 'intéressement', 'int');
				UPDATE c_rubriques SET texte_adapte = replace(texte_adapte, 'défiscalisées', 'défisc');
				UPDATE c_rubriques SET texte_adapte = replace(texte_adapte, 'salaire', 'sal');

				UPDATE c_rubriques SET texte_soundex = base.cti_soundex_nom(texte_adapte);


				DROP TABLE IF EXISTS w_rubriques_to;
				CREATE TEMP TABLE w_rubriques_to AS
				SELECT
					*,
					lower(texte) AS texte_adapte,
					''::text AS texte_soundex
				FROM rh.t_rubriques
				WHERE 
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0' OR
					t_rubriques.code IN ('C000') AND oid <= 999999999
				;

				UPDATE w_rubriques_to SET texte_adapte = replace(texte_adapte, 'apprenti', 'app');
				UPDATE w_rubriques_to SET texte_adapte = replace(texte_adapte, 'absence', 'abs');
				UPDATE w_rubriques_to SET texte_adapte = replace(texte_adapte, 'vieillesse', 'vieil');
				UPDATE w_rubriques_to SET texte_adapte = replace(texte_adapte, 'provisions', 'prov');
				UPDATE w_rubriques_to SET texte_adapte = replace(texte_adapte, 'intéressement', 'int');
				UPDATE w_rubriques_to SET texte_adapte = replace(texte_adapte, 'défiscalisées', 'défisc');
				UPDATE w_rubriques_to SET texte_adapte = replace(texte_adapte, 'salaire', 'sal');

				UPDATE w_rubriques_to SET texte_soundex = base.cti_soundex_nom(texte_adapte);

				UPDATE c_rubriques
				SET to_oid = oid + [IPX],
					to_code = CASE WHEN (SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0' THEN code ELSE '[CPX]' || code END
				WHERE code NOT IN (SELECT code FROM w_rubriques_to)
				;
				

				UPDATE c_rubriques
				SET to_oid = w_rubriques_to.oid,
					to_code = c_rubriques.code
				FROM w_rubriques_to
				WHERE to_oid = 0 AND
					c_rubriques.code = w_rubriques_to.code AND
					(c_rubriques.texte_soundex = w_rubriques_to.texte_soundex OR c_rubriques.code = 'C000');


				UPDATE c_rubriques
				SET to_oid = oid + [IPX],
					to_code = code || '_[PX]'
				WHERE to_oid = 0;

				ALTER TABLE c_rubriques ADD CONSTRAINT c_rubriques_pkey PRIMARY KEY(oid);
				
				UPDATE rh.t_rubriques SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_rubriques SET code_original = oid WHERE code_original <> oid::text;

				INSERT INTO rh.t_rubriques(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court,
					rang_edition,
					chapitre_bulletin,
					heures_payees,
					code_calcul,
					unite,
					coefficient) 
				SELECT 
					to_oid,
					to_oid AS code_original, 
					to_code,
					texte, 
					texte_court,
					rang_edition,
					chapitre_bulletin,
					heures_payees,
					code_calcul,
					unite,
					coefficient
				FROM c_rubriques
				WHERE oid <> 0 AND to_oid NOT IN (SELECT oid FROM rh.t_rubriques);
				
				UPDATE rh.t_rubriques SET
					code = c_rubriques.to_code,
					code_original = c_rubriques.to_oid,
					texte = c_rubriques.texte,
					texte_court = c_rubriques.texte_court
				FROM c_rubriques
				WHERE t_rubriques.oid = c_rubriques.to_oid AND
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' AND
					(
						t_rubriques.code IS DISTINCT FROM c_rubriques.to_code OR
						t_rubriques.code_original IS DISTINCT FROM c_rubriques.to_oid OR
						t_rubriques.texte IS DISTINCT FROM c_rubriques.texte OR
						t_rubriques.texte_court IS DISTINCT FROM c_rubriques.texte_court
					)
				;
				
				-- comptes
				
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_compte
					WHERE oid <> 0
					', 'temp t_compte'
				);



				INSERT INTO rh.t_compte(
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					code_original, 
					code, 
					texte, 
					texte_court
				FROM t_compte
				WHERE oid <> 0 AND 
					code NOT IN (SELECT code FROM rh.t_compte WHERE code IS NOT NULL) AND
					code_original NOT IN (SELECT code_original FROM rh.t_compte WHERE code IS NOT NULL);



				INSERT INTO rh.t_compte(oid, code_original, code, texte, texte_court)
				SELECT 0, '', chr(1) || '*', 'Non renseigné', 'N/R'
				WHERE 0 NOT IN (SELECT oid FROM rh.t_compte);


				DROP TABLE IF EXISTS c_compte;
				CREATE TEMP TABLE c_compte AS
				SELECT
					t_compte.oid,
					MIN(t_compte_conso.oid) AS to_oid
				FROM t_compte
				JOIN rh.t_compte t_compte_conso ON t_compte_conso.code = t_compte.code
				GROUP BY 1;


				ALTER TABLE c_compte ADD CONSTRAINT c_compte_pkey PRIMARY KEY(oid);
		
				
				-- Catégories socio professionnelles
								
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_categories_socio_professionnelle
					WHERE oid <> 0
					', 'temp t_categories_socio_professionnelle'
				);


				DROP TABLE IF EXISTS c_categories_socio_professionnelle;
				CREATE TEMP TABLE c_categories_socio_professionnelle AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex,
					''::text AS to_code,
					''::text AS to_texte,
					0::bigint AS to_oid
				FROM t_categories_socio_professionnelle;


				DROP TABLE IF EXISTS w_categories_socio_professionnelle_to;
				CREATE TEMP TABLE w_categories_socio_professionnelle_to AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex
				FROM rh.t_categories_socio_professionnelle
				WHERE (SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0';


				UPDATE c_categories_socio_professionnelle
				SET to_oid = oid + [IPX],
					to_code = code,
					to_texte = texte
				WHERE code NOT IN (SELECT code FROM w_categories_socio_professionnelle_to);
				

				UPDATE c_categories_socio_professionnelle
				SET to_oid = w_categories_socio_professionnelle_to.oid,
					to_code = c_categories_socio_professionnelle.code,
					to_texte = c_categories_socio_professionnelle.texte
				FROM w_categories_socio_professionnelle_to
				WHERE to_oid = 0 AND
					c_categories_socio_professionnelle.code = w_categories_socio_professionnelle_to.code AND
					c_categories_socio_professionnelle.texte_soundex = w_categories_socio_professionnelle_to.texte_soundex;


				UPDATE c_categories_socio_professionnelle
				SET to_oid = oid + [IPX],
					to_code = '[CPX]' || code
				WHERE to_oid = 0 OR
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' ;

				UPDATE rh.t_categories_socio_professionnelle SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_categories_socio_professionnelle SET code_original = oid WHERE code_original <> oid::text;

				INSERT INTO rh.t_categories_socio_professionnelle(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					to_oid,
					to_oid AS code_original, 
					to_code,
					texte, 
					texte_court
				FROM c_categories_socio_professionnelle
				WHERE oid <> 0 AND to_oid NOT IN (SELECT oid FROM rh.t_categories_socio_professionnelle);
				
				UPDATE rh.t_categories_socio_professionnelle SET
					code = c_categories_socio_professionnelle.to_code,
					code_original = c_categories_socio_professionnelle.to_oid,
					texte = c_categories_socio_professionnelle.texte,
					texte_court = c_categories_socio_professionnelle.texte_court
				FROM c_categories_socio_professionnelle
				WHERE t_categories_socio_professionnelle.oid = c_categories_socio_professionnelle.to_oid AND
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' AND
					(
						t_categories_socio_professionnelle.code = c_categories_socio_professionnelle.to_code OR
						t_categories_socio_professionnelle.code_original = c_categories_socio_professionnelle.to_oid OR
						t_categories_socio_professionnelle.texte = c_categories_socio_professionnelle.texte OR
						t_categories_socio_professionnelle.texte_court = c_categories_socio_professionnelle.texte_court
					)
				;
				
				-- codes_emploi
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_codes_emploi
					WHERE oid <> 0
					', 'temp t_codes_emploi'
				);


				DROP TABLE IF EXISTS c_codes_emploi;
				CREATE TEMP TABLE c_codes_emploi AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex,
					''::text AS to_code,
					''::text AS to_texte,
					0::bigint AS to_oid
				FROM t_codes_emploi;


				DROP TABLE IF EXISTS w_codes_emploi_to;
				CREATE TEMP TABLE w_codes_emploi_to AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex
				FROM rh.t_codes_emploi
				WHERE (SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0';


				UPDATE c_codes_emploi
				SET to_oid = oid + [IPX],
					to_code = code,
					to_texte = texte
				WHERE code NOT IN (SELECT code FROM w_codes_emploi_to);
				

				UPDATE c_codes_emploi
				SET to_oid = w_codes_emploi_to.oid,
					to_code = w_codes_emploi_to.code,
					to_texte = w_codes_emploi_to.texte
				FROM w_codes_emploi_to
				WHERE to_oid = 0 AND
					c_codes_emploi.code = w_codes_emploi_to.code AND
					c_codes_emploi.texte_soundex = w_codes_emploi_to.texte_soundex;

				UPDATE c_codes_emploi
				SET to_oid = oid + [IPX],
					to_code = '[CPX]' || code,
					to_texte = texte_court
				WHERE to_oid = 0 OR
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0';

				ALTER TABLE c_codes_emploi ADD CONSTRAINT c_codes_emploi_pkey PRIMARY KEY(oid);
				
				UPDATE rh.t_codes_emploi SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_codes_emploi SET code_original = oid WHERE code_original <> oid::text;

				INSERT INTO rh.t_codes_emploi(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					to_oid,
					to_oid AS code_original, 
					to_code,
					texte, 
					texte_court
				FROM c_codes_emploi
				WHERE oid <> 0 AND to_oid NOT IN (SELECT oid FROM rh.t_codes_emploi);
				
				UPDATE rh.t_codes_emploi SET
					code = c_codes_emploi.to_code,
					code_original = c_codes_emploi.to_oid,
					texte = c_codes_emploi.texte,
					texte_court = c_codes_emploi.texte_court
				FROM c_codes_emploi
				WHERE t_codes_emploi.oid = c_codes_emploi.to_oid AND
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' AND
					(
						t_codes_emploi.code = c_codes_emploi.to_code OR
						t_codes_emploi.code_original = c_codes_emploi.to_oid OR
						t_codes_emploi.texte = c_codes_emploi.texte OR
						t_codes_emploi.texte_court = c_codes_emploi.texte_court
					)
				;
				
				-- cadre_emploi
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_cadre_emploi
					WHERE oid <> 0
					', 'temp t_cadre_emploi'
				);


				DROP TABLE IF EXISTS c_cadre_emploi;
				CREATE TEMP TABLE c_cadre_emploi AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex,
					''::text AS to_code,
					''::text AS to_texte,
					0::bigint AS to_oid
				FROM t_cadre_emploi;


				DROP TABLE IF EXISTS w_cadre_emploi_to;
				CREATE TEMP TABLE w_cadre_emploi_to AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex
				FROM rh.t_cadre_emploi
				WHERE (SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0';


				UPDATE c_cadre_emploi
				SET to_oid = oid + [IPX],
					to_code = code,
					to_texte = texte
				WHERE code NOT IN (SELECT code FROM w_cadre_emploi_to);
				

				UPDATE c_cadre_emploi
				SET to_oid = w_cadre_emploi_to.oid,
					to_code = w_cadre_emploi_to.code,
					to_texte = w_cadre_emploi_to.texte
				FROM w_cadre_emploi_to
				WHERE to_oid = 0 AND
					c_cadre_emploi.code = w_cadre_emploi_to.code AND
					c_cadre_emploi.texte_soundex = w_cadre_emploi_to.texte_soundex;

				UPDATE c_cadre_emploi
				SET to_oid = oid + [IPX],
					to_code = '[CPX]' || code,
					to_texte = texte_court
				WHERE to_oid = 0 OR
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0';

				ALTER TABLE c_cadre_emploi ADD CONSTRAINT c_cadre_emploi_pkey PRIMARY KEY(oid);
				
				UPDATE rh.t_cadre_emploi SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_cadre_emploi SET code_original = oid WHERE code_original <> oid::text;

				INSERT INTO rh.t_cadre_emploi(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					to_oid,
					to_oid AS code_original, 
					to_code,
					texte, 
					texte_court
				FROM c_cadre_emploi
				WHERE oid <> 0 AND to_oid NOT IN (SELECT oid FROM rh.t_cadre_emploi);
				
				UPDATE rh.t_cadre_emploi SET
					code = c_cadre_emploi.to_code,
					code_original = c_cadre_emploi.to_oid,
					texte = c_cadre_emploi.texte,
					texte_court = c_cadre_emploi.texte_court
				FROM c_cadre_emploi
				WHERE t_cadre_emploi.oid = c_cadre_emploi.to_oid AND
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' AND
					(
						t_cadre_emploi.code = c_cadre_emploi.to_code OR
						t_cadre_emploi.code_original = c_cadre_emploi.to_oid OR
						t_cadre_emploi.texte = c_cadre_emploi.texte OR
						t_cadre_emploi.texte_court = c_cadre_emploi.texte_court
					)
				;
				
				
				
				
				-- motifs_arret
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_motifs_arret
					WHERE oid <> 0
					', 'temp t_motifs_arret'
				);

				DROP TABLE IF EXISTS c_motifs_arret;
				CREATE TEMP TABLE c_motifs_arret AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex,
					''::text AS to_code,
					0::bigint AS to_oid
				FROM t_motifs_arret;

				DROP TABLE IF EXISTS w_motifs_arret_to;
				CREATE TEMP TABLE w_motifs_arret_to AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex
				FROM rh.t_motifs_arret
				WHERE (SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0';

				UPDATE c_motifs_arret
				SET to_oid = oid + [IPX],
					to_code = code
				WHERE code NOT IN (SELECT code FROM w_motifs_arret_to);
				
				UPDATE c_motifs_arret
				SET to_oid = w_motifs_arret_to.oid,
					to_code = c_motifs_arret.code
				FROM w_motifs_arret_to
				WHERE to_oid = 0 AND
					c_motifs_arret.code = w_motifs_arret_to.code AND
					c_motifs_arret.texte_soundex = w_motifs_arret_to.texte_soundex;

				UPDATE c_motifs_arret
				SET to_oid = oid + [IPX],
					to_code = '[CPX]' || code
				WHERE to_oid = 0 OR
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0';

				UPDATE rh.t_motifs_arret SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_motifs_arret SET code_original = oid WHERE code_original <> oid::text;

				INSERT INTO rh.t_motifs_arret(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					to_oid,
					to_oid AS code_original, 
					to_code,
					texte, 
					texte_court
				FROM c_motifs_arret
				WHERE oid <> 0 AND to_oid NOT IN (SELECT oid FROM rh.t_motifs_arret);
				
				UPDATE rh.t_motifs_arret SET
					code = c_motifs_arret.to_code,
					code_original = c_motifs_arret.to_oid,
					texte = c_motifs_arret.texte,
					texte_court = c_motifs_arret.texte_court
				FROM c_motifs_arret
				WHERE t_motifs_arret.oid = c_motifs_arret.to_oid AND
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' AND
					(
						t_motifs_arret.code = c_motifs_arret.to_code OR
						t_motifs_arret.code_original = c_motifs_arret.to_oid OR
						t_motifs_arret.texte = c_motifs_arret.texte OR
						t_motifs_arret.texte_court = c_motifs_arret.texte_court
					)
				;
				
				-- motifs_debut_contrat
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_motifs_debut_contrat
					WHERE oid <> 0
					', 'temp t_motifs_debut_contrat'
				);

				DROP TABLE IF EXISTS c_motifs_debut_contrat;
				CREATE TEMP TABLE c_motifs_debut_contrat AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex,
					''::text AS to_code,
					''::text AS to_texte,
					0::bigint AS to_oid
				FROM t_motifs_debut_contrat;

				DROP TABLE IF EXISTS w_motifs_debut_contrat_to;
				CREATE TEMP TABLE w_motifs_debut_contrat_to AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex
				FROM rh.t_motifs_debut_contrat
				WHERE (SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0';

				UPDATE c_motifs_debut_contrat
				SET to_oid = oid + [IPX],
					to_code = code,
					to_texte = texte
				WHERE code NOT IN (SELECT code FROM w_motifs_debut_contrat_to);
				
				UPDATE c_motifs_debut_contrat
				SET to_oid = w_motifs_debut_contrat_to.oid,
					to_code = c_motifs_debut_contrat.code,
					to_texte = c_motifs_debut_contrat.texte
				FROM w_motifs_debut_contrat_to
				WHERE to_oid = 0 AND
					c_motifs_debut_contrat.code = w_motifs_debut_contrat_to.code AND
					c_motifs_debut_contrat.texte_soundex = w_motifs_debut_contrat_to.texte_soundex;

				UPDATE c_motifs_debut_contrat
				SET to_oid = oid + [IPX],
					to_code = '[CPX]' || code,
					to_texte = texte
				WHERE to_oid = 0 OR
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0';

				UPDATE rh.t_motifs_debut_contrat SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_motifs_debut_contrat SET code_original = oid WHERE code_original <> oid::text;

				INSERT INTO rh.t_motifs_debut_contrat(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					to_oid,
					to_oid AS code_original, 
					to_code,
					texte, 
					texte_court
				FROM c_motifs_debut_contrat
				WHERE oid <> 0 AND to_oid NOT IN (SELECT oid FROM rh.t_motifs_debut_contrat);
				
				UPDATE rh.t_motifs_debut_contrat SET
					code = c_motifs_debut_contrat.to_code,
					code_original = c_motifs_debut_contrat.to_oid,
					texte = c_motifs_debut_contrat.texte,
					texte_court = c_motifs_debut_contrat.texte_court
				FROM c_motifs_debut_contrat
				WHERE t_motifs_debut_contrat.oid = c_motifs_debut_contrat.to_oid AND
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' AND
					(
						t_motifs_debut_contrat.code = c_motifs_debut_contrat.to_code OR
						t_motifs_debut_contrat.code_original = c_motifs_debut_contrat.to_oid OR
						t_motifs_debut_contrat.texte = c_motifs_debut_contrat.texte OR
						t_motifs_debut_contrat.texte_court = c_motifs_debut_contrat.texte_court
					)
				;
				
				-- motifs_fin_contrat
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_motifs_fin_contrat
					WHERE oid <> 0
					', 'temp t_motifs_fin_contrat'
				);

				DROP TABLE IF EXISTS c_motifs_fin_contrat;
				CREATE TEMP TABLE c_motifs_fin_contrat AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex,
					''::text AS to_code,
					''::text AS to_texte,
					0::bigint AS to_oid
				FROM t_motifs_fin_contrat;

				DROP TABLE IF EXISTS w_motifs_fin_contrat_to;
				CREATE TEMP TABLE w_motifs_fin_contrat_to AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex
				FROM rh.t_motifs_fin_contrat
				WHERE (SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0';

				UPDATE c_motifs_fin_contrat
				SET to_oid = oid + [IPX],
					to_code = code,
					to_texte = texte
				WHERE code NOT IN (SELECT code FROM w_motifs_fin_contrat_to);
				
				UPDATE c_motifs_fin_contrat
				SET to_oid = w_motifs_fin_contrat_to.oid,
					to_code = c_motifs_fin_contrat.code,
					to_texte = c_motifs_fin_contrat.texte
				FROM w_motifs_fin_contrat_to
				WHERE to_oid = 0 AND
					c_motifs_fin_contrat.code = w_motifs_fin_contrat_to.code AND
					c_motifs_fin_contrat.texte_soundex = w_motifs_fin_contrat_to.texte_soundex;

				UPDATE c_motifs_fin_contrat
				SET to_oid = oid + [IPX],
					to_code = '[CPX]' || code,
					to_texte = texte
				WHERE to_oid = 0 OR
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0';

				UPDATE rh.t_motifs_fin_contrat SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_motifs_fin_contrat SET code_original = oid WHERE code_original <> oid::text;

				INSERT INTO rh.t_motifs_fin_contrat(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					to_oid,
					to_oid AS code_original, 
					to_code,
					texte, 
					texte_court
				FROM c_motifs_fin_contrat
				WHERE oid <> 0 AND to_oid NOT IN (SELECT oid FROM rh.t_motifs_fin_contrat);
				
				UPDATE rh.t_motifs_fin_contrat SET
					code = c_motifs_fin_contrat.to_code,
					code_original = c_motifs_fin_contrat.to_oid,
					texte = c_motifs_fin_contrat.texte,
					texte_court = c_motifs_fin_contrat.texte_court
				FROM c_motifs_fin_contrat
				WHERE t_motifs_fin_contrat.oid = c_motifs_fin_contrat.to_oid AND
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' AND
					(
						t_motifs_fin_contrat.code = c_motifs_fin_contrat.to_code OR
						t_motifs_fin_contrat.code_original = c_motifs_fin_contrat.to_oid OR
						t_motifs_fin_contrat.texte = c_motifs_fin_contrat.texte OR
						t_motifs_fin_contrat.texte_court = c_motifs_fin_contrat.texte_court
					)
				;
				
				
				-- nationalites
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_nationalites
					WHERE oid <> 0
					', 'temp t_nationalites'
				);

				DROP TABLE IF EXISTS c_nationalites;
				CREATE TEMP TABLE c_nationalites AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex,
					''::text AS to_code,
					0::bigint AS to_oid
				FROM t_nationalites;

				DROP TABLE IF EXISTS w_nationalites_to;
				CREATE TEMP TABLE w_nationalites_to AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex
				FROM rh.t_nationalites
				;

				UPDATE c_nationalites
				SET to_oid = oid + [IPX],
					to_code = code
				WHERE code NOT IN (SELECT code FROM w_nationalites_to);
				
				UPDATE c_nationalites
				SET to_oid = w_nationalites_to.oid,
					to_code = c_nationalites.code
				FROM w_nationalites_to
				WHERE to_oid = 0 AND
					c_nationalites.code = w_nationalites_to.code AND
					c_nationalites.texte_soundex = w_nationalites_to.texte_soundex;

				UPDATE c_nationalites
				SET to_oid = oid + [IPX],
					to_code = '[CPX]' || code
				WHERE to_oid = 0;

				ALTER TABLE c_nationalites ADD CONSTRAINT c_nationalites_pkey PRIMARY KEY(oid);
				
				UPDATE rh.t_nationalites SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_nationalites SET code_original = oid WHERE code_original <> oid::text;

				INSERT INTO rh.t_nationalites(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					to_oid,
					to_oid AS code_original, 
					to_code,
					texte, 
					texte_court
				FROM c_nationalites
				WHERE oid <> 0 AND to_oid NOT IN (SELECT oid FROM rh.t_nationalites);
				
				-- qualifications
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_qualifications
					WHERE oid <> 0
					', 'temp t_qualifications'
				);

				DROP TABLE IF EXISTS c_qualifications;
				CREATE TEMP TABLE c_qualifications AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex,
					''::text AS to_code,
					''::text AS to_texte,
					0::bigint AS to_oid
				FROM t_qualifications;

				DROP TABLE IF EXISTS w_qualifications_to;
				CREATE TEMP TABLE w_qualifications_to AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex
				FROM rh.t_qualifications
				WHERE (SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0';

				UPDATE c_qualifications
				SET to_oid = oid + [IPX],
					to_code = code,
					to_texte = texte_court
				WHERE code NOT IN (SELECT code FROM w_qualifications_to);
				
				UPDATE c_qualifications
				SET to_oid = w_qualifications_to.oid,
					to_code = w_qualifications_to.code,
					to_texte = w_qualifications_to.texte_court
				FROM w_qualifications_to
				WHERE to_oid = 0 AND
					c_qualifications.code = w_qualifications_to.code AND
					c_qualifications.texte_soundex = w_qualifications_to.texte_soundex;

				UPDATE c_qualifications
				SET to_oid = oid + [IPX],
					to_code = '[CPX]' || code,
					to_texte = texte_court
				WHERE to_oid = 0 OR
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0';
				
				ALTER TABLE c_qualifications ADD CONSTRAINT c_qualifications_pkey PRIMARY KEY(oid);
				
				UPDATE rh.t_qualifications SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_qualifications SET code_original = oid WHERE code_original <> oid::text;

				INSERT INTO rh.t_qualifications(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					to_oid,
					to_oid AS code_original, 
					to_code,
					texte, 
					texte_court
				FROM c_qualifications
				WHERE oid <> 0 AND to_oid NOT IN (SELECT oid FROM rh.t_qualifications);
				
				UPDATE rh.t_qualifications SET
					code = c_qualifications.to_code,
					code_original = c_qualifications.to_oid,
					texte = c_qualifications.texte,
					texte_court = c_qualifications.texte_court
				FROM c_qualifications
				WHERE t_qualifications.oid = c_qualifications.to_oid AND
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' AND
					(
						t_qualifications.code = c_qualifications.to_code OR
						t_qualifications.code_original = c_qualifications.to_oid OR
						t_qualifications.texte = c_qualifications.texte OR
						t_qualifications.texte_court = c_qualifications.texte_court
					)
				;
				
				
				
				
				-- sections_analytiques
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_sections_analytiques
					WHERE oid <> 0
					', 'temp t_sections_analytiques'
				);

				DROP TABLE IF EXISTS c_sections_analytiques;
				CREATE TEMP TABLE c_sections_analytiques AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex,
					''::text AS to_code,
					''::text AS to_texte,
					0::bigint AS to_oid
				FROM t_sections_analytiques;

				DROP TABLE IF EXISTS w_sections_analytiques_to;
				CREATE TEMP TABLE w_sections_analytiques_to AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex
				FROM rh.t_sections_analytiques
				WHERE (SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0';

				UPDATE c_sections_analytiques
				SET to_oid = oid + [IPX],
					to_code = code,
					to_texte = texte_court
				WHERE code NOT IN (SELECT code FROM w_sections_analytiques_to);
				
				UPDATE c_sections_analytiques
				SET to_oid = w_sections_analytiques_to.oid,
					to_code = w_sections_analytiques_to.code,
					to_texte = w_sections_analytiques_to.texte_court
				FROM w_sections_analytiques_to
				WHERE to_oid = 0 AND
					c_sections_analytiques.code = w_sections_analytiques_to.code AND
					c_sections_analytiques.texte_soundex = w_sections_analytiques_to.texte_soundex;


				UPDATE c_sections_analytiques
				SET to_oid = oid + [IPX],
					to_code = '[CPX]' || code,
					to_texte = texte_court
				WHERE to_oid = 0 OR
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0';

				ALTER TABLE c_sections_analytiques ADD CONSTRAINT c_sections_analytiques_pkey PRIMARY KEY(oid);
				
				UPDATE rh.t_sections_analytiques SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_sections_analytiques SET code_original = oid WHERE code_original <> oid::text;

				INSERT INTO rh.t_sections_analytiques(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					to_oid,
					to_oid AS code_original, 
					to_code,
					texte, 
					texte_court
				FROM c_sections_analytiques
				WHERE oid <> 0 AND to_oid NOT IN (SELECT oid FROM rh.t_sections_analytiques);
				
				UPDATE rh.t_sections_analytiques SET
					code = c_sections_analytiques.to_code,
					code_original = c_sections_analytiques.to_oid,
					texte = c_sections_analytiques.texte,
					texte_court = c_sections_analytiques.texte_court
				FROM c_sections_analytiques
				WHERE t_sections_analytiques.oid = c_sections_analytiques.to_oid AND
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' AND
					(
						t_sections_analytiques.code = c_sections_analytiques.to_code OR
						t_sections_analytiques.code_original = c_sections_analytiques.to_oid OR
						t_sections_analytiques.texte = c_sections_analytiques.texte OR
						t_sections_analytiques.texte_court = c_sections_analytiques.texte_court
					)
				;

				-- sections_analytiques paie
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_sections_analytiques_paie
					WHERE oid <> 0
					', 'temp t_sections_analytiques_paie'
				);

				DROP TABLE IF EXISTS c_sections_analytiques_paie;
				CREATE TEMP TABLE c_sections_analytiques_paie AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex,
					''::text AS to_code,
					''::text AS to_texte,
					0::bigint AS to_oid
				FROM t_sections_analytiques_paie;

				DROP TABLE IF EXISTS w_sections_analytiques_paie_to;
				CREATE TEMP TABLE w_sections_analytiques_paie_to AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex
				FROM rh.t_sections_analytiques_paie
				WHERE (SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0';

				UPDATE c_sections_analytiques_paie
				SET to_oid = oid + [IPX],
					to_code = code,
					to_texte = texte_court
				WHERE code NOT IN (SELECT code FROM w_sections_analytiques_paie_to);
				
				UPDATE c_sections_analytiques_paie
				SET to_oid = w_sections_analytiques_paie_to.oid,
					to_code = w_sections_analytiques_paie_to.code,
					to_texte = w_sections_analytiques_paie_to.texte_court
				FROM w_sections_analytiques_paie_to
				WHERE to_oid = 0 AND
					c_sections_analytiques_paie.code = w_sections_analytiques_paie_to.code AND
					c_sections_analytiques_paie.texte_soundex = w_sections_analytiques_paie_to.texte_soundex;


				UPDATE c_sections_analytiques_paie
				SET to_oid = oid + [IPX],
					to_code = '[CPX]' || code,
					to_texte = texte_court
				WHERE to_oid = 0 OR
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0';

				ALTER TABLE c_sections_analytiques_paie ADD CONSTRAINT c_sections_analytiques_paie_pkey PRIMARY KEY(oid);
				
				UPDATE rh.t_sections_analytiques_paie SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_sections_analytiques_paie SET code_original = oid WHERE code_original <> oid::text;

				INSERT INTO rh.t_sections_analytiques_paie(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					to_oid,
					to_oid AS code_original, 
					to_code,
					texte, 
					texte_court
				FROM c_sections_analytiques_paie
				WHERE oid <> 0 AND to_oid NOT IN (SELECT oid FROM rh.t_sections_analytiques_paie);
				
				UPDATE rh.t_sections_analytiques_paie SET
					code = c_sections_analytiques_paie.to_code,
					code_original = c_sections_analytiques_paie.to_oid,
					texte = c_sections_analytiques_paie.texte,
					texte_court = c_sections_analytiques_paie.texte_court
				FROM c_sections_analytiques_paie
				WHERE t_sections_analytiques_paie.oid = c_sections_analytiques_paie.to_oid AND
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' AND
					(
						t_sections_analytiques_paie.code = c_sections_analytiques_paie.to_code OR
						t_sections_analytiques_paie.code_original = c_sections_analytiques_paie.to_oid OR
						t_sections_analytiques_paie.texte = c_sections_analytiques_paie.texte OR
						t_sections_analytiques_paie.texte_court = c_sections_analytiques_paie.texte_court
					)
				;
				
				
				
				-- services
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_services
					WHERE oid <> 0
					', 'temp t_services'
				);
				
				DROP TABLE IF EXISTS c_services;
				CREATE TEMP TABLE c_services AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex,
					''::text AS to_code,
					''::text AS to_texte,
					0::bigint AS to_oid
				FROM t_services;

				DROP TABLE IF EXISTS w_services_to;
				CREATE TEMP TABLE w_services_to AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex
				FROM rh.t_services
				WHERE (SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0';

				UPDATE c_services
				SET to_oid = oid + [IPX],
					to_code = code,
					to_texte = texte_court
				WHERE code NOT IN (SELECT code FROM w_services_to);
				
				UPDATE c_services
				SET to_oid = w_services_to.oid,
					to_code = w_services_to.code,
					to_texte = w_services_to.texte_court
				FROM w_services_to
				WHERE to_oid = 0 AND
					c_services.code = w_services_to.code AND
					c_services.texte_soundex = w_services_to.texte_soundex;

				UPDATE c_services
				SET to_oid = oid + [IPX],
					to_code = '[CPX]' || code,
					to_texte = texte_court
				WHERE to_oid = 0 OR
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0';
				
				ALTER TABLE c_services ADD CONSTRAINT c_services_pkey PRIMARY KEY(oid);
				
				UPDATE rh.t_services SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_services SET code_original = oid WHERE code_original <> oid::text;

				INSERT INTO rh.t_services(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					oid + [IPX] AS to_oid,
					oid + [IPX] AS code_original, 
					'[CPX]' || code AS code,
					texte, 
					texte_court
				FROM t_services
				WHERE oid <> 0 AND oid + [IPX] NOT IN (SELECT oid FROM rh.t_services);
				
				UPDATE rh.t_services SET
					code = c_services.to_code,
					code_original = c_services.to_oid,
					texte = c_services.texte,
					texte_court = c_services.texte_court
				FROM c_services
				WHERE t_services.oid = c_services.to_oid AND
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' AND
					(
						t_services.code = c_services.to_code OR
						t_services.code_original = c_services.to_oid OR
						t_services.texte = c_services.texte OR
						t_services.texte_court = c_services.texte_court
					)
				;
				
				
				
				
				-- situations_famille
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_situations_famille
					WHERE oid <> 0
					', 'temp t_situations_famille'
				);

				DROP TABLE IF EXISTS c_situations_famille;
				CREATE TEMP TABLE c_situations_famille AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex,
					''::text AS to_code,
					0::bigint AS to_oid
				FROM t_situations_famille;

				DROP TABLE IF EXISTS w_situations_famille_to;
				CREATE TEMP TABLE w_situations_famille_to AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex
				FROM rh.t_situations_famille;

				UPDATE c_situations_famille
				SET to_oid = oid + [IPX],
					to_code = code
				WHERE code NOT IN (SELECT code FROM w_situations_famille_to);
				
				UPDATE c_situations_famille
				SET to_oid = w_situations_famille_to.oid,
					to_code = c_situations_famille.code
				FROM w_situations_famille_to
				WHERE to_oid = 0 AND
					c_situations_famille.code = w_situations_famille_to.code AND
					c_situations_famille.texte_soundex = w_situations_famille_to.texte_soundex;

				UPDATE c_situations_famille
				SET to_oid = oid + [IPX],
					to_code = '[CPX]' || code
				WHERE to_oid = 0;

				ALTER TABLE c_situations_famille ADD CONSTRAINT c_situations_famille_pkey PRIMARY KEY(oid);
				
				UPDATE rh.t_situations_famille SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_situations_famille SET code_original = oid WHERE code_original <> oid::text;

				INSERT INTO rh.t_situations_famille(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					to_oid,
					to_oid AS code_original, 
					to_code,
					texte, 
					texte_court
				FROM c_situations_famille
				WHERE oid <> 0 AND to_oid NOT IN (SELECT oid FROM rh.t_situations_famille);
				
				
				-- specialites
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_specialites
					WHERE oid <> 0
					', 'temp t_specialites'
				);

				DROP TABLE IF EXISTS c_specialites;
				CREATE TEMP TABLE c_specialites AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex,
					''::text AS to_code,
					''::text AS to_texte,
					0::bigint AS to_oid
				FROM t_specialites;

				DROP TABLE IF EXISTS w_specialites_to;
				CREATE TEMP TABLE w_specialites_to AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex
				FROM rh.t_specialites
				WHERE (SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0';

				UPDATE c_specialites
				SET to_oid = oid + [IPX],
					to_code = code,
					to_texte = texte_court
				WHERE code NOT IN (SELECT code FROM w_specialites_to);
				
				UPDATE c_specialites
				SET to_oid = w_specialites_to.oid,
					to_code = w_specialites_to.code,
					to_texte = w_specialites_to.texte
				FROM w_specialites_to
				WHERE to_oid = 0 AND
					c_specialites.code = w_specialites_to.code AND
					c_specialites.texte_soundex = w_specialites_to.texte_soundex;

				UPDATE c_specialites
				SET to_oid = oid + [IPX],
					to_code = '[CPX]' || code,
					to_texte = texte_court
				WHERE to_oid = 0 OR
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0';

				ALTER TABLE c_specialites ADD CONSTRAINT c_specialites_pkey PRIMARY KEY(oid);
				
				UPDATE rh.t_specialites SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_specialites SET code_original = oid WHERE code_original <> oid::text;

				INSERT INTO rh.t_specialites(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					to_oid,
					to_oid AS code_original, 
					to_code,
					texte, 
					texte_court
				FROM c_specialites
				WHERE oid <> 0 AND to_oid NOT IN (SELECT oid FROM rh.t_specialites);
				
				UPDATE rh.t_specialites SET
					code = c_specialites.to_code,
					code_original = c_specialites.to_oid,
					texte = c_specialites.texte,
					texte_court = c_specialites.texte_court
				FROM c_specialites
				WHERE t_specialites.oid = c_specialites.to_oid AND
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' AND
					(
						t_specialites.code = c_specialites.to_code OR
						t_specialites.code_original = c_specialites.to_oid OR
						t_specialites.texte = c_specialites.texte OR
						t_specialites.texte_court = c_specialites.texte_court
					)
				;
				
				-- types_contrat
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_types_contrat
					WHERE oid <> 0
					', 'temp t_types_contrat'
				);

				DROP TABLE IF EXISTS c_types_contrat;
				CREATE TEMP TABLE c_types_contrat AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex,
					''::text AS to_code,
					''::text AS to_texte,
					0::bigint AS to_oid
				FROM t_types_contrat;

				DROP TABLE IF EXISTS w_types_contrat_to;
				CREATE TEMP TABLE w_types_contrat_to AS
				SELECT
					*,
					base.cti_soundex_nom(texte) AS texte_soundex
				FROM rh.t_types_contrat
				WHERE (SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0';

				UPDATE c_types_contrat
				SET to_oid = oid + [IPX],
					to_code = code,
					to_texte = texte_court
				WHERE code NOT IN (SELECT code FROM w_types_contrat_to);
				
				UPDATE c_types_contrat
				SET to_oid = w_types_contrat_to.oid,
					to_code = w_types_contrat_to.code,
					to_texte = w_types_contrat_to.texte_court
				FROM w_types_contrat_to
				WHERE to_oid = 0 AND
					c_types_contrat.code = w_types_contrat_to.code AND
					c_types_contrat.texte_soundex = w_types_contrat_to.texte_soundex;

				UPDATE c_types_contrat
				SET to_oid = oid + [IPX],
					to_code = '[CPX]' || code,
					to_texte = texte_court
				WHERE to_oid = 0 OR
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0';
				
				ALTER TABLE c_types_contrat ADD CONSTRAINT c_types_contrat_pkey PRIMARY KEY(oid);

				UPDATE rh.t_types_contrat SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_types_contrat SET code_original = oid WHERE code_original <> oid::text;
				
				INSERT INTO rh.t_types_contrat(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					to_oid,
					to_oid AS code_original, 
					to_code,
					texte, 
					texte_court
				FROM c_types_contrat
				WHERE oid <> 0 AND to_oid NOT IN (SELECT oid FROM rh.t_types_contrat);
				
				UPDATE rh.t_types_contrat SET
					code = c_types_contrat.to_code,
					code_original = c_types_contrat.to_oid,
					texte = c_types_contrat.texte,
					texte_court = c_types_contrat.texte_court
				FROM c_types_contrat
				WHERE t_types_contrat.oid = c_types_contrat.to_oid AND
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' AND
					(
						t_types_contrat.code = c_types_contrat.to_code OR
						t_types_contrat.code_original = c_types_contrat.to_oid OR
						t_types_contrat.texte = c_types_contrat.texte OR
						t_types_contrat.texte_court = c_types_contrat.texte_court
					)
				;
				
				
                -- Types de temps de travail.
                SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
                    SELECT *
                    FROM rh.t_types_temps_travail
                    WHERE oid != 0
                    ', 'temp t_types_temps_travail')
                ;
                
                DROP TABLE IF EXISTS c_types_temps_travail
                ;
                CREATE TEMP TABLE c_types_temps_travail AS
                    SELECT
                        *,
                        code AS texte_soundex,
                        ''::text AS to_code,
                        ''::text AS to_texte,
                        0::bigint AS to_oid
                    FROM t_types_temps_travail
                ;

                DROP TABLE IF EXISTS w_types_temps_travail_to
                ;
                CREATE TEMP TABLE w_types_temps_travail_to AS
                    SELECT
                        *,
                        code AS texte_soundex
                    FROM rh.t_types_temps_travail
					WHERE (SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0'
                ;

                UPDATE c_types_temps_travail SET
                    to_oid = oid + [IPX],
                    to_code = code,
                    to_texte = texte
                WHERE code NOT IN (SELECT code FROM w_types_temps_travail_to)
                ;

                UPDATE c_types_temps_travail SET
                    to_oid = w_types_temps_travail_to.oid,
                    to_code = w_types_temps_travail_to.code,
                    to_texte = w_types_temps_travail_to.texte
                FROM w_types_temps_travail_to
                WHERE 1=1
                    AND to_oid = 0
                    AND c_types_temps_travail.code = w_types_temps_travail_to.code
                    AND c_types_temps_travail.texte_soundex = w_types_temps_travail_to.texte_soundex
                ;

                UPDATE c_types_temps_travail SET
                    to_oid = oid + [IPX],
                    to_code = '[CPX]' || code,
                    to_texte = texte_court
                WHERE to_oid = 0 OR
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0'
                ;

                ALTER TABLE c_types_temps_travail ADD CONSTRAINT c_types_temps_travail_pkey PRIMARY KEY(oid)
                ;
                
                UPDATE rh.t_types_temps_travail SET
                    code_original = '#'||oid
                WHERE code_original != oid::text
                ;
                UPDATE rh.t_types_temps_travail SET
                    code_original = oid
                WHERE code_original != oid::text
                ;

                INSERT INTO rh.t_types_temps_travail(
                    oid,
                    code_original,
                    code,
                    texte,
                    texte_court)
                    SELECT
                        to_oid,
                        to_oid AS code_original,
                        to_code,
                        texte,
                        texte_court
                    FROM c_types_temps_travail
                    WHERE 1=1
                        AND oid != 0
                        AND to_oid NOT IN (SELECT oid FROM rh.t_types_temps_travail)
                ;
				
				UPDATE rh.t_types_temps_travail SET
					code = c_types_temps_travail.to_code,
					code_original = c_types_temps_travail.to_oid,
					texte = c_types_temps_travail.texte,
					texte_court = c_types_temps_travail.texte_court
				FROM c_types_temps_travail
				WHERE t_types_temps_travail.oid = c_types_temps_travail.to_oid AND
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' AND
					(
						t_types_temps_travail.code = c_types_temps_travail.to_code OR
						t_types_temps_travail.code_original = c_types_temps_travail.to_oid OR
						t_types_temps_travail.texte = c_types_temps_travail.texte OR
						t_types_temps_travail.texte_court = c_types_temps_travail.texte_court
					)
				;

				-- types_horaire
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_types_horaire
					WHERE oid <> 0
					', 'temp t_types_horaire'
				);
				
				
				DROP TABLE IF EXISTS c_types_horaire;
				CREATE TEMP TABLE c_types_horaire AS
				SELECT
					*,
					code AS texte_soundex,
					''::text AS to_code,
					''::text AS to_texte,
					0::bigint AS to_oid
				FROM t_types_horaire;


				DROP TABLE IF EXISTS w_types_horaire_to;
				CREATE TEMP TABLE w_types_horaire_to AS
				SELECT
					*,
					code AS texte_soundex
				FROM rh.t_types_horaire
				WHERE (SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0';


				UPDATE c_types_horaire
				SET to_oid = oid + [IPX],
					to_code = code,
					to_texte = texte
				WHERE code NOT IN (SELECT code FROM w_types_horaire_to);
				

				UPDATE c_types_horaire
				SET to_oid = w_types_horaire_to.oid,
					to_code = w_types_horaire_to.code,
					to_texte = w_types_horaire_to.texte
				FROM w_types_horaire_to
				WHERE to_oid = 0 AND
					c_types_horaire.code = w_types_horaire_to.code AND
					c_types_horaire.texte_soundex = w_types_horaire_to.texte_soundex;

				UPDATE c_types_horaire
				SET to_oid = oid + [IPX],
					to_code = '[CPX]' || code,
					to_texte = texte_court
				WHERE to_oid = 0 OR
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0';

				ALTER TABLE c_types_horaire ADD CONSTRAINT c_types_horaire_pkey PRIMARY KEY(oid);
				
				UPDATE rh.t_types_horaire SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_types_horaire SET code_original = oid WHERE code_original <> oid::text;

				INSERT INTO rh.t_types_horaire(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					to_oid,
					to_oid AS code_original, 
					to_code,
					texte, 
					texte_court
				FROM c_types_horaire
				WHERE oid <> 0 AND to_oid NOT IN (SELECT oid FROM rh.t_types_horaire);
				
				UPDATE rh.t_types_horaire SET
					code = c_types_horaire.to_code,
					code_original = c_types_horaire.to_oid,
					texte = c_types_horaire.texte,
					texte_court = c_types_horaire.texte_court
				FROM c_types_horaire
				WHERE t_types_horaire.oid = c_types_horaire.to_oid AND
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' AND
					(
						t_types_horaire.code = c_types_horaire.to_code OR
						t_types_horaire.code_original = c_types_horaire.to_oid OR
						t_types_horaire.texte = c_types_horaire.texte OR
						t_types_horaire.texte_court = c_types_horaire.texte_court
					)
				;

            -- Grilles
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_grilles
					WHERE oid <> 0
					', 'temp t_grilles'
				);
				
				
				DROP TABLE IF EXISTS c_grilles;
				CREATE TEMP TABLE c_grilles AS
				SELECT
					*,
					code AS texte_soundex,
					''::text AS to_code,
					''::text AS to_texte,
					0::bigint AS to_oid
				FROM t_grilles;


				DROP TABLE IF EXISTS w_grilles_to;
				CREATE TEMP TABLE w_grilles_to AS
				SELECT
					*,
					code AS texte_soundex
				FROM rh.t_grilles
				WHERE (SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0';


				UPDATE c_grilles
				SET to_oid = oid + [IPX],
					to_code = code,
					to_texte = texte
				WHERE code NOT IN (SELECT code FROM w_grilles_to);
				

				UPDATE c_grilles
				SET to_oid = w_grilles_to.oid,
					to_code = w_grilles_to.code,
					to_texte = w_grilles_to.texte
				FROM w_grilles_to
				WHERE to_oid = 0 AND
					c_grilles.code = w_grilles_to.code AND
					c_grilles.texte_soundex = w_grilles_to.texte_soundex;

				UPDATE c_grilles
				SET to_oid = oid + [IPX],
					to_code = '[CPX]' || code,
					to_texte = texte_court
				WHERE to_oid = 0 OR
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0';

				ALTER TABLE c_grilles ADD CONSTRAINT c_grilles_pkey PRIMARY KEY(oid);
				
				UPDATE rh.t_grilles SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_grilles SET code_original = oid WHERE code_original <> oid::text;

				INSERT INTO rh.t_grilles(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					to_oid,
					to_oid AS code_original, 
					to_code,
					texte, 
					texte_court
				FROM c_grilles
				WHERE oid <> 0 AND to_oid NOT IN (SELECT oid FROM rh.t_grilles);
				
				UPDATE rh.t_grilles SET
					code = c_grilles.to_code,
					code_original = c_grilles.to_oid,
					texte = c_grilles.texte,
					texte_court = c_grilles.texte_court
				FROM c_grilles
				WHERE t_grilles.oid = c_grilles.to_oid AND
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' AND
					(
						t_grilles.code = c_grilles.to_code OR
						t_grilles.code_original = c_grilles.to_oid OR
						t_grilles.texte = c_grilles.texte OR
						t_grilles.texte_court = c_grilles.texte_court
					)
				;
				
                -- Groupes de grille.
				
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM rh.t_grilles_groupes
					WHERE oid <> 0
					', 'temp t_grilles_groupes'
				);
				
				
				DROP TABLE IF EXISTS c_grilles_groupes;
				CREATE TEMP TABLE c_grilles_groupes AS
				SELECT
					*,
					code AS texte_soundex,
					''::text AS to_code,
					''::text AS to_texte,
					0::bigint AS to_oid
				FROM t_grilles_groupes;


				DROP TABLE IF EXISTS w_grilles_groupes_to;
				CREATE TEMP TABLE w_grilles_groupes_to AS
				SELECT
					*,
					code AS texte_soundex
				FROM rh.t_grilles_groupes
				WHERE (SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0';


				UPDATE c_grilles_groupes
				SET to_oid = oid + [IPX],
					to_code = code,
					to_texte = texte
				WHERE code NOT IN (SELECT code FROM w_grilles_groupes_to);
				

				UPDATE c_grilles_groupes
				SET to_oid = w_grilles_groupes_to.oid,
					to_code = w_grilles_groupes_to.code,
					to_texte = w_grilles_groupes_to.texte
				FROM w_grilles_groupes_to
				WHERE to_oid = 0 AND
					c_grilles_groupes.code = w_grilles_groupes_to.code AND
					c_grilles_groupes.texte_soundex = w_grilles_groupes_to.texte_soundex;

				UPDATE c_grilles_groupes
				SET to_oid = oid + [IPX],
					to_code = '[CPX]' || code,
					to_texte = texte_court
				WHERE to_oid = 0 OR
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0';

				ALTER TABLE c_grilles_groupes ADD CONSTRAINT c_grilles_groupes_pkey PRIMARY KEY(oid);
				
				UPDATE rh.t_grilles_groupes SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE rh.t_grilles_groupes SET code_original = oid WHERE code_original <> oid::text;

				INSERT INTO rh.t_grilles_groupes(
					oid,
					code_original, 
					code, 
					texte, 
					texte_court) 
				SELECT 
					to_oid,
					to_oid AS code_original, 
					to_code,
					texte, 
					texte_court
				FROM c_grilles_groupes
				WHERE oid <> 0 AND to_oid NOT IN (SELECT oid FROM rh.t_grilles_groupes);
				
				UPDATE rh.t_grilles_groupes SET
					code = c_grilles_groupes.to_code,
					code_original = c_grilles_groupes.to_oid,
					texte = c_grilles_groupes.texte,
					texte_court = c_grilles_groupes.texte_court
				FROM c_grilles_groupes
				WHERE t_grilles_groupes.oid = c_grilles_groupes.to_oid AND
					(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' AND
					(
						t_grilles_groupes.code = c_grilles_groupes.to_code OR
						t_grilles_groupes.code_original = c_grilles_groupes.to_oid OR
						t_grilles_groupes.texte = c_grilles_groupes.texte OR
						t_grilles_groupes.texte_court = c_grilles_groupes.texte_court
					)
				;

            -- Statuts
            SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
                SELECT *
                FROM rh.t_statuts
                WHERE oid != 0
                ', 'temp t_statuts')
            ;
            
            DROP TABLE IF EXISTS c_statuts
            ;
            CREATE TEMP TABLE c_statuts AS
                SELECT
                    *,
                    code AS texte_soundex,
                    ''::text AS to_code,
                    ''::text AS to_texte,
                    0::bigint AS to_oid
                FROM t_statuts
            ;

            DROP TABLE IF EXISTS w_statuts_to
            ;
            CREATE TEMP TABLE w_statuts_to AS
                SELECT
                    *,
                    code AS texte_soundex
                FROM rh.t_statuts
				WHERE (SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') IS DISTINCT FROM '0'
            ;

            UPDATE c_statuts SET
                to_oid = oid + [IPX],
                to_code = code,
                to_texte = texte
            WHERE code NOT IN (SELECT code FROM w_statuts_to)
            ;

            UPDATE c_statuts SET
                to_oid = w_statuts_to.oid,
                to_code = w_statuts_to.code,
                to_texte = w_statuts_to.texte
            FROM w_statuts_to
            WHERE 1=1
                AND to_oid = 0
                AND c_statuts.code = w_statuts_to.code
                AND c_statuts.texte_soundex = w_statuts_to.texte_soundex
            ;

            UPDATE c_statuts SET
                to_oid = oid + [IPX],
                to_code = '[CPX]' || code,
                to_texte = texte_court
            WHERE to_oid = 0 OR
				(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' 
            ;

            ALTER TABLE c_statuts ADD CONSTRAINT c_statuts_pkey PRIMARY KEY(oid)
            ;
            
            UPDATE rh.t_statuts SET
                code_original = '#'||oid
            WHERE code_original != oid::text
            ;
            UPDATE rh.t_statuts SET
                code_original = oid
            WHERE code_original != oid::text
            ;

            INSERT INTO rh.t_statuts(
                oid,
                code_original,
                code,
                texte,
                texte_court)
                SELECT
                    to_oid,
                    to_oid AS code_original,
                    to_code,
                    texte,
                    texte_court
                FROM c_statuts
                WHERE 1=1
                    AND oid != 0
                    AND to_oid NOT IN (SELECT oid FROM rh.t_statuts)
            ;
			
			UPDATE rh.t_statuts SET
				code = c_statuts.to_code,
				code_original = c_statuts.to_oid,
				texte = c_statuts.texte,
				texte_court = c_statuts.texte_court
			FROM c_statuts
			WHERE t_statuts.oid = c_statuts.to_oid AND
				(SELECT valeur FROM rh.t_divers WHERE t_divers.code = 'CONSO_FUSIONPARAM') = '0' AND
				(
					t_statuts.code = c_statuts.to_code OR
					t_statuts.code_original = c_statuts.to_oid OR
					t_statuts.texte = c_statuts.texte OR
					t_statuts.texte_court = c_statuts.texte_court
				)
			;

			]]></sqlcmd>
		</NODE>
        <NODE label="Listes et classes (Montant)">
            <condition><![CDATA[
                
                select valeur = 'M'
                from rh.t_divers
                where code = 'SENSPARAMETRE_CLASSE'
                ;

            ]]></condition>
			<sqlcmd><![CDATA[
			
				-- Listes
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT 
                        t_listes.oid,
                        t_listes.code,
                        t_listes_tables.code AS table_code,
                        ''0''::text AS ok_liste
                    FROM rh.t_listes
                        JOIN rh.t_listes_tables ON t_listes.table_id = t_listes_tables.oid
                    WHERE 1=1
                        AND t_listes.oid IN (SELECT liste_id FROM rh.t_listes_contenu)
                        AND contenu_by_code != ''1''
                        AND t_listes.code != ''''
					', 'temp e_listes'
				);
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT * FROM rh.t_listes_contenu
					', 'temp e_listes_contenu'
				);
				
				UPDATE e_listes SET
				    ok_liste = '1'
				WHERE code IN (SELECT code FROM rh.t_listes)
				;

				DELETE FROM rh.t_listes_contenu
				WHERE 1=1
                    AND liste_id IN (
                        SELECT t_listes.oid
                        FROM rh.t_listes
                            JOIN rh.t_listes_tables ON t_listes.table_id = t_listes_tables.oid
                            JOIN e_listes ON 1=1
                                AND t_listes.code = e_listes.code
                                AND t_listes_tables.code = table_code
                        WHERE ok_liste = '1')
					AND (
						to_id BETWEEN [IPX]::bigint AND ([IPX]::bigint + 99999999999::bigint) OR
						to_id BETWEEN (100000000000 + [IPX]::bigint) AND (100000000000 + [IPX]::bigint + 99999999999::bigint) OR
						to_id BETWEEN (200000000000 + [IPX]::bigint) AND (200000000000 + [IPX]::bigint + 99999999999::bigint) 
						)
				;
				
				INSERT INTO rh.t_listes_contenu (liste_id, to_id)
                    SELECT 
                        t_listes.oid,
                        [IPX] + to_id
                    FROM e_listes_contenu
                        JOIN e_listes ON e_listes_contenu.liste_id = e_listes.oid
                        JOIN rh.t_listes ON t_listes.code = e_listes.code
                        JOIN rh.t_listes_tables ON 1=1
                            AND t_listes.table_id = t_listes_tables.oid
                            AND t_listes_tables.code = e_listes.table_code
                    WHERE ok_liste = '1'
				;
				
				-- Classes
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT 
                        t_classes.oid,
                        t_classes.code,
                        t_listes_tables.code AS table_code,
                        ''0''::text AS ok_classe
                    FROM rh.t_classes
                        JOIN rh.t_listes_tables ON t_classes.table_id = t_listes_tables.oid
                    WHERE 1=1
                        AND t_classes.oid IN (
                            SELECT classe_id 
                            FROM rh.t_classes_sections 
                                JOIN rh.t_classes_sections_elements ON section_id = t_classes_sections.oid)
                        AND contenu_by_code != ''1''
					', 'temp e_classes'
				);

				UPDATE e_classes SET
					ok_classe = '1'
                WHERE code IN (SELECT code FROM rh.t_classes)
                ;

				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT 
                        classe_id,
                        t_classes_sections.oid,
                        t_classes_sections.code,
                        t_classes_sections.texte
                    FROM rh.t_classes_sections
					', 'temp e_classes_sections'
				);

				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT 
                        section_id,
                        t_classes_sections_elements.to_id
                    FROM rh.t_classes_sections_elements
					', 'temp e_classes_sections_elements'
				);

                INSERT INTO rh.t_classes_sections (code, texte, classe_id)
                    SELECT
                        e_classes_sections.code,
                        e_classes_sections.texte,
                        t_classes.oid
                    FROM e_classes_sections
                        JOIN e_classes ON 1=1
                            AND e_classes_sections.classe_id = e_classes.oid
                            AND e_classes.ok_classe = '1'
                        JOIN rh.t_listes_tables ON e_classes.table_code = t_listes_tables.code
                        JOIN rh.t_classes ON 1=1
                            AND e_classes.code = t_classes.code
                            AND t_classes.table_id = t_listes_tables.oid
                        LEFT JOIN rh.t_classes_sections ON 1=1
                            AND t_classes.oid = t_classes_sections.classe_id
                            AND t_classes_sections.code = e_classes_sections.code
                    WHERE t_classes_sections.oid IS NULL
                ;

				DELETE FROM rh.t_classes_sections_elements
                WHERE 1=1
                    AND section_id IN 
                        (SELECT t_classes_sections.oid
                        FROM rh.t_classes
                            JOIN rh.t_listes_tables ON t_classes.table_id = t_listes_tables.oid
                            JOIN e_classes ON 1=1
                                AND t_classes.code = e_classes.code
                                AND t_listes_tables.code = table_code
                            JOIN rh.t_classes_sections ON t_classes_sections.classe_id = t_classes.oid
                        WHERE ok_classe = '1')
					AND (
						to_id BETWEEN [IPX]::bigint AND ([IPX]::bigint + 99999999999::bigint) OR
						to_id BETWEEN (100000000000 + [IPX]::bigint) AND (100000000000 + [IPX]::bigint + 99999999999::bigint) OR
						to_id BETWEEN (200000000000 + [IPX]::bigint) AND (200000000000 + [IPX]::bigint + 99999999999::bigint) 
						)
                ;

				INSERT INTO rh.t_classes_sections_elements (section_id, to_id)
                    SELECT
                        t_classes_sections.oid,
                        [IPX] + to_id
                    FROM e_classes_sections_elements
                        JOIN e_classes_sections ON e_classes_sections_elements.section_id = e_classes_sections.oid
                        JOIN e_classes ON 1=1
                            AND e_classes.oid = e_classes_sections.classe_id
                            AND ok_classe = '1'
                        JOIN rh.t_classes ON e_classes.code = t_classes.code 
                        JOIN rh.t_listes_tables ON 1=1
                            AND t_classes.table_id = t_listes_tables.oid
                            AND e_classes.table_code = t_listes_tables.code
                        JOIN rh.t_classes_sections ON 1=1
                            AND t_classes_sections.classe_id = t_classes.oid
                            AND t_classes_sections.code = e_classes_sections.code
                    GROUP BY 1,2
				;
			
		]]></sqlcmd>
		</NODE>
	</NODE>

	<NODE name="POST" label="TRAITEMENTS COMPLÉMENTAIRES">
		<NODE label="Adaptation des codes">
			<sqlcmd><![CDATA[
				
				-- Les salaries qui ne sont pas en doublon sur plusieurs environnements gardent leur code
				UPDATE rh.p_salaries
				SET matricule = trim(split_part(matricule,'(',1))
				WHERE trim(split_part(matricule,'(',1))
				NOT IN (
					SELECT
						trim(split_part(matricule,'(',1))
					FROM rh.p_salaries
					GROUP BY 1
					HAVING count(*) > 1
				);
				
				-- Les contrats 'unique' sur tous les sites sont remis avec le numero initial
				UPDATE rh.p_contrats
				SET numero_contrat = trim(split_part(numero_contrat,'(',1))
				WHERE trim(split_part(numero_contrat,'(',1))
				NOT IN (
					SELECT
						trim(split_part(numero_contrat,'(',1))
					FROM rh.p_contrats
					GROUP BY 1
					HAVING count(*) > 1
				);
				
			]]></sqlcmd>
		</NODE>
	</NODE>

	<NODE name="DISABLE_INDEX" label="DÉSACTIVATION INDEX">
		<NODE label="Salariés">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_disable_index('rh', 'i_salaries_1');
				SELECT base.cti_disable_index('rh', 'i_salaries_2');
				SELECT base.cti_disable_index('rh', 'i_salaries_3');
				
			]]></sqlcmd>
		</NODE>
		<NODE label="Contrats">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_disable_index('rh', 'i_contrats_1');
				SELECT base.cti_disable_index('rh', 'i_contrats_2');
				SELECT base.cti_disable_index('rh', 'i_contrats_3');
				SELECT base.cti_disable_index('rh', 'i_contrats_4');
				SELECT base.cti_disable_index('rh', 'i_contrats_5');
				SELECT base.cti_disable_index('rh', 'i_contrats_6');
				SELECT base.cti_disable_index('rh', 'i_contrats_7');
				SELECT base.cti_disable_index('rh', 'i_contrats_8');
				SELECT base.cti_disable_index('rh', 'i_contrats_9');
				SELECT base.cti_disable_index('rh', 'i_contrats_10');
				SELECT base.cti_disable_index('rh', 'i_contrats_11');
				SELECT base.cti_disable_index('rh', 'i_contrats_12');
				SELECT base.cti_disable_index('rh', 'i_contrats_13');
				SELECT base.cti_disable_index('rh', 'i_contrats_14');
				SELECT base.cti_disable_index('rh', 'i_contrats_15');
				SELECT base.cti_disable_index('rh', 'i_contrats_16');
				SELECT base.cti_disable_index('rh', 'i_contrats_17');


				SELECT base.cti_disable_index('rh', 'i_contrats_mois_1');
				SELECT base.cti_disable_index('rh', 'i_contrats_mois_2');
				SELECT base.cti_disable_index('rh', 'i_contrats_mois_3');
				SELECT base.cti_disable_index('rh', 'i_contrats_mois_4');

			]]></sqlcmd>
		</NODE>
		<NODE label="Profils">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_disable_index('rh', 'i_profils_1');
				SELECT base.cti_disable_index('rh', 'i_profils_2');
				SELECT base.cti_disable_index('rh', 'i_profils_3');
				SELECT base.cti_disable_index('rh', 'i_profils_4');
				SELECT base.cti_disable_index('rh', 'i_profils_5');
				SELECT base.cti_disable_index('rh', 'i_profils_6');
				SELECT base.cti_disable_index('rh', 'i_profils_7');
				SELECT base.cti_disable_index('rh', 'i_profils_8');
				SELECT base.cti_disable_index('rh', 'i_profils_9');
				SELECT base.cti_disable_index('rh', 'i_profils_10');
				SELECT base.cti_disable_index('rh', 'i_profils_11');
				SELECT base.cti_disable_index('rh', 'i_profils_12');
				SELECT base.cti_disable_index('rh', 'i_profils_13');
				SELECT base.cti_disable_index('rh', 'i_profils_14');
				SELECT base.cti_disable_index('rh', 'i_profils_15');
				SELECT base.cti_disable_index('rh', 'i_profils_16');
				SELECT base.cti_disable_index('rh', 'i_profils_17');

			]]></sqlcmd>
		</NODE>
		<NODE label="Historique de paie">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_disable_index('rh', 'i_historique_paie_1');
				SELECT base.cti_disable_index('rh', 'i_historique_paie_2');
				SELECT base.cti_disable_index('rh', 'i_historique_paie_3');
				SELECT base.cti_disable_index('rh', 'i_historique_paie_4');
				SELECT base.cti_disable_index('rh', 'i_historique_paie_5');
				SELECT base.cti_disable_index('rh', 'i_historique_paie_6');
				SELECT base.cti_disable_index('rh', 'i_historique_paie_7');
				SELECT base.cti_disable_index('rh', 'i_historique_paie_8');
				SELECT base.cti_disable_index('rh', 'i_historique_paie_9');
				SELECT base.cti_disable_index('rh', 'i_historique_paie_10');
				
			]]></sqlcmd>
		</NODE>
	</NODE>

	<NODE name="ENABLE_INDEX" label="RÉACTIVATION INDEX">
		<NODE label="Salariés">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_enable_index('rh', 'i_salaries_1');
				SELECT base.cti_enable_index('rh', 'i_salaries_2');
				SELECT base.cti_enable_index('rh', 'i_salaries_3');
				
			]]></sqlcmd>
		</NODE>
        <NODE label="Contrats">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_enable_index('rh', 'i_contrats_1');
				SELECT base.cti_enable_index('rh', 'i_contrats_2');
				SELECT base.cti_enable_index('rh', 'i_contrats_3');
				SELECT base.cti_enable_index('rh', 'i_contrats_4');
				SELECT base.cti_enable_index('rh', 'i_contrats_5');
				SELECT base.cti_enable_index('rh', 'i_contrats_6');
				SELECT base.cti_enable_index('rh', 'i_contrats_7');
				SELECT base.cti_enable_index('rh', 'i_contrats_8');
				SELECT base.cti_enable_index('rh', 'i_contrats_9');
				SELECT base.cti_enable_index('rh', 'i_contrats_10');
				SELECT base.cti_enable_index('rh', 'i_contrats_11');
				SELECT base.cti_enable_index('rh', 'i_contrats_12');
				SELECT base.cti_enable_index('rh', 'i_contrats_13');
				SELECT base.cti_enable_index('rh', 'i_contrats_14');
				SELECT base.cti_enable_index('rh', 'i_contrats_15');
				SELECT base.cti_enable_index('rh', 'i_contrats_16');
				SELECT base.cti_enable_index('rh', 'i_contrats_17');

				
				SELECT base.cti_enable_index('rh', 'i_contrats_mois_1');
				SELECT base.cti_enable_index('rh', 'i_contrats_mois_2');
				SELECT base.cti_enable_index('rh', 'i_contrats_mois_3');
				SELECT base.cti_enable_index('rh', 'i_contrats_mois_4');

			]]></sqlcmd>
		</NODE>
		<NODE label="Profils">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_enable_index('rh', 'i_profils_1');
				SELECT base.cti_enable_index('rh', 'i_profils_2');
				SELECT base.cti_enable_index('rh', 'i_profils_3');
				SELECT base.cti_enable_index('rh', 'i_profils_4');
				SELECT base.cti_enable_index('rh', 'i_profils_5');
				SELECT base.cti_enable_index('rh', 'i_profils_6');
				SELECT base.cti_enable_index('rh', 'i_profils_7');
				SELECT base.cti_enable_index('rh', 'i_profils_8');
				SELECT base.cti_enable_index('rh', 'i_profils_9');
				SELECT base.cti_enable_index('rh', 'i_profils_10');
				SELECT base.cti_enable_index('rh', 'i_profils_11');
				SELECT base.cti_enable_index('rh', 'i_profils_12');
				SELECT base.cti_enable_index('rh', 'i_profils_13');
				SELECT base.cti_enable_index('rh', 'i_profils_14');
				SELECT base.cti_enable_index('rh', 'i_profils_15');
				SELECT base.cti_enable_index('rh', 'i_profils_16');
				SELECT base.cti_enable_index('rh', 'i_profils_17');

			]]></sqlcmd>
		</NODE>
		<NODE label="Historique de paie">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_enable_index('rh', 'i_historique_paie_1');
				SELECT base.cti_enable_index('rh', 'i_historique_paie_2');
				SELECT base.cti_enable_index('rh', 'i_historique_paie_3');
				SELECT base.cti_enable_index('rh', 'i_historique_paie_4');
				SELECT base.cti_enable_index('rh', 'i_historique_paie_5');
				SELECT base.cti_enable_index('rh', 'i_historique_paie_6');
				SELECT base.cti_enable_index('rh', 'i_historique_paie_7');
				SELECT base.cti_enable_index('rh', 'i_historique_paie_8');
				SELECT base.cti_enable_index('rh', 'i_historique_paie_9');
				SELECT base.cti_enable_index('rh', 'i_historique_paie_10');
				
			]]></sqlcmd>
		</NODE>
	</NODE>

	<NODE name="RAZ" label="RAZ BASE">

		<NODE label="Nettoyage des tables">
			<sqlcmd><![CDATA[

				TRUNCATE rh.p_salaries;

			]]></sqlcmd>
		</NODE>

	</NODE>

</ROOT>