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_aj_imports') THEN
  		DROP TABLE IF EXISTS w_aj_imports;
  		CREATE TEMP TABLE w_aj_imports (import_id bigint, mat2a_import_id bigint, mat2a_annee numeric, mat2a_mois numeric);
  	END IF;
  	TRUNCATE w_aj_imports;
  	
  	INSERT INTO w_aj_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 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;
  	-- Import à traiter ?
  	IF NOT EXISTS (SELECT * FROM w_aj_imports LIMIT 1) THEN
  		return 'Pas d''import à traiter';
  	END IF;
  	RAISE NOTICE '%' , 'Comparaison';			
  	PERFORM pmsi.cti_compare_with_mat2a(i_import_id, i_mat2a_import_id);
  	-- Import à traiter ?
  	IF NOT EXISTS (SELECT *
  		FROM pmsi.p_mat2a_data
  		JOIN pmsi.p_mat2a_import ON p_mat2a_data.mat2a_import_id = p_mat2a_import.oid
  		JOIN pmsi.p_imports ON p_imports.mat2a_import_id = p_mat2a_import.oid
  		JOIN w_aj_imports ON w_aj_imports.import_id = p_imports.oid
  		WHERE file_code = 'T1V1RAV'
  		 LIMIT 1) THEN
  		return 'Ajustement seulement sur public';
  	END IF ;
  	
  	--RAISE NOTICE '%' , 'Recherche Limitation SRC';
  	--PERFORM pmsi.cti_limitation_sc();
  
  	-- Table des RSS à recumuler
  	DROP TABLE IF EXISTS w_aj_cum_rss;
  	CREATE TEMP TABLE w_aj_cum_rss (rss_id bigint);
  
  
  	RAISE NOTICE '%' , 'Lettres cles non valorisee';
  	DROP TABLE IF EXISTS w_aj_non_valorise;
  	CREATE TEMP TABLE w_aj_non_valorise AS 
  	SELECT p_rsf_detail.CTID AS fromCTID, p_rsf_detail.rss_id
  	FROM pmsi.p_rsf_detail
  	JOIN pmsi.t_prestations ON prestation_id = t_prestations.oid
  	JOIN pmsi.p_rss ON rss_id = p_rss.oid
  	JOIN w_aj_imports ON p_rss.import_id = w_aj_imports.import_id
  	JOIN
  	(
  		SELECT w_aj_imports.import_id, p_mat2a_data.data_mat2a[1] AS prestation_code
  		FROM pmsi.p_mat2a_data
  		JOIN w_aj_imports ON p_mat2a_data.mat2a_import_id = w_aj_imports.mat2a_import_id
  		LEFT JOIN pmsi.p_mat2a_data p_mat2a_data_ngap ON
  			p_mat2a_data_ngap.mat2a_import_id = p_mat2a_data.mat2a_import_id AND
  			p_mat2a_data_ngap.file_code = 'T2VVNGAP' AND
  			p_mat2a_data_ngap.data_mat2a[1] LIKE p_mat2a_data.data_mat2a[1] || '%'
  		WHERE p_mat2a_data.file_code = 'T2QHPNPC' AND
  			p_mat2a_data.line_type = 'D' AND
  			p_mat2a_data_ngap.file_code IS NULL
  		GROUP BY 1,2
  	) subview
  	ON p_rss.import_id = subview.import_id AND
  		t_prestations.code = subview.prestation_code AND
  		p_rss.ghm_id <= 0 AND
  		subview.prestation_code NOT IN ('FTN','FTR','FSD','ATU','SE1','SE2','SE3','SE4','SE5','SE6','SE7')
  	;
  
  
  	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, coefficient_pacte_resp, no_ligne)
  	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, 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, t_regles_non_valorisation.oid AS regle_id, coefficient_pacte_resp, no_ligne
  	FROM pmsi.p_rsf_detail
  	JOIN pmsi.t_regles_non_valorisation ON t_regles_non_valorisation.code = '09'
  	JOIN w_aj_non_valorise ON 
  		w_aj_non_valorise.rss_id = p_rsf_detail.rss_id AND
  		w_aj_non_valorise.fromCTID = p_rsf_detail.CTID
  	;
  
  
  	DELETE FROM pmsi.p_rsf_detail
  	USING w_aj_non_valorise
  	WHERE w_aj_non_valorise.rss_id = p_rsf_detail.rss_id AND
  		w_aj_non_valorise.fromCTID = p_rsf_detail.CTID
  	;
  
  	INSERT INTO w_aj_cum_rss
  	SELECT w_aj_non_valorise.rss_id
  	FROM w_aj_non_valorise
  	WHERE w_aj_non_valorise.rss_id NOT IN (SELECT w_aj_cum_rss.rss_id FROM w_aj_cum_rss)
  	GROUP BY 1
  	;
  
  	
  	RAISE NOTICE '%' , 'Analyse ecarts prestations';
  	
  	DROP TABLE IF EXISTS w_aj_mat2a;
  	CREATE TEMP TABLE w_aj_mat2a AS
  	SELECT CASE WHEN MAX(base.cti_to_number(data_mat2a[5])) - SUM(CASE WHEN fides_detail = '1' THEN p_rsf_detail.sejour_remboursable ELSE 0 END) >= 0 THEN 0.01 ELSE -0.01 END AS ajout_fides_p,
  		(abs(MAX(base.cti_to_number(data_mat2a[5])) - SUM(CASE WHEN fides_detail = '1' THEN p_rsf_detail.sejour_remboursable ELSE 0 END))*100)::integer AS ajout_fides_n
  	FROM pmsi.p_rsf_detail
  	JOIN pmsi.p_rss ON rss_id = p_rss.oid
  	JOIN w_aj_imports ON p_rss.import_id = w_aj_imports.import_id
  	JOIN pmsi.t_prestations ON prestation_id = t_prestations.oid
  	JOIN pmsi.p_mat2a_data ON
  		p_mat2a_data.mat2a_import_id = w_aj_imports.mat2a_import_id AND
  		p_mat2a_data.file_code = 'T2VFIDES' AND
  		table_sequence = 2 AND
  		data_mat2a[1] = 'CCAM'
  	WHERE traitement_epmsi = '30' AND
  		t_prestations.code IN ('ACO', 'ADA', 'ADC', 'ADE', 'ADI', 'ATM', 'DEN')
  	HAVING abs(SUM(p_rsf_detail.sejour_remboursable) - MAX(base.cti_to_number(data_mat2a[4])) - MAX(base.cti_to_number(data_mat2a[5]))) < 10
  	;
  
  
  
  
  
  
  
  
  
  
  
  
  	DROP TABLE IF EXISTS w_aj_mat2a;
  	CREATE TEMP TABLE w_aj_mat2a AS
  	SELECT
  		p_imports.oid AS import_id,
  		CASE 
  			WHEN file_code = 'T1V1VIVG' THEN '32'
  			ELSE '30' END AS traitement_epmsi,
  		CASE 
  		WHEN file_code = 'T01_STC' AND table_sequence = 1 AND line_type= 'D' AND 
  			data_mat2a[1] ILIKE '%Nb de RSA transmis%' THEN '*SEJ'
  		WHEN file_code = 'T34_STC' AND table_sequence = 3 AND line_type= 'D' AND 
  			data_mat2a[1] ILIKE '%Valorisation des GHS%' THEN 'GHS'
  		WHEN file_code = 'T34_STC' AND table_sequence = 3 AND line_type= 'D' AND 
  			data_mat2a[1] ILIKE '%Valorisation extrême bas%' THEN 'GHS'
  		WHEN file_code = 'T34_STC' AND table_sequence = 3 AND line_type= 'D' AND 
  			data_mat2a[1] ILIKE '%Valorisation séjours avec rehosp dans même GHM%' THEN 'GHS'
  		WHEN file_code = 'T34_STC' AND table_sequence = 3 AND line_type= 'D' AND 
  			data_mat2a[1] ILIKE '%Valorisation journées extrême haut%' THEN 'EXH'
  		WHEN file_code = 'T34_STC' AND table_sequence = 3 AND line_type= 'D' AND 
  			data_mat2a[1] ILIKE '%Valorisation suppléments antepartum%' THEN 'ANT'
  		WHEN file_code = 'T34_STC' AND table_sequence = 3 AND line_type= 'D' AND 
  			data_mat2a[1] ILIKE '%Valorisation suppléments de surveillance continue validés%' THEN 'SRC'
  	
  		WHEN file_code = 'T1V5SYNTS' AND table_sequence = 1 AND line_type= 'D' AND 
  			data_mat2a[1] ILIKE '%Séjours transmis%' THEN '*SEJ'
  		WHEN file_code = 'T1V1RAV' AND table_sequence = 1 AND line_type= 'D' AND 
  			data_mat2a[1] ILIKE '%Valorisation des GHS%' THEN 'GHS'
  		WHEN file_code = 'T1V1RAV' AND table_sequence = 1 AND line_type= 'D' AND 
  			data_mat2a[1] ILIKE '%Valorisation extrême bas%' THEN 'GHS'
  		WHEN file_code = 'T1V1RAV' AND table_sequence = 1 AND line_type= 'D' AND 
  			data_mat2a[1] ILIKE '%Valorisation séjours avec rehosp dans même GHM%' THEN 'GHS'
  		WHEN file_code = 'T1V1RAV' AND table_sequence = 1 AND line_type= 'D' AND 
  			data_mat2a[1] ILIKE '%Valorisation journées extrême haut%' THEN 'EXH'
  		WHEN file_code = 'T1V1RAV' AND table_sequence = 1 AND line_type= 'D' AND 
  			data_mat2a[1] ILIKE '%Valorisation suppléments de néonat sans SI%' THEN 'NN1'
  		WHEN file_code = 'T1V1RAV' AND table_sequence = 1 AND line_type= 'D' AND 
  			data_mat2a[1] ILIKE '%Valorisation suppléments antepartum%' THEN 'ANT'
  		WHEN file_code = 'T1V1RAV' AND table_sequence = 1 AND line_type= 'D' AND 
  			data_mat2a[1] ILIKE '%Valorisation suppléments de surveillance continue validés%' THEN 'SRC'
  		WHEN file_code = 'T1V1RAV' AND table_sequence = 1 AND line_type= 'D' AND 
  			data_mat2a[1] ILIKE '%Valorisation suppléments de soins intensifs validés%' THEN 'STF'
  		WHEN file_code = 'T1V1RAV' AND table_sequence = 1 AND line_type= 'D' AND 
  			data_mat2a[1] ILIKE '%Valorisation suppléments de réanimation%' THEN 'REA'
  		WHEN file_code = 'T2VVSE' AND table_sequence = 1 AND line_type = 'D' THEN substr(data_mat2a[1],1,3)  
  		WHEN file_code = 'T2VVFFM' AND table_sequence = 1 AND line_type = 'F' THEN 'FFM'
  		WHEN file_code = 'T2VVCCAM' AND table_sequence = 1 AND line_type= 'D' THEN substr(data_mat2a[1],1,3) 
  		WHEN file_code = 'T2VVNGAP' AND table_sequence = 1 AND line_type= 'D' THEN trim(substr(data_mat2a[1],1,5))
  		WHEN file_code = 'T2VVFTN' AND table_sequence = 1 AND line_type= 'D' THEN substr(data_mat2a[1],1,3) 
  		WHEN file_code = 'T2VVATU' AND table_sequence = 1 AND line_type = 'F' THEN 'ATU'
  		WHEN file_code = 'T2QHPNPC' AND table_sequence = 1 AND line_type= 'D' THEN data_mat2a[1]
  		WHEN file_code = 'T1V1VIVG' AND table_sequence = 1 AND line_type = 'D' THEN substr(data_mat2a[1],1,3) 
  		ELSE '' END AS prestation_code,
  		0::bigint AS prestation_id,
  		CASE 
  			WHEN file_code = 'T2VVSE' AND table_sequence = 1 THEN base.cti_to_number(data_mat2a[2])
  			WHEN file_code = 'T2VVFFM' AND table_sequence = 1 AND line_type = 'F' THEN 0
  			WHEN file_code = 'T2VVNGAP' AND table_sequence = 1 THEN base.cti_to_number(data_mat2a[2])
  			WHEN file_code = 'T2VVFTN' AND table_sequence = 1 THEN base.cti_to_number(data_mat2a[3])
  			WHEN file_code = 'T2QHPNPC' AND table_sequence = 1 THEN base.cti_to_number(data_mat2a[2])
  			ELSE 0 END::numeric AS prix_unitaire,
  		CASE 
  			WHEN file_code = 'T2VVFTN' AND table_sequence = 1 THEN data_mat2a[2]
  			ELSE '' END::text AS dmt,
  		MAX(
  			CASE 
  			WHEN file_code = 'T2VVSE' AND table_sequence = 1 AND line_type = 'D' THEN 'SE'
  			WHEN file_code = 'T2VVFFM' AND table_sequence = 1 AND line_type = 'F' THEN 'FFM'
  			WHEN file_code = 'T2VVNGAP' AND table_sequence = 1 AND line_type = 'D' THEN 'NGAP'
  			WHEN file_code = 'T2VVCCAM' AND table_sequence = 1 AND line_type = 'D' THEN 'CCAM'
  			WHEN file_code = 'T2VVFTN' AND table_sequence = 1 AND line_type = 'D' THEN 'FTN'
  			WHEN file_code = 'T2VVATU' AND table_sequence = 1 AND line_type = 'D' THEN 'ATU'
  			ELSE '' END::text) AS chapitre,
  			
  	SUM(CASE 
  		WHEN file_code = 'T1V5SYNTS' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[2]) 
  		WHEN file_code = 'T01_STC' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[2]) 
  		WHEN file_code = 'T2VVSE' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[3]) 
  		WHEN file_code = 'T2VVFFM' AND table_sequence = 1 AND line_type = 'F' THEN base.cti_to_number(data_mat2a[3]) 
  		WHEN file_code = 'T2VVCCAM' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[2]) 
  		WHEN file_code = 'T2VVNGAP' AND table_sequence = 1 AND line_type= 'D' THEN CASE WHEN data_mat2a[1] NOT ILIKE '%Participation%' THEN base.cti_to_number(data_mat2a[3]) ELSE 0-base.cti_to_number(data_mat2a[3])END 
  		WHEN file_code = 'T2VVFTN' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[4]) 
  		WHEN file_code = 'T2VVATU' AND table_sequence = 1 AND line_type = 'F' THEN base.cti_to_number(data_mat2a[3]) 
  		WHEN file_code = 'T2QHPNPC' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[3]) 
  		WHEN file_code = 'T1V1VIVG' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[2]) 
  		ELSE 0 END) AS MAT2A_nombre,
  	SUM(CASE 
  		WHEN file_code = 'T1V1RAV' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[4]) 
  		WHEN file_code = 'T34_STC' AND table_sequence = 3 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[4]) 
  		WHEN file_code = 'T2VVSE' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[4]) 
  		WHEN file_code = 'T2VVFFM' AND table_sequence = 1 AND line_type = 'F' THEN base.cti_to_number(data_mat2a[4]) 
  		WHEN file_code = 'T2VVCCAM' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[3]) 
  		WHEN file_code = 'T2VVNGAP' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[4]) 
  		WHEN file_code = 'T2VVFTN' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[5]) 
  		WHEN file_code = 'T2VVATU' AND table_sequence = 1 AND line_type = 'F' THEN base.cti_to_number(data_mat2a[4]) 
  		WHEN file_code = 'T2QHPNPC' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[4]) 
  		WHEN file_code = 'T1V1VIVG' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[3]) 
  		ELSE 0 END) AS MAT2A_base_remboursement_sauf_transition,
  	SUM(CASE 
  		WHEN file_code = 'T1V1RAV' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[5]) 
  		WHEN file_code = 'T34_STC' AND table_sequence = 3 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[5]) 
  		WHEN file_code = 'T2VVSE' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[4]) 
  		WHEN file_code = 'T2VVFFM' AND table_sequence = 1 AND line_type = 'F' THEN base.cti_to_number(data_mat2a[4]) 
  		WHEN file_code = 'T2VVCCAM' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[3]) 
  		WHEN file_code = 'T2VVNGAP' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[4])
  		WHEN file_code = 'T2VVFTN' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[5])
  		WHEN file_code = 'T2VVATU' AND table_sequence = 1 AND line_type = 'F' THEN base.cti_to_number(data_mat2a[4])  
  		WHEN file_code = 'T2QHPNPC' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[4])
  		WHEN file_code = 'T1V1VIVG' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[3]) 
  		ELSE 0 END) AS MAT2A_base_remboursement,
  	SUM(CASE 
  		WHEN file_code = 'T1V1RAV' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[6]) 
  		WHEN file_code = 'T34_STC' AND table_sequence = 3 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[6]) 
  		WHEN file_code = 'T2VVSE' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[5]) 
  		WHEN file_code = 'T2VVFFM' AND table_sequence = 1 AND line_type = 'F' THEN base.cti_to_number(data_mat2a[5]) 
  		WHEN file_code = 'T2VVCCAM' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[4]) 
  		WHEN file_code = 'T2VVNGAP' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[5]) 
  		WHEN file_code = 'T2VVFTN' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[6]) 
  		WHEN file_code = 'T2VVATU' AND table_sequence = 1 AND line_type = 'F' THEN base.cti_to_number(data_mat2a[5])  
  		WHEN file_code = 'T2QHPNPC' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[5]) 
  		WHEN file_code = 'T1V1VIVG' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_mat2a[4]) 
  		ELSE 0 END) AS MAT2A_montant_remboursable,
  	0::numeric AS MAT2A_nombre_limitation,
  	0::numeric AS MAT2A_montant_limitation,
  	SUM(CASE 
  		WHEN file_code = 'T1V5SYNTS' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[2]) 
  		WHEN file_code = 'T01_STC' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[2]) 
  		WHEN file_code = 'T2VVSE' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[3]) 
  		WHEN file_code = 'T2VVFFM' AND table_sequence = 1 AND line_type = 'F' THEN base.cti_to_number(data_cti[3]) 
  		WHEN file_code = 'T2VVCCAM' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[2]) 
  		WHEN file_code = 'T2VVNGAP' AND table_sequence = 1 AND line_type= 'D' THEN CASE WHEN data_mat2a[1] NOT ILIKE '%Participation%' THEN base.cti_to_number(data_cti[3]) ELSE 0-base.cti_to_number(data_cti[3])END 
  		WHEN file_code = 'T2VVFTN' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[4]) 
  		WHEN file_code = 'T2VVATU' AND table_sequence = 1 AND line_type = 'F' THEN base.cti_to_number(data_cti[3]) 
  		WHEN file_code = 'T2QHPNPC' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[3]) 
  		WHEN file_code = 'T1V1VIVG' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[2]) 
  		ELSE 0 END) AS CTI_nombre,
  	SUM(CASE 
  		WHEN file_code = 'T1V1RAV' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[4]) 
  		WHEN file_code = 'T34_STC' AND table_sequence = 3 AND line_type= 'D' THEN base.cti_to_number(data_cti[4]) 
  		WHEN file_code = 'T2VVSE' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[4]) 
  		WHEN file_code = 'T2VVFFM' AND table_sequence = 1 AND line_type = 'F' THEN base.cti_to_number(data_cti[4]) 
  		WHEN file_code = 'T2VVCCAM' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[3]) 
  		WHEN file_code = 'T2VVNGAP' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[4]) 
  		WHEN file_code = 'T2VVFTN' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[5]) 
  		WHEN file_code = 'T2VVATU' AND table_sequence = 1 AND line_type = 'F' THEN base.cti_to_number(data_cti[4]) 
  		WHEN file_code = 'T2QHPNPC' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[4]) 
  		WHEN file_code = 'T1V1VIVG' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[3]) 
  		ELSE 0 END) AS CTI_base_remboursement_sauf_transition,
  	SUM(CASE 
  		WHEN file_code = 'T1V1RAV' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[5]) 
  		WHEN file_code = 'T34_STC' AND table_sequence = 3 AND line_type= 'D' THEN base.cti_to_number(data_cti[5]) 
  		WHEN file_code = 'T2VVSE' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[4]) 
  		WHEN file_code = 'T2VVFFM' AND table_sequence = 1 AND line_type = 'F' THEN base.cti_to_number(data_cti[4]) 
  		WHEN file_code = 'T2VVCCAM' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[3]) 
  		WHEN file_code = 'T2VVNGAP' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[4])
  		WHEN file_code = 'T2VVFTN' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[5])
  		WHEN file_code = 'T2VVATU' AND table_sequence = 1 AND line_type = 'F' THEN base.cti_to_number(data_cti[4])  
  		WHEN file_code = 'T2QHPNPC' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[4])
  		WHEN file_code = 'T1V1VIVG' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[3]) 
  		ELSE 0 END) AS CTI_base_remboursement,
  	SUM(CASE 
  		WHEN file_code = 'T1V1RAV' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[6]) 
  		WHEN file_code = 'T34_STC' AND table_sequence = 3 AND line_type= 'D' THEN base.cti_to_number(data_cti[6]) 
  		WHEN file_code = 'T2VVSE' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[5]) 
  		WHEN file_code = 'T2VVFFM' AND table_sequence = 1 AND line_type = 'F' THEN base.cti_to_number(data_cti[5]) 
  		WHEN file_code = 'T2VVCCAM' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[4]) 
  		WHEN file_code = 'T2VVNGAP' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[5]) 
  		WHEN file_code = 'T2VVFTN' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[6]) 
  		WHEN file_code = 'T2VVATU' AND table_sequence = 1 AND line_type = 'F' THEN base.cti_to_number(data_cti[5])  
  		WHEN file_code = 'T2QHPNPC' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[5]) 
  		WHEN file_code = 'T1V1VIVG' AND table_sequence = 1 AND line_type= 'D' THEN base.cti_to_number(data_cti[4]) 
  		ELSE 0 END) AS CTI_montant_remboursable
  	FROM pmsi.p_mat2a_data
  		JOIN pmsi.p_mat2a_import ON p_mat2a_data.mat2a_import_id = p_mat2a_import.oid
  		JOIN pmsi.p_imports ON p_imports.mat2a_import_id = p_mat2a_import.oid
  		JOIN w_aj_imports ON w_aj_imports.import_id = p_imports.oid
  		WHERE line_type IN ('D','F') AND
  			p_mat2a_import.mat2a_annee >= 2012
  	GROUP BY 1,2,3,4,5,6;
  
  	UPDATE w_aj_mat2a
  	SET prestation_code = 'PAT'
  	WHERE prestation_code = 'Ticke' AND
  		chapitre = 'NGAP'
  	;
  
  	UPDATE w_aj_mat2a SET
  		MAT2A_nombre_limitation = subview.nombre_limitation,
  		MAT2A_montant_limitation = subview.montant_limitation
  	FROM
  	(
  	SELECT w_aj_imports.import_id, 
  		SUM(base.cti_to_number(data_mat2a[2])) AS nombre_limitation, 
  		SUM(base.cti_to_number(data_mat2a[3])) AS montant_limitation
  	FROM pmsi.p_mat2a_data
  	JOIN pmsi.p_mat2a_import ON p_mat2a_data.mat2a_import_id = p_mat2a_import.oid
  	JOIN pmsi.p_imports ON p_imports.mat2a_import_id = p_mat2a_import.oid
  	JOIN w_aj_imports ON w_aj_imports.import_id = p_imports.oid
  	WHERE 
  		file_code = 'T1V1SV' AND 
  		table_sequence = 1 AND 
  		line_type= 'D' AND
  		data_mat2a[1] ILIKE '%suppression des SI/SC en cas de limitation%'
  	GROUP BY 1
  	) subview
  	WHERE w_aj_mat2a.import_id = subview.import_id AND
  		w_aj_mat2a.prestation_code = 'SRC'
  	;
  	
  	RAISE NOTICE '%' , 'Imports eligibles';
  	DELETE FROM w_aj_imports 
  	WHERE import_id IN
  	(
  	SELECT p_rss.import_id
  	FROM pmsi.p_rss
  	JOIN pmsi.t_ghm_c ON p_rss.ghm_id = t_ghm_c.oid
  	JOIN w_aj_imports ON w_aj_imports.import_id = p_rss.import_id
  	JOIN w_aj_mat2a ON w_aj_mat2a.import_id = p_rss.import_id AND prestation_code = '*SEJ'
  	WHERE en_cours = '0' AND
  		ghm_id <> 0
  	GROUP BY 1
  	HAVING SUM(CASE WHEN t_ghm_c.cmd_code <> '28' OR mat2a_annee <= 2012 THEN 1 ELSE nb_seances END) <> MAX(MAT2A_nombre) 
  	);
  	-- Import à traiter ?
  	IF NOT EXISTS (SELECT * FROM w_aj_imports LIMIT 1) THEN
  		return 'Pas d''import eligible à traiter (nombre transmis différent)';
  	END IF;
  	UPDATE w_aj_mat2a
  	SET prestation_id = t_prestations.oid
  	FROM pmsi.t_prestations 
  	WHERE t_prestations.code = prestation_code;
  
  	UPDATE w_aj_mat2a
  	SET prestation_id = t_prestations.oid,
  		prestation_code = 'PAT'
  	FROM pmsi.t_prestations 
  	WHERE prestation_code ILIKE 'Participation' AND t_prestations.code = 'PAT';
  
  
  	
  	RAISE NOTICE '%' , 'Limitations SRC';
  
  	DROP TABLE IF EXISTS w_aj_limsrc;
  	CREATE TEMP TABLE w_aj_limsrc AS
  	SELECT i, 
  		0::bigint AS rss_id, 
  		0::bigint AS taux_remboursement,
  		0::numeric AS coefficient_geo,
  		0::numeric AS coefficient_mco,
  		0::numeric AS coefficient_pacte_resp,
  		subview.*
  	FROM 
  	(
  	SELECT import_id,
  		mat2a_nombre_limitation,
  		(cti_base_remboursement-mat2a_base_remboursement) AS ecart_br,
  		(cti_base_remboursement_sauf_transition-mat2a_base_remboursement_sauf_transition) AS ecart_br_ht,
  		(cti_montant_remboursable-mat2a_montant_remboursable) AS ecart_remb,
  		round((cti_base_remboursement-mat2a_base_remboursement)/mat2a_nombre_limitation,2) AS pu_br, 
  		round((cti_base_remboursement_sauf_transition-mat2a_base_remboursement_sauf_transition)/mat2a_nombre_limitation,2) AS pu_br_ht, 
  		round((cti_montant_remboursable-mat2a_montant_remboursable)/mat2a_nombre_limitation,2) AS pu_remb
  	FROM w_aj_mat2a
  	WHERE prestation_code = 'SRC' AND
  		mat2a_nombre_limitation <> 0 
  	) subview
  	JOIN generate_series(1,(mat2a_nombre_limitation-1)::integer) i ON 1=1
  	WHERE ecart_br <> 0 OR ecart_br_ht <> 0 OR ecart_remb <> 0
  	;
  	
  	INSERT INTO w_aj_limsrc
  	SELECT MAX(mat2a_nombre_limitation) AS i,
  		0::bigint AS rss_id, 
  		0::bigint AS taux_remboursement,
  		0::numeric AS coefficient_geo,
  		0::numeric AS coefficient_mco,
  		0::numeric AS coefficient_pacte_resp,
  		import_id,
  		MAX(mat2a_nombre_limitation),
  		0::numeric,
  		0::numeric,
  		0::numeric,
  		MAX(ecart_br)-SUM(pu_br) AS pu_br,
  		MAX(ecart_br_ht)-SUM(pu_br_ht) AS pu_br_ht,
  		MAX(ecart_remb)-SUM(pu_remb) AS pu_remb
  	FROM w_aj_limsrc
  	WHERE ecart_br <> 0 OR ecart_br_ht <> 0 OR ecart_remb <> 0
  	GROUP BY import_id
  	;
  
  	UPDATE w_aj_limsrc SET
  		rss_id = subview.rss_id,
  		taux_remboursement = subview.taux_remboursement,
  		coefficient_geo = subview.coefficient_geo,
  		coefficient_mco = subview.coefficient_mco,
  		coefficient_pacte_resp = subview.coefficient_pacte_resp
  	FROM
  	(
  	SELECT import_id, 
  		rss_id,
  		taux_remboursement,
  		coefficient_geo,
  		coefficient_mco,
  		coefficient_pacte_resp, 
  		nombre, 
  		SUM(nombre_avant) OVER (PARTITION BY import_id ORDER BY date_sortie DESC, rss_id) + 1 AS index_d,
  		SUM(nombre) OVER (PARTITION BY import_id ORDER BY date_sortie DESC, rss_id) AS index_f
  	FROM
  	(
  	SELECT w_aj_mat2a.import_id,
  		rss_id, 
  		date_sortie,
  		p_rsf_detail.taux_remboursement,
  		p_rsf_detail.coefficient_geo,
  		p_rsf_detail.coefficient_mco,
  		p_rsf_detail.coefficient_pacte_resp,
  		COALESCE(LAG(nombre) OVER (PARTITION BY w_aj_mat2a.import_id ORDER BY p_rss.date_sortie DESC, rss_id),0) AS nombre_avant,
  		nombre
  	FROM pmsi.p_rsf_detail
  	JOIN pmsi.p_rss ON rss_id = p_rss.oid
  	JOIN pmsi.t_prestations ON prestation_id = t_prestations.oid
  	JOIN w_aj_mat2a ON 
  		w_aj_mat2a.import_id = p_rss.import_id AND
  		w_aj_mat2a.prestation_code = 'SRC' AND
  		w_aj_mat2a.mat2a_montant_limitation <> 0
  	WHERE p_rss.traitement_epmsi = '30' AND
  		t_prestations.code = 'SRC' AND 
  		est_ligne_rss = '1'
  		
  	) subview
  	) subview
  	WHERE w_aj_limsrc.import_id = subview.import_id AND
  		w_aj_limsrc.i BETWEEN index_d AND index_f
  
  		;
  
  
  	
  
  	INSERT INTO pmsi.p_rsf_detail (
  		rss_id,
  		no_rss,
  		date_debut,
  		date_fin,
  		prestation_id,
  		prix_unitaire,
  		taux_remboursement,
  		base_remboursement_sauf_transition,
  		base_remboursement,
  		sejour_remboursable,
  		coefficient_geo,
  		coefficient_mco,
  		coefficient_pacte_resp,
  		coefficient,
  		nombre
  	)
  	SELECT
  		rss_id,
  		MAX(no_rss) AS no_rss,
  		MIN(date_entree) AS date_debut,
  		MIN(date_sortie) AS date_fin,
  		MAX(t_prestations.oid) AS prestation_id,
  		MAX(pu_br_ht) AS prix_unitaire,
  		MAX(taux_remboursement) AS taux_remboursement,
  		0-SUM(pu_br_ht) AS base_remboursement_sauf_transition,
  		0-SUM(pu_br) AS base_remboursement,
  		0-SUM(pu_remb) AS sejour_remboursable,
  		MAX(coefficient_geo),
  		MAX(coefficient_mco),
  		MAX(coefficient_pacte_resp),
  		1::numeric AS coefficient,
  		0-count(*) AS nombre
  	FROM w_aj_limsrc
  	JOIN pmsi.p_rss ON rss_id = p_rss.oid
  	JOIN pmsi.t_prestations ON t_prestations.code = 'LIM_SRC'
  	GROUP BY 1
  	;
  
  	INSERT INTO w_aj_cum_rss
  	SELECT w_aj_limsrc.rss_id
  	FROM w_aj_limsrc
  	WHERE w_aj_limsrc.rss_id NOT IN (SELECT w_aj_cum_rss.rss_id FROM w_aj_cum_rss)
  	GROUP BY 1
  	;
  
  	
  	RAISE NOTICE '%' , 'Ajustement prestations';
  	
  	DROP TABLE IF EXISTS w_aj_ecarts;
  	CREATE TEMP TABLE w_aj_ecarts AS
  	SELECT
  		import_id,
  		traitement_epmsi,
  		prestation_id, 
  		prestation_code,
  		prix_unitaire,
  		dmt,
  		MAT2A_nombre,
  		CTI_nombre,
  		CTI_nombre - MAT2A_nombre AS ecart_nombre,
  		MAT2A_base_remboursement,
  		CTI_base_remboursement,
  		CTI_base_remboursement - MAT2A_base_remboursement AS ecart_base_remboursement,
  		CTI_base_remboursement_sauf_transition - MAT2A_base_remboursement_sauf_transition AS ecart_base_remboursement_sauf_transition,
  		CTI_montant_remboursable - MAT2A_montant_remboursable AS ecart_montant_remboursable
  	FROM w_aj_mat2a
  	WHERE prestation_id <> 0 AND prestation_code not in ('SE1','SE2','SE3','SE4','SE5','SE6','SE7') AND
  		(
  			CTI_base_remboursement IS DISTINCT FROM MAT2A_base_remboursement OR 
  			CTI_base_remboursement_sauf_transition IS DISTINCT FROM MAT2A_base_remboursement_sauf_transition OR 
  			CTI_montant_remboursable IS DISTINCT FROM MAT2A_montant_remboursable
  		) AND
  		(
  			ABS(CTI_nombre - MAT2A_nombre) < 1 AND
  			(
  				ABS(CTI_base_remboursement - MAT2A_base_remboursement) < 50 OR 
  				ABS(base.cti_division(ABS(CTI_base_remboursement - MAT2A_base_remboursement), CTI_base_remboursement))  < 0.01
  			) AND
  			(
  				ABS(CTI_base_remboursement_sauf_transition - MAT2A_base_remboursement_sauf_transition) < 50 OR 
  				ABS(base.cti_division(ABS(CTI_base_remboursement - MAT2A_base_remboursement), CTI_base_remboursement))  < 0.01
  			) AND
  			(
  				ABS(base.cti_division(ABS(CTI_montant_remboursable - MAT2A_montant_remboursable), CTI_montant_remboursable))  < 0.01 OR
  				ABS(CTI_montant_remboursable - MAT2A_montant_remboursable) < 1000
  			)
  		) OR
  		prestation_code = 'PAT'
  		
  	ORDER BY 2;
  
  	ANALYSE w_aj_ecarts
  	;
  	
  	RAISE NOTICE '%' , 'Ajustement prestations valorisees';
  	IF EXISTS (SELECT * FROM w_aj_ecarts WHERE prestation_code <> 'PAT' LIMIT 1) THEN
  
  		-- 1 centime par dossier en commencant par ceux qui on des arrondis
  		DROP TABLE IF EXISTS w_aj_ecarts_cti;
  		CREATE TEMP TABLE w_aj_ecarts_cti AS
  		SELECT *
  		FROM
  		(
  		SELECT p_rss.import_id,
  			w_aj_ecarts.traitement_epmsi AS ecart_traitement_epmsi,
  			w_aj_ecarts.prestation_id AS ecart_prestation_id,
  			w_aj_ecarts.prestation_code AS ecart_prestation_code,
  			w_aj_ecarts.prix_unitaire AS ecart_prix_unitaire,
  			w_aj_ecarts.dmt AS ecart_dmt,
  			ecart_nombre,
  			CASE WHEN ecart_base_remboursement = 0 THEN 0 WHEN ecart_base_remboursement > 0 THEN -0.01 ELSE 0.01 END AS ajout_base_remboursement_p,
  			CASE WHEN ecart_base_remboursement_sauf_transition = 0 THEN 0 WHEN ecart_base_remboursement_sauf_transition > 0 THEN -0.01 ELSE 0.01 END AS ajout_base_remboursement_sauf_transition_p,
  			CASE WHEN ecart_montant_remboursable = 0 THEN 0 WHEN ecart_montant_remboursable > 0 THEN -0.01 ELSE 0.01 END AS ajout_montant_remboursable_p,
  			ABS(ecart_base_remboursement*100)::integer AS ajout_base_remboursement_n,
  			ABS(ecart_base_remboursement_sauf_transition*100)::integer AS ajout_base_remboursement_sauf_transition_n,
  			ABS(ecart_montant_remboursable*100)::integer AS ajout_montant_remboursable_n,
  			ecart_base_remboursement,
  			ecart_base_remboursement_sauf_transition,
  			ecart_montant_remboursable,
  			rss_id AS ecart_rss_id,
  			row_number() OVER (
  				PARTITION BY w_aj_ecarts.CTID 
  				ORDER BY abs(round(p_rsf_detail.nombre*p_rsf_detail.coefficient*p_rsf_detail.coefficient_mco*p_rsf_detail.prix_unitaire,3) - p_rsf_detail.base_remboursement) DESC, p_rsf_detail.base_remboursement DESC
  				) AS row_br,
  			row_number() OVER (
  				PARTITION BY w_aj_ecarts.CTID 
  				ORDER BY abs(round(p_rsf_detail.nombre*p_rsf_detail.coefficient*p_rsf_detail.coefficient_mco*p_rsf_detail.prix_unitaire*p_rsf_detail.taux_remboursement/100,3) - p_rsf_detail.sejour_remboursable) DESC, p_rsf_detail.base_remboursement DESC
  				) AS row_remb,
  			p_rsf_detail.CTID AS ajust_CTID
  		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_aj_ecarts ON w_aj_ecarts.import_id = p_rss.import_id AND
  			w_aj_ecarts.traitement_epmsi = p_rss.traitement_epmsi AND
  			w_aj_ecarts.prestation_id = p_rsf_detail.prestation_id AND
  			(w_aj_ecarts.prix_unitaire = 0 OR w_aj_ecarts.prix_unitaire = p_rsf_detail.prix_unitaire) AND
  			(w_aj_ecarts.dmt = '' OR split_part(w_aj_ecarts.dmt,' ',1) = p_rsf_detail.dmt)
  		WHERE en_cours = '0' AND
  			est_ligne_rss = '1' AND 
  			t_prestations.code <> 'PAT'
  		) subview
  		WHERE row_br <= ajout_base_remboursement_n OR
  			row_br <= ajout_base_remboursement_sauf_transition_n OR
  			row_remb <= ajout_montant_remboursable_n
  		;
  
  		ANALYSE w_aj_ecarts_cti
  		;
  
  		UPDATE pmsi.p_rsf_detail SET
  			base_remboursement = base_remboursement + CASE WHEN row_br <= ajout_base_remboursement_n THEN ajout_base_remboursement_p ELSE 0 END,
  			base_remboursement_sauf_transition = base_remboursement_sauf_transition + CASE WHEN row_br <= ajout_base_remboursement_sauf_transition_n THEN ajout_base_remboursement_sauf_transition_p ELSE 0 END,
  			sejour_remboursable = sejour_remboursable + CASE WHEN row_remb <= ajout_montant_remboursable_n THEN ajout_montant_remboursable_p ELSE 0 END
  		FROM w_aj_ecarts_cti
  		WHERE p_rsf_detail.rss_id = w_aj_ecarts_cti.ecart_rss_id AND
  			p_rsf_detail.CTID = ajust_CTID
  		;
  		
  		
  		-- RSS a recalculer
  		INSERT INTO w_aj_cum_rss
  		SELECT ecart_rss_id 
  		FROM w_aj_ecarts_cti
  		WHERE ecart_rss_id NOT IN (SELECT rss_id FROM w_aj_cum_rss)
  		GROUP BY 1;
  
  
  
  		-- Ecarts en masse
  		IF 1 <> 1 THEN
  		DROP TABLE IF EXISTS w_aj_ecarts_cti;
  		CREATE TEMP TABLE w_aj_ecarts_cti AS
  		SELECT p_rss.import_id,
  			w_aj_ecarts.traitement_epmsi AS ecart_traitement_epmsi,
  			w_aj_ecarts.prestation_id AS ecart_prestation_id,
  			w_aj_ecarts.prestation_code AS ecart_prestation_code,
  			w_aj_ecarts.prix_unitaire AS ecart_prix_unitaire,
  			w_aj_ecarts.dmt AS ecart_dmt,
  			count(*) AS nombre_lignes,
  			(MAX(array[p_rsf_detail.base_remboursement_sauf_transition , p_rsf_detail.rss_id]))[2] AS ecart_rss_id,
  			MAX(ecart_nombre) AS ecart_nombre,
  			MAX(ecart_base_remboursement) AS ecart_base_remboursement,
  			MAX(ecart_base_remboursement_sauf_transition) AS ecart_base_remboursement_sauf_transition,
  			MAX(ecart_montant_remboursable) AS ecart_montant_remboursable,
  			NULL::tid AS ecart_ctid
  		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_aj_ecarts ON w_aj_ecarts.import_id = p_rss.import_id AND
  			w_aj_ecarts.traitement_epmsi = p_rss.traitement_epmsi AND
  			w_aj_ecarts.prestation_id = p_rsf_detail.prestation_id AND
  			(w_aj_ecarts.prix_unitaire = 0 OR w_aj_ecarts.prix_unitaire = p_rsf_detail.prix_unitaire) AND
  			(w_aj_ecarts.dmt = '' OR w_aj_ecarts.dmt = p_rsf_detail.dmt)
  		WHERE en_cours = '0' AND
  			est_ligne_rss = '1'
  		GROUP BY 1,2,3,4,5,6;
  
  		UPDATE w_aj_ecarts_cti
  		SET ecart_ctid = p_rsf_detail.CTID
  		FROM pmsi.p_rsf_detail
  		WHERE p_rsf_detail.rss_id = ecart_rss_id AND
  			p_rsf_detail.prestation_id = ecart_prestation_id AND
  			(ecart_prix_unitaire = 0 OR ecart_prix_unitaire = p_rsf_detail.prix_unitaire) AND
  			(ecart_dmt = '' OR ecart_dmt = p_rsf_detail.dmt) AND
  			est_ligne_rss = '1' ;
  			
  
  		UPDATE pmsi.p_rsf_detail SET
  			base_remboursement = base_remboursement - ecart_base_remboursement,
  			base_remboursement_sauf_transition = base_remboursement_sauf_transition - ecart_base_remboursement_sauf_transition,
  			sejour_remboursable = sejour_remboursable - ecart_montant_remboursable,
  			update_rules = array_append(p_rsf_detail.update_rules, 101)
  		FROM w_aj_ecarts_cti
  		WHERE p_rsf_detail.CTID = ecart_ctid AND ecart_prestation_code <> 'PAT';
  		
  		-- RSS a recalculer
  		INSERT INTO w_aj_cum_rss
  		SELECT ecart_rss_id 
  		FROM w_aj_ecarts_cti
  		WHERE ecart_rss_id NOT IN (SELECT rss_id FROM w_aj_cum_rss)
  		GROUP BY 1;
  
  		END IF;
  	END IF;
  	RAISE NOTICE '%' , 'Ajustement prestations non valorisees';
  	IF EXISTS (SELECT * FROM w_aj_ecarts WHERE prestation_code <> 'PAT' LIMIT 1) THEN
  		DROP TABLE IF EXISTS w_aj_ecarts_cti;
  		CREATE TEMP TABLE w_aj_ecarts_cti AS
  		SELECT p_rss.import_id,
  			w_aj_ecarts.traitement_epmsi AS ecart_traitement_epmsi,
  			w_aj_ecarts.prestation_id AS ecart_prestation_id,
  			w_aj_ecarts.prestation_code AS ecart_prestation_code,
  			w_aj_ecarts.prix_unitaire AS ecart_prix_unitaire,
  			w_aj_ecarts.dmt AS ecart_dmt,
  			count(*) AS nombre_lignes,
  			(MAX(array[p_rsf_detail.base_remboursement_sauf_transition , p_rsf_detail.rss_id]))[2] AS ecart_rss_id,
  			MAX(ecart_nombre) AS ecart_nombre,
  			MAX(ecart_base_remboursement) AS ecart_base_remboursement,
  			MAX(ecart_base_remboursement_sauf_transition) AS ecart_base_remboursement_sauf_transition,
  			MAX(ecart_montant_remboursable) AS ecart_montant_remboursable,
  			NULL::tid AS ecart_ctid
  		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_prestations ON prestation_id = t_prestations.oid
  		JOIN w_aj_ecarts ON w_aj_ecarts.import_id = p_rss.import_id AND
  			w_aj_ecarts.traitement_epmsi = p_rss.traitement_epmsi AND
  			w_aj_ecarts.prestation_id = p_rsf_detail.prestation_id AND
  			(w_aj_ecarts.prix_unitaire = 0 OR w_aj_ecarts.prix_unitaire = p_rsf_detail.prix_unitaire) AND
  			(w_aj_ecarts.dmt = '' OR w_aj_ecarts.dmt = p_rsf_detail.dmt)
  		WHERE en_cours = '0' AND
  			est_ligne_rss = '1'
  		GROUP BY 1,2,3,4,5,6;
  		UPDATE w_aj_ecarts_cti
  		SET ecart_ctid = p_rsf_detail.CTID
  		FROM pmsi.p_rsf_detail_non_valorise p_rsf_detail
  		WHERE p_rsf_detail.rss_id = ecart_rss_id AND
  			p_rsf_detail.prestation_id = ecart_prestation_id AND
  			(ecart_prix_unitaire = 0 OR ecart_prix_unitaire = p_rsf_detail.prix_unitaire) AND
  			(ecart_dmt = '' OR ecart_dmt = p_rsf_detail.dmt) AND
  			est_ligne_rss = '1' ;
  		UPDATE pmsi.p_rsf_detail_non_valorise p_rsf_detail SET
  			base_remboursement = base_remboursement - ecart_base_remboursement,
  			base_remboursement_sauf_transition = base_remboursement_sauf_transition - ecart_base_remboursement_sauf_transition,
  			sejour_remboursable = sejour_remboursable - ecart_montant_remboursable
  		FROM w_aj_ecarts_cti
  		WHERE p_rsf_detail.CTID = ecart_ctid AND ecart_prestation_code <> 'PAT';
  	END IF;
  	RAISE NOTICE '%' , 'Ajustement forfait PAT';
  	-- Trop de PAT, suppression aléatoire
  	IF EXISTS (SELECT * FROM w_aj_ecarts WHERE prestation_code = 'PAT' AND ecart_nombre < 0 LIMIT 1) THEN
  		DROP SEQUENCE IF EXISTS w_aj_ecarts_pat_rss_seq;
  		CREATE TEMP SEQUENCE w_aj_ecarts_pat_rss_seq;
  		DROP TABLE IF EXISTS w_aj_ecarts_pat_rssd;
  		CREATE TEMP TABLE w_aj_ecarts_pat_rssd AS
  		SELECT
  			nextval('w_aj_ecarts_pat_rss_seq'::regclass) AS sequence,
  			subview.*
  		FROM (
  			SELECT p_rss.oid AS ecart_rss_id, 
  				w_aj_ecarts.prestation_id,
  				p_rsf_detail.CTID AS delCTID,
  				p_rss.no_rss,
  				p_rss.base_remboursement AS montant,
  				0 - ecart_nombre AS ecart
  			FROM pmsi.p_rss
  			JOIN w_aj_ecarts ON w_aj_ecarts.import_id = p_rss.import_id AND prestation_code = 'PAT' AND ecart_nombre < 0
  			JOIN pmsi.p_rsf_detail ON p_rss.oid = p_rsf_detail.rss_id
  			JOIN pmsi.t_prestations ON p_rsf_detail.prestation_id = t_prestations.oid
  			WHERE p_rss.traitement_epmsi = '30' AND
  				t_prestations.code = 'PAT' AND
  				est_ligne_rss = '1'
  			ORDER BY 4 DESC) subview
  		;
  		DELETE FROM pmsi.p_rsf_detail
  		USING w_aj_ecarts_pat_rssd
  		WHERE w_aj_ecarts_pat_rssd.ecart_rss_id = p_rsf_detail.rss_id AND
  			w_aj_ecarts_pat_rssd.delCTID = p_rsf_detail.CTID AND
  			w_aj_ecarts_pat_rssd.sequence <= w_aj_ecarts_pat_rssd.ecart;
  		-- RSS a recalculer
  		INSERT INTO w_aj_cum_rss
  		SELECT ecart_rss_id 
  		FROM w_aj_ecarts_pat_rssd
  		WHERE w_aj_ecarts_pat_rssd.sequence <= w_aj_ecarts_pat_rssd.ecart AND
  			ecart_rss_id NOT IN (SELECT rss_id FROM w_aj_cum_rss)
  		GROUP BY 1;
  	END IF;
  	-- pas assez de PAT, ajouts
  	IF EXISTS (SELECT * FROM w_aj_ecarts WHERE prestation_code = 'PAT' AND ecart_nombre > 0 LIMIT 1) THEN
  		DROP SEQUENCE IF EXISTS w_aj_ecarts_pat_rss_seq;
  		CREATE TEMP SEQUENCE w_aj_ecarts_pat_rss_seq;
  		DROP TABLE IF EXISTS w_aj_ecarts_pat_rss;
  		CREATE TEMP TABLE w_aj_ecarts_pat_rss AS
  		SELECT 0::bigint AS sequence,
  			p_rss.oid AS ecart_rss_id, 
  			w_aj_ecarts.prestation_id,
  			p_rss.no_rss,
  			p_rss.date_entree,
  			p_rss.date_sortie,
              CASE WHEN p_rss.date_sortie >= '20190101'::date THEN 24 ELSE 18 END as montant_pat,
  			MIN(CASE WHEN t_prestations.code IN ('SE1', 'SE2', 'SE3', 'SE4', 'SE5', 'SE6', 'SE7', 'ATU', 'PAT', 'PAE') THEN '0' ELSE '1' END) AS eligible, 
  			SUM(CASE WHEN t_prestations.code NOT IN ('SE1', 'SE2', 'SE3', 'SE4', 'SE5', 'SE6', 'SE7', 'ATU', 'PAT', 'PAE') THEN p_rss.base_remboursement ELSE 0 END) AS montant
  		FROM pmsi.p_rss
  		JOIN w_aj_ecarts ON w_aj_ecarts.import_id = p_rss.import_id AND prestation_code = 'PAT' AND ecart_nombre > 0
  		JOIN pmsi.p_rsf_total ON p_rss.oid = p_rsf_total.rss_id
  		JOIN base.t_pec ON code_pec_id = t_pec.oid
  		JOIN base.t_exoneration_tm ON exoneration_tm_id = t_exoneration_tm.oid
  		JOIN pmsi.p_rsf_detail ON p_rss.oid = p_rsf_detail.rss_id
  		JOIN pmsi.t_prestations ON p_rsf_detail.prestation_id = t_prestations.oid
  		WHERE p_rss.traitement_epmsi = '30' AND
  			t_pec.code = '1' AND
  			t_exoneration_tm.code = '0' AND
  			pat_benef_cmu IS DISTINCT FROM '1' AND
  			est_ligne_rss = '1' AND ghm_id = 0
  		GROUP BY 1,2,3,4,5,6,7
  		HAVING MIN(CASE WHEN t_prestations.code IN ('SE1', 'SE2', 'SE3', 'SE4', 'SE5', 'SE6', 'SE7', 'ATU', 'PAT', 'PAE') THEN '0' ELSE '1' END) = '1' 
  		ORDER BY 8 DESC
  		LIMIT 1000;
  		UPDATE w_aj_ecarts_pat_rss
  		SET sequence = nextval('w_aj_ecarts_pat_rss_seq'::regclass);
  		DELETE FROM w_aj_ecarts_pat_rss
  		WHERE sequence > (SELECT GREATEST(ecart_nombre,0) FROM w_aj_ecarts WHERE prestation_code = 'PAT');
  		INSERT INTO pmsi.p_rsf_detail(  
  			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, pec_fj_id,   
  			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)   
  		SELECT '', w_aj_ecarts_pat_rss.no_rss, '', '', '', w_aj_ecarts_pat_rss.date_entree, w_aj_ecarts_pat_rss.date_sortie,   
  			-1 AS nombre,  
  			1,   
  			w_aj_ecarts_pat_rss.montant_pat AS prix_unitaire,  
  			0 - w_aj_ecarts_pat_rss.montant_pat AS base_remboursement, 100, 0 - w_aj_ecarts_pat_rss.montant_pat, 0 - w_aj_ecarts_pat_rss.montant_pat, '', '', '', 0, 1, 0, 0, 0, '', prestation_id, w_aj_ecarts_pat_rss.ecart_rss_id, 'B', 
  			1, 	'1', '0', 0, 0, 0  
  		FROM w_aj_ecarts_pat_rss;
  		-- RSS a recalculer
  		INSERT INTO w_aj_cum_rss
  		SELECT ecart_rss_id 
  		FROM w_aj_ecarts_pat_rss
  		WHERE ecart_rss_id NOT IN (SELECT rss_id FROM w_aj_cum_rss)
  		GROUP BY 1;
  		
  	END IF;
  	RAISE NOTICE '%' , 'Ajustement UCD';
  	-- Ecarts
  	-- Avant mars 2015
  	DROP TABLE IF EXISTS w_aj_ecarts_ucd;
  	CREATE TEMP TABLE w_aj_ecarts_ucd AS
  	SELECT	import_id,
  		data_mat2a[1] AS ucd_code,
  		''::text AS indication_code,
  		base.cti_to_number(data_mat2a[3]) - base.cti_to_number(data_cti[3]) AS ecart_nombre,
  		round(base.cti_to_number(data_mat2a[4]) / base.cti_to_number(data_mat2a[3]),3) AS prix_unitaire,
  		round(base.cti_to_number(data_mat2a[5]) / base.cti_to_number(data_mat2a[3]),3) AS prix_unitaire_tr,
  		round(base.cti_to_number(data_mat2a[6]) / base.cti_to_number(data_mat2a[3]),3) AS prix_unitaire_mat2a
  	FROM pmsi.p_mat2a_data
  		JOIN pmsi.p_mat2a_import ON p_mat2a_data.mat2a_import_id = p_mat2a_import.oid
  		JOIN pmsi.p_imports ON p_imports.mat2a_import_id = p_mat2a_import.oid
  		JOIN w_aj_imports ON w_aj_imports.import_id = p_imports.oid
  	WHERE file_code = 'T1V2VMED' AND
  		(w_aj_imports.mat2a_annee < 2015 OR (w_aj_imports.mat2a_annee = 2015 AND w_aj_imports.mat2a_mois < 3)) AND
  		table_sequence = 2 AND
  		line_type IN ('D') AND
  		p_mat2a_import.mat2a_annee >= 2012 AND
  		(
  			ABS(base.cti_to_number(data_mat2a[4]) - base.cti_to_number(data_cti[4])) > 0.01 OR
  			ABS(base.cti_to_number(data_mat2a[5]) - base.cti_to_number(data_cti[5])) > 0.01 OR
  			ABS(base.cti_to_number(data_mat2a[6]) - base.cti_to_number(data_cti[6])) > 0.01 OR
  			ABS(base.cti_to_number(data_mat2a[7]) - base.cti_to_number(data_cti[7])) > 0.01
  			
  		)
  	ORDER BY row_sequence;
  	-- Après mars 2015, le format du tableau a changé, une colonne a été rajoutée au début du tableau
      -- Après mars 2018, une colonne indication a ete rajoutee entre le code et le libelle
  	DROP TABLE IF EXISTS w_aj_ecarts_ucd;
  	CREATE TEMP TABLE w_aj_ecarts_ucd AS
  	SELECT	import_id,
  		trim(split_part(CASE WHEN line_type = 'D' THEN data_mat2a[2] ELSE '*' END,' ',1)) AS ucd_code,
  		CASE WHEN w_aj_imports.mat2a_annee >= 2018 THEN data_mat2a[3] ELSE '' END AS indication_code,
  		base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_mat2a[4] ELSE data_mat2a[5] END) - base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_cti[4] ELSE data_cti[5] END) AS ecart_nombre,
  		base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_mat2a[5] ELSE data_mat2a[6] END) AS total_depense,
  		base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_mat2a[6] ELSE data_mat2a[7] END) AS total_tr,
  		base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_mat2a[7] ELSE data_mat2a[8] END) AS total_mat2a,
  		round(base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_mat2a[5] ELSE data_mat2a[6] END) / base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_mat2a[4] ELSE data_mat2a[5] END),3) AS prix_unitaire,
  		round(base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_mat2a[6] ELSE data_mat2a[7] END) / base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_mat2a[4] ELSE data_mat2a[5] END),3) AS prix_unitaire_tr,
  		round(base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_mat2a[7] ELSE data_mat2a[8] END) / base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_mat2a[4] ELSE data_mat2a[5] END),3) AS prix_unitaire_mat2a
  	FROM pmsi.p_mat2a_data
  		JOIN pmsi.p_mat2a_import ON p_mat2a_data.mat2a_import_id = p_mat2a_import.oid
  		JOIN pmsi.p_imports ON p_imports.mat2a_import_id = p_mat2a_import.oid
  		JOIN w_aj_imports ON w_aj_imports.import_id = p_imports.oid
  	WHERE file_code = 'T1V2VMED' AND
  		(w_aj_imports.mat2a_annee > 2015) AND
  		table_sequence = 2 AND
  		line_type IN ('D','F') AND
  		p_mat2a_import.mat2a_annee >= 2012 AND
  		(
  		    ABS(base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_mat2a[5] ELSE data_mat2a[6] END) - base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_cti[4] ELSE data_cti[6] END )) > 0.01 OR
  		    ABS(base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_mat2a[6] ELSE data_mat2a[7] END) - base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_cti[5] ELSE data_cti[7] END )) > 0.01 OR
  		    ABS(base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_mat2a[7] ELSE data_mat2a[8] END) - base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_cti[6] ELSE data_cti[8] END )) > 0.01 OR
  		    ABS(base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_mat2a[8] ELSE data_mat2a[9] END) - base.cti_to_number(CASE WHEN w_aj_imports.mat2a_annee < 2018 THEN data_cti[7] ELSE data_cti[9] END )) > 0.01
  		)
  	ORDER BY row_sequence;
  	
  	IF EXISTS (SELECT * FROM w_aj_ecarts_ucd LIMIT 1) THEN
  		-- Mise à jour fichier UCD
  		UPDATE pmsi.p_rss_ucd SET
  			montant_facture = round(p_rss_ucd.nombre * w_aj_ecarts_ucd.prix_unitaire,3),
  			prix_unitaire_tr = w_aj_ecarts_ucd.prix_unitaire_tr,
  			montant_tr = round(p_rss_ucd.nombre * w_aj_ecarts_ucd.prix_unitaire_tr,3),
  			montant_t2a_accorde = round(p_rss_ucd.nombre * w_aj_ecarts_ucd.prix_unitaire_mat2a,3),
  			montant_ecart_indemnisable = round(p_rss_ucd.nombre * w_aj_ecarts_ucd.prix_unitaire_mat2a,3) - round(p_rss_ucd.nombre * w_aj_ecarts_ucd.prix_unitaire,3)
  		FROM pmsi.p_rss,
  			pmsi.t_ucd,
  			pmsi.t_indication,
  			w_aj_ecarts_ucd 
  		WHERE 
  			p_rss_ucd.rss_id = p_rss.oid AND
  			t_ucd.oid = ucd_id AND
  			t_indication.oid = indication_id AND
  			w_aj_ecarts_ucd.import_id = p_rss.import_id AND
  			w_aj_ecarts_ucd.ucd_code <> '*' AND
  			w_aj_ecarts_ucd.ucd_code = t_ucd.code AND
  			(w_aj_ecarts_ucd.indication_code = t_indication.code OR w_aj_ecarts_ucd.indication_code = '')AND
  			p_rss_ucd.non_pris_en_compte <> '1' AND
  			ecart_nombre = 0;
  		-- Top pris en compte si ecart nombre DROP TABLE IF EXISTS w_rss_ucd_pris;
  		DROP TABLE IF EXISTS w_rss_ucd_pris;
  		CREATE TEMP TABLE w_rss_ucd_pris AS
  		SELECT p_rss_ucd.CTID AS ucdCTID, ecart_nombre, p_rss_ucd.rss_id, p_rss_ucd.ucd_id, p_rss_ucd.date_ucd, p_rss_ucd.nombre, 0::numeric AS nombre_cumule
  		FROM pmsi.p_rss_ucd, pmsi.p_rss,
  			pmsi.t_ucd,
  			pmsi.t_indication,
  			w_aj_ecarts_ucd 
  		WHERE 
  			p_rss_ucd.rss_id = p_rss.oid AND
  			t_ucd.oid = ucd_id AND
  			t_indication.oid = indication_id AND
  			w_aj_ecarts_ucd.import_id = p_rss.import_id AND
  			w_aj_ecarts_ucd.ucd_code = t_ucd.code AND
  			(w_aj_ecarts_ucd.indication_code = t_indication.code OR w_aj_ecarts_ucd.indication_code = '')AND
  			p_rss_ucd.non_pris_en_compte = '1' AND
  			ecart_nombre > 0 
  		ORDER BY p_rss_ucd.date_ucd DESC, p_rss_ucd.rss_id;
  		
  		UPDATE w_rss_ucd_pris
  		SET nombre_cumule = 
  			(SELECT SUM(nombre) 
  			FROM w_rss_ucd_pris w_rss_ucd_pris2
  			WHERE w_rss_ucd_pris2.ucd_id = w_rss_ucd_pris.ucd_id AND
  				w_rss_ucd_pris2.date_ucd >= w_rss_ucd_pris.date_ucd AND
  				w_rss_ucd_pris2.rss_id >= w_rss_ucd_pris.rss_id
  				);
  		UPDATE pmsi.p_rss_ucd SET
  			non_pris_en_compte = '0',
  			montant_facture = round(p_rss_ucd.nombre * w_aj_ecarts_ucd.prix_unitaire,3),
  			prix_unitaire_tr = w_aj_ecarts_ucd.prix_unitaire_tr,
  			montant_tr = round(p_rss_ucd.nombre * w_aj_ecarts_ucd.prix_unitaire_tr,3),
  			montant_t2a_accorde = round(p_rss_ucd.nombre * w_aj_ecarts_ucd.prix_unitaire_mat2a,3),
  			montant_ecart_indemnisable = round(p_rss_ucd.nombre * w_aj_ecarts_ucd.prix_unitaire_mat2a,3) - round(p_rss_ucd.nombre * w_aj_ecarts_ucd.prix_unitaire,3)
  		FROM w_rss_ucd_pris, w_aj_ecarts_ucd 
  		WHERE p_rss_ucd.CTID = ucdCTID AND
  			w_rss_ucd_pris.nombre_cumule <= w_rss_ucd_pris.ecart_nombre
  		;
  				
  		-- Regénération prestations pharmacie
  		DROP TABLE IF EXISTS w_aj_ecarts_ucd_rss;
  		CREATE TEMP TABLE w_aj_ecarts_ucd_rss AS
  		SELECT p_rss.oid AS ecart_rss_id
  		FROM pmsi.p_rss_ucd
  			JOIN pmsi.p_rss ON p_rss_ucd.rss_id = p_rss.oid
  			JOIN pmsi.t_ucd ON t_ucd.oid = ucd_id
  			JOIN w_aj_ecarts_ucd ON w_aj_ecarts_ucd.ucd_code = t_ucd.code
  		WHERE w_aj_ecarts_ucd.import_id = p_rss.import_id
  		GROUP BY 1;
  
  		-- Non prise en compte AVASTIN
  		DROP TABLE IF EXISTS w_aj_ucd_avastin;
  		CREATE TEMP TABLE w_aj_ucd_avastin AS
  		SELECT import_id,
  			trim(split_part(CASE WHEN line_type = 'D' THEN data_mat2a[2] ELSE '*' END,' ',1)) AS ucd_code
  		FROM pmsi.p_mat2a_data
  			JOIN pmsi.p_mat2a_import ON p_mat2a_data.mat2a_import_id = p_mat2a_import.oid
  			JOIN pmsi.p_imports ON p_imports.mat2a_import_id = p_mat2a_import.oid
  			JOIN w_aj_imports ON w_aj_imports.import_id = p_imports.oid
  		WHERE file_code = 'T1V2VMED' AND
  			(w_aj_imports.mat2a_annee > 2015) AND
  			table_sequence = 2 AND
  			line_type IN ('D') AND
  			trim(split_part(CASE WHEN line_type = 'D' THEN data_mat2a[2] ELSE '*' END,' ',1)) IN ('9261104','9261110')
  		GROUP BY 1,2
  		;
  
  
  		DROP TABLE IF EXISTS w_aj_ucd_avastin_non;
  		CREATE TEMP TABLE w_aj_ucd_avastin_non AS
  		SELECT p_rss_ucd.CTID AS ucdCTID, p_rss_ucd.rss_id
  		FROM pmsi.p_rss_ucd
  		JOIN pmsi.p_rss ON rss_id = p_rss.oid
  		JOIN pmsi.t_ucd ON ucd_id = t_ucd.oid
  		JOIN w_aj_imports ON w_aj_imports.import_id = p_rss.import_id
  		LEFT JOIN w_aj_ucd_avastin ON
  			p_rss.import_id = w_aj_ucd_avastin.import_id AND
  			t_ucd.code = ucd_code
  		WHERE t_ucd.code IN ('9261104','9261110') AND
  			p_rss.ghm_id > 0 AND
  			non_pris_en_compte = '0' AND
  			w_aj_ucd_avastin.import_id IS NULL
  		;
  
  		UPDATE pmsi.p_rss_ucd
  		SET non_pris_en_compte = '1'
  		FROM w_aj_ucd_avastin_non
  		WHERE p_rss_ucd.rss_id = w_aj_ucd_avastin_non.rss_id AND
  			p_rss_ucd.CTID = w_aj_ucd_avastin_non.ucdCTID
  		;
  
  		INSERT INTO w_aj_ecarts_ucd_rss
  		SELECT rss_id
  		FROM w_aj_ucd_avastin_non
  		WHERE rss_id NOT IN (SELECT ecart_rss_id FROM w_aj_ecarts_ucd_rss)
  		GROUP BY 1
  		;
  
  		-- Suppression si problème association ucd/indication
  		DROP TABLE IF EXISTS w_aj_delete_ucd;
  		CREATE TEMP TABLE w_aj_delete_ucd AS
  		SELECT p_rss_ucd.rss_id, p_rss_ucd.ucd_id, p_rss_ucd.indication
  		FROM w_aj_imports
  		JOIN pmsi.p_mat2a_data ON 
  			p_mat2a_data.mat2a_import_id = w_aj_imports.mat2a_import_id  AND
  			p_mat2a_data.file_code = 'T1V2VMED' AND
  			p_mat2a_data.table_sequence = 3 AND
  			line_type = 'D'
  		JOIN pmsi.t_ucd ON trim(split_part(data_mat2a[2],' ',1)) = t_ucd.code
  		JOIN pmsi.p_rss ON p_rss.import_id = w_aj_imports.import_id
  		JOIN pmsi.p_rss_ucd ON p_rss_ucd.rss_id = p_rss.oid AND
  			p_rss_ucd.ucd_id = t_ucd.oid AND
  			p_rss_ucd.indication = trim(split_part(data_mat2a[3],' ',1))
  		WHERE w_aj_imports.mat2a_annee >= 2019 AND
  			non_pris_en_compte <> '1'
  		;
  
  		INSERT INTO w_aj_delete_ucd
  		SELECT p_rss_ucd.rss_id, ucd_id, p_rss_ucd.indication
  		FROM pmsi.p_mat2a_data
  		JOIN pmsi.p_mat2a_import ON p_mat2a_data.mat2a_import_id = p_mat2a_import.oid
  		JOIN pmsi.p_imports ON p_imports.mat2a_import_id = p_mat2a_import.oid
  		JOIN w_aj_imports ON w_aj_imports.import_id = p_imports.oid
  		JOIN pmsi.p_rss ON p_rss.import_id = p_imports.oid
  		JOIN pmsi.p_rss_ucd ON p_rss_ucd.rss_id = p_rss.oid
  		JOIN pmsi.t_ucd ON t_ucd.oid = ucd_id
    		WHERE file_code = 'T1V2VMED' AND 
  			table_sequence = 1 AND
  			line_type = 'D' AND
  			base.cti_to_number(data_mat2a[9]) <> 0  AND
  			base.cti_to_number(data_mat2a[10]) = 0  AND
  			t_ucd.code = data_mat2a[2] AND
  			p_rss_ucd.indication = data_mat2a[3] AND
  			non_pris_en_compte = '0'
  		;
  
  		
  
  		UPDATE pmsi.p_rss_ucd
  		SET non_pris_en_compte = '1'
  		FROM w_aj_delete_ucd
  		WHERE p_rss_ucd.rss_id = w_aj_delete_ucd.rss_id AND
  			p_rss_ucd.ucd_id = w_aj_delete_ucd.ucd_id AND
  			p_rss_ucd.indication = w_aj_delete_ucd.indication
  		;
  
  		INSERT INTO w_aj_ecarts_ucd_rss
  		SELECT rss_id
  		FROM w_aj_delete_ucd
  		WHERE rss_id NOT IN (SELECT ecart_rss_id FROM w_aj_ecarts_ucd_rss)
  		GROUP BY 1
  		;
  		
  		DELETE
  		FROM pmsi.p_rsf_detail
  		WHERE rss_id IN (SELECT ecart_rss_id FROM w_aj_ecarts_ucd_rss) AND
  		(
  		prestation_id IN (SELECT prestation_defaut_id FROM pmsi.t_ucd) OR
  		prestation_id IN (SELECT oid FROM pmsi.t_prestations WHERE code = 'EMI')
  		);
  
  
  		
  		INSERT INTO pmsi.p_rsf_detail( 
  			finess, no_rss, nature, mt, dmt, date_debut, date_fin, nombre,  
  			coefficient, prix_unitaire, base_remboursement, base_remboursement_sauf_transition, taux_remboursement,  
  			sejour_facture, sejour_remboursable, compteur, ligne_t2a, pec_fj, pec_fj_id,  
  			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)  
  		SELECT p_rss.finess, p_rss.no_rss, '', '', '', date_ucd, date_ucd, nombre, 
  			1, p_rss_ucd.montant_facture / p_rss_ucd.nombre, montant_facture, montant_facture, 100, montant_facture, montant_facture, '', '', '', 0, 
  			1, 0, 0, montant_facture, '', t_ucd.prestation_defaut_id, rss_id, 'B', 1, '1', '0', 0, 0, 0
  		FROM pmsi.p_rss_ucd
  			JOIN pmsi.p_rss ON p_rss_ucd.rss_id = p_rss.oid 
  			JOIN pmsi.t_ucd ON p_rss_ucd.ucd_id = t_ucd.oid 
  			JOIN w_aj_ecarts_ucd_rss ON p_rss_ucd.rss_id = ecart_rss_id
  		WHERE (p_rss.mode_entree <> '0' AND p_rss.mode_sortie <> '0') AND
  			montant_facture <> 0 AND non_pris_en_compte <> '1';
  
  			
  		INSERT INTO pmsi.p_rsf_detail( 
  		finess, no_rss, nature, mt, dmt, date_debut, date_fin, nombre,  
  		coefficient, prix_unitaire, base_remboursement, base_remboursement_sauf_transition, taux_remboursement,  
  		sejour_facture, sejour_remboursable, compteur, ligne_t2a, pec_fj, pec_fj_id,  
  		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)  
  		SELECT p_rss.finess, p_rss.no_rss, '', '', '', date_ucd, date_ucd, nombre, 
  			1, p_rss_ucd.montant_ecart_indemnisable / p_rss_ucd.nombre, montant_ecart_indemnisable, montant_ecart_indemnisable, 100, montant_ecart_indemnisable, montant_ecart_indemnisable, '', '', '', 0, 
  			1, 0, 0, montant_ecart_indemnisable, '', t_prestations.oid, rss_id, 'B', 1, '1', '0', 0, 0, 0
  		FROM pmsi.p_rss_ucd
  			JOIN pmsi.p_rss ON p_rss_ucd.rss_id = p_rss.oid 
  			JOIN pmsi.t_ucd ON p_rss_ucd.ucd_id = t_ucd.oid 
  			JOIN pmsi.t_prestations ON t_prestations.code = 'EMI'
  			JOIN w_aj_ecarts_ucd_rss ON p_rss_ucd.rss_id = ecart_rss_id
  		WHERE (p_rss.mode_entree <> '0' AND p_rss.mode_sortie <> '0') AND
  			montant_ecart_indemnisable <> 0 AND non_pris_en_compte <> '1';
  		-- RSS a recalculer
  		INSERT INTO w_aj_cum_rss
  		SELECT ecart_rss_id 
  		FROM w_aj_ecarts_ucd_rss
  		WHERE ecart_rss_id NOT IN (SELECT rss_id FROM w_aj_cum_rss)
  		GROUP BY 1;
  	END IF;
  	RAISE NOTICE '%' , 'Ajustement LPP';
  	-- Ecarts en montant
  	DROP TABLE IF EXISTS w_aj_ecarts_lpp;
  	CREATE TEMP TABLE w_aj_ecarts_lpp AS
  	SELECT	import_id,
  		data_mat2a[1] AS lpp_code,
  		round(base.cti_to_number(data_mat2a[4]) / base.cti_to_number(data_mat2a[3]),3) AS prix_unitaire,
  		round(base.cti_to_number(data_mat2a[5]) / base.cti_to_number(data_mat2a[3]),3) AS prix_unitaire_tr,
  		round(base.cti_to_number(data_mat2a[6]) / base.cti_to_number(data_mat2a[3]),3) AS prix_unitaire_mat2a,
  		base.cti_to_number(data_mat2a[4]) AS mat2a_montant_facture,
  		base.cti_to_number(data_mat2a[5]) AS mat2a_montant_tr,
  		base.cti_to_number(data_mat2a[6]) AS mat2a_montant_t2a
  	FROM pmsi.p_mat2a_data
  		JOIN pmsi.p_mat2a_import ON p_mat2a_data.mat2a_import_id = p_mat2a_import.oid
  		JOIN pmsi.p_imports ON p_imports.mat2a_import_id = p_mat2a_import.oid
  		JOIN w_aj_imports ON w_aj_imports.import_id = p_imports.oid
  	WHERE file_code = 'T1V3VDMI' AND
  		table_sequence = 2 AND
  		line_type IN ('D') AND
  		p_mat2a_import.mat2a_annee >= 2012 AND
  		base.cti_to_number(data_mat2a[3]) = base.cti_to_number(data_cti[3]) AND
  		(
  			ABS(base.cti_to_number(data_mat2a[4]) - base.cti_to_number(data_cti[4])) > 0.01 OR
  			ABS(base.cti_to_number(data_mat2a[5]) - base.cti_to_number(data_cti[5])) > 0.01 OR
  			ABS(base.cti_to_number(data_mat2a[6]) - base.cti_to_number(data_cti[6])) > 0.01 OR
  			ABS(base.cti_to_number(data_mat2a[7]) - base.cti_to_number(data_cti[7])) > 0.01
  			
  		)
  	ORDER BY row_sequence;
  
  	-- LPP non valorisés
  	DROP TABLE IF EXISTS w_aj_delete_lpp;
  	CREATE TEMP TABLE w_aj_delete_lpp AS
  	SELECT p_rss_lpp.rss_id, p_rss_lpp.lpp_id
  	FROM pmsi.p_rss_lpp
  	JOIN pmsi.P_rss ON p_rss_lpp.rss_id = p_rss.oid
  	JOIN pmsi.t_lpp ON p_rss_lpp.lpp_id = t_lpp.oid
  	JOIN w_aj_imports ON p_rss.import_id = w_aj_imports.import_id
  	LEFT JOIN pmsi.p_mat2a_data ON 
  		p_mat2a_data.mat2a_import_id = w_aj_imports.mat2a_import_id  AND
  		p_mat2a_data.file_code = 'T1V3VDMI' AND
  		p_mat2a_data.table_sequence = 2 AND
  		t_lpp.code = data_mat2a[1]
  	WHERE traitement_epmsi = '30' AND
  		p_rss.ghm_id > 0 AND
  		non_pris_en_compte = '0' AND
  		p_mat2a_data.oid IS NULL
  	GROUP BY 1,2
  	;
  	
  	IF EXISTS (SELECT * FROM w_aj_ecarts_lpp LIMIT 1) OR EXISTS (SELECT * FROM w_aj_delete_lpp LIMIT 1) THEN
  		-- Mise à jour fichier LPP
  		UPDATE pmsi.p_rss_lpp SET
  			montant_facture = round(p_rss_lpp.nombre * w_aj_ecarts_lpp.prix_unitaire,3),
  			prix_unitaire_tr = w_aj_ecarts_lpp.prix_unitaire_tr,
  			montant_tr = round(p_rss_lpp.nombre * w_aj_ecarts_lpp.prix_unitaire_tr,3),
  			montant_t2a_accorde = round(p_rss_lpp.nombre * w_aj_ecarts_lpp.prix_unitaire_mat2a,3),
  			montant_ecart_indemnisable = round(p_rss_lpp.nombre * w_aj_ecarts_lpp.prix_unitaire_mat2a,3) - round(p_rss_lpp.nombre * w_aj_ecarts_lpp.prix_unitaire,3)
  		FROM pmsi.p_rss,
  			pmsi.t_lpp,
  			w_aj_ecarts_lpp 
  		WHERE 
  			p_rss_lpp.rss_id = p_rss.oid AND
  			p_rss_lpp.non_pris_en_compte = '0' AND
  			traitement_epmsi IN ('30') AND
  			p_rss.ghm_id > 0 AND
  			t_lpp.oid = lpp_id AND
  			w_aj_ecarts_lpp.import_id = p_rss.import_id AND
  			w_aj_ecarts_lpp.lpp_code = t_lpp.code;
  		-- arrondis
  		UPDATE pmsi.p_rss_lpp
  		SET 
  			montant_facture = montant_facture - ecart_facture,
  			montant_tr = montant_tr - ecart_tr,
  			montant_t2a_accorde = montant_t2a_accorde - ecart_t2a,
  			montant_ecart_indemnisable = montant_ecart_indemnisable - ecart_ecart
  		FROM
  		(
  			SELECT lpp_id,
  				MAX(p_rss_lpp.CTID) AS updCTID,
  				SUM(montant_facture) - 
  					MAX(mat2a_montant_facture) AS ecart_facture,
  				SUM(montant_tr) -
  					MAX(mat2a_montant_tr) AS ecart_tr,
  				SUM(montant_t2a_accorde) -
  					MAX(mat2a_montant_t2a) AS ecart_t2a,
  				(SUM(montant_t2a_accorde) - SUM(montant_facture)) -
  					(MAX(mat2a_montant_t2a) - MAX(mat2a_montant_facture) ) AS ecart_ecart
  			FROM pmsi.p_rss_lpp
  			JOIN pmsi.t_lpp ON t_lpp.oid = lpp_id
  			JOIN pmsi.p_rss ON p_rss_lpp.rss_id = p_rss.oid AND traitement_epmsi IN ('30')
  			JOIN w_aj_ecarts_lpp ON w_aj_ecarts_lpp.import_id = p_rss.import_id AND
  				w_aj_ecarts_lpp.lpp_code = t_lpp.code
  			WHERE p_rss_lpp.non_pris_en_compte = '0' AND
  				p_rss.ghm_id > 0
  			GROUP BY 1
  		) subview
  		WHERE subview.updCTID = p_rss_lpp.CTID;
  
  		-- 
  		UPDATE pmsi.p_rss_lpp
  		SET non_pris_en_compte = '1'
  		FROM w_aj_delete_lpp
  		WHERE p_rss_lpp.rss_id = w_aj_delete_lpp.rss_id AND
  			p_rss_lpp.lpp_id = w_aj_delete_lpp.lpp_id
  			;
  			
  		-- Regénération prestations pharmacie
  		DROP TABLE IF EXISTS w_aj_ecarts_lpp_rss;
  		CREATE TEMP TABLE w_aj_ecarts_lpp_rss AS
  		SELECT p_rss.oid AS ecart_rss_id
  		FROM pmsi.p_rss_lpp
  			JOIN pmsi.p_rss ON p_rss_lpp.rss_id = p_rss.oid
  			JOIN pmsi.t_lpp ON t_lpp.oid = lpp_id
  			JOIN w_aj_ecarts_lpp ON w_aj_ecarts_lpp.lpp_code = t_lpp.code
  		WHERE w_aj_ecarts_lpp.import_id = p_rss.import_id AND
  			p_rss.ghm_id > 0
  		GROUP BY 1;
  
  		INSERT INTO w_aj_ecarts_lpp_rss
  		SELECT rss_id
  		FROM w_aj_delete_lpp
  		WHERE rss_id NOT IN (SELECT ecart_rss_id FROM w_aj_ecarts_lpp_rss)
  		GROUP BY 1
  		;
  		
  		
  		DELETE
  		FROM pmsi.p_rsf_detail
  		WHERE rss_id IN (SELECT ecart_rss_id FROM w_aj_ecarts_lpp_rss) AND
  		(
  		prestation_id IN (SELECT prestation_defaut_id FROM pmsi.t_lpp) OR
  		prestation_id IN (SELECT oid FROM pmsi.t_prestations WHERE code = 'ETI')
  		);
  		INSERT INTO pmsi.p_rsf_detail( 
  			finess, no_rss, nature, mt, dmt, date_debut, date_fin, nombre,  
  			coefficient, prix_unitaire, base_remboursement, base_remboursement_sauf_transition, taux_remboursement,  
  			sejour_facture, sejour_remboursable, compteur, ligne_t2a, pec_fj, pec_fj_id,  
  			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)  
  		SELECT p_rss.finess, p_rss.no_rss, '', '', '', date_lpp, date_lpp, nombre, 
  			1, p_rss_lpp.montant_facture / p_rss_lpp.nombre, montant_facture, montant_facture, 100, montant_facture, montant_facture, '', '', '', 0, 
  			1, 0, 0, montant_facture, '', t_lpp.prestation_defaut_id, rss_id, 'B', 1, '1', '0', 0, 0, 0
  		FROM pmsi.p_rss_lpp
  			JOIN pmsi.p_rss ON p_rss_lpp.rss_id = p_rss.oid 
  			JOIN pmsi.t_lpp ON p_rss_lpp.lpp_id = t_lpp.oid 
  			JOIN w_aj_ecarts_lpp_rss ON p_rss_lpp.rss_id = ecart_rss_id
  		WHERE (p_rss.mode_entree <> '0' AND p_rss.mode_sortie <> '0') AND
  			montant_facture <> 0 AND non_pris_en_compte <> '1';
  		INSERT INTO pmsi.p_rsf_detail( 
  		finess, no_rss, nature, mt, dmt, date_debut, date_fin, nombre,  
  		coefficient, prix_unitaire, base_remboursement, base_remboursement_sauf_transition, taux_remboursement,  
  		sejour_facture, sejour_remboursable, compteur, ligne_t2a, pec_fj, pec_fj_id,  
  		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)  
  		SELECT p_rss.finess, p_rss.no_rss, '', '', '', date_lpp, date_lpp, nombre, 
  			1, p_rss_lpp.montant_ecart_indemnisable / p_rss_lpp.nombre, montant_ecart_indemnisable, montant_ecart_indemnisable, 100, montant_ecart_indemnisable, montant_ecart_indemnisable, '', '', '', 0, 
  			1, 0, 0, montant_ecart_indemnisable, '', t_prestations.oid, rss_id, 'B', 1, '1', '0', 0, 0, 0
  		FROM pmsi.p_rss_lpp
  			JOIN pmsi.p_rss ON p_rss_lpp.rss_id = p_rss.oid 
  			JOIN pmsi.t_lpp ON p_rss_lpp.lpp_id = t_lpp.oid 
  			JOIN pmsi.t_prestations ON t_prestations.code = 'ETI'
  			JOIN w_aj_ecarts_lpp_rss ON p_rss_lpp.rss_id = ecart_rss_id
  		WHERE (p_rss.mode_entree <> '0' AND p_rss.mode_sortie <> '0') AND
  			montant_ecart_indemnisable <> 0 AND non_pris_en_compte <> '1';
  		-- RSS a recalculer
  		INSERT INTO w_aj_cum_rss
  		SELECT ecart_rss_id 
  		FROM w_aj_ecarts_lpp_rss
  		WHERE ecart_rss_id NOT IN (SELECT rss_id FROM w_aj_cum_rss)
  		GROUP BY 1;
  	END IF;
  
  	-- Ajustement des prestations CCAM pour FIDES ou Pas FIDES
  	-- Objectif : basculer des centimes de fides vers pas fides si écart dans VFIDES 
  
  	-- Ecart total constaté
  	DROP TABLE IF EXISTS w_ajust_ccam_fides;
  	CREATE TEMP TABLE w_ajust_ccam_fides AS
  	SELECT data_mat2a[1] AS chapitre,
  		CASE WHEN MAX(base.cti_to_number(data_mat2a[5])) - SUM(CASE WHEN fides_detail = '1' THEN p_rsf_detail.sejour_remboursable ELSE 0 END) >= 0 THEN 0.01 ELSE -0.01 END AS ajout_fides_p,
  		(abs(MAX(base.cti_to_number(data_mat2a[5])) - SUM(CASE WHEN fides_detail = '1' THEN p_rsf_detail.sejour_remboursable ELSE 0 END))*100)::integer AS ajout_fides_n,
  		base.cti_array_accum(DISTINCT prestation_id) AS prestation_id_array
  	FROM pmsi.p_rsf_detail
  	JOIN pmsi.p_rss ON rss_id = p_rss.oid
  	JOIN w_aj_imports ON p_rss.import_id = w_aj_imports.import_id
  	JOIN pmsi.t_prestations ON prestation_id = t_prestations.oid
  	JOIN pmsi.p_mat2a_data ON
  		p_mat2a_data.mat2a_import_id = w_aj_imports.mat2a_import_id AND
  		p_mat2a_data.file_code = 'T2VFIDES' AND
  		table_sequence = 2 
  	WHERE traitement_epmsi = '30' AND
  		(
  		data_mat2a[1] = 'CCAM' AND t_prestations.code IN (SELECT prestation_code FROM w_aj_mat2a WHERE chapitre = 'CCAM') OR
  		data_mat2a[1] = 'NGAP' AND t_prestations.code IN (SELECT prestation_code FROM w_aj_mat2a WHERE chapitre = 'NGAP') 
  		)
  	GROUP BY 1	
  	HAVING abs(SUM(p_rsf_detail.sejour_remboursable) - MAX(base.cti_to_number(data_mat2a[4])) - MAX(base.cti_to_number(data_mat2a[5]))) < 10
  	;
  
  
  	ANALYSE w_ajust_ccam_fides
  	;
  
  			
  	-- Identification des prestations à retraiter, il faut 1 prestation fides pour 1 prestation non fides pour faire la bascule entre deux actes
  	DROP TABLE IF EXISTS w_ajust_ccam_fides_rss;
  	CREATE TEMP TABLE w_ajust_ccam_fides_rss AS
  	SELECT chapitre,
  		prestation_id, 
  		CASE WHEN chapitre ='NGAP' THEN p_rsf_detail.prix_unitaire ELSE 0 END AS prix_unitaire,
  		rss_id,
  		fides_detail,
  		p_rsf_detail.CTID AS ajust_CTID,
  		row_number() OVER (PARTITION BY prestation_id, fides_detail ORDER BY rss_id) AS row_ajust
  	FROM pmsi.p_rsf_detail
  	JOIN pmsi.p_rss ON rss_id = p_rss.oid
  	JOIN w_aj_imports ON p_rss.import_id = w_aj_imports.import_id
  	JOIN w_ajust_ccam_fides ON prestation_id = ANY(prestation_id_array) 
  	WHERE traitement_epmsi = '30' AND
  		sejour_remboursable > 1
  	;
  
  	
  
  	DELETE FROM w_ajust_ccam_fides_rss
  	USING
  	(
  	SELECT chapitre, prestation_id, prix_unitaire, row_ajust 
  	FROM w_ajust_ccam_fides_rss
  	GROUP BY 1,2,3,4
  	HAVING MAX(fides_detail) =  MIN(fides_detail) 
  	) subview
  	WHERE w_ajust_ccam_fides_rss.chapitre = subview.chapitre AND
  		w_ajust_ccam_fides_rss.prestation_id = subview.prestation_id AND
  		w_ajust_ccam_fides_rss.prix_unitaire = subview.prix_unitaire AND
  		w_ajust_ccam_fides_rss.row_ajust = subview.row_ajust
  	;
  
  	UPDATE w_ajust_ccam_fides_rss
  	SET row_ajust = subview.row_ajust_new
  	FROM
  	(
  	SELECT chapitre, prestation_id, fides_detail, row_ajust, row_number() OVER (PARTITION BY chapitre, fides_detail ORDER BY prestation_id, prix_unitaire, row_ajust) AS row_ajust_new
  	FROM w_ajust_ccam_fides_rss
  	ORDER BY 4
  	) subview
  	WHERE w_ajust_ccam_fides_rss.chapitre = subview.chapitre AND
  		w_ajust_ccam_fides_rss.prestation_id = subview.prestation_id AND
  		w_ajust_ccam_fides_rss.row_ajust = subview.row_ajust
  	;
  
  	-- Mise à jour des lignes
  	UPDATE pmsi.p_rsf_detail
  	SET sejour_remboursable = sejour_remboursable + CASE WHEN fides_detail = 1 THEN ajout_fides_p ELSE 0 - ajout_fides_p END 
  	FROM
  	(
  	SELECT rss_id, ajust_CTID, ajout_fides_p
  	FROM w_ajust_ccam_fides_rss
  	JOIN w_ajust_ccam_fides ON 
  		w_ajust_ccam_fides_rss.chapitre = w_ajust_ccam_fides.chapitre AND
  		row_ajust <= ajout_fides_n
  	) subview
  	WHERE p_rsf_detail.rss_id = subview.rss_id AND
  		p_rsf_detail.CTID = ajust_CTID
  	;
  
  	INSERT INTO w_aj_cum_rss
  	SELECT w_ajust_ccam_fides_rss.rss_id
  	FROM w_ajust_ccam_fides_rss
  	JOIN w_ajust_ccam_fides ON row_ajust <= ajout_fides_n
  	WHERE w_ajust_ccam_fides_rss.rss_id NOT IN (SELECT w_aj_cum_rss.rss_id FROM w_aj_cum_rss)
  	GROUP BY 1
  	;
  	
  	-- Ecarts ?
  	IF NOT EXISTS (SELECT * FROM w_aj_cum_rss LIMIT 1) THEN
  		return 'Pas d''ecart';
  	END IF;	
  		
  	RAISE NOTICE '%' , 'Mise à jour totaux. Valorise';
  	-- total facture
  	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_aj_cum_rss)
  		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_aj_cum_rss);
  	-- total rss	
  	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_aj_cum_rss)
  		;
  
  
  	
  	
  	RAISE NOTICE '%' , 'Reventilation UM';
  	PERFORM pmsi.cti_ventilation_ca_um();
  	RAISE NOTICE '%' , 'Recomparaison';			
  	PERFORM pmsi.cti_compare_with_mat2a(i_import_id, i_mat2a_import_id);
  		
  	RETURN 'OK';
  END;