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

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

	<NODE name="PRODUCTION" label="RÉCUPÉRATION DE LA PRODUCTION">

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


				-- Commandes

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM eco.p_commandes
					', 'temp p_commandes'
				);

				INSERT INTO eco.p_commandes(
					provider_id,
					numero,
					oid,
					date_commande,
					objet,
					reference,
					date_livraison_prevue,
					etat_reception,
					date_reception,
					fournisseur_id,
					gestionnaire_id,
					lieu_facturation_id,
					lieu_livraison_id,
					montant_commande_ht,
					montant_commande_remise,
					montant_commande_tva,
					montant_commande_port,
					montant_commande_total,
					montant_liquidation_ht,
					montant_liquidation_remise,
					montant_liquidation_tva,
					montant_liquidation_port,
					montant_liquidation_total,
					etat_liquidation,
					code_original,
					unite_fonctionnelle_id,
					lieu_commande_id
				)
				SELECT
					[PPX] AS provider_id,
					'[CPX]' || numero AS numero,
					oid + [IPX] AS oid,
					date_commande,
					objet,
					reference,
					date_livraison_prevue,
					etat_reception,
					date_reception,
					COALESCE(c_fournisseurs.to_oid,0) AS fournisseur_id,
					COALESCE(c_gestionnaires.to_oid,0) AS gestionnaire_id,
					COALESCE(c_lieux_facturation.to_oid,0) AS lieu_facturation_id,
					COALESCE(c_lieux_livraison.to_oid,0) AS lieu_livraison_id,
					montant_commande_ht,
					montant_commande_remise,
					montant_commande_tva,
					montant_commande_port,
					montant_commande_total,
					montant_liquidation_ht,
					montant_liquidation_remise,
					montant_liquidation_tva,
					montant_liquidation_port,
					montant_liquidation_total,
					etat_liquidation,
					'[CPX]' || code_original,
					COALESCE(c_unites_fonctionnelles.to_oid,0) AS unite_fonctionnelle_id,
					COALESCE(c_lieux_commande.to_oid,0) AS lieu_commande_id
				FROM p_commandes
				LEFT JOIN c_fournisseurs ON fournisseur_id = c_fournisseurs.from_oid
				LEFT JOIN c_gestionnaires ON gestionnaire_id = c_gestionnaires.from_oid
				LEFT JOIN c_lieux c_lieux_facturation ON lieu_facturation_id = c_lieux_facturation.from_oid
				LEFT JOIN c_lieux c_lieux_livraison ON lieu_livraison_id = c_lieux_livraison.from_oid
				LEFT JOIN c_lieux c_lieux_commande ON lieu_commande_id = c_lieux_commande.from_oid
				LEFT JOIN c_unites_fonctionnelles ON unite_fonctionnelle_id = c_unites_fonctionnelles.from_oid
				;


				-- Lignes Commandes

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM eco.p_lignes_commandes
					', 'temp p_lignes_commandes'
				);

				INSERT INTO eco.p_lignes_commandes(
					oid,
					commande_id,
					ligne_commande,
					texte,
					article_id,
					ucd_id,
					lpp_id,
					unite_approvisionnement_id,
					compte_id,
					quantite_approvisionnement,
					prix_unitaire_approvisionnement,
					multiplicateur_stockage,
					quantite_stockage,
					prix_unitaire_stockage,
					montant_commande_ht,
					montant_commande_remise,
					montant_commande_tva,
					montant_commande_port,
					montant_commande_ttc,
					quantite_livraison_approvisionnement,
					quantite_livraison_stockage,
					etat_livraison,
					montant_livraison_ht,
					montant_livraison_ttc,
					montant_liquidation_ht,
					montant_liquidation_remise,
					montant_liquidation_tva,
					montant_liquidation_port,
					montant_liquidation_ttc,
					etat_liquidation,
					code_original,
					article_texte_specifique,
					taux_prorata_tva,
					montant_livraison_tva
					)
				SELECT
					oid + [IPX] AS oid,
					commande_id + [IPX] AS commande_id,
					ligne_commande,
					texte,
					COALESCE(c_articles.to_oid,0) AS article_id,
					COALESCE(c_ucd.to_oid,0) AS ucd_id,
					COALESCE(c_lpp.to_oid,0) AS lpp_id,
					COALESCE(c_unites_approvisionnement.to_oid,0) AS unite_approvisionnement_id,
					COALESCE(c_compte.to_oid,0) AS compte_id,
					quantite_approvisionnement,
					prix_unitaire_approvisionnement,
					multiplicateur_stockage,
					quantite_stockage,
					prix_unitaire_stockage,
					montant_commande_ht,
					montant_commande_remise,
					montant_commande_tva,
					montant_commande_port,
					montant_commande_ttc,
					quantite_livraison_approvisionnement,
					quantite_livraison_stockage,
					etat_livraison,
					montant_livraison_ht,
					montant_livraison_ttc,
					montant_liquidation_ht,
					montant_liquidation_remise,
					montant_liquidation_tva,
					montant_liquidation_port,
					montant_liquidation_ttc,
					etat_liquidation,
					'[CPX]' || code_original,
					article_texte_specifique,
					taux_prorata_tva,
					montant_livraison_tva
				FROM p_lignes_commandes
				LEFT JOIN c_articles ON article_id = c_articles.from_oid
				LEFT JOIN c_unites c_unites_approvisionnement ON unite_approvisionnement_id = c_unites_approvisionnement.from_oid
				LEFT JOIN c_compte ON compte_id = c_compte.from_oid
				LEFT JOIN c_ucd ON ucd_id = c_ucd.from_oid
				LEFT JOIN c_lpp ON lpp_id = c_lpp.from_oid




			]]></sqlcmd>
		</NODE>

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


				-- Commandes

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM eco.p_mouvements_articles
					', 'temp p_mouvements_articles'
				);

				INSERT INTO eco.p_mouvements_articles(
					oid,
					provider_id,
					date,
					type_mouvement_id,
					texte,
					gestionnaire_id,
					lieu_id,
					profil_comptable_id,
					article_id,
					compte_id,
					prix_unitaire,
					entree_quantite,
					sortie_quantite,
					fournisseur_id,
					commande_id,
					ligne_commande,
					stock_quantite_debut,
					stock_quantite_fin,
					entree_montant,
					entree_montant_ht,
					sortie_montant,
					sortie_montant_ht,
					sens_mouvement,
					ucd_id,
					lpp_id,
					unite_fonctionnelle_id
				)
				SELECT
					oid + [IPX] AS oid,
					[PPX] AS provider_id,
					date,
					COALESCE(c_types_mouvements.to_oid,0) AS type_mouvement_id,
					texte,
					COALESCE(c_gestionnaires.to_oid,0) AS gestionnaire_id,
					COALESCE(c_lieux.to_oid,0) AS lieu_id,
					profil_comptable_id + [IPX] AS profil_comptable_id,
					COALESCE(c_articles.to_oid,0) AS article_id,
					COALESCE(c_compte.to_oid,0) AS compte_id,
					prix_unitaire,
					entree_quantite,
					sortie_quantite,
					COALESCE(c_fournisseurs.to_oid,0) AS fournisseur_id,
					commande_id + [IPX] AS commande_id,
					ligne_commande,
					stock_quantite_debut,
					stock_quantite_fin,
					entree_montant,
					entree_montant_ht,
					sortie_montant,
					sortie_montant_ht,
					sens_mouvement,
					COALESCE(c_ucd.to_oid,0) AS ucd_id,
					COALESCE(c_lpp.to_oid,0) AS lpp_id,
					COALESCE(c_unites_fonctionnelles.to_oid,0) AS unite_fonctionnelle_id
				FROM p_mouvements_articles
				LEFT JOIN c_fournisseurs ON fournisseur_id = c_fournisseurs.from_oid
				LEFT JOIN c_types_mouvements ON type_mouvement_id = c_types_mouvements.from_oid
				LEFT JOIN c_gestionnaires ON gestionnaire_id = c_gestionnaires.from_oid
				LEFT JOIN c_lieux ON lieu_id = c_lieux.from_oid
				LEFT JOIN c_articles ON article_id = c_articles.from_oid
				LEFT JOIN c_compte ON compte_id = c_compte.from_oid
				LEFT JOIN c_ucd ON ucd_id = c_ucd.from_oid
				LEFT JOIN c_lpp ON lpp_id = c_lpp.from_oid
				LEFT JOIN c_unites_fonctionnelles ON unite_fonctionnelle_id = c_unites_fonctionnelles.from_oid
				;


			]]></sqlcmd>
		</NODE>

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


				-- Commandes

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM eco.p_stock
					', 'temp p_stock'
				);

				INSERT INTO eco.p_stock(
					oid,
					provider_id,
					date_fin,
					date_debut,
					article_id,
					ucd_id,
					lpp_id,
					compte_id,
					lieu_id,
					stock_quantite_debut,
					stock_valeur_debut,
					entree_quantite,
					entree_montant,
					sortie_quantite,
					sortie_montant,
					stock_quantite_fin,
					stock_valeur_fin,
					pump
				)
				SELECT
					oid + [IPX] AS oid,
					[PPX] AS provider_id,
					date_fin,
					date_debut,
					COALESCE(c_articles.to_oid,0) AS article_id,
					COALESCE(c_ucd.to_oid,0) AS ucd_id,
					COALESCE(c_lpp.to_oid,0) AS lpp_id,
					COALESCE(c_compte.to_oid,0) AS compte_id,
					COALESCE(c_lieux.to_oid,0) AS lieu_id,
					stock_quantite_debut,
					stock_valeur_debut,
					entree_quantite,
					entree_montant,
					sortie_quantite,
					sortie_montant,
					stock_quantite_fin,
					stock_valeur_fin,
					pump
				FROM p_stock
				LEFT JOIN c_lieux ON lieu_id = c_lieux.from_oid
				LEFT JOIN c_articles ON article_id = c_articles.from_oid
				LEFT JOIN c_compte ON compte_id = c_compte.from_oid
				LEFT JOIN c_ucd ON ucd_id = c_ucd.from_oid
				LEFT JOIN c_lpp ON lpp_id = c_lpp.from_oid

				;


			]]></sqlcmd>
		</NODE>

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


				-- Factures

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM eco.p_facture
					', 'temp p_facture'
				);

				INSERT INTO eco.p_facture(
					oid,
					provider_id,
					code_original,
					no_facture,
					no_liquidation,
					date_facture,
					texte,
					commande_id,
					fournisseur_id,
					montant_facture_ht,
					montant_facture_tva,
					montant_facture_ttc,
					montant_facture_article_ht,
					montant_facture_article_tva,
					montant_facture_article_ttc,
					montant_facture_port_ht,
					montant_facture_port_tva,
					montant_facture_port_ttc
				)
				SELECT
					oid + [IPX] AS oid,
					[PPX] AS provider_id,
					'[CPX]' || code_original AS code_original,
					'[CPX]' || no_facture AS no_facture,
					'[CPX]' || no_liquidation AS no_liquidation,
					date_facture,
					texte,
					commande_id + [IPX] AS commande_id,
					COALESCE(c_fournisseurs.to_oid,0) AS fournisseur_id,
					montant_facture_ht,
					montant_facture_tva,
					montant_facture_ttc,
					montant_facture_article_ht,
					montant_facture_article_tva,
					montant_facture_article_ttc,
					montant_facture_port_ht,
					montant_facture_port_tva,
					montant_facture_port_ttc
				FROM p_facture
				LEFT JOIN c_fournisseurs ON fournisseur_id = c_fournisseurs.from_oid
				;

				-- Lignes Factures

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM eco.p_lignes_facture
					', 'temp p_lignes_facture'
				);

				INSERT INTO eco.p_lignes_facture(
					oid,
					code_original,
					facture_id,
					ligne_commande,
					ligne_commande_id,
					article_id,
					compte_id,
					no_liquidation,
					montant_facture_ht,
					montant_facture_tva,
					montant_facture_ttc,
					montant_facture_article_ht,
					montant_facture_article_tva,
					montant_facture_article_ttc,
					montant_facture_port_ht,
					montant_facture_port_tva,
					montant_facture_port_ttc,
					taux_prorata_tva
				)
				SELECT
					oid + [IPX] AS oid,
					'[CPX]' || code_original AS code_original,
					facture_id + [IPX] AS facture_id,
					ligne_commande,
					ligne_commande_id + [IPX] AS ligne_commande_id,
					COALESCE(c_articles.to_oid,0) AS article_id,
					COALESCE(c_compte.to_oid,0) AS compte_id,
					'[CPX]' || no_liquidation AS no_liquidation,
					montant_facture_ht,
					montant_facture_tva,
					montant_facture_ttc,
					montant_facture_article_ht,
					montant_facture_article_tva,
					montant_facture_article_ttc,
					montant_facture_port_ht,
					montant_facture_port_tva,
					montant_facture_port_ttc,
					taux_prorata_tva
				FROM p_lignes_facture
				LEFT JOIN c_articles ON article_id = c_articles.from_oid
				LEFT JOIN c_compte ON compte_id = c_compte.from_oid
				;
			]]></sqlcmd>
		</NODE>

		<NODE label="Chiffrier">
			<sqlcmd><![CDATA[
				TRUNCATE eco.p_chiffrier_production;

				INSERT INTO eco.p_chiffrier_production (mois)
				SELECT extract('year' FROM date) * 100 + extract('month' FROM date)
				FROM eco.p_mouvements_articles
				GROUP BY 1;

			]]></sqlcmd>
		</NODE>

	</NODE>

	<NODE name="PARAM" label="RÉCUPÉRATION DES PARAMÈTRES">

		<NODE label="Initialisation paramètres">
			<sqlcmd><![CDATA[

				SELECT eco.cti_initialize_tables();

				INSERT INTO eco.t_providers(
					oid,
					code,
					texte)
				SELECT
					[PPX] AS oid,
					'[PX]' AS code,
					'[TX]' AS texte
				WHERE [PPX] NOT IN (SELECT oid FROM eco.t_providers);

			]]></sqlcmd>
		</NODE>

		<NODE label="Tables Articles">
			<sqlcmd><![CDATA[

				-- ucd

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM base.t_ucd
					WHERE oid <> 0
					', 'temp t_ucd'
				);

				INSERT INTO base.t_ucd(
					code,
					texte,
					texte_court,
					laboratoire_texte,
					conditionnement_texte,
					atc_code,
					atc_texte,
					ephmra_code,
					ephmra_texte,
					prestation_defaut_id)
				SELECT
					code,
					texte,
					texte_court,
					laboratoire_texte,
					conditionnement_texte,
					atc_code,
					atc_texte,
					ephmra_code,
					ephmra_texte,
					0
				FROM t_ucd
				WHERE code NOT IN (SELECT code FROM base.t_ucd WHERE code IS NOT NULL);

				DROP TABLE IF EXISTS c_ucd;
				CREATE TEMP TABLE c_ucd AS
				SELECT
					t_ucd_from.oid AS from_oid,
					COALESCE(t_ucd.oid,0) AS to_oid
				FROM t_ucd t_ucd_from
				LEFT JOIN base.t_ucd ON t_ucd_from.code = t_ucd.code;

				-- lpp

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM base.t_lpp
					WHERE oid <> 0
					', 'temp t_lpp'
				);

				INSERT INTO base.t_lpp(
					code,
					texte,
					texte_court)
				SELECT
					code,
					texte,
					texte_court
				FROM t_lpp
				WHERE code NOT IN (SELECT code FROM base.t_lpp WHERE code IS NOT NULL);

				DROP TABLE IF EXISTS c_lpp;
				CREATE TEMP TABLE c_lpp AS
				SELECT
					t_lpp_from.oid AS from_oid,
					COALESCE(t_lpp.oid,0) AS to_oid
				FROM t_lpp t_lpp_from
				LEFT JOIN base.t_lpp ON t_lpp_from.code = t_lpp.code;

				-- comptes
				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM eco.t_compte
					WHERE oid <> 0
					', 'temp t_compte'
				);

                -- Traitement des codes déjà remontés avec l'ancienne stratégie : on enlève le suffixe aux codes.
                UPDATE eco.t_compte SET
                    code = replace(code, '[TPX]', '')
                WHERE strpos(code, '[TPX]') > 0
                ;

                -- Màj des champs potentiellement màj dans l'env source.
                -- Le code est traité comme le texte[_court].
                UPDATE eco.t_compte SET
                    code = t_compte_from.code,
                    texte = t_compte_from.texte,
                    texte_court = t_compte_from.texte_court
                FROM t_compte AS t_compte_from
				WHERE 1=1
                    AND t_compte.code_original = '[CPX]' || t_compte_from.oid
                    AND (1!=1
                        OR t_compte.code IS DISTINCT FROM t_compte_from.code
                        OR t_compte.texte IS DISTINCT FROM t_compte_from.texte
                        OR t_compte.texte_court IS DISTINCT FROM t_compte_from.texte_court)
				;

                -- Insertion des nouveaux comptes.
				INSERT INTO eco.t_compte (
					code,
					code_original,
					texte,
					texte_court)
                    SELECT
                        code AS code,
                        '[CPX]' || oid AS code_original,
                        texte,
                        texte_court
                    FROM t_compte
                    WHERE '[CPX]' || oid NOT IN (SELECT code_original FROM eco.t_compte WHERE code_original IS NOT NULL)
                ;

                -- Création de la table de correspondance oid env source -> oid consolidé.
                -- Lorsque le code/texte est identique n'en prendre qu'un seul (au hasard) pour le "fusionner".
				DROP TABLE IF EXISTS c_compte
                ;
				CREATE TEMP TABLE c_compte AS
                    SELECT
                        t_compte_from.oid AS from_oid,
                        max(COALESCE(t_compte_fusion.oid, t_compte.oid, 0)) AS to_oid
                    FROM t_compte AS t_compte_from
                        LEFT JOIN eco.t_compte ON t_compte.code_original = '[CPX]' || t_compte_from.oid
                        LEFT JOIN eco.t_compte as t_compte_fusion ON 1=1
                            AND t_compte_fusion.code = t_compte_from.code
                            AND t_compte_fusion.texte = t_compte_from.texte
                    GROUP BY 1
                ;

				-- ATC

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM eco.t_classification_atc
					WHERE oid <> 0
					', 'temp t_classification_atc'
				);


				INSERT INTO eco.t_classification_atc
				(
					code,
					code_original,
					texte,
					texte_court
				)
				SELECT
					code,
					code_original,
					texte,
					texte_court
				FROM t_classification_atc
				WHERE code NOT IN
					(SELECT code FROM eco.t_classification_atc WHERE code IS NOT NULL);


				DROP TABLE IF EXISTS c_classification_atc;
				CREATE TEMP TABLE c_classification_atc AS
				SELECT
					t_classification_atc_from.oid AS from_oid,
					COALESCE(t_classification_atc.oid,0) AS to_oid
				FROM t_classification_atc t_classification_atc_from
				LEFT JOIN eco.t_classification_atc ON t_classification_atc_from.code = t_classification_atc.code;


				-- Standard ATC
				SELECT eco.cti_reorganize_classification_atc();



				-- unites

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *, 0::bigint AS to_oid
					FROM eco.t_unites
					WHERE oid <> 0
					', 'temp t_unites_from'
				);


				UPDATE t_unites_from
				SET to_oid = t_unites.oid
				FROM eco.t_unites
				WHERE upper(t_unites.code) = upper(t_unites_from.code) || '[TPX]' AND
					upper(t_unites.texte) = upper(t_unites_from.texte) OR
					t_unites.oid = t_unites_from.oid + [IPX];


				INSERT INTO eco.t_unites
				(
					oid,
					code,
					code_original,
					texte,
					texte_court
				)
				SELECT
					oid + [IPX],
					code || '[TPX]' AS code,
					'[CPX]' || oid AS code_original,
					texte,
					texte_court
				FROM t_unites_from
				WHERE to_oid = 0;

				DROP TABLE IF EXISTS c_unites;
				CREATE TEMP TABLE c_unites AS
				SELECT
					oid AS from_oid,
					CASE WHEN to_oid <> 0 THEN to_oid ELSE oid + [IPX] END AS to_oid
				FROM t_unites_from;




				-- familles articles

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *, 0::bigint AS to_oid
					FROM eco.t_familles_articles
					WHERE oid <> 0
					', 'temp t_familles_articles_from'
				);


				UPDATE t_familles_articles_from
				SET to_oid = t_familles_articles.oid
				FROM eco.t_familles_articles
				WHERE upper(t_familles_articles.code) = upper(t_familles_articles_from.code) || '[TPX]'  AND
					upper(t_familles_articles.texte) = upper(t_familles_articles_from.texte) OR
					t_familles_articles.oid = t_familles_articles_from.oid + [IPX];


				INSERT INTO eco.t_familles_articles
				(
					oid,
					code,
					code_original,
					texte,
					texte_court
				)
				SELECT
					oid + [IPX],
					code || '[TPX]' AS code,
					'[CPX]' || oid AS code_original,
					texte,
					texte_court
				FROM t_familles_articles_from
				WHERE to_oid = 0 ;

				DROP TABLE IF EXISTS c_familles_articles;
				CREATE TEMP TABLE c_familles_articles AS
				SELECT
					oid AS from_oid,
					CASE WHEN to_oid <> 0 THEN to_oid ELSE oid + [IPX] END AS to_oid
				FROM t_familles_articles_from;



				-- sous_familles articles

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *, 0::bigint AS to_oid
					FROM eco.t_sous_familles_articles
					WHERE oid <> 0
					', 'temp t_sous_familles_articles_from'
				);


				UPDATE t_sous_familles_articles_from
				SET to_oid = t_sous_familles_articles.oid
				FROM eco.t_sous_familles_articles
				WHERE upper(t_sous_familles_articles.code) = upper(t_sous_familles_articles_from.code) || '[TPX]'  AND
					upper(t_sous_familles_articles.texte) = upper(t_sous_familles_articles_from.texte) OR
					t_sous_familles_articles.oid = t_sous_familles_articles_from.oid + [IPX];


				INSERT INTO eco.t_sous_familles_articles
				(
					oid,
					code,
					code_original,
					texte,
					texte_court
				)
				SELECT
					oid + [IPX],
					code || '[TPX]' AS code,
					'[CPX]' || oid AS code_original,
					texte,
					texte_court
				FROM t_sous_familles_articles_from
				WHERE to_oid = 0 ;

				DROP TABLE IF EXISTS c_sous_familles_articles;
				CREATE TEMP TABLE c_sous_familles_articles AS
				SELECT
					oid AS from_oid,
					CASE WHEN to_oid <> 0 THEN to_oid ELSE oid + [IPX] END AS to_oid
				FROM t_sous_familles_articles_from;


				-- types articles

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *, 0::bigint AS to_oid
					FROM eco.t_types_articles
					WHERE oid <> 0
					', 'temp t_types_articles_from'
				);


				UPDATE t_types_articles_from
				SET to_oid = t_types_articles.oid
				FROM eco.t_types_articles
				WHERE (upper(t_types_articles.code) = upper(t_types_articles_from.code) || '[TPX]'  AND
					upper(t_types_articles.texte) = upper(t_types_articles_from.texte)) OR
					t_types_articles.oid = t_types_articles_from.oid + [IPX];


				INSERT INTO eco.t_types_articles
				(
					oid,
					code,
					code_original,
					texte,
					texte_court
				)
				SELECT
					oid + [IPX],
					code || '[TPX]' AS code,
					'[CPX]' || oid AS code_original,
					texte,
					texte_court
				FROM t_types_articles_from
				WHERE to_oid = 0 ;

				DROP TABLE IF EXISTS c_types_articles;
				CREATE TEMP TABLE c_types_articles AS
				SELECT
					oid AS from_oid,
					CASE WHEN to_oid <> 0 THEN to_oid ELSE oid + [IPX] END AS to_oid
				FROM t_types_articles_from;




				-- categories articles

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *, 0::bigint AS to_oid
					FROM eco.t_categories_articles
					WHERE oid <> 0
					', 'temp t_categories_articles_from'
				);


				UPDATE t_categories_articles_from
				SET to_oid = t_categories_articles.oid
				FROM eco.t_categories_articles
				WHERE upper(t_categories_articles.code) = upper(t_categories_articles_from.code) || '[TPX]'  AND
					upper(t_categories_articles.texte) = upper(t_categories_articles_from.texte) OR
					t_categories_articles.oid = t_categories_articles_from.oid + [IPX];


				INSERT INTO eco.t_categories_articles
				(
					oid,
					code,
					code_original,
					texte,
					texte_court
				)
				SELECT
					oid + [IPX],
					code || '[TPX]' AS code,
					'[CPX]' || oid AS code_original,
					texte,
					texte_court
				FROM t_categories_articles_from
				WHERE to_oid = 0 ;

				DROP TABLE IF EXISTS c_categories_articles;
				CREATE TEMP TABLE c_categories_articles AS
				SELECT
					oid AS from_oid,
					CASE WHEN to_oid <> 0 THEN to_oid ELSE oid + [IPX] END AS to_oid
				FROM t_categories_articles_from;



				-- sous_categories articles

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *, 0::bigint AS to_oid
					FROM eco.t_sous_categories_articles
					WHERE oid <> 0
					', 'temp t_sous_categories_articles_from'
				);


				UPDATE t_sous_categories_articles_from
				SET to_oid = t_sous_categories_articles.oid
				FROM eco.t_sous_categories_articles
				WHERE upper(t_sous_categories_articles.code) = upper(t_sous_categories_articles_from.code) || '[TPX]'  AND
					upper(t_sous_categories_articles.texte) = upper(t_sous_categories_articles_from.texte) OR
					t_sous_categories_articles.oid = t_sous_categories_articles_from.oid + [IPX];


				INSERT INTO eco.t_sous_categories_articles
				(
					oid,
					code,
					code_original,
					texte,
					texte_court
				)
				SELECT
					oid + [IPX],
					code || '[TPX]' AS code,
					'[CPX]' || oid AS code_original,
					texte,
					texte_court
				FROM t_sous_categories_articles_from
				WHERE to_oid = 0 ;

				DROP TABLE IF EXISTS c_sous_categories_articles;
				CREATE TEMP TABLE c_sous_categories_articles AS
				SELECT
					oid AS from_oid,
					CASE WHEN to_oid <> 0 THEN to_oid ELSE oid + [IPX] END AS to_oid
				FROM t_sous_categories_articles_from;




				-- lieux

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *, 0::bigint AS to_oid
					FROM eco.t_lieux
					WHERE oid <> 0
					', 'temp t_lieux_from'
				);


				UPDATE t_lieux_from
				SET to_oid = t_lieux.oid
				FROM eco.t_lieux
				WHERE upper(t_lieux.code) = upper(t_lieux_from.code) || '[TPX]'  AND
					upper(t_lieux.texte) = upper(t_lieux_from.texte) OR
					t_lieux.oid = t_lieux_from.oid + [IPX];


				INSERT INTO eco.t_lieux
				(
					oid,
					code,
					code_original,
					texte,
					texte_court
				)
				SELECT
					oid + [IPX],
					code || '[TPX]' AS code,
					'[CPX]' || oid AS code_original,
					texte,
					texte_court
				FROM t_lieux_from
				WHERE to_oid = 0;

				DROP TABLE IF EXISTS c_lieux;
				CREATE TEMP TABLE c_lieux AS
				SELECT
					oid AS from_oid,
					CASE WHEN to_oid <> 0 THEN to_oid ELSE oid + [IPX] END AS to_oid
				FROM t_lieux_from;






				-- articles

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *, 0::bigint AS to_oid
					FROM eco.t_articles
					WHERE oid <> 0 AND
						oid IN (SELECT oid FROM eco.p_oids WHERE code_table = ''articles'')
					', 'temp t_articles_from'
				);

				CREATE INDEX i_articles_from_oid ON t_articles_from USING btree (oid);
				CREATE INDEX i_articles_from_code ON t_articles_from USING btree (code);
				CREATE INDEX i_articles_from_famille_id ON t_articles_from USING btree (famille_id);
				CREATE INDEX i_articles_from_sous_famille_id ON t_articles_from USING btree (sous_famille_id);
				CREATE INDEX i_articles_from_categorie_id ON t_articles_from USING btree (categorie_id);
				CREATE INDEX i_articles_from_sous_categorie_id ON t_articles_from USING btree (sous_categorie_id);
				CREATE INDEX i_articles_from_unite_stockage_id  ON t_articles_from USING btree (unite_stockage_id );
				CREATE INDEX i_articles_from_unite_distribution_id ON t_articles_from USING btree (unite_distribution_id);
				CREATE INDEX i_articles_from_classification_atc_id ON t_articles_from USING btree (classification_atc_id);
				CREATE INDEX i_articles_from_compte_id ON t_articles_from USING btree (compte_id);


				UPDATE t_articles_from
				SET to_oid = t_articles.oid
				FROM eco.t_articles
				LEFT JOIN eco.t_providers ON t_providers.oid = 0
				WHERE t_providers.oid IS DISTINCT FROM NULL AND
					upper(t_articles.code) = upper(t_articles_from.code) || '[TPX]'  AND
					upper(translate(t_articles.texte,' ,./:=()','')) = upper(translate(t_articles_from.texte,' ,./:=()','')) AND
					t_articles.oid <  2000000000000 ;

				UPDATE t_articles_from
				SET to_oid = subview.oid
				FROM (
					SELECT upper(translate(t_articles.texte,' ,./:=()','')) AS texte, MIN(oid) AS oid
					FROM eco.t_articles
					WHERE oid <> 0
					GROUP BY 1
				) subview
				LEFT JOIN eco.t_providers ON t_providers.oid = 0
				WHERE t_providers.oid IS DISTINCT FROM NULL AND
					subview.texte = upper(translate(t_articles_from.texte,' ,./:=()',''));

				UPDATE t_articles_from
				SET to_oid = t_articles.oid
				FROM eco.t_articles
				WHERE to_oid = 0 AND t_articles.oid = t_articles_from.oid + [IPX]  ;


				INSERT INTO eco.t_articles
				(
					oid,
					code,
					code_original,
					texte,
					texte_court
				)
				SELECT
					oid + [IPX],
					code || '[TPX]' AS code,
					'[CPX]' || oid AS code_original,
					texte,
					texte_court
				FROM t_articles_from
				WHERE to_oid = 0;

				DROP TABLE IF EXISTS c_articles;
				CREATE TEMP TABLE c_articles AS
				SELECT
					oid AS from_oid,
					CASE WHEN to_oid <> 0 THEN to_oid ELSE oid + [IPX] END AS to_oid
				FROM t_articles_from;

				CREATE INDEX c_articles_i1 ON c_articles USING btree (from_oid);
				CREATE INDEX c_articles_i2 ON c_articles USING btree (to_oid);

				UPDATE eco.t_articles
				SET
					code = t_articles_from.code || '[TPX]',
					texte = t_articles_from.texte,
					texte_court = t_articles_from.texte_court
				FROM t_articles_from
				WHERE t_articles.oid = t_articles_from.to_oid AND
					(t_articles.code IS DISTINCT FROM t_articles_from.code || '[TPX]'
					OR t_articles.texte IS DISTINCT FROM t_articles_from.texte) AND
					t_articles.code LIKE '%[TPX]'
				;

				UPDATE eco.t_articles
				SET famille_id = COALESCE(c_familles_articles.to_oid,0)
				FROM t_articles_from
				JOIN c_articles ON t_articles_from.oid = c_articles.from_oid
				LEFT JOIN c_familles_articles ON famille_id = c_familles_articles.from_oid
				WHERE t_articles.oid = c_articles.to_oid AND
					t_articles.famille_id IS DISTINCT FROM COALESCE(c_familles_articles.to_oid,0) AND
					t_articles.code LIKE '%[TPX]';


				UPDATE eco.t_articles
				SET sous_famille_id = COALESCE(c_sous_familles_articles.to_oid,0)
				FROM t_articles_from
				JOIN c_articles ON t_articles_from.oid = c_articles.from_oid
				LEFT JOIN c_sous_familles_articles ON sous_famille_id = c_sous_familles_articles.from_oid
				WHERE t_articles.oid = c_articles.to_oid AND
					t_articles.sous_famille_id IS DISTINCT FROM COALESCE(c_sous_familles_articles.to_oid,0) AND
					t_articles.code LIKE '%[TPX]';


				UPDATE eco.t_articles
				SET categorie_id = COALESCE(c_categories_articles.to_oid,0)
				FROM t_articles_from
				JOIN c_articles ON t_articles_from.oid = c_articles.from_oid
				LEFT JOIN c_categories_articles ON categorie_id = c_categories_articles.from_oid
				WHERE t_articles.oid = c_articles.to_oid AND
					t_articles.categorie_id IS DISTINCT FROM COALESCE(c_categories_articles.to_oid,0) AND
					t_articles.code LIKE '%[TPX]';;


				UPDATE eco.t_articles
				SET sous_categorie_id = COALESCE(c_sous_categories_articles.to_oid,0)
				FROM t_articles_from
				JOIN c_articles ON t_articles_from.oid = c_articles.from_oid
				LEFT JOIN c_sous_categories_articles ON sous_categorie_id = c_sous_categories_articles.from_oid
				WHERE t_articles.oid = c_articles.to_oid AND
					t_articles.sous_categorie_id IS DISTINCT FROM COALESCE(c_sous_categories_articles.to_oid,0) AND
					t_articles.code LIKE '%[TPX]';;


				UPDATE eco.t_articles
				SET type_id = COALESCE(c_types_articles.to_oid,0)
				FROM t_articles_from
				JOIN c_articles ON t_articles_from.oid = c_articles.from_oid
				LEFT JOIN c_types_articles ON type_id = c_types_articles.from_oid
				WHERE t_articles.oid = c_articles.to_oid AND
					t_articles.type_id IS DISTINCT FROM COALESCE(c_types_articles.to_oid,0) AND
					t_articles.code LIKE '%[TPX]';

				UPDATE eco.t_articles
				SET lpp_id = COALESCE(c_lpp.to_oid,0)
				FROM t_articles_from
				JOIN c_articles ON t_articles_from.oid = c_articles.from_oid
				LEFT JOIN c_lpp ON lpp_id = c_lpp.from_oid
				WHERE t_articles.oid = c_articles.to_oid AND
					t_articles.lpp_id IS DISTINCT FROM COALESCE(c_lpp.to_oid,0) AND
					t_articles.code LIKE '%[TPX]';

				UPDATE eco.t_articles
				SET ucd_id = COALESCE(c_ucd.to_oid,0)
				FROM t_articles_from
				JOIN c_articles ON t_articles_from.oid = c_articles.from_oid
				LEFT JOIN c_ucd ON ucd_id = c_ucd.from_oid
				WHERE t_articles.oid = c_articles.to_oid AND
					t_articles.ucd_id IS DISTINCT FROM COALESCE(c_ucd.to_oid,0) AND
					t_articles.code LIKE '%[TPX]';

				UPDATE eco.t_articles
				SET unite_stockage_id = COALESCE(c_unites.to_oid,0)
				FROM t_articles_from
				JOIN c_articles ON t_articles_from.oid = c_articles.from_oid
				LEFT JOIN c_unites ON unite_stockage_id = c_unites.from_oid
				WHERE t_articles.oid = c_articles.to_oid AND
					t_articles.unite_stockage_id IS DISTINCT FROM COALESCE(c_unites.to_oid,0) AND
					t_articles.code LIKE '%[TPX]';


				UPDATE eco.t_articles
				SET unite_distribution_id = COALESCE(c_unites.to_oid,0)
				FROM t_articles_from
				JOIN c_articles ON t_articles_from.oid = c_articles.from_oid
				LEFT JOIN c_unites ON unite_distribution_id = c_unites.from_oid
				WHERE t_articles.oid = c_articles.to_oid AND
					t_articles.unite_distribution_id IS DISTINCT FROM COALESCE(c_unites.to_oid,0) AND
					t_articles.code LIKE '%[TPX]';


				UPDATE eco.t_articles
				SET classification_atc_id = COALESCE(c_classification_atc.to_oid,0)
				FROM t_articles_from
				JOIN c_articles ON t_articles_from.oid = c_articles.from_oid
				LEFT JOIN c_classification_atc ON classification_atc_id = c_classification_atc.from_oid
				WHERE t_articles.oid = c_articles.to_oid AND
					t_articles.classification_atc_id IS DISTINCT FROM COALESCE(c_classification_atc.to_oid,0) AND
					t_articles.code LIKE '%[TPX]';

				UPDATE eco.t_articles
				SET compte_id = COALESCE(c_compte.to_oid,0)
				FROM t_articles_from
				JOIN c_articles ON t_articles_from.oid = c_articles.from_oid
				LEFT JOIN c_compte ON compte_id = c_compte.from_oid
				WHERE t_articles.oid = c_articles.to_oid AND
					t_articles.compte_id IS DISTINCT FROM COALESCE(c_compte.to_oid,0) AND
					t_articles.code LIKE '%[TPX]';

		]]></sqlcmd>
		</NODE>

		<NODE label="Tables Fournisseurs">
			<sqlcmd><![CDATA[


				-- t_types_fournisseurs

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *, 0::bigint AS to_oid
					FROM eco.t_types_fournisseurs
					WHERE oid <> 0
					', 'temp t_types_fournisseurs_from'
				);

				UPDATE t_types_fournisseurs_from
				SET to_oid = t_types_fournisseurs.oid
				FROM eco.t_types_fournisseurs
				WHERE upper(t_types_fournisseurs.code) = upper(t_types_fournisseurs_from.code) || '[TPX]'  AND
					upper(t_types_fournisseurs.texte) = upper(t_types_fournisseurs_from.texte) OR
					t_types_fournisseurs.oid = t_types_fournisseurs_from.oid + [IPX];


				INSERT INTO eco.t_types_fournisseurs
				(
					oid,
					code,
					code_original,
					texte,
					texte_court
				)
				SELECT
					oid + [IPX],
					code || '[TPX]' AS code,
					'[CPX]' || oid AS code_original,
					texte,
					texte_court
				FROM t_types_fournisseurs_from
				WHERE to_oid = 0;

				DROP TABLE IF EXISTS c_types_fournisseurs;
				CREATE TEMP TABLE c_types_fournisseurs AS
				SELECT
					oid AS from_oid,
					CASE WHEN to_oid <> 0 THEN to_oid ELSE oid + [IPX] END AS to_oid
				FROM t_types_fournisseurs_from;



				-- fournisseurs

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *, 0::bigint AS to_oid
					FROM eco.t_fournisseurs
					WHERE oid <> 0 AND
						oid IN (SELECT oid FROM eco.p_oids WHERE code_table = ''fournisseurs'')
					', 'temp t_fournisseurs_from'
				);


				UPDATE t_fournisseurs_from
				SET to_oid = t_fournisseurs.oid
				FROM eco.t_fournisseurs
				LEFT JOIN eco.t_providers ON t_providers.oid = 0
				WHERE t_providers.oid IS DISTINCT FROM NULL AND
					upper(t_fournisseurs.code) = upper(t_fournisseurs_from.code) || '[TPX]'  AND
					upper(t_fournisseurs.texte) = upper(t_fournisseurs_from.texte);

				UPDATE t_fournisseurs_from
				SET to_oid = t_fournisseurs.oid
				FROM eco.t_fournisseurs
				LEFT JOIN eco.t_providers ON t_providers.oid = 0
				WHERE t_providers.oid IS DISTINCT FROM NULL AND
					upper(t_fournisseurs.code) = upper(t_fournisseurs_from.code) || '[TPX]'  AND
					upper(translate(t_fournisseurs.texte,' ,./:','')) = upper(translate(t_fournisseurs_from.texte,' ,./:',''))  AND
					t_fournisseurs.oid <  2000000000000;



				UPDATE t_fournisseurs_from
				SET to_oid = t_fournisseurs.oid
				FROM eco.t_fournisseurs
				WHERE to_oid = 0 AND
					t_fournisseurs.oid = t_fournisseurs_from.oid + [IPX];


				INSERT INTO eco.t_fournisseurs
				(
					oid,
					code,
					code_original,
					texte,
					texte_court
				)
				SELECT
					oid + [IPX],
					code || '[TPX]' AS code,
					'[CPX]' || oid AS code_original,
					texte,
					texte_court
				FROM t_fournisseurs_from
				WHERE to_oid = 0;

				DROP TABLE IF EXISTS c_fournisseurs;
				CREATE TEMP TABLE c_fournisseurs AS
				SELECT
					oid AS from_oid,
					CASE WHEN to_oid <> 0 THEN to_oid ELSE oid + [IPX] END AS to_oid
				FROM t_fournisseurs_from;

				UPDATE eco.t_fournisseurs SET
					code = t_fournisseurs_from.code || '[TPX]',
					texte = t_fournisseurs_from.texte,
					texte_court = t_fournisseurs_from.texte_court,
					code_original = '#[CPX]' || t_fournisseurs_from.oid
				FROM c_fournisseurs
				JOIN t_fournisseurs_from ON t_fournisseurs_from.oid = c_fournisseurs.from_oid
				WHERE t_fournisseurs.oid = c_fournisseurs.to_oid AND
					(SELECT count(*) FROM eco.t_providers WHERE t_providers.oid = 0) = 0 AND
					(
					t_fournisseurs.code IS DISTINCT FROM t_fournisseurs_from.code OR
					t_fournisseurs.texte IS DISTINCT FROM t_fournisseurs_from.texte OR
					t_fournisseurs.texte_court IS DISTINCT FROM t_fournisseurs_from.texte_court OR
					t_fournisseurs.code_original IS DISTINCT FROM '[CPX]' || t_fournisseurs_from.oid
					)
				;

				DELETE
					FROM eco.t_fournisseurs
					WHERE (SELECT count(*) FROM eco.t_providers WHERE t_providers.oid = 0) = 0 AND
					oid BETWEEN [IPX] AND [IPX] + 900000000000 AND
						(oid - [IPX]) NOT IN (SELECT oid FROM t_fournisseurs_from)
				;


				UPDATE eco.t_fournisseurs SET
					code_original = '[CPX]' || t_fournisseurs_from.oid
				FROM c_fournisseurs
				JOIN t_fournisseurs_from ON t_fournisseurs_from.oid = c_fournisseurs.from_oid
				WHERE (SELECT count(*) FROM eco.t_providers WHERE t_providers.oid = 0) = 0 AND
					t_fournisseurs.oid = c_fournisseurs.to_oid AND
					t_fournisseurs.oid BETWEEN [IPX] AND [IPX] + 900000000000  AND
					(
					t_fournisseurs.code_original IS DISTINCT FROM ('[CPX]' || t_fournisseurs_from.oid)
					)
				;

				UPDATE eco.t_fournisseurs
				SET type_id = COALESCE(c_types_fournisseurs.to_oid,0)
				FROM t_fournisseurs_from
				JOIN c_fournisseurs ON t_fournisseurs_from.oid = c_fournisseurs.from_oid
				LEFT JOIN c_types_fournisseurs ON type_id = c_types_fournisseurs.from_oid
				WHERE t_fournisseurs.oid = c_fournisseurs.to_oid AND
					t_fournisseurs.type_id IS DISTINCT FROM COALESCE(c_types_fournisseurs.to_oid,0) AND
					t_fournisseurs.code LIKE '%[TPX]'
				;

				UPDATE eco.t_articles
				SET
					ref_fournisseur_id = sub.ref_ids,
					ref_fournisseur_code = sub.ref_codes,
					ref_fournisseur_texte = sub.ref_textes
				FROM (
				SELECT
					sub2.to_oid,
					array_agg(c_fournisseurs.to_oid) AS ref_ids,
					array_agg(codes) AS ref_codes,
					array_agg(textes) AS ref_textes
				FROM
					(
					SELECT
						to_oid,
						unnest(ref_fournisseur_id) AS ids,
						unnest(ref_fournisseur_code) AS codes,
						unnest(ref_fournisseur_texte) as textes
					FROM
						t_articles_from
					) sub2
					LEFT JOIN c_fournisseurs ON ids = from_oid
					GROUP BY 1
				) sub
				WHERE t_articles.oid = sub.to_oid AND
					(ref_fournisseur_id IS DISTINCT FROM ref_ids OR
					ref_fournisseur_code IS DISTINCT FROM ref_codes OR
					ref_fournisseur_texte IS DISTINCT FROM ref_textes)
					AND
					t_articles.code LIKE '%[TPX]'
				;
			]]></sqlcmd>
		</NODE>

		<NODE label="Autres tables">
			<sqlcmd><![CDATA[


				-- unites_fonctionnelles

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *, 0::bigint AS to_oid
					FROM eco.t_unites_fonctionnelles
					WHERE oid <> 0
					', 'temp t_unites_fonctionnelles_from'
				);


				UPDATE t_unites_fonctionnelles_from
				SET to_oid = t_unites_fonctionnelles.oid
				FROM eco.t_unites_fonctionnelles
				WHERE upper(t_unites_fonctionnelles.code) = upper(t_unites_fonctionnelles_from.code) || '[TPX]'  AND
					upper(t_unites_fonctionnelles.texte) = upper(t_unites_fonctionnelles_from.texte) OR
					t_unites_fonctionnelles.oid = t_unites_fonctionnelles_from.oid + [IPX];


				INSERT INTO eco.t_unites_fonctionnelles
				(
					oid,
					code,
					code_original,
					texte,
					texte_court
				)
				SELECT
					oid + [IPX],
					code || '[TPX]' AS code,
					'[CPX]' || oid AS code_original,
					texte,
					texte_court
				FROM t_unites_fonctionnelles_from
				WHERE to_oid = 0;

				DROP TABLE IF EXISTS c_unites_fonctionnelles;
				CREATE TEMP TABLE c_unites_fonctionnelles AS
				SELECT
					oid AS from_oid,
					CASE WHEN to_oid <> 0 THEN to_oid ELSE oid + [IPX] END AS to_oid
				FROM t_unites_fonctionnelles_from;





				-- types_mouvements

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *, 0::bigint AS to_oid
					FROM eco.t_types_mouvements
					WHERE oid <> 0
					', 'temp t_types_mouvements_from'
				);


				UPDATE t_types_mouvements_from
				SET to_oid = t_types_mouvements.oid
				FROM eco.t_types_mouvements
				WHERE upper(t_types_mouvements.code) = upper(t_types_mouvements_from.code) || '[TPX]'  AND
					upper(t_types_mouvements.texte) = upper(t_types_mouvements_from.texte) OR
					t_types_mouvements.oid = t_types_mouvements_from.oid + [IPX];


				INSERT INTO eco.t_types_mouvements
				(
					oid,
					code,
					code_original,
					texte,
					texte_court
				)
				SELECT
					oid + [IPX],
					code || '[TPX]' AS code,
					'[CPX]' || oid AS code_original,
					texte,
					texte_court
				FROM t_types_mouvements_from
				WHERE to_oid = 0;

				DROP TABLE IF EXISTS c_types_mouvements;
				CREATE TEMP TABLE c_types_mouvements AS
				SELECT
					oid AS from_oid,
					CASE WHEN to_oid <> 0 THEN to_oid ELSE oid + [IPX] END AS to_oid
				FROM t_types_mouvements_from;




				-- types_mouvements

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *, 0::bigint AS to_oid
					FROM eco.t_types_mouvements
					WHERE oid <> 0
					', 'temp t_types_mouvements_from'
				);


				UPDATE t_types_mouvements_from
				SET to_oid = t_types_mouvements.oid
				FROM eco.t_types_mouvements
				WHERE upper(t_types_mouvements.code) = upper(t_types_mouvements_from.code) || '[TPX]'  AND
					upper(t_types_mouvements.texte) = upper(t_types_mouvements_from.texte) OR
					t_types_mouvements.oid = t_types_mouvements_from.oid + [IPX];


				INSERT INTO eco.t_types_mouvements
				(
					oid,
					code,
					code_original,
					texte,
					texte_court
				)
				SELECT
					oid + [IPX],
					code || '[TPX]' AS code,
					'[CPX]' || oid AS code_original,
					texte,
					texte_court
				FROM t_types_mouvements_from
				WHERE to_oid = 0;

				DROP TABLE IF EXISTS c_types_mouvements;
				CREATE TEMP TABLE c_types_mouvements AS
				SELECT
					oid AS from_oid,
					CASE WHEN to_oid <> 0 THEN to_oid ELSE oid + [IPX] END AS to_oid
				FROM t_types_mouvements_from;






				-- gestionnaires

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *, 0::bigint AS to_oid
					FROM eco.t_gestionnaires
					WHERE oid <> 0
					', 'temp t_gestionnaires_from'
				);


				UPDATE t_gestionnaires_from
				SET to_oid = t_gestionnaires.oid
				FROM eco.t_gestionnaires
				WHERE upper(t_gestionnaires.code) = upper(t_gestionnaires_from.code) || '[TPX]'  AND
					upper(t_gestionnaires.texte) = upper(t_gestionnaires_from.texte) OR
					t_gestionnaires.oid = t_gestionnaires_from.oid + [IPX];


				INSERT INTO eco.t_gestionnaires
				(
					oid,
					code,
					code_original,
					texte,
					texte_court
				)
				SELECT
					oid + [IPX],
					code || '[TPX]' AS code,
					'[CPX]' || oid AS code_original,
					texte,
					texte_court
				FROM t_gestionnaires_from
				WHERE to_oid = 0;

				DROP TABLE IF EXISTS c_gestionnaires;
				CREATE TEMP TABLE c_gestionnaires AS
				SELECT
					oid AS from_oid,
					CASE WHEN to_oid <> 0 THEN to_oid ELSE oid + [IPX] END AS to_oid
				FROM t_gestionnaires_from;






				-- centres_responsabilites

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *, 0::bigint AS to_oid
					FROM eco.t_centres_responsabilites
					WHERE oid <> 0
					', 'temp t_centres_responsabilites_from'
				);


				UPDATE t_centres_responsabilites_from
				SET to_oid = t_centres_responsabilites.oid
				FROM eco.t_centres_responsabilites
				WHERE upper(t_centres_responsabilites.code) = upper(t_centres_responsabilites_from.code) || '[TPX]'  AND
					upper(t_centres_responsabilites.texte) = upper(t_centres_responsabilites_from.texte) OR
					t_centres_responsabilites.oid = t_centres_responsabilites_from.oid + [IPX];


				INSERT INTO eco.t_centres_responsabilites
				(
					oid,
					code,
					code_original,
					texte,
					texte_court
				)
				SELECT
					oid + [IPX],
					code || '[TPX]' AS code,
					'[CPX]' || oid AS code_original,
					texte,
					texte_court
				FROM t_centres_responsabilites_from
				WHERE to_oid = 0;

				DROP TABLE IF EXISTS c_centres_responsabilites;
				CREATE TEMP TABLE c_centres_responsabilites AS
				SELECT
					oid AS from_oid,
					CASE WHEN to_oid <> 0 THEN to_oid ELSE oid + [IPX] END AS to_oid
				FROM t_centres_responsabilites_from;






				-- profils comptables

				SELECT base.cti_copy_table('dbname=[DBNAME] host=[DBHOST] port=[DBPORT] user=[DBUSERNAME] password=[DBPASSWORD]', '
					SELECT *
					FROM eco.t_profils_comptables
					WHERE oid <> 0
					', 'temp t_profils_comptables'
				);

				INSERT INTO eco.t_profils_comptables (
					oid,
					code_original,
					centre_responsabilite_id,
					unite_fonctionnelle_id
					)
				SELECT
					oid + [IPX] AS oid,
					'[CPX]' || code_original AS code_original,
					COALESCE(c_centres_responsabilites.to_oid,0) AS centre_responsabilite_id,
					COALESCE(c_unites_fonctionnelles.to_oid,0) AS unite_fonctionnelle_id
				FROM t_profils_comptables
				LEFT JOIN c_centres_responsabilites ON centre_responsabilite_id = c_centres_responsabilites.from_oid
				LEFT JOIN c_unites_fonctionnelles ON unite_fonctionnelle_id = c_unites_fonctionnelles.from_oid;


			]]></sqlcmd>
		</NODE>

		<NODE label="Stockage translations">
			<sqlcmd><![CDATA[

				INSERT INTO eco.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT
					'[DBNAME]'::text AS from_database, 'FAA'::text AS table_code, corr_table.from_oid, COALESCE(from_table.code,'') AS from_code, corr_table.to_oid, COALESCE(to_table.code,'') AS to_code
				FROM c_familles_articles corr_table
				LEFT JOIN t_familles_articles_from from_table ON corr_table.from_oid = from_table.oid
				LEFT JOIN eco.t_familles_articles to_table ON corr_table.to_oid = to_table.oid
				;

				INSERT INTO eco.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT
					'[DBNAME]'::text AS from_database, 'SFA'::text AS table_code, corr_table.from_oid, COALESCE(from_table.code,'') AS from_code, corr_table.to_oid, COALESCE(to_table.code,'') AS to_code
				FROM c_sous_familles_articles corr_table
				LEFT JOIN t_sous_familles_articles_from from_table ON corr_table.from_oid = from_table.oid
				LEFT JOIN eco.t_sous_familles_articles to_table ON corr_table.to_oid = to_table.oid
				;

				INSERT INTO eco.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT
					'[DBNAME]'::text AS from_database, 'CAA'::text AS table_code, corr_table.from_oid, COALESCE(from_table.code,'') AS from_code, corr_table.to_oid, COALESCE(to_table.code,'') AS to_code
				FROM c_categories_articles corr_table
				LEFT JOIN t_categories_articles_from from_table ON corr_table.from_oid = from_table.oid
				LEFT JOIN eco.t_categories_articles to_table ON corr_table.to_oid = to_table.oid
				;

				INSERT INTO eco.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT
					'[DBNAME]'::text AS from_database, 'SCA'::text AS table_code, corr_table.from_oid, COALESCE(from_table.code,'') AS from_code, corr_table.to_oid, COALESCE(to_table.code,'') AS to_code
				FROM c_sous_categories_articles corr_table
				LEFT JOIN t_sous_categories_articles_from from_table ON corr_table.from_oid = from_table.oid
				LEFT JOIN eco.t_sous_categories_articles to_table ON corr_table.to_oid = to_table.oid
				;

				INSERT INTO eco.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT
					'[DBNAME]'::text AS from_database, 'TYA'::text AS table_code, corr_table.from_oid, COALESCE(from_table.code,'') AS from_code, corr_table.to_oid, COALESCE(to_table.code,'') AS to_code
				FROM c_types_articles corr_table
				LEFT JOIN t_types_articles_from from_table ON corr_table.from_oid = from_table.oid
				LEFT JOIN eco.t_types_articles to_table ON corr_table.to_oid = to_table.oid
				;


				INSERT INTO eco.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT
					'[DBNAME]'::text AS from_database, 'ART'::text AS table_code, corr_table.from_oid, COALESCE(from_table.code,'') AS from_code, corr_table.to_oid, COALESCE(to_table.code,'') AS to_code
				FROM c_articles corr_table
				LEFT JOIN t_articles_from from_table ON corr_table.from_oid = from_table.oid
				LEFT JOIN eco.t_articles to_table ON corr_table.to_oid = to_table.oid
				;


				INSERT INTO eco.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT
					'[DBNAME]'::text AS from_database, 'TYF'::text AS table_code, corr_table.from_oid, COALESCE(from_table.code,'') AS from_code, corr_table.to_oid, COALESCE(to_table.code,'') AS to_code
				FROM c_types_fournisseurs corr_table
				LEFT JOIN t_types_fournisseurs_from from_table ON corr_table.from_oid = from_table.oid
				LEFT JOIN eco.t_types_fournisseurs to_table ON corr_table.to_oid = to_table.oid
				;


				INSERT INTO eco.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT
					'[DBNAME]'::text AS from_database, 'FOU'::text AS table_code, corr_table.from_oid, COALESCE(from_table.code,'') AS from_code, corr_table.to_oid, COALESCE(to_table.code,'') AS to_code
				FROM c_fournisseurs corr_table
				LEFT JOIN t_fournisseurs_from from_table ON corr_table.from_oid = from_table.oid
				LEFT JOIN eco.t_fournisseurs to_table ON corr_table.to_oid = to_table.oid
				;



				INSERT INTO eco.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT
					'[DBNAME]'::text AS from_database, 'TYM'::text AS table_code, corr_table.from_oid, COALESCE(from_table.code,'') AS from_code, corr_table.to_oid, COALESCE(to_table.code,'') AS to_code
				FROM c_types_mouvements corr_table
				LEFT JOIN t_types_mouvements_from from_table ON corr_table.from_oid = from_table.oid
				LEFT JOIN eco.t_types_mouvements to_table ON corr_table.to_oid = to_table.oid
				;


				INSERT INTO eco.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT
					'[DBNAME]'::text AS from_database, 'GES'::text AS table_code, corr_table.from_oid, COALESCE(from_table.code,'') AS from_code, corr_table.to_oid, COALESCE(to_table.code,'') AS to_code
				FROM c_gestionnaires corr_table
				LEFT JOIN t_gestionnaires_from from_table ON corr_table.from_oid = from_table.oid
				LEFT JOIN eco.t_gestionnaires to_table ON corr_table.to_oid = to_table.oid
				;

				INSERT INTO eco.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT
					'[DBNAME]'::text AS from_database, 'LIE'::text AS table_code, corr_table.from_oid, COALESCE(from_table.code,'') AS from_code, corr_table.to_oid, COALESCE(to_table.code,'') AS to_code
				FROM c_lieux corr_table
				LEFT JOIN t_lieux_from from_table ON corr_table.from_oid = from_table.oid
				LEFT JOIN eco.t_lieux to_table ON corr_table.to_oid = to_table.oid
				;

				INSERT INTO eco.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT
					'[DBNAME]'::text AS from_database, 'UNI'::text AS table_code, corr_table.from_oid, COALESCE(from_table.code,'') AS from_code, corr_table.to_oid, COALESCE(to_table.code,'') AS to_code
				FROM c_unites corr_table
				LEFT JOIN t_unites_from from_table ON corr_table.from_oid = from_table.oid
				LEFT JOIN eco.t_unites to_table ON corr_table.to_oid = to_table.oid
				;

				INSERT INTO eco.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT
					'[DBNAME]'::text AS from_database, 'UFO'::text AS table_code, corr_table.from_oid, COALESCE(from_table.code,'') AS from_code, corr_table.to_oid, COALESCE(to_table.code,'') AS to_code
				FROM c_unites_fonctionnelles corr_table
				LEFT JOIN t_unites_fonctionnelles_from from_table ON corr_table.from_oid = from_table.oid
				LEFT JOIN eco.t_unites_fonctionnelles to_table ON corr_table.to_oid = to_table.oid
				;

				INSERT INTO eco.p_consolidation_translation (from_database, table_code, from_id, from_code, to_id, to_code)
				SELECT
					'[DBNAME]'::text AS from_database, 'CRE'::text AS table_code, corr_table.from_oid, COALESCE(from_table.code,'') AS from_code, corr_table.to_oid, COALESCE(to_table.code,'') AS to_code
				FROM c_centres_responsabilites corr_table
				LEFT JOIN t_centres_responsabilites_from from_table ON corr_table.from_oid = from_table.oid
				LEFT JOIN eco.t_centres_responsabilites to_table ON corr_table.to_oid = to_table.oid
				;



			]]></sqlcmd>
		</NODE>

	</NODE>

	<NODE name="POST" label="TRAITEMENTS COMPLÉMENTAIRES">

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

				TRUNCATE eco.p_oids;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'articles', article_id
				FROM eco.p_mouvements_articles
				WHERE article_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'articles')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'articles', article_id
				FROM eco.p_lignes_commandes
				WHERE article_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'articles')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'categories_articles', categorie_id
				FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE categorie_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'categories_articles')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'categories_articles', categorie_id
				FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE categorie_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'categories_articles')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'sous_categories_articles', sous_categorie_id
				FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE sous_categorie_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'sous_categories_articles')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'sous_categories_articles', sous_categorie_id
				FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE sous_categorie_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'sous_categories_articles')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'classification_atc', classification_atc_id
				FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE classification_atc_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'classification_atc')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'classification_atc', classification_atc_id
				FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE classification_atc_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'classification_atc')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'familles_articles', famille_id
				FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE famille_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'familles_articles')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'familles_articles', famille_id
				FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE famille_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'familles_articles')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'sous_familles_articles', sous_famille_id
				FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE sous_famille_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'sous_familles_articles')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'sous_familles_articles', sous_famille_id
				FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE sous_famille_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'sous_familles_articles')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'compte', t_articles.compte_id
				FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE t_articles.compte_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'compte')
				GROUP BY 1,2;


				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'compte', t_articles.compte_id
				FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE t_articles.compte_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'compte')
				GROUP BY 1,2;


				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'compte', t_articles.compte_id
				FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE t_articles.compte_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'compte')
				GROUP BY 1,2;


				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'compte', compte_id
				FROM eco.p_lignes_facture
				WHERE compte_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'compte')
				GROUP BY 1,2;


				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'types_articles', t_articles.type_id
				FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE t_articles.type_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'types_articles')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'unites_stockage_articles', t_articles.unite_stockage_id
				FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE t_articles.unite_stockage_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'unites_stockage_articles')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'unites_stockage_articles', t_articles.unite_stockage_id
				FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE t_articles.unite_stockage_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'unites_stockage_articles')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'gestionnaires', gestionnaire_id
				FROM eco.p_mouvements_articles
				WHERE gestionnaire_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'gestionnaires')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'gestionnaires', gestionnaire_id
				FROM eco.p_commandes
				WHERE gestionnaire_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'gestionnaires')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'centres_responsabilites', centre_responsabilite_id
				FROM eco.t_profils_comptables
				WHERE centre_responsabilite_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'centres_responsabilites')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'unites_fonctionnelles', unite_fonctionnelle_id
				FROM eco.t_profils_comptables
				WHERE unite_fonctionnelle_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'unites_fonctionnelles')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'unites_fonctionnelles', unite_fonctionnelle_id
				FROM eco.p_commandes
				WHERE unite_fonctionnelle_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'unites_fonctionnelles')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'unites_fonctionnelles', unite_fonctionnelle_id
				FROM eco.p_mouvements_articles
				WHERE unite_fonctionnelle_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'unites_fonctionnelles')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'fournisseurs', fournisseur_id
				FROM eco.p_mouvements_articles
				WHERE fournisseur_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'fournisseurs')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'fournisseurs', fournisseur_id
				FROM eco.p_commandes
				WHERE fournisseur_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'fournisseurs')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'types_fournisseurs', t_fournisseurs.type_id
				FROM eco.p_mouvements_articles JOIN eco.t_fournisseurs ON fournisseur_id = t_fournisseurs.oid
				WHERE t_fournisseurs.type_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'types_fournisseurs')
				GROUP BY 1,2;


				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'types_fournisseurs', t_fournisseurs.type_id
				FROM eco.p_commandes JOIN eco.t_fournisseurs ON fournisseur_id = t_fournisseurs.oid
				WHERE t_fournisseurs.type_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'types_fournisseurs')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'lieux_mouvements', lieu_id
				FROM eco.p_mouvements_articles
				WHERE lieu_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'lieux_mouvements')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'lieux_commandes', lieu_livraison_id
				FROM eco.p_commandes
				WHERE lieu_livraison_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'lieux_commandes')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'types_mouvements', type_mouvement_id
				FROM eco.p_mouvements_articles
				WHERE type_mouvement_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'types_mouvements')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'ucd', ucd_id
				FROM eco.p_mouvements_articles
				WHERE ucd_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'ucd')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid)
				SELECT 'lpp', lpp_id
				FROM eco.p_mouvements_articles
				WHERE lpp_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'lpp')
				GROUP BY 1,2;


			]]></sqlcmd>
		</NODE>

		<NODE label="Réplication données">
			<sqlcmd><![CDATA[

				TRUNCATE eco.t_profils;

				INSERT INTO eco.t_profils (categorie_article_id, article_id, classification_atc_id, compte_id, famille_article_id, sous_famille_article_id, sous_categorie_article_id, type_article_id, fournisseur_id, lieu_id, gestionnaire_id, unite_fonctionnelle_id)
				SELECT categorie_article_id, article_id, classification_atc_id, compte_article_id, famille_article_id, sous_famille_article_id, sous_categorie_article_id, type_article_id, fournisseur_id, lieu_id, gestionnaire_id, unite_fonctionnelle_id
				FROM eco.v_mouvements_articles_1
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12
				UNION
				SELECT categorie_article_id, article_id, classification_atc_id, compte_article_id, famille_article_id, sous_famille_article_id, sous_categorie_article_id, type_article_id, fournisseur_id, lieu_id, gestionnaire_id, unite_fonctionnelle_id
				FROM eco.v_lignes_commandes_1
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12;


				SELECT eco.cti_reorganize_ucd_lpp_c();

				SELECT eco.cti_gen_referentiel_cti();

				SELECT eco.cti_update_schema_classes('*ALL');


			]]></sqlcmd>
		</NODE>

		<NODE label="Mise à jour des dates">
			<sqlcmd><![CDATA[

                -- Initialisation mois en cours/mois clôturé (NOW/NOWCLOTURE)
                SELECT eco.cti_now('NOW','FORCE');
                SELECT eco.cti_now('NOWCLOTURE','FORCE');

            ]]></sqlcmd>
		</NODE>

	</NODE>

	<NODE name="DISABLE_INDEX" label="DÉSACTIVATION INDEX">

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

				SELECT base.cti_disable_index('eco', 'i_commandes_1');
				SELECT base.cti_disable_index('eco', 'i_commandes_2');
				SELECT base.cti_disable_index('eco', 'i_commandes_3');
				SELECT base.cti_disable_index('eco', 'i_commandes_4');
				SELECT base.cti_disable_index('eco', 'i_commandes_5');
				SELECT base.cti_disable_index('eco', 'i_commandes_6');
				SELECT base.cti_disable_index('eco', 'i_commandes_7');
				SELECT base.cti_disable_index('eco', 'i_commandes_8');

				SELECT base.cti_disable_index('eco', 'i_ligne_commandes_1');
				SELECT base.cti_disable_index('eco', 'i_ligne_commandes_2');
				SELECT base.cti_disable_index('eco', 'i_ligne_commandes_3');

			]]></sqlcmd>
		</NODE>

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

				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_1');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_2');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_3');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_4');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_5');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_6');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_7');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_8');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_9');

			]]></sqlcmd>
		</NODE>

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

				SELECT base.cti_disable_index('eco', 'i_facture_1');
				SELECT base.cti_disable_index('eco', 'i_facture_2');
				SELECT base.cti_disable_index('eco', 'i_facture_3');
				SELECT base.cti_disable_index('eco', 'i_facture_4');
				SELECT base.cti_disable_index('eco', 'i_facture_5');

				SELECT base.cti_disable_index('eco', 'i_ligne_facture_1');
				SELECT base.cti_disable_index('eco', 'i_ligne_facture_2');
				SELECT base.cti_disable_index('eco', 'i_ligne_facture_3');
				SELECT base.cti_disable_index('eco', 'i_ligne_facture_4');

			]]></sqlcmd>
		</NODE>

	</NODE>

	<NODE name="ENABLE_INDEX" label="RÉACTIVATION INDEX">

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

				SELECT base.cti_enable_index('eco', 'i_commandes_1');
				SELECT base.cti_enable_index('eco', 'i_commandes_2');
				SELECT base.cti_enable_index('eco', 'i_commandes_3');
				SELECT base.cti_enable_index('eco', 'i_commandes_4');
				SELECT base.cti_enable_index('eco', 'i_commandes_5');
				SELECT base.cti_enable_index('eco', 'i_commandes_6');
				SELECT base.cti_enable_index('eco', 'i_commandes_7');
				SELECT base.cti_enable_index('eco', 'i_commandes_8');

				SELECT base.cti_enable_index('eco', 'i_ligne_commandes_1');
				SELECT base.cti_enable_index('eco', 'i_ligne_commandes_2');
				SELECT base.cti_enable_index('eco', 'i_ligne_commandes_3');

			]]></sqlcmd>
		</NODE>

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

				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_1');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_2');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_3');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_4');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_5');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_6');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_7');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_8');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_9');

			]]></sqlcmd>
		</NODE>

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

				SELECT base.cti_enable_index('eco', 'i_facture_1');
				SELECT base.cti_enable_index('eco', 'i_facture_2');
				SELECT base.cti_enable_index('eco', 'i_facture_3');
				SELECT base.cti_enable_index('eco', 'i_facture_4');
				SELECT base.cti_enable_index('eco', 'i_facture_5');

				SELECT base.cti_enable_index('eco', 'i_ligne_facture_1');
				SELECT base.cti_enable_index('eco', 'i_ligne_facture_2');
				SELECT base.cti_enable_index('eco', 'i_ligne_facture_3');
				SELECT base.cti_enable_index('eco', 'i_ligne_facture_4');

			]]></sqlcmd>
		</NODE>

	</NODE>

	<NODE name="RAZ" label="RAZ BASE">

		<NODE label="Nettoyage des tables">
			<sqlcmd><![CDATA[

				TRUNCATE eco.p_commandes;
				TRUNCATE eco.p_lignes_commandes;
				TRUNCATE eco.p_mouvements_articles;
				TRUNCATE eco.p_facture;
				TRUNCATE eco.p_lignes_facture;
				TRUNCATE eco.t_profils_comptables;
				TRUNCATE eco.p_stock;

				TRUNCATE eco.p_consolidation_translation;

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

</ROOT>