= 100 then 'TC' else 'TP'||(@toto.horpourcttxpl)::text end as ttt_code_original,
@toto.horpourcttxpl / 100 as etp_cnt
from toto
window w as (partition by toto.oid order by toto.begin_date)
;
drop table if exists w_cnt_tho
;
create temp table w_cnt_tho as
select
salarie.matricule as mat,
salarie.matricule||'-'||(rank() over w)::text as tho_num,
xrcaffectcompl.begin_date::date as date_debut,
coalesce(lead(xrcaffectcompl.begin_date::date) over w - 1, '20991231'::date) as date_fin,
xta_jrnuitaltsr.code as tho_code_original
from prod_pleiades2.relationcontrat
join prod_pleiades2.salarie on salarie.oid = relationcontrat.relatmatricule
join prod_pleiades2.xrcaffectcompl on xrcaffectcompl.relationcontrat = relationcontrat.oid
join prod_pleiades2.xta_jrnuitaltsr on xta_jrnuitaltsr.oid = xrcaffectcompl.xta_jrnuitaltsr
window w as (partition by salarie.oid order by xrcaffectcompl.begin_date)
;
-- Création table des contrats.
-- Source intégrateur Pleiades : "Attention, sur E5, le passage de CDD à CDI n'est pas à l'origine d'un nouveau contrat contrairement à Pléiades CS".
-- On mélange donc les 2 historiques de contrat et de type de contrat pour n'en faire qu'une seul comme dans CS.
drop table if exists w_cnt
;
create temp table w_cnt as
with toto as (
select
salarie.matricule as mat,
salarie.matricule||'-'||row_number() over w as cnt, -- relationcontrat.relatnum
relationcontrat.relatnum,
typejuridqctr.begin_date::date as date_debut, -- relationcontrat.relatdatedeb
coalesce(nullif(typejuridqctr.end_date::date, '29990101'::date), relationcontrat.relatdatefin::date, '20991231'::date) as date_fin, -- relationcontrat.relatdatefin
nullif(ta_relamotifdeb.relatcodedeb, '') as motif_debut,
coalesce(case when lead(contrat.ctrrelation) over w = contrat.ctrrelation then 'CTI-TO_CDI' else null end, nullif(ta_relamotiffin.relationcodefin, '')) as motif_fin,
ta_relamotiffin.relationcodefin,
ta_contrattype.contrattypecode as type_contrat,
contrat.ctrsociete as etablissement,
coalesce(xunitefct.code, chr(1)||'*') as uf_localisation,
null::text as mat_remp
from prod_pleiades2.contrat
join prod_pleiades2.relationcontrat on relationcontrat.oid = contrat.ctrrelation
join prod_pleiades2.salarie on salarie.oid = relationcontrat.relatmatricule
join prod_pleiades2.ta_relamotifdeb on ta_relamotifdeb.oid = relationcontrat.relatmotifdeb
left join prod_pleiades2.ta_relamotiffin on ta_relamotiffin.oid = relationcontrat.relatmotiffin
join prod_pleiades2.typejuridqctr on contrat.oid = typejuridqctr.typecontrat
join prod_pleiades2.ta_contrattype on ta_contrattype.oid = typejuridqctr.typejuridique
left join prod_pleiades2.xrccomplement on xrccomplement.relationcontrat = relationcontrat.oid
left join prod_pleiades2.xunitefct on xunitefct.oid = xrccomplement.uflocalisation
window w as (partition by relationcontrat.relatmatricule order by typejuridqctr.begin_date))
,tata as (
select
*,
date_fin - date_debut + 1 as duree,
coalesce((lag(date_fin) over w) + 1 = date_debut, false) as continue,
coalesce((lead(date_debut) over w) - 1 = date_fin, false) as continue2
from toto
window w as (partition by mat order by date_debut))
,titi as (
select
*,
case when lag(continue2) over w then false else continue2 end as new_cluster,
case when continue then lag(duree) over w else 0 end as prec_duree
from tata
window w as (partition by mat order by mat, date_debut))
,tutu as (
select * ,
case when new_cluster then count(new_cluster) over w else null end as cluster_id
from titi
window w as (partition by mat order by date_debut))
,tyty as (
select
*,
max(cluster_id) over wb as cl2
from tutu
window wb as (PARTITION BY mat ORDER BY date_debut rows unbounded preceding))
select
mat,
cnt,
relatnum,
date_debut,
date_fin,
motif_debut,
motif_fin,
relationcodefin,
type_contrat,
etablissement,
uf_localisation,
mat_remp,
case when continue then sum(prec_duree) over w else 0 end as anciennete_anterieure_jours,
case when continue then sum(prec_duree) over w else 0 end / 30.0 as anciennete_anterieure_calculee_mois
from tyty
window w as (partition by mat, cl2 order by date_debut)
;
CREATE INDEX i_cnt_0 ON w_cnt USING btree(mat)
;
CREATE INDEX i_cnt_1 ON w_cnt USING btree(cnt)
;
-- Création table de compte comptables CS.
drop table if exists w_cs_compte
;
create temp table w_cs_compte as
select
a01_compta.poste as poste,
matricule,
compte,
('20'||periode_imputation)::numeric as periode_imputation,
sous_periode,
periode_origine,
montant,
montant_total
from w_a01_compta as a01_compta
where 1=1
and ecriture = 'C'
and compte != '999999999'
group by 1,2,3,4,5,6,7,8
;
-- Création table de compte comptables e5.
drop table if exists w_compte
;
create temp table w_compte as
with compte_9955 as (
select
'9955'::text as poste,
poste_paie.code as rub,
to_char(begin_date, 'YYYYMM')::int as date_debut,
to_char(coalesce(nullif(end_date::date, '29990101'::date), '2099-12-31'::date), 'YYYYMM')::int as date_fin
from prod_pleiades2.assiettetdp
join prod_pleiades2.ass on ass.oid = assiettetdp.assiette
join prod_pleiades2.cumulassiette on cumulassiette.oid = assiettetdp.codecumul
join prod_pleiades2.poste_paie on poste_paie.oid = assiettetdp.postepaie
where true
and ass.code = '235')
,compte_9956 as (
select
'9956'::text as poste,
poste_paie.code as rub,
to_char(begin_date, 'YYYYMM')::int as date_debut,
to_char(coalesce(nullif(end_date::date, '29990101'::date), '2099-12-31'::date), 'YYYYMM')::int as date_fin
from prod_pleiades2.assiettetdp
join prod_pleiades2.ass on ass.oid = assiettetdp.assiette
join prod_pleiades2.cumulassiette on cumulassiette.oid = assiettetdp.codecumul
join prod_pleiades2.poste_paie on poste_paie.oid = assiettetdp.postepaie
where true
and ass.code = '236'
and cumulassiette.code != '0')
select
coalesce(compte_9955.rub, compte_9956.rub, a01_compta.poste) as poste,
matricule,
compte,
('20'||periode_imputation)::numeric as periode_imputation,
sous_periode,
periode_origine,
montant,
montant_total
from w_a01_compta as a01_compta
left join compte_9955 on 1=1
and compte_9955.poste = a01_compta.regroupement
and ('20'||periode_imputation)::numeric between compte_9955.date_debut and compte_9955.date_fin
left join compte_9956 on 1=1
and compte_9956.poste = a01_compta.poste
and ('20'||periode_imputation)::numeric between compte_9956.date_debut and compte_9956.date_fin
where 1=1
and ecriture = 'C'
and compte != '999999999'
group by 1,2,3,4,5,6,7,8
;
-- Création d'une table brut de paie
-- Suppression de l'ancienne vue w_hp du schéma public.
DROP VIEW IF EXISTS public.w_hp
;
DROP TABLE IF EXISTS w_hp
;
CREATE TEMP TABLE w_hp AS
SELECT
paie_poste.ctid as hp_ctid,
salarie.matricule as mat,
relationcontrat.relatnum,
paie_periode.imputation::numeric as mois,
paie_periode.datevirement as date_virement,
rpad(paie_periode.moispaie, 6)||rpad(paie_periode.traitement, 1)||rpad(paie_periode.sousperiode, 2) as sous_periode,
paie_periode.datedebut as date_debut,
paie_periode.datefin as date_fin,
rpad(poste_paie.code, 4) as rub,
paie_poste.nombre as base,
sum(paie_poste.nombre) over w as base_total,
sum(paie_poste.nombre) over wi as base_total_imputation,
paie_poste.taux as tx,
paie_poste.montant as mt,
sum(paie_poste.montant) over w as montant_total,
sum(paie_poste.montant) over wi as montant_total_imputation
FROM prod_pleiades2.paie_poste
join prod_pleiades2.paie_periode on paie_periode.oid = paie_poste.paieperiode
join prod_pleiades2.poste_paie on poste_paie.oid = paie_poste.postepaie
join prod_pleiades2.relationcontrat on relationcontrat.oid = paie_periode.relationcontrat
join prod_pleiades2.salarie on salarie.oid = relationcontrat.relatmatricule
where to_char(paie_periode.datevirement, 'YYYYMM') >= CONCAT(SPLIT_PART(rhp('rhprovider_start'), '-', 1), SPLIT_PART(rhp('rhprovider_start'), '-', 2))
window w as (partition by salarie.matricule, relationcontrat.relatnum, poste_paie.code, paie_periode.imputation, to_char(paie_periode.datedebut, 'YYYYMM')),
wi as (partition by salarie.matricule, relationcontrat.relatnum, poste_paie.code, paie_periode.imputation)
;
CREATE INDEX i_hp_0 ON w_hp USING btree(mat)
;
CREATE INDEX i_hp_1 ON w_hp USING btree(rub)
;
CREATE INDEX i_hp_3 ON w_hp USING btree(hp_ctid)
;
-- Créer une table des bulletins.
-- Les rubriques de régulation (en "R") seront affectées systématiquement au 1er bulletin du mois.
DROP TABLE IF EXISTS w_bul
;
CREATE TEMP TABLE w_bul AS
with bul_mois as (
select
mat,
mois,
case when mois = to_char(date_debut, 'YYYYMM')::numeric then date_debut else (mois||'01')::date end as date_debut,
case when mois = to_char(date_debut, 'YYYYMM')::numeric then date_fin else base.cti_last_day((mois||'01')::date) end as date_fin,
count(*) over (partition by mat, mois) as nb_bul_month,
count(case when mois = to_char(date_debut, 'YYYYMM')::numeric then date_debut else null end) as nb_lines_month
from w_hp
group by 1,2,3,4)
--
select
mat||mois||lpad(rank() over (partition by mat, mois order by date_debut), 2, '0') as bul,
*
from bul_mois
where case when nb_lines_month = 0 then nb_bul_month = 1 else true end -- on ne conserve que les bulletins mois qui n'ont aucune ligne sur le mois d'imputation (100% de régul).
;
]]>
''
group by 1,2,3,4
;
]]>
= rhp('rhprovider_start')::date
AND (1!=1
OR w_cnt.mat is not NULL
OR w_cnt_csp is not null
OR w_cnt_ufs is not null
OR w_cnt_qua is not null
OR w_cnt_ttt is not null
-- OR w_cnt_ttt is not null
OR w_bul.mat is not null)
group by 1,4,5,6,7,8,9,10,11,12,13,14,15,16--,17
;
]]>
comptes e5.
join w_compte on true
and w_compte.matricule = w_hp.mat
and w_compte.poste = w_hp.rub
and w_compte.periode_imputation = w_hp.mois
and w_compte.periode_origine||w_compte.sous_periode = substr(w_hp.sous_periode, 3, 4)||substr(w_hp.sous_periode, 8, 2)
join rh.t_compte as cpt_e5 on cpt_e5.code_original = w_compte.compte
group by 1)
UPDATE w_asso_comptes SET
compte_id = subq.compte_id
FROM subq
WHERE 1=1
AND w_asso_comptes.hp_ctid = subq.hp_ctid
AND w_asso_comptes.compte_id = 0
;
WITH subq AS (
select
w_hp.hp_ctid,
max(cpt_e5_bis.oid) as compte_id
from w_hp
-- Association rubriques <=> comptes e5.
join w_compte as w_compte2 on true
and w_compte2.matricule = w_hp.mat
and w_compte2.poste = w_hp.rub
and w_compte2.periode_imputation = w_hp.mois
and @w_compte2.montant_total = @w_hp.mt
join rh.t_compte as cpt_e5_bis on cpt_e5_bis.code_original = w_compte2.compte
group by 1)
UPDATE w_asso_comptes SET
compte_id = subq.compte_id
FROM subq
WHERE 1=1
AND w_asso_comptes.hp_ctid = subq.hp_ctid
AND w_asso_comptes.compte_id = 0
;
WITH subq AS (
select
w_hp.hp_ctid,
max(cpt_e5_ter.oid) as compte_id
from w_hp
-- Association rubriques <=> comptes e5.
join w_compte as w_compte3 on true
and w_compte3.matricule = w_hp.mat
and w_compte3.poste = w_hp.rub
and w_compte3.periode_imputation = w_hp.mois
and @w_compte3.montant = @w_hp.montant_total
join rh.t_compte as cpt_e5_ter on cpt_e5_ter.code_original = w_compte3.compte
group by 1)
UPDATE w_asso_comptes SET
compte_id = subq.compte_id
FROM subq
WHERE 1=1
AND w_asso_comptes.hp_ctid = subq.hp_ctid
AND w_asso_comptes.compte_id = 0
;
WITH subq AS (
select
w_hp.hp_ctid,
max(cpt_e5_qua.oid) as compte_id
from w_hp
-- Association rubriques <=> comptes e5.
join w_compte as w_compte4 on true
and w_compte4.matricule = w_hp.mat
and w_compte4.poste = w_hp.rub
and w_compte4.periode_imputation = w_hp.mois
and @w_compte4.montant_total = @w_hp.montant_total
join rh.t_compte as cpt_e5_qua on cpt_e5_qua.code_original = w_compte4.compte
group by 1)
UPDATE w_asso_comptes SET
compte_id = subq.compte_id
FROM subq
WHERE 1=1
AND w_asso_comptes.hp_ctid = subq.hp_ctid
AND w_asso_comptes.compte_id = 0
;
ALTER SEQUENCE rh.s_historique_paie RESTART WITH 1
;
DROP TABLE IF EXISTS w_historique_paie
;
CREATE TEMP TABLE w_historique_paie AS
select
w_hp.hp_ctid, -- permet d'être sûr qu'on n'agrège pas à tort des lignes de paie w_hp.
date_part('year', age(w_bul.date_fin, date_naissance)) AS age_id,
w_hp.mat||w_hp.mois||(case when to_char(w_hp.date_debut, 'YYYYMM')::numeric = w_hp.mois then substring(w_hp.sous_periode, 8, 2) else '01' end), -- numero fictif de bulletin. Rang 01 lorsque régulation.
w_hp.mat,
w_hp.relatnum,
w_hp.mois::numeric as mois_paie,
w_bul.date_debut,
w_bul.date_fin,
w_bul.date_fin as date_paie,
substring(w_hp.sous_periode, 1, 6)::numeric as mois_activite,
date_virement,
t_rubriques.oid AS rubrique_id,
p_salaries.oid AS salarie_id,
w_hp.sous_periode,
w_hp.date_debut as date_debut_ori,
w_hp.date_fin as date_fin_ori,
w_hp.rub,
w_bul.bul,
w_hp.mt * coalesce(w_rub_brut.signe, w_rub_cotpat.signe, w_rub_netpayer.signe) as ori_mt, -- orienté masse salariale à cause du signe.
w_hp.montant_total * coalesce(w_rub_brut.signe, w_rub_cotpat.signe, w_rub_netpayer.signe) as montant_total, -- orienté masse salariale à cause du signe.
w_hp.montant_total_imputation * coalesce(w_rub_brut.signe, w_rub_cotpat.signe, w_rub_netpayer.signe) as montant_total_imputation, -- orienté masse salariale à cause du signe.
case
when w_rub_h_2.rub is not null then
case w_rub_h_2.is_montant
when true then w_hp.mt * w_rub_h_2.signe
when false then (case when w_hp.mt = 0 then 0 else w_hp.base end)
end
when w_rub_heures_dads.rub is not null then w_hp.mt -- on injecte la rubrique de cumul des heures DADS dans l'indicateur iCTI "base".
else w_hp.base
end as base,
case
when w_rub_h_2.rub is not null then
case w_rub_h_2.is_montant
when true then w_hp.montant_total * w_rub_h_2.signe
when false then (case when w_hp.montant_total = 0 then 0 else w_hp.base_total end)
end
when w_rub_heures_dads.rub is not null then w_hp.montant_total -- on injecte la rubrique de cumul des heures DADS dans l'indicateur iCTI "base".
else w_hp.base_total
end as base_total,
case
when w_rub_h_2.rub is not null then
case w_rub_h_2.is_montant
when true then w_hp.montant_total_imputation * w_rub_h_2.signe
when false then (case when w_hp.montant_total_imputation = 0 then 0 else w_hp.base_total_imputation end)
end
when w_rub_heures_dads.rub is not null then w_hp.montant_total_imputation -- on injecte la rubrique de cumul des heures DADS dans l'indicateur iCTI "base".
else w_hp.base_total_imputation
end as base_total_imputation,
case when w_rub_brut.rub is not null then w_hp.mt * w_rub_brut.signe else 0 end as montant_brut,
case when w_rub_cotsal.rub is not null then w_hp.tx else 0 end as taux_cotisation_salarie,
case when w_rub_cotsal.rub is not null then w_hp.mt * w_rub_cotsal.signe else 0 end as montant_cotisation_salarie,
case when w_rub_cotpat.rub is not null then w_hp.tx else 0 end as taux_cotisation_patronale,
case when w_rub_cotpat.rub is not null then w_hp.mt * w_rub_cotpat.signe else 0 end as montant_cotisation_patronale,
0 as montant_avantage_nature,
0 as montant_frais_imposables,
case when w_rub_netpayer.rub is not null then w_hp.mt * w_rub_netpayer.signe else 0 end as montant_net_a_payer_salarie,
case when w_rub_netimpos.rub is not null then w_hp.mt * w_rub_netimpos.signe else 0 end as montant_net_imposable_salarie,
0 as montant_od_net_salarie,
case when w_rub_odnet_pat.rub is not null then w_hp.mt * w_rub_netpayer.signe else 0 end as montant_od_net_patronale, -- on prend le signe du net à payer.
0 as organisme_cotisation_id,
w_asso_comptes.compte_id,
(max(array[extract(epoch from w_profils.date_debut), p_contrats.oid]))[2] AS contrat_id,
(max(array[extract(epoch from w_profils.date_debut), p_contrats_mois.oid]))[2] AS contrat_mois_id,
(max(array[extract(epoch from w_profils.date_debut), w_profils.profil_id]))[2] AS profil_id,
nextval('rh.s_historique_paie'::regclass) as oid
from w_hp
JOIN w_bul on w_bul.bul = w_hp.mat||w_hp.mois||(case when to_char(w_hp.date_debut, 'YYYYMM')::numeric = w_hp.mois then substring(sous_periode, 8, 2) else '01' end)
JOIN rh.t_rubriques ON t_rubriques.code_original = w_hp.rub
JOIN rh.p_salaries ON p_salaries.matricule = w_bul.mat
JOIN w_profils ON 1=1
AND w_profils.bul = w_bul.bul
AND w_profils.bul_date_debut = w_bul.date_debut
left join w_rub_brut on true
and w_rub_brut.rub = w_hp.rub
and w_hp.date_fin between w_rub_brut.date_debut and w_rub_brut.date_fin
left join w_rub_cotsal on true
and w_rub_cotsal.rub = w_hp.rub
and w_hp.date_fin between w_rub_cotsal.date_debut and w_rub_cotsal.date_fin
left join w_rub_cotpat on true
and w_rub_cotpat.rub = w_hp.rub
and w_hp.date_fin between w_rub_cotpat.date_debut and w_rub_cotpat.date_fin
left join w_rub_netimpos on true
and w_rub_netimpos.rub = w_hp.rub
and w_hp.date_fin between w_rub_netimpos.date_debut and w_rub_netimpos.date_fin
left join w_rub_netpayer on true
and w_rub_netpayer.rub = w_hp.rub
and w_hp.date_fin between w_rub_netpayer.date_debut and w_rub_netpayer.date_fin
left join w_rub_h_2 on true
and w_rub_h_2.rub = w_hp.rub
--and w_hp.date_fin between w_rub_heures.date_debut and w_rub_heures.date_fin
left join w_rub_heures_dads on true
and w_rub_heures_dads.rub = w_hp.rub
left join w_asso_comptes on w_asso_comptes.hp_ctid = w_hp.hp_ctid
LEFT JOIN rh.p_contrats on p_contrats.numero_contrat = w_profils.cnt
LEFT JOIN rh.p_contrats_mois on 1=1
AND p_contrats_mois.contrat_id = p_contrats.oid
AND p_contrats_mois.mois_activite = w_bul.mois
left join w_rub_odnet_pat on w_rub_odnet_pat.rub = w_hp.rub
where true
group by 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,
w_rub_h_2.rub, w_rub_h_2.is_montant, w_rub_heures_dads.rub, w_rub_brut.rub, w_rub_cotsal.rub, w_rub_cotpat.rub, w_rub_netpayer.rub, w_rub_netimpos.rub, w_hp.mt, w_hp.base
HAVING 1!=1
OR case when w_rub_h_2.rub is not null then 1 else 0 end != 0
OR case when w_rub_heures_dads.rub is not null then 1 else 0 end != 0
OR case when w_rub_brut.rub is not null then w_hp.mt else 0 end != 0
OR case when w_rub_cotpat.rub is not null then w_hp.mt else 0 end != 0
OR case when w_rub_cotsal.rub is not null then w_hp.mt else 0 end != 0
OR case when w_rub_netpayer.rub is not null then w_hp.mt else 0 end != 0
OR case when w_rub_netimpos.rub is not null then w_hp.mt else 0 end != 0
;
-- Associer les bulletins hors contrat (is null) avec le contrat précédent.
UPDATE w_historique_paie
SET contrat_id = subq.contrat_id,
contrat_mois_id = subq.contrat_mois_id
FROM (
select
w_historique_paie.oid,
(max(array[extract(epoch from p_contrats_mois.date_fin), p_contrats_mois.contrat_id]))[2] as contrat_id,
(max(array[extract(epoch from p_contrats_mois.date_fin), p_contrats_mois.oid]))[2] as contrat_mois_id
from w_historique_paie
join rh.p_contrats_mois on true
and p_contrats_mois.salarie_id = w_historique_paie.salarie_id
and p_contrats_mois.date_fin < w_historique_paie.date_debut
where contrat_mois_id is null
group by 1) as subq
WHERE true
AND w_historique_paie.oid = subq.oid
AND w_historique_paie.contrat_mois_id is null
;
SELECT base.cti_stash_table_constraints('rh.p_historique_paie')
;
SELECT base.cti_stash_table_indexes('rh.p_historique_paie')
;
TRUNCATE rh.p_historique_paie
;
INSERT INTO rh.p_historique_paie (
oid,
age_id,
base,
contrat_id,
contrat_mois_id,
date_debut,
date_fin,
date_paie,
mois_activite,
mois_paie,
montant_avantage_nature,
montant_brut,
montant_cotisation_patronale,
montant_cotisation_salarie,
montant_frais_imposables,
montant_net_a_payer_salarie,
montant_net_imposable_salarie,
montant_od_net_salarie,
montant_od_net_patronale,
organisme_cotisation_id,
profil_id,
rubrique_id,
salarie_id,
taux_cotisation_patronale,
taux_cotisation_salarie,
compte_id)
SELECT
oid,
age_id,
base,
contrat_id,
contrat_mois_id,
date_debut,
date_fin,
date_paie,
mois_activite,
mois_paie,
montant_avantage_nature,
montant_brut,
montant_cotisation_patronale,
montant_cotisation_salarie,
montant_frais_imposables,
montant_net_a_payer_salarie,
montant_net_imposable_salarie,
montant_od_net_salarie,
montant_od_net_patronale,
organisme_cotisation_id,
profil_id,
rubrique_id,
salarie_id,
taux_cotisation_patronale,
taux_cotisation_salarie,
compte_id
FROM w_historique_paie
;
-- Inserer pour chaque bulletin une ligne qui va faire le compte avec le total
INSERT INTO rh.p_historique_paie (
age_id,
base,
contrat_id,
contrat_mois_id,
date_debut,
date_fin,
date_paie,
mois_activite,
mois_paie,
montant_avantage_nature,
montant_brut,
montant_cotisation_patronale,
montant_cotisation_salarie,
montant_frais_imposables,
montant_net_a_payer_salarie,
montant_net_imposable_salarie,
montant_od_net_salarie,
organisme_cotisation_id,
profil_id,
rubrique_id,
salarie_id,
taux_cotisation_patronale,
taux_cotisation_salarie,
compte_id,
code_original)
SELECT
age_id,
0,
contrat_id,
contrat_mois_id,
subq.date_debut,
subq.date_fin,
subq.date_paie,
subq.mois_activite,
subq.mois_paie,
0,
sum(chi.montant_brut - subq.montant_brut),
0, -- sum(chi.montant_cotisation_patronale - subq.montant_cotisation_patronale), désactivé pour l'instant.
sum(chi.montant_cotisation_salarie - subq.montant_cotisation_salarie),
0,
sum(chi.montant_net_a_payer_salarie - subq.montant_net_a_payer_salarie),
sum(chi.montant_net_imposable_salarie - subq.montant_net_imposable_salarie),
0,
subq.organisme_cotisation_id,
subq.profil_id,
(SELECT oid FROM rh.t_rubriques WHERE code = 'C000'),
subq.salarie_id,
0,
0,
0,
0 -- Le code original est utilisé pour identifier quelle vague d'UPDATE a servi à faire la mise à jour (cf. plus bas).
FROM (
SELECT
hp.age_id,
hp.contrat_id,
hp.contrat_mois_id,
hp.date_debut,
hp.date_fin,
hp.date_paie,
hp.mois_paie as mois_activite, -- il ne faut pas mettre le mois d'activite sinon on a plusieurs lignes et la C000 est mal calculée.
hp.mois_paie,
sum(montant_brut) as montant_brut,
sum(montant_cotisation_patronale) as montant_cotisation_patronale,
sum(montant_cotisation_salarie) as montant_cotisation_salarie,
sum(montant_net_a_payer_salarie) as montant_net_a_payer_salarie,
sum(montant_net_imposable_salarie) as montant_net_imposable_salarie,
hp.organisme_cotisation_id,
hp.profil_id,
(SELECT oid FROM rh.t_rubriques WHERE code = 'C000'),
hp.salarie_id,
hp.bul
FROM w_historique_paie AS hp
JOIN rh.p_salaries ON p_salaries.oid = hp.salarie_id
GROUP BY 1,2,3,4,5,6,7,8, 14,15,16,17,18) as subq
JOIN rh.p_salaries ON p_salaries.oid = subq.salarie_id
JOIN w_chiffrier AS chi ON 1=1
AND chi.mat = p_salaries.matricule
AND chi.bul = subq.bul
AND chi.date_debut = subq.date_debut
AND chi.date_fin = subq.date_fin
GROUP BY 1,2,3,4,5,6,7,8,9,10, 14, 17,18,19,20,21,22,23
HAVING 1!=1
OR sum(chi.montant_brut - subq.montant_brut) != 0
-- OR sum(chi.montant_cotisation_patronale - subq.montant_cotisation_patronale) != 0 -- désactivé pour l'instant.
OR sum(chi.montant_cotisation_salarie - subq.montant_cotisation_salarie) != 0
OR sum(chi.montant_net_a_payer_salarie - subq.montant_net_a_payer_salarie) != 0
OR sum(chi.montant_net_imposable_salarie - subq.montant_net_imposable_salarie) != 0
;
]]>
= CONCAT(SPLIT_PART(rhp('rhprovider_start'), '-', 1), SPLIT_PART(rhp('rhprovider_start'), '-', 2))::int
group by 1,2,3,4,5,6,7
having sum(montant) is not null),
ven_1 as (
select
imputation,
periode_imputation,
periode_origine,
matricule,
contrat,
poste,
compte,
montant,
case sum(montant) over w1
when 0 then 1.0 / count(*) over w1
else round(base.cti_division(montant, sum(montant) over w1), 6)
end as ratio,
row_number() over w2 as rang
from ven_0
window w1 as (partition by matricule, contrat, periode_imputation, periode_origine, poste, compte),
w2 as (partition by matricule, contrat, periode_imputation, periode_origine, poste, compte order by montant desc)),
ven_corr as (
select
matricule,
contrat,
periode_imputation,
periode_origine,
poste,
compte,
1.0 - sum(ratio) as corr
from ven_1
group by 1,2,3,4,5,6
having sum(ratio) != 1),
ven_tot as (
select
ven_1.periode_imputation,
ven_1.periode_origine,
ven_1.matricule,
ven_1.contrat,
ven_1.poste,
ven_1.compte,
sum(ven_1.montant) as montant,
array_agg(ven_1.imputation order by ven_1.imputation) as imp,
array_agg(case when corr is not null and ven_1.rang = 1 then ven_1.ratio + ven_corr.corr else ven_1.ratio end order by ven_1.imputation) as rat,
nextval('ventilation_id') as ventilation_id
from ven_1
left join ven_corr on 1=1
and ven_corr.matricule = ven_1.matricule
and ven_corr.contrat = ven_1.contrat
and ven_corr.periode_imputation = ven_1.periode_imputation
and ven_corr.periode_origine = ven_1.periode_origine
and ven_corr.poste = ven_1.poste
and ven_corr.compte = ven_1.compte
group by 1,2,3,4,5,6)
select
periode_imputation,
periode_origine,
matricule,
contrat,
poste,
compte,
sum(montant) over w as montant_tot, -- sans periode_origine.
0 = ANY(array_agg(montant) over w) as contains_zero,-- indicateur qui permet de traiter le cas des sous-périodes != imputation avec des montant_det à 0.
montant as montant_det,
imp,
rat,
ventilation_id
from ven_tot
window w as (partition by matricule, contrat, periode_imputation, poste, compte)
;
-- Création de la table des ventilations corrigées 2 (avec totalisation à date d'imputation).
DROP TABLE IF EXISTS w_ven_imp
;
CREATE TEMP TABLE w_ven_imp AS
with ven_0 as (
select
replace(imputation, 'JR00', 'JH00') as imputation,
('20'||periode_imputation)::int as periode_imputation,
matricule,
contrat,
poste,
compte,
sum(montant) as montant
from prod_pleiades2.a01_compta as a01_compta -- plus v_a01_compta car pas de notion de contrat avant 2017.
where 1=1
AND ecriture = 'C'
AND ('20'||periode_imputation)::int >= CONCAT(SPLIT_PART(rhp('rhprovider_start'), '-', 1), SPLIT_PART(rhp('rhprovider_start'), '-', 2))::int
group by 1,2,3,4,5,6
having sum(montant) is not null),
ven_1 as (
select
imputation,
periode_imputation,
matricule,
contrat,
poste,
compte,
montant,
round(base.cti_division(montant, sum(montant) over (partition by matricule, contrat, periode_imputation, poste, compte)), 6) as ratio,
row_number() over (partition by matricule, contrat, periode_imputation, poste, compte order by montant desc) as rang
from ven_0),
ven_corr as (
select
matricule,
contrat,
periode_imputation,
poste,
compte,
1.0 - sum(ratio) as corr
from ven_1
group by 1,2,3,4,5
having sum(ratio) != 1),
ven_tot as (
select
ven_1.periode_imputation,
ven_1.matricule,
ven_1.contrat,
ven_1.poste,
ven_1.compte,
sum(ven_1.montant) as montant,
array_agg(ven_1.imputation order by ven_1.imputation) as imp,
array_agg(case when corr is not null and ven_1.rang = 1 then ven_1.ratio + ven_corr.corr else ven_1.ratio end order by ven_1.imputation) as rat,
nextval('ventilation_id') as ventilation_id
from ven_1
left join ven_corr on 1=1
and ven_corr.matricule = ven_1.matricule
and ven_corr.contrat = ven_1.contrat
and ven_corr.periode_imputation = ven_1.periode_imputation
and ven_corr.poste = ven_1.poste
and ven_corr.compte = ven_1.compte
group by 1,2,3,4,5)
select
periode_imputation,
matricule,
contrat,
poste,
compte,
sum(montant) over w as montant_tot, -- sans periode_origine.
0 = ANY(array_agg(montant) over w) as contains_zero,-- indicateur qui permet de traiter le cas des sous-périodes != imputation avec des montant_det à 0.
montant as montant_det,
imp,
rat,
ventilation_id
from ven_tot
window w as (partition by matricule, contrat, periode_imputation, poste, compte)
;
-- Récupération des différents schémas/modèles de ventilation.
TRUNCATE rh.p_ventilation
;
INSERT INTO rh.p_ventilation(oid, code, texte, plan_analytique_id)
select
ventilation_id,
ventilation_id::text,
array_to_string(imp, ',')||' @ '||array_to_string(rat, ','),
1
FROM w_ven
group by 1,2,3,4
UNION ALL
select
ventilation_id,
ventilation_id::text,
array_to_string(imp, ',')||' @ '||array_to_string(rat, ','),
1
FROM w_ven_imp
group by 1,2,3,4
UNION ALL
SELECT
0,
'N/R',
'Non renseignée',
1
;
TRUNCATE rh.p_ventilation_section
;
INSERT INTO rh.p_ventilation_section(
ventilation_id,
section_analytique_id,
section_analytique_code,
section_analytique_texte,
ratio)
SELECT
ventilation_id,
t_section_analytique.oid,
t_section_analytique.code,
t_section_analytique.texte,
rat
FROM (
SELECT
ventilation_id,
unnest(imp) as imp,
unnest(rat) as rat
FROM w_ven
group by 1,2,3) as subq
JOIN rh.t_section_analytique on t_section_analytique.code_original = imp
UNION ALL
SELECT
ventilation_id,
t_section_analytique.oid,
t_section_analytique.code,
t_section_analytique.texte,
rat
FROM (
SELECT
ventilation_id,
unnest(imp) as imp,
unnest(rat) as rat
FROM w_ven_imp
group by 1,2,3) as subq
JOIN rh.t_section_analytique on t_section_analytique.code_original = imp
UNION ALL
SELECT
0,
oid,
code,
texte,
1
FROM rh.t_section_analytique
WHERE oid = 0
;
-- VENTILATION DE LA MASSE SALARIALE.
-- OK BRUT Ventilation précise...
WITH subq AS (
select
w_historique_paie.oid,
w_ven.ventilation_id,
case when compte = '999999999' then w_historique_paie.compte_id else t_compte.oid end as compte_id -- on ne change pas le compte lorsqu'il s'agit du 999999999.
from w_historique_paie
join w_ven on 1=1
and w_ven.matricule = w_historique_paie.mat
and w_ven.contrat = w_historique_paie.relatnum
and w_ven.poste = w_historique_paie.rub
and w_ven.periode_imputation = w_historique_paie.mois_paie
and w_ven.periode_origine = substr(w_historique_paie.sous_periode, 3, 4) -- ... On prend en compte la sous_periode (date d'activité).
and w_ven.montant_det = w_historique_paie.ori_mt-- Association obligatoire en détaillé.
-- and not w_ven.contains_zero -- Ne pas prendre les ventilations qui sont égales à 0 en total pour les sous-période != imputation.
join rh.t_compte on t_compte.code_original = w_ven.compte
where rub not in (select rub from w_exc_rub))
UPDATE rh.p_historique_paie SET
ventilation_1_id = subq.ventilation_id,
compte_id = subq.compte_id,
code_original = 1
FROM subq
WHERE true
and p_historique_paie.oid = subq.oid
AND p_historique_paie.ventilation_1_id = 0
;
-- OK @BRUT Ventilation précise mais en valeur absolue (= 1ère passe)...
WITH subq AS (
select
w_historique_paie.oid,
w_ven.ventilation_id,
case when compte = '999999999' then w_historique_paie.compte_id else t_compte.oid end as compte_id -- on ne change pas le compte lorsqu'il s'agit du 999999999.
from w_historique_paie
join w_ven on 1=1
and w_ven.matricule = w_historique_paie.mat
and w_ven.contrat = w_historique_paie.relatnum
and w_ven.poste = w_historique_paie.rub
and w_ven.periode_imputation = w_historique_paie.mois_paie
and w_ven.periode_origine = substr(w_historique_paie.sous_periode, 3, 4) -- ... On prend en compte la sous_periode (date d'activité).
and @w_ven.montant_det = @w_historique_paie.ori_mt-- Association obligatoire en détaillé.
join rh.t_compte on t_compte.code_original = w_ven.compte
where rub not in (select rub from w_exc_rub))
UPDATE rh.p_historique_paie SET
ventilation_1_id = subq.ventilation_id,
compte_id = subq.compte_id,
code_original = 2
FROM subq
WHERE true
and p_historique_paie.oid = subq.oid
AND p_historique_paie.ventilation_1_id = 0
;
-- OK BRUT HP TOT Ventilation avec montant totalisé en paie en prenant en compte la sous-période...
WITH subq AS (
select
w_historique_paie.oid,
w_ven.ventilation_id,
case when compte = '999999999' then w_historique_paie.compte_id else t_compte.oid end as compte_id -- on ne change pas le compte lorsqu'il s'agit du 999999999.
from w_historique_paie
join w_ven on 1=1
and w_ven.matricule = w_historique_paie.mat
and w_ven.contrat = w_historique_paie.relatnum
and w_ven.poste = w_historique_paie.rub
and w_ven.periode_imputation = w_historique_paie.mois_paie
and w_ven.periode_origine = substr(w_historique_paie.sous_periode, 3, 4) -- ... On prend en compte la sous_periode (date d'activité).
and w_ven.montant_det = w_historique_paie.montant_total -- Association obligatoire en détaillé imputation mais totalisé.
-- and not w_ven.contains_zero -- Ne pas prendre les ventilations qui sont égales à 0 en total pour les sous-période != imputation.
join rh.t_compte on t_compte.code_original = w_ven.compte
where rub not in (select rub from w_exc_rub))
UPDATE rh.p_historique_paie SET
ventilation_1_id = subq.ventilation_id,
compte_id = subq.compte_id,
code_original = 3
FROM subq
WHERE true
and p_historique_paie.oid = subq.oid
AND p_historique_paie.ventilation_1_id = 0
;
-- OK BRUT HP TOT Ventilation avec montant totalisé @bsolue en paie en prenant en compte la sous-période...
WITH subq AS (
select
w_historique_paie.oid,
w_ven.ventilation_id,
case when compte = '999999999' then w_historique_paie.compte_id else t_compte.oid end as compte_id -- on ne change pas le compte lorsqu'il s'agit du 999999999.
from w_historique_paie
join w_ven on 1=1
and w_ven.matricule = w_historique_paie.mat
and w_ven.contrat = w_historique_paie.relatnum
and w_ven.poste = w_historique_paie.rub
and w_ven.periode_imputation = w_historique_paie.mois_paie
and w_ven.periode_origine = substr(w_historique_paie.sous_periode, 3, 4) -- ... On prend en compte la sous_periode (date d'activité).
and @w_ven.montant_det = @w_historique_paie.montant_total -- Association obligatoire en détaillé imputation mais totalisé.
-- and not w_ven.contains_zero -- Ne pas prendre les ventilations qui sont égales à 0 en total pour les sous-période != imputation.
join rh.t_compte on t_compte.code_original = w_ven.compte
where rub not in (select rub from w_exc_rub))
UPDATE rh.p_historique_paie SET
ventilation_1_id = subq.ventilation_id,
compte_id = subq.compte_id,
code_original = '3.1'
FROM subq
WHERE true
and p_historique_paie.oid = subq.oid
AND p_historique_paie.ventilation_1_id = 0
;
-- OK BRUT Ventilation lorsque pas les mêmes montant sous-période entre paie et a01_compta (totaux égaux)...
WITH subq AS (
select
w_historique_paie.oid,
w_ven.ventilation_id,
montant_total != montant_total_imputation as contains_retro, -- permet de cibler uniquement les rubriques avec de la rétro.
case when compte = '999999999' then w_historique_paie.compte_id else t_compte.oid end as compte_id -- on ne change pas le compte lorsqu'il s'agit du 999999999.
from w_historique_paie
join w_ven on 1=1
and w_ven.matricule = w_historique_paie.mat
and w_ven.contrat = w_historique_paie.relatnum
and w_ven.poste = w_historique_paie.rub
and w_ven.periode_imputation = w_historique_paie.mois_paie
and w_ven.periode_origine = substr(w_historique_paie.sous_periode, 3, 4) -- ... On prend en compte la sous_periode (date d'activité).
--and w_ven.montant_det = w_historique_paie.ori_mt-- non.
-- and not w_ven.contains_zero -- Ne pas prendre les ventilations qui sont égales à 0 en total pour les sous-période != imputation.
join rh.t_compte on t_compte.code_original = w_ven.compte
where rub not in (select rub from w_exc_rub))
UPDATE rh.p_historique_paie SET
ventilation_1_id = subq.ventilation_id,
compte_id = subq.compte_id,
code_original = 4
FROM subq
WHERE true
and p_historique_paie.oid = subq.oid
and subq.contains_retro
AND p_historique_paie.ventilation_1_id = 0
;
-- COT ventilation sans sous-période avec montant (totalisé à l'imputation)...
WITH subq AS (
select
w_historique_paie.oid,
w_ven_imp.ventilation_id,
case when compte = '999999999' then w_historique_paie.compte_id else t_compte.oid end as compte_id -- on ne change pas le compte lorsqu'il s'agit du 999999999.
from w_historique_paie
join w_ven_imp on 1=1
and w_ven_imp.matricule = w_historique_paie.mat
and w_ven_imp.contrat = w_historique_paie.relatnum
and w_ven_imp.poste = w_historique_paie.rub
and w_ven_imp.periode_imputation = w_historique_paie.mois_paie
-- and w_ven_imp.periode_origine = substr(w_historique_paie.sous_periode, 3, 4) -- ... On prend en compte la sous_periode (date d'activité).
and w_ven_imp.montant_tot = w_historique_paie.montant_total_imputation -- Association obligatoire en total imputation.
join rh.t_compte on t_compte.code_original = w_ven_imp.compte
where rub not in (select rub from w_exc_rub))
UPDATE rh.p_historique_paie SET
ventilation_1_id = subq.ventilation_id,
compte_id = subq.compte_id,
code_original = 5
FROM subq
WHERE true
and p_historique_paie.oid = subq.oid
AND p_historique_paie.ventilation_1_id = 0
;
-- OK AST Ventilation des astreintes
WITH subq AS (
select
w_historique_paie.oid,
w_ven.ventilation_id,
t_compte.oid as compte_id
from w_historique_paie
join (select to_id from rh.t_listes join rh.t_listes_contenu on t_listes_contenu.liste_id = t_listes.oid where code = 'RUB_AST_9956') as subq ON subq.to_id = w_historique_paie.rubrique_id
join w_ven on 1=1
and w_ven.matricule = w_historique_paie.mat
and w_ven.contrat = w_historique_paie.relatnum
and w_ven.periode_imputation = w_historique_paie.mois_paie
and w_ven.poste = '9956'
join rh.t_compte on t_compte.code_original = w_ven.compte
where rub not in (select rub from w_exc_rub))
UPDATE rh.p_historique_paie SET
ventilation_1_id = subq.ventilation_id,
compte_id = subq.compte_id,
code_original = 6
FROM subq
WHERE true
and p_historique_paie.oid = subq.oid
AND p_historique_paie.ventilation_1_id = 0
;
-- VENTILATION DES ETPS.
-- OK Ventilation précise...
WITH subq AS (
select
w_historique_paie.oid,
w_ven.ventilation_id,
case when compte = '999999999' then w_historique_paie.compte_id else t_compte.oid end as compte_id -- on ne change pas le compte lorsqu'il s'agit du 999999999.
from w_historique_paie
join w_ven on 1=1
and w_ven.matricule = w_historique_paie.mat
and w_ven.contrat = w_historique_paie.relatnum
and w_ven.poste = w_historique_paie.rub
and w_ven.periode_imputation = w_historique_paie.mois_paie
and w_ven.periode_origine = substr(w_historique_paie.sous_periode, 3, 4) -- ... On prend en compte la sous_periode (date d'activité).
and w_ven.montant_det = w_historique_paie.base -- Association obligatoire en détaillé.
-- and not w_ven.contains_zero -- Ne pas prendre les ventilations qui sont égales à 0 en total pour les sous-période != imputation.
join rh.t_compte on t_compte.code_original = w_ven.compte
where rub not in (select rub from w_exc_rub))
UPDATE rh.p_historique_paie SET
ventilation_1_id = subq.ventilation_id,
compte_id = subq.compte_id,
code_original = 7
FROM subq
WHERE true
and p_historique_paie.oid = subq.oid
AND p_historique_paie.ventilation_1_id = 0
;
-- ETP @BSOLUE.
WITH subq AS (
select
w_historique_paie.oid,
w_ven.ventilation_id,
case when compte = '999999999' then w_historique_paie.compte_id else t_compte.oid end as compte_id -- on ne change pas le compte lorsqu'il s'agit du 999999999.
from w_historique_paie
join w_ven on 1=1
and w_ven.matricule = w_historique_paie.mat
and w_ven.contrat = w_historique_paie.relatnum
and w_ven.poste = w_historique_paie.rub
and w_ven.periode_imputation = w_historique_paie.mois_paie
and w_ven.periode_origine = substr(w_historique_paie.sous_periode, 3, 4) -- ... On prend en compte la sous_periode (date d'activité).
and @w_ven.montant_det = @w_historique_paie.base -- Association obligatoire en détaillé.
-- and not w_ven.contains_zero -- Ne pas prendre les ventilations qui sont égales à 0 en total pour les sous-période != imputation.
join rh.t_compte on t_compte.code_original = w_ven.compte
where rub not in (select rub from w_exc_rub))
UPDATE rh.p_historique_paie SET
ventilation_1_id = subq.ventilation_id,
compte_id = subq.compte_id,
code_original = 8
FROM subq
WHERE true
and p_historique_paie.oid = subq.oid
AND p_historique_paie.ventilation_1_id = 0
;
-- OK Ventilation avec base totalisé en paie en prenant en compte la sous-période...
WITH subq AS (
select
w_historique_paie.oid,
w_ven.ventilation_id,
case when compte = '999999999' then w_historique_paie.compte_id else t_compte.oid end as compte_id -- on ne change pas le compte lorsqu'il s'agit du 999999999.
from w_historique_paie
join w_ven on 1=1
and w_ven.matricule = w_historique_paie.mat
and w_ven.contrat = w_historique_paie.relatnum
and w_ven.poste = w_historique_paie.rub
and w_ven.periode_imputation = w_historique_paie.mois_paie
and w_ven.periode_origine = substr(w_historique_paie.sous_periode, 3, 4) -- ... On prend en compte la sous_periode (date d'activité).
and w_ven.montant_det = w_historique_paie.base_total -- Association obligatoire en détaillé imputation mais totalisé.
-- and not w_ven.contains_zero -- NON car en total. Ne pas prendre les ventilations qui sont égales à 0 en total pour les sous-période != imputation.
join rh.t_compte on t_compte.code_original = w_ven.compte
where rub not in (select rub from w_exc_rub))
UPDATE rh.p_historique_paie SET
ventilation_1_id = subq.ventilation_id,
compte_id = subq.compte_id,
code_original = 9
FROM subq
WHERE true
and p_historique_paie.oid = subq.oid
AND p_historique_paie.ventilation_1_id = 0
;
SELECT base.cti_stash_pop_table_constraints('rh.p_historique_paie')
;
SELECT base.cti_stash_pop_table_indexes('rh.p_historique_paie')
;
]]>
0 FROM rh.t_divers WHERE code = 'IMP_INC_MAR') then ta_motifevt.motif IN (SELECT trim(unnest(string_to_array(valeur, ','))) FROM rh.t_divers WHERE code = 'IMP_INC_MAR') else true end
and ta_motifevt.motif NOT IN (SELECT trim(unnest(string_to_array(valeur, ','))) FROM rh.t_divers WHERE code = 'IMP_EXC_MAR')
group by 1,2,3,4,5,6,7,8
;
DROP TABLE IF EXISTS w_arrets_mois -- version retravaillée (aggrégation des arrêts de même type continus sur un même mois).
;
CREATE TEMP TABLE w_arrets_mois AS
with clustering as (
select
*,
case when lag(date_fin) over (partition by mat, motif order by date_debut) = date_debut - '1 day'::interval then null else true end as new_cluster
from w_arrets_0
order by mat, motif, date_debut),
assigned_clustering as (
select
*,
mat||'-'||count(new_cluster) over (order by mat, motif, date_debut) as cluster_id
from clustering
)
select
mat as matricule,
motif,
cluster_id,
array_agg(uid) as uid, -- uid d'origine potentiellement dédoublé (inutilisable).
mat||'-'||row_number() over (order by mois) as uid2, -- uid ventilé.
min(date_debut) as date_debut,
max(date_fin) as date_fin,
mois as mois,
sum(nb_jours) as nb_jours,
sum(nb_heures) as nb_heures
from assigned_clustering
group by mat, motif, cluster_id, mois
;
DROP TABLE IF EXISTS w_arrets_mois_1 -- ventilation retravaillée + id de contrats.
;
CREATE TEMP TABLE w_arrets_mois_1 AS
select
w_arrets_mois.*,
p_salaries.oid as salarie_id,
p_contrats_mois.contrat_id,
p_contrats_mois.oid as contrat_mois_id
from w_arrets_mois
join rh.p_salaries on p_salaries.matricule = w_arrets_mois.matricule
join rh.p_contrats_mois on 1=1
and p_contrats_mois.salarie_id = p_salaries.oid
and base.cti_overlaps(p_contrats_mois.date_debut, p_contrats_mois.date_fin, w_arrets_mois.date_debut, w_arrets_mois.date_fin)
;
-- Agrégation des arrêts de travail.
DROP TABLE IF EXISTS w_arrets
;
CREATE TEMP TABLE w_arrets AS
select
matricule,
motif,
cluster_id,
matricule||'-'||motif||'-'||cluster_id as uid3, -- un uid3 par arrêt
--array_agg(uid) as uid,
array_agg(uid2) as uid2, -- plusieurs uid2 par uid3
min(date_debut) as date_debut,
max(date_fin) as date_fin,
max(mois) as mois,
sum(nb_jours) as nb_jours,
sum(nb_heures) as nb_heures
from w_arrets_mois
group by matricule, motif, cluster_id
;
-- Alimentation des arrêts de travail
TRUNCATE rh.p_arrets_travail
;
ALTER SEQUENCE rh.s_arrets_travail RESTART WITH 1
;
DROP TABLE IF EXISTS w_arrets_travail
;
CREATE TEMP TABLE w_arrets_travail AS
SELECT p_salaries.oid as salarie_id,
(max(ARRAY[extract(EPOCH from p_contrats.date_fin), p_contrats.oid]))[2] as contrat_id,
w_arrets.mois as mois_activite,
w_arrets.date_debut,
w_arrets.date_fin,
CASE WHEN w_arrets.date_fin = '2099-12-31'::date THEN '2099-12-31'::date ELSE (w_arrets.date_fin + INTERVAL '1 day')::date END AS date_reprise,
coalesce(t_motifs_arret.oid, 0) AS motif_arret_id,
0 AS precision_motif_arret_id,
w_arrets.nb_jours,
w_arrets.nb_heures,
w_arrets.cluster_id,
w_arrets.uid2,
w_arrets.uid3,
nextval('rh.s_arrets_travail'::regclass) as oid
FROM w_arrets
JOIN rh.p_salaries ON p_salaries.matricule = w_arrets.matricule
LEFT JOIN rh.t_motifs_arret ON t_motifs_arret.code_original = w_arrets.motif
JOIN rh.p_contrats ON 1=1 -- Ne pas s'inquièter si cetains arrets ne sont pas remontés faute d'association avec un contrat car il doit OBLIGRATOIREMENT avoir un contrat associé dans PLEIADES.
and p_contrats.salarie_id = p_salaries.oid
and base.cti_overlaps(p_contrats.date_debut, p_contrats.date_fin, w_arrets.date_debut, w_arrets.date_fin)
GROUP BY 1,3,4,5,6,7,8,9,10,11,12,13
;
INSERT INTO rh.p_arrets_travail(
oid,
salarie_id,
contrat_id,
mois_activite,
date_debut,
date_fin,
date_reprise,
motif_arret_id,
precision_motif_arret_id,
nb_jours,
nb_heures
)
select
oid,
salarie_id,
contrat_id,
mois_activite,
date_debut,
date_fin,
date_reprise,
motif_arret_id,
precision_motif_arret_id,
nb_jours,
nb_heures
from w_arrets_travail
;
-- Ventilation mensuelle des arrêts de travail
TRUNCATE rh.p_arrets_travail_mois
;
INSERT INTO rh.p_arrets_travail_mois(
arret_travail_id,
salarie_id,
contrat_id,
contrat_mois_id,
mois_activite,
nb_debut_arret,
nb_fin_arret,
nb_reprise_apres_arret,
date_debut,
date_fin,
nb_jours,
nb_heures,
nb_arret
)
SELECT
subq.oid AS arret_travail_id,
w_arrets_mois_1.salarie_id AS salarie_id,
w_arrets_mois_1.contrat_id AS contrat_id,
w_arrets_mois_1.contrat_mois_id AS contrat_mois_id,
p_contrats_mois.mois_activite,
CASE WHEN w_arrets_mois_1.date_debut BETWEEN min(p_contrats_mois.date_debut) AND max(p_contrats_mois.date_fin) THEN 1 ELSE 0 END AS nombre_debut_arret,
CASE WHEN w_arrets_mois_1.date_fin BETWEEN min(p_contrats_mois.date_debut) AND max(p_contrats_mois.date_fin) THEN 1 ELSE 0 END AS nombre_fin_arret,
0 AS nb_reprise_apres_arret,
w_arrets_mois_1.date_debut,
w_arrets_mois_1.date_fin,
nb_jours,
nb_heures,
1
FROM (select
unnest(uid2) as uid2,
salarie_id,
contrat_id,
cluster_id,
oid
from w_arrets_travail) as subq
join w_arrets_mois_1 on w_arrets_mois_1.uid2 = subq.uid2
join rh.p_contrats_mois on p_contrats_mois.oid = w_arrets_mois_1.contrat_mois_id
group by 1,2,3,4,5,8,9,10,11,12,13,w_arrets_mois_1.date_debut,w_arrets_mois_1.date_fin
;
-- Calcul de l'indicateur 'nb reprise après arrêt'.
update rh.p_arrets_travail_mois
set nb_reprise_apres_arret = subq.nb_reprise_apres_arret
from (
select
t1.arret_travail_id,
t1.mois_activite,
min(case when p_arrets_travail.date_reprise between t2.date_debut and t2.date_fin then 0 else 1 end) as nb_reprise_apres_arret
from rh.p_arrets_travail_mois as t1
join rh.p_arrets_travail ON p_arrets_travail.oid = t1.arret_travail_id
left join rh.p_arrets_travail_mois as t2 ON 1=1
AND t1.salarie_id = t2.salarie_id
AND t1.date_debut != t2.date_debut
AND t1.date_fin != t2.date_fin
GROUP BY t1.arret_travail_id, t1.mois_activite
order by 2 desc
) as subq
where 1=1
and p_arrets_travail_mois.arret_travail_id = subq.arret_travail_id
and p_arrets_travail_mois.mois_activite = subq.mois_activite
;
]]>