src: |
  SELECT p_factures_encours.sejour_id,
      p_factures_encours.oid AS facture_id,
      p_factures_encours.code_sorti,
      p_factures_encours.date_debut AS date_debut_facture,
      p_factures_encours.date_fin AS date_fin_facture,
      to_char(p_factures_encours.date_fin::timestamp with time zone, 'YYYYMM'::text) AS mois_fin_facture,
      p_factures_encours.no_sejour,
      p_facture_encours_ligne_valorise_c.no_facture,
      to_char(p_factures_encours.date_encours::timestamp with time zone, 'YYYYMM'::text) AS mois_encours,
      p_factures_encours.date_encours,
      p_factures_encours.date_calcul,
      to_char(p_facture_encours_ligne_valorise_c.date_debut::timestamp with time zone, 'YYYYMM'::text) AS mois_debut,
      to_char(p_facture_encours_ligne_valorise_c.date_fin::timestamp with time zone, 'YYYYMM'::text) AS mois_fin,
      p_facture_encours_ligne_valorise_c.date_debut,
      p_facture_encours_ligne_valorise_c.date_fin,
      p_facture_encours_ligne_valorise_c.coefficient,
      p_facture_encours_ligne_valorise_c.coefficient_mco,
      p_facture_encours_ligne_valorise_c.prix_unitaire,
      p_facture_encours_ligne_valorise_c.lieu_id,
      t_lieux_c.service_facturation_id AS lieu_service_id,
      t_lieux_c.service_facturation_code AS lieu_service_code,
      t_lieux_c.service_facturation_texte AS lieu_service_texte,
      t_lieux_c.service_facturation_section_id AS lieu_service_section_id,
      t_lieux_c.service_facturation_section_code AS lieu_service_section_code,
      t_lieux_c.service_facturation_section_texte AS lieu_service_section_texte,
      t_lieux_c.activite_id AS lieu_activite_id,
      t_lieux_c.activite_code AS lieu_activite_code,
      t_lieux_c.activite_texte AS lieu_activite_texte,
      t_lieux_c.etage_id AS lieu_etage_id,
      t_lieux_c.etage_code AS lieu_etage_code,
      t_lieux_c.etage_texte AS lieu_etage_texte,
      t_lieux_c.etage_section_id AS lieu_etage_section_id,
      t_lieux_c.etage_section_code AS lieu_etage_section_code,
      t_lieux_c.etage_section_texte AS lieu_etage_section_texte,
      t_lieux_c.lit_id AS lieu_lit_id,
      t_lieux_c.lit_code AS lieu_lit_code,
      t_lieux_c.lit_texte AS lieu_lit_texte,
      t_lieux_c.lit_section_id AS lieu_lit_section_id,
      t_lieux_c.lit_section_code AS lieu_lit_section_code,
      t_lieux_c.lit_section_texte AS lieu_lit_section_texte,
      t_lieux_c.unite_medicale_id AS lieu_unite_medicale_id,
      t_lieux_c.unite_medicale_code AS lieu_unite_medicale_code,
      t_lieux_c.unite_medicale_texte AS lieu_unite_medicale_texte,
      t_lieux_c.unite_medicale_section_id AS lieu_unite_medicale_section_id,
      t_lieux_c.unite_medicale_section_code AS lieu_unite_medicale_section_code,
      t_lieux_c.unite_medicale_section_texte AS lieu_unite_medicale_section_texte,
      t_lieux_c.unite_fonctionnelle_id AS lieu_unite_fonctionnelle_id,
      t_lieux_c.unite_fonctionnelle_code AS lieu_unite_fonctionnelle_code,
      t_lieux_c.unite_fonctionnelle_texte AS lieu_unite_fonctionnelle_texte,
      t_lieux_c.unite_fonctionnelle_section_id AS lieu_unite_fonctionnelle_section_id,
      t_lieux_c.unite_fonctionnelle_section_code AS lieu_unite_fonctionnelle_section_code,
      t_lieux_c.unite_fonctionnelle_section_texte AS lieu_unite_fonctionnelle_section_texte,
      p_facture_encours_ligne_valorise_c.rubrique_facturation_id,
      t_rubriques_facturation.code AS rubrique_facturation_code,
      t_rubriques_facturation.texte AS rubrique_facturation_texte,
      t_rubriques_facturation.section_id AS rubrique_facturation_section_id,
      t_rubriques_facturation.section_code AS rubrique_facturation_section_code,
      t_rubriques_facturation.section_texte AS rubrique_facturation_section_texte,
      p_facture_encours_ligne_valorise_c.prestation_id,
      t_prestations.code AS prestation_code,
      t_prestations.texte 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,
      p_facture_encours_ligne_valorise_c.lpp_id,
      COALESCE(t_lpp_c.code, ''::text) AS lpp_code,
      COALESCE(t_lpp_c.texte, ''::text) AS lpp_texte,
      p_facture_encours_ligne_valorise_c.ucd_id,
      COALESCE(t_ucd_c.code, ''::text) AS ucd_code,
      COALESCE(t_ucd_c.texte, ''::text) AS ucd_texte,
      p_facture_encours_ligne_valorise_c.nb_rubrique,
      p_facture_encours_ligne_valorise_c.nb_prestation,
      p_facture_encours_ligne_valorise_c.montant_encours,
      p_facture_encours_ligne_valorise_c.montant_valorise_total,
          CASE p_facture_encours_ligne_valorise_c.type_ligne_code
              WHEN 'GHS'::text THEN 1
              WHEN 'GHSBB'::text THEN 2
              WHEN 'GHSNG'::text THEN 3
              WHEN 'GHSBBM'::text THEN 4
              WHEN 'EXH'::text THEN 11
              WHEN 'EXHNG'::text THEN 12
              WHEN 'SSS'::text THEN 21
              WHEN 'IVG'::text THEN 22
              WHEN 'NNX'::text THEN 23
              WHEN 'DIA'::text THEN 24
              WHEN 'PJ'::text THEN 31
              WHEN 'TMBG'::text THEN 32
              WHEN 'FJ'::text THEN 33
              WHEN 'PMS'::text THEN 34
              WHEN 'ENT'::text THEN 35
              WHEN 'GHT'::text THEN 81
              WHEN 'FJA'::text THEN 91
              WHEN 'CPC'::text THEN 101
              WHEN 'PRESTAM'::text THEN 201
              WHEN 'SEH'::text THEN 701
              WHEN 'ATU'::text THEN 702
              WHEN 'SAISIE'::text THEN 999
              ELSE 801
          END AS type_ligne_id,
      p_facture_encours_ligne_valorise_c.type_ligne_code,
          CASE p_facture_encours_ligne_valorise_c.type_ligne_code
              WHEN 'GHS'::text THEN 'GHS dossier groupé'::text
              WHEN 'GHSBB'::text THEN 'GHS bébé dossier bébé groupé'::text
              WHEN 'GHSNG'::text THEN 'GHS dossier non groupé'::text
              WHEN 'GHSBB'::text THEN 'GHS moyen bébé dossier maman groupé'::text
              WHEN 'EXH'::text THEN 'EXH dossier groupé'::text
              WHEN 'EXHNG'::text THEN 'EXH dossier non groupé'::text
              WHEN 'SSS'::text THEN 'Suppléments SRC,STF,REA'::text
              WHEN 'IVG'::text THEN 'Forfaits IVG'::text
              WHEN 'NNX'::text THEN 'Suppléments NN'::text
              WHEN 'DIA'::text THEN 'Dialyse'::text
              WHEN 'PJ'::text THEN 'PJ'::text
              WHEN 'TMBG'::text THEN 'TM Budget Global MCO'::text
              WHEN 'FJ'::text THEN 'FJ'::text
              WHEN 'PMS'::text THEN 'Forfait PMS SSR'::text
              WHEN 'ENT'::text THEN 'Forfait entrée'::text
              WHEN 'GHT'::text THEN 'GHT (HAD'::text
              WHEN 'FJA'::text THEN 'Forfait jour de sortie'::text
              WHEN 'CPC'::text THEN 'Chambre particulière'::text
              WHEN 'PRESTAM'::text THEN 'Montant moyen par jour'::text
              WHEN 'SEH'::text THEN 'Forfaits SEh'::text
              WHEN 'ATU'::text THEN 'ATU'::text
              WHEN 'SAISIE'::text THEN 'Prestations saisies'::text
              ELSE p_facture_encours_ligne_valorise_c.type_ligne_code
          END AS type_ligne_texte,
      p_facture_encours_ligne_valorise_c.mode_calcul_code,
      p_facture_encours_ligne_valorise_c.mode_calcul_texte
     FROM activite.p_facture_encours_ligne_valorise_c
       JOIN activite.p_factures_encours ON p_facture_encours_ligne_valorise_c.no_facture = p_factures_encours.no_facture
       JOIN activite.t_lieux_c ON p_facture_encours_ligne_valorise_c.lieu_id = t_lieux_c.oid
       JOIN activite.t_rubriques_facturation ON p_facture_encours_ligne_valorise_c.rubrique_facturation_id = t_rubriques_facturation.oid
       JOIN activite.t_prestations ON p_facture_encours_ligne_valorise_c.prestation_id = t_prestations.oid
       LEFT JOIN activite.t_lpp_c ON p_facture_encours_ligne_valorise_c.lpp_id = t_lpp_c.oid
       LEFT JOIN activite.t_ucd_c ON p_facture_encours_ligne_valorise_c.ucd_id = t_ucd_c.oid;