return: text
lang: plpgsql
src: |
  DECLARE	
  	
  	
  	result TEXT;
  BEGIN
  	
      DROP TABLE IF EXISTS w_finess_mco_date
      ;
      CREATE TEMP TABLE w_finess_mco_date AS
      SELECT 
          p_rss.finess, 
          CASE WHEN SUBSTR (date_sortie ,6, 2 )::int > 02 THEN SUBSTR (date_sortie ,1 ,4 ) ELSE (SUBSTR (date_sortie ,1 ,4 )::int - 1 )::text END || '-03-01' as date_ref , 
          coefficient_mco,
          count(*) AS nb
      FROM pmsi.p_rsf_detail 
      JOIN pmsi.p_rss ON rss_id = p_rss.oid ANd p_rss.en_cours = '0'
      JOIN pmsi.t_prestations ON t_prestations.oid = p_rsf_detail.prestation_id AND t_prestations.code = 'GHS'
      GROUP BY 1,2,3
      ;
  
      DROP TABLE IF EXISTS w_finess_mco
      ;
      CREATE TEMP TABLE w_finess_mco AS
      SELECT 
  	finess, 
  	date_ref,
  	(MAX(Array[to_char(nb,'FM000000000000'),coefficient_mco::text]))[2]::numeric AS coefficient_mco
      FROM w_finess_mco_date
      GROUP BY 1,2
      ORDER BY 3
      ;      
                      
      DROP TABLE IF EXISTS w_val_encours;
      CREATE TEMP TABLE w_val_encours AS
      SELECT p_rss.finess, p_rss.oid AS rss_id, no_rss, no_sejour_administratif,
          date_entree, date_sortie, date_sortie - date_entree AS duree_sejour,
          en_cours_etat, ghs_id, t_ghs.code AS ghs_code, p_rss.ghm_id, t_ghm.code AS ghm_code,
          CASE 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_1 AND p_rss.date_sortie <= t_ghs.date_fin_prive_1 THEN t_ghs.tarif_ghs_prive_1 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_2 AND p_rss.date_sortie <= t_ghs.date_fin_prive_2 THEN t_ghs.tarif_ghs_prive_2 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_3 AND p_rss.date_sortie <= t_ghs.date_fin_prive_3 THEN t_ghs.tarif_ghs_prive_3 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_4 AND p_rss.date_sortie <= t_ghs.date_fin_prive_4 THEN t_ghs.tarif_ghs_prive_4 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_5 AND p_rss.date_sortie <= t_ghs.date_fin_prive_5 THEN t_ghs.tarif_ghs_prive_5 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_6 AND p_rss.date_sortie <= t_ghs.date_fin_prive_6 THEN t_ghs.tarif_ghs_prive_6 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_7 AND p_rss.date_sortie <= t_ghs.date_fin_prive_7 THEN t_ghs.tarif_ghs_prive_7 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_8 AND p_rss.date_sortie <= t_ghs.date_fin_prive_8 THEN t_ghs.tarif_ghs_prive_8 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_9 AND p_rss.date_sortie <= t_ghs.date_fin_prive_9 THEN t_ghs.tarif_ghs_prive_9 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_1 AND p_rss.date_sortie <= t_ghs.date_fin_public_1 THEN t_ghs.tarif_ghs_public_1 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_2 AND p_rss.date_sortie <= t_ghs.date_fin_public_2 THEN t_ghs.tarif_ghs_public_2 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_3 AND p_rss.date_sortie <= t_ghs.date_fin_public_3 THEN t_ghs.tarif_ghs_public_3 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_4 AND p_rss.date_sortie <= t_ghs.date_fin_public_4 THEN t_ghs.tarif_ghs_public_4 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_5 AND p_rss.date_sortie <= t_ghs.date_fin_public_5 THEN t_ghs.tarif_ghs_public_5 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_6 AND p_rss.date_sortie <= t_ghs.date_fin_public_6 THEN t_ghs.tarif_ghs_public_6 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_7 AND p_rss.date_sortie <= t_ghs.date_fin_public_7 THEN t_ghs.tarif_ghs_public_7 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_8 AND p_rss.date_sortie <= t_ghs.date_fin_public_8 THEN t_ghs.tarif_ghs_public_8 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_9 AND p_rss.date_sortie <= t_ghs.date_fin_public_9 THEN t_ghs.tarif_ghs_public_9 
          ELSE 0::numeric 
          END 
          AS tarif_ghs,
          CASE 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_1 AND p_rss.date_sortie <= t_ghs.date_fin_prive_1 THEN t_ghs.borne_haute_prive_1 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_2 AND p_rss.date_sortie <= t_ghs.date_fin_prive_2 THEN t_ghs.borne_haute_prive_2 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_3 AND p_rss.date_sortie <= t_ghs.date_fin_prive_3 THEN t_ghs.borne_haute_prive_3 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_4 AND p_rss.date_sortie <= t_ghs.date_fin_prive_4 THEN t_ghs.borne_haute_prive_4 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_5 AND p_rss.date_sortie <= t_ghs.date_fin_prive_5 THEN t_ghs.borne_haute_prive_5 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_6 AND p_rss.date_sortie <= t_ghs.date_fin_prive_6 THEN t_ghs.borne_haute_prive_6 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_7 AND p_rss.date_sortie <= t_ghs.date_fin_prive_7 THEN t_ghs.borne_haute_prive_7 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_8 AND p_rss.date_sortie <= t_ghs.date_fin_prive_8 THEN t_ghs.borne_haute_prive_8 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_9 AND p_rss.date_sortie <= t_ghs.date_fin_prive_9 THEN t_ghs.borne_haute_prive_9 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_1 AND p_rss.date_sortie <= t_ghs.date_fin_public_1 THEN t_ghs.borne_haute_public_1 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_2 AND p_rss.date_sortie <= t_ghs.date_fin_public_2 THEN t_ghs.borne_haute_public_2 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_3 AND p_rss.date_sortie <= t_ghs.date_fin_public_3 THEN t_ghs.borne_haute_public_3 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_4 AND p_rss.date_sortie <= t_ghs.date_fin_public_4 THEN t_ghs.borne_haute_public_4 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_5 AND p_rss.date_sortie <= t_ghs.date_fin_public_5 THEN t_ghs.borne_haute_public_5 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_6 AND p_rss.date_sortie <= t_ghs.date_fin_public_6 THEN t_ghs.borne_haute_public_6 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_7 AND p_rss.date_sortie <= t_ghs.date_fin_public_7 THEN t_ghs.borne_haute_public_7 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_8 AND p_rss.date_sortie <= t_ghs.date_fin_public_8 THEN t_ghs.borne_haute_public_8 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_9 AND p_rss.date_sortie <= t_ghs.date_fin_public_9 THEN t_ghs.borne_haute_public_9 
          ELSE 0::numeric 
          END 
          AS borne_haute,
          CASE 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_1 AND p_rss.date_sortie <= t_ghs.date_fin_prive_1 THEN t_ghs.tarif_exh_prive_1 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_2 AND p_rss.date_sortie <= t_ghs.date_fin_prive_2 THEN t_ghs.tarif_exh_prive_2 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_3 AND p_rss.date_sortie <= t_ghs.date_fin_prive_3 THEN t_ghs.tarif_exh_prive_3 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_4 AND p_rss.date_sortie <= t_ghs.date_fin_prive_4 THEN t_ghs.tarif_exh_prive_4 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_5 AND p_rss.date_sortie <= t_ghs.date_fin_prive_5 THEN t_ghs.tarif_exh_prive_5 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_6 AND p_rss.date_sortie <= t_ghs.date_fin_prive_6 THEN t_ghs.tarif_exh_prive_6 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_7 AND p_rss.date_sortie <= t_ghs.date_fin_prive_7 THEN t_ghs.tarif_exh_prive_7 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_8 AND p_rss.date_sortie <= t_ghs.date_fin_prive_8 THEN t_ghs.tarif_exh_prive_8 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_9 AND p_rss.date_sortie <= t_ghs.date_fin_prive_9 THEN t_ghs.tarif_exh_prive_9 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_1 AND p_rss.date_sortie <= t_ghs.date_fin_public_1 THEN t_ghs.tarif_exh_public_1 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_2 AND p_rss.date_sortie <= t_ghs.date_fin_public_2 THEN t_ghs.tarif_exh_public_2 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_3 AND p_rss.date_sortie <= t_ghs.date_fin_public_3 THEN t_ghs.tarif_exh_public_3 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_4 AND p_rss.date_sortie <= t_ghs.date_fin_public_4 THEN t_ghs.tarif_exh_public_4 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_5 AND p_rss.date_sortie <= t_ghs.date_fin_public_5 THEN t_ghs.tarif_exh_public_5 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_6 AND p_rss.date_sortie <= t_ghs.date_fin_public_6 THEN t_ghs.tarif_exh_public_6 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_7 AND p_rss.date_sortie <= t_ghs.date_fin_public_7 THEN t_ghs.tarif_exh_public_7 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_8 AND p_rss.date_sortie <= t_ghs.date_fin_public_8 THEN t_ghs.tarif_exh_public_8 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_9 AND p_rss.date_sortie <= t_ghs.date_fin_public_9 THEN t_ghs.tarif_exh_public_9 
          ELSE 0::numeric 
          END 
          AS tarif_exh,
          CASE 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_1 AND p_rss.date_sortie <= t_ghs.date_fin_prive_1 THEN t_ghs.borne_basse_prive_1 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_2 AND p_rss.date_sortie <= t_ghs.date_fin_prive_2 THEN t_ghs.borne_basse_prive_2 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_3 AND p_rss.date_sortie <= t_ghs.date_fin_prive_3 THEN t_ghs.borne_basse_prive_3 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_4 AND p_rss.date_sortie <= t_ghs.date_fin_prive_4 THEN t_ghs.borne_basse_prive_4 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_5 AND p_rss.date_sortie <= t_ghs.date_fin_prive_5 THEN t_ghs.borne_basse_prive_5 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_6 AND p_rss.date_sortie <= t_ghs.date_fin_prive_6 THEN t_ghs.borne_basse_prive_6 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_7 AND p_rss.date_sortie <= t_ghs.date_fin_prive_7 THEN t_ghs.borne_basse_prive_7 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_8 AND p_rss.date_sortie <= t_ghs.date_fin_prive_8 THEN t_ghs.borne_basse_prive_8 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_9 AND p_rss.date_sortie <= t_ghs.date_fin_prive_9 THEN t_ghs.borne_basse_prive_9 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_1 AND p_rss.date_sortie <= t_ghs.date_fin_public_1 THEN t_ghs.borne_basse_public_1 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_2 AND p_rss.date_sortie <= t_ghs.date_fin_public_2 THEN t_ghs.borne_basse_public_2 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_3 AND p_rss.date_sortie <= t_ghs.date_fin_public_3 THEN t_ghs.borne_basse_public_3 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_4 AND p_rss.date_sortie <= t_ghs.date_fin_public_4 THEN t_ghs.borne_basse_public_4 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_5 AND p_rss.date_sortie <= t_ghs.date_fin_public_5 THEN t_ghs.borne_basse_public_5 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_6 AND p_rss.date_sortie <= t_ghs.date_fin_public_6 THEN t_ghs.borne_basse_public_6 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_7 AND p_rss.date_sortie <= t_ghs.date_fin_public_7 THEN t_ghs.borne_basse_public_7 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_8 AND p_rss.date_sortie <= t_ghs.date_fin_public_8 THEN t_ghs.borne_basse_public_8 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_9 AND p_rss.date_sortie <= t_ghs.date_fin_public_9 THEN t_ghs.borne_basse_public_9 
          ELSE 0::numeric 
          END 
          AS borne_basse,
          CASE 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_1 AND p_rss.date_sortie <= t_ghs.date_fin_prive_1 THEN t_ghs.tarif_exb_prive_1 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_2 AND p_rss.date_sortie <= t_ghs.date_fin_prive_2 THEN t_ghs.tarif_exb_prive_2 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_3 AND p_rss.date_sortie <= t_ghs.date_fin_prive_3 THEN t_ghs.tarif_exb_prive_3 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_4 AND p_rss.date_sortie <= t_ghs.date_fin_prive_4 THEN t_ghs.tarif_exb_prive_4 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_5 AND p_rss.date_sortie <= t_ghs.date_fin_prive_5 THEN t_ghs.tarif_exb_prive_5 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_6 AND p_rss.date_sortie <= t_ghs.date_fin_prive_6 THEN t_ghs.tarif_exb_prive_6 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_7 AND p_rss.date_sortie <= t_ghs.date_fin_prive_7 THEN t_ghs.tarif_exb_prive_7 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_8 AND p_rss.date_sortie <= t_ghs.date_fin_prive_8 THEN t_ghs.tarif_exb_prive_8 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_9 AND p_rss.date_sortie <= t_ghs.date_fin_prive_9 THEN t_ghs.tarif_exb_prive_9 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_1 AND p_rss.date_sortie <= t_ghs.date_fin_public_1 THEN t_ghs.tarif_exb_public_1 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_2 AND p_rss.date_sortie <= t_ghs.date_fin_public_2 THEN t_ghs.tarif_exb_public_2 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_3 AND p_rss.date_sortie <= t_ghs.date_fin_public_3 THEN t_ghs.tarif_exb_public_3 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_4 AND p_rss.date_sortie <= t_ghs.date_fin_public_4 THEN t_ghs.tarif_exb_public_4 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_5 AND p_rss.date_sortie <= t_ghs.date_fin_public_5 THEN t_ghs.tarif_exb_public_5 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_6 AND p_rss.date_sortie <= t_ghs.date_fin_public_6 THEN t_ghs.tarif_exb_public_6 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_7 AND p_rss.date_sortie <= t_ghs.date_fin_public_7 THEN t_ghs.tarif_exb_public_7 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_8 AND p_rss.date_sortie <= t_ghs.date_fin_public_8 THEN t_ghs.tarif_exb_public_8 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_9 AND p_rss.date_sortie <= t_ghs.date_fin_public_9 THEN t_ghs.tarif_exb_public_9 
          ELSE 0::numeric 
          END 
          AS tarif_exb,
          CASE 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_1 AND p_rss.date_sortie <= t_ghs.date_fin_prive_1 THEN t_ghs.forfait_exb_prive_1 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_2 AND p_rss.date_sortie <= t_ghs.date_fin_prive_2 THEN t_ghs.forfait_exb_prive_2 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_3 AND p_rss.date_sortie <= t_ghs.date_fin_prive_3 THEN t_ghs.forfait_exb_prive_3 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_4 AND p_rss.date_sortie <= t_ghs.date_fin_prive_4 THEN t_ghs.forfait_exb_prive_4 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_5 AND p_rss.date_sortie <= t_ghs.date_fin_prive_5 THEN t_ghs.forfait_exb_prive_5 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_6 AND p_rss.date_sortie <= t_ghs.date_fin_prive_6 THEN t_ghs.forfait_exb_prive_6 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_7 AND p_rss.date_sortie <= t_ghs.date_fin_prive_7 THEN t_ghs.forfait_exb_prive_7 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_8 AND p_rss.date_sortie <= t_ghs.date_fin_prive_8 THEN t_ghs.forfait_exb_prive_8 
          WHEN type_etablissement = '1' AND p_rss.date_sortie >= t_ghs.date_debut_prive_9 AND p_rss.date_sortie <= t_ghs.date_fin_prive_9 THEN t_ghs.forfait_exb_prive_9 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_1 AND p_rss.date_sortie <= t_ghs.date_fin_public_1 THEN t_ghs.forfait_exb_public_1 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_2 AND p_rss.date_sortie <= t_ghs.date_fin_public_2 THEN t_ghs.forfait_exb_public_2 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_3 AND p_rss.date_sortie <= t_ghs.date_fin_public_3 THEN t_ghs.forfait_exb_public_3 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_4 AND p_rss.date_sortie <= t_ghs.date_fin_public_4 THEN t_ghs.forfait_exb_public_4 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_5 AND p_rss.date_sortie <= t_ghs.date_fin_public_5 THEN t_ghs.forfait_exb_public_5 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_6 AND p_rss.date_sortie <= t_ghs.date_fin_public_6 THEN t_ghs.forfait_exb_public_6 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_7 AND p_rss.date_sortie <= t_ghs.date_fin_public_7 THEN t_ghs.forfait_exb_public_7 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_8 AND p_rss.date_sortie <= t_ghs.date_fin_public_8 THEN t_ghs.forfait_exb_public_8 
          WHEN type_etablissement <> '1' AND p_rss.date_sortie >= t_ghs.date_debut_public_9 AND p_rss.date_sortie <= t_ghs.date_fin_public_9 THEN t_ghs.forfait_exb_public_9 
          ELSE 0::numeric 
          END 
          AS forfait_exb,
          0::numeric AS base_remboursement_ghs,
          0::numeric AS nombre_exh,
          0::numeric AS base_remboursement_exh,
          0::numeric AS nombre_exb,
          COALESCE(w_finess_mco.coefficient_mco, 0) AS coefficient_mco,
          1::numeric AS coefficient_exb
      FROM pmsi.p_rss
      JOIN pmsi.t_ghs ON ghs_id = t_ghs.oid
      JOIN pmsi.t_ghm ON p_rss.ghm_id = t_ghm.oid
      JOIN base.t_finess ON p_rss.finess = t_finess.code
      LEFT JOIN w_finess_mco ON p_rss.finess = w_finess_mco.finess AND  (CASE WHEN SUBSTR (date_sortie ,6, 2 )::int > 02 THEN SUBSTR (date_sortie ,1 ,4 ) ELSE (SUBSTR (date_sortie ,1 ,4 )::int - 1 )::text END || '-03-01' ) = w_finess_mco.date_ref 
  
      WHERE en_cours = '1' AND 
          -- on peut avoir une base de remboursement <> 0 si des DMI ou des PHC sont présents sur le dossier en_cours
          -- base_remboursement = 0 AND
          ca_ghs + ca_ghs_theorique = 0 AND
          ghs_id <> 0 AND
          t_ghs.code <> 9999 AND
          p_rss.oid != ALL( ARRAY(SELECT distinct p_rsf_detail.rss_id FROM pmsi.p_rsf_detail join pmsi.t_prestations on t_prestations.oid = p_rsf_detail.prestation_id
                      WHERE t_prestations.code like any (array['GHS', 'I0%']) GROUP BY p_rsf_detail.rss_id order by 1)::bigint[]);
        
    UPDATE w_val_encours 
        SET coefficient_mco = v_coefficient_mco.coefficient_mco_calcul        
    FROM base.v_coefficient_mco
    WHERE 1=1
        AND coefficient_mco = 0
        AND v_coefficient_mco.finess = w_val_encours.finess
        AND w_val_encours.date_sortie BETWEEN v_coefficient_mco.date_debut AND v_coefficient_mco.date_fin
    ;
      
         
  	UPDATE w_val_encours SET
  		base_remboursement_ghs = round(tarif_ghs*coefficient_mco,2),
  		nombre_exh = CASE WHEN borne_haute <> 0 and duree_sejour > borne_haute THEN duree_sejour - borne_haute ELSE 0 END,
  		nombre_exb = CASE WHEN borne_basse <> 0 and duree_sejour < borne_basse THEN borne_basse - duree_sejour ELSE 0 END;
  		
  	UPDATE w_val_encours SET
  		base_remboursement_exh = round(tarif_exh*nombre_exh*coefficient_mco,2)
  	WHERE nombre_exh > 0;
  	UPDATE w_val_encours SET
  		base_remboursement_ghs = base_remboursement_ghs - 
  			CASE WHEN forfait_exb > 0 THEN round(forfait_exb * coefficient_mco,2) 
  			ELSE round(nombre_exb * tarif_exb * coefficient_mco,2) END,
  		coefficient_exb = base.cti_division(
  				base_remboursement_ghs - 
  				CASE WHEN forfait_exb > 0 THEN round(forfait_exb * coefficient_mco,2) 
  				ELSE round(nombre_exb * tarif_exb * coefficient_mco,2) END, base_remboursement_ghs)
  	WHERE nombre_exb > 0;
  			
  	INSERT INTO pmsi.p_rsf_detail(
  		finess, 
  		rss_id, 
  		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, 
  		type, 
  		coefficient_geo, 
  		est_ligne_rss, 
  		est_ligne_rum, 
  		no_rum, 
  		unite_medicale_id, 
  		base_remboursement_rum)
  	SELECT 
  		finess, 
  		rss_id, 
  		no_rss, 
  		'V' AS nature, 
  		'' AS mt, 
  		'' AS dmt, 
  		date_entree AS date_debut, 
  		date_sortie AS date_fin, 
  		1 AS nombre, 
  		coefficient_exb AS coefficient, 
  		tarif_ghs AS prix_unitaire, 
  		base_remboursement_ghs AS base_remboursement, 
  		100 AS taux_remboursement, 
  		base_remboursement_ghs AS sejour_facture, 
  		base_remboursement_ghs AS sejour_remboursable, 
  		0 AS compteur, 
  		'' AS ligne_t2a, 
  		'' AS pec_fj, 
  		coefficient_mco, 
  		0 AS sejour_remboursable_2, 
  		ghs_id,
  		0 AS sejour_rembourse_noemie, 
  		'' AS nature_noemie, 
  		COALESCE(t_prestations.oid, 0) AS prestation_id, 
  		'B' AS type, 
  		0 AS coefficient_geo, 
  		'1' AS est_ligne_rss, 
  		'0' AS est_ligne_rum, 
  		1 AS no_rum, 
  		0 AS unite_medicale_id, 
  		0 AS base_remboursement_rum
  	FROM w_val_encours
  		JOIN pmsi.t_prestations ON t_prestations.code = 'GHS';
  INSERT INTO pmsi.p_rsf_detail(
  		finess, 
  		rss_id, 
  		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, 
  		type, 
  		coefficient_geo, 
  		est_ligne_rss, 
  		est_ligne_rum, 
  		no_rum, 
  		unite_medicale_id, 
  		base_remboursement_rum)
  	SELECT 
  		finess, 
  		rss_id, 
  		no_rss, 
  		'V' AS nature, 
  		'' AS mt, 
  		'' AS dmt, 
  		date_entree AS date_debut, 
  		date_sortie AS date_fin, 
  		nombre_exh AS nombre, 
  		1 AS coefficient, 
  		tarif_exh AS prix_unitaire, 
  		base_remboursement_exh AS base_remboursement, 
  		100 AS taux_remboursement, 
  		base_remboursement_exh AS sejour_facture, 
  		base_remboursement_exh AS sejour_remboursable, 
  		0 AS compteur, 
  		'' AS ligne_t2a, 
  		'' AS pec_fj, 
  		coefficient_mco, 
  		0 AS sejour_remboursable_2, 
  		ghs_id,
  		0 AS sejour_rembourse_noemie, 
  		'' AS nature_noemie, 
  		COALESCE(t_prestations.oid, 0) AS prestation_id, 
  		'B' AS type, 
  		0 AS coefficient_geo, 
  		'1' AS est_ligne_rss, 
  		'0' AS est_ligne_rum, 
  		1 AS no_rum, 
  		0 AS unite_medicale_id, 
  		0 AS base_remboursement_rum
  	FROM w_val_encours
  		JOIN pmsi.t_prestations ON t_prestations.code = 'EXH'
  	WHERE nombre_exh > 0;
  	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_val_encours)
  		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_val_encours);
  	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_val_encours)
  		;
  			
  	
  		
  	RETURN 'OK';
  END;