src: |
  SELECT p_factures.sejour_id,
      p_factures.oid AS facture_id,
      p_factures.code_facture,
      p_factures.date_debut AS date_debut_facture,
      p_factures.date_fin AS date_fin_facture,
      to_char(p_factures.date_fin::timestamp with time zone, 'YYYYMM'::text) AS mois_fin_facture,
      p_factures.no_sejour,
      p_factures_lignes_c.no_facture,
      to_char(p_factures.date_facture::timestamp with time zone, 'YYYYMM'::text) AS mois_facture,
      p_factures.date_facture,
      to_char(p_factures.date_vente::timestamp with time zone, 'YYYYMM'::text) AS mois_vente,
      p_factures.date_vente,
      to_char(p_factures_lignes_c.date_debut::timestamp with time zone, 'YYYYMM'::text) AS mois_debut,
      to_char(p_factures_lignes_c.date_fin::timestamp with time zone, 'YYYYMM'::text) AS mois_fin,
      p_factures_lignes_c.date_debut,
      p_factures_lignes_c.date_fin,
      p_factures_lignes_c.coefficient,
      p_factures_lignes_c.coefficient_mco,
      p_factures_lignes_c.prix_unitaire,
      p_factures_lignes_c.taux_0,
      p_factures_lignes_c.taux_1,
      p_factures_lignes_c.taux_2,
      p_factures_lignes_c.taux_22,
      p_factures_lignes_c.lieu_id,
      p_factures_lignes_c.rubrique_facture_id,
      p_factures_lignes_c.rubrique_facturation_id,
      p_factures_lignes_c.rubrique_comptabilisation_id,
      p_factures_lignes_c.prestation_id,
      p_factures_lignes_c.compte_produit_id,
      p_factures_lignes_c.compte_produit_analytique_id,
      p_factures_lignes_c.nb_rubrique,
      p_factures_lignes_c.nb_prestation,
      p_factures_lignes_c.montant_facture,
      p_factures_lignes_c.montant_facture_0,
      p_factures_lignes_c.montant_facture_1,
      p_factures_lignes_c.montant_facture_2,
      p_factures_lignes_c.montant_facture_22,
      p_factures_lignes_c.origine_facturation_id,
          CASE p_factures_lignes_c.origine_facturation_id
              WHEN 1 THEN '1'::text
              WHEN 2 THEN '2'::text
              WHEN 3 THEN '3'::text
              WHEN 9 THEN '9'::text
              ELSE p_factures_lignes_c.origine_facturation_id::text
          END AS origine_facturation_code,
          CASE p_factures_lignes_c.origine_facturation_id
              WHEN 1 THEN 'Séjour'::text
              WHEN 2 THEN 'Actes'::text
              WHEN 3 THEN 'Actes inclus dans séjour'::text
              WHEN 9 THEN 'Lignes fictives pour facture uniquement en honoraires'::text
              ELSE p_factures_lignes_c.origine_facturation_id::text
          END AS origine_facturation_texte,
      p_factures_lignes_c.montant_facture_actes_inclus_dans_sejour,
      p_factures_lignes_c.montant_facture_0_actes_inclus_dans_sejour,
      p_factures_lignes_c.montant_facture_1_actes_inclus_dans_sejour,
      p_factures_lignes_c.montant_facture_2_actes_inclus_dans_sejour,
      p_factures_lignes_c.montant_facture_22_actes_inclus_dans_sejour,
      p_factures_lignes_c.montant_comptabilise,
      p_factures_lignes_c.montant_comptabilise_0,
      p_factures_lignes_c.montant_comptabilise_1,
      p_factures_lignes_c.montant_comptabilise_2,
      p_factures_lignes_c.montant_comptabilise_22,
      p_factures_lignes_c.montant_comptabilise_budget_global_1 AS montant_comptabilise_budget_global,
      0 AS montant_comptabilise_budget_global_0,
      p_factures_lignes_c.montant_comptabilise_budget_global_1,
      0 AS montant_comptabilise_budget_global_2,
      0 AS montant_comptabilise_budget_global_22,
      COALESCE(p_factures_lignes_c.montant_encours, 0::numeric) AS montant_encours,
      COALESCE(p_factures_lignes_c.montant_encours_0, 0::numeric) AS montant_encours_0,
      COALESCE(p_factures_lignes_c.montant_encours_1, 0::numeric) AS montant_encours_1,
      COALESCE(p_factures_lignes_c.montant_encours_2, 0::numeric) AS montant_encours_2,
      COALESCE(p_factures_lignes_c.montant_encours_22, 0::numeric) AS montant_encours_22,
      0 AS no_oid,
      0 AS no_nb_lits,
          CASE
              WHEN p_factures.code_facture >= '1'::bpchar THEN '1'::text
              ELSE '0'::text
          END AS est_ligne_facturee,
          CASE
              WHEN p_factures.code_facture >= '1'::bpchar THEN p_factures_lignes_c.nb_rubrique
              ELSE 0::numeric
          END AS nb_rubrique_facture,
          CASE
              WHEN p_factures.code_facture >= '1'::bpchar THEN p_factures_lignes_c.nb_prestation
              ELSE 0::numeric
          END AS nb_prestation_facture,
          CASE
              WHEN p_factures.code_facture >= '1'::bpchar AND p_factures.code_vente = '1'::bpchar THEN '1'::text
              ELSE '0'::text
          END AS est_ligne_comptabilisee,
          CASE
              WHEN p_factures.code_facture >= '1'::bpchar AND p_factures.code_vente = '1'::bpchar THEN p_factures_lignes_c.nb_rubrique
              ELSE 0::numeric
          END AS nb_rubrique_comptabilise,
          CASE
              WHEN p_factures.code_facture >= '1'::bpchar AND p_factures.code_vente = '1'::bpchar THEN p_factures_lignes_c.nb_prestation
              ELSE 0::numeric
          END AS nb_prestation_comptabilise,
          CASE
              WHEN p_factures.code_facture = '0'::bpchar THEN '1'::text
              ELSE '0'::text
          END AS est_ligne_encours,
          CASE
              WHEN p_factures.code_facture = '0'::bpchar THEN p_factures_lignes_c.nb_prestation
              ELSE 0::numeric
          END AS nb_rubrique_encours,
          CASE
              WHEN p_factures.code_facture = '0'::bpchar THEN p_factures_lignes_c.nb_prestation
              ELSE 0::numeric
          END AS nb_prestation_encours,
      p_factures_lignes_c.pole_id,
      p_factures_lignes_c.budget_cle_id,
      p_factures_lignes_c.lpp_id,
      p_factures_lignes_c.ucd_id
     FROM activite.p_factures_lignes_c
       JOIN activite.p_factures ON p_factures_lignes_c.no_facture = p_factures.no_facture;