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

		
	<NODE name="INIT" label="INITIALISATIONS">
	
		<NODE label="Identification des séjours">
			<sqlcmd><![CDATA[

					
					
			]]></sqlcmd>
		</NODE>
					
		<NODE label="Détection modifications">
			<sqlcmd><![CDATA[
				
				
			
		
			]]></sqlcmd>
		</NODE>
		
	</NODE>

	<NODE name="SEJOUR" label="RECUPERATION DES SEJOURS">
	
		<NODE label="Patients">
			<sqlcmd><![CDATA[
			
				INSERT INTO activite.p_patients(
					finess, 
					no_patient, 
					nom, 
					prenom, 
					date_naissance, 
					nom_naissance, 
					code_sexe)
				SELECT 
					p_patients_e.finess, 
					'[CPX]' || p_patients_e.no_patient, 
					p_patients_e.nom, 
					p_patients_e.prenom, 
					p_patients_e.date_naissance, 
					p_patients_e.nom_naissance, 
					p_patients_e.code_sexe
				FROM activite[PX].p_patients p_patients_e
					LEFT JOIN activite.p_patients ON p_patients_e.no_patient = p_patients.no_patient AND
						(upper(p_patients_e.nom) = upper(p_patients.nom) OR [IPX] = 0)
					LEFT JOIN activite.p_patients p_patients_2 ON ('[CPX]' || p_patients_e.no_patient) = p_patients_2.no_patient
				WHERE p_patients.no_patient IS NULL AND p_patients_2.no_patient IS NULL;
				
				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.no_patient AS no_patient
				FROM activite[PX].p_patients p_patients_e
				JOIN activite.p_patients ON p_patients_e.no_patient = p_patients.no_patient AND
						(upper(p_patients_e.nom) = upper(p_patients.nom) OR [IPX] = 0);
						
				INSERT INTO w_correspondances_patients
				SELECT 
					p_patients_e.no_patient AS no_patient_source,
					p_patients.no_patient AS no_patient
				FROM activite[PX].p_patients p_patients_e
				JOIN activite.p_patients ON ('[CPX]' || p_patients_e.no_patient) = p_patients.no_patient AND [IPX] <> 0 AND
					p_patients_e.no_patient NOT IN (SELECT no_patient_source FROM w_correspondances_patients);

				CREATE INDEX w_correspondances_patients_1
				  ON w_correspondances_patients
				  USING btree
				  (no_patient_source);
										
										
			]]></sqlcmd>
		</NODE>
				
		<NODE label="Séjours">
			<sqlcmd><![CDATA[
				-- Génération auto ignorés d'après selon date arret
				INSERT INTO activite.t_sejour (no_sejour, est_ignore)
				SELECT no_sejour, '1'
				FROM
				(
				SELECT '[TPX]'||no_sejour AS no_sejour, type_sejour, code_sorti, code_prevu, date_entree, date_sortie, date_arret
				FROM activite[PX].p_sejours
				JOIN activite.t_providers ON t_providers.oid = [PPX] AND [PPX] > 0
				WHERE date_arret < '20991231' AND
					no_sejour IN (SELECT no_sejour FROM activite[PX].p_factures WHERE code_facture = '0') AND
					(
						code_prevu = '1' OR
						type_sejour IN ('1','5','6') AND code_sorti <> '1' OR
						type_sejour IN ('1','5','6') AND date_sortie > date_arret OR
						type_sejour IN ('2','3','9') AND date_entree > date_arret
					)
				) subview
				WHERE no_sejour NOT IN (SELECT no_sejour FROM activite.t_sejour)
				;
				
				-- Génération auto ignorés d'après selon date demarrage
				INSERT INTO activite.t_sejour (no_sejour, est_ignore)
				SELECT no_sejour, '1'
				FROM
				(
				SELECT '[TPX]'||no_sejour AS no_sejour, type_sejour, code_sorti, code_prevu, date_entree, date_sortie, date_arret
				FROM activite[PX].p_sejours
				JOIN activite.t_providers ON t_providers.oid = [PPX] AND [PPX] = 0
				WHERE date_demarrage > '00010101' AND
					no_sejour NOT IN (SELECT no_sejour FROM activite[PX].p_factures WHERE code_facture = '1') AND
					(
						type_sejour IN ('1','5','6') AND date_sortie < date_demarrage OR
						type_sejour IN ('2','3','9') AND code_sorti <> '1' AND date_entree < date_demarrage
					)
				) subview
				WHERE no_sejour NOT IN (SELECT no_sejour FROM activite.t_sejour)
				;
				
				DROP TABLE IF EXISTS w_sejours_ignore;
				CREATE TEMP TABLE w_sejours_ignore AS
				SELECT p_sejours.no_sejour AS no_sejour_ignore, p_sejours.oid AS sejour_id_ignore
				FROM activite[PX].p_sejours
				JOIN activite.t_sejour ON '[CPX]' || p_sejours.no_sejour = t_sejour.no_sejour
				WHERE t_sejour.est_ignore = '1';
				
				INSERT INTO w_sejours_ignore
				SELECT p_sejours.no_sejour AS no_sejour_ignore, p_sejours.oid AS sejour_id_ignore
				FROM activite[PX].p_sejours
				JOIN activite.t_sejour ON '[TPX]' || p_sejours.no_sejour = t_sejour.no_sejour
				WHERE t_sejour.est_ignore = '1' AND '[CPX]' = '';
				
				CREATE INDEX w_sejours_ignore_1
				  ON w_sejours_ignore
				  USING btree
				  (no_sejour_ignore);

				DROP TABLE IF EXISTS w_factures_ignore;
				CREATE TEMP TABLE w_factures_ignore AS
				SELECT no_facture AS no_facture_ignore
				FROM activite[PX].p_factures
				JOIN w_sejours_ignore ON p_factures.no_sejour = w_sejours_ignore.no_sejour_ignore;
				
				CREATE INDEX w_factures_ignore_1
				  ON w_factures_ignore
				  USING btree
				  (no_facture_ignore);
				
				
				
				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,
					gmt_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,
					medecin_traitant_id)
				SELECT
					finess,
					'[CPX]' || no_sejour,
					date_entree,
					date_sortie,
					COALESCE(w_correspondances_patients.no_patient, p_sejours.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,
					ghs_id,
					COALESCE(ghm_id,0) AS ghm_id,
					gmt_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,
					CASE WHEN p_sejours.oid <> 0 THEN p_sejours.oid + [IPX] ELSE 0 END AS oid,
					[PPX]::bigint AS provider_id,
					COALESCE(est_sans_facturation,'0'),
					type_pmsi,
					COALESCE(w_correspondances_risque.oid, 0) as risque_id,
					CASE WHEN medecin_traitant_id <> 0 THEN medecin_traitant_id + [IPX] ELSE 0 END AS medecin_traitant_id
				FROM activite[PX].p_sejours
				LEFT JOIN w_sejours_ignore ON p_sejours.no_sejour = no_sejour_ignore
				LEFT JOIN w_correspondances_patients ON p_sejours.no_patient = w_correspondances_patients.no_patient_source
				LEFT JOIN w_correspondances_risque ON p_sejours.risque_id = w_correspondances_risque.oid_source
				WHERE no_sejour_ignore IS NULL;
				
				
				-- Médecin séjour forcé
				UPDATE activite.p_sejours
				SET medecin_sejour_id = t_sejour.medecin_sejour_id
				FROM activite.t_sejour
				WHERE t_sejour.no_sejour = p_sejours.no_sejour AND
					t_sejour.medecin_sejour_id <> 0 AND
					p_sejours.medecin_sejour_id IS DISTINCT FROM t_sejour.medecin_sejour_id;
				
				
				
				INSERT INTO activite.p_sejours_assurance(
					sejour_id, 
					nom_assure,
					prenom_assure,
					tiers_payant_0_id, 
					tiers_payant_1_id, 
					matricule_1, 
					tiers_payant_2_id, 
					matricule_2, 
					tiers_payant_22_id, 
					matricule_22)
				SELECT
					CASE WHEN sejour_id <> 0 THEN sejour_id + [IPX] ELSE 0 END AS sejour_id, 
					nom_assure,
					prenom_assure,
					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, 
					matricule_1, 
					CASE WHEN tiers_payant_2_id <> 0 THEN tiers_payant_2_id + [IPX] ELSE 0 END AS tiers_payant_2_id, 
					matricule_2, 
					CASE WHEN tiers_payant_22_id <> 0 THEN tiers_payant_22_id + [IPX] ELSE 0 END AS tiers_payant_22_id, 
					matricule_22
				FROM activite[PX].p_sejours_assurance
				LEFT JOIN w_sejours_ignore ON p_sejours_assurance.sejour_id = sejour_id_ignore
				WHERE no_sejour_ignore IS NULL;
				
				INSERT INTO activite.p_sejours_transferts(
					sejour_id,
					provenance_id,
					destination_id)
				SELECT
					CASE WHEN sejour_id <> 0 THEN sejour_id + [IPX] ELSE 0 END AS sejour_id, 
					provenance_id,
					destination_id
				FROM activite[PX].p_sejours_transferts
				LEFT JOIN w_sejours_ignore ON p_sejours_transferts.sejour_id = sejour_id_ignore
				WHERE no_sejour_ignore IS NULL;
										
				INSERT INTO activite.p_coordonnees_patient (
					sejour_id,
					adresse,
					code_postal_id,
					commune,
					telephone_fixe,
					telephone_portable,
					email						
				)
				SELECT
					CASE WHEN sejour_id <> 0 THEN sejour_id + [IPX] ELSE 0 END AS sejour_id, 	
					adresse,
					code_postal_id,
					commune,
					telephone_fixe,
					telephone_portable,
					email
				FROM activite[PX].p_coordonnees_patient	
					LEFT JOIN w_sejours_ignore ON p_coordonnees_patient.sejour_id = sejour_id_ignore
				WHERE no_sejour_ignore IS NULL;			
				
				INSERT INTO activite.p_sejours_groupes_smr(
					no_sejour
					, date_entree 
					, date_sortie 
					, date_groupage
					, gmt_code 
					, gmt_id
					, gme_code 
					, gme_id
					, date_debut_gmt 
					, date_fin_gmt
					, nb_jours_presence
					, types_gmt_code
				)
				SELECT 
					'[CPX]' || no_sejour
					, date_entree
					, date_sortie
					, date_groupage
					, gmt_code
					, gmt_id
					, gme_code
					, gme_id
					, date_debut_gmt
					, date_fin_gmt
					, nb_jours_presence
					, types_gmt_code
				FROM activite[PX].p_sejours_groupes_smr
				;
				
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Mouvements">
			<sqlcmd><![CDATA[

				
				
				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_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,
					est_premier_mouvement_jour,
					est_dernier_mouvement_jour,
					jour_semaine,
					is_weekend,
					est_facture)
				SELECT
					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_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,
					est_premier_mouvement_jour,
					est_dernier_mouvement_jour,
					jour_semaine,
					is_weekend,
					COALESCE(est_facture,'1') AS est_facture
				FROM activite[PX].p_mouvements_sejour
				LEFT JOIN w_sejours_ignore ON p_mouvements_sejour.no_sejour = no_sejour_ignore
				WHERE no_sejour_ignore IS NULL;
				
				
				
				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)
				SELECT
					CASE WHEN oid <> 0 THEN 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,
					mode_traitement_id
				FROM activite[PX].t_lieux
				WHERE CASE WHEN oid <> 0 THEN oid + [IPX] ELSE 0 END NOT IN (SELECT oid FROM activite.t_lieux)
				;
										
				UPDATE activite.t_lieux 
				SET
					code_original_1 = CASE WHEN t_lieux_src.code_original_1 <> '' THEN '[TPX]' || t_lieux_src.code_original_1 ELSE '' END,
					code_original_2 = CASE WHEN t_lieux_src.code_original_2 <> '' THEN '[TPX]' || t_lieux_src.code_original_2 ELSE '' END,
					code_original_3 = CASE WHEN t_lieux_src.code_original_3 <> '' THEN '[TPX]' || t_lieux_src.code_original_3 ELSE '' END,
					code_original_4 = CASE WHEN t_lieux_src.code_original_4 <> '' THEN '[TPX]' || t_lieux_src.code_original_4 ELSE '' END,
					code_original_5 = CASE WHEN t_lieux_src.code_original_5 <> '' THEN '[TPX]' || t_lieux_src.code_original_5 ELSE '' END,
					service_facturation_id = CASE WHEN t_lieux_src.service_facturation_id <> 0 THEN t_lieux_src.service_facturation_id + [IPX] ELSE 0 END,
					activite_id = CASE WHEN t_lieux_src.activite_id <> 0 THEN t_lieux_src.activite_id + [IPX] ELSE 0 END,
					lit_id = CASE WHEN t_lieux_src.lit_id <> 0 THEN t_lieux_src.lit_id + [IPX] ELSE 0 END,
					unite_medicale_id = CASE WHEN t_lieux_src.unite_medicale_id <> 0 THEN t_lieux_src.unite_medicale_id + [IPX] ELSE 0 END,
					unite_fonctionnelle_id = CASE WHEN t_lieux_src.unite_fonctionnelle_id <> 0 THEN t_lieux_src.unite_fonctionnelle_id + [IPX] ELSE 0 END,
					code_original_6 = CASE WHEN t_lieux_src.code_original_6 <> '' THEN '[TPX]' || t_lieux_src.code_original_6 ELSE '' END,
					code_original_7 = CASE WHEN t_lieux_src.code_original_7 <> '' THEN '[TPX]' || t_lieux_src.code_original_7 ELSE '' END,
					mode_traitement_id = t_lieux_src.mode_traitement_id
				FROM activite[PX].t_lieux t_lieux_src
				WHERE CASE WHEN t_lieux_src.oid <> 0 THEN t_lieux_src.oid + [IPX] ELSE 0 END = t_lieux.oid
					AND (
					CASE WHEN t_lieux_src.code_original_1 <> '' THEN '[TPX]' || t_lieux_src.code_original_1 ELSE '' END != t_lieux.code_original_1 OR
					CASE WHEN t_lieux_src.code_original_2 <> '' THEN '[TPX]' || t_lieux_src.code_original_2 ELSE '' END != t_lieux.code_original_2 OR
					CASE WHEN t_lieux_src.code_original_3 <> '' THEN '[TPX]' || t_lieux_src.code_original_3 ELSE '' END != t_lieux.code_original_3 OR
					CASE WHEN t_lieux_src.code_original_4 <> '' THEN '[TPX]' || t_lieux_src.code_original_4 ELSE '' END != t_lieux.code_original_4 OR
					CASE WHEN t_lieux_src.code_original_5 <> '' THEN '[TPX]' || t_lieux_src.code_original_5 ELSE '' END != t_lieux.code_original_5 OR
					CASE WHEN t_lieux_src.service_facturation_id <> 0 THEN t_lieux_src.service_facturation_id + [IPX] ELSE 0 END != t_lieux.service_facturation_id OR
					CASE WHEN t_lieux_src.activite_id <> 0 THEN t_lieux_src.activite_id + [IPX] ELSE 0 END != t_lieux.activite_id OR
					CASE WHEN t_lieux_src.lit_id <> 0 THEN t_lieux_src.lit_id + [IPX] ELSE 0 END != t_lieux.lit_id OR
					CASE WHEN t_lieux_src.unite_medicale_id <> 0 THEN t_lieux_src.unite_medicale_id + [IPX] ELSE 0 END != t_lieux.unite_medicale_id OR
					CASE WHEN t_lieux_src.unite_fonctionnelle_id <> 0 THEN t_lieux_src.unite_fonctionnelle_id + [IPX] ELSE 0 END != t_lieux.unite_fonctionnelle_id OR
					CASE WHEN t_lieux_src.code_original_6 <> '' THEN '[TPX]' || t_lieux_src.code_original_6 ELSE '' END != t_lieux.code_original_6 OR
					CASE WHEN t_lieux_src.code_original_7 <> '' THEN '[TPX]' || t_lieux_src.code_original_7 ELSE '' END != t_lieux.code_original_7 OR
					t_lieux_src.mode_traitement_id != t_lieux.mode_traitement_id
				)
				;
										
										
			]]></sqlcmd>
		</NODE>
		<NODE label="Pré-admissions (web100t)">
			<condition><![CDATA[
				SELECT count(*) <> 0 FROM information_schema.tables WHERE table_schema = 'prod_web100t'
			]]></condition>
			<sqlcmd><![CDATA[
			
				INSERT INTO activite.p_sejours_preadmission(
					no_sejour,
					date_entree,
					date_validation_preadmission,
					"preadmission_j1",
					"preadmission_j2"
				)
				SELECT
					no_sejour,
					date_entree,
					date_validation_preadmission,
					"preadmission_j1",
					"preadmission_j2"
				FROM activite[PX].p_sejours_preadmission
				WHERE NOT EXISTS (
					SELECT p_sejours_preadmission.no_sejour 
					FROM activite[PX].p_sejours_preadmission p_sejours_preadmission_w1
						JOIN activite.p_sejours_preadmission ON p_sejours_preadmission_w1.no_sejour = p_sejours_preadmission.no_sejour
				)
				;
			]]></sqlcmd>
		</NODE>
		
	</NODE>

	<NODE name="FACTURE" label="RECUPERATION DES FACTURES">
	
		<NODE label="Entêtes">
			<sqlcmd><![CDATA[
			
								
				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,
					motif_non_facturation_id,
					motif_non_facturation_comment)
				SELECT 
					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,
					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,
					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 oid <> 0 THEN 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,
					[PPX]::bigint AS provider_id,
					CASE WHEN motif_non_facturation_id <> 0 THEN  motif_non_facturation_id + [IPX] ELSE 0 END AS motif_non_facturation_id,
					motif_non_facturation_comment
				FROM activite[PX].p_factures
				LEFT JOIN w_factures_ignore ON p_factures.no_facture = no_facture_ignore
				WHERE no_facture_ignore IS NULL;
				
				
				-- Ajustement des motifs de non facturation génériques
				UPDATE activite.p_factures
				SET motif_non_facturation_id = subview.oid
				FROM 
				(
				SELECT t_motif_non_facturation_source.oid AS oid_source, t_motif_non_facturation.oid
				FROM activite.t_motif_non_facturation
				JOIN activite.t_motif_non_facturation t_motif_non_facturation_source ON 
					(
					t_motif_non_facturation.code_original = '*CTING' AND t_motif_non_facturation_source.code_original LIKE '%*CTING' OR
					t_motif_non_facturation.code_original = '*CTIZZZ' AND t_motif_non_facturation_source.code_original LIKE '%*CTIZZZ'
					) AND 
					t_motif_non_facturation.oid <> t_motif_non_facturation_source.oid
				) subview
				WHERE motif_non_facturation_id <> 0 AND
					motif_non_facturation_id = subview.oid_source
				;
				
								
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lignes non facturées Clinique">
			<sqlcmd><![CDATA[

				INSERT INTO activite.p_factures_lignes_non_facturees_c(
					no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					coefficient,
					rubrique_facturation_id,
					prestation_id,
					ucd_id,
					lpp_id,
					prix_unitaire,
					lieu_id,
					facture_id)
				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,
					prestation_id,
					ucd_id,
					lpp_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
				FROM activite[PX].p_factures_lignes_non_facturees_c
				LEFT JOIN w_factures_ignore ON p_factures_lignes_non_facturees_c.no_facture = no_facture_ignore
				WHERE no_facture_ignore IS NULL;
										
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lignes factures Clinique">
			<sqlcmd><![CDATA[
				
				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,
					prestation_id,
					COALESCE(w_correspondances_compte.oid,0) AS compte_produit_id
				FROM activite[PX].t_rubrique_facture_c
				LEFT JOIN w_correspondances_compte ON compte_produit_id = w_correspondances_compte.oid_source
				WHERE (t_rubrique_facture_c.oid + [IPX]) NOT IN (SELECT oid FROM activite.t_rubrique_facture_c);
							
				INSERT INTO activite.p_factures_lignes_c(
					no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					coefficient,
					coefficient_mco,
					rubrique_facture_id,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					compte_produit_id,
					prestation_id,
					ucd_id,
					lpp_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,
					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,
					taux_tva)
				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,
					CASE WHEN rubrique_comptabilisation_id <> 0 THEN rubrique_comptabilisation_id + [IPX] ELSE 0 END AS rubrique_comptabilisation_id,
					COALESCE(w_correspondances_compte.oid,0),
					prestation_id,
					ucd_id,
					lpp_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,
					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,
					taux_tva
				FROM activite[PX].p_factures_lignes_c
					LEFT JOIN w_correspondances_compte ON compte_produit_id = w_correspondances_compte.oid_source
					LEFT JOIN w_factures_ignore ON p_factures_lignes_c.no_facture = no_facture_ignore
				WHERE no_facture_ignore IS NULL;
				
				
										
			]]></sqlcmd>
		</NODE>
	
		<NODE label="Compte client Clinique">
			<sqlcmd><![CDATA[
				
				
				
				
				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,
					od_non_comptabilise)
				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,
					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,
					od_non_comptabilise
				FROM activite[PX].p_factures_soldes_c
					LEFT JOIN w_factures_ignore ON p_factures_soldes_c.no_facture = no_facture_ignore
				WHERE no_facture_ignore IS NULL;	

				
										
			]]></sqlcmd>
		</NODE>
	
		<NODE label="Lignes non facturées Honoraires">
			<sqlcmd><![CDATA[

				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,
					prestation_id,
					prix_unitaire,
					montant_depassement,
					CASE WHEN lieu_id <> 0 THEN lieu_id + [IPX] ELSE 0 END AS lieu_id,
					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 activite[PX].p_factures_lignes_non_facturees_h
					LEFT JOIN w_factures_ignore ON p_factures_lignes_non_facturees_h.no_facture = no_facture_ignore
				WHERE no_facture_ignore IS NULL;
										
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lignes factures Honoraires">
			<sqlcmd><![CDATA[
			
				
			
				INSERT INTO activite.p_factures_lignes_h(
					finess,
					no_facture,
					date_debut,
					date_fin,
					coefficient,
					coefficient_mco,
					prestation_id,
					rubrique_facturation_id,
					rubrique_comptabilisation_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,
					protocole_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,
					montant_non_facture)
				SELECT
					finess,
					'[CPX]' || no_facture AS no_facture,
					date_debut,
					date_fin,
					coefficient,
					coefficient_mco,
					prestation_id,
					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,
					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,
					acte_id,
					CASE WHEN COALESCE(protocole_id,0) <> 0 THEN protocole_id + [IPX] ELSE 0 END AS protocole_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,
					montant_non_facture
				FROM activite[PX].p_factures_lignes_h
					LEFT JOIN w_factures_ignore ON p_factures_lignes_h.no_facture = no_facture_ignore
				WHERE no_facture_ignore IS NULL;
									
										
			]]></sqlcmd>
		</NODE>
	
		<NODE label="Compte client Honoraires">
			<sqlcmd><![CDATA[
			
				
				
				INSERT INTO activite.p_factures_soldes_h(
					no_facture,
					date_comptable,
					rubrique_comptabilisee_id,
					prestation_id,
					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,
					od_non_comptabilise)
				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,
					prestation_id,
					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,
					od_non_comptabilise
				FROM activite[PX].p_factures_soldes_h
					LEFT JOIN w_factures_ignore ON p_factures_soldes_h.no_facture = no_facture_ignore
				WHERE no_facture_ignore IS NULL;
				
				
										
			]]></sqlcmd>
		</NODE>
		
		
		<NODE label="En-cours">
			<sqlcmd><![CDATA[
			
				
				DROP TABLE IF EXISTS w_factures_ignore;
				CREATE TEMP TABLE w_factures_ignore AS
				SELECT no_facture AS no_facture_ignore
				FROM activite[PX].p_factures_encours
				JOIN w_sejours_ignore ON p_factures_encours.no_sejour = w_sejours_ignore.no_sejour_ignore;
				
				CREATE INDEX w_factures_ignore_1
				  ON w_factures_ignore
				  USING btree
				  (no_facture_ignore);
				  
				
				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
				)
				SELECT
					CASE WHEN oid <> 0 THEN 0 - (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,
					'[CPX]' || no_facture_reference 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,
					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
				FROM activite[PX].p_factures_encours
					LEFT JOIN w_factures_ignore ON p_factures_encours.no_facture = no_facture_ignore
				WHERE no_facture_ignore IS NULL AND
					code_origine = 'P';
				
				
				
				
				INSERT INTO activite.p_factures_encours_lignes_c(
					finess,
					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
				)
				SELECT
					finess,
					CASE WHEN facture_id <> 0 THEN 0 - (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),
					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
				FROM activite[PX].p_factures_encours_lignes_c
					LEFT JOIN w_correspondances_compte ON compte_produit_id = w_correspondances_compte.oid_source
					LEFT JOIN w_factures_ignore ON p_factures_encours_lignes_c.no_facture = no_facture_ignore
				WHERE no_facture_ignore IS NULL AND
					facture_id IN (SELECT oid FROM activite[PX].p_factures_encours WHERE code_origine = 'P');
							
					
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Rejets">
			<sqlcmd><![CDATA[

				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, 
					rejet_id, 
					rejet_code, 
					rejet_texte, 
					CASE WHEN facture_id <> 0 THEN facture_id + [IPX] ELSE 0 END AS facture_id
				FROM activite[PX].p_factures_rejets
					LEFT JOIN w_factures_ignore ON p_factures_rejets.no_facture = no_facture_ignore
				WHERE no_facture_ignore IS NULL;
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Ecritures comptables">
			<sqlcmd><![CDATA[

				INSERT INTO activite.p_ecriture_comptable(
					code_original,
					provider_id,
					date_comptable,
					type_ecriture,
					type_piece,
					piece,
					clinique_honoraire,
					compte_id,
					journal_id,
					sejour_id,
					no_sejour,
					facture_id,
					no_facture,
					medecin_id,
					tiers_payant_id,
					texte,
					montant_credit,
					montant_debit,
					code_erreur,
					est_comptabilise,
					mode_reglement_code,
					taux_tva
					)
				SELECT
					'[TPX]' || code_original,
					[PPX]::bigint AS provider_id,
					date_comptable,
					type_ecriture,
					type_piece,
					piece,
					clinique_honoraire,
					COALESCE(w_correspondances_compte.oid,0) AS compte_id,
					CASE WHEN journal_id <> 0 THEN journal_id + [IPX] ELSE 0 END AS journal_id,
					CASE WHEN sejour_id <> 0 THEN sejour_id + [IPX] ELSE 0 END AS sejour_id,
					'[CPX]' || no_sejour,
					CASE WHEN facture_id <> 0 THEN facture_id + [IPX] ELSE 0 END AS facture_id,
					'[CPX]' || no_facture,
					CASE WHEN medecin_id <> 0 THEN medecin_id + [IPX] ELSE 0 END AS medecin_id,
					CASE WHEN tiers_payant_id <> 0 THEN tiers_payant_id + [IPX] ELSE 0 END AS tiers_payant_id,
					texte,
					montant_credit,
					montant_debit,
					code_erreur,
					est_comptabilise,
					mode_reglement_code,
					taux_tva
				FROM activite[PX].p_ecriture_comptable
					LEFT JOIN w_correspondances_compte ON compte_id = w_correspondances_compte.oid_source
				;
										
			]]></sqlcmd>
		</NODE>

	</NODE>
	
	
	<NODE name="CHIFFRIER" label="CREATION DU CHIFFRIER">
		
		<NODE label="Récupération chiffrier">
			<sqlcmd><![CDATA[

				INSERT INTO activite.p_chiffrier_comptable(
					date_comptable,
					montant_ventes_c,
					montant_reglements_c,
					montant_solde_client_c,
					montant_ventes_h,
					montant_reglements_h,
					montant_solde_client_h,
					provider_id)
				SELECT 
					date_comptable,
					SUM(montant_ventes_c),
					SUM(montant_reglements_c),
					SUM(montant_solde_client_c),
					SUM(montant_ventes_h),
					SUM(montant_reglements_h),
					SUM(montant_solde_client_h),
					[PPX]::bigint AS provider_id
				FROM activite[PX].p_chiffrier_comptable
				GROUP BY 1
				;


				

				INSERT INTO activite.p_factures_comptables(
					no_facture,
					date_comptable,
					montant_ventes_c,
					montant_reglements_c,
					montant_ventes_h,
					montant_reglements_h,
					facture_id,
					provider_id)
				SELECT
					'[CPX]' || no_facture AS no_facture,
					date_comptable,
					montant_ventes_c,
					montant_reglements_c,
					montant_ventes_h,
					montant_reglements_h,
					CASE WHEN facture_id <> 0 THEN facture_id + [IPX] ELSE 0 END AS facture_id,
					[PPX]::bigint AS provider_id
				FROM activite[PX].p_factures_comptables;


										
			]]></sqlcmd>
		</NODE>
		
		<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>
	
	<NODE name="PARAM" label="RECUPERATION DES PARAMETRES">
	
		<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 code NOT IN (SELECT code FROM activite.t_types_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 0 NOT IN (SELECT oid FROM activite.t_tiers_payant);

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

				INSERT INTO activite.t_tiers_payant(
					oid,
					code_original, 
					code, 
					type_tiers_payant, 
					texte, 
					texte_court) 
				SELECT 
					oid + [IPX],
					'[TPX]' || code_original, 
					'[CPX]' || code, 
					type_tiers_payant, 
					texte, 
					texte_court
				FROM activite[PX].t_tiers_payant
				WHERE oid <> 0 AND oid + [IPX] NOT IN (SELECT oid FROM activite.t_tiers_payant);
										
				UPDATE activite.t_tiers_payant
				SET 
					type_tiers_payant = source.type_tiers_payant,
					code = source.code, 
					texte = source.texte, 
					texte_court = source.texte_court,
					adresse = source.adresse,
					code_postal_id = source.code_postal_id,
					ville = source.ville,
					telephone = source.telephone,
					fax = source.fax,
					email = source.email
				FROM activite[PX].t_tiers_payant source
				WHERE
					source.oid + [IPX] = t_tiers_payant.oid AND
					(
						t_tiers_payant.type_tiers_payant IS DISTINCT FROM source.type_tiers_payant OR
						t_tiers_payant.texte IS DISTINCT FROM source.texte OR
						t_tiers_payant.texte_court IS DISTINCT FROM source.texte_court OR
						t_tiers_payant.adresse IS DISTINCT FROM source.adresse OR
						t_tiers_payant.code_postal_id IS DISTINCT FROM source.code_postal_id OR
						t_tiers_payant.ville IS DISTINCT FROM source.ville OR
						t_tiers_payant.telephone IS DISTINCT FROM source.telephone OR
						t_tiers_payant.fax IS DISTINCT FROM source.fax OR
						t_tiers_payant.email IS DISTINCT FROM source.email
					)
				;
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Médecins">
			<sqlcmd><![CDATA[

				INSERT INTO activite.t_specialites_medecin(oid, code, code_original, texte, texte_court) 
				SELECT 0, '', '0', 'Non saisie', 'Non saisie'
				WHERE 0 NOT IN (SELECT oid FROM activite.t_specialites_medecin);
				
				INSERT INTO activite.t_medecins_administratifs(oid, code_original, code, nom, prenom, specialite_id, medecin_id) 
				SELECT 0, '0', '****', 'Non renseigné', '', 0, 0 WHERE 0 NOT IN (SELECT oid FROM activite.t_medecins_administratifs);

				INSERT INTO activite.t_medecins_administratifs(
					oid,
					code,
					code_original,
					nom,
					prenom,
					specialite_id,
					medecin_id)
				SELECT 
					oid + [IPX],
					'[CPX]' || code,  
					'[TPX]' || code_original,
					nom,
					prenom,
					specialite_id,
					medecin_id
				FROM activite[PX].t_medecins_administratifs
				WHERE oid <> 0 AND oid + [IPX] NOT IN (SELECT oid FROM activite.t_medecins_administratifs);
				
				UPDATE activite.t_medecins_administratifs
				SET 
					nom = source.nom, 
					prenom = source.prenom,
					adresse = source.adresse,
					code_postal_id = source.code_postal_id,
					ville = source.ville,
					telephone = source.telephone,
					fax = source.fax,
					email = source.email,
					no_adeli = COALESCE(source.no_adeli,''),
					code_rpps = CASE WHEN source.code_rpps <> '' AND length(source.code_rpps) = 11 THEN source.code_rpps ELSE '' END
				FROM activite[PX].t_medecins_administratifs source
				WHERE
					source.oid + [IPX] = t_medecins_administratifs.oid AND
					'[TPX]' || source.code_original = t_medecins_administratifs.code_original AND
					(
						t_medecins_administratifs.nom IS DISTINCT FROM source.nom OR
						t_medecins_administratifs.prenom IS DISTINCT FROM source.prenom OR
						t_medecins_administratifs.adresse IS DISTINCT FROM source.adresse OR
						t_medecins_administratifs.code_postal_id IS DISTINCT FROM source.code_postal_id OR
						t_medecins_administratifs.ville IS DISTINCT FROM source.ville OR
						t_medecins_administratifs.telephone IS DISTINCT FROM source.telephone OR
						t_medecins_administratifs.fax IS DISTINCT FROM source.fax OR
						t_medecins_administratifs.email IS DISTINCT FROM source.email OR
						t_medecins_administratifs.no_adeli IS DISTINCT FROM COALESCE(source.no_adeli,'') OR
						t_medecins_administratifs.code_rpps IS DISTINCT FROM CASE WHEN source.code_rpps <> '' AND length(source.code_rpps) = 11 THEN source.code_rpps ELSE '' END
					)
				;
				
				SELECT base.cti_execute('TRUNCATE activite.t_medecins_traitants_administratifs',1)
				WHERE [IPX]=0
				;

				INSERT INTO activite.t_medecins_traitants_administratifs(oid, code_original, code, nom, prenom, specialite_id, medecin_id) 
				SELECT 0, '0', '****', 'Non renseigné', '', 0, 0 WHERE 0 NOT IN (SELECT oid FROM activite.t_medecins_administratifs);

				INSERT INTO activite.t_medecins_traitants_administratifs(
					oid,
					code,
					code_original,
					nom,
					prenom,
					statut,
					specialite_id,
					is_medecin_sejour,
					medecin_id,
					is_medecin_executant)
				SELECT 
					oid + [IPX],
					'[CPX]' || code,  
					'[TPX]' || code_original,
					nom,
					prenom,
					statut,
					specialite_id,
					is_medecin_sejour,
					medecin_id,
					is_medecin_executant
				FROM activite[PX].t_medecins_traitants_administratifs
				WHERE oid <> 0 AND oid + [IPX] NOT IN (SELECT oid FROM activite.t_medecins_traitants_administratifs);

				UPDATE activite.t_medecins_traitants_administratifs
				SET nom = source.nom,
					prenom = source.prenom,
					adresse = source.adresse,
					code_postal_id = source.code_postal_id,
					ville = source.ville,
					no_adeli = COALESCE(source.no_adeli, ''),
					no_rpps = CASE
						WHEN source.no_rpps <> ''
						AND length(source.no_rpps) = 11 THEN source.no_rpps
						ELSE ''
					END
				FROM activite[PX].t_medecins_traitants_administratifs source
				WHERE source.oid + [IPX] = t_medecins_traitants_administratifs.oid
					AND '[TPX]' || source.code_original = t_medecins_traitants_administratifs.code_original
					AND (
						t_medecins_traitants_administratifs.nom IS DISTINCT
						FROM source.nom
							OR t_medecins_traitants_administratifs.prenom IS DISTINCT
						FROM source.prenom
							OR t_medecins_traitants_administratifs.adresse IS DISTINCT
						FROM source.adresse
							OR t_medecins_traitants_administratifs.code_postal_id IS DISTINCT
						FROM source.code_postal_id
							OR t_medecins_traitants_administratifs.ville IS DISTINCT
						FROM source.ville
							OR t_medecins_traitants_administratifs.no_adeli IS DISTINCT
						FROM COALESCE(source.no_adeli, '')
							OR t_medecins_traitants_administratifs.no_rpps IS DISTINCT
						FROM CASE
								WHEN source.no_rpps <> ''
								AND length(source.no_rpps) = 11 THEN source.no_rpps
								ELSE ''
							END
					);							
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Hébergement">
			<sqlcmd><![CDATA[
				
				-- Services
				
				INSERT INTO activite.t_services_facturation(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '****', 'Non renseigné', 'Non renseigné' WHERE 0 NOT IN (SELECt oid FROM activite.t_services_facturation);

				
				INSERT INTO activite.t_services_facturation(
					oid,
					code,
					code_original,
					texte,
					texte_court,
					statut,
					nb_lits,
					type_t2a,
					nb_cp,
					dmt_id,
					mode_traitement_id,
					unite_medicale_modifiable,
					est_sans_mouvement)
				SELECT 
					oid + [IPX],
					'[CPX]' || code,  
					'[TPX]' || code_original,
					texte,
					texte_court,
					statut,
					nb_lits,
					type_t2a,
					nb_cp,
					dmt_id,
					mode_traitement_id,
					unite_medicale_modifiable,
					est_sans_mouvement
				FROM activite[PX].t_services_facturation
				WHERE oid <> 0 AND oid + [IPX] NOT IN (SELECT oid FROM activite.t_services_facturation);
				
				UPDATE activite.t_services_facturation SET
					code = t_services_facturation_in.code,
					texte = t_services_facturation_in.texte,
					texte_court = t_services_facturation_in.texte_court
				FROM activite[PX].t_services_facturation t_services_facturation_in
				WHERE t_services_facturation.oid = (t_services_facturation_in.oid + [IPX]) AND
					(
					t_services_facturation.code IS DISTINCT FROM t_services_facturation_in.code OR
					t_services_facturation.texte IS DISTINCT FROM t_services_facturation_in.texte OR
					t_services_facturation.texte_court IS DISTINCT FROM t_services_facturation_in.texte_court
					)
				;

				UPDATE activite.t_services_facturation SET
					code = t_services_facturation_in.code,
					texte = t_services_facturation_in.texte,
					texte_court = t_services_facturation_in.texte_court
				FROM activite[PX].t_services_facturation t_services_facturation_in
				WHERE t_services_facturation.oid = (t_services_facturation_in.oid + [IPX]) AND
					(
					t_services_facturation.code IS DISTINCT FROM t_services_facturation_in.code OR
					t_services_facturation.texte IS DISTINCT FROM t_services_facturation_in.texte OR
					t_services_facturation.texte_court IS DISTINCT FROM t_services_facturation_in.texte_court
					)
				;
	
				-- 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 0 NOT IN (SELECt oid FROM activite.t_unites_medicales);

				INSERT INTO activite.t_unites_medicales(
					oid,
					code,
					code_original,
					texte,
					texte_court)
				SELECT 
					oid + [IPX],
					'[CPX]' || code,  
					'[TPX]' || code_original,
					texte,
					texte_court
				FROM activite[PX].t_unites_medicales
				WHERE oid <> 0 AND oid + [IPX] NOT IN (SELECT oid FROM activite.t_unites_medicales);
				
				UPDATE activite.t_unites_medicales SET
					code = t_unites_medicales_in.code,
					texte = t_unites_medicales_in.texte,
					texte_court = t_unites_medicales_in.texte_court
				FROM activite[PX].t_unites_medicales t_unites_medicales_in
				WHERE t_unites_medicales.oid = (t_unites_medicales_in.oid + [IPX]) AND
					(
					t_unites_medicales.code IS DISTINCT FROM t_unites_medicales_in.code OR
					t_unites_medicales.texte IS DISTINCT FROM t_unites_medicales_in.texte OR
					t_unites_medicales.texte_court IS DISTINCT FROM t_unites_medicales_in.texte_court
					)
				;

				-- 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 0 NOT IN (SELECt oid FROM activite.t_unites_fonctionnelles);

				INSERT INTO activite.t_unites_fonctionnelles(
					oid,
					code,
					code_original,
					texte,
					texte_court)
				SELECT 
					oid + [IPX],
					'[CPX]' || code,  
					'[TPX]' || code_original,
					texte,
					texte_court
				FROM activite[PX].t_unites_fonctionnelles
				WHERE oid <> 0 AND oid + [IPX] NOT IN (SELECT oid FROM activite.t_unites_fonctionnelles);

				UPDATE activite.t_unites_fonctionnelles SET
					code = t_unites_fonctionnelles_in.code,
					texte = t_unites_fonctionnelles_in.texte,
					texte_court = t_unites_fonctionnelles_in.texte_court
				FROM activite[PX].t_unites_fonctionnelles t_unites_fonctionnelles_in
				WHERE t_unites_fonctionnelles.oid = (t_unites_fonctionnelles_in.oid + [IPX]) AND
					(
					t_unites_fonctionnelles.code IS DISTINCT FROM t_unites_fonctionnelles_in.code OR
					t_unites_fonctionnelles.texte IS DISTINCT FROM t_unites_fonctionnelles_in.texte OR
					t_unites_fonctionnelles.texte_court IS DISTINCT FROM t_unites_fonctionnelles_in.texte_court
					)
				;

				-- Activités

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

				INSERT INTO activite.t_activites(
					oid,
					code,
					code_original,
					texte,
					texte_court)
				SELECT 
					oid + [IPX],
					'[CPX]' || code,  
					'[TPX]' || code_original,
					texte,
					texte_court
				FROM activite[PX].t_activites
				WHERE oid <> 0 AND oid + [IPX] NOT IN (SELECT oid FROM activite.t_activites);

				UPDATE activite.t_activites SET
					code = t_activites_in.code,
					texte = t_activites_in.texte,
					texte_court = t_activites_in.texte_court
				FROM activite[PX].t_activites t_activites_in
				WHERE t_activites.oid = (t_activites_in.oid + [IPX]) AND
					(
					t_activites.code IS DISTINCT FROM t_activites_in.code OR
					t_activites.texte IS DISTINCT FROM t_activites_in.texte OR
					t_activites.texte_court IS DISTINCT FROM t_activites_in.texte_court
					)
				;



				-- 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 0 NOT IN (SELECT oid FROM activite.t_etages);

				INSERT INTO activite.t_etages(
					oid,
					code,
					code_original,
					texte,
					texte_court,
					nb_lits,
					nb_cp)
				SELECT 
					oid + [IPX],
					'[CPX]' || code,  
					'[TPX]' || code_original,
					texte,
					texte_court,
					nb_lits,
					nb_cp
				FROM activite[PX].t_etages
				WHERE oid <> 0 AND oid + [IPX] NOT IN (SELECT oid FROM activite.t_etages);

				UPDATE activite.t_etages SET
					code = t_etages_in.code,
					texte = t_etages_in.texte,
					texte_court = t_etages_in.texte_court
				FROM activite[PX].t_etages t_etages_in
				WHERE t_etages.oid = (t_etages_in.oid + [IPX]) AND
					(
					t_etages.code IS DISTINCT FROM t_etages_in.code OR
					t_etages.texte IS DISTINCT FROM t_etages_in.texte OR
					t_etages.texte_court IS DISTINCT FROM t_etages_in.texte_court
					)
				;
				

				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 0 NOT IN (SELECT oid FROM activite.t_lits);
				
				-- suppression des anciennes entrées
				DELETE FROM activite.t_lits
				USING activite[PX].t_lits t_lits_px
				WHERE 1=1
					AND t_lits.code = '[CPX]' || t_lits_px.code
					AND t_lits.etage_id = t_lits_px.etage_id + [IPX]
					AND t_lits.oid IS DISTINCT FROM t_lits_px.oid + [IPX];
				
				UPDATE activite.t_lits SET
					code = t_lits_px.code || '_BAK',
					texte = t_lits_px.texte,
					texte_court = t_lits_px.texte_court,
					etage_id = CASE WHEN t_lits_px.etage_id <> 0 THEN t_lits_px.etage_id + [IPX] ELSE 0 END
				FROM activite[PX].t_lits t_lits_px
				WHERE t_lits_px.oid + [IPX] = t_lits.oid AND
				(
					t_lits.code IS DISTINCT FROM t_lits_px.code OR
					t_lits.texte IS DISTINCT FROM t_lits_px.texte OR
					t_lits.texte IS DISTINCT FROM t_lits_px.texte OR
					t_lits.etage_id IS DISTINCT FROM (CASE WHEN t_lits_px.etage_id <> 0 THEN t_lits_px.etage_id + [IPX] ELSE 0 END)
				);

				INSERT INTO activite.t_lits(
					oid,
					code,
					code_original,
					texte,
					texte_court,
					chambre_particuliere,
					etage_id)
				SELECT 
					oid + [IPX],
					'[CPX]' || code,  
					'[TPX]' || code_original,
					texte,
					texte_court,
					chambre_particuliere,
					CASE WHEN etage_id <> 0 THEN etage_id + [IPX] ELSE 0 END AS etage_id
				FROM activite[PX].t_lits
				WHERE oid <> 0 AND oid + [IPX] NOT IN (SELECT oid FROM activite.t_lits);
				
				UPDATE activite.t_lits SET
					code = t_lits_px.code,
					texte = t_lits_px.texte,
					texte_court = t_lits_px.texte_court,
					etage_id = CASE WHEN t_lits_px.etage_id <> 0 THEN t_lits_px.etage_id + [IPX] ELSE 0 END
				FROM activite[PX].t_lits t_lits_px
				WHERE t_lits_px.oid + [IPX] = t_lits.oid AND
				(
					t_lits.code IS DISTINCT FROM t_lits_px.code OR
					t_lits.texte IS DISTINCT FROM t_lits_px.texte OR
					t_lits.texte IS DISTINCT FROM t_lits_px.texte OR
					t_lits.etage_id IS DISTINCT FROM (CASE WHEN t_lits_px.etage_id <> 0 THEN t_lits_px.etage_id + [IPX] ELSE 0 END)
				);

				
			]]></sqlcmd>
		</NODE>
				
		<NODE label="Facturation">
			<sqlcmd><![CDATA[
				
				

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

				INSERT INTO activite.t_rubriques_facturation(
					oid,
					code,
					code_original,
					texte,
					texte_court)
				SELECT 
					oid + [IPX],
					'[CPX]' || code,  
					'[TPX]' || code_original,
					texte,
					texte_court
				FROM activite[PX].t_rubriques_facturation
				WHERE oid <> 0 AND oid + [IPX] NOT IN (SELECT oid FROM activite.t_rubriques_facturation);

				UPDATE activite.t_rubriques_facturation SET
					code = t_rubriques_facturation_in.code,
					texte = t_rubriques_facturation_in.texte,
					texte_court = t_rubriques_facturation_in.texte_court
				FROM activite[PX].t_rubriques_facturation t_rubriques_facturation_in
				WHERE t_rubriques_facturation.oid = (t_rubriques_facturation_in.oid + [IPX]) AND
					(
					t_rubriques_facturation.code IS DISTINCT FROM t_rubriques_facturation_in.code OR
					t_rubriques_facturation.texte IS DISTINCT FROM t_rubriques_facturation_in.texte OR
					t_rubriques_facturation.texte_court IS DISTINCT FROM t_rubriques_facturation_in.texte_court
					)
				;


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

				INSERT INTO activite[PX].t_prestations(
					oid,
					code,
					code_original,
					texte,
					texte_court,
					clinique_honoraire)
				SELECT 
					oid,
					code,  
					code_original,
					texte,
					texte_court,
					clinique_honoraire
				FROM activite.t_prestations
				WHERE oid NOT IN (SELECT oid FROM activite[PX].t_prestations) AND
					code NOT IN (SELECT code FROM activite[PX].t_prestations);
				
				
				INSERT INTO activite.t_protocoles(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '***', 'Non renseigné', 'Non renseigné' WHERE 0 NOT IN (SELECT oid FROM activite.t_protocoles);
				
				UPDATE activite.t_protocoles SET
					code = '[CPX]'::text || prot_px.code::text,
					texte = prot_px.texte,
					texte_court = prot_px.texte
				FROM
					activite[PX].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,  
					'[TPX]' || code_original,
					texte,
					texte_court
				FROM activite[PX].t_protocoles
				WHERE oid <> 0 AND oid + [IPX] NOT IN (SELECT oid FROM activite.t_protocoles);
				
				-- Motifs de non facturation
				-- Attention, il y a des codes génériques
				DELETE FROM activite.t_motif_non_facturation
				USING activite[PX].t_motif_non_facturation t_motif_non_facturation_source
				WHERE t_motif_non_facturation.oid = t_motif_non_facturation_source.oid AND
					t_motif_non_facturation.code <> t_motif_non_facturation_source.code AND
					[IPX] = 0
				;
				
				DELETE FROM activite.t_motif_non_facturation
				WHERE [IPX] = 0 AND
					oid < 100000000000 AND
					oid NOT IN (SELECT oid FROM activite[PX].t_motif_non_facturation)
				;
	
				INSERT INTO activite.t_motif_non_facturation(
					oid,
					code,
					code_original,
					texte,
					texte_court)
				SELECT 
					oid + [IPX],
					'[CPX]' || code,  
					CASE WHEN [IPX] = 0 AND code_original LIKE '*CTI%' THEN '' ELSE '[TPX]' END || code_original,
					texte,
					texte_court
				FROM activite[PX].t_motif_non_facturation
				WHERE oid <> 0 AND
					oid + [IPX] NOT IN (SELECT oid FROM activite.t_motif_non_facturation)
				AND '[CPX]' || code NOT IN (SELECT code FROM activite.t_motif_non_facturation)	
				;
				
				
				
				INSERT INTO activite.t_compte(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '', 'Non renseigné', 'Non renseigné' WHERE 0 NOT IN (SELECt oid FROM activite.t_compte);

				INSERT INTO activite.t_compte(
					code,
					code_original,
					texte,
					texte_court)
				SELECT 
					code,  
					code_original,
					texte,
					texte_court
				FROM activite[PX].t_compte
				WHERE oid <> 0 
					AND code_original NOT IN (SELECT code_original FROM activite.t_compte)
				;
				
				
				UPDATE activite.t_compte SET
					texte = t_compte_from.texte,
					texte_court = t_compte_from.texte_court,
					taux_tva = CASE WHEN t_compte_from.taux_tva > 0 THEN t_compte_from.taux_tva ELSE t_compte.taux_tva END
				FROM activite[PX].t_compte t_compte_from
				WHERE t_compte.code = t_compte_from.code AND
					[IPX] = 0 AND
					(
					t_compte.texte <> t_compte_from.texte OR
					t_compte.texte_court <> t_compte_from.texte_court OR
					t_compte.taux_tva <> CASE WHEN t_compte_from.taux_tva > 0 THEN t_compte_from.taux_tva ELSE t_compte.taux_tva END
					)
				;
				
				
				DROP TABLE IF EXISTS w_correspondances_compte;
				CREATE TEMP TABLE w_correspondances_compte AS
				SELECT 
					t_compte_source.oid AS oid_source,
					coalesce(t_compte.oid,0) AS oid
				FROM activite[PX].t_compte AS t_compte_source 
					LEFT JOIN activite.t_compte ON t_compte_source.code = t_compte.code AND t_compte_source.code <> '';
				
				CREATE INDEX w_correspondances_compte_1
				  ON w_correspondances_compte
				  USING btree
				  (oid_source);
				  
				  
				INSERT INTO activite.t_journal(oid, code_original, code, texte) 
				SELECT 0, 0, '***', 'Non renseigné' WHERE 0 NOT IN (SELECT oid FROM activite.t_journal);
				
				UPDATE activite.t_journal SET
					code = '[CPX]'::text || journal_from.code::text,
					texte = journal_from.texte
				FROM
					activite[PX].t_journal journal_from
				WHERE 1=1
					AND t_journal.oid = (journal_from.oid + [IPX])
					AND ((t_journal.code::text <> ('[CPX]'::text || journal_from.code::text)) OR (t_journal.texte <> journal_from.texte));

				INSERT INTO activite.t_journal(
					oid,
					code,
					code_original,
					texte)
				SELECT 
					oid + [IPX],
					'[CPX]' || code,  
					'[TPX]' || code_original,
					texte
				FROM activite[PX].t_journal
				WHERE oid <> 0 AND oid + [IPX] NOT IN (SELECT oid FROM activite.t_journal);
				
				
				INSERT INTO activite.t_risque (oid, code, texte) 
				SELECT 0, '**', 'Non renseigné' WHERE 0 NOT IN (SELECT oid FROM activite.t_risque);

				INSERT INTO activite.t_risque (
					code,
					texte)
				SELECT 
					t_risque.code,  
					t_risque.texte
				FROM 
					activite[PX].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 activite[PX].t_risque AS t_risque_source 
					LEFT JOIN activite.t_risque ON t_risque_source.code = t_risque.code;
				
				CREATE INDEX w_correspondances_risque_1
				  ON w_correspondances_risque
				  USING btree
				  (oid_source);
										
			]]></sqlcmd>
		</NODE>
		
	</NODE>
		
	<NODE name="POST" label="TRAITEMENTS COMPLEMENTAIRES">
	
		<NODE label="Fusion des séjours en double">
			<sqlcmd><![CDATA[
			
			
				-- Fusion manuelle (prépare à la fusion automatique en consolidant les sejours)
				UPDATE activite.p_mouvements_sejour SET
					nb_entrees_directes = 0
				FROM activite.t_sejour
				JOIN activite.p_sejours p_sejours_fusion ON t_sejour.no_sejour_fusion = p_sejours_fusion.no_sejour
				JOIN activite.p_sejours p_sejours ON t_sejour.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
				WHERE t_sejour.no_sejour_fusion <> '' AND
					p_mouvements_sejour.no_sejour = p_sejours_fusion.no_sejour AND
					p_mouvements_sejour.date = p_sejours_fusion.date_entree AND
					p_mouvements_sejour.nb_entrees_directes > 0;


				UPDATE activite.p_mouvements_sejour SET
					no_sejour = p_sejours.no_sejour,
					sejour_id = p_sejours.oid,
					nb_sorties_directes = 0,
					est_dernier_mouvement_jour = CASE WHEN nb_sorties_directes > 0 OR (p_mouvements_sejour.date = p_sejours.date_entree AND p_mouvements_sejour.heure_fin = 240000) THEN '0' ELSE p_mouvements_sejour.est_dernier_mouvement_jour END,
					heure_fin = CASE WHEN nb_sorties_directes > 0  OR (p_mouvements_sejour.date = p_sejours.date_entree AND p_mouvements_sejour.heure_fin = 240000) THEN to_char(to_timestamp(to_char(p_sejours.heure_entree,'FM000000'),'HH24MISS')::timestamp without time zone - interval '1 second','HH24MISS')::numeric  ELSE p_mouvements_sejour.heure_fin END
				FROM activite.t_sejour
				JOIN activite.p_sejours ON t_sejour.no_sejour_fusion = p_sejours.no_sejour
				WHERE t_sejour.no_sejour_fusion <> '' AND
					p_mouvements_sejour.no_sejour = t_sejour.no_sejour AND
					(p_mouvements_sejour.date < p_sejours.date_entree OR
					(p_mouvements_sejour.date = p_sejours.date_entree AND
					p_mouvements_sejour.heure_debut < p_sejours.heure_entree)
					);
	
				-- Fusion automatique sur nom, date de naissance (séjours présents chez les deux prestataires avec même date entrée)
				DROP TABLE IF EXISTS w_fusion_sejours;
				CREATE TEMP TABLE w_fusion_sejours AS
				SELECT
					p_sejours_1.no_sejour AS no_sejour_source, 
					p_sejours_1.oid AS oid_source, 
					p_sejours_1.date_entree AS date_entree_source, 
					p_patients_1.nom AS nom_source,  
					p_sejours_2.no_sejour AS no_sejour_cible, 
					p_sejours_2.oid AS oid_cible, 
					p_sejours_2.date_entree AS date_entree_cible, 
					p_patients_2.nom AS nom_cible
				FROM activite.p_sejours p_sejours_1
					JOIN activite.p_patients p_patients_1 ON p_sejours_1.no_patient = p_patients_1.no_patient
				JOIN activite.p_sejours p_sejours_2 ON 
					p_sejours_1.date_entree = p_sejours_2.date_entree AND
					p_sejours_1.no_sejour <> p_sejours_2.no_sejour AND
					p_sejours_1.oid >= 200000000000 AND 
					p_sejours_2.oid < 200000000000 AND
					(p_sejours_1.date_sortie = p_sejours_2.date_sortie OR p_sejours_1.code_sorti <> '1')
				JOIN activite.p_patients p_patients_2 ON p_sejours_2.no_patient = p_patients_2.no_patient
				WHERE 
					(base.cti_soundex_nom(p_patients_1.nom) = base.cti_soundex_nom(p_patients_2.nom) AND 
					base.cti_soundex_nom(p_patients_1.prenom) = base.cti_soundex_nom(p_patients_2.prenom) AND 
					p_patients_1.date_naissance = p_patients_2.date_naissance) 
					OR
					(base.cti_soundex_nom(p_patients_1.nom) = base.cti_soundex_nom(p_patients_2.nom_naissance) AND 
					base.cti_soundex_nom(p_patients_1.prenom) = base.cti_soundex_nom(p_patients_2.prenom) AND 
					p_patients_1.date_naissance = p_patients_2.date_naissance) 
					OR
					(upper(p_patients_1.nom) = upper(p_patients_2.nom) AND 
					p_patients_1.date_naissance = p_patients_2.date_naissance);

				-- Fusion automatique sur n° de séjour
				INSERT INTO w_fusion_sejours
				SELECT
					p_sejours_1.no_sejour AS no_sejour_source, 
					p_sejours_1.oid AS oid_source, 
					p_sejours_1.date_entree AS date_entree_source, 
					p_patients_1.nom AS nom_source,  
					p_sejours_2.no_sejour AS no_sejour_cible, 
					p_sejours_2.oid AS oid_cible, 
					p_sejours_2.date_entree AS date_entree_cible, 
					p_patients_2.nom AS nom_cible
				FROM activite.p_sejours p_sejours_1
					JOIN activite.p_patients p_patients_1 ON p_sejours_1.no_patient = p_patients_1.no_patient
					JOIN activite.p_sejours p_sejours_2 ON 
						substr(p_sejours_1.no_sejour,4) = p_sejours_2.no_sejour AND
						p_sejours_1.oid >= 200000000000 AND 
						p_sejours_2.oid < 200000000000 AND
						(p_sejours_1.date_entree = p_sejours_2.date_entree)
				JOIN activite.p_patients p_patients_2 ON p_sejours_2.no_patient = p_patients_2.no_patient
				WHERE 
					p_sejours_1.no_sejour NOT IN (SELECT no_sejour_source FROM w_fusion_sejours) AND
					(base.cti_soundex_nom(p_patients_1.nom) = base.cti_soundex_nom(p_patients_2.nom) AND 
					base.cti_soundex_nom(p_patients_1.prenom) = base.cti_soundex_nom(p_patients_2.prenom) AND 
					p_patients_1.date_naissance = p_patients_2.date_naissance) 
					OR
					(base.cti_soundex_nom(p_patients_1.nom) = base.cti_soundex_nom(p_patients_2.nom_naissance) AND 
					base.cti_soundex_nom(p_patients_1.prenom) = base.cti_soundex_nom(p_patients_2.prenom) AND 
					p_patients_1.date_naissance = p_patients_2.date_naissance) 
					OR
					(upper(p_patients_1.nom) = upper(p_patients_2.nom) AND 
					p_patients_1.date_naissance = p_patients_2.date_naissance)
					OR
					(upper(p_patients_1.nom) = 'INDISPONIBLE');
					
				-- Cas des doublons à supprimer complètement de l'ancien prestataire.
				INSERT INTO activite.t_divers (code, texte, valeur, description)
				SELECT
					'FUSION_DELETE',
					'Fusion. Suppression sejour ancien prestataire si sejour commun entre deux prestataires',
					'0'::text,
					''
				WHERE 'FUSION_DELETE' NOT IN (SELECT code FROM activite.t_divers);
				
				DELETE FROM activite.p_mouvements_sejour
				USING activite.t_divers
				WHERE t_divers.code = 'FUSION_DELETE' AND
					t_divers.valeur = '1' AND
					p_mouvements_sejour.no_sejour IN (SELECT oid_source FROM w_fusion_sejours)
				;
				
				DELETE FROM activite.p_sejours_assurance
				USING activite.t_divers
				WHERE t_divers.code = 'FUSION_DELETE' AND
					t_divers.valeur = '1' AND
					p_sejours_assurance.sejour_id IN (SELECT no_sejour_source FROM w_fusion_sejours)
				;
			
				DELETE FROM activite.p_factures_lignes_c
				USING activite.p_factures,
					activite.t_divers
				WHERE p_factures_lignes_c.no_facture = p_factures.no_facture AND
					t_divers.code = 'FUSION_DELETE' AND
					t_divers.valeur = '1' AND
					p_factures.no_sejour IN (SELECT no_sejour_source FROM w_fusion_sejours)
				;
				
				DELETE FROM activite.p_factures_lignes_h
				USING activite.p_factures,
					activite.t_divers
				WHERE p_factures_lignes_h.no_facture = p_factures.no_facture AND
					t_divers.code = 'FUSION_DELETE' AND
					t_divers.valeur = '1' AND
					p_factures.no_sejour IN (SELECT no_sejour_source FROM w_fusion_sejours)
				;
				
				DELETE FROM activite.p_factures_soldes_c
				USING activite.p_factures,
					activite.t_divers
				WHERE p_factures_soldes_c.no_facture = p_factures.no_facture AND
					t_divers.code = 'FUSION_DELETE' AND
					t_divers.valeur = '1' AND
					p_factures.no_sejour IN (SELECT no_sejour_source FROM w_fusion_sejours)
				;
				
				DELETE FROM activite.p_factures_soldes_h
				USING activite.p_factures,
					activite.t_divers
				WHERE p_factures_soldes_h.no_facture = p_factures.no_facture AND
					t_divers.code = 'FUSION_DELETE' AND
					t_divers.valeur = '1' AND
					p_factures.no_sejour IN (SELECT no_sejour_source FROM w_fusion_sejours)
				;
				
				DELETE FROM activite.p_factures
				USING activite.t_divers
				WHERE t_divers.code = 'FUSION_DELETE' AND
					t_divers.valeur = '1' AND
					p_factures.no_sejour IN (SELECT no_sejour_source FROM w_fusion_sejours)
				;
				
				DELETE FROM activite.p_factures_reference
				USING activite.t_divers
				WHERE t_divers.code = 'FUSION_DELETE' AND
					t_divers.valeur = '1' AND
					p_factures_reference.no_sejour IN (SELECT no_sejour_source FROM w_fusion_sejours)
				;
				
				DELETE FROM activite.p_factures_reference_tiers
				USING activite.t_divers
				WHERE t_divers.code = 'FUSION_DELETE' AND
					t_divers.valeur = '1' AND
					p_factures_reference_tiers.no_sejour IN (SELECT no_sejour_source FROM w_fusion_sejours)
				;


					UPDATE activite.p_factures
					SET sejour_id = oid_cible,
						no_sejour = no_sejour_cible
					FROM w_fusion_sejours
					WHERE p_factures.no_sejour = no_sejour_source;
					
					UPDATE activite.p_factures_reference
					SET sejour_id = oid_cible,
						no_sejour = no_sejour_cible
					FROM w_fusion_sejours
					WHERE p_factures_reference.no_sejour = no_sejour_source;
					
					UPDATE activite.p_factures_reference_tiers
					SET sejour_id = oid_cible,
						no_sejour = no_sejour_cible
					FROM w_fusion_sejours
					WHERE p_factures_reference_tiers.no_sejour = no_sejour_source;

					UPDATE activite.p_factures_encours
					SET sejour_id = oid_cible
					FROM w_fusion_sejours
					WHERE p_factures_encours.sejour_id = oid_source;

					UPDATE activite.p_factures_lignes_c
					SET lieu_id = p_mouvements_sejour.lieu_id
					FROM activite.p_factures,
						activite.p_mouvements_sejour
					WHERE
						p_factures_lignes_c.no_facture = p_factures.no_facture AND
						p_factures.no_sejour IN (SELECT no_sejour_cible FROM w_fusion_sejours) AND
						p_factures.no_sejour = p_mouvements_sejour.no_sejour AND
						p_factures_lignes_c.date_fin = p_mouvements_sejour.date AND
						(230000 BETWEEN p_mouvements_sejour.heure_debut AND p_mouvements_sejour.heure_fin OR 
						nb_entrees_directes = 1 AND heure_fin = 240000 OR 
						nb_sorties_directes = 1 ) AND
						p_factures_lignes_c.lieu_id <> p_mouvements_sejour.lieu_id;


					UPDATE activite.p_factures_lignes_h
					SET lieu_id = p_mouvements_sejour.lieu_id
					FROM activite.p_factures,
						activite.p_mouvements_sejour
					WHERE
						p_factures_lignes_h.no_facture = p_factures.no_facture AND
						p_factures.no_sejour IN (SELECT no_sejour_cible FROM w_fusion_sejours) AND
						p_factures.no_sejour = p_mouvements_sejour.no_sejour AND
						p_factures_lignes_h.date_fin = p_mouvements_sejour.date AND
						(230000 BETWEEN p_mouvements_sejour.heure_debut AND p_mouvements_sejour.heure_fin OR 
						nb_entrees_directes = 1 AND heure_fin = 240000 OR 
						nb_sorties_directes = 1 ) AND
						p_factures_lignes_h.lieu_id <> p_mouvements_sejour.lieu_id;
							
					-- Suppression des factures BG qui ont été récupérées 2 fois (on garde celle du provider en cours)		
					DROP TABLE IF EXISTS w_factures_bg_double;
					CREATE TEMP TABLE w_factures_bg_double AS
					SELECT no_sejour, (MAX(ARRAY[provider_id::text, no_facture]))[2] AS no_facture_double
					FROM activite.p_factures
					WHERE no_facture LIKE '%BG' AND
						code_facture = '1'
					GROUP BY 1
					HAVING count(DISTINCT provider_id) > 1
					;
					
					ANALYSE w_factures_bg_double
					;
										
					INSERT INTO w_factures_bg_double
					SELECT no_sejour, no_facture
					FROM
					(
					SELECT 
						MAX(CASE WHEN no_sejour = replace(no_facture,'.BG','') THEN '' ELSE no_sejour END) AS no_sejour,
						MAX(CASE WHEN no_sejour = replace(no_facture,'.BG','') THEN '' ELSE no_facture END) AS no_facture,
						CASE WHEN substr(no_facture,3,1) = '_' THEN substr(no_facture,4) ELSE no_facture END AS no_facture_g
					FROM activite.p_factures
					WHERE no_facture LIKE '%BG' AND
						code_facture = '1' 
					GROUP BY 3
					HAVING count(DISTINCT no_sejour) > 1 AND
						count(DISTINCT provider_id) > 1 AND
						MAX(CASE WHEN no_sejour = replace(no_facture,'.BG','') THEN 1 ELSE 0 END) > 0
					) subview
					WHERE no_sejour NOT IN (SELECT w_factures_bg_double.no_sejour FROM w_factures_bg_double)
					;
					
					
					-- BG sur ancien prestataire et en cours sur nouveau -> Supprimer encours
					INSERT INTO w_factures_bg_double
					SELECT no_sejour, (MIN(ARRAY[code_facture, no_facture]))[2] AS no_facture_double
					FROM activite.p_factures
					WHERE no_facture LIKE '%BG' AND
						no_sejour NOT IN (SELECT w_factures_bg_double.no_sejour FROM w_factures_bg_double)
					GROUP BY 1
					HAVING count(DISTINCT provider_id) > 1 AND MAX(code_facture) = '1'
					;
					
					INSERT INTO w_factures_bg_double
					SELECT no_sejour, no_facture
					FROM
					(
					SELECT 
						MAX(CASE WHEN no_sejour = replace(no_facture,'.BG','') THEN '' ELSE no_sejour END) AS no_sejour,
						MAX(CASE WHEN no_sejour = replace(no_facture,'.BG','') THEN '' ELSE no_facture END) AS no_facture,
						CASE WHEN substr(no_facture,3,1) = '_' THEN substr(no_facture,4) ELSE no_facture END AS no_facture_g
					FROM activite.p_factures
					WHERE no_facture LIKE '%BG'
					GROUP BY 3
					HAVING count(DISTINCT no_sejour) > 1 AND
						count(DISTINCT provider_id) > 1 AND
						MAX(CASE WHEN no_sejour = replace(no_facture,'.BG','') THEN 1 ELSE 0 END) > 0
					) subview
					WHERE no_sejour NOT IN (SELECT w_factures_bg_double.no_sejour FROM w_factures_bg_double)
					;
					
					
					ANALYSE w_factures_bg_double
					;

					DELETE FROM activite.p_factures
					USING w_factures_bg_double
					WHERE no_facture = no_facture_double
					;
					
					DELETE FROM activite.p_factures_lignes_c
					USING w_factures_bg_double
					WHERE no_facture = no_facture_double
					;
					
					DELETE FROM activite.p_factures_lignes_h
					USING w_factures_bg_double
					WHERE no_facture = no_facture_double
					;
						
						
					-- Suppression encours calculés chez les deux prestataires pour même séjour	
					UPDATE activite.p_factures
					SET type_facture = 'E'
					FROM activite.t_providers
					WHERE provider_id = t_providers.oid AND
						t_providers.code ILIKE 'SH' AND
						code_facture = '0' AND type_facture = '0'
					;
				
					DROP TABLE IF EXISTS w_factures_encours_trop;
					CREATE TEMP TABLE w_factures_encours_trop AS
					SELECT p_factures.no_sejour
					FROM activite.p_factures
					JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.provider_id = 0
					GROUP BY 1
					HAVING count(DISTINCT CASE WHEN type_facture = 'E' AND p_factures.provider_id > 0 THEN no_facture ELSE NULL END) > 0 AND
						count(DISTINCT CASE WHEN p_factures.provider_id = 0 THEN no_facture ELSE NULL END) > 0 AND
						count(DISTINCT CASE WHEN p_factures.provider_id > 0 THEN no_facture ELSE NULL END) > 0
					ORDER BY 1;
					
					-- Transfert des lignes non facturées sur les autres factures
					UPDATE activite.p_factures_lignes_non_facturees_c SET
						no_facture = p_factures_0.no_facture,
						facture_id = p_factures_0.oid
					FROM activite.p_factures p_factures_2
					JOIN w_factures_encours_trop ON w_factures_encours_trop.no_sejour = p_factures_2.no_sejour
					JOIN activite.p_factures p_factures_0 ON p_factures_2.no_sejour = p_factures_0.no_sejour AND
											p_factures_0.provider_id = 0 AND
											p_factures_0.type_facture = 'E'
					WHERE p_factures_lignes_non_facturees_c.no_facture = p_factures_2.no_facture AND 
						p_factures_2.provider_id > 0 AND
						p_factures_2.type_facture = 'E' AND
						p_factures_lignes_non_facturees_c.date_debut BETWEEN p_factures_0.date_debut AND p_factures_0.date_fin  AND
						p_factures_0.no_facture || '-' || p_factures_lignes_non_facturees_c.prestation_id NOT IN 
							(SELECT p_factures_lignes_non_facturees_c.no_facture || '-' || p_factures_lignes_non_facturees_c.prestation_id 
							FROM activite.p_factures_lignes_non_facturees_c)
					;
	
					-- Suppression lignes en trop
					DELETE
					FROM activite.p_factures_lignes_c
					USING activite.p_factures
					WHERE p_factures_lignes_c.no_facture = p_factures.no_facture AND
						p_factures.type_facture = 'E' AND 
						p_factures.provider_id > 0 AND
						p_factures.no_sejour IN (SELECT no_sejour FROM w_factures_encours_trop);

					-- Suppression factures en cours en trop
					DELETE
					FROM activite.p_factures
					WHERE no_sejour IN (SELECT no_sejour FROM w_factures_encours_trop) AND
						p_factures.type_facture = 'E' AND 
						p_factures.provider_id > 0;

						
					-- Suppression factures en cours prestataire actuel qui ont été facturées par le précédent
					DROP TABLE IF EXISTS w_factures_encours_trop;
					CREATE TEMP TABLE w_factures_encours_trop AS
					SELECT p_factures.no_sejour, 
						MIN(CASE WHEN p_factures.provider_id > 0 AND code_facture = '1' THEN date_debut ELSE null END) AS date_debut, 
						MAX(CASE WHEN p_factures.provider_id > 0 AND code_facture = '1' THEN date_fin ELSE null END) AS date_fin
					FROM activite.p_factures
					JOIN activite.p_sejours ON p_factures.no_sejour = p_sejours.no_sejour AND p_sejours.provider_id = 0
					GROUP BY 1
					HAVING count(DISTINCT CASE WHEN type_facture = 'E' AND p_factures.provider_id = 0 THEN no_facture ELSE NULL END) > 0 AND
						count(DISTINCT CASE WHEN type_facture <> 'E' AND p_factures.provider_id > 0 THEN no_facture ELSE NULL END) > 0 AND
						MIN(CASE WHEN p_factures.provider_id = 0 AND code_facture <> '1' THEN date_debut ELSE null END) <=
							MAX(CASE WHEN p_factures.provider_id > 0 AND code_facture = '1' THEN date_fin ELSE null END)
					ORDER BY 1;


					DELETE
					FROM activite.p_factures_lignes_c
					USING activite.p_factures
					JOIN w_factures_encours_trop ON w_factures_encours_trop.no_sejour = p_factures.no_sejour AND
						p_factures.date_debut < w_factures_encours_trop.date_fin
					WHERE p_factures_lignes_c.no_facture = p_factures.no_facture AND
						p_factures.type_facture = 'E' AND 
						p_factures.provider_id = 0 ;


					DELETE
					FROM activite.p_factures
					USING w_factures_encours_trop
					WHERE w_factures_encours_trop.no_sejour = p_factures.no_sejour AND
						p_factures.date_debut < w_factures_encours_trop.date_fin AND
						p_factures.type_facture = 'E' AND 
						p_factures.provider_id = 0;

					DELETE FROM activite.p_sejours WHERE no_sejour IN (SELECT no_sejour_source FROM w_fusion_sejours);
					DELETE FROM activite.p_mouvements_sejour WHERE no_sejour IN (SELECT no_sejour_source FROM w_fusion_sejours);
					DELETE FROM activite.p_sejours_assurance WHERE sejour_id IN (SELECT oid_source FROM w_fusion_sejours);
					
					-- Si séjours en double complet, les factures ont été supprimées et donc le chiffrier ne veux plus rien dire
					UPDATE activite.p_chiffrier_comptable SET
						montant_ventes_c = 0,
						montant_ventes_h = 0,
						montant_reglements_c = 0,
						montant_reglements_h = 0,
						montant_solde_client_c = 0,
						montant_solde_client_h = 0
					FROM activite.t_divers
					WHERE t_divers.code = 'FUSION_DELETE' AND
						t_divers.valeur = '1' AND
						provider_id = 2
					;


		]]></sqlcmd>
		</NODE>
		
		<NODE label="Lien séjour sur en-cours">
			<sqlcmd><![CDATA[
			
				UPDATE activite.p_factures_encours
				SET no_sejour = p_sejours.no_sejour, sejour_id = p_sejours.oid
				FROM activite.p_sejours
				WHERE substr(p_factures_encours.no_sejour,4) = p_sejours.no_sejour AND p_sejours.provider_id = 0 AND
					substr(p_factures_encours.no_sejour,3,1) = '_'
				;


				UPDATE activite.p_factures_encours
				SET no_sejour = p_sejours.no_sejour, sejour_id = p_sejours.oid
				FROM activite.p_sejours
				WHERE p_factures_encours.no_sejour = substr(p_sejours.no_sejour,4) AND p_sejours.provider_id > 0 AND
					substr(p_factures_encours.no_sejour,3,1) <> '_'
					;
	
			]]></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 date_sortie - date_entree NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'dursej')
				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 code_postal_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'c_postaux')
				GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid) 
				SELECT 'medecin_s', medecin_sejour_id 
				FROM activite.p_sejours 
				WHERE medecin_sejour_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'medecin_s')
				GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid) 
				SELECT 'medecin_s', medecin_sejour_id 
				FROM activite.p_mouvements_sejour 
				WHERE medecin_sejour_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'medecin_s')
				GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid) 
				SELECT 'ghs', ghs_id 
				FROM activite.p_sejours 
				WHERE ghs_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'ghs')
				GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid) 
				SELECT 'ghs', ghs_id 
				FROM activite.p_factures 
				WHERE ghs_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'ghs')
				GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid) 
				SELECT 'medecin_x', medecin_facture_id 
				FROM activite.p_factures_lignes_h 
				WHERE medecin_facture_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'medecin_x')
				GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'rubrique_c', rubrique_facturation_id
				FROM activite.p_factures_lignes_c WHERE rubrique_facturation_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'rubrique_c')
				GROUP BY 2;
	
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'rubrique_c', rubrique_comptabilisation_id
				FROM activite.p_factures_lignes_c WHERE rubrique_comptabilisation_id NOT IN (SELECT oid	FROM activite.p_oids WHERE code_table = 'rubrique_c')
				GROUP BY 2;
	
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'rubrique_c', rubrique_comptabilisee_id
				FROM activite.p_factures_soldes_c WHERE rubrique_comptabilisee_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'rubrique_c')
				GROUP BY 2;
	
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'prestation_c', prestation_id
				FROM activite.p_factures_lignes_c WHERE prestation_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'prestation_c')
				GROUP BY 2;
				
				
				
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'prestation_c', prestation_id
				FROM activite.p_factures_soldes_c WHERE prestation_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'prestation_c')
				GROUP BY 2;
				
				
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'prestation_h', prestation_id
				FROM activite.p_factures_lignes_h WHERE prestation_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'prestation_h')
				GROUP BY 2;
				
				
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'acte_h', acte_id
				FROM activite.p_factures_lignes_h WHERE acte_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'acte_h')
				GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'protocole_h', protocole_id
				FROM activite.p_factures_lignes_h 
				WHERE protocole_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'protocole_h')
				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 tiers_payant_0_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'tiers_payant')
				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 tiers_payant_1_id NOT IN (SELECT oid
				FROM activite.p_oids WHERE code_table = 'tiers_payant')
				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 tiers_payant_2_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'tiers_payant')
				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 tiers_payant_22_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'tiers_payant')
				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 
					t_services_facturation.mode_traitement_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'mode_traitement' AND oid IS NOT NULL) 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 lit_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'lit')
				GROUP BY 2;
				

					
				INSERT INTO activite.p_oids (code_table, oid)
				SELECT 'service', service_facturation_id
				FROM activite.t_lieux WHERE service_facturation_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'service')
				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 etage_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'etage')
				GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid) 
				SELECT 'uf', unite_fonctionnelle_id
				FROM activite.t_lieux WHERE unite_fonctionnelle_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'uf')
				GROUP BY 2;
				
				INSERT INTO activite.p_oids (code_table, oid) 
				SELECT 'um', unite_medicale_id
				FROM activite.t_lieux WHERE unite_medicale_id NOT IN (SELECT oid FROM activite.p_oids WHERE code_table = 'um')
				GROUP BY 2;
										
				INSERT INTO activite[PX].p_oids (code_table, oid) 
				SELECT 'medecin_t', medecin_traitant_id 
				FROM activite[PX].p_sejours 
				WHERE medecin_traitant_id NOT IN (SELECT oid FROM activite[PX].p_oids WHERE code_table = 'medecin_t')
				GROUP BY 2;
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Tables répliquées">
			<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="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="En_cours">
			<sqlcmd><![CDATA[
				
				
				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_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');
				
				DELETE FROM activite.p_factures_encours_lignes_c 
					WHERE no_facture IN (SELECT no_facture FROM activite.p_factures_encours WHERE code_origine = 'P')
				;
				DELETE FROM activite.p_factures_encours WHERE code_origine = 'P';
				
				SELECT base.cti_disable_index('activite', 'i_factures_encours_1');
				SELECT base.cti_disable_index('activite', 'i_factures_encours_lignes_c_1');
				
				

			]]></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>
	
	<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="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>
	
	
	
	
	<NODE name="INIT_SI_FUSION" label="INITIALISATIONS SI FUSION">
	
		<NODE label="Synchronisation schéma">
		
			<sqlcmd><![CDATA[

				SELECT base.cti_synchronize_schema('activite', 'activite[PX]');
										
										
			]]></sqlcmd>
		
		</NODE>
	
		<NODE label="Paramètres CTI fusionnés">
			<sqlcmd><![CDATA[

				-- Services
				
				UPDATE activite[PX].t_services_facturation
				SET mode_traitement_id = t_services_facturation_fusionne.mode_traitement_id,
					dmt_id = t_services_facturation_fusionne.dmt_id,
					type_t2a = t_services_facturation_fusionne.type_t2a,
					type_sejour = t_services_facturation_fusionne.type_sejour,
					est_sans_facturation = t_services_facturation_fusionne.est_sans_facturation,
					avec_facturation_intermediaire = t_services_facturation_fusionne.avec_facturation_intermediaire,
					est_budget_global = t_services_facturation_fusionne.est_budget_global
				FROM activite.t_services_facturation t_services_facturation_fusionne
				WHERE t_services_facturation_fusionne.oid = t_services_facturation.oid + [IPX] AND
					(
						t_services_facturation.mode_traitement_id IS DISTINCT FROM t_services_facturation_fusionne.mode_traitement_id OR
						t_services_facturation.dmt_id IS DISTINCT FROM t_services_facturation_fusionne.dmt_id OR
						t_services_facturation.type_t2a IS DISTINCT FROM t_services_facturation_fusionne.type_t2a OR
						t_services_facturation.type_sejour IS DISTINCT FROM t_services_facturation_fusionne.type_sejour OR
						t_services_facturation.est_sans_facturation IS DISTINCT FROM t_services_facturation_fusionne.est_sans_facturation OR
						t_services_facturation.avec_facturation_intermediaire IS DISTINCT FROM t_services_facturation_fusionne.avec_facturation_intermediaire OR
						t_services_facturation.est_budget_global IS DISTINCT FROM t_services_facturation_fusionne.est_budget_global
					)
				
				;
				
				
				-- Unités fonctionnelles
				
				UPDATE activite[PX].t_unites_fonctionnelles
				SET 
					est_sans_facturation = t_unites_fonctionnelles_fusionne.est_sans_facturation
				FROM activite.t_unites_fonctionnelles t_unites_fonctionnelles_fusionne
				WHERE t_unites_fonctionnelles_fusionne.oid = t_unites_fonctionnelles.oid + [IPX] AND
					(
						t_unites_fonctionnelles.est_sans_facturation IS DISTINCT FROM t_unites_fonctionnelles_fusionne.est_sans_facturation
					)
				
				;
				
				
				-- Lits
				
				UPDATE activite[PX].t_lits
				SET 
					chambre_particuliere = t_lits_fusionne.chambre_particuliere
				FROM activite.t_lits t_lits_fusionne
				WHERE t_lits_fusionne.oid = t_lits.oid + [IPX] AND
					(
						t_lits.chambre_particuliere IS DISTINCT FROM t_lits_fusionne.chambre_particuliere
					)
				
				;
				
				-- Médecins
				
				UPDATE activite[PX].t_medecins_administratifs
				SET 
					est_medecin_salarie = t_medecins_administratifs_fusionne.est_medecin_salarie
				FROM activite.t_medecins_administratifs t_medecins_administratifs_fusionne
				WHERE t_medecins_administratifs_fusionne.oid = t_medecins_administratifs.oid + [IPX] AND
					(
						t_medecins_administratifs.est_medecin_salarie IS DISTINCT FROM t_medecins_administratifs_fusionne.est_medecin_salarie
					)
				
				;
				
				-- Taux de TVA dans comptes (pour ecritures comptables)
				UPDATE activite[PX].t_compte
				SET 
					taux_tva = t_compte_fusionne.taux_tva
				FROM activite.t_compte t_compte_fusionne
				WHERE t_compte_fusionne.code = t_compte.code AND
					(
						t_compte.taux_tva IS DISTINCT FROM t_compte_fusionne.taux_tva
					)
				;
										
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Tables remplacées">
		
			<sqlcmd><![CDATA[

				DELETE FROM activite.p_factures_encours_lignes_c 
					WHERE no_facture IN (SELECT no_facture FROM activite.p_factures_encours WHERE code_origine = 'P')
				;
				DELETE FROM activite.p_factures_encours WHERE code_origine = 'P';
						
			]]></sqlcmd>
		
		</NODE>
		
	</NODE>
	
	
	
	
</ROOT>
