<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>

		
	<NODE name="INIT" label="INITIALISATIONS ENVIRONNEMENT [FINESS] [FINESS_TEXTE] ([MODE])">
	</NODE>

	<NODE name="SEJOUR" label="RECUPERATION DES SEJOURS">
	
		<NODE label="Patients">
			<sqlcmd><![CDATA[
			
				-- Récupération tables sources
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *, ''''::text AS no_patient_cible, ''0''::text AS existe_deja
					FROM activite.p_patients limit 100000000000
					', 'temp p_patients'
				);
				-- détection existance patient
				-- le même en tout point
				UPDATE p_patients p_patients_e
				SET no_patient_cible = trim(p_patients_e.no_patient),
					existe_deja = '1'
				FROM activite.p_patients
				WHERE no_patient_cible = '' AND
					trim(p_patients_e.no_patient) = trim(p_patients.no_patient) AND
					upper(trim(p_patients_e.nom)) = upper(trim(p_patients.nom)) AND
					upper(trim(p_patients_e.prenom)) = upper(trim(p_patients.prenom)) AND
					(p_patients.date_naissance IS NULL OR p_patients_e.date_naissance = p_patients.date_naissance) AND
					upper(trim(p_patients_e.nom_naissance)) = upper(trim(p_patients.nom_naissance)) AND
					p_patients_e.code_sexe = p_patients.code_sexe;

				-- avec suffixe
				UPDATE p_patients p_patients_e
				SET no_patient_cible = trim(p_patients_e.no_patient) || '_[PX]',
					existe_deja = '1'
				FROM activite.p_patients
				WHERE 1=1
                    AND p_patients_e.no_patient_cible = ''
					AND trim(p_patients_e.no_patient) || '_[PX]' = trim(p_patients.no_patient)
					AND p_patients_e.existe_deja = '0';
					;
				
				-- retrocompat
				UPDATE p_patients p_patients_e
				SET no_patient_cible = trim(p_patients_e.no_patient) || '_[PX]',
					existe_deja = '1'
				FROM activite.p_patients
				WHERE 1=1
					AND p_patients_e.existe_deja = '0'
                    AND p_patients_e.no_patient_cible = ''
					AND trim(p_patients_e.no_patient) = substr(trim(p_patients.no_patient),1,length(trim(p_patients_e.no_patient))+1)
					AND p_patients.nom = p_patients_e.nom 
					AND p_patients.prenom = p_patients_e.prenom
					AND (p_patients.date_naissance IS NULL OR p_patients_e.date_naissance = p_patients.date_naissance)
					AND upper(trim(p_patients_e.nom_naissance)) = upper(trim(p_patients.nom_naissance))
					AND p_patients_e.code_sexe = p_patients.code_sexe
				;
				
				-- patient non existant
				-- on garde le numero s'il n'existe pas
				WITH missing_patients as (
					SELECT p_patients_e.no_patient
					FROM  p_patients p_patients_e
					LEFT JOIN activite.p_patients ON trim(p_patients.no_patient) = trim(p_patients_e.no_patient)
					WHERE p_patients.no_patient IS NULL
					AND no_patient_cible = ''
				)
				UPDATE p_patients
				SET no_patient_cible = trim(p_patients.no_patient)
				FROM missing_patients
				WHERE missing_patients.no_patient = p_patients.no_patient;

				-- sinon on suffixe
				UPDATE p_patients p_patients_e
				SET no_patient_cible = trim(no_patient) || '_[PX]'
				WHERE no_patient_cible = '';
				
				ANALYSE p_patients
				;

				-- comptabilité avec ancienne méthode (sans _)
				UPDATE activite.p_patients
				SET 
					no_patient = trim(p_patients_e.no_patient_cible)
				FROM p_patients p_patients_e
				WHERE
					trim(p_patients_e.no_patient) || '_[PX]' = trim(p_patients.no_patient)
                    AND p_patients_e.existe_deja != '0'
					AND p_patients.no_patient != p_patients_e.no_patient_cible
				;
				UPDATE activite.p_patients
				SET 
					no_patient = trim(p_patients_e.no_patient_cible)
				FROM p_patients p_patients_e
				WHERE
					trim(p_patients_e.no_patient) || '[PX]' = trim(p_patients.no_patient)
						AND p_patients.nom = p_patients_e.nom 
						AND p_patients.prenom = p_patients_e.prenom
						AND (p_patients.date_naissance IS NULL OR p_patients_e.date_naissance = p_patients.date_naissance)
						AND upper(trim(p_patients_e.nom_naissance)) = upper(trim(p_patients.nom_naissance))
						AND p_patients_e.code_sexe = p_patients.code_sexe
                    AND p_patients_e.existe_deja != '0'
					AND p_patients.no_patient != p_patients_e.no_patient_cible
				;

                -- Mise à jour des noms et prénoms des patients depuis l'environnement source
                
                UPDATE activite.p_patients SET
                    nom = p_patients_e.nom,
                    prenom = p_patients_e.prenom,
                    nom_naissance = p_patients_e.nom_naissance,
					date_naissance = p_patients_e.date_naissance,
					code_sexe = p_patients_e.code_sexe
                FROM
                    p_patients p_patients_e
                WHERE 1=1
                    AND p_patients_e.existe_deja = '1'
                    AND p_patients_e.no_patient_cible = p_patients.no_patient
                    AND(p_patients.date_naissance IS NULL OR p_patients_e.date_naissance = p_patients.date_naissance)
                    AND p_patients_e.code_sexe = p_patients.code_sexe
                    AND ( 1!=1
                        OR p_patients.nom != p_patients_e.nom
                        OR p_patients.prenom != p_patients_e.prenom
                        OR p_patients.nom_naissance != p_patients_e.nom_naissance
						OR p_patients.date_naissance != p_patients_e.date_naissance
						OR p_patients.code_sexe != p_patients_e.code_sexe
                        );
				
				-- Ajout

				INSERT INTO activite.p_patients(
					finess, 
					no_patient, 
					nom, 
					prenom, 
					date_naissance, 
					nom_naissance, 
					code_sexe)
				SELECT 
					'', 
					p_patients_e.no_patient_cible, 
					p_patients_e.nom, 
					p_patients_e.prenom, 
					p_patients_e.date_naissance, 
					p_patients_e.nom_naissance, 
					p_patients_e.code_sexe
				FROM p_patients p_patients_e
				WHERE existe_deja = '0'
				;


				DROP TABLE IF EXISTS w_correspondances_patients;
				CREATE TEMP TABLE w_correspondances_patients AS
				SELECT 
					p_patients_e.no_patient AS no_patient_source,
					p_patients_e.no_patient_cible AS no_patient
				FROM p_patients p_patients_e;

				CREATE INDEX w_correspondances_patients_1
				  ON w_correspondances_patients
				  USING btree
				  (no_patient_source);
										
										
			]]></sqlcmd>
		</NODE>
				
		<NODE label="Séjours">
			<sqlcmd><![CDATA[
				
			-- Extraction selon date					
				SELECT dblink_exec('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', 
					'
					DROP TABLE IF EXISTS temp.w_sejours CASCADE
					.,
					CREATE TABLE temp.w_sejours AS
					SELECT no_sejour, oid
					FROM activite.p_sejours
					WHERE code_sorti <> ''1'' OR date_sortie >= ''[ENV_ADM_ANNEEDEBUT]0101''
					',
					true);


				SELECT dblink_exec('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', 
					'
					CREATE INDEX i_w_sejours_1
						  ON temp.w_sejours
						  USING btree
						  (no_sejour)
					',
					true);
					
				SELECT dblink_exec('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', 
					'
					CREATE INDEX i_w_sejours_2
						  ON temp.w_sejours
						  USING btree
						  (oid)
					',
					true);
					
				SELECT dblink_exec('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', 
					'	
					INSERT INTO temp.w_sejours
					SElECT p_sejours.no_sejour, p_sejours.oid
					FROM activite.p_factures
					JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour
					WHERE (p_factures.code_facture <= ''0'' OR
						p_factures.date_vente >= ''[ENV_ADM_ANNEEDEBUT]0101'') AND
						NOT EXISTS (SELECT 1 FROM temp.w_sejours WHERE temp.w_sejours.no_sejour = p_factures.no_sejour)
					',
					true);
					
				SELECT dblink_exec('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', 
					'
					INSERT INTO temp.w_sejours
					SELECT p_sejours.no_sejour, p_sejours.oid
					FROM activite.p_factures_soldes_c
					JOIN activite.p_factures ON p_factures_soldes_c.no_facture = p_factures.no_facture
					JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour
					WHERE date_comptable >= ''[ENV_ADM_ANNEEDEBUT]0101'' AND
						NOT EXISTS (SELECT 1 FROM temp.w_sejours WHERE temp.w_sejours.no_sejour = p_sejours.no_sejour)
					',
					true);
					
				SELECT dblink_exec('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', 
					'	
					INSERT INTO temp.w_sejours
					SELECT p_sejours.no_sejour, p_sejours.oid
					FROM activite.p_factures_soldes_h
					JOIN activite.p_factures ON p_factures_soldes_h.no_facture = p_factures.no_facture
					JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour
					WHERE date_comptable >= ''[ENV_ADM_ANNEEDEBUT]0101'' AND
						NOT EXISTS (SELECT 1 FROM temp.w_sejours WHERE temp.w_sejours.no_sejour = p_sejours.no_sejour)
					'
					,
					true);
					
				SELECT dblink_exec('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', 
					'	
					INSERT INTO temp.w_sejours
					SELECT p_sejours.no_sejour, p_sejours.oid
					FROM activite.p_factures_encours
					JOIN activite.p_factures ON p_factures_encours.no_facture = p_factures.no_facture
					JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour
					WHERE p_factures_encours.date_encours >= ''[ENV_ADM_ANNEEDEBUT]0101'' AND
						NOT EXISTS (SELECT 1 FROM temp.w_sejours WHERE temp.w_sejours.no_sejour = p_sejours.no_sejour)
					'
					,
					true);
					
				SELECT dblink_exec('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', 
					'
					ANALYSE temp.w_sejours
					',
					true);


				SELECT dblink_exec('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', 
					'
					DROP TABLE IF EXISTS temp.w_factures CASCADE
					.,
					CREATE TABLE temp.w_factures AS
					SELECT no_facture, p_factures.oid
					FROM activite.p_factures
					JOIN temp.w_sejours ON p_factures.no_sejour = w_sejours.no_sejour
					',
					true);
					
				SELECT dblink_exec('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', 
					'
					ANALYSE temp.w_factures
					',
					true);


				SELECT dblink_exec('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', 
					'
					CREATE INDEX i_w_factures_1
						  ON temp.w_factures
						  USING btree
						  (no_facture)
					',
					true);

				SELECT dblink_exec('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', 
					'
					CREATE INDEX i_w_factures_2
						  ON temp.w_factures
						  USING btree
						  (oid)
					',
					true);

				
				-- Récupération tables sources
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_sejours 
					WHERE EXISTS (SELECT 1 FROM temp.w_sejours WHERE temp.w_sejours.no_sejour = p_sejours.no_sejour)
					limit 100000000000
					', 'temp p_sejours'
				);
				
				CREATE INDEX p_sejours_i1
				  ON p_sejours
				  USING btree
				  (ghs_id);
				  
				CREATE INDEX p_sejours_i2
				  ON p_sejours
				  USING btree
				  (mode_traitement_id);
				  
				CREATE INDEX p_sejours_i3
				  ON p_sejours
				  USING btree
				  (ghm_id);
				
				INSERT INTO activite.p_sejours(
					finess,
					no_sejour,
					date_entree,
					date_sortie,
					no_patient,
					code_sexe,
					age,
					code_sorti,
					code_prevu,
					type_sejour,
					date_facture,
					date_solde,
					delai_facture,
					delai_solde,
					montant_facture_c,
					montant_facture_h,
					montant_regle_c,
					montant_regle_h,
					nb_factures,
					nb_factures_regularisation,
					date_expedition,
					delai_expedition,
					lieu_sortie_id,
					medecin_sejour_id,
					tiers_payant_1_id,
					tiers_payant_2_id,
					tiers_payant_0_id,
					tiers_payant_22_id,
					montant_facture_0_c,
					montant_facture_0_h,
					montant_facture_1_c,
					montant_facture_1_h,
					montant_facture_12_c,
					montant_facture_12_h,
					montant_facture_2_c,
					montant_facture_2_h,
					montant_facture_22_c,
					montant_facture_22_h,
					montant_regle_0_c,
					montant_regle_0_h,
					montant_regle_1_c,
					montant_regle_1_h,
					montant_regle_12_c,
					montant_regle_12_h,
					montant_regle_2_c,
					montant_regle_2_h,
					montant_regle_22_c,
					montant_regle_22_h,
					date_expedition_0,
					date_expedition_1,
					date_expedition_12,
					date_expedition_2,
					date_expedition_22,
					delai_expedition_0,
					delai_expedition_1,
					delai_expedition_12,
					delai_expedition_2,
					delai_expedition_22,
					date_solde_0,
					date_solde_1,
					date_solde_12,
					date_solde_2,
					date_solde_22,
					delai_solde_0,
					delai_solde_1,
					delai_solde_12,
					delai_solde_2,
					delai_solde_22,
					ghs_id,
					ghm_id,
					gme_id,
					particularite_t2a,
					code_postal_id,
					mode_traitement_id,
					date_groupage,
					delai_groupage,
					nb_factures_rejet,
					etat,
					code_cp_demandee,
					ghs_bebe1_id,
					ghs_bebe2_id,
					ghs_bebe3_id,
					mode_entree,
					provenance,
					mode_sortie,
					destination,
					heure_entree,
					heure_sortie,
					code_original,
					montant_comptabilise_c,
					montant_comptabilise_h,
					montant_comptabilise_0_c,
					montant_comptabilise_0_h,
					montant_comptabilise_1_c,
					montant_comptabilise_1_h,
					montant_comptabilise_2_c,
					montant_comptabilise_2_h,
					montant_comptabilise_22_c,
					montant_comptabilise_22_h,
					montant_facture_c_actes_inclus_dans_sejour,
					montant_facture_h_actes_inclus_dans_sejour,
					est_budget_global,
					montant_comptabilise_budget_global_c,
					oid,
					provider_id,
					est_sans_facturation,
					type_pmsi,
                    risque_id,
                    forme_activite_id,
                    etat_sejour)
				SELECT
					COALESCE(w_correspondances_finess.code,'[FINESS]') AS finess,
					'[CPX]' || no_sejour,
					date_entree,
					date_sortie,
					substr(COALESCE(w_correspondances_patients.no_patient,p_sejours.no_patient),1,20) AS no_patient,
					code_sexe,
					age,
					code_sorti,
					code_prevu,
					type_sejour,
					date_facture,
					date_solde,
					delai_facture,
					delai_solde,
					montant_facture_c,
					montant_facture_h,
					montant_regle_c,
					montant_regle_h,
					nb_factures,
					nb_factures_regularisation,
					date_expedition,
					delai_expedition,
					CASE WHEN lieu_sortie_id <> 0 THEN lieu_sortie_id + [IPX] ELSE 0 END AS lieu_sortie_id,
					CASE WHEN medecin_sejour_id <> 0 THEN medecin_sejour_id + [IPX] ELSE 0 END AS medecin_sejour_id,
					CASE WHEN tiers_payant_1_id <> 0 THEN tiers_payant_1_id + [IPX] ELSE 0 END AS tiers_payant_1_id,
					CASE WHEN tiers_payant_2_id <> 0 THEN tiers_payant_2_id + [IPX] ELSE 0 END AS tiers_payant_2_id,
					CASE WHEN tiers_payant_0_id <> 0 THEN tiers_payant_0_id + [IPX] ELSE 0 END AS tiers_payant_0_id,
					CASE WHEN tiers_payant_22_id <> 0 THEN tiers_payant_22_id + [IPX] ELSE 0 END AS tiers_payant_22_id,
					montant_facture_0_c,
					montant_facture_0_h,
					montant_facture_1_c,
					montant_facture_1_h,
					montant_facture_12_c,
					montant_facture_12_h,
					montant_facture_2_c,
					montant_facture_2_h,
					montant_facture_22_c,
					montant_facture_22_h,
					montant_regle_0_c,
					montant_regle_0_h,
					montant_regle_1_c,
					montant_regle_1_h,
					montant_regle_12_c,
					montant_regle_12_h,
					montant_regle_2_c,
					montant_regle_2_h,
					montant_regle_22_c,
					montant_regle_22_h,
					date_expedition_0,
					date_expedition_1,
					date_expedition_12,
					date_expedition_2,
					date_expedition_22,
					delai_expedition_0,
					delai_expedition_1,
					delai_expedition_12,
					delai_expedition_2,
					delai_expedition_22,
					date_solde_0,
					date_solde_1,
					date_solde_12,
					date_solde_2,
					date_solde_22,
					delai_solde_0,
					delai_solde_1,
					delai_solde_12,
					delai_solde_2,
					delai_solde_22,
					COALESCE(w_correspondances_ghs.oid,0) AS ghs_id,
					COALESCE(w_correspondances_ghm.oid,0) AS ghm_id,
					COALESCE(w_correspondances_gme.oid,0) AS gme_id,
					particularite_t2a,
					COALESCE(w_correspondances_codes_postaux.oid,0) AS code_postal_id,
					COALESCE(w_correspondances_modes_traitement.oid,0) AS mode_traitement_id,
					date_groupage,
					delai_groupage,
					nb_factures_rejet,
					etat,
					code_cp_demandee,
					0 AS ghs_bebe1_id,
					0 AS ghs_bebe2_id,
					0 AS ghs_bebe3_id,
					mode_entree,
					provenance,
					mode_sortie,
					destination,
					heure_entree,
					heure_sortie,
					'[CPX]' || code_original,
					montant_comptabilise_c,
					montant_comptabilise_h,
					montant_comptabilise_0_c,
					montant_comptabilise_0_h,
					montant_comptabilise_1_c,
					montant_comptabilise_1_h,
					montant_comptabilise_2_c,
					montant_comptabilise_2_h,
					montant_comptabilise_22_c,
					montant_comptabilise_22_h,
					montant_facture_c_actes_inclus_dans_sejour,
					montant_facture_h_actes_inclus_dans_sejour,
					est_budget_global,
					montant_comptabilise_budget_global_c,
					CASE WHEN p_sejours.oid <> 0 THEN p_sejours.oid + [IPX] ELSE 0 END AS oid,
					provider_id,
					COALESCE(est_sans_facturation,'0'),
					type_pmsi,
                    COALESCE(w_correspondances_risque.oid, 0) AS risque_id,
                    COALESCE(w_correspondances_forme_activite.oid, 0) AS forme_activite_id,
                    etat_sejour
				FROM p_sejours
                    LEFT JOIN w_correspondances_finess ON p_sejours.finess = w_correspondances_finess.code_source
					LEFT JOIN w_correspondances_patients ON p_sejours.no_patient = w_correspondances_patients.no_patient_source
					LEFT JOIN w_correspondances_ghs ON ghs_id = w_correspondances_ghs.oid_source
					LEFT JOIN w_correspondances_ghm ON ghm_id = w_correspondances_ghm.oid_source
					LEFT JOIN w_correspondances_gme ON gme_id = w_correspondances_gme.oid_source
					LEFT JOIN w_correspondances_modes_traitement ON mode_traitement_id = w_correspondances_modes_traitement.oid_source
					LEFT JOIN w_correspondances_codes_postaux ON code_postal_id = w_correspondances_codes_postaux.oid_source
					LEFT JOIN w_correspondances_risque ON risque_id = w_correspondances_risque.oid_source
                    LEFT JOIN w_correspondances_forme_activite ON forme_activite_id = w_correspondances_forme_activite.oid_source
					;
				
				-- recreation des lien activite / pmsi
				DELETE FROM activite.p_sejour_pmsi WHERE no_sejour LIKE '[CPX]%';
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
						SELECT * FROM information_schema.tables WHERE  table_schema = ''pmsi'' AND table_name = ''v_rsf_total_1''
						', 'temp e_has_pmsi'
					);
				
				SELECT base.cti_execute('
					SELECT base.cti_copy_table(''dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]'', ''
						SELECT *
						FROM activite.p_sejour_pmsi 
						WHERE EXISTS (SELECT 1 FROM temp.w_sejours WHERE temp.w_sejours.oid = p_sejour_pmsi.sejour_id)
						limit 100000000000
						'', ''temp p_sejour_pmsi''
					).,

					CREATE INDEX p_sejour_pmsi_i1
					  ON p_sejour_pmsi
					  USING btree
					  (sejour_id).,
					  
					SELECT base.cti_copy_table(''dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]'', ''
						SELECT *
						FROM pmsi.p_rss
						limit 100000000000
						'', ''temp p_rss''
					).,
					
					CREATE INDEX p_rss_i1
					  ON p_rss
					  USING btree
					  (oid).,

					DROP TABLE IF EXISTS w_correspondances_sejour_pmsi.,
					CREATE TEMP TABLE w_correspondances_sejour_pmsi AS
					SELECT 
						p_sejours_source.oid AS oid_source,
						coalesce(p_sejours.oid,0) AS oid
					FROM p_sejours AS p_sejours_source 
						JOIN activite.p_sejours ON ''[CPX]'' || p_sejours_source.no_sejour = p_sejours.no_sejour.,

					CREATE INDEX w_correspondances_sejour_pmsi_1
					  ON w_correspondances_sejour_pmsi
					  USING btree
					  (oid_source).,

					DROP TABLE IF EXISTS w_correspondances_p_rss.,
					CREATE TEMP TABLE w_correspondances_p_rss AS
					SELECT 
						p_rss_source.oid AS oid_source,
						coalesce(p_rss.oid,0) AS oid
					FROM p_rss AS p_rss_source 
						JOIN pmsi.p_rss ON p_rss_source.no_rss = p_rss.no_rss AND p_rss.finess = p_rss_source.finess AND p_rss.date_sortie = p_rss_source.date_sortie.,
					CREATE INDEX w_correspondances_p_rss1
					  ON w_correspondances_p_rss
					  USING btree
					  (oid_source).,
					
					INSERT INTO activite.p_sejour_pmsi(no_rss, no_sejour,pmsi_type,rss_id,sejour_id)
					SELECT
					  p_rss.no_rss,
					  p_sejours.no_sejour,
					  pmsi_type,
					  p_rss.oid,
					p_sejours.oid
					FROM 
					p_sejour_pmsi
					JOIN w_correspondances_p_rss ON w_correspondances_p_rss.oid_source = p_sejour_pmsi.rss_id
					JOIN (SELECT oid, no_rss FROM pmsi.p_rss) p_rss ON w_correspondances_p_rss.oid = p_rss.oid
					JOIN w_correspondances_sejour_pmsi ON w_correspondances_sejour_pmsi.oid_source = p_sejour_pmsi.sejour_id
					JOIN activite.p_sejours ON w_correspondances_sejour_pmsi.oid = p_sejours.oid.,
					
					UPDATE pmsi.p_rss
					SET no_sejour_administratif = p_sejour_pmsi.no_sejour
					FROM activite.p_sejour_pmsi
					WHERE rss_id = p_rss.oid AND
						no_sejour_administratif IS DISTINCT FROM no_sejour',1)
					WHERE EXISTS (SELECT 1 FROM information_schema.tables WHERE  table_schema = 'pmsi' AND table_name = 'v_rsf_total_1')
					 AND EXISTS (SELECT 1 FROM e_has_pmsi WHERE  table_schema = 'pmsi' AND table_name = 'v_rsf_total_1');
				
                SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_sejour_effet_tarif 
					WHERE EXISTS (SELECT 1 FROM temp.w_sejours WHERE temp.w_sejours.oid = p_sejour_effet_tarif.sejour_id)
					limit 100000000000
					', 'temp p_sejour_effet_tarif'
				);

                INSERT INTO activite.p_sejour_effet_tarif(
					sejour_id,
					no_sejour,
					ok_ghs_f,
					coefficient_c,
					ok_ghs_c,
					tarif_ghs_c,
					tarif_exh_c,
					tarif_exb_c,
					forfait_exb_c,
					borne_basse_c,
					borne_haute_c,
					tarif_ghs_bb1_c,
					tarif_ghs_bb2_c,
					tarif_ghs_bb3_c,
					coefficient_p,
					ok_ghs_p,
					tarif_ghs_p,
					tarif_exh_p,
					tarif_exb_p,
					forfait_exb_p,
					borne_basse_p,
					borne_haute_p,
					tarif_ghs_bb1_p,
					tarif_ghs_bb2_p,
					tarif_ghs_bb3_p,
					montant_ghs_f,
					montant_exh_f,
					montant_tot_f,
					montant_ghs_c,
					montant_exh_c,
					montant_exb_c,
					montant_ghs_bb1_c,
					montant_ghs_bb2_c,
					montant_ghs_bb3_c,
					montant_tot_c,
					montant_ghs_p,
					montant_exh_p,
					montant_exb_p,
					montant_tot_p,
					montant_ghs_bb1_p,
					montant_ghs_bb2_p,
					montant_ghs_bb3_p
					)
				SELECT 
					CASE WHEN p_sejour_effet_tarif.sejour_id <> 0 THEN p_sejour_effet_tarif.sejour_id + [IPX] ELSE 0 END sejour_id,
					'[CPX]' || no_sejour,
					ok_ghs_f,
					coefficient_c,
					ok_ghs_c,
					tarif_ghs_c,
					tarif_exh_c,
					tarif_exb_c,
					forfait_exb_c,
					borne_basse_c,
					borne_haute_c,
					tarif_ghs_bb1_c,
					tarif_ghs_bb2_c,
					tarif_ghs_bb3_c,
					coefficient_p,
					ok_ghs_p,
					tarif_ghs_p,
					tarif_exh_p,
					tarif_exb_p,
					forfait_exb_p,
					borne_basse_p,
					borne_haute_p,
					tarif_ghs_bb1_p,
					tarif_ghs_bb2_p,
					tarif_ghs_bb3_p,
					montant_ghs_f,
					montant_exh_f,
					montant_tot_f,
					montant_ghs_c,
					montant_exh_c,
					montant_exb_c,
					montant_ghs_bb1_c,
					montant_ghs_bb2_c,
					montant_ghs_bb3_c,
					montant_tot_c,
					montant_ghs_p,
					montant_exh_p,
					montant_exb_p,
					montant_tot_p,
					montant_ghs_bb1_p,
					montant_ghs_bb2_p,
					montant_ghs_bb3_p
				FROM p_sejour_effet_tarif
				;
				
				-- TRANSFERTS --
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM base.t_etablissements WHERE EXISTS (SELECT 1 FROM activite.p_sejours_transferts WHERE provenance_id = base.t_etablissements.oid)
					OR EXISTS (SELECT 1 FROM activite.p_sejours_transferts WHERE destination_id = base.t_etablissements.oid)
					', 'temp e_etablissements'
				);

				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT p_sejours_transferts.*
					FROM activite.p_sejours_transferts
					WHERE EXISTS (SELECT 1 FROM temp.w_sejours WHERE oid = activite.p_sejours_transferts.sejour_id)
					', 'temp e_sejours_transferts'
				);

				INSERT INTO base.t_etablissements (code, texte, texte_court)
				SELECT code, texte, texte_court 
				FROM e_etablissements WHERE NOT EXISTS (SELECT 1 FROM base.t_etablissements WHERE code = e_etablissements.code)
				;
				
				
				DROP TABLE IF EXISTS c_etablissements;
				CREATE TEMP TABLE c_etablissements AS
				SELECT e_etablissements.oid AS oid_source, MAX(COALESCE(t_etablissements.oid,0)) AS oid
				FROM e_etablissements 
					JOIN base.t_etablissements ON t_etablissements.code = e_etablissements.code
				GROUP BY 1;
				
				
				INSERT INTO activite.p_sejours_transferts(
					sejour_id,
					provenance_id,
					destination_id
				)
				SELECT 
					sejour_id + [IPX],
					prov.oid,
					dest.oid
				FROM e_sejours_transferts
					LEFT JOIN c_etablissements prov ON provenance_id = prov.oid_source
					LEFT JOIN c_etablissements dest ON destination_id = dest.oid_source
				;
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_sejour_lieu 
					limit 100000000000
					', 'temp i_sejour_lieu'
				);

                INSERT INTO activite.p_sejour_lieu(
					sejour_id,
					no_sejour,
					lieu_id,
					pole_id
					)
				SELECT 
					CASE WHEN i_sejour_lieu.sejour_id <> 0 THEN i_sejour_lieu.sejour_id + [IPX] ELSE 0 END sejour_id,
					'[CPX]' || no_sejour,
					CASE WHEN i_sejour_lieu.lieu_id <> 0 THEN i_sejour_lieu.lieu_id + [IPX] ELSE 0 END lieu_id,
					COALESCE(w_correspondances_pole.oid, 0) AS pole_id
				FROM i_sejour_lieu
				LEFT JOIN w_correspondances_pole ON pole_id = w_correspondances_pole.oid_source
				;
							
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Mouvements">
			<sqlcmd><![CDATA[

				-- Récupération tables sources
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_mouvements_sejour
					WHERE EXISTS (SELECT 1 FROM temp.w_sejours WHERE temp.w_sejours.no_sejour = activite.p_mouvements_sejour.no_sejour)
					limit 100000000000
					', 'temp p_mouvements_sejour'
				);
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
				SELECT 
					t_lieux_c.oid,
					COALESCE(t_lieux.code_original_1,'''') AS code_original_1,
					COALESCE(t_lieux.code_original_2,'''') AS code_original_2,
					COALESCE(t_lieux.code_original_3,'''') AS code_original_3,
					COALESCE(t_lieux.code_original_4,'''') AS code_original_4,
					COALESCE(t_lieux.code_original_5,'''') AS code_original_5,
					COALESCE(t_lieux.code_original_6,'''') AS code_original_6,
					COALESCE(t_lieux.code_original_7,'''') AS code_original_7,
					COALESCE(t_lieux_c.service_facturation_id, t_lieux.service_facturation_id) AS service_facturation_id,
					COALESCE(t_lieux_c.activite_id, t_lieux.activite_id) AS activite_id,
					COALESCE(t_lieux_c.lit_id, t_lieux.lit_id) AS lit_id,
					COALESCE(t_lieux_c.unite_fonctionnelle_id, t_lieux.unite_fonctionnelle_id) AS unite_fonctionnelle_id,
					COALESCE(t_lieux_c.unite_medicale_id, t_lieux.unite_medicale_id) AS unite_medicale_id,
					COALESCE(t_lieux_c.mode_traitement_id, t_lieux.mode_traitement_id) AS mode_traitement_id,
                    COALESCE(t_lieux.gir_code_original,'''') AS gir_code_original,
                    COALESCE(t_lieux.gir_id,0) AS gir_id
				FROM activite.t_lieux_c
				LEFT JOIN activite.t_lieux ON t_lieux_c.oid = t_lieux.oid
					', 'temp t_lieux'
				);
				

				CREATE INDEX t_lieux_i2
				  ON t_lieux
				  USING btree
				  (mode_traitement_id);
				
				
				
				INSERT INTO activite.p_mouvements_sejour(
					finess,
					no_sejour,
					date,
					est_mouvement_previsionnel,
					nb_entrees_mutations,
					nb_jours_js_non_inclus,
					nb_sorties_mutations,
					lieu_id,
					nb_entrees_directes,
					nb_sorties_directes,
					nb_urgences,
					nb_externes,
					nb_ambulatoires,
					medecin_sejour_id,
					nb_jours_js_inclus,
					nb_seances,
                    nb_seances_dans_taux,
					nb_entrees_mutation_service,
					nb_sorties_mutation_service,
					nb_entrees_mutation_activite,
					nb_sorties_mutation_activite,
					nb_entrees_mutation_etage,
					nb_sorties_mutation_etage,
					nb_chambres_particulieres,
					heure_debut,
					heure_fin,
					est_jour_entree,
					est_jour_sortie,
					est_jour_hospitalisation,
					est_chambre_particuliere,
					est_chambre_particuliere_facturee,
					nb_entrees_mutation_unite_medicale,
					nb_sorties_mutation_unite_medicale,
					nb_bebes,
					sejour_id,
					jour_semaine,
					is_weekend,
					est_facture,
					nb_jours_d,
					nb_jours_f,
					nb_jours_12,
					nb_jours_prorata,
					nb_jours_cp_d,
					nb_jours_cp_f,
					nb_jours_cp_12,
					equivalent_malade_complet,
					nb_passage_externe,
					nb_deces,
					nb_seances_chimio,
					nb_seances_dialyse,
					nb_seances_autre,
					pole_id,
					nb_jour_sejour_sorti)
				SELECT
					'' AS finess,
					'[CPX]' || no_sejour,
					date,
					est_mouvement_previsionnel,
					nb_entrees_mutations,
					nb_jours_js_non_inclus,
					nb_sorties_mutations,
					CASE WHEN lieu_id <> 0 THEN lieu_id + [IPX] ELSE 0 END AS lieu_id,
					nb_entrees_directes,
					nb_sorties_directes,
					nb_urgences,
					nb_externes,
					nb_ambulatoires,
					CASE WHEN medecin_sejour_id <> 0 THEN medecin_sejour_id + [IPX] ELSE 0 END AS medecin_sejour_id,
					nb_jours_js_inclus,
					nb_seances,
                    nb_seances_dans_taux,
					nb_entrees_mutation_service,
					nb_sorties_mutation_service,
					nb_entrees_mutation_activite,
					nb_sorties_mutation_activite,
					nb_entrees_mutation_etage,
					nb_sorties_mutation_etage,
					nb_chambres_particulieres,
					heure_debut,
					heure_fin,
					est_jour_entree,
					est_jour_sortie,
					est_jour_hospitalisation,
					est_chambre_particuliere,
					est_chambre_particuliere_facturee,
					nb_entrees_mutation_unite_medicale,
					nb_sorties_mutation_unite_medicale,
					nb_bebes,
					CASE WHEN sejour_id <> 0 THEN sejour_id + [IPX] ELSE 0 END AS sejour_id,
					jour_semaine,
					is_weekend,
					est_facture,
					nb_jours_d,
					nb_jours_f,
					nb_jours_12,
					nb_jours_prorata,
					nb_jours_cp_d,
					nb_jours_cp_f,
					nb_jours_cp_12,
					equivalent_malade_complet,
					nb_passage_externe,
					nb_deces,
					nb_seances_chimio,
					nb_seances_dialyse,
					nb_seances_autre,
					COALESCE(w_correspondances_pole.oid, 0) AS pole_id,
					nb_jour_sejour_sorti
				FROM p_mouvements_sejour
				LEFT JOIN w_correspondances_pole ON pole_id = w_correspondances_pole.oid_source
				;
				
				
				DROP TABLE IF EXISTS w_lieux;
                CREATE TEMP TABLE w_lieux AS
                SELECT
					CASE WHEN t_lieux.oid <> 0 THEN  t_lieux.oid + [IPX] ELSE 0 END AS oid,
					CASE WHEN code_original_1 <> '' THEN '[TPX]' || code_original_1 ELSE '' END AS code_original_1,
					CASE WHEN code_original_2 <> '' THEN '[TPX]' || code_original_2 ELSE '' END AS code_original_2,
					CASE WHEN code_original_3 <> '' THEN '[TPX]' || code_original_3 ELSE '' END AS code_original_3,
					CASE WHEN code_original_4 <> '' THEN '[TPX]' || code_original_4 ELSE '' END AS code_original_4,
					CASE WHEN code_original_5 <> '' THEN '[TPX]' || code_original_5 ELSE '' END AS code_original_5,
					CASE WHEN service_facturation_id <> 0 THEN service_facturation_id + [IPX] ELSE 0 END AS service_facturation_id,
					CASE WHEN activite_id <> 0 THEN activite_id + [IPX] ELSE 0 END AS activite_id,
					CASE WHEN lit_id <> 0 THEN lit_id + [IPX] ELSE 0 END AS lit_id,
					CASE WHEN unite_medicale_id <> 0 THEN unite_medicale_id + [IPX] ELSE 0 END AS unite_medicale_id,
					CASE WHEN unite_fonctionnelle_id <> 0 THEN unite_fonctionnelle_id + [IPX] ELSE 0 END AS unite_fonctionnelle_id,
					CASE WHEN code_original_6 <> '' THEN '[TPX]' || code_original_6 ELSE '' END AS code_original_6,
					CASE WHEN code_original_7 <> '' THEN '[TPX]' || code_original_7 ELSE '' END AS code_original_7,
					COALESCE(w_correspondances_modes_traitement.oid,0) AS mode_traitement_id,
                    COALESCE(gir_code_original,'') AS gir_code_original,
                    COALESCE(gir_id,0) AS gir_id
				FROM t_lieux
					LEFT JOIN w_correspondances_modes_traitement ON mode_traitement_id = w_correspondances_modes_traitement.oid_source
                    ;
				
				INSERT INTO activite.t_lieux(
					oid,
					code_original_1,
					code_original_2,
					code_original_3,
					code_original_4,
					code_original_5,
					service_facturation_id,
					activite_id,
					lit_id,
					unite_medicale_id,
					unite_fonctionnelle_id,
					code_original_6,
					code_original_7,
					mode_traitement_id,
                    gir_code_original,
                    gir_id)
				SELECT
					oid,
					code_original_1,
					code_original_2,
					code_original_3,
					code_original_4,
					code_original_5,
					service_facturation_id,
					activite_id,
					lit_id,
					unite_medicale_id,
					unite_fonctionnelle_id,
					code_original_6,
					code_original_7,
					mode_traitement_id,
                    gir_code_original,
                    gir_id
				FROM w_lieux
				WHERE NOT EXISTS (SELECT 1 FROM activite.t_lieux WHERE oid = CASE WHEN w_lieux.oid <> 0 THEN w_lieux.oid ELSE 0 END)
				;						
							
				UPDATE activite.t_lieux
				SET
					code_original_1 = w_lieux.code_original_1,
					code_original_2 = w_lieux.code_original_2,
					code_original_3 = w_lieux.code_original_3,
					code_original_4 = w_lieux.code_original_4,
					code_original_5 = w_lieux.code_original_5,
					service_facturation_id = w_lieux.service_facturation_id,
					activite_id = w_lieux.activite_id,
					lit_id = w_lieux.lit_id,
					unite_medicale_id = w_lieux.unite_medicale_id,
					unite_fonctionnelle_id = w_lieux.unite_fonctionnelle_id,
					code_original_6 = w_lieux.code_original_6,
					code_original_7 = w_lieux.code_original_7,
					mode_traitement_id = w_lieux.mode_traitement_id,
                    gir_code_original = w_lieux.gir_code_original,
                    gir_id = w_lieux.gir_id
				FROM w_lieux
				WHERE 
                    t_lieux.oid = w_lieux.oid AND
                    (
                        t_lieux.code_original_1 != w_lieux.code_original_1 OR
                        t_lieux.code_original_2 != w_lieux.code_original_2 OR
                        t_lieux.code_original_3 != w_lieux.code_original_3 OR
                        t_lieux.code_original_4 != w_lieux.code_original_4 OR
                        t_lieux.code_original_5 != w_lieux.code_original_5 OR
                        t_lieux.service_facturation_id != w_lieux.service_facturation_id OR
                        t_lieux.activite_id != w_lieux.activite_id OR
                        t_lieux.lit_id != w_lieux.lit_id OR
                        t_lieux.unite_medicale_id != w_lieux.unite_medicale_id OR
                        t_lieux.unite_fonctionnelle_id != w_lieux.unite_fonctionnelle_id OR
                        t_lieux.code_original_6 != w_lieux.code_original_6 OR
                        t_lieux.code_original_7 != w_lieux.code_original_7 OR
                        t_lieux.mode_traitement_id != w_lieux.mode_traitement_id OR
                        t_lieux.gir_code_original != w_lieux.gir_code_original OR
                        t_lieux.gir_id != w_lieux.gir_id
                    )
					;						
				
                DELETE FROM activite.t_lieux WHERE oid BETWEEN [IPX] AND [IPX] + 1000000000000 
                    AND NOT EXISTS (SELECT 1 FROM w_lieux WHERE oid = activite.t_lieux.oid)
                    ;					
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Interventions">
			<sqlcmd><![CDATA[
			
				-- Récupération tables sources
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_intervention
					WHERE EXISTS (SELECT 1 FROM temp.w_sejours WHERE oid = activite.p_intervention.sejour_id)
					limit 100000000000
					', 'temp p_intervention'
				);
				
				CREATE INDEX p_intervention_i1
				  ON p_intervention
				  USING btree
				  (acte_id);

				INSERT INTO activite.p_intervention(
					sejour_id, 
					date_debut, 
					heure_debut, 
					salle_id, 
					medecin_chirurgien_id, 
					medecin_anesthesiste_id, 
					acte_id
				)
				SELECT 
					CASE WHEN sejour_id <> 0 THEN sejour_id + [IPX] ELSE 0 END AS sejour_id,
					date_debut, 
					heure_debut, 
					0::bigint AS salle_id, 
					CASE WHEN medecin_chirurgien_id <> 0 THEN medecin_chirurgien_id + [IPX] ELSE 0 END AS medecin_chirurgien_id, 
					CASE WHEN medecin_anesthesiste_id <> 0 THEN medecin_anesthesiste_id + [IPX] ELSE 0 END AS medecin_anesthesiste_id, 
					COALESCE(w_correspondances_actes.oid,0) AS acte_id
				FROM p_intervention
					LEFT JOIN w_correspondances_actes ON acte_id = w_correspondances_actes.oid_source;
										
			]]></sqlcmd>
		</NODE>
		
	</NODE>

	<NODE name="FACTURE" label="RECUPERATION DES FACTURES">
	
		<NODE label="Entêtes">
			<sqlcmd><![CDATA[
			
				-- Récupération tables sources
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_factures
					WHERE EXISTS (SELECT 1 FROM temp.w_factures WHERE temp.w_factures.no_facture = activite.p_factures.no_facture)
					limit 100000000000
					', 'temp p_factures'
				);
				
				CREATE INDEX p_factures_i1
				  ON p_factures
				  USING btree
				  (ghs_id);

				
				
			
								
				INSERT INTO activite.p_factures(
					finess,
					no_sejour,
					no_facture,
					date_debut,
					date_fin,
					type_facture,
					code_facture,
					date_facture,
					code_vente,
					mois_vente,
					date_solde,
					montant_facture_c,
					montant_facture_h,
					montant_regle_c,
					montant_regle_h,
					delai_facture,
					delai_solde,
					date_expedition,
					delai_expedition,
					tiers_payant_1_id,
					tiers_payant_2_id,
					tiers_payant_22_id,
					tiers_payant_0_id,
					date_solde_0_c,
					date_solde_0_h,
					date_solde_1_c,
					date_solde_1_h,
					date_solde_2_c,
					date_solde_2_h,
					date_solde_22_c,
					date_solde_22_h,
					date_expedition_0,
					date_expedition_1,
					date_expedition_2,
					date_expedition_22,
					code_expedie_0,
					code_expedie_1,
					code_expedie_2,
					code_expedie_22,
					no_bordereau_0,
					no_bordereau_1,
					no_bordereau_2,
					no_bordereau_22,
					montant_facture_0_c,
					montant_facture_0_h,
					montant_facture_1_c,
					montant_facture_1_h,
					montant_facture_2_c,
					montant_facture_2_h,
					montant_facture_22_c,
					montant_facture_22_h,
					montant_regle_0_c,
					montant_regle_0_h,
					montant_regle_1_c,
					montant_regle_1_h,
					montant_regle_2_c,
					montant_regle_2_h,
					montant_regle_22_c,
					montant_regle_22_h,
					delai_expedition_0,
					delai_expedition_1,
					delai_expedition_2,
					delai_expedition_22,
					delai_solde_0_c,
					delai_solde_0_h,
					delai_solde_1_c,
					delai_solde_1_h,
					delai_solde_2_c,
					delai_solde_2_h,
					delai_solde_22_c,
					delai_solde_22_h,
					ghs_id,
					particularite_t2a,
					date_vente,
					code_cloture,
					montant_encours_c,
					montant_encours_0_c,
					montant_encours_1_c,
					montant_encours_2_c,
					montant_encours_22_c,
					montant_encours_h,
					montant_encours_0_h,
					montant_encours_1_h,
					montant_encours_2_h,
					montant_encours_22_h,
					code_cp_demandee,
					date_encours,
					ghs_bebe1_id,
					ghs_bebe2_id,
					ghs_bebe3_id,
					nb_rejets,
					montant_comptabilise_c,
					montant_comptabilise_h,
					montant_comptabilise_0_c,
					montant_comptabilise_0_h,
					montant_comptabilise_1_c,
					montant_comptabilise_1_h,
					montant_comptabilise_2_c,
					montant_comptabilise_2_h,
					montant_comptabilise_22_c,
					montant_comptabilise_22_h,
					no_facture_reference,
					date_solde_reference,
					date_solde_reference_c,
					date_solde_reference_h,
					date_solde_reference_0_c,
					date_solde_reference_1_c,
					date_solde_reference_2_c,
					date_solde_reference_22_c,
					date_solde_reference_0_h,
					date_solde_reference_1_h,
					date_solde_reference_2_h,
					date_solde_reference_22_h,
					date_solde_c,
					date_solde_h,
					montant_facture_c_actes_inclus_dans_sejour,
					montant_facture_h_actes_inclus_dans_sejour,
					montant_comptabilise_budget_global_c,
					no_facture_od_avoir,
					oid,
					sejour_id,
					facture_reference_id,
					facture_od_avoir_id,
					provider_id,
					ghm_id)
				SELECT 
					'' AS finess,
					'[CPX]' || no_sejour AS no_sejour,
					'[CPX]' || no_facture AS no_facture,
					date_debut,
					date_fin,
					type_facture,
					code_facture,
					date_facture,
					code_vente,
					mois_vente,
					date_solde,
					montant_facture_c,
					montant_facture_h,
					montant_regle_c,
					montant_regle_h,
					delai_facture,
					delai_solde,
					date_expedition,
					delai_expedition,
					CASE WHEN tiers_payant_1_id <> 0 THEN tiers_payant_1_id + [IPX] ELSE 0 END AS tiers_payant_1_id,
					CASE WHEN tiers_payant_2_id <> 0 THEN tiers_payant_2_id + [IPX] ELSE 0 END AS tiers_payant_2_id,
					CASE WHEN tiers_payant_22_id <> 0 THEN tiers_payant_22_id + [IPX] ELSE 0 END AS tiers_payant_22_id,
					CASE WHEN tiers_payant_0_id <> 0 THEN tiers_payant_0_id + [IPX] ELSE 0 END AS tiers_payant_0_id,
					date_solde_0_c,
					date_solde_0_h,
					date_solde_1_c,
					date_solde_1_h,
					date_solde_2_c,
					date_solde_2_h,
					date_solde_22_c,
					date_solde_22_h,
					date_expedition_0,
					date_expedition_1,
					date_expedition_2,
					date_expedition_22,
					code_expedie_0,
					code_expedie_1,
					code_expedie_2,
					code_expedie_22,
					no_bordereau_0,
					no_bordereau_1,
					no_bordereau_2,
					no_bordereau_22,
					montant_facture_0_c,
					montant_facture_0_h,
					montant_facture_1_c,
					montant_facture_1_h,
					montant_facture_2_c,
					montant_facture_2_h,
					montant_facture_22_c,
					montant_facture_22_h,
					montant_regle_0_c,
					montant_regle_0_h,
					montant_regle_1_c,
					montant_regle_1_h,
					montant_regle_2_c,
					montant_regle_2_h,
					montant_regle_22_c,
					montant_regle_22_h,
					delai_expedition_0,
					delai_expedition_1,
					delai_expedition_2,
					delai_expedition_22,
					delai_solde_0_c,
					delai_solde_0_h,
					delai_solde_1_c,
					delai_solde_1_h,
					delai_solde_2_c,
					delai_solde_2_h,
					delai_solde_22_c,
					delai_solde_22_h,
					COALESCE(w_correspondances_ghs.oid,0) AS ghs_id,
					particularite_t2a,
					date_vente,
					code_cloture,
					montant_encours_c,
					montant_encours_0_c,
					montant_encours_1_c,
					montant_encours_2_c,
					montant_encours_22_c,
					montant_encours_h,
					montant_encours_0_h,
					montant_encours_1_h,
					montant_encours_2_h,
					montant_encours_22_h,
					code_cp_demandee,
					date_encours,
					0 AS ghs_bebe1_id,
					0 AS ghs_bebe2_id,
					0 AS ghs_bebe3_id,
					nb_rejets,
					montant_comptabilise_c,
					montant_comptabilise_h,
					montant_comptabilise_0_c,
					montant_comptabilise_0_h,
					montant_comptabilise_1_c,
					montant_comptabilise_1_h,
					montant_comptabilise_2_c,
					montant_comptabilise_2_h,
					montant_comptabilise_22_c,
					montant_comptabilise_22_h,
					CASE WHEN no_facture_reference <> '' THEN '[CPX]' || no_facture_reference ELSE '' END AS no_facture_reference,
					date_solde_reference,
					date_solde_reference_c,
					date_solde_reference_h,
					date_solde_reference_0_c,
					date_solde_reference_1_c,
					date_solde_reference_2_c,
					date_solde_reference_22_c,
					date_solde_reference_0_h,
					date_solde_reference_1_h,
					date_solde_reference_2_h,
					date_solde_reference_22_h,
					date_solde_c,
					date_solde_h,
					montant_facture_c_actes_inclus_dans_sejour,
					montant_facture_h_actes_inclus_dans_sejour,
					montant_comptabilise_budget_global_c,
					CASE WHEN no_facture_od_avoir <> '' THEN '[CPX]' || no_facture_od_avoir ELSE '' END AS no_facture_od_avoir,
					CASE WHEN p_factures.oid <> 0 THEN p_factures.oid + [IPX] ELSE 0 END AS oid,
					CASE WHEN sejour_id <> 0 THEN sejour_id + [IPX] ELSE 0 END AS sejour_id,
					CASE WHEN facture_reference_id <> 0 THEN facture_reference_id + [IPX] ELSE 0 END AS facture_reference_id,
					CASE WHEN facture_od_avoir_id <> 0 THEN facture_od_avoir_id + [IPX] ELSE 0 END AS facture_od_avoir_id,
					provider_id,
					COALESCE(w_correspondances_ghm.oid,0) AS ghm_id
				FROM p_factures
				LEFT JOIN w_correspondances_ghs ON ghs_id = w_correspondances_ghs.oid_source
				LEFT JOIN w_correspondances_ghm ON ghm_id = w_correspondances_ghm.oid_source
				;
				
				
				
				
				

				
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lignes non facturées Clinique">
			<sqlcmd><![CDATA[
			
				-- Récupération tables sources
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_factures_lignes_non_facturees_c 
					WHERE EXISTS (SELECT 1 FROM temp.w_factures WHERE temp.w_factures.no_facture = activite.p_factures_lignes_non_facturees_c.no_facture)
					limit 100000000000
					', 'temp p_factures_lignes_non_facturees_c'
				);
				
				CREATE INDEX p_factures_lignes_non_facturees_c_i1
				  ON p_factures_lignes_non_facturees_c
				  USING btree
				  (prestation_id);
				  
				

				INSERT INTO activite.p_factures_lignes_non_facturees_c(
					no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					coefficient,
					rubrique_facturation_id,
					prestation_id,
					prix_unitaire,
					lieu_id,
					facture_id,
					montant_encours)
				SELECT 
					'[CPX]' || no_facture AS no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					coefficient,
					CASE WHEN rubrique_facturation_id <> 0 THEN rubrique_facturation_id + [IPX] ELSE 0 END AS rubrique_facturation_id,
					COALESCE(w_correspondances_prestations.oid,0) AS prestation_id,
					prix_unitaire,
					CASE WHEN lieu_id <> 0 THEN lieu_id + [IPX] ELSE 0 END AS lieu_id,
					CASE WHEN facture_id <> 0 THEN facture_id + [IPX] ELSE 0 END AS facture_id,
					montant_encours
				FROM p_factures_lignes_non_facturees_c
				LEFT JOIN w_correspondances_prestations ON prestation_id = w_correspondances_prestations.oid_source;
										
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lignes factures Clinique">
			<sqlcmd><![CDATA[
			
				-- Récupération tables sources
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.t_rubrique_facture_c
					', 'temp t_rubrique_facture_c'
				);
				
				
				INSERT INTO activite.t_rubrique_facture_c(
					oid,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					prestation_id,
					compte_produit_id
					)
				SELECT
					t_rubrique_facture_c.oid + [IPX] AS oid,
					CASE WHEN rubrique_facturation_id <> 0 THEN rubrique_facturation_id + [IPX] ELSE 0 END AS rubrique_facturation_id,
					CASE WHEN rubrique_comptabilisation_id <> 0 THEN rubrique_comptabilisation_id + [IPX] ELSE 0 END AS rubrique_comptabilisation_id,
					COALESCE(w_correspondances_prestations.oid,0) AS prestation_id,
					COALESCE(w_correspondances_compte.oid,0) AS compte_produit_id
				FROM t_rubrique_facture_c
				LEFT JOIN w_correspondances_prestations ON prestation_id = w_correspondances_prestations.oid_source
				LEFT JOIN w_correspondances_compte ON compte_produit_id = w_correspondances_compte.oid_source
				WHERE NOT EXISTS (SELECT 1 FROM activite.t_rubrique_facture_c WHERE activite.t_rubrique_facture_c.oid = (t_rubrique_facture_c.oid + [IPX]));
				
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_factures_lignes_c 
					WHERE EXISTS (SELECT 1 FROM temp.w_factures WHERE temp.w_factures.no_facture = activite.p_factures_lignes_c.no_facture)
					limit 100000000000
					', 'temp p_factures_lignes_c'
				);
				
				CREATE INDEX p_factures_lignes_c_i1
				  ON p_factures_lignes_c
				  USING btree
				  (prestation_id);
				
							
				INSERT INTO activite.p_factures_lignes_c(
					no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					coefficient,
					coefficient_mco,
					rubrique_facture_id,
					rubrique_facturation_id,
					compte_produit_id,
					prestation_id,
					lpp_id,
					ucd_id,
					montant_facture,
					montant_facture_0,
					montant_facture_1,
					montant_facture_2,
					montant_facture_22,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					finess,
					prix_unitaire,
					lieu_id,
					nb_prestation,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					rubrique_comptabilisation_id,
					montant_encours,
					montant_encours_0,
					montant_encours_1,
					montant_encours_2,
					montant_encours_22,
					montant_facture_actes_inclus_dans_sejour,
					montant_facture_0_actes_inclus_dans_sejour,
					montant_facture_1_actes_inclus_dans_sejour,
					montant_facture_2_actes_inclus_dans_sejour,
					montant_facture_22_actes_inclus_dans_sejour,
					origine_facturation_id,
					montant_comptabilise_budget_global_1,
					facture_id,
					pole_id)
				SELECT
					'[CPX]' || no_facture AS no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					coefficient,
					coefficient_mco,
					CASE WHEN rubrique_facture_id <> 0 THEN rubrique_facture_id + [IPX] ELSE 0 END AS rubrique_facture_id,
					CASE WHEN rubrique_facturation_id <> 0 THEN rubrique_facturation_id + [IPX] ELSE 0 END AS rubrique_facturation_id,
					COALESCE(w_correspondances_compte.oid,0) AS compte_produit_id,
					COALESCE(w_correspondances_prestations.oid,0) AS prestation_id,
					COALESCE(w_correspondances_lpp.oid,0) AS lpp_id,
					COALESCE(w_correspondances_ucd.oid,0) AS ucd_id,
					montant_facture,
					montant_facture_0,
					montant_facture_1,
					montant_facture_2,
					montant_facture_22,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					finess,
					prix_unitaire,
					CASE WHEN lieu_id <> 0 THEN lieu_id + [IPX] ELSE 0 END AS lieu_id,
					nb_prestation,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					CASE WHEN rubrique_comptabilisation_id <> 0 THEN rubrique_comptabilisation_id + [IPX] ELSE 0 END AS rubrique_comptabilisation_id,
					montant_encours,
					montant_encours_0,
					montant_encours_1,
					montant_encours_2,
					montant_encours_22,
					montant_facture_actes_inclus_dans_sejour,
					montant_facture_0_actes_inclus_dans_sejour,
					montant_facture_1_actes_inclus_dans_sejour,
					montant_facture_2_actes_inclus_dans_sejour,
					montant_facture_22_actes_inclus_dans_sejour,
					origine_facturation_id,
					montant_comptabilise_budget_global_1,
					CASE WHEN facture_id <> 0 THEN facture_id + [IPX] ELSE 0 END AS facture_id,
					COALESCE(w_correspondances_pole.oid, 0) AS pole_id
				FROM p_factures_lignes_c
				LEFT JOIN w_correspondances_prestations ON prestation_id = w_correspondances_prestations.oid_source
				LEFT JOIN w_correspondances_compte ON compte_produit_id = w_correspondances_compte.oid_source
				LEFT JOIN w_correspondances_pole ON pole_id = w_correspondances_pole.oid_source
				LEFT JOIN w_correspondances_lpp ON lpp_id = w_correspondances_lpp.oid_source
				LEFT JOIN w_correspondances_ucd ON ucd_id = w_correspondances_ucd.oid_source
				;
				
				
										
			]]></sqlcmd>
		</NODE>
	
		<NODE label="Compte client Clinique">
			<sqlcmd><![CDATA[
				
				-- Récupération tables sources
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_factures_soldes_c 
					WHERE EXISTS (SELECT 1 FROM temp.w_factures WHERE temp.w_factures.no_facture = activite.p_factures_soldes_c.no_facture)
					limit 100000000000
					', 'temp p_factures_soldes_c'
				);
				
				CREATE INDEX p_factures_soldes_c_i1
				  ON p_factures_soldes_c
				  USING btree
				  (prestation_id);
				
				
				INSERT INTO activite.p_factures_soldes_c(
					no_facture,
					date_comptable,
					rubrique_comptabilisee_id,
					prestation_id,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					montant_regle,
					montant_regle_0,
					montant_regle_1,
					montant_regle_2,
					montant_regle_22,
					finess,
					od_avoir,
					facture_id)
				SELECT 
					'[CPX]' || no_facture AS no_facture,
					date_comptable,
					CASE WHEN rubrique_comptabilisee_id <> 0 THEN rubrique_comptabilisee_id + [IPX] ELSE 0 END AS rubrique_comptabilisee_id,
					COALESCE(w_correspondances_prestations.oid,0) AS prestation_id,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					montant_regle,
					montant_regle_0,
					montant_regle_1,
					montant_regle_2,
					montant_regle_22,
					finess,
					od_avoir,
					CASE WHEN facture_id <> 0 THEN facture_id + [IPX] ELSE 0 END AS facture_id
				FROM p_factures_soldes_c
				LEFT JOIN w_correspondances_prestations ON prestation_id = w_correspondances_prestations.oid_source;	

				
				-- Récupération tables sources
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_facture_solde_tiers_c 
					WHERE EXISTS (SELECT 1 FROM temp.w_factures WHERE temp.w_factures.no_facture = activite.p_facture_solde_tiers_c.no_facture)
					limit 100000000000
					', 'temp p_facture_solde_tiers_c'
				);
				
				CREATE INDEX p_facture_solde_tiers_c_i1
				  ON p_facture_solde_tiers_c
				  USING btree
				  (prestation_id);
				
				
				INSERT INTO activite.p_facture_solde_tiers_c(
					facture_id,
					no_facture,
					date_comptable,
					rubrique_comptabilisation_id,
					prestation_id,
					tiers_payant_id,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_regle,
					montant_regle_0,
					montant_regle_1,
					montant_regle_2,
					od_avoir
					)
				SELECT 
					CASE WHEN facture_id <> 0 THEN facture_id + [IPX] ELSE 0 END AS facture_id,
					'[CPX]' || no_facture AS no_facture,
					date_comptable,
					CASE WHEN rubrique_comptabilisation_id <> 0 THEN rubrique_comptabilisation_id + [IPX] ELSE 0 END AS rubrique_comptabilisation_id,
					COALESCE(w_correspondances_prestations.oid,0) AS prestation_id,
					CASE WHEN tiers_payant_id <> 0 THEN tiers_payant_id + [IPX] ELSE 0 END AS tiers_payant_id,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_regle,
					montant_regle_0,
					montant_regle_1,
					montant_regle_2,
					od_avoir
				FROM p_facture_solde_tiers_c
				LEFT JOIN w_correspondances_prestations ON prestation_id = w_correspondances_prestations.oid_source;	
			]]></sqlcmd>
		</NODE>
	
		<NODE label="Lignes non facturées Honoraires">
			<sqlcmd><![CDATA[
			
				-- Récupération tables sources
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_factures_lignes_non_facturees_h 
					WHERE EXISTS (SELECT 1 FROM temp.w_factures WHERE temp.w_factures.no_facture = activite.p_factures_lignes_non_facturees_h.no_facture)
					limit 100000000000
					', 'temp p_factures_lignes_non_facturees_h'
				);
				
				CREATE INDEX p_factures_lignes_non_facturees_h_i1
				  ON p_factures_lignes_non_facturees_h
				  USING btree
				  (prestation_id);
				  
				CREATE INDEX p_factures_lignes_non_facturees_h_i2
				  ON p_factures_lignes_non_facturees_h
				  USING btree
				  (acte_id);

				INSERT INTO activite.p_factures_lignes_non_facturees_h(
					no_facture,
					date_debut,
					date_fin,
					heure_debut,
					nb_rubrique,
					coefficient,
					medecin_facture_id,
					prestation_id,
					prix_unitaire,
					montant_depassement,
					lieu_id,
					acte_id,
					phase_ccam,
					activite_ccam,
					extension_ccam,
					modificateur_ccam_1,
					modificateur_ccam_2,
					modificateur_ccam_3,
					modificateur_ccam_4,
					facture_id)
				SELECT
					'[CPX]' || no_facture AS no_facture,
					date_debut,
					date_fin,
					heure_debut,
					nb_rubrique,
					coefficient,
					CASE WHEN medecin_facture_id <> 0 THEN medecin_facture_id + [IPX] ELSE 0 END AS medecin_facture_id,
					COALESCE(w_correspondances_prestations.oid,0) AS prestation_id,
					prix_unitaire,
					montant_depassement,
					CASE WHEN lieu_id <> 0 THEN lieu_id + [IPX] ELSE 0 END AS lieu_id,
					COALESCE(w_correspondances_actes.oid,0) AS acte_id,
					phase_ccam,
					activite_ccam,
					extension_ccam,
					modificateur_ccam_1,
					modificateur_ccam_2,
					modificateur_ccam_3,
					modificateur_ccam_4,
					CASE WHEN facture_id <> 0 THEN facture_id + [IPX] ELSE 0 END AS facture_id
				FROM p_factures_lignes_non_facturees_h
				LEFT JOIN w_correspondances_prestations ON prestation_id = w_correspondances_prestations.oid_source
				LEFT JOIN w_correspondances_actes ON acte_id = w_correspondances_actes.oid_source;
										
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lignes factures Honoraires">
			<sqlcmd><![CDATA[
			
				-- Récupération tables sources
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_factures_lignes_h 
					WHERE EXISTS (SELECT 1 FROM temp.w_factures WHERE temp.w_factures.no_facture = activite.p_factures_lignes_h.no_facture)
					limit 100000000000
					', 'temp p_factures_lignes_h'
				);
				
				CREATE INDEX p_factures_lignes_h_i1
				  ON p_factures_lignes_h
				  USING btree
				  (prestation_id);
				  
				CREATE INDEX p_factures_lignes_h_i2
				  ON p_factures_lignes_h
				  USING btree
				  (acte_id);
			
				INSERT INTO activite.p_factures_lignes_h(
					finess,
					no_facture,
					date_debut,
					date_fin,
					coefficient,
					coefficient_mco,
					prestation_id,
					nb_rubrique,
					nb_prestation,
					prix_unitaire,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					montant_facture,
					montant_facture_0,
					montant_facture_1,
					montant_facture_2,
					montant_facture_22,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					lieu_id,
					montant_depassement,
					acte_id,
					medecin_facture_id,
					medecin_comptabilise_id,
					heure_debut,
					phase_ccam,
					activite_ccam,
					extension_ccam,
					modificateur_ccam_1,
					modificateur_ccam_2,
					modificateur_ccam_3,
					modificateur_ccam_4,
					montant_encours,
					montant_encours_0,
					montant_encours_1,
					montant_encours_2,
					montant_encours_22,
					montant_facture_actes_inclus_dans_sejour,
					montant_facture_0_actes_inclus_dans_sejour,
					montant_facture_1_actes_inclus_dans_sejour,
					montant_facture_2_actes_inclus_dans_sejour,
					montant_facture_22_actes_inclus_dans_sejour,
					facture_id,
					protocole_id,
					montant_non_facture,
					pole_id)
				SELECT
					finess,
					'[CPX]' || no_facture AS no_facture,
					date_debut,
					date_fin,
					coefficient,
					coefficient_mco,
					COALESCE(w_correspondances_prestations.oid,0) AS prestation_id,
					nb_rubrique,
					nb_prestation,
					prix_unitaire,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					montant_facture,
					montant_facture_0,
					montant_facture_1,
					montant_facture_2,
					montant_facture_22,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					CASE WHEN lieu_id <> 0 THEN lieu_id + [IPX] ELSE 0 END AS lieu_id,
					montant_depassement,
					COALESCE(w_correspondances_actes.oid,0) AS acte_id,
					CASE WHEN medecin_facture_id <> 0 THEN medecin_facture_id + [IPX] ELSE 0 END AS medecin_facture_id,
					CASE WHEN medecin_comptabilise_id <> 0 THEN medecin_comptabilise_id + [IPX] ELSE 0 END AS medecin_comptabilise_id,
					heure_debut,
					phase_ccam,
					activite_ccam,
					extension_ccam,
					modificateur_ccam_1,
					modificateur_ccam_2,
					modificateur_ccam_3,
					modificateur_ccam_4,
					montant_encours,
					montant_encours_0,
					montant_encours_1,
					montant_encours_2,
					montant_encours_22,
					montant_facture_actes_inclus_dans_sejour,
					montant_facture_0_actes_inclus_dans_sejour,
					montant_facture_1_actes_inclus_dans_sejour,
					montant_facture_2_actes_inclus_dans_sejour,
					montant_facture_22_actes_inclus_dans_sejour,
					CASE WHEN facture_id <> 0 THEN facture_id + [IPX] ELSE 0 END AS facture_id,
					CASE WHEN protocole_id <> 0 THEN protocole_id + [IPX] ELSE 0 END AS protocole_id,
					montant_non_facture,
					COALESCE(w_correspondances_pole.oid, 0) AS pole_id
				FROM p_factures_lignes_h
				LEFT JOIN w_correspondances_prestations ON prestation_id = w_correspondances_prestations.oid_source
				LEFT JOIN w_correspondances_actes ON acte_id = w_correspondances_actes.oid_source
				LEFT JOIN w_correspondances_pole ON pole_id = w_correspondances_pole.oid_source
				;
				
				
										
										
			]]></sqlcmd>
		</NODE>
	
		<NODE label="Compte client Honoraires">
			<sqlcmd><![CDATA[
			
				-- Récupération tables sources
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_factures_soldes_h 
					WHERE EXISTS (SELECT 1 FROM temp.w_factures WHERE temp.w_factures.no_facture = activite.p_factures_soldes_h.no_facture)
					limit 100000000000
					', 'temp p_factures_soldes_h'
				);

				INSERT INTO activite.p_factures_soldes_h(
					no_facture,
					date_comptable,
					medecin_comptabilise_id,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					montant_regle,
					montant_regle_0,
					montant_regle_1,
					montant_regle_2,
					montant_regle_22,
					montant_solde,
					montant_solde_0,
					montant_solde_1,
					montant_solde_2,
					montant_solde_22,
					finess,
					od_avoir,
					facture_id)
				SELECT
					'[CPX]' || no_facture AS no_facture,
					date_comptable,
					CASE WHEN medecin_comptabilise_id <> 0 THEN medecin_comptabilise_id + [IPX] ELSE 0 END AS medecin_comptabilise_id,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					montant_regle,
					montant_regle_0,
					montant_regle_1,
					montant_regle_2,
					montant_regle_22,
					montant_solde,
					montant_solde_0,
					montant_solde_1,
					montant_solde_2,
					montant_solde_22,
					finess,
					od_avoir,
					CASE WHEN facture_id <> 0 THEN facture_id + [IPX] ELSE 0 END AS facture_id
				FROM p_factures_soldes_h;
				
				
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="En-cours">
			<sqlcmd><![CDATA[
			
				-- Récupération tables sources
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_factures_encours 
					WHERE EXISTS (SELECT 1 FROM temp.w_sejours WHERE temp.w_sejours.no_sejour = activite.p_factures_encours.no_sejour)
					limit 100000000000
					', 'temp p_factures_encours'
				);
				
				CREATE INDEX p_factures_encours_i1
				  ON p_factures_encours
				  USING btree
				  (ghs_id);
				  
				  
				INSERT INTO activite.p_factures_encours (
					oid,
					sejour_id,
					no_sejour,
					no_facture,
					no_facture_reference,
					date_debut,
					date_fin,
					date_encours,
					code_origine,
					code_sorti,
					tiers_payant_1_id,
					tiers_payant_2_id,
					tiers_payant_22_id,
					tiers_payant_0_id,
					ghs_id,
					ghs_bebe1_id,
					ghs_bebe2_id,
					ghs_bebe3_id,
					code_cp_demandee,
					montant_encours_c,
					montant_encours_0_c,
					montant_encours_1_c,
					montant_encours_2_c,
					montant_encours_22_c,
					montant_encours_h,
					montant_encours_0_h,
					montant_encours_1_h,
					montant_encours_2_h,
					montant_encours_22_h,
					date_entree,
					heure_entree,
					date_sortie,
					heure_sortie,
					type_sejour,
					lieu_sortie_id,
					date_groupage,
					provider_id,
					ghm_id
					)
				SELECT 
					CASE WHEN p_factures_encours.oid <> 0 THEN p_factures_encours.oid + [IPX] ELSE 0 END AS oid,
					CASE WHEN sejour_id <> 0 THEN sejour_id + [IPX] ELSE 0 END AS sejour_id,
					'[CPX]' || no_sejour AS no_sejour,
					'[CPX]' || no_facture AS no_facture,
					CASE WHEN no_facture_reference <> '' THEN '[CPX]' || no_facture_reference ELSE '' END AS no_facture_reference,
					date_debut,
					date_fin,
					date_encours,
					code_origine,
					code_sorti,
					CASE WHEN tiers_payant_1_id <> 0 THEN tiers_payant_1_id + [IPX] ELSE 0 END AS tiers_payant_1_id,
					CASE WHEN tiers_payant_2_id <> 0 THEN tiers_payant_2_id + [IPX] ELSE 0 END AS tiers_payant_2_id,
					CASE WHEN tiers_payant_22_id <> 0 THEN tiers_payant_22_id + [IPX] ELSE 0 END AS tiers_payant_22_id,
					CASE WHEN tiers_payant_0_id <> 0 THEN tiers_payant_0_id + [IPX] ELSE 0 END AS tiers_payant_0_id,
					COALESCE(w_correspondances_ghs.oid,0) AS ghs_id,
					0 AS ghs_bebe1_id,
					0 AS ghs_bebe2_id,
					0 AS ghs_bebe3_id,
					code_cp_demandee,
					montant_encours_c,
					montant_encours_0_c,
					montant_encours_1_c,
					montant_encours_2_c,
					montant_encours_22_c,
					montant_encours_h,
					montant_encours_0_h,
					montant_encours_1_h,
					montant_encours_2_h,
					montant_encours_22_h,
					date_entree,
					heure_entree,
					date_sortie,
					heure_sortie,
					type_sejour,
					CASE WHEN lieu_sortie_id <> 0 THEN lieu_sortie_id + [IPX] ELSE 0 END AS lieu_sortie_id,
					date_groupage,
					provider_id,
					COALESCE(w_correspondances_ghm.oid,0) AS ghm_id
				FROM p_factures_encours
					LEFT JOIN w_correspondances_ghs ON ghs_id = w_correspondances_ghs.oid_source
					LEFT JOIN w_correspondances_ghm ON ghm_id = w_correspondances_ghm.oid_source
				;
					
				-- Récupération tables sources
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_factures_encours_lignes_c 
					WHERE EXISTS (SELECT 1 FROM temp.w_sejours JOIN activite.p_factures_encours ON w_sejours.no_sejour = p_factures_encours.no_sejour WHERE p_factures_encours.no_facture = activite.p_factures_encours_lignes_c.no_facture)
					limit 100000000000
					', 'temp p_factures_encours_lignes_c'
				);
				
				CREATE INDEX p_factures_encours_lignes_c_i1
				  ON p_factures_encours_lignes_c
				  USING btree
				  (prestation_id);
				  

				
				
				
				INSERT INTO activite.p_factures_encours_lignes_c(
					facture_id,
					no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					coefficient,
					coefficient_mco,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					compte_produit_id,
					prestation_id,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					prix_unitaire,
					lieu_id,
					nb_prestation,
					montant_encours,
					montant_encours_0,
					montant_encours_1,
					montant_encours_2,
					montant_encours_22,
					pole_id
				)
				SELECT
					CASE WHEN facture_id <> 0 THEN facture_id + [IPX] ELSE 0 END AS facture_id,
					'[CPX]' || no_facture AS no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					coefficient,
					coefficient_mco,
					CASE WHEN rubrique_facturation_id <> 0 THEN rubrique_facturation_id + [IPX] ELSE 0 END AS rubrique_facturation_id,
					CASE WHEN rubrique_comptabilisation_id <> 0 THEN rubrique_comptabilisation_id + [IPX] ELSE 0 END AS rubrique_comptabilisation_id,
					COALESCE(w_correspondances_compte.oid,0) AS compte_produit_id,
					COALESCE(w_correspondances_prestations.oid,0) AS prestation_id,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					prix_unitaire,
					CASE WHEN lieu_id <> 0 THEN lieu_id + [IPX] ELSE 0 END AS lieu_id,
					nb_prestation,
					montant_encours,
					montant_encours_0,
					montant_encours_1,
					montant_encours_2,
					montant_encours_22,
					COALESCE(w_correspondances_pole.oid, 0) AS pole_id
				FROM p_factures_encours_lignes_c
					LEFT JOIN w_correspondances_prestations ON prestation_id = w_correspondances_prestations.oid_source
					LEFT JOIN w_correspondances_compte ON compte_produit_id = w_correspondances_compte.oid_source
					LEFT JOIN w_correspondances_pole ON pole_id = w_correspondances_pole.oid_source;
				
				
				
				
				
										
			]]></sqlcmd>
		</NODE>
		
		
		<NODE label="Factures de référence">
			<sqlcmd><![CDATA[
			
				-- Récupération tables sources
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_factures_reference 
					WHERE EXISTS (SELECT 1 FROM temp.w_sejours WHERE temp.w_sejours.no_sejour = activite.p_factures_reference.no_sejour)
					limit 100000000000
					', 'temp p_factures_reference'
				);
				
				CREATE INDEX p_factures_reference_i1
				  ON p_factures_reference
				  USING btree
				  (ghs_id);
				  
				  
				INSERT INTO activite.p_factures_reference(
					sejour_id,
					no_sejour,
					date_entree,
					date_sortie,
					code_sorti,
					facture_reference_id,
					no_facture_reference,
					type_t2a,
					date_debut_facture,
					date_fin_facture,
					type_facture,
					date_groupage,
					retard_groupage,
					delai_groupage,
					nb_groupe,
					nb_non_groupe,
					nb_calculable,
					ghs_id,
					ghs_bebe1_id,
					ghs_bebe2_id,
					ghs_bebe3_id,
					tiers_payant_0_id,
					tiers_payant_1_id,
					tiers_payant_2_id,
					tiers_payant_22_id,
					code_facture,
					date_facture,
					retard_facture,
					delai_facture,
					code_vente,
					date_vente,
					nb_factures,
					nb_non_calcules,
					nb_factures_regularisation,
					nb_rejets,
					code_expedie_0,
					code_expedie_1,
					code_expedie_2,
					date_expedition,
					date_expedition_0,
					date_expedition_1,
					date_expedition_2,
					no_bordereau_0,
					no_bordereau_1,
					no_bordereau_2,
					delai_expedition,
					delai_expedition_0,
					delai_expedition_1,
					delai_expedition_2,
					nb_non_expedie,
					nb_non_expedie_c,
					nb_non_expedie_h,
					nb_non_expedie_0,
					nb_non_expedie_0_c,
					nb_non_expedie_0_h,
					nb_non_expedie_1,
					nb_non_expedie_1_c,
					nb_non_expedie_1_h,
					nb_non_expedie_2,
					nb_non_expedie_2_c,
					nb_non_expedie_2_h,
					montant_facture,
					montant_facture_c,
					montant_facture_h,
					montant_facture_0,
					montant_facture_0_c,
					montant_facture_0_h,
					montant_facture_1,
					montant_facture_1_c,
					montant_facture_1_h,
					montant_facture_2,
					montant_facture_2_c,
					montant_facture_2_h,
					montant_facture_c_actes_inclus_dans_sejour,
					montant_facture_h_actes_inclus_dans_sejour,
					montant_encours,
					montant_encours_c,
					montant_encours_h,
					montant_encours_0,
					montant_encours_0_c,
					montant_encours_0_h,
					montant_encours_1,
					montant_encours_1_c,
					montant_encours_1_h,
					montant_encours_2,
					montant_encours_2_c,
					montant_encours_2_h,
					montant_comptabilise,
					montant_comptabilise_c,
					montant_comptabilise_h,
					montant_comptabilise_0,
					montant_comptabilise_0_c,
					montant_comptabilise_0_h,
					montant_comptabilise_1,
					montant_comptabilise_1_c,
					montant_comptabilise_1_h,
					montant_comptabilise_2,
					montant_comptabilise_2_c,
					montant_comptabilise_2_h,
					montant_comptabilise_budget_global_c,
					montant_regle,
					montant_regle_c,
					montant_regle_h,
					montant_regle_0,
					montant_regle_0_c,
					montant_regle_0_h,
					montant_regle_1,
					montant_regle_1_c,
					montant_regle_1_h,
					montant_regle_2,
					montant_regle_2_c,
					montant_regle_2_h,
					date_solde,
					date_solde_c,
					date_solde_h,
					date_solde_0,
					date_solde_0_c,
					date_solde_0_h,
					date_solde_1,
					date_solde_1_c,
					date_solde_1_h,
					date_solde_2,
					date_solde_2_c,
					date_solde_2_h,
					delai_solde,
					delai_solde_c,
					delai_solde_h,
					delai_solde_0,
					delai_solde_0_c,
					delai_solde_0_h,
					delai_solde_1,
					delai_solde_1_c,
					delai_solde_1_h,
					delai_solde_2,
					delai_solde_2_c,
					delai_solde_2_h,
					nb_non_solde,
					nb_non_solde_c,
					nb_non_solde_h,
					nb_non_solde_0,
					nb_non_solde_0_c,
					nb_non_solde_0_h,
					nb_non_solde_1,
					nb_non_solde_1_c,
					nb_non_solde_1_h,
					nb_non_solde_2,
					nb_non_solde_2_c,
					nb_non_solde_2_h,
					nb_non_cloture,
					nb_non_cloture_c,
					nb_non_cloture_h,
					nb_non_cloture_0,
					nb_non_cloture_0_c,
					nb_non_cloture_0_h,
					nb_non_cloture_1,
					nb_non_cloture_1_c,
					nb_non_cloture_1_h,
					nb_non_cloture_2,
					nb_non_cloture_2_c,
					nb_non_cloture_2_h,
					pole_sortie_id
				)
				SELECT 
					CASE WHEN sejour_id <> 0 THEN sejour_id + [IPX] ELSE 0 END AS sejour_id,
					'[CPX]' || no_sejour AS no_sejour,
					date_entree,
					date_sortie,
					code_sorti,
					CASE WHEN facture_reference_id <> 0 THEN facture_reference_id + [IPX] ELSE 0 END AS facture_reference_id,
					'[CPX]' || no_facture_reference AS no_facture_reference,
					type_t2a,
					date_debut_facture,
					date_fin_facture,
					type_facture,
					date_groupage,
					retard_groupage,
					delai_groupage,
					nb_groupe,
					nb_non_groupe,
					nb_calculable,
					COALESCE(w_correspondances_ghs.oid,0) AS ghs_id,
					0 AS ghs_bebe1_id,
					0 AS ghs_bebe2_id,
					0 AS ghs_bebe3_id,
					CASE WHEN tiers_payant_0_id <> 0 THEN tiers_payant_0_id + [IPX] ELSE 0 END AS tiers_payant_0_id,
					CASE WHEN tiers_payant_1_id <> 0 THEN tiers_payant_1_id + [IPX] ELSE 0 END AS tiers_payant_1_id,
					CASE WHEN tiers_payant_2_id <> 0 THEN tiers_payant_2_id + [IPX] ELSE 0 END AS tiers_payant_2_id,
					CASE WHEN tiers_payant_22_id <> 0 THEN tiers_payant_22_id + [IPX] ELSE 0 END AS tiers_payant_22_id,
					code_facture,
					date_facture,
					retard_facture,
					delai_facture,
					code_vente,
					date_vente,
					nb_factures,
					nb_non_calcules,
					nb_factures_regularisation,
					nb_rejets,
					code_expedie_0,
					code_expedie_1,
					code_expedie_2,
					date_expedition,
					date_expedition_0,
					date_expedition_1,
					date_expedition_2,
					no_bordereau_0,
					no_bordereau_1,
					no_bordereau_2,
					delai_expedition,
					delai_expedition_0,
					delai_expedition_1,
					delai_expedition_2,
					nb_non_expedie,
					nb_non_expedie_c,
					nb_non_expedie_h,
					nb_non_expedie_0,
					nb_non_expedie_0_c,
					nb_non_expedie_0_h,
					nb_non_expedie_1,
					nb_non_expedie_1_c,
					nb_non_expedie_1_h,
					nb_non_expedie_2,
					nb_non_expedie_2_c,
					nb_non_expedie_2_h,
					montant_facture,
					montant_facture_c,
					montant_facture_h,
					montant_facture_0,
					montant_facture_0_c,
					montant_facture_0_h,
					montant_facture_1,
					montant_facture_1_c,
					montant_facture_1_h,
					montant_facture_2,
					montant_facture_2_c,
					montant_facture_2_h,
					montant_facture_c_actes_inclus_dans_sejour,
					montant_facture_h_actes_inclus_dans_sejour,
					montant_encours,
					montant_encours_c,
					montant_encours_h,
					montant_encours_0,
					montant_encours_0_c,
					montant_encours_0_h,
					montant_encours_1,
					montant_encours_1_c,
					montant_encours_1_h,
					montant_encours_2,
					montant_encours_2_c,
					montant_encours_2_h,
					montant_comptabilise,
					montant_comptabilise_c,
					montant_comptabilise_h,
					montant_comptabilise_0,
					montant_comptabilise_0_c,
					montant_comptabilise_0_h,
					montant_comptabilise_1,
					montant_comptabilise_1_c,
					montant_comptabilise_1_h,
					montant_comptabilise_2,
					montant_comptabilise_2_c,
					montant_comptabilise_2_h,
					montant_comptabilise_budget_global_c,
					montant_regle,
					montant_regle_c,
					montant_regle_h,
					montant_regle_0,
					montant_regle_0_c,
					montant_regle_0_h,
					montant_regle_1,
					montant_regle_1_c,
					montant_regle_1_h,
					montant_regle_2,
					montant_regle_2_c,
					montant_regle_2_h,
					date_solde,
					date_solde_c,
					date_solde_h,
					date_solde_0,
					date_solde_0_c,
					date_solde_0_h,
					date_solde_1,
					date_solde_1_c,
					date_solde_1_h,
					date_solde_2,
					date_solde_2_c,
					date_solde_2_h,
					delai_solde,
					delai_solde_c,
					delai_solde_h,
					delai_solde_0,
					delai_solde_0_c,
					delai_solde_0_h,
					delai_solde_1,
					delai_solde_1_c,
					delai_solde_1_h,
					delai_solde_2,
					delai_solde_2_c,
					delai_solde_2_h,
					nb_non_solde,
					nb_non_solde_c,
					nb_non_solde_h,
					nb_non_solde_0,
					nb_non_solde_0_c,
					nb_non_solde_0_h,
					nb_non_solde_1,
					nb_non_solde_1_c,
					nb_non_solde_1_h,
					nb_non_solde_2,
					nb_non_solde_2_c,
					nb_non_solde_2_h,
					nb_non_cloture,
					nb_non_cloture_c,
					nb_non_cloture_h,
					nb_non_cloture_0,
					nb_non_cloture_0_c,
					nb_non_cloture_0_h,
					nb_non_cloture_1,
					nb_non_cloture_1_c,
					nb_non_cloture_1_h,
					nb_non_cloture_2,
					nb_non_cloture_2_c,
					nb_non_cloture_2_h,
					COALESCE(w_correspondances_pole.oid, 0) AS pole_sortie_id
				FROM p_factures_reference
					LEFT JOIN w_correspondances_ghs ON ghs_id = w_correspondances_ghs.oid_source
					LEFT JOIN w_correspondances_pole ON pole_sortie_id = w_correspondances_pole.oid_source;
				
				
				
				-- Récupération tables sources (detail tiers)
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_factures_reference_tiers 
					WHERE EXISTS (SELECT 1 FROM temp.w_sejours WHERE temp.w_sejours.no_sejour = activite.p_factures_reference_tiers.no_sejour)
					limit 100000000000
					', 'temp p_factures_reference_tiers'
				);
				
				CREATE INDEX p_factures_reference_tiers_i1
				  ON p_factures_reference_tiers
				  USING btree
				  (ghs_id);
				  
				  
				INSERT INTO activite.p_factures_reference_tiers(
					sejour_id,
					no_sejour,
					date_entree,
					date_sortie,
					code_sorti,
					facture_reference_id,
					no_facture_reference,
					type_t2a,
					date_debut_facture,
					date_fin_facture,
					type_facture,
					date_groupage,
					retard_groupage,
					delai_groupage,
					nb_groupe,
					nb_non_groupe,
					nb_calculable,
					ghs_id,
					ghs_bebe1_id,
					ghs_bebe2_id,
					ghs_bebe3_id,
					tiers_payant_id,
					code_facture,
					date_facture,
					retard_facture,
					delai_facture,
					code_vente,
					date_vente,
					nb_factures,
					nb_non_calcules,
					nb_factures_regularisation,
					nb_rejets,
					code_expedie_0,
					code_expedie_1,
					code_expedie_2,
					date_expedition,
					date_expedition_0,
					date_expedition_1,
					date_expedition_2,
					no_bordereau_0,
					no_bordereau_1,
					no_bordereau_2,
					delai_expedition,
					delai_expedition_0,
					delai_expedition_1,
					delai_expedition_2,
					nb_non_expedie,
					nb_non_expedie_c,
					nb_non_expedie_h,
					nb_non_expedie_0,
					nb_non_expedie_0_c,
					nb_non_expedie_0_h,
					nb_non_expedie_1,
					nb_non_expedie_1_c,
					nb_non_expedie_1_h,
					nb_non_expedie_2,
					nb_non_expedie_2_c,
					nb_non_expedie_2_h,
					montant_facture,
					montant_facture_c,
					montant_facture_h,
					montant_facture_0,
					montant_facture_0_c,
					montant_facture_0_h,
					montant_facture_1,
					montant_facture_1_c,
					montant_facture_1_h,
					montant_facture_2,
					montant_facture_2_c,
					montant_facture_2_h,
					montant_facture_c_actes_inclus_dans_sejour,
					montant_facture_h_actes_inclus_dans_sejour,
					montant_encours,
					montant_encours_c,
					montant_encours_h,
					montant_encours_0,
					montant_encours_0_c,
					montant_encours_0_h,
					montant_encours_1,
					montant_encours_1_c,
					montant_encours_1_h,
					montant_encours_2,
					montant_encours_2_c,
					montant_encours_2_h,
					montant_comptabilise,
					montant_comptabilise_c,
					montant_comptabilise_h,
					montant_comptabilise_0,
					montant_comptabilise_0_c,
					montant_comptabilise_0_h,
					montant_comptabilise_1,
					montant_comptabilise_1_c,
					montant_comptabilise_1_h,
					montant_comptabilise_2,
					montant_comptabilise_2_c,
					montant_comptabilise_2_h,
					montant_comptabilise_budget_global_c,
					montant_regle,
					montant_regle_c,
					montant_regle_h,
					montant_regle_0,
					montant_regle_0_c,
					montant_regle_0_h,
					montant_regle_1,
					montant_regle_1_c,
					montant_regle_1_h,
					montant_regle_2,
					montant_regle_2_c,
					montant_regle_2_h,
					date_solde,
					date_solde_c,
					date_solde_h,
					date_solde_0,
					date_solde_0_c,
					date_solde_0_h,
					date_solde_1,
					date_solde_1_c,
					date_solde_1_h,
					date_solde_2,
					date_solde_2_c,
					date_solde_2_h,
					delai_solde,
					delai_solde_c,
					delai_solde_h,
					delai_solde_0,
					delai_solde_0_c,
					delai_solde_0_h,
					delai_solde_1,
					delai_solde_1_c,
					delai_solde_1_h,
					delai_solde_2,
					delai_solde_2_c,
					delai_solde_2_h,
					nb_non_solde,
					nb_non_solde_c,
					nb_non_solde_h,
					nb_non_solde_0,
					nb_non_solde_0_c,
					nb_non_solde_0_h,
					nb_non_solde_1,
					nb_non_solde_1_c,
					nb_non_solde_1_h,
					nb_non_solde_2,
					nb_non_solde_2_c,
					nb_non_solde_2_h,
					nb_non_cloture,
					nb_non_cloture_c,
					nb_non_cloture_h,
					nb_non_cloture_0,
					nb_non_cloture_0_c,
					nb_non_cloture_0_h,
					nb_non_cloture_1,
					nb_non_cloture_1_c,
					nb_non_cloture_1_h,
					nb_non_cloture_2,
					nb_non_cloture_2_c,
					nb_non_cloture_2_h,
					pole_sortie_id
				)
				SELECT 
					CASE WHEN sejour_id <> 0 THEN sejour_id + [IPX] ELSE 0 END AS sejour_id,
					'[CPX]' || no_sejour AS no_sejour,
					date_entree,
					date_sortie,
					code_sorti,
					CASE WHEN facture_reference_id <> 0 THEN facture_reference_id + [IPX] ELSE 0 END AS facture_reference_id,
					'[CPX]' || no_facture_reference AS no_facture_reference,
					type_t2a,
					date_debut_facture,
					date_fin_facture,
					type_facture,
					date_groupage,
					retard_groupage,
					delai_groupage,
					nb_groupe,
					nb_non_groupe,
					nb_calculable,
					COALESCE(w_correspondances_ghs.oid,0) AS ghs_id,
					0 AS ghs_bebe1_id,
					0 AS ghs_bebe2_id,
					0 AS ghs_bebe3_id,
					CASE WHEN tiers_payant_id <> 0 THEN tiers_payant_id + [IPX] ELSE 0 END AS tiers_payant_id,
					code_facture,
					date_facture,
					retard_facture,
					delai_facture,
					code_vente,
					date_vente,
					nb_factures,
					nb_non_calcules,
					nb_factures_regularisation,
					nb_rejets,
					code_expedie_0,
					code_expedie_1,
					code_expedie_2,
					date_expedition,
					date_expedition_0,
					date_expedition_1,
					date_expedition_2,
					no_bordereau_0,
					no_bordereau_1,
					no_bordereau_2,
					delai_expedition,
					delai_expedition_0,
					delai_expedition_1,
					delai_expedition_2,
					nb_non_expedie,
					nb_non_expedie_c,
					nb_non_expedie_h,
					nb_non_expedie_0,
					nb_non_expedie_0_c,
					nb_non_expedie_0_h,
					nb_non_expedie_1,
					nb_non_expedie_1_c,
					nb_non_expedie_1_h,
					nb_non_expedie_2,
					nb_non_expedie_2_c,
					nb_non_expedie_2_h,
					montant_facture,
					montant_facture_c,
					montant_facture_h,
					montant_facture_0,
					montant_facture_0_c,
					montant_facture_0_h,
					montant_facture_1,
					montant_facture_1_c,
					montant_facture_1_h,
					montant_facture_2,
					montant_facture_2_c,
					montant_facture_2_h,
					montant_facture_c_actes_inclus_dans_sejour,
					montant_facture_h_actes_inclus_dans_sejour,
					montant_encours,
					montant_encours_c,
					montant_encours_h,
					montant_encours_0,
					montant_encours_0_c,
					montant_encours_0_h,
					montant_encours_1,
					montant_encours_1_c,
					montant_encours_1_h,
					montant_encours_2,
					montant_encours_2_c,
					montant_encours_2_h,
					montant_comptabilise,
					montant_comptabilise_c,
					montant_comptabilise_h,
					montant_comptabilise_0,
					montant_comptabilise_0_c,
					montant_comptabilise_0_h,
					montant_comptabilise_1,
					montant_comptabilise_1_c,
					montant_comptabilise_1_h,
					montant_comptabilise_2,
					montant_comptabilise_2_c,
					montant_comptabilise_2_h,
					montant_comptabilise_budget_global_c,
					montant_regle,
					montant_regle_c,
					montant_regle_h,
					montant_regle_0,
					montant_regle_0_c,
					montant_regle_0_h,
					montant_regle_1,
					montant_regle_1_c,
					montant_regle_1_h,
					montant_regle_2,
					montant_regle_2_c,
					montant_regle_2_h,
					date_solde,
					date_solde_c,
					date_solde_h,
					date_solde_0,
					date_solde_0_c,
					date_solde_0_h,
					date_solde_1,
					date_solde_1_c,
					date_solde_1_h,
					date_solde_2,
					date_solde_2_c,
					date_solde_2_h,
					delai_solde,
					delai_solde_c,
					delai_solde_h,
					delai_solde_0,
					delai_solde_0_c,
					delai_solde_0_h,
					delai_solde_1,
					delai_solde_1_c,
					delai_solde_1_h,
					delai_solde_2,
					delai_solde_2_c,
					delai_solde_2_h,
					nb_non_solde,
					nb_non_solde_c,
					nb_non_solde_h,
					nb_non_solde_0,
					nb_non_solde_0_c,
					nb_non_solde_0_h,
					nb_non_solde_1,
					nb_non_solde_1_c,
					nb_non_solde_1_h,
					nb_non_solde_2,
					nb_non_solde_2_c,
					nb_non_solde_2_h,
					nb_non_cloture,
					nb_non_cloture_c,
					nb_non_cloture_h,
					nb_non_cloture_0,
					nb_non_cloture_0_c,
					nb_non_cloture_0_h,
					nb_non_cloture_1,
					nb_non_cloture_1_c,
					nb_non_cloture_1_h,
					nb_non_cloture_2,
					nb_non_cloture_2_c,
					nb_non_cloture_2_h,
					COALESCE(w_correspondances_pole.oid, 0) AS pole_sortie_id
				FROM p_factures_reference_tiers
					LEFT JOIN w_correspondances_ghs ON ghs_id = w_correspondances_ghs.oid_source
					LEFT JOIN w_correspondances_pole ON pole_sortie_id = w_correspondances_pole.oid_source
					;
					

			]]></sqlcmd>
		</NODE>
		
		<NODE label="Rejets">
			<sqlcmd><![CDATA[
			
				-- Récupération tables sources
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_factures_rejets 
					WHERE EXISTS (SELECT 1 FROM temp.w_factures WHERE temp.w_factures.no_facture = activite.p_factures_rejets.no_facture)
					limit 100000000000
					', 'temp p_factures_rejets'
				);
				
				CREATE INDEX p_factures_rejets_i1
				  ON p_factures_rejets
				  USING btree
				  (rejet_id);

				INSERT INTO activite.p_factures_rejets(
					no_facture, 
					date_rejet, 
					rejet_id, 
					rejet_code, 
					rejet_texte, 
					facture_id)
				SELECT 
					'[CPX]' || no_facture AS no_facture, 
					date_rejet, 
					COALESCE(w_correspondances_rejets_noemie.oid,0) AS rejet_id, 
					rejet_code, 
					rejet_texte, 
					CASE WHEN facture_id <> 0 THEN facture_id + [IPX] ELSE 0 END AS facture_id
				FROM p_factures_rejets
					LEFT JOIN w_correspondances_rejets_noemie ON rejet_id = w_correspondances_rejets_noemie.oid_source;
										
			]]></sqlcmd>
		</NODE>
		
		
		

	</NODE>
	
	
	<NODE name="CHIFFRIER" label="CREATION DU CHIFFRIER Désactivé">
		
		<NODE label="Récupération chiffrier">
			<sqlcmd><![CDATA[
			
				-- Récupération tables sources
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_chiffrier_comptable limit 100000000000
					', 'temp p_chiffrier_comptable'
				);
						
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.p_factures_comptables limit 100000000000
					', 'temp p_factures_comptables'
				);
				

				INSERT INTO activite.p_chiffrier_comptable(
					finess,
					date_comptable,
					provider_id,
					montant_ventes_c,
					montant_reglements_c,
					montant_solde_client_c,
					montant_ventes_h,
					montant_reglements_h,
					montant_solde_client_h)
				SELECT 
					COALESCE(w_correspondances_finess.code,'[FINESS]') AS finess,
					date_comptable,
					provider_id,
					0 AS montant_ventes_c,
					0 AS montant_reglements_c,
					0 AS montant_solde_client_c,
					0 AS montant_ventes_h,
					0 AS montant_reglements_h,
					0 AS montant_solde_client_h
				FROM p_chiffrier_comptable
                LEFT JOIN w_correspondances_finess ON p_chiffrier_comptable.finess = w_correspondances_finess.code_source
				WHERE date_comptable >= '[ENV_ADM_ANNEEDEBUT]0101'
                GROUP BY 1,2,3;


				

				--INSERT INTO activite.p_factures_comptables(
				--	finess,
				--	no_facture,
				--	date_comptable,
				--	facture_id,
				--	provider_id,
				--	montant_ventes_c,
				--	montant_reglements_c,
				--	montant_ventes_h,
				--	montant_reglements_h)
				--SELECT
				--	COALESCE(w_correspondances_finess.code,'[FINESS]') AS finess,
				--	'[CPX]' || no_facture AS no_facture,
				--	date_comptable,
				--	CASE WHEN facture_id <> 0 THEN facture_id + [IPX] ELSE 0 END AS facture_id,
				--	provider_id,
				--	montant_ventes_c,
				--	montant_reglements_c,
				--	montant_ventes_h,
				--	montant_reglements_h
				--FROM p_factures_comptables
                --LEFT JOIN w_correspondances_finess ON p_chiffrier_comptable.finess = w_correspondances_finess.code_source
				--WHERE date_comptable >= '[ENV_ADM_ANNEEDEBUT]0101'
                --GROUP BY 1,2,3,4,5;


										
			]]></sqlcmd>
		</NODE>
		
		
	</NODE>
	
	<NODE name="PARAM" label="RECUPERATION DES PARAMETRES">
        <NODE label="FINESS">
			<sqlcmd><![CDATA[

				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM base.t_finess
					', 'temp i_finess'
				);
                
				INSERT INTO base.t_finess(
					code, 
					texte, 
					texte_court,
                    type_etablissement,
                    secondaire) 
				SELECT 
					code, 
					texte, 
					texte_court,
                    type_etablissement,
                    '1' AS secondaire
				FROM i_finess
				WHERE oid <> 0 
				AND NOT EXISTS (SELECT 1 FROM base.t_finess WHERE code = i_finess.code)
                ;
                
                DROP TABLE IF EXISTS w_correspondances_finess;
				CREATE TEMP TABLE w_correspondances_finess AS
				SELECT i_finess.oid AS oid_source, i_finess.code AS code_source, t_finess.oid AS oid, t_finess.code AS code, t_finess.oid AS oid_base
				FROM i_finess
				JOIN base.t_finess ON t_finess.code = i_finess.code
				WHERE i_finess.oid <> 0
				UNION
				SELECT 0, '', t_finess.oid AS oid, t_finess.code AS code, t_finess.oid AS oid_base
				FROM base.t_finess
				WHERE t_finess.code = '[FINESS]'
				;
                
                UPDATE w_correspondances_finess SET
				oid = t_finess.oid,
				code = t_finess.code
				FROM base.t_finess
				WHERE t_finess.code = '[FINESS]' AND
					'[KEEP_FINESS]' <> '1'
                ;
            
                
                CREATE INDEX w_correspondances_finess_1
				  ON w_correspondances_finess
				  USING btree
				  (oid_source);
                  
                CREATE INDEX w_correspondances_finess_2
				  ON w_correspondances_finess
				  USING btree
				  (code_source);
						
			]]></sqlcmd>
		</NODE>
		<NODE label="Suppression des données">
			<sqlcmd><![CDATA[
			
				
                DELETE FROM activite.p_sejour_effet_tarif
				USING activite.p_sejours
				WHERE p_sejour_effet_tarif.sejour_id = p_sejours.oid AND
					p_sejours.no_sejour LIKE '[PX]\_%' AND '[MODE]' <> 'RAZ'
				;
				DELETE FROM activite.p_sejours WHERE no_sejour LIKE '[PX]\_%' AND '[MODE]' <> 'RAZ'
				;
				DELETE FROM activite.p_mouvements_sejour WHERE no_sejour LIKE '[PX]\_%' AND '[MODE]' <> 'RAZ'
				;
				DELETE FROM activite.p_sejour_lieu WHERE split_part(no_sejour,'_',1) = '[PX]'
				;
				DELETE FROM activite.p_intervention WHERE sejour_id BETWEEN [IPX] AND ([IPX] + 999999999999)
				;
				DELETE FROM activite.p_factures WHERE no_facture LIKE '[PX]\_%' AND '[MODE]' <> 'RAZ'
				;
				DELETE FROM activite.p_factures_lignes_non_facturees_c WHERE no_facture LIKE '[PX]\_%' AND '[MODE]' <> 'RAZ'
				;
				DELETE FROM activite.p_factures_lignes_c WHERE no_facture LIKE '[PX]\_%' AND '[MODE]' <> 'RAZ'
				;
				DELETE FROM activite.p_factures_lignes_non_facturees_h WHERE no_facture LIKE '[PX]\_%' AND '[MODE]' <> 'RAZ'
				;
				DELETE FROM activite.p_factures_lignes_h WHERE no_facture LIKE '[PX]\_%' AND '[MODE]' <> 'RAZ'
				;
				DELETE FROM activite.p_factures_soldes_c WHERE no_facture LIKE '[PX]\_%' AND '[MODE]' <> 'RAZ'
				;
				DELETE FROM activite.p_facture_solde_tiers_c WHERE no_facture LIKE '[PX]\_%' AND '[MODE]' <> 'RAZ'
				;
				DELETE FROM activite.p_factures_soldes_h WHERE no_facture LIKE '[PX]\_%' AND '[MODE]' <> 'RAZ'
				;
				DELETE FROM activite.p_factures_encours WHERE no_facture LIKE '[PX]\_%' AND '[MODE]' <> 'RAZ'
				;
				DELETE FROM activite.p_factures_encours_lignes_c WHERE no_facture LIKE '[PX]\_%' AND '[MODE]' <> 'RAZ'
				;
				DELETE FROM activite.p_factures_reference WHERE no_sejour LIKE '[PX]\_%' AND '[MODE]' <> 'RAZ'
				;
				DELETE FROM activite.p_factures_reference_tiers WHERE no_sejour LIKE '[PX]\_%' AND '[MODE]' <> 'RAZ'
				;
				DELETE FROM activite.p_factures_rejets WHERE no_facture LIKE '[PX]\_%' AND '[MODE]' <> 'RAZ'
				;
				
				DELETE FROM activite.p_chiffrier_comptable 
				WHERE EXISTS (SELECT 1 FROM w_correspondances_finess WHERE code_source = activite.p_chiffrier_comptable.finess) 
				OR EXISTS (SELECT 1 FROM w_correspondances_finess WHERE code = activite.p_chiffrier_comptable.finess)
				;
				DELETE FROM activite.p_factures_comptables 
				WHERE EXISTS (SELECT 1 FROM w_correspondances_finess WHERE code_source = activite.p_factures_comptables.finess) 
				OR EXISTS (SELECT 1 FROM w_correspondances_finess WHERE code = activite.p_factures_comptables.finess)
				;
                
                DELETE FROM activite.p_consolidation_translation WHERE from_database ILIKE '[DBNAME]'
				;
					
			]]></sqlcmd>
		</NODE>
		<NODE label="Tiers payant">
			<sqlcmd><![CDATA[

				INSERT INTO activite.t_types_tiers_payant(code, texte, texte_court)
				SELECT code, texte, texte FROM
				(
				SELECT '0' AS code, 'Patients' AS texte
				UNION ALL
				SELECT '1','Régime général'
				UNION ALL
				SELECT '2','Mutuelles'
				) subview
				WHERE NOT EXISTS (SELECT 1 FROM activite.t_types_tiers_payant WHERE code = subview.code);
				
				DROP TABLE IF EXISTS w_t_tiers_payant;
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.t_tiers_payant
					', 'temp w_t_tiers_payant'
				);

				INSERT INTO activite.t_tiers_payant(oid, code, code_original, type_tiers_payant, texte, texte_court) 
				SELECT 0, '', '0', '0', 'Non saisi', 'Non saisi'
				WHERE NOT EXISTS (SELECT 1 FROM activite.t_tiers_payant WHERE oid = 0);
				

				INSERT INTO activite.t_tiers_payant(code, code_original, type_tiers_payant, texte, texte_court) 
				SELECT '0000', '0000', '0', 'PATIENTS', 'PATIENTS'
				WHERE NOT EXISTS (SELECT 1 FROM activite.t_tiers_payant WHERE code_original = '0000');

				UPDATE activite.t_tiers_payant SET
					code = '[CPX]' || w_tiers_payant.code,
					texte = w_tiers_payant.texte,
					texte_court = w_tiers_payant.texte_court
				FROM w_t_tiers_payant  w_tiers_payant
				WHERE 1=1 AND
					t_tiers_payant.oid = w_tiers_payant.oid + [IPX] AND
					(
					t_tiers_payant.code IS DISTINCT FROM ('[CPX]' || w_tiers_payant.code) OR
					t_tiers_payant.texte = w_tiers_payant.texte OR
					t_tiers_payant.texte_court = w_tiers_payant.texte_court
					)
				;

				INSERT INTO activite.t_tiers_payant(
					oid,
					code_original, 
					code, 
					type_tiers_payant, 
					texte, 
					texte_court) 
				SELECT 
					oid + [IPX],
					oid + [IPX] AS code_original, 
					'[CPX]' || code, 
					type_tiers_payant, 
					texte, 
					texte_court
				FROM w_t_tiers_payant
				WHERE oid <> 0 
				AND NOT EXISTS (SELECT 1 FROM activite.t_tiers_payant WHERE t_tiers_payant.oid = w_t_tiers_payant.oid + [IPX]);
				
				SELECT activite.cti_update_schema_classes('TIERSP');
				
				
										
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Médecins">
			<sqlcmd><![CDATA[
			
				
				INSERT INTO base.t_specialites_medecin(oid, code, code_original, texte, texte_court) 
				SELECT 0, '', '0', 'Non saisie', 'Non saisie'
				WHERE NOT EXISTS (SELECT 1 FROM base.t_specialites_medecin WHERE oid = 0);

				DROP TABLE IF EXISTS w_t_specialites_medecin; 
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM base.t_specialites_medecin
					', 'temp w_t_specialites_medecin'
				);
				
				INSERT INTO base.t_specialites_medecin (
					code, 
					texte, 
					texte_court, 
					code_original
				)
				SELECT 
					code, 
					texte, 
					texte_court, 
					code_original
				FROM w_t_specialites_medecin
				WHERE oid <> 0 AND 
					NOT EXISTS (SELECT 1 FROM base.t_specialites_medecin WHERE code = w_t_specialites_medecin.code) AND oid <> 0;

					
					
				DROP TABLE IF EXISTS w_correspondances_specialites_medecin;
				CREATE TEMP TABLE w_correspondances_specialites_medecin AS
				SELECT 
					t_specialites_medecin_source.oid AS oid_source,
					coalesce(t_specialites_medecin.oid,0) AS oid
				FROM w_t_specialites_medecin AS t_specialites_medecin_source 
					LEFT JOIN base.t_specialites_medecin ON t_specialites_medecin_source.code = t_specialites_medecin.code;
				
				CREATE INDEX w_correspondances_specialites_medecin_1
				  ON w_correspondances_specialites_medecin
				  USING btree
				  (oid_source);
				  
				  
				INSERT INTO base.t_medecins(oid, nom, prenom, numero_ordre, specialite_id ) 
				SELECT 0, 'Non renseigné', '', '', 0 WHERE NOT EXISTS (SELECT 1 FROM base.t_medecins WHERE oid = 0);
				
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM base.t_medecins
					WHERE EXISTS (SELECT 1 FROM activite.t_medecins_administratifs WHERE medecin_id = base.t_medecins.oid)
					', 'temp t_medecins'
				);
				  
				 
				INSERT INTO base.t_medecins(
					nom, 
					prenom, 
					numero_ordre, 
					code_rpps,
					specialite_id)
				SELECT
					t_medecins_source.nom, 
					t_medecins_source.prenom, 
					t_medecins_source.numero_ordre, 
					t_medecins_source.code_rpps, 
					COALESCE(w_correspondances_specialites_medecin.oid,0)
				FROM t_medecins t_medecins_source
					LEFT JOIN w_correspondances_specialites_medecin ON t_medecins_source.specialite_id = oid_source
					LEFT JOIN base.t_medecins ON 
						(
						t_medecins.nom =  t_medecins_source.nom AND
						t_medecins.prenom = t_medecins_source.prenom AND
						t_medecins.numero_ordre = t_medecins_source.numero_ordre AND
						length(t_medecins_source.numero_ordre) = 9
						) OR
						(
						t_medecins.nom =  t_medecins_source.nom AND
						t_medecins.prenom = t_medecins_source.prenom AND
						length(t_medecins_source.numero_ordre) <> 9 AND
						t_medecins.specialite_id = COALESCE(w_correspondances_specialites_medecin.oid,0)
						) OR
						(
						t_medecins.nom =  t_medecins_source.nom AND
						t_medecins.prenom = t_medecins_source.prenom AND
						t_medecins.code_rpps = t_medecins_source.code_rpps AND
						length(t_medecins_source.code_rpps) = 11
						) 
				WHERE t_medecins_source.oid <> 0 AND t_medecins.oid IS NULL
				GROUP BY 1,2,3,4,5;
				
				
				DROP TABLE IF EXISTS w_correspondances_medecins;
				CREATE TEMP TABLE w_correspondances_medecins AS
				SELECT 
					t_medecins_source.oid AS oid_source,
					(MAX(ARRAY[(CASE WHEN EXISTS (SELECT 1 FROM activite.t_medecins_administratifs WHERE medecin_id = t_medecins.oid) THEN 1 ELSE 0 END)::bigint,coalesce(t_medecins.oid,0)]))[2] AS oid
				FROM
					t_medecins AS t_medecins_source 
					LEFT JOIN w_correspondances_specialites_medecin ON t_medecins_source.specialite_id = oid_source
					LEFT JOIN base.t_medecins ON 
						(
						t_medecins.nom =  t_medecins_source.nom AND
						t_medecins.prenom = t_medecins_source.prenom AND
						t_medecins.numero_ordre = t_medecins_source.numero_ordre AND
						length(t_medecins_source.numero_ordre) = 9
						) OR
						(
						t_medecins.nom =  t_medecins_source.nom AND
						t_medecins.prenom = t_medecins_source.prenom AND
						length(t_medecins_source.numero_ordre) <> 9 AND
						t_medecins.specialite_id = COALESCE(w_correspondances_specialites_medecin.oid,0)
						) OR
						(
						t_medecins.nom =  t_medecins_source.nom AND
						t_medecins.prenom = t_medecins_source.prenom AND
						t_medecins.code_rpps = t_medecins_source.code_rpps AND
						length(t_medecins_source.code_rpps) = 11
						)
				GROUP BY 1;
				
				
				CREATE INDEX w_correspondances_medecins_1
				  ON w_correspondances_medecins
				  USING btree
				  (oid_source);
                  
                UPDATE base.t_medecins
				SET code_rpps = t_medecins_source.code_rpps
				FROM w_correspondances_medecins
				JOIN t_medecins t_medecins_source ON w_correspondances_medecins.oid_source = t_medecins_source.oid
				WHERE t_medecins.oid = w_correspondances_medecins.oid AND
					t_medecins.code_rpps = '' AND t_medecins_source.code_rpps <> ''
				;
				

				INSERT INTO activite.t_specialites_medecin(oid, code, code_original, texte, texte_court) 
				SELECT 0, '', '0', 'Non saisie', 'Non saisie'
				WHERE NOT EXISTS (SELECT 1 FROM activite.t_specialites_medecin WHERE oid = 0);
				
				INSERT INTO activite.t_medecins_administratifs(oid, code_original, code, nom, prenom, specialite_id, medecin_id) 
				SELECT 0, '0', '****', 'Non renseigné', '', 0, 0 WHERE NOT EXISTS (SELECT 1 FROM activite.t_medecins_administratifs WHERE oid = 0);
				
				DROP TABLE IF EXISTS w_t_medecins_administratifs;
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.t_medecins_administratifs
					', 'temp w_t_medecins_administratifs'
				);

				INSERT INTO activite.t_medecins_administratifs(
					oid,
					code,
					code_original,
					nom,
					prenom,
					specialite_id,
					medecin_id)
				SELECT
					w_t_medecins_administratifs.oid + [IPX],
					'[CPX]' || w_t_medecins_administratifs.code,
					w_t_medecins_administratifs.oid + [IPX] AS code_original,
					w_t_medecins_administratifs.nom,
					w_t_medecins_administratifs.prenom,
					COALESCE(w_correspondances_specialites_medecin.oid,0) AS specialite_id,
					COALESCE(w_correspondances_medecins.oid,0) AS medecin_id
				FROM w_t_medecins_administratifs
					LEFT JOIN w_correspondances_specialites_medecin ON w_t_medecins_administratifs.specialite_id = w_correspondances_specialites_medecin.oid_source
					LEFT JOIN w_correspondances_medecins ON w_t_medecins_administratifs.medecin_id = w_correspondances_medecins.oid_source
				WHERE w_t_medecins_administratifs.oid <> 0 AND NOT EXISTS (SELECT 1 FROM activite.t_medecins_administratifs WHERE oid = w_t_medecins_administratifs.oid + [IPX]);
				                
                INSERT INTO activite.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT
					'[DBNAME]'::text AS from_database,
                    'MEDECIN'::text AS table_code,
                    oid AS from_id,
                    code AS from_code,
                    oid + [IPX] AS to_oid,
                    '[CPX]' || code AS to_code
				FROM w_t_medecins_administratifs
                WHERE oid <> 0
				;
				
				
				-- Correction de la requête suivante qui faisait un UPDATE abusif qui ne vérifiait pas la correspondance du code (ajoutée depuis)
				UPDATE activite.t_medecins_administratifs SET
					code = ('[CPX]'::text || w_med.code::text),
					nom = w_med.nom,
					prenom = w_med.prenom
				FROM w_t_medecins_administratifs w_med
				WHERE
					w_med.oid + [IPX] = t_medecins_administratifs.oid
					AND ('[CPX]'::text || w_med.code::text) IS DISTINCT FROM t_medecins_administratifs.code;
				
				UPDATE activite.t_medecins_administratifs SET
					specialite_id = COALESCE(w_correspondances_specialites_medecin.oid,0),
					nom = t_medecins_administratifs_env.nom,
					prenom = t_medecins_administratifs_env.prenom,
					no_adeli = t_medecins_administratifs_env.no_adeli,
					est_medecin_salarie = t_medecins_administratifs_env.est_medecin_salarie
				FROM w_t_medecins_administratifs t_medecins_administratifs_env
					LEFT JOIN w_correspondances_specialites_medecin ON t_medecins_administratifs_env.specialite_id = w_correspondances_specialites_medecin.oid_source
				WHERE 
					t_medecins_administratifs_env.oid + [IPX] = t_medecins_administratifs.oid AND
					'[CPX]'::text || t_medecins_administratifs_env.code::text = t_medecins_administratifs.code AND
					(t_medecins_administratifs.specialite_id IS DISTINCT FROM COALESCE(w_correspondances_specialites_medecin.oid,0) OR
					t_medecins_administratifs.nom IS DISTINCT FROM t_medecins_administratifs_env.nom OR
					t_medecins_administratifs.prenom IS DISTINCT FROM t_medecins_administratifs_env.prenom OR
					t_medecins_administratifs.no_adeli IS DISTINCT FROM t_medecins_administratifs_env.no_adeli OR
					t_medecins_administratifs.est_medecin_salarie IS DISTINCT FROM t_medecins_administratifs_env.est_medecin_salarie
					);
					
				-- Correction reference medecin suite à fusion ou autre (selon un taux d'erreurs > 10% ou nombre d'erreurs > 100
				-- On efface la reference qui sera recree depuis la source
				DROP TABLE IF EXISTS w_referentiel_ko;
				CREATE TEMP TABLE w_referentiel_ko AS
				SELECT 
					t_medecins_administratifs.nom AS adm_nom, 
					t_medecins_administratifs.prenom AS adm_prenom,
					t_medecins.nom, 
					t_medecins.prenom,
					t_medecins_source.nom AS nom_source, 
					t_medecins_source.prenom AS prenom_source,
					t_medecins_administratifs.medecin_id, 
					t_medecins_administratifs.oid AS medecin_administratif_id,
					CASE WHEN 
						base.cti_soundex_nom(t_medecins_administratifs.nom || ' ' || t_medecins_administratifs.prenom) <>
							base.cti_soundex_nom(t_medecins.nom || ' ' || t_medecins.prenom) AND

						base.cti_soundex_nom(t_medecins_administratifs.prenom || ' ' || t_medecins_administratifs.nom) <>
							base.cti_soundex_nom(t_medecins.nom || ' ' || t_medecins.prenom) AND

						base.cti_soundex_nom(t_medecins_administratifs.nom) <>
							base.cti_soundex_nom(t_medecins.nom) AND

						upper(left(t_medecins_administratifs.nom,5)) <> 
							upper(left(t_medecins.nom,5)) 
					THEN '0' ELSE '1' END AS is_ok,
					CASE WHEN t_medecins.nom = t_medecins_source.nom THEN '1' ELSE '0' END AS is_ok_source			
				FROM activite.t_medecins_administratifs
				JOIN base.t_medecins ON medecin_id = t_medecins.oid
                JOIN w_t_medecins_administratifs t_medecins_administratifs_source ON t_medecins_administratifs.oid = (t_medecins_administratifs_source.oid + [IPX])
				JOIN t_medecins t_medecins_source ON t_medecins_administratifs_source.medecin_id = t_medecins_source.oid
				WHERE length(t_medecins_administratifs.nom) > 2 AND
					t_medecins_administratifs.medecin_id <> 0 AND
					t_medecins_administratifs.oid BETWEEN [IPX] AND [IPX]+999999999999
				;
				
				UPDATE activite.t_medecins_administratifs
				SET medecin_id = 0
				FROM w_referentiel_ko
				WHERE t_medecins_administratifs.oid = medecin_administratif_id AND
					is_ok = '0' AND
					is_ok_source = '0'
				;

				UPDATE activite.t_medecins_administratifs
				SET medecin_id = 0
				FROM w_referentiel_ko
				JOIN (
				SELECT 
					count(*), 
					SUM(CASE WHEN is_ok <> '1' THEN 1 ELSE 0 END),
					base.cti_division(SUM(CASE WHEN is_ok <> '1' THEN 1 ELSE 0 END), count(*))
				FROM w_referentiel_ko
				HAVING base.cti_division(SUM(CASE WHEN is_ok <> '1' THEN 1 ELSE 0 END), count(*)) >= 0.1 OR
					SUM(CASE WHEN is_ok <> '1' THEN 1 ELSE 0 END) > 100
				) subview ON 1=1
				WHERE t_medecins_administratifs.oid = medecin_administratif_id AND
					is_ok = '0'
				;
				
				UPDATE activite.t_medecins_administratifs
				SET medecin_id = w_correspondances_medecins.oid
				FROM w_t_medecins_administratifs t_medecins_administratifs_source
				JOIN w_correspondances_medecins ON t_medecins_administratifs_source.medecin_id = w_correspondances_medecins.oid_source
				WHERE t_medecins_administratifs.oid = t_medecins_administratifs_source.oid + [IPX] AND
					t_medecins_administratifs.medecin_id <> w_correspondances_medecins.oid_source AND
					t_medecins_administratifs.medecin_id = 0
				;
				
				-- Suppression des médecins qui n'existent plus
				DELETE FROM activite.t_medecins_administratifs 
				WHERE oid BETWEEN [IPX] AND [IPX]+999999999999 AND
						NOT EXISTS (SELECT 1 FROM w_t_medecins_administratifs WHERE oid = activite.t_medecins_administratifs.oid-[IPX])
				;
				
				-- Initialisation spécialites si modifées après dans la source			
				UPDATE base.t_medecins
				SET specialite_id = subview.to_specialite_id
				FROM
				(
				SELECT t_medecins.oid, 
					MAX(t_medecins_source.nom) AS source_nom, 
					MAX(t_medecins_source.prenom) AS source_prenom, 
					MAX(t_medecins_source.specialite_id) AS source_specialite_id, 
					MAX(t_specialites_medecin_source.code) AS source_specialite_code, 
					MAX(t_medecins.nom) AS nom, 
					MAX(t_medecins.prenom) AS prenom,
					MAX(t_medecins.specialite_id) AS specialite_id, 
					MAX(w_correspondances_specialites_medecin.oid) AS to_specialite_id
				FROM t_medecins t_medecins_source
				JOIN w_t_medecins_administratifs t_medecins_administratifs_source ON medecin_id = t_medecins_source.oid
				JOIN w_t_specialites_medecin t_specialites_medecin_source ON t_medecins_source.specialite_id = t_specialites_medecin_source.oid
				JOIN w_correspondances_medecins ON t_medecins_administratifs_source.medecin_id = w_correspondances_medecins.oid_source
				JOIN base.t_medecins ON w_correspondances_medecins.oid = t_medecins.oid
				JOIN base.t_specialites_medecin ON t_medecins.specialite_id = t_specialites_medecin.oid
				JOIN w_correspondances_specialites_medecin ON t_medecins_source.specialite_id = w_correspondances_specialites_medecin.oid_source
				WHERE t_specialites_medecin_source.code <> t_specialites_medecin.code AND
					(
					t_specialites_medecin.code LIKE '%**' OR
					t_specialites_medecin.oid = 0
					) AND
					t_specialites_medecin_source.code NOT LIKE '%**' AND
					t_specialites_medecin_source.oid <> 0
				GROUP BY 1
				) subview
				WHERE t_medecins.oid = subview.oid
				;
				
				
				SELECT activite.cti_update_schema_classes('MEDECIN');

										
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Hébergement">
			<sqlcmd><![CDATA[
			
				-- Récupération tables sources
                
                SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.t_forme_activite
					', 'temp i_forme_activite'
				);
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM base.t_pole
					', 'temp i_pole'
				);
				
				DROP TABLE IF EXISTS w_t_modes_traitement;
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM base.t_modes_traitement
					', 'temp w_t_modes_traitement'
				);
				
				DROP TABLE IF EXISTS w_t_codes_postaux;
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM base.t_codes_postaux
					', 'temp w_t_codes_postaux'
				);
				
				DROP TABLE IF EXISTS w_t_dmt;
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM base.t_dmt
					', 'temp w_t_dmt'
				);

				DROP TABLE IF EXISTS w_t_services_facturation;
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.t_services_facturation
					', 'temp w_t_services_facturation'
				);
				
				DROP TABLE IF EXISTS w_t_activites;
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.t_activites
					', 'temp w_t_activites'
				);
				
				DROP TABLE IF EXISTS w_t_unites_fonctionnelles;
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.t_unites_fonctionnelles
					', 'temp w_t_unites_fonctionnelles'
				);
				
				DROP TABLE IF EXISTS w_t_unites_medicales;
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.t_unites_medicales
					', 'temp w_t_unites_medicales'
				);
				
				DROP TABLE IF EXISTS w_t_etages;
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.t_etages
					', 'temp w_t_etages'
				);
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.t_risque
					', 'temp t_risque'
				);
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.t_etages_historique
					', 'temp t_etages_historique'
				);
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.t_lits
					', 'temp t_lits'
				);
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.t_lit_historique
					', 'temp t_lit_historique'
				);
                
                -- Forme d'activité
				INSERT INTO activite.t_forme_activite (
					code, 
					texte, 
					texte_court
				)
				SELECT 
					code, 
					texte, 
					texte_court
				FROM i_forme_activite
				WHERE oid <> 0 AND 
					 NOT EXISTS (SELECT 1 FROM activite.t_forme_activite WHERE code = i_forme_activite.code);
                    
                INSERT INTO activite.t_forme_activite_rule (code, texte)
                SELECT '*NORULE_CONSO', 'Pas de règle sur environnement consolidé'
                WHERE NOT EXISTS (SELECT 1 FROM activite.t_forme_activite_rule WHERE code = '*NORULE_CONSO')
                ;
                DELETE FROM activite.t_forme_activite_rule
                WHERE code <> '*NORULE_CONSO'
                ;
                    
                DROP TABLE IF EXISTS w_correspondances_forme_activite;
				CREATE TEMP TABLE w_correspondances_forme_activite AS
				SELECT 
					i_forme_activite.oid AS oid_source,
					COALESCE(t_forme_activite.oid,0) AS oid
				FROM i_forme_activite
					LEFT JOIN activite.t_forme_activite ON i_forme_activite.code = t_forme_activite.code;
				
				CREATE INDEX w_correspondances_forme_activite_1
				  ON w_correspondances_forme_activite
				  USING btree
				  (oid_source);
				  
				-- Pole
				INSERT INTO base.t_pole (
					code, 
					texte, 
					texte_court
				)
				SELECT 
					code, 
					texte, 
					texte_court
				FROM i_pole
				WHERE oid <> 0 AND 
					NOT EXISTS (SELECT 1 FROM base.t_pole WHERE code = i_pole.code);
                    
                 
                DROP TABLE IF EXISTS w_correspondances_pole;
				CREATE TEMP TABLE w_correspondances_pole AS
				SELECT 
					i_pole.oid AS oid_source,
					COALESCE(t_pole.oid,0) AS oid
				FROM i_pole
					LEFT JOIN base.t_pole ON i_pole.code = t_pole.code;
				
				CREATE INDEX w_correspondances_pole_1
				  ON w_correspondances_pole
				  USING btree
				  (oid_source);
				
				-- Codes postaux
							
				INSERT INTO base.t_codes_postaux (
					code, 
					texte, 
					texte_court
				)
				SELECT 
					code, 
					texte, 
					texte_court
				FROM w_t_codes_postaux
				WHERE oid <> 0 AND 
					NOT EXISTS (SELECT 1 FROM base.t_codes_postaux WHERE code = w_t_codes_postaux.code)
					AND oid <> 0;
					
					
				DROP TABLE IF EXISTS w_correspondances_codes_postaux;
				CREATE TEMP TABLE w_correspondances_codes_postaux AS
				SELECT 
					t_codes_postaux_source.oid AS oid_source,
					coalesce(t_codes_postaux.oid,0) AS oid
				FROM w_t_codes_postaux AS t_codes_postaux_source 
					LEFT JOIN base.t_codes_postaux ON t_codes_postaux_source.code = t_codes_postaux.code;
				
				CREATE INDEX w_correspondances_codes_postaux_1
				  ON w_correspondances_codes_postaux
				  USING btree
				  (oid_source);
				
				-- Modes de traitement
							
				INSERT INTO base.t_modes_traitement (
					code, 
					texte, 
					texte_court, 
					code_original
				)
				SELECT 
					code, 
					texte, 
					texte_court, 
					code_original
				FROM w_t_modes_traitement
				WHERE oid <> 0 AND 
					NOT EXISTS (SELECT 1 FROM base.t_modes_traitement WHERE code = w_t_modes_traitement.code);
					
				DROP TABLE IF EXISTS w_correspondances_modes_traitement;
				CREATE TEMP TABLE w_correspondances_modes_traitement AS
				SELECT 
					t_modes_traitement_source.oid AS oid_source,
					coalesce(t_modes_traitement.oid,0) AS oid
				FROM w_t_modes_traitement AS t_modes_traitement_source 
					LEFT JOIN base.t_modes_traitement ON t_modes_traitement_source.code = t_modes_traitement.code;
				
				CREATE INDEX w_correspondances_modes_traitement_1
				  ON w_correspondances_modes_traitement
				  USING btree
				  (oid_source);
				  
				  
				-- DMT
				
				INSERT INTO base.t_dmt (
					code, 
					texte, 
					texte_court, 
					code_original
				)
				SELECT 
					code, 
					texte, 
					texte_court, 
					code_original
				FROM w_t_dmt
				WHERE oid <> 0 AND 
					NOT EXISTS (SELECT 1 FROM base.t_dmt WHERE code = w_t_dmt.code);
					
				DROP TABLE IF EXISTS w_correspondances_dmt;
				CREATE TEMP TABLE w_correspondances_dmt AS
				SELECT 
					t_dmt_source.oid AS oid_source,
					coalesce(t_dmt.oid,0) AS oid
				FROM w_t_dmt AS t_dmt_source 
					LEFT JOIN base.t_dmt ON t_dmt_source.code = t_dmt.code;
				
				CREATE INDEX w_correspondances_dmt_1
				  ON w_correspondances_dmt
				  USING btree
				  (oid_source);

				
				-- Services
				
				INSERT INTO activite.t_services_facturation(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '****', 'Non renseigné', 'Non renseigné' WHERE NOT EXISTS (SELECT 1 FROM activite.t_services_facturation WHERE oid = 0);
				
				
				UPDATE activite.t_services_facturation SET
					code = '[CPX]' || w_services_facturation.code,
					texte = w_services_facturation.texte,
					texte_court = w_services_facturation.texte_court
				FROM w_t_services_facturation  w_services_facturation
				WHERE 1=1 AND
					t_services_facturation.oid = w_services_facturation.oid + [IPX] AND
					(
					t_services_facturation.code IS DISTINCT FROM ('[CPX]' || w_services_facturation.code) OR
					t_services_facturation.texte IS DISTINCT FROM w_services_facturation.texte OR
					t_services_facturation.texte_court IS DISTINCT FROM w_services_facturation.texte_court
					)
				;
								
				INSERT INTO activite.t_services_facturation(
					oid,
					code,
					code_original,
					texte,
					texte_court,
                    finess_id,
					statut,
					nb_lits,
					type_t2a,
					nb_cp,
					dmt_id,
					mode_traitement_id,
					unite_medicale_modifiable,
					est_sans_mouvement,
					etage_par_defaut_id,
					etage_force_id)
				SELECT 
					w_t_services_facturation.oid + [IPX],
					'[CPX]' || w_t_services_facturation.code,  
					w_t_services_facturation.oid + [IPX] AS code_original,
					w_t_services_facturation.texte,
					w_t_services_facturation.texte_court,
					COALESCE(w_correspondances_finess.oid,0) AS finess_id,
					w_t_services_facturation.statut,
					w_t_services_facturation.nb_lits,
					w_t_services_facturation.type_t2a,
					w_t_services_facturation.nb_cp,
					COALESCE(w_correspondances_dmt.oid,0) AS dmt_id,
					COALESCE(w_correspondances_modes_traitement.oid,0) AS mode_traitement_id,
					w_t_services_facturation.unite_medicale_modifiable,
					w_t_services_facturation.est_sans_mouvement,
					CASE WHEN etage_par_defaut_id <> 0 THEN etage_par_defaut_id + [IPX] ELSE 0 END AS etage_par_defaut_id,
					CASE WHEN etage_force_id <> 0 THEN etage_force_id + [IPX] ELSE 0 END AS etage_force_id
				FROM w_t_services_facturation
                    LEFT JOIN w_correspondances_finess ON w_t_services_facturation.finess_id = w_correspondances_finess.oid_source
					LEFT JOIN w_correspondances_modes_traitement ON w_t_services_facturation.mode_traitement_id = w_correspondances_modes_traitement.oid_source
					LEFT JOIN w_correspondances_dmt ON w_t_services_facturation.dmt_id = w_correspondances_dmt.oid_source
				WHERE w_t_services_facturation.oid <> 0 AND NOT EXISTS (SELECT 1 FROM activite.t_services_facturation WHERE oid = w_t_services_facturation.oid + [IPX]); 
                
                UPDATE activite.t_services_facturation
				SET finess_id = w_correspondances_finess.oid
				FROM w_t_services_facturation i_services_facturation
				JOIN w_correspondances_finess ON i_services_facturation.finess_id = w_correspondances_finess.oid_source
				WHERE t_services_facturation.oid = i_services_facturation.oid + [IPX] AND
					t_services_facturation.finess_id <> w_correspondances_finess.oid
				;
				
				UPDATE activite.t_services_facturation SET
					type_t2a = i_services_facturation.type_t2a,
					etage_par_defaut_id = CASE WHEN i_services_facturation.etage_par_defaut_id <> 0 THEN i_services_facturation.etage_par_defaut_id + [IPX] ELSE 0 END,
					etage_force_id = CASE WHEN i_services_facturation.etage_force_id <> 0 THEN i_services_facturation.etage_force_id + [IPX] ELSE 0 END
				FROM w_t_services_facturation i_services_facturation
				WHERE t_services_facturation.oid = i_services_facturation.oid + [IPX] AND
					(
					t_services_facturation.type_t2a IS DISTINCT FROM i_services_facturation.type_t2a OR
					t_services_facturation.etage_par_defaut_id IS DISTINCT FROM CASE WHEN i_services_facturation.etage_par_defaut_id <> 0 THEN i_services_facturation.etage_par_defaut_id + [IPX] ELSE 0 END OR
					t_services_facturation.etage_force_id IS DISTINCT FROM CASE WHEN i_services_facturation.etage_force_id <> 0 THEN i_services_facturation.etage_force_id + [IPX] ELSE 0 END
					)
				;
                
                INSERT INTO activite.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT 
					'[DBNAME]'::text AS from_database, 
                    'SERVICE_F'::text AS table_code, 
                    oid AS from_id, 
                    code AS from_code, 
                    oid + [IPX] AS to_oid, 
                    '[CPX]' || code AS to_code
				FROM w_t_services_facturation
                WHERE oid <> 0
				;

					
				-- Unités médicales

				INSERT INTO activite.t_unites_medicales(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '', 'Non renseignée', 'Non renseignée' WHERE NOT EXISTS (SELECT 1 FROM activite.t_unites_medicales WHERE oid = 0); 

				UPDATE activite.t_unites_medicales SET
					code = '[CPX]' || w_unites_medicales.code,
					texte = w_unites_medicales.texte,
					texte_court = w_unites_medicales.texte_court
				FROM w_t_unites_medicales  w_unites_medicales
				WHERE 1=1 AND
					t_unites_medicales.oid = w_unites_medicales.oid + [IPX] AND
					(
					t_unites_medicales.code IS DISTINCT FROM ('[CPX]' || w_unites_medicales.code) OR
					t_unites_medicales.texte IS DISTINCT FROM w_unites_medicales.texte OR
					t_unites_medicales.texte_court IS DISTINCT FROM w_unites_medicales.texte_court
					);
				
				INSERT INTO activite.t_unites_medicales(
					oid,
					code,
					code_original,
					texte,
					texte_court)
				SELECT 
					oid + [IPX],
					'[CPX]' || code,  
					oid + [IPX] AS code_original,
					texte,
					texte_court
				FROM w_t_unites_medicales
				WHERE oid <> 0 AND NOT EXISTS (SELECT 1 FROM activite.t_unites_medicales WHERE oid = w_t_unites_medicales.oid + [IPX]); 
                
                INSERT INTO activite.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT 
					'[DBNAME]'::text AS from_database, 
                    'UM'::text AS table_code, 
                    oid AS from_id, 
                    code AS from_code, 
                    oid + [IPX] AS to_oid, 
                    '[CPX]' || code AS to_code
				FROM w_t_unites_medicales
                WHERE oid <> 0
				;
				
				
				-- Unités fonctionnelles

				INSERT INTO activite.t_unites_fonctionnelles(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '', 'Non renseignée', 'Non renseignée' WHERE NOT EXISTS (SELECT 1 FROM activite.t_unites_fonctionnelles WHERE oid = 0); 

				UPDATE activite.t_unites_fonctionnelles SET
					code = '[CPX]' || w_unites_fonctionnelles.code,
					texte = w_unites_fonctionnelles.texte,
					texte_court = w_unites_fonctionnelles.texte_court
				FROM w_t_unites_fonctionnelles  w_unites_fonctionnelles
				WHERE 1=1 AND
					t_unites_fonctionnelles.oid = w_unites_fonctionnelles.oid + [IPX] AND
					(
					t_unites_fonctionnelles.code IS DISTINCT FROM ('[CPX]' || w_unites_fonctionnelles.code) OR
					t_unites_fonctionnelles.texte IS DISTINCT FROM w_unites_fonctionnelles.texte OR
					t_unites_fonctionnelles.texte_court IS DISTINCT FROM w_unites_fonctionnelles.texte_court
					);
				
				INSERT INTO activite.t_unites_fonctionnelles(
					oid,
					code,
					code_original,
					texte,
					texte_court,
                    finess_id)
				SELECT 
					w_t_unites_fonctionnelles.oid + [IPX],
					'[CPX]' || w_t_unites_fonctionnelles.code,  
					w_t_unites_fonctionnelles.oid + [IPX] AS code_original,
					w_t_unites_fonctionnelles.texte,
					w_t_unites_fonctionnelles.texte_court,
                    COALESCE(w_correspondances_finess.oid,0) AS finess_id
				FROM w_t_unites_fonctionnelles
                LEFT JOIN w_correspondances_finess ON w_t_unites_fonctionnelles.finess_id = w_correspondances_finess.oid_source
				WHERE w_t_unites_fonctionnelles.oid <> 0 AND NOT EXISTS (SELECT 1 FROM activite.t_unites_fonctionnelles WHERE oid = w_t_unites_fonctionnelles.oid + [IPX]); 

                UPDATE activite.t_unites_fonctionnelles
				SET finess_id = w_correspondances_finess.oid
				FROM w_t_unites_fonctionnelles i_unites_fonctionnelles
				JOIN w_correspondances_finess ON i_unites_fonctionnelles.finess_id = w_correspondances_finess.oid_source
				WHERE t_unites_fonctionnelles.oid = i_unites_fonctionnelles.oid + [IPX] AND
					t_unites_fonctionnelles.finess_id <> w_correspondances_finess.oid
				;
                
                INSERT INTO activite.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT 
					'[DBNAME]'::text AS from_database, 
                    'UF'::text AS table_code, 
                    oid AS from_id, 
                    code AS from_code, 
                    oid + [IPX] AS to_oid, 
                    '[CPX]' || code AS to_code
				FROM w_t_unites_fonctionnelles
                WHERE oid <> 0
				;
                
				-- Activités

				INSERT INTO activite.t_activites(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '', 'Non renseignée', 'Non renseignée' WHERE NOT EXISTS (SELECT 1 FROM activite.t_activites WHERE oid = 0); 

				-- Mise à jour préalable des codes s'ils ont été modifiés dans l'environnement source
				UPDATE activite.t_activites SET
                    code = '[CPX]'::text || w_activites.code::text,
                    code_original = w_activites.oid + [IPX],
                    texte = w_activites.texte,
                    texte_court = w_activites.texte_court
                FROM
                    w_t_activites w_activites
                WHERE 1=1
                    AND t_activites.oid = w_activites.oid + [IPX] 
                    AND t_activites.code::text IS DISTINCT FROM ('[CPX]'::text || w_activites.code::text);
    
                -- Ajout des nouveaux codes
                INSERT INTO activite.t_activites(
					oid,
					code,
					code_original,
					texte,
					texte_court)
				SELECT 
					oid + [IPX],
					'[CPX]' || code,  
					oid + [IPX] AS code_original,
					texte,
					texte_court
				FROM w_t_activites
				WHERE oid <> 0 AND NOT EXISTS (SELECT 1 FROM activite.t_activites WHERE oid = w_t_activites.oid + [IPX]);  

				-- Risques

				INSERT INTO activite.t_risque(oid, code, texte) 
				SELECT 0, '**', 'Non renseigné' WHERE NOT EXISTS (SELECT 1 FROM activite.t_risque WHERE oid = 0); 

				INSERT INTO activite.t_risque (
					code,
					texte)
				SELECT 
					t_risque.code,  
					t_risque.texte
				FROM 
                    t_risque
                    LEFT JOIN activite.t_risque ris ON ris.code = t_risque.code
				WHERE t_risque.oid <> 0 AND ris.code IS NULL;
                
				DROP TABLE IF EXISTS w_correspondances_risque;
				CREATE TEMP TABLE w_correspondances_risque AS
				SELECT 
					t_risque_source.oid AS oid_source,
					COALESCE(t_risque.oid, 0) AS oid
				FROM t_risque AS t_risque_source 
					LEFT JOIN activite.t_risque ON t_risque_source.code = t_risque.code;
				
				CREATE INDEX w_correspondancesrisque_1
				  ON w_correspondances_risque
				  USING btree
				  (oid_source);

				-- Etages et lits

				INSERT INTO activite.t_etages(oid, code_original, code, texte, texte_court, nb_lits) 
				SELECT 0, 0, '', 'Non renseigné', 'Non renseigné', 0 WHERE NOT EXISTS (SELECT 1 FROM activite.t_etages WHERE oid = 0); 

				UPDATE activite.t_etages SET
					code = '[CPX]' || w_etages.code
				FROM w_t_etages  w_etages
					LEFT JOIN w_correspondances_finess ON w_etages.finess_id = w_correspondances_finess.oid_source
				WHERE 1=1 AND
					t_etages.oid = w_etages.oid + [IPX] AND 
					(
					t_etages.code IS DISTINCT FROM ('[CPX]' || w_etages.code) OR 
					t_etages.nb_lits != w_etages.nb_lits OR
					t_etages.nb_cp != w_etages.nb_cp OR
					COALESCE(w_correspondances_finess.oid,0) != t_etages.finess_id
					);
				
				INSERT INTO activite.t_etages(
					oid,
					code,
					code_original,
					texte,
					texte_court,
					nb_lits,
					nb_cp,
					finess_id)
				SELECT 
					w_t_etages.oid + [IPX],
					'[CPX]' || w_t_etages.code,  
					w_t_etages.oid + [IPX] AS code_original,
					w_t_etages.texte,
					w_t_etages.texte_court,
					w_t_etages.nb_lits,
					w_t_etages.nb_cp,
					COALESCE(w_correspondances_finess.oid,0) AS finess_id
				FROM w_t_etages
                LEFT JOIN w_correspondances_finess ON w_t_etages.finess_id = w_correspondances_finess.oid_source
				WHERE w_t_etages.oid <> 0 AND NOT EXISTS (SELECT 1 FROM activite.t_etages WHERE oid = w_t_etages.oid + [IPX]); 
				
				UPDATE activite.t_etages SET
					texte = t_etages_source.texte,
					texte_court = t_etages_source.texte_court
				FROM w_t_etages t_etages_source
				WHERE t_etages.oid = t_etages_source.oid + [IPX] AND
					(
					t_etages.texte IS DISTINCT FROM t_etages_source.texte OR
					t_etages.texte_court IS DISTINCT FROM t_etages_source.texte_court
					)
				;
				
				UPDATE activite.t_etages
				SET finess_id = w_correspondances_finess.oid
				FROM w_t_etages i_etages
				JOIN w_correspondances_finess ON i_etages.finess_id = w_correspondances_finess.oid_source
				WHERE t_etages.oid = i_etages.oid + [IPX] AND
					t_etages.finess_id <> w_correspondances_finess.oid
				;
                
                INSERT INTO activite.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT 
					'[DBNAME]'::text AS from_database, 
                    'ETAGE'::text AS table_code, 
                    oid AS from_id, 
                    code AS from_code, 
                    oid + [IPX] AS to_oid, 
                    '[CPX]' || code AS to_code
				FROM w_t_etages
                WHERE oid <> 0
				;
				

				INSERT INTO activite.t_lits(oid, code_original, code, texte, texte_court, etage_id, chambre_particuliere) 
				SELECT 0, 0, '', 'Non renseigné', 'Non renseigné', 0, 'N' WHERE NOT EXISTS (SELECT 1 FROM activite.t_lits WHERE oid = 0); 
                
                -- Mise à jour préalable des codes s'ils ont été modifiés dans l'environnement source
				SELECT base.cti_stash_table_constraints('activite.t_lits');
				
				DELETE FROM activite.t_lits WHERE code like '[CPX]' || '%' AND NOT EXISTS (SELECT 1 FROM activite.t_lits WHERE oid = t_lits.oid - [IPX]); 
				
				UPDATE activite.t_lits SET
                    code = '[CPX]' || w_lits.code,
                    code_original = w_lits.oid + [IPX],
                    texte = w_lits.texte,
                    texte_court = w_lits.texte_court,
                    chambre_particuliere = w_lits.chambre_particuliere,
                    etage_id = CASE WHEN w_lits.etage_id <> 0 THEN w_lits.etage_id + [IPX] ELSE 0 END
                FROM
                    t_lits w_lits
                WHERE 1=1
                    AND t_lits.oid = w_lits.oid + [IPX] 
                    AND (
						t_lits.code::text IS DISTINCT FROM ('[CPX]'::text || w_lits.code::text) OR
						t_lits.chambre_particuliere IS DISTINCT FROM w_lits.chambre_particuliere OR
						(w_lits.etage_id <> 0 AND t_lits.etage_id != (w_lits.etage_id + [IPX])))
				;

				SELECT base.cti_stash_pop_table_constraints('activite.t_lits');
    
                -- Ajout des nouveaux codes
                INSERT INTO activite.t_lits(
					oid,
					code,
					code_original,
					texte,
					texte_court,
					chambre_particuliere,
					etage_id)
				SELECT 
					oid + [IPX],
					'[CPX]' || code,  
					oid + [IPX] AS code_original,
					texte,
					texte_court,
					chambre_particuliere,
					CASE WHEN etage_id <> 0 THEN etage_id + [IPX] ELSE 0 END AS etage_id
				FROM t_lits
				WHERE oid <> 0 AND NOT EXISTS (SELECT 1 FROM activite.t_lits WHERE oid = t_lits.oid + [IPX]);
                
                INSERT INTO activite.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT 
					'[DBNAME]'::text AS from_database, 
                    'LIT'::text AS table_code, 
                    oid AS from_id, 
                    code AS from_code, 
                    oid + [IPX] AS to_oid, 
                    '[CPX]' || code AS to_code
				FROM t_lits
                WHERE oid <> 0
				;
				
				-- Historique des places
				DELETE FROM activite.t_etages_historique WHERE etage_id BETWEEN [IPX] AND [IPX] + 900000000000;
				INSERT INTO activite.t_etages_historique(
					etage_id, 
					date, 
					pole_occupation_id,
					nb_lits_theoriques, 
					nb_lits_cp_theoriques, 
					nb_lits_ouverts, 
					nb_lits_cp_ouverts, 
					est_ouvert_1, 
					est_ouvert_2, 
					est_ouvert_3, 
					est_ouvert_4, 
					est_ouvert_5, 
					est_ouvert_6, 
					est_ouvert_7, 
					heberge_hospitalises, 
					heberge_ambulatoires, 
					heberge_externes, 
					heberge_bebes,
					heberge_urgences, 
					date_fin,
					nb_box_ambulatoires_theoriques,
					nb_box_ambulatoires_ouverts,
					nb_box_seances_theoriques,
					nb_box_seances_ouverts)
				SELECT 
					etage_id  + [IPX], 
					date, 
					COALESCE(w_correspondances_pole.oid, 0) AS pole_occupation_id,
					nb_lits_theoriques, 
					nb_lits_cp_theoriques, 
					nb_lits_ouverts, 
					nb_lits_cp_ouverts, 
					est_ouvert_1, 
					est_ouvert_2, 
					est_ouvert_3, 
					est_ouvert_4, 
					est_ouvert_5, 
					est_ouvert_6, 
					est_ouvert_7, 
					heberge_hospitalises, 
					heberge_ambulatoires, 
					heberge_externes, 
					heberge_bebes,
					heberge_urgences, 
					date_fin,
					nb_box_ambulatoires_theoriques,
					nb_box_ambulatoires_ouverts,
					nb_box_seances_theoriques,
					nb_box_seances_ouverts
				FROM t_etages_historique
				LEFT JOIN w_correspondances_pole ON pole_occupation_id = w_correspondances_pole.oid_source;
				
				
				DELETE FROM activite.t_lit_historique WHERE lit_id BETWEEN [IPX] AND [IPX] + 900000000000;
				INSERT INTO activite.t_lit_historique(
					lit_id,
					date_debut,
					date_fin,
					etage_id)
								
				SELECT 
					lit_id  + [IPX], 
					date_debut, 
					date_fin,
					etage_id + [IPX]
				FROM t_lit_historique;

			]]></sqlcmd>
		</NODE>
				
		<NODE label="Facturation">
			<sqlcmd><![CDATA[
				
				-- Récupération tables sources
				
				DROP TABLE IF EXISTS w_t_rubriques_facturation;
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.t_rubriques_facturation
					', 'temp w_t_rubriques_facturation'
				);
				
				DROP TABLE IF EXISTS w_t_protocoles;
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.t_protocoles
					', 'temp w_t_protocoles'
				);

				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.t_prestations
					', 'temp t_prestations'
				);
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.t_compte
					', 'temp t_compte'
				);
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM base.t_ghs
					', 'temp t_ghs'
				);
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM base.t_ghm
					', 'temp t_ghm'
				);
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM base.t_gme
					', 'temp t_gme'
				);
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM base.t_ccam_regroupements
					', 'temp t_ccam_regroupements'
				);
				
				DROP TABLE IF EXISTS w_t_actes;
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM base.t_actes WHERE nomenclature <> ''CDAM''
					', 'temp w_t_actes'
				);
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM base.t_lpp
					', 'temp t_lpp'
				);
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM base.t_ucd
					', 'temp t_ucd'
				);
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM activite.t_rejets_noemie
					', 'temp t_rejets_noemie'
				);

				
				
				DROP TABLE IF EXISTS w_correspondances_ghs;
				CREATE TEMP TABLE w_correspondances_ghs AS
				SELECT 
					t_ghs_source.oid AS oid_source,
					coalesce(t_ghs.oid,0) AS oid
				FROM t_ghs AS t_ghs_source 
					LEFT JOIN base.t_ghs ON t_ghs_source.code = t_ghs.code;
				
				CREATE INDEX w_correspondances_ghs_1
				  ON w_correspondances_ghs
				  USING btree
				  (oid_source);
				  
				  
				  
				DROP TABLE IF EXISTS w_correspondances_ghm;
				CREATE TEMP TABLE w_correspondances_ghm AS
				SELECT 
					t_ghm_source.oid AS oid_source,
					coalesce(t_ghm.oid,0) AS oid
				FROM t_ghm AS t_ghm_source 
					LEFT JOIN base.t_ghm ON t_ghm_source.code = t_ghm.code;
				
				CREATE INDEX w_correspondances_ghm_1
				  ON w_correspondances_ghm
				  USING btree
				  (oid_source);
				  
				DROP TABLE IF EXISTS w_correspondances_gme;
				CREATE TEMP TABLE w_correspondances_gme AS
				SELECT 
					t_gme_source.oid AS oid_source,
					coalesce(t_gme.oid,0) AS oid
				FROM t_gme AS t_gme_source 
					LEFT JOIN base.t_gme ON t_gme_source.code = t_gme.code;
				
				CREATE INDEX w_correspondances_gme_1
				  ON w_correspondances_gme
				  USING btree
				  (oid_source);
				  
				 
				
				

				INSERT INTO activite.t_rubriques_facturation(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '', 'Non renseignée', 'Non renseignée' WHERE NOT EXISTS (SELECT 1 FROM activite.t_rubriques_facturation WHERE oid = 0); 

				UPDATE activite.t_rubriques_facturation SET
					code = '#'::text || '[CPX]' || src.code,
					texte = src.texte,
					texte_court = src.texte_court
				FROM w_t_rubriques_facturation src
				WHERE t_rubriques_facturation.oid = (src.oid + [IPX]) AND
					(
					t_rubriques_facturation.code IS DISTINCT FROM ('[CPX]' || src.code) OR
					t_rubriques_facturation.texte IS DISTINCT FROM src.texte OR
					t_rubriques_facturation.texte_court IS DISTINCT FROM src.texte_court
					)
				;
				
				UPDATE activite.t_rubriques_facturation SET
					code = '[CPX]' || src.code,
					texte = src.texte,
					texte_court = src.texte_court
				FROM w_t_rubriques_facturation src
				WHERE t_rubriques_facturation.oid = (src.oid + [IPX]) AND
					(
					t_rubriques_facturation.code IS DISTINCT FROM ('[CPX]' || src.code) OR
					t_rubriques_facturation.texte IS DISTINCT FROM src.texte OR
					t_rubriques_facturation.texte_court IS DISTINCT FROM src.texte_court
					)
				;

				INSERT INTO activite.t_rubriques_facturation(
					oid,
					code,
					code_original,
					texte,
					texte_court)
				SELECT 
					oid + [IPX],
					'[CPX]' || code,  
					oid + [IPX] AS code_original,
					texte,
					texte_court
				FROM w_t_rubriques_facturation
				WHERE oid <> 0 AND NOT EXISTS (SELECT 1 FROM activite.t_rubriques_facturation WHERE oid = w_t_rubriques_facturation.oid + [IPX]); 
                
                INSERT INTO activite.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT 
					'[DBNAME]'::text AS from_database, 
                    'RUBRIQUE'::text AS table_code, 
                    oid AS from_id, 
                    code AS from_code, 
                    oid + [IPX] AS to_oid, 
                    '[CPX]' || code AS to_code
				FROM w_t_rubriques_facturation
                WHERE oid <> 0
				;

				INSERT INTO activite.t_prestations(oid, code_original, code, texte, texte_court, clinique_honoraire) 
				SELECT 0, 0, '', 'Non renseignée', 'Non renseignée', '' WHERE NOT EXISTS (SELECT 1 FROM activite.t_prestations WHERE oid = 0); 

				INSERT INTO activite.t_prestations(
					code,
					code_original,
					texte,
					texte_court,
					clinique_honoraire)
				SELECT 
					code,  
					code_original,
					texte,
					texte_court,
					clinique_honoraire
				FROM t_prestations
				WHERE oid <> 0 AND
					NOT EXISTS (SELECT 1 FROM activite.t_prestations t_prestations_c WHERE t_prestations_c.code = t_prestations.code); 
				
				
				DROP TABLE IF EXISTS w_correspondances_prestations;
				CREATE TEMP TABLE w_correspondances_prestations AS
				SELECT 
					t_prestations_source.oid AS oid_source,
					coalesce(t_prestations.oid,0) AS oid
				FROM t_prestations AS t_prestations_source 
					LEFT JOIN activite.t_prestations ON t_prestations_source.code = t_prestations.code;
				
				CREATE INDEX w_correspondances_prestations_1
				  ON w_correspondances_prestations
				  USING btree
				  (oid_source);
				  
				
				INSERT INTO activite.t_compte(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '', 'Non renseignée', 'Non renseignée' WHERE NOT EXISTS (SELECT 1 FROM activite.t_compte WHERE oid = 0);  
				
				DROP TABLE IF EXISTS c_compte;
				CREATE TEMP TABLE c_compte AS
				SELECT
					oid,
					code,
					MAX(texte) as texte,
					MAX(texte_court) as texte_court,
					''::text AS texte_soundex,
					''::text AS to_code,
					0::bigint AS to_oid
				FROM t_compte
				GROUP BY 1,2;

				UPDATE c_compte SET texte_soundex = base.cti_soundex_nom(lower(texte));

				DROP TABLE IF EXISTS w_compte_to;
				CREATE TEMP TABLE w_compte_to AS
				SELECT
					*,
					''::text AS texte_soundex
				FROM activite.t_compte;

				UPDATE w_compte_to SET texte_soundex = base.cti_soundex_nom(lower(texte));

				UPDATE c_compte
				SET to_oid = oid + [IPX],
					to_code = code
				WHERE NOT EXISTS (SELECT 1 FROM w_compte_to WHERE code = c_compte.code);  

				UPDATE c_compte
				SET to_oid = w_compte_to.oid,
					to_code = c_compte.code
				FROM w_compte_to
				WHERE to_oid = 0 AND
					c_compte.code = w_compte_to.code AND
					c_compte.texte_soundex = w_compte_to.texte_soundex;

				UPDATE c_compte
				SET to_oid = oid + [IPX],
					to_code = code || '_[PX]'
				WHERE to_oid = 0;
				
				ALTER TABLE c_compte ADD CONSTRAINT c_compte_pkey PRIMARY KEY(oid);
				
				UPDATE activite.t_compte SET code_original = '#'||oid WHERE code_original <> oid::text;
				UPDATE activite.t_compte SET code_original = oid WHERE code_original <> oid::text;
				
				INSERT INTO activite.t_compte(
					oid,
					code,
					code_original,
					texte,
					texte_court)
				SELECT 
					to_oid,
					to_code,  
					to_oid::text AS code_original,
					texte,
					texte_court
				FROM c_compte
				WHERE oid != 0 AND NOT EXISTS (SELECT 1 FROM activite.t_compte WHERE t_compte.oid = c_compte.to_oid); 
				;				
				
				DROP TABLE IF EXISTS w_correspondances_compte;
				CREATE TEMP TABLE w_correspondances_compte AS
				SELECT 
					t_compte_source.oid AS oid_source,
					MAX(coalesce(t_compte.oid,0)) AS oid
				FROM c_compte AS t_compte_source 
					LEFT JOIN activite.t_compte ON t_compte_source.to_code = t_compte.code 
				GROUP BY 1;
				
				CREATE INDEX w_correspondances_compte_1
				  ON w_correspondances_compte
				  USING btree
				  (oid_source);
				
				
				  
				INSERT INTO activite.t_protocoles(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '', 'Non renseigné', 'Non renseigné' WHERE NOT EXISTS (SELECT 1 FROM activite.t_protocoles WHERE oid = 0); 
				
				UPDATE activite.t_protocoles SET
					code = '[CPX]'::text || prot_px.code::text,
					texte = prot_px.texte,
					texte_court = prot_px.texte
				FROM
					w_t_protocoles prot_px
				WHERE 1=1
					AND t_protocoles.oid = (prot_px.oid + [IPX])
					AND ((t_protocoles.code::text <> ('[CPX]'::text || prot_px.code::text)) OR (t_protocoles.texte <> prot_px.texte));

				INSERT INTO activite.t_protocoles(
					oid,
					code,
					code_original,
					texte,
					texte_court)
				SELECT 
					oid + [IPX],
					'[CPX]' || code,  
					oid + [IPX] AS code_original,
					texte,
					texte_court
				FROM w_t_protocoles
				WHERE oid <> 0 AND NOT EXISTS (SELECT 1 FROM activite.t_protocoles WHERE oid = w_t_protocoles.oid + [IPX])
				AND NOT EXISTS (SELECT 1 FROM activite.t_protocoles WHERE activite.t_protocoles.code = '[CPX]' || w_t_protocoles.code); 
				 
				 
				
				DROP TABLE IF EXISTS w_correspondances_ccam_regroupements;
				CREATE TEMP TABLE w_correspondances_ccam_regroupements AS
				SELECT 
					t_ccam_regroupements_source.oid AS oid_source,
					coalesce(t_ccam_regroupements.oid,0) AS oid
				FROM t_ccam_regroupements AS t_ccam_regroupements_source 
					LEFT JOIN base.t_ccam_regroupements ON t_ccam_regroupements_source.code = t_ccam_regroupements.code;
				
				CREATE INDEX w_correspondances_ccam_regroupements_1
				  ON w_correspondances_ccam_regroupements
				  USING btree
				  (oid_source);
				  
				  
				
				INSERT INTO base.t_actes(
					code,
					texte,
					texte_court,
					nomenclature,
					ccam_regroupement_id_1,
					ccam_regroupement_id_4)
				SELECT 
					code,  
					texte,
					texte_court,
					nomenclature,
					COALESCE(w_correspondances_ccam_regroupements_1.oid,0),
					COALESCE(w_correspondances_ccam_regroupements_4.oid,0)
				FROM w_t_actes
				LEFT JOIN w_correspondances_ccam_regroupements w_correspondances_ccam_regroupements_1 ON ccam_regroupement_id_1 = w_correspondances_ccam_regroupements_1.oid_source
				LEFT JOIN w_correspondances_ccam_regroupements w_correspondances_ccam_regroupements_4 ON ccam_regroupement_id_4 = w_correspondances_ccam_regroupements_4.oid_source
				WHERE w_t_actes.oid <> 0 AND
					NOT EXISTS (SELECT 1 FROM base.t_actes WHERE code = w_t_actes.code); 
				
				
				DROP TABLE IF EXISTS w_correspondances_actes;
				CREATE TEMP TABLE w_correspondances_actes AS
				SELECT 
					t_actes_source.oid AS oid_source,
					coalesce(t_actes.oid,0) AS oid
				FROM w_t_actes AS t_actes_source 
					LEFT JOIN base.t_actes ON t_actes_source.code = t_actes.code;
				
				CREATE INDEX w_correspondances_actes_1
				  ON w_correspondances_actes
				  USING btree
				  (oid_source);
				  
				
				INSERT INTO base.t_lpp(
					code,
					texte,
					texte_court)
				SELECT 
					code,  
					texte,
					texte_court
				FROM t_lpp
				WHERE t_lpp.oid <> 0 AND
					NOT EXISTS (SELECT 1 FROM base.t_lpp WHERE code = t_lpp.code); 
				
				
				DROP TABLE IF EXISTS w_correspondances_lpp;
				CREATE TEMP TABLE w_correspondances_lpp AS
				SELECT 
					t_lpp_source.oid AS oid_source,
					coalesce(t_lpp.oid,0) AS oid
				FROM t_lpp AS t_lpp_source 
					LEFT JOIN base.t_lpp ON t_lpp_source.code = t_lpp.code;
				
				CREATE INDEX w_correspondances_lpp_1
				  ON w_correspondances_lpp
				  USING btree
				  (oid_source);
				  
				  
				INSERT INTO base.t_ucd(
					code,
					texte,
					texte_court)
				SELECT 
					code,  
					texte,
					texte_court
				FROM t_ucd
				WHERE t_ucd.oid <> 0 AND
					NOT EXISTS (SELECT 1 FROM base.t_ucd WHERE code = t_ucd.code); 
				
				
				DROP TABLE IF EXISTS w_correspondances_ucd;
				CREATE TEMP TABLE w_correspondances_ucd AS
				SELECT 
					t_ucd_source.oid AS oid_source,
					coalesce(t_ucd.oid,0) AS oid
				FROM t_ucd AS t_ucd_source 
					LEFT JOIN base.t_ucd ON t_ucd_source.code = t_ucd.code;
				
				CREATE INDEX w_correspondances_ucd_1
				  ON w_correspondances_ucd
				  USING btree
				  (oid_source);
				  
				  
				
				INSERT INTO activite.t_rejets_noemie(
					code,
					texte)
				SELECT 
					code,  
					texte
				FROM t_rejets_noemie
				WHERE oid <> 0 AND NOT EXISTS (SELECT 1 FROM activite.t_rejets_noemie WHERE code || texte = t_rejets_noemie.code || t_rejets_noemie.texte);
				
				
				DROP TABLE IF EXISTS w_correspondances_rejets_noemie;
				CREATE TEMP TABLE w_correspondances_rejets_noemie AS
				SELECT 
					t_rejets_noemie_source.oid AS oid_source,
					coalesce(t_rejets_noemie.oid,0) AS oid
				FROM t_rejets_noemie AS t_rejets_noemie_source 
					LEFT JOIN activite.t_rejets_noemie ON 
						t_rejets_noemie_source.code = t_rejets_noemie.code AND
						t_rejets_noemie_source.texte = t_rejets_noemie.texte;
				
				CREATE INDEX w_correspondances_rejets_noemie_1
				  ON w_correspondances_rejets_noemie
				  USING btree
				  (oid_source);
				  
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Listes et classes (Montant)">
            <condition><![CDATA[
                "[SENSPARAMETRE_CLASSE]" == "M"
            ]]></condition>
			<sqlcmd><![CDATA[
			
				-- Listes
			
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT 
							t_listes.oid,
							t_listes.code,
							t_listes_tables.code AS table_code,
							''0''::text AS ok_liste
						FROM activite.t_listes
						JOIN activite.t_listes_tables ON t_listes.table_id = t_listes_tables.oid
						WHERE EXISTS (SELECT 1 FROM activite.t_listes_contenu WHERE liste_id = t_listes.oid) AND
							contenu_by_code <> ''1'' AND t_listes.code <> ''''
					', 'temp e_listes'
				);
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT * FROM activite.t_listes_contenu
					', 'temp e_listes_contenu'
				);
				
				UPDATE e_listes
				SET ok_liste = '1'
				WHERE table_code IN ('MEDECIN','RUBRIQUE','ETAGE','SERVICE_F','COMPTE') AND
					EXISTS (SELECT 1 FROM activite.t_listes WHERE code = e_listes.code)
				;


				DELETE FROM activite.t_listes_contenu
				WHERE EXISTS 
				(
					SELECT 1 FROM activite.t_listes
					JOIN activite.t_listes_tables ON t_listes.table_id = t_listes_tables.oid
					JOIN e_listes ON t_listes.code = e_listes.code AND 
						t_listes_tables.code = table_code
					WHERE ok_liste = '1' AND activite.t_listes_contenu.liste_id = t_listes.oid
				) AND
					to_id BETWEEN [IPX] AND ([IPX] + 999999999999)
				;

				
				
				INSERT INTO activite.t_listes_contenu (liste_id, to_id)
				SELECT 
					t_listes.oid,
					[IPX] + to_id
				FROM e_listes_contenu
				JOIN e_listes ON e_listes_contenu.liste_id = e_listes.oid
				JOIN activite.t_listes ON t_listes.code = e_listes.code
				JOIN activite.t_listes_tables ON t_listes.table_id = t_listes_tables.oid AND t_listes_tables.code = e_listes.table_code
				WHERE ok_liste = '1'
				;
				
				
				
				
				-- Classes
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT 
							t_classes.oid,
							t_classes.code,
							t_listes_tables.code AS table_code,
							''0''::text AS ok_classe
						FROM activite.t_classes
						JOIN activite.t_listes_tables ON t_classes.table_id = t_listes_tables.oid
						WHERE EXISTS 
							(
								SELECT 1 
								FROM activite.t_classes_sections 
								JOIN activite.t_classes_sections_elements ON section_id = t_classes_sections.oid 
								WHERE classe_id = t_classes.oid
							) 
						AND t_classes.code <> '''' 
						AND contenu_by_code <> ''1''
					', 'temp e_classes'
				);


				UPDATE e_classes
					SET ok_classe = '1'
					WHERE table_code IN ('MEDECIN','RUBRIQUE','ETAGE','SERVICE_F','COMPTE') AND
						EXISTS (SELECT 1 FROM activite.t_classes WHERE code = e_classes.code)
					;



				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT 
							classe_id,
							t_classes_sections.oid,
							t_classes_sections.code,
							t_classes_sections.texte
						FROM activite.t_classes_sections
						
					', 'temp e_classes_sections'
				);


				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT 
							section_id,
							t_classes_sections_elements.to_id
						FROM activite.t_classes_sections_elements
						
					', 'temp e_classes_sections_elements'
				);


                INSERT INTO activite.t_classes_sections (code, texte, classe_id)
                SELECT e_classes_sections.code, e_classes_sections.texte, t_classes.oid
                FROM e_classes_sections
                JOIN e_classes ON e_classes_sections.classe_id = e_classes.oid AND e_classes.ok_classe = '1'
                JOIN activite.t_listes_tables ON e_classes.table_code = t_listes_tables.code
                JOIN activite.t_classes ON e_classes.code = t_classes.code AND t_classes.table_id = t_listes_tables.oid
                LEFT JOIN activite.t_classes_sections ON t_classes.oid = t_classes_sections.classe_id AND 
                    t_classes_sections.code = e_classes_sections.code
                    WHERE t_classes_sections.oid IS NULL
                ;


				DELETE FROM activite.t_classes_sections_elements
					WHERE EXISTS
					(
					SELECT 1
					FROM activite.t_classes
					JOIN activite.t_listes_tables ON t_classes.table_id = t_listes_tables.oid
					JOIN e_classes ON t_classes.code = e_classes.code AND 
						t_listes_tables.code = table_code
					JOIN activite.t_classes_sections ON t_classes_sections.classe_id = t_classes.oid
					WHERE ok_classe = '1' AND section_id = t_classes_sections.oid
					) AND
					to_id BETWEEN [IPX] AND ([IPX] + 999999999999)
					;


				INSERT INTO activite.t_classes_sections_elements (section_id, to_id)
				SELECT
					t_classes_sections.oid,
					[IPX] + to_id
				FROM e_classes_sections_elements
				JOIN e_classes_sections ON e_classes_sections_elements.section_id = e_classes_sections.oid
				JOIN e_classes ON e_classes.oid = e_classes_sections.classe_id AND ok_classe = '1'
				JOIN activite.t_classes ON e_classes.code = t_classes.code 
				JOIN activite.t_listes_tables ON t_classes.table_id = t_listes_tables.oid AND 
					e_classes.table_code = t_listes_tables.code
				JOIN activite.t_classes_sections ON t_classes_sections.classe_id = t_classes.oid AND
					t_classes_sections.code = e_classes_sections.code
				GROUP BY 1,2
				;
			
			
		]]></sqlcmd>
		</NODE>
		
        <NODE label="Listes et classes (Descendant)">
            <condition><![CDATA[
                "[SENSPARAMETRE_CLASSE]" == "D"
            ]]></condition>
			<sqlcmd><![CDATA[
			
				-- Listes
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT 
							t_listes.oid,
							t_listes.code,
							t_listes_tables.code AS table_code,
							''0''::text AS ok_liste
						FROM activite.t_listes
						JOIN activite.t_listes_tables ON t_listes.table_id = t_listes_tables.oid
						WHERE t_listes.code <> '''' AND contenu_by_code <> ''1''
					', 'temp e_listes'
				);
				
				UPDATE e_listes
				SET ok_liste = '1'
				WHERE table_code IN ('MEDECIN','RUBRIQUE','ETAGE','SERVICE_F','UF') AND 
					EXISTS (SELECT 1 FROM activite.t_listes WHERE code = e_listes.code)
				;
                
                -- Recopie contenu vers environnement établissement
                SELECT dblink_exec('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', 
					(
					SELECT base.cti_group_concat_without_sep(sqlcmd)
					FROM
						(
						SELECT '1', 'DELETE FROM activite.t_listes_contenu WHERE liste_id IN ('||
						(SELECT base.cti_group_concat(oid::text) FROM e_listes WHERE ok_liste = '1')||
						')., ' AS sqlcmd
						UNION
						SELECT '2', 'INSERT INTO activite.t_listes_contenu(liste_id, to_id) VALUES('||e_listes.oid||', '||t_listes_contenu.to_id - [IPX]||')., '
						FROM activite.t_listes
						JOIN activite.t_listes_tables On t_listes.table_id = t_listes_tables.oid
						JOIN e_listes ON e_listes.code = t_listes.code AND
							e_listes.table_code = t_listes_tables.code
						JOIN activite.t_listes_contenu ON t_listes.oid = t_listes_contenu.liste_id AND
							to_id BETWEEN [IPX] AND ([IPX] + 999999999999)
						WHERE ok_liste = '1'
						ORDER BY 1
						) subview
					),
					true);
                    
                    
                -- classes
                SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT 
							t_classes.oid,
							t_classes.code,
							t_listes_tables.code AS table_code,
							''0''::text AS ok_classe
						FROM activite.t_classes
						JOIN activite.t_listes_tables ON t_classes.table_id = t_listes_tables.oid
						WHERE contenu_by_code <> ''1'' AND t_classes.code <> ''''
					', 'temp e_classes'
				);

				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT 
							classe_id,
							t_classes_sections.oid,
							t_classes_sections.code,
							t_classes_sections.texte
						FROM activite.t_classes_sections
						
					', 'temp e_classes_sections'
				);



				UPDATE e_classes
                SET ok_classe = '1'
                WHERE table_code IN ('MEDECIN','RUBRIQUE','ETAGE','SERVICE_F','UF','UM') AND
                    EXISTS (SELECT 1 FROM activite.t_classes WHERE code = e_classes.code) AND
					code NOT IN ('CTI_MEDLIBSAL')
                ;
				
				-- Creation des sections inexistantes
				SELECT dblink_exec('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', 
                (
                SELECT base.cti_group_concat_without_sep(sqlcmd)
                FROM
                    (
                    SELECT '1', 'INSERT INTO activite.t_classes_sections (classe_id, code, texte) VALUES('||e_classes.oid||', '''||replace(t_classes_sections.code,'''','''''')||''','''||replace(t_classes_sections.texte,'''','''''')||''').,' AS sqlcmd
					FROM activite.t_classes
                    JOIN activite.t_classes_sections ON t_classes_sections.classe_id = t_classes.oid
                    JOIN activite.t_listes_tables ON t_classes.table_id = t_listes_tables.oid
                    JOIN e_classes ON t_classes.code = e_classes.code AND
                        e_classes.table_code = t_listes_tables.code AND
                        ok_classe = '1'
                    LEFT JOIN e_classes_sections ON e_classes_sections.code = t_classes_sections.code AND
                        e_classes_sections.classe_id = e_classes.oid
                    WHERE e_classes_sections.oid IS NULL
					UNION
                    SELECT '2', 'UPDATE activite.t_classes_sections SET texte = ''' || replace(t_classes_sections.texte,'''','''''')||''' WHERE oid = ' || e_classes_sections.oid || '.,' AS sqlcmd
					FROM activite.t_classes
                    JOIN activite.t_classes_sections ON t_classes_sections.classe_id = t_classes.oid
                    JOIN activite.t_listes_tables ON t_classes.table_id = t_listes_tables.oid
                    JOIN e_classes ON t_classes.code = e_classes.code AND
                        e_classes.table_code = t_listes_tables.code AND
                        ok_classe = '1'
                    JOIN e_classes_sections ON e_classes_sections.code = t_classes_sections.code AND
                        e_classes_sections.classe_id = e_classes.oid
                    WHERE e_classes_sections.texte IS DISTINCT FROM t_classes_sections.texte
					ORDER BY 1
                    ) subview
                ),
                true)
                ;
				
				SELECT base.cti_copy_table('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT 
							classe_id,
							t_classes_sections.oid,
							t_classes_sections.code,
							t_classes_sections.texte
						FROM activite.t_classes_sections
						
					', 'temp e_classes_sections'
				);

                SELECT dblink_exec('dbname=[DBNAME] port=[DBPORT] host=[DBHOST] user=[DBUSERNAME] password=[DBPASSWORD]', 
                (
                SELECT base.cti_group_concat_without_sep(sqlcmd)
                FROM
                    (
                    SELECT '1', 'DELETE FROM activite.t_classes_sections_elements WHERE section_id IN ('||
                    (SELECT base.cti_group_concat(e_classes_sections.oid::text) FROM e_classes_sections JOIN e_classes ON e_classes.oid = e_classes_sections.classe_id WHERE ok_classe = '1')||
                    ')., ' AS sqlcmd
                    UNION
                    SELECT '2', 'INSERT INTO activite.t_classes_sections_elements(section_id, to_id) VALUES('||e_classes_sections.oid||', '||t_classes_sections_elements.to_id - [IPX] || ')., '
                    FROM activite.t_classes
                    JOIN activite.t_classes_sections ON t_classes_sections.classe_id = t_classes.oid
                    JOIN activite.t_classes_sections_elements ON t_classes_sections_elements.section_id = t_classes_sections.oid
                    JOIN activite.t_listes_tables ON t_classes.table_id = t_listes_tables.oid
                    JOIN e_classes ON t_classes.code = e_classes.code AND
                        e_classes.table_code = t_listes_tables.code AND
                        ok_classe = '1'
                    JOIN e_classes_sections ON e_classes_sections.code = t_classes_sections.code AND
                        e_classes_sections.classe_id = e_classes.oid
                    WHERE t_classes_sections_elements.to_id BETWEEN [IPX] AND ([IPX] + 999999999999)
                ORDER BY 1
                    ) subview
                ),
                true)
                ;

			
		]]></sqlcmd>
		</NODE>
		
	</NODE>
		
	<NODE name="POST" label="TRAITEMENTS COMPLEMENTAIRES">
		
		<NODE label="Chiffrier CTI">
			<sqlcmd><![CDATA[

				TRUNCATE activite.p_chiffrier
				;

				INSERT INTO activite.p_chiffrier (mois)
				SELECT mois
				FROM (
					SELECT DISTINCT to_char(date_sortie, 'YYYYMM')::numeric AS mois FROM activite.p_sejours
					WHERE date_sortie >= 20100101 AND to_char(date_sortie, 'YYYYMM')::numeric <= to_char(now() + interval '1 month', 'YYYYMM')::numeric
				) subview
				;

				DELETE FROM activite.p_chiffrier WHERE mois > to_char(now() + interval '1 month', 'YYYYMM')::numeric
				;

			]]></sqlcmd>
		</NODE>

		<NODE label="Compléments factures">
			<sqlcmd><![CDATA[
	
				-- Recalcul des retards groupage (moyens) en prenant le max par etablissement et type séjour
				-- pour que le total soit coherent
				DROP TABLE IF EXISTS w_retard_groupage_coefficient;
				CREATE TEMP TABLE w_retard_groupage_coefficient AS
				SELECT 
					type_sejour,
					base.cti_division(MAX(retard_groupage) , SUM(nb)) AS retard_groupage
				FROM 
					(
						SELECT finess, 
							type_sejour, 
							SUM(retard_groupage) AS retard_groupage, 
							count(*) AS nb
						FROM activite.p_factures_reference
						JOIN activite.p_sejours ON p_factures_reference.no_sejour = p_sejours.no_sejour
						WHERE retard_groupage <> 0
						GROUP BY 1,2
						ORDER BY 1,2
					) subview
				GROUP BY 1;

				UPDATE activite.p_factures_reference
				SET retard_groupage = w_retard_groupage_coefficient.retard_groupage
				FROM activite.p_sejours
				JOIN w_retard_groupage_coefficient ON w_retard_groupage_coefficient.type_sejour = p_sejours.type_sejour
				WHERE p_factures_reference.no_sejour = p_sejours.no_sejour AND
					 p_factures_reference.retard_groupage <> 0;

				UPDATE activite.p_factures_reference
				SET retard_groupage = w_retard_groupage_coefficient.retard_groupage
				FROM activite.p_sejours
				JOIN w_retard_groupage_coefficient ON w_retard_groupage_coefficient.type_sejour = p_sejours.type_sejour
				WHERE p_factures_reference.no_sejour = p_sejours.no_sejour AND
					 p_factures_reference.retard_groupage <> 0;

					 
					 
					 
				-- Recalcul des retards facture (moyens) en prenant le max par etablissement et type séjour
				-- pour que le total soit coherent
				DROP TABLE IF EXISTS w_retard_facture_coefficient;
				CREATE TEMP TABLE w_retard_facture_coefficient AS
				SELECT 
					type_sejour,
					base.cti_division(MAX(retard_facture) , SUM(nb)) AS retard_facture
				FROM 
					(
						SELECT finess, 
							type_sejour, 
							SUM(retard_facture) AS retard_facture, 
							count(*) AS nb
						FROM activite.p_factures_reference
						JOIN activite.p_sejours ON p_factures_reference.no_sejour = p_sejours.no_sejour
						WHERE retard_facture <> 0
						GROUP BY 1,2
						ORDER BY 1,2
					) subview
				GROUP BY 1;

				UPDATE activite.p_factures_reference
				SET retard_facture = w_retard_facture_coefficient.retard_facture
				FROM activite.p_sejours
				JOIN w_retard_facture_coefficient ON w_retard_facture_coefficient.type_sejour = p_sejours.type_sejour
				WHERE p_factures_reference.no_sejour = p_sejours.no_sejour AND
					 p_factures_reference.retard_facture <> 0;

				UPDATE activite.p_factures_reference
				SET retard_facture = w_retard_facture_coefficient.retard_facture
				FROM activite.p_sejours
				JOIN w_retard_facture_coefficient ON w_retard_facture_coefficient.type_sejour = p_sejours.type_sejour
				WHERE p_factures_reference.no_sejour = p_sejours.no_sejour AND
					 p_factures_reference.retard_facture <> 0;
					
			]]></sqlcmd>
		</NODE>

	
		<NODE label="Référencements">
			<sqlcmd><![CDATA[

				INSERT INTO activite.p_oids (code_table, oid) 
				SELECT 'dursej', date_sortie - date_entree 
				FROM activite.p_sejours 
				WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'dursej' AND oid = date_sortie - date_entree)
				AND date_sortie >= date_entree 
				AND date_sortie < '2099-12-31'::date
				GROUP BY 2
				ORDER BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid) 
				SELECT 'c_postaux', code_postal_id 
				FROM activite.p_sejours 
				WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'c_postaux' AND oid = code_postal_id)
				GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid) 
				SELECT 'medecin_s', medecin_sejour_id 
				FROM activite.p_sejours 
				WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'medecin_s' AND oid = medecin_sejour_id)
				GROUP BY 2;
								
				INSERT INTO activite.p_oids (code_table, oid) 
				SELECT 'medecin_s', medecin_sejour_id 
				FROM activite.p_mouvements_sejour 
				WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'medecin_s' AND oid = medecin_sejour_id)
				GROUP BY 2;
                
                INSERT INTO activite.p_oids (code_table, oid) 
                SELECT 'forme_activite', forme_activite_id 
                FROM activite.p_sejours 
                WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'forme_activite' AND oid = forme_activite_id)
                GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid) 
				SELECT 'ghs', ghs_id 
				FROM activite.p_sejours 
				WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'ghs' AND oid = ghs_id)
				GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid) 
				SELECT 'ghs', ghs_id 
				FROM activite.p_factures 
				WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'ghs' AND oid = ghs_id)
				GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid) 
				SELECT 'medecin_x', medecin_facture_id 
				FROM activite.p_factures_lignes_h 
				WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'medecin_x' AND oid = medecin_facture_id)
				GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'rubrique_c', rubrique_facturation_id
				FROM activite.p_factures_lignes_c WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'rubrique_c' AND oid = rubrique_facturation_id)
				GROUP BY 2;
	
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'rubrique_c', rubrique_comptabilisation_id
				FROM activite.p_factures_lignes_c WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'rubrique_c' AND oid = rubrique_comptabilisation_id)
				GROUP BY 2;
	
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'rubrique_c', rubrique_comptabilisee_id
				FROM activite.p_factures_soldes_c WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'rubrique_c' AND oid = rubrique_comptabilisee_id)
				GROUP BY 2;
	
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'prestation_c', prestation_id
				FROM activite.p_factures_lignes_c WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'prestation_c' AND oid = prestation_id)
				GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'lpp', lpp_id
				FROM activite.p_factures_lignes_c WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'lpp' AND oid = lpp_id)
				GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'ucd', ucd_id
				FROM activite.p_factures_lignes_c WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'ucd' AND oid = ucd_id)
				GROUP BY 2;
				
				
				
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'prestation_c', prestation_id
				FROM activite.p_factures_soldes_c WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'prestation_c' AND oid = prestation_id)
				GROUP BY 2;
				
				
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'prestation_h', prestation_id
				FROM activite.p_factures_lignes_h WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'prestation_h' AND oid = prestation_id)
				GROUP BY 2;
				
				
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'acte_h', acte_id
				FROM activite.p_factures_lignes_h WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'acte_h' AND oid = acte_id)
				GROUP BY 2;
				
				
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'tiers_payant', tiers_payant_0_id
				FROM activite.p_factures WHERE tiers_payant_0_id <> 0 AND NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'tiers_payant' AND oid = tiers_payant_0_id)
				GROUP BY 2;
				
				
				
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'tiers_payant', tiers_payant_1_id
				FROM activite.p_factures WHERE tiers_payant_1_id <> 0 AND NOT EXISTS (SELECT 1
				FROM activite.p_oids WHERE code_table = 'tiers_payant' AND oid = tiers_payant_1_id)
				GROUP BY 2;
				
				
				
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'tiers_payant', tiers_payant_2_id
				FROM activite.p_factures WHERE tiers_payant_2_id <> 0 AND NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'tiers_payant' AND oid = tiers_payant_2_id)
				GROUP BY 2;
				
				
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'tiers_payant', tiers_payant_22_id
				FROM activite.p_factures WHERE tiers_payant_22_id <> 0 AND NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'tiers_payant' AND oid = tiers_payant_22_id)
				GROUP BY 2;
				
				
				INSERT INTO activite.p_oids (code_table, oid) 
				SELECT 'mode_traitement', t_services_facturation.mode_traitement_id
				FROM activite.t_lieux JOIN activite.t_services_facturation ON service_facturation_id = t_services_facturation.oid
				WHERE 
					NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'mode_traitement' AND oid IS NOT NULL AND oid = t_services_facturation.mode_traitement_id) AND t_services_facturation.mode_traitement_id IS NOT NULL
				GROUP BY 2;
	
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'lit', lit_id
				FROM activite.t_lieux WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'lit' AND oid = lit_id)
				GROUP BY 2;
				

					
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'service', service_facturation_id
				FROM activite.t_lieux WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'service' AND oid = service_facturation_id)
				GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid) 
						SELECT 'etage', etage_id
				FROM activite.t_lieux, activite.t_lits WHERE lit_id = t_lits.oid AND NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'etage' AND oid = etage_id)
				GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid) 
				SELECT 'uf', unite_fonctionnelle_id
				FROM activite.t_lieux WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'uf' AND oid = unite_fonctionnelle_id)
				GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid) 
				SELECT 'um', unite_medicale_id
				FROM activite.t_lieux WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'um' AND oid = unite_medicale_id)
				GROUP BY 2;
                
                INSERT INTO activite.p_oids (code_table, oid) 
                SELECT 'pole', pole_id 
                FROM activite.p_sejour_lieu
                WHERE NOT EXISTS (SELECT 1 FROM activite.p_oids WHERE code_table = 'pole' AND oid = pole_id)
                GROUP BY 2;
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Tables répliquées">
			<sqlcmd><![CDATA[
				
				SELECT base.analyze_schema('base')
				;
				SELECT base.analyze_schema('activite')
				;
										
			]]></sqlcmd>

			<NODE label="cti_reorganize_ghs_c">
				<sqlcmd><![CDATA[
				
					SELECT activite.cti_reorganize_ghs_c();
	
				]]></sqlcmd>
			</NODE>

			<NODE label="cti_reorganize_ghm_c">
				<sqlcmd><![CDATA[

					SELECT activite.cti_reorganize_ghm_c();
					
				]]></sqlcmd>
			</NODE>

			<NODE label="cti_reorganize_gme_c">
				<sqlcmd><![CDATA[
				
					SELECT activite.cti_reorganize_gme_c();
					
				]]></sqlcmd>
			</NODE>

			<NODE label="cti_reorganize_actes_c">
				<sqlcmd><![CDATA[
				
					SELECT activite.cti_reorganize_actes_c();
	
				]]></sqlcmd>
			</NODE>

			<NODE label="cti_reorganize_lieux_c">
				<sqlcmd><![CDATA[

					SELECT activite.cti_reorganize_lieux_c();

				]]></sqlcmd>
			</NODE>

			<NODE label="cti_reorganize_medecins_administratifs_c">
				<sqlcmd><![CDATA[

					SELECT activite.cti_reorganize_medecins_administratifs_c();
					
				]]></sqlcmd>
			</NODE>

			<NODE label="cti_reorganize_tiers_payant">
				<sqlcmd><![CDATA[

					SELECT activite.cti_reorganize_tiers_payant();

				]]></sqlcmd>
			</NODE>

			<NODE label="cti_reorganize_ages_c">
				<sqlcmd><![CDATA[
				
					SELECT activite.cti_reorganize_ages_c();
					
				]]></sqlcmd>
			</NODE>

			<NODE label="cti_reorganize_codes_postaux_c">
				<sqlcmd><![CDATA[
				
					SELECT base.cti_reorganize_codes_postaux_c('activite');
					
				]]></sqlcmd>
			</NODE>

			<NODE label="cti_reorganize_durees_sejour_c">
				<sqlcmd><![CDATA[
					
					SELECT activite.cti_reorganize_durees_sejour_c();
					
				]]></sqlcmd>
			</NODE>

			<NODE label="cti_reorganize_finess_c">
				<sqlcmd><![CDATA[
				
					SELECT activite.cti_reorganize_finess_c();
					
				]]></sqlcmd>
			</NODE>

			<NODE label="cti_reorganize_ucd_lpp_c">
				<sqlcmd><![CDATA[

					SELECT activite.cti_reorganize_ucd_lpp_c();
					
				]]></sqlcmd>
			</NODE>

			<NODE label="cti_reorganize_controle_chiffrier">
				<sqlcmd><![CDATA[
				
					SELECT activite.cti_reorganize_controle_chiffrier();
					
				]]></sqlcmd>
			</NODE>

			<NODE label="cti_update_schema_classes">
				<sqlcmd><![CDATA[

					SELECT activite.cti_update_schema_classes('*ALL');
					
				]]></sqlcmd>
			</NODE>

			<NODE label="cti_now">
				<sqlcmd><![CDATA[
					
					SELECT activite.cti_now('NOW','FORCE');
					
				]]></sqlcmd>
			</NODE>

			<NODE label="cti_reorganize_rubrique_facture_c">
				<sqlcmd><![CDATA[
				
					SELECT activite.cti_reorganize_rubrique_facture_c('*NONE');
				
				]]></sqlcmd>
			</NODE>

			<NODE label="cti_budget_generate_keys">
				<sqlcmd><![CDATA[

					SELECT activite.cti_budget_generate_keys();
					
				]]></sqlcmd>
			</NODE>

			<NODE label="cti_budget_execute_rules">
				<sqlcmd><![CDATA[

					SELECT activite.cti_budget_execute_rules();

				]]></sqlcmd>
			</NODE>

			<NODE label="Insertion des Pôles">
				<sqlcmd><![CDATA[

					TRUNCATE activite.t_pole;

					INSERT INTO activite.t_pole (oid, code, texte, texte_court, section_id, section_code, section_texte)
					SELECT oid, code, texte, texte_court, section_id, section_code, section_texte
					FROM base.t_pole
					;

				]]></sqlcmd>
			</NODE>

		</NODE>
	</NODE>
	
	<NODE name="VACUUM" label="REORGANISATION BASE DE DONNEES">
			
		<NODE label="Réorganisation tables">
			
			<sqlcmd><![CDATA[
			
				SELECT base.cti_reindex_schema('activite');
							
				SELECT base.analyze_schema('base')
				;
				SELECT base.analyze_schema('activite')
				;
			
				VACUUM ANALYSE activite.p_chiffrier;
				VACUUM ANALYSE activite.p_chiffrier_comptable;
				VACUUM ANALYSE activite.p_chiffrier_shs;
				VACUUM ANALYSE activite.p_factures;
				VACUUM ANALYSE activite.p_factures_comptables;
				VACUUM ANALYSE activite.p_factures_encours;
				VACUUM ANALYSE activite.p_factures_encours_lignes_c;
				VACUUM ANALYSE activite.p_factures_lignes_c;
				VACUUM ANALYSE activite.p_factures_lignes_h;
				VACUUM ANALYSE activite.p_factures_lignes_non_facturees_c;
				VACUUM ANALYSE activite.p_factures_lignes_non_facturees_h;
				VACUUM ANALYSE activite.p_factures_rejets;
				VACUUM ANALYSE activite.p_factures_soldes_c;
				VACUUM ANALYSE activite.p_factures_soldes_h;
				VACUUM ANALYSE activite.p_intervention;
				VACUUM ANALYSE activite.p_mouvements_sejour;
				VACUUM ANALYSE activite.p_oids;
				VACUUM ANALYSE activite.p_patients;
				VACUUM ANALYSE activite.p_sejours;
				VACUUM ANALYSE activite.p_sejours_assurance;
                VACUUM ANALYSE activite.p_sejour_effet_tarif;
				VACUUM ANALYSE activite.t_activites;
				VACUUM ANALYSE activite.t_classes;
				VACUUM ANALYSE activite.t_classes_sections;
				VACUUM ANALYSE activite.t_classes_sections_elements;
				VACUUM ANALYSE activite.t_etages;
				VACUUM ANALYSE activite.t_ghs_c;
				VACUUM ANALYSE activite.t_indicateurs;
				VACUUM ANALYSE activite.t_lieux;
				VACUUM ANALYSE activite.t_lieux_c;
				VACUUM ANALYSE activite.t_listes;
				VACUUM ANALYSE activite.t_listes_contenu;
				VACUUM ANALYSE activite.t_listes_tables;
				VACUUM ANALYSE activite.t_lits;
				VACUUM ANALYSE activite.t_medecins_administratifs;
				VACUUM ANALYSE activite.t_medecins_administratifs_c;
				VACUUM ANALYSE activite.t_prestations;
				VACUUM ANALYSE activite.t_rapports;
				VACUUM ANALYSE activite.t_rapports_rubriques;
				VACUUM ANALYSE activite.t_rejets_noemie;
				VACUUM ANALYSE activite.t_rubriques_facturation;
				VACUUM ANALYSE activite.t_services_facturation;
				VACUUM ANALYSE activite.t_specialites_medecin;
				VACUUM ANALYSE activite.t_tiers_payant;
				VACUUM ANALYSE activite.t_types_tiers_payant;
				VACUUM ANALYSE activite.t_unites_fonctionnelles;
				VACUUM ANALYSE activite.t_unites_medicales;
				
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Vide cache">
			<sqlcmd><![CDATA[
			
				SELECT cti_cache_reload as log from cache.cti_cache_reload('iCTI_activite', true);
				
			]]></sqlcmd>
		</NODE>
			

	</NODE>
	
	
	<NODE name="RAZ" label="RAZ BASE">
	
		<NODE label="Parametres">
			<sqlcmd><![CDATA[
							
					SELECT activite.cti_initialize_tables();
				
			]]></sqlcmd>
		</NODE>
	
	
		<NODE label="Nettoyage des tables">
			<sqlcmd><![CDATA[
			
				TRUNCATE activite.p_chiffrier_comptable;
				
				TRUNCATE activite.p_factures_comptables;

				TRUNCATE activite.p_patients;
				
				TRUNCATE activite.p_sejours;
				
				TRUNCATE activite.p_sejours_assurance;
				
				TRUNCATE activite.p_sejours_transferts;
				
				TRUNCATE activite.p_sejour_lieu;
				
				ALTER SEQUENCE activite.s_sejours RESTART WITH 1;
				
				TRUNCATE activite.p_mouvements_sejour;
                
                TRUNCATE activite.p_sejour_effet_tarif;
				
				TRUNCATE activite.p_intervention;
				
				TRUNCATE activite.t_lieux;
				
				ALTER SEQUENCE activite.s_lieux RESTART WITH 1;
				
				TRUNCATE activite.p_factures;
				
				ALTER SEQUENCE activite.s_factures RESTART WITH 1;

				TRUNCATE activite.p_factures_lignes_non_facturees_c;
				
				TRUNCATE activite.p_factures_lignes_non_facturees_h;
				
				TRUNCATE activite.p_factures_lignes_c;
				
				TRUNCATE activite.p_factures_lignes_h;
				
				TRUNCATE activite.p_factures_rejets;
				
				TRUNCATE activite.p_factures_soldes_c;
				TRUNCATE activite.p_facture_solde_tiers_c;
				
				TRUNCATE activite.p_factures_soldes_h;
				
				TRUNCATE activite.p_factures_comptables;
				
				TRUNCATE activite.p_factures_reference;
				TRUNCATE activite.p_factures_reference_tiers;
				
				TRUNCATE activite.p_intervention;
                
                TRUNCATE activite.p_consolidation_translation;
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Pré-initialisation">
			<sqlcmd><![CDATA[ 
				
				
				
			]]></sqlcmd>
		</NODE>
		
	</NODE>

	
	
	<NODE name="DISABLE_INDEX" label="DESACTIVATION INDEX">
	
		<NODE label="Séjours">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_disable_index('activite', 'i_sejours_1');
				SELECT base.cti_disable_index('activite', 'i_sejours_2');
				SELECT base.cti_disable_index('activite', 'i_sejours_3');
				SELECT base.cti_disable_index('activite', 'i_sejours_4');
				SELECT base.cti_disable_index('activite', 'i_sejours_5');
				SELECT base.cti_disable_index('activite', 'i_sejours_6');
				SELECT base.cti_disable_index('activite', 'i_sejours_7');
				SELECT base.cti_disable_index('activite', 'i_sejours_8');
				SELECT base.cti_disable_index('activite', 'i_sejours_9');
				SELECT base.cti_disable_index('activite', 'i_sejours_10');
				SELECT base.cti_disable_index('activite', 'i_sejours_11');
				SELECT base.cti_disable_index('activite', 'i_sejours_12');
				SELECT base.cti_disable_index('activite', 'i_sejours_13');
					
					
			]]></sqlcmd>
		</NODE>
					
		<NODE label="Mouvements">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_disable_index('activite', 'i_mouvements_sejour_1');
				SELECT base.cti_disable_index('activite', 'i_mouvements_sejour_2');
				SELECT base.cti_disable_index('activite', 'i_mouvements_sejour_3');
				SELECT base.cti_disable_index('activite', 'i_mouvements_sejour_4');
				SELECT base.cti_disable_index('activite', 'i_mouvements_sejour_5');

			]]></sqlcmd>
		</NODE>
		
		<NODE label="Interventions">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_disable_index('activite', 'i_intervention_1');
				SELECT base.cti_disable_index('activite', 'i_intervention_2');
				SELECT base.cti_disable_index('activite', 'i_intervention_3');
				SELECT base.cti_disable_index('activite', 'i_intervention_4');
				SELECT base.cti_disable_index('activite', 'i_intervention_5');

			]]></sqlcmd>
		</NODE>
		
		<NODE label="Factures">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_disable_index('activite', 'i_factures_2');
				SELECT base.cti_disable_index('activite', 'i_factures_3');
				SELECT base.cti_disable_index('activite', 'i_factures_4');
				SELECT base.cti_disable_index('activite', 'i_factures_5');
				SELECT base.cti_disable_index('activite', 'i_factures_6');
				SELECT base.cti_disable_index('activite', 'i_factures_7');
				SELECT base.cti_disable_index('activite', 'i_factures_8');
				SELECT base.cti_disable_index('activite', 'i_factures_9');
				SELECT base.cti_disable_index('activite', 'i_factures_10');
				SELECT base.cti_disable_index('activite', 'i_factures_11');
				SELECT base.cti_disable_index('activite', 'i_factures_12');
				SELECT base.cti_disable_index('activite', 'i_factures_13');
				SELECT base.cti_disable_index('activite', 'i_factures_14');

			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lignes factures clinique">
			<sqlcmd><![CDATA[
			
				TRUNCATE activite.t_rubrique_facture_c;
				
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_1');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_2');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_3');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_4');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_5');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_6');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_7');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_8');

			]]></sqlcmd>
		</NODE>
		
		<NODE label="Compte client clinique">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_c_1');
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_c_2');
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_c_3');
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_c_4');
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_c_8');

			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lignes factures honoraire">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_1');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_2');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_3');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_4');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_5');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_6');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_7');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_8');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_9');

			]]></sqlcmd>
		</NODE>
		
		<NODE label="Compte client honoraire">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_h_1');
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_h_3');
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_h_4');
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_h_8');

			]]></sqlcmd>
		</NODE>
		
		<NODE label="En-cours">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_disable_index('activite', 'i_factures_encours_1');
				SELECT base.cti_disable_index('activite', 'i_factures_encours_2');
				SELECT base.cti_disable_index('activite', 'i_factures_encours_4');
				SELECT base.cti_disable_index('activite', 'i_factures_encours_5');
				SELECT base.cti_disable_index('activite', 'i_factures_encours_6');
				SELECT base.cti_disable_index('activite', 'i_factures_encours_7');
				SELECT base.cti_disable_index('activite', 'i_factures_encours_8');
				SELECT base.cti_disable_index('activite', 'i_factures_encours_9');
				SELECT base.cti_disable_index('activite', 'i_factures_encours_10');
				SELECT base.cti_disable_index('activite', 'i_factures_encours_11');
				
				SELECT base.cti_disable_index('activite', 'i_factures_encours_lignes_c_1');
				SELECT base.cti_disable_index('activite', 'i_factures_encours_lignes_c_2');
				SELECT base.cti_disable_index('activite', 'i_factures_encours_lignes_c_3');
				SELECT base.cti_disable_index('activite', 'i_factures_encours_lignes_c_4');
				SELECT base.cti_disable_index('activite', 'i_factures_encours_lignes_c_5');
				SELECT base.cti_disable_index('activite', 'i_factures_encours_lignes_c_6');
				SELECT base.cti_disable_index('activite', 'i_factures_encours_lignes_c_7');
				SELECT base.cti_disable_index('activite', 'i_factures_encours_lignes_c_8');
				
				
				TRUNCATE activite.p_factures_encours;
				TRUNCATE activite.p_factures_encours_lignes_c;

			]]></sqlcmd>
		</NODE>
		
		<NODE label="Factures référence">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_disable_index('activite', 'i_factures_reference_1');
				SELECT base.cti_disable_index('activite', 'i_factures_reference_2');
				SELECT base.cti_disable_index('activite', 'i_factures_reference_3');
				SELECT base.cti_disable_index('activite', 'i_factures_reference_4');

				SELECT base.cti_disable_index('activite', 'i_factures_reference_tiers_1');
				SELECT base.cti_disable_index('activite', 'i_factures_reference_tiers_2');
				SELECT base.cti_disable_index('activite', 'i_factures_reference_tiers_3');
				SELECT base.cti_disable_index('activite', 'i_factures_reference_tiers_4');

			]]></sqlcmd>
		</NODE>
		
	</NODE>
	
	<NODE name="ENABLE_INDEX" label="REACTIVATION INDEX">
	
		<NODE label="Séjours">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_enable_index('activite', 'i_sejours_1');
				SELECT base.cti_enable_index('activite', 'i_sejours_2');
				SELECT base.cti_enable_index('activite', 'i_sejours_3');
				SELECT base.cti_enable_index('activite', 'i_sejours_4');
				SELECT base.cti_enable_index('activite', 'i_sejours_5');
				SELECT base.cti_enable_index('activite', 'i_sejours_6');
				SELECT base.cti_enable_index('activite', 'i_sejours_7');
				SELECT base.cti_enable_index('activite', 'i_sejours_8');
				SELECT base.cti_enable_index('activite', 'i_sejours_9');
				SELECT base.cti_enable_index('activite', 'i_sejours_10');
				SELECT base.cti_enable_index('activite', 'i_sejours_11');
				SELECT base.cti_enable_index('activite', 'i_sejours_12');
				SELECT base.cti_enable_index('activite', 'i_sejours_13');
					
					
			]]></sqlcmd>
		</NODE>
					
		<NODE label="Mouvements">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_enable_index('activite', 'i_mouvements_sejour_1');
				SELECT base.cti_enable_index('activite', 'i_mouvements_sejour_2');
				SELECT base.cti_enable_index('activite', 'i_mouvements_sejour_3');
				SELECT base.cti_enable_index('activite', 'i_mouvements_sejour_4');
				SELECT base.cti_enable_index('activite', 'i_mouvements_sejour_5');

			]]></sqlcmd>
		</NODE>
		
		<NODE label="Interventions">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_enable_index('activite', 'i_intervention_1');
				SELECT base.cti_enable_index('activite', 'i_intervention_2');
				SELECT base.cti_enable_index('activite', 'i_intervention_3');
				SELECT base.cti_enable_index('activite', 'i_intervention_4');
				SELECT base.cti_enable_index('activite', 'i_intervention_5');

			]]></sqlcmd>
		</NODE>
		
		<NODE label="Factures">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_enable_index('activite', 'i_factures_2');
				SELECT base.cti_enable_index('activite', 'i_factures_3');
				SELECT base.cti_enable_index('activite', 'i_factures_4');
				SELECT base.cti_enable_index('activite', 'i_factures_5');
				SELECT base.cti_enable_index('activite', 'i_factures_6');
				SELECT base.cti_enable_index('activite', 'i_factures_7');
				SELECT base.cti_enable_index('activite', 'i_factures_8');
				SELECT base.cti_enable_index('activite', 'i_factures_9');
				SELECT base.cti_enable_index('activite', 'i_factures_10');
				SELECT base.cti_enable_index('activite', 'i_factures_11');
				SELECT base.cti_enable_index('activite', 'i_factures_12');
				SELECT base.cti_enable_index('activite', 'i_factures_13');
				SELECT base.cti_enable_index('activite', 'i_factures_14');

			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lignes factures clinique">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_1');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_2');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_3');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_4');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_5');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_6');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_7');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_8');

			]]></sqlcmd>
		</NODE>
		
		<NODE label="Compte client clinique">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_c_1');
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_c_2');
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_c_3');
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_c_4');
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_c_8');

			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lignes factures honoraire">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_1');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_2');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_3');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_4');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_5');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_6');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_7');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_8');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_9');

			]]></sqlcmd>
		</NODE>
		
		<NODE label="Compte client honoraire">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_h_1');
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_h_3');
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_h_4');
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_h_8');

			]]></sqlcmd>
		</NODE>
		
		<NODE label="En-cours">
			<sqlcmd><![CDATA[
			
				SELECT base.cti_enable_index('activite', 'i_factures_encours_1');
				SELECT base.cti_enable_index('activite', 'i_factures_encours_2');
				SELECT base.cti_enable_index('activite', 'i_factures_encours_4');
				SELECT base.cti_enable_index('activite', 'i_factures_encours_5');
				SELECT base.cti_enable_index('activite', 'i_factures_encours_6');
				SELECT base.cti_enable_index('activite', 'i_factures_encours_7');
				SELECT base.cti_enable_index('activite', 'i_factures_encours_8');
				SELECT base.cti_enable_index('activite', 'i_factures_encours_9');
				SELECT base.cti_enable_index('activite', 'i_factures_encours_10');
				SELECT base.cti_enable_index('activite', 'i_factures_encours_11');
				
				SELECT base.cti_enable_index('activite', 'i_factures_encours_lignes_c_1');
				SELECT base.cti_enable_index('activite', 'i_factures_encours_lignes_c_2');
				SELECT base.cti_enable_index('activite', 'i_factures_encours_lignes_c_3');
				SELECT base.cti_enable_index('activite', 'i_factures_encours_lignes_c_4');
				SELECT base.cti_enable_index('activite', 'i_factures_encours_lignes_c_5');
				SELECT base.cti_enable_index('activite', 'i_factures_encours_lignes_c_6');
				SELECT base.cti_enable_index('activite', 'i_factures_encours_lignes_c_7');
				SELECT base.cti_enable_index('activite', 'i_factures_encours_lignes_c_8');


			]]></sqlcmd>
		</NODE>
		
		<NODE label="Factures référence">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_enable_index('activite', 'i_factures_reference_1');
				SELECT base.cti_enable_index('activite', 'i_factures_reference_2');
				SELECT base.cti_enable_index('activite', 'i_factures_reference_3');
				SELECT base.cti_enable_index('activite', 'i_factures_reference_4');

				SELECT base.cti_enable_index('activite', 'i_factures_reference_tiers_1');
				SELECT base.cti_enable_index('activite', 'i_factures_reference_tiers_2');
				SELECT base.cti_enable_index('activite', 'i_factures_reference_tiers_3');
				SELECT base.cti_enable_index('activite', 'i_factures_reference_tiers_4');

			]]></sqlcmd>
		</NODE>
		
	</NODE>
	
	
	
	
	
	
</ROOT>
