src: |
  SELECT p_factures_lignes_h.finess,
      p_factures.no_sejour,
      p_factures.sejour_id,
      p_factures.oid AS facture_id,
      p_sejours.date_sortie,
      p_factures_lignes_h.no_facture,
      p_factures_lignes_h.pole_id,
      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,
      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_lignes_h.medecin_facture_id AS medecin_adm_facture_id,
      t_medecins_administratifs_fac.adm_code AS medecin_adm_facture_code,
      t_medecins_administratifs_fac.adm_nom AS medecin_adm_facture_nom,
      t_medecins_administratifs_fac.adm_prenom AS medecin_adm_facture_prenom,
      t_medecins_administratifs_fac.adm_nom_prenom AS medecin_adm_facture_nom_prenom,
      t_medecins_administratifs_fac.section_id AS medecin_adm_facture_section_id,
      t_medecins_administratifs_fac.section_code AS medecin_adm_facture_section_code,
      t_medecins_administratifs_fac.section_texte AS medecin_adm_facture_section_texte,
      t_medecins_administratifs_fac.medecin_id AS medecin_facture_id,
      t_medecins_administratifs_fac.medecin_code AS medecin_facture_code,
      t_medecins_administratifs_fac.nom AS medecin_facture_nom,
      t_medecins_administratifs_fac.prenom AS medecin_facture_prenom,
      t_medecins_administratifs_fac.nom_prenom AS medecin_facture_nom_prenom,
      t_medecins_administratifs_fac.specialite_id AS medecin_facture_specialite_id,
      t_medecins_administratifs_fac.specialite_code AS medecin_facture_specialite_code,
      t_medecins_administratifs_fac.specialite_texte AS medecin_facture_specialite_texte,
      p_factures_lignes_h.medecin_comptabilise_id AS medecin_adm_comptabilise_id,
      t_medecins_administratifs_cpt.adm_code AS medecin_adm_comptabilise_code,
      t_medecins_administratifs_cpt.adm_nom AS medecin_adm_comptabilise_nom,
      t_medecins_administratifs_cpt.adm_prenom AS medecin_adm_comptabilise_prenom,
      t_medecins_administratifs_cpt.adm_nom_prenom AS medecin_adm_comptabilise_nom_prenom,
      t_medecins_administratifs_cpt.medecin_id AS medecin_comptabilise_id,
      t_medecins_administratifs_cpt.medecin_code AS medecin_comptabilise_code,
      t_medecins_administratifs_cpt.nom AS medecin_comptabilise_nom,
      t_medecins_administratifs_cpt.prenom AS medecin_comptabilise_prenom,
      t_medecins_administratifs_cpt.nom_prenom AS medecin_comptabilise_nom_prenom,
      t_medecins_administratifs_cpt.specialite_id AS medecin_comptabilise_specialite_id,
      t_medecins_administratifs_cpt.specialite_code AS medecin_comptabilise_specialite_code,
      t_medecins_administratifs_cpt.specialite_texte AS medecin_comptabilise_specialite_texte,
      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,
      t_protocoles.code AS protocole_code,
      t_protocoles.texte_court AS protocole_texte,
      t_protocoles.section_id AS protocole_section_id,
      t_protocoles.section_code AS protocole_section_code,
      t_protocoles.section_texte AS protocole_section_texte,
      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,
          CASE
              WHEN p_factures_lignes_h.montant_facture = 0::numeric AND (p_sejours.est_budget_global IS DISTINCT FROM 1::numeric OR p_factures.code_facture <> '0'::bpchar) AND COALESCE(p_factures_lignes_h.montant_facture_actes_inclus_dans_sejour, 0::numeric) = 0::numeric THEN round(p_factures_lignes_h.prix_unitaire * p_factures_lignes_h.nb_prestation * p_factures_lignes_h.coefficient * p_factures_lignes_h.coefficient_mco, 2)
              ELSE 0::numeric
          END 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,
      0 AS no_oid,
      0 AS no_nb_lits
     FROM activite.p_factures_lignes_h
       JOIN activite.p_factures ON p_factures_lignes_h.no_facture = p_factures.no_facture
       JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour
       JOIN activite.t_lieux_c ON p_factures_lignes_h.lieu_id = t_lieux_c.oid
       JOIN activite.t_medecins_administratifs_c t_medecins_administratifs_fac ON p_factures_lignes_h.medecin_facture_id = t_medecins_administratifs_fac.oid
       JOIN activite.t_medecins_administratifs_c t_medecins_administratifs_cpt ON p_factures_lignes_h.medecin_comptabilise_id = t_medecins_administratifs_cpt.oid
       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
       JOIN activite.t_protocoles ON p_factures_lignes_h.protocole_id = t_protocoles.oid;