<?xml version="1.0" encoding="ISO-8859-1"?>
<DBSETUPDATA softCode="iCTI_eco" schema="eco">
	<!-- SCHEMA "BASE" -->
	<TABLE name="p_calendrier" label="Calendrier" schema="base" />
	<TABLE name="p_calendrier_mois" label="Calendrier (mois)" schema="base" />
	<TABLE name="t_lpp" label="LPP" schema="base" />
	<TABLE name="t_ucd" label="UCD" schema="base" />
	
	<TABLE name="t_divers" label="Divers">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_divers.code,
				t_divers.texte, 
				t_divers.valeur,
				t_divers.valeur_date::text,
				t_divers.description
			FROM 
				eco.t_divers
			WHERE t_divers.code IN ('CLE_REPARTITION', 'UPDATE PMSI')
			ORDER BY code
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
					
			-- Mise à jour des données
			INSERT INTO eco.t_divers(
				code,
				texte,
				valeur,
				valeur_date,
				description
			)
			SELECT
				code,
				texte,
				valeur,
				CASE WHEN valeur_date = '' THEN NULL::date ELSE valeur_date::date END AS valeur_date,
				description
			FROM w_dbsetup
			WHERE code NOT IN (SELECT code FROM eco.t_divers)

			
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_classification_atc" label="ATC-CLADIMED">
		<prepareCmd><![CDATA[ 
			
			-- Ajout de nouvelles colonnes sur versions précédentes
			SELECT base.cti_execute('ALTER TABLE eco.t_classification_atc ADD COLUMN est_atc text;
					ALTER TABLE eco.t_classification_atc ALTER COLUMN est_atc SET DEFAULT ''''::text;
					UPDATE eco.t_classification_atc SET est_atc = '''' WHERE est_atc IS NULL',0)
			WHERE 'est_atc' NOT IN (SELECT column_name FROM information_schema.columns WHERE table_name = 't_classification_atc');

			SELECT base.cti_execute('ALTER TABLE eco.t_classification_atc ADD COLUMN est_cladimed text;
					ALTER TABLE eco.t_classification_atc ALTER COLUMN est_cladimed SET DEFAULT ''''::text;
					UPDATE eco.t_classification_atc SET est_cladimed = '''' WHERE est_cladimed IS NULL',0)
			WHERE 'est_cladimed' NOT IN (SELECT column_name FROM information_schema.columns WHERE table_name = 't_classification_atc');
		]]></prepareCmd>
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_classification_atc.oid, 
				t_classification_atc.code_original, 
				t_classification_atc.code, 
				t_classification_atc.texte, 
				t_classification_atc.texte_court,
				t_classification_atc.est_atc,
				t_classification_atc.est_cladimed
			FROM 
				eco.t_classification_atc
			WHERE t_classification_atc.code <> t_classification_atc.texte
			ORDER BY code
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
					
			-- Mise à jour des données
			UPDATE eco.t_classification_atc SET 
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE 
				t_classification_atc.oid = w_dbsetup.oid 
				AND w_dbsetup.oid = 0;

			UPDATE eco.t_classification_atc SET 
				code = w_dbsetup.code, 
				texte = w_dbsetup.texte, 
				texte_court = w_dbsetup.texte_court,
				est_atc = w_dbsetup.est_atc,
				est_cladimed = w_dbsetup.est_cladimed
			FROM w_dbsetup
			WHERE 
				t_classification_atc.code_original = w_dbsetup.code_original
				AND (
					t_classification_atc.code IS DISTINCT FROM w_dbsetup.code OR
					t_classification_atc.texte IS DISTINCT FROM w_dbsetup.texte OR
					t_classification_atc.texte_court IS DISTINCT FROM w_dbsetup.texte_court OR
					t_classification_atc.est_atc IS DISTINCT FROM w_dbsetup.est_atc OR
					t_classification_atc.est_cladimed IS DISTINCT FROM w_dbsetup.est_cladimed
				);
			
			
			INSERT INTO eco.t_classification_atc(
				oid, 
				code_original, 
				code, 
				texte, 
				texte_court,
				est_atc,
				est_cladimed)
			SELECT 
				oid, 
				code_original, 
				code, 
				texte, 
				texte_court,
				'0',
				'0'
			FROM w_dbsetup 
			WHERE 
				oid = 0 
				AND oid NOT IN (SELECT oid FROM eco.t_classification_atc);
			
			
			INSERT INTO eco.t_classification_atc(
				code_original, 
				code, 
				texte, 
				texte_court,
				est_atc,
				est_cladimed)
			SELECT 
				w_dbsetup.code_original, 
				w_dbsetup.code,
				w_dbsetup.texte, 
				w_dbsetup.texte_court,
				w_dbsetup.est_atc,
				w_dbsetup.est_cladimed
			FROM w_dbsetup
			WHERE 
				w_dbsetup.oid <> 0 
				AND w_dbsetup.code_original NOT IN (SELECT code_original FROM eco.t_classification_atc WHERE code_original IS NOT NULL);
				
			

			SELECT eco.cti_reorganize_classification_atc();
			
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_type_article_cti" label="ATC-CLADIMED">
		
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_type_article_cti.oid, 
				t_type_article_cti.code, 
				t_type_article_cti.texte
			FROM eco.t_type_article_cti
			ORDER BY code
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
					
			-- Mise à jour des données
			UPDATE eco.t_type_article_cti SET 
				code = w_dbsetup.code
			FROM w_dbsetup
			WHERE 
				t_type_article_cti.oid = w_dbsetup.oid 
				AND w_dbsetup.oid = 0;

			UPDATE eco.t_type_article_cti SET 
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE 
				t_type_article_cti.code = w_dbsetup.code
				AND (
					t_type_article_cti.texte IS DISTINCT FROM w_dbsetup.texte
				);
			
			
			INSERT INTO eco.t_type_article_cti(
				oid, 
				code, 
				texte)
			SELECT 
				oid, 
				code,
				texte
			FROM w_dbsetup 
			WHERE 
				oid = 0 
				AND oid NOT IN (SELECT oid FROM eco.t_type_article_cti);
			
			
			INSERT INTO eco.t_type_article_cti(
				code, 
				texte)
			SELECT 
				w_dbsetup.code,
				w_dbsetup.texte
			FROM w_dbsetup
			WHERE 
				w_dbsetup.oid <> 0 
				AND w_dbsetup.code NOT IN (SELECT code FROM eco.t_type_article_cti WHERE code IS NOT NULL);
				
			
		]]></updateCmd>
	</TABLE>
	
	
	
	<TABLE name="t_listes_tables" label="Tables pour les listes">
		<selectCmd><![CDATA[ 
			SELECT oid,
				name,
				texte,
				select_cmd,
				code  
			FROM eco.t_listes_tables
			ORDER BY code;		
		]]></selectCmd>
		<updateCmd><![CDATA[ 
						UPDATE eco.t_listes_tables SET 
							name = w_dbsetup.name,
							texte = w_dbsetup.texte, 
							select_cmd = w_dbsetup.select_cmd
						FROM w_dbsetup
						WHERE t_listes_tables.code = w_dbsetup.code;

						INSERT INTO eco.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 eco.t_listes_tables);
						
						INSERT INTO eco.t_listes_tables(code, name, texte, select_cmd)
							SELECT code, name, texte, select_cmd FROM w_dbsetup WHERE oid <> 0 AND code NOT IN (SELECT code FROM eco.t_listes_tables);
		]]></updateCmd>
	</TABLE>
		 	
	<TABLE name="t_classes" label="Classifications Eco">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_classes.oid,
				t_classes.code,
				t_classes.texte,
				t_classes.is_cti,
				t_classes.sequence, 
				CASE WHEN NOT (t_classes.sequence = 30 AND t_listes_tables.code = (SELECT valeur FROM eco.t_divers WHERE code = 'CLE_REPARTITION')) THEN t_listes_tables.code ELSE 'TYA' END as table_code 
			FROM 
				eco.t_classes, 
				eco.t_listes_tables 
			WHERE 1=1
				AND (is_cti = '1'  OR (t_classes.code ilike 'CTI_%' AND t_classes.code NOT IN ('CTI_CPTLETBUD','CTI_CPTVOTE','CTI_CPTRCV','CTI_UFCR','CTI_UFUM','CTI_UFSERS','CTI_UFSITE')))
				AND table_id = t_listes_tables.oid
			ORDER BY t_classes.code;
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE eco.t_classes SET
				code = w_dbsetup.code,       
				texte = w_dbsetup.texte,       
				table_id = t_listes_tables.oid,      
				is_cti = w_dbsetup.is_cti     
			FROM 
				w_dbsetup,
				eco.t_listes_tables     
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code 
				AND t_classes.sequence = w_dbsetup.sequence 
				AND t_classes.table_id = t_listes_tables.oid ;    
			
			
			INSERT INTO eco.t_classes(code, texte, table_id, is_cti, sequence)     
			SELECT 
				w_dbsetup.code, 
				w_dbsetup.texte, 
				t_listes_tables.oid, 
				w_dbsetup.is_cti, 
				w_dbsetup.sequence      
			FROM 
				w_dbsetup, 
				eco.t_listes_tables       
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code 
				AND t_listes_tables.oid::text || '-' || w_dbsetup.sequence::text NOT IN (SELECT table_id::text || '-' || sequence::text FROM eco.t_classes) AND w_dbsetup.texte NOT LIKE '*SUPPRIME%'
				AND NOT (
					-- pour la classe de cle de rÃ©partition
					w_dbsetup.code = 'CTI_CLE_REP'
					-- la valeur de la cle est initialisÃ©e
					AND (SELECT code FROM eco.t_classes WHERE code = 'CTI_CLE_REP') IS NOT NULL 
				)
			
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_classes_sections" label="Classifications Eco (sections)">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_classes_sections.oid,
				t_classes_sections.code,
				t_classes_sections.texte,
				t_classes_sections.condition,
				t_classes_sections.include_code,
				t_classes_sections.exclude_code,
				CASE WHEN NOT (t_classes.sequence != 30 AND t_listes_tables.code = (SELECT valeur FROM eco.t_divers WHERE code = 'CLE_REPARTITION')) THEN t_listes_tables.code ELSE 'TYA' END as table_code,
				t_classes.sequence as classe_sequence				
			FROM 
				eco.t_classes_sections, 
				eco.t_classes, 
				eco.t_listes_tables 
			WHERE 1=1
				AND t_classes_sections.classe_id = t_classes.oid 
				AND table_id = t_listes_tables.oid
				AND (t_classes.is_cti = '1'  OR (t_classes.code ilike 'CTI_%' AND t_classes.code NOT IN ('CTI_CPTLETBUD','CTI_CPTVOTE','CTI_CPTRCV','CTI_UFCR','CTI_UFUM','CTI_UFSERS','CTI_UFSITE')))
			ORDER BY 
				t_listes_tables.code, 
				t_classes.sequence, 
				t_classes_sections.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			-- Sections à supprimer -> Sections CTI non-livrées
			DROP TABLE IF EXISTS w_del_sections
			;
			CREATE TEMP TABLE w_del_sections as (
				SELECT oid FROM eco.t_classes_sections 
				WHERE classe_id IN (
					SELECT
						t_classes.oid
					FROM eco.t_classes
					LEFT JOIN w_dbsetup ON w_dbsetup.code = t_classes_sections.code
					WHERE 1=1
						AND is_cti = '1'
						AND w_dbsetup.code IS NULL
				)
			);

			DELETE FROM eco.t_classes_sections_elements 
			WHERE section_id IN (SELECT oid FROM w_del_sections);
			
			
			DELETE FROM eco.t_classes_sections 
			WHERE oid IN (SELECT oid FROM w_del_sections);
			
			UPDATE eco.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, eco.t_classes, eco.t_listes_tables
			WHERE t_classes.oid = t_classes_sections.classe_id
            AND t_listes_tables.oid = t_classes.table_id
			AND w_dbsetup.code||'@'||w_dbsetup.classe_sequence||'@'||w_dbsetup.table_code = t_classes_sections.code||'@'||t_classes.sequence||'@'||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
			);
			
			INSERT INTO eco.t_classes_sections(code, texte, classe_id, include_code, exclude_code)
    			SELECT 
    				w_dbsetup.code, 
    				w_dbsetup.texte, 
    				t_classes.oid,
					w_dbsetup.include_code,
					w_dbsetup.exclude_code
    			FROM 
    				w_dbsetup 
    				JOIN eco.t_listes_tables ON (w_dbsetup.table_code = t_listes_tables.code 
					-- ou s'il s'agit de la classe des cles
						OR (classe_sequence = 30 AND t_listes_tables.code = (SELECT t_listes_tables.code FROM eco.t_listes_tables JOIN eco.t_classes ON t_classes.code = 'CTI_CLE_REP' AND table_id = t_listes_tables.oid)))
					JOIN eco.t_classes ON 1=1
						AND t_classes.table_id = t_listes_tables.oid
						AND t_classes.sequence = w_dbsetup.classe_sequence
    			WHERE w_dbsetup.code||'@'||w_dbsetup.classe_sequence||'@'|| CASE WHEN w_dbsetup.classe_sequence != 30 THEN w_dbsetup.table_code ELSE (SELECT valeur FROM eco.t_divers WHERE code = 'CLE_REPARTITION') END
					NOT IN (
					SELECT t_classes_sections.code||'@'||t_classes.sequence||'@'||CASE WHEN w_dbsetup.classe_sequence != 30 THEN t_listes_tables.code ELSE (SELECT valeur FROM eco.t_divers WHERE code = 'CLE_REPARTITION') END
					FROM eco.t_classes_sections
						JOIN eco.t_classes ON t_classes.oid = t_classes_sections.classe_id
						JOIN eco.t_listes_tables ON t_listes_tables.oid = t_classes.table_id)
				;
			
			
		]]></updateCmd>
	</TABLE>
		
	<TABLE name="t_indicateur_theme" label="Thèmes des indicateurs">
        <selectCmd><![CDATA[ 
            SELECT 
                t_indicateur_theme.oid,
                t_indicateur_theme.code,
                t_indicateur_theme.texte,
                t_indicateur_theme.level,
                COALESCE(t_indicateur_parent_theme.code,'') AS parent_code
            FROM eco.t_indicateur_theme
            LEFT JOIN eco.t_indicateur_theme t_indicateur_parent_theme ON t_indicateur_theme.parent_id = t_indicateur_parent_theme.oid
            ORDER BY 2
        ]]></selectCmd>
		<updateCmd><![CDATA[
            INSERT INTO eco.t_indicateur_theme (code, texte, level)
            SELECT code, texte, level
            FROM w_dbsetup
            WHERE code NOT IN (SELECT code FROM eco.t_indicateur_theme WHERE t_indicateur_theme.code IS NOT NULL)
            ;
            
            UPDATE eco.t_indicateur_theme SET
                texte = w_dbsetup.texte,
                level = w_dbsetup.level,
                parent_id = COALESCE(t_indicateur_parent_theme.oid,0)
            FROM w_dbsetup
            LEFT JOIN eco.t_indicateur_theme t_indicateur_parent_theme ON w_dbsetup.parent_code = t_indicateur_parent_theme.code
            WHERE t_indicateur_theme.code = w_dbsetup.code AND
                (
                t_indicateur_theme.texte IS DISTINCT FROM w_dbsetup.texte OR
                t_indicateur_theme.level IS DISTINCT FROM w_dbsetup.level OR
                t_indicateur_theme.parent_id IS DISTINCT FROM COALESCE(t_indicateur_parent_theme.oid,0)
                )
            ;
            
            
            SELECT base.cti_reorganize_indicateur_theme('eco')
            ;
		]]></updateCmd>
	</TABLE>	
	
	<TABLE 	name="t_indicateurs" label="Indicateurs de rapports">
		<selectCmd><![CDATA[ 
			SELECT t_indicateurs.oid,
				t_indicateurs.texte,
				t_indicateurs.table_name,
				t_indicateurs.column_name,
				t_indicateurs.is_cti,
				t_indicateurs.entete,
				t_indicateurs.code,
				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,
				t_indicateurs.is_template,
				t_indicateurs_template.code as indicateur_template_code,
				COALESCE(t_indicateurs.variable_definition[1],'') AS variable_definition_1,
				COALESCE(t_indicateurs.variable_definition[2],'') AS variable_definition_2,
				COALESCE(t_indicateurs.variable_definition[3],'') AS variable_definition_3,
				COALESCE(t_indicateurs.variable_value[1],'') AS variable_value_1,
				COALESCE(t_indicateurs.variable_value[2],'') AS variable_value_2,
				COALESCE(t_indicateurs.variable_value[3],'') AS variable_value_3,
				COALESCE(t_indicateurs.variable_value_text[1],'') AS variable_value_text_1,
				COALESCE(t_indicateurs.variable_value_text[2],'') AS variable_value_text_2,
				COALESCE(t_indicateurs.variable_value_text[3],'') AS variable_value_text_3,
				t_indicateurs.description,
				COALESCE(t_indicateur_theme_1.code,'') AS theme_1_code,
				COALESCE(t_indicateur_theme_2.code,'') AS theme_2_code,
				COALESCE(t_indicateur_theme_3.code,'') AS theme_3_code,
				COALESCE(t_indicateur_theme_4.code,'') AS theme_4_code,
				COALESCE(t_indicateur_theme_5.code,'') AS theme_5_code,
				COALESCE(t_indicateur_theme_6.code,'') AS theme_6_code,
				COALESCE(t_indicateur_theme_7.code,'') AS theme_7_code,
				COALESCE(t_indicateur_theme_8.code,'') AS theme_8_code,
				COALESCE(t_indicateur_theme_9.code,'') AS theme_9_code,
				t_indicateurs.is_hide
			FROM eco.t_indicateurs 
				LEFT JOIN eco.t_indicateurs t_indicateurs_1 ON (t_indicateurs.indicateur_associe_1_id = t_indicateurs_1.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_2 ON (t_indicateurs.indicateur_associe_2_id = t_indicateurs_2.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_3 ON (t_indicateurs.indicateur_associe_3_id = t_indicateurs_3.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_4 ON (t_indicateurs.indicateur_associe_4_id = t_indicateurs_4.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_5 ON (t_indicateurs.indicateur_associe_5_id = t_indicateurs_5.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_6 ON (t_indicateurs.indicateur_associe_6_id = t_indicateurs_6.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_7 ON (t_indicateurs.indicateur_associe_7_id = t_indicateurs_7.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_8 ON (t_indicateurs.indicateur_associe_8_id = t_indicateurs_8.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_9 ON (t_indicateurs.indicateur_associe_9_id = t_indicateurs_9.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_10 ON (t_indicateurs.indicateur_associe_10_id = t_indicateurs_10.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_11 ON (t_indicateurs.indicateur_associe_11_id = t_indicateurs_11.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_12 ON (t_indicateurs.indicateur_associe_12_id = t_indicateurs_12.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_13 ON (t_indicateurs.indicateur_associe_13_id = t_indicateurs_13.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_14 ON (t_indicateurs.indicateur_associe_14_id = t_indicateurs_14.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_15 ON (t_indicateurs.indicateur_associe_15_id = t_indicateurs_15.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_16 ON (t_indicateurs.indicateur_associe_16_id = t_indicateurs_16.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_17 ON (t_indicateurs.indicateur_associe_17_id = t_indicateurs_17.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_18 ON (t_indicateurs.indicateur_associe_18_id = t_indicateurs_18.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_19 ON (t_indicateurs.indicateur_associe_19_id = t_indicateurs_19.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_20 ON (t_indicateurs.indicateur_associe_20_id = t_indicateurs_20.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_reference ON (t_indicateurs.indicateur_reference_id = t_indicateurs_reference.oid) 
				LEFT JOIN eco.t_indicateurs t_indicateurs_template ON (t_indicateurs.indicateur_template_id = t_indicateurs_template.oid)
				LEFT JOIN eco.t_indicateur_theme t_indicateur_theme_1 ON t_indicateur_theme_1.oid = t_indicateurs.theme_id[1]
				LEFT JOIN eco.t_indicateur_theme t_indicateur_theme_2 ON t_indicateur_theme_2.oid = t_indicateurs.theme_id[2]
				LEFT JOIN eco.t_indicateur_theme t_indicateur_theme_3 ON t_indicateur_theme_3.oid = t_indicateurs.theme_id[3]
				LEFT JOIN eco.t_indicateur_theme t_indicateur_theme_4 ON t_indicateur_theme_4.oid = t_indicateurs.theme_id[4]
				LEFT JOIN eco.t_indicateur_theme t_indicateur_theme_5 ON t_indicateur_theme_5.oid = t_indicateurs.theme_id[5]
				LEFT JOIN eco.t_indicateur_theme t_indicateur_theme_6 ON t_indicateur_theme_6.oid = t_indicateurs.theme_id[6]
				LEFT JOIN eco.t_indicateur_theme t_indicateur_theme_7 ON t_indicateur_theme_6.oid = t_indicateurs.theme_id[7]
				LEFT JOIN eco.t_indicateur_theme t_indicateur_theme_8 ON t_indicateur_theme_6.oid = t_indicateurs.theme_id[8]
				LEFT JOIN eco.t_indicateur_theme t_indicateur_theme_9 ON t_indicateur_theme_6.oid = t_indicateurs.theme_id[9]
			WHERE 1=1
				AND t_indicateurs.is_cti = '1' 
				AND t_indicateurs.indicateur_reference_id = 0
			ORDER BY t_indicateurs.code
			;
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			DELETE FROM eco.t_indicateurs 
			USING
			(
			SELECT code, MIN(oid) AS minoid FROM eco.t_indicateurs WHERE code <> '' GROUP BY 1 HAVING count(*) > 1
			) subview 
			WHERE t_indicateurs.code = subview.code AND t_indicateurs.oid <> subview.minoid;
			
			UPDATE eco.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,
				is_template = w_dbsetup.is_template, 
				variable_definition = ARRAY[variable_definition_1,variable_definition_2,variable_definition_3] ,
				variable_value = ARRAY[variable_value_1,variable_value_2,variable_value_3],  
				variable_value_text = ARRAY[variable_value_text_1,variable_value_text_2,variable_value_text_3],  
				description = w_dbsetup.description,
				is_hide = CASE WHEN t_indicateurs.is_hide = '1' THEN '1' ELSE w_dbsetup.is_hide END
			FROM w_dbsetup
			WHERE t_indicateurs.code = w_dbsetup.code;
			
			-- Ajout des indicateurs
			INSERT INTO eco.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,
				 is_template,
				 variable_definition,
				 variable_value,
				 variable_value_text,
				 description,
				 is_hide
			)
			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,
				 is_template,
				 ARRAY[variable_definition_1,variable_definition_2,variable_definition_3] ,
				 ARRAY[variable_value_1,variable_value_2,variable_value_3],  
				 ARRAY[variable_value_text_1,variable_value_text_2,variable_value_text_3], 
				 description,
				 is_hide
			FROM w_dbsetup WHERE oid <> 0 AND code NOT IN (SELECT code FROM eco.t_indicateurs WHERE code IS NOT NULL);
			
			-- Thèmes
			UPDATE eco.t_indicateurs SET
				theme_id = ARRAY[
					t_indicateur_theme_1.oid,
					t_indicateur_theme_2.oid,
					t_indicateur_theme_3.oid,
					t_indicateur_theme_4.oid,
					t_indicateur_theme_5.oid,
					t_indicateur_theme_6.oid,
					t_indicateur_theme_7.oid,
					t_indicateur_theme_8.oid,
					t_indicateur_theme_9.oid
					]
			FROM w_dbsetup
			LEFT JOIN eco.t_indicateur_theme t_indicateur_theme_1 ON w_dbsetup.theme_1_code = t_indicateur_theme_1.code
			LEFT JOIN eco.t_indicateur_theme t_indicateur_theme_2 ON w_dbsetup.theme_2_code = t_indicateur_theme_2.code
			LEFT JOIN eco.t_indicateur_theme t_indicateur_theme_3 ON w_dbsetup.theme_3_code = t_indicateur_theme_3.code
			LEFT JOIN eco.t_indicateur_theme t_indicateur_theme_4 ON w_dbsetup.theme_4_code = t_indicateur_theme_4.code
			LEFT JOIN eco.t_indicateur_theme t_indicateur_theme_5 ON w_dbsetup.theme_5_code = t_indicateur_theme_5.code
			LEFT JOIN eco.t_indicateur_theme t_indicateur_theme_6 ON w_dbsetup.theme_6_code = t_indicateur_theme_6.code
			LEFT JOIN eco.t_indicateur_theme t_indicateur_theme_7 ON w_dbsetup.theme_7_code = t_indicateur_theme_7.code
			LEFT JOIN eco.t_indicateur_theme t_indicateur_theme_8 ON w_dbsetup.theme_8_code = t_indicateur_theme_8.code
			LEFT JOIN eco.t_indicateur_theme t_indicateur_theme_9 ON w_dbsetup.theme_9_code = t_indicateur_theme_9.code
			WHERE t_indicateurs.code = w_dbsetup.code AND
				theme_id IS DISTINCT FROM ARRAY[
					t_indicateur_theme_1.oid,
					t_indicateur_theme_2.oid,
					t_indicateur_theme_3.oid,
					t_indicateur_theme_4.oid,
					t_indicateur_theme_5.oid,
					t_indicateur_theme_6.oid,
					t_indicateur_theme_7.oid,
					t_indicateur_theme_8.oid,
					t_indicateur_theme_9.oid
					]
			 ;
			
			 -- Mise à jour des références
			UPDATE eco.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),
				indicateur_template_id = COALESCE(t_indicateurs_template.oid,0)
			FROM w_dbsetup
				LEFT JOIN eco.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 eco.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 eco.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 eco.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 eco.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 eco.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 eco.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 eco.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 eco.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 eco.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 eco.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 eco.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 eco.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 eco.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 eco.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 eco.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 eco.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 eco.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 eco.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 eco.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 eco.t_indicateurs t_indicateurs_reference ON (w_dbsetup.indicateur_reference_code = t_indicateurs_reference.code AND w_dbsetup.indicateur_reference_code <> '') 
				LEFT JOIN eco.t_indicateurs t_indicateurs_template ON (w_dbsetup.indicateur_template_code = t_indicateurs_template.code AND w_dbsetup.indicateur_template_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 <> '' OR
					indicateur_template_code <> '');
					
					
			SELECT base.cti_reorganize_indicateurs('eco')
			;
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_listes" label="Listes de codes">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_listes.oid, 
				t_listes.code,
				t_listes.texte,
				t_listes.chapitre,
				t_listes.contenu_by_code,
				t_listes.include_code,
				t_listes.exclude_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 
				eco.t_listes, 
				eco.t_listes_tables 
			WHERE 
				(is_cti = '1' OR t_listes.code LIKE 'CTI%')
				AND t_listes.code <> ''
				AND table_id = t_listes_tables.oid
			ORDER BY t_listes.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE eco.t_listes SET 
				texte = w_dbsetup.texte, 
				chapitre = w_dbsetup.chapitre, 
				contenu_by_code= w_dbsetup.contenu_by_code, 
				include_code = w_dbsetup.include_code, 
				exclude_code = w_dbsetup.exclude_code, 
				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,
				table_id = t_listes_tables.oid
			FROM 
				w_dbsetup, 
				eco.t_listes_tables
			WHERE 
				w_dbsetup.table_name = t_listes_tables.name 
				AND t_listes.code = w_dbsetup.code
			;
			
			
			INSERT INTO eco.t_listes(
				code, 
				texte, 
				chapitre,
				contenu_by_code,
				include_code,
				exclude_code,
				is_cti, 
				show_omit, 
				utilisateur_createur, 
				est_partagee_modification, 
				table_id
				)
				SELECT 
					w_dbsetup.code, 
					w_dbsetup.texte, 
					w_dbsetup.chapitre,
					w_dbsetup.contenu_by_code,
					w_dbsetup.include_code,
					w_dbsetup.exclude_code,
					w_dbsetup.is_cti, 
					w_dbsetup.show_omit, 
					w_dbsetup.utilisateur_createur, 
					w_dbsetup.est_partagee_modification, 
					t_listes_tables.oid
				FROM 
					w_dbsetup, 
					eco.t_listes_tables 
				WHERE 
					w_dbsetup.table_name = t_listes_tables.name 
					AND w_dbsetup.code NOT IN (SELECT code FROM eco.t_listes) 
					AND w_dbsetup.texte NOT LIKE '*SUPPRIME%'
			;
			
			
			DELETE FROM eco.t_listes WHERE texte like '*SUPPRIME%' AND is_cti = '1'
			;
			
			SELECT base.cti_initialize_listes_by_code('eco');
			
		]]></updateCmd>
	</TABLE>
	
	<TABLE 	name="t_rapports" label="Rapports">
		<selectCmd><![CDATA[ 
			SELECT 
				t_rapports.oid,
				t_rapports.code,
				t_rapports.texte,
				t_rapports.is_cti, 
				template.code AS template_code,
				t_rapports.is_template_updatable,
				ARRAY[
				CASE WHEN NOT (classe_0.sequence = 30 AND table_0.code = (SELECT valeur FROM eco.t_divers WHERE code = 'CLE_REPARTITION')) THEN table_0.code ELSE 'TYA' END ||'|'||classe_0.code||'|'||section_0.code,
				CASE WHEN NOT (classe_1.sequence = 30 AND table_1.code = (SELECT valeur FROM eco.t_divers WHERE code = 'CLE_REPARTITION')) THEN table_1.code ELSE 'TYA' END ||'|'||classe_1.code||'|'||section_1.code,
				CASE WHEN NOT (classe_2.sequence = 30 AND table_2.code = (SELECT valeur FROM eco.t_divers WHERE code = 'CLE_REPARTITION')) THEN table_2.code ELSE 'TYA' END ||'|'||classe_2.code||'|'||section_2.code,
				CASE WHEN NOT (classe_3.sequence = 30 AND table_3.code = (SELECT valeur FROM eco.t_divers WHERE code = 'CLE_REPARTITION')) THEN table_3.code ELSE 'TYA' END ||'|'||classe_3.code||'|'||section_3.code,
				CASE WHEN NOT (classe_4.sequence = 30 AND table_4.code = (SELECT valeur FROM eco.t_divers WHERE code = 'CLE_REPARTITION')) THEN table_4.code ELSE 'TYA' END ||'|'||classe_4.code||'|'||section_4.code] AS condition_codes,
				t_rapports.is_essentiel
			FROM eco.t_rapports 
				LEFT JOIN eco.t_rapports template ON t_rapports.rapport_template_id = template.oid

			LEFT JOIN eco.t_indicateur_condition condition_0 ON t_rapports.rapport_indicateur_condition_id[1] = condition_0.oid
			LEFT JOIN eco.t_listes_tables table_0 ON regexp_replace(regexp_replace(regexp_replace(regexp_replace(table_0.name,'^t_',''),'s_','','g'),'x',''),'s$','') || 'id' = regexp_replace(condition_0.column_name,'_','','g')
			LEFT JOIN eco.t_classes classe_0 ON table_0.oid = classe_0.table_id AND classe_0.sequence = substring(split_part(condition_0.code,'#',2),3,2) AND left(split_part(condition_0.code,'#',2),2) = 'LS'
			LEFT JOIN eco.t_classes_sections section_0 ON classe_0.oid = section_0.classe_id AND section_0.oid = right(split_part(condition_0.code,'#',2),-4)

			LEFT JOIN eco.t_indicateur_condition condition_1 ON t_rapports.rapport_indicateur_condition_id[2] = condition_1.oid
			LEFT JOIN eco.t_listes_tables table_1 ON regexp_replace(regexp_replace(regexp_replace(regexp_replace(table_1.name,'^t_',''),'s_','','g'),'x',''),'s$','') || 'id' = regexp_replace(condition_1.column_name,'_','','g')
			LEFT JOIN eco.t_classes classe_1 ON table_1.oid = classe_1.table_id AND classe_1.sequence = substring(split_part(condition_1.code,'#',2),3,2) AND left(split_part(condition_1.code,'#',2),2) = 'LS'
			LEFT JOIN eco.t_classes_sections section_1 ON classe_1.oid = section_1.classe_id AND section_1.oid = right(split_part(condition_1.code,'#',2),-4)

			LEFT JOIN eco.t_indicateur_condition condition_2 ON t_rapports.rapport_indicateur_condition_id[3] = condition_2.oid
			LEFT JOIN eco.t_listes_tables table_2 ON regexp_replace(regexp_replace(regexp_replace(regexp_replace(table_2.name,'^t_',''),'s_','','g'),'x',''),'s$','') || 'id' = regexp_replace(condition_2.column_name,'_','','g')
			LEFT JOIN eco.t_classes classe_2 ON table_2.oid = classe_2.table_id AND classe_2.sequence = substring(split_part(condition_2.code,'#',2),3,2) AND left(split_part(condition_2.code,'#',2),2) = 'LS'
			LEFT JOIN eco.t_classes_sections section_2 ON classe_2.oid = section_2.classe_id AND section_2.oid = right(split_part(condition_2.code,'#',2),-4)
			
			LEFT JOIN eco.t_indicateur_condition condition_3 ON t_rapports.rapport_indicateur_condition_id[4] = condition_3.oid
			LEFT JOIN eco.t_listes_tables table_3 ON regexp_replace(regexp_replace(regexp_replace(regexp_replace(table_3.name,'^t_',''),'s_','','g'),'x',''),'s$','') || 'id' = regexp_replace(condition_3.column_name,'_','','g')
			LEFT JOIN eco.t_classes classe_3 ON table_3.oid = classe_3.table_id AND classe_3.sequence = substring(split_part(condition_3.code,'#',2),3,2) AND left(split_part(condition_3.code,'#',2),2) = 'LS'
			LEFT JOIN eco.t_classes_sections section_3 ON classe_3.oid = section_3.classe_id AND section_3.oid = right(split_part(condition_3.code,'#',2),-4)

			LEFT JOIN eco.t_indicateur_condition condition_4 ON t_rapports.rapport_indicateur_condition_id[5] = condition_4.oid
			LEFT JOIN eco.t_listes_tables table_4 ON regexp_replace(regexp_replace(regexp_replace(regexp_replace(table_4.name,'^t_',''),'s_','','g'),'x',''),'s$','') || 'id' = regexp_replace(condition_4.column_name,'_','','g')
			LEFT JOIN eco.t_classes classe_4 ON table_4.oid = classe_4.table_id AND classe_4.sequence = substring(split_part(condition_4.code,'#',2),3,2) AND left(split_part(condition_4.code,'#',2),2) = 'LS'
			LEFT JOIN eco.t_classes_sections section_4 ON classe_4.oid = section_4.classe_id AND section_4.oid = right(split_part(condition_4.code,'#',2),-4)

			WHERE (t_rapports.is_cti = '1' OR t_rapports.code LIKE 'CTI_%') AND
				t_rapports.code <> 'CTI_PORTFO'
			ORDER BY t_rapports.code
			;
		]]></selectCmd>
		<updateCmd><![CDATA[ 	
			SELECT eco.cti_reorganize_indicateur_condition();
		
			INSERT INTO eco.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 eco.t_rapports);
			
			INSERT INTO eco.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 eco.t_rapports)
			;
		
			UPDATE eco.t_rapports SET 
				texte = w_dbsetup.texte, 
				is_cti = w_dbsetup.is_cti, 
				is_essentiel = w_dbsetup.is_essentiel,
				rapport_template_id = template.oid,
				is_template_updatable = w_dbsetup.is_template_updatable,
				rapport_indicateur_condition_id = ARRAY[condition_0.oid,condition_1.oid,condition_2.oid,condition_3.oid,condition_4.oid]
			FROM w_dbsetup
				LEFT JOIN eco.t_rapports template ON template_code = template.code
				
				LEFT JOIN eco.t_listes_tables table_0 ON split_part(condition_codes[1],'|',1) = table_0.code
					OR table_0.code = (SELECT t_listes_tables.code FROM eco.t_listes_tables JOIN eco.t_classes ON t_classes.code = 'CTI_CLE_REP' AND table_id = t_listes_tables.oid)
				LEFT JOIN eco.t_classes classe_0 ON table_0.oid = classe_0.table_id AND classe_0.code = split_part(condition_codes[1] ,'|',2)
				LEFT JOIN eco.t_classes_sections section_0 ON classe_0.oid = section_0.classe_id AND section_0.code = split_part(condition_codes[1] ,'|',3)
				LEFT JOIN eco.t_indicateur_condition condition_0 ON regexp_replace(regexp_replace(regexp_replace(regexp_replace(table_0.name,'^t_',''),'s_','','g'),'x',''),'s$','') || 'id' = regexp_replace(condition_0.column_name,'_','','g')
					AND classe_0.sequence = substring(split_part(condition_0.code,'#',2),3,2) AND left(split_part(condition_0.code,'#',2),2) = 'LS'
					AND section_0.oid = right(split_part(condition_0.code,'#',2),-4)
				
				LEFT JOIN eco.t_listes_tables table_1 ON split_part(condition_codes[2],'|',1) = table_1.code
					OR table_1.code = (SELECT t_listes_tables.code FROM eco.t_listes_tables JOIN eco.t_classes ON t_classes.code = 'CTI_CLE_REP' AND table_id = t_listes_tables.oid)
				LEFT JOIN eco.t_classes classe_1 ON table_1.oid = classe_1.table_id AND classe_1.code = split_part(condition_codes[2] ,'|',2)
				LEFT JOIN eco.t_classes_sections section_1 ON classe_1.oid = section_1.classe_id AND section_1.code = split_part(condition_codes[2] ,'|',3)
				LEFT JOIN eco.t_indicateur_condition condition_1 ON regexp_replace(regexp_replace(regexp_replace(regexp_replace(table_1.name,'^t_',''),'s_','','g'),'x',''),'s$','') || 'id' = regexp_replace(condition_1.column_name,'_','','g')
					AND classe_1.sequence = substring(split_part(condition_1.code,'#',2),3,2) AND left(split_part(condition_1.code,'#',2),2) = 'LS'
					AND section_1.oid = right(split_part(condition_1.code,'#',2),-4)
					
				LEFT JOIN eco.t_listes_tables table_2 ON split_part(condition_codes[3],'|',1) = table_2.code
					OR table_2.code = (SELECT t_listes_tables.code FROM eco.t_listes_tables JOIN eco.t_classes ON t_classes.code = 'CTI_CLE_REP' AND table_id = t_listes_tables.oid)
				LEFT JOIN eco.t_classes classe_2 ON table_2.oid = classe_2.table_id AND classe_2.code = split_part(condition_codes[3] ,'|',2)
				LEFT JOIN eco.t_classes_sections section_2 ON classe_2.oid = section_2.classe_id AND section_2.code = split_part(condition_codes[3] ,'|',3)
				LEFT JOIN eco.t_indicateur_condition condition_2 ON regexp_replace(regexp_replace(regexp_replace(regexp_replace(table_2.name,'^t_',''),'s_','','g'),'x',''),'s$','') || 'id' = regexp_replace(condition_2.column_name,'_','','g')
					AND classe_2.sequence = substring(split_part(condition_2.code,'#',2),3,2) AND left(split_part(condition_2.code,'#',2),2) = 'LS'
					AND section_2.oid = right(split_part(condition_2.code,'#',2),-4)
					
				LEFT JOIN eco.t_listes_tables table_3 ON split_part(condition_codes[4],'|',1) = table_3.code
					OR table_3.code = (SELECT t_listes_tables.code FROM eco.t_listes_tables JOIN eco.t_classes ON t_classes.code = 'CTI_CLE_REP' AND table_id = t_listes_tables.oid)
				LEFT JOIN eco.t_classes classe_3 ON table_3.oid = classe_3.table_id AND classe_3.code = split_part(condition_codes[4] ,'|',2)
				LEFT JOIN eco.t_classes_sections section_3 ON classe_3.oid = section_3.classe_id AND section_3.code = split_part(condition_codes[4] ,'|',3)
				LEFT JOIN eco.t_indicateur_condition condition_3 ON regexp_replace(regexp_replace(regexp_replace(regexp_replace(table_3.name,'^t_',''),'s_','','g'),'x',''),'s$','') || 'id' = regexp_replace(condition_3.column_name,'_','','g')
					AND classe_3.sequence = substring(split_part(condition_3.code,'#',2),3,2) AND left(split_part(condition_3.code,'#',2),2) = 'LS'
					AND section_3.oid = right(split_part(condition_3.code,'#',2),-4)
				
				LEFT JOIN eco.t_listes_tables table_4 ON split_part(condition_codes[5],'|',1) = table_4.code
					OR table_4.code = (SELECT t_listes_tables.code FROM eco.t_listes_tables JOIN eco.t_classes ON t_classes.code = 'CTI_CLE_REP' AND table_id = t_listes_tables.oid)
				LEFT JOIN eco.t_classes classe_4 ON table_4.oid = classe_4.table_id AND classe_4.code = split_part(condition_codes[5] ,'|',2)
				LEFT JOIN eco.t_classes_sections section_4 ON classe_4.oid = section_4.classe_id AND section_4.code = split_part(condition_codes[5] ,'|',3)
				LEFT JOIN eco.t_indicateur_condition condition_4 ON regexp_replace(regexp_replace(regexp_replace(regexp_replace(table_4.name,'^t_',''),'s_','','g'),'x',''),'s$','') || 'id' = regexp_replace(condition_4.column_name,'_','','g')
					AND classe_4.sequence = substring(split_part(condition_4.code,'#',2),3,2) AND left(split_part(condition_4.code,'#',2),2) = 'LS'
					AND section_4.oid = right(split_part(condition_4.code,'#',2),-4)
				WHERE t_rapports.code = w_dbsetup.code;
				
		]]></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.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.is_hide, 
				t_rapports_rubriques.hide_on_zero_field, 
				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,
                t_rapports_rubriques.description,
                t_rapports_rubriques.description_2,
                t_rapports_rubriques.entete_etendue_2,
                t_rapports_rubriques.output_format_2,
                t_rapports_rubriques.picto_enabled_2,
                t_rapports_rubriques.positif_is_good_2
			FROM 
				eco.t_rapports_rubriques
				JOIN eco.t_rapports ON t_rapports_rubriques.rapport_id = t_rapports.oid
				JOIN eco.t_indicateurs ind1 ON t_rapports_rubriques.indicateur_id = ind1.oid
				LEFT JOIN eco.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' OR t_rapports.code LIKE 'CTI_%')
				AND t_rapports.code <> 'CTI_PORTFO'
				AND ind2.is_cti IS DISTINCT FROM '0'
			ORDER BY 
				t_rapports.code, 
				ind1.code, 
				t_rapports_rubriques.numero_rubrique
			; 
			
		]]></selectCmd>
		<updateCmd><![CDATA[
            
            -- Sauvegarder les données sources pour rejouer le spécifique
            SELECT base.cti_verify_table_encoding('eco','t_rapports_rubriques')
            ;
            DROP TABLE IF EXISTS w_specific_rapports_rubriques ;
            CREATE TEMP TABLE w_specific_rapports_rubriques AS
            SELECT t_rapports_rubriques.* 
            FROM eco.t_rapports_rubriques
            JOIN eco.t_rapports ON t_rapports_rubriques.rapport_id = t_rapports.oid
            WHERE t_rapports.is_essentiel = '1'
            ;
            
            -- Installer les données CTI
			DELETE FROM eco.t_rapports_rubriques 
			WHERE 1=1
				AND rapport_id IN (SELECT oid FROM eco.t_rapports WHERE is_cti = '1')
			;

			INSERT INTO eco.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, 
				is_hide,
				hide_on_zero_field,
				viewlink, 
				viewlink_label, 
				viewlink_param, 
				picto_enabled, 
				positif_is_good,
				indicateur_associe_id,
                description,
                description_2,
                entete_etendue_2,
                output_format_2,
                picto_enabled_2,
                positif_is_good_2
				)
			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.is_hide,
				w_dbsetup.hide_on_zero_field,
				w_dbsetup.viewlink, 
				w_dbsetup.viewlink_label, 
				w_dbsetup.viewlink_param, 
				w_dbsetup.picto_enabled, 
				w_dbsetup.positif_is_good, 
				COALESCE(ind2.oid, 0),
                w_dbsetup.description,
                w_dbsetup.description_2,
                w_dbsetup.entete_etendue_2,
                w_dbsetup.output_format_2,
                w_dbsetup.picto_enabled_2,
                w_dbsetup.positif_is_good_2
			FROM 
				w_dbsetup
				JOIN eco.t_rapports ON w_dbsetup.rapport_code = t_rapports.code
				JOIN eco.t_indicateurs ind1 ON w_dbsetup.indicateur_code = ind1.code
				LEFT JOIN eco.t_indicateurs ind2 ON w_dbsetup.indicateur_associe_code = ind2.code
			WHERE ind2.is_cti IS DISTINCT FROM '0'
			;
            
            -- Rejouer le spécifique
            UPDATE eco.t_rapports_rubriques SET
                is_hide = w_specific_rapports_rubriques.is_hide,
				hide_on_zero_field = w_specific_rapports_rubriques.hide_on_zero_field,
                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>

</DBSETUPDATA>

			
