dep:
  - v_rsf_detail_1
src: |
  SELECT p_rsf_detail.rss_id,
      p_rsf_detail.finess,
      p_rsf_detail.no_rss,
      p_rsf_detail.date_debut,
      p_rsf_detail.date_fin,
      p_rsf_detail.taux_remboursement,
      p_rsf_detail.coefficient_mco,
      p_rsf_detail.ghs_id,
      t_ghs.code_text AS ghs_code,
      t_ghs.texte AS ghs_texte,
      p_rsf_detail.prix_unitaire AS tarif_ghs_facture,
      p_rsf_detail.coefficient AS coefficient_ghs_facture,
      p_rsf_detail.base_remboursement AS montant_ghs_facture,
          CASE
              WHEN t_ghs.date_debut_prive_1 = '2006-09-01'::date THEN t_ghs.tarif_ghs_prive_1
              WHEN t_ghs.date_debut_prive_2 = '2006-09-01'::date THEN t_ghs.tarif_ghs_prive_2
              WHEN t_ghs.date_debut_prive_3 = '2006-09-01'::date THEN t_ghs.tarif_ghs_prive_3
              WHEN t_ghs.date_debut_prive_4 = '2006-09-01'::date THEN t_ghs.tarif_ghs_prive_4
              WHEN t_ghs.date_debut_prive_5 = '2006-09-01'::date THEN t_ghs.tarif_ghs_prive_5
              WHEN t_ghs.date_debut_prive_6 = '2006-09-01'::date THEN t_ghs.tarif_ghs_prive_6
              WHEN t_ghs.date_debut_prive_7 = '2006-09-01'::date THEN t_ghs.tarif_ghs_prive_7
              WHEN t_ghs.date_debut_prive_8 = '2006-09-01'::date THEN t_ghs.tarif_ghs_prive_8
              WHEN t_ghs.date_debut_prive_9 = '2006-09-01'::date THEN t_ghs.tarif_ghs_prive_9
              ELSE 0::numeric
          END AS tarif_ghs_afacturer,
      p_rsf_detail.coefficient AS coefficient_ghs_afacturer,
          CASE
              WHEN t_ghs.date_debut_prive_1 = '2006-09-01'::date THEN round(t_ghs.tarif_ghs_prive_1 * p_rsf_detail.coefficient_mco * p_rsf_detail.coefficient, 2)
              WHEN t_ghs.date_debut_prive_2 = '2006-09-01'::date THEN round(t_ghs.tarif_ghs_prive_2 * p_rsf_detail.coefficient_mco * p_rsf_detail.coefficient, 2)
              WHEN t_ghs.date_debut_prive_3 = '2006-09-01'::date THEN round(t_ghs.tarif_ghs_prive_3 * p_rsf_detail.coefficient_mco * p_rsf_detail.coefficient, 2)
              WHEN t_ghs.date_debut_prive_4 = '2006-09-01'::date THEN round(t_ghs.tarif_ghs_prive_4 * p_rsf_detail.coefficient_mco * p_rsf_detail.coefficient, 2)
              WHEN t_ghs.date_debut_prive_5 = '2006-09-01'::date THEN round(t_ghs.tarif_ghs_prive_5 * p_rsf_detail.coefficient_mco * p_rsf_detail.coefficient, 2)
              WHEN t_ghs.date_debut_prive_6 = '2006-09-01'::date THEN round(t_ghs.tarif_ghs_prive_6 * p_rsf_detail.coefficient_mco * p_rsf_detail.coefficient, 2)
              WHEN t_ghs.date_debut_prive_7 = '2006-09-01'::date THEN round(t_ghs.tarif_ghs_prive_7 * p_rsf_detail.coefficient_mco * p_rsf_detail.coefficient, 2)
              WHEN t_ghs.date_debut_prive_8 = '2006-09-01'::date THEN round(t_ghs.tarif_ghs_prive_8 * p_rsf_detail.coefficient_mco * p_rsf_detail.coefficient, 2)
              WHEN t_ghs.date_debut_prive_9 = '2006-09-01'::date THEN round(t_ghs.tarif_ghs_prive_9 * p_rsf_detail.coefficient_mco * p_rsf_detail.coefficient, 2)
              ELSE 0::numeric
          END AS montant_ghs_afacturer,
          CASE
              WHEN t_ghs.code < 5800::numeric OR t_ghs.code > 5820::numeric THEN view_exh.nombre_exh_facture
              ELSE 0::numeric
          END AS nombre_exh_facture,
          CASE
              WHEN t_ghs.code < 5800::numeric OR t_ghs.code > 5820::numeric THEN view_exh.tarif_exh_facture
              ELSE 0::numeric
          END AS tarif_exh_facture,
          CASE
              WHEN t_ghs.code < 5800::numeric OR t_ghs.code > 5820::numeric THEN view_exh.montant_exh_facture
              ELSE 0::numeric
          END AS montant_exh_facture,
          CASE
              WHEN t_ghs.code < 5800::numeric OR t_ghs.code > 5820::numeric THEN
              CASE
                  WHEN t_ghs.date_debut_prive_1 = '2006-09-01'::date THEN t_ghs.tarif_exh_prive_1
                  WHEN t_ghs.date_debut_prive_2 = '2006-09-01'::date THEN t_ghs.tarif_exh_prive_2
                  WHEN t_ghs.date_debut_prive_3 = '2006-09-01'::date THEN t_ghs.tarif_exh_prive_3
                  WHEN t_ghs.date_debut_prive_4 = '2006-09-01'::date THEN t_ghs.tarif_exh_prive_4
                  WHEN t_ghs.date_debut_prive_5 = '2006-09-01'::date THEN t_ghs.tarif_exh_prive_5
                  WHEN t_ghs.date_debut_prive_6 = '2006-09-01'::date THEN t_ghs.tarif_exh_prive_6
                  WHEN t_ghs.date_debut_prive_7 = '2006-09-01'::date THEN t_ghs.tarif_exh_prive_7
                  WHEN t_ghs.date_debut_prive_8 = '2006-09-01'::date THEN t_ghs.tarif_exh_prive_8
                  WHEN t_ghs.date_debut_prive_9 = '2006-09-01'::date THEN t_ghs.tarif_exh_prive_9
                  ELSE 0::numeric
              END
              ELSE 0::numeric
          END AS tarif_exh_afacturer,
          CASE
              WHEN t_ghs.code < 5800::numeric OR t_ghs.code > 5820::numeric THEN
              CASE
                  WHEN p_rss.mode_sortie <> ALL (ARRAY['7'::bpchar, '9'::bpchar]) THEN
                  CASE
                      WHEN t_ghs.date_debut_prive_1 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree)::numeric > t_ghs.borne_haute_prive_1 AND t_ghs.borne_haute_prive_1 > 0::numeric THEN (p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_1
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_2 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree)::numeric > t_ghs.borne_haute_prive_2 AND t_ghs.borne_haute_prive_2 > 0::numeric THEN (p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_2
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_3 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree)::numeric > t_ghs.borne_haute_prive_3 AND t_ghs.borne_haute_prive_3 > 0::numeric THEN (p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_3
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_4 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree)::numeric > t_ghs.borne_haute_prive_4 AND t_ghs.borne_haute_prive_4 > 0::numeric THEN (p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_4
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_5 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree)::numeric > t_ghs.borne_haute_prive_5 AND t_ghs.borne_haute_prive_5 > 0::numeric THEN (p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_5
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_6 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree)::numeric > t_ghs.borne_haute_prive_6 AND t_ghs.borne_haute_prive_6 > 0::numeric THEN (p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_6
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_7 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree)::numeric > t_ghs.borne_haute_prive_7 AND t_ghs.borne_haute_prive_7 > 0::numeric THEN (p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_7
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_8 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree)::numeric > t_ghs.borne_haute_prive_8 AND t_ghs.borne_haute_prive_8 > 0::numeric THEN (p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_8
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_9 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree)::numeric > t_ghs.borne_haute_prive_9 AND t_ghs.borne_haute_prive_9 > 0::numeric THEN (p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_9
                          ELSE 0::numeric
                      END
                      ELSE 0::numeric
                  END
                  ELSE
                  CASE
                      WHEN t_ghs.date_debut_prive_1 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree + 1)::numeric > t_ghs.borne_haute_prive_1 AND t_ghs.borne_haute_prive_1 > 0::numeric THEN (p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_1 + 1::numeric
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_2 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree + 1)::numeric > t_ghs.borne_haute_prive_2 AND t_ghs.borne_haute_prive_2 > 0::numeric THEN (p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_2 + 1::numeric
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_3 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree + 1)::numeric > t_ghs.borne_haute_prive_3 AND t_ghs.borne_haute_prive_3 > 0::numeric THEN (p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_3 + 1::numeric
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_4 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree + 1)::numeric > t_ghs.borne_haute_prive_4 AND t_ghs.borne_haute_prive_4 > 0::numeric THEN (p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_4 + 1::numeric
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_5 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree + 1)::numeric > t_ghs.borne_haute_prive_5 AND t_ghs.borne_haute_prive_5 > 0::numeric THEN (p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_5 + 1::numeric
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_6 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree + 1)::numeric > t_ghs.borne_haute_prive_6 AND t_ghs.borne_haute_prive_6 > 0::numeric THEN (p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_6 + 1::numeric
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_7 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree + 1)::numeric > t_ghs.borne_haute_prive_7 AND t_ghs.borne_haute_prive_7 > 0::numeric THEN (p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_7 + 1::numeric
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_8 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree + 1)::numeric > t_ghs.borne_haute_prive_8 AND t_ghs.borne_haute_prive_8 > 0::numeric THEN (p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_8 + 1::numeric
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_9 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree + 1)::numeric > t_ghs.borne_haute_prive_9 AND t_ghs.borne_haute_prive_9 > 0::numeric THEN (p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_9 + 1::numeric
                          ELSE 0::numeric
                      END
                      ELSE 0::numeric
                  END
              END
              ELSE 0::numeric
          END AS nombre_exh_afacturer,
          CASE
              WHEN t_ghs.code < 5800::numeric OR t_ghs.code > 5820::numeric THEN
              CASE
                  WHEN p_rss.mode_sortie <> ALL (ARRAY['7'::bpchar, '9'::bpchar]) THEN
                  CASE
                      WHEN t_ghs.date_debut_prive_1 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree)::numeric > t_ghs.borne_haute_prive_1 AND t_ghs.borne_haute_prive_1 > 0::numeric THEN round(((p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_1) * t_ghs.tarif_exh_prive_1 * p_rsf_detail.coefficient_mco, 2)
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_2 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree)::numeric > t_ghs.borne_haute_prive_2 AND t_ghs.borne_haute_prive_2 > 0::numeric THEN round(((p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_2) * t_ghs.tarif_exh_prive_2 * p_rsf_detail.coefficient_mco, 2)
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_3 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree)::numeric > t_ghs.borne_haute_prive_3 AND t_ghs.borne_haute_prive_3 > 0::numeric THEN round(((p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_3) * t_ghs.tarif_exh_prive_3 * p_rsf_detail.coefficient_mco, 2)
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_4 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree)::numeric > t_ghs.borne_haute_prive_4 AND t_ghs.borne_haute_prive_4 > 0::numeric THEN round(((p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_4) * t_ghs.tarif_exh_prive_4 * p_rsf_detail.coefficient_mco, 2)
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_5 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree)::numeric > t_ghs.borne_haute_prive_5 AND t_ghs.borne_haute_prive_5 > 0::numeric THEN round(((p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_5) * t_ghs.tarif_exh_prive_5 * p_rsf_detail.coefficient_mco, 2)
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_6 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree)::numeric > t_ghs.borne_haute_prive_6 AND t_ghs.borne_haute_prive_6 > 0::numeric THEN round(((p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_6) * t_ghs.tarif_exh_prive_6 * p_rsf_detail.coefficient_mco, 2)
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_7 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree)::numeric > t_ghs.borne_haute_prive_7 AND t_ghs.borne_haute_prive_7 > 0::numeric THEN round(((p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_7) * t_ghs.tarif_exh_prive_7 * p_rsf_detail.coefficient_mco, 2)
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_8 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree)::numeric > t_ghs.borne_haute_prive_8 AND t_ghs.borne_haute_prive_8 > 0::numeric THEN round(((p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_8) * t_ghs.tarif_exh_prive_8 * p_rsf_detail.coefficient_mco, 2)
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_9 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree)::numeric > t_ghs.borne_haute_prive_9 AND t_ghs.borne_haute_prive_9 > 0::numeric THEN round(((p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_9) * t_ghs.tarif_exh_prive_9 * p_rsf_detail.coefficient_mco, 2)
                          ELSE 0::numeric
                      END
                      ELSE 0::numeric
                  END
                  ELSE
                  CASE
                      WHEN t_ghs.date_debut_prive_1 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree + 1)::numeric > t_ghs.borne_haute_prive_1 AND t_ghs.borne_haute_prive_1 > 0::numeric THEN round(((p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_1 + 1::numeric) * t_ghs.tarif_exh_prive_1 * p_rsf_detail.coefficient_mco, 2)
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_2 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree + 1)::numeric > t_ghs.borne_haute_prive_2 AND t_ghs.borne_haute_prive_2 > 0::numeric THEN round(((p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_2 + 1::numeric) * t_ghs.tarif_exh_prive_2 * p_rsf_detail.coefficient_mco, 2)
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_3 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree + 1)::numeric > t_ghs.borne_haute_prive_3 AND t_ghs.borne_haute_prive_3 > 0::numeric THEN round(((p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_3 + 1::numeric) * t_ghs.tarif_exh_prive_3 * p_rsf_detail.coefficient_mco, 2)
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_4 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree + 1)::numeric > t_ghs.borne_haute_prive_4 AND t_ghs.borne_haute_prive_4 > 0::numeric THEN round(((p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_4 + 1::numeric) * t_ghs.tarif_exh_prive_4 * p_rsf_detail.coefficient_mco, 2)
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_5 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree + 1)::numeric > t_ghs.borne_haute_prive_5 AND t_ghs.borne_haute_prive_5 > 0::numeric THEN round(((p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_5 + 1::numeric) * t_ghs.tarif_exh_prive_5 * p_rsf_detail.coefficient_mco, 2)
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_6 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree + 1)::numeric > t_ghs.borne_haute_prive_6 AND t_ghs.borne_haute_prive_6 > 0::numeric THEN round(((p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_6 + 1::numeric) * t_ghs.tarif_exh_prive_6 * p_rsf_detail.coefficient_mco, 2)
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_7 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree + 1)::numeric > t_ghs.borne_haute_prive_7 AND t_ghs.borne_haute_prive_7 > 0::numeric THEN round(((p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_7 + 1::numeric) * t_ghs.tarif_exh_prive_7 * p_rsf_detail.coefficient_mco, 2)
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_8 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree + 1)::numeric > t_ghs.borne_haute_prive_8 AND t_ghs.borne_haute_prive_8 > 0::numeric THEN round(((p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_8 + 1::numeric) * t_ghs.tarif_exh_prive_8 * p_rsf_detail.coefficient_mco, 2)
                          ELSE 0::numeric
                      END
                      WHEN t_ghs.date_debut_prive_9 = '2006-09-01'::date THEN
                      CASE
                          WHEN (p_rss.date_sortie - p_rss.date_entree + 1)::numeric > t_ghs.borne_haute_prive_9 AND t_ghs.borne_haute_prive_9 > 0::numeric THEN round(((p_rss.date_sortie - p_rss.date_entree)::numeric - t_ghs.borne_haute_prive_9 + 1::numeric) * t_ghs.tarif_exh_prive_9 * p_rsf_detail.coefficient_mco, 2)
                          ELSE 0::numeric
                      END
                      ELSE 0::numeric
                  END
              END
              ELSE 0::numeric
          END AS montant_exh_afacturer
     FROM pmsi.p_rsf_detail,
      pmsi.t_prestations,
      pmsi.t_ghs,
      pmsi.p_rss
       LEFT JOIN ( SELECT v_rsf_detail_1.rss_id,
              max(
                  CASE
                      WHEN v_rsf_detail_1.prestation_code::text = 'EXH'::text THEN v_rsf_detail_1.prix_unitaire
                      ELSE 0::numeric
                  END) AS tarif_exh_facture,
              sum(
                  CASE
                      WHEN v_rsf_detail_1.prestation_code::text = 'EXH'::text THEN v_rsf_detail_1.base_remboursement
                      ELSE 0::numeric
                  END) AS montant_exh_facture,
              sum(
                  CASE
                      WHEN v_rsf_detail_1.prestation_code::text = 'EXH'::text THEN v_rsf_detail_1.nombre
                      ELSE 0::numeric
                  END) AS nombre_exh_facture
             FROM pmsi.v_rsf_detail_1
            GROUP BY v_rsf_detail_1.rss_id) view_exh ON p_rss.oid = view_exh.rss_id
    WHERE p_rsf_detail.rss_id = p_rss.oid AND p_rsf_detail.prestation_id = t_prestations.oid AND p_rsf_detail.type = 'B'::bpchar AND p_rsf_detail.est_ligne_rss = '1'::bpchar AND t_prestations.code::text = 'GHS'::text AND p_rsf_detail.ghs_id > 0 AND p_rsf_detail.ghs_id = t_ghs.oid AND p_rss.date_sortie >= '2006-10-01'::date AND p_rss.date_sortie <= '2006-12-31'::date;