src: |
  SELECT p_factures_lignes_h.finess,
      p_factures.sejour_id,
      p_factures.oid AS facture_id,
      p_sejours.no_sejour,
      p_sejours.no_patient,
      p_sejours.date_entree,
      p_factures_lignes_h.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_sejours.date_sortie::timestamp with time zone, 'YYYYMM'::text) AS mois_sortie,
      p_sejours.date_sortie,
      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.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_sejours.ghs_id,
      p_sejours.type_sejour,
          CASE
              WHEN p_sejours.type_sejour = '1'::text THEN 'Hospitalisés'::text
              WHEN p_sejours.type_sejour = '2'::text THEN 'Ambulatoires'::text
              WHEN p_sejours.type_sejour = '3'::text THEN 'Externes'::text
              WHEN p_sejours.type_sejour = '4'::text THEN 'Long séjours'::text
              WHEN p_sejours.type_sejour = '5'::text THEN 'Séances'::text
              ELSE p_sejours.type_sejour
          END AS type_sejour_texte,
      p_factures_lignes_h.lieu_id,
      p_sejours.medecin_sejour_id AS medecin_adm_sejour_id,
      t_medecins_administratifs_c.adm_code AS medecin_adm_sejour_code,
      t_medecins_administratifs_c.adm_nom AS medecin_adm_sejour_nom,
      t_medecins_administratifs_c.adm_prenom AS medecin_adm_sejour_prenom,
      t_medecins_administratifs_c.adm_nom_prenom AS medecin_adm_sejour_nom_prenom,
      t_medecins_administratifs_c.medecin_id AS medecin_sejour_id,
      t_medecins_administratifs_c.nom AS medecin_sejour_nom,
      t_medecins_administratifs_c.prenom AS medecin_sejour_prenom,
      t_medecins_administratifs_c.nom_prenom AS medecin_sejour_nom_prenom,
      t_medecins_administratifs_c.specialite_id AS medecin_sejour_specialite_id,
      t_medecins_administratifs_c.specialite_code AS medecin_sejour_specialite_code,
      t_medecins_administratifs_c.specialite_texte AS medecin_sejour_specialite_texte,
      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.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.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.unite_medicale_id AS lieu_unite_id,
      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.medecin_id AS medecin_facture_id,
      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.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,
      p_factures_lignes_h.acte_id,
      t_actes_c.code AS acte_code,
      t_actes_c.nomenclature AS acte_nomenclature,
      t_actes_c.texte_court AS acte_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,
          CASE
              WHEN p_factures_lignes_h.montant_facture = 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,
          CASE
              WHEN t_ghs_c.cmd_code = '28'::text THEN 'S'::text
              ELSE t_ghs_c.mco_code
          END AS mcos_code,
          CASE
              WHEN t_ghs_c.cmd_code = '28'::text THEN t_ghs_c.cmd_texte
              ELSE
              CASE
                  WHEN t_ghs_c.mco_code = '*'::text THEN 'Hors GHM ou N/A'::text
                  ELSE t_ghs_c.mco_texte
              END
          END AS mcos_texte,
          CASE
              WHEN t_ghs_c.cmd_code = '28'::text THEN 4
              ELSE
              CASE t_ghs_c.mco_code
                  WHEN '*'::text THEN 0
                  WHEN 'M'::text THEN 1
                  WHEN 'C'::text THEN 2
                  WHEN 'O'::text THEN 3
                  ELSE NULL::integer
              END
          END AS mcos_id
     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_ghs_c ON p_sejours.ghs_id = t_ghs_c.ghs_id
       JOIN activite.t_ghm_c ON p_sejours.ghm_id = t_ghm_c.ghm_id
       JOIN activite.t_medecins_administratifs_c ON p_sejours.medecin_sejour_id = t_medecins_administratifs_c.oid
       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;