<?xml version="1.0" encoding="ISO-8859-1"?>
<DBSETUPDATA softCode="iCTI_rh" schema="rh">
	<!-- SCHEMA "BASE" -->
	<TABLE name="p_calendrier" label="Calendrier" schema="base" />
	<TABLE name="p_calendrier_mois" label="Calendrier (mois)" schema="base" />
	<TABLE name="t_departements" label="Départements" schema="base" />
	<TABLE name="t_territoires_sante" label="Territoires de santé"	schema="base" />
	<TABLE name="t_codes_geographiques_pmsi" label="Codes géographiques PMSI" schema="base" />
	<TABLE name="t_codes_postaux" label="Codes postaux" schema="base" />

	<!-- SCHEMA "RH" -->
	<TABLE name="t_ages" label="Ages">
		<selectCmd><![CDATA[
		
			SELECT 
				oid AS keepoid, 
				code, 
				texte
			FROM rh.t_ages
			ORDER BY code
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[
		
			UPDATE rh.t_ages SET 
				code = w_dbsetup.code,
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE t_ages.oid = w_dbsetup.keepoid
			;

			INSERT INTO rh.t_ages(oid, code, texte)
				SELECT keepoid, code, texte FROM w_dbsetup WHERE keepoid NOT IN (SELECT oid FROM rh.t_ages)
			;
				
			SELECT * FROM rh.cti_update_schema_classes('AGE')
			;
			
	]]></updateCmd></TABLE>
	<TABLE name="t_anciennetes_annee" label="Ages">
		<selectCmd><![CDATA[
			SELECT 
				oid AS keepoid, 
				code, 
				texte
			FROM rh.t_anciennetes_annee
			ORDER BY code
			;
						
		]]></selectCmd>
		<updateCmd><![CDATA[
		
			UPDATE rh.t_anciennetes_annee SET 
				code = w_dbsetup.code,
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE t_anciennetes_annee.oid = w_dbsetup.keepoid
			;

			INSERT INTO rh.t_anciennetes_annee(oid, code, texte)
				SELECT keepoid, code, texte FROM w_dbsetup WHERE keepoid NOT IN (SELECT oid FROM rh.t_anciennetes_annee)
			;
				
			SELECT * FROM rh.cti_update_schema_classes('AGE')
			;

		]]></updateCmd>
	</TABLE>
	<TABLE name="t_categories_socio_professionnelle" label="Catégories socio-professionelle">
		<selectCmd><![CDATA[
		
			SELECT
				max(oid) AS oid,
				code, 
				texte,
				texte_court
			FROM rh.t_categories_socio_professionnelle
			WHERE 1=1
				AND code != texte
				AND substr(code, 4, 1) !~ '[0-9]'
				AND code NOT IN ('642k', '643d')
			GROUP BY 2,3,4
			ORDER BY code
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[
		
			UPDATE rh.t_categories_socio_professionnelle 
			SET code = w_dbsetup.code
			FROM w_dbsetup
			WHERE 1=1
				AND t_categories_socio_professionnelle.oid = w_dbsetup.oid
				AND w_dbsetup.oid = 0
			;
			 
			UPDATE rh.t_categories_socio_professionnelle
			SET 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court
			FROM w_dbsetup
			WHERE t_categories_socio_professionnelle.code = w_dbsetup.code
			;
			 
			INSERT INTO rh.t_categories_socio_professionnelle(oid, code, texte, texte_court, code_original)
				SELECT oid, code, texte, texte_court, code
				FROM w_dbsetup 
				WHERE 1=1
					AND oid = 0 
					AND oid NOT IN (SELECT oid FROM rh.t_categories_socio_professionnelle)
			;
			
			INSERT INTO rh.t_categories_socio_professionnelle(code, texte, texte_court, code_original)
				SELECT code, texte, texte_court, code
				FROM w_dbsetup
				WHERE 1=1
					AND oid != 0 
					AND code NOT IN (SELECT code FROM rh.t_categories_socio_professionnelle)
			;
				
			SELECT rh.cti_update_schema_classes('CSC')
			;

		]]></updateCmd>
	</TABLE>
	<TABLE name="t_listes_tables" label="Tables pour les listes">
		<selectCmd><![CDATA[
		
			SELECT 
				oid,
				name,
				texte,
				select_cmd,
				code  
			FROM rh.t_listes_tables
			ORDER BY code
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[
		
			UPDATE rh.t_listes_tables SET 
				code = w_dbsetup.code,
				texte = w_dbsetup.texte, 
				select_cmd = w_dbsetup.select_cmd
			FROM w_dbsetup
			WHERE t_listes_tables.name = w_dbsetup.name
			;

			INSERT INTO rh.t_listes_tables(oid, code, name, texte, select_cmd)
				SELECT oid, code, name, texte, select_cmd FROM w_dbsetup WHERE oid = 0 AND oid NOT IN (SELECT oid FROM rh.t_listes_tables)
			;
			
			INSERT INTO rh.t_listes_tables(code, name, texte, select_cmd)
				SELECT code, name, texte, select_cmd FROM w_dbsetup WHERE oid <> 0 AND name NOT IN (SELECT name FROM rh.t_listes_tables)
			;
				
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_listes" label="Listes de codes">
		<selectCmd><![CDATA[
		
			SELECT 
				t_listes.oid,
				t_listes.texte,
				t_listes.chapitre,
				t_listes.code,
				t_listes.is_cti,
				t_listes.show_omit,
				t_listes.utilisateur_createur,
				t_listes.est_partagee_modification,
				t_listes_tables.name as table_name
			FROM
				rh.t_listes,
				rh.t_listes_tables
			WHERE 1=1
				AND is_cti = '1'
				AND t_listes.code != ''
				AND table_id = t_listes_tables.oid
			ORDER BY t_listes.code
			;
				
		]]></selectCmd>
		<updateCmd><![CDATA[
		
			UPDATE rh.t_listes SET 
				texte = w_dbsetup.texte, 
				chapitre = w_dbsetup.chapitre, 
				table_id = t_listes_tables.oid,
				is_cti = w_dbsetup.is_cti,
                show_omit = w_dbsetup.show_omit,
                utilisateur_createur = w_dbsetup.utilisateur_createur,
                est_partagee_modification = w_dbsetup.est_partagee_modification
			FROM w_dbsetup, rh.t_listes_tables
			WHERE  w_dbsetup.table_name = t_listes_tables.name and t_listes.code = w_dbsetup.code
			;

			INSERT INTO rh.t_listes(code, texte, chapitre, table_id, is_cti, show_omit, utilisateur_createur, est_partagee_modification)
			SELECT w_dbsetup.code, w_dbsetup.texte, w_dbsetup.chapitre, t_listes_tables.oid, w_dbsetup.is_cti, w_dbsetup.show_omit, w_dbsetup.utilisateur_createur, w_dbsetup.est_partagee_modification
				FROM w_dbsetup, rh.t_listes_tables 
				WHERE 1=1
                    AND w_dbsetup.table_name = t_listes_tables.name
                    AND w_dbsetup.code NOT IN (SELECT code FROM rh.t_listes)
                    AND w_dbsetup.texte NOT LIKE '*SUPPRIME%'
			;

			DELETE FROM rh.t_listes WHERE texte like '*SUPPRIME%' AND is_cti = '1'
			;

		]]></updateCmd>
	</TABLE>
	<TABLE name="t_indicateurs" label="Indicateurs de rapports">
		<selectCmd><![CDATA[
		
			SELECT 
				t_indicateurs.oid,
				t_indicateurs.code,
				t_indicateurs.texte,
				t_indicateurs.table_name,
				t_indicateurs.column_name,
				t_indicateurs.is_cti,
				t_indicateurs.entete,
				t_indicateurs.total_function,
				t_indicateurs.output_format,
				t_indicateurs.width,
				t_indicateurs.entete_etendue,
				t_indicateurs.dimension_date_1,
				t_indicateurs.dimension_date_2,
				t_indicateurs.dimension_date_3,
				t_indicateurs.dimension_date_4,
				t_indicateurs.dimension_date_5,
				t_indicateurs.dimension_date,
				t_indicateurs_1.code as indicateur_associe_1_code, 
				t_indicateurs_2.code as indicateur_associe_2_code, 
				t_indicateurs_3.code as indicateur_associe_3_code, 
				t_indicateurs_4.code as indicateur_associe_4_code, 
				t_indicateurs_5.code as indicateur_associe_5_code, 
				t_indicateurs_6.code as indicateur_associe_6_code, 
				t_indicateurs_7.code as indicateur_associe_7_code, 
				t_indicateurs_8.code as indicateur_associe_8_code, 
				t_indicateurs_9.code as indicateur_associe_9_code, 
				t_indicateurs_10.code as indicateur_associe_10_code, 
				t_indicateurs_11.code as indicateur_associe_11_code, 
				t_indicateurs_12.code as indicateur_associe_12_code, 
				t_indicateurs_13.code as indicateur_associe_13_code, 
				t_indicateurs_14.code as indicateur_associe_14_code, 
				t_indicateurs_15.code as indicateur_associe_15_code, 
				t_indicateurs_16.code as indicateur_associe_16_code, 
				t_indicateurs_17.code as indicateur_associe_17_code, 
				t_indicateurs_18.code as indicateur_associe_18_code, 
				t_indicateurs_19.code as indicateur_associe_19_code, 
				t_indicateurs_20.code as indicateur_associe_20_code,
				t_indicateurs_reference.code as indicateur_reference_code
			FROM rh.t_indicateurs 
				LEFT JOIN rh.t_indicateurs t_indicateurs_1 ON (t_indicateurs.indicateur_associe_1_id = t_indicateurs_1.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_2 ON (t_indicateurs.indicateur_associe_2_id = t_indicateurs_2.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_3 ON (t_indicateurs.indicateur_associe_3_id = t_indicateurs_3.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_4 ON (t_indicateurs.indicateur_associe_4_id = t_indicateurs_4.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_5 ON (t_indicateurs.indicateur_associe_5_id = t_indicateurs_5.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_6 ON (t_indicateurs.indicateur_associe_6_id = t_indicateurs_6.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_7 ON (t_indicateurs.indicateur_associe_7_id = t_indicateurs_7.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_8 ON (t_indicateurs.indicateur_associe_8_id = t_indicateurs_8.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_9 ON (t_indicateurs.indicateur_associe_9_id = t_indicateurs_9.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_10 ON (t_indicateurs.indicateur_associe_10_id = t_indicateurs_10.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_11 ON (t_indicateurs.indicateur_associe_11_id = t_indicateurs_11.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_12 ON (t_indicateurs.indicateur_associe_12_id = t_indicateurs_12.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_13 ON (t_indicateurs.indicateur_associe_13_id = t_indicateurs_13.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_14 ON (t_indicateurs.indicateur_associe_14_id = t_indicateurs_14.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_15 ON (t_indicateurs.indicateur_associe_15_id = t_indicateurs_15.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_16 ON (t_indicateurs.indicateur_associe_16_id = t_indicateurs_16.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_17 ON (t_indicateurs.indicateur_associe_17_id = t_indicateurs_17.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_18 ON (t_indicateurs.indicateur_associe_18_id = t_indicateurs_18.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_19 ON (t_indicateurs.indicateur_associe_19_id = t_indicateurs_19.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_20 ON (t_indicateurs.indicateur_associe_20_id = t_indicateurs_20.oid) 
				LEFT JOIN rh.t_indicateurs t_indicateurs_reference ON (t_indicateurs.indicateur_reference_id = t_indicateurs_reference.oid) 
			WHERE t_indicateurs.is_cti = '1'
			ORDER BY t_indicateurs.code
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[
		
			DELETE FROM rh.t_indicateurs 
			USING
				(
				SELECT code, MIN(oid) AS minoid FROM rh.t_indicateurs WHERE code <> '' GROUP BY 1 HAVING count(*) > 1
				) subview 
			WHERE t_indicateurs.code = subview.code AND t_indicateurs.oid <> subview.minoid
			;
			
			UPDATE rh.t_indicateurs SET 
				texte = w_dbsetup.texte, 
				table_name = w_dbsetup.table_name,
				column_name = w_dbsetup.column_name,
				is_cti = w_dbsetup.is_cti, 
				entete = w_dbsetup.entete,
				entete_etendue = w_dbsetup.entete_etendue,
				total_function = w_dbsetup.total_function,
				output_format = w_dbsetup.output_format,
				width = w_dbsetup.width,
				dimension_date_1 = w_dbsetup.dimension_date_1,
				dimension_date_2 = w_dbsetup.dimension_date_2,
				dimension_date_3 = w_dbsetup.dimension_date_3,
				dimension_date_4 = w_dbsetup.dimension_date_4,
				dimension_date_5 = w_dbsetup.dimension_date_5,
				dimension_date = w_dbsetup.dimension_date	
			FROM w_dbsetup
			WHERE t_indicateurs.code = w_dbsetup.code
			;
			
			INSERT INTO rh.t_indicateurs(texte, table_name, column_name, is_cti, entete, entete_etendue, code, total_function, output_format, width,
				dimension_date_1, dimension_date_2, dimension_date_3, dimension_date_4, dimension_date_5, dimension_date)
			SELECT texte, table_name, column_name, is_cti, entete, entete_etendue, code, total_function, output_format, width,
				dimension_date_1, dimension_date_2, dimension_date_3, dimension_date_4, dimension_date_5, dimension_date 
			FROM w_dbsetup WHERE oid <> 0 AND code NOT IN (SELECT code FROM rh.t_indicateurs WHERE code IS NOT NULL)
			;
				
			UPDATE rh.t_indicateurs SET 
				indicateur_associe_1_id = COALESCE(t_indicateurs_1.oid,0),
				indicateur_associe_2_id = COALESCE(t_indicateurs_2.oid,0),
				indicateur_associe_3_id = COALESCE(t_indicateurs_3.oid,0),
				indicateur_associe_4_id = COALESCE(t_indicateurs_4.oid,0),
				indicateur_associe_5_id = COALESCE(t_indicateurs_5.oid,0),
				indicateur_associe_6_id = COALESCE(t_indicateurs_6.oid,0),
				indicateur_associe_7_id = COALESCE(t_indicateurs_7.oid,0),
				indicateur_associe_8_id = COALESCE(t_indicateurs_8.oid,0),
				indicateur_associe_9_id = COALESCE(t_indicateurs_9.oid,0),
				indicateur_associe_10_id = COALESCE(t_indicateurs_10.oid,0),
				indicateur_associe_11_id = COALESCE(t_indicateurs_11.oid,0),
				indicateur_associe_12_id = COALESCE(t_indicateurs_12.oid,0),
				indicateur_associe_13_id = COALESCE(t_indicateurs_13.oid,0),
				indicateur_associe_14_id = COALESCE(t_indicateurs_14.oid,0),
				indicateur_associe_15_id = COALESCE(t_indicateurs_15.oid,0),
				indicateur_associe_16_id = COALESCE(t_indicateurs_16.oid,0),
				indicateur_associe_17_id = COALESCE(t_indicateurs_17.oid,0),
				indicateur_associe_18_id = COALESCE(t_indicateurs_18.oid,0),
				indicateur_associe_19_id = COALESCE(t_indicateurs_19.oid,0),
				indicateur_associe_20_id = COALESCE(t_indicateurs_20.oid,0),
				indicateur_reference_id = COALESCE(t_indicateurs_reference.oid,0)
			FROM w_dbsetup
				LEFT JOIN rh.t_indicateurs t_indicateurs_1 ON (w_dbsetup.indicateur_associe_1_code = t_indicateurs_1.code AND w_dbsetup.indicateur_associe_1_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_2 ON (w_dbsetup.indicateur_associe_2_code = t_indicateurs_2.code AND w_dbsetup.indicateur_associe_2_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_3 ON (w_dbsetup.indicateur_associe_3_code = t_indicateurs_3.code AND w_dbsetup.indicateur_associe_3_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_4 ON (w_dbsetup.indicateur_associe_4_code = t_indicateurs_4.code AND w_dbsetup.indicateur_associe_4_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_5 ON (w_dbsetup.indicateur_associe_5_code = t_indicateurs_5.code AND w_dbsetup.indicateur_associe_5_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_6 ON (w_dbsetup.indicateur_associe_6_code = t_indicateurs_6.code AND w_dbsetup.indicateur_associe_6_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_7 ON (w_dbsetup.indicateur_associe_7_code = t_indicateurs_7.code AND w_dbsetup.indicateur_associe_7_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_8 ON (w_dbsetup.indicateur_associe_8_code = t_indicateurs_8.code AND w_dbsetup.indicateur_associe_8_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_9 ON (w_dbsetup.indicateur_associe_9_code = t_indicateurs_9.code AND w_dbsetup.indicateur_associe_9_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_10 ON (w_dbsetup.indicateur_associe_10_code = t_indicateurs_10.code AND w_dbsetup.indicateur_associe_10_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_11 ON (w_dbsetup.indicateur_associe_11_code = t_indicateurs_11.code AND w_dbsetup.indicateur_associe_11_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_12 ON (w_dbsetup.indicateur_associe_12_code = t_indicateurs_12.code AND w_dbsetup.indicateur_associe_12_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_13 ON (w_dbsetup.indicateur_associe_13_code = t_indicateurs_13.code AND w_dbsetup.indicateur_associe_13_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_14 ON (w_dbsetup.indicateur_associe_14_code = t_indicateurs_14.code AND w_dbsetup.indicateur_associe_14_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_15 ON (w_dbsetup.indicateur_associe_15_code = t_indicateurs_15.code AND w_dbsetup.indicateur_associe_15_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_16 ON (w_dbsetup.indicateur_associe_16_code = t_indicateurs_16.code AND w_dbsetup.indicateur_associe_16_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_17 ON (w_dbsetup.indicateur_associe_17_code = t_indicateurs_17.code AND w_dbsetup.indicateur_associe_17_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_18 ON (w_dbsetup.indicateur_associe_18_code = t_indicateurs_18.code AND w_dbsetup.indicateur_associe_18_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_19 ON (w_dbsetup.indicateur_associe_19_code = t_indicateurs_19.code AND w_dbsetup.indicateur_associe_19_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_20 ON (w_dbsetup.indicateur_associe_20_code = t_indicateurs_20.code AND w_dbsetup.indicateur_associe_20_code <> '') 
				LEFT JOIN rh.t_indicateurs t_indicateurs_reference ON (w_dbsetup.indicateur_reference_code = t_indicateurs_reference.code AND w_dbsetup.indicateur_reference_code <> '') 
			WHERE t_indicateurs.code = w_dbsetup.code 
				AND (indicateur_associe_1_code <> '' OR 
					indicateur_associe_2_code <> '' OR 
					indicateur_associe_3_code <> '' OR 
					indicateur_reference_code <> '')
			;
					
			SELECT base.cti_reorganize_indicateurs('rh')
			;
						
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_rapports" label="Rapports">
		<selectCmd><![CDATA[
			
			SELECT
                oid,
                code,
                texte,
                is_cti,
                is_essentiel
			FROM rh.t_rapports 
			WHERE is_cti = '1'
			ORDER BY code
			;
		
		]]></selectCmd>
		<updateCmd><![CDATA[
		
			UPDATE rh.t_rapports SET 
				texte = w_dbsetup.texte, 
				is_cti = w_dbsetup.is_cti, 
				is_essentiel = w_dbsetup.is_essentiel
			FROM w_dbsetup
			WHERE t_rapports.code = w_dbsetup.code
			;

			INSERT INTO rh.t_rapports(oid, code, texte, is_cti, is_essentiel)
				SELECT oid, code, texte, is_cti, is_essentiel FROM w_dbsetup WHERE oid = 0 AND oid NOT IN (SELECT oid FROM rh.t_rapports)
			;
			
			INSERT INTO rh.t_rapports(code, texte, is_cti, is_essentiel)
				SELECT code, texte, is_cti, is_essentiel FROM w_dbsetup WHERE oid <> 0 AND code NOT IN (SELECT code FROM rh.t_rapports)
			;
				
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_rapports_rubriques" label="Rubriques des rapports">
		<selectCmd><![CDATA[
			
			SELECT
				t_rapports.code AS rapport_code,
				ind1.code AS indicateur_code,
				t_rapports_rubriques.numero_rubrique,
				t_rapports_rubriques.rapport_id,
				t_rapports_rubriques.indicateur_id,
				t_rapports_rubriques.entete,
				t_rapports_rubriques.output_format,
				t_rapports_rubriques.width,
				t_rapports_rubriques.entete_etendue,
				t_rapports_rubriques.periode_reference,
				t_rapports_rubriques.row_height,
				t_rapports_rubriques.padding,
				t_rapports_rubriques.font_size,
				t_rapports_rubriques.font_weight,
				t_rapports_rubriques.border_thickness,
				t_rapports_rubriques.color,
				t_rapports_rubriques.background_color,
				t_rapports_rubriques.base_nationale,
				t_rapports_rubriques.base_nationale_qualifiee,
				t_rapports_rubriques.viewlink,
				t_rapports_rubriques.viewlink_label,
				t_rapports_rubriques.viewlink_param,
				t_rapports_rubriques.picto_enabled,
				t_rapports_rubriques.positif_is_good,
				t_rapports_rubriques.indicateur_associe_id,
				ind2.code AS indicateur_associe_code
			FROM rh.t_rapports_rubriques
				JOIN rh.t_rapports ON t_rapports_rubriques.rapport_id = t_rapports.oid
				JOIN rh.t_indicateurs ind1 ON t_rapports_rubriques.indicateur_id = ind1.oid
				LEFT JOIN rh.t_indicateurs ind2 ON t_rapports_rubriques.indicateur_associe_id = ind2.oid
			WHERE 1=1
				AND ind1.is_cti = '1' 
				AND ind1.code <> ''
				AND t_rapports.is_cti = '1'
			ORDER BY t_rapports.code, ind1.code, t_rapports_rubriques.numero_rubrique
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[
            
            -- Sauvegarder les donnees sources pour rejouer le specifique
            SELECT base.cti_verify_table_encoding('rh','t_rapports_rubriques')
            ;
            DROP TABLE IF EXISTS w_specific_rapports_rubriques ;
            CREATE TEMP TABLE w_specific_rapports_rubriques AS
            SELECT t_rapports_rubriques.* 
            FROM rh.t_rapports_rubriques
            JOIN rh.t_rapports ON t_rapports_rubriques.rapport_id = t_rapports.oid
            WHERE t_rapports.is_essentiel = '1'
            ;
            
            -- Installer les donnees CTI
			DELETE FROM rh.t_rapports_rubriques 
			WHERE 1=1
				AND rapport_id IN (SELECT oid FROM rh.t_rapports WHERE is_cti = '1')
			;

			INSERT INTO rh.t_rapports_rubriques(
				rapport_id,
				numero_rubrique,
				indicateur_id,
				entete,
				entete_etendue,
				output_format,
				width,
				periode_reference,
				row_height,
				padding,
				font_size,
				font_weight,
				border_thickness,
				color,
				base_nationale,
				base_nationale_qualifiee,
				background_color,
				viewlink,
				viewlink_label,
				viewlink_param,
				picto_enabled,
				positif_is_good,
				indicateur_associe_id
				)
			SELECT
				t_rapports.oid,
				numero_rubrique,
				ind1.oid,
				w_dbsetup.entete,
				w_dbsetup.entete_etendue,
				w_dbsetup.output_format,
				w_dbsetup.width,
				w_dbsetup.periode_reference,
				w_dbsetup.row_height,
				w_dbsetup.padding,
				w_dbsetup.font_size,
				w_dbsetup.font_weight,
				w_dbsetup.border_thickness,
				w_dbsetup.color,
				w_dbsetup.base_nationale,
				w_dbsetup.base_nationale_qualifiee,
				w_dbsetup.background_color,
				w_dbsetup.viewlink,
				w_dbsetup.viewlink_label,
				w_dbsetup.viewlink_param,
				w_dbsetup.picto_enabled,
				w_dbsetup.positif_is_good,
				COALESCE(ind2.oid, 0)
			FROM w_dbsetup
				JOIN rh.t_rapports ON w_dbsetup.rapport_code = t_rapports.code
				JOIN rh.t_indicateurs ind1 ON w_dbsetup.indicateur_code = ind1.code
				LEFT JOIN rh.t_indicateurs ind2 ON w_dbsetup.indicateur_associe_code = ind2.code
			;
            
            -- Rejouer le specifique
            UPDATE rh.t_rapports_rubriques SET
                is_hide = w_specific_rapports_rubriques.is_hide,
                specific_viewlink = w_specific_rapports_rubriques.specific_viewlink,
                specific_viewlink_label = w_specific_rapports_rubriques.specific_viewlink_label,
                specific_viewlink_param = w_specific_rapports_rubriques.specific_viewlink_param
            FROM w_specific_rapports_rubriques
            WHERE w_specific_rapports_rubriques.rapport_id = t_rapports_rubriques.rapport_id AND
                w_specific_rapports_rubriques.indicateur_id = t_rapports_rubriques.indicateur_id AND
                w_specific_rapports_rubriques.entete_etendue = t_rapports_rubriques.entete_etendue AND
                (
                    w_specific_rapports_rubriques.is_hide = '1' OR
                    w_specific_rapports_rubriques.specific_viewlink <> '' OR
                    w_specific_rapports_rubriques.specific_viewlink_label <> '' OR
                    w_specific_rapports_rubriques.specific_viewlink_param <> ''
                )
            ;
		  ]]></updateCmd>
	</TABLE>
	<TABLE name="t_classes" label="Classes RH">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_listes_tables.code AS table_code,
				t_classes.code AS classe_code,
				t_classes.texte,
				t_classes.is_cti,
				t_classes.contenu_by_code,
				t_classes.contenu_non_replicable_ailleurs
			FROM rh.t_classes
			JOIN rh.t_listes_tables ON table_id = t_listes_tables.oid 
			WHERE t_classes.code <> '' AND
				(is_cti = '1'  OR t_classes.code ilike 'CTI_%') AND is_cti = '1'
			ORDER BY 1,2;
			
		]]></selectCmd>
		<updateCmd><![CDATA[
		
			ALTER TABLE w_dbsetup ADD COLUMN IF NOT EXISTS table_id bigint DEFAULT 0
			;

			UPDATE w_dbsetup
			SET table_id = t_listes_tables.oid
			FROM rh.t_listes_tables
			WHERE t_listes_tables.code = table_code
			;
			
			ALTER TABLE w_dbsetup ADD COLUMN IF NOT EXISTS sequence integer DEFAULT 0
			;
			
			UPDATE w_dbsetup
			SET sequence = t_classes.sequence
			FROM rh.t_classes
			WHERE t_classes.code = w_dbsetup.classe_code AND
				t_classes.table_id = w_dbsetup.table_id
			;
			
			
			UPDATE w_dbsetup
			SET sequence = subview.sequence
			FROM
			(
				SELECT 
					w_dbsetup.classe_code,
					w_dbsetup.table_id,
					subview.max_sequence_by_table + row_number() OVER (PARTITION BY w_dbsetup.table_id ORDER BY classe_code) AS sequence 
				FROM w_dbsetup
				JOIN (
					SELECT 
						t_listes_tables.oid AS table_id, 
						MAX(COALESCE(t_classes.sequence, 0)) AS max_sequence_by_table
					FROM rh.t_listes_tables
					LEFT JOIN rh.t_classes ON t_classes.table_id = t_listes_tables.oid
					GROUP BY 1
				) AS subview ON w_dbsetup.table_id = subview.table_id
				WHERE w_dbsetup.sequence = 0
			) AS subview
			WHERE w_dbsetup.table_id = subview.table_id AND
				w_dbsetup.classe_code = subview.classe_code AND
				w_dbsetup.sequence = 0
			;
			
			UPDATE rh.t_classes SET
				texte = w_dbsetup.texte,       
				table_id = w_dbsetup.table_id,      
				is_cti = w_dbsetup.is_cti,
                contenu_by_code = w_dbsetup.contenu_by_code,
                contenu_non_replicable_ailleurs = w_dbsetup.contenu_non_replicable_ailleurs
			FROM w_dbsetup
			WHERE 1=1
				AND t_classes.code = w_dbsetup.classe_code
				AND t_classes.table_id = w_dbsetup.table_id
                AND (1!=1
                    OR t_classes.texte IS DISTINCT FROM w_dbsetup.texte
                    OR t_classes.is_cti IS DISTINCT FROM w_dbsetup.is_cti
                    OR t_classes.contenu_by_code IS DISTINCT FROM w_dbsetup.contenu_by_code
                    OR t_classes.contenu_non_replicable_ailleurs IS DISTINCT FROM w_dbsetup.contenu_non_replicable_ailleurs)
            ;
			
			INSERT INTO rh.t_classes(
				code, 
				texte, 
				table_id, 
				is_cti, 
				sequence, 
				contenu_by_code, 
				contenu_non_replicable_ailleurs
				)
			SELECT 
				w_dbsetup.classe_code, 
				w_dbsetup.texte, 
				w_dbsetup.table_id, 
				w_dbsetup.is_cti, 
				w_dbsetup.sequence,
				w_dbsetup.contenu_by_code,
				w_dbsetup.contenu_non_replicable_ailleurs
			FROM w_dbsetup
			WHERE 1=1
				AND w_dbsetup.table_id::text || '-' || w_dbsetup.sequence::text NOT IN (SELECT table_id::text || '-' || sequence::text FROM rh.t_classes)
				AND w_dbsetup.texte NOT LIKE '*SUPPRIME%'
            ;
			
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_classes_sections" label="Classes RH (sections)">
		<selectCmd>
			<![CDATA[
			SELECT 
				t_classes_sections.code,
				t_classes_sections.texte,
				t_classes_sections.condition,
				t_classes_sections.exclude_code,
				t_classes_sections.include_code,
				t_listes_tables.code as table_code, 
				t_classes.code as classe_code 
			FROM rh.t_classes_sections
				JOIN rh.t_classes ON t_classes.oid = t_classes_sections.classe_id
				JOIN rh.t_listes_tables ON t_listes_tables.oid = t_classes.table_id
			WHERE t_classes.is_cti = 1
			ORDER BY
				t_listes_tables.code, 
				t_classes.code, 
				t_classes_sections.code
			;
			]]>
		</selectCmd>
		<updateCmd>
			<![CDATA[
			
			-- Màj des sections de classe.
			UPDATE rh.t_classes_sections
			SET 
				texte = w_dbsetup.texte,
				condition = w_dbsetup.condition,
				exclude_code = w_dbsetup.exclude_code,
				include_code = w_dbsetup.include_code
			FROM
				w_dbsetup,
				rh.t_classes,
				rh.t_listes_tables
			WHERE 1=1
				AND t_classes.oid = t_classes_sections.classe_id
				AND t_listes_tables.oid = t_classes.table_id
				AND w_dbsetup.code = t_classes_sections.code
				AND w_dbsetup.classe_code = t_classes.code
				AND w_dbsetup.table_code = t_listes_tables.code AND 
				(
					t_classes_sections.texte IS DISTINCT FROM w_dbsetup.texte OR
					t_classes_sections.condition IS DISTINCT FROM w_dbsetup.condition OR
					t_classes_sections.exclude_code IS DISTINCT FROM w_dbsetup.exclude_code OR
					t_classes_sections.include_code IS DISTINCT FROM w_dbsetup.include_code
				)
			;

			-- Alimentation des sections de classe.
			INSERT INTO rh.t_classes_sections(code, texte, condition, exclude_code, include_code, classe_id)
				SELECT
					w_dbsetup.code,
					w_dbsetup.texte,
					w_dbsetup.condition,
					w_dbsetup.exclude_code,
					w_dbsetup.include_code,
					t_classes.oid
				FROM w_dbsetup
					JOIN rh.t_listes_tables ON t_listes_tables.code = w_dbsetup.table_code
					JOIN rh.t_classes ON 1=1
						AND t_classes.table_id = t_listes_tables.oid
						AND t_classes.code = w_dbsetup.classe_code
				WHERE 1=1
					AND w_dbsetup.code||'@'||w_dbsetup.classe_code||'@'||w_dbsetup.table_code
						NOT IN (
							SELECT t_classes_sections.code||'@'||t_classes.code||'@'||t_listes_tables.code
							FROM rh.t_classes_sections
								JOIN rh.t_classes ON t_classes.oid = t_classes_sections.classe_id
								JOIN rh.t_listes_tables ON t_listes_tables.oid = t_classes.table_id
						)
			;
			
			]]>
</updateCmd>
	</TABLE>
	<TABLE name="t_classes_sections_elements_ages" label="Classes RH (ages)">
		<selectCmd>
			<![CDATA[
			SELECT
				t_classes_sections.code AS section_code, 
				t_ages.code as to_code, 
				t_listes_tables.code as table_code, 
				t_classes.code as classe_code 
			FROM 
				rh.t_classes_sections_elements, 
				rh.t_classes_sections, 
				rh.t_classes, 
				rh.t_listes_tables, 
				rh.t_ages
			WHERE 
				t_classes_sections_elements.section_id = t_classes_sections.oid
				AND t_classes_sections.classe_id = t_classes.oid 
				AND t_classes.is_cti = 1
				AND table_id = t_listes_tables.oid
				AND t_listes_tables.code = 'AGE'
				AND t_classes_sections_elements.to_id = t_ages.oid
			ORDER BY 
				t_classes.code, 
				t_classes_sections.code,
				t_ages.code
			;
			]]>
		</selectCmd>
		<updateCmd>
			<![CDATA[
			INSERT INTO rh.t_classes_sections_elements(section_id, to_id)     
				SELECT
					t_classes_sections.oid,
					t_ages.oid 
				FROM w_dbsetup
					JOIN rh.t_listes_tables ON t_listes_tables.code = w_dbsetup.table_code
					JOIN rh.t_classes ON 1=1
						AND t_classes.table_id = t_listes_tables.oid
						AND t_classes.code = w_dbsetup.classe_code
					JOIN rh.t_classes_sections ON 1=1
						AND t_classes_sections.classe_id = t_classes.oid
						AND t_classes_sections.code = w_dbsetup.section_code
					JOIN rh.t_ages ON t_ages.code = w_dbsetup.to_code
				WHERE t_classes_sections.oid::text || '-' || t_ages.oid::text NOT IN 
						(
						SELECT section_id::text || '-' || to_id::text 
						FROM rh.t_classes_sections_elements
						)
			;
			]]>
</updateCmd>
	</TABLE>
	<TABLE name="t_classes_sections_elements_anciennetes_annee" label="Classes RH (anciennetés années)">
		<selectCmd>
			<![CDATA[
			SELECT
				t_classes_sections.code AS section_code, 
				t_anciennetes_annee.code as to_code, 
				t_listes_tables.code as table_code, 
				t_classes.code as classe_code 
			FROM 
				rh.t_classes_sections_elements, 
				rh.t_classes_sections, 
				rh.t_classes, 
				rh.t_listes_tables, 
				rh.t_anciennetes_annee
			WHERE 
				t_classes_sections_elements.section_id = t_classes_sections.oid
				AND t_classes_sections.classe_id = t_classes.oid 
				AND t_classes.is_cti = 1
				AND table_id = t_listes_tables.oid
				AND t_listes_tables.code = 'ANC'
				AND t_classes_sections_elements.to_id = t_anciennetes_annee.oid
			ORDER BY 
				t_classes.code, 
				t_classes_sections.code,
				t_anciennetes_annee.code
			;
			]]>
		</selectCmd>
		<updateCmd>
			<![CDATA[
			INSERT INTO rh.t_classes_sections_elements(section_id, to_id)     
				SELECT
					t_classes_sections.oid,
					t_anciennetes_annee.oid 
				FROM w_dbsetup
					JOIN rh.t_listes_tables ON t_listes_tables.code = w_dbsetup.table_code
					JOIN rh.t_classes ON 1=1
						AND t_classes.table_id = t_listes_tables.oid
						AND t_classes.code = w_dbsetup.classe_code
					JOIN rh.t_classes_sections ON 1=1
						AND t_classes_sections.classe_id = t_classes.oid
						AND t_classes_sections.code = w_dbsetup.section_code
					JOIN rh.t_anciennetes_annee ON t_anciennetes_annee.code = w_dbsetup.to_code
				WHERE t_classes_sections.oid::text || '-' || t_anciennetes_annee.oid::text NOT IN 
						(
						SELECT section_id::text || '-' || to_id::text 
						FROM rh.t_classes_sections_elements
						)
			;
			]]>
</updateCmd>
	</TABLE>

    <TABLE name="t_vue_parametre" label="Listes des données prédéfinis">
		<selectCmd><![CDATA[ 
			
		SELECT  oid,
				code, 
				texte, 
				array_to_string(data_code, '@') as data_code, 
				array_to_string(data_entete, '@') as data_entete, 
				array_to_string(data_format, '@') as data_format, 
				array_to_string(data_description, '@') as data_description,
                position_ratio,
                rang
		FROM rh.t_vue_parametre 
			ORDER BY 2;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO rh.t_vue_parametre(code, texte, data_code, data_entete, data_format, data_description,position_ratio,rang)
				SELECT  code, 
						texte, 
						string_to_array(data_code, '@') as data_code, 
						string_to_array(data_entete, '@') as data_entete, 
						string_to_array(data_format, '@') as data_format, 
						string_to_array(data_description, '@') as data_description,
                        position_ratio,
                        rang
				FROM w_dbsetup 
				WHERE 1=1 
					AND code NOT IN (SELECT code FROM rh.t_vue_parametre)
			;
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_indicateur_serie" label="Liste serie disponibles pour les graphiques">
		<selectCmd><![CDATA[ 
		
		WITH flatSeries as (
			SELECT 
				oid,
				cast(jsonb_array_elements(replace('[' || serie_id || ']', '|', ',')::jsonb)->>'pos' as bigint) as position,
				cast(jsonb_array_elements(replace('[' || serie_id || ']', '|', ',')::jsonb)->>'id' as bigint) as id
			FROM rh.t_indicateur_serie
			ORDER BY oid, position
		),

		flatCodes as (SELECT 
			flatSeries.oid,
			array_agg(position::text || ':' || t_indicateur_serie.code::text) as codes
		FROM flatSeries
		LEFT JOIN rh.t_indicateur_serie on t_indicateur_serie.oid = id
		GROUP BY 1)

		SELECT
			t_indicateur_serie.code,
			coalesce(t_indicateurs.code, '') as indicateur_code,
			t_indicateur_serie.name,
			t_indicateur_serie.color,
			t_indicateur_serie.formula,
			t_indicateur_serie.calc,
			t_indicateur_serie.total,
			t_indicateur_serie.format,
			case when array_agg(DISTINCT t_indicateur_condition.external_code2) = ARRAY[null] then ARRAY[]::text[] else array_agg(DISTINCT t_indicateur_condition.external_code2) end  as external_code2,
    		CASE WHEN flatCodes.codes = ARRAY[null] THEN '{}'::text[] ELSE flatCodes.codes::text[] END as serie_code
		FROM rh.t_indicateur_serie
		LEFT JOIN rh.t_indicateurs ON t_indicateurs.oid = indicateur_id
		LEFT JOIN rh.t_indicateur_condition ON ARRAY[t_indicateur_condition.oid] <@ t_indicateur_serie.condition_id
		LEFT JOIN flatCodes on flatCodes.oid = t_indicateur_serie.oid
		GROUP BY 1,2,3,4,5,6,7,8, flatCodes.codes
		ORDER BY 2,1;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			SELECT rh.cti_reorganize_indicateur_condition();

			TRUNCATE rh.t_indicateur_serie;

			INSERT INTO rh.t_indicateur_serie(indicateur_id, condition_id, name, color, formula, calc, serie_id, total, format, code)
				SELECT
					coalesce(t_indicateurs.oid, 0),
					case when array_agg(DISTINCT t_indicateur_condition.oid) = ARRAY[NULL]::bigint[] then ARRAY[]::bigint[] else array_agg(DISTINCT t_indicateur_condition.oid) end,
					w_dbsetup.name,
					w_dbsetup.color,
					w_dbsetup.formula,
					w_dbsetup.calc,
					case when array_agg(DISTINCT rec.oid) = ARRAY[NULL]::bigint[] then ARRAY[]::bigint[] else array_agg(DISTINCT rec.oid) end,
					w_dbsetup.total,
					w_dbsetup.format,
					w_dbsetup.code
			FROM w_dbsetup
			LEFT JOIN rh.t_indicateurs ON t_indicateurs.code = w_dbsetup.indicateur_code
			LEFT JOIN rh.t_indicateur_condition ON ARRAY[t_indicateur_condition.external_code2] <@ w_dbsetup.external_code2
			LEFT JOIN rh.t_indicateur_serie as rec ON ARRAY[rec.code] <@ w_dbsetup.serie_code
			GROUP BY 1,3,4,5,6,8,9,10
			;

			WITH series_id as (
			SELECT
					w_dbsetup.code,
					split_part(unnest(w_dbsetup.serie_code), ':', 1) as pos,
					split_part(unnest(w_dbsetup.serie_code), ':', 2) as serie_code
			FROM w_dbsetup
			), 

			full_series as (
			SELECT
				series_id.code,
				coalesce(string_agg('{"pos": ' || pos || ', "id": ' || t_indicateur_serie.oid || '}', '|'), '') as serie_id
			FROM series_id
			LEFT JOIN rh.t_indicateur_serie ON t_indicateur_serie.code = serie_code
			GROUP BY 1
			)
			

			UPDATE rh.t_indicateur_serie SET
				serie_id = CASE WHEN full_series.serie_id IS NULL THEN '' ELSE full_series.serie_id END
				FROM w_dbsetup
				LEFT JOIN full_series ON full_series.code = w_dbsetup.code
			WHERE t_indicateur_serie.code = w_dbsetup.code
			;

		]]></updateCmd>
	</TABLE>

	<TABLE name="t_chart" label="Liste des graphiques">
		<selectCmd><![CDATA[ 
		
		SELECT
			t_chart.type,
			t_chart.category,
			t_chart.name,
			t_chart.default_time_calc,
			t_chart.label_view_code,
			t_chart.code,
			array_agg(DISTINCT t_indicateur_serie.code) as serie_code
		FROM rh.t_chart
		LEFT JOIN rh.t_indicateur_serie ON ARRAY[t_indicateur_serie.oid] <@ t_chart.serie_id
		GROUP BY 1,2,3,4,5,6
		ORDER BY 1;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 

			TRUNCATE rh.t_chart;
			
			INSERT INTO rh.t_chart(type, serie_id, category, name, default_time_calc, label_view_code, code)
				SELECT
					type,
					array_agg(DISTINCT t_indicateur_serie.oid),
					w_dbsetup.category,
					w_dbsetup.name,
					w_dbsetup.default_time_calc,
					w_dbsetup.label_view_code,
					w_dbsetup.code
				FROM w_dbsetup
				LEFT JOIN rh.t_indicateur_serie ON ARRAY[t_indicateur_serie.code] <@ w_dbsetup.serie_code
				GROUP BY 1,3,4,5,6,7
			;
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_dashboard" label="Liste des dashboards">
		<selectCmd><![CDATA[ 
		
		SELECT
			code, label
		FROM rh.t_dashboard
		ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 

			TRUNCATE rh.t_dashboard;
			
			INSERT INTO rh.t_dashboard(code, label)
				SELECT
					w_dbsetup.code,
					w_dbsetup.label
				FROM w_dbsetup
			;
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_dashboard_widget" label="Association des graphiques avec les dashboard">
		<selectCmd><![CDATA[ 
		
		SELECT
			t_dashboard_widget.oid, 
			t_chart.code as chart_code,
			t_dashboard.code as dashboard_code,
			t_dashboard_widget.deprecated
		FROM rh.t_dashboard_widget
		LEFT JOIN rh.t_chart ON t_chart.oid = chart_id
		LEFT JOIN rh.t_dashboard ON t_dashboard.oid = dashboard_id
		ORDER BY 1;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 

			TRUNCATE rh.t_dashboard_widget;
			
			INSERT INTO rh.t_dashboard_widget(dashboard_id, chart_id, deprecated)
				SELECT
					t_dashboard.oid,
					t_chart.oid,
					w_dbsetup.deprecated
				FROM w_dbsetup
				LEFT JOIN rh.t_chart ON t_chart.code = chart_code
				LEFT JOIN rh.t_dashboard ON t_dashboard.code = dashboard_code
			;
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_dashboard_filter" label="Liste des filtres disponibles pour un dashboard">
		<selectCmd><![CDATA[ 
		
		SELECT
			t_dashboard_filter.oid,
			t_dashboard.code as dashboard_code,
			t_indicateur_condition.external_code2
		FROM rh.t_dashboard_filter
		LEFT JOIN rh.t_dashboard ON t_dashboard.oid = dashboard_id
		LEFT JOIN rh.t_indicateur_condition ON t_indicateur_condition.oid = condition_id
		ORDER BY 2, 3;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 

			TRUNCATE rh.t_dashboard_filter;
			
			INSERT INTO rh.t_dashboard_filter(dashboard_id,condition_id)
				SELECT
					t_dashboard.oid,
					t_indicateur_condition.oid
				FROM w_dbsetup
				LEFT JOIN rh.t_dashboard ON t_dashboard.code = w_dbsetup.dashboard_code
				LEFT JOIN rh.t_indicateur_condition ON t_indicateur_condition.external_code2 = w_dbsetup.external_code2
			;
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_dashboard_onglet" label="Onglets pour un meme dashboard">
		<selectCmd><![CDATA[ 
		
		SELECT
			t_dashboard_onglet.oid,
			t_dashboard.code as dashboard_code,
			name,
			index
		FROM rh.t_dashboard_onglet
		LEFT JOIN rh.t_dashboard ON t_dashboard.oid = dashboard_id
		ORDER BY 2, 4;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 

			TRUNCATE rh.t_dashboard_onglet;
			
			INSERT INTO rh.t_dashboard_onglet(dashboard_id, name, index)
				SELECT
					t_dashboard.oid,
					w_dbsetup.name,
					w_dbsetup.index
				FROM w_dbsetup
				LEFT JOIN rh.t_dashboard ON t_dashboard.code = dashboard_code
			;
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_widget_user_parameter" label="Configs utilisateur des dashboard">
		<selectCmd><![CDATA[ 
		
		SELECT
			t_widget_user_parameter.oid as uuid,
			t_dashboard.code || '|' || t_chart.code as dashboard_widget_code,
			username,
			title,
			x,
			y,
			w,
			h,
			time_calc,
			visible,
			t_dashboard.code || '|' || t_dashboard_onglet.index as tab_code,
			max_cat,
			array_agg(DISTINCT t_indicateur_serie.code) as include_series_code
		FROM rh.t_widget_user_parameter
		JOIN rh.t_dashboard_widget ON t_dashboard_widget.oid = dashboard_widget_id
		JOIN rh.t_dashboard ON t_dashboard.oid= t_dashboard_widget.dashboard_id
			JOIN rh.t_chart on t_chart.oid = t_dashboard_widget.chart_id
		JOIN rh.t_dashboard_onglet on t_dashboard_onglet.oid = tab_id
		LEFT JOIN rh.t_indicateur_serie ON ARRAY[t_indicateur_serie.oid] <@ include_series_id
		WHERE username = 'cti'
		GROUP BY 1,2,3,4,5,6,7,8,9,10,11, t_dashboard_onglet.index
		ORDER BY 2, 6, 7;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 

			DELETE FROM rh.t_widget_user_parameter WHERE username = 'cti';
			
			INSERT INTO rh.t_widget_user_parameter(
				oid, 
				dashboard_widget_id,
				username,
				title,
				x,
				y,
				w,
				h,
				time_calc,
				visible,
				tab_id,
				max_cat,
				include_series_id
			)
				SELECT
					w_dbsetup.uuid,
					t_dashboard_widget.oid,
					w_dbsetup.username,
					w_dbsetup.title,
					w_dbsetup.x,
					w_dbsetup.y,
					w_dbsetup.w,
					w_dbsetup.h,
					w_dbsetup.time_calc,
					w_dbsetup.visible,
					rh.t_dashboard_onglet.oid,
					w_dbsetup.max_cat,
					case when array_agg(DISTINCT t_indicateur_serie.oid) = ARRAY[NULL]::bigint[] then ARRAY[]::bigint[] else array_agg(DISTINCT t_indicateur_serie.oid) end
				FROM w_dbsetup
				JOIN rh.t_dashboard ON t_dashboard.code = SPLIT_PART(dashboard_widget_code, '|', 1)
				JOIN rh.t_chart on t_chart.code = SPLIT_PART(dashboard_widget_code, '|', 2)
				JOIN rh.t_dashboard_widget ON t_dashboard_widget.dashboard_id = t_dashboard.oid  and t_dashboard_widget.chart_id = t_chart.oid
				JOIN rh.t_dashboard_onglet ON t_dashboard_onglet.dashboard_id = t_dashboard.oid and t_dashboard_onglet.index = SPLIT_PART(tab_code, '|', 2)
				LEFT JOIN rh.t_indicateur_serie ON ARRAY[t_indicateur_serie.code] <@ include_series_code
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12
				ORDER BY 1
			;
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_expert_controle" label="Contrôles expert">
		<selectCmd><![CDATA[ 
			
			SELECT 
				oid,
				code,
				texte,
				description,
				is_cti,
				gravite_id,
				start_periode
			FROM rh.t_expert_controle
			WHERE is_cti = '1' 
			ORDER BY code;
			
		]]>		</selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE rh.t_expert_controle SET
				code = w_dbsetup.code,
				texte = w_dbsetup.texte,
				description = w_dbsetup.description,
				is_cti = w_dbsetup.is_cti,
				gravite_id = w_dbsetup.gravite_id,
				start_periode = w_dbsetup.start_periode
			FROM w_dbsetup
			WHERE 1=1
				AND w_dbsetup.oid = 0
				AND rh.t_expert_controle.oid = w_dbsetup.oid
				AND 
				(
				t_expert_controle.code IS DISTINCT FROM w_dbsetup.code OR
				t_expert_controle.texte IS DISTINCT FROM w_dbsetup.texte OR
				t_expert_controle.description IS DISTINCT FROM w_dbsetup.description OR
				t_expert_controle.is_cti IS DISTINCT FROM w_dbsetup.is_cti OR
				t_expert_controle.gravite_id IS DISTINCT FROM w_dbsetup.gravite_id OR
				t_expert_controle.start_periode IS DISTINCT FROM w_dbsetup.start_periode
				)
			;
			
			UPDATE rh.t_expert_controle SET
				code = w_dbsetup.code,
				texte = w_dbsetup.texte,
				description = w_dbsetup.description,
				is_cti = w_dbsetup.is_cti,
				gravite_id = w_dbsetup.gravite_id,
				start_periode = w_dbsetup.start_periode
			FROM w_dbsetup
			WHERE 1=1
				AND w_dbsetup.oid <> 0
				AND rh.t_expert_controle.code = w_dbsetup.code
				AND 
				(
				t_expert_controle.texte IS DISTINCT FROM w_dbsetup.texte OR
				t_expert_controle.description IS DISTINCT FROM w_dbsetup.description OR
				t_expert_controle.is_cti IS DISTINCT FROM w_dbsetup.is_cti OR
				t_expert_controle.gravite_id IS DISTINCT FROM w_dbsetup.gravite_id OR
				t_expert_controle.start_periode IS DISTINCT FROM w_dbsetup.start_periode
				)
			;
			
			INSERT INTO rh.t_expert_controle(
				oid,
				code,
				texte,
				description,
				is_active,
				is_hide,
				alerte_id,
				is_cti,
				gravite_id,
				start_periode
				)
			SELECT 
				oid,
				code,
				texte,
				description,
				'1' AS is_active,
				'0' AS is_hide,
				0::bigint AS alerte_id,
				is_cti,
				gravite_id,
				start_periode
			FROM w_dbsetup 
			WHERE 1=1
				AND w_dbsetup.oid = 0
				AND w_dbsetup.oid NOT IN (SELECT oid FROM rh.t_expert_controle)
			;

			INSERT INTO rh.t_expert_controle(
				code,
				texte,
				description,
				is_active,
				is_hide,
				alerte_id,
				is_cti,
				gravite_id,
				start_periode
				)
			SELECT 
				code,
				texte,
				description,
				'1' AS is_active,
				'0' AS is_hide,
				0::bigint AS alerte_id,
				is_cti,
				gravite_id,
				start_periode
			FROM w_dbsetup 
			WHERE 1=1
				AND w_dbsetup.oid <> 0
				AND w_dbsetup.code NOT IN (SELECT code FROM rh.t_expert_controle)
			;
			
			
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_expert_controle_rule" label="règles expert_controles">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_expert_controle_rule.oid,
				t_expert_controle.code AS expert_controle_code,
				t_expert_controle_rule.data_type,
				t_indicateurs.code AS indicateur_code,
				COALESCE(t_indicateur_condition_1.external_code2,'') AS indicateur_condition_external_code_1,
				COALESCE(t_indicateur_condition_2.external_code2,'') AS indicateur_condition_external_code_2,
				COALESCE(t_indicateur_condition_3.external_code2,'') AS indicateur_condition_external_code_3,
				t_expert_controle_rule.formula,
				t_indicateurs_2.code AS indicateur_code_2,
				COALESCE(t_indicateur_condition_21.external_code2,'') AS indicateur_condition_external_code_21,
				COALESCE(t_indicateur_condition_22.external_code2,'') AS indicateur_condition_external_code_22,
				COALESCE(t_indicateur_condition_23.external_code2,'') AS indicateur_condition_external_code_23,
				t_expert_controle_rule.formula_2,
				t_indicateurs_3.code AS indicateur_code_3,
				COALESCE(t_indicateur_condition_31.external_code2,'') AS indicateur_condition_external_code_31,
				COALESCE(t_indicateur_condition_32.external_code2,'') AS indicateur_condition_external_code_32,
				COALESCE(t_indicateur_condition_33.external_code2,'') AS indicateur_condition_external_code_33,
				t_expert_controle_rule.formula_3,
				0::bigint AS expert_controle_id,
				0::bigint AS expert_controle_rule_id,
				0::bigint AS indicateur_id,
				0::bigint AS indicateur_condition_id_1,
				0::bigint AS indicateur_condition_id_2,
				0::bigint AS indicateur_condition_id_3,
				0::bigint AS indicateur_2_id,
				0::bigint AS indicateur_condition_id_21,
				0::bigint AS indicateur_condition_id_22,
				0::bigint AS indicateur_condition_id_23,
				0::bigint AS indicateur_3_id,
				0::bigint AS indicateur_condition_id_31,
				0::bigint AS indicateur_condition_id_32,
				0::bigint AS indicateur_condition_id_33
			FROM rh.t_expert_controle_rule
			JOIN rh.t_expert_controle ON expert_controle_id = t_expert_controle.oid
			JOIN rh.t_indicateurs t_indicateurs ON indicateur_id = t_indicateurs.oid
			LEFT JOIN rh.t_indicateur_condition t_indicateur_condition_1 ON indicateur_condition_id[1] = t_indicateur_condition_1.oid
			LEFT JOIN rh.t_indicateur_condition t_indicateur_condition_2 ON indicateur_condition_id[2] = t_indicateur_condition_2.oid
			LEFT JOIN rh.t_indicateur_condition t_indicateur_condition_3 ON indicateur_condition_id[3] = t_indicateur_condition_3.oid
			LEFT JOIN rh.t_indicateurs t_indicateurs_2 ON indicateur_2_id = t_indicateurs_2.oid
			LEFT JOIN rh.t_indicateur_condition t_indicateur_condition_21 ON indicateur_condition_2_id[1] = t_indicateur_condition_21.oid
			LEFT JOIN rh.t_indicateur_condition t_indicateur_condition_22 ON indicateur_condition_2_id[2] = t_indicateur_condition_22.oid
			LEFT JOIN rh.t_indicateur_condition t_indicateur_condition_23 ON indicateur_condition_2_id[3] = t_indicateur_condition_23.oid
			LEFT JOIN rh.t_indicateurs t_indicateurs_3 ON indicateur_3_id = t_indicateurs_3.oid
			LEFT JOIN rh.t_indicateur_condition t_indicateur_condition_31 ON indicateur_condition_3_id[1] = t_indicateur_condition_31.oid
			LEFT JOIN rh.t_indicateur_condition t_indicateur_condition_32 ON indicateur_condition_3_id[2] = t_indicateur_condition_32.oid
			LEFT JOIN rh.t_indicateur_condition t_indicateur_condition_33 ON indicateur_condition_3_id[3] = t_indicateur_condition_33.oid
			WHERE t_expert_controle_rule.is_cti = '1' AND 
				t_expert_controle_rule.indicateur_id > 0 AND
				t_expert_controle.is_cti = '1'
			ORDER BY t_expert_controle.code, 
				t_indicateurs.code,
				t_indicateur_condition_1.code,
				t_indicateur_condition_2.code,
				t_indicateur_condition_3.code
				
			;
			
		]]>		</selectCmd>
		<updateCmd><![CDATA[

			SELECT rh.cti_reorganize_indicateur_condition()
			;
			
			UPDATE w_dbsetup
			SET expert_controle_id = t_expert_controle.oid
			FROM rh.t_expert_controle
			WHERE expert_controle_code = t_expert_controle.code
			;

			UPDATE w_dbsetup
			SET indicateur_id = t_indicateurs.oid
			FROM rh.t_indicateurs
			WHERE indicateur_code = t_indicateurs.code
			;
			UPDATE w_dbsetup
			SET indicateur_2_id = t_indicateurs.oid
			FROM rh.t_indicateurs
			WHERE indicateur_code_2 = t_indicateurs.code
			;
			UPDATE w_dbsetup
			SET indicateur_3_id = t_indicateurs.oid
			FROM rh.t_indicateurs
			WHERE indicateur_code_3 = t_indicateurs.code
			;
			
			UPDATE w_dbsetup
			SET indicateur_condition_id_1 = t_indicateur_condition.oid
			FROM rh.t_indicateur_condition
			WHERE indicateur_condition_external_code_1 = t_indicateur_condition.external_code2
			;
			UPDATE w_dbsetup
			SET indicateur_condition_id_2 = t_indicateur_condition.oid
			FROM rh.t_indicateur_condition
			WHERE indicateur_condition_external_code_2 = t_indicateur_condition.external_code2
			;
			UPDATE w_dbsetup
			SET indicateur_condition_id_3 = t_indicateur_condition.oid
			FROM rh.t_indicateur_condition
			WHERE indicateur_condition_external_code_3 = t_indicateur_condition.external_code2
			;
			UPDATE w_dbsetup
			SET indicateur_condition_id_21 = t_indicateur_condition.oid
			FROM rh.t_indicateur_condition
			WHERE indicateur_condition_external_code_21 = t_indicateur_condition.external_code2
			;
			UPDATE w_dbsetup
			SET indicateur_condition_id_22 = t_indicateur_condition.oid
			FROM rh.t_indicateur_condition
			WHERE indicateur_condition_external_code_22 = t_indicateur_condition.external_code2
			;
			UPDATE w_dbsetup
			SET indicateur_condition_id_23 = t_indicateur_condition.oid
			FROM rh.t_indicateur_condition
			WHERE indicateur_condition_external_code_23 = t_indicateur_condition.external_code2
			;

			UPDATE w_dbsetup
			SET indicateur_condition_id_31 = t_indicateur_condition.oid
			FROM rh.t_indicateur_condition
			WHERE indicateur_condition_external_code_31 = t_indicateur_condition.external_code2
			;
			UPDATE w_dbsetup
			SET indicateur_condition_id_32 = t_indicateur_condition.oid
			FROM rh.t_indicateur_condition
			WHERE indicateur_condition_external_code_32 = t_indicateur_condition.external_code2
			;
			UPDATE w_dbsetup
			SET indicateur_condition_id_33 = t_indicateur_condition.oid
			FROM rh.t_indicateur_condition
			WHERE indicateur_condition_external_code_33 = t_indicateur_condition.external_code2
			;
			

			UPDATE w_dbsetup
			SET expert_controle_rule_id = t_expert_controle_rule.oid
			FROM rh. t_expert_controle_rule
			WHERE w_dbsetup.expert_controle_id = t_expert_controle_rule.expert_controle_id AND
				w_dbsetup.indicateur_id = t_expert_controle_rule.indicateur_id AND
				w_dbsetup.indicateur_condition_id_1 = COALESCE(t_expert_controle_rule.indicateur_condition_id[1],0) AND
				w_dbsetup.indicateur_condition_id_2 = COALESCE(t_expert_controle_rule.indicateur_condition_id[2],0) AND
				w_dbsetup.indicateur_condition_id_3 = COALESCE(t_expert_controle_rule.indicateur_condition_id[3],0) AND
				w_dbsetup.indicateur_2_id = t_expert_controle_rule.indicateur_2_id AND
				w_dbsetup.indicateur_condition_id_21 = COALESCE(t_expert_controle_rule.indicateur_condition_2_id[1],0) AND
				w_dbsetup.indicateur_condition_id_22 = COALESCE(t_expert_controle_rule.indicateur_condition_2_id[2],0) AND
				w_dbsetup.indicateur_condition_id_23 = COALESCE(t_expert_controle_rule.indicateur_condition_2_id[3],0) AND
				w_dbsetup.indicateur_3_id = t_expert_controle_rule.indicateur_3_id AND
				w_dbsetup.indicateur_condition_id_31 = COALESCE(t_expert_controle_rule.indicateur_condition_3_id[1],0) AND
				w_dbsetup.indicateur_condition_id_32 = COALESCE(t_expert_controle_rule.indicateur_condition_3_id[2],0) AND
				w_dbsetup.indicateur_condition_id_33 = COALESCE(t_expert_controle_rule.indicateur_condition_3_id[3],0) AND
				w_dbsetup.data_type = t_expert_controle_rule.data_type AND
				w_dbsetup.formula = t_expert_controle_rule.formula
			;

			DELETE
			FROM rh.t_expert_controle_rule
			WHERE expert_controle_id IN
			(SELECT oid
			FROM rh.t_expert_controle
			WHERE code IN (SELECT expert_controle_code FROM w_dbsetup)) AND
				is_cti = '1' AND
				oid NOT IN (SELECT expert_controle_rule_id FROM w_dbsetup) 
			;

			INSERT INTO rh.t_expert_controle_rule(
				expert_controle_id, 
				data_type, 
				indicateur_id,
				indicateur_condition_id, 
				formula, 
				indicateur_2_id,
				indicateur_condition_2_id, 
				formula_2, 
				indicateur_3_id,
				indicateur_condition_3_id, 
				formula_3, 
				is_cti)
			SELECT 
				expert_controle_id, 
				data_type,
				indicateur_id, 
				ARRAY[w_dbsetup.indicateur_condition_id_1,w_dbsetup.indicateur_condition_id_2,w_dbsetup.indicateur_condition_id_3], 
				formula,
				indicateur_2_id, 
				ARRAY[w_dbsetup.indicateur_condition_id_21,w_dbsetup.indicateur_condition_id_22,w_dbsetup.indicateur_condition_id_23], 
				formula_2,
				indicateur_3_id, 
				ARRAY[w_dbsetup.indicateur_condition_id_31,w_dbsetup.indicateur_condition_id_32,w_dbsetup.indicateur_condition_id_33], 
				formula_3,
				'1'::text AS is_cti
			FROM w_dbsetup
			WHERE expert_controle_rule_id = 0 AND
				indicateur_id > 0
			;
			
			
		]]></updateCmd>
	</TABLE>
</DBSETUPDATA>
