src: |
  SELECT p_rsf_detail.rss_id,
      p_rsf_detail.finess,
      p_rsf_detail.no_rss,
      p_rsf_detail.nature,
      p_rsf_detail.mt,
      p_rsf_detail.dmt,
      p_rsf_detail.date_debut,
      p_rsf_detail.date_fin,
      p_rsf_detail.operation_lamda,
      p_rsf_detail.date_comptable,
      p_rsf_detail.nombre,
      p_rsf_detail.coefficient,
      p_rsf_detail.nombre * p_rsf_detail.coefficient AS nombre_coefficient,
      p_rsf_detail.prix_unitaire,
      p_rsf_detail.taux_remboursement,
      p_rsf_detail.base_remboursement,
          CASE
              WHEN p_rsf_detail.base_remboursement_sauf_transition <> 0::numeric THEN p_rsf_detail.base_remboursement_sauf_transition
              WHEN p_rsf_detail.coefficient_mco = 1::numeric THEN p_rsf_detail.base_remboursement
              WHEN t_prestations.code::text <> 'GHS'::text OR p_rsf_detail.coefficient = 1::numeric THEN round(p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_geo * p_rsf_detail.coefficient * p_rsf_detail.coefficient_pacte_resp, 2)
              ELSE round(base.cti_division(p_rsf_detail.base_remboursement, p_rsf_detail.coefficient_mco) * p_rsf_detail.coefficient_geo, 2)
          END AS base_remboursement_sauf_transition,
      p_rsf_detail.sejour_remboursable,
          CASE
              WHEN p_rsf_detail.coefficient_mco = 1::numeric THEN p_rsf_detail.base_remboursement
              WHEN t_prestations.code::text <> 'GHS'::text OR p_rsf_detail.coefficient = 1::numeric THEN p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco * p_rsf_detail.coefficient
              ELSE p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco * base.cti_division(p_rsf_detail.base_remboursement, p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco)
          END AS base_remboursement_sans_arrondi,
          CASE
              WHEN p_rsf_detail.base_remboursement_sauf_transition <> 0::numeric THEN p_rsf_detail.base_remboursement_sauf_transition
              WHEN p_rsf_detail.coefficient_mco = 1::numeric THEN p_rsf_detail.base_remboursement
              WHEN t_prestations.code::text <> 'GHS'::text OR p_rsf_detail.coefficient = 1::numeric THEN p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_geo * p_rsf_detail.coefficient * p_rsf_detail.coefficient_pacte_resp
              ELSE p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_geo * p_rsf_detail.coefficient
          END AS base_remboursement_sauf_transition_sans_arrondi,
          CASE
              WHEN p_rsf_detail.coefficient_mco = 1::numeric THEN p_rsf_detail.sejour_remboursable
              WHEN t_prestations.code::text <> 'GHS'::text OR p_rsf_detail.coefficient = 1::numeric THEN p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco * p_rsf_detail.coefficient
              ELSE p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco * base.cti_division(p_rsf_detail.base_remboursement, p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco)
          END - p_rsf_detail.sejour_remboursable_2 AS sejour_remboursable_sans_arrondi,
          CASE
              WHEN t_prestations.code::text = 'GHS'::text THEN
              CASE
                  WHEN p_rsf_detail.coefficient = 1::numeric THEN p_rsf_detail.base_remboursement
                  ELSE round(p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco, 2)
              END
              ELSE 0::numeric
          END AS base_remboursement_ghst,
          CASE
              WHEN t_prestations.code::text = 'GHS'::text THEN
              CASE
                  WHEN p_rsf_detail.coefficient = 1::numeric THEN p_rsf_detail.base_remboursement_sauf_transition
                  ELSE round(p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_geo * p_rsf_detail.coefficient_pacte_resp, 2)
              END
              ELSE 0::numeric
          END AS base_remboursement_ghst_sauf_transition,
          CASE
              WHEN t_prestations.code::text = 'GHS'::text AND p_rsf_detail.coefficient <> 1::numeric AND p_rsf_detail.taux_remboursement <> 100::numeric THEN round(p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco * p_rsf_detail.taux_remboursement / 100::numeric, 2)
              WHEN t_prestations.code::text = 'GHS'::text AND p_rsf_detail.coefficient <> 1::numeric AND p_rsf_detail.taux_remboursement = 100::numeric THEN round(p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco, 2) - p_rsf_detail.base_remboursement + p_rsf_detail.sejour_remboursable
              WHEN t_prestations.code::text = 'GHS'::text THEN p_rsf_detail.sejour_remboursable
              ELSE 0::numeric
          END AS sejour_remboursable_ghst,
          CASE
              WHEN t_prestations.code::text = 'GHS'::text THEN
              CASE
                  WHEN p_rsf_detail.coefficient = 1::numeric THEN p_rsf_detail.base_remboursement
                  ELSE p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco
              END
              ELSE 0::numeric
          END AS base_remboursement_ghst_sans_arrondi,
          CASE
              WHEN t_prestations.code::text = 'GHS'::text THEN
              CASE
                  WHEN p_rsf_detail.coefficient = 1::numeric THEN p_rsf_detail.base_remboursement_sauf_transition
                  ELSE p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_geo * p_rsf_detail.coefficient_pacte_resp
              END
              ELSE 0::numeric
          END AS base_remboursement_ghst_sauf_transition_sans_arrondi,
          CASE
              WHEN t_prestations.code::text = 'GHS'::text AND p_rsf_detail.coefficient <> 1::numeric AND p_rsf_detail.taux_remboursement <> 100::numeric THEN p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco * p_rsf_detail.taux_remboursement / 100::numeric
              WHEN t_prestations.code::text = 'GHS'::text AND p_rsf_detail.coefficient <> 1::numeric AND p_rsf_detail.taux_remboursement = 100::numeric THEN p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco - p_rsf_detail.base_remboursement + p_rsf_detail.sejour_remboursable
              WHEN t_prestations.code::text = 'GHS'::text THEN p_rsf_detail.sejour_remboursable
              ELSE 0::numeric
          END AS sejour_remboursable_ghst_sans_arrondi,
          CASE
              WHEN t_prestations.code::text = 'GHS'::text THEN p_rsf_detail.base_remboursement -
              CASE
                  WHEN p_rsf_detail.coefficient = 1::numeric THEN p_rsf_detail.base_remboursement
                  ELSE round(p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco, 2)
              END
              ELSE 0::numeric
          END AS base_remboursement_exb,
          CASE
              WHEN t_prestations.code::text = 'GHS'::text THEN p_rsf_detail.base_remboursement_sauf_transition -
              CASE
                  WHEN p_rsf_detail.coefficient = 1::numeric THEN p_rsf_detail.base_remboursement_sauf_transition
                  ELSE round(p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_geo * p_rsf_detail.coefficient_pacte_resp, 2)
              END
              ELSE 0::numeric
          END AS base_remboursement_exb_sauf_transition,
          CASE
              WHEN t_prestations.code::text = 'GHS'::text THEN p_rsf_detail.sejour_remboursable -
              CASE
                  WHEN t_prestations.code::text = 'GHS'::text AND p_rsf_detail.coefficient <> 1::numeric AND p_rsf_detail.taux_remboursement <> 100::numeric THEN round(p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco * p_rsf_detail.taux_remboursement / 100::numeric, 2)
                  WHEN t_prestations.code::text = 'GHS'::text AND p_rsf_detail.coefficient <> 1::numeric AND p_rsf_detail.taux_remboursement = 100::numeric THEN round(p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco, 2) - p_rsf_detail.base_remboursement + p_rsf_detail.sejour_remboursable
                  ELSE p_rsf_detail.sejour_remboursable
              END
              ELSE 0::numeric
          END AS sejour_remboursable_exb,
          CASE
              WHEN t_prestations.code::text = 'GHS'::text THEN p_rsf_detail.base_remboursement -
              CASE
                  WHEN p_rsf_detail.coefficient = 1::numeric THEN p_rsf_detail.base_remboursement
                  ELSE p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco
              END
              ELSE 0::numeric
          END AS base_remboursement_exb_sans_arrondi,
          CASE
              WHEN t_prestations.code::text = 'GHS'::text THEN p_rsf_detail.base_remboursement_sauf_transition -
              CASE
                  WHEN p_rsf_detail.coefficient = 1::numeric THEN p_rsf_detail.base_remboursement_sauf_transition
                  ELSE p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_geo * p_rsf_detail.coefficient_pacte_resp
              END
              ELSE 0::numeric
          END AS base_remboursement_exb_sauf_transition_sans_arrondi,
          CASE
              WHEN t_prestations.code::text = 'GHS'::text THEN p_rsf_detail.sejour_remboursable -
              CASE
                  WHEN t_prestations.code::text = 'GHS'::text AND p_rsf_detail.coefficient <> 1::numeric AND p_rsf_detail.taux_remboursement <> 100::numeric THEN p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco * p_rsf_detail.taux_remboursement / 100::numeric
                  WHEN t_prestations.code::text = 'GHS'::text AND p_rsf_detail.coefficient <> 1::numeric AND p_rsf_detail.taux_remboursement = 100::numeric THEN p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient_mco - p_rsf_detail.base_remboursement + p_rsf_detail.sejour_remboursable
                  ELSE p_rsf_detail.sejour_remboursable
              END
              ELSE 0::numeric
          END AS sejour_remboursable_exb_sans_arrondi,
      p_rsf_detail.sejour_facture,
          CASE
              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 t_prestations.compteur_rsf::text = '3'::text THEN p_rsf_detail.sejour_facture
              ELSE 0::numeric
          END AS dmi_facture,
          CASE
              WHEN t_prestations.compteur_rsf::text = '7'::text THEN p_rsf_detail.sejour_facture
              ELSE 0::numeric
          END AS phc_facture,
      p_rsf_detail.compteur,
      p_rsf_detail.ligne_t2a,
      p_rsf_detail.pec_fj,
      p_rsf_detail.pec_fj_id,
      t_pec_fj.code AS pec_fj_code,
      t_pec_fj.texte AS pec_fj_texte,
      p_rsf_detail.coefficient_mco,
      p_rsf_detail.coefficient_geo,
      p_rsf_detail.sejour_remboursable_2,
      p_rsf_detail.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,
      p_rsf_detail.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,
          CASE
              WHEN p_rsf_detail.base_remboursement_sauf_transition_sauf_ame <> 0::numeric THEN p_rsf_detail.base_remboursement_sauf_transition_sauf_ame
              ELSE round(p_rsf_detail.prix_unitaire * p_rsf_detail.nombre * p_rsf_detail.coefficient, 2)
          END AS base_remboursement_sauf_transition_sauf_ame,
      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
     FROM pmsi.p_rsf_detail
       JOIN pmsi.t_prestations ON t_prestations.oid = p_rsf_detail.prestation_id
       JOIN pmsi.t_ghs ON t_ghs.oid = p_rsf_detail.ghs_id
       JOIN base.t_pec_fj ON t_pec_fj.oid = p_rsf_detail.pec_fj_id
    WHERE p_rsf_detail.est_ligne_rss = '1'::bpchar;