return: text
lang: plpgsql
src: |
  DECLARE	
  	
  	sqlcmd text;
  	def_cursor refcursor;
  	_schemaname text;
  	result TEXT;
  BEGIN
  
  	-- Formes d'activité
  	DROP TABLE IF EXISTS w_indicateur_condition;
  	CREATE TEMP TABLE w_indicateur_condition AS
  	SELECT 
  		'CTI_COMPTE#' || v_source.oid::text AS code,
  		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Compte. ' || v_source.texte::text) AS texte,
  		v_source.texte AS texte_court,
  		'compte_id'::text AS column_name,
  		''::text AS rule,
  		v_source.oid::text AS value,
  		'COMPTE_C='||v_source.oid AS view_select_rule,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
  				ELSE '9' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
  				WHEN v_source.oid LIKE 'LI%' THEN COALESCE('LI#'||t_listes.code,v_source.oid)
  				WHEN v_source.oid LIKE 'SL%' THEN COALESCE('SL#'||t_listes.code,v_source.oid)
  				ELSE v_source.oid END
  				
  		] AS arbre_code,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
  				ELSE 'Valeurs' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes_sections.texte
  				ELSE v_source.texte END
  				
  		] AS arbre_texte
  	FROM compta.v_comptes_c_1 v_source
  		LEFT JOIN compta.t_classes_sections ON v_source.oid LIKE 'LS%' AND
  			t_classes_sections.oid::text = substr(v_source.oid,5)
  		LEFT JOIN compta.t_classes ON t_classes.oid = t_classes_sections.classe_id
  		LEFT JOIN compta.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
  		LEFT JOIN compta.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
  			t_listes.oid::text = substr(v_source.oid,3) AND
  			t_listes.code <> ''
  	WHERE v_source.oid NOT LIKE 'LC%' AND
  		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
  		v_source.oid NOT LIKE 'CP%' AND
  		v_source.oid NOT LIKE 'CL%' AND
  		v_source.oid NOT LIKE 'LS09%' AND
  		v_source.oid <> '-1'
  	;
  
  	-- Journaux
  	INSERT INTO w_indicateur_condition
  	SELECT 
  		'CTI_JOURNAL#' || v_source.oid::text AS code,
  		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Journal. ' || v_source.texte::text) AS texte,
  		v_source.texte AS texte_court,
  		'journal_id'::text AS column_name,
  		''::text AS rule,
  		v_source.oid::text AS value,
  		'JOURNAL_C='||v_source.oid AS view_select_rule,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
  				ELSE '9' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
  				WHEN v_source.oid LIKE 'LI%' THEN COALESCE('LI#'||t_listes.code,v_source.oid)
  				WHEN v_source.oid LIKE 'SL%' THEN COALESCE('SL#'||t_listes.code,v_source.oid)
  				WHEN v_source.oid LIKE 'JR%' THEN COALESCE('JR#'||t_source.code,v_source.oid)
  				ELSE v_source.oid END
  				
  		] AS arbre_code,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
  				ELSE 'Valeurs' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes_sections.texte
  				ELSE v_source.texte END
  		] AS arbre_texte
  	FROM compta.v_journaux_c_1 v_source
  		LEFT JOIN compta.t_classes_sections ON v_source.oid LIKE 'LS%' AND
  		t_classes_sections.oid::text = substr(v_source.oid,5)
  		LEFT JOIN compta.t_classes ON t_classes.oid = t_classes_sections.classe_id
  		LEFT JOIN compta.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
  		LEFT JOIN compta.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
  			t_listes.oid::text = substr(v_source.oid,3) AND
  			t_listes.code <> ''
  		LEFT JOIN compta.t_journaux t_source ON v_source.oid LIKE 'JR%' AND t_source.oid = substr(v_source.oid,3)
  	WHERE v_source.oid NOT LIKE 'LC%' AND
  		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
  		v_source.oid <> '-1'
  	;
  
  	-- POLES
  	INSERT INTO w_indicateur_condition
  	SELECT 
  		'CTI_POLE#' || v_source.oid::text AS code,
  		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Pole. ' || v_source.texte::text) AS texte,
  		v_source.texte AS texte_court,
  		'pole_id'::text AS column_name,
  		''::text AS rule,
  		v_source.oid::text AS value,
  		'POLE='||v_source.oid AS view_select_rule,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
  				ELSE '9' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
  				WHEN v_source.oid LIKE 'LI%' THEN COALESCE('LI#'||t_listes.code,v_source.oid)
  				WHEN v_source.oid LIKE 'SL%' THEN COALESCE('SL#'||t_listes.code,v_source.oid)
  				WHEN v_source.oid LIKE 'JR%' THEN COALESCE('JR#'||t_source.code,v_source.oid)
  				ELSE v_source.oid END
  				
  		] AS arbre_code,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
  				ELSE 'Valeurs' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes_sections.texte
  				ELSE v_source.texte END
  		] AS arbre_texte
  	FROM compta.v_pole_1 v_source
  		LEFT JOIN compta.t_classes_sections ON v_source.oid LIKE 'LS%' AND
  		t_classes_sections.oid::text = substr(v_source.oid,5)
  		LEFT JOIN compta.t_classes ON t_classes.oid = t_classes_sections.classe_id
  		LEFT JOIN compta.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
  		LEFT JOIN compta.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
  			t_listes.oid::text = substr(v_source.oid,3) AND
  			t_listes.code <> ''
  		LEFT JOIN compta.t_journaux t_source ON v_source.oid LIKE 'JR%' AND t_source.oid = substr(v_source.oid,3)
  	WHERE v_source.oid NOT LIKE 'LC%' AND
  		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
  		v_source.oid <> '-1'
  	;
  
  	-- Sites
  	INSERT INTO w_indicateur_condition
  	SELECT 
  		'CTI_SITE#' || v_source.oid::text AS code,
  		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Site. ' || v_source.texte::text) AS texte,
  		v_source.texte AS texte_court,
  		'site_id'::text AS column_name,
  		''::text AS rule,
  		v_source.oid::text AS value,
  		'SITE_C='||v_source.oid AS view_select_rule,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
  				ELSE '9' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
  				WHEN v_source.oid LIKE 'LI%' THEN COALESCE('LI#'||t_listes.code,v_source.oid)
  				WHEN v_source.oid LIKE 'SL%' THEN COALESCE('SL#'||t_listes.code,v_source.oid)
  				WHEN v_source.oid LIKE 'SI%' THEN COALESCE('SI#'||t_source.code,v_source.oid)
  				ELSE v_source.oid END
  				
  		] AS arbre_code,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
  				ELSE 'Valeurs' END,
  			v_source.texte
  		] AS arbre_texte
  	FROM compta.v_sites_c_1 v_source
  		LEFT JOIN compta.t_classes_sections ON v_source.oid LIKE 'LS%' AND
  		t_classes_sections.oid::text = substr(v_source.oid,5)
  		LEFT JOIN compta.t_classes ON t_classes.oid = t_classes_sections.classe_id
  		LEFT JOIN compta.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
  		LEFT JOIN compta.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
  			t_listes.oid::text = substr(v_source.oid,3) AND
  			t_listes.code <> ''
  		LEFT JOIN compta.t_sites t_source ON v_source.oid LIKE 'SI%' AND t_source.oid = substr(v_source.oid,3)
  	WHERE v_source.oid NOT LIKE 'LC%' AND
  		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
  		v_source.oid <> '-1' AND
  		v_source.oid <> 'SI0'
  	;
  
  
  	-- Sections analytiques
  	INSERT INTO w_indicateur_condition
  	SELECT 
  		'CTI_SECANA#' || v_source.oid::text AS code,
  		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Section. ' || v_source.texte::text) AS texte,
  		v_source.texte AS texte_court,
  		'section_analytique_id'::text AS column_name,
  		''::text AS rule,
  		v_source.oid::text AS value,
  		'SECTION_ANALYTIQUE_C='||v_source.oid AS view_select_rule,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
  				ELSE '9' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
  				WHEN v_source.oid LIKE 'LI%' THEN COALESCE('LI#'||t_listes.code,v_source.oid)
  				WHEN v_source.oid LIKE 'SL%' THEN COALESCE('SL#'||t_listes.code,v_source.oid)
  				WHEN v_source.oid LIKE 'SA%' THEN COALESCE('SA#'||t_source.code,v_source.oid)
  				ELSE v_source.oid END
  				
  		] AS arbre_code,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
  				ELSE 'Valeurs' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes_sections.texte
  				ELSE v_source.texte END
  		] AS arbre_texte
  	FROM compta.v_sections_analytiques_c_1 v_source
  		LEFT JOIN compta.t_classes_sections ON v_source.oid LIKE 'LS%' AND
  		t_classes_sections.oid::text = substr(v_source.oid,5)
  		LEFT JOIN compta.t_classes ON t_classes.oid = t_classes_sections.classe_id
  		LEFT JOIN compta.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
  		LEFT JOIN compta.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
  			t_listes.oid::text = substr(v_source.oid,3) AND
  			t_listes.code <> ''
  		LEFT JOIN compta.t_sections_analytiques t_source ON v_source.oid LIKE 'SA%' AND t_source.oid = substr(v_source.oid,3)
  	WHERE v_source.oid NOT LIKE 'LC%' AND
  		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
  		v_source.oid <> '-1'
  	;
  
  	-- Sections analytiques
  	INSERT INTO w_indicateur_condition
  	SELECT 
  		'CTI_SECANA2#' || v_source.oid::text AS code,
  		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Section (2). ' || v_source.texte::text) AS texte,
  		v_source.texte AS texte_court,
  		'section_analytique_2_id'::text AS column_name,
  		''::text AS rule,
  		v_source.oid::text AS value,
  		'SECTION_ANALYTIQUE_2_C='||v_source.oid AS view_select_rule,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
  				ELSE '9' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
  				WHEN v_source.oid LIKE 'LI%' THEN COALESCE('LI#'||t_listes.code,v_source.oid)
  				WHEN v_source.oid LIKE 'SL%' THEN COALESCE('SL#'||t_listes.code,v_source.oid)
  				WHEN v_source.oid LIKE 'SA%' THEN COALESCE('SA#'||t_source.code,v_source.oid)
  				ELSE v_source.oid END
  				
  		] AS arbre_code,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
  				ELSE 'Valeurs' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes_sections.texte
  				ELSE v_source.texte END
  		] AS arbre_texte
  	FROM compta.v_sections_analytiques_2_c_1 v_source
  		LEFT JOIN compta.t_classes_sections ON v_source.oid LIKE 'LS%' AND
  		t_classes_sections.oid::text = substr(v_source.oid,5)
  		LEFT JOIN compta.t_classes ON t_classes.oid = t_classes_sections.classe_id
  		LEFT JOIN compta.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
  		LEFT JOIN compta.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
  			t_listes.oid::text = substr(v_source.oid,3) AND
  			t_listes.code <> ''
  		LEFT JOIN compta.t_sections_analytiques t_source ON v_source.oid LIKE 'SA%' AND t_source.oid = substr(v_source.oid,3)
  	WHERE v_source.oid NOT LIKE 'LC%' AND
  		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
  		v_source.oid <> '-1'
  	;
  
  	-- Sections analytiques
  	INSERT INTO w_indicateur_condition
  	SELECT 
  		'CTI_SECANA3#' || v_source.oid::text AS code,
  		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Section (3). ' || v_source.texte::text) AS texte,
  		v_source.texte AS texte_court,
  		'section_analytique_3_id'::text AS column_name,
  		''::text AS rule,
  		v_source.oid::text AS value,
  		'SECTION_ANALYTIQUE_3_C='||v_source.oid AS view_select_rule,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
  				ELSE '9' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
  				WHEN v_source.oid LIKE 'LI%' THEN COALESCE('LI#'||t_listes.code,v_source.oid)
  				WHEN v_source.oid LIKE 'SL%' THEN COALESCE('SL#'||t_listes.code,v_source.oid)
  				WHEN v_source.oid LIKE 'SA%' THEN COALESCE('SA#'||t_source.code,v_source.oid)
  				ELSE v_source.oid END
  				
  		] AS arbre_code,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
  				ELSE 'Valeurs' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes_sections.texte
  				ELSE v_source.texte END
  		] AS arbre_texte
  	FROM compta.v_sections_analytiques_3_c_1 v_source
  		LEFT JOIN compta.t_classes_sections ON v_source.oid LIKE 'LS%' AND
  		t_classes_sections.oid::text = substr(v_source.oid,5)
  		LEFT JOIN compta.t_classes ON t_classes.oid = t_classes_sections.classe_id
  		LEFT JOIN compta.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
  		LEFT JOIN compta.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
  			t_listes.oid::text = substr(v_source.oid,3) AND
  			t_listes.code <> ''
  		LEFT JOIN compta.t_sections_analytiques t_source ON v_source.oid LIKE 'SA%' AND t_source.oid = substr(v_source.oid,3)
  	WHERE v_source.oid NOT LIKE 'LC%' AND
  		(v_source.oid NOT LIKE 'SA%' OR (SELECT count(*) FROM compta.v_sections_analytiques_3_c_1) < 1000) AND
  		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
  		v_source.oid <> '-1'
  	;
  
  	-- Sections analytiques
  	INSERT INTO w_indicateur_condition
  	SELECT 
  		'CTI_SECANA4#' || v_source.oid::text AS code,
  		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Section (4). ' || v_source.texte::text) AS texte,
  		v_source.texte AS texte_court,
  		'section_analytique_4_id'::text AS column_name,
  		''::text AS rule,
  		v_source.oid::text AS value,
  		'SECTION_ANALYTIQUE_4_C='||v_source.oid AS view_select_rule,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
  				ELSE '9' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
  				WHEN v_source.oid LIKE 'LI%' THEN COALESCE('LI#'||t_listes.code,v_source.oid)
  				WHEN v_source.oid LIKE 'SL%' THEN COALESCE('SL#'||t_listes.code,v_source.oid)
  				WHEN v_source.oid LIKE 'SA%' THEN COALESCE('SA#'||t_source.code,v_source.oid)
  				ELSE v_source.oid END
  				
  		] AS arbre_code,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
  				ELSE 'Valeurs' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes_sections.texte
  				ELSE v_source.texte END
  		] AS arbre_texte
  	FROM compta.v_sections_analytiques_4_c_1 v_source
  		LEFT JOIN compta.t_classes_sections ON v_source.oid LIKE 'LS%' AND
  		t_classes_sections.oid::text = substr(v_source.oid,5)
  		LEFT JOIN compta.t_classes ON t_classes.oid = t_classes_sections.classe_id
  		LEFT JOIN compta.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
  		LEFT JOIN compta.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
  			t_listes.oid::text = substr(v_source.oid,3) AND
  			t_listes.code <> ''
  		LEFT JOIN compta.t_sections_analytiques t_source ON v_source.oid LIKE 'SA%' AND t_source.oid = substr(v_source.oid,3)
  	WHERE v_source.oid NOT LIKE 'LC%' AND
  		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
  		v_source.oid <> '-1'
  	;
  
  	-- Sections analytiques
  	INSERT INTO w_indicateur_condition
  	SELECT 
  		'CTI_SECANA5#' || v_source.oid::text AS code,
  		COALESCE(t_classes.texte || '. ' || t_classes_sections.texte,'Section (5). ' || v_source.texte::text) AS texte,
  		v_source.texte AS texte_court,
  		'section_analytique_5_id'::text AS column_name,
  		''::text AS rule,
  		v_source.oid::text AS value,
  		'SECTION_ANALYTIQUE_5_C='||v_source.oid AS view_select_rule,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN '1-Classe' || substr(v_source.oid,3,2)
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '2-Liste CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN '3-Liste CTI' 
  				ELSE '9' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN 'LS#'||t_listes_tables_classes.code||':'||CASE WHEN t_classes.code <> '' THEN t_classes.code ELSE '*CLS'||to_char(t_classes.sequence,'FM00') END||':'||t_classes_sections.code
  				WHEN v_source.oid LIKE 'LI%' THEN COALESCE('LI#'||t_listes.code,v_source.oid)
  				WHEN v_source.oid LIKE 'SL%' THEN COALESCE('SL#'||t_listes.code,v_source.oid)
  				WHEN v_source.oid LIKE 'SA%' THEN COALESCE('SA#'||t_source.code,v_source.oid)
  				ELSE v_source.oid END
  				
  		] AS arbre_code,
  		ARRAY[
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes.texte
  				WHEN v_source.is_cti = '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes CTI' 
  				WHEN v_source.is_cti <> '1' AND (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') THEN 'Listes Etablissement' 
  				ELSE 'Valeurs' END,
  			CASE 
  				WHEN v_source.oid LIKE 'LS%' THEN t_classes_sections.texte
  				ELSE v_source.texte END
  		] AS arbre_texte
  	FROM compta.v_sections_analytiques_5_c_1 v_source
  		LEFT JOIN compta.t_classes_sections ON v_source.oid LIKE 'LS%' AND
  		t_classes_sections.oid::text = substr(v_source.oid,5)
  		LEFT JOIN compta.t_classes ON t_classes.oid = t_classes_sections.classe_id
  		LEFT JOIN compta.t_listes_tables t_listes_tables_classes ON t_classes.table_id = t_listes_tables_classes.oid
  		LEFT JOIN compta.t_listes ON (v_source.oid LIKE 'LI%' OR v_source.oid LIKE 'SL%') AND
  			t_listes.oid::text = substr(v_source.oid,3) AND
  			t_listes.code <> ''
  		LEFT JOIN compta.t_sections_analytiques t_source ON v_source.oid LIKE 'SA%' AND t_source.oid = substr(v_source.oid,3)
  	WHERE v_source.oid NOT LIKE 'LC%' AND
  		(v_source.oid NOT LIKE 'LS%' OR substr(v_source.oid,5) <> '0') AND
  		v_source.oid <> '-1'
  	;
  
  
  	-- Ne pas garder les listes vides
  	-- DELETE FROM w_indicateur_condition
  	-- WHERE value LIKE 'LI%' AND
  	-- 	substr(value,3) NOT IN (SELECT liste_id::text FROM compta.t_listes_contenu)
          -- ;                   
  
  	INSERT INTO compta.t_indicateur_condition(code, texte,texte_court,column_name,rule,value,view_select_rule, arbre_code, arbre_texte)
  	SELECT code, texte,texte_court,column_name,rule,value,view_select_rule, arbre_code, arbre_texte
  	FROM w_indicateur_condition
  	WHERE code NOT IN (SELECT code FROM compta.t_indicateur_condition WHERE code IS NOT NULL)
  	;
  
  	UPDATE compta.t_indicateur_condition SET
  		texte = w_indicateur_condition.texte,
  		texte_court = w_indicateur_condition.texte_court,
  		column_name = w_indicateur_condition.column_name,
  		rule = w_indicateur_condition.rule,
  		value = w_indicateur_condition.value,
  		view_select_rule = w_indicateur_condition.view_select_rule,
  		arbre_code = w_indicateur_condition.arbre_code,
  		arbre_texte = w_indicateur_condition.arbre_texte
  	FROM w_indicateur_condition
  	WHERE w_indicateur_condition.code = t_indicateur_condition.code AND
  		(
  		t_indicateur_condition.texte IS DISTINCT FROM w_indicateur_condition.texte OR
  		t_indicateur_condition.texte_court IS DISTINCT FROM w_indicateur_condition.texte_court OR
  		t_indicateur_condition.column_name IS DISTINCT FROM w_indicateur_condition.column_name OR
  		t_indicateur_condition.rule IS DISTINCT FROM w_indicateur_condition.rule OR
  		t_indicateur_condition.value IS DISTINCT FROM w_indicateur_condition.value OR
  		t_indicateur_condition.view_select_rule IS DISTINCT FROM w_indicateur_condition.view_select_rule OR
  		t_indicateur_condition.arbre_code IS DISTINCT FROM w_indicateur_condition.arbre_code OR
  		t_indicateur_condition.arbre_texte IS DISTINCT FROM w_indicateur_condition.arbre_texte
  		)
  	;
  
  	DELETE FROM compta.t_indicateur_condition
  	WHERE code LIKE 'CTI%' AND
  		code NOT IN (SELECT code FROM w_indicateur_condition)
  		;
  
  	UPDATE compta.t_indicateur_condition SET
  		column_texte = CASE column_name
  			WHEN 'x' THEN 'x'
  			ELSE replace(Upper(substr(split_part(column_name,'_id',1),1,1)) || substr(split_part(column_name,'_id',1),2),'_',' ') END
  	WHERE 
  		column_texte IS DISTINCT FROM 
  			CASE column_name
  			WHEN 'x' THEN 'x'
  			ELSE replace(Upper(substr(split_part(column_name,'_id',1),1,1)) || substr(split_part(column_name,'_id',1),2),'_',' ') END
  	;
  
  	UPDATE compta.t_indicateur_condition
  	SET external_code = split_part(code,'#',1)||'#'||arbre_code[2]
  	WHERE external_code IS DISTINCT FROM (split_part(code,'#',1)||'#'||arbre_code[2])
  	;
  	
  	DROP TABLE IF EXISTS w_indicateur_condition_table;
  	CREATE TEMP TABLE w_indicateur_condition_table AS
  	SELECT t_indicateur_condition.oid AS indicateur_condition_id,
  		''::text As table_name,
  		t_indicateur_condition.column_name || ' ' ||
  		CASE 
  			WHEN t_indicateur_condition.value LIKE 'LS%' THEN '= ANY(ARRAY'
  			WHEN t_indicateur_condition.value LIKE 'LI%' THEN '= ANY(ARRAY'
  			WHEN t_indicateur_condition.value LIKE 'SL%' THEN '<> ALL(ARRAY'
  			WHEN t_indicateur_condition.value LIKE 'DT%' THEN '='
  			ELSE '='
  		END || ' ' ||
  		CASE 
  			WHEN t_indicateur_condition.value LIKE 'LS%' THEN 
  				'(SELECT to_id FROM compta.t_classes_sections_elements WHERE section_id = ' || substr(value,5) || ')'
  			WHEN t_indicateur_condition.value LIKE 'LI%' THEN 
  				'(SELECT to_id FROM compta.t_listes_contenu WHERE liste_id = ' || substr(value,3) || ')'
  			WHEN t_indicateur_condition.value LIKE 'SL%' THEN 
  				'(SELECT to_id FROM compta.t_listes_contenu WHERE liste_id = ' || substr(value,3) || ')'
  			WHEN t_indicateur_condition.value LIKE 'DT%' THEN 
  				substr(value,3)
  			ELSE substr(value,3)
  		END || ' ' ||
  		CASE 
  			WHEN t_indicateur_condition.value LIKE 'LS%' THEN ')'
  			WHEN t_indicateur_condition.value LIKE 'LI%' THEN ')'
  			WHEN t_indicateur_condition.value LIKE 'SL%' THEN ')'
  			ELSE ''
  		END AS check_where
  	FROM compta.t_indicateur_condition
  	WHERE t_indicateur_condition.code LIKE 'CTI%' 
  	;
  
  	
  
  
  
  	INSERT INTO compta.t_indicateur_condition_table(indicateur_condition_id, table_name, check_where)
  	SELECT indicateur_condition_id, table_name, check_where
  	FROM w_indicateur_condition_table
  	WHERE (indicateur_condition_id || '-' || table_name) NOT IN
  		(SELECT indicateur_condition_id || '-' || table_name FROM compta.t_indicateur_condition_table WHERE table_name IS NOT NULL)
  	;
  
  	UPDATE compta.t_indicateur_condition_table SET
  		check_where = w_indicateur_condition_table.check_where
  	FROM w_indicateur_condition_table
  	WHERE t_indicateur_condition_table.indicateur_condition_id = w_indicateur_condition_table.indicateur_condition_id AND
  		t_indicateur_condition_table.table_name = w_indicateur_condition_table.table_name AND
  		(
  		t_indicateur_condition_table.check_where IS DISTINCT FROM w_indicateur_condition_table.check_where 
  		)
  	;
  
  	DELETE FROM compta.t_indicateur_condition_table
  	USING compta.t_indicateur_condition
  	WHERE t_indicateur_condition.oid = t_indicateur_condition_table.indicateur_condition_id AND
  		t_indicateur_condition.code LIKE 'CTI%' AND
  		(indicateur_condition_id || '-' || table_name) NOT IN
  		(SELECT indicateur_condition_id || '-' || table_name FROM w_indicateur_condition_table WHERE table_name IS NOT NULL)
  	;
  
  
  	RETURN 'OK';
  
  END;