src: |
  SELECT p_factures_lignes_h.finess,
      p_factures.no_sejour,
      p_factures.sejour_id,
      p_factures.oid AS facture_id,
      p_factures_lignes_h.no_facture,
      p_factures.no_facture_reference,
      to_char(p_factures.date_facture::timestamp with time zone, 'YYYYMM'::text) AS mois_facture,
      p_factures.date_facture,
      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,
      to_char(p_factures.date_vente::timestamp with time zone, 'YYYYMM'::text) AS mois_vente,
      p_factures.date_vente,
      to_char(p_factures_lignes_h.date_debut::timestamp with time zone, 'YYYYMM'::text) AS mois_debut,
      to_char(p_factures_lignes_h.date_fin::timestamp with time zone, 'YYYYMM'::text) AS mois_fin,
      p_factures_lignes_h.date_debut,
      p_factures_lignes_h.heure_debut,
      p_factures_lignes_h.date_fin,
      p_factures_lignes_h.coefficient,
      p_factures_lignes_h.coefficient_mco,
      p_factures_lignes_h.prix_unitaire,
      p_factures_lignes_h.taux_0,
      p_factures_lignes_h.taux_1,
      p_factures_lignes_h.taux_2,
      p_factures_lignes_h.taux_22,
      p_factures_lignes_h.lieu_id,
      p_factures_lignes_h.medecin_facture_id AS medecin_adm_facture_id,
      p_factures_lignes_h.medecin_comptabilise_id AS medecin_adm_comptabilise_id,
      p_factures_lignes_h.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_lignes_h.acte_id,
      t_actes_c.code AS acte_code,
      t_actes_c.texte_court AS acte_texte,
      t_actes_c.section_id AS acte_section_id,
      t_actes_c.section_code AS acte_section_code,
      t_actes_c.section_texte AS acte_section_texte,
      t_actes_c.nomenclature AS acte_nomenclature,
          CASE
              WHEN t_prestations.code = ANY (ARRAY['ADA'::text, 'ACO'::text, 'ADC'::text, 'ADE'::text, 'ADI'::text, 'ATM'::text, 'DEN'::text]) THEN 1
              ELSE 2
          END AS acte_nomenclature_id,
          CASE
              WHEN t_prestations.code = ANY (ARRAY['ADA'::text, 'ACO'::text, 'ADC'::text, 'ADE'::text, 'ADI'::text, 'ATM'::text, 'DEN'::text]) THEN 'CCAM'::text
              ELSE 'NGAP'::text
          END AS acte_nomenclature_code,
          CASE
              WHEN t_prestations.code = ANY (ARRAY['ADA'::text, 'ACO'::text, 'ADC'::text, 'ADE'::text, 'ADI'::text, 'ATM'::text, 'DEN'::text]) THEN 'Nomenclature CCAM'::text
              ELSE 'Nomenclature NGAP'::text
          END AS acte_nomenclature_texte,
          CASE
              WHEN p_factures_lignes_h.activite_ccam <> '4'::bpchar AND t_prestations.code <> 'ADA'::text THEN t_actes_c.ccam_regroupement_id_1
              ELSE t_actes_c.ccam_regroupement_id_1
          END AS ccam_regroupement_id,
          CASE
              WHEN p_factures_lignes_h.activite_ccam <> '4'::bpchar AND t_prestations.code <> 'ADA'::text THEN t_actes_c.ccam_regroupement_code_1
              ELSE t_actes_c.ccam_regroupement_code_1
          END AS ccam_regroupement_code,
          CASE
              WHEN p_factures_lignes_h.activite_ccam <> '4'::bpchar AND t_prestations.code <> 'ADA'::text THEN t_actes_c.ccam_regroupement_texte_1
              ELSE t_actes_c.ccam_regroupement_texte_1
          END AS ccam_regroupement_texte,
      p_factures_lignes_h.protocole_id,
      p_factures_lignes_h.nb_rubrique,
      p_factures_lignes_h.nb_prestation,
      p_factures_lignes_h.montant_facture,
      p_factures_lignes_h.montant_facture_0,
      p_factures_lignes_h.montant_facture_1,
      p_factures_lignes_h.montant_facture_2,
      p_factures_lignes_h.montant_facture_22,
      p_factures_lignes_h.montant_comptabilise,
      p_factures_lignes_h.montant_comptabilise_0,
      p_factures_lignes_h.montant_comptabilise_1,
      p_factures_lignes_h.montant_comptabilise_2,
      p_factures_lignes_h.montant_comptabilise_22,
      p_factures_lignes_h.montant_depassement,
      p_factures_lignes_h.montant_facture_actes_inclus_dans_sejour,
      p_factures_lignes_h.montant_facture_0_actes_inclus_dans_sejour,
      p_factures_lignes_h.montant_facture_1_actes_inclus_dans_sejour,
      p_factures_lignes_h.montant_facture_2_actes_inclus_dans_sejour,
      p_factures_lignes_h.montant_facture_22_actes_inclus_dans_sejour,
      COALESCE(p_factures_lignes_h.montant_non_facture,0)::numeric(13,2) as montant_non_facture,
          CASE
              WHEN p_factures_lignes_h.montant_encours IS NOT NULL THEN p_factures_lignes_h.montant_encours
              ELSE 0::numeric
          END AS montant_encours,
          CASE
              WHEN p_factures_lignes_h.montant_encours_0 IS NOT NULL THEN p_factures_lignes_h.montant_encours_0
              ELSE 0::numeric
          END AS montant_encours_0,
          CASE
              WHEN p_factures_lignes_h.montant_encours_1 IS NOT NULL THEN p_factures_lignes_h.montant_encours_1
              ELSE 0::numeric
          END AS montant_encours_1,
          CASE
              WHEN p_factures_lignes_h.montant_encours_2 IS NOT NULL THEN p_factures_lignes_h.montant_encours_2
              ELSE 0::numeric
          END AS montant_encours_2,
          CASE
              WHEN p_factures_lignes_h.montant_encours_22 IS NOT NULL THEN p_factures_lignes_h.montant_encours_22
              ELSE 0::numeric
          END AS montant_encours_22,
          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_h.nb_rubrique
              ELSE 0::numeric
          END AS nb_rubrique_facture,
          CASE
              WHEN p_factures.code_facture >= '1'::bpchar THEN p_factures_lignes_h.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_h.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_h.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_h.nb_prestation
              ELSE 0::numeric
          END AS nb_rubrique_encours,
          CASE
              WHEN p_factures.code_facture = '0'::bpchar THEN p_factures_lignes_h.nb_prestation
              ELSE 0::numeric
          END AS nb_prestation_encours,
      0 AS no_oid,
      0 AS no_nb_lits,
      p_factures_lignes_h.pole_id
     FROM activite.p_factures_lignes_h
       JOIN activite.p_factures ON p_factures_lignes_h.no_facture = p_factures.no_facture
       JOIN activite.t_prestations ON p_factures_lignes_h.prestation_id = t_prestations.oid
       JOIN activite.t_actes_c ON p_factures_lignes_h.acte_id = t_actes_c.oid;