return: text
lang: plpgsql
parameters:
  p0:
    type: bigint
    name: i_import_id
  p1:
    type: bigint
    name: i_mat2a_import_id
src: |
  DECLARE	
  
    BEGIN
    	RAISE NOTICE '%' , 'Identification des imports';
    	IF NOT EXISTS (SELECT * FROM pg_class WHERE relnamespace = pg_my_temp_schema() AND relname = 'w_cp_imports') THEN
    		DROP TABLE IF EXISTS w_cp_imports;
    		CREATE TEMP TABLE w_cp_imports (import_id bigint, mat2a_import_id bigint, mat2a_annee numeric, mat2a_mois numeric);
    	END IF;
    	TRUNCATE w_cp_imports;
    	INSERT INTO w_cp_imports 
    	SELECT MAX(p_imports.oid) AS import_id, (MAX(Array[p_imports.oid, p_imports.mat2a_import_id]))[2] AS mat2a_import_id, MAX(mat2a_annee) AS mat2a_annee, MAX(mat2a_mois) AS mat2a_mois
    	FROM pmsi.p_imports
    	JOIN pmsi.p_mat2a_import ON p_imports.mat2a_import_id = p_mat2a_import.oid
    	WHERE date_fin BETWEEN '20140101' AND date(now()) AND
    		mat2a_import_id <> 0 AND
    		etat = 'G' AND
    		p_imports.oid IN (SELECT import_id FROM pmsi.p_rss WHERE etat = '') AND
    		(i_import_id = -1 OR p_imports.oid = i_import_id) AND
    		(i_mat2a_import_id = -1 OR p_imports.mat2a_import_id = i_mat2a_import_id)
    	HAVING MAX(p_imports.oid) IS NOT NULL;
    	--DROP TABLE IF EXISTS temp.w_cp_imports; CREATE TABLE temp.w_cp_imports AS SELECT * FROM w_cp_imports;
    	--drop table if exists temp.w_cp_cti_fact2a; CREATE Table temp.w_cp_cti_fact2a as select * from w_cp_cti_fact2a;
    	-- Import à traiter ?
    	IF NOT EXISTS (SELECT * FROM w_cp_imports LIMIT 1) THEN
    		return 'Pas d''import à traiter';
    	END IF;
    	RAISE NOTICE '%' , 'Initialisations';
    	UPDATE pmsi.p_mat2a_data
    	SET data_mat2a[1] = 'CG  F Majoration consultation suivi de grossesse Férié'
    	WHERE data_mat2a[1] ILIKE '%CG Consultation suivi de grossesse Férié%';
    	UPDATE pmsi.p_mat2a_data
    	SET data_mat2a[1] = 'CG  N Majoration consultation suivi de grossesse Nuit'
    	WHERE data_mat2a[1] ILIKE '%CG Consultation suivi de grossesse Nuit%';
  
        -- Numéro de table dans la page 2.Q.HPNPC où apparaissent les prestations non prises en compte
  
  	IF NOT EXISTS (SELECT * FROM pg_class WHERE relnamespace = pg_my_temp_schema() AND relname = 'w_seq_hpnpc') THEN
  		DROP TABLE IF EXISTS w_seq_hpnpc;
  		CREATE TEMP TABLE w_seq_hpnpc (
  			import_id bigint, 
    			mat2a_import_id bigint,
    			mat2a_annee numeric,
    			seq_hpnpc bigint
    			);
  	END IF;
  	TRUNCATE w_seq_hpnpc;
  	INSERT INTO w_seq_hpnpc
  	SELECT
  		w_cp_imports.import_id,
  		w_cp_imports.mat2a_import_id,
  		w_cp_imports.mat2a_annee,
  		MAX(table_sequence) as table_sequence
  	FROM
  		pmsi.p_mat2a_data
  		JOIN w_cp_imports ON w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id
  	WHERE 1=1
  		AND file_code = 'T2QHPNPC'
  		AND line_type = 'T'
  		AND array_to_string(data_mat2a,',') like '%non prises en compte%' OR array_to_string(data_mat2a,',')like '%Lettres clé non valorisées%'
  	GROUP BY 1,2,3;
  
  	      -- Réaffectation des prestations non valorisées d'après le tableau mat2a
  	IF 1 <> 1 THEN 
  
  		RAISE NOTICE '%' , 'Prestations non valorisees';
  
  		IF NOT EXISTS (SELECT * FROM pg_class WHERE relnamespace = pg_my_temp_schema() AND relname = 'w_cp_non_valorises') THEN
  			DROP TABLE IF EXISTS w_cp_non_valorises;
  			CREATE TEMP TABLE w_cp_non_valorises (rss_id bigint, prestation_id bigint);
  			CREATE INDEX i_w_cp_non_valorises_1
  			  ON w_cp_non_valorises
  			  USING btree
  			  (rss_id);
  			CREATE INDEX i_w_cp_non_valorises_2
  			  ON w_cp_non_valorises
  			  USING btree
  			  (prestation_id);
  		END IF;
  		TRUNCATE w_cp_non_valorises;
  		INSERT INTO w_cp_non_valorises
  		SELECT rss_id, prestation_id	
  		FROM pmsi.p_rss
  		JOIN pmsi.p_rsf_detail ON p_rsf_detail.rss_id = p_rss.oid
  		JOIN w_cp_imports ON w_cp_imports.import_id = p_rss.import_id
  		WHERE en_cours = '0' AND 1<>1 AND
  			est_ligne_rss = '1' AND
  			prestation_id = ANY ( array(
  				SELECT 
  					t_prestations.oid
  				FROM 
  					pmsi.p_mat2a_data
  					JOIN pmsi.t_prestations ON t_prestations.code = split_part(data_mat2a[1],' ',1)
  					JOIN w_cp_imports ON w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id
  					JOIN w_seq_hpnpc ON w_seq_hpnpc.mat2a_import_id = p_mat2a_data.mat2a_import_id
  				WHERE 
  					file_code = 'T2QHPNPC' AND 
  					table_sequence = w_seq_hpnpc.seq_hpnpc AND 
  					line_type IN ('D')
  				GROUP BY 1
  				)::bigint[])
  		GROUP BY 1,2
  		;
  
  		-- Les prestations APC, APV, APY, AVY et APU ne sont valorisées qu'à partir d'octobre 2017
  		-- Il faut donc partiellement les prendre en compte dans le tableau 'T2QHPNPC'
  		WITH w_to_delete AS (
  			SELECT
  				rss_id,
  				prestation_id
  			FROM
  				pmsi.p_rsf_detail
  				JOIN pmsi.t_prestations ON t_prestations.oid = p_rsf_detail.prestation_id
  			WHERE 1=1
  				AND t_prestations.code IN ('APC','APV','APY','AVY','APU')
  				AND p_rsf_detail.date_debut >= '20171001'::date
  		)
  		DELETE FROM w_cp_non_valorises
  		USING
  			w_to_delete
  		WHERE 1=1
  			AND w_cp_non_valorises.rss_id = w_to_delete.rss_id
  			AND w_cp_non_valorises.prestation_id = w_to_delete.prestation_id;
  
  		INSERT INTO pmsi.p_rsf_detail_non_valorise(
  			finess, no_rss, nature, mt, dmt, date_debut, date_fin, nombre, 
  			coefficient, prix_unitaire, base_remboursement, taux_remboursement, 
  			sejour_facture, sejour_remboursable, compteur, ligne_t2a, pec_fj, 
  			coefficient_mco, sejour_remboursable_2, ghs_id, sejour_rembourse_noemie, 
  			nature_noemie, prestation_id, rss_id, type, coefficient_geo, 
  			est_ligne_rss, est_ligne_rum, no_rum, unite_medicale_id, base_remboursement_rum, 
  			montant_supplement_nf, sejour_remboursable_rum, pec_fj_id, base_remboursement_sauf_transition, 
  			base_remboursement_rum_sauf_transition, regle_id)
  		SELECT
  			finess, no_rss, nature, mt, dmt, date_debut, date_fin, nombre, 
  			coefficient, prix_unitaire, base_remboursement, taux_remboursement, 
  			sejour_facture, sejour_remboursable, compteur, ligne_t2a, pec_fj, 
  			coefficient_mco, sejour_remboursable_2, ghs_id, sejour_rembourse_noemie, 
  			nature_noemie, p_rsf_detail.prestation_id, p_rsf_detail.rss_id, type, coefficient_geo, 
  			est_ligne_rss, est_ligne_rum, no_rum, unite_medicale_id, base_remboursement_rum, 
  			montant_supplement_nf, sejour_remboursable_rum, pec_fj_id, base_remboursement_sauf_transition, 
  			base_remboursement_rum_sauf_transition, COALESCE(t_regles_non_valorisation.oid, 0) as regle_id
  		FROM pmsi.p_rsf_detail
  		JOIN w_cp_non_valorises ON w_cp_non_valorises.rss_id = p_rsf_detail.rss_id AND
  					w_cp_non_valorises.prestation_id = p_rsf_detail.prestation_id AND
  					est_ligne_rss = '1'
  	      LEFT JOIN pmsi.t_regles_non_valorisation ON t_regles_non_valorisation.code = '09'::text;
  
  
  
  
  
  		DELETE FROM pmsi.p_rsf_detail
  		USING w_cp_non_valorises
  		WHERE w_cp_non_valorises.rss_id = p_rsf_detail.rss_id AND
  			w_cp_non_valorises.prestation_id = p_rsf_detail.prestation_id;
  		UPDATE pmsi.p_rsf_total SET 
  			base_remboursement = COALESCE(subview.base_remboursement,0),
  			t2a_facture = COALESCE(subview.t2a_facture,0),
  			dmi_facture = COALESCE(subview.dmi_facture,0),
  			phc_facture = COALESCE(subview.phc_facture,0),
  			forfait_facture = COALESCE(subview.forfait_facture,0),
  			nb_exh = COALESCE(subview.nb_exh,0),
  			ca_exh = COALESCE(subview.ca_exh,0),
  			nb_exb = COALESCE(subview.nb_exb,0),
  			ca_exb = COALESCE(subview.ca_exb,0),
  			nb_ghs =COALESCE( subview.nb_ghs,0),
  			ca_ghs = COALESCE(subview.ca_ghs,0),
  			nb_ghsmin = COALESCE(subview.nb_ghsmin,0)
  		FROM pmsi.p_rss
  		LEFT JOIN ( 
  		SELECT rss_id, 
  			SUM(p_rsf_detail.base_remboursement) as base_remboursement,   
  			SUM(CASE WHEN t_prestations.section_code[9] LIKE 'S1%' THEN p_rsf_detail.base_remboursement ELSE 0 END) as t2a_facture,  
  			SUM(CASE WHEN t_prestations.section_code[9] LIKE 'S2%' THEN p_rsf_detail.base_remboursement ELSE 0 END) as dmi_facture,  
  			SUM(CASE WHEN t_prestations.section_code[9] LIKE 'S3%' THEN p_rsf_detail.base_remboursement ELSE 0 END) as phc_facture, 
  
  			SUM(CASE WHEN t_prestations.code = 'EXH' THEN p_rsf_detail.nombre ELSE 0 END) as nb_exh,  
  			SUM(CASE WHEN t_prestations.code = 'EXH' THEN p_rsf_detail.base_remboursement ELSE 0 END) as ca_exh, 
  			SUM(CASE WHEN t_prestations.code = 'GHS' AND coefficient > 0 AND coefficient < 1 AND t_ghs.code NOT IN (9606,9616) THEN p_rsf_detail.nombre ELSE 0 END) as nb_exb,  
  			SUM(CASE WHEN t_prestations.code = 'GHS' AND coefficient > 0 AND coefficient < 1 AND t_ghs.code NOT IN (9606,9616) THEN p_rsf_detail.base_remboursement ELSE 0 END) as ca_exb, 
  			SUM(CASE WHEN t_prestations.code = 'GHS' AND coefficient > 0 AND coefficient < 1 AND t_ghs.code IN (9606,9616) THEN p_rsf_detail.nombre ELSE 0 END) as nb_ghsmin,  
  			SUM(CASE WHEN t_prestations.code = 'GHS' THEN p_rsf_detail.nombre ELSE 0 END) as nb_ghs,  
  			SUM(CASE WHEN t_prestations.code = 'GHS' THEN p_rsf_detail.base_remboursement ELSE 0 END) as ca_ghs,
  			SUM(CASE WHEN t_prestations.section_code[9] LIKE 'S13%' THEN p_rsf_detail.base_remboursement ELSE 0 END) as forfait_facture 
  		FROM pmsi.p_rsf_detail
  			JOIN pmsi.t_prestations ON p_rsf_detail.prestation_id =  t_prestations.oid JOIN pmsi.t_ghs ON t_ghs.oid = p_rsf_detail.ghs_id
  		WHERE p_rsf_detail.type = 'B' AND p_rsf_detail.est_ligne_rss = '1' AND p_rsf_detail.rss_id IN (SELECT rss_id FROM w_cp_non_valorises)
  		GROUP BY rss_id) subview ON p_rss.oid = subview.rss_id
  		WHERE p_rsf_total.rss_id = p_rss.oid AND p_rsf_total.rss_id IN (SELECT rss_id FROM w_cp_non_valorises);
  		UPDATE pmsi.p_rss 
  		SET base_remboursement = p_rsf_total.base_remboursement, 
  			sejour_facture = p_rsf_total.sejour_facture, 
  			honoraires_factures = p_rsf_total.honoraires_factures, 
  			t2a_facture = p_rsf_total.t2a_facture, 
  			dmi_facture = p_rsf_total.dmi_facture, 
  			phc_facture = p_rsf_total.phc_facture, 
  			ca_ghs_theorique = p_rsf_total.ca_ghs_theorique, 
  			nb_ghs = p_rsf_total.nb_ghs, 
  			ca_ghs = p_rsf_total.ca_ghs, 
  			nb_exb = p_rsf_total.nb_exb, 
  			ca_exb = p_rsf_total.ca_exb, 
  			nb_exh = p_rsf_total.nb_exh, 
  			ca_exh = p_rsf_total.ca_exh,
  			nb_ghsmin = p_rsf_total.nb_ghsmin,
  			forfait_facture = p_rsf_total.forfait_facture 
  		FROM pmsi.p_rsf_total 
  		WHERE p_rss.oid = p_rsf_total.rss_id AND
  			p_rss.oid IN (SELECT rss_id FROM w_cp_non_valorises);
  
    	END IF;
  
    	RAISE NOTICE '%' , 'Cumuls CTI. Sejours T2A';
    	IF NOT EXISTS (SELECT * FROM pg_class WHERE relnamespace = pg_my_temp_schema() AND relname = 'w_cp_cti_sejt2a') THEN
    		DROP TABLE IF EXISTS w_cp_cti_sejt2a;
    		CREATE TEMP TABLE w_cp_cti_sejt2a (
    			import_id bigint, 
    			mat2a_import_id bigint,
    			cti_transmis numeric,
    			cti_transmis_2 numeric,
    			cti_cmd_90 numeric,
    			cti_non_am numeric,
    			cti_non_am_2 numeric,
    			cti_valorises numeric,
    			cti_valorises_2 numeric,
    			cti_inter_etablissement numeric,
    			cti_inter_etablissement_nv numeric,
    			cti_ghs_9999 numeric,
    			cti_ivg numeric,
    			cti_rsa_seances numeric,
    			cti_seances numeric,
    			cti_ds_0 numeric
    		);
    	END IF;
    	TRUNCATE w_cp_cti_sejt2a;
  
    	/*
    		Modifs de CLA le 06-11-2015 : 
    		. Visiblement, pour les GHM de préparation aux irradiations (24Z04Z,28Z08Z,28Z09Z,28Z19Z,28Z20Z,28Z21Z,28Z22Z),
    		  OVALIDE ne compte qu'une séance par RSS même si nb_seances > 1
    		. Pour le décompte des séjours 0 nuit, il faut exclure les séjours de la CMD 90
  
    		Modif CLA le 18-03-2016 :
    		. Visiblement, depuis mars 2015, le décompte des prestations inter-établissements ne concerne pas que les séjours non traités.
    		  A partir de cette date, on compte les PIE à partir des séjours ME=0 et MS=0, peu importe leurs traitements ePMSI
    	*/
    	INSERT INTO w_cp_cti_sejt2a
    	SELECT p_rss.import_id, mat2a_import_id, 
    		count(*) AS cti_transmis,
    		SUM(CASE WHEN t_ghm_c.cmd_code <> '28' THEN 1 ELSE nb_seances END) AS cti_transmis_2,
    		SUM(CASE WHEN t_ghm_c.cmd_code = '90' THEN 1 ELSE 0 END) AS cti_cmd_90,
    		count(CASE WHEN traitement_epmsi IN ('24','31') THEN p_rss.oid ELSE NULL END) AS cti_non_am,
    		SUM(CASE WHEN traitement_epmsi IN ('24','31') THEN CASE WHEN t_ghm_c.cmd_code <> '28' THEN 1 ELSE nb_seances END ELSE 0 END) AS cti_non_am,
    		count(CASE WHEN traitement_epmsi = '30' THEN p_rss.oid ELSE NULL END) AS cti_valorises,
    		SUM(CASE WHEN traitement_epmsi = '30' THEN CASE WHEN t_ghm_c.cmd_code <> '28' THEN 1 ELSE nb_seances END ELSE NULL END) AS cti_valorises_2,
    		count(CASE WHEN (p_rss.date_sortie >= '20150301' OR traitement_epmsi = '12') AND traitement_epmsi <> '31' AND (mode_entree = '0' AND mode_sortie = '0') THEN p_rss.oid ELSE NULL END) AS cti_inter_etablissement,
    		count(CASE WHEN traitement_epmsi = '12' THEN p_rss.oid ELSE NULL END) AS cti_inter_etablissement_nv,
    		count(CASE WHEN traitement_epmsi IN ('13','32') OR (t_ghm_c.code LIKE ANY (ARRAY['14Z08Z', '09Z02%']::text[]) AND traitement_epmsi = '26') THEN p_rss.oid ELSE NULL END) AS cti_ghs_9999,
    		count(CASE WHEN t_ghm_c.code IN ('14Z08Z') THEN p_rss.oid ELSE NULL END) AS cti_ivg,
    		SUM(CASE WHEN t_ghm_c.cmd_code = '28' THEN 1 ELSE 0 END) AS cti_rsa_seances,
    		SUM(CASE WHEN t_ghm_c.code IN ('24Z04Z','28Z08Z','28Z09Z','28Z19Z','28Z20Z','28Z21Z','28Z22Z') THEN 1 WHEN t_ghm_c.cmd_code = '28' THEN nb_seances ELSE 0 END) AS cti_seances,
    		SUM(CASE WHEN t_ghm_c.cmd_code NOT IN ('28', '90') AND duree_sejour = 0 THEN 1 ELSE 0 END) AS cti_ds_0
    	FROM pmsi.p_rss
    	JOIN w_cp_imports ON w_cp_imports.import_id = p_rss.import_id
    	JOIN pmsi.t_ghm_c ON ghm_id = t_ghm_c.oid
    	WHERE en_cours = '0' AND
    		ghm_id <> 0
    	GROUP BY 1,2;
    	-- 2014
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[2] = 
    			CASE 
    			WHEN file_code = 'T1V5SYNTS' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Séjours transmis%' THEN cti_transmis_2
    			WHEN file_code = 'T1D2RTP' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Nb de RSA transmis%' THEN cti_transmis
    			WHEN file_code = 'T1V5SYNTS' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%séjours en CM 90%' THEN cti_cmd_90
    			WHEN file_code = 'T1D2RTP' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Nb de RSA en CMD 90%' THEN cti_cmd_90
    			WHEN file_code = 'T1V5SYNTS' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Séjours en prestation inter-établissement%' THEN cti_inter_etablissement_nv
    			WHEN file_code = 'T1D2RTP' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Nb de RSA prestation inter-établissement%' THEN cti_inter_etablissement_nv
    			WHEN file_code = 'T1V5SYNTS' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Séjours en GHS 9999%' THEN cti_ghs_9999
    			WHEN file_code = 'T1D2RTP' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Nb de RSA en GHS 9999%' THEN cti_ghs_9999
    			WHEN file_code = 'T1D2RTP' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Nb de RSA séances%' THEN cti_rsa_seances
    			WHEN file_code = 'T1D2RTP' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Nb de séances%' THEN cti_seances
    			WHEN file_code = 'T1D2RTP' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Nb de RSA DS=0%' THEN cti_ds_0
    			WHEN file_code = 'T1V5SYNTS' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Séjours non facturable à l''AM hors PO%' THEN cti_non_am_2
    			WHEN file_code = 'T1V5SYNTS' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Séjours valorisés%' THEN cti_valorises_2
    			WHEN file_code = 'T1V5SYNTS' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Séjours d''IVG%' THEN cti_ivg
    			ELSE NULL END
    	FROM w_cp_cti_sejt2a
    	WHERE w_cp_cti_sejt2a.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		(file_code = 'T1V5SYNTS' AND table_sequence = 1 OR
    		file_code = 'T1D2RTP' AND table_sequence = 1
    		) AND
    		line_type = 'D';
    	-- 2012
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[2] = 
    			CASE 
    			WHEN file_code = 'T1V5SYNTS' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Séjours transmis%' THEN cti_transmis_2
    			WHEN file_code = 'T1V5SYNTS' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%séjours en CM 90%' THEN cti_cmd_90
    			WHEN file_code = 'T1V5SYNTS' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Séjours en prestation inter-établissement%' THEN cti_inter_etablissement_nv
    			WHEN file_code = 'T1V5SYNTS' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Séjours en GHS 9999%' THEN cti_ghs_9999
    			WHEN file_code = 'T1V5SYNTS' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Séjours non facturable à l''AM hors PO%' THEN cti_non_am_2
    			WHEN file_code = 'T1V5SYNTS' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Séjours valorisés%' THEN cti_valorises_2
    			WHEN file_code = 'T1V5SYNTS' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Séjours d''IVG%' THEN cti_ivg
    			ELSE NULL END
    	FROM w_cp_cti_sejt2a
    	WHERE w_cp_cti_sejt2a.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T1V5SYNTS' AND table_sequence = 1 AND
    		line_type = 'D';
  
    	RAISE NOTICE '%' , 'Cumuls CTI. GHS Minoré';
  
    	IF NOT EXISTS (SELECT * FROM pg_class WHERE relnamespace = pg_my_temp_schema() AND relname = 'w_cp_ghsminor') THEN
    		DROP TABLE IF EXISTS w_cp_ghsminor;
    		CREATE TEMP TABLE w_cp_ghsminor (
    			import_id bigint, 
    			mat2a_import_id bigint,
    			ghm_id bigint,
    			ghs_id bigint,
    			date_sortie date,
    			taux_remboursement numeric,
    			nb_ghs_minore numeric
    		);
    	END IF;
  
    	-- A partir de mars 2015, pour les couples GHS-GHM 9606-28Z07Z et 9616-28Z17Z, si un UCD a été facturé en SUS, une minoration de 40¤ s'applique au GHS
        -- modif CLA 25-05-2016, on ne conserve que les séjours Valorisés (hors valo AME)
      -- La minoration forfaitaire est annulée à partir du M01 2018
    	TRUNCATE w_cp_ghsminor;
    	INSERT INTO w_cp_ghsminor
    	SELECT
    		w_cp_imports.import_id,
    		w_cp_imports.mat2a_import_id,
    		p_rss.ghm_id,
    		p_rss.ghs_id,
    		p_rss.date_sortie,
    		p_rsf_total.taux_remboursement,
    		count(distinct CASE WHEN p_rss.mois_sortie >= 201503 AND p_rss_ucd.rss_id is distinct from null THEN p_rss.no_rss ELSE null END) as nb_ghs_minore
    	FROM pmsi.p_rss
    	JOIN pmsi.p_rsf_total on p_rsf_total.rss_id = p_rss.oid
    	JOIN pmsi.t_ghs ON t_ghs.oid = p_rss.ghs_id
    	JOIN pmsi.t_ghm ON t_ghm.oid = p_rss.ghm_id
    	JOIN w_cp_imports ON w_cp_imports.import_id = p_rss.import_id
    	LEFT JOIN pmsi.p_rss_ucd ON p_rss_ucd.rss_id = p_rss.oid
    	WHERE 1=1
    		AND ((t_ghs.code = 9606 AND t_ghm.code = '28Z07Z') OR (t_ghs.code = 9616 AND t_ghm.code = '28Z17Z'))
    		AND p_rss.en_cours = '0'
          AND p_rss.mois_sortie < 201801
    		AND p_rss.ghm_id <> 0
    		-- AND p_rss.traitement_epmsi IN ('30','31','32')
    		AND p_rss.traitement_epmsi = '30'
    	GROUP BY 1,2,3,4,5,6;
  
    	RAISE NOTICE '%' , 'Cumuls CTI. Facturation T2A';
  
    	IF NOT EXISTS (SELECT * FROM pg_class WHERE relnamespace = pg_my_temp_schema() AND relname = 'w_cp_cti_fact2a') THEN
    		DROP TABLE IF EXISTS w_cp_cti_fact2a;
    		CREATE TEMP TABLE w_cp_cti_fact2a (
    			import_id bigint, 
    			mat2a_import_id bigint,
    			ghm_id bigint,
    			ghm_code text,
    			ghs_id bigint,
    			ghs_code text,
    			date_sortie date,
    			prestation_code text,
    			pec_amo_ame text,
    			cti_nb_ghs_minore numeric,
    			taux_remboursement numeric,
              traitement_epmsi text,
    			cti_nombre numeric,
    			cti_base_remboursement numeric,
    			cti_base_remboursement_sauf_transition numeric,
    			cti_montant_remboursable numeric
    		);
    	END IF;
  
    	TRUNCATE w_cp_cti_fact2a;
    	INSERT INTO w_cp_cti_fact2a
    	SELECT p_rss.import_id,
    		mat2a_import_id,
    		p_rss.ghm_id AS ghm_id,
    		''::text AS ghm_code,
    		p_rss.ghs_id,
    		'0'::text AS ghs_code,
    		p_rss.date_sortie,
    		t_prestations.code AS prestation_code,
    		CASE WHEN traitement_epmsi NOT IN ('31','32','33') THEN 'AMO' 
    			WHEN traitement_epmsi IN ('31') THEN 'AME'
    			WHEN traitement_epmsi IN ('32') THEN 'IVG'
    			WHEN traitement_epmsi IN ('33') THEN 'SU'
    			ELSE '' END AS pec_amo_ame,
    		0::numeric as cti_nb_ghs_minore,
    		p_rsf_detail.taux_remboursement,
          p_rss.traitement_epmsi,
    		SUM(CASE 
    			WHEN t_prestations.code = 'GHS' THEN 0
    			ELSE round(p_rsf_detail.nombre * p_rsf_detail.coefficient, 2) END) AS cti_nombre,
    		SUM(CASE 
    			WHEN t_prestations.code = 'GHS' AND (nb_exb > 0 OR nb_ghsmin > 0) THEN round(p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco, 2) 
    			ELSE p_rsf_detail.base_remboursement END) AS cti_base_remboursement,
    		SUM(CASE 
    			WHEN t_prestations.code = 'GHS' AND  (nb_exb > 0 OR nb_ghsmin > 0) THEN round(base.Cti_division(p_rsf_detail.base_remboursement_sauf_transition , p_rsf_detail.coefficient), 2) 
    			ELSE p_rsf_detail.base_remboursement_sauf_transition END) AS cti_base_remboursement_sauf_transition,
    		SUM(CASE 
    			WHEN t_prestations.code = 'GHS' AND  (nb_exb > 0 OR nb_ghsmin > 0) AND p_rsf_detail.taux_remboursement <> 100 THEN round(p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco * p_rsf_detail.taux_remboursement / 100, 2) 
    			WHEN t_prestations.code = 'GHS' AND  (nb_exb > 0 OR nb_ghsmin > 0) AND p_rsf_detail.taux_remboursement = 100 THEN round(p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco, 2) - p_rsf_detail.base_remboursement + p_rsf_detail.sejour_remboursable
    			ELSE p_rsf_detail.sejour_remboursable END) AS cti_montant_remboursable
    	FROM pmsi.p_rss
    	JOIN pmsi.p_rsf_detail ON p_rsf_detail.rss_id = p_rss.oid
    	JOIN pmsi.t_prestations ON prestation_id = t_prestations.oid
    	JOIN w_cp_imports ON w_cp_imports.import_id = p_rss.import_id
    	JOIN base.t_finess ON p_rss.finess = t_finess.code
    	WHERE en_cours = '0' AND
    		ghm_id <> 0 AND
    		(
  			traitement_epmsi IN ('30','31','32','33') OR 
  			t_prestations.code LIKE 'TDE%' OR 
  			t_prestations.code LIKE 'TSE%'
  			) AND
    		est_ligne_rss = '1'
    	GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12;
  
    	INSERT INTO w_cp_cti_fact2a
    	SELECT p_rss.import_id,
    		mat2a_import_id,
    		p_rss.ghm_id AS ghm_id,
    		''::text AS ghm_code,
    		p_rss.ghs_id,
    		'0'::text AS ghs_code,
    		p_rss.date_sortie,
    		CASE WHEN rehosp_meme_ghm = '0' THEN 'EXB' ELSE 'EXBR' END AS prestation_code,
    		CASE WHEN traitement_epmsi NOT IN ('31') THEN 'AMO' ELSE 'AME' END AS pec_amo_ame,
    		0::numeric as cti_nb_ghs_minore,
    		p_rsf_detail.taux_remboursement,
          p_rss.traitement_epmsi,
    		0 AS cti_nombre,
    		SUM(p_rsf_detail.base_remboursement- round(p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco, 2) ) AS cti_base_remboursement,
    		SUM(CASE 
    			WHEN t_prestations.code = 'GHS' AND  (nb_exb > 0 OR nb_ghsmin > 0) THEN p_rsf_detail.base_remboursement_sauf_transition-round(base.Cti_division(p_rsf_detail.base_remboursement_sauf_transition , p_rsf_detail.coefficient), 2) 
    			ELSE p_rsf_detail.base_remboursement_sauf_transition END) AS cti_base_remboursement_sauf_transition,
    		SUM(p_rsf_detail.sejour_remboursable - CASE 
    			WHEN t_prestations.code = 'GHS' AND nb_exb > 0 AND p_rsf_detail.taux_remboursement <> 100 THEN round(p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco * p_rsf_detail.taux_remboursement / 100, 2) 
    			WHEN t_prestations.code = 'GHS' AND nb_exb > 0 AND p_rsf_detail.taux_remboursement = 100 THEN round(p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco, 2) - p_rsf_detail.base_remboursement + p_rsf_detail.sejour_remboursable
    			ELSE 0 END) AS cti_montant_remboursable
    	FROM pmsi.p_rss
    	JOIN pmsi.p_rsf_detail ON p_rsf_detail.rss_id = p_rss.oid
    	JOIN pmsi.t_prestations ON prestation_id = t_prestations.oid
    	JOIN w_cp_imports ON w_cp_imports.import_id = p_rss.import_id
    	JOIN base.t_finess ON p_rss.finess = t_finess.code
    	WHERE en_cours = '0' AND
    		ghm_id <> 0 AND
    		traitement_epmsi IN ('30','31') AND
    		est_ligne_rss = '1' AND
    		t_prestations.code = 'GHS' AND
    		nb_exb > 0
    	GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12;
  
    	UPDATE w_cp_cti_fact2a
    	SET ghm_code = t_ghm.code
    	FROM pmsi.t_ghm 
    	WHERE ghm_id = t_ghm.oid;
    	UPDATE w_cp_cti_fact2a
    	SET ghs_code = CASE WHEN t_ghs.code::text = '9618' THEN 'D11'::text WHEN t_ghs.code::text = '9604' THEN 'D24'::text ELSE t_ghs.code::text END
    	FROM pmsi.t_ghs 
    	WHERE ghs_id = t_ghs.oid;
  
    	UPDATE w_cp_cti_fact2a
    	SET cti_nb_ghs_minore = w_cp_ghsminor.nb_ghs_minore
    	FROM w_cp_ghsminor
    	WHERE 1=1
    		AND w_cp_ghsminor.import_id = w_cp_cti_fact2a.import_id
    		AND w_cp_ghsminor.mat2a_import_id = w_cp_cti_fact2a.mat2a_import_id
    		AND w_cp_ghsminor.ghm_id = w_cp_cti_fact2a.ghm_id
    		AND w_cp_ghsminor.ghs_id = w_cp_cti_fact2a.ghs_id
    		AND w_cp_ghsminor.date_sortie = w_cp_cti_fact2a.date_sortie
    		AND w_cp_ghsminor.taux_remboursement = w_cp_cti_fact2a.taux_remboursement
    		AND w_cp_ghsminor.nb_ghs_minore > 0;
    	--drop table if exists temp.w_cp_cti_fact2a; CREATE Table temp.w_cp_cti_fact2a as select * from w_cp_cti_fact2a;
  
    	-- 2014 Tableau T1V1RAV (lignes et total)
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[4] = '0',
    		data_cti[5] = '0',
    		data_cti[6] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T1V1RAV' AND 
    		table_sequence = 1 AND 
    		line_type IN ('D', 'F') AND
    		(
    			data_mat2a[1] ILIKE '%Valorisation des GHS%' OR
    			data_mat2a[1] ILIKE '%Valorisation extrême bas%' OR
    			data_mat2a[1] ILIKE '%Valorisation séjours avec rehosp dans même GHM%' OR
    			data_mat2a[1] ILIKE '%Valorisation séjours avec minoration forfaitaire liste en sus%' OR
    			data_mat2a[1] ILIKE '%Valorisation journées extrême haut%' OR
    			data_mat2a[1] ILIKE '%Valorisation actes GHS 9615 en Hospit%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments radiothérapie pédiatrique%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments de dialyse%' OR
    			data_mat2a[1] ILIKE '%Valorisation des actes de caissons hyperbares en sus%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments antepartum%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments de néonat sans SI%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments de néonat avec SI%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments de réanimation néonat%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments de surveillance continue validés%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments de soins intensifs validés%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments de réanimation%' AND data_mat2a[1] NOT ILIKE '%néonat%'OR
    			data_mat2a[1] ILIKE '%Valorisation actes RDTH en Hospit%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments de réa pédiatrique%' OR
    			data_mat2a[1] ILIKE '%Valorisation prélévements d''organe%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments pour pose de défibrillateur%' OR
    			data_mat2a[1] ILIKE '%Total valorisation%'
    		);
  
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[4] = cti_base_remboursement_sauf_transition,
    		data_cti[5] = cti_base_remboursement,
    		data_cti[6] = cti_montant_remboursable
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(cti_base_remboursement) AS cti_base_remboursement,
    			SUM(cti_base_remboursement_sauf_transition) AS cti_base_remboursement_sauf_transition,
    			SUM(cti_montant_remboursable) AS cti_montant_remboursable
    		FROM w_cp_cti_fact2a
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_fact2a.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE pec_amo_ame ='AMO' AND 
    			file_code = 'T1V1RAV' AND table_sequence = 1 AND 
    			(w_cp_cti_fact2a.prestation_code IN ('GHS','EXB','EXBR','EXH','ANT','APHE','NN1','NN2','NN3','RAP','SRC','LIM_SRC','STF','REA','DIP','D09','D17','D18','D19','OHB','REP','SDC') OR
    			w_cp_cti_fact2a.prestation_code LIKE 'IRRE%' OR
    			w_cp_cti_fact2a.prestation_code LIKE 'PO%') AND
    			(
    			w_cp_cti_fact2a.prestation_code = 'GHS' AND data_mat2a[1] ILIKE '%Valorisation des GHS%' OR
    			w_cp_cti_fact2a.prestation_code = 'EXB' AND data_mat2a[1] ILIKE '%Valorisation extrême bas%' OR
    			w_cp_cti_fact2a.prestation_code = 'EXBR' AND data_mat2a[1] ILIKE '%Valorisation séjours avec rehosp dans même GHM%' OR
    			w_cp_cti_fact2a.prestation_code = 'EXH' AND data_mat2a[1] ILIKE '%Valorisation journées extrême haut%' OR
    			w_cp_cti_fact2a.prestation_code = 'DIP' AND data_mat2a[1] ILIKE '%Valorisation suppléments de dialyse%' OR
    			w_cp_cti_fact2a.prestation_code = 'D09' AND data_mat2a[1] ILIKE '%Valorisation suppléments de dialyse%' OR
    			w_cp_cti_fact2a.prestation_code = 'D17' AND data_mat2a[1] ILIKE '%Valorisation suppléments de dialyse%' OR
    			w_cp_cti_fact2a.prestation_code = 'D18' AND data_mat2a[1] ILIKE '%Valorisation suppléments de dialyse%' OR
    			w_cp_cti_fact2a.prestation_code = 'D19' AND data_mat2a[1] ILIKE '%Valorisation suppléments de dialyse%' OR
    			w_cp_cti_fact2a.prestation_code = 'OHB' AND data_mat2a[1] ILIKE '%Valorisation des actes de caissons hyperbares en sus%' OR
    			w_cp_cti_fact2a.prestation_code = 'ANT' AND data_mat2a[1] ILIKE '%Valorisation suppléments antepartum%' OR
    			w_cp_cti_fact2a.prestation_code = 'APHE' AND data_mat2a[1] ILIKE '%Valorisation actes GHS 9615 en Hospit%' OR
    			w_cp_cti_fact2a.prestation_code = 'NN1' AND data_mat2a[1] ILIKE '%Valorisation suppléments de néonat sans SI%' OR
    			w_cp_cti_fact2a.prestation_code = 'NN2' AND data_mat2a[1] ILIKE '%Valorisation suppléments de néonat avec SI%' OR
    			w_cp_cti_fact2a.prestation_code = 'NN3' AND data_mat2a[1] ILIKE '%Valorisation suppléments de réanimation néonat%' OR
    			w_cp_cti_fact2a.prestation_code = 'RAP' AND data_mat2a[1] ILIKE '%Valorisation suppléments radiothérapie pédiatrique%' OR
    			w_cp_cti_fact2a.prestation_code IN ('SRC', 'LIM_SRC') AND data_mat2a[1] ILIKE '%Valorisation suppléments de surveillance continue validés%' OR
    			w_cp_cti_fact2a.prestation_code = 'STF' AND data_mat2a[1] ILIKE '%Valorisation suppléments de soins intensifs validés%' OR
    			w_cp_cti_fact2a.prestation_code = 'REP' AND data_mat2a[1] ILIKE '%Valorisation suppléments de réa pédiatrique%' OR
    			w_cp_cti_fact2a.prestation_code = 'REA' AND data_mat2a[1] ILIKE '%Valorisation suppléments de réanimation%' AND data_mat2a[1] NOT ILIKE '%néonat%'OR
    			w_cp_cti_fact2a.prestation_code LIKE 'IRRE%' AND data_mat2a[1] ILIKE '%Valorisation actes RDTH en Hospit%' OR
    			w_cp_cti_fact2a.prestation_code LIKE 'PO%' AND data_mat2a[1] ILIKE '%Valorisation prélévements d''organe%' OR
    			w_cp_cti_fact2a.prestation_code = 'SDC' AND data_mat2a[1] ILIKE '%Valorisation suppléments pour pose de défibrillateur%' OR
    			data_mat2a[1] ILIKE '%Total valorisation%'
    			)
    		GROUP BY 1) subview
    	WHERE file_code = 'T1V1RAV' AND p_mat2a_data.oid = subview.oid;
    	-- Ajustement EXB (base remboursement entre EXB et GHS si somme identique)
    	UPDATE pmsi.p_mat2a_data
    	SET data_cti[4] =
    		CASE 
    			WHEN data_mat2a[1] ILIKE '%Valorisation des GHS%' THEN base.cti_to_number(data_mat2a[4]) - ecart_base_remboursement_sauf_transition
    			WHEN data_mat2a[1] ILIKE '%Valorisation extrême bas%' THEN base.cti_to_number(data_mat2a[4])
    			WHEN data_mat2a[1] ILIKE '%Valorisation séjours avec rehosp dans même GHM%' THEN base.cti_to_number(data_mat2a[4])
    			ELSE base.cti_to_number(data_cti[4]) END,
    		data_cti[5] =
    		CASE 
    			WHEN data_mat2a[1] ILIKE '%Valorisation des GHS%' THEN base.cti_to_number(data_mat2a[5]) - ecart_base_remboursement
    			WHEN data_mat2a[1] ILIKE '%Valorisation extrême bas%' THEN base.cti_to_number(data_mat2a[5])
    			WHEN data_mat2a[1] ILIKE '%Valorisation séjours avec rehosp dans même GHM%' THEN base.cti_to_number(data_mat2a[5])
    			ELSE base.cti_to_number(data_cti[5]) END
    	FROM (
    		SELECT
    			p_mat2a_data.mat2a_import_id,
    			SUM(base.cti_to_number(data_mat2a[4])) AS mat2a_base_remboursement_sauf_transition,
    			SUM(base.cti_to_number(data_cti[4])) AS cti_base_remboursement_sauf_transition,
    			SUM(base.cti_to_number(data_mat2a[4])) - SUM(base.cti_to_number(data_cti[4])) AS ecart_base_remboursement_sauf_transition,
    			SUM(base.cti_to_number(data_mat2a[5])) AS mat2a_base_remboursement,
    			SUM(base.cti_to_number(data_cti[5])) AS cti_base_remboursement,
    			SUM(base.cti_to_number(data_mat2a[5])) - SUM(base.cti_to_number(data_cti[5])) AS ecart_base_remboursement
    		FROM pmsi.p_mat2a_data
    		JOIN w_cp_imports ON w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE file_code = 'T1V1RAV' AND
    			(data_mat2a[1] ILIKE '%Valorisation des GHS%' OR
    			data_mat2a[1] ILIKE '%Valorisation extrême bas%' OR
    			data_mat2a[1] ILIKE '%Valorisation séjours avec rehosp dans même GHM%')
    		GROUP BY 1
    		HAVING ABS(SUM(base.cti_to_number(data_mat2a[4])) - SUM(base.cti_to_number(data_cti[4]))) < 0.05 AND
    			ABS(SUM(base.cti_to_number(data_mat2a[5])) - SUM(base.cti_to_number(data_cti[5]))) < 0.05
    		) subview
    	WHERE subview.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T1V1RAV' AND
    		(data_mat2a[1] ILIKE '%Valorisation des GHS%' OR
    		data_mat2a[1] ILIKE '%Valorisation extrême bas%' OR
    		data_mat2a[1] ILIKE '%Valorisation séjours avec rehosp dans même GHM%');
    	-- Ajustement EXB (ventilation remboursement entre EXB et GHS si somme identique)
    	UPDATE pmsi.p_mat2a_data
    	SET data_cti[6] =
    		CASE 
    			WHEN data_mat2a[1] ILIKE '%Valorisation des GHS%' THEN base.cti_to_number(data_mat2a[6]) - ecart
    			WHEN data_mat2a[1] ILIKE '%Valorisation extrême bas%' THEN base.cti_to_number(data_mat2a[6])
    			WHEN data_mat2a[1] ILIKE '%Valorisation séjours avec rehosp dans même GHM%' THEN base.cti_to_number(data_mat2a[6])
    			ELSE base.cti_to_number(data_cti[6]) END
    	FROM (
    		SELECT
    			p_mat2a_data.mat2a_import_id,
    			SUM(base.cti_to_number(data_mat2a[6])) AS mat2a_remb,
    			SUM(base.cti_to_number(data_cti[6])) AS cti_remb,
    			SUM(base.cti_to_number(data_mat2a[6])) - SUM(base.cti_to_number(data_cti[6])) AS ecart
    		FROM pmsi.p_mat2a_data
    		JOIN w_cp_imports ON w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE file_code = 'T1V1RAV' AND
    			(data_mat2a[1] ILIKE '%Valorisation des GHS%' OR
    			data_mat2a[1] ILIKE '%Valorisation extrême bas%' OR
    			data_mat2a[1] ILIKE '%Valorisation séjours avec rehosp dans même GHM%')
    		GROUP BY 1
    		HAVING ABS(SUM(base.cti_to_number(data_mat2a[4])) - SUM(base.cti_to_number(data_cti[4]))) < 0.05 AND
    			ABS(SUM(base.cti_to_number(data_mat2a[5])) - SUM(base.cti_to_number(data_cti[5]))) < 0.05 AND
    			ABS(SUM(base.cti_to_number(data_mat2a[6])) - SUM(base.cti_to_number(data_cti[6]))) < 1
    		) subview
    	WHERE subview.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T1V1RAV' AND
    		(data_mat2a[1] ILIKE '%Valorisation des GHS%' OR
    		data_mat2a[1] ILIKE '%Valorisation extrême bas%' OR
    		data_mat2a[1] ILIKE '%Valorisation séjours avec rehosp dans même GHM%');
    	-- 2015 Minoration forfaire GHS liste en SUS
  
        WITH w_sub as (
            SELECT
                p_mat2a_data.oid,
                w_cp_imports.mat2a_annee,
                extract('month' from w_cp_cti_fact2a.date_sortie) as mois_sortie,
                SUM(cti_nb_ghs_minore) * -40 AS cti_base_remboursement_sauf_transition,
                SUM(cti_nb_ghs_minore) * -40 * CASE WHEN w_cp_imports.mat2a_annee * 100 + extract('month' from w_cp_cti_fact2a.date_sortie) >= 201603 THEN 0.9950 ELSE 0.9965 END AS cti_base_remboursement,
                SUM(cti_nb_ghs_minore * base.cti_division(taux_remboursement, 100)) * -40 * CASE WHEN w_cp_imports.mat2a_annee * 100 + extract('month' from w_cp_cti_fact2a.date_sortie) >= 201603 THEN 0.9950 ELSE 0.9965 END AS cti_montant_remboursable
            FROM w_cp_cti_fact2a
            JOIN pmsi.p_mat2a_data ON
                w_cp_cti_fact2a.mat2a_import_id = p_mat2a_data.mat2a_import_id
                JOIN w_cp_imports ON w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id
            WHERE 1=1 AND
                w_cp_imports.mat2a_annee >= 2015 AND
                w_cp_imports.mat2a_annee < 2018 AND
                pec_amo_ame ='AMO' AND 
                file_code = 'T1V1RAV' AND table_sequence = 1 AND 
                w_cp_cti_fact2a.prestation_code = 'GHS' AND
                w_cp_cti_fact2a.cti_nb_ghs_minore >= 1 AND
                (
                data_mat2a[1] ILIKE '%Valorisation séjours avec minoration forfaitaire liste en sus%' OR
                data_mat2a[1] ILIKE '%Valorisation des GHS%' OR
                data_mat2a[1] ILIKE '%Total valorisation%'
                )
            GROUP BY p_mat2a_data.oid, w_cp_imports.mat2a_annee, extract('month' from w_cp_cti_fact2a.date_sortie)
        )
        UPDATE pmsi.p_mat2a_data SET
                data_cti[4] =
                    CASE 
                        WHEN data_mat2a[1] ILIKE '%Valorisation séjours avec minoration forfaitaire liste en sus%' THEN subview.cti_base_remboursement_sauf_transition
                        --WHEN data_mat2a[1] ILIKE '%Valorisation des GHS%' THEN base.cti_to_number(data_cti[4]) + subview.cti_base_remboursement_sauf_transition
                        WHEN mat2a_annee < 2018 AND data_mat2a[1] ILIKE '%Total valorisation%' THEN base.cti_to_number(data_cti[4]) + cti_base_remboursement_sauf_transition
                        ELSE base.cti_to_number(data_cti[4])
                        --WHEN data_mat2a[1] ILIKE '%Total valorisation%' THEN base.cti_to_number(data_cti[4]) + cti_base_remboursement_sauf_transition
                    END,
                data_cti[5] =
                    CASE 
                        WHEN data_mat2a[1] ILIKE '%Valorisation séjours avec minoration forfaitaire liste en sus%' THEN subview.cti_base_remboursement
                        --WHEN data_mat2a[1] ILIKE '%Valorisation des GHS%' THEN base.cti_to_number(data_cti[5]) + subview.cti_base_remboursement
                        WHEN mat2a_annee < 2018 AND data_mat2a[1] ILIKE '%Total valorisation%' THEN base.cti_to_number(data_cti[5]) + subview.cti_base_remboursement
                        ELSE base.cti_to_number(data_cti[5])
                        --WHEN data_mat2a[1] ILIKE '%Total valorisation%' THEN base.cti_to_number(data_cti[5]) + cti_base_remboursement
                        --ELSE cti_base_remboursement
                    END,
                data_cti[6] =
                    CASE 
                        WHEN data_mat2a[1] ILIKE '%Valorisation séjours avec minoration forfaitaire liste en sus%' THEN subview.cti_montant_remboursable
                        WHEN data_mat2a[1] ILIKE '%Valorisation des GHS%' THEN base.cti_to_number(data_cti[6]) + subview.cti_montant_remboursable
                        WHEN mat2a_annee < 2018 AND data_mat2a[1] ILIKE '%Total valorisation%' THEN base.cti_to_number(data_cti[6]) + subview.cti_montant_remboursable
                        ELSE base.cti_to_number(data_cti[6])
                        --WHEN data_mat2a[1] ILIKE '%Total valorisation%' THEN base.cti_to_number(data_cti[6]) + cti_montant_remboursable
                        --ELSE cti_montant_remboursable
                    END
            FROM (
                SELECT 
                    w_sub.oid,
                    w_sub.mat2a_annee,
                    SUM(w_sub.cti_base_remboursement_sauf_transition) as cti_base_remboursement_sauf_transition,
                    SUM(w_sub.cti_base_remboursement) as cti_base_remboursement,
                    SUM(w_sub.cti_montant_remboursable) as cti_montant_remboursable
                FROM w_sub
                GROUP BY
                    w_sub.oid,
                    w_sub.mat2a_annee
            ) subview
            WHERE p_mat2a_data.file_code = 'T1V1RAV' AND p_mat2a_data.oid = subview.oid;
  
  
  
  
  	-- Nouvelle colonne GHS UCD en 2021
  	-- 2021 Tableau T1V1CMV (lignes et total)
  	UPDATE pmsi.p_mat2a_data SET
    		data_cti = null
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		w_cp_imports.mat2a_annee >= 2021 AND
    		file_code = 'T1V1CMV' AND 
    		table_sequence = 1 AND 
    		line_type IN ('D', 'F')
    	;
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[7] = '0',
    		data_cti[8] = '0',
    		data_cti[10] = '0',
    		data_cti[11] = '0',
    		data_cti[12] = '0',
    		data_cti[13] = '0',
    		data_cti[14] = '0',
    		data_cti[15] = '0',
    		data_cti[16] = '0',
    		data_cti[17] = '0',
    		data_cti[18] = '0',
    		data_cti[19] = '0',
    		data_cti[20] = '0',
    		data_cti[21] = '0',
    		data_cti[22] = '0',
    		data_cti[23] = '0',
    		data_cti[24] = '0',
    		data_cti[25] = '0',
    		data_cti[26] = '0',
    		data_cti[27] = '0',
    		data_cti[28] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		w_cp_imports.mat2a_annee >= 2021 AND
    		file_code = 'T1V1CMV' AND 
    		table_sequence = 1 AND 
    		line_type IN ('D', 'F');
  
  
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[7] = cti_base_remboursement_ghs - CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_ghsminor ELSE 0 END,
    		data_cti[8] = cti_base_remboursement_exb,
    		data_cti[10] = cti_base_remboursement_exbr,
    		data_cti[11] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_ghsminor ELSE cti_base_remboursement_exh END,
    		data_cti[12] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_exh ELSE null END,
    		data_cti[13] = CASE WHEN subview.mat2a_annee < 2018 THEN null ELSE cti_base_remboursement_irre END,
    		data_cti[14] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_irre ELSE cti_base_remboursement_rap END,
    		data_cti[15] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_rap ELSE cti_base_remboursement_ant END,
    		data_cti[16] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_ant ELSE cti_base_remboursement_rea END,
    		data_cti[17] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_rea ELSE cti_base_remboursement_rep END,
    		data_cti[18] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_rep ELSE cti_base_remboursement_stf END,
    		data_cti[19] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_stf ELSE cti_base_remboursement_src END,
    		data_cti[20] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_src ELSE cti_base_remboursement_dia END,
    		data_cti[21] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_dia ELSE cti_base_remboursement_nn1 END,
    		data_cti[22] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_nn1 ELSE cti_base_remboursement_nn2 END,
    		data_cti[23] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_nn2 ELSE cti_base_remboursement_nn3 END,
    		data_cti[24] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_nn3 ELSE cti_base_remboursement_po END,
    		data_cti[25] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_po ELSE cti_base_remboursement_ohb END,
    		data_cti[26] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_ohb ELSE cti_base_remboursement_aphe END,
    		data_cti[27] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_aphe ELSE cti_base_remboursement_sdc END,
    		data_cti[28] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_sdc ELSE null END
    	FROM (
    		SELECT p_mat2a_data.oid,
    			MAX(w_cp_imports.mat2a_annee) AS mat2a_annee,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'GHS' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_ghs,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'EXB' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_exb,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'EXBR' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_exbr,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'GHS' THEN cti_nb_ghs_minore ELSE NULL END) * -40 AS cti_base_remboursement_ghsminor,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'EXH' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_exh,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'ANT' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_ant,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code IN ('DIP', 'D09', 'D17', 'D18', 'D19') THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_dia,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'SRC' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_src,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'STF' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_stf,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'REA' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_rea,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'REP' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_rep,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'RAP' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_rap,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'NN1' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_nn1,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'NN2' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_nn2,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'NN3' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_nn3,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'OHB' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_ohb,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'SDC' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_sdc,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code LIKE 'IRRE%' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_irre,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code LIKE 'APHE%' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_aphe,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code IN ('PO1', 'PO2', 'PO3', 'PO4', 'PO5', 'PO6', 'PO7', 'PO8', 'PO9', 'POA') THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_po
    		FROM w_cp_cti_fact2a
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_fact2a.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		JOIN w_cp_imports ON w_cp_imports.mat2a_import_id = w_cp_cti_fact2a.mat2a_import_id
    		WHERE pec_amo_ame ='AMO' AND 
    			file_code = 'T1V1CMV' AND table_sequence = 1 AND 
    			(w_cp_cti_fact2a.prestation_code IN ('GHS','EXB','EXBR','EXH','ANT','APHE','NN1','NN2','NN3','RAP','SRC','STF','REA','DIP','D09','D17','D18','D19','OHB','SDC') OR
    			w_cp_cti_fact2a.prestation_code LIKE 'IRRE%' OR
    			w_cp_cti_fact2a.prestation_code LIKE 'PO%') AND
    			(
    			line_type = 'D' AND data_mat2a[1] ILIKE ghs_code AND data_mat2a[3] ILIKE ghm_code || '%' OR
    			line_type = 'F'
    			) AND
    			w_cp_imports.mat2a_annee >= 2021
    		GROUP BY 1) subview
    	WHERE file_code = 'T1V1CMV' AND p_mat2a_data.oid = subview.oid;
  
    	-- 2015 Tableau T1V1CMV (lignes et total)
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[6] = '0',
    		data_cti[7] = '0',
    		data_cti[9] = '0',
    		data_cti[10] = '0',
    		data_cti[11] = '0',
    		data_cti[13] = '0',
    		data_cti[14] = '0',
    		data_cti[15] = '0',
    		data_cti[16] = '0',
    		data_cti[17] = '0',
    		data_cti[18] = '0',
    		data_cti[19] = '0',
    		data_cti[20] = '0',
    		data_cti[21] = '0',
    		data_cti[22] = '0',
    		data_cti[23] = '0',
    		data_cti[24] = '0',
    		data_cti[25] = '0',
    		data_cti[26] = '0',
    		data_cti[27] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		w_cp_imports.mat2a_annee BETWEEN 2015 AND 2020 AND
    		file_code = 'T1V1CMV' AND 
    		table_sequence = 1 AND 
    		line_type IN ('D', 'F');
    	/* 
    		le montant GHS du tableau OVALIDE (data_mat2a[6]) ne comprend pas la minoration forfaitaire, 
    		il faut donc la rajouter au montant GHS CTI (data_cti[6]) qui en tient déjà compte 
  
          Attention, à partir de M01 2018, la minoration forfaitaire n'apparait plus dans le tableau
    	*/
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[6] = cti_base_remboursement_ghs - CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_ghsminor ELSE 0 END,
    		data_cti[7] = cti_base_remboursement_exb,
    		data_cti[9] = cti_base_remboursement_exbr,
    		data_cti[10] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_ghsminor ELSE cti_base_remboursement_exh END,
    		data_cti[11] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_exh ELSE null END,
    		data_cti[12] = CASE WHEN subview.mat2a_annee < 2018 THEN null ELSE cti_base_remboursement_irre END,
    		data_cti[13] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_irre ELSE cti_base_remboursement_rap END,
    		data_cti[14] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_rap ELSE cti_base_remboursement_ant END,
    		data_cti[15] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_ant ELSE cti_base_remboursement_rea END,
    		data_cti[16] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_rea ELSE cti_base_remboursement_rep END,
    		data_cti[17] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_rep ELSE cti_base_remboursement_stf END,
    		data_cti[18] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_stf ELSE cti_base_remboursement_src END,
    		data_cti[19] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_src ELSE cti_base_remboursement_dia END,
    		data_cti[20] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_dia ELSE cti_base_remboursement_nn1 END,
    		data_cti[21] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_nn1 ELSE cti_base_remboursement_nn2 END,
    		data_cti[22] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_nn2 ELSE cti_base_remboursement_nn3 END,
    		data_cti[23] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_nn3 ELSE cti_base_remboursement_po END,
    		data_cti[24] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_po ELSE cti_base_remboursement_ohb END,
    		data_cti[25] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_ohb ELSE cti_base_remboursement_aphe END,
    		data_cti[26] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_aphe ELSE cti_base_remboursement_sdc END,
    		data_cti[27] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_base_remboursement_sdc ELSE null END
    	FROM (
    		SELECT p_mat2a_data.oid,
    			MAX(w_cp_imports.mat2a_annee) AS mat2a_annee,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'GHS' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_ghs,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'EXB' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_exb,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'EXBR' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_exbr,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'GHS' THEN cti_nb_ghs_minore ELSE NULL END) * -40 AS cti_base_remboursement_ghsminor,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'EXH' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_exh,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'ANT' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_ant,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code IN ('DIP', 'D09', 'D17', 'D18', 'D19') THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_dia,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'SRC' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_src,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'STF' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_stf,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'REA' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_rea,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'REP' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_rep,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'RAP' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_rap,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'NN1' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_nn1,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'NN2' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_nn2,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'NN3' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_nn3,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'OHB' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_ohb,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'SDC' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_sdc,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code LIKE 'IRRE%' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_irre,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code LIKE 'APHE%' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_aphe,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code IN ('PO1', 'PO2', 'PO3', 'PO4', 'PO5', 'PO6', 'PO7', 'PO8', 'PO9', 'POA') THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_po
    		FROM w_cp_cti_fact2a
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_fact2a.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		JOIN w_cp_imports ON w_cp_imports.mat2a_import_id = w_cp_cti_fact2a.mat2a_import_id
    		WHERE pec_amo_ame ='AMO' AND 
    			file_code = 'T1V1CMV' AND table_sequence = 1 AND 
    			(w_cp_cti_fact2a.prestation_code IN ('GHS','EXB','EXBR','EXH','ANT','APHE','NN1','NN2','NN3','RAP','SRC','STF','REA','DIP','D09','D17','D18','D19','OHB','SDC') OR
    			w_cp_cti_fact2a.prestation_code LIKE 'IRRE%' OR
    			w_cp_cti_fact2a.prestation_code LIKE 'PO%') AND
    			(
    			line_type = 'D' AND data_mat2a[1] ILIKE ghs_code AND data_mat2a[2] ILIKE ghm_code || '%' OR
    			line_type = 'F'
    			) AND
    			w_cp_imports.mat2a_annee BETWEEN 2015 AND 2020
    		GROUP BY 1) subview
    	WHERE file_code = 'T1V1CMV' AND p_mat2a_data.oid = subview.oid;
    	-- 2014 Tableau T1V1CMV (lignes et total)
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[6] = '0',
    		data_cti[7] = '0',
    		data_cti[9] = '0',
    		data_cti[10] = '0',
    		data_cti[12] = '0',
    		data_cti[13] = '0',
    		data_cti[14] = '0',
    		data_cti[15] = '0',
    		data_cti[16] = '0',
    		data_cti[17] = '0',
    		data_cti[18] = '0',
    		data_cti[19] = '0',
    		data_cti[20] = '0',
    		data_cti[21] = '0',
    		data_cti[22] = '0',
    		data_cti[23] = '0',
    		data_cti[24] = '0',
    		data_cti[25] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		w_cp_imports.mat2a_annee < 2015 AND
    		file_code = 'T1V1CMV' AND 
    		table_sequence = 1 AND 
    		line_type IN ('D', 'F');
  
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[6] = cti_base_remboursement_ghs,
    		data_cti[7] = cti_base_remboursement_exb,
    		data_cti[9] = cti_base_remboursement_exbr,
    		data_cti[10] = cti_base_remboursement_exh,
    		data_cti[12] = cti_base_remboursement_irre,
    		data_cti[13] = cti_base_remboursement_rap,
    		data_cti[14] = cti_base_remboursement_ant,
    		data_cti[15] = cti_base_remboursement_rea,
    		data_cti[16] = cti_base_remboursement_rep,
    		data_cti[17] = cti_base_remboursement_stf,
    		data_cti[18] = cti_base_remboursement_src,
    		data_cti[19] = cti_base_remboursement_dia,
    		data_cti[20] = cti_base_remboursement_nn1,
    		data_cti[21] = cti_base_remboursement_nn2,
    		data_cti[22] = cti_base_remboursement_nn3,
    		data_cti[23] = cti_base_remboursement_po,
    		data_cti[24] = cti_base_remboursement_ohb,
    		data_cti[25] = cti_base_remboursement_aphe
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'GHS' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_ghs,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'EXB' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_exb,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'EXBR' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_exbr,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'EXH' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_exh,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'ANT' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_ant,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code IN ('DIP', 'D09', 'D17', 'D18', 'D19') THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_dia,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'SRC' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_src,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'STF' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_stf,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'REA' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_rea,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'REP' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_rep,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'RAP' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_rap,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'NN1' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_nn1,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'NN2' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_nn2,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'NN3' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_nn3,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'OHB' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_ohb,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code LIKE 'IRRE%' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_irre,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code LIKE 'APHE%' THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_aphe,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code IN ('PO1', 'PO2', 'PO3', 'PO4', 'PO5', 'PO6', 'PO7', 'PO8', 'PO9', 'POA') THEN cti_base_remboursement_sauf_transition ELSE 0 END) AS cti_base_remboursement_po
    		FROM w_cp_cti_fact2a
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_fact2a.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		JOIN w_cp_imports ON w_cp_imports.mat2a_import_id = w_cp_cti_fact2a.mat2a_import_id
    		WHERE pec_amo_ame ='AMO' AND 
    			file_code = 'T1V1CMV' AND table_sequence = 1 AND 
    			(w_cp_cti_fact2a.prestation_code IN ('GHS','EXB','EXBR','EXH','ANT','APHE','NN1','NN2','NN3','RAP','SRC','STF','REA','DIP','D09','D17','D18','D19','OHB','SDC') OR
    			w_cp_cti_fact2a.prestation_code LIKE 'IRRE%' OR
    			w_cp_cti_fact2a.prestation_code LIKE 'PO%') AND
    			(
    			line_type = 'D' AND data_mat2a[1] ILIKE ghs_code AND data_mat2a[2] ILIKE ghm_code || '%' OR
    			line_type = 'F'
    			) AND
    			w_cp_imports.mat2a_annee < 2015
    		GROUP BY 1) subview
    	WHERE file_code = 'T1V1CMV' AND p_mat2a_data.oid = subview.oid;
  
   -- Reventilation EXB
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[6] = base.cti_to_number(data_mat2a[6])
    			+ (base.cti_to_number(data_mat2a[6]) 
    			+ base.cti_to_number(data_mat2a[7]) 
    			+ base.cti_to_number(data_mat2a[9]) 
    			- base.cti_to_number(data_cti[6])
    			- base.cti_to_number(data_cti[7])
    			- base.cti_to_number(data_cti[9])),
    		data_cti[7] = data_mat2a[7],
    		data_cti[9] = data_mat2a[9]
    	FROM
    		w_cp_imports
    	WHERE 
    		w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		w_cp_imports.mat2a_annee < 2015 AND
    		file_code = 'T1V1CMV' AND 
    		table_sequence = 1 AND
    		line_type IN ('D', 'F') AND
    		ABS(base.cti_to_number(data_mat2a[6]) - base.cti_to_number(data_cti[6])) <= 0.05 AND
    		ABS(base.cti_to_number(data_mat2a[6]) + base.cti_to_number(data_mat2a[7]) + base.cti_to_number(data_mat2a[9]) 
    			- base.cti_to_number(data_cti[6])
    			- base.cti_to_number(data_cti[7])
    			- base.cti_to_number(data_cti[9])) <= 0.05 AND
    		(base.cti_to_number(data_cti[7]) <> base.cti_to_number(data_mat2a[7]) OR base.cti_to_number(data_cti[9]) <> base.cti_to_number(data_mat2a[9]));
  
  	-- Cas de séjours avec EXB ET GHS Minoré. Si total EXB+GHS minoré OK
  	UPDATE pmsi.p_mat2a_data SET
  		data_cti[8] = data_mat2a[8],
  		data_cti[10] = base.cti_to_number(data_cti[8]) + base.cti_to_number(data_cti[10]) - base.cti_to_number(data_mat2a[8])
  	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
  		w_cp_imports.mat2a_annee >= 2023 AND
    		file_code = 'T1V1CMV' AND 
    		table_sequence = 1 AND
    		line_type IN ('D', 'F') AND
  		base.cti_to_number(data_mat2a[8]) <> 0 AND
  		base.cti_to_number(data_mat2a[10]) <> 0 AND
  		(
  		base.cti_to_number(data_mat2a[8]) <> base.cti_to_number(data_cti[8]) OR
  		base.cti_to_number(data_mat2a[10]) <> base.cti_to_number(data_cti[10])
  		) AND
  		ABS(
  		base.cti_to_number(data_mat2a[8]) + base.cti_to_number(data_mat2a[10]) -
  		base.cti_to_number(data_cti[8]) - base.cti_to_number(data_cti[10])
  		) < 1
  	;
  
   -- Tableau T1V1CP (OQN)
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[4] = 0
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T1V1CP' AND 
    		table_sequence = 1 AND 
    		(
    		line_type = 'D' OR
    		line_type = 'F' AND data_mat2a[1] ILIKE 'lib'
    		);
  
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[4] = cti_base_remboursement
  
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(cti_base_remboursement_sauf_transition) AS cti_base_remboursement
    		FROM w_cp_cti_fact2a
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_fact2a.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE pec_amo_ame ='AMO' AND 
    			file_code = 'T1V1CP' AND table_sequence = 1 AND 
    			(
    			line_type = 'D' AND data_mat2a[1] ILIKE prestation_code OR
    			line_type = 'D' AND data_mat2a[1] LIKE 'GHS' AND prestation_code = 'EXB' OR
    			line_type = 'F' AND data_mat2a[1] ILIKE 'lib'
    			)
    		GROUP BY 1) subview
    	WHERE file_code = 'T1V1CP' AND p_mat2a_data.oid = subview.oid;
    	-- Tableau T1V1CMG (OQN : lignes et total)
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[4] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T1V1CMG' AND 
    		table_sequence = 2 AND 
    		line_type IN ('D', 'F');
  
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[4] = cti_base_remboursement
  
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(cti_base_remboursement_sauf_transition) AS cti_base_remboursement
    		FROM w_cp_cti_fact2a
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_fact2a.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE pec_amo_ame ='AMO' AND 
    			file_code = 'T1V1CMG' AND table_sequence = 2 AND 
    			(
    			line_type = 'D' AND data_mat2a[1] ILIKE substr(ghm_code,1,5) || '%' OR
    			line_type = 'F'
    			)
    		GROUP BY 1) subview
    	WHERE file_code = 'T1V1CMG' AND p_mat2a_data.oid = subview.oid;
    	-- 2014 Tableau T1V4RAME (AME) (4 = synthèse)
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[2] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T1V4RAME' AND 
    		table_sequence = 4 AND 
    		line_type IN ('D', 'F') AND
    		(
    			data_mat2a[1] ILIKE '%Valorisation des GHS%' OR
    			data_mat2a[1] ILIKE '%Valorisation extrême bas%' OR
    			data_mat2a[1] ILIKE '%Valorisation séjours avec rehosp dans même GHM%' OR
    			data_mat2a[1] ILIKE '%Valorisation séjours avec minoration forfaitaire%' OR
    			data_mat2a[1] ILIKE '%Valorisation journées extrême haut%' OR
    			data_mat2a[1] ILIKE '%Valorisation actes GHS 9615 en Hospit%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments radiothérapie pédiatrique%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments antepartum%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments de réa pédiatrique%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments de dialyse%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments de néonat sans SI%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments de néonat avec SI%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments de réanimation néonat%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments de surveillance continue%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments de soins intensifs%' OR
    			data_mat2a[1] ILIKE '%Valorisation suppléments de réanimation%' AND data_mat2a[1] NOT ILIKE '%néonat%'OR
    			data_mat2a[1] ILIKE '%Valorisation actes RDTH en Hospit%' OR
    			data_mat2a[1] ILIKE '%Valorisation des actes de caissons hyperbares%' OR
    			data_mat2a[1] ILIKE '%Total valorisation%'
    		);
  
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[2] = cti_montant_remboursable
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(cti_base_remboursement) AS cti_base_remboursement,
    			SUM(cti_base_remboursement_sauf_transition) AS cti_base_remboursement_sauf_transition,
    			SUM(cti_montant_remboursable) AS cti_montant_remboursable
    		FROM w_cp_cti_fact2a
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_fact2a.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE pec_amo_ame ='AME' AND 
    			file_code = 'T1V4RAME' AND table_sequence = 4 AND 
    			(w_cp_cti_fact2a.prestation_code IN ('GHS','EXB','EXBR','EXH','ANT','APHE','NN1','NN2','NN3','RAP','SRC','STF','REA','DIP','D09','D17','D18','D19','OHB','REP') OR
    			w_cp_cti_fact2a.prestation_code LIKE 'IRRE%') AND
    			(
    			w_cp_cti_fact2a.prestation_code = 'GHS' AND data_mat2a[1] ILIKE '%Valorisation des GHS%' OR
    			w_cp_cti_fact2a.prestation_code = 'EXB' AND data_mat2a[1] ILIKE '%Valorisation extrême bas%' OR
    			w_cp_cti_fact2a.prestation_code = 'EXBR' AND data_mat2a[1] ILIKE '%Valorisation séjours avec rehosp dans même GHM%' OR
    			w_cp_cti_fact2a.prestation_code = 'EXH' AND data_mat2a[1] ILIKE '%Valorisation journées extrême haut%' OR
    			w_cp_cti_fact2a.prestation_code = 'DIP' AND data_mat2a[1] ILIKE '%Valorisation suppléments de dialyse%' OR
    			w_cp_cti_fact2a.prestation_code = 'D09' AND data_mat2a[1] ILIKE '%Valorisation suppléments de dialyse%' OR
    			w_cp_cti_fact2a.prestation_code = 'D17' AND data_mat2a[1] ILIKE '%Valorisation suppléments de dialyse%' OR
    			w_cp_cti_fact2a.prestation_code = 'D18' AND data_mat2a[1] ILIKE '%Valorisation suppléments de dialyse%' OR
    			w_cp_cti_fact2a.prestation_code = 'D19' AND data_mat2a[1] ILIKE '%Valorisation suppléments de dialyse%' OR
    			w_cp_cti_fact2a.prestation_code = 'OHB' AND data_mat2a[1] ILIKE '%Valorisation des actes de caissons hyperbares%' OR
    			w_cp_cti_fact2a.prestation_code = 'ANT' AND data_mat2a[1] ILIKE '%Valorisation suppléments antepartum%' OR
    			w_cp_cti_fact2a.prestation_code = 'NN1' AND data_mat2a[1] ILIKE '%Valorisation suppléments de néonat sans SI%' OR
    			w_cp_cti_fact2a.prestation_code = 'NN2' AND data_mat2a[1] ILIKE '%Valorisation suppléments de néonat avec SI%' OR
    			w_cp_cti_fact2a.prestation_code = 'NN3' AND data_mat2a[1] ILIKE '%Valorisation suppléments de réanimation néonat%' OR
    			w_cp_cti_fact2a.prestation_code = 'RAP' AND data_mat2a[1] ILIKE '%Valorisation suppléments radiothérapie pédiatrique%' OR
    			w_cp_cti_fact2a.prestation_code = 'SRC' AND data_mat2a[1] ILIKE '%Valorisation suppléments de surveillance continue%' OR
    			w_cp_cti_fact2a.prestation_code = 'STF' AND data_mat2a[1] ILIKE '%Valorisation suppléments de soins intensifs%' OR
    			w_cp_cti_fact2a.prestation_code = 'REP' AND data_mat2a[1] ILIKE '%Valorisation suppléments de réa pédiatrique%' OR
    			w_cp_cti_fact2a.prestation_code = 'REA' AND data_mat2a[1] ILIKE '%Valorisation suppléments de réanimation%' AND data_mat2a[1] NOT ILIKE '%néonat%'OR
    			w_cp_cti_fact2a.prestation_code LIKE 'IRRE%' AND data_mat2a[1] ILIKE '%Valorisation actes RDTH en Hospit%' OR
    			data_mat2a[1] ILIKE '%Total valorisation%'
    			)
    		GROUP BY 1) subview
    	WHERE file_code = 'T1V4RAME' AND p_mat2a_data.oid = subview.oid;
    	-- Ajustement EXB AME (ventilation remboursement entre EXB et GHS si somme identique)
    	UPDATE pmsi.p_mat2a_data
    	SET data_cti[2] =
    		CASE 
    			WHEN data_mat2a[1] ILIKE '%Valorisation des GHS%' THEN base.cti_to_number(data_mat2a[2]) - ecart
    			WHEN data_mat2a[1] ILIKE '%Valorisation extrême bas%' THEN base.cti_to_number(data_mat2a[2])
    			WHEN data_mat2a[1] ILIKE '%Valorisation séjours avec rehosp dans même GHM%' THEN base.cti_to_number(data_mat2a[2])
    			ELSE base.cti_to_number(data_cti[2]) END
    	FROM (
    		SELECT
    			p_mat2a_data.mat2a_import_id,
    			SUM(base.cti_to_number(data_mat2a[2])) AS mat2a_val,
    			SUM(base.cti_to_number(data_cti[2])) AS cti_val,
    			SUM(base.cti_to_number(data_mat2a[2])) - SUM(base.cti_to_number(data_cti[2])) AS ecart
    		FROM pmsi.p_mat2a_data
    		JOIN w_cp_imports ON w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE file_code = 'T1V4RAME' AND table_sequence = 4 AND
    			(data_mat2a[1] ILIKE '%Valorisation des GHS%' OR
    			data_mat2a[1] ILIKE '%Valorisation extrême bas%' OR
    			data_mat2a[1] ILIKE '%Valorisation séjours avec rehosp dans même GHM%')
    		GROUP BY 1
    		HAVING ABS(SUM(base.cti_to_number(data_mat2a[2])) - SUM(base.cti_to_number(data_cti[2]))) < 0.05
    		) subview
    	WHERE subview.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T1V4RAME' AND table_sequence = 4 AND
    		(data_mat2a[1] ILIKE '%Valorisation des GHS%' OR
    		data_mat2a[1] ILIKE '%Valorisation extrême bas%' OR
    		data_mat2a[1] ILIKE '%Valorisation séjours avec rehosp dans même GHM%');
    	-- Tableau T1V4RAME (AME) (2 = total et detail par GHS GHM)
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[6] = '0',
    		data_cti[7] = '0',
    		data_cti[8] = '0',
    		--data_cti[9] = '0',
    		data_cti[10] = '0',
    		data_cti[11] = '0',
    		data_cti[12] = '0',
    		data_cti[13] = '0',
    		data_cti[14] = '0',
    		data_cti[15] = '0',
    		data_cti[16] = '0',
    		-- data_cti[17] = '0',
    		data_cti[18] = '0',
    		data_cti[19] = '0',
    		data_cti[20] = '0',
    		data_cti[21] = '0'
    		--data_cti[22] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T1V4RAME' AND 
    		table_sequence = 2 AND 
    		line_type IN ('D', 'F');
  
      /* Attention, depuis 2018, la minoration forfaitaire n'apparait plus dans les tableaux  */
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[6] = cti_montant_remboursable_ghs,
    		data_cti[7] = cti_montant_remboursable_exb,
    		data_cti[8] = cti_montant_remboursable_exbr,
    		data_cti[9] =  CASE WHEN subview.mat2a_annee < 2018 THEN null ELSE cti_montant_remboursable_exh END,
    		data_cti[10] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_montant_remboursable_exh ELSE cti_montant_remboursable_irre END,
    		data_cti[11] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_montant_remboursable_irre ELSE cti_montant_remboursable_rap END,
    		data_cti[12] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_montant_remboursable_rap ELSE cti_montant_remboursable_rea END,
    		data_cti[13] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_montant_remboursable_rea ELSE cti_montant_remboursable_rep END,
    		data_cti[14] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_montant_remboursable_rep ELSE cti_montant_remboursable_stf END,
    		data_cti[15] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_montant_remboursable_stf ELSE cti_montant_remboursable_src END,
    		data_cti[16] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_montant_remboursable_src ELSE null END,
    		data_cti[17] = CASE WHEN subview.mat2a_annee < 2018 THEN null ELSE cti_montant_remboursable_nn1 END,
    		data_cti[18] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_montant_remboursable_nn1 ELSE cti_montant_remboursable_nn2 END,
    		data_cti[19] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_montant_remboursable_nn2 ELSE cti_montant_remboursable_nn3 END,
    		data_cti[20] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_montant_remboursable_nn3 ELSE cti_montant_remboursable_ant END,
    		data_cti[21] = CASE WHEN subview.mat2a_annee < 2018 THEN cti_montant_remboursable_ant ELSE null END
    		--data_cti[22] = cti_montant_remboursable_oxh
  
    	FROM (
    		SELECT p_mat2a_data.oid,
    			MAX(w_cp_imports.mat2a_annee) AS mat2a_annee,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'GHS' THEN cti_montant_remboursable ELSE 0 END) AS cti_montant_remboursable_ghs,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'EXB' THEN cti_montant_remboursable ELSE 0 END) AS cti_montant_remboursable_exb,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'EXBR' THEN cti_montant_remboursable ELSE 0 END) AS cti_montant_remboursable_exbr,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'EXH' THEN cti_montant_remboursable ELSE 0 END) AS cti_montant_remboursable_exh,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'ANT' THEN cti_montant_remboursable ELSE 0 END) AS cti_montant_remboursable_ant,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'RAP' THEN cti_montant_remboursable ELSE 0 END) AS cti_montant_remboursable_rap,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'REA' THEN cti_montant_remboursable ELSE 0 END) AS cti_montant_remboursable_rea,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'REP' THEN cti_montant_remboursable ELSE 0 END) AS cti_montant_remboursable_rep,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'SRC' THEN cti_montant_remboursable ELSE 0 END) AS cti_montant_remboursable_src,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'STF' THEN cti_montant_remboursable ELSE 0 END) AS cti_montant_remboursable_stf,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'NN1' THEN cti_montant_remboursable ELSE 0 END) AS cti_montant_remboursable_nn1,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'NN2' THEN cti_montant_remboursable ELSE 0 END) AS cti_montant_remboursable_nn2,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code = 'NN3' THEN cti_montant_remboursable ELSE 0 END) AS cti_montant_remboursable_nn3,
    			SUM(CASE WHEN w_cp_cti_fact2a.prestation_code LIKE 'IRRE%' THEN cti_montant_remboursable ELSE 0 END) AS cti_montant_remboursable_irre
    		FROM w_cp_cti_fact2a
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_fact2a.mat2a_import_id = p_mat2a_data.mat2a_import_id
      		JOIN w_cp_imports ON w_cp_imports.mat2a_import_id = w_cp_cti_fact2a.mat2a_import_id
    		WHERE pec_amo_ame ='AME' AND 
    			file_code = 'T1V4RAME' AND table_sequence = 2 AND 
    			(w_cp_cti_fact2a.prestation_code IN ('GHS','EXB','EXBR','EXH','ANT','NN1','NN2','NN3','REA','SRC','STF','RAP','REA') OR
    			w_cp_cti_fact2a.prestation_code LIKE 'IRRE%') AND
    			(
    			line_type = 'D' AND data_mat2a[1] ILIKE ghs_code AND data_mat2a[2] ILIKE ghm_code || '%' OR
    			line_type = 'F'
    			)
    		GROUP BY 1) subview
    	WHERE file_code = 'T1V4RAME' AND p_mat2a_data.oid = subview.oid;
    	-- reventilation EXB
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[7] = data_mat2a[7],
    		data_cti[8] = data_mat2a[8],
    		data_cti[6] = base.cti_to_number(data_mat2a[6])
    			+ (base.cti_to_number(data_mat2a[6]) 
    			+ base.cti_to_number(data_mat2a[7]) 
    			+ base.cti_to_number(data_mat2a[8]) 
    			- base.cti_to_number(data_cti[6])
    			- base.cti_to_number(data_cti[7])
    			- base.cti_to_number(data_cti[8]))
    	WHERE file_code = 'T1V4RAME' AND 
    		table_sequence = 2 AND
    		line_type IN ('D', 'F') AND
    		ABS(base.cti_to_number(data_mat2a[6]) - base.cti_to_number(data_cti[6])) > 0.05 AND
    		ABS(base.cti_to_number(data_mat2a[6]) + base.cti_to_number(data_mat2a[7]) + base.cti_to_number(data_mat2a[8]) 
    			- base.cti_to_number(data_cti[6])
    			- base.cti_to_number(data_cti[7])
    			- base.cti_to_number(data_cti[8])) <= 0.05;
    	-- 2015 Minoration forfaitaire GHS liste en SUS
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[9] = cti_base_remboursement_sauf_transition	
    	FROM (
    		SELECT 
    			p_mat2a_data.oid,
    			SUM(cti_nb_ghs_minore) * -40 AS cti_base_remboursement_sauf_transition
    		FROM w_cp_cti_fact2a
    			JOIN pmsi.p_mat2a_data ON w_cp_cti_fact2a.mat2a_import_id = p_mat2a_data.mat2a_import_id
    			JOIN w_cp_imports ON w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE 1=1
    			AND w_cp_imports.mat2a_annee >= 2015
    			AND pec_amo_ame ='AME'
    			AND file_code = 'T1V4RAME' AND table_sequence = 2 
    			AND w_cp_cti_fact2a.prestation_code = 'GHS'
    			AND w_cp_cti_fact2a.cti_nb_ghs_minore >= 1
    			AND
    				(
    				line_type = 'D' AND data_mat2a[1] ILIKE ghs_code AND data_mat2a[2] ILIKE ghm_code || '%' OR
    				line_type = 'F'
    				)
    		GROUP BY 1) subview
    	WHERE file_code = 'T1V4RAME' AND p_mat2a_data.oid = subview.oid;
  
    	RAISE NOTICE '%' , 'Cumuls CTI. IVG';
    	-- 2014 : T1V1VIVG
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[2] = '0',
    		data_cti[3] = '0',
    		data_cti[4] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T1V1VIVG' AND 
    		table_sequence = 1 AND 
    		line_type IN ('D', 'F');
    	/*
    		Modif CLA du 12/11/2015
    		Filtre sur les prestations d'IVG uniquement (chapitre 1.V.1.IVG du guide de lecture OVALIDE DGF 2015)
    		pour ne pas remonter les autres prestations des séjours valorisés IVG.
    		Cas avec un séjour ou des prestations PHH et EMI étaient également présentes dans le décompte CTI à tort
    	*/
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[2] = cti_nombre,
    		data_cti[3] = cti_base_remboursement,
    		data_cti[4] = cti_montant_remboursable
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(cti_nombre) AS cti_nombre,
    			SUM(cti_base_remboursement) AS cti_base_remboursement,
    			SUM(cti_base_remboursement_sauf_transition) AS cti_base_remboursement_sauf_transition,
    			SUM(cti_montant_remboursable) AS cti_montant_remboursable
    		FROM w_cp_cti_fact2a
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_fact2a.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE pec_amo_ame='IVG' AND
    			file_code = 'T1V1VIVG' AND table_sequence = 1 AND
    			w_cp_cti_fact2a.prestation_code IN ('AMD', 'IMD', 'APD', 'IPD', 'AMF') AND
    			(line_type = 'D' AND data_mat2a[1] ILIKE prestation_code || '%' OR
    			line_type = 'F')
    		GROUP BY 1
    		) subview
    	WHERE file_code = 'T1V1VIVG' AND p_mat2a_data.oid = subview.oid;
  
    	RAISE NOTICE '%' , 'Cumuls CTI. UCD';
  
    	DROP TABLE IF EXISTS w_cp_cti_facucd_transmis;
  	CREATE TEMP TABLE w_cp_cti_facucd_transmis AS
    	SELECT 
    		p_rss.import_id,
    		mat2a_import_id,
    		ucd_id,
    		t_ucd.code AS ucd_code,
  		p_rss_ucd.indication,
    		w_cp_imports.mat2a_annee,
    		w_cp_imports.mat2a_mois,
  		SUM(nombre) AS nombre_transmis,
    		SUM(CASE WHEN traitement_epmsi NOT IN ('30') OR t_ucd.code IN ('9261110','9261104') AND non_pris_en_compte = '1' THEN nombre ELSE 0 END) AS nombre_non_valorise_1,
    		SUM(CASE WHEN traitement_epmsi IN ('30') AND t_ucd.code NOT IN ('9261110','9261104') AND non_pris_en_compte = '1' THEN nombre ELSE 0 END) AS nombre_non_valorise_2,
  		0::numeric AS nombre_non_valorise_3,
    		SUM(CASE WHEN traitement_epmsi IN ('30') AND non_pris_en_compte <> '1' THEN nombre ELSE 0 END) AS nombre_valorise
    	FROM pmsi.p_rss_ucd
    	JOIN pmsi.p_rss ON rss_id = p_rss.oid
    	JOIN w_cp_imports ON w_cp_imports.import_id = p_rss.import_id
    	JOIN pmsi.t_ucd ON ucd_id = t_ucd.oid
  	LEFT JOIN pmsi.t_type_ucd ON t_type_ucd.oid = p_rss_ucd.type_ucd_id
    	WHERE en_cours = '0' AND
    		ghm_id <> 0 AND
  		(t_type_ucd.code = 'SUS' OR t_type_ucd.code = 'ATU' AND t_ucd.code IN ('9261110','9261104') ) 
    	GROUP BY 1,2,3,4,5,6,7
    	;
  
    	IF NOT EXISTS (SELECT * FROM pg_class WHERE relnamespace = pg_my_temp_schema() AND relname = 'w_cp_cti_facucd') THEN
    		DROP TABLE IF EXISTS w_cp_cti_facucd;
    		CREATE TEMP TABLE w_cp_cti_facucd (
    			import_id bigint, 
    			mat2a_import_id bigint,
    			ucd_id bigint,
    			ucd_code text,
  			indication text,
    			pec_amo_ame text,
    			mat2a_annee numeric,
    			mat2a_mois numeric,
    			nombre numeric,
    			montant_facture numeric(13,2),
    			montant_tr numeric(13,2),
    			montant_t2a_accorde numeric(13,2)
    		);
    	END IF;
    	TRUNCATE w_cp_cti_facucd;
    	INSERT INTO w_cp_cti_facucd
    	SELECT 
    		p_rss.import_id,
    		mat2a_import_id,
    		ucd_id,
    		t_ucd.code AS ucd_code,
  		p_rss_ucd.indication,
    		CASE WHEN traitement_epmsi IN ('30') THEN 'AMO' WHEN traitement_epmsi IN ('31') THEN 'AME' WHEN traitement_epmsi IN ('32') THEN 'IVG' ELSE 'SU' END AS pec_amo_ame,
    		w_cp_imports.mat2a_annee,
    		w_cp_imports.mat2a_mois,
    		round(SUM(CASE WHEN p_rss_ucd.non_pris_en_compte <> '1' AND montant_facture <> 0 THEN nombre ELSE 0 END),2) AS nombre,
    		round(SUM(CASE WHEN p_rss_ucd.non_pris_en_compte <> '1' AND montant_facture <> 0 THEN montant_facture ELSE 0 END),2) AS montant_facture,
    		round(SUM(CASE WHEN p_rss_ucd.non_pris_en_compte <> '1' AND montant_facture <> 0 THEN montant_tr ELSE 0 END),2) AS montant_tr,
    		round(SUM(CASE WHEN p_rss_ucd.non_pris_en_compte <> '1' AND montant_facture <> 0 THEN montant_t2a_accorde ELSE 0 END),2) AS montant_t2a_accorde
    	FROM pmsi.p_rss_ucd
    	JOIN pmsi.p_rss ON rss_id = p_rss.oid
    	JOIN w_cp_imports ON w_cp_imports.import_id = p_rss.import_id
    	JOIN pmsi.t_ucd ON ucd_id = t_ucd.oid
  	LEFT JOIN pmsi.t_type_ucd ON t_type_ucd.oid = p_rss_ucd.type_ucd_id
    	WHERE en_cours = '0' AND
          (t_type_ucd.code = 'SUS' OR t_type_ucd.code = 'ATU' AND t_ucd.code IN ('9261104','9261110') AND non_pris_en_compte = '0') AND
  		montant_tr <> 0 AND
    		ghm_id <> 0 AND
    		traitement_epmsi IN ('30','31','32','33') 
    	GROUP BY 1,2,3,4,5,6,7,8;
  
    	-- 2015 : T1V2VMED
  
    	-- Transmis
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[5] = '0',
    		data_cti[6] = '0',
    		data_cti[7] = '0',
    		data_cti[10] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		w_cp_imports.mat2a_annee >= 2019 AND
    		file_code = 'T1V2VMED' AND 
    		table_sequence = 1 AND 
    		line_type IN ('D', 'F')
    	;
  
  	UPDATE pmsi.p_mat2a_data SET
    		data_cti[5] = nombre_transmis,
    		data_cti[6] = nombre_non_valorise_1,
    		data_cti[7] = CASE WHEN nombre_non_valorise_2 >= base.cti_to_number(data_mat2a[9]) THEN nombre_non_valorise_2 - base.cti_to_number(data_mat2a[9]) ELSE nombre_non_valorise_2 END,
    		data_cti[9] = CASE WHEN nombre_non_valorise_2 >= base.cti_to_number(data_mat2a[9]) THEN base.cti_to_number(data_mat2a[9]) ELSE 0 END,
    		data_cti[10] = nombre_valorise
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(nombre_transmis) AS nombre_transmis,
    			SUM(nombre_non_valorise_1) AS nombre_non_valorise_1,
    			SUM(nombre_non_valorise_2) AS nombre_non_valorise_2,
    			SUM(nombre_non_valorise_3) AS nombre_non_valorise_3,
  			SUM(nombre_valorise) AS nombre_valorise
    		FROM w_cp_cti_facucd_transmis
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_facucd_transmis.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE file_code = 'T1V2VMED' AND 
  			table_sequence = 1 AND
  			w_cp_cti_facucd_transmis.mat2a_annee >= 2019 AND
    			(line_type = 'D' AND data_mat2a[2] = ucd_code OR
    			line_type = 'F') AND
  			(line_type = 'D' AND data_mat2a[3] = indication OR
    			line_type = 'F')
    		GROUP BY 1
    		) subview
    	WHERE file_code = 'T1V2VMED' AND p_mat2a_data.oid = subview.oid
    	;
  
    	-- Valorise
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[4] = '0',
    		data_cti[5] = '0',
    		data_cti[6] = '0',
    		data_cti[7] = '0',
    		data_cti[8] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		w_cp_imports.mat2a_annee >= 2015 AND
    		file_code = 'T1V2VMED' AND 
    		table_sequence = 2 AND 
    		line_type IN ('D', 'F');
  
    	-- Concaténation du code et du texte dans le champ de code 
      -- A partir de M03 2018, nouvelle colonne "indicatrion" entre le code et le libelle
    	UPDATE pmsi.p_mat2a_data SET
          data_mat2a[2] = trim(split_part(data_mat2a[2]::text,' ',1))|| ' - '::text || CASE WHEN w_cp_imports.mat2a_annee >= 2018 THEN data_mat2a[4]::text ELSE data_mat2a[3]::text END
      FROM w_cp_imports 
      WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
          file_code = 'T1V2VMED' AND 
    		table_sequence = 2 AND 
    		line_type = 'D';
  
    	UPDATE pmsi.p_mat2a_data SET
          data_cti[3] = CASE WHEN subview.mat2a_annee >= 2018 THEN indication ELSE null END,
          data_cti[4] = CASE WHEN subview.mat2a_annee >= 2018 THEN null ELSE nombre END,
          data_cti[5] = CASE WHEN subview.mat2a_annee >= 2018 THEN nombre ELSE montant_facture END,
          data_cti[6] = CASE WHEN subview.mat2a_annee >= 2018 THEN montant_facture ELSE montant_tr END,
          data_cti[7] = CASE WHEN subview.mat2a_annee >= 2018 THEN montant_tr ELSE montant_t2a_accorde END,
          data_cti[8] = CASE WHEN subview.mat2a_annee >= 2018 THEN montant_t2a_accorde ELSE montant_t2a_accorde - montant_facture END,
          data_cti[9] = CASE WHEN subview.mat2a_annee >= 2018 THEN montant_t2a_accorde - montant_facture ELSE null END
    	FROM (
    		SELECT
              p_mat2a_data.oid,
              CASE WHEN line_type = 'F' THEN '' ELSE w_cp_cti_facucd.indication END as indication,
    			SUM(nombre) AS nombre,
    			SUM(montant_facture) AS montant_facture,
    			SUM(montant_tr) AS montant_tr,
              SUM(montant_t2a_accorde) AS montant_t2a_accorde,
              MAX(w_cp_imports.mat2a_annee) AS mat2a_annee
    		FROM 
              w_cp_cti_facucd
              JOIN pmsi.p_mat2a_data ON w_cp_cti_facucd.mat2a_import_id = p_mat2a_data.mat2a_import_id
              JOIN w_cp_imports ON w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE 
              (pec_amo_ame='AMO' OR pec_amo_ame = 'IVG') AND
    			w_cp_cti_facucd.mat2a_annee >= 2015 AND
    			file_code = 'T1V2VMED' AND table_sequence = 2 AND
    			(line_type = 'D' 
                  AND substr(data_mat2a[2], 1, 7) || CASE WHEN w_cp_imports.mat2a_annee >= 2018 THEN data_mat2a[3] ELSE '' END ILIKE ucd_code || indication 
              OR line_type = 'F')
    		GROUP BY 1,2
    		) subview
    	WHERE file_code = 'T1V2VMED' AND p_mat2a_data.oid = subview.oid;
    	-- 2014 : T1V2VMED
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[3] = '0',
    		data_cti[4] = '0',
    		data_cti[5] = '0',
    		data_cti[6] = '0',
    		data_cti[7] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		w_cp_imports.mat2a_annee < 2015 AND
    		file_code = 'T1V2VMED' AND 
    		table_sequence = 2 AND 
    		line_type IN ('D', 'F');
  
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[3] = nombre,
    		data_cti[4] = montant_facture,
    		data_cti[5] = montant_tr,
    		data_cti[6] = montant_t2a_accorde,
    		data_cti[7] = montant_t2a_accorde - montant_facture
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(nombre) AS nombre,
    			SUM(montant_facture) AS montant_facture,
    			SUM(montant_tr) AS montant_tr,
    			SUM(montant_t2a_accorde) AS montant_t2a_accorde
    		FROM w_cp_cti_facucd
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_facucd.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE pec_amo_ame='AMO' AND
    			w_cp_cti_facucd.mat2a_annee < 2015 AND
    			file_code = 'T1V2VMED' AND table_sequence = 2 AND
    			(line_type = 'D' AND data_mat2a[1] ILIKE ucd_code || '%' OR
    			line_type = 'F')
    		GROUP BY 1
    		) subview
    	WHERE file_code = 'T1V2VMED' AND p_mat2a_data.oid = subview.oid;
  
  
    	RAISE NOTICE '%' , 'Cumuls CTI. LPP';
  
    	DROP TABLE IF EXISTS w_cp_cti_faclpp_transmis;
    	CREATE TEMP TABLE w_cp_cti_faclpp_transmis AS
    	SELECT 
    		p_rss.import_id,
    		mat2a_import_id,
    		lpp_id,
    		t_lpp.code AS lpp_code,
    		SUM(nombre) AS nombre_transmis,
    		SUM(CASE WHEN traitement_epmsi NOT IN ('30') THEN nombre ELSE 0 END) AS nombre_non_valorise_1,
    		SUM(CASE WHEN traitement_epmsi IN ('30') AND non_pris_en_compte = '1' AND prix_unitaire <> 0 THEN nombre ELSE 0 END) AS nombre_non_valorise_2,
  		SUM(CASE WHEN traitement_epmsi IN ('30') AND non_pris_en_compte = '1' AND prix_unitaire = 0 THEN nombre ELSE 0 END) AS nombre_non_valorise_3,
    		SUM(CASE WHEN traitement_epmsi IN ('30') AND non_pris_en_compte <> '1' THEN nombre ELSE 0 END) AS nombre_valorise
    	FROM pmsi.p_rss_lpp
    	JOIN pmsi.p_rss ON rss_id = p_rss.oid
    	JOIN w_cp_imports ON w_cp_imports.import_id = p_rss.import_id
    	JOIN pmsi.t_lpp ON lpp_id = t_lpp.oid
    	WHERE en_cours = '0' AND
  		ghm_id > 0
    	GROUP BY 1,2,3,4
    	;
  
  
    	IF NOT EXISTS (SELECT * FROM pg_class WHERE relnamespace = pg_my_temp_schema() AND relname = 'w_cp_cti_faclpp') THEN
    		DROP TABLE IF EXISTS w_cp_cti_faclpp;
    		CREATE TEMP TABLE w_cp_cti_faclpp (
    			import_id bigint, 
    			mat2a_import_id bigint,
    			lpp_id bigint,
    			lpp_code text,
    			pec_amo_ame text,
    			nombre numeric,
    			montant_facture numeric(13,2),
    			montant_tr numeric(13,2),
    			montant_t2a_accorde numeric(13,2)
    		);
    	END IF;
    	TRUNCATE w_cp_cti_faclpp;
    	INSERT INTO w_cp_cti_faclpp
    	SELECT 
    		p_rss.import_id,
    		mat2a_import_id,
    		lpp_id,
    		t_lpp.code,
    		CASE WHEN traitement_epmsi IN ('30') THEN 'AMO' ELSE 'AME' END AS pec_amo_ame,
    		SUM(CASE WHEN p_rss_lpp.non_pris_en_compte <> '1' AND montant_facture <> 0 THEN nombre ELSE 0 END) AS nombre,
    		round(SUM(CASE WHEN p_rss_lpp.non_pris_en_compte <> '1' AND montant_facture <> 0 THEN montant_facture ELSE 0 END),2) AS montant_facture,
    		round(SUM(CASE WHEN p_rss_lpp.non_pris_en_compte <> '1' AND montant_facture <> 0 THEN montant_tr ELSE 0 END),2) AS montant_tr,
    		round(SUM(CASE WHEN p_rss_lpp.non_pris_en_compte <> '1' AND montant_facture <> 0 THEN montant_t2a_accorde ELSE 0 END),2) AS montant_t2a_accorde
    	FROM pmsi.p_rss_lpp
    	JOIN pmsi.p_rss ON rss_id = p_rss.oid
    	JOIN w_cp_imports ON w_cp_imports.import_id = p_rss.import_id
    	JOIN pmsi.t_lpp ON lpp_id = t_lpp.oid
    	WHERE en_cours = '0' AND
  		p_rss.ghm_id > 0 AND
    		traitement_epmsi IN ('30','31') 
    	GROUP BY 1,2,3,4,5;
  
    	-- 2014 : T1V3VDMI
  
    	-- Transmission
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[3] = '0',
    		data_cti[4] = '0',
    		data_cti[5] = '0',
    		data_cti[6] = '0',
    		data_cti[7] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T1V3VDMI' AND 
    		table_sequence = 1 AND 
    		line_type IN ('D');
  
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[3] = nombre_transmis,
    		data_cti[4] = nombre_non_valorise_1,
    		data_cti[5] = nombre_non_valorise_2,
  		data_cti[6] = nombre_non_valorise_3,
    		data_cti[7] = nombre_valorise
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(nombre_transmis) AS nombre_transmis,
    			SUM(nombre_non_valorise_1) AS nombre_non_valorise_1,
    			SUM(nombre_non_valorise_2) AS nombre_non_valorise_2,
    			SUM(nombre_non_valorise_3) AS nombre_non_valorise_3,
  			SUM(nombre_valorise) AS nombre_valorise
    		FROM w_cp_cti_faclpp_transmis
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_faclpp_transmis.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE file_code = 'T1V3VDMI' AND table_sequence = 1 AND
    			line_type = 'D' AND data_mat2a[1] ILIKE lpp_code || '%'
    		GROUP BY 1
    		) subview
    	WHERE file_code = 'T1V3VDMI' AND p_mat2a_data.oid = subview.oid
    	;
  
    	-- Valorisation
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[3] = '0',
    		data_cti[4] = '0',
    		data_cti[5] = '0',
    		data_cti[6] = '0',
    		data_cti[7] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T1V3VDMI' AND 
    		table_sequence = 2 AND 
    		line_type IN ('D', 'F');
  
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[3] = nombre,
    		data_cti[4] = montant_facture,
    		data_cti[5] = montant_tr,
    		data_cti[6] = montant_t2a_accorde,
    		data_cti[7] = montant_t2a_accorde - montant_facture
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(nombre) AS nombre,
    			SUM(montant_facture) AS montant_facture,
    			SUM(montant_tr) AS montant_tr,
    			SUM(montant_t2a_accorde) AS montant_t2a_accorde
    		FROM w_cp_cti_faclpp
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_faclpp.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE pec_amo_ame='AMO' AND
    			file_code = 'T1V3VDMI' AND table_sequence = 2 AND
    			(line_type = 'D' AND data_mat2a[1] ILIKE lpp_code || '%' OR
    			line_type = 'F')
    		GROUP BY 1
    		) subview
    	WHERE file_code = 'T1V3VDMI' AND p_mat2a_data.oid = subview.oid;
  
  
    	-- A partir de 2015, la partie des UCD dans le tableau T1V4MDAME a une colonne UCD13, on la vire
    	-- pour n'avoir que 7 colonnes au final
    	UPDATE pmsi.p_mat2a_data SET
    		data_mat2a = data_mat2a[2:array_length(data_mat2a, 1)]
    	--FROM w_cp_imports 
    	WHERE 1=1
    		--AND w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		AND line_type IN ('D','F','H')
    		AND array_length(data_mat2a,1) = 8
    		AND p_mat2a_data.mat2a_import_id::text || file_code::text || table_sequence::text IN (
    				SELECT 
    					p_mat2a_data.mat2a_import_id::text || file_code::text || table_sequence::text
    				FROM
    					pmsi.p_mat2a_data
    					JOIN w_cp_imports ON w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id
    				WHERE 1=1
    					AND data_mat2a[2] ilike '%molécules onéreuses%' 
    					AND line_type = 'T' 
    					and file_code = 'T1V4MDAME'
    					AND w_cp_imports.mat2a_annee >= 2015
    		);
  
  
  
    	UPDATE pmsi.p_mat2a_data SET
  		data_cti[3] = NULL,
  		data_cti[4] = NULL,
    		data_cti[5] = '0',
    		data_cti[6] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T1V4MDAME' AND 
    		line_type IN ('D', 'F') 
    		AND table_sequence IN (1,2);
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[5] = nombre,
    		data_cti[6] = montant_facture
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(nombre) AS nombre,
    			SUM(montant_facture) AS montant_facture
    		FROM w_cp_cti_faclpp
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_faclpp.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE pec_amo_ame='AME' AND
    			file_code = 'T1V4MDAME' AND table_sequence = 2 AND
    			(line_type = 'D' AND data_mat2a[1] ILIKE lpp_code || '%' OR
    			line_type = 'F')
    		GROUP BY 1
    		UNION ALL
    		SELECT p_mat2a_data.oid,
    			SUM(nombre) AS nombre,
    			SUM(montant_facture) AS montant_facture
    		FROM w_cp_cti_facucd
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_facucd.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE pec_amo_ame='AME' AND
    			file_code = 'T1V4MDAME' AND table_sequence = 1 AND
    			(line_type = 'D' AND data_mat2a[2] = ucd_code OR
    			line_type = 'F') AND
  			(line_type = 'D' AND data_mat2a[3] = indication OR
    			line_type = 'F')
    		GROUP BY 1
    		) subview
    	WHERE file_code = 'T1V4MDAME' AND p_mat2a_data.oid = subview.oid;
  
    	RAISE NOTICE '%' , 'Cumuls CTI. Prise en charge T2A';
    	-- Forfait journalier
    	UPDATE pmsi.p_mat2a_data
    	SET data_cti[3] = 0
    	FROM w_cp_imports
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		p_mat2a_data.file_code = 'T1V1TXR' AND
    		p_mat2a_data.table_sequence = 2;
  
    	UPDATE pmsi.p_mat2a_data
    	SET data_cti[3] = subview.effectif
    	FROM
    	(SELECT mat2a_import_id,
    			CASE 
  
    				WHEN p_rss.date_entree - p_rss.date_naissance <= 30 THEN 'Y'
    				WHEN t_ghm.code IN ('28Z08Z', '28Z09Z', '28Z11Z', '28Z12Z', '28Z13Z', '28Z18Z', '28Z19Z', '28Z20Z', '28Z21Z', '28Z22Z', '28Z23Z', '28Z24Z', '28Z25Z') THEN 'Y'
    				WHEN p_rss.oid IN (SELECT rss_id 
    						FROM pmsi.p_rsf_detail
    						JOIN pmsi.t_prestations ON prestation_id = t_prestations.oid
    						WHERE t_prestations.code IN ('PO1', 'PO2', 'PO3', 'PO4', 'PO5', 'PO6', 'PO7', 'PO8', 'PO9', 'POA')
    						) THEN 'Y'
    				WHEN t_pec.code IN ('2') THEN 'Z'
    				WHEN duree_sejour = 0 THEN 'X'
    				WHEN w_cp_imports.mat2a_annee < 2015 AND t_ghm.code LIKE '15%' THEN 'X'
    				WHEN t_ghm.code LIKE '28%' THEN 'X'
    				WHEN t_ghm.code in ('23K02Z' , '14Z08Z') THEN 'X'
  
    				ELSE t_pec_fj.code END AS pec_fj,
    			CASE 
    				WHEN p_rss.date_entree - p_rss.date_naissance <= 30 THEN 'E'
    				WHEN t_ghm.code IN ('28Z08Z', '28Z09Z', '28Z11Z', '28Z12Z', '28Z13Z', '28Z18Z', '28Z19Z', '28Z20Z', '28Z21Z', '28Z22Z', '28Z23Z', '28Z24Z', '28Z25Z') THEN 'X'
    				WHEN p_rss.oid IN (SELECT rss_id 
    						FROM pmsi.p_rsf_detail
    						JOIN pmsi.t_prestations ON prestation_id = t_prestations.oid
    						WHERE t_prestations.code IN ('PO1', 'PO2', 'PO3', 'PO4', 'PO5', 'PO6', 'PO7', 'PO8', 'PO9', 'POA')
    						) THEN 'X'
    				WHEN t_pec.code IN ('2') THEN 'A'
    				WHEN duree_sejour = 0 THEN 'X'
    				WHEN w_cp_imports.mat2a_annee < 2015 AND t_ghm.code LIKE '15%' THEN 'X'
    				WHEN t_ghm.code LIKE '28%' THEN 'X'
    				WHEN t_ghm.code in ('23K02Z' , '14Z08Z') THEN 'X'		
  
    				WHEN t_pec_fj.code = 'X' THEN 'X'
    				WHEN t_pec_fj.code = 'L' AND mat2a_annee >= 2016 THEN 'A'
    				WHEN t_pec_fj.code = 'L' AND mat2a_annee < 2016 THEN 'E'
    				WHEN t_pec_fj.code = 'R' THEN 'E'
    				WHEN w_cp_imports.mat2a_annee < 2015 AND p_vidhosp.nature_pec IN ('13','30','41') THEN 'E' 
    				WHEN w_cp_imports.mat2a_annee > 2014 AND p_vidhosp.nature_pec IN ('13','41') THEN 'E' 
    				WHEN t_pec_fj.code = 'A' THEN 'A'
    				ELSE '' END AS pec_reel_fj,
    			count(*) AS effectif
    		FROM pmsi.p_rss
    			JOIN pmsi.p_rsf_total ON p_rsf_total.rss_id = p_rss.oid
    			JOIN base.t_pec ON code_pec_id = t_pec.oid
    			JOIN base.t_pec_fj ON code_pec_fj_id = t_pec_fj.oid
    			JOIN pmsi.p_vidhosp ON p_vidhosp.rss_id = p_rss.oid
    			JOIN pmsi.t_ghm on ghm_id = t_ghm.oid
    			JOIN w_cp_imports ON w_cp_imports.import_id = p_rss.import_id
    		WHERE en_cours = '0' AND
    			ghm_id <> 0 AND
    			traitement_epmsi IN ('30') 
    		GROUP BY 1,2,3
    		ORDER BY 1,2,3) subview
    	WHERE subview.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		p_mat2a_data.file_code = 'T1V1TXR' AND
    		p_mat2a_data.table_sequence = 2 AND
    		(
    			data_mat2a[1] LIKE 'A:%' AND pec_fj = 'A' OR
    			data_mat2a[1] LIKE 'L:%' AND pec_fj = 'L' OR
    			data_mat2a[1] LIKE 'R:%' AND pec_fj = 'R' OR
    			data_mat2a[1] LIKE '%FJ non applicable%' AND pec_fj = 'X' OR
    			data_mat2a[1] LIKE '%Nouveaux-nés%' AND pec_fj = 'Y' OR
    			data_mat2a[1] LIKE '%Séjours en attente%' AND pec_fj = 'Z'
    		) AND
    		(
    			data_mat2a[2] ILIKE '%FJ à la charge de l''assuré%' AND pec_reel_fj = 'A' OR
    			data_mat2a[2] ILIKE '%exonération du FJ%' AND pec_reel_fj = 'E' OR
    			data_mat2a[2] ILIKE '%FJ non applicable%' AND pec_reel_fj = 'X'
    		);
    	-- Taux de prise en charge
    	UPDATE pmsi.p_mat2a_data
    	SET data_cti[3] = subview.effectif
    	FROM
    	(
    		SELECT mat2a_import_id,
    			to_char(p_rsf_total.taux_remboursement,'FM990') || '%' ||
    				CASE 
    				WHEN p_rss.date_entree - p_rss.date_naissance <= 30 THEN ' forcé (*)'
    				WHEN t_ghm.code IN ('28Z08Z', '28Z09Z', '28Z11Z', '28Z12Z', '28Z13Z', '28Z18Z', '28Z19Z', '28Z20Z', '28Z21Z', '28Z22Z', '28Z23Z', '28Z24Z', '28Z25Z') THEN ' forcé (*)'
    				WHEN p_rss.oid IN (SELECT rss_id 
    						FROM pmsi.p_rsf_detail
    						JOIN pmsi.t_prestations ON prestation_id = t_prestations.oid
    						WHERE t_prestations.code IN ('PO1', 'PO2', 'PO3', 'PO4', 'PO5', 'PO6', 'PO7', 'PO8', 'PO9', 'POA')
    						) THEN ' forcé (*)'
    				ELSE CASE WHEN p_rsf_total.code_pec IN ('2') THEN ' en attente de taux' ELSE '' END
    				END AS taux,
    			CASE WHEN montant_tm <> 0 THEN 'montant TM numérique' else 'montant TM non numérique ou à 0' END AS montant_tm, 
    			count(*) AS effectif,
    			MAX(p_rss.oid)
    		FROM pmsi.p_rss
    			JOIN pmsi.p_rsf_total ON p_rsf_total.rss_id = p_rss.oid
    			JOIN pmsi.p_vidhosp ON p_vidhosp.rss_id = p_rss.oid
    			JOIN pmsi.t_ghm on ghm_id = t_ghm.oid
    			JOIN w_cp_imports ON w_cp_imports.import_id = p_rss.import_id
    		WHERE en_cours = '0' AND
    			ghm_id <> 0 AND
    			traitement_epmsi IN ('30')
    		GROUP BY 1,2,3
    		ORDER BY 1,2,3
    	) subview
    	WHERE subview.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		p_mat2a_data.file_code = 'T1V1TXR' AND
    		p_mat2a_data.table_sequence = 3 AND
    		data_mat2a[1] = subview.taux AND
    		data_mat2a[2] = subview.montant_tm;
  
  
  
    	RAISE NOTICE '%' , 'Transports 2020';
  
    	UPDATE pmsi.p_mat2a_data SET 
  		data_cti[5] = 0,
  		data_cti[6] = 0
    	FROM w_cp_imports
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		p_mat2a_data.file_code = 'T11TRANS' AND
    		p_mat2a_data.line_type = 'D' AND
    		mat2a_annee <= 2020;
  
  	UPDATE pmsi.p_mat2a_data SET
  		data_cti[5] = cti_nombre,
  		data_cti[6] = cti_montant_remboursable
  	FROM (
  	SELECT p_mat2a_data.oid,
  	    SUM(w_cp_cti_fact2a.cti_nombre) AS cti_nombre,
  	    SUM(w_cp_cti_fact2a.cti_montant_remboursable) AS cti_montant_remboursable
  	FROM w_cp_cti_fact2a
  	JOIN pmsi.p_mat2a_data ON
  	    w_cp_cti_fact2a.mat2a_import_id = p_mat2a_data.mat2a_import_id
  	JOIN pmsi.p_mat2a_import ON p_mat2a_data.mat2a_import_id = p_mat2a_import.oid
  	WHERE file_code = 'T11TRANS' AND table_sequence = 1 AND 
  	    p_mat2a_import.mat2a_annee <= 2020 AND
  	    line_type = 'D' AND data_mat2a[1] ILIKE prestation_code
  	GROUP BY 1) subview
  	WHERE file_code = 'T11TRANS' AND p_mat2a_data.oid = subview.oid;
  
  
  	UPDATE pmsi.p_mat2a_data SET 
  		data_cti[5] = NULL,
  		data_cti[6] = NULL
  	FROM w_cp_imports
  	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
  		p_mat2a_data.file_code = 'T11TRANS' AND
  		p_mat2a_data.line_type = 'D' AND
    		mat2a_annee <= 2020;
  
  
      RAISE NOTICE '%' , 'Transports 2021';
  
  	DROP TABLE IF EXISTS w_transports_2021;
  	CREATE TEMP TABLE w_transports_2021 AS
  	SELECT 
  		w_cp_imports.mat2a_import_id,
  		t_prestations.code AS prestation_code, 
  		to_char(date_part('month',date_debut),'FM00') AS mois, 
  		count(*) AS cti_nombre,
  		SUM(p_rsf_detail.base_remboursement) AS cti_base_remboursement	
  	FROM pmsi.p_rss
    	JOIN pmsi.p_rsf_detail ON p_rsf_detail.rss_id = p_rss.oid
    	JOIN pmsi.t_prestations ON prestation_id = t_prestations.oid
    	JOIN w_cp_imports ON w_cp_imports.import_id = p_rss.import_id
    	JOIN base.t_finess ON p_rss.finess = t_finess.code
    	WHERE en_cours = '0' AND
  		mat2a_annee >= 2021 AND
    		ghm_id <> 0 AND
  		t_prestations.code IN ('TDE', 'TDE1', 'TDE2', 'TDE3', 'TDE4', 'TDE5', 'TSE', 'TSE6', 'TSE7', 'TSE8', 'TSE9' ) AND
    		est_ligne_rss = '1'
    	GROUP BY 1,2,3
    	;
  
    	UPDATE pmsi.p_mat2a_data SET 
  		data_cti[7] = 0,
  		data_cti[8] = 0,
  		data_mat2a[1] = CASE WHEN data_mat2a[1] = 'TDE' OR data_mat2a[1] = 'TSE' THEN data_mat2a[1] || substr(data_mat2a[3],2,1) ELSE data_mat2a[1] END,
  		data_mat2a[2] = CASE WHEN data_mat2a[1] LIKE 'TDE%' THEN 'Transport définitif ' || data_mat2a[3] WHEN data_mat2a[1] LIKE 'TSE%' THEN 'Transport provisoire ' || data_mat2a[3] ELSE data_mat2a[2] END  
    	FROM w_cp_imports
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		p_mat2a_data.file_code = 'T11TRANS' AND
    		p_mat2a_data.line_type = 'D' AND
    		mat2a_annee >= 2021;
  
  	UPDATE pmsi.p_mat2a_data SET
  		data_cti[7] = cti_nombre,
  		data_cti[8] = cti_base_remboursement
  	FROM w_transports_2021
  	WHERE file_code = 'T11TRANS' AND 
  		p_mat2a_data.mat2a_import_id = w_transports_2021.mat2a_import_id AND
  		data_mat2a[1] = w_transports_2021.prestation_code AND
  		data_mat2a[4] = w_transports_2021.mois
  	;
  
  	UPDATE pmsi.p_mat2a_data SET
  		data_cti[7] = cti_nombre,
  		data_cti[8] = cti_base_remboursement
  	FROM (SELECT mat2a_import_id, SUM(cti_nombre) AS cti_nombre, SUM(cti_base_remboursement) AS cti_base_remboursement FROM w_transports_2021 GROUP BY 1) w_transports_2021
  	WHERE file_code = 'T11TRANS' AND 
  		p_mat2a_data.mat2a_import_id = w_transports_2021.mat2a_import_id AND
  		p_mat2a_data.line_type = 'F'
  	;
  
  
  	UPDATE pmsi.p_mat2a_data SET 
  		data_cti[3] = 0,
  		data_cti[4] = 0,
  		data_cti[6] = 0,
  		data_cti[7] = 0 
    	FROM w_cp_imports
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		p_mat2a_data.file_code = 'T1V1TRANS' AND
    		table_sequence = 1 AND
    		p_mat2a_data.line_type IN ('D','F') AND
    		mat2a_annee >= 2021;
  
  
  	UPDATE pmsi.p_mat2a_data SET
  		data_cti[3] = cti_nombre,
  		data_cti[4] = cti_nombre_valorise,
  		data_cti[6] = cti_base_remboursement_valorise,
  		data_cti[7] = cti_montant_remboursable
  	FROM (
  	SELECT p_mat2a_data.oid,
  	    SUM(w_cp_cti_fact2a.cti_nombre) AS cti_nombre,
  	    SUM(CASE WHEN traitement_epmsi = '30' THEN w_cp_cti_fact2a.cti_nombre ELSE 0 END) AS cti_nombre_valorise,
  	    SUM(CASE WHEN traitement_epmsi = '30' THEN w_cp_cti_fact2a.cti_base_remboursement ELSE 0 END) AS cti_base_remboursement_valorise,
  	    SUM(CASE WHEN traitement_epmsi = '30' THEN w_cp_cti_fact2a.cti_montant_remboursable ELSE 0 END) AS cti_montant_remboursable
  	FROM w_cp_cti_fact2a
  	JOIN pmsi.p_mat2a_data ON
  	    w_cp_cti_fact2a.mat2a_import_id = p_mat2a_data.mat2a_import_id
  	JOIN pmsi.p_mat2a_import ON p_mat2a_data.mat2a_import_id = p_mat2a_import.oid
  	WHERE file_code = 'T1V1TRANS' AND table_sequence = 1 AND 
  	    p_mat2a_import.mat2a_annee >= 2021 AND
  	    line_type = 'D' AND substr(data_mat2a[1],1,3) || substr(data_mat2a[2],2,1) ILIKE prestation_code
  	GROUP BY 1) subview
  	WHERE file_code = 'T1V1TRANS' AND p_mat2a_data.oid = subview.oid;
  
  	UPDATE pmsi.p_mat2a_data SET
  		data_cti[3] = cti_nombre,
  		data_cti[4] = cti_nombre_valorise,
  		data_cti[6] = cti_base_remboursement_valorise,
  		data_cti[7] = cti_montant_remboursable
  	FROM (
  	SELECT p_mat2a_data.oid,
  	    SUM(w_cp_cti_fact2a.cti_nombre) AS cti_nombre,
  	    SUM(CASE WHEN traitement_epmsi = '30' THEN w_cp_cti_fact2a.cti_nombre ELSE 0 END) AS cti_nombre_valorise,
  	    SUM(CASE WHEN traitement_epmsi = '30' THEN w_cp_cti_fact2a.cti_base_remboursement ELSE 0 END) AS cti_base_remboursement_valorise,
  	    SUM(CASE WHEN traitement_epmsi = '30' THEN w_cp_cti_fact2a.cti_montant_remboursable ELSE 0 END) AS cti_montant_remboursable
  	FROM w_cp_cti_fact2a
  	JOIN pmsi.p_mat2a_data ON
  	    w_cp_cti_fact2a.mat2a_import_id = p_mat2a_data.mat2a_import_id
  	JOIN pmsi.p_mat2a_import ON p_mat2a_data.mat2a_import_id = p_mat2a_import.oid
  	WHERE file_code = 'T1V1TRANS' AND table_sequence = 1 AND 
  	    p_mat2a_import.mat2a_annee >= 2021 AND
  	    prestation_code IN ('TDE1', 'TDE2', 'TDE3', 'TDE4', 'TDE5', 'TSE6', 'TSE7', 'TSE8', 'TSE9') AND
  	    line_type = 'F' 
  	GROUP BY 1) subview
  	WHERE file_code = 'T1V1TRANS' AND p_mat2a_data.oid = subview.oid;
  
  
  	-- Transports - SU
  	UPDATE pmsi.p_mat2a_data SET 
  		data_cti[3] = 0,
  		data_cti[4] = 0
    	FROM w_cp_imports
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		p_mat2a_data.file_code = 'T1V1TRANS' AND
    		table_sequence = 2 AND
    		p_mat2a_data.line_type IN ('D','F') AND
    		mat2a_annee >= 2021;
  
  
  	UPDATE pmsi.p_mat2a_data SET
  		data_cti[3] = cti_nombre,
  		data_cti[4] = cti_montant_remboursable
  	FROM (
  	SELECT p_mat2a_data.oid,
  	    SUM(w_cp_cti_fact2a.cti_nombre) AS cti_nombre,
  	    SUM(w_cp_cti_fact2a.cti_montant_remboursable) AS cti_montant_remboursable
  	FROM w_cp_cti_fact2a
  	JOIN pmsi.p_mat2a_data ON
  	    w_cp_cti_fact2a.mat2a_import_id = p_mat2a_data.mat2a_import_id
  	JOIN pmsi.p_mat2a_import ON p_mat2a_data.mat2a_import_id = p_mat2a_import.oid
  	WHERE file_code = 'T1V1TRANS' AND table_sequence = 2 AND 
  	    p_mat2a_import.mat2a_annee >= 2021 AND
  	    traitement_epmsi = '33' AND
  	    line_type = 'D' AND substr(data_mat2a[1],1,3) || substr(data_mat2a[2],2,1) ILIKE prestation_code
  	GROUP BY 1) subview
  	WHERE file_code = 'T1V1TRANS' AND p_mat2a_data.oid = subview.oid;
  
  	UPDATE pmsi.p_mat2a_data SET
  		data_cti[3] = cti_nombre,
  		data_cti[4] = cti_montant_remboursable
  	FROM (
  	SELECT p_mat2a_data.oid,
  	    SUM(w_cp_cti_fact2a.cti_nombre) AS cti_nombre,
  	    SUM(w_cp_cti_fact2a.cti_montant_remboursable) AS cti_montant_remboursable
  	FROM w_cp_cti_fact2a
  	JOIN pmsi.p_mat2a_data ON
  	    w_cp_cti_fact2a.mat2a_import_id = p_mat2a_data.mat2a_import_id
  	JOIN pmsi.p_mat2a_import ON p_mat2a_data.mat2a_import_id = p_mat2a_import.oid
  	WHERE file_code = 'T1V1TRANS' AND table_sequence = 2 AND 
  	    p_mat2a_import.mat2a_annee >= 2021 AND
  	    traitement_epmsi = '33' AND
  	    prestation_code IN ('TDE1', 'TDE2', 'TDE3', 'TDE4', 'TDE5', 'TSE6', 'TSE7', 'TSE8', 'TSE9') AND
  	    line_type = 'F' 
  	GROUP BY 1) subview
  	WHERE file_code = 'T1V1TRANS' AND p_mat2a_data.oid = subview.oid;
  
  
      RAISE NOTICE '%' , 'Cumuls CTI. Facturation RAFAEL.';
  
    	IF NOT EXISTS (SELECT * FROM pg_class WHERE relnamespace = pg_my_temp_schema() AND relname = 'w_cp_cti_facrsf') THEN
    		DROP TABLE IF EXISTS w_cp_cti_facrsf;
    		CREATE TEMP TABLE w_cp_cti_facrsf (
    			import_id bigint, 
    			mat2a_import_id bigint,
    			mat2a_annee numeric,
    			dmt text,
    			type_ligne text,
    			prestation_chapitre text,
    			prestation_code text,
    			prestation_id bigint,
    			code_pec text,
    			prix_unitaire numeric,
    			cti_nombre numeric(13,2), 
    			cti_base_remboursement numeric(13,2),
    			cti_base_remboursement_sauf_transition numeric(13,2),
    			cti_montant_remboursable numeric(13,2),
    			cti_montant_remboursable_fides numeric(13,2),
    			cti_montant_remboursable_nonfides numeric(13,2),
  			est_valorise integer
    		);
    	END IF;
  
    	TRUNCATE w_cp_cti_facrsf;
    	-- Valorisé
    	INSERT INTO w_cp_cti_facrsf
    	SELECT p_rss.import_id,
    		mat2a_import_id,
    		mat2a_annee,
    		dmt,
    		p_rsf_detail.type AS type_ligne,
    		''::text AS prestation_chapitre,
    		t_prestations.code AS prestation_code,
    		prestation_id,
    		CASE WHEN traitement_epmsi = '30' THEN '1' ELSE '0' END AS code_pec,
    		prix_unitaire,
    		SUM(p_rsf_detail.nombre * p_rsf_detail.coefficient) AS cti_nombre,
    		SUM(p_rsf_detail.base_remboursement) AS cti_base_remboursement,
    		SUM(p_rsf_detail.base_remboursement_sauf_transition) AS cti_base_remboursement_sauf_transition,
    		SUM(p_rsf_detail.sejour_remboursable) AS cti_montant_remboursable,
    		SUM(CASE WHEN p_rsf_detail.fides_detail = 1 THEN p_rsf_detail.sejour_remboursable ELSE 0 END) AS cti_montant_remboursable_fides,
    		SUM(CASE WHEN p_rsf_detail.fides_detail <> 1 THEN p_rsf_detail.sejour_remboursable ELSE 0 END) AS cti_montant_remboursable_nonfides,
  		1 as est_valorise
    	FROM pmsi.p_rss
    	JOIN pmsi.p_rsf_detail ON p_rsf_detail.rss_id = p_rss.oid
    	JOIN pmsi.p_rsf_total ON p_rsf_total.rss_id = p_rss.oid
    	JOIN base.t_motif_non_facturation ON motif_non_facturation_id = t_motif_non_facturation.oid
    	JOIN pmsi.t_prestations ON prestation_id = t_prestations.oid
    	JOIN w_cp_imports ON w_cp_imports.import_id = p_rss.import_id
    	WHERE en_cours = '0' AND
    		ghm_id = 0 AND
    		est_ligne_rss = '1' 
    	GROUP BY 1,2,3,4,5,6,7,8,9,10;
    	-- Non Valorisé (on ignore les hors période)
    	INSERT INTO w_cp_cti_facrsf
    	SELECT p_rss.import_id,
    		mat2a_import_id,
    		mat2a_annee,
    		dmt,
    		p_rsf_detail.type AS type_ligne,
    		''::text AS prestation_chapitre,
    		t_prestations.code AS prestation_code,
    		prestation_id,
    		'0' AS code_pec,
    		prix_unitaire,
    		SUM(p_rsf_detail.nombre * p_rsf_detail.coefficient) AS cti_nombre,
    		SUM(p_rsf_detail.base_remboursement) AS cti_base_remboursement,
    		SUM(p_rsf_detail.base_remboursement_sauf_transition) AS cti_base_remboursement_sauf_transition,
    		SUM(p_rsf_detail.sejour_remboursable) AS cti_montant_remboursable,
    		0 as cti_montant_remboursable_fides,
    		0 as cti_montant_remboursable_nonfides,
  		0 as est_valorise
    	FROM pmsi.p_rss
    	JOIN pmsi.p_rsf_detail_non_valorise p_rsf_detail ON p_rsf_detail.rss_id = p_rss.oid
        JOIN pmsi.t_regles_non_valorisation ON t_regles_non_valorisation.oid = p_rsf_detail.regle_id
    	JOIN pmsi.t_prestations ON prestation_id = t_prestations.oid
    	JOIN w_cp_imports ON w_cp_imports.import_id = p_rss.import_id
    	WHERE en_cours = '0' AND
    		ghm_id = 0 AND
    		est_ligne_rss = '1' AND
            (t_regles_non_valorisation.code != '16' OR t_regles_non_valorisation.code = '16' AND t_prestations.code IN ('APU', 'APC', 'APV', 'APY', 'AVY') AND mat2a_annee = 2017)
    	GROUP BY 1,2,3,4,5,6,7,8,9,10;
    	-- Association chapitre 
    	UPDATE w_cp_cti_facrsf
    	SET prestation_chapitre = 'T2VVATU'
    	WHERE prestation_code = 'ATU';
    	UPDATE w_cp_cti_facrsf
    	SET prestation_chapitre = 'T2VVFFM'
    	WHERE prestation_code = 'FFM';
    	UPDATE w_cp_cti_facrsf
    	SET prestation_chapitre = 'T2VVDME'
    	WHERE prestation_code IN ('APE','PII','AP2');
      UPDATE w_cp_cti_facrsf
      SET prestation_chapitre = 'INNOVATION'
      WHERE prestation_code IN ('I01','I02','I03','I04','I05','I14','I15');
    	UPDATE w_cp_cti_facrsf
    	SET prestation_chapitre = 'T2VVDME', prestation_code = 'DMI'
    	WHERE prestation_id IN (SELECT prestation_defaut_id FROM pmsi.t_lpp)
  	; 
      UPDATE w_cp_cti_facrsf
      SET prestation_chapitre = 'URGENCE'
      WHERE prestation_code IN (SELECT split_part(data_mat2a[1],' ',1) AS prestation_code FROM pmsi.p_mat2a_data WHERE file_code IN ('T2VVFU') AND line_type = 'D' GROUP BY 1);
  
  
  
    	UPDATE w_cp_cti_facrsf
    	SET prestation_chapitre = file_code
    	FROM (
    		SELECT w_cp_cti_facrsf.prestation_code, file_code, est_valorise
    		FROM w_cp_cti_facrsf
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_facrsf.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE file_code IN ('T2VVNGAP','T2VVCCAM','T2VVFTN','T2VVSE') AND table_sequence = 1 AND
    			line_type = 'D' AND 
    			(
    				data_mat2a[1] || ' ' ILIKE prestation_code || ' %' OR
    				(1=1
  					AND split_part(data_mat2a[1], ' ', 1) = split_part(prestation_code, ' ', 1)
  					AND data_mat2a[1] LIKE ANY (ARRAY['% Férié', '% Nuit'])
  					AND prestation_code LIKE ANY (ARRAY['% F', '% N'])
  				) OR
    				data_mat2a[1] ILIKE '%participation assur%' AND data_mat2a[1] ILIKE '%18 euros%' AND prestation_code = 'PAE' OR
    				data_mat2a[1] ILIKE '%ticket mod%' AND data_mat2a[1] ILIKE '%rateur forfaitaire%' AND prestation_code = 'PAT'
    			) AND
    			prestation_chapitre = '' AND
    			est_valorise = 1
    		GROUP BY 1,2,3
    		ORDER BY 1,2
    		) subview
    	WHERE w_cp_cti_facrsf.prestation_code = subview.prestation_code AND
  		w_cp_cti_facrsf.est_valorise = subview.est_valorise;
  
    	-- Chapitre non pris en compte 
    	WITH subview AS (
            SELECT w_cp_cti_facrsf.prestation_code, file_code, est_valorise
    		FROM w_cp_cti_facrsf
    		JOIN pmsi.p_mat2a_data ON w_cp_cti_facrsf.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		JOIN w_seq_hpnpc ON w_cp_cti_facrsf.mat2a_import_id = w_seq_hpnpc.mat2a_import_id
    		WHERE file_code IN ('T2QHPNPC') AND table_sequence = w_seq_hpnpc.seq_hpnpc AND
    			line_type = 'D' AND 
    			(prestation_code ILIKE data_mat2a[1] OR prestation_code like '%**%') AND
    			prestation_chapitre = '' AND
  			est_valorise = 0
    		GROUP BY 1,2,3
    		ORDER BY 1,2
        )
        UPDATE w_cp_cti_facrsf
    	SET prestation_chapitre = subview.file_code
    	FROM subview
    	WHERE w_cp_cti_facrsf.prestation_code = subview.prestation_code AND
  		w_cp_cti_facrsf.est_valorise = subview.est_valorise;
    	RAISE NOTICE '%' , 'Cumuls CTI. Facturation RAFAEL. ATU.';
    	-- 2014 T2VVATU
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[3] = '0',
    		data_cti[4] = '0',
    		data_cti[5] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T2VVATU' AND 
    		table_sequence = 1 AND 
    		line_type IN ('F');
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[3] = cti_nombre,
    		data_cti[4] = cti_base_remboursement,
    		data_cti[5] = cti_montant_remboursable
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(cti_nombre) AS cti_nombre,
    			SUM(cti_base_remboursement) AS cti_base_remboursement,
    			SUM(cti_montant_remboursable) AS cti_montant_remboursable
    		FROM w_cp_cti_facrsf
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_facrsf.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE file_code = 'T2VVATU' AND table_sequence = 1 AND
    			line_type = 'F' AND
    			prestation_code = 'ATU' AND
    			code_pec = '1'
    		GROUP BY 1
    		) subview
    	WHERE file_code = 'T2VVATU' AND p_mat2a_data.oid = subview.oid;
    	RAISE NOTICE '%' , 'Cumuls CTI. Facturation RAFAEL. FFM.';
    	-- 2014 T2VVFFM
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[3] = '0',
    		data_cti[4] = '0',
    		data_cti[5] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T2VVFFM' AND 
    		table_sequence = 1 AND 
    		line_type IN ('F');
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[3] = cti_nombre,
    		data_cti[4] = cti_base_remboursement,
    		data_cti[5] = cti_montant_remboursable
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(cti_nombre) AS cti_nombre,
    			SUM(cti_base_remboursement) AS cti_base_remboursement,
    			SUM(cti_montant_remboursable) AS cti_montant_remboursable
    		FROM w_cp_cti_facrsf
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_facrsf.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE file_code = 'T2VVFFM' AND table_sequence = 1 AND
    			line_type = 'F' AND
    			prestation_code = 'FFM' AND
    			code_pec = '1'
    		GROUP BY 1
    		) subview
    	WHERE file_code = 'T2VVFFM' AND p_mat2a_data.oid = subview.oid;
  
    	RAISE NOTICE '%' , 'Cumuls CTI. Facturation RAFAEL. FTN-FTR.';
    	-- 2014 T2VVFTN
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[4] = '0',
    		data_cti[5] = '0',
    		data_cti[6] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T2VVFTN' AND 
    		table_sequence = 1 AND 
    		line_type IN ('D', 'F');
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[4] = cti_nombre,
    		data_cti[5] = cti_base_remboursement,
    		data_cti[6] = cti_montant_remboursable
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(cti_nombre) AS cti_nombre,
    			SUM(cti_base_remboursement) AS cti_base_remboursement,
    			SUM(cti_montant_remboursable) AS cti_montant_remboursable
    		FROM w_cp_cti_facrsf
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_facrsf.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE file_code = 'T2VVFTN' AND table_sequence = 1 AND
    			(line_type = 'D' AND 
    				data_mat2a[1] ILIKE prestation_code || '%' AND 
    				data_mat2a[2] ILIKE dmt || '%' AND dmt <> '' AND
    				data_mat2a[3] = prix_unitaire OR
    			line_type = 'D' AND 
    				data_mat2a[1] ILIKE prestation_code || '%' AND 
    				data_mat2a[2] ILIKE 'Non renseignée' AND dmt = '' AND
    				data_mat2a[3] = prix_unitaire OR
    			line_type = 'F') AND
    			prestation_chapitre = 'T2VVFTN' AND
    			code_pec = '1'
    		GROUP BY 1
    		) subview
    	WHERE file_code = 'T2VVFTN' AND p_mat2a_data.oid = subview.oid;
  
    	RAISE NOTICE '%' , 'Cumuls CTI. Facturation RAFAEL. SEH.';
    	-- 2014 : T2VVSE
  
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[3] = '0',
    		data_cti[4] = '0',
    		data_cti[5] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T2VVSE' AND 
    		table_sequence = 1 AND 
    		line_type IN ('F');
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[3] = cti_nombre,
    		data_cti[4] = cti_base_remboursement,
    		data_cti[5] = cti_montant_remboursable
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(cti_nombre) AS cti_nombre,
    			SUM(cti_base_remboursement) AS cti_base_remboursement,
    			SUM(cti_montant_remboursable) AS cti_montant_remboursable
    		FROM w_cp_cti_facrsf
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_facrsf.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE file_code = 'T2VVSE' AND table_sequence = 1 AND
    			line_type = 'F' AND
    			prestation_chapitre = 'T2VVSE' AND
    			code_pec = '1'
    		GROUP BY 1
    		) subview
    	WHERE file_code = 'T2VVSE' AND p_mat2a_data.oid = subview.oid;
  
    	RAISE NOTICE '%' , 'Cumuls CTI. Facturation RAFAEL. CCAM.';
    	-- 2014 : T2VVCCAM
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[2] = '0',
    		data_cti[3] = '0',
    		data_cti[4] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T2VVCCAM' AND 
    		table_sequence = 1 AND 
    		line_type IN ('D', 'F');
  
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[2] = cti_nombre,
    		data_cti[3] = cti_base_remboursement,
    		data_cti[4] = cti_montant_remboursable
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(cti_nombre) AS cti_nombre,
    			SUM(cti_base_remboursement) AS cti_base_remboursement,
    			SUM(cti_montant_remboursable) AS cti_montant_remboursable
    		FROM w_cp_cti_facrsf
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_facrsf.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE file_code = 'T2VVCCAM' AND table_sequence = 1 AND
    			(line_type = 'D' AND 
    				data_mat2a[1] ILIKE prestation_code || '%' OR
    			line_type = 'F') AND
    			prestation_chapitre = 'T2VVCCAM' AND
    			code_pec = '1'
    		GROUP BY 1
    		) subview
    	WHERE file_code = 'T2VVCCAM' AND p_mat2a_data.oid = subview.oid;
  
   RAISE NOTICE '%' , 'Cumuls CTI. Facturation RAFAEL. NGAP.';
    	-- 2014 : T2VVNGAP
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[3] = '0',
    		data_cti[4] = '0',
    		data_cti[5] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T2VVNGAP' AND 
    		table_sequence = 1 AND 
    		line_type IN ('D', 'F');
  
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[3] = cti_nombre,
    		data_cti[4] = cti_base_remboursement,
    		data_cti[5] = cti_montant_remboursable
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(CASE WHEN prestation_code <> 'PAT' AND prestation_code <> 'PAE' THEN cti_nombre ELSE 0 - cti_nombre END) AS cti_nombre,
    			SUM(cti_base_remboursement) AS cti_base_remboursement,
    			SUM(cti_montant_remboursable) AS cti_montant_remboursable
    		FROM w_cp_cti_facrsf
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_facrsf.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE file_code = 'T2VVNGAP' AND table_sequence = 1 AND
    			(
    				line_type = 'D' AND 
    				trim(substr(data_mat2a[1],1,5)) ILIKE prestation_code AND
    				abs(base.cti_to_number(data_mat2a[2])) = prix_unitaire 
  
    			OR
    				line_type = 'D' AND 
    				(
  				    data_mat2a[1] ILIKE '%Participation assur%' AND
  				    data_mat2a[1] ILIKE '%18 euros%'
  				OR
  				    data_mat2a[1] ILIKE '%ticket mod%' AND
  				    data_mat2a[1] ILIKE '%rateur forfaitaire%'
  				) AND
    				abs(base.cti_to_number(data_mat2a[2])) = prix_unitaire AND
    				prestation_code = 'PAT' 
    			OR
    				line_type = 'F'
    			) AND
    			prestation_chapitre = 'T2VVNGAP' AND
    			code_pec = '1'
    		GROUP BY 1
    		) subview
    	WHERE file_code = 'T2VVNGAP' AND p_mat2a_data.oid = subview.oid;
  
   RAISE NOTICE '%' , 'Cumuls CTI. Facturation RAFAEL. APE/DMI.';
      -- Numéro de table dans la page 2.V.VDME où apparaissent les prestations APE/AP2 externes
    	IF NOT EXISTS (SELECT * FROM pg_class WHERE relnamespace = pg_my_temp_schema() AND relname = 'w_seq_vdme') THEN
    		DROP TABLE IF EXISTS w_seq_vdme;
          CREATE TEMP TABLE w_seq_vdme (code_vdme text, seq_vdme bigint);
    	END IF;
    	TRUNCATE w_seq_vdme;
      INSERT INTO w_seq_vdme (code_vdme, seq_vdme)
      (
      SELECT
          'AP2', table_sequence
      FROM
          pmsi.p_mat2a_data
      WHERE 1=1
          AND file_code = 'T2VVDME'
          AND line_type = 'T'
          AND data_mat2a[2] like '%Valorisation des prestations AP2%'
      UNION
      SELECT
          'APE', table_sequence
      FROM
          pmsi.p_mat2a_data
      WHERE 1=1
          AND file_code = 'T2VVDME'
          AND line_type = 'T'
          AND data_mat2a[1] like '%Valorisation des prestations APE (DM)%'
      )
  	;
  
    	-- 2014 T2VVDME
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[3] = '0',
    		data_cti[4] = '0',
    		data_cti[5] = '0'
    	FROM w_cp_imports
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T2VVDME' AND 
    		table_sequence = ANY (ARRAY(SELECT seq_vdme FROM w_seq_vdme)::bigint[]) AND 
    		line_type IN ('F');
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[3] = cti_nombre,
    		data_cti[4] = cti_base_remboursement,
    		data_cti[5] = cti_montant_remboursable
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(cti_nombre) AS cti_nombre,
    			SUM(cti_base_remboursement) AS cti_base_remboursement,
    			SUM(cti_montant_remboursable) AS cti_montant_remboursable
    		FROM w_cp_cti_facrsf
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_facrsf.mat2a_import_id = p_mat2a_data.mat2a_import_id,
              w_seq_vdme
    		WHERE file_code = 'T2VVDME' AND
              w_seq_vdme.code_vdme = 'APE' AND
              table_sequence = w_seq_vdme.seq_vdme AND
    			line_type = 'F' AND
    			prestation_code = 'APE' AND
    			code_pec = '1'
    		GROUP BY 1
    		) subview
    	WHERE file_code = 'T2VVDME' AND p_mat2a_data.oid = subview.oid;
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[3] = cti_nombre,
    		data_cti[4] = cti_base_remboursement,
    		data_cti[5] = cti_montant_remboursable
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(cti_nombre) AS cti_nombre,
    			SUM(cti_base_remboursement) AS cti_base_remboursement,
    			SUM(cti_montant_remboursable) AS cti_montant_remboursable
    		FROM w_cp_cti_facrsf
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_facrsf.mat2a_import_id = p_mat2a_data.mat2a_import_id,
              w_seq_vdme
    		WHERE file_code = 'T2VVDME' AND
              w_seq_vdme.code_vdme = 'AP2' AND
              table_sequence = w_seq_vdme.seq_vdme AND
    			line_type = 'F' AND
    			prestation_code = 'AP2' AND
    			code_pec = '1'
    		GROUP BY 1
    		) subview
    	WHERE file_code = 'T2VVDME' AND p_mat2a_data.oid = subview.oid;
  
  
    	RAISE NOTICE '%' , 'Cumuls CTI. Facturation RAFAEL. Lettres cle non prises en charge.';
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[2] = NULL,
    		data_cti[3] = '0',
    		data_cti[4] = CASE WHEN w_cp_imports.mat2a_annee >= 2020 THEN NULL ELSE '0' END,
    		data_cti[5] = CASE WHEN w_cp_imports.mat2a_annee >= 2020 THEN NULL ELSE '0' END
    	FROM w_cp_imports
    	JOIN w_seq_hpnpc ON w_cp_imports.mat2a_import_id = w_seq_hpnpc.mat2a_import_id
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T2QHPNPC' AND 
    		table_sequence = w_seq_hpnpc.seq_hpnpc AND 
    		line_type IN ('D', 'F');
  
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[3] = cti_nombre,
    		data_cti[4] = cti_base_remboursement,
    		data_cti[5] = cti_montant_remboursable
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(cti_nombre) AS cti_nombre,
    			SUM(cti_base_remboursement) AS cti_base_remboursement,
    			SUM(cti_montant_remboursable) AS cti_montant_remboursable
    		FROM w_cp_cti_facrsf
    		JOIN pmsi.p_mat2a_data ON w_cp_cti_facrsf.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		JOIN w_seq_hpnpc ON w_cp_cti_facrsf.mat2a_import_id = w_seq_hpnpc.mat2a_import_id
    		WHERE file_code = 'T2QHPNPC' AND table_sequence = w_seq_hpnpc.seq_hpnpc AND
    			(line_type = 'D' AND 
    				(prestation_code ILIKE data_mat2a[1]  OR prestation_code ILIKE '%**%') AND
    				data_mat2a[2] = prix_unitaire OR
    			line_type = 'F') AND
    			prestation_chapitre = 'T2QHPNPC'
    		GROUP BY 1
    		) subview
    	WHERE file_code = 'T2QHPNPC' AND p_mat2a_data.oid = subview.oid;
    	-- Les montants Mat2a sont faux, il ne faut pas d'erreur si nombre identique
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[4] = data_mat2a[4],
    		data_cti[5] = data_mat2a[5]
  	FROM w_seq_hpnpc
    	WHERE w_seq_hpnpc.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
  		file_code = 'T2QHPNPC' AND 
    		table_sequence = w_seq_hpnpc.seq_hpnpc AND 
    		line_type IN ('D', 'F') AND
    		round(base.cti_to_number(data_cti[3]),0) = round(base.cti_to_number(data_mat2a[3]),0);
    	RAISE NOTICE '%' , 'Cumuls CTI. Facturation RAFAEL. Synthese';
    	-- 2014 : T2VRAEM
  
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[2] = '0',
    		data_cti[3] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T2VRAEM' AND 
    		table_sequence = 1 AND 
    		line_type IN ('D', 'F') AND
    		(
    			data_mat2a[1] = 'ATU' OR
    			data_mat2a[1] = 'FFM' OR
    			data_mat2a[1] = 'FTN' OR
    			data_mat2a[1] = 'CCAM' OR
    			data_mat2a[1] = 'SE' OR
    			data_mat2a[1] = 'NGAP' OR
    			data_mat2a[1] = 'AP2' OR
    			data_mat2a[1] = 'DMI' OR
    			data_mat2a[1] = 'MED'  OR
    			data_mat2a[1] = 'MOP' OR
          data_mat2a[1] = 'INNOVATION' OR
          data_mat2a[1] = 'URGENCE'
    		);
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[2] = cti_base_remboursement,
    		data_cti[3] = cti_montant_remboursable
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(cti_base_remboursement) AS cti_base_remboursement,
    			SUM(cti_montant_remboursable) AS cti_montant_remboursable
    		FROM w_cp_cti_facrsf
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_facrsf.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE file_code = 'T2VRAEM' AND table_sequence = 1 AND
    			(
    				prestation_chapitre = 'T2VVATU' AND data_mat2a[1] = 'ATU' OR
    				prestation_chapitre = 'T2VVFFM' AND data_mat2a[1] = 'FFM' OR
    				prestation_chapitre = 'T2VVFTN' AND data_mat2a[1] = 'FTN' OR
    				prestation_chapitre = 'T2VVCCAM' AND data_mat2a[1] = 'CCAM' OR
    				prestation_chapitre = 'T2VVSE' AND data_mat2a[1] = 'SE' OR
    				prestation_chapitre = 'T2VVNGAP' AND data_mat2a[1] = 'NGAP' OR
    				prestation_chapitre = 'T2VVDME' AND data_mat2a[1] = 'DMI' AND prestation_code = 'DMI' OR
    				prestation_chapitre = 'T2VVDME' AND data_mat2a[1] = 'AP2' OR
    				data_mat2a[1] = 'MOP' AND prestation_code = 'MOPHNG' OR
    				prestation_chapitre = 'INNOVATION' AND data_mat2a[1] = 'INNOVATION'OR
    				prestation_chapitre = 'URGENCE' AND data_mat2a[1] = 'URGENCE'
    			) AND
    			code_pec = '1'
    		GROUP BY 1
    		) subview
    	WHERE file_code = 'T2VRAEM' AND p_mat2a_data.oid = subview.oid;
  
  
    	-- Tableau T1V1CP (OQN)
  
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti = NULL
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T1V1CP' AND 
    		table_sequence = 2 AND 
    		(
    		line_type = 'D' OR
    		line_type = 'F' AND data_mat2a[1] ILIKE 'lib'
    		);
  
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[4] = cti_base_remboursement
  
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(w_cp_cti_facrsf.cti_base_remboursement_sauf_transition) AS cti_base_remboursement
    		FROM w_cp_cti_facrsf
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_facrsf.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE file_code = 'T1V1CP' AND table_sequence = 2 AND 
    			(
    			line_type = 'D' AND data_mat2a[1] ILIKE prestation_code OR
    			line_type = 'F' AND data_mat2a[1] ILIKE 'lib'
    			)
    		GROUP BY 1) subview
    	WHERE file_code = 'T1V1CP' AND p_mat2a_data.oid = subview.oid;
    	-- Création arrete versement
    	DELETE FROM pmsi.p_mat2a_data
    		USING w_cp_imports 
    		WHERE p_mat2a_data.mat2a_import_id = w_cp_imports.mat2a_import_id AND
    			file_code = 'VERSEMENT';
    		INSERT INTO pmsi.p_mat2a_data (
    			mat2a_import_id,
    			file_code,
    			table_sequence,
    			row_sequence,
    			line_type,
    			data_mat2a,
    			data_cti)
    		SELECT w_cp_imports.mat2a_import_id, 'VERSEMENT', 1, 0, 'T',
    			ARRAY[replace(data_mat2a[1],'1.V.1.RAV','VERSEMENT'), 
    			'Arrété versement', 
    			data_mat2a[3]] AS data_mat2a, 
    			data_cti
    		FROM pmsi.p_mat2a_data
    		JOIN w_cp_imports ON p_mat2a_data.mat2a_import_id = w_cp_imports.mat2a_import_id
    		WHERE file_code = 'T1V1RAV' AND table_sequence = 1 AND line_type IN ('T');
    		INSERT INTO pmsi.p_mat2a_data (
    			mat2a_import_id,
    			file_code,
    			table_sequence,
    			row_sequence,
    			line_type,
    			data_mat2a,
    			data_cti)
    		SELECT w_cp_imports.mat2a_import_id, 'VERSEMENT', 1, 0, 'H',
    			ARRAY['Rubrique', 
    			'Montant versé'] AS data_mat2a, 
    			data_cti
    		FROM pmsi.p_mat2a_data
    		JOIN w_cp_imports ON p_mat2a_data.mat2a_import_id = w_cp_imports.mat2a_import_id
    		WHERE file_code = 'T1V1RAV' AND table_sequence = 1 AND line_type IN ('H');
    		INSERT INTO pmsi.p_mat2a_data (
    			mat2a_import_id,
    			file_code,
    			table_sequence,
    			row_sequence,
    			line_type,
    			data_mat2a,
    			data_cti)
    		SELECT w_cp_imports.mat2a_import_id, 'VERSEMENT', 1, 1, 'D',
    			ARRAY['GHS y compris suppléments',data_mat2a[6]] AS data_mat2a, 
    			ARRAY[NULL::text,data_cti[6]] AS data_cti
    		FROM pmsi.p_mat2a_data
    		JOIN w_cp_imports ON p_mat2a_data.mat2a_import_id = w_cp_imports.mat2a_import_id
    		WHERE file_code = 'T1V1RAV' AND table_sequence = 1 AND data_mat2a[1] ILIKE '%Total valorisation%'
    		UNION
    		SELECT w_cp_imports.mat2a_import_id, 'VERSEMENT', 1, 2, 'D',
    			ARRAY['FFM',data_mat2a[3]] AS data_mat2a, 
    			ARRAY[NULL::text,data_cti[3]] AS data_cti
    		FROM pmsi.p_mat2a_data
    		JOIN w_cp_imports ON p_mat2a_data.mat2a_import_id = w_cp_imports.mat2a_import_id
    		WHERE file_code = 'T2VRAEM' AND table_sequence = 1 AND line_type = 'D' AND data_mat2a[1] = 'FFM'
    		UNION
    		SELECT w_cp_imports.mat2a_import_id, 'VERSEMENT', 1, 3, 'D',
    			ARRAY['IVG',data_mat2a[4]] AS data_mat2a, 
    			ARRAY[NULL::text,data_cti[4]] AS data_cti
    		FROM pmsi.p_mat2a_data
    		JOIN w_cp_imports ON p_mat2a_data.mat2a_import_id = w_cp_imports.mat2a_import_id
    		WHERE file_code = 'T1V1VIVG' AND table_sequence = 1 AND line_type = 'F'
    		UNION
    		SELECT w_cp_imports.mat2a_import_id, 'VERSEMENT', 1, 4, 'D',
    			ARRAY['Actes et consultations externes',SUM(base.cti_to_number(data_mat2a[3]))::text] AS data_mat2a, 
    			ARRAY[NULL::text,SUM(base.cti_to_number(data_cti[3]))::text] AS data_cti
    		FROM pmsi.p_mat2a_data
    		JOIN w_cp_imports ON p_mat2a_data.mat2a_import_id = w_cp_imports.mat2a_import_id
    		WHERE file_code = 'T2VRAEM' AND table_sequence = 1 AND line_type = 'D' AND data_mat2a[1] <> 'SE' AND data_mat2a[1] <> 'FFM'
    		GROUP BY 1
    		UNION
    		SELECT w_cp_imports.mat2a_import_id, 'VERSEMENT', 1, 5, 'D',
    			ARRAY['Forfait sécurité et environnement hospitalier',data_mat2a[3]] AS data_mat2a, 
    			ARRAY[NULL::text,data_cti[3]] AS data_cti
    		FROM pmsi.p_mat2a_data
    		JOIN w_cp_imports ON p_mat2a_data.mat2a_import_id = w_cp_imports.mat2a_import_id
    		WHERE file_code = 'T2VRAEM' AND table_sequence = 1 AND line_type = 'D' AND data_mat2a[1] = 'SE'
    		ORDER BY 1,4;
    		INSERT INTO pmsi.p_mat2a_data (
    			mat2a_import_id,
    			file_code,
    			table_sequence,
    			row_sequence,
    			line_type,
    			data_mat2a,
    			data_cti)
    		SELECT w_cp_imports.mat2a_import_id, 'VERSEMENT', 1, 9, 'F',
    			ARRAY['Total Prestations d''hospitalisation',SUM(base.cti_to_number(data_mat2a[2]))::text] AS data_mat2a, 
    			ARRAY[NULL::text,SUM(base.cti_to_number(data_cti[2]))::text] AS data_cti
    		FROM pmsi.p_mat2a_data
    		JOIN w_cp_imports ON p_mat2a_data.mat2a_import_id = w_cp_imports.mat2a_import_id
    		WHERE file_code = 'VERSEMENT' AND table_sequence = 1 AND line_type = 'D'
    		GROUP BY 1;
    		INSERT INTO pmsi.p_mat2a_data (
    			mat2a_import_id,
    			file_code,
    			table_sequence,
    			row_sequence,
    			line_type,
    			data_mat2a,
    			data_cti)
    		SELECT w_cp_imports.mat2a_import_id, 'VERSEMENT', 1, 11, 'D',
    			ARRAY['Médicaments',data_mat2a[6]] AS data_mat2a, 
    			ARRAY[NULL::text,data_cti[6]] AS data_cti
    		FROM pmsi.p_mat2a_data
    		JOIN w_cp_imports ON p_mat2a_data.mat2a_import_id = w_cp_imports.mat2a_import_id
    		WHERE file_code = 'T1V2VMED' AND table_sequence = 2 AND line_type = 'F'
    		UNION
    		SELECT w_cp_imports.mat2a_import_id, 'VERSEMENT', 1, 12, 'D',
    			ARRAY['DMI',data_mat2a[6]] AS data_mat2a, 
    			ARRAY[NULL::text,data_cti[6]] AS data_cti
    		FROM pmsi.p_mat2a_data
    		JOIN w_cp_imports ON p_mat2a_data.mat2a_import_id = w_cp_imports.mat2a_import_id
    		WHERE file_code = 'T1V3VDMI' AND table_sequence = 2 AND line_type = 'F'
    		UNION
    		SELECT w_cp_imports.mat2a_import_id, 'VERSEMENT', 1, 21, 'D',
    			ARRAY['Activité AME',data_mat2a[2]] AS data_mat2a, 
    			ARRAY[NULL::text,data_cti[2]] AS data_cti
    		FROM pmsi.p_mat2a_data
    		JOIN w_cp_imports ON p_mat2a_data.mat2a_import_id = w_cp_imports.mat2a_import_id
    		WHERE file_code = 'T1V4RAME' AND table_sequence = 4 AND data_mat2a[1] ILIKE '%Total valorisation%'
    		ORDER BY 1,4;
    		INSERT INTO pmsi.p_mat2a_data (
    			mat2a_import_id,
    			file_code,
    			table_sequence,
    			row_sequence,
    			line_type,
    			data_mat2a,
    			data_cti)
    		SELECT w_cp_imports.mat2a_import_id, 'VERSEMENT', 1, 99, 'F',
    			ARRAY['Total Général',SUM(base.cti_to_number(data_mat2a[2]))::text] AS data_mat2a, 
    			ARRAY[NULL::text,SUM(base.cti_to_number(data_cti[2]))::text] AS data_cti
    		FROM pmsi.p_mat2a_data
    		JOIN w_cp_imports ON p_mat2a_data.mat2a_import_id = w_cp_imports.mat2a_import_id
    		WHERE file_code = 'VERSEMENT' AND table_sequence = 1 AND line_type = 'D'
    		GROUP BY 1;
  
  
  
  
    	-- 2014 : T2VFIDES
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[4] = '0',
    		data_cti[5] = '0'
    	FROM w_cp_imports 
    	WHERE w_cp_imports.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
    		file_code = 'T2VFIDES' AND 
    		table_sequence = 2 AND 
    		line_type IN ('D');
  
    	UPDATE pmsi.p_mat2a_data SET
    		data_cti[4] = cti_montant_remboursable_nonfides,
    		data_cti[5] = cti_montant_remboursable_fides
    	FROM (
    		SELECT p_mat2a_data.oid,
    			SUM(cti_montant_remboursable_fides) AS cti_montant_remboursable_fides,
    			SUM(cti_montant_remboursable_nonfides) AS cti_montant_remboursable_nonfides
    		FROM w_cp_cti_facrsf
    		JOIN pmsi.p_mat2a_data ON
    			w_cp_cti_facrsf.mat2a_import_id = p_mat2a_data.mat2a_import_id
    		WHERE file_code = 'T2VFIDES' AND 
  			table_sequence = 2 AND 
  			line_type IN ('D') AND
     			(
     			prestation_code <> 'MOP' AND prestation_code ILIKE trim(split_part(data_mat2a[1],' ',1)) || '%' OR
     			prestation_chapitre = 'T2VVCCAM' AND data_mat2a[1] = 'CCAM' OR
     			prestation_chapitre = 'T2VVNGAP' AND data_mat2a[1] = 'NGAP' OR
     			prestation_chapitre = 'T2VVFTN' AND data_mat2a[1] = 'FTN' OR
     			prestation_code = 'MOPHNG' AND data_mat2a[1] = 'MOP' OR
          prestation_chapitre = 'INNOVATION' AND data_mat2a[1] = 'INNOVATION' OR
          prestation_chapitre = 'URGENCE' AND data_mat2a[1] = 'URGENCE'
     			) AND
    			code_pec = '1'
    		GROUP BY 1
    		) subview
    	WHERE file_code = 'T2VFIDES' AND p_mat2a_data.oid = subview.oid;
  
  
    	-- Mise à jour des classes et listes
    	DROP TABLE IF EXISTS w_prestations;
  		CREATE TEMP TABLE w_prestations AS
  		SELECT t_prestations.code, t_prestations.oid AS prestation_id, 
  			CASE file_code
  			WHEN 'T2VVFTN'::text THEN '14'
  			WHEN 'T2VVSE'::text THEN '16'
  			WHEN 'T2VVCCAM'::text THEN '15'
  			WHEN 'T2VVNGAP'::text THEN '17'
  			WHEN 'T2VVFU'::text THEN '26'
  			ELSE '' END AS prestation_section_code
  		FROM pmsi.t_prestations
  		JOIN
  		(
  			SELECT 
  				file_code,
  				CASE 
  				WHEN data_mat2a[1] ILIKE 'participation assu%' THEN 'PAE'
  				WHEN data_mat2a[1] ILIKE 'ticket mod%' THEN 'PAT'
  				WHEN data_mat2a[1] LIKE 'FSD %' THEN 'FSD'
  				WHEN substr(data_mat2a[1],5,1) IN ('F','N') THEN left(data_mat2a[1],5) 
  				ELSE split_part(data_mat2a[1],' ',1) END AS prestation_code,
  				0::bigint AS prestation_id
  			FROM pmsi.p_mat2a_data
  			WHERE file_code IN ('T2VVCCAM','T2VVNGAP','T2VVFTN','T2VVSE','T2VVFU') AND
  				line_type = 'D'
  			GROUP BY 1,2
  			ORDER BY 1,2
  		) subview ON
  			t_prestations.code = subview.prestation_code
  			;
  		INSERT INTO w_prestations
  		SELECT t_prestations.code, t_prestations.oid AS prestation_id, '23' AS prestation_section_code
  		FROM pmsi.t_prestations
  		WHERE code = 'MOPHNG'
  		;
  
  	DELETE FROM pmsi.t_classes_sections_elements
  	USING
  	(
  	SELECT section_id, to_id
  	FROM w_prestations
  	JOIN (SELECT t_classes_sections.code AS section_code, section_id, to_id
  		FROM pmsi.t_classes
  		JOIN pmsi.t_classes_sections ON classe_id = t_classes.oid
  		JOIN pmsi.t_classes_sections_elements ON section_id = t_classes_sections.oid
  		WHERE t_classes.code = 'CTI_RAFA_P' 
  	) subview ON prestation_id = to_id
  	WHERE section_code <> prestation_section_code AND 
  		prestation_section_code <> ''
  	) subview
  	WHERE t_classes_sections_elements.section_id = subview.section_id AND
  		t_classes_sections_elements.to_id = subview.to_id
  	;
  
  	INSERT INTO pmsi.t_classes_sections_elements(section_id, to_id)
  	SELECT t_classes_sections.oid, prestation_id
  	FROM w_prestations
  	JOIN pmsi.t_classes_sections ON prestation_section_code = t_classes_sections.code
  	JOIN pmsi.t_classes ON classe_id = t_classes.oid
  	WHERE t_classes.code = 'CTI_RAFA_P' AND
  		(t_classes_sections.oid::text || '-' || prestation_id::text) NOT IN (SELECT section_id::text || '-' || to_id::text FROm pmsi.t_classes_sections_elements)
  	;
  
  
    	RETURN 'OK';
    END;
