return: text
lang: plpgsql
src: |
  DECLARE	
  	
  	
  	result TEXT;
  BEGIN
  	-- Paramétrage
  	INSERT INTO pmsi.t_divers (code, texte, valeur, description)
  	SELECT
  		'VENTILUM_MODE_GHS',
  		'Mode ventilation GHS par UM',
  		'PMS',
  		'DUR = Durée séjour, PMS = Durée séjour pondérée par PMS, PMJ = Durée séjour pondérée par PMJ'
  	WHERE 'VENTILUM_MODE_GHS' NOT IN (SELECT code FROM pmsi.t_divers);
  	UPDATE pmsi.t_divers
  	SET valeur = 'PMS'
  	WHERE code = 'VENTILUM_MODE_GHS' AND
  		valeur NOT IN ('DUR', 'PMS', 'PMJ');
  	-- Suppression génération précédente
  	DELETE FROM pmsi.p_rsf_detail WHERE est_ligne_rss <> '1' AND operation_lamda NOT IN ('1','2');
  	UPDATE pmsi.p_rsf_detail
  	SET 
  		no_rum = 0,
  		unite_medicale_id = 0,
  		base_remboursement_rum = 0,
  		base_remboursement_rum_sauf_transition = 0,
  		base_remboursement_rum_sauf_transition_sauf_ame = 0,
  		sejour_remboursable_rum = 0,
  		est_ligne_rum = '0'
  	FROM pmsi.p_rss
  	WHERE 
  		p_rsf_detail.rss_id = p_rss.oid
  		AND p_rss.nb_rum > 1
  		AND operation_lamda NOT IN ('1','2')
  		AND (no_rum IS DISTINCT FROM 0 OR
  			unite_medicale_id IS DISTINCT FROM 0 OR
  			base_remboursement_rum IS DISTINCT FROM 0 OR
  			sejour_remboursable_rum IS DISTINCT FROM 0 OR
  			base_remboursement_rum_sauf_transition IS DISTINCT FROM 0 OR
  			base_remboursement_rum_sauf_transition_sauf_ame IS DISTINCT FROM 0 OR
  			est_ligne_rum <> '0');
  	-- Mono rum
  	UPDATE pmsi.p_rsf_detail
  	SET 
  		no_rum = p_rss_rum.no_rum,
  		unite_medicale_id = p_rss_rum.unite_medicale_id,
  		base_remboursement_rum = round(p_rsf_detail.base_remboursement,2),
  		base_remboursement_rum_sauf_transition = round(p_rsf_detail.base_remboursement_sauf_transition,2),
  		base_remboursement_rum_sauf_transition_sauf_ame = round(p_rsf_detail.base_remboursement_sauf_transition_sauf_ame,2),
  		sejour_remboursable_rum = round(p_rsf_detail.sejour_remboursable,2),
  		est_ligne_rum = '1'
  	FROM pmsi.p_rss_rum
  		JOIN pmsi.p_rss ON p_rss.oid = p_rss_rum.rss_id
  	WHERE 
  		p_rsf_detail.rss_id = p_rss.oid
  		AND p_rss.nb_rum = 1
  		AND operation_lamda NOT IN ('1','2')
  		AND (
  			p_rsf_detail.no_rum IS DISTINCT FROM p_rss_rum.no_rum OR
  			p_rsf_detail.unite_medicale_id IS DISTINCT FROM p_rss_rum.unite_medicale_id OR
  			p_rsf_detail.base_remboursement_rum IS DISTINCT FROM round(p_rsf_detail.base_remboursement,2) OR
  			p_rsf_detail.base_remboursement_rum_sauf_transition IS DISTINCT FROM round(p_rsf_detail.base_remboursement_sauf_transition,2) OR
  			p_rsf_detail.base_remboursement_rum_sauf_transition_sauf_ame IS DISTINCT FROM round(p_rsf_detail.base_remboursement_sauf_transition_sauf_ame,2) OR
  			p_rsf_detail.sejour_remboursable_rum IS DISTINCT FROM round(p_rsf_detail.sejour_remboursable,2) OR
  			p_rsf_detail.est_ligne_rum <> '1'
  		);
  	UPDATE pmsi.p_rsf_detail
  	SET 
  		date_fin = CASE WHEN date_fin > date_sortie THEN date_sortie WHEN date_fin < date_entree THEN date_entree ELSE date_fin END,
  		date_debut = CASE WHEN date_debut > date_sortie THEN date_sortie WHEN date_debut < date_entree THEN date_entree ELSE date_debut END
  	FROM pmsi.p_rss
  	WHERE 
  		p_rsf_detail.rss_id = p_rss.oid
  		AND nb_rum > 1
  		AND operation_lamda NOT IN ('1','2')
  		AND (date_fin > date_sortie OR date_debut > date_sortie OR date_fin < date_entree OR date_debut < date_entree);
  	DROP TABLE IF EXISTS w_rsf_detail_rum;
  	CREATE TEMP TABLE w_rsf_detail_rum AS
  	SELECT p_rsf_detail.finess, 
  		p_rsf_detail.no_rss, 
  		nature, 
  		mt, 
  		dmt, 
  		date_debut, 
  		date_fin, 
  		nombre, 
  		coefficient, 
  		prix_unitaire, 
  		0::numeric AS base_remboursement, 
  		taux_remboursement, 
  		0::numeric AS sejour_facture, 
  		0::numeric AS sejour_remboursable, 
  		compteur, 
  		ligne_t2a, 
  		pec_fj, 
  		pec_fj_id, 
  		coefficient_mco, 
  		p_rsf_detail.sejour_remboursable AS sejour_remboursable_2,  
  		p_rsf_detail.ghs_id, 
  		p_rsf_detail.base_remboursement AS base_remboursement_total, 
  		COALESCE(p_rsf_detail.base_remboursement_sauf_transition,p_rsf_detail.base_remboursement) AS base_remboursement_total_sauf_transition, 
  		COALESCE(p_rsf_detail.base_remboursement_sauf_transition_sauf_ame,p_rsf_detail.base_remboursement) AS base_remboursement_total_sauf_transition_sauf_ame, 
  		nature_noemie, 
  		prestation_id, 
  		p_rsf_detail.rss_id, 
  		type, 
  		coefficient_geo, 
  		'0'::text AS est_ligne_rss, 
  		'1'::text AS est_ligne_rum, 
  		p_rss_rum.no_rum, 
  		p_rss_rum.unite_medicale_id, 
  		0::numeric AS base_remboursement_rum, 
  		0::numeric AS base_remboursement_rum_sauf_transition, 
  		0::numeric AS base_remboursement_rum_sauf_transition_sauf_ame, 
  		0::numeric AS sejour_remboursable_rum,
  		montant_supplement_nf,
  		date_comptable,
  		operation_lamda,
  		fides_detail
  	FROM 
  		pmsi.p_rss_rum, pmsi.p_rsf_detail, pmsi.p_rss
  	WHERE p_rsf_detail.rss_id = p_rss_rum.rss_id AND
  		p_rsf_detail.rss_id = p_rss.oid AND p_rss.nb_rum > 1 AND
  		operation_lamda NOT IN ('1','2');
  	
  	
  	
  	
  	DROP TABLE IF EXISTS w_rsf_detail_presta;
  	CREATE TEMP TABLE w_rsf_detail_presta AS
  	SELECT p_rsf_detail.rss_id,
  		p_rsf_detail.prestation_id,
  		SUM(p_rsf_detail.base_remboursement) AS base_remboursement_presta, 
  		SUM(COALESCE(p_rsf_detail.base_remboursement_sauf_transition,p_rsf_detail.base_remboursement)) AS base_remboursement_presta_sauf_transition,
  		SUM(COALESCE(p_rsf_detail.base_remboursement_sauf_transition_sauf_ame,p_rsf_detail.base_remboursement)) AS base_remboursement_presta_sauf_transition_sauf_ame,
  		SUM(p_rsf_detail.sejour_remboursable) AS sejour_remboursable_presta
  	FROM 
  		pmsi.p_rsf_detail
  		JOIN pmsi.p_rss ON p_rsf_detail.rss_id = p_rss.oid AND p_rss.nb_rum > 1
  	WHERE est_ligne_rss = '1' AND
  		operation_lamda NOT IN ('1','2')
  	GROUP BY 1,2;
  	
  	
  	TRUNCATE pmsi.p_ventilation_um;
  	INSERT INTO pmsi.p_ventilation_um(
  				unite_medicale_id, annee, nb_sejours_monorum, nb_jours_monorum, ca_ghs_monorum, pms, pmj)
  	SELECT t_unites_medicales.oid AS unite_medicale_id, 
  		round(p_chiffrier.mois / 100,0) AS annee, 
  		0, 
  		0,
  		0, 
  		0,
  		0
  	FROM pmsi.t_unites_medicales, pmsi.p_chiffrier
  	WHERE t_unites_medicales.oid IN (SELECT DISTINCT unite_medicale_id FROM pmsi.p_rss_rum)
  	GROUP BY 1,2;
  	UPDATE pmsi.p_ventilation_um SET 
  		nb_sejours_monorum = subview.nb_rsa, 
  		nb_jours_monorum = subview.duree_sejour
  	FROM
  	(
  		SELECT 
  			date_part('year', p_rss.date_sortie) AS annee,
  			unite_medicale_principale_id as unite_medicale_id,
  			SUM(nb_rsa) as nb_rsa,
  			SUM(duree_sejour+1) as duree_sejour
  		FROM pmsi.p_rss
  		WHERE nb_rum = 1 AND 
  			p_rss.ghm_id > 0 AND 
  			p_rss.etat = '' AND p_rss.ca_ghs <> 0
  		GROUP BY 1,2) subview
  	WHERE pmsi.p_ventilation_um.unite_medicale_id = subview.unite_medicale_id AND 
  		pmsi.p_ventilation_um.annee = subview.annee;
  	
  	UPDATE pmsi.p_ventilation_um SET 
  		ca_ghs_monorum = subview.ca_ghs
  	FROM
  	(
  		SELECT 
  			date_part('year', p_rss.date_sortie) AS annee,
  			unite_medicale_id as unite_medicale_id,
  			base.cti_division(SUM(p_rsf_detail.base_remboursement), SUM(p_rss.nb_rsa) ) as pms,
  			COUNT(DISTINCT p_rss.oid) as nb_rsa,
  			SUM(p_rsf_detail.base_remboursement) as ca_ghs
  		FROM pmsi.p_rss, pmsi.p_rsf_detail 
  		WHERE p_rss.oid = p_rsf_detail.rss_id AND nb_rum = 1 AND
  			operation_lamda NOT IN ('1','2') AND p_rss.ghm_id > 0 AND p_rss.etat = '' AND 
  			p_rss.ca_ghs <> 0 AND p_rsf_detail.base_remboursement <> 0 AND
  			p_rsf_detail.est_ligne_rss = '1' AND prestation_id IN (SELECT oid FROM pmsi.t_prestations WHERE code = 'GHS')
  		GROUP BY 1,2) subview
  	WHERE pmsi.p_ventilation_um.unite_medicale_id = subview.unite_medicale_id AND 
  		pmsi.p_ventilation_um.annee = subview.annee;
  	UPDATE pmsi.p_ventilation_um SET 
  		pms = base.cti_division(ca_ghs_monorum,nb_sejours_monorum),
  		pmj = base.cti_division(ca_ghs_monorum,nb_jours_monorum);
  	-- Ajustement PMJ/PMS pour ono rum non significatifs (< 10)
  	UPDATE pmsi.p_ventilation_um
  	SET pms = subview.pms, pmj = subview.pmj
  	FROM (
  	SELECT annee, AVG(pms) AS pms, AVG(pmj) AS pmj
  	FROM pmsi.p_ventilation_um
  	WHERE nb_sejours_monorum >= 10
  	GROUP BY 1) subview
  	WHERE p_ventilation_um.annee = subview.annee AND
  		p_ventilation_um.nb_sejours_monorum < 10;
  	
  		
  	DROP TABLE IF EXISTS rss_resume;
  	CREATE TEMP TABLE rss_resume AS
  	SELECT 
  		rss_id,
  		date_part('year', p_rss.date_sortie) AS annee,
  		p_rss.date_sortie,
  		p_rss.duree_sejour,
  		p_rss.nb_rum,
  		SUM(CASE WHEN t_prestations.code = 'GHS' THEN p_rsf_detail.base_remboursement ELSE 0 END) as ca_ghs
  	FROM pmsi.p_rss, pmsi.p_rsf_detail, pmsi.t_prestations
  	WHERE p_rss.oid = p_rsf_detail.rss_id 
  		AND nb_rum > 1 AND p_rss.ghm_id > 0 AND p_rss.etat = ''
  		AND prestation_id = t_prestations.oid
  		AND p_rsf_detail.est_ligne_rss = '1' AND
  		operation_lamda NOT IN ('1','2')
  	GROUP BY 1,2,3,4,5
  	ORDER BY 1;
  	
  	
  	DROP TABLE IF EXISTS rum_pmsval;
  	CREATE TEMP TABLE rum_pmsval AS
  	SELECT 
  		rss_resume.rss_id , 
  		rss_resume.ca_ghs as ca_ghs,
  		v_rss_rum_1.no_rum,
  		v_rss_rum_1.unite_medicale_id,
  		t_unites_medicales.type_autorisation,
  		t_divers.valeur AS type_ventilation_ca_ghs,
  		type_ventilation_ca AS type_ventilation_ca_special_um,
  		rss_resume.duree_sejour + rss_resume.nb_rum as duree_sejour,  
  		v_rss_rum_1.duree_sejour_rum + 1 as duree_sejour_rum,
  		v_rss_rum_1.date_entree_rum, v_rss_rum_1.date_sortie_rum,
  		(v_rss_rum_1.duree_sejour_rum + 1) / (rss_resume.duree_sejour + rss_resume.nb_rum) as dur_coeff,
  		pms,
  		pms * (v_rss_rum_1.duree_sejour_rum + 1) / (rss_resume.duree_sejour + rss_resume.nb_rum) as pms_val,
  		pmj,
  		pmj * (v_rss_rum_1.duree_sejour_rum + 1) / (rss_resume.duree_sejour + rss_resume.nb_rum) as pmj_val
  	FROM pmsi.v_rss_rum_1 
  		JOIN rss_resume ON rss_resume.rss_id = v_rss_rum_1.rss_id
  		JOIN pmsi.p_ventilation_um ON v_rss_rum_1.unite_medicale_id = p_ventilation_um.unite_medicale_id AND rss_resume.annee = p_ventilation_um.annee 
  		JOIN pmsi.t_unites_medicales ON v_rss_rum_1.unite_medicale_id = t_unites_medicales.oid
  		JOIN pmsi.t_divers ON t_divers.code = 'VENTILUM_MODE_GHS'
  	ORDER BY rss_resume.rss_id, v_rss_rum_1.no_rum;
  	
  	
  	CREATE INDEX rum_pmsval_i1 ON rum_pmsval USING btree (rss_id);
  	
  	
  	UPDATE rum_pmsval 
  	SET pms_val = ca_ghs, pmj_val = ca_ghs / duree_sejour 
  	WHERE rss_id IN (SELECT rss_id FROM rum_pmsval WHERE pms = 0);
  	
  	
  	DROP TABLE IF EXISTS rss_pmsval;
  	CREATE TEMP TABLE rss_pmsval AS
  	SELECT 
  		rum_pmsval.rss_id, 
  		MIN(rum_pmsval.no_rum) AS premier_rum,
  		MAX(rum_pmsval.no_rum) AS dernier_rum,
  		MIN(CASE WHEN type_autorisation LIKE '01%' THEN rum_pmsval.no_rum ELSE 999 END) AS premier_rum_01,
  		MIN(CASE WHEN type_autorisation LIKE '02%' THEN rum_pmsval.no_rum ELSE 999 END) AS premier_rum_02,
  		MIN(CASE WHEN type_autorisation LIKE '03%' THEN rum_pmsval.no_rum ELSE 999 END) AS premier_rum_03,
  		MIN(CASE WHEN type_autorisation LIKE '04%' OR type_autorisation LIKE '05%' OR type_autorisation LIKE '06%' THEN rum_pmsval.no_rum ELSE 999 END) AS premier_rum_04_05_06,
  		MIN(CASE WHEN type_autorisation LIKE '13%' THEN rum_pmsval.no_rum ELSE 999 END) AS premier_rum_13,
  		SUM(CASE WHEN type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN pms_val ELSE 0 END) as pms_val,
  		SUM(CASE WHEN type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN pmj_val ELSE 0 END) as pmj_val,
  		SUM(CASE WHEN type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN dur_coeff ELSE 0 END) as pms_dur_coeff
  	FROM rum_pmsval
  	GROUP BY 1
  	ORDER BY 1;
  	
  	
  	CREATE INDEX rss_pmsval_i1 ON rss_pmsval USING btree (rss_id);
  	
  	
  	UPDATE rss_pmsval SET premier_rum_01 = premier_rum WHERE premier_rum_01 = 999;
  	UPDATE rss_pmsval SET premier_rum_02 = premier_rum WHERE premier_rum_02 = 999;
  	UPDATE rss_pmsval SET premier_rum_03 = premier_rum WHERE premier_rum_03 = 999;
  	UPDATE rss_pmsval SET premier_rum_04_05_06 = premier_rum WHERE premier_rum_04_05_06 = 999;
  	UPDATE rss_pmsval SET premier_rum_13 = premier_rum WHERE premier_rum_13 = 999;
  	
  	
  	UPDATE w_rsf_detail_rum p_rsf_detail SET
  		base_remboursement_rum = 
  			CASE 
  				WHEN type_ventilation_ca_ghs = 'DUR' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base_remboursement_total * dur_coeff,2)
  				WHEN type_ventilation_ca_ghs = 'PMS' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base.cti_division(base_remboursement_total * pms_dur_coeff * rum_pmsval.pms_val , rss_pmsval.pms_val),2) 
  				WHEN type_ventilation_ca_ghs = 'PMJ' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base.cti_division(base_remboursement_total * pms_dur_coeff * rum_pmsval.pmj_val , rss_pmsval.pmj_val),2) 
  			ELSE round(base_remboursement_total * dur_coeff,2) END,
  		base_remboursement_rum_sauf_transition = 
  			CASE 
  				WHEN type_ventilation_ca_ghs = 'DUR' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base_remboursement_total_sauf_transition * dur_coeff,2)
  				WHEN type_ventilation_ca_ghs = 'PMS' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base.cti_division(base_remboursement_total_sauf_transition * pms_dur_coeff * rum_pmsval.pms_val , rss_pmsval.pms_val),2) 
  				WHEN type_ventilation_ca_ghs = 'PMJ' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base.cti_division(base_remboursement_total_sauf_transition * pms_dur_coeff * rum_pmsval.pmj_val , rss_pmsval.pmj_val),2) 
  			ELSE round(base_remboursement_total_sauf_transition * dur_coeff,2) END,
  		base_remboursement_rum_sauf_transition_sauf_ame = 
  			CASE 
  				WHEN type_ventilation_ca_ghs = 'DUR' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base_remboursement_total_sauf_transition_sauf_ame * dur_coeff,2)
  				WHEN type_ventilation_ca_ghs = 'PMS' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base.cti_division(base_remboursement_total_sauf_transition_sauf_ame * pms_dur_coeff * rum_pmsval.pms_val , rss_pmsval.pms_val),2) 
  				WHEN type_ventilation_ca_ghs = 'PMJ' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base.cti_division(base_remboursement_total_sauf_transition_sauf_ame * pms_dur_coeff * rum_pmsval.pmj_val , rss_pmsval.pmj_val),2) 
  			ELSE round(base_remboursement_total_sauf_transition_sauf_ame * dur_coeff,2) END,
  		base_remboursement = 0,
  		sejour_remboursable_rum = 
  			CASE 
  				WHEN type_ventilation_ca_ghs = 'DUR' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(sejour_remboursable_2 * dur_coeff,2)
  				WHEN type_ventilation_ca_ghs = 'PMS' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base.cti_division(sejour_remboursable_2 * pms_dur_coeff * rum_pmsval.pms_val , rss_pmsval.pms_val),2) 
  				WHEN type_ventilation_ca_ghs = 'PMJ' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base.cti_division(sejour_remboursable_2 * pms_dur_coeff * rum_pmsval.pmj_val , rss_pmsval.pmj_val),2) 
  			ELSE round(sejour_remboursable_2 * dur_coeff,2) END,
  		sejour_remboursable = 0
  	FROM rum_pmsval
  	JOIN rss_pmsval ON rum_pmsval.rss_id = rss_pmsval.rss_id
  	, pmsi.t_prestations
  	WHERE 
  		p_rsf_detail.rss_id = rum_pmsval.rss_id
  		AND prestation_id = t_prestations.oid
  		AND p_rsf_detail.no_rum = rum_pmsval.no_rum
  		AND est_ligne_rum = '1'
  		AND t_prestations.code IN ('GHS', 'EXH')
  		AND rss_pmsval.pms_val <> 0
  		AND (
  			base_remboursement_rum IS DISTINCT FROM 
  			CASE 
  				WHEN type_ventilation_ca_ghs = 'DUR' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base_remboursement_total * dur_coeff,2)
  				WHEN type_ventilation_ca_ghs = 'PMS' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base.cti_division(base_remboursement_total * pms_dur_coeff * rum_pmsval.pms_val , rss_pmsval.pms_val),2) 
  				WHEN type_ventilation_ca_ghs = 'PMJ' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base.cti_division(base_remboursement_total * pms_dur_coeff * rum_pmsval.pmj_val , rss_pmsval.pmj_val),2) 
  			ELSE round(base_remboursement_total * dur_coeff,2) END 
  			OR
  			base_remboursement_rum_sauf_transition IS DISTINCT FROM 
  			CASE 
  				WHEN type_ventilation_ca_ghs = 'DUR' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base_remboursement_total_sauf_transition * dur_coeff,2)
  				WHEN type_ventilation_ca_ghs = 'PMS' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base.cti_division(base_remboursement_total_sauf_transition * pms_dur_coeff * rum_pmsval.pms_val , rss_pmsval.pms_val),2) 
  				WHEN type_ventilation_ca_ghs = 'PMJ' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base.cti_division(base_remboursement_total_sauf_transition * pms_dur_coeff * rum_pmsval.pmj_val , rss_pmsval.pmj_val),2) 
  			ELSE round(base_remboursement_total_sauf_transition * dur_coeff,2) END  
  			OR
  			base_remboursement_rum_sauf_transition_sauf_ame IS DISTINCT FROM 
  			CASE 
  				WHEN type_ventilation_ca_ghs = 'DUR' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base_remboursement_total_sauf_transition_sauf_ame * dur_coeff,2)
  				WHEN type_ventilation_ca_ghs = 'PMS' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base.cti_division(base_remboursement_total_sauf_transition_sauf_ame * pms_dur_coeff * rum_pmsval.pms_val , rss_pmsval.pms_val),2) 
  				WHEN type_ventilation_ca_ghs = 'PMJ' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base.cti_division(base_remboursement_total_sauf_transition_sauf_ame * pms_dur_coeff * rum_pmsval.pmj_val , rss_pmsval.pmj_val),2) 
  			ELSE round(base_remboursement_total_sauf_transition_sauf_ame * dur_coeff,2) END 
  			OR
  			base_remboursement IS DISTINCT FROM 0
  			OR
  			sejour_remboursable_rum IS DISTINCT FROM  
  			CASE 
  				WHEN type_ventilation_ca_ghs = 'DUR' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(sejour_remboursable_2 * dur_coeff,2)
  				WHEN type_ventilation_ca_ghs = 'PMS' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base.cti_division(sejour_remboursable_2 * pms_dur_coeff * rum_pmsval.pms_val , rss_pmsval.pms_val),2) 
  				WHEN type_ventilation_ca_ghs = 'PMJ' AND type_ventilation_ca_special_um IS DISTINCT FROM '1' THEN round(base.cti_division(sejour_remboursable_2 * pms_dur_coeff * rum_pmsval.pmj_val , rss_pmsval.pmj_val),2) 
  			ELSE round(sejour_remboursable_2 * dur_coeff,2) END
  			OR
  			sejour_remboursable IS DISTINCT FROM 0
  		);
  	
  	
  	UPDATE w_rsf_detail_rum p_rsf_detail SET
  		base_remboursement_rum = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_01 THEN base_remboursement_total ELSE 0 END,
  		base_remboursement_rum_sauf_transition = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_01 THEN base_remboursement_total_sauf_transition ELSE 0 END,
  		base_remboursement_rum_sauf_transition_sauf_ame = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_01 THEN base_remboursement_total_sauf_transition_sauf_ame ELSE 0 END,
  		sejour_remboursable_rum = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_01 THEN sejour_remboursable_2 ELSE 0 END
  	FROM rum_pmsval, rss_pmsval, pmsi.t_prestations, rss_resume
  	WHERE 
  		p_rsf_detail.rss_id = rum_pmsval.rss_id
  		AND rum_pmsval.rss_id = rss_pmsval.rss_id
  		AND rum_pmsval.rss_id = rss_resume.rss_id
  		AND prestation_id = t_prestations.oid
  		AND p_rsf_detail.no_rum = rum_pmsval.no_rum
  		AND est_ligne_rum = '1'
  		AND t_prestations.code = 'REA'
  		AND (
  			base_remboursement_rum IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_01 THEN base_remboursement_total ELSE 0 END 
  			OR
  			base_remboursement_rum_sauf_transition IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_01 THEN base_remboursement_total_sauf_transition ELSE 0 END 
  			OR
  			base_remboursement_rum_sauf_transition_sauf_ame IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_01 THEN base_remboursement_total_sauf_transition_sauf_ame ELSE 0 END 
  			OR
  			sejour_remboursable_rum IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_01 THEN sejour_remboursable_2 ELSE 0 END
  		);
  	
  	UPDATE w_rsf_detail_rum p_rsf_detail SET
  		base_remboursement_rum = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_13 THEN base_remboursement_total ELSE 0 END,
  		base_remboursement_rum_sauf_transition = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_13 THEN base_remboursement_total_sauf_transition ELSE 0 END,
  		base_remboursement_rum_sauf_transition_sauf_ame = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_13 THEN base_remboursement_total_sauf_transition_sauf_ame ELSE 0 END,
  		sejour_remboursable_rum = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_13 THEN sejour_remboursable_2 ELSE 0 END
  	FROM rum_pmsval, rss_pmsval, pmsi.t_prestations, rss_resume
  	WHERE 
  		p_rsf_detail.rss_id = rum_pmsval.rss_id
  		AND rum_pmsval.rss_id = rss_pmsval.rss_id
  		AND rum_pmsval.rss_id = rss_resume.rss_id
  		AND prestation_id = t_prestations.oid
  		AND p_rsf_detail.no_rum = rum_pmsval.no_rum
  		AND est_ligne_rum = '1'
  		AND t_prestations.code = 'REP'
  		AND (
  			base_remboursement_rum IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_13 THEN base_remboursement_total ELSE 0 END
  			OR
  			base_remboursement_rum_sauf_transition IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_13 THEN base_remboursement_total_sauf_transition ELSE 0 END
  			OR
  			base_remboursement_rum_sauf_transition_sauf_ame IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_13 THEN base_remboursement_total_sauf_transition_sauf_ame ELSE 0 END
  			OR
  			sejour_remboursable_rum IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_13 THEN sejour_remboursable_2 ELSE 0 END
  			);
  	
  	
  	UPDATE w_rsf_detail_rum p_rsf_detail SET
  		base_remboursement_rum = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_02 THEN base_remboursement_total ELSE 0 END,
  		base_remboursement_rum_sauf_transition = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_02 THEN base_remboursement_total_sauf_transition ELSE 0 END,
  		base_remboursement_rum_sauf_transition_sauf_ame = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_02 THEN base_remboursement_total_sauf_transition_sauf_ame ELSE 0 END,
  		sejour_remboursable_rum = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_02 THEN sejour_remboursable_2 ELSE 0 END
  	FROM rum_pmsval, rss_pmsval, pmsi.t_prestations, rss_resume
  	WHERE 
  		p_rsf_detail.rss_id = rum_pmsval.rss_id
  		AND rum_pmsval.rss_id = rss_pmsval.rss_id
  		AND rum_pmsval.rss_id = rss_resume.rss_id
  		AND prestation_id = t_prestations.oid
  		AND p_rsf_detail.no_rum = rum_pmsval.no_rum
  		AND est_ligne_rum = '1'
  		AND t_prestations.code = 'STF'
  		AND (
  			base_remboursement_rum IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_02 THEN base_remboursement_total ELSE 0 END
  			OR
  			base_remboursement_rum_sauf_transition IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_02 THEN base_remboursement_total_sauf_transition ELSE 0 END
  			OR
  			base_remboursement_rum_sauf_transition_sauf_ame IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_02 THEN base_remboursement_total_sauf_transition_sauf_ame ELSE 0 END
  			OR
  			sejour_remboursable_rum IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_02 THEN sejour_remboursable_2 ELSE 0 END
  			);
  	
  	
  	UPDATE w_rsf_detail_rum p_rsf_detail SET
  		base_remboursement_rum = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_03 THEN base_remboursement_total ELSE 0 END,
  		base_remboursement_rum_sauf_transition = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_03 THEN base_remboursement_total_sauf_transition ELSE 0 END,
  		base_remboursement_rum_sauf_transition_sauf_ame = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_03 THEN base_remboursement_total_sauf_transition_sauf_ame ELSE 0 END,
  		sejour_remboursable_rum = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_03 THEN sejour_remboursable_2 ELSE 0 END
  	FROM rum_pmsval, rss_pmsval, pmsi.t_prestations, rss_resume
  	WHERE 
  		p_rsf_detail.rss_id = rum_pmsval.rss_id
  		AND rum_pmsval.rss_id = rss_pmsval.rss_id
  		AND rum_pmsval.rss_id = rss_resume.rss_id
  		AND prestation_id = t_prestations.oid
  		AND p_rsf_detail.no_rum = rum_pmsval.no_rum
  		AND est_ligne_rum = '1'
  		AND t_prestations.code = 'SRC'
  		AND (
  			base_remboursement_rum IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_03 THEN base_remboursement_total ELSE 0 END
  			OR
  			base_remboursement_rum_sauf_transition IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_03 THEN base_remboursement_total_sauf_transition ELSE 0 END
  			OR
  			base_remboursement_rum_sauf_transition_sauf_ame IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_03 THEN base_remboursement_total_sauf_transition_sauf_ame ELSE 0 END
  			OR
  			sejour_remboursable_rum IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_03 THEN sejour_remboursable_2 ELSE 0 END
  		);
  	
  	
  	UPDATE w_rsf_detail_rum p_rsf_detail SET
  		base_remboursement_rum = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_04_05_06 THEN base_remboursement_total ELSE 0 END,
  		base_remboursement_rum_sauf_transition = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_04_05_06 THEN base_remboursement_total_sauf_transition ELSE 0 END,
  		base_remboursement_rum_sauf_transition_sauf_ame = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_04_05_06 THEN base_remboursement_total_sauf_transition_sauf_ame ELSE 0 END,
  		sejour_remboursable_rum = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_04_05_06 THEN sejour_remboursable_2 ELSE 0 END
  	FROM rum_pmsval, rss_pmsval, pmsi.t_prestations, rss_resume
  	WHERE 
  		p_rsf_detail.rss_id = rum_pmsval.rss_id
  		AND rum_pmsval.rss_id = rss_pmsval.rss_id
  		AND rum_pmsval.rss_id = rss_resume.rss_id
  		AND prestation_id = t_prestations.oid
  		AND p_rsf_detail.no_rum = rum_pmsval.no_rum
  		AND est_ligne_rum = '1'
  		AND t_prestations.code IN ('NN1', 'NN2', 'NN3')
  		AND (
  			base_remboursement_rum IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_04_05_06 THEN base_remboursement_total ELSE 0 END
  			OR
  			base_remboursement_rum_sauf_transition IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_04_05_06 THEN base_remboursement_total_sauf_transition ELSE 0 END
  			OR
  			base_remboursement_rum_sauf_transition_sauf_ame IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_04_05_06 THEN base_remboursement_total_sauf_transition_sauf_ame ELSE 0 END
  			OR
  			sejour_remboursable_rum IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.premier_rum_04_05_06 THEN sejour_remboursable_2 ELSE 0 END
  		);
  	
  	
  	UPDATE w_rsf_detail_rum p_rsf_detail SET
  		base_remboursement_rum = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.dernier_rum THEN base_remboursement_total ELSE 0 END,
  		base_remboursement_rum_sauf_transition = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.dernier_rum THEN base_remboursement_total_sauf_transition ELSE 0 END,
  		base_remboursement_rum_sauf_transition_sauf_ame = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.dernier_rum THEN base_remboursement_total_sauf_transition_sauf_ame ELSE 0 END,
  		sejour_remboursable_rum = CASE WHEN p_rsf_detail.no_rum = rss_pmsval.dernier_rum THEN sejour_remboursable_2 ELSE 0 END
  	FROM rum_pmsval, rss_pmsval, pmsi.t_prestations, rss_resume
  	WHERE 
  		p_rsf_detail.rss_id = rum_pmsval.rss_id
  		AND rum_pmsval.rss_id = rss_pmsval.rss_id
  		AND rum_pmsval.rss_id = rss_resume.rss_id
  		AND prestation_id = t_prestations.oid
  		AND p_rsf_detail.no_rum = rum_pmsval.no_rum
  		AND est_ligne_rum = '1'
  		AND t_prestations.code IN ('FJA', 'FJR', 'FRL')
  		AND (
  			base_remboursement_rum IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.dernier_rum THEN base_remboursement_total ELSE 0 END
  			OR
  			base_remboursement_rum_sauf_transition IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.dernier_rum THEN base_remboursement_total_sauf_transition ELSE 0 END
  			OR
  			base_remboursement_rum_sauf_transition_sauf_ame IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.dernier_rum THEN base_remboursement_total_sauf_transition_sauf_ame ELSE 0 END
  			OR
  			sejour_remboursable_rum IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rss_pmsval.dernier_rum THEN sejour_remboursable_2 ELSE 0 END
  		);
  	
  	
  	DROP TABLE IF EXISTS rum_by_date;
  	CREATE TEMP TABLE rum_by_date AS
  	SELECT p_rss_rum.rss_id, p_calendrier.date, MIN(p_rss_rum.no_rum) as no_rum
  	FROM pmsi.p_rss_rum,  rss_resume, base.p_calendrier
  	WHERE p_rss_rum.rss_id = rss_resume.rss_id
  	AND p_calendrier.date BETWEEN p_rss_rum.date_entree AND p_rss_rum.date_sortie
  	GROUP BY p_rss_rum.rss_id, p_calendrier.date
  	ORDER BY p_rss_rum.rss_id, p_calendrier.date;
  	
  	
  	UPDATE w_rsf_detail_rum p_rsf_detail SET
  		base_remboursement_rum = CASE WHEN p_rsf_detail.no_rum = rum_by_date.no_rum THEN base_remboursement_total ELSE 0 END,
  		base_remboursement_rum_sauf_transition = CASE WHEN p_rsf_detail.no_rum = rum_by_date.no_rum THEN base_remboursement_total_sauf_transition ELSE 0 END,
  		base_remboursement_rum_sauf_transition_sauf_ame = CASE WHEN p_rsf_detail.no_rum = rum_by_date.no_rum THEN base_remboursement_total_sauf_transition_sauf_ame ELSE 0 END,
  		sejour_remboursable_rum = CASE WHEN p_rsf_detail.no_rum = rum_by_date.no_rum THEN sejour_remboursable_2 ELSE 0 END
  	FROM pmsi.t_prestations, rss_resume, rum_by_date
  	WHERE 
  		rss_resume.rss_id = p_rsf_detail.rss_id
  		AND rum_by_date.rss_id = p_rsf_detail.rss_id
  		AND rum_by_date.date = p_rsf_detail.date_debut 
  		AND prestation_id = t_prestations.oid
  		AND est_ligne_rum = '1'
  		AND t_prestations.code NOT IN ('GHS', 'EXH', 'REA', 'RAP', 'REP', 'SRC', 'STF', 'NN1', 'NN2', 'NN2', 'NN3', 'FJA', 'FJR', 'FRL')
  		AND (
  			base_remboursement_rum IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rum_by_date.no_rum THEN base_remboursement_total ELSE 0 END
  			OR
  			base_remboursement_rum_sauf_transition IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rum_by_date.no_rum THEN base_remboursement_total_sauf_transition ELSE 0 END
  			OR
  			base_remboursement_rum_sauf_transition_sauf_ame IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rum_by_date.no_rum THEN base_remboursement_total_sauf_transition_sauf_ame ELSE 0 END
  			OR
  			sejour_remboursable_rum IS DISTINCT FROM CASE WHEN p_rsf_detail.no_rum = rum_by_date.no_rum THEN sejour_remboursable_2 ELSE 0 END
  		)
  	;
  	
  	
  	
  	DROP TABLE IF EXISTS w_rsf_detail_rum_presta;
  	CREATE TEMP TABLE w_rsf_detail_rum_presta AS
  	SELECT p_rsf_detail.rss_id,
  		p_rsf_detail.prestation_id,
  		MIN(CTID) AS from_CTID,
  		SUM(p_rsf_detail.base_remboursement_rum) AS base_remboursement_rum_presta, 
  		SUM(p_rsf_detail.base_remboursement_rum_sauf_transition) AS base_remboursement_rum_presta_sauf_transition, 
  		SUM(p_rsf_detail.base_remboursement_rum_sauf_transition_sauf_ame) AS base_remboursement_rum_presta_sauf_transition_sauf_ame, 
  		SUM(p_rsf_detail.sejour_remboursable_rum) AS sejour_remboursable_rum_presta,
  		MIN(CASE WHEN base_remboursement_rum <> 0 THEN no_rum ELSE 999 END) as premier_rum
  	FROM 
  		w_rsf_detail_rum p_rsf_detail
  	GROUP BY 1,2;
  	
  	
  	UPDATE w_rsf_detail_rum p_rsf_detail SET
  		base_remboursement_rum = base_remboursement_rum + ecart_arrondi,
  		base_remboursement_rum_sauf_transition = base_remboursement_rum_sauf_transition + ecart_arrondi_ht,
  		base_remboursement_rum_sauf_transition_sauf_ame = base_remboursement_rum_sauf_transition_sauf_ame + ecart_arrondi_hthame,
  		sejour_remboursable_rum = sejour_remboursable_rum + ecart_arrondi_r
  	FROM pmsi.t_prestations, (
  		SELECT w_rsf_detail_presta.rss_id, w_rsf_detail_presta.prestation_id, premier_rum,
  			base_remboursement_presta - COALESCE(base_remboursement_rum_presta) AS ecart_arrondi,
  			base_remboursement_presta_sauf_transition - COALESCE(base_remboursement_rum_presta_sauf_transition) AS ecart_arrondi_ht,
  			base_remboursement_presta_sauf_transition_sauf_ame - COALESCE(base_remboursement_rum_presta_sauf_transition_sauf_ame) AS ecart_arrondi_hthame,
  			sejour_remboursable_presta - COALESCE(sejour_remboursable_rum_presta) AS ecart_arrondi_r
  		FROM w_rsf_detail_presta
  		LEFT JOIN w_rsf_detail_rum_presta ON w_rsf_detail_rum_presta.rss_id = w_rsf_detail_presta.rss_id AND
  						w_rsf_detail_rum_presta.prestation_id = w_rsf_detail_presta.prestation_id
  		WHERE 
  			base_remboursement_presta IS DISTINCT FROM COALESCE(base_remboursement_rum_presta) OR
  			base_remboursement_presta_sauf_transition IS DISTINCT FROM COALESCE(base_remboursement_rum_presta_sauf_transition) OR
  			base_remboursement_presta_sauf_transition_sauf_ame IS DISTINCT FROM COALESCE(base_remboursement_rum_presta_sauf_transition_sauf_ame) OR
  			sejour_remboursable_presta IS DISTINCT FROM COALESCE(sejour_remboursable_rum_presta) 
  		) subview
  	WHERE p_rsf_detail.rss_id = subview.rss_id
  		AND p_rsf_detail.prestation_id = t_prestations.oid
  		AND p_rsf_detail.prestation_id = subview.prestation_id
  		
  		AND p_rsf_detail.no_rum = subview.premier_rum
  		AND (
  			base_remboursement_rum IS DISTINCT FROM base_remboursement_rum + ecart_arrondi 
  			OR
  			base_remboursement_rum_sauf_transition IS DISTINCT FROM base_remboursement_rum_sauf_transition + ecart_arrondi_ht 
  			OR
  			base_remboursement_rum_sauf_transition_sauf_ame IS DISTINCT FROM base_remboursement_rum_sauf_transition_sauf_ame + ecart_arrondi_hthame 
  			OR
  			sejour_remboursable_rum IS DISTINCT FROM sejour_remboursable_rum + ecart_arrondi_r
  		);
  	
  	
  	DELETE FROM w_rsf_detail_rum p_rsf_detail WHERE est_ligne_rss <> '1' AND base_remboursement_rum = 0 AND sejour_remboursable_rum = 0;
  	-- Validation
  	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,
  		coefficient_mco,
  		sejour_remboursable_2,
  		ghs_id,
  		nature_noemie,
  		prestation_id,
  		rss_id,
  		type,
  		coefficient_geo,
  		est_ligne_rss,
  		est_ligne_rum,
  		no_rum,
  		unite_medicale_id,
  		base_remboursement_rum,
  		base_remboursement_rum_sauf_transition,
  		base_remboursement_rum_sauf_transition_sauf_ame,
  		montant_supplement_nf,
  		sejour_remboursable_rum,
  		pec_fj_id,
  		date_comptable,
  		operation_lamda,
  		fides_detail)
  	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,
  		nature_noemie,
  		prestation_id,
  		rss_id,
  		type,
  		coefficient_geo,
  		est_ligne_rss,
  		est_ligne_rum,
  		no_rum,
  		unite_medicale_id,
  		base_remboursement_rum,
  		base_remboursement_rum_sauf_transition,
  		base_remboursement_rum_sauf_transition_sauf_ame,
  		montant_supplement_nf,
  		sejour_remboursable_rum,
  		pec_fj_id,
  		date_comptable,
  		operation_lamda,
  		fides_detail
  	FROM w_rsf_detail_rum;
  	-- Spécial BACLESSE 
  	-- Ventilation suppléments radio à UM SRT
  	-- Ventilation supplément RAP hors UM 'PRT' dans UM 'SRT' (Tout ce qui n'est pas PRT, on ventile dans SRT)
  	IF EXISTS (SELECT * FROM base.t_finess WHERE code IN ('140000639', '140000555') LIMIT 1) THEN
  		UPDATE pmsi.p_rsf_detail SET 
  		unite_medicale_id = (SELECT oid FROM pmsi.t_unites_medicales WHERE code = 'SRT') 
  		WHERE est_ligne_rum = 1 AND prestation_id IN (SELECT oid FROM pmsi.t_prestations WHERE code like 'IRRE%' OR code = 'APHE');
  		
  		UPDATE pmsi.p_rsf_detail SET
  			unite_medicale_id = (SELECT oid FROM pmsi.t_unites_medicales WHERE code = 'SRT')
  		WHERE est_ligne_rum = 1 AND prestation_id IN (SELECT oid FROM pmsi.t_prestations WHERE code  = 'RAP') AND 
  			unite_medicale_id <> (SELECT oid FROM pmsi.t_unites_medicales WHERE code = 'PRT') ;
  		UPDATE pmsi.p_rsf_detail SET 
  			unite_medicale_id = (SELECT oid FROM pmsi.t_unites_medicales WHERE code = 'SRT') 
  		WHERE est_ligne_rum = 1 AND prestation_id IN (SELECT oid FROM pmsi.t_prestations WHERE code  = 'RAP') AND 
  			unite_medicale_id <> (SELECT oid FROM pmsi.t_unites_medicales WHERE code = 'PRT');
  	END IF;
  		
  	RETURN 'OK';
  END;