src: |
  SELECT p_factures_encours_lignes_c.finess,
      p_factures_encours.sejour_id,
      p_factures_encours.oid AS facture_id,
      p_factures_encours.code_origine,
      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_sejours.no_sejour,
      p_sejours.date_sortie,
          CASE
              WHEN p_factures_encours.ghs_id > 0 THEN p_factures_encours.date_groupage
              ELSE '2099-12-31'::date
          END AS encours_date_groupage,
      p_factures_encours.ghs_id AS encours_ghs_id,
      p_factures_encours_lignes_c.no_facture,
      to_char(p_factures_encours.date_encours::timestamp with time zone, 'YYYYMM'::text) AS mois_comptable,
      p_factures_encours.date_encours AS date_comptable,
      to_char(p_factures_encours_lignes_c.date_debut::timestamp with time zone, 'YYYYMM'::text) AS mois_debut,
      to_char(p_factures_encours_lignes_c.date_fin::timestamp with time zone, 'YYYYMM'::text) AS mois_fin,
      p_factures_encours_lignes_c.date_debut,
      p_factures_encours_lignes_c.date_fin,
      p_factures_encours_lignes_c.coefficient,
      p_factures_encours_lignes_c.coefficient_mco,
      p_factures_encours_lignes_c.prix_unitaire,
      p_factures_encours_lignes_c.taux_0,
      p_factures_encours_lignes_c.taux_1,
      p_factures_encours_lignes_c.taux_2,
      p_factures_encours_lignes_c.taux_22,
      p_factures_encours_lignes_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_factures_encours_lignes_c.rubrique_facturation_id,
      t_rubriques_facturation.code AS rubrique_facturation_code,
      t_rubriques_facturation.texte_court 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_factures_encours_lignes_c.rubrique_comptabilisation_id,
      t_rubriques_facturation_c.code AS rubrique_comptabilisation_code,
      t_rubriques_facturation_c.section_id AS rubrique_comptabilisation_section_id,
      t_rubriques_facturation_c.section_code AS rubrique_comptabilisation_section_code,
      t_rubriques_facturation_c.section_texte AS rubrique_comptabilisation_section_texte,
      t_rubriques_facturation_c.texte_court AS rubrique_comptabilisation_texte,
      p_factures_encours_lignes_c.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,
      p_factures_encours_lignes_c.compte_produit_id,
      t_compte.code AS compte_produit_code,
      t_compte.texte_court AS compte_produit_texte,
      t_compte.section_id AS compte_produit_section_id,
      t_compte.section_code AS compte_produit_section_code,
      t_compte.section_texte AS compte_produit_section_texte,
      p_factures_encours_lignes_c.nb_rubrique,
      p_factures_encours_lignes_c.nb_prestation,
      0::numeric AS montant_comptabilise,
      0::numeric AS montant_comptabilise_0,
      0::numeric AS montant_comptabilise_1,
      0::numeric AS montant_comptabilise_2,
      0::numeric AS montant_comptabilise_22,
          CASE
              WHEN p_factures_encours_lignes_c.montant_encours IS NOT NULL THEN p_factures_encours_lignes_c.montant_encours
              ELSE 0::numeric
          END AS montant_encours,
          CASE
              WHEN p_factures_encours_lignes_c.montant_encours_0 IS NOT NULL THEN p_factures_encours_lignes_c.montant_encours_0
              ELSE 0::numeric
          END AS montant_encours_0,
          CASE
              WHEN p_factures_encours_lignes_c.montant_encours_1 IS NOT NULL THEN p_factures_encours_lignes_c.montant_encours_1
              ELSE 0::numeric
          END AS montant_encours_1,
          CASE
              WHEN p_factures_encours_lignes_c.montant_encours_2 IS NOT NULL THEN p_factures_encours_lignes_c.montant_encours_2
              ELSE 0::numeric
          END AS montant_encours_2,
          CASE
              WHEN p_factures_encours_lignes_c.montant_encours_22 IS NOT NULL THEN p_factures_encours_lignes_c.montant_encours_22
              ELSE 0::numeric
          END AS montant_encours_22,
          CASE
              WHEN p_factures_encours_lignes_c.montant_encours IS NOT NULL AND p_factures_encours.code_sorti = '1'::bpchar THEN p_factures_encours_lignes_c.montant_encours
              ELSE 0::numeric
          END AS montant_encours_sorti,
          CASE
              WHEN p_factures_encours_lignes_c.montant_encours_0 IS NOT NULL AND p_factures_encours.code_sorti = '1'::bpchar THEN p_factures_encours_lignes_c.montant_encours_0
              ELSE 0::numeric
          END AS montant_encours_sorti_0,
          CASE
              WHEN p_factures_encours_lignes_c.montant_encours_1 IS NOT NULL AND p_factures_encours.code_sorti = '1'::bpchar THEN p_factures_encours_lignes_c.montant_encours_1
              ELSE 0::numeric
          END AS montant_encours_sorti_1,
          CASE
              WHEN p_factures_encours_lignes_c.montant_encours_2 IS NOT NULL AND p_factures_encours.code_sorti = '1'::bpchar THEN p_factures_encours_lignes_c.montant_encours_2
              ELSE 0::numeric
          END AS montant_encours_sorti_2,
          CASE
              WHEN p_factures_encours_lignes_c.montant_encours_22 IS NOT NULL AND p_factures_encours.code_sorti = '1'::bpchar THEN p_factures_encours_lignes_c.montant_encours_22
              ELSE 0::numeric
          END AS montant_encours_sorti_22,
          CASE
              WHEN p_factures_encours_lignes_c.montant_encours IS NOT NULL AND p_factures_encours.code_sorti <> '1'::bpchar THEN p_factures_encours_lignes_c.montant_encours
              ELSE 0::numeric
          END AS montant_encours_present,
          CASE
              WHEN p_factures_encours_lignes_c.montant_encours_0 IS NOT NULL AND p_factures_encours.code_sorti <> '1'::bpchar THEN p_factures_encours_lignes_c.montant_encours_0
              ELSE 0::numeric
          END AS montant_encours_present_0,
          CASE
              WHEN p_factures_encours_lignes_c.montant_encours_1 IS NOT NULL AND p_factures_encours.code_sorti <> '1'::bpchar THEN p_factures_encours_lignes_c.montant_encours_1
              ELSE 0::numeric
          END AS montant_encours_present_1,
          CASE
              WHEN p_factures_encours_lignes_c.montant_encours_2 IS NOT NULL AND p_factures_encours.code_sorti <> '1'::bpchar THEN p_factures_encours_lignes_c.montant_encours_2
              ELSE 0::numeric
          END AS montant_encours_present_2,
          CASE
              WHEN p_factures_encours_lignes_c.montant_encours_22 IS NOT NULL AND p_factures_encours.code_sorti <> '1'::bpchar THEN p_factures_encours_lignes_c.montant_encours_22
              ELSE 0::numeric
          END AS montant_encours_present_22,
          CASE
              WHEN p_factures_encours.code_sorti <> '1'::bpchar THEN 0::numeric
              WHEN p_factures_encours_lignes_c.montant_encours IS NOT NULL AND t_lieux_c_sortie.type_t2a <> '1'::bpchar THEN p_factures_encours_lignes_c.montant_encours
              WHEN p_factures_encours_lignes_c.montant_encours IS NOT NULL AND (p_factures_encours.type_sejour <> ALL (ARRAY['1'::bpchar::text, '2'::bpchar::text, '5'::bpchar::text])) THEN p_factures_encours_lignes_c.montant_encours
              WHEN p_factures_encours_lignes_c.montant_encours IS NOT NULL AND date(date_trunc('month'::text, p_factures_encours.date_groupage::timestamp with time zone)) <= p_factures_encours.date_encours THEN p_factures_encours_lignes_c.montant_encours
              ELSE 0::numeric
          END AS montant_encours_groupe,
          CASE
              WHEN p_factures_encours_lignes_c.montant_encours IS NOT NULL AND t_lieux_c_sortie.type_t2a = '1'::bpchar AND (p_factures_encours.type_sejour = ANY (ARRAY['1'::bpchar::text, '2'::bpchar::text, '5'::bpchar::text])) AND (date(date_trunc('month'::text, p_factures_encours.date_groupage::timestamp with time zone)) > p_factures_encours.date_encours OR p_factures_encours.ghs_id < 0) THEN p_factures_encours_lignes_c.montant_encours
              ELSE 0::numeric
          END AS montant_encours_non_groupe,
          CASE
              WHEN p_factures_encours_lignes_c.montant_encours IS NOT NULL AND t_lieux_c_sortie.type_t2a = '1'::bpchar AND (p_factures_encours.type_sejour = ANY (ARRAY['1'::bpchar::text, '2'::bpchar::text, '5'::bpchar::text])) AND p_factures_encours.code_sorti = '1'::bpchar AND (date(date_trunc('month'::text, p_factures_encours.date_groupage::timestamp with time zone)) > p_factures_encours.date_encours OR p_factures_encours.ghs_id < 0) THEN p_factures_encours_lignes_c.montant_encours
              ELSE 0::numeric
          END AS montant_encours_non_groupe_sorti,
          CASE
              WHEN p_factures_encours_lignes_c.montant_encours IS NOT NULL AND t_lieux_c_sortie.type_t2a = '1'::bpchar AND (p_factures_encours.type_sejour = ANY (ARRAY['1'::bpchar::text, '2'::bpchar::text, '5'::bpchar::text])) AND p_factures_encours.code_sorti <> '1'::bpchar AND (date(date_trunc('month'::text, p_factures_encours.date_groupage::timestamp with time zone)) > p_factures_encours.date_encours OR p_factures_encours.ghs_id < 0) THEN p_factures_encours_lignes_c.montant_encours
              ELSE 0::numeric
          END AS montant_encours_non_groupe_present,
      0 AS no_oid,
      0 AS no_nb_lits,
      p_factures_encours_lignes_c.pole_id,
      p_factures_encours.filiere_soin_principale_id,
      p_factures_encours.type_sejour,
      t_type_sejour.oid AS type_sejour_id,
      p_factures_encours.type_sejour AS type_sejour_code,
      t_type_sejour.texte AS type_sejour_texte,
      t_type_sejour.section_id AS type_sejour_section_id,
      t_type_sejour.section_code AS type_sejour_section_code,
      t_type_sejour.section_texte AS type_sejour_section_texte
     FROM activite.p_factures_encours_lignes_c
       JOIN activite.p_factures_encours ON p_factures_encours_lignes_c.facture_id = p_factures_encours.oid
       JOIN activite.p_sejours ON p_factures_encours.sejour_id = p_sejours.oid
       JOIN activite.t_lieux_c ON p_factures_encours_lignes_c.lieu_id = t_lieux_c.oid
       JOIN activite.t_rubriques_facturation ON p_factures_encours_lignes_c.rubrique_facturation_id = t_rubriques_facturation.oid
       JOIN activite.t_rubriques_facturation t_rubriques_facturation_c ON p_factures_encours_lignes_c.rubrique_comptabilisation_id = t_rubriques_facturation_c.oid
       JOIN activite.t_prestations ON p_factures_encours_lignes_c.prestation_id = t_prestations.oid
       JOIN activite.t_type_sejour ON p_factures_encours.type_sejour = t_type_sejour.code
       LEFT JOIN activite.t_lieux_c t_lieux_c_sortie ON p_factures_encours.lieu_sortie_id = t_lieux_c_sortie.oid
       LEFT JOIN activite.t_compte ON p_factures_encours_lignes_c.compte_produit_id = t_compte.oid;