<?xml version="1.0" encoding="ISO-8859-1"?>
<DBSETUPDATA softCode="iCTI_activite" schema="activite">
	<!-- SCHEMA "BASE" -->
	<TABLE name="p_calendrier" label="Calendrier" schema="base" />
	<TABLE name="p_calendrier_mois" label="Calendrier (mois)" schema="base" />
	<TABLE name="t_modes_traitement" label="Modes de traitement" schema="base" />
	<TABLE name="t_classification_atc" label="Classification ATC" schema="base" />
	<TABLE name="t_dmt" label="DMT"	schema="base" />
	<TABLE name="t_gestes_marqueurs" label="Gestes marqueurs" schema="base" />	
	<TABLE name="t_ccam_regroupements" label="Regroupements CCAM" schema="base" />
	<TABLE name="t_actes" label="Actes" schema="base" />
	<TABLE name="t_cmd" label="CMD" schema="base" />
	<TABLE name="t_mco" label="Aso"	schema="base" />
	<TABLE name="t_cas" label="CAS"	schema="base" />
	<TABLE name="t_poles_oap" label="Poles OAP"	schema="base" />
	<TABLE name="t_lignes_oap" label="Lignes OAP" schema="base" />
	<TABLE name="t_groupes_activite" label="Groupes d'activité"	schema="base" />
	<TABLE name="t_severites_ghm" label="Complexités des GHM" schema="base" />
	<TABLE name="t_codes_postaux" label="Codes postaux"	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_ghm5" label="GHM (5)" schema="base" />
	<TABLE name="t_ghm" label="GHM" schema="base" />
	<TABLE name="t_ghs" label="GHS" schema="base" />
	<TABLE name="t_ghs_tarifs" label="Tarifs GHS" schema="base" />
	<TABLE name="t_grands_regimes" label="Grands régimes" schema="base" />
	<TABLE name="t_durees_sejour" label="Durées de séjour" schema="base" />
	<TABLE name="t_ages" label="Ages" schema="base" />
	<TABLE name="t_prestataires" label="Prestataires" schema="base" />
	
	<!-- SCHEMA "ACTIVITE" -->
    <TABLE name="t_forme_activite" label="Formes d'activité">
		<selectCmd><![CDATA[ 
			
			SELECT
                CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				code,
				texte,
				texte_court
			FROM activite.t_forme_activite
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE activite.t_forme_activite SET 
				code = w_dbsetup.code,
				texte = w_dbsetup.texte, texte_court = w_dbsetup.texte_court 
			FROM w_dbsetup
			WHERE w_dbsetup.oid = t_forme_activite.oid AND w_dbsetup.oid = 0;
			
			UPDATE activite.t_forme_activite SET
				texte = w_dbsetup.texte,
				texte_court = w_dbsetup.texte
			FROM w_dbsetup
			WHERE activite.t_forme_activite.code = w_dbsetup.code AND
                (
                activite.t_forme_activite.texte IS DISTINCT FROM w_dbsetup.texte OR
                activite.t_forme_activite.texte_court IS DISTINCT FROM w_dbsetup.texte_court
                )
			;

			INSERT INTO activite.t_forme_activite(oid, code, texte, texte_court)
				SELECT oid, code, texte, texte_court
				FROM w_dbsetup 
				WHERE oid = 0 AND
					oid NOT IN (SELECT oid FROM activite.t_forme_activite)
			;
				
			INSERT INTO activite.t_forme_activite(code, texte, texte_court)
				SELECT code, texte, texte_court
				FROM w_dbsetup 
				WHERE oid <> 0 AND 
					code NOT IN (SELECT code FROM activite.t_forme_activite WHERE code IS NOT NULL)
			;
			
			SELECT activite.cti_update_schema_classes('FORMEACTIVITE');
			
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_etat_sejour" label="Etat des séjours">
		<selectCmd><![CDATA[ 
			
			SELECT
                CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				code,
				texte
			FROM activite.t_etat_sejour
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE activite.t_etat_sejour SET 
				code = w_dbsetup.code,
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE w_dbsetup.oid = t_etat_sejour.oid AND w_dbsetup.oid = 0;
			
			UPDATE activite.t_etat_sejour SET
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE 1=1
				AND activite.t_etat_sejour.code = w_dbsetup.code
				AND activite.t_etat_sejour.texte IS DISTINCT FROM w_dbsetup.texte
			;

			INSERT INTO activite.t_etat_sejour(oid, code, texte)
				SELECT oid, code, texte
				FROM w_dbsetup 
				WHERE 1=1
					AND oid = 0 
					AND oid NOT IN (SELECT oid FROM activite.t_etat_sejour)
			;
				
			INSERT INTO activite.t_etat_sejour(code, texte)
				SELECT code, texte
				FROM w_dbsetup 
				WHERE 1=1
					AND oid <> 0 
					AND code NOT IN (SELECT code FROM activite.t_etat_sejour)
			;
			
			
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_prestations" label="Prestations hospitalières">
		<selectCmd><![CDATA[ 
			
			SELECT
                CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				code,
				texte,
				texte_court,
				norme
			FROM activite.t_prestations
			WHERE norme = 'B2' OR oid = 0
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE activite.t_prestations SET 
				code = w_dbsetup.code,
				texte = w_dbsetup.texte, texte_court = w_dbsetup.texte_court 
			FROM w_dbsetup
			WHERE w_dbsetup.oid = t_prestations.oid AND w_dbsetup.oid = 0;
			
			UPDATE activite.t_prestations SET
				texte = w_dbsetup.texte,
				texte_court = w_dbsetup.texte,
				norme = 'B2'
			FROM w_dbsetup
			WHERE 1=1
				AND activite.t_prestations.code = w_dbsetup.code
				AND activite.t_prestations.texte IS DISTINCT FROM w_dbsetup.texte
			;

			INSERT INTO activite.t_prestations(oid, code, texte, texte_court, norme)
				SELECT oid, code, texte, texte_court, norme 
				FROM w_dbsetup 
				WHERE 1=1
					AND oid = 0 
					AND oid NOT IN (SELECT oid FROM activite.t_prestations)
			;
				
			INSERT INTO activite.t_prestations(code, texte, texte_court, norme)
				SELECT code, texte, texte_court, norme 
				FROM w_dbsetup 
				WHERE 1=1
					AND oid <> 0 
					AND code NOT IN (SELECT code FROM activite.t_prestations)
			;
			
			SELECT activite.cti_update_schema_classes('PRESTA_C');
			
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_listes_tables" label="Tables pour les listes">
		<selectCmd><![CDATA[ 
			
			SELECT
                CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				name,
				texte,
				select_cmd,
				code 
			FROM activite.t_listes_tables
			ORDER BY code
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE activite.t_listes_tables SET code = 'PRESTA_C_B' WHERE oid =
			(SELECT MIN(oid) FROM activite.t_listes_tables WHERE code = 'PRESTA_C') 
			AND (SELECT count(*) FROM activite.t_listes_tables WHERE code = 'PRESTA_C') > 1;

			UPDATE activite.t_listes SET table_id = (SELECT oid FROM  activite.t_listes_tables WHERE code = 'PRESTA_C') 
			WHERE table_id IN (SELECT oid FROM activite.t_listes_tables WHERE code = 'PRESTA_C_B');

			DELETE FROM activite.t_listes_tables WHERE code = 'PRESTA_C_B';
			
			UPDATE activite.t_listes_tables SET 
				code = w_dbsetup.code,
				texte = w_dbsetup.texte, 
				select_cmd = w_dbsetup.select_cmd,
				name = w_dbsetup.name
			FROM w_dbsetup
			WHERE t_listes_tables.code = w_dbsetup.code;

			INSERT INTO activite.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 activite.t_listes_tables);
			
			INSERT INTO activite.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 activite.t_listes_tables);
			
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_listes" label="Listes de codes">
		<selectCmd><![CDATA[ 
			
			SELECT 
				CASE WHEN t_listes.oid = 0 THEN t_listes.oid ELSE 1 END AS oid,
				t_listes.texte, 
				t_listes.chapitre,
				t_listes.code, 
				t_listes.is_cti, 
				t_listes.show_omit, 
				'CTI'::text AS utilisateur_createur, 
				t_listes.est_partagee_modification,
				t_listes_tables.name as table_name,
                t_listes.contenu_by_code,
                t_listes.exclude_code,
                t_listes.include_code,
                t_listes.contenu_non_replicable_ailleurs
			FROM activite.t_listes, activite.t_listes_tables 
			WHERE (is_cti = '1' OR t_listes.code LIKE 'CTI_%')  and table_id = t_listes_tables.oid
			ORDER BY 4
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE activite.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,
                -- contenu_by_code = w_dbsetup.contenu_by_code,
                exclude_code = w_dbsetup.exclude_code,
                include_code = w_dbsetup.include_code,
                contenu_non_replicable_ailleurs = w_dbsetup.contenu_non_replicable_ailleurs
			FROM w_dbsetup, activite.t_listes_tables
			WHERE 1=1
                AND w_dbsetup.table_name = t_listes_tables.name
                AND t_listes.code = w_dbsetup.code
            ;
			
			INSERT INTO activite.t_listes(code, texte, chapitre, table_id, is_cti, show_omit, contenu_by_code, exclude_code, include_code, contenu_non_replicable_ailleurs)
                SELECT w_dbsetup.code, w_dbsetup.texte, w_dbsetup.chapitre, t_listes_tables.oid, w_dbsetup.is_cti, w_dbsetup.show_omit, w_dbsetup.contenu_by_code, w_dbsetup.exclude_code, w_dbsetup.include_code, w_dbsetup.contenu_non_replicable_ailleurs
                    FROM w_dbsetup, activite.t_listes_tables 
                    WHERE w_dbsetup.table_name = t_listes_tables.name AND w_dbsetup.code NOT IN (SELECT code FROM activite.t_listes) AND w_dbsetup.texte NOT LIKE '*SUPPRIME%'
            ;
				
			DELETE FROM activite.t_listes WHERE texte like '*SUPPRIME%' AND is_cti = '1'
            ;
			
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_classes" label="Classifications Activité">
		<selectCmd><![CDATA[ 
			
			SELECT 
				CASE WHEN t_classes.oid = 0 THEN t_classes.oid ELSE 1 END AS oid,
				t_classes.code,
				t_classes.texte,
				t_classes.is_cti,
				t_classes.sequence, 
				t_listes_tables.code as table_code,
                t_classes.contenu_by_code,
                t_classes.contenu_non_replicable_ailleurs
			FROM 
				activite.t_classes, 
				activite.t_listes_tables 
			WHERE 1=1
				AND (is_cti = '1'  OR t_classes.code ilike 'CTI_%')
				AND table_id = t_listes_tables.oid 
			ORDER BY t_classes.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE activite.t_classes SET
				code = w_dbsetup.code,       
				texte = w_dbsetup.texte,       
				table_id = t_listes_tables.oid,      
				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,
				activite.t_listes_tables     
			WHERE 1=1
				AND w_dbsetup.table_code = t_listes_tables.code 
				AND t_classes.sequence = w_dbsetup.sequence 
				AND t_classes.table_id = t_listes_tables.oid
                AND (1!=1
                    OR t_classes.code IS DISTINCT FROM w_dbsetup.code     
                    OR t_classes.texte IS DISTINCT FROM w_dbsetup.texte
                    OR t_classes.table_id IS DISTINCT FROM t_listes_tables.oid
                    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 activite.t_classes(code, texte, table_id, is_cti, sequence, contenu_by_code, contenu_non_replicable_ailleurs)
                SELECT 
                    w_dbsetup.code, 
                    w_dbsetup.texte, 
                    t_listes_tables.oid, 
                    w_dbsetup.is_cti, 
                    w_dbsetup.sequence,
                    w_dbsetup.contenu_by_code,
                    w_dbsetup.contenu_non_replicable_ailleurs
                FROM 
                    w_dbsetup, 
                    activite.t_listes_tables       
                WHERE 1=1
                    AND 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 activite.t_classes)
                    AND w_dbsetup.texte NOT LIKE '*SUPPRIME%'
            ;
			
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_classes_sections" label="Classifications Activité (sections)">
		<selectCmd><![CDATA[ 
			
			SELECT 
				CASE WHEN t_classes_sections.oid = 0 THEN t_classes_sections.oid ELSE 1 END AS oid,
				t_classes_sections.code,
				t_classes_sections.texte,
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence,
                t_classes_sections.condition,
                t_classes_sections.exclude_code,
                t_classes_sections.include_code
			FROM 
				activite.t_classes_sections, 
				activite.t_classes, 
				activite.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 LIKE 'CTI_%')
			ORDER BY 
				t_listes_tables.code, 
				t_classes.sequence, 
				t_classes_sections.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 

			-- Implémentation oids
			ALTER TABLE w_dbsetup ADD COLUMN table_id bigint DEFAULT 0
			;
			UPDATE w_dbsetup
			SET table_id = t_listes_tables.oid
			FROM activite.t_listes_tables
			WHERE table_code = t_listes_tables.code
			;
			ALTER TABLE w_dbsetup ADD COLUMN classe_id bigint DEFAULT 0
			;
			UPDATE w_dbsetup
			SET classe_id = t_classes.oid
			FROM activite.t_classes
			WHERE w_dbsetup.classe_sequence = t_classes.sequence AND
				w_dbsetup.table_id = t_classes.table_id
			;


			-- Sections à supprimer -> Sections CTI non-livrées
			-- La classe CTI_SPE_BUDG est créée et aliméntée lors de l'import activité et n'est pas livrée
			DROP TABLE IF EXISTS w_del_sections
			;
			CREATE TEMP TABLE w_del_sections as (
				SELECT oid FROM activite.t_classes_sections 
				WHERE classe_id IN (
					SELECT
						t_classes.oid
					FROM activite.t_classes
					WHERE 1=1
						AND is_cti = '1'
						AND t_classes.code <> 'CTI_SPE_BUDG'
				)
			);

			DELETE FROM activite.t_classes_sections_elements 
			WHERE section_id IN (SELECT oid FROM w_del_sections);


			DELETE FROM activite.t_classes_sections
			USING
			(
			SELECT t_classes_sections.classe_id, t_classes_sections.oid
			FROM activite.t_classes_sections
			LEFT JOIN w_dbsetup ON
				w_dbsetup.classe_id = t_classes_sections.classe_id AND
				w_dbsetup.code = t_classes_sections.code
			WHERE w_dbsetup.oid IS NULL AND
				t_classes_sections.classe_id IN (SELECT classe_id FROM w_dbsetup) AND
				t_classes_sections.oid NOT IN (SELECT section_id FROM activite.t_classes_sections_elements)
			) subview
			WHERE subview.classe_id = t_classes_sections.classe_id AND
				subview.oid = t_classes_sections.oid
			;

			-- Mise à jour
			UPDATE activite.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
			WHERE w_dbsetup.classe_id = t_classes_sections.classe_id AND
				w_dbsetup.code = t_classes_sections.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 activite.t_classes_sections(code, texte, classe_id, condition, exclude_code, include_code)
			SELECT 
				w_dbsetup.code, 
				w_dbsetup.texte, 
				w_dbsetup.classe_id,
				w_dbsetup.condition,
				w_dbsetup.exclude_code,
				w_dbsetup.include_code
			FROM w_dbsetup 
			WHERE w_dbsetup.classe_id > 0 AND
				(w_dbsetup.code||'@'||w_dbsetup.classe_id::text)
				NOT IN (SELECT t_classes_sections.code||'@'||t_classes_sections.classe_id FROM activite.t_classes_sections)
			;
			
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_classes_sections_elements_ghm" label="Classifications Activité (GHM)">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_classes_sections.code AS section_code, 
				t_ghm.code as to_code, 
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence 
			FROM 
				activite.t_classes_sections_elements, 
				activite.t_classes_sections,
				activite.t_classes, 
				activite.t_listes_tables,
				base.t_ghm
			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 = 'GHM'
				AND t_classes_sections_elements.to_id = t_ghm.oid
			ORDER BY t_classes.sequence, t_classes_sections.code, t_ghm.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO activite.t_classes_sections_elements(section_id, to_id)     
			SELECT t_classes_sections.oid, t_ghm.oid 
			FROM w_dbsetup, activite.t_listes_tables, activite.t_classes, activite.t_classes_sections, base.t_ghm   
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code       
				AND t_classes.table_id = t_listes_tables.oid   
				AND t_classes_sections.classe_id = t_classes.oid   
				AND w_dbsetup.section_code = t_classes_sections.code
				AND w_dbsetup.classe_sequence = t_classes.sequence
				AND w_dbsetup.to_code = t_ghm.code
				AND t_classes_sections.oid::text || '-' || t_ghm.oid::text NOT IN (SELECT section_id::text || '-' || to_id::text FROM activite.t_classes_sections_elements);
			
			
			SELECT * FROM activite.cti_update_schema_classes('GHM');
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_classes_sections_elements_duree_sejour" label="Classifications Activité (durees_sejour)">
		<selectCmd><![CDATA[ 
			
			SELECT 
				t_classes_sections.code AS section_code, 
				t_durees_sejour.code as to_code, 
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence 
			FROM 
				activite.t_classes_sections_elements, 
				activite.t_classes_sections,
				activite.t_classes, 
				activite.t_listes_tables,
				base.t_durees_sejour
			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 = 'DURSEJ'
				AND t_classes_sections_elements.to_id = t_durees_sejour.oid
			ORDER BY t_classes.sequence, t_classes_sections.code, t_durees_sejour.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO activite.t_classes_sections_elements(section_id, to_id)     
			SELECT 
				t_classes_sections.oid, 
				t_durees_sejour.oid 
			FROM 
				w_dbsetup, 
				activite.t_listes_tables, 
				activite.t_classes, 
				activite.t_classes_sections, 
				base.t_durees_sejour   
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code       
				AND t_classes.table_id = t_listes_tables.oid   
				AND t_classes_sections.classe_id = t_classes.oid   
				AND w_dbsetup.section_code = t_classes_sections.code
				AND w_dbsetup.classe_sequence = t_classes.sequence
				AND w_dbsetup.to_code = t_durees_sejour.code
				AND t_classes_sections.oid::text || '-' || t_durees_sejour.oid::text NOT IN (SELECT section_id::text || '-' || to_id::text FROM activite.t_classes_sections_elements);
			
			
			SELECT * FROM activite.cti_update_schema_classes('DURSEJ');
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_classes_sections_elements_ages" label="Classifications Activité (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.sequence as classe_sequence 
			FROM 
				activite.t_classes_sections_elements, 
				activite.t_classes_sections, 
				activite.t_classes, 
				activite.t_listes_tables, 
				base.t_ages
			WHERE 1=1
				AND 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.sequence, t_classes_sections.code, t_ages.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO activite.t_classes_sections_elements(section_id, to_id)     
			SELECT 
				t_classes_sections.oid, 
				t_ages.oid 
			FROM 
				w_dbsetup, 
				activite.t_listes_tables, 
				activite.t_classes, 
				activite.t_classes_sections, 
				base.t_ages   
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code       
				AND t_classes.table_id = t_listes_tables.oid   
				AND t_classes_sections.classe_id = t_classes.oid   
				AND w_dbsetup.section_code = t_classes_sections.code
				AND w_dbsetup.classe_sequence = t_classes.sequence
				AND w_dbsetup.to_code = t_ages.code
				AND t_classes_sections.oid::text || '-' || t_ages.oid::text NOT IN (SELECT section_id::text || '-' || to_id::text FROM activite.t_classes_sections_elements);
			
			SELECT * FROM activite.cti_update_schema_classes('AGE');
			
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_classes_sections_elements_codes_postaux" label="Classifications Activité (codes_postaux)">
		<selectCmd><![CDATA[ 
			
			SELECT
				t_classes_sections.code AS section_code, 
				t_codes_postaux.code as to_code, 
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence 
			FROM 
				activite.t_classes_sections_elements, 
				activite.t_classes_sections, 
				activite.t_classes, 
				activite.t_listes_tables, 
				base.t_codes_postaux
			WHERE 1=1
				AND 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 = 'CODEPOST'
				AND t_classes_sections_elements.to_id = t_codes_postaux.oid
			ORDER BY t_classes.sequence, t_classes_sections.code, t_codes_postaux.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO activite.t_classes_sections_elements(section_id, to_id)     
			SELECT 
				t_classes_sections.oid, 
				t_codes_postaux.oid 
			FROM 
				w_dbsetup, 
				activite.t_listes_tables, 
				activite.t_classes, 
				activite.t_classes_sections, 
				base.t_codes_postaux   
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code       
				AND t_classes.table_id = t_listes_tables.oid   
				AND t_classes_sections.classe_id = t_classes.oid   
				AND w_dbsetup.section_code = t_classes_sections.code
				AND w_dbsetup.classe_sequence = t_classes.sequence
				AND w_dbsetup.to_code = t_codes_postaux.code
				AND t_classes_sections.oid::text || '-' || t_codes_postaux.oid::text NOT IN 
					(SELECT section_id::text || '-' || to_id::text FROM activite.t_classes_sections_elements);
			
			SELECT * FROM activite.cti_update_schema_classes('CODEPOST');
			
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_classes_sections_elements_prestations_c" label="Classifications Activité (prestations clinique)">
		<selectCmd><![CDATA[ 
			
			SELECT
				t_classes_sections.code AS section_code, 
				t_prestations.code as to_code, 
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence 
			FROM 
				activite.t_classes_sections_elements, 
				activite.t_classes_sections, 
				activite.t_classes, 
				activite.t_listes_tables, 
				activite.t_prestations
			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 = 'PRESTA_C'
				AND t_classes_sections_elements.to_id = t_prestations.oid
			ORDER BY t_classes.sequence, t_classes_sections.code, t_prestations.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO activite.t_classes_sections_elements(section_id, to_id)     
			SELECT 
				t_classes_sections.oid, 
				t_prestations.oid 
			FROM 
				w_dbsetup, 
				activite.t_listes_tables, 
				activite.t_classes, 
				activite.t_classes_sections, 
				activite.t_prestations   
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code       
				AND t_classes.table_id = t_listes_tables.oid   
				AND t_classes_sections.classe_id = t_classes.oid   
				AND w_dbsetup.section_code = t_classes_sections.code
				AND w_dbsetup.classe_sequence = t_classes.sequence
				AND w_dbsetup.to_code = t_prestations.code
				AND t_classes_sections.oid::text || '-'::text || t_prestations.oid::text NOT IN (SELECT section_id::text || '-'::text || to_id::text FROM activite.t_classes_sections_elements);
			
			SELECT * FROM activite.cti_update_schema_classes('PRESTA_C');
			
		]]></updateCmd>
	</TABLE>

    <TABLE name="t_classes_sections_elements_forme_activite" label="Classifications Activité (Formes d'activité)">
		<selectCmd><![CDATA[ 
			
			SELECT
				t_classes_sections.code AS section_code, 
				t_forme_activite.code as to_code, 
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence 
			FROM 
				activite.t_classes_sections_elements, 
				activite.t_classes_sections, 
				activite.t_classes, 
				activite.t_listes_tables, 
				activite.t_forme_activite
			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 = 'FORMEACTIVITE'
				AND t_classes_sections_elements.to_id = t_forme_activite.oid
			ORDER BY t_classes.sequence, t_classes_sections.code, t_forme_activite.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO activite.t_classes_sections_elements(section_id, to_id)     
			SELECT 
				t_classes_sections.oid, 
				t_forme_activite.oid 
			FROM 
				w_dbsetup, 
				activite.t_listes_tables, 
				activite.t_classes, 
				activite.t_classes_sections, 
				activite.t_forme_activite   
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code       
				AND t_classes.table_id = t_listes_tables.oid   
				AND t_classes_sections.classe_id = t_classes.oid   
				AND w_dbsetup.section_code = t_classes_sections.code
				AND w_dbsetup.classe_sequence = t_classes.sequence
				AND w_dbsetup.to_code = t_forme_activite.code
				AND t_classes_sections.oid::text || '-'::text || t_forme_activite.oid::text NOT IN (SELECT section_id::text || '-'::text || to_id::text FROM activite.t_classes_sections_elements);
			
			SELECT * FROM activite.cti_update_schema_classes('FORMEACTIVITE');
			
		]]></updateCmd>
	</TABLE>    
    <TABLE name="t_classes_sections_elements_etat_sejour" label="état des séjours">
		<selectCmd><![CDATA[ 
			
			SELECT
				t_classes_sections.code AS section_code, 
				t_etat_sejour.code as to_code, 
				t_listes_tables.code as table_code, 
				t_classes.sequence as classe_sequence 
			FROM 
				activite.t_classes_sections_elements, 
				activite.t_classes_sections, 
				activite.t_classes, 
				activite.t_listes_tables, 
				activite.t_etat_sejour
			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 = 'ETATSEJOUR'
				AND t_classes_sections_elements.to_id = t_etat_sejour.oid
			ORDER BY t_classes.sequence, t_classes_sections.code, t_etat_sejour.code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO activite.t_classes_sections_elements(section_id, to_id)     
			SELECT 
				t_classes_sections.oid, 
				t_etat_sejour.oid 
			FROM 
				w_dbsetup, 
				activite.t_listes_tables, 
				activite.t_classes, 
				activite.t_classes_sections, 
				activite.t_etat_sejour   
			WHERE 
				w_dbsetup.table_code = t_listes_tables.code       
				AND t_classes.table_id = t_listes_tables.oid   
				AND t_classes_sections.classe_id = t_classes.oid   
				AND w_dbsetup.section_code = t_classes_sections.code
				AND w_dbsetup.classe_sequence = t_classes.sequence
				AND w_dbsetup.to_code = t_etat_sejour.code
				AND t_classes_sections.oid::text || '-'::text || t_etat_sejour.oid::text NOT IN (SELECT section_id::text || '-'::text || to_id::text FROM activite.t_classes_sections_elements);
			
			SELECT * FROM activite.cti_update_schema_classes('ETATSEJOUR');
			
		]]></updateCmd>
	</TABLE>
    
    <TABLE name="t_indicateur_theme" label="Thèmes des indicateurs">
        <selectCmd><![CDATA[ 
            SELECT 
				CASE WHEN t_indicateur_theme.oid = 0 THEN t_indicateur_theme.oid ELSE 1 END AS oid,
                t_indicateur_theme.code,
                t_indicateur_theme.texte,
                t_indicateur_theme.level,
                COALESCE(t_indicateur_parent_theme.code,'') AS parent_code
            FROM activite.t_indicateur_theme
            LEFT JOIN activite.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 activite.t_indicateur_theme (code, texte, level)
            SELECT code, texte, level
            FROM w_dbsetup
            WHERE code NOT IN (SELECT code FROM activite.t_indicateur_theme WHERE t_indicateur_theme.code IS NOT NULL)
            ;
            
            UPDATE activite.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 activite.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('activite')
            ;
		]]></updateCmd>
	</TABLE>	
	
	<TABLE name="t_indicateurs" label="Indicateurs de rapports">
		<selectCmd><![CDATA[ 
			
			SELECT 
				CASE WHEN t_indicateurs.oid = 0 THEN t_indicateurs.oid ELSE 1 END AS oid,
                t_indicateurs.code,
                t_indicateurs.table_name,
                t_indicateurs.texte,
                t_indicateurs.entete_etendue,
                t_indicateurs.entete,
                t_indicateurs.column_name,
                t_indicateurs.is_cti,
                t_indicateurs.total_function,
                t_indicateurs.output_format,
                t_indicateurs.width,
                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
            FROM activite.t_indicateurs 
                LEFT JOIN activite.t_indicateurs t_indicateurs_1 ON (t_indicateurs.indicateur_associe_1_id = t_indicateurs_1.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_2 ON (t_indicateurs.indicateur_associe_2_id = t_indicateurs_2.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_3 ON (t_indicateurs.indicateur_associe_3_id = t_indicateurs_3.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_4 ON (t_indicateurs.indicateur_associe_4_id = t_indicateurs_4.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_5 ON (t_indicateurs.indicateur_associe_5_id = t_indicateurs_5.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_6 ON (t_indicateurs.indicateur_associe_6_id = t_indicateurs_6.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_7 ON (t_indicateurs.indicateur_associe_7_id = t_indicateurs_7.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_8 ON (t_indicateurs.indicateur_associe_8_id = t_indicateurs_8.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_9 ON (t_indicateurs.indicateur_associe_9_id = t_indicateurs_9.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_10 ON (t_indicateurs.indicateur_associe_10_id = t_indicateurs_10.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_11 ON (t_indicateurs.indicateur_associe_11_id = t_indicateurs_11.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_12 ON (t_indicateurs.indicateur_associe_12_id = t_indicateurs_12.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_13 ON (t_indicateurs.indicateur_associe_13_id = t_indicateurs_13.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_14 ON (t_indicateurs.indicateur_associe_14_id = t_indicateurs_14.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_15 ON (t_indicateurs.indicateur_associe_15_id = t_indicateurs_15.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_16 ON (t_indicateurs.indicateur_associe_16_id = t_indicateurs_16.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_17 ON (t_indicateurs.indicateur_associe_17_id = t_indicateurs_17.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_18 ON (t_indicateurs.indicateur_associe_18_id = t_indicateurs_18.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_19 ON (t_indicateurs.indicateur_associe_19_id = t_indicateurs_19.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_20 ON (t_indicateurs.indicateur_associe_20_id = t_indicateurs_20.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_reference ON (t_indicateurs.indicateur_reference_id = t_indicateurs_reference.oid) 
                LEFT JOIN activite.t_indicateurs t_indicateurs_template ON (t_indicateurs.indicateur_template_id = t_indicateurs_template.oid)
                LEFT JOIN activite.t_indicateur_theme t_indicateur_theme_1 ON t_indicateur_theme_1.oid = t_indicateurs.theme_id[1]
                LEFT JOIN activite.t_indicateur_theme t_indicateur_theme_2 ON t_indicateur_theme_2.oid = t_indicateurs.theme_id[2]
                LEFT JOIN activite.t_indicateur_theme t_indicateur_theme_3 ON t_indicateur_theme_3.oid = t_indicateurs.theme_id[3]
                LEFT JOIN activite.t_indicateur_theme t_indicateur_theme_4 ON t_indicateur_theme_4.oid = t_indicateurs.theme_id[4]
                LEFT JOIN activite.t_indicateur_theme t_indicateur_theme_5 ON t_indicateur_theme_5.oid = t_indicateurs.theme_id[5]
                LEFT JOIN activite.t_indicateur_theme t_indicateur_theme_6 ON t_indicateur_theme_6.oid = t_indicateurs.theme_id[6]
                LEFT JOIN activite.t_indicateur_theme t_indicateur_theme_7 ON t_indicateur_theme_6.oid = t_indicateurs.theme_id[7]
                LEFT JOIN activite.t_indicateur_theme t_indicateur_theme_8 ON t_indicateur_theme_6.oid = t_indicateurs.theme_id[8]
                LEFT JOIN activite.t_indicateur_theme t_indicateur_theme_9 ON t_indicateur_theme_6.oid = t_indicateurs.theme_id[9]
            WHERE t_indicateurs.is_cti = '1' AND 
                t_indicateurs.code LIKE 'CTI%' AND 
                t_indicateurs.indicateur_reference_id = 0
            ORDER BY t_indicateurs.code
            ;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
            -- Suppression des indicateurs en double
			DELETE FROM activite.t_indicateurs 
			USING
			(
			SELECT code, MIN(oid) AS minoid FROM activite.t_indicateurs WHERE code <> '' GROUP BY 1 HAVING count(*) > 1
			) subview 
			WHERE t_indicateurs.code = subview.code AND t_indicateurs.oid <> subview.minoid;
			
            -- Mise à jour indicateurs déjà présents
			UPDATE activite.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                 
			FROM w_dbsetup
			WHERE t_indicateurs.code = w_dbsetup.code;

            -- Ajout des indicateurs
			INSERT INTO activite.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
            )
            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
            FROM w_dbsetup WHERE oid <> 0 AND code NOT IN (SELECT code FROM activite.t_indicateurs WHERE code IS NOT NULL)
            ;
			
            CREATE INDEX i_w_dbsetup
            ON w_dbsetup
            USING btree
            (code)
            ;

            ANALYZE w_dbsetup
            ;
            
            -- Mise à jour des références
			UPDATE activite.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 activite.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 activite.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 activite.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 activite.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 activite.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 activite.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 activite.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 activite.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 activite.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 activite.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 activite.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 activite.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 activite.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 activite.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 activite.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 activite.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 activite.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 activite.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 activite.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 activite.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 activite.t_indicateurs t_indicateurs_reference ON (w_dbsetup.indicateur_reference_code = t_indicateurs_reference.code AND w_dbsetup.indicateur_reference_code <> '') 
                LEFT JOIN activite.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 <> '');
                    
            -- Thèmes
            UPDATE activite.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 activite.t_indicateur_theme t_indicateur_theme_1 ON w_dbsetup.theme_1_code = t_indicateur_theme_1.code
            LEFT JOIN activite.t_indicateur_theme t_indicateur_theme_2 ON w_dbsetup.theme_2_code = t_indicateur_theme_2.code
            LEFT JOIN activite.t_indicateur_theme t_indicateur_theme_3 ON w_dbsetup.theme_3_code = t_indicateur_theme_3.code
            LEFT JOIN activite.t_indicateur_theme t_indicateur_theme_4 ON w_dbsetup.theme_4_code = t_indicateur_theme_4.code
            LEFT JOIN activite.t_indicateur_theme t_indicateur_theme_5 ON w_dbsetup.theme_5_code = t_indicateur_theme_5.code
            LEFT JOIN activite.t_indicateur_theme t_indicateur_theme_6 ON w_dbsetup.theme_6_code = t_indicateur_theme_6.code
            LEFT JOIN activite.t_indicateur_theme t_indicateur_theme_7 ON w_dbsetup.theme_7_code = t_indicateur_theme_7.code
            LEFT JOIN activite.t_indicateur_theme t_indicateur_theme_8 ON w_dbsetup.theme_8_code = t_indicateur_theme_8.code
            LEFT JOIN activite.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
                    ]
             ;
					
			-- Réorganisation des indicateurs		
			SELECT base.cti_reorganize_indicateurs('activite');
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_rapports" label="Rapports">
		<selectCmd><![CDATA[ 
			
			SELECT 
				CASE WHEN t_rapports.oid = 0 THEN t_rapports.oid ELSE 1 END AS oid,
				t_rapports.code, 
				t_rapports.texte, 
				t_rapports.is_cti, 
				t_rapports.is_essentiel, 
				t_rapports.essentiel_date_level, 
				t_rapports.colonnes_essentiel, 
				t_rapports.entete, 
				t_rapports.entete_2, 
				CASE WHEN t_rapports.group_id = 0 THEN t_rapports.group_id ELSE 1 END AS group_id,
				t_rapports.is_hide, 
				array_to_string(
					ARRAY[
					COALESCE(t_indicateur_condition_1.external_code::text,''),
					COALESCE(t_indicateur_condition_2.external_code::text,''),
					COALESCE(t_indicateur_condition_3.external_code::text,''),
					COALESCE(t_indicateur_condition_4.external_code::text,'')
					]
					,'||') AS rapport_indicateur_condition_code, 
				t_rapports.label, 
				array_to_string(serie_entete,'||') AS serie_entete, 
				t_rapports.colonnes_essentiel_style
			FROM activite.t_rapports 
			LEFT JOIN activite.t_indicateur_condition t_indicateur_condition_1 ON t_rapports.rapport_indicateur_condition_id[1] = t_indicateur_condition_1.oid
			LEFT JOIN activite.t_indicateur_condition t_indicateur_condition_2 ON t_rapports.rapport_indicateur_condition_id[2] = t_indicateur_condition_2.oid
			LEFT JOIN activite.t_indicateur_condition t_indicateur_condition_3 ON t_rapports.rapport_indicateur_condition_id[3] = t_indicateur_condition_3.oid
			LEFT JOIN activite.t_indicateur_condition t_indicateur_condition_4 ON t_rapports.rapport_indicateur_condition_id[4] = t_indicateur_condition_4.oid
			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[ 
		
			
			ALTER TABLE w_dbsetup ADD COLUMN to_serie_entete text[];
			UPDATE w_dbsetup SET to_serie_entete = string_to_array(serie_entete,'||');
			
			
			ALTER TABLE w_dbsetup ADD COLUMN to_rapport_indicateur_condition_code text[];
			UPDATE w_dbsetup SET to_rapport_indicateur_condition_code = string_to_array(rapport_indicateur_condition_code,'||');
			
			ALTER TABLE w_dbsetup ADD COLUMN to_rapport_indicateur_condition_id bigint[];
			UPDATE w_dbsetup SET to_rapport_indicateur_condition_id[1] = t_indicateur_condition.oid FROM activite.t_indicateur_condition WHERE t_indicateur_condition.code = to_rapport_indicateur_condition_code[1] AND to_rapport_indicateur_condition_code[1] <> '';
			UPDATE w_dbsetup SET to_rapport_indicateur_condition_id[2] = t_indicateur_condition.oid FROM activite.t_indicateur_condition WHERE t_indicateur_condition.code = to_rapport_indicateur_condition_code[2] AND to_rapport_indicateur_condition_code[2] <> '';
			UPDATE w_dbsetup SET to_rapport_indicateur_condition_id[3] = t_indicateur_condition.oid FROM activite.t_indicateur_condition WHERE t_indicateur_condition.code = to_rapport_indicateur_condition_code[3] AND to_rapport_indicateur_condition_code[3] <> '';
			UPDATE w_dbsetup SET to_rapport_indicateur_condition_id[4] = t_indicateur_condition.oid FROM activite.t_indicateur_condition WHERE t_indicateur_condition.code = to_rapport_indicateur_condition_code[4] AND to_rapport_indicateur_condition_code[4] <> '';
			UPDATE w_dbsetup SET to_rapport_indicateur_condition_id = array_remove(to_rapport_indicateur_condition_id,NULL);
			
			UPDATE activite.t_rapports SET 
				texte = w_dbsetup.texte, 
				is_cti = w_dbsetup.is_cti,
				is_essentiel = w_dbsetup.is_essentiel,
				essentiel_date_level = w_dbsetup.essentiel_date_level,
				colonnes_essentiel = w_dbsetup.colonnes_essentiel,
				entete = w_dbsetup.entete,
				entete_2 = w_dbsetup.entete_2,
				group_id  = w_dbsetup.group_id,
				rapport_indicateur_condition_id = to_rapport_indicateur_condition_id,
				label  = w_dbsetup.label,
				serie_entete  = to_serie_entete,
				colonnes_essentiel_style = w_dbsetup.colonnes_essentiel_style
			FROM w_dbsetup
			WHERE t_rapports.code = w_dbsetup.code;
			
			
			INSERT INTO activite.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 activite.t_rapports);
			
			
			INSERT INTO activite.t_rapports(
				code, 
				texte, 
				is_cti, 
				is_essentiel, 
				essentiel_date_level, 
				colonnes_essentiel, 
				entete, 
				entete_2, 
				group_id, 
				is_hide, 
				rapport_indicateur_condition_id, 
				label, 
				serie_entete, 
				colonnes_essentiel_style
				)
			SELECT 
				code, 
				texte, 
				is_cti, 
				is_essentiel, 
				essentiel_date_level, 
				colonnes_essentiel, 
				entete, 
				entete_2, 
				group_id, 
				is_hide, 
				to_rapport_indicateur_condition_id, 
				label, 
				to_serie_entete, 
				colonnes_essentiel_style
			FROM w_dbsetup 
			WHERE 
				oid <> 0 AND
				code NOT IN (SELECT code FROM activite.t_rapports);
				
					
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_rapports_rubriques" label="Rubriques des rapports" >
		<selectCmd><![CDATA[ 
			
			SELECT
				t_rapports.code AS rapport_code, 
				t_rapports_rubriques.numero_rubrique,
				t_indicateurs.code AS indicateur_code,
				COALESCE(t_indicateurs_associes.code, '') as indicateur_associe_code,
				t_rapports_rubriques.entete,
				t_rapports_rubriques.entete_etendue,
				t_rapports_rubriques.output_format,
				t_rapports_rubriques.width,
				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.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,
				t_rapports_rubriques.is_hide,
				t_rapports_rubriques.specific_viewlink,
				t_rapports_rubriques.specific_viewlink_label,
				t_rapports_rubriques.specific_viewlink_param,
				array_to_string(
					ARRAY[
						COALESCE(t_indicateur_condition_1.external_code::text,''),
						COALESCE(t_indicateur_condition_2.external_code::text,''),
						COALESCE(t_indicateur_condition_3.external_code::text,''),
						COALESCE(t_indicateur_condition_4.external_code::text,'')
					],'||')::text AS indicateur_condition_external_code,
				t_rapports_rubriques.erase_rapport_indicateur_condition,
				t_rapports_rubriques.calc_formula,
				t_rapports_rubriques.hide_on_zero_field,
				t_rapports_rubriques.field_tag,
				array_to_string(
					ARRAY[
						COALESCE(t_indicateurs_serie_1.code,''),
						COALESCE(t_indicateurs_serie_2.code,''),
						COALESCE(t_indicateurs_serie_3.code,''),
						COALESCE(t_indicateurs_serie_4.code,''),
						COALESCE(t_indicateurs_serie_5.code,''),
						COALESCE(t_indicateurs_serie_6.code,''),
						COALESCE(t_indicateurs_serie_7.code,''),
						COALESCE(t_indicateurs_serie_8.code,''),
						COALESCE(t_indicateurs_serie_9.code,'')
					],'||')::text AS serie_indicateur_code,
				array_to_string(
					ARRAY[
						COALESCE(t_indicateurs_serie_1.table_name,''),
						COALESCE(t_indicateurs_serie_2.table_name,''),
						COALESCE(t_indicateurs_serie_3.table_name,''),
						COALESCE(t_indicateurs_serie_4.table_name,''),
						COALESCE(t_indicateurs_serie_5.table_name,''),
						COALESCE(t_indicateurs_serie_6.table_name,''),
						COALESCE(t_indicateurs_serie_7.table_name,''),
						COALESCE(t_indicateurs_serie_8.table_name,''),
						COALESCE(t_indicateurs_serie_9.table_name,'')
					],'||')::text  AS serie_indicateur_table_name,
				array_to_string(t_rapports_rubriques.serie_entete_etendue,'||')::text AS serie_entete_etendue,
				array_to_string(t_rapports_rubriques.serie_description,'||')::text AS serie_description,
				array_to_string(t_rapports_rubriques.serie_output_format,'||')::text AS serie_output_format,
				array_to_string(t_rapports_rubriques.serie_picto_enabled,'||')::text AS serie_picto_enabled,
				array_to_string(t_rapports_rubriques.serie_positif_is_good,'||')::text AS serie_positif_is_good
			FROM 
				activite.t_rapports_rubriques
				JOIN activite.t_rapports ON t_rapports_rubriques.rapport_id = t_rapports.oid
				JOIN activite.t_indicateurs t_indicateurs ON t_rapports_rubriques.indicateur_id = t_indicateurs.oid
				LEFT JOIN activite.t_indicateurs t_indicateurs_associes ON t_rapports_rubriques.indicateur_associe_id = t_indicateurs_associes.oid AND
					t_indicateurs_associes.is_cti IS DISTINCT FROM '0'
				LEFT JOIN activite.t_indicateur_condition t_indicateur_condition_1 ON t_rapports_rubriques.indicateur_condition_id[1] = t_indicateur_condition_1.oid
				LEFT JOIN activite.t_indicateur_condition t_indicateur_condition_2 ON t_rapports_rubriques.indicateur_condition_id[2] = t_indicateur_condition_2.oid
				LEFT JOIN activite.t_indicateur_condition t_indicateur_condition_3 ON t_rapports_rubriques.indicateur_condition_id[3] = t_indicateur_condition_3.oid
				LEFT JOIN activite.t_indicateur_condition t_indicateur_condition_4 ON t_rapports_rubriques.indicateur_condition_id[4] = t_indicateur_condition_4.oid
				LEFT JOIN activite.t_indicateurs t_indicateurs_serie_1 ON t_rapports_rubriques.serie_indicateur_id[1] = t_indicateurs_serie_1.oid
				LEFT JOIN activite.t_indicateurs t_indicateurs_serie_2 ON t_rapports_rubriques.serie_indicateur_id[2] = t_indicateurs_serie_2.oid
				LEFT JOIN activite.t_indicateurs t_indicateurs_serie_3 ON t_rapports_rubriques.serie_indicateur_id[3] = t_indicateurs_serie_3.oid
				LEFT JOIN activite.t_indicateurs t_indicateurs_serie_4 ON t_rapports_rubriques.serie_indicateur_id[4] = t_indicateurs_serie_4.oid
				LEFT JOIN activite.t_indicateurs t_indicateurs_serie_5 ON t_rapports_rubriques.serie_indicateur_id[5] = t_indicateurs_serie_5.oid
				LEFT JOIN activite.t_indicateurs t_indicateurs_serie_6 ON t_rapports_rubriques.serie_indicateur_id[6] = t_indicateurs_serie_6.oid
				LEFT JOIN activite.t_indicateurs t_indicateurs_serie_7 ON t_rapports_rubriques.serie_indicateur_id[7] = t_indicateurs_serie_7.oid
				LEFT JOIN activite.t_indicateurs t_indicateurs_serie_8 ON t_rapports_rubriques.serie_indicateur_id[8] = t_indicateurs_serie_8.oid
				LEFT JOIN activite.t_indicateurs t_indicateurs_serie_9 ON t_rapports_rubriques.serie_indicateur_id[9] = t_indicateurs_serie_9.oid
			WHERE 1=1
				AND t_rapports.is_cti = '1'
				AND t_rapports.code <> 'CTI_PORTFO'
				AND t_indicateurs.is_cti = '1' 
				AND t_indicateurs.code <> ''
			ORDER BY 
				t_rapports.code, 
				t_indicateurs.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('activite','t_rapports_rubriques')
            ;
            DROP TABLE IF EXISTS w_specific_rapports_rubriques ;
            CREATE TEMP TABLE w_specific_rapports_rubriques AS
            SELECT t_rapports_rubriques.* 
            FROM activite.t_rapports_rubriques
            JOIN activite.t_rapports ON t_rapports_rubriques.rapport_id = t_rapports.oid
            WHERE t_rapports.is_essentiel = '1'
            ;
			
			ALTER TABLE w_dbsetup ADD COLUMN to_rapport_id bigint DEFAULT 0;
			UPDATE w_dbsetup SET to_rapport_id = t_rapports.oid FROM activite.t_rapports WHERE rapport_code = t_rapports.code;
			
			ALTER TABLE w_dbsetup ADD COLUMN to_indicateur_id bigint DEFAULT 0;
			UPDATE w_dbsetup SET to_indicateur_id = t_indicateurs.oid FROM activite.t_indicateurs WHERE indicateur_code = t_indicateurs.code AND t_indicateurs.code <> '';

			ALTER TABLE w_dbsetup ADD COLUMN to_indicateur_associe_id bigint DEFAULT 0;
			UPDATE w_dbsetup SET to_indicateur_associe_id = t_indicateurs.oid FROM activite.t_indicateurs WHERE indicateur_associe_code = t_indicateurs.code AND t_indicateurs.code <> '';

			ALTER TABLE w_dbsetup ADD COLUMN to_serie_indicateur_code text[];
			UPDATE w_dbsetup SET to_serie_indicateur_code = string_to_array(serie_indicateur_code,'||');

			ALTER TABLE w_dbsetup ADD COLUMN to_serie_indicateur_id bigint[];
			UPDATE w_dbsetup SET to_serie_indicateur_id[1] = t_indicateurs.oid FROM activite.t_indicateurs WHERE to_serie_indicateur_code[1] = t_indicateurs.code AND t_indicateurs.code <> '';
			UPDATE w_dbsetup SET to_serie_indicateur_id[2] = t_indicateurs.oid FROM activite.t_indicateurs WHERE to_serie_indicateur_code[2] = t_indicateurs.code AND t_indicateurs.code <> '';
			UPDATE w_dbsetup SET to_serie_indicateur_id[3] = t_indicateurs.oid FROM activite.t_indicateurs WHERE to_serie_indicateur_code[3] = t_indicateurs.code AND t_indicateurs.code <> '';
			UPDATE w_dbsetup SET to_serie_indicateur_id[4] = t_indicateurs.oid FROM activite.t_indicateurs WHERE to_serie_indicateur_code[4] = t_indicateurs.code AND t_indicateurs.code <> '';
			UPDATE w_dbsetup SET to_serie_indicateur_id[5] = t_indicateurs.oid FROM activite.t_indicateurs WHERE to_serie_indicateur_code[5] = t_indicateurs.code AND t_indicateurs.code <> '';
			UPDATE w_dbsetup SET to_serie_indicateur_id[6] = t_indicateurs.oid FROM activite.t_indicateurs WHERE to_serie_indicateur_code[6] = t_indicateurs.code AND t_indicateurs.code <> '';
			UPDATE w_dbsetup SET to_serie_indicateur_id[7] = t_indicateurs.oid FROM activite.t_indicateurs WHERE to_serie_indicateur_code[7] = t_indicateurs.code AND t_indicateurs.code <> '';
			UPDATE w_dbsetup SET to_serie_indicateur_id[8] = t_indicateurs.oid FROM activite.t_indicateurs WHERE to_serie_indicateur_code[8] = t_indicateurs.code AND t_indicateurs.code <> '';
			UPDATE w_dbsetup SET to_serie_indicateur_id[9] = t_indicateurs.oid FROM activite.t_indicateurs WHERE to_serie_indicateur_code[9] = t_indicateurs.code AND t_indicateurs.code <> '';
			
			
			ALTER TABLE w_dbsetup ADD COLUMN to_indicateur_condition_external_code text[];
			UPDATE w_dbsetup SET to_indicateur_condition_external_code = string_to_array(indicateur_condition_external_code,'||');


			ALTER TABLE w_dbsetup ADD COLUMN to_indicateur_condition_id bigint[];
			UPDATE w_dbsetup SET to_indicateur_condition_id[1] = t_indicateur_condition.oid FROM activite.t_indicateur_condition WHERE to_indicateur_condition_external_code[1] = t_indicateur_condition.external_code AND t_indicateur_condition.external_code <> '';
			UPDATE w_dbsetup SET to_indicateur_condition_id[2] = t_indicateur_condition.oid FROM activite.t_indicateur_condition WHERE to_indicateur_condition_external_code[2] = t_indicateur_condition.external_code AND t_indicateur_condition.external_code <> '';
			UPDATE w_dbsetup SET to_indicateur_condition_id[3] = t_indicateur_condition.oid FROM activite.t_indicateur_condition WHERE to_indicateur_condition_external_code[3] = t_indicateur_condition.external_code AND t_indicateur_condition.external_code <> '';
			UPDATE w_dbsetup SET to_indicateur_condition_id[4] = t_indicateur_condition.oid FROM activite.t_indicateur_condition WHERE to_indicateur_condition_external_code[4] = t_indicateur_condition.external_code AND t_indicateur_condition.external_code <> '';
			UPDATE w_dbsetup SET to_indicateur_condition_id[5] = t_indicateur_condition.oid FROM activite.t_indicateur_condition WHERE to_indicateur_condition_external_code[5] = t_indicateur_condition.external_code AND t_indicateur_condition.external_code <> '';
			UPDATE w_dbsetup SET to_indicateur_condition_id[6] = t_indicateur_condition.oid FROM activite.t_indicateur_condition WHERE to_indicateur_condition_external_code[6] = t_indicateur_condition.external_code AND t_indicateur_condition.external_code <> '';
			UPDATE w_dbsetup SET to_indicateur_condition_id[7] = t_indicateur_condition.oid FROM activite.t_indicateur_condition WHERE to_indicateur_condition_external_code[7] = t_indicateur_condition.external_code AND t_indicateur_condition.external_code <> '';
			UPDATE w_dbsetup SET to_indicateur_condition_id[8] = t_indicateur_condition.oid FROM activite.t_indicateur_condition WHERE to_indicateur_condition_external_code[8] = t_indicateur_condition.external_code AND t_indicateur_condition.external_code <> '';
			UPDATE w_dbsetup SET to_indicateur_condition_id[9] = t_indicateur_condition.oid FROM activite.t_indicateur_condition WHERE to_indicateur_condition_external_code[9] = t_indicateur_condition.external_code AND t_indicateur_condition.external_code <> '';

            
            -- Installer les données CTI
			DELETE FROM activite.t_rapports_rubriques 
			WHERE 1=1
				AND rapport_id IN (SELECT to_rapport_id FROM w_dbsetup)
			;

			INSERT INTO activite.t_rapports_rubriques(
				rapport_id, 
				numero_rubrique,
				indicateur_id,
				indicateur_associe_id,
				entete,
				entete_etendue,
				output_format,
				width,
				row_height,
				padding,
				font_size,
				font_weight,
				border_thickness,
				color,
				background_color,
				base_nationale,
				base_nationale_qualifiee,
				viewlink,
				viewlink_label,
				viewlink_param,
				picto_enabled,
				positif_is_good,
				description,
				description_2,
				entete_etendue_2,
				output_format_2,
				picto_enabled_2,
				positif_is_good_2,
				is_hide,
				specific_viewlink,
				specific_viewlink_label,
				specific_viewlink_param,
				indicateur_condition_id,
				erase_rapport_indicateur_condition,
				calc_formula,
				hide_on_zero_field,
				field_tag,
				serie_indicateur_id,
				serie_entete_etendue,
				serie_description,
				serie_output_format,
				serie_picto_enabled,
				serie_positif_is_good
				)
			SELECT 
				to_rapport_id, 
				numero_rubrique,
				to_indicateur_id,
				to_indicateur_associe_id,
				entete,
				entete_etendue,
				output_format,
				width,
				row_height,
				padding,
				font_size,
				font_weight,
				border_thickness,
				color,
				background_color,
				base_nationale,
				base_nationale_qualifiee,
				viewlink,
				viewlink_label,
				viewlink_param,
				picto_enabled,
				positif_is_good,
				description,
				description_2,
				entete_etendue_2,
				output_format_2,
				picto_enabled_2,
				positif_is_good_2,
				is_hide,
				specific_viewlink,
				specific_viewlink_label,
				specific_viewlink_param,
				to_indicateur_condition_id,
				erase_rapport_indicateur_condition,
				calc_formula,
				hide_on_zero_field,
				field_tag,
				to_serie_indicateur_id,
				string_to_array(serie_entete_etendue,'||'),
				string_to_array(serie_description,'||'),
				string_to_array(serie_output_format,'||'),
				string_to_array(serie_picto_enabled,'||'),
				string_to_array(serie_positif_is_good,'||')
			FROM w_dbsetup
			WHERE to_rapport_id <> 0 AND to_indicateur_id <> 0
			;
            
            -- Rejouer le spécifique
            UPDATE activite.t_rapports_rubriques SET
                is_hide = w_specific_rapports_rubriques.is_hide,
                specific_viewlink = CASE WHEN w_specific_rapports_rubriques.specific_viewlink IN ('1', '2') THEN '' ELSE w_specific_rapports_rubriques.specific_viewlink END,
                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_expert_controle" label="t_expert_controle">
		<selectCmd><![CDATA[
			
			SELECT 
				CASE WHEN t_expert_controle.oid = 0 THEN t_expert_controle.oid ELSE 1 END AS controle_id,
				t_expert_controle.code as controle_code,
				t_expert_controle.texte as controle_texte,
				t_expert_controle.texte_court as controle_texte_court,
				t_expert_controle.texte_abrege as controle_texte_abrege,
				t_expert_controle.description as controle_description,
				t_expert_controle.is_cti as controle_is_cti,
				t_expert_controle.etat_cible as controle_etat_cible,
				t_prestataires.code as prestataire_code
			FROM activite.t_expert_controle
				LEFT JOIN base.t_prestataires ON t_prestataires.oid = t_expert_controle.prestataire_id
			WHERE t_expert_controle.is_cti = '1' AND t_expert_controle.code <> ''
			ORDER BY t_expert_controle.code
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[
		
			UPDATE activite.t_expert_controle SET 
				texte = w_dbsetup.controle_texte, 
				texte_court = w_dbsetup.controle_texte_court, 
				texte_abrege = w_dbsetup.controle_texte_abrege, 
				description = w_dbsetup.controle_description, 
				is_cti = w_dbsetup.controle_is_cti, 
				etat_cible = w_dbsetup.controle_etat_cible,
				prestataire_id = COALESCE(t_prestataires.oid, -1)
			FROM w_dbsetup
				LEFT JOIN base.t_prestataires ON t_prestataires.code = w_dbsetup.prestataire_code
			WHERE t_expert_controle.code = w_dbsetup.controle_code
			AND (
				t_expert_controle.texte IS DISTINCT FROM w_dbsetup.controle_texte OR
				t_expert_controle.texte_court IS DISTINCT FROM w_dbsetup.controle_texte_court OR
				t_expert_controle.texte_abrege IS DISTINCT FROM w_dbsetup.controle_texte_abrege OR
				t_expert_controle.description IS DISTINCT FROM w_dbsetup.controle_description OR
				t_expert_controle.is_cti IS DISTINCT FROM w_dbsetup.controle_is_cti OR
				t_expert_controle.etat_cible IS DISTINCT FROM w_dbsetup.controle_etat_cible OR 
				t_prestataires.code != w_dbsetup.prestataire_code 
			);
			
			UPDATE activite.t_expert_controle
			SET gravite_id = 5 
			WHERE gravite_id IS NULL OR gravite_id NOT IN (0,5,9);
			
			UPDATE activite.t_expert_controle
			SET is_hide = '0' 
			WHERE is_hide IS NULL OR is_hide NOT IN ('0','1');
			
			UPDATE activite.t_expert_controle
			SET etat_cible = 0 
			WHERE etat_cible IS NULL;
			
			UPDATE activite.t_expert_controle
			SET prestataire_id = -1 
			WHERE prestataire_id IS NULL;
			
			UPDATE activite.t_expert_controle
			SET is_active = '1' 
			WHERE is_active IS NULL;
			
			
			INSERT INTO activite.t_expert_controle(
				code,
				texte,
				texte_court,
				texte_abrege,
				description,
				is_cti,
				gravite_id,
				is_hide,
				etat_cible,
				prestataire_id,
				is_active
				)
			SELECT 
				w_dbsetup.controle_code,
				w_dbsetup.controle_texte,
				w_dbsetup.controle_texte_court,
				w_dbsetup.controle_texte_abrege,
				w_dbsetup.controle_description,
				w_dbsetup.controle_is_cti,
				5,
				'0',
				w_dbsetup.controle_etat_cible,
				COALESCE(t_prestataires.oid, -1),
				'1'
			FROM w_dbsetup 
				LEFT JOIN base.t_prestataires ON t_prestataires.code = w_dbsetup.prestataire_code
			WHERE 
				w_dbsetup.controle_id <> 0 
				AND w_dbsetup.controle_code NOT IN (SELECT code FROM activite.t_expert_controle WHERE code IS NOT NULL);

			
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_expert_controle_rule" label="t_expert_controle_rule">
		<selectCmd><![CDATA[
			
			SELECT 
				CASE WHEN t_expert_controle_rule.oid = 0 THEN t_expert_controle_rule.oid ELSE 1 END AS oid,
				t_expert_controle.code AS controle_code,
				t_expert_controle_rule.numero, 
				t_expert_controle_rule.sqlcmd_where, 
				t_expert_controle_rule.sqlcmd_justificatif,
				t_expert_controle_rule.table_id as tab
			FROM activite.t_expert_controle_rule
			JOIN activite.t_expert_controle ON controle_id = t_expert_controle.oid
			WHERE t_expert_controle.is_cti = '1' AND
				t_expert_controle.code <> ''
			ORDER BY t_expert_controle.code, t_expert_controle_rule.numero
			;
			
		]]></selectCmd>
		<updateCmd><![CDATA[
		
			UPDATE activite.t_expert_controle_rule
			SET numero = 1
			WHERE numero IS NULL or NUMERO NOT IN (1,2,3);
			
			UPDATE activite.t_expert_controle_rule
			SET 
				sqlcmd_where = w_dbsetup.sqlcmd_where,
				sqlcmd_justificatif = w_dbsetup.sqlcmd_justificatif,
				table_id = w_dbsetup.tab
			FROM w_dbsetup
				JOIN activite.t_expert_controle ON t_expert_controle.code = w_dbsetup.controle_code
			WHERE t_expert_controle_rule.controle_id = t_expert_controle.oid AND
				t_expert_controle_rule.numero = w_dbsetup.numero AND
				(
				t_expert_controle_rule.sqlcmd_where IS DISTINCT FROM w_dbsetup.sqlcmd_where OR
				t_expert_controle_rule.table_id IS DISTINCT FROM w_dbsetup.tab OR
				t_expert_controle_rule.sqlcmd_justificatif IS DISTINCT FROM w_dbsetup.sqlcmd_justificatif
				);
				
			INSERT INTO activite.t_expert_controle_rule (
				controle_id,
				numero, 
				sqlcmd_where, 
				sqlcmd_justificatif,
				table_id
			)
			SELECT
				t_expert_controle.oid,
				w_dbsetup.numero, 
				w_dbsetup.sqlcmd_where, 
				w_dbsetup.sqlcmd_justificatif,
				w_dbsetup.tab
			FROM w_dbsetup
				JOIN activite.t_expert_controle ON t_expert_controle.code = w_dbsetup.controle_code
				LEFT JOIN activite.t_expert_controle_rule ON
					t_expert_controle_rule.controle_id = t_expert_controle.oid AND
					t_expert_controle_rule.numero = w_dbsetup.numero
			WHERE t_expert_controle_rule.oid IS NULL;	
			

			
		]]></updateCmd>
	</TABLE>
	
	<TABLE name="t_vue_parametre" label="Listes des données prédéfinis">
		<selectCmd><![CDATA[ 
			
			SELECT
				CASE WHEN oid = 0 THEN oid ELSE 1 END AS 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 activite.t_vue_parametre 
			ORDER BY 1;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			INSERT INTO activite.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 activite.t_vue_parametre)
			;
		]]></updateCmd>
	</TABLE>	
    <TABLE name="t_severites_cti" label="Sévérité CTI">
		<selectCmd><![CDATA[ 
			
			SELECT 
				severite_id as keepoid,
				severite_code,
				severite_texte
			FROM activite.t_severites_cti
			ORDER BY severite_code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE activite.t_severites_cti SET 
				severite_code = w_dbsetup.severite_code,
				severite_texte = w_dbsetup.severite_code
			FROM w_dbsetup
			WHERE w_dbsetup.keepoid = t_severites_cti.severite_id AND w_dbsetup.keepoid = 0;
            
            UPDATE activite.t_severites_cti SET
				severite_texte = w_dbsetup.severite_texte
			FROM w_dbsetup
			WHERE 1=1
				AND activite.t_severites_cti.severite_code = w_dbsetup.severite_code
				AND activite.t_severites_cti.severite_texte IS DISTINCT FROM w_dbsetup.severite_texte
			;

			INSERT INTO activite.t_severites_cti(severite_id, severite_code, severite_texte)
				SELECT keepoid, severite_code, severite_texte
				FROM w_dbsetup 
				WHERE 1=1
					AND keepoid NOT IN (SELECT severite_id FROM activite.t_severites_cti)
			;
			
			
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_traitement_epmsi" label="Traitement e-PMSI">
		<selectCmd><![CDATA[ 
			
			SELECT 
				oid as keepoid,
				code,
				texte
			FROM activite.t_traitement_epmsi
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
			UPDATE activite.t_traitement_epmsi SET 
				code = w_dbsetup.code,
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE w_dbsetup.keepoid = t_traitement_epmsi.oid AND w_dbsetup.keepoid = 0;
			
			UPDATE activite.t_traitement_epmsi SET
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE 1=1
				AND activite.t_traitement_epmsi.code = w_dbsetup.code
				AND activite.t_traitement_epmsi.texte IS DISTINCT FROM w_dbsetup.texte
			;

			INSERT INTO activite.t_traitement_epmsi(oid, code, texte)
				SELECT keepoid, code, texte
				FROM w_dbsetup 
				WHERE 1=1
					AND keepoid = 0 
					AND keepoid NOT IN (SELECT oid FROM activite.t_traitement_epmsi)
			;

			INSERT INTO activite.t_traitement_epmsi(oid,code,texte)
				SELECT keepoid, code, texte
				FROM w_dbsetup 
				WHERE 1=1
					AND keepoid <> 0 
					AND code NOT IN (SELECT code FROM activite.t_traitement_epmsi)
			;
			
			
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_type_sejour" label="Types séjours">
		<selectCmd><![CDATA[ 
			
			SELECT
                CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				code,
				texte
			FROM activite.t_type_sejour
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
            UPDATE w_dbsetup SET 
				oid =  w_dbsetup.code::bigint;

			UPDATE activite.t_type_sejour SET 
				oid =  w_dbsetup.oid,
				code = w_dbsetup.code,
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE w_dbsetup.oid = t_type_sejour.oid AND w_dbsetup.oid = 0;
			
			UPDATE activite.t_type_sejour SET
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE 1=1
				AND activite.t_type_sejour.code = w_dbsetup.code
				AND activite.t_type_sejour.texte IS DISTINCT FROM w_dbsetup.texte
			;

			INSERT INTO activite.t_type_sejour(oid,code,texte)
				SELECT oid, code, texte
				FROM w_dbsetup 
				WHERE 1=1 
					AND oid NOT IN (SELECT oid FROM activite.t_type_sejour)
			;
			
			
		]]></updateCmd>
	</TABLE>
	<TABLE name="t_type_hospi_pmsi" label="Type d'hospitalisation pmsi (MCO-SSR-PSY-HAD)">
		<selectCmd><![CDATA[ 
			
			SELECT
                CASE WHEN oid = 0 THEN oid ELSE 1 END AS oid,
				code,
				texte
			FROM activite.t_type_hospi_pmsi
			ORDER BY code;
			
		]]></selectCmd>
		<updateCmd><![CDATA[ 
			
            UPDATE w_dbsetup SET 
				oid =  w_dbsetup.code::bigint;

			UPDATE activite.t_type_hospi_pmsi SET 
				oid =  w_dbsetup.oid,
				code = w_dbsetup.code,
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE w_dbsetup.oid = t_type_hospi_pmsi.oid AND w_dbsetup.oid = 0;
			
			UPDATE activite.t_type_hospi_pmsi SET
				texte = w_dbsetup.texte
			FROM w_dbsetup
			WHERE 1=1
				AND activite.t_type_hospi_pmsi.code = w_dbsetup.code
				AND activite.t_type_hospi_pmsi.texte IS DISTINCT FROM w_dbsetup.texte
			;

			INSERT INTO activite.t_type_hospi_pmsi(oid,code,texte)
				SELECT oid, code, texte
				FROM w_dbsetup 
				WHERE 1=1 
					AND oid NOT IN (SELECT oid FROM activite.t_type_hospi_pmsi)
			;
		]]></updateCmd>
	</TABLE>

	<TABLE name="t_divers" label="Divers">
		<selectCmd><![CDATA[ 
            SELECT 
                t_divers.code, 
                t_divers.description,
                t_divers.show_info_module,
                t_divers.texte,
                t_divers.valeur,
                t_divers.valeur_date::text
            FROM 
                activite.t_divers
            ORDER BY code; 
        ]]>		</selectCmd>
		<updateCmd><![CDATA[ 
            
            INSERT INTO activite.t_divers(code, description, show_info_module, texte, valeur, valeur_date)
                SELECT 
                    w_dbsetup.code,
                    w_dbsetup.description,
                    w_dbsetup.show_info_module,
                    w_dbsetup.texte,
                    w_dbsetup.valeur,
                    CASE WHEN w_dbsetup.valeur_date = '' THEN '19700101'::date ELSE w_dbsetup.valeur_date::date END AS valeur_date
                FROM
                    w_dbsetup
                WHERE w_dbsetup.code NOT IN (SELECT code from activite.t_divers)
            ;
            
        ]]>		</updateCmd>
	</TABLE>
	<TABLE name="t_kiwee_params" label="Kiwee">
		<selectCmd><![CDATA[ 
            SELECT 
                t_kiwee_params.code, 
                t_kiwee_params.texte,
                t_kiwee_params.valeur,
                t_kiwee_params.description
            FROM 
                activite.t_kiwee_params
            ORDER BY code; 
        ]]>		</selectCmd>
		<updateCmd><![CDATA[ 
            
            INSERT INTO activite.t_kiwee_params(code, texte, valeur, description)
                SELECT 
                    w_dbsetup.code,
                    w_dbsetup.texte,
                    w_dbsetup.valeur,
                    w_dbsetup.description
                FROM
                    w_dbsetup
                WHERE w_dbsetup.code NOT IN (SELECT code from activite.t_kiwee_params)
            ;
            
        ]]>		</updateCmd>
	</TABLE>
	<TABLE name="t_types_gmt" label="Types GMT">
		<selectCmd><![CDATA[ 
            SELECT 
                t_types_gmt.code, 
                t_types_gmt.label,
                t_types_gmt.description
            FROM 
                activite.t_types_gmt
            ORDER BY code; 
        ]]>		</selectCmd>
		<updateCmd><![CDATA[ 
            
            INSERT INTO activite.t_types_gmt(code, label, description)
                SELECT 
                    w_dbsetup.code,
                    w_dbsetup.label,
                    w_dbsetup.description
                FROM
                    w_dbsetup
                WHERE w_dbsetup.code NOT IN (SELECT code from activite.t_types_gmt)
            ;
            
        ]]>		</updateCmd>
	</TABLE>

</DBSETUPDATA>
