src: |
  SELECT t_expert_controle.oid AS expert_controle_id,
      t_expert_controle.code AS expert_controle_code,
      t_expert_controle.texte_court AS expert_controle_texte,
      COALESCE(t_expert_controle.texte_abrege, t_expert_controle.texte_court) AS expert_controle_texte_abrege,
      t_expert_controle.is_datim AS expert_controle_is_datim,
      t_expert_controle.code_datim AS expert_controle_code_datim,
      t_expert_controle.gravite_id AS expert_controle_gravite_id,
      to_char(t_expert_controle.gravite_id, 'FM0'::text) AS expert_controle_gravite_code,
          CASE
              WHEN t_expert_controle.gravite_id = 0 THEN 'INFO'::text
              WHEN t_expert_controle.gravite_id >= 1 AND t_expert_controle.gravite_id <= 5 THEN 'ATT'::text
              WHEN t_expert_controle.gravite_id >= 6 AND t_expert_controle.gravite_id <= 9 THEN 'ERREUR'::text
              ELSE 'ATT'::text
          END AS expert_controle_gravite_texte,
      t_expert_controle.seuil_alert_exp,
      t_expert_controle.alert_exp,
      t_expert_controle.alert_exp_constatee,
      t_expert_controle.date_validite_debut AS expert_controle_date_validite_debut,
      t_expert_controle.date_validite_fin AS expert_controle_date_validite_fin,
      t_expert_controle.section_id AS expert_controle_section_id,
      t_expert_controle.section_code AS expert_controle_section_code,
      t_expert_controle.section_texte AS expert_controle_section_texte,
      p_expert_rss_controle.date_signalement,
      date_part('epoch'::text, p_expert_rss_controle.date_signalement) AS date_signalement_id,
      p_expert_rss_controle.date_signalement::text AS date_signalement_code,
      p_calendrier_s.texte_court AS date_signalement_texte,
      p_calendrier_s.mois AS date_signalement_mois_id,
      to_char(p_calendrier_s.mois, 'FM000000'::text) AS date_signalement_mois_code,
      (p_calendrier_s.mois_texte || ' '::text) || to_char(p_calendrier_s.iso_annee, 'FM0000'::text) AS date_signalement_mois_texte,
      p_calendrier_s.semaine AS date_signalement_semaine_id,
      to_char(p_calendrier_s.semaine, 'FM000000'::text) AS date_signalement_semaine_code,
      (('Semaine '::text || to_char(p_calendrier_s.iso_semaine, 'FM00'::text)) || ' '::text) || to_char(p_calendrier_s.iso_annee, 'FM0000'::text) AS date_signalement_semaine_texte,
      p_expert_rss_controle.date_correction,
          CASE
              WHEN p_expert_rss_controle.etat = ANY (ARRAY['I'::text, 'T'::text]) THEN date_part('epoch'::text, p_expert_rss_controle.date_correction)
              ELSE 0::double precision
          END AS date_correction_id,
          CASE
              WHEN p_expert_rss_controle.etat = ANY (ARRAY['I'::text, 'T'::text]) THEN p_expert_rss_controle.date_correction::text
              ELSE chr(127) || '********'::text
          END AS date_correction_code,
          CASE
              WHEN p_expert_rss_controle.etat = ANY (ARRAY['I'::text, 'T'::text]) THEN p_calendrier_c.texte_court::character varying
              ELSE 'Non corrigé'::character varying
          END AS date_correction_texte,
          CASE
              WHEN p_expert_rss_controle.etat = ANY (ARRAY['I'::text, 'T'::text]) THEN p_calendrier_c.mois
              ELSE 0::numeric
          END AS date_correction_mois_id,
          CASE
              WHEN p_expert_rss_controle.etat = ANY (ARRAY['I'::text, 'T'::text]) THEN to_char(p_calendrier_c.mois, 'FM000000'::text)
              ELSE chr(127) || '******'::text
          END AS date_correction_mois_code,
          CASE
              WHEN p_expert_rss_controle.etat = ANY (ARRAY['I'::text, 'T'::text]) THEN (p_calendrier_c.mois_texte || ' '::text) || to_char(p_calendrier_c.iso_annee, 'FM0000'::text)
              ELSE 'Non corrigé'::text
          END AS date_correction_mois_texte,
          CASE
              WHEN p_expert_rss_controle.etat = ANY (ARRAY['I'::text, 'T'::text]) THEN p_calendrier_c.semaine
              ELSE 0::numeric
          END AS date_correction_semaine_id,
          CASE
              WHEN p_expert_rss_controle.etat = ANY (ARRAY['I'::text, 'T'::text]) THEN to_char(p_calendrier_c.semaine, 'FM000000'::text)
              ELSE chr(127) || '*****'::text
          END AS date_correction_semaine_code,
          CASE
              WHEN p_expert_rss_controle.etat = ANY (ARRAY['I'::text, 'T'::text]) THEN (('Semaine '::text || to_char(p_calendrier_c.iso_semaine, 'FM00'::text)) || ' '::text) || to_char(p_calendrier_c.iso_annee, 'FM0000'::text)
              ELSE 'Non corrigé'::text
          END AS date_correction_semaine_texte,
          CASE
              WHEN (p_expert_rss_controle.etat = ANY (ARRAY['I'::text, 'T'::text])) AND p_expert_rss_controle.date_correction <= date(now()) AND p_expert_rss_controle.date_correction >= p_expert_rss_controle.date_signalement THEN p_expert_rss_controle.date_correction - p_expert_rss_controle.date_signalement
              ELSE NULL::integer
          END AS delai_correction,
      p_expert_rss_controle.etat,
          CASE p_expert_rss_controle.etat
              WHEN ''::text THEN ''::text
              WHEN 'I'::text THEN 'Ignoré'::text
              WHEN 'T'::text THEN 'Traité'::text
              ELSE p_expert_rss_controle.etat
          END AS etat_texte,
      p_expert_rss_controle.rss_id,
      p_expert_rss_controle.code_groupe,
      p_expert_rss_controle.code_justificatif,
      p_expert_rss_controle.cumul_base,
      p_expert_rss_controle.coefficient_attendu,
      p_expert_rss_controle.cumul_realise,
      p_expert_rss_controle.cumul_attendu,
      p_expert_rss_controle.borne_alerte
     FROM pmsi.t_expert_controle
       JOIN pmsi.p_expert_rss_controle ON p_expert_rss_controle.controle_id = t_expert_controle.oid
       JOIN base.p_calendrier p_calendrier_s ON p_expert_rss_controle.date_signalement = p_calendrier_s.date
       LEFT JOIN base.p_calendrier p_calendrier_c ON p_expert_rss_controle.date_correction = p_calendrier_c.date
    WHERE t_expert_controle.is_hide IS DISTINCT FROM '1'::bpchar AND t_expert_controle.is_global IS DISTINCT FROM '1'::bpchar;