return: text
lang: plpgsql
src: |
  DECLARE	
  	
  	
  	result TEXT;
  BEGIN
     
      -- récupérer les dossiers MCO 
      DROP TABLE IF EXISTS w_rsf_detail;
      CREATE TEMP TABLE w_rsf_detail AS
  
      SELECT rss_id , p_rsf_detail.no_rss, 
  		   p_rsf_detail.finess , 
  		   p_rss.date_sortie, 
  		   p_rsf_detail.ghs_id ,
  		   sum(p_rsf_detail.base_remboursement) as w_base_remboursement
          FROM pmsi.p_rsf_detail
          JOIN pmsi.p_rss ON p_rss.oid = p_rsf_detail.rss_id
      WHERE 1=1
          AND p_rsf_detail.ghs_id > 0
          AND p_rsf_detail.base_remboursement <> 18
      GROUP BY 1,2,3,4,5
          ;
      
      ALTER TABLE w_rsf_detail ADD COLUMN w_ca_ghs_sans_coefficient numeric (13,2) DEFAULT 0;
  
     
      -- CA_GHS brut
  
     UPDATE w_rsf_detail 
          SET w_ca_ghs_sans_coefficient =
              CASE WHEN t_finess.type_etablissement = ANY (ARRAY['2'::text, '3'::text]) THEN 
                  CASE 
                      WHEN w_rsf_detail.date_sortie >= t_ghs.date_debut_public_1 AND w_rsf_detail.date_sortie <= t_ghs.date_fin_public_1 THEN t_ghs.tarif_ghs_public_1 
                      WHEN w_rsf_detail.date_sortie >= t_ghs.date_debut_public_2 AND w_rsf_detail.date_sortie <= t_ghs.date_fin_public_2 THEN t_ghs.tarif_ghs_public_2 
                      WHEN w_rsf_detail.date_sortie >= t_ghs.date_debut_public_3 AND w_rsf_detail.date_sortie <= t_ghs.date_fin_public_3 THEN t_ghs.tarif_ghs_public_3 
                      WHEN w_rsf_detail.date_sortie >= t_ghs.date_debut_public_4 AND w_rsf_detail.date_sortie <= t_ghs.date_fin_public_4 THEN t_ghs.tarif_ghs_public_4 
                      WHEN w_rsf_detail.date_sortie >= t_ghs.date_debut_public_5 AND w_rsf_detail.date_sortie <= t_ghs.date_fin_public_5 THEN t_ghs.tarif_ghs_public_5 
                      WHEN w_rsf_detail.date_sortie >= t_ghs.date_debut_public_6 AND w_rsf_detail.date_sortie <= t_ghs.date_fin_public_6 THEN t_ghs.tarif_ghs_public_6 
                      WHEN w_rsf_detail.date_sortie >= t_ghs.date_debut_public_7 AND w_rsf_detail.date_sortie <= t_ghs.date_fin_public_7 THEN t_ghs.tarif_ghs_public_7 
                      WHEN w_rsf_detail.date_sortie >= t_ghs.date_debut_public_8 AND w_rsf_detail.date_sortie <= t_ghs.date_fin_public_8 THEN t_ghs.tarif_ghs_public_8 
                      WHEN w_rsf_detail.date_sortie >= t_ghs.date_debut_public_9 AND w_rsf_detail.date_sortie <= t_ghs.date_fin_public_9 THEN t_ghs.tarif_ghs_public_9 
                  ELSE 0::numeric 
                  END
  			ELSE  CASE                    
  			 		WHEN w_rsf_detail.date_sortie >= t_ghs.date_debut_prive_1 AND w_rsf_detail.date_sortie <= t_ghs.date_fin_prive_1 THEN t_ghs.tarif_ghs_prive_1 
                      WHEN w_rsf_detail.date_sortie >= t_ghs.date_debut_prive_2 AND w_rsf_detail.date_sortie <= t_ghs.date_fin_prive_2 THEN t_ghs.tarif_ghs_prive_2 
                      WHEN w_rsf_detail.date_sortie >= t_ghs.date_debut_prive_3 AND w_rsf_detail.date_sortie <= t_ghs.date_fin_prive_3 THEN t_ghs.tarif_ghs_prive_3 
                      WHEN w_rsf_detail.date_sortie >= t_ghs.date_debut_prive_4 AND w_rsf_detail.date_sortie <= t_ghs.date_fin_prive_4 THEN t_ghs.tarif_ghs_prive_4 
                      WHEN w_rsf_detail.date_sortie >= t_ghs.date_debut_prive_5 AND w_rsf_detail.date_sortie <= t_ghs.date_fin_prive_5 THEN t_ghs.tarif_ghs_prive_5 
                      WHEN w_rsf_detail.date_sortie >= t_ghs.date_debut_prive_6 AND w_rsf_detail.date_sortie <= t_ghs.date_fin_prive_6 THEN t_ghs.tarif_ghs_prive_6 
                      WHEN w_rsf_detail.date_sortie >= t_ghs.date_debut_prive_7 AND w_rsf_detail.date_sortie <= t_ghs.date_fin_prive_7 THEN t_ghs.tarif_ghs_prive_7 
                      WHEN w_rsf_detail.date_sortie >= t_ghs.date_debut_prive_8 AND w_rsf_detail.date_sortie <= t_ghs.date_fin_prive_8 THEN t_ghs.tarif_ghs_prive_8 
                      WHEN w_rsf_detail.date_sortie >= t_ghs.date_debut_prive_9 AND w_rsf_detail.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.oid = w_rsf_detail.ghs_id 
  	    	AND w_rsf_detail.finess = t_finess.code
  	    	AND ghs_id <> 0
      ;
  
      DROP TABLE IF EXISTS w_rsf_total;
      CREATE TEMP TABLE w_rsf_total  AS
  
      SELECT rss_id , 
             no_rss , 
             sum (w_base_remboursement), 
             sum(w_ca_ghs_sans_coefficient) as w_ca_ghs_sans_coefficient
      FROM w_rsf_detail
      GROUP BY 1,2;  
  
          UPDATE pmsi.p_rsf_total 
              SET ca_ghs_sans_coefficient = w_rsf_total.w_ca_ghs_sans_coefficient
          FROM w_rsf_total
              WHERE p_rsf_total.rss_id = w_rsf_total.rss_id
              AND ca_ghs_sans_coefficient IS DISTINCT FROM w_rsf_total.w_ca_ghs_sans_coefficient;
  
  	
  		
  	RETURN 'OK';
  END;