src: |
  SELECT p_sejours.finess,
      p_sejours.no_patient,
      p_factures.sejour_id,
      p_factures.oid AS facture_id,
      p_sejours.no_sejour,
      p_factures.no_facture,
      p_sejours.code_sexe,
          CASE
              WHEN p_sejours.code_sexe::text = '1'::text THEN 'M'::text
              WHEN p_sejours.code_sexe::text = '2'::text THEN 'F'::text
              ELSE p_sejours.type_sejour
          END AS sexe_texte_court,
      p_sejours.age,
      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,
      to_char(p_sejours.date_entree::timestamp with time zone, 'YYYYMM'::text)::numeric AS mois_entree,
      p_sejours.date_entree,
      p_sejours.code_sorti,
      to_char(p_sejours.date_sortie::timestamp with time zone, 'YYYYMM'::text)::numeric AS mois_sortie,
      p_sejours.date_sortie,
      to_char(p_factures.date_facture::timestamp with time zone, 'YYYYMM'::text)::numeric AS mois_facture,
      p_factures.date_facture,
      to_char(p_factures.date_vente::timestamp with time zone, 'YYYYMM'::text)::numeric AS mois_vente,
      p_factures.date_vente,
      to_char(p_factures_soldes_c.date_comptable::timestamp with time zone, 'YYYYMM'::text)::numeric AS mois_comptable,
      p_factures_soldes_c.date_comptable,
      p_sejours.ghs_id,
      t_types_tiers_payant.code AS type_tiers_payant_id,
      t_types_tiers_payant.code AS type_tiers_payant_code,
      t_types_tiers_payant.texte_court AS type_tiers_payant_texte,
          CASE
              WHEN p_factures.tiers_payant_0_id = t_tiers_payant.oid THEN p_factures.tiers_payant_0_id
              WHEN p_factures.tiers_payant_1_id = t_tiers_payant.oid THEN p_factures.tiers_payant_1_id
              WHEN p_factures.tiers_payant_2_id = t_tiers_payant.oid THEN p_factures.tiers_payant_2_id
              WHEN p_factures.tiers_payant_22_id = t_tiers_payant.oid THEN p_factures.tiers_payant_22_id
              ELSE NULL::bigint
          END AS tiers_payant_id,
      t_tiers_payant.code AS tiers_payant_code,
      t_tiers_payant.texte_court AS tiers_payant_texte,
      t_tiers_payant.grand_regime_id AS tiers_payant_grand_regime_id,
      t_tiers_payant.grand_regime_code AS tiers_payant_grand_regime_code,
      t_tiers_payant.grand_regime_texte AS tiers_payant_grand_regime_texte,
      t_tiers_payant.section_id AS tiers_payant_section_id,
      t_tiers_payant.section_code AS tiers_payant_section_code,
      t_tiers_payant.section_texte AS tiers_payant_section_texte,
      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.section_id AS medecin_adm_sejour_section_id,
      t_medecins_administratifs_c.section_code AS medecin_adm_sejour_section_code,
      t_medecins_administratifs_c.section_texte AS medecin_adm_sejour_section_texte,
      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,
      p_sejours.lieu_sortie_id,
      t_lieux_c.service_facturation_id AS lieu_sortie_service_id,
      t_lieux_c.service_facturation_code AS lieu_sortie_service_code,
      t_lieux_c.service_facturation_texte AS lieu_sortie_service_texte,
      t_lieux_c.service_facturation_section_id AS lieu_sortie_service_section_id,
      t_lieux_c.service_facturation_section_code AS lieu_sortie_service_section_code,
      t_lieux_c.service_facturation_section_texte AS lieu_sortie_service_section_texte,
      t_lieux_c.activite_id AS lieu_sortie_activite_id,
      t_lieux_c.activite_code AS lieu_sortie_activite_code,
      t_lieux_c.activite_texte AS lieu_sortie_activite_texte,
      t_lieux_c.etage_id AS lieu_sortie_etage_id,
      t_lieux_c.etage_code AS lieu_sortie_etage_code,
      t_lieux_c.etage_texte AS lieu_sortie_etage_texte,
      t_lieux_c.etage_section_id AS lieu_sortie_etage_section_id,
      t_lieux_c.etage_section_code AS lieu_sortie_etage_section_code,
      t_lieux_c.etage_section_texte AS lieu_sortie_etage_section_texte,
      t_lieux_c.lit_id AS lieu_sortie_lit_id,
      t_lieux_c.lit_code AS lieu_sortie_lit_code,
      t_lieux_c.lit_texte AS lieu_sortie_lit_texte,
      t_lieux_c.lit_section_id,
      t_lieux_c.lit_section_code,
      t_lieux_c.lit_section_texte,
      t_lieux_c.unite_medicale_id AS lieu_sortie_unite_id,
      p_factures_soldes_c.rubrique_comptabilisee_id,
      t_rubriques_facturation.code AS rubrique_comptabilisee_code,
      t_rubriques_facturation.texte_court AS rubrique_comptabilisee_texte,
      p_factures_soldes_c.prestation_id,
      t_prestations.code AS prestation_code,
      t_prestations.texte_court AS prestation_texte,
          CASE
              WHEN p_factures.tiers_payant_0_id = t_tiers_payant.oid THEN p_factures_soldes_c.montant_comptabilise_0
              WHEN p_factures.tiers_payant_1_id = t_tiers_payant.oid THEN p_factures_soldes_c.montant_comptabilise_1
              WHEN p_factures.tiers_payant_2_id = t_tiers_payant.oid THEN p_factures_soldes_c.montant_comptabilise_2
              WHEN p_factures.tiers_payant_22_id = t_tiers_payant.oid THEN p_factures_soldes_c.montant_comptabilise_22
              ELSE 0::numeric
          END AS montant_comptabilise,
          CASE
              WHEN p_factures.tiers_payant_0_id = t_tiers_payant.oid THEN p_factures_soldes_c.montant_regle_0
              WHEN p_factures.tiers_payant_1_id = t_tiers_payant.oid THEN p_factures_soldes_c.montant_regle_1
              WHEN p_factures.tiers_payant_2_id = t_tiers_payant.oid THEN p_factures_soldes_c.montant_regle_2
              WHEN p_factures.tiers_payant_22_id = t_tiers_payant.oid THEN p_factures_soldes_c.montant_regle_22
              ELSE 0::numeric
          END AS montant_regle,
          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_soldes_c
       JOIN activite.p_factures ON p_factures_soldes_c.no_facture = p_factures.no_facture
       JOIN activite.t_tiers_payant ON p_factures.tiers_payant_0_id = t_tiers_payant.oid AND p_factures.tiers_payant_0_id > 0 OR p_factures.tiers_payant_1_id = t_tiers_payant.oid AND p_factures.tiers_payant_1_id > 0 OR p_factures.tiers_payant_2_id = t_tiers_payant.oid AND p_factures.tiers_payant_2_id > 0 OR p_factures.tiers_payant_22_id = t_tiers_payant.oid AND p_factures.tiers_payant_22_id > 0
       JOIN activite.t_types_tiers_payant ON t_tiers_payant.type_tiers_payant = t_types_tiers_payant.code
       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_sejours.lieu_sortie_id = t_lieux_c.oid
       JOIN activite.t_rubriques_facturation ON p_factures_soldes_c.rubrique_comptabilisee_id = t_rubriques_facturation.oid
       JOIN activite.t_prestations ON p_factures_soldes_c.prestation_id = t_prestations.oid;