<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	<NODE name="INIT" label="INITIALISATIONS">
		 <NODE label="Identification des entreprises/établissements">
            <sqlcmd><![CDATA[

            -- Création d'une table temp des établissements pour faciliter les futures jointures.
            DROP TABLE IF EXISTS w_ets
            ;

            CREATE TEMP TABLE w_ets AS
                SELECT
					id1 as ets_id1,
					max(case when rub = 'A501' THEN opf else null end) as ets_opf
                FROM prod_aliciarh.a_dos
                GROUP BY 1
				HAVING max(case when rub = 'E007' THEN id1 else null end) = rhp('siren') -- SIRET paramétré
            ;

            -- Identification des Entreprises.
            DROP TABLE IF EXISTS temp.x_ref_ent
            ;

            CREATE TABLE temp.x_ref_ent AS
                SELECT
                    ets_id1 as code_original,
                    ets_id1 as code,
                    MAX(ets_opf) as texte,
                    MAX(ets_opf) as siren,
                    null::text as texte_court
                FROM w_ets
                GROUP BY 1,2
            ;

            -- Identification des Etablissements.
            DROP TABLE IF EXISTS temp.x_ref_ets
            ;

            CREATE TABLE temp.x_ref_ets AS
                SELECT
                    ets_id1 as ent_code_original,
                    ets_id1||'-'||ets_id1 as ety_code_original,
                    ets_id1 as code_original,
                    ets_id1 as code,
                    MAX(ets_opf) as siret,
                    MAX(ets_opf) as texte,
                    null::text as texte_court
                FROM w_ets
                GROUP BY 1,2,3,4
            ;

            ]]></sqlcmd>
        </NODE>
		<NODE label="Identification des informations permanentes">
			<sqlcmd><![CDATA[
			
			-- Màj des catégories socio-professionnelles.
			DROP TABLE IF EXISTS temp.x_ref_categorie_socio_professionnelle
            ;

            CREATE TABLE temp.x_ref_categorie_socio_professionnelle AS
				with csp_0 as (
					select
						max(case when rub = 'AZ02' then opf else null end) as code,
						max(case when rub = 'AZ01' then opf else null end) as texte
					from prod_aliciarh.a_cont
						join w_ets on w_ets.ets_id1 = a_cont.dossier
					where rub in ('AZ01', 'AZ02')
					group by dossier, id1, no
				)
				select
					code as code_original,
					code as code,
					max(texte) as texte,
					null::text as texte_court
				from csp_0
				group by 1,2
			;

			-- Identification des Nationalités.
            DROP TABLE IF EXISTS temp.x_ref_nationalite
            ;

            CREATE TABLE temp.x_ref_nationalite AS
                select
                    code as code_original,
                    code as code,
                    libelle as texte,
                    null::text as texte_court
                from prod_aliciarh.a_ref_access
                where "table" = 'STD_PAYS'
                group by 1,2,3,4
            ;

            -- Identification des motifs de début de contrat.
            DROP TABLE IF EXISTS temp.x_ref_motif_debut_contrat
            ;

            CREATE TABLE temp.x_ref_motif_debut_contrat AS
                select
                    opf::text as code_original,
                    opf::text as code,
                    opf::text as texte,
                    null::text as texte_court
                from prod_aliciarh.a_cont
                    join w_ets on w_ets.ets_id1 = a_cont.dossier
                where rub = 'A008'
                group by 1,2,3,4
            ;

            -- Identification des motifs de fin de contrat.
            DROP TABLE IF EXISTS temp.x_ref_motif_fin_contrat
            ;

            CREATE TABLE temp.x_ref_motif_fin_contrat AS
                select
                    opf::text as code_original,
                    opf::text as code,
                    opf::text as texte,
                    null::text as texte_court
                from prod_aliciarh.a_cont
                    join w_ets on w_ets.ets_id1 = a_cont.dossier
                where rub = 'A016'
                group by 1,2,3,4
            ;

            -- Identification des types de contrat.
            DROP TABLE IF EXISTS temp.x_ref_type_contrat
            ;

            CREATE TABLE temp.x_ref_type_contrat AS
                select
                    opf::text as code_original,
                    opf::text as code,
                    opf::text as texte,
                    null::text as texte_court
                from prod_aliciarh.a_cont
                    join w_ets on w_ets.ets_id1 = a_cont.dossier
                where rub = 'A070'
                group by 1,2,3,4
            ;

            -- Identification des groupes cotisants.
            DROP TABLE IF EXISTS temp.x_ref_groupe_cotisant
            ;

            CREATE TABLE temp.x_ref_groupe_cotisant AS
                select 
                    null::text as code_original,
                    null::text as code,
                    null::text as texte,
                    null::text as texte_court
                limit 0
            ;

            -- Identification des organismes de cotisation
            DROP TABLE IF EXISTS temp.x_ref_organisme_cotisation
            ;

            CREATE TABLE temp.x_ref_organisme_cotisation AS
                select 
                    null::text as code_original,
                    null::text as code,
                    null::text as texte,
                    null::text as texte_court
                limit 0
            ;

            -- Identification des qualifications
            DROP TABLE IF EXISTS temp.x_ref_qualification
            ;

            CREATE TABLE temp.x_ref_qualification AS
                select
                    code as code_original,
					code as code,
					libelle as texte,
					null::text as texte_court
                from prod_aliciarh.a_ref_obj
				where dictionnaire = 'TBX_FHPMETIER'
                group by 1,2,3,4
            ;
			
			-- Ajout des qualifications issues d'un champ libre
			INSERT INTO temp.x_ref_qualification
				SELECT 
					replace(opf::text,' ','_') as code_original,
					replace(opf::text,' ','_') as code,
					opf::text as texte,
					null::text as texte_court
                from prod_aliciarh.a_cont
				where rub = 'AV10'
                group by 1,2,3,4
			;

            -- Identification des Comptes

            -- Identification des situations de famille
            DROP TABLE IF EXISTS temp.x_ref_situation_famille
            ;

            CREATE TABLE temp.x_ref_situation_famille AS
                select 
                    null::text as code_original,
                    null::text as code,
                    null::text as texte,
                    null::text as texte_court
                limit 0
            ;

            -- Identification des statuts
            DROP TABLE IF EXISTS temp.x_ref_statut
            ;

            CREATE TABLE temp.x_ref_statut AS
                select
                    code as code_original,
					code as code,
					libelle as texte,
					null::text as texte_court
                from prod_aliciarh.a_ref_obj
				where dictionnaire = 'ST_SALARIE'
                group by 1,2,3,4
            ;

            -- Identification des types de temps de travail
            DROP TABLE IF EXISTS temp.x_ref_type_temps_travail
            ;

            CREATE TABLE temp.x_ref_type_temps_travail AS
                select
                    case when opa = '151.67' then 'C' else 'P'||opa end as code_original,
                    case when opa = '151.67' then 'C' else 'P'||to_char(opa::numeric, 'FM000.00') end as code,
                    case when opa = '151.67' then 'Temps complet' else 'Temps partiel '||replace(opa, '.', ',')||' h' end as texte,
                    null::text as texte_court
                from prod_aliciarh.a_cont
                    join w_ets on w_ets.ets_id1 = a_cont.dossier
                where rub = 'A079'
                group by 1,2,3,4
            ;

            -- Identification des services
            DROP TABLE IF EXISTS temp.x_ref_service
            ;

            CREATE TABLE temp.x_ref_service AS
                select
                    code as code_original,
					code as code,
					libelle as texte,
					null::text as texte_court
                from prod_aliciarh.a_ref_obj
				where dictionnaire = 'TBX_PFC_UNITE'
                group by 1,2,3,4
            ;

            -- Identification des spécialités

            -- Identification des codes emploi
            DROP TABLE IF EXISTS temp.x_ref_code_emploi
            ;

            CREATE TABLE temp.x_ref_code_emploi AS
				select 
                    null::text as code_original,
                    null::text as code,
                    null::text as texte,
                    null::text as texte_court
                limit 0
            ;

            -- Identification des types d'horaire

            -- Identification des grilles
            DROP TABLE IF EXISTS temp.x_ref_grille
            ;

            CREATE TABLE temp.x_ref_grille AS
                select 
                    null::text as code_original,
                    null::text as code,
                    null::text as texte,
                    null::text as texte_court
                limit 0
            ;

            -- Identification des groupes de grilles

            -- Identification des motifs d'arret

            -- Identification des précisions du motif d'arrêt

            -- Identification des motifs de visite médicale

            -- Identification des circonstances d'accident du travail

            -- Identification des lieux d'accident du travail

            -- Identification de la nature de l'accident du travail

            -- Identification des sièges d'accident du travail

            -- Identification des listes de formations

            DROP TABLE IF EXISTS temp.x_ref_section_analytique
            ;

            CREATE TABLE temp.x_ref_section_analytique AS
                select 
                    null::int as oid,
                    null::text as code_original,
                    null::text as code,
                    null::text as texte,
                    null::text as texte_court
                limit 0
            ;

			-- Identification des rubriques de paie.
			DROP TABLE IF EXISTS temp.x_ref_rubrique
            ;

            CREATE TABLE temp.x_ref_rubrique AS
                with rub_det_0 as (
                    select
						rub,
						max(lib_rub) as lib_rub
					from prod_aliciarh.a_bult
					group by 1
                )
                , rub_det_1 as (
                    select
                        rub,
                        lib_rub as texte,
                        rub between 'B000' and 'B999' or rub between 'C100' and 'C999' or rub between 'E000' and 'F999' AS p_detail,
                        rub between 'B000' and 'B999' or rub between 'C100' and 'C999' or rub between 'E000' and 'F999' AS p_nombre,
                        rub between 'B000' and 'B999' or rub between 'C100' and 'C999' or rub between 'E000' and 'F999' AS p_base,
                        false AS p_heures_payees,
                        false AS p_heures_travaillees,
                        rub between 'B000' and 'B999' or rub between 'C100' and 'C999' AS p_masse_salariale,
                        rub between 'B000' and 'B999' AS p_brut,
                        false AS p_avantage_nature,
                        false AS p_frais_imposables,
                        rub between 'C100' and 'C999' AS p_cotisation_salarie,
                        rub between 'C100' and 'C999' AS p_cotisation_patronale,
                        rub between 'E000' and 'F999' AS p_od_net_salarie,
                        false AS p_od_net_patronale,
                        rub between 'B000' and 'B999' or rub between 'C100' and 'C999' AS p_net_imposable,
                        rub between 'B000' and 'B999' or rub between 'C100' and 'C999' or rub between 'E000' and 'F999' AS p_net_a_payer
                    from rub_det_0
                )
                    SELECT
                        rub as code_original,
                        rub as code,
                        texte as texte,
                        null::text as texte_court,
                        -1 as rang_edition,
                        false as p_cumul,
                        p_detail as p_detail,
                        p_nombre as p_nombre,
                        0 as s_nombre,
                        1 as c_nombre,
                        p_base as p_base,
                        1 as s_base,
                        1 as c_base,
                        false as p_heures_contrat,
                        0 as s_heures_contrat,
                        1 as c_heures_contrat,
                        p_heures_payees as p_heures_payees,
                        0 as s_heures_payees,
                        1 as c_heures_payees,
                        p_heures_travaillees as p_heures_travaillees,
                        0 as s_heures_travaillees,
                        1 as c_heures_travaillees,
                        p_masse_salariale as p_masse_salariale,
                        case when p_cotisation_patronale or p_od_net_patronale then 5 else 3 end as s_masse_salariale,
                        1 as c_masse_salariale,
                        p_brut as p_brut,
                        3 as s_brut,
                        1 as c_brut,
                        p_avantage_nature as p_avantage_nature,
                        3 as s_avantage_nature,
                        1 as c_avantage_nature,
                        p_frais_imposables as p_frais_imposables,
                        3 as s_frais_imposables,
                        1 as c_frais_imposables,
                        p_cotisation_salarie as p_cotisation_salarie,
                        3 as s_cotisation_salarie,
                        -1 as c_cotisation_salarie,
                        p_cotisation_patronale as p_cotisation_patronale,
                        5 as s_cotisation_patronale,
                        1 as c_cotisation_patronale,
                        p_od_net_salarie as p_od_net_salarie,
                        3 as s_od_net_salarie,
                        1 as c_od_net_salarie,
                        p_od_net_patronale as p_od_net_patronale,
                        5 as s_od_net_patronale,
                        1 as c_od_net_patronale,
                        p_net_imposable as p_net_imposable,
                        3 as s_net_imposable,
                        case when p_cotisation_salarie then -1 else 1 end as c_net_imposable,
                        p_net_a_payer as p_net_a_payer,
                        3 as s_net_a_payer,
                        case when p_cotisation_salarie then -1 else 1 end as c_net_a_payer
                    FROM rub_det_1
            ;

			]]></sqlcmd>
		</NODE>
		<NODE label="Pré-traitements">
            <sqlcmd><![CDATA[

            -- ALIMENTATION DES HISTORIQUES

			-- SALARIES
            DROP TABLE IF EXISTS temp.x_ref_salarie
            ;

            CREATE TABLE temp.x_ref_salarie AS
				select
					a_sal.id1 as matricule,
					a_sal.id1 as code,
					a_sal.id1 as code_original,
					max(ets_id1) as entreprise,
					max(case when a_sal.rub = 'A001' THEN a_sal.opf else null end) as nom,
					max(case when a_sal.rub = 'A002' THEN a_sal.opf else null end) as prenom,
					coalesce(max(case when a_sal.rub = 'A003' THEN a_sal.opf else null end), '')::text as nom_naissance,
					to_date(max(case when a_sal.rub = 'A008' THEN a_sal.opg else null end), 'DD/MM/YYYY') as date_naissance,
					max(case when a_sal.rub = 'A007' THEN a_sal.opf else null end) as sexe,
					max(case when a_sal.rub = 'A039' THEN a_sal.opf else null end) as nationalite,
					max(case when a_sal.rub = 'C001' THEN a_sal.opf else null end) as situation_famille,
				    max(case when a_sal.rub = 'A027' THEN a_sal.opf else null end) as code_postal,
					max(case when a_sal.rub = 'A004' THEN a_sal.opf else null end) as nir,
					-- max(case when a_sal.rub = 'A005' THEN a_sal.opf else null end) as intitule,
					-- max(case when a_sal.rub = 'A030' THEN a_sal.opf else null end) as fixe,
					-- max(case when a_sal.rub = 'A031' THEN a_sal.opf else null end) as mobile,
					max(case when a_sal.rub = 'A02Z' THEN a_sal.opf else null end) as adresse1,
					'' as adresse2,
					-- max(case when a_sal.rub = 'A028' THEN a_sal.opf else null end) as commune,
					to_date(max(case when a_sal.rub = 'A105' THEN a_sal.opg else null end), 'DD/MM/YYYY') as date_debut,
					to_date(case when (max(case when a_sal.rub = 'A106' THEN a_sal.opg else null end)) is null then '31/12/2099' else (max(case when a_sal.rub = 'A106' THEN a_sal.opg else null end)) end, 'DD/MM/YYYY') as date_fin,
					to_date(min(case when a_sal.rub = 'A105' THEN a_sal.opg else null end), 'DD/MM/YYYY') as date_entree_ets,
					to_date(case when (max(case when a_sal.rub = 'A106' THEN a_sal.opg else null end)) is null then '31/12/2099' else (max(case when a_sal.rub = 'A106' THEN a_sal.opg else null end)) end, 'DD/MM/YYYY') as date_sortie_ets,
					to_date(max(case when a_sal.rub = 'A105' THEN a_sal.opg else null end), 'DD/MM/YYYY') as date_anciennete,
                    -- Champs dédiés au public.
                    null::date AS date_entree_fp,
                    null::date AS date_entree_fph,
                    0 AS no_adeli,
                    0 AS code_cotisation_id,
                    0 AS matricule_retraite
				from prod_aliciarh.a_sal
					join w_ets on w_ets.ets_id1 = a_sal.dossier
				where a_sal.rub in ('A001', 'A002', 'A003', 'A004', 'A005', 'A007', 'A008', 'A030', 'A031', 'A02Z', 'A027', 'A028', 'A039', 'A105', 'A106', 'C001')
				group by 1,2,3
				having max(case when a_sal.rub = 'A001' THEN a_sal.opf else null end) is not null
			;

			-- HISTORIQUE DES CONTRATS.
            DROP TABLE IF EXISTS temp.x_hst_contrat
            ;

            CREATE TABLE temp.x_hst_contrat AS
				select
					row_number() over () as row_id,
					dossier as ent_code_original,
                    dossier as ets_code_original,
                    dossier||'-'||dossier as ety_code_original,
					id1 as sal_code_original,
					id1||'-'||lpad(no, 3, '0') as cnt_code_original,
					id1||'-'||lpad(no, 3, '0') as numero_contrat,
					to_date(min(case when rub = 'A005' then opg else null end), 'DD/MM/YYYY') as date_debut,
					min(case when rub = 'A006' and to_date(opg, 'DD/MM/YYYY') between '1900-01-01'::date and '2050-01-01'::date then to_date(opg, 'DD/MM/YYYY') else '2099-12-31'::date end) as date_fin
				from prod_aliciarh.a_cont
					join w_ets on w_ets.ets_id1 = a_cont.dossier
                    join temp.x_ref_salarie on x_ref_salarie.code_original = a_cont.id1
				where rub in ('A005', 'A006', 'A007', 'A015')
				group by 2,3,4,5,6,7
            ;

            WITH dates_es as (
                select
                    sal_code_original,
                    max(date_debut) as cn_date_debut,
                    max(date_fin)   as cn_date_fin,
                    min(date_debut) as es_date_debut,
                    max(date_fin)   as es_date_fin
                from temp.x_hst_contrat
                group by 1
            )
            UPDATE temp.x_ref_salarie SET
                date_debut = dates_es.cn_date_debut,
                date_fin = dates_es.cn_date_fin,
                date_entree_ets = dates_es.es_date_debut,
                date_sortie_ets = dates_es.es_date_fin
            FROM dates_es
            WHERE x_ref_salarie.matricule = dates_es.sal_code_original 
            ;

            -- HISTORIQUE DES BULLETINS.
			DROP TABLE IF EXISTS temp.x_hst_bulletin
            ;

            CREATE TABLE temp.x_hst_bulletin AS
                with w_bult as (
                    select
						dense_rank() OVER (order BY id1, datapp) AS bul_id,
						*
					FROM prod_aliciarh.a_bult
                )
				SELECT
					dossier as ent_code_original,
					dossier as ets_code_original,
					dossier||'-'||dossier as ety_code_original,
					id1 AS sal_code_original,
					row_number() OVER (ORDER BY id1, datapp) AS bul_code_original,
					max(case when rub = 'A005' then to_date(opg, 'DD/MM/YYYY') else null end) AS date_debut,
					max(case when rub = 'A006' then to_date(opg, 'DD/MM/YYYY') else null end) AS date_fin,
					max(case when rub = 'A001' then to_date(opg, 'DD/MM/YYYY') else null end) as date_paie,
					to_char(to_date(datapp, 'DD/MM/YYYY'), 'YYYYMM')::numeric AS mois_paie,
					row_number() over () as row_id,
					bul_id,
					null::text as cnt_code_original, -- un bulletin est rattaché à un seul et unique contrat.
                    0 as est_hors_periode
				FROM w_bult as a_bult
					join w_ets on w_ets.ets_id1 = a_bult.dossier
                    join temp.x_ref_salarie on x_ref_salarie.code_original = a_bult.id1
				WHERE 1=1
				GROUP BY dossier, id1, datapp, bul_id
			;

			-- Association avec le contrat correspondant aux dates.
            -- Un bulletin est rattaché à un seul et unique contrat.
            with toto as (
                SELECT
                    x_hst_bulletin.row_id,
                    (max(array[x_hst_contrat.date_fin::text, x_hst_contrat.cnt_code_original]))[2] as cnt_code_original
                from temp.x_hst_bulletin
                    join temp.x_hst_contrat on 1=1
                        and x_hst_contrat.sal_code_original = x_hst_bulletin.sal_code_original
                        and base.cti_overlaps(x_hst_contrat.date_debut, x_hst_contrat.date_fin, x_hst_bulletin.date_debut, x_hst_bulletin.date_fin)
                group by 1
            )
            UPDATE temp.x_hst_bulletin set
                cnt_code_original = toto.cnt_code_original
            from toto
            where x_hst_bulletin.row_id = toto.row_id
            ;

            -- Association avec le contrat précédent.
            with toto as (
                SELECT
                    x_hst_bulletin.row_id,
                    (max(array[x_hst_contrat.date_fin::text, x_hst_contrat.cnt_code_original]))[2] as cnt_code_original
                from temp.x_hst_bulletin
                    join temp.x_hst_contrat on 1=1
                        and x_hst_contrat.sal_code_original = x_hst_bulletin.sal_code_original
                        and x_hst_contrat.date_fin < x_hst_bulletin.date_debut
                where x_hst_bulletin.cnt_code_original is null
                group by 1
            )
            UPDATE temp.x_hst_bulletin set
                cnt_code_original = toto.cnt_code_original,
                est_hors_periode = 1
            from toto
            where x_hst_bulletin.row_id = toto.row_id
            ;

            -- Association avec le contrat suivant.
            with toto as (
                SELECT
                    x_hst_bulletin.row_id,
                    (max(array[x_hst_contrat.date_fin::text, x_hst_contrat.cnt_code_original]))[2] as cnt_code_original
                from temp.x_hst_bulletin
                    join temp.x_hst_contrat on 1=1
                        and x_hst_contrat.sal_code_original = x_hst_bulletin.sal_code_original
                        and x_hst_contrat.date_fin > x_hst_bulletin.date_debut
                where x_hst_bulletin.cnt_code_original is null
                group by 1
            )
            UPDATE temp.x_hst_bulletin set
                cnt_code_original = toto.cnt_code_original,
                est_hors_periode = 1
            from toto
            where x_hst_bulletin.row_id = toto.row_id
            ;

            -- ICI : Tous les bulletins doivent avoir un contrat de renseigné.
            -- A remonter plus tard dans une table spéciale avant suppression pour controle expert.
            delete from temp.x_hst_bulletin
            where cnt_code_original is null
            ;

			-- HISTORIQUE ETP Théorique contrat.
            DROP TABLE IF EXISTS temp.x_hst_etp_contrat
            ;

            CREATE TABLE temp.x_hst_etp_contrat AS
                select
                    dossier||'-'||dossier as ety_code_original,
                    id1 as sal_code_original,
                    id1||'-'||lpad(no, 3, '0') as cnt_code_original,
                    to_date(datapp, 'DD/MM/YYYY') as date_effet,
                    opa::numeric / 151.67 as etp_contrat
                from prod_aliciarh.a_cont
                    join w_ets on w_ets.ets_id1 = a_cont.dossier
                    join temp.x_ref_salarie on x_ref_salarie.code_original = a_cont.id1
                where rub = 'A079'
                group by 1,2,3,4,5
            ;

			-- HISTORIQUE CSP.
            DROP TABLE IF EXISTS temp.x_hst_categorie_socio_professionnelle
            ;

            CREATE TABLE temp.x_hst_categorie_socio_professionnelle AS
                select
                    dossier||'-'||dossier as ety_code_original,
                    id1 as sal_code_original,
                    id1||'-'||lpad(no, 3, '0') as cnt_code_original,
                    to_date(datapp, 'DD/MM/YYYY') as date_effet,
                    opf::text as categorie_socio_professionnelle_code_original
                from prod_aliciarh.a_cont
                    join w_ets on w_ets.ets_id1 = a_cont.dossier
                    join temp.x_ref_salarie on x_ref_salarie.code_original = a_cont.id1
                where rub = 'AZ02'
                group by 1,2,3,4,5
            ;

			-- HISTORIQUE Motif de début.
            DROP TABLE IF EXISTS temp.x_hst_motif_debut_contrat
            ;

            CREATE TABLE temp.x_hst_motif_debut_contrat AS
				SELECT
                    null::text as ety_code_original,
                    null::text as sal_code_original,
                    null::text as cnt_code_original,
                    null::date as date_effet,
                    null::text as motif_debut_code_original
                limit 0
            ;

            -- HISTORIQUE Motif de fin.
            DROP TABLE IF EXISTS temp.x_hst_motif_fin_contrat
            ;

            CREATE TABLE temp.x_hst_motif_fin_contrat AS
				SELECT
                    null::text as ety_code_original,
                    null::text as sal_code_original,
                    null::text as cnt_code_original,
                    null::date as date_effet,
                    null::text as motif_fin_code_original
                limit 0
            ;


            -- HISTORIQUE Types de contrat.
            DROP TABLE IF EXISTS temp.x_hst_type_contrat
            ;

            CREATE TABLE temp.x_hst_type_contrat AS
                select
                    dossier||'-'||dossier as ety_code_original,
                    id1 as sal_code_original,
                    id1||'-'||lpad(no, 3, '0') as cnt_code_original,
                    to_date(datapp, 'DD/MM/YYYY') as date_effet,
                    opf::text as type_contrat_code_original
                from prod_aliciarh.a_cont
                    join w_ets on w_ets.ets_id1 = a_cont.dossier
                    join temp.x_ref_salarie on x_ref_salarie.code_original = a_cont.id1
                where rub = 'A070'
                group by 1,2,3,4,5
            ;

            -- HISTORIQUE CSP.
            DROP TABLE IF EXISTS temp.x_hst_qualification
            ;

            CREATE TABLE temp.x_hst_qualification AS
                select
                    dossier||'-'||dossier as ety_code_original,
                    id1 as sal_code_original,
                    id1||'-'||lpad(no, 3, '0') as cnt_code_original,
                    to_date(datapp, 'DD/MM/YYYY') as date_effet,
                    replace(opf::text,' ','_') as qualification_code_original
                from prod_aliciarh.a_cont
                    join w_ets on w_ets.ets_id1 = a_cont.dossier
                    join temp.x_ref_salarie on x_ref_salarie.code_original = a_cont.id1
                where rub = 'AVF1'
				or rub = 'AV10' --qualifications non référencées issues d'un champ libre
                group by 1,2,3,4,5
            ;

            -- HISTORIQUE Status.
            DROP TABLE IF EXISTS temp.x_hst_statut
            ;

            CREATE TABLE temp.x_hst_statut AS
				select
                    dossier||'-'||dossier as ety_code_original,
                    id1 as sal_code_original,
                    id1||'-'||lpad(no, 3, '0') as cnt_code_original,
                    to_date(datapp, 'DD/MM/YYYY') as date_effet,
                    opf::text as statut_code_original
                from prod_aliciarh.a_cont
                    join w_ets on w_ets.ets_id1 = a_cont.dossier
                    join temp.x_ref_salarie on x_ref_salarie.code_original = a_cont.id1
                where rub = 'A072'
                group by 1,2,3,4,5
            ;

            -- HISTORIQUE CSP.
            DROP TABLE IF EXISTS temp.x_hst_type_temps_travail
            ;

            CREATE TABLE temp.x_hst_type_temps_travail AS
                select
                    dossier||'-'||dossier as ety_code_original,
                    id1 as sal_code_original,
                    id1||'-'||lpad(no, 3, '0') as cnt_code_original,
                    to_date(datapp, 'DD/MM/YYYY') as date_effet,
                    case when opa = '151.67' then 'C' else 'P'||opa end as type_temps_travail_code_original
                from prod_aliciarh.a_cont
                    join w_ets on w_ets.ets_id1 = a_cont.dossier
                    join temp.x_ref_salarie on x_ref_salarie.code_original = a_cont.id1
                where rub = 'A079'
                group by 1,2,3,4,5
            ;

            -- HISTORIQUE CSP.
            DROP TABLE IF EXISTS temp.x_hst_service
            ;

            CREATE TABLE temp.x_hst_service AS
                select
                    dossier||'-'||dossier as ety_code_original,
                    id1 as sal_code_original,
                    id1||'-'||lpad(no, 3, '0') as cnt_code_original,
                    to_date(datapp, 'DD/MM/YYYY') as date_effet,
                    opf::text as service_code_original
                from prod_aliciarh.a_cont
                    join w_ets on w_ets.ets_id1 = a_cont.dossier
                    join temp.x_ref_salarie on x_ref_salarie.code_original = a_cont.id1
                where rub = 'A948'
                group by 1,2,3,4,5
            ;

            -- HISTORIQUE CSP.
            DROP TABLE IF EXISTS temp.x_hst_code_emploi
            ;

            CREATE TABLE temp.x_hst_code_emploi AS
				SELECT
                    null::text as ety_code_original,
                    null::text as sal_code_original,
                    null::text as cnt_code_original,
                    null::date as date_effet,
                    null::text as code_emploi_code_original
                limit 0
            ;

            -- HISTORIQUE GRILLE.
            DROP TABLE IF EXISTS temp.x_hst_grille
            ;

            CREATE TABLE temp.x_hst_grille AS
                SELECT
                    null::text as ety_code_original,
                    null::text as sal_code_original,
                    null::text as cnt_code_original,
                    null::date as date_effet,
                    null::text as grille_code_original
                limit 0
            ;


			]]></sqlcmd>
		</NODE>
		<NODE label="Historique de la paie">
            <sqlcmd><![CDATA[

            DROP TABLE IF EXISTS temp.x_hst_paie
            ;

            CREATE TABLE temp.x_hst_paie AS
                with w_bult as (
                    select
						dense_rank() OVER (order BY id1, datapp) AS bul_id,
						*
					FROM prod_aliciarh.a_bult
                )
                , w_paie as (
					select
						bul_id,
						id1 AS mat,
						to_date(datapp, 'DD/MM/YYYY') as datapp,
						dossier,
						rub,
						opa::numeric as base,
                       	opb::numeric as txsal,
                    	opc::numeric as mntsal,
                       	opd::numeric as txpat,
                     	ope::numeric as mntpat
					FROM w_bult
					WHERE 1!=1
						OR opa::numeric != 0
                       	OR opb::numeric != 0
                    	OR opc::numeric != 0
                       	OR opd::numeric != 0
                     	OR ope::numeric != 0
                )
                    select
                        bulletins.bul_id,
                        bulletins.ety_code_original,
                        bulletins.sal_code_original,
                        bulletins.cnt_code_original,
                        bulletins.bul_code_original,
                        bulletins.date_debut,
                        bulletins.date_fin,
                        bulletins.mois_paie as mois_activite,
                        bulletins.date_paie,
                        bulletins.mois_paie,
                        rub as rub_code_original,
                        base,
                        txsal,
                        mntsal,
                        txpat,
                        mntpat
                    from temp.x_hst_bulletin as bulletins
                        join temp.x_ref_ets on x_ref_ets.ety_code_original = bulletins.ety_code_original
                        join w_paie on w_paie.bul_id = bulletins.bul_id
                    where 1=1
                        AND bulletins.mois_paie >= to_char(rhp('rhprovider_start')::date, 'YYYYMM')
            ;

            ]]></sqlcmd>
        </NODE>
	</NODE>
	<NODE name="PROD" label="RECUPERATION DES DONNEES DE PRODUCTION">
        <NODE name="PROD_COMMON" type="common" />
    </NODE>
	<NODE name="POST" label="POST-TRAITEMENTS">
		<NODE name="POST" type="common"/>
	</NODE>
	<NODE name="VACUUM" label="REORGANISATION BASE DE DONNEES">
		<NODE name="VACUUM" type="common" />
	</NODE>
	<NODE name="RAZ" label="RAZ BASE">
		<NODE name="RAZ" type="common" />
	</NODE>
	<NODE name="RAZ_ALL" label="RAZ ALL">
		<NODE name="RAZ_ALL" type="common" />
	</NODE>
</ROOT>
