return: text
lang: plpgsql
src: |
  DECLARE	
    	
    BEGIN
  
  
  		DROP TABLE IF EXISTS w_indicateurs;
  		CREATE TEMP TABLE w_indicateurs AS 
  		SELECT 'CTI_OVA_T1V1SV_NBSJTRANS'::text AS indcod, 'T1V1SV'::text AS tabcod, 1::numeric AS tabseq, 'D'::text AS lintyp, '%transmis%'::text AS lintxt, ''::text AS linoxt, 2::numeric AS valcol
  		UNION
  		SELECT 'CTI_OVA_T1V1SV_MTBR_TRANS', 'T1V1SV', 1, 'D', '%transmis%', '', 3
  		UNION
  		SELECT 'CTI_OVA_T1V1SV_NBSJNTRAIT_AMO'::text AS indcod, 'T1V1SV'::text AS tabcod, 1::numeric AS tabseq, 'D'::text AS lintyp, '%CM 90%|%inter-établissement%|%GHS 9999%'::text AS lintxt, ''::text AS linoxt, 2::numeric AS valcol
  		UNION
  		SELECT 'CTI_OVA_T1V1SV_MTBR_NTRAIT_AMO'::text AS indcod, 'T1V1SV'::text AS tabcod, 1::numeric AS tabseq, 'D'::text AS lintyp, '%CM 90%|%inter-établissement%|%GHS 9999%'::text AS lintxt, ''::text AS linoxt, 3::numeric AS valcol
  		UNION
  		SELECT 'CTI_OVA_T1S1SV_NBSJNVALO_AMO'::text AS indcod, 'T1V1SV'::text AS tabcod, 1::numeric AS tabseq, 'D'::text AS lintyp, '%non facturable%'::text AS lintxt, ''::text AS linoxt, 2::numeric AS valcol
  		UNION
  		SELECT 'CTI_OVA_T1S1SV_MTBR_NVALO_AMO'::text AS indcod, 'T1V1SV'::text AS tabcod, 1::numeric AS tabseq, 'D'::text AS lintyp, '%non facturable%'::text AS lintxt, ''::text AS linoxt, 3::numeric AS valcol
  		UNION
  		SELECT 'CTI_OVA_T1V1SV_NBSJVALO_AMO'::text AS indcod, 'T1V1SV'::text AS tabcod, 1::numeric AS tabseq, 'D'::text AS lintyp, '%séjours valorisés%'::text AS lintxt, ''::text AS linoxt, 2::numeric AS valcol
  		UNION
  		SELECT 'CTI_OVA_T1S1SV_MTBR_VALO_AMO'::text AS indcod, 'T1V1SV'::text AS tabcod, 1::numeric AS tabseq, 'D'::text AS lintyp, '%séjours valorisés%'::text AS lintxt, ''::text AS linoxt, 3::numeric AS valcol
  		UNION
  		
  		SELECT 'CTI_OVA_T1V1RAV_MTBR_VALO_AMO_GHS', 'T1V1RAV', 1, 'D', '%Valorisation des GHS%', '', 4
  		UNION
  		SELECT 'CTI_OVA_T1V1RAV_MTRB_VALO_AMO_GHS', 'T1V1RAV', 1, 'D', '%Valorisation des GHS%', '', 6
  		UNION
  		SELECT 'CTI_OVA_T1V1RAV_MTBR_VALO_AMO_EXB', 'T1V1RAV', 1, 'D', '%extrême bas%|%rehosp dans même GHM%', '', 4
  		UNION
  		SELECT 'CTI_OVA_T1V1RAV_MTRB_VALO_AMO_EXB', 'T1V1RAV', 1, 'D', '%extrême bas%|%rehosp dans même GHM%', '', 6
  		UNION
  		SELECT 'CTI_OVA_T1V1RAV_MTBR_VALO_AMO_EXH', 'T1V1RAV', 1, 'D', '%extrême haut%', '', 4
  		UNION
  		SELECT 'CTI_OVA_T1V1RAV_MTRB_VALO_AMO_EXH', 'T1V1RAV', 1, 'D', '%extrême haut%', '', 6
  		UNION
  		SELECT 'CTI_OVA_T1V1RAV_MTBR_VALO_AMO_SUP', 'T1V1RAV', 1, 'D', '', '%extrême bas%|%rehosp dans même GHM%|%extrême haut%|%Valorisation des GHS%|%Total valorisation%', 4
  		UNION
  		SELECT 'CTI_OVA_T1V1RAV_MTRB_VALO_AMO_SUP', 'T1V1RAV', 1, 'D', '', '%extrême bas%|%rehosp dans même GHM%|%extrême haut%|%Valorisation des GHS%|%Total valorisation%', 6
  		UNION
  		SELECT 'CTI_OVA_T1V1RAV_MTBR_VALO_AMO_GHSSUP', 'T1V1RAV', 1, 'D', '%Total valorisation%', '', 4
  		UNION
  		SELECT 'CTI_OVA_T1V1RAV_MTRB_VALO_AMO_GHSSUP', 'T1V1RAV', 1, 'D', '%Total valorisation%', '', 6
  		UNION
  		SELECT 'CTI_OVA_T1V2VMED_MTBR_VALO_AMO_UCDEMI', 'T1V2VMED', 2, 'F', '', '', 8
  		UNION
  		SELECT 'CTI_OVA_T1V2VMED_MTRB_VALO_AMO_UCDEMI', 'T1V2VMED', 2, 'F', '', '', 8
  		UNION
  		SELECT 'CTI_OVA_T1V3VDMI_MTBR_VALO_AMO_DMIETI', 'T1V3VDMI', 2, 'F', '', '', 6
  		UNION
  		SELECT 'CTI_OVA_T1V3VDMI_MTRB_VALO_AMO_DMIETI', 'T1V3VDMI', 2, 'F', '', '', 6
  
  		
  		UNION
  		SELECT 'CTI_OVA_T1V4RAME_MTRB_VALO_AME_GHS', 'T1V4RAME', 4, 'D', '%Valorisation des GHS%', '', 2
  		UNION
  		SELECT 'CTI_OVA_T1V4RAME_MTBR_VALO_AME_EXB', 'T1V4RAME', 4, 'D', '%extrême bas%', '', 2
  		UNION
  		SELECT 'CTI_OVA_T1V4RAME_MTRB_VALO_AME_EXB', 'T1V4RAME', 4, 'D', '%extrême bas%', '', 2
  		UNION
  		SELECT 'CTI_OVA_T1V4RAME_MTBR_VALO_AME_EXH', 'T1V4RAME', 4, 'D', '%extrême haut%', '', 2
  		UNION
  		SELECT 'CTI_OVA_T1V4RAME_MTRB_VALO_AME_EXH', 'T1V4RAME', 4, 'D', '%extrême haut%', '', 2
  		UNION
  		SELECT 'CTI_OVA_T1V4RAME_MTBR_VALO_AME_SUP', 'T1V4RAME', 4, 'D', '', '%extrême bas%|%extrême haut%|%Valorisation des GHS%|%Total valorisation%', 2
  		UNION
  		SELECT 'CTI_OVA_T1V4RAME_MTRB_VALO_AME_SUP', 'T1V4RAME', 4, 'D', '', '%extrême bas%|%extrême haut%|%Valorisation des GHS%|%Total valorisation%', 2
  		
  		UNION
  		SELECT 'CTI_OVA_T1V4RAME_MTRB_VALO_AME_GHSSUP', 'T1V4RAME', 4, 'D', '%Total valorisation%', '', 2
  		UNION
  		SELECT 'CTI_OVA_T1V4MDAME_MTBR_VALO_AME_DMIETI', 'T1V4MDAME', 2, 'F', '', '', 6
  		UNION
  		SELECT 'CTI_OVA_T1V4MDAME_MTRB_VALO_AME_DMIETI', 'T1V4MDAME', 2, 'F', '', '', 6
  		
  		UNION
  		SELECT 'CTI_OVA_T2VFIDES_MTRB_VALO_AMO_CCAM', 'T2VFIDES', 2, 'D', 'CCAM', '', 4
  		UNION
  		SELECT 'CTI_OVA_T2VFIDES_MTRB_VALO_AMO_CCAM', 'T2VFIDES', 2, 'D', 'CCAM', '', 5
  		UNION
  		SELECT 'CTI_OVA_T2VFIDES_MTRB_VALO_AMO_NGAP', 'T2VFIDES', 2, 'D', 'NGAP', '', 4
  		UNION
  		SELECT 'CTI_OVA_T2VFIDES_MTRB_VALO_AMO_NGAP', 'T2VFIDES', 2, 'D', 'NGAP', '', 5
  		UNION
  		SELECT 'CTI_OVA_T2VFIDES_MTRB_VALO_AMO_FFM', 'T2VFIDES', 2, 'D', 'FFM', '', 4
  		UNION
  		SELECT 'CTI_OVA_T2VFIDES_MTRB_VALO_AMO_FFM', 'T2VFIDES', 2, 'D', 'FFM', '', 5
  		UNION
  		SELECT 'CTI_OVA_T2VFIDES_MTRB_VALO_AMO_SE', 'T2VFIDES', 2, 'D', 'SE', '', 4
  		UNION
  		SELECT 'CTI_OVA_T2VFIDES_MTRB_VALO_AMO_SE', 'T2VFIDES', 2, 'D', 'SE', '', 5
  		UNION
  		SELECT 'CTI_OVA_T2VFIDES_MTRB_VALO_AMO_AP2', 'T2VFIDES', 2, 'D', 'AP2', '', 4
  		UNION
  		SELECT 'CTI_OVA_T2VFIDES_MTRB_VALO_AMO_AP2', 'T2VFIDES', 2, 'D', 'AP2', '', 5
  		UNION
  		SELECT 'CTI_OVA_T2VFIDES_MTRB_VALO_AMO_MOP', 'T2VFIDES', 2, 'D', 'MOP', '', 4
  		UNION
  		SELECT 'CTI_OVA_T2VFIDES_MTRB_VALO_AMO_MOP', 'T2VFIDES', 2, 'D', 'MOP', '', 5
  		UNION
  		SELECT 'CTI_OVA_T2VFIDES_MTRB_VALO_AMO_FPI', 'T2VFIDES', 2, 'D', 'FPI', '', 4
  		UNION
  		SELECT 'CTI_OVA_T2VFIDES_MTRB_VALO_AMO_FPI', 'T2VFIDES', 2, 'D', 'FPI', '', 5
  		UNION
  		SELECT 'CTI_OVA_T2VFIDES_MTRB_VALO_AMO', 'T2VFIDES', 2, 'F', '', '', 4
  		UNION
  		SELECT 'CTI_OVA_T2VFIDES_MTRB_VALO_AMO', 'T2VFIDES', 2, 'F', '', '', 5
  		
  		;
  
  			
  
  		
  		DROP TABLE IF EXISTS w_indicateurs_ovalide;
  		CREATE TEMP TABLE w_indicateurs_ovalide AS
  		SELECT 
  			t_indicateurs.oid AS indicateur_id,
  			p_calendrier_mois.date_debut AS date_reference,
  			SUM(base.Cti_to_number(data_mat2a[valcol])) AS value
  		FROM pmsi.p_imports 
  		JOIN 
  			(SELECT
  			 	annee_rss, (MAX(ARRAY[nb,import_id]))[2] AS import_id
  			 FROM
  			 (SELECT 
  			 	date_part('year',date_sortie) AS annee_rss, import_id, count(*) AS nb
  			 FROM pmsi.p_rss
  			 WHERE import_id > 0 AND
  			 	date_sortie >= '20180101' AND
  			 	etat = ''
  			GROUP BY 1,2
  			 ) subview
  			 GROUP BY 1
  			 ) subview ON p_imports.oid = import_id
  		JOIN pmsi.p_mat2a_import ON p_imports.mat2a_import_id = p_mat2a_import.oid
  		JOIN (SELECT mat2a_annee AS mat2a_annee_last, MAX(oid) AS import_id_last FROM pmsi.p_mat2a_import GROUP BY 1) p_mat2a_import_last ON p_mat2a_import.oid = p_mat2a_import_last.import_id_last
  		JOIN pmsi.p_mat2a_data ON p_mat2a_data.mat2a_import_id = p_mat2a_import.oid
  		JOIN w_indicateurs ON 1=1
  		JOIN pmsi.t_indicateurs ON t_indicateurs.code = indcod
  		JOIN base.p_calendrier_mois ON
  			(mat2a_annee * 100 + mat2a_mois) = p_calendrier_mois.mois
  		WHERE mat2a_annee >= 2018 AND
  			file_code = tabcod AND
  			table_sequence = tabseq AND
  			line_type = lintyp AND
  			(
  				lintxt = '' OR
  				data_mat2a[1] ILIKE ANY(string_to_array(lintxt,'|'))
  				
  			) AND
  			(
  				linoxt = '' OR
  				data_mat2a[1] NOT ILIKE ALL(string_to_array(linoxt,'|'))
  			)
  		GROUP BY 1,2
  		ORDER BY 1,2,3
  		;
  
  					
  
  		DELETE FROM pmsi.p_indicateurs_cumules
  		WHERE periode_type IN ('M_MCOOVALIDE'::text)
  		;
  
  
  		INSERT INTO pmsi.p_indicateurs_cumules(
  			date_reference, 
  			finess_id, 
  			pole_id, 
  			indicateur_id, 
  			periode_type, 
  			value, 
  			value_cum, 
  			value_cum12, 
  			value_exercice, 
  			value_trimestre, 
  			value_cum4t, 
  			indicateur_condition_id
  			)
  		SELECT
  			date_reference, 
  			0 AS finess_id, 
  			0 AS pole_id, 
  			indicateur_id, 
  			'M_MCOOVALIDE'::text AS periode_type, 
  			value, 
  			value AS value_cum, 
  			0 AS value_cum12, 
  			0 AS value_exercice, 
  			0 AS value_trimestre, 
  			0 AS value_cum4t, 
  			NULL AS indicateur_condition_id
  		FROM w_indicateurs_ovalide
  		WHERE date_reference >= '20160101' AND
  			value <> 0
  		;
  					
  					
  						
    		
    	RETURN 'OK';
    END;