return: text
lang: plpgsql
src: |
  DECLARE	
  	
  	
  	result TEXT;
  BEGIN
  
  	DROP TABLE IF EXISTS w_raac_impact;
  	CREATE TEMP TABLE w_raac_impact AS
  	SELECT  finess,
  		no_rss ,
  		date_sortie,
  		p_rss.ghm_id as p_ghm_id,
  		t_ghm.oid as t_ghm_id,
  		t_ghm.code as t_ghm_code,
  		p_rss.ghs_id as p_ghs_id,
  		t_ghs.oid as t_ghs_id ,
  		sum(ca_ghs) as ca_ghs_avec_raac,
  		0 as ca_ghs_sans_raac_sans_coeff,
  		0::numeric as coefficient_mco,
  		0 as ca_ghs_sans_raac
  	FROM pmsi.p_rss 
  	join pmsi.t_ghm ON p_rss.ghm_id = t_ghm.oid
  	join  pmsi.t_ghs on p_rss.ghs_id = t_ghs.oid 
  	where 1=1 
  	AND raac_id > 0 
  	AND t_ghs.ghm_id <> p_rss.ghm_id
  	group by 1,2,3,4,5,6,7,8
  	order by 3;
  
  	DROP TABLE IF EXISTS w_raac_sans_impact;
  	CREATE TEMP TABLE w_raac_sans_impact AS
  	SELECT  finess,
  		no_rss ,
  		date_sortie,
  		p_rss.ghm_id as p_ghm_id,
  		t_ghm.oid as t_ghm_id,
  		t_ghm.code as t_ghm_code,
  		p_rss.ghs_id as p_ghs_id,
  		t_ghs.oid as t_ghs_id ,
  		sum(ca_ghs) as ca_ghs_avec_raac,
  		0 as ca_ghs_sans_raac_sans_coeff,
  		0::numeric as coefficient_mco,
  		0 as ca_ghs_sans_raac
  	FROM pmsi.p_rss 
  	join pmsi.t_ghm ON p_rss.ghm_id = t_ghm.oid
  	join  pmsi.t_ghs on p_rss.ghs_id = t_ghs.oid 
  	where 1=1 
  	AND raac_id > 0 
  	AND t_ghs.ghm_id = p_rss.ghm_id
  	group by 1,2,3,4,5,6,7,8
  	order by 3;
  
  	UPDATE w_raac_impact
  	SET ca_ghs_sans_raac =  
  			CASE WHEN t_finess.type_etablissement = ANY (ARRAY['2'::text, '3'::text]) THEN 
  	                  CASE 
  	                      WHEN w_raac_impact.date_sortie >= t_ghs.date_debut_public_1 AND w_raac_impact.date_sortie <= t_ghs.date_fin_public_1 THEN t_ghs.tarif_ghs_public_1 
  	                      WHEN w_raac_impact.date_sortie >= t_ghs.date_debut_public_2 AND w_raac_impact.date_sortie <= t_ghs.date_fin_public_2 THEN t_ghs.tarif_ghs_public_2 
  	                      WHEN w_raac_impact.date_sortie >= t_ghs.date_debut_public_3 AND w_raac_impact.date_sortie <= t_ghs.date_fin_public_3 THEN t_ghs.tarif_ghs_public_3 
  	                      WHEN w_raac_impact.date_sortie >= t_ghs.date_debut_public_4 AND w_raac_impact.date_sortie <= t_ghs.date_fin_public_4 THEN t_ghs.tarif_ghs_public_4 
  	                      WHEN w_raac_impact.date_sortie >= t_ghs.date_debut_public_5 AND w_raac_impact.date_sortie <= t_ghs.date_fin_public_5 THEN t_ghs.tarif_ghs_public_5 
  	                      WHEN w_raac_impact.date_sortie >= t_ghs.date_debut_public_6 AND w_raac_impact.date_sortie <= t_ghs.date_fin_public_6 THEN t_ghs.tarif_ghs_public_6 
  	                      WHEN w_raac_impact.date_sortie >= t_ghs.date_debut_public_7 AND w_raac_impact.date_sortie <= t_ghs.date_fin_public_7 THEN t_ghs.tarif_ghs_public_7 
  	                      WHEN w_raac_impact.date_sortie >= t_ghs.date_debut_public_8 AND w_raac_impact.date_sortie <= t_ghs.date_fin_public_8 THEN t_ghs.tarif_ghs_public_8 
  	                      WHEN w_raac_impact.date_sortie >= t_ghs.date_debut_public_9 AND w_raac_impact.date_sortie <= t_ghs.date_fin_public_9 THEN t_ghs.tarif_ghs_public_9 
  	                  ELSE 0::numeric 
  	                  END
  	  			ELSE  CASE                    
  	  		      WHEN w_raac_impact.date_sortie >= t_ghs.date_debut_prive_1 AND w_raac_impact.date_sortie <= t_ghs.date_fin_prive_1 THEN t_ghs.tarif_ghs_prive_1 
  	                      WHEN w_raac_impact.date_sortie >= t_ghs.date_debut_prive_2 AND w_raac_impact.date_sortie <= t_ghs.date_fin_prive_2 THEN t_ghs.tarif_ghs_prive_2 
  	                      WHEN w_raac_impact.date_sortie >= t_ghs.date_debut_prive_3 AND w_raac_impact.date_sortie <= t_ghs.date_fin_prive_3 THEN t_ghs.tarif_ghs_prive_3 
  	                      WHEN w_raac_impact.date_sortie >= t_ghs.date_debut_prive_4 AND w_raac_impact.date_sortie <= t_ghs.date_fin_prive_4 THEN t_ghs.tarif_ghs_prive_4 
  	                      WHEN w_raac_impact.date_sortie >= t_ghs.date_debut_prive_5 AND w_raac_impact.date_sortie <= t_ghs.date_fin_prive_5 THEN t_ghs.tarif_ghs_prive_5 
  	                      WHEN w_raac_impact.date_sortie >= t_ghs.date_debut_prive_6 AND w_raac_impact.date_sortie <= t_ghs.date_fin_prive_6 THEN t_ghs.tarif_ghs_prive_6 
  	                      WHEN w_raac_impact.date_sortie >= t_ghs.date_debut_prive_7 AND w_raac_impact.date_sortie <= t_ghs.date_fin_prive_7 THEN t_ghs.tarif_ghs_prive_7 
  	                      WHEN w_raac_impact.date_sortie >= t_ghs.date_debut_prive_8 AND w_raac_impact.date_sortie <= t_ghs.date_fin_prive_8 THEN t_ghs.tarif_ghs_prive_8 
  	                      WHEN w_raac_impact.date_sortie >= t_ghs.date_debut_prive_9 AND w_raac_impact.date_sortie <= t_ghs.date_fin_prive_9 THEN t_ghs.tarif_ghs_prive_9  
  	                  ELSE 0::numeric 
  	                  END
  	  			END 
  	      FROM  pmsi.t_ghs,base.t_finess
  	          WHERE  1=1
  	  	    	AND t_ghs.ghm_id = w_raac_impact.p_ghm_id 
  	  	    	AND w_raac_impact.finess = t_finess.code
  	;
  
  	UPDATE w_raac_impact 
          SET coefficient_mco = v_rsf_detail_1.coefficient_mco
  	FROM pmsi.v_rsf_detail_1
  		where w_raac_impact.no_rss = v_rsf_detail_1.no_rss  AND prestation_code = 'GHS';
  
  	UPDATE pmsi.p_rss 
  		SET CA_GHS_RAAC = ca_ghs_sans_raac * w_raac_impact.coefficient_mco
  	FROM w_raac_impact 
  		WHERE w_raac_impact.no_rss = p_rss.no_rss;
  
  	UPDATE pmsi.p_rss 
  		SET CA_GHS_RAAC = ca_ghs_avec_raac
  	FROM w_raac_sans_impact 
  		WHERE w_raac_sans_impact.no_rss = p_rss.no_rss;
  
  	RETURN 'OK';
  END;