src: |
  SELECT t_finess_c.oid AS finess_id,
     p_sejours.finess AS finess_code,
     t_finess_c.texte_court AS finess_texte,
     t_finess_c.section_id AS finess_section_id,
     t_finess_c.section_code AS finess_section_code,
     t_finess_c.section_texte AS finess_section_texte,
     p_sejours.finess,
     p_sejours.provider_id,
     p_sejours.no_sejour,
     p_sejours.oid AS sejour_id,
     p_sejours.no_patient,
     p_sejours.code_sexe,
         CASE
             WHEN p_sejours.code_sexe = '1'::bpchar THEN 'M'::bpchar
             WHEN p_sejours.code_sexe = '2'::bpchar THEN 'F'::bpchar
             ELSE p_sejours.code_sexe
         END AS sexe_texte_court,
     p_sejours.age,
     p_sejours.code_postal_id,
     p_sejours.code_prevu,
     p_sejours.type_sejour,
     p_sejours.est_budget_global,
     t_type_sejour.oid AS type_sejour_id,
     p_sejours.type_sejour AS type_sejour_code,
     t_type_sejour.texte AS type_sejour_texte,
     t_type_sejour.section_id AS type_sejour_section_id,
     t_type_sejour.section_code AS type_sejour_section_code,
     t_type_sejour.section_texte AS type_sejour_section_texte,
     t_lieux_c.mode_traitement_id,
     t_lieux_c.mode_traitement_code,
     t_lieux_c.mode_traitement_texte,
     to_char(p_sejours.date_entree::timestamp with time zone, 'YYYYMM'::text)::numeric AS mois_entree,
     p_sejours.date_entree,
     p_sejours.heure_entree,
     p_sejours.mode_entree,
     p_sejours.provenance,
     to_char(p_sejours.date_sortie::timestamp with time zone, 'YYYYMM'::text)::numeric AS mois_sortie,
     p_sejours.code_sorti,
     p_sejours.date_sortie,
     p_sejours.heure_sortie,
     p_sejours.mode_sortie,
     p_sejours.destination,
     p_sejours.code_cp_demandee,
     p_sejours.ghs_id,
     t_ghs_c.ghs_code_text AS ghs_code,
     t_ghs_c.ghs_texte,
     p_sejours.ghm_id,
     t_ghm_c.ghm_code,
     t_ghm_c.ghm_texte,
     t_ghm_c.ghm5_id,
     t_ghm_c.ghm5_code,
     t_ghm_c.ghm5_texte,
     t_ghm_c.severite_ghm_id,
     t_ghm_c.severite_ghm_code,
     t_ghm_c.severite_ghm_texte,
     t_ghm_c.cas_id,
     t_ghm_c.cas_code,
     t_ghm_c.cas_texte,
     t_ghm_c.mco_id,
     t_ghm_c.mco_code,
     t_ghm_c.mco_texte,
     t_ghm_c.cmd_id,
     t_ghm_c.cmd_code,
     t_ghm_c.cmd_texte,
     t_ghm_c.mco_id AS mcos_id,
     t_ghm_c.mco_code AS mcos_code,
     t_ghm_c.mco_texte AS mcos_texte,
     t_ghm_c.ghm_section_id,
     t_ghm_c.ghm_section_code,
     t_ghm_c.ghm_section_texte,
     p_sejours.tiers_payant_0_id,
     p_sejours.tiers_payant_1_id,
     p_sejours.tiers_payant_2_id,
     p_sejours.tiers_payant_22_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.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.medecin_code AS medecin_sejour_code,
     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.service_facturation_section_nb_lits AS lieu_sortie_service_section_nb_lits,
     t_lieux_c.service_facturation_section_nb_cp AS lieu_sortie_service_section_nb_cp,
     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 AS lieu_sortie_lit_section_id,
     t_lieux_c.lit_section_code AS lieu_sortie_lit_section_code,
     t_lieux_c.lit_section_texte AS lieu_sortie_lit_section_texte,
     t_lieux_c.unite_medicale_id AS lieu_sortie_unite_medicale_id,
     t_lieux_c.unite_medicale_code AS lieu_sortie_unite_medicale_code,
     t_lieux_c.unite_medicale_texte AS lieu_sortie_unite_medicale_texte,
     t_lieux_c.unite_medicale_section_id AS lieu_sortie_unite_medicale_section_id,
     t_lieux_c.unite_medicale_section_code AS lieu_sortie_unite_medicale_section_code,
     t_lieux_c.unite_medicale_section_texte AS lieu_sortie_unite_medicale_section_texte,
     t_lieux_c.unite_fonctionnelle_id AS lieu_sortie_unite_fonctionnelle_id,
     t_lieux_c.unite_fonctionnelle_code AS lieu_sortie_unite_fonctionnelle_code,
     t_lieux_c.unite_fonctionnelle_texte AS lieu_sortie_unite_fonctionnelle_texte,
     t_lieux_c.unite_fonctionnelle_section_id AS lieu_sortie_unite_fonctionnelle_section_id,
     t_lieux_c.unite_fonctionnelle_section_code AS lieu_sortie_unite_fonctionnelle_section_code,
     t_lieux_c.unite_fonctionnelle_section_texte AS lieu_sortie_unite_fonctionnelle_section_texte,
     to_char(
         CASE
             WHEN p_sejours.ghs_id > 0 OR p_sejours.gmt_id > 0 THEN p_sejours.date_groupage
             ELSE '2099-12-31'::date
         END::timestamp with time zone, 'YYYYMM'::text)::numeric AS mois_groupage,
         CASE
             WHEN p_sejours.ghs_id > 0 OR p_sejours.gmt_id > 0 THEN p_sejours.date_groupage
             ELSE '2099-12-31'::date
         END AS date_groupage,
         CASE
             WHEN t_lieux_c.type_t2a = '1'::bpchar AND (p_sejours.type_sejour = ANY (ARRAY['1'::bpchar::text, '2'::bpchar::text, '5'::bpchar::text, '6'::bpchar::text])) AND (p_sejours.ghs_id > 0 OR p_sejours.gmt_id > 0) AND p_sejours.date_groupage < '2099-12-31'::date THEN 1
             ELSE 0
         END AS nb_delai_groupage,
         CASE
             WHEN t_lieux_c.type_t2a = '1'::bpchar AND (p_sejours.type_sejour = ANY (ARRAY['1'::bpchar::text, '2'::bpchar::text, '5'::bpchar::text, '6'::bpchar::text])) AND ((p_sejours.ghs_id > 0 OR p_sejours.gmt_id > 0) AND p_sejours.date_groupage < '2099-12-31'::date OR p_sejours.date_facture < '2099-12-31'::date) THEN 1
             ELSE 0
         END AS nb_groupe,
         CASE
             WHEN t_lieux_c.type_t2a = '1'::bpchar AND (p_sejours.type_sejour = ANY (ARRAY['1'::bpchar::text, '2'::bpchar::text, '5'::bpchar::text, '6'::bpchar::text])) AND (p_sejours.ghs_id > 0 OR p_sejours.gmt_id > 0) AND p_sejours.date_groupage < '2099-12-31'::date THEN p_sejours.delai_groupage
             ELSE 0::numeric
         END AS delai_groupage,
         CASE
             WHEN p_sejours.type_sejour <> ALL (ARRAY['1'::bpchar::text, '2'::bpchar::text, '5'::bpchar::text, '6'::bpchar::text]) THEN 0
             WHEN p_sejours.code_sorti <> 1::numeric OR t_lieux_c.type_t2a <> '1'::bpchar OR p_sejours.date_groupage < '2099-12-31'::date AND (p_sejours.ghs_id > 0 OR p_sejours.gmt_id > 0) OR p_sejours.date_facture < '2099-12-31'::date THEN 0
             ELSE 1
         END AS nb_non_groupe,
     p_sejours.est_sans_facturation,
     to_char(p_sejours.date_facture::timestamp with time zone, 'YYYYMM'::text)::numeric AS mois_facture,
     p_sejours.date_facture,
         CASE
             WHEN p_sejours.est_sans_facturation = '1'::bpchar THEN 0
             WHEN p_sejours.date_facture < '2099-12-31'::date THEN 1
             ELSE 0
         END AS nb_delai_facture,
         CASE
             WHEN p_sejours.est_sans_facturation = '1'::bpchar THEN 0::numeric
             WHEN p_sejours.date_facture < '2099-12-31'::date THEN p_sejours.delai_facture
             ELSE 0::numeric
         END AS delai_facture,
         CASE
             WHEN p_sejours.est_sans_facturation = '1'::bpchar THEN 0
             WHEN p_sejours.date_facture = '2099-12-31'::date THEN 1
             ELSE 0
         END AS nb_non_facture,
     to_char(p_sejours.date_expedition::timestamp with time zone, 'YYYYMM'::text)::numeric AS mois_expedition,
     p_sejours.date_expedition,
         CASE
             WHEN p_sejours.date_facture < '2099-12-31'::date AND p_sejours.date_expedition < '2099-12-31'::date THEN 1
             ELSE 0
         END AS nb_delai_expedition,
         CASE
             WHEN p_sejours.date_facture = '2099-12-31'::date THEN 1
             WHEN p_sejours.date_expedition_1 = '2099-12-31'::date AND (p_sejours.montant_comptabilise_1_c <> p_sejours.montant_regle_1_c OR p_sejours.montant_comptabilise_1_h <> p_sejours.montant_regle_1_h) THEN 1
             WHEN p_sejours.date_expedition_2 = '2099-12-31'::date AND (p_sejours.montant_comptabilise_2_c <> p_sejours.montant_regle_2_c OR p_sejours.montant_comptabilise_2_h <> p_sejours.montant_regle_2_h) THEN 1
             WHEN p_sejours.date_expedition_22 = '2099-12-31'::date AND (p_sejours.montant_comptabilise_22_c <> p_sejours.montant_regle_22_c OR p_sejours.montant_comptabilise_22_h <> p_sejours.montant_regle_22_h) THEN 1
             ELSE 0
         END AS nb_non_expedie,
         CASE
             WHEN p_sejours.date_facture = '2099-12-31'::date OR p_sejours.date_expedition_1 = '2099-12-31'::date THEN p_sejours.montant_comptabilise_1_c + p_sejours.montant_comptabilise_1_h - p_sejours.montant_regle_1_c - p_sejours.montant_regle_1_h
             ELSE 0::numeric
         END +
         CASE
             WHEN p_sejours.date_facture = '2099-12-31'::date OR p_sejours.date_expedition_2 = '2099-12-31'::date THEN p_sejours.montant_comptabilise_2_c + p_sejours.montant_comptabilise_2_h - p_sejours.montant_regle_2_c - p_sejours.montant_regle_2_h
             ELSE 0::numeric
         END +
         CASE
             WHEN p_sejours.date_facture = '2099-12-31'::date OR p_sejours.date_expedition_22 = '2099-12-31'::date THEN p_sejours.montant_comptabilise_22_c + p_sejours.montant_comptabilise_22_h - p_sejours.montant_regle_22_c - p_sejours.montant_regle_22_h
             ELSE 0::numeric
         END AS montant_non_expedie,
         CASE
             WHEN p_sejours.date_facture < '2099-12-31'::date OR p_sejours.date_expedition < '2099-12-31'::date THEN p_sejours.delai_expedition
             ELSE 0::numeric
         END AS delai_expedition,
     to_char(p_sejours.date_solde::timestamp with time zone, 'YYYYMM'::text)::numeric AS mois_solde,
     p_sejours.date_solde,
         CASE
             WHEN p_sejours.date_facture < '2099-12-31'::date AND p_sejours.date_solde < '2099-12-31'::date AND p_sejours.montant_regle_c = p_sejours.montant_comptabilise_c AND p_sejours.montant_regle_h = p_sejours.montant_comptabilise_h THEN 1
             ELSE 0
         END AS nb_delai_solde,
         CASE
             WHEN p_sejours.date_facture = '2099-12-31'::date OR p_sejours.montant_regle_c <> p_sejours.montant_comptabilise_c OR p_sejours.montant_regle_h <> p_sejours.montant_comptabilise_h THEN 1
             ELSE 0
         END AS nb_non_solde,
         CASE
             WHEN p_sejours.date_facture < '2099-12-31'::date AND p_sejours.montant_regle_c = p_sejours.montant_comptabilise_c AND p_sejours.montant_regle_h = p_sejours.montant_comptabilise_h THEN 0::numeric
             ELSE p_sejours.montant_comptabilise_c + p_sejours.montant_comptabilise_h - p_sejours.montant_regle_c - p_sejours.montant_regle_h
         END AS montant_non_solde,
         CASE
             WHEN p_sejours.date_facture < '2099-12-31'::date AND p_sejours.date_solde < '2099-12-31'::date AND p_sejours.montant_regle_c = p_sejours.montant_comptabilise_c AND p_sejours.montant_regle_h = p_sejours.montant_comptabilise_h THEN p_sejours.delai_solde
             ELSE 0::numeric
         END AS delai_solde,
     p_sejours.montant_facture_c,
     p_sejours.montant_facture_0_c,
     p_sejours.montant_facture_1_c,
     p_sejours.montant_facture_2_c,
     p_sejours.montant_facture_22_c,
     p_sejours.montant_facture_h,
     p_sejours.montant_facture_0_h,
     p_sejours.montant_facture_1_h,
     p_sejours.montant_facture_2_h,
     p_sejours.montant_facture_22_h,
     p_sejours.montant_facture_c_actes_inclus_dans_sejour,
     p_sejours.montant_facture_h_actes_inclus_dans_sejour,
     p_sejours.montant_comptabilise_c,
     p_sejours.montant_comptabilise_h,
     p_sejours.montant_comptabilise_budget_global_c,
     p_sejours.montant_regle_c,
     p_sejours.montant_regle_h,
     1 AS nb_sejours,
     p_sejours.nb_factures,
     p_sejours.nb_factures_regularisation,
     p_sejours.nb_factures_rejet,
     0 AS no_oid,
         CASE
             WHEN p_sejours.type_sejour = ANY (ARRAY['3'::bpchar::text, '5'::bpchar::text, '9'::bpchar::text]) THEN 0
             WHEN p_sejours.date_sortie <> '2099-12-31'::date AND p_sejours.date_sortie <= date(now()) AND p_sejours.date_sortie IS NOT NULL AND p_sejours.date_sortie >= p_sejours.date_entree THEN p_sejours.date_sortie - p_sejours.date_entree
             WHEN p_sejours.type_sejour = '1'::text AND p_sejours.date_entree > '1970-01-01'::date AND date(now()) > p_sejours.date_entree THEN date(now()) - p_sejours.date_entree
             ELSE 0
         END AS duree,
     p_sejours.gme_id,
     t_gme_c.gme_code,
     t_gme_c.gme_texte,
     t_gme_c.rgme_id,
     t_gme_c.rgme_code,
     t_gme_c.rgme_texte,
     t_gme_c.gn_id,
     t_gme_c.gn_code,
     t_gme_c.gn_texte,
     t_gme_c.cm_id,
     t_gme_c.cm_code,
     t_gme_c.cm_texte,
     p_sejours.risque_id,
         CASE
             WHEN t_ghm_c.ghm_code ~~* '28%'::text THEN '1'::text
             WHEN t_ghm_c.severite_ghm_code = 'J'::bpchar THEN '2'::text
             WHEN t_ghm_c.severite_ghm_code = 'T'::bpchar THEN '3'::text
             WHEN t_ghm_c.severite_ghm_code = 'Z'::bpchar THEN '4'::text
             WHEN t_ghm_c.severite_ghm_code = '1'::bpchar THEN '5'::text
             WHEN t_ghm_c.severite_ghm_code = ANY (ARRAY['A'::bpchar, 'B'::bpchar, 'C'::bpchar, 'D'::bpchar, 'E'::bpchar]) THEN '6'::text
             WHEN
             CASE
                 WHEN p_sejours.date_sortie <> '2099-12-31'::date AND p_sejours.date_sortie <= date(now()) AND p_sejours.date_sortie IS NOT NULL AND p_sejours.date_sortie >= p_sejours.date_entree THEN p_sejours.date_sortie - p_sejours.date_entree
                 ELSE 0
             END::numeric > 2::numeric THEN '7'::text
             WHEN t_ghm_c.severite_ghm_code = '*'::bpchar THEN '8'::text
             ELSE NULL::text
         END AS severite_cti_code,
         CASE
             WHEN t_ghm_c.ghm_code ~~* '28%'::text THEN 'Séances'::text
             WHEN t_ghm_c.severite_ghm_code = 'J'::bpchar THEN 'Sévérité J'::text
             WHEN t_ghm_c.severite_ghm_code = 'T'::bpchar THEN 'Sévérité T'::text
             WHEN t_ghm_c.severite_ghm_code = 'Z'::bpchar THEN 'Sévérité Z'::text
             WHEN t_ghm_c.severite_ghm_code = '1'::bpchar THEN 'Sévérité 1'::text
             WHEN t_ghm_c.severite_ghm_code = ANY (ARRAY['A'::bpchar, 'B'::bpchar, 'C'::bpchar, 'D'::bpchar, 'E'::bpchar]) THEN 'Sévérité ABCDE'::text
             WHEN
             CASE
                 WHEN p_sejours.date_sortie <> '2099-12-31'::date AND p_sejours.date_sortie <= date(now()) AND p_sejours.date_sortie IS NOT NULL AND p_sejours.date_sortie >= p_sejours.date_entree THEN p_sejours.date_sortie - p_sejours.date_entree
                 ELSE 0
             END::numeric > 2::numeric THEN 'DS > 2 nuits'::text
             WHEN t_ghm_c.severite_ghm_code = '*'::bpchar THEN 'Non PMSI'::text
             ELSE NULL::text
         END AS severite_cti_texte,
         CASE
             WHEN t_ghm_c.ghm_code ~~* '28%'::text THEN '1'::text
             WHEN t_ghm_c.severite_ghm_code = 'J'::bpchar THEN '2'::text
             WHEN t_ghm_c.severite_ghm_code = 'T'::bpchar THEN '3'::text
             WHEN t_ghm_c.severite_ghm_code = 'Z'::bpchar THEN '4'::text
             WHEN t_ghm_c.severite_ghm_code = '1'::bpchar THEN '5'::text
             WHEN t_ghm_c.severite_ghm_code = ANY (ARRAY['A'::bpchar, 'B'::bpchar, 'C'::bpchar, 'D'::bpchar, 'E'::bpchar]) THEN '6'::text
             WHEN
             CASE
                 WHEN p_sejours.date_sortie <> '2099-12-31'::date AND p_sejours.date_sortie <= date(now()) AND p_sejours.date_sortie IS NOT NULL AND p_sejours.date_sortie >= p_sejours.date_entree THEN p_sejours.date_sortie - p_sejours.date_entree
                 ELSE 0
             END::numeric > 2::numeric THEN '7'::text
             WHEN t_ghm_c.severite_ghm_code = '*'::bpchar THEN '8'::text
             ELSE NULL::text
         END AS severite_cti_id,
     p_sejours.gmt_id,
     t_gmt_c.gmt_code
    FROM activite.p_sejours
      JOIN activite.t_type_sejour ON p_sejours.type_sejour = t_type_sejour.code
      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_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_gme_c ON p_sejours.gme_id = t_gme_c.gme_id
      LEFT JOIN activite.t_finess_c ON p_sejours.finess = t_finess_c.code
      LEFT JOIN activite.t_gmt_c ON p_sejours.gmt_id = t_gmt_c.gmt_id
   WHERE p_sejours.etat = ''::bpchar;