src: |
  SELECT p_croisement_paie_planning.planning_id,
      p_croisement_paie_planning.salarie_id,
      p_croisement_paie_planning.date,
      p_croisement_paie_planning.mois_paie,
      p_croisement_paie_planning.etablissement_id AS rhp_etablissement_id,
      p_croisement_paie_planning.contrat_mois_id,
      p_croisement_paie_planning.presence_detail,
      p_croisement_paie_planning.temps_du_initial AS temps_du_init,
      p_croisement_paie_planning.temps_du,
      p_croisement_paie_planning.temps_valide,
      p_croisement_paie_planning.heur_cont_pror_temp_du_init,
      p_croisement_paie_planning.heur_cont_pror_temp_du,
      p_croisement_paie_planning.heur_cont_pror_temp_vali,
      p_croisement_paie_planning.heur_paye_pror_temp_du_init,
      p_croisement_paie_planning.heur_paye_pror_temp_du,
      p_croisement_paie_planning.heur_paye_pror_temp_vali,
      p_croisement_paie_planning.heur_trav_pror_temp_du_init,
      p_croisement_paie_planning.heur_trav_pror_temp_du,
      p_croisement_paie_planning.heur_trav_pror_temp_vali,
      p_croisement_paie_planning.mont_brut_pror_temp_du_init,
      p_croisement_paie_planning.mont_brut_pror_temp_du,
      p_croisement_paie_planning.mont_brut_pror_temp_vali,
      p_croisement_paie_planning.mont_mass_sala_pror_temp_du_init,
      p_croisement_paie_planning.mont_mass_sala_pror_temp_du,
      p_croisement_paie_planning.mont_mass_sala_pror_temp_vali,
      p_croisement_paie_planning.mont_coti_patr_pror_temp_du_init,
      p_croisement_paie_planning.mont_coti_patr_pror_temp_du,
      p_croisement_paie_planning.mont_coti_patr_pror_temp_vali,
      p_croisement_paie_planning.mont_prov_pror_temp_du_init,
      p_croisement_paie_planning.mont_prov_pror_temp_du,
      p_croisement_paie_planning.mont_prov_pror_temp_vali,
      p_croisement_paie_planning.mont_mass_sala_prov_pror_temp_du_init,
      p_croisement_paie_planning.mont_mass_sala_prov_pror_temp_du,
      p_croisement_paie_planning.mont_mass_sala_prov_pror_temp_vali,
      p_croisement_paie_planning.mont_inte_pror_temp_du_init,
      p_croisement_paie_planning.mont_inte_pror_temp_du,
      p_croisement_paie_planning.mont_inte_pror_temp_vali,
      p_croisement_paie_planning.mont_mass_sala_inte_pror_temp_du_init,
      p_croisement_paie_planning.mont_mass_sala_inte_pror_temp_du,
      p_croisement_paie_planning.mont_mass_sala_inte_pror_temp_vali,
      p_croisement_paie_planning.mont_mass_sala_prov_inte_pror_temp_du_init,
      p_croisement_paie_planning.mont_mass_sala_prov_inte_pror_temp_du,
      p_croisement_paie_planning.mont_mass_sala_prov_inte_pror_temp_vali,
      p_croisement_paie_planning.mois_paie::bigint AS mois_activite_id,
      p_croisement_paie_planning.mois_paie::text AS mois_activite_code,
      (base.cti_to_french(to_char((p_croisement_paie_planning.mois_paie::text || '01'::text)::timestamp without time zone, 'Month'::text)) || ' '::text) || substr(p_croisement_paie_planning.mois_paie::text, 1, 4) AS mois_activite_texte,
      p_croisement_paie_planning.mois_paie AS annee_mois_activite_id,
      p_croisement_paie_planning.mois_paie AS annee_mois_activite_code,
      (
          CASE
              WHEN date_part('month'::text, p_croisement_paie_planning.date) = 1::double precision THEN 'Janvier'::text
              WHEN date_part('month'::text, p_croisement_paie_planning.date) = 2::double precision THEN 'Février'::text
              WHEN date_part('month'::text, p_croisement_paie_planning.date) = 3::double precision THEN 'Mars'::text
              WHEN date_part('month'::text, p_croisement_paie_planning.date) = 4::double precision THEN 'Avril'::text
              WHEN date_part('month'::text, p_croisement_paie_planning.date) = 5::double precision THEN 'Mai'::text
              WHEN date_part('month'::text, p_croisement_paie_planning.date) = 6::double precision THEN 'Juin'::text
              WHEN date_part('month'::text, p_croisement_paie_planning.date) = 7::double precision THEN 'Juillet'::text
              WHEN date_part('month'::text, p_croisement_paie_planning.date) = 8::double precision THEN 'Aout'::text
              WHEN date_part('month'::text, p_croisement_paie_planning.date) = 9::double precision THEN 'Septembre'::text
              WHEN date_part('month'::text, p_croisement_paie_planning.date) = 10::double precision THEN 'Octobre'::text
              WHEN date_part('month'::text, p_croisement_paie_planning.date) = 11::double precision THEN 'Novembre'::text
              WHEN date_part('month'::text, p_croisement_paie_planning.date) = 12::double precision THEN 'Décembre'::text
              ELSE date_part('month'::text, p_croisement_paie_planning.date)::text
          END || ' '::text) || date_part('year'::text, p_croisement_paie_planning.date)::text AS annee_mois_activite_texte,
      date_part('year'::text, p_croisement_paie_planning.date) AS annee_activite_id,
      date_part('year'::text, p_croisement_paie_planning.date) AS annee_activite_code,
      date_part('year'::text, p_croisement_paie_planning.date) AS annee_activite_texte,
      (substr(p_croisement_paie_planning.mois_paie::text, 1, 4) || date_part('quarter'::text, p_croisement_paie_planning.date)::text)::bigint AS annee_trimestre_activite_id,
      substr(p_croisement_paie_planning.mois_paie::text, 1, 4) || date_part('quarter'::text, p_croisement_paie_planning.date)::text AS annee_trimestre_activite_code,
      (('T'::text || date_part('quarter'::text, p_croisement_paie_planning.date)::text) || ' '::text) || substr(p_croisement_paie_planning.mois_paie::text, 1, 4) AS annee_trimestre_activite_texte,
      substr(p_croisement_paie_planning.mois_paie::text, 5, 2)::bigint AS mois_only_activite_id,
      substr(p_croisement_paie_planning.mois_paie::text, 5, 2) AS mois_only_activite_code,
      base.cti_to_french(to_char(p_croisement_paie_planning.date::timestamp with time zone, 'Month'::text)) AS mois_only_activite_texte,
      date_part('quarter'::text, p_croisement_paie_planning.date)::bigint AS trimestre_activite_id,
      date_part('quarter'::text, p_croisement_paie_planning.date)::text AS trimestre_activite_code,
      'T'::text || date_part('quarter'::text, p_croisement_paie_planning.date)::text AS trimestre_activite_texte,
      to_char(p_croisement_paie_planning.date::timestamp with time zone, 'IYYYIW'::text) AS semaine_activite_id,
      to_char(p_croisement_paie_planning.date::timestamp with time zone, 'IYYY-IW'::text) AS semaine_activite_code,
      'Semaine '::text || base.cti_to_french(to_char(p_croisement_paie_planning.date::timestamp with time zone, 'IW IYYY'::text)) AS semaine_activite_texte,
      to_char(p_croisement_paie_planning.date::timestamp with time zone, 'YYYYMMDD'::text) AS jour_activite_id,
      to_char(p_croisement_paie_planning.date::timestamp with time zone, 'YYYY-MM-DD'::text) AS jour_activite_code,
      base.cti_to_french(to_char(p_croisement_paie_planning.date::timestamp with time zone, 'Day DD month YYYY'::text)) AS jour_activite_texte,
      p_croisement_paie_planning.date AS date_debut_activite,
      p_croisement_paie_planning.date AS date_fin_activite,
      p_planning_mouvement.service_id AS rhp_planning_service_id,
      p_planning_mouvement.qualification_id AS rhp_planning_qualification_id,
      p_planning_mouvement.type_absence_id AS rhp_planning_type_absence_id,
      p_planning_mouvement.etablissement_id AS rhp_planning_etablissement_id,
      p_planning_mouvement.code_horaire_id AS rhp_planning_code_horaire_id,
      p_planning_mouvement.niveau_id AS rhp_planning_niveau_id,
      p_planning_mouvement.pole_id AS rhp_pole_id,
      p_planning_mouvement.absence_id AS planning_absence_id,
      p_planning_mouvement.absence_nb_jours AS planning_absence_nb_jours,
      p_salaries.finess,
      p_salaries.matricule AS salarie_code,
      (p_salaries.nom::text || ' '::text) || p_salaries.prenom::text AS salarie_texte,
      p_salaries.nom,
      p_salaries.nom_naissance,
      p_salaries.prenom,
      p_salaries.date_naissance,
      p_salaries.oid AS nir_id,
      p_salaries.nir AS nir_code,
      (p_salaries.nom::text || ' '::text) || p_salaries.prenom::text AS nir_texte,
          CASE
              WHEN p_salaries.sexe = 'F'::bpchar THEN 2
              ELSE 1
          END AS sexe_id,
      p_salaries.sexe AS sexe_code,
          CASE
              WHEN p_salaries.sexe = 'F'::bpchar THEN 'Féminin'::text
              ELSE 'Masculin'::text
          END AS sexe_texte,
      p_salaries.situation_famille_id AS rhp_situation_famille_id,
      p_salaries.code_original,
      p_salaries.entreprise_id AS rhp_entreprise_id,
      p_salaries.nationalite_id AS rhp_nationalite_id,
      p_salaries.code_postal_id AS rhp_code_postal_id,
      p_salaries.personne_id AS rhp_personne_id,
      (p_salaries.nom::text || ' '::text) || p_salaries.prenom::text AS contrat_texte,
      p_salaries.date_anciennete,
      p_salaries.date_entree_ets,
      p_salaries.date_sortie_ets,
      p_salaries.date_entree_fp,
      p_salaries.date_entree_fph,
      p_salaries.no_adeli,
      p_salaries.matricule_retraite,
      p_salaries.code_cotisation_id AS rhp_code_cotisation_id,
      p_salaries.section_id AS salarie_section_id,
      p_salaries.section_code AS salarie_section_code,
      p_salaries.section_texte AS salarie_section_texte,
      p_contrats_mois.fictif,
      p_contrats_mois.est_hors_periode,
      p_contrats_mois.nombre_debut_contrat,
      p_contrats_mois.nombre_fin_contrat,
      p_contrats_mois.nombre_entrees,
      p_contrats_mois.nombre_departs,
      p_contrats_mois.effectif_permanent,
      p_contrats_mois.present_debut_mois,
      p_contrats_mois.present_fin_mois,
      p_contrats_mois.nombre_transformation_cdi,
      p_contrats_mois.nombre_heures,
      p_contrats_mois.equivalent_temps_plein * p_profil_contrat_mois.ratio AS equivalent_temps_plein,
      p_contrats_mois.age_jours,
      p_contrats_mois.age_id AS rhp_age_id,
      p_contrats_mois.anciennete_annee_id AS rhp_anciennete_annee_id,
      p_contrats_mois.anciennete_reprise_annee_id AS rhp_anciennete_reprise_annee_id,
      p_contrats_mois.anciennete_jours,
      p_contrats_mois.anciennete_reprise_jours,
      p_contrats_mois.anciennete_mois,
      p_contrats_mois.anciennete_reprise_mois,
      p_contrats_mois.anciennete_fp_mois,
      p_contrats_mois.anciennete_fp_annee_id,
      p_contrats_mois.anciennete_fph_mois,
      p_contrats_mois.anciennete_fph_annee_id,
      p_contrats.numero_contrat AS contrat_code,
      p_contrats.salarie_remplace_id AS rhp_salarie_remplace_id,
      p_profil_contrat_mois.ratio,
      p_profil_contrat_mois.profil_id AS rhp_profil_id,
      p_profil_contrat_mois.population_id[1] AS rhp_population_id,
      p_profil_contrat_mois.budget_cle_id AS rhp_budget_cle_id
     FROM rh.p_croisement_paie_planning
       LEFT JOIN rh.p_planning_mouvement ON p_planning_mouvement.oid = p_croisement_paie_planning.planning_id
       JOIN rh.p_salaries ON p_salaries.oid = p_croisement_paie_planning.salarie_id
       JOIN rh.p_contrats_mois ON p_contrats_mois.oid = p_croisement_paie_planning.contrat_mois_id
       JOIN rh.p_contrats ON p_contrats.oid = p_contrats_mois.contrat_id
       JOIN rh.p_profil_contrat_mois ON p_profil_contrat_mois.contrat_mois_id = p_contrats_mois.oid AND p_profil_contrat_mois.profil_id = p_contrats_mois.profil_id;