return: text
lang: plpgsql
src: |
  DECLARE	
  	result TEXT;
  BEGIN
  	UPDATE compta.t_indicateurs
  	SET external_code = code
  	WHERE substr(code,1,3) IN ('*L_', '*C_') AND COALESCE(external_code,'') = '';
  
  	UPDATE compta.t_indicateurs
  	SET external_code = ''
  	WHERE external_code IS NULL
  	;
  		
  	DROP TABLE IF EXISTS w_indicateurs;
  	CREATE TEMP TABLE w_indicateurs AS
  	SELECT 
  		code, 
  		external_code,
  		old_code, 
  		liste_code,
  		texte, 
  		table_name, 
  		CASE 
  			WHEN classe IN ('2', '3', '5', '6') THEN column_name_debit 
  			WHEN classe2 IN ('41') THEN column_name_debit 
  			WHEN classe2 IN ('44') THEN column_name_debit 
  			WHEN classe2 IN ('45') THEN column_name_debit 
  			WHEN classe2 IN ('46') THEN column_name_debit 
  			WHEN classe IN ('1', '4', '7') THEN column_name_credit
  			ELSE column_name_debit  END AS column_name,
  		is_cti, 
  		entete, 
  		entete_etendue, 
  		total_function
  	FROM (
  		SELECT 
  			'*L_' || t_listes.code::text AS code, 
  			'*L_' || t_listes.oid::text AS external_code, 
  			'L_' || t_listes.code AS old_code, 
  			t_listes.code::text AS liste_code,
  			'Comptes de la liste : ' || t_listes.texte || CASE WHEN t_listes.code <> '' THEN ' (' || t_listes.code || ')' ELSE '' END as texte, 
  			'v_historique_ecritures_1' as table_name, 
  			'CASE WHEN ecriture_consolidee <> ''1'' AND v_historique_ecritures_1.clinique_honoraire = ''C'' AND journal_id NOT IN [LISTE:CTISIGJSAUFJRN] AND compte_id IN [LISTE:' || t_listes.code ||'] THEN  montant_solde_debit ELSE 0 END' as column_name_debit,
  			'CASE WHEN ecriture_consolidee <> ''1'' AND v_historique_ecritures_1.clinique_honoraire = ''C'' AND journal_id NOT IN [LISTE:CTISIGJSAUFJRN] AND compte_id IN [LISTE:' || t_listes.code ||'] THEN  montant_solde_credit ELSE 0 END' as column_name_credit,
  			'0' AS is_cti, 
  			t_listes.texte AS entete, 
  			t_listes.texte AS entete_etendue, 
  			'SUM'::text AS total_function,
  			MIN(substr(t_comptes.numero,1,1)) AS classe,
  			MIN(substr(t_comptes.numero,1,2)) AS classe2
  		FROM compta.t_listes 
  			JOIN compta.t_listes_tables ON (table_id = t_listes_tables.oid)
  			JOIN compta.t_listes_contenu ON (t_listes.oid = t_listes_contenu.liste_id)
  			JOIN compta.t_comptes ON (t_listes_contenu.to_id = t_comptes.oid)
  		WHERE t_listes_tables.code = 'CPT' AND t_listes.code <> '' AND  t_listes.is_cti <> '1' 
  		GROUP BY 1,2,3,4,5,6,7,8,9,10
  		HAVING MAX(clinique_honoraire) <> 'H' OR MIN(clinique_honoraire) <> 'H'
  	) w_indicateurs;
  	-- Comptes honoraires
  	INSERT INTO w_indicateurs
  	SELECT 
  		code, 
  		external_code,
  		old_code, 
  		liste_code,
  		texte, 
  		table_name, 
  		CASE 
  			WHEN classe IN ('2', '3', '5', '6') THEN column_name_debit 
  			WHEN classe2 IN ('41') THEN column_name_debit 
  			WHEN classe2 IN ('44') THEN column_name_debit 
  			WHEN classe2 IN ('45') THEN column_name_debit 
  			WHEN classe2 IN ('46') THEN column_name_debit 
  			WHEN classe IN ('1', '4', '7') THEN column_name_credit
  			ELSE column_name_debit  END AS column_name,
  		is_cti, 
  		entete, 
  		entete_etendue, 
  		total_function
  	FROM (
  		SELECT 
  		'*L_' || t_listes.code::text || '_H' AS code, 
  		'*L_' || t_listes.oid::text || '_H' AS external_code, 
  		'L_' || t_listes.code || '_H' AS old_code, 
  		t_listes.code AS liste_code,
  		'Comptes de la liste : ' || t_listes.texte || ' (HON)' || CASE WHEN t_listes.code <> '' THEN ' (' || t_listes.code || ')' ELSE '' END as texte, 
  		'v_historique_ecritures_1' as table_name, 
  		'CASE WHEN ecriture_consolidee <> ''1'' AND v_historique_ecritures_1.clinique_honoraire = ''H'' AND journal_id NOT IN [LISTE:CTISIGJSAUFJRN] AND compte_id IN [LISTE:' || t_listes.code ||'] THEN  montant_solde_debit ELSE 0 END' as column_name_debit,
  		'CASE WHEN ecriture_consolidee <> ''1'' AND v_historique_ecritures_1.clinique_honoraire = ''H'' AND journal_id NOT IN [LISTE:CTISIGJSAUFJRN] AND compte_id IN [LISTE:' || t_listes.code ||'] THEN  montant_solde_credit ELSE 0 END' as column_name_credit,
  		'0' AS is_cti, 
  		t_listes.texte AS entete, 
  		t_listes.texte AS entete_etendue, 
  		'SUM'::text AS total_function,
  		MIN(substr(t_comptes.numero,1,1)) AS classe,
  		MIN(substr(t_comptes.numero,1,2)) AS classe2
  	FROM compta.t_listes 
  		JOIN compta.t_listes_tables ON (table_id = t_listes_tables.oid)
  		JOIN compta.t_listes_contenu ON (t_listes.oid = t_listes_contenu.liste_id)
  		JOIN compta.t_comptes ON (t_listes_contenu.to_id = t_comptes.oid)
  	WHERE t_listes_tables.code = 'CPT' AND t_listes.code <> '' AND  t_listes.is_cti <> '1' 
  	GROUP BY 1,2,3,4,5,6,7,8,9,10
  	HAVING MAX(clinique_honoraire) = 'H' AND MIN(clinique_honoraire) = 'H'
  	) w_indicateurs;
  	INSERT INTO w_indicateurs
  	SELECT 
  		code, 
  		external_code,
  		old_code, 
  		liste_code,
  		texte, 
  		table_name, 
  		CASE 
  			WHEN classe IN ('2', '3', '5', '6') THEN column_name_debit 
  			WHEN classe2 IN ('41') THEN column_name_debit 
  			WHEN classe2 IN ('44') THEN column_name_debit 
  			WHEN classe2 IN ('45') THEN column_name_debit 
  			WHEN classe2 IN ('46') THEN column_name_debit 
  			WHEN classe IN ('1', '4', '7') THEN column_name_credit
  			ELSE column_name_debit  END AS column_name,
  		is_cti, 
  		entete, 
  		entete_etendue, 
  		total_function
  	FROM (
  		SELECT 
  		'*L_' || t_listes.code::text || '_H' AS code, 
  		'*L_' || t_listes.oid::text || '_H' AS external_code, 
  		'L_' || t_listes.code || '_H' AS old_code, 
  		t_listes.code AS liste_code,
  		'Comptes de la liste : ' || t_listes.texte || ' (HON)' || CASE WHEN t_listes.code <> '' THEN ' (' || t_listes.code || ')' ELSE '' END as texte, 
  		'v_historique_ecritures_1' as table_name, 
  		'CASE WHEN ecriture_consolidee <> ''1'' AND v_historique_ecritures_1.clinique_honoraire = ''H'' AND journal_id NOT IN [LISTE:CTISIGJSAUFJRN] AND compte_id IN [LISTE:' || t_listes.code ||'] THEN  montant_solde_debit ELSE 0 END' as column_name_debit,
  		'CASE WHEN ecriture_consolidee <> ''1'' AND v_historique_ecritures_1.clinique_honoraire = ''H'' AND journal_id NOT IN [LISTE:CTISIGJSAUFJRN] AND compte_id IN [LISTE:' || t_listes.code ||'] THEN  montant_solde_credit ELSE 0 END' as column_name_credit,
  		'0' AS is_cti, 
  		t_listes.texte AS entete, 
  		t_listes.texte AS entete_etendue, 
  		'SUM'::text AS total_function,
  		MIN(substr(t_comptes.numero,1,1)) AS classe,
  		MIN(substr(t_comptes.numero,1,2)) AS classe2
  	FROM compta.t_listes 
  		JOIN compta.t_listes_tables ON (table_id = t_listes_tables.oid)
  		JOIN compta.t_listes_contenu ON (t_listes.oid = t_listes_contenu.liste_id)
  		JOIN compta.t_comptes ON (t_listes_contenu.to_id = t_comptes.oid)
  	WHERE t_listes_tables.code = 'CPT' AND t_listes.code <> '' AND  t_listes.is_cti <> '1' 
  	GROUP BY 1,2,3,4,5,6,7,8,9,10
  	HAVING MAX(clinique_honoraire) = 'H' AND MIN(clinique_honoraire) <> 'H'
  	) w_indicateurs;
  	
  	UPDATE compta.t_indicateurs
  	SET code = w_indicateurs.code,
  		texte = w_indicateurs.texte
  	FROM w_indicateurs
  	WHERE w_indicateurs.external_code = t_indicateurs.external_code AND
  		(
  		t_indicateurs.texte IS DISTINCT FROM w_indicateurs.texte OR
  		t_indicateurs.code IS DISTINCT FROM w_indicateurs.code
  		);
  		
  	UPDATE compta.t_indicateurs
  	SET column_name = w_indicateurs.column_name
  	FROM w_indicateurs
  	WHERE w_indicateurs.external_code = t_indicateurs.external_code AND
  		t_indicateurs.column_name IS DISTINCT FROM w_indicateurs.column_name AND
  		t_indicateurs.column_name NOT LIKE '%LISTE:' || liste_code || '%' AND liste_code <> '';
  		
  	INSERT INTO compta.t_indicateurs(
  		code, 
  		external_code,
  		texte, 
  		table_name, 
  		column_name, 
  		is_cti, 
  		entete, 
  		entete_etendue, 
  		total_function,
  		dimension_date)
  	SELECT 
  		code, 
  		external_code,
  		texte, 
  		table_name, 
  		column_name, 
  		is_cti, 
  		entete, 
  		entete_etendue, 
  		total_function,
  		'4'::text AS dimension_date
  	FROM w_indicateurs 
  	WHERE external_code NOT IN (SELECT external_code FROM compta.t_indicateurs WHERE external_code IS NOT NULL);
  	-- CLasses
  	TRUNCATE w_indicateurs;
  	INSERT INTO w_indicateurs
  	SELECT 
  		code, 
  		external_code,
  		old_code, 
  		classe_section_code,
  		texte, 
  		table_name, 
  		column_name,
  		is_cti, 
  		entete, 
  		entete_etendue, 
  		total_function
  	FROM (
  		SELECT 
  		'*C_' || t_classes.code|| '-' || t_classes_sections.code || '-D' AS code,
  		'*C_' || t_classes.oid::text || '-' || t_classes_sections.oid::text || '-D' AS external_code, 
  		''::text AS old_code, 
  		t_classes.code|| ':' || t_classes_sections.code AS classe_section_code,
  		'Comptes de la section : ' || t_classes.texte || ' - ' || t_classes_sections.texte || ' (DEBIT)' as texte, 
  		'v_historique_ecritures_1' as table_name, 
  		'CASE WHEN ecriture_consolidee <> ''1'' AND v_historique_ecritures_1.clinique_honoraire = ''C'' AND journal_id NOT IN [LISTE:CTISIGJSAUFJRN] AND compte_id IN [CLASSE:' || t_classes.code || ':' || t_classes_sections.code ||'] THEN  montant_solde_debit ELSE 0 END' as column_name,
  		'0' AS is_cti, 
  		t_classes_sections.texte AS entete, 
  		t_classes_sections.texte AS entete_etendue, 
  		'SUM'::text AS total_function
  	FROM compta.t_classes
  		JOIN compta.t_listes_tables ON (table_id = t_listes_tables.oid)
  		JOIN compta.t_classes_sections ON t_classes_sections.classe_id = t_classes.oid
  	WHERE t_listes_tables.code = 'CPT' AND t_classes.code <> '' AND  t_classes.is_cti <> '1' AND t_classes.sequence <> 9
  	GROUP BY 1,2,3,4,5,6,7,8,9,10,11
  	) w_indicateurs;
  	INSERT INTO w_indicateurs
  	SELECT 
  		code, 
  		external_code,
  		old_code, 
  		classe_section_code,
  		texte, 
  		table_name, 
  		column_name,
  		is_cti, 
  		entete, 
  		entete_etendue, 
  		total_function
  	FROM (
  		SELECT 
  		'*C_' || t_classes.code|| '-' || t_classes_sections.code || '-C' AS code,
  		'*C_' || t_classes.oid::text || '-' || t_classes_sections.oid::text || '-C' AS external_code, 
  		''::text AS old_code, 
  		t_classes.code|| ':' || t_classes_sections.code AS classe_section_code,
  		'Comptes de la section : ' || t_classes.texte || ' - ' || t_classes_sections.texte || ' (CREDIT)' as texte, 
  		'v_historique_ecritures_1' as table_name, 
  		'CASE WHEN ecriture_consolidee <> ''1'' AND v_historique_ecritures_1.clinique_honoraire = ''C'' AND journal_id NOT IN [LISTE:CTISIGJSAUFJRN] AND compte_id IN [CLASSE:' || t_classes.code || ':' || t_classes_sections.code ||'] THEN  montant_solde_credit ELSE 0 END' as column_name,
  		'0' AS is_cti, 
  		t_classes_sections.texte AS entete, 
  		t_classes_sections.texte AS entete_etendue, 
  		'SUM'::text AS total_function
  	FROM compta.t_classes
  		JOIN compta.t_listes_tables ON (table_id = t_listes_tables.oid)
  		JOIN compta.t_classes_sections ON t_classes_sections.classe_id = t_classes.oid
  	WHERE t_listes_tables.code = 'CPT' AND t_classes.code <> '' AND  t_classes.is_cti <> '1' AND t_classes.sequence <> 9
  	GROUP BY 1,2,3,4,5,6,7,8,9,10,11
  	) w_indicateurs;
  	UPDATE compta.t_indicateurs
  	SET code = w_indicateurs.code,
  		texte = w_indicateurs.texte
  	FROM w_indicateurs
  	WHERE w_indicateurs.external_code = t_indicateurs.external_code AND
  		(
  		t_indicateurs.texte IS DISTINCT FROM w_indicateurs.texte OR
  		t_indicateurs.code IS DISTINCT FROM w_indicateurs.code
  		);
  	INSERT INTO compta.t_indicateurs(
  		code, 
  		external_code,
  		texte, 
  		table_name, 
  		column_name, 
  		is_cti, 
  		entete, 
  		entete_etendue, 
  		total_function,
  		dimension_date)
  	SELECT 
  		code, 
  		external_code,
  		texte, 
  		table_name, 
  		column_name, 
  		is_cti, 
  		entete, 
  		entete_etendue, 
  		total_function,
  		'4'::text AS dimension_date
  	FROM w_indicateurs 
  	WHERE external_code NOT IN (SELECT external_code FROM compta.t_indicateurs WHERE external_code IS NOT NULL);
  	-- Changement de codes à reporter
  	UPDATE compta.t_indicateurs
  	SET column_name = replace(column_name,split_part(split_part(column_name,'[CLASSE:',2),']',1),(t_classes.code || ':' || t_classes_sections.code))
  	FROM compta.t_classes_sections
  	JOIN compta.t_classes ON t_classes.oid = t_classes_sections.classe_id
  	WHERE t_indicateurs.external_code like '*C%' AND
  		t_classes_sections.oid::text = split_part(substr(t_indicateurs.external_code,4),'-',2) AND
  		t_classes_sections.classe_id::text = split_part(substr(t_indicateurs.external_code,4),'-',1) AND
  		column_name LIKE '%[CLASSE:%' AND
  		split_part(split_part(column_name,'[CLASSE:',2),']',1) <> (t_classes.code || ':' || t_classes_sections.code)
  	;
  	-- Ménage
  	DELETE FROM compta.t_indicateurs
  	WHERE oid IN
  	(
  	SELECT
  		t_indicateurs.oid
  	FROM compta.t_indicateurs
  	LEFT JOIN compta.t_classes_sections ON t_classes_sections.oid = split_part(substr(t_indicateurs.external_code,4),'-',2)::bigint
  	WHERE t_indicateurs.external_code like '*C%' AND
  		t_indicateurs.oid NOT IN (SELECT indicateur_id FROM compta.t_rapports_rubriques) AND
  		t_indicateurs.oid NOT IN (SELECT indicateur_id FROM compta.p_indicateurs_cumules) AND
  		(t_classes_sections.oid IS NULL OR
  		t_classes_sections.classe_id IS DISTINCT FROM split_part(substr(t_indicateurs.external_code,4),'-',1)) 
  	);
  
  	-- Reorganisation et valeurs par defaut
  	PERFORM base.cti_reorganize_indicateurs('compta');
  			
  	
  	RETURN 'OK';
  END;