dep:
  - v_unites_medicales_2
src: |
  SELECT p_rss_rum.rss_id,
      p_rss_rum.finess,
      p_rss_rum.no_rss,
      p_rss_rum.no_rum,
      p_rss_rum.date_entree AS date_entree_rum,
      p_rss_rum.date_sortie AS date_sortie_rum,
      p_rsf_detail.operation_lamda,
      p_rsf_detail.date_comptable,
      p_rss_rum.duree_sejour AS duree_sejour_rum,
          CASE
              WHEN p_rsf_detail.rss_id IS NULL THEN '0'::text
              ELSE '1'::text
          END AS rum_valorise,
      p_rsf_detail.nature,
      p_rsf_detail.mt,
      p_rsf_detail.dmt,
      COALESCE(p_rsf_detail.date_debut, p_rss_rum.date_entree) AS date_debut,
      COALESCE(p_rsf_detail.date_fin, p_rss_rum.date_sortie) AS date_fin,
      COALESCE(p_rsf_detail.nombre, 0::numeric) AS nombre,
      COALESCE(p_rsf_detail.coefficient, 0::numeric) AS coefficient,
      COALESCE(p_rsf_detail.nombre * p_rsf_detail.coefficient, 0::numeric) AS nombre_coefficient,
      COALESCE(p_rsf_detail.prix_unitaire, 0::numeric) AS prix_unitaire,
          CASE
              WHEN p_rsf_detail.rss_id IS NULL THEN 0::numeric
              WHEN p_rsf_detail.base_remboursement_rum_sauf_transition IS NOT NULL THEN p_rsf_detail.base_remboursement_rum_sauf_transition
              WHEN p_rsf_detail.coefficient_mco > 0::numeric THEN round(p_rsf_detail.base_remboursement_rum / p_rsf_detail.coefficient_mco * p_rsf_detail.coefficient_geo * p_rsf_detail.coefficient_pacte_resp, 2)
              ELSE p_rsf_detail.base_remboursement_rum
          END AS base_remboursement_rum_sauf_transition,
      COALESCE(p_rsf_detail.base_remboursement_rum, 0::numeric) AS base_remboursement_rum,
      COALESCE(p_rsf_detail.taux_remboursement, 0::numeric) AS taux_remboursement,
      COALESCE(p_rsf_detail.sejour_remboursable_rum, 0::numeric) AS sejour_remboursable_rum,
      COALESCE(p_rsf_detail.sejour_facture, 0::numeric) AS sejour_facture,
          CASE
              WHEN p_rsf_detail.rss_id IS NULL THEN 0::numeric
              WHEN t_prestations.compteur_rsf::text = '1'::text OR p_rsf_detail.ligne_t2a = '1'::bpchar THEN p_rsf_detail.sejour_facture
              ELSE 0::numeric
          END AS t2a_facture,
          CASE
              WHEN p_rsf_detail.rss_id IS NULL THEN 0::numeric
              WHEN t_prestations.compteur_rsf::text = '3'::text THEN p_rsf_detail.sejour_facture
              ELSE 0::numeric
          END AS dmi_facture,
          CASE
              WHEN p_rsf_detail.rss_id IS NULL THEN 0::numeric
              WHEN t_prestations.compteur_rsf::text = '7'::text THEN p_rsf_detail.sejour_facture
              ELSE 0::numeric
          END AS phc_facture,
      COALESCE(p_rsf_detail.sejour_remboursable, 0::numeric) AS sejour_remboursable,
      COALESCE(p_rsf_detail.compteur, ''::bpchar) AS compteur,
      COALESCE(p_rsf_detail.ligne_t2a, ''::bpchar) AS ligne_t2a,
      COALESCE(p_rsf_detail.pec_fj, ''::bpchar) AS pec_fj,
      COALESCE(p_rsf_detail.pec_fj_id, 0::bigint) AS pec_fj_id,
      t_pec_fj.code AS pec_fj_code,
      t_pec_fj.texte AS pec_fj_texte,
      COALESCE(p_rsf_detail.coefficient_mco, 0::numeric) AS coefficient_mco,
      COALESCE(p_rsf_detail.coefficient_geo, 0::numeric) AS coefficient_geo,
      COALESCE(p_rsf_detail.sejour_remboursable_2, 0::numeric) AS sejour_remboursable_2,
      COALESCE(p_rsf_detail.ghs_id, 0::bigint) AS ghs_id,
      t_ghs.code_text AS ghs_code,
      t_ghs.texte AS ghs_texte,
      p_rsf_detail.sejour_rembourse_noemie,
      p_rsf_detail.nature_noemie,
      COALESCE(p_rsf_detail.prestation_id, 0::bigint) AS prestation_id,
      t_prestations.code AS prestation_code,
      t_prestations.texte_court AS prestation_texte,
      t_prestations.section_id AS prestation_section_id,
      t_prestations.section_code AS prestation_section_code,
      t_prestations.section_texte AS prestation_section_texte,
          CASE
              WHEN t_prestations.code::text = 'GHS'::text AND p_rsf_detail.coefficient < 1::numeric THEN 'EXB'::text
              WHEN t_prestations.code::text = 'EXH'::text THEN 'EXH'::text
              ELSE ''::text
          END AS prestation_exb_exh,
      p_rss_rum.pole_id AS pole_rum_id,
      t_pole.code AS pole_rum_code,
      t_pole.texte AS pole_rum_texte,
      t_pole.section_id AS pole_rum_section_id,
      t_pole.section_code AS pole_rum_section_code,
      t_pole.section_texte AS pole_rum_section_texte,
      COALESCE(v_unites_medicales_2_rsf.oid, p_rss_rum.unite_medicale_id) AS unite_medicale_id,
      COALESCE(v_unites_medicales_2_rsf.code, v_unites_medicales_2.code) AS unite_medicale_code,
      COALESCE(v_unites_medicales_2_rsf.texte, v_unites_medicales_2.texte) AS unite_medicale_texte,
      COALESCE(v_unites_medicales_2_rsf.type_autorisation, v_unites_medicales_2.type_autorisation) AS type_autorisation,
      COALESCE(v_unites_medicales_2_rsf.type_autorisation_texte, v_unites_medicales_2.type_autorisation_texte) AS type_autorisation_texte,
      COALESCE(v_unites_medicales_2_rsf.section_id, v_unites_medicales_2.section_id) AS unite_medicale_section_id,
      COALESCE(v_unites_medicales_2_rsf.section_code, v_unites_medicales_2.section_code) AS unite_medicale_section_code,
      COALESCE(v_unites_medicales_2_rsf.section_texte, v_unites_medicales_2.section_texte) AS unite_medicale_section_texte,
      COALESCE(v_unites_medicales_2_rsf.finess_geographique, v_unites_medicales_2.finess_geographique) AS finess_geographique,
          CASE
              WHEN p_rsf_detail.rss_id IS NULL THEN 0::numeric
              WHEN p_rsf_detail.base_remboursement_rum_sauf_transition_sauf_ame IS NOT NULL THEN p_rsf_detail.base_remboursement_rum_sauf_transition_sauf_ame
              ELSE round(p_rsf_detail.prix_unitaire * p_rsf_detail.prix_unitaire * p_rsf_detail.coefficient, 2)
          END AS base_remboursement_rum_sauf_transition_sauf_ame,
      t_conversion_hp_hc.oid AS conversion_hp_hc_id,
      t_conversion_hp_hc.code AS conversion_hp_hc_code,
      t_conversion_hp_hc.texte AS conversion_hp_hc_texte,
      p_rsf_detail.fides_detail,
      p_rsf_detail.fides_detail AS fides_detail_id,
      p_rsf_detail.fides_detail::text AS fides_detail_code,
          CASE
              WHEN p_rsf_detail.fides_detail = 1 THEN 'Valorisé FIDES (ligne)'::text
              WHEN p_rsf_detail.fides_detail = 0 THEN 'Non Valorisé FIDES (ligne)'::text
              ELSE 'Non renseigné'::text
          END AS fides_detail_texte,
      p_rss_rum.medecin_rum_id,
      t_medecins_c.code AS medecin_rum_code,
      t_medecins_c.nom_prenom AS medecin_rum_nom,
      t_medecins_c.specialite_medecin_reference_id AS specialite_medecin_reference_rum_id,
      t_medecins_c.specialite_medecin_reference_code AS specialite_medecin_reference_rum_code,
      t_medecins_c.specialite_medecin_reference_texte AS specialite_medecin_reference_rum_texte,
      t_medecins_c.section_id AS medecin_rum_section_id,
      t_medecins_c.section_code AS medecin_rum_section_code,
      t_medecins_c.section_texte AS medecin_rum_section_texte
     FROM pmsi.p_rss_rum
       LEFT JOIN pmsi.p_rsf_detail ON p_rsf_detail.rss_id = p_rss_rum.rss_id AND p_rsf_detail.no_rum = p_rss_rum.no_rum AND p_rsf_detail.est_ligne_rum = '1'::bpchar
       JOIN pmsi.t_prestations ON t_prestations.oid = COALESCE(p_rsf_detail.prestation_id, 0::bigint)
       JOIN pmsi.t_ghs ON t_ghs.oid = COALESCE(p_rsf_detail.ghs_id, 0::bigint)
       JOIN pmsi.v_unites_medicales_2 ON v_unites_medicales_2.oid = p_rss_rum.unite_medicale_id
       JOIN pmsi.t_types_autorisations ON v_unites_medicales_2.type_autorisation = t_types_autorisations.code
       JOIN pmsi.t_disciplines_sae ON t_types_autorisations.discipline_sae_id = t_disciplines_sae.oid
       JOIN pmsi.t_conversion_hp_hc ON t_conversion_hp_hc.oid = p_rss_rum.conversion_hp_hc_id
       LEFT JOIN pmsi.v_unites_medicales_2 v_unites_medicales_2_rsf ON v_unites_medicales_2_rsf.oid = p_rsf_detail.unite_medicale_id AND p_rsf_detail.unite_medicale_id <> p_rss_rum.unite_medicale_id
       JOIN base.t_pec_fj ON t_pec_fj.oid = COALESCE(p_rsf_detail.pec_fj_id, 0::bigint)
       LEFT JOIN pmsi.t_pole ON p_rss_rum.pole_id = t_pole.oid
       JOIN pmsi.t_medecins_c ON p_rss_rum.medecin_rum_id = t_medecins_c.oid;