src: |
  SELECT p_historique_paie.ctid,
      p_historique_paie.ctid AS rhp_ctid,
      p_historique_paie.etablissement_id AS rhp_etablissement_id,
      p_historique_paie.salarie_id,
      p_salaries.code_original AS salarie_code_original,
      p_salaries.code AS salarie_code,
      p_salaries.matricule,
      (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.date_debut AS date_debut_salarie,
      p_salaries.date_fin AS date_fin_salarie,
      p_salaries.situation_famille_id AS rhp_situation_famille_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.date_entree_ets,
      p_salaries.date_entree_fp,
      p_salaries.date_entree_fph,
      p_salaries.date_sortie_ets,
      p_contrats.salarie_remplace_id AS rhp_salarie_remplace_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_salaries.no_adeli,
      p_salaries.matricule_retraite,
      p_historique_paie.contrat_id,
      p_contrats_mois.oid AS contrat_mois_id,
      p_contrats.numero_contrat AS contrat_code,
      (p_salaries.nom::text || ' '::text) || p_salaries.prenom::text AS contrat_texte,
      p_contrats_mois.date_debut AS date_debut_activite,
      p_contrats_mois.date_fin AS date_fin_activite,
      p_contrats.date_debut AS date_debut_contrat,
      p_contrats.date_fin AS date_fin_contrat,
      p_contrats_mois.est_hors_periode,
      p_contrats_mois.nombre_debut_contrat,
      p_contrats_mois.nombre_fin_contrat,
      p_contrats_mois.effectif_permanent,
      p_contrats_mois.present_debut_mois,
      p_contrats_mois.present_fin_mois,
      p_contrats_mois.age_id AS rhp_age_id,
      p_salaries.date_anciennete,
      p_contrats_mois.anciennete_jours,
      p_contrats_mois.anciennete_reprise_jours,
      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_profil_contrat_mois.profil_id AS rhp_profil_id,
      p_contrats_mois.anciennete_fp_annee_id AS rhp_anciennete_fp_annee_id,
      p_contrats_mois.anciennete_fph_annee_id AS rhp_anciennete_fph_annee_id,
      p_salaries.code_cotisation_id AS rhp_code_cotisation_id,
      p_historique_paie.compte_id AS rhp_compte_id,
      p_historique_paie.organisme_cotisation_id AS rhp_organisme_cotisation_id,
      p_historique_paie.mois_activite::bigint AS mois_activite_id,
      p_historique_paie.mois_activite::text AS mois_activite_code,
      (base.cti_to_french(to_char((p_historique_paie.mois_activite::text || '01'::text)::timestamp without time zone, 'Month'::text)) || ' '::text) || substr(p_historique_paie.mois_activite::text, 1, 4) AS mois_activite_texte,
      p_historique_paie.mois_activite::bigint AS annee_mois_activite_id,
      p_historique_paie.mois_activite::text AS annee_mois_activite_code,
      (base.cti_to_french(to_char((p_historique_paie.mois_activite::text || '01'::text)::timestamp without time zone, 'Month'::text)) || ' '::text) || substr(p_historique_paie.mois_activite::text, 1, 4) AS annee_mois_activite_texte,
      (substr(p_historique_paie.mois_activite::text, 1, 4) || date_part('quarter'::text, (p_historique_paie.mois_activite::text || '01'::text)::date)::text)::bigint AS annee_trimestre_activite_id,
      substr(p_historique_paie.mois_activite::text, 1, 4) || date_part('quarter'::text, (p_historique_paie.mois_activite::text || '01'::text)::date)::text AS annee_trimestre_activite_code,
      (('T'::text || date_part('quarter'::text, (p_historique_paie.mois_activite::text || '01'::text)::date)::text) || ' '::text) || substr(p_historique_paie.mois_activite::text, 1, 4) AS annee_trimestre_activite_texte,
      substr(p_historique_paie.mois_activite::text, 1, 4)::bigint AS annee_activite_id,
      substr(p_historique_paie.mois_activite::text, 1, 4) AS annee_activite_code,
      substr(p_historique_paie.mois_activite::text, 1, 4) AS annee_activite_texte,
      substr(p_historique_paie.mois_activite::text, 5, 2)::bigint AS mois_only_activite_id,
      substr(p_historique_paie.mois_activite::text, 5, 2) AS mois_only_activite_code,
      base.cti_to_french(to_char((p_historique_paie.mois_activite::text || '01'::text)::timestamp without time zone, 'Month'::text)) AS mois_only_activite_texte,
      date_part('quarter'::text, (p_historique_paie.mois_activite::text || '01'::text)::date)::bigint AS trimestre_activite_id,
      date_part('quarter'::text, (p_historique_paie.mois_activite::text || '01'::text)::date)::text AS trimestre_activite_code,
      'T'::text || date_part('quarter'::text, (p_historique_paie.mois_activite::text || '01'::text)::date)::text AS trimestre_activite_texte,
      p_historique_paie.mois_paie::bigint AS mois_paie_id,
      p_historique_paie.mois_paie::text AS mois_paie_code,
      (base.cti_to_french(to_char(p_historique_paie.date_paie::timestamp with time zone, 'Month'::text)) || ' '::text) || substr(p_historique_paie.mois_paie::text, 1, 4) AS mois_paie_texte,
      p_historique_paie.mois_paie::bigint AS annee_mois_paie_id,
      p_historique_paie.mois_paie::text AS annee_mois_paie_code,
      (base.cti_to_french(to_char(p_historique_paie.date_paie::timestamp with time zone, 'Month'::text)) || ' '::text) || substr(p_historique_paie.mois_paie::text, 1, 4) AS annee_mois_paie_texte,
      (substr(p_historique_paie.mois_paie::text, 1, 4) || date_part('quarter'::text, p_historique_paie.date_paie)::text)::bigint AS annee_trimestre_paie_id,
      substr(p_historique_paie.mois_paie::text, 1, 4) || date_part('quarter'::text, p_historique_paie.date_paie)::text AS annee_trimestre_paie_code,
      (('T'::text || date_part('quarter'::text, p_historique_paie.date_paie)::text) || ' '::text) || substr(p_historique_paie.mois_paie::text, 1, 4) AS annee_trimestre_paie_texte,
      substr(p_historique_paie.mois_paie::text, 1, 4)::bigint AS annee_paie_id,
      substr(p_historique_paie.mois_paie::text, 1, 4) AS annee_paie_code,
      substr(p_historique_paie.mois_paie::text, 1, 4) AS annee_paie_texte,
      substr(p_historique_paie.mois_paie::text, 5, 2)::bigint AS mois_only_paie_id,
      substr(p_historique_paie.mois_paie::text, 5, 2) AS mois_only_paie_code,
      base.cti_to_french(to_char(p_historique_paie.date_paie::timestamp with time zone, 'Month'::text)) AS mois_only_paie_texte,
      date_part('quarter'::text, p_historique_paie.date_paie)::bigint AS trimestre_paie_id,
      date_part('quarter'::text, p_historique_paie.date_paie)::text AS trimestre_paie_code,
      'T'::text || date_part('quarter'::text, p_historique_paie.date_paie)::text AS trimestre_paie_texte,
      p_historique_paie.mois_activite AS mois_activite_paie_id,
      p_historique_paie.mois_activite AS mois_activite_paie_code,
      (
          CASE date_part('month'::text, p_historique_paie.date_fin)
              WHEN 1::double precision THEN 'Janvier'::text
              WHEN 2::double precision THEN 'Février'::text
              WHEN 3::double precision THEN 'Mars'::text
              WHEN 4::double precision THEN 'Avril'::text
              WHEN 5::double precision THEN 'Mai'::text
              WHEN 6::double precision THEN 'Juin'::text
              WHEN 7::double precision THEN 'Juillet'::text
              WHEN 8::double precision THEN 'Aout'::text
              WHEN 9::double precision THEN 'Septembre'::text
              WHEN 10::double precision THEN 'Octobre'::text
              WHEN 11::double precision THEN 'Novembre'::text
              WHEN 12::double precision THEN 'Décembre'::text
              ELSE date_part('month'::text, p_historique_paie.date_fin)::text
          END || ' '::text) || date_part('year'::text, p_historique_paie.date_fin)::text AS mois_activite_paie_texte,
      p_historique_paie.date_debut AS date_debut_paie,
      p_historique_paie.date_fin AS date_fin_paie,
      p_historique_paie.date_paie,
      p_historique_paie.rubrique_id,
      t_rubriques.code AS rubrique_code,
      t_rubriques.texte AS rubrique_texte,
      t_rubriques.rang_edition AS rubrique_rang_edition,
      t_rubriques.section_id AS rubrique_section_id,
      t_rubriques.section_code AS rubrique_section_code,
      t_rubriques.section_texte AS rubrique_section_texte,
      p_profil_contrat_mois.ratio * 999::numeric AS ratio,
      p_contrats_mois.equivalent_temps_plein * p_profil_contrat_mois.ratio * 999::numeric AS equivalent_temps_plein,
      p_historique_paie.base * p_profil_contrat_mois.ratio * 999::numeric AS base,
      p_historique_paie.nombre * p_profil_contrat_mois.ratio * 999::numeric AS nombre,
      p_historique_paie.montant_brut * p_profil_contrat_mois.ratio * 999::numeric AS montant_brut,
      p_historique_paie.montant_avantage_nature * p_profil_contrat_mois.ratio * 999::numeric AS montant_avantage_nature,
      p_historique_paie.montant_frais_imposables * p_profil_contrat_mois.ratio * 999::numeric AS montant_frais_imposables,
      p_historique_paie.taux_cotisation_salarie * p_profil_contrat_mois.ratio * 999::numeric AS taux_cotisation_salarie,
      p_historique_paie.montant_cotisation_salarie * p_profil_contrat_mois.ratio * 999::numeric AS montant_cotisation_salarie,
      p_historique_paie.montant_od_net_salarie * p_profil_contrat_mois.ratio * 999::numeric AS montant_od_net_salarie,
      p_historique_paie.montant_od_net_patronale * p_profil_contrat_mois.ratio * 999::numeric AS montant_od_net_patronale,
      p_historique_paie.taux_cotisation_patronale * p_profil_contrat_mois.ratio * 999::numeric AS taux_cotisation_patronale,
      p_historique_paie.montant_cotisation_patronale * p_profil_contrat_mois.ratio * 999::numeric AS montant_cotisation_patronale,
      p_historique_paie.montant_net_imposable_salarie * p_profil_contrat_mois.ratio * 999::numeric AS montant_net_imposable_salarie,
      p_historique_paie.montant_net_a_payer_salarie * p_profil_contrat_mois.ratio * 999::numeric AS montant_net_a_payer_salarie,
      (p_historique_paie.montant_brut + p_historique_paie.montant_avantage_nature + p_historique_paie.montant_frais_imposables + p_historique_paie.montant_cotisation_patronale + p_historique_paie.montant_od_net_patronale) * p_profil_contrat_mois.ratio * 999::numeric AS montant_masse_salariale,
      p_historique_paie.heure_contrat * p_profil_contrat_mois.ratio * 999::numeric AS heures_contrat,
      p_historique_paie.heure_payee * p_profil_contrat_mois.ratio * 999::numeric AS heures_payees,
      p_historique_paie.heure_travaillee * p_profil_contrat_mois.ratio * 999::numeric AS heures_travaillees,
      base.cti_division(p_historique_paie.heure_contrat * p_profil_contrat_mois.ratio * 999::numeric, t_etablissements.base_calcul_etp) AS etp_contrat,
      t_etablissements.base_calcul_etp,
          CASE
              WHEN t_rubriques.code::text = 'EFF1'::text THEN p_historique_paie.base * p_profil_contrat_mois.ratio * 999::numeric / 30::numeric
              ELSE 0::numeric
          END AS etp_30_paye,
      base.cti_division(p_historique_paie.heure_payee * p_profil_contrat_mois.ratio * 999::numeric, t_etablissements.base_calcul_etp) AS etp_heures_paye,
          CASE
              WHEN t_etablissements.mode_calcul_etp_paye::text = '1'::text THEN
              CASE
                  WHEN t_rubriques.code::text = 'EFF1'::text THEN p_historique_paie.base * p_profil_contrat_mois.ratio * 999::numeric / 30::numeric
                  ELSE 0::numeric
              END
              ELSE base.cti_division(p_historique_paie.heure_payee * p_profil_contrat_mois.ratio * 999::numeric, t_etablissements.base_calcul_etp)
          END AS etp_paye,
      base.cti_division(p_historique_paie.heure_travaillee * p_profil_contrat_mois.ratio * 999::numeric, t_etablissements.base_calcul_etp) AS etp_travaille,
      p_historique_paie.montant_brut_annuel AS montant_brut_annuel,
      p_historique_paie.montant_brut_mensuel_moyen AS montant_brut_mensuel_moyen,
      p_historique_paie.nombre_provisions * p_profil_contrat_mois.ratio AS nombre_provisions,
      p_historique_paie.montant_provisions * p_profil_contrat_mois.ratio AS montant_provisions,
      p_historique_paie.montant_masse_salariale_provisionnee * p_profil_contrat_mois.ratio AS montant_masse_salariale_provisionnee,
      999::bigint AS to_replace_id,
      'section_analytique_ng_code'::text AS to_replace_code,
      'section_analytique_ng_texte'::text AS to_replace_texte,
      p_profil_contrat_mois.pole_id AS rhp_pole_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_historique_paie
       JOIN ( SELECT 1) subq ON 1 = 1
       JOIN rh.t_divers ON t_divers.code = 'HEURES_W'::text
       JOIN rh.p_salaries ON p_historique_paie.salarie_id = p_salaries.oid
       JOIN rh.p_contrats_mois ON p_historique_paie.contrat_mois_id = p_contrats_mois.oid
       JOIN rh.p_contrats ON p_contrats_mois.contrat_id = p_contrats.oid
       JOIN rh.t_rubriques ON p_historique_paie.rubrique_id = t_rubriques.oid
       JOIN rh.t_etablissements ON p_contrats_mois.etablissement_id = t_etablissements.oid
       JOIN rh.p_profil_contrat_mois ON p_profil_contrat_mois.contrat_mois_id = p_contrats_mois.oid;