'' OR w_HIST_RUBR_SALA_T.ID_SALA IS NOT NULL) and id_etab in (select id_etab from w_ets group by 1) and hist_salarie.id_sala not in (select id_sala from w_sal2excl) -- suppression des salariés de démo. group by 1,2,3,4,5,6) select id_etab, id_sala, matr, ncnt, ncnt_ori, date_debu_cont, coalesce(date_fin_cont, lead(date_debu_cont) over w - '1 day'::interval, '2099-12-31'::date)::date as date_fin_cont, sala_supp, case when date_anci > date_debu_cont then date_debu_cont else date_anci end as date_anci from cnt_agg window w as (partition by id_etab, id_sala order by date_debu_cont) ; DROP TABLE IF EXISTS w_cnt_bul ; CREATE TEMP TABLE w_cnt_bul AS with histo_rub as ( SELECT id_etab_rub, id_sala_rub, id_empl_rub, peri_rub FROM (select id_etab as id_etab_rub, id_sala as id_sala_rub, id_empl as id_empl_rub, peri as peri_rub, COUNT(*) OVER (PARTITION BY id_sala, id_etab, peri) AS nb_lignes from MATERIAL_HIST_RUBR_SALA_T HIST_RUBR_SALA_T group by 1,2,3,4 ) AS subview -- Permet d'éviter le problème de rubriques en doubles dans certains cas WHERE subview.nb_lignes = 1 OR (subview.nb_lignes > 1 AND id_empl_rub IS NOT NULL) ) , w_emplois as ( select trim(id_empl) as id_empl, trim(libe) as libe, trim(code_inse) as code_inse from prod_nibelis.emplois where id_soci::bigint = (select id_soci from w_ets limit 1) group by 1,2,3) , w_coeff as ( select id_sala as coeff_id_sala, peri as coeff_peri, coalesce(vale, '0') as coeff from MATERIAL_HIST_CONS_SALA hist_cons_sala where code_cons = 'COEFFIC' group by 1,2,3) select hist_salarie.id_etab::bigint, hist_salarie.id_sala::bigint, trim(hist_salarie.matr) as matr, trim(hist_salarie.matr)||'-'::text||to_char(to_date(hist_salarie.date_debu_cont,'DD/MM/YYYY'), 'YYYYMMDD') as ncnt, trim(hist_salarie.id_etab)||'-'::text||trim(hist_salarie.id_sala)||'-'::text||trim(hist_salarie.matr)||'-'::text||to_char(to_date(hist_salarie.date_debu_cont,'DD/MM/YYYY'), 'YYYYMMDD') as ncnt_ori, w_cnt.date_debu_cont, w_cnt.date_fin_cont, w_cnt.date_anci, hist_salarie.peri::date, to_char(hist_salarie.peri::date, 'YYYYMM')::int as mois_paie, trim(hist_salarie.nume_secu_soci) as nume_secu_soci, trim(hist_salarie.cle_secu_soci) as cle_secu_soci, trim(hist_salarie.titr) as titr, trim(hist_salarie.nom) as nom, trim(hist_salarie.nom_jeun_fill) as nom_jeun_fill, trim(hist_salarie.pren) as pren, trim(hist_salarie.code_post) as code_post, trim(hist_salarie.nati) as nati, hist_salarie.date_nais::date, trim(hist_salarie.situ_fami) as situ_fami, hist_salarie.date_emba::date, trim(hist_salarie.code_moti_recr_cdd) as code_moti_recr_cdd, coalesce(hist_salarie.date_depa::date, '2099-12-31'::date) as date_depa, trim(hist_salarie.moti_depa) as moti_depa, w_emplois.id_empl as id_empl, w_emplois.code_inse as code_inse, trim(hist_salarie.cate_prof) as cate_prof, trim(hist_salarie.natu_cont) as natu_cont, trim(hist_salarie.serv) as serv, trim(hist_salarie.equi) as equi, trim(hist_salarie.regr) as regr, trim(hist_salarie.cate_conv) as cate_conv, trim(hist_salarie.fili_conv) as fili_conv, REPLACE(hist_salarie.hora_mens_fich, ',', '.')::numeric AS hora_mens_fich, w_coeff.coeff, case when replace(hist_salarie.hora_mens_fich, ',', '.')::numeric = 0 then 1.0 else replace(hist_salarie.hora_mens_fich, ',', '.')::numeric / 151.67 end as ratio_theo, '':: text as ttt, '':: text as ttt_libelle, '':: text as ttt_libelle_court, array_remove(ARRAY[ CASE WHEN (hist_salarie.code_anal IS NOT NULL AND hist_salarie.code_anal != '') THEN hist_salarie.code_anal ELSE 'NO_CODE_ANAL' END, CASE WHEN (hist_salarie.code_anal_axe2 IS NOT NULL AND hist_salarie.code_anal_axe2 != '') THEN hist_salarie.code_anal_axe2 ELSE 'NO_CODE_ANAL' END, CASE WHEN (hist_salarie.code_anal_axe3 IS NOT NULL AND hist_salarie.code_anal_axe3 != '') THEN hist_salarie.code_anal_axe3 ELSE 'NO_CODE_ANAL' END, CASE WHEN (hist_salarie.code_anal_axe4 IS NOT NULL AND hist_salarie.code_anal_axe4 != '') THEN hist_salarie.code_anal_axe4 ELSE 'NO_CODE_ANAL' END, CASE WHEN (hist_salarie.code_anal IS NULL OR hist_salarie.code_anal = '') AND (hist_salarie.code_anal_axe2 IS NULL OR hist_salarie.code_anal = '') AND (hist_salarie.code_anal_axe3 IS NULL OR hist_salarie.code_anal = '') AND (hist_salarie.code_anal_axe4 IS NULL OR hist_salarie.code_anal = '') THEN chr(1)||'*' ELSE 'NO_CODE_ANAL' END ], 'NO_CODE_ANAL') AS codes_anal, array_remove(ARRAY[ CASE WHEN (hist_salarie.code_anal IS NOT NULL AND hist_salarie.code_anal != '') THEN REPLACE(hist_salarie.pour_affe_anal, ',', '.') ELSE 'NO_CODE_ANAL' END, CASE WHEN (hist_salarie.code_anal_axe2 IS NOT NULL AND hist_salarie.code_anal_axe2 != '') THEN REPLACE(hist_salarie.pour_affe_anal_axe2, ',', '.') ELSE 'NO_CODE_ANAL' END, CASE WHEN (hist_salarie.code_anal_axe3 IS NOT NULL AND hist_salarie.code_anal_axe3 != '') THEN REPLACE(hist_salarie.pour_affe_anal_axe3, ',', '.') ELSE 'NO_CODE_ANAL' END, CASE WHEN (hist_salarie.code_anal_axe4 IS NOT NULL AND hist_salarie.code_anal_axe4 != '') THEN REPLACE(hist_salarie.pour_affe_anal_axe4, ',', '.') ELSE 'NO_CODE_ANAL' END, CASE WHEN (hist_salarie.code_anal IS NULL OR hist_salarie.code_anal = '') AND (hist_salarie.code_anal_axe2 IS NULL OR hist_salarie.code_anal = '') AND (hist_salarie.code_anal_axe3 IS NULL OR hist_salarie.code_anal = '') AND (hist_salarie.code_anal_axe4 IS NULL OR hist_salarie.code_anal = '') THEN '100' ELSE 'NO_CODE_ANAL' END ], 'NO_CODE_ANAL') AS codes_anal_ratio from w_HIST_SALARIE AS HIST_SALARIE join w_cnt on w_cnt.ncnt_ori = trim(hist_salarie.id_etab)||'-'::text||trim(hist_salarie.id_sala)||'-'::text||trim(hist_salarie.matr)||'-'::text||to_char(to_date(hist_salarie.date_debu_cont,'DD/MM/YYYY'), 'YYYYMMDD') left join histo_rub on true and histo_rub.id_etab_rub = hist_salarie.id_etab and histo_rub.id_sala_rub = hist_salarie.id_sala and histo_rub.peri_rub = hist_salarie.peri left join w_emplois on w_emplois.id_empl = histo_rub.id_empl_rub left join w_coeff on true and w_coeff.coeff_id_sala = hist_salarie.id_sala and w_coeff.coeff_peri = hist_salarie.peri where true and hist_salarie.id_etab in (select id_etab from w_ets group by 1) and hist_salarie.id_sala not in (select id_sala from w_sal2excl) -- suppression des salariés de démo. ; update w_cnt_bul set ttt = case when ratio_theo = 1 then 'TC100' else 'TP'||lpad((ratio_theo * 100)::int::text, 3, '0') end, ttt_libelle = case when ratio_theo = 1 then 'Temps Complet' else 'Temps Partiel '||lpad((ratio_theo * 100)::int::text, 3, '0')|| ' %' end, ttt_libelle_court = case when ratio_theo = 1 then 'TC' else 'TP '||lpad((ratio_theo * 100)::int::text, 3, '0')|| ' %' end ; DROP TABLE IF EXISTS w_hp_0 ; CREATE TEMP TABLE w_hp_0 AS select id_etab::bigint, id_sala::bigint, trim(HIST_RUBR_SALA_T.id_rubr) as id_rubr, peri::date, to_char(peri::date, 'YYYYMM')::int as mois_paie, -- cate_prof, -- serv, id_modbull::bigint, id_cais::bigint, neut_code, round(replace(nomb, ',', '.')::numeric, 2) as nombre, round(replace(base, ',', '.')::numeric, 2) as base, replace(taux_sala, ',', '.')::numeric as tx_sal, round(replace(mont_sala, ',', '.')::numeric, 2) as mt_sal, replace(taux_patr, ',', '.')::numeric as tx_pat, round(replace(mont_patr, ',', '.')::numeric, 2) as mt_pat from MATERIAL_HIST_RUBR_SALA_T HIST_RUBR_SALA_T left join rh.t_rubriques on t_rubriques.code_original = HIST_RUBR_SALA_T.id_rubr where true and id_etab in (select id_etab from w_ets group by 1) and id_sala not in (select id_sala from w_sal2excl) -- suppression des salariés de démo. and (false or t_rubriques.p_cumul -- il faut remonter toutes les rubriques de totalisation, même celles à 0. or replace(nomb, ',', '.')::numeric != 0 or replace(base, ',', '.')::numeric != 0 or replace(mont_sala, ',', '.')::numeric != 0 or replace(mont_patr, ',', '.')::numeric != 0) ; with ets_sal as ( select id_etab, id_sala as ets_id_sala, peri as ets_peri from w_hp_0 group by 1,2,3) INSERT INTO w_hp_0 select id_etab::bigint, id_sala::bigint, code_cons as id_rubr, peri::date, to_char(peri::date, 'YYYYMM')::int as mois_paie, -- cate_prof, -- serv, 0, 0, '', round(replace(vale, ',', '.')::numeric, 2) as nombre, round(replace(vale, ',', '.')::numeric, 2) as base, 0 as tx_sal, 0 as mt_sal, 0 as tx_pat, 0 as mt_pat from MATERIAL_HIST_CONS_SALA hist_cons_sala join ets_sal on true and ets_sal.ets_id_sala = hist_cons_sala.id_sala::bigint and ets_sal.ets_peri = hist_cons_sala.peri::date where code_cons in ('PRESENCE', 'TOTALH', 'TOTALHTRAV') ; ]]> '1' ; -- Màj des Entreprises. INSERT INTO rh.t_entreprises(code_original, code, texte, texte_court) select id_soci, MAX(substr(sire, 1, 9)), MAX(nom), MAX(substr(nom, 1, 50)) from prod_nibelis.etablissement_t where 1=1 AND id_soci NOT IN (SELECT code_original FROM rh.t_entreprises WHERE code_original IS NOT NULL) AND id_etab in (select id_etab from w_ets group by 1) GROUP BY 1 ; -- Màj des Etablissements. INSERT INTO rh.t_etablissements(code_original, code, texte, texte_court, entreprise_id) SELECT id_etab, sire, nom, substr(nom, 1, 50), t_entreprises.oid from prod_nibelis.etablissement_t JOIN rh.t_entreprises ON t_entreprises.code_original = etablissement_t.id_soci where 1=1 AND id_etab NOT IN (SELECT code_original FROM rh.t_etablissements WHERE code_original IS NOT NULL) and id_etab in (select id_etab from w_ets group by 1) ; -- Màj des catégories socio-professionnelles INSERT INTO rh.t_categories_socio_professionnelle (code_original, code, texte, texte_court) SELECT lower(code_inse), lower(code_inse), lower(code_inse), substr(lower(code_inse), 1, 50) FROM prod_nibelis.emplois WHERE true and lower(code_inse) NOT IN (SELECT code_original FROM rh.t_categories_socio_professionnelle WHERE code_original IS NOT NULL) and id_soci::bigint = (select id_soci from w_ets limit 1) GROUP BY 1,2,3,4 ; -- Màj des sections analytiques comptables. INSERT INTO rh.t_sections_analytiques(code_original, code, texte, texte_court) SELECT code_anal, code_anal, libe_code_anal, substr(libe_code_anal, 1, 50) FROM prod_nibelis.code_analytique WHERE code_anal NOT IN (SELECT code_original FROM rh.t_sections_analytiques WHERE code_original IS NOT NULL) ; -- Màj des sections analytiques paie. INSERT INTO rh.t_sections_analytiques_paie(code_original, code, texte, texte_court) SELECT code_anal, code_anal, libe_code_anal, substr(libe_code_anal, 1, 50) FROM prod_nibelis.code_analytique WHERE code_anal NOT IN (SELECT code_original FROM rh.t_sections_analytiques_paie WHERE code_original IS NOT NULL) ; -- Màj des motifs de début de contrat. INSERT INTO rh.t_motifs_debut_contrat(code_original, code, texte, texte_court) SELECT code_moti_recr_cdd, code_moti_recr_cdd, code_moti_recr_cdd, substr(code_moti_recr_cdd, 1, 50) FROM w_cnt_bul WHERE code_moti_recr_cdd NOT IN (SELECT code_original FROM rh.t_motifs_debut_contrat WHERE code_original IS NOT NULL) GROUP BY 1,2,3,4 ; -- Màj des motifs de fin de contrat. INSERT INTO rh.t_motifs_fin_contrat(code_original, code, texte, texte_court) SELECT moti_depa, moti_depa, moti_depa, substr(moti_depa, 1, 50) FROM w_cnt_bul WHERE moti_depa NOT IN (SELECT code_original FROM rh.t_motifs_fin_contrat WHERE code_original IS NOT NULL) GROUP BY 1,2,3,4 ; -- Màj des nationalités. INSERT INTO rh.t_nationalites(code_original, code, texte, texte_court) SELECT nati, nati, nati, substr(nati, 1, 50) FROM w_cnt_bul WHERE nati NOT IN (SELECT code_original FROM rh.t_nationalites WHERE code_original IS NOT NULL) GROUP BY 1,2,3,4 ; -- Màj des qualifications INSERT INTO rh.t_qualifications(code_original, code, texte, texte_court) SELECT id_empl, id_empl, libe, substr(libe, 1, 50) FROM prod_nibelis.emplois WHERE true and id_empl NOT IN (SELECT code_original FROM rh.t_qualifications WHERE code_original IS NOT NULL) and id_soci::bigint = (select id_soci from w_ets limit 1) GROUP BY 1,2,3,4 ; -- Rubriques DROP TABLE IF EXISTS w_RUBRIQUE_HIST; CREATE TEMP TABLE w_RUBRIQUE_HIST AS SELECT ID_RUBR, MAX(CODE_RUBR) AS CODE_RUBR, MAX(LIBELLE) AS LIBELLE, MAX(base.cti_to_number(CODE_RUBR)) AS ORDR FROM MATERIAL_HIST_RUBR_SALA_T HIST_RUBR_SALA_T GROUP BY 1 ; INSERT INTO w_RUBRIQUE_HIST SELECT CODE_CONS, CODE_CONS, CODE_CONS, -1 FROM MATERIAL_HIST_CONS_SALA HIST_CONS_SALA WHERE CODE_CONS IN ('PRESENCE', 'TOTALH', 'TOTALHTRAV') GROUP BY 1 ; UPDATE rh.t_rubriques SET code = CODE_RUBR, texte = LIBELLE, texte_court = substr(LIBELLE, 1, 50) FROM w_RUBRIQUE_HIST WHERE t_rubriques.code_original = ID_RUBR AND ( code IS DISTINCT FROM CODE_RUBR OR texte IS DISTINCT FROM LIBELLE OR texte_court IS DISTINCT FROM substr(LIBELLE, 1, 50) ) ; INSERT INTO rh.t_rubriques(code_original, code, texte, texte_court, rang_edition) SELECT ID_RUBR, CODE_RUBR, LIBELLE, substr(LIBELLE, 1, 50), ORDR FROM w_RUBRIQUE_HIST WHERE ID_RUBR NOT IN (SELECT code_original FROM rh.t_rubriques WHERE code_original IS NOT NULL) GROUP BY 1,2,3,4,5 ; -- Màj des situations de famille INSERT INTO rh.t_situations_famille(code_original, code, texte, texte_court) SELECT situ_fami, situ_fami, situ_fami, substr(situ_fami,1,50) FROM w_cnt_bul WHERE situ_fami NOT IN (SELECT code_original FROM rh.t_situations_famille WHERE code_original IS NOT NULL) GROUP BY 1,2,3,4 ; -- Màj des types de contrat INSERT INTO rh.t_types_contrat(code_original, code, texte, texte_court) SELECT natu_cont, natu_cont, natu_cont, substr(natu_cont, 1, 50) FROM w_cnt_bul WHERE natu_cont NOT IN (SELECT code_original FROM rh.t_types_contrat WHERE code_original IS NOT NULL) GROUP BY 1,2,3,4 ; -- Màj des statuts INSERT INTO rh.t_statuts(code_original, code, texte, texte_court) SELECT cate_prof, cate_prof, cate_prof, substr(cate_prof, 1, 50) FROM w_cnt_bul WHERE cate_prof NOT IN (SELECT code_original FROM rh.t_statuts WHERE code_original IS NOT NULL) GROUP BY 1,2,3,4 ; -- Màj des types de temps de travail INSERT INTO rh.t_types_temps_travail(code_original, code, texte, texte_court) select ttt, ttt, ttt_libelle, ttt_libelle_court from w_cnt_bul where ttt NOT IN (SELECT code_original FROM rh.t_types_temps_travail WHERE code_original IS NOT NULL) group by 1,2,3,4 ; -- Màj des services INSERT INTO rh.t_services(code_original, code, texte, texte_court) SELECT serv, serv, serv, substr(serv, 1, 50) FROM w_cnt_bul WHERE serv NOT IN (SELECT code_original FROM rh.t_services WHERE code_original IS NOT NULL) GROUP BY 1,2,3,4 ; -- Màj des types horaires. INSERT INTO rh.t_types_horaire(code_original, code, texte, texte_court) SELECT equi, substr(equi,1,30), substr(equi,1,255), substr(equi, 1, 50) FROM w_cnt_bul WHERE equi NOT IN (SELECT code_original FROM rh.t_types_horaire WHERE code_original IS NOT NULL) GROUP BY 1,2,3,4 ; -- Màj des codes emploi INSERT INTO rh.t_codes_emploi(code_original, code, texte, texte_court) SELECT regr, regr, regr, substr(regr, 1, 50) FROM w_cnt_bul WHERE regr NOT IN (SELECT code_original FROM rh.t_codes_emploi WHERE code_original IS NOT NULL) GROUP BY 1,2,3,4 ; -- Màj des groupes de grilles INSERT INTO rh.t_grilles_groupes(code_original, code, texte, texte_court) SELECT cate_conv, cate_conv, cate_conv, substr(cate_conv, 1, 50) FROM w_cnt_bul WHERE cate_conv NOT IN (SELECT code_original FROM rh.t_grilles_groupes WHERE code_original IS NOT NULL) GROUP BY 1,2,3,4 ; -- Màj des grilles INSERT INTO rh.t_grilles(code_original, code, texte, texte_court) SELECT fili_conv, fili_conv, fili_conv, substr(fili_conv, 1, 50) FROM w_cnt_bul WHERE fili_conv NOT IN (SELECT code_original FROM rh.t_grilles WHERE code_original IS NOT NULL) GROUP BY 1,2,3,4 ; -- Màj des spécialités (coefficient nibélis). INSERT INTO rh.t_specialites(code_original, code, texte, texte_court) SELECT coeff, coeff, coeff, substr(coeff, 1, 50) FROM w_cnt_bul WHERE coeff NOT IN (SELECT code_original FROM rh.t_specialites WHERE code_original IS NOT NULL) GROUP BY 1,2,3,4 ; ]]> = rhp('rhprovider_start')::date -- Filtrer les lignes générées systématiquement tous les mois par Sage pour chaque salarié méme s'il n'y a rien. AND (false OR p_nombre OR p_base OR p_heures_contrat OR p_heures_payees OR p_heures_travaillees OR p_masse_salariale OR p_brut OR p_avantage_nature OR p_frais_imposables OR p_cotisation_salarie OR p_cotisation_patronale OR p_od_net_salarie OR p_od_net_patronale OR p_net_imposable OR p_net_a_payer) ; ]]> = to_char(rhp('rhprovider_start')::date, 'YYYYMM')::int GROUP BY 1,2,3 ; ]]> 1 AND sum(ratio_theo) <> 0 ) AS subview WHERE w_profil_contrat_mois_anal.contrat_mois_id = subview.contrat_mois_id ; UPDATE w_profil_contrat_mois_anal SET ratio_theo = ratio_corr FROM ( SELECT contrat_mois_id, sum(ratio_theo), base.cti_division(1,count(*)) AS ratio_corr FROM w_profil_contrat_mois_anal GROUP BY 1 HAVING sum(ratio_theo) = 0 ) AS subview WHERE w_profil_contrat_mois_anal.contrat_mois_id = subview.contrat_mois_id ; -- Alimentation des profils simultanés. TRUNCATE rh.p_profil_contrat_mois ; INSERT INTO rh.p_profil_contrat_mois ( profil_id, contrat_mois_id, salarie_id, ratio, mois) SELECT w_profil_contrat_mois_anal.profil_id, p_contrats_mois.oid AS contrat_mois_id, p_contrats_mois.salarie_id, w_profil_contrat_mois_anal.ratio_theo AS ratio, p_contrats_mois.mois_activite FROM rh.p_contrats_mois JOIN w_profil_contrat_mois_anal ON w_profil_contrat_mois_anal.contrat_mois_id = p_contrats_mois.oid GROUP BY 1,2,3,4,5 ; ]]>