src: |
  SELECT t_expert_controle.oid AS expert_controle_id,
      t_expert_controle.code AS expert_controle_code,
      t_expert_controle.texte AS expert_controle_texte,
      COALESCE(t_expert_controle.texte_abrege, t_expert_controle.texte_court) AS expert_controle_texte_abrege,
      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.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,
      t_expert_controle.alert_exp,
      p_expert_controle_data.date_signalement,
      date_part('epoch'::text, p_expert_controle_data.date_signalement) AS date_signalement_id,
      p_expert_controle_data.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_controle_data.date_correction,
      date_part('epoch'::text, p_expert_controle_data.date_correction) AS date_correction_id,
      p_expert_controle_data.date_correction::text AS date_correction_code,
      p_calendrier_c.texte_court AS date_correction_texte,
      p_calendrier_c.mois AS date_correction_mois_id,
      to_char(p_calendrier_c.mois, 'FM000000'::text) AS date_correction_mois_code,
      (p_calendrier_c.mois_texte || ' '::text) || to_char(p_calendrier_c.iso_annee, 'FM0000'::text) AS date_correction_mois_texte,
      p_calendrier_c.semaine AS date_correction_semaine_id,
      to_char(p_calendrier_c.semaine, 'FM000000'::text) AS date_correction_semaine_code,
      (('Semaine '::text || to_char(p_calendrier_c.iso_semaine, 'FM00'::text)) || ' '::text) || to_char(p_calendrier_c.iso_annee, 'FM0000'::text) AS date_correction_semaine_texte,
      p_expert_controle_data.etat,
        CASE p_expert_controle_data.etat
            WHEN ''::text THEN ''::text
            WHEN 'I'::text THEN 'Ignoré'::text
            WHEN 'T'::text THEN 'Traité'::text
            ELSE p_expert_controle_data.etat
        END AS etat_texte,
      p_expert_controle_data.data_type,
      p_contrats_mois.oid AS contrat_mois_id,
      p_contrats_mois.date_debut AS date_debut_activite,
      p_contrats_mois.date_fin AS date_fin_activite,
      p_contrats.oid AS contrat_id,
      p_contrats.salarie_id AS salarie_id,
      t_expert_controle.is_active AS expert_controle_is_active,
      p_expert_controle_data.texte AS justificatif
    FROM rh.t_expert_controle
    JOIN rh.p_expert_controle_data ON p_expert_controle_data.expert_controle_id = t_expert_controle.oid
    JOIN rh.p_contrats ON p_contrats.numero_contrat = p_expert_controle_data.contrat_mois_data[3]
    JOIN rh.p_contrats_mois ON p_contrats_mois.contrat_id = p_contrats.oid AND p_contrats_mois.date_debut::text = p_expert_controle_data.contrat_mois_data[1] AND p_contrats_mois.date_fin::text = p_expert_controle_data.contrat_mois_data[2]
    JOIN base.p_calendrier p_calendrier_s ON p_expert_controle_data.date_signalement = p_calendrier_s.date
    JOIN base.p_calendrier p_calendrier_c ON p_expert_controle_data.date_signalement = 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 AND p_expert_controle_data.nb_cree = 1::numeric;