<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	
	
	<NODE name="INIT" label="INITIALISATIONS">
	
			
		<NODE label="Tables Articles">
			
			<sqlcmd><![CDATA[
			
				-- Unites de stockage
				INSERT INTO eco.t_unites(code_original, code, texte, texte_court)
				SELECT
					IDUNITE,
					UNITE_PRISE,
					LIBELLE_UNITE,
					LIBELLE_UNITE
				FROM
					prod_evolucare_hygie.UNITE
				WHERE IDUNITE <> 0 AND
					IDUNITE::text NOT IN (SELECT code_original FROM eco.t_unites WHERE code_original IS NOT NULL);
					
				-- LPP
				
				
				-- UCD
				INSERT INTO base.t_ucd (code, texte, texte_court, laboratoire_texte, conditionnement_texte)
				SELECT CODE_UCD_7, CODE_UCD_7, CODE_UCD_7, '', ''
				FROM prod_evolucare_hygie.ARTICLES
				WHERE CODE_UCD_7 <> '' AND
					CODE_UCD_7 NOT IN (SELECT code FROM base.t_ucd WHERE code IS NOT NULL)
				GROUP BY 1;

				
				-- ATC
				INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
				SELECT UPPER(CODE_ATC), UPPER(CODE_ATC), MAX(LIBELLE_ATC), MAX(LIBELLE_ATC) 
				FROM prod_evolucare_hygie.ATC
				LEFT JOIN eco.t_classification_atc ON code_original = UPPER(CODE_ATC)
				WHERE code_original IS NULL 
				GROUP BY 1
				ORDER BY 1;

				
				
				-- Standard ATC
				SELECT eco.cti_reorganize_classification_atc();

				-- Comptes
				INSERT INTO eco.t_compte(code_original, code, texte, texte_court)
				SELECT 
					CPTE_ACHAT_FAMI_COMPTA code_original,
					CPTE_ACHAT_FAMI_COMPTA AS code, 
					MAX(LIB_FAMI_COMPTA) AS texte, 
					MAX(LIB_FAMI_COMPTA) AS texte_court
						
				FROM prod_evolucare_hygie.FAMILLES_COMPTA
				WHERE length(CPTE_ACHAT_FAMI_COMPTA) > 5 AND
					CPTE_ACHAT_FAMI_COMPTA NOT IN (SELECT code_original FROM eco.t_compte WHERE code_original IS NOT NULL) AND
					CODE_FAMI_COMPTA IN (SELECT CODE_FAMI_COMPTA FROM prod_evolucare_hygie.ARTICLES)
				GROUP BY 1
				ORDER BY 1;
				
					
				-- Types d'articles
				INSERT INTO eco.t_types_articles(code_original, code, texte, texte_court)
				SELECT NOMENCLATURE, NOMENCLATURE,NOMENCLATURE, NOMENCLATURE
				FROM prod_evolucare_hygie.ARTICLES
				WHERE NOMENCLATURE NOT IN (SELECT code_original FROM eco.t_types_articles WHERE code_original IS NOT NULL)
				GROUP BY 1,2,3,4
				ORDER BY 1;
					
				
				-- Familles d'articles
				INSERT INTO eco.t_familles_articles(code_original, code, texte, texte_court)
				SELECT COD_FAMILLE_ART, COD_FAMILLE_ART, 
                    MAX(CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END), 
                    MAX(CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END)
				FROM prod_evolucare_hygie.FAMILLE_ARTICLES
				WHERE COD_FAMILLE_ART <> 0 AND
					COD_FAMILLE_ART NOT IN (SELECT code_original FROM eco.t_familles_articles WHERE code_original IS NOT NULL)
				GROUP BY 1,2
				ORDER BY 1;

				UPDATE eco.t_familles_articles
				SET texte = CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END, 
                    texte_court = CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END
				FROM prod_evolucare_hygie.FAMILLE_ARTICLES
				WHERE COD_FAMILLE_ART = code_original AND
					(texte IS DISTINCT FROM CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END OR 
					texte_court IS DISTINCT FROM CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END);

				
					
				-- Sous familles
				INSERT INTO eco.t_sous_familles_articles(code_original, code, texte, texte_court)
				SELECT COD_FAMILLE_ART, COD_FAMILLE_ART, 
                    MAX(CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END), 
                    MAX(CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END)
				FROM prod_evolucare_hygie.FAMILLE_ARTICLES
				WHERE COD_FAMILLE_ART <> 0 AND
					LIB_FAMILLE_ART <> '' AND
					COD_FAMILLE_ART NOT IN (SELECT code_original FROM eco.t_sous_familles_articles WHERE code_original IS NOT NULL)
				GROUP BY 1,2
				ORDER BY 1;

				UPDATE eco.t_sous_familles_articles
				SET texte = CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END, 
                    texte_court = CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END
				FROM prod_evolucare_hygie.FAMILLE_ARTICLES
				WHERE COD_FAMILLE_ART = code_original AND
					(texte IS DISTINCT FROM CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END OR 
					texte_court IS DISTINCT FROM CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END);
				
				
					
				-- Catégories
				INSERT INTO eco.t_categories_articles(code_original, code, texte, texte_court)
				SELECT IDTYPE_PRODUIT, CODE_TYPE_PROD,LIBELLLE_TYPE_PROD, LEFT(LIBELLLE_TYPE_PROD,50)
				FROM prod_evolucare_hygie.TYPE_PRODUIT
				WHERE IDTYPE_PRODUIT NOT IN (SELECT code_original FROM eco.t_categories_articles WHERE code_original IS NOT NULL)
				GROUP BY 1,2,3,4
				ORDER BY 1;
				
					
				-- Sous catégories
				

				
				-- Articles
				INSERT INTO eco.t_articles(code_original, code, texte, texte_court)
				SELECT REF_ART_INT, 
					REF_ART_INT, 
					DESIGN_USUELLE, 
					substr(DESIGN_USUELLE,1,50)
				FROM prod_evolucare_hygie.ARTICLES
					LEFT JOIN eco.t_articles ON (code_original = REF_ART_INT)
				WHERE code_original IS NULL;


				UPDATE eco.t_articles
				SET 
					texte = DESIGN_USUELLE,
					texte_court = substr(DESIGN_USUELLE,1,50)
				FROM prod_evolucare_hygie.ARTICLES
				WHERE code_original = REF_ART_INT
				AND (
					texte IS DISTINCT FROM DESIGN_USUELLE OR
					texte_court IS DISTINCT FROM substr(DESIGN_USUELLE,1,50))
				;




				UPDATE eco.t_articles SET
					type_id = COALESCE(t_types_articles.oid,0),
					famille_id = COALESCE(t_familles_articles.oid,0),
					sous_famille_id = COALESCE(t_sous_familles_articles.oid,0),
					categorie_id = COALESCE(t_categories_articles.oid,0),
					sous_categorie_id = COALESCE(t_sous_categories_articles.oid,0),
					classification_atc_id = COALESCE(t_classification_atc.oid,0),
					unite_stockage_id = COALESCE(t_unites_stockage.oid,0),
					unite_distribution_id = COALESCE(t_unites_distribution.oid,0),
					lpp_id = COALESCE(t_lpp.oid,0),
					ucd_id = COALESCE(t_ucd.oid,0)
				FROM prod_evolucare_hygie.ARTICLES
					LEFT JOIN prod_evolucare_hygie.FAMILLE_ARTICLES ON ARTICLES.COD_FAMILLE_ART::text = FAMILLE_ARTICLES.COD_FAMILLE_ART 
					LEFT JOIN eco.t_familles_articles ON CASE WHEN ARTICLES.SOUS_FAMILLE <> '' THEN ARTICLES.SOUS_FAMILLE ELSE FAMILLE_ARTICLES.COD_FAMILLE_ART::text END = t_familles_articles.code_original
					LEFT JOIN eco.t_sous_familles_articles ON FAMILLE_ARTICLES.COD_FAMILLE_ART = t_sous_familles_articles.code_original
					LEFT JOIN eco.t_types_articles ON NOMENCLATURE = upper(t_types_articles.code_original)
					LEFT JOIN eco.t_classification_atc ON CODE_ATC = upper(t_classification_atc.code_original)
					LEFT JOIN eco.t_categories_articles ON TYPE_DE_PRODUIT = t_categories_articles.code_original
					LEFT JOIN eco.t_sous_categories_articles ON t_sous_categories_articles.oid = 0
					LEFT JOIN eco.t_unites t_unites_stockage ON t_unites_stockage.oid = 0
					LEFT JOIN eco.t_unites t_unites_distribution ON t_unites_distribution.oid = 0
					LEFT JOIN base.t_lpp ON t_lpp.oid = 0
					LEFT JOIN base.t_ucd ON CODE_UCD_7 = t_ucd.code
				WHERE 
					t_articles.code_original = REF_ART_INT AND
					(t_articles.type_id IS DISTINCT FROM COALESCE(t_types_articles.oid,0) OR
					t_articles.famille_id IS DISTINCT FROM COALESCE(t_familles_articles.oid,0) OR
					t_articles.sous_famille_id IS DISTINCT FROM COALESCE(t_sous_familles_articles.oid,0) OR
					t_articles.categorie_id IS DISTINCT FROM COALESCE(t_categories_articles.oid,0) OR
					t_articles.sous_categorie_id IS DISTINCT FROM COALESCE(t_sous_categories_articles.oid,0) OR
					t_articles.classification_atc_id IS DISTINCT FROM COALESCE(t_classification_atc.oid,0) OR
					t_articles.unite_stockage_id IS DISTINCT FROM COALESCE(t_unites_stockage.oid,0) OR
					t_articles.unite_distribution_id IS DISTINCT FROM COALESCE(t_unites_distribution.oid,0) OR
					lpp_id IS DISTINCT FROM COALESCE(t_lpp.oid,0) OR
					ucd_id IS DISTINCT FROM COALESCE(t_ucd.oid,0)
					);


		
				UPDATE eco.t_articles SET
					compte_id = COALESCE(t_compte.oid,0)
				FROM prod_evolucare_hygie.ARTICLES
				LEFT JOIN prod_evolucare_hygie.FAMILLES_COMPTA ON ARTICLES.CODE_FAMI_COMPTA = FAMILLES_COMPTA.CODE_FAMI_COMPTA
					LEFT JOIN eco.t_compte ON CPTE_ACHAT_FAMI_COMPTA = t_compte.code_original
				WHERE 
					t_articles.code_original = REF_ART_INT AND
					(
					t_articles.compte_id IS DISTINCT FROM COALESCE(t_compte.oid,0)
					);

				UPDATE eco.t_articles SET compte_id = 0 WHERE compte_id IS NULL;

                
                
                UPDATE eco.t_articles
                SET gere_en_stock = '1'
                FROM prod_evolucare_hygie.ARTICLES
                WHERE REF_ART_INT = code_original AND
                    gere_en_stock != '1'
                ;

				UPDATE eco.t_articles SET
					type_code = t_types_articles.code,
					type_texte = t_types_articles.texte_court,
					type_section_id = t_types_articles.section_id,
					type_section_code = t_types_articles.section_code,
					type_section_texte = t_types_articles.section_texte,
					
					famille_code = t_familles_articles.code,
					famille_texte = t_familles_articles.texte_court,
					famille_section_id = t_familles_articles.section_id,
					famille_section_code = t_familles_articles.section_code,
					famille_section_texte = t_familles_articles.section_texte,

					sous_famille_code = t_sous_familles_articles.code,
					sous_famille_texte = t_sous_familles_articles.texte_court,
					sous_famille_section_id = t_sous_familles_articles.section_id,
					sous_famille_section_code = t_sous_familles_articles.section_code,
					sous_famille_section_texte = t_sous_familles_articles.section_texte,

					categorie_code = t_categories_articles.code,
					categorie_texte = t_categories_articles.texte_court,
					categorie_section_id = t_categories_articles.section_id,
					categorie_section_code = t_categories_articles.section_code,
					categorie_section_texte = t_categories_articles.section_texte,

					sous_categorie_code = t_sous_categories_articles.code,
					sous_categorie_texte = t_sous_categories_articles.texte_court,
					sous_categorie_section_id = t_sous_categories_articles.section_id,
					sous_categorie_section_code = t_sous_categories_articles.section_code,
					sous_categorie_section_texte = t_sous_categories_articles.section_texte,

					classification_atc_code = t_classification_atc.code,
					classification_atc_texte = t_classification_atc.texte_court,
					classification_atc_section_id = t_classification_atc.section_id,
					classification_atc_section_code = t_classification_atc.section_code,
					classification_atc_section_texte = t_classification_atc.section_texte,
					classification_atc_1_id = t_classification_atc.classification_atc_1_id,
					classification_atc_1_code = t_classification_atc.classification_atc_1_code,
					classification_atc_1_texte = t_classification_atc.classification_atc_1_texte,
					classification_atc_2_id = t_classification_atc.classification_atc_2_id,
					classification_atc_2_code = t_classification_atc.classification_atc_2_code,
					classification_atc_2_texte = t_classification_atc.classification_atc_2_texte,
					classification_atc_3_id = t_classification_atc.classification_atc_3_id,
					classification_atc_3_code = t_classification_atc.classification_atc_3_code,
					classification_atc_3_texte = t_classification_atc.classification_atc_3_texte,
					classification_atc_4_id = t_classification_atc.classification_atc_4_id,
					classification_atc_4_code = t_classification_atc.classification_atc_4_code,
					classification_atc_4_texte = t_classification_atc.classification_atc_4_texte,

					unite_stockage_code = t_unites_stockage.code,
					unite_stockage_texte = t_unites_stockage.texte_court,
					unite_stockage_section_id = t_unites_stockage.section_id,
					unite_stockage_section_code = t_unites_stockage.section_code,
					unite_stockage_section_texte = t_unites_stockage.section_texte,

					unite_distribution_code = t_unites_distribution.code,
					unite_distribution_texte = t_unites_distribution.texte_court,
					unite_distribution_section_id = t_unites_distribution.section_id,
					unite_distribution_section_code = t_unites_distribution.section_code,
					unite_distribution_section_texte = t_unites_distribution.section_texte,
					
					compte_code = t_compte.code,
					compte_texte = t_compte.texte_court,
					compte_section_id = t_compte.section_id,
					compte_section_code = t_compte.section_code,
					compte_section_texte = t_compte.section_texte

				FROM eco.t_types_articles ,
					eco.t_familles_articles,
					eco.t_sous_familles_articles,
					eco.t_categories_articles,
					eco.t_sous_categories_articles,
					eco.t_classification_atc,
					eco.t_unites t_unites_stockage,
					eco.t_unites t_unites_distribution,
					eco.t_compte
					
				WHERE type_id = t_types_articles.oid
					AND famille_id = t_familles_articles.oid
					AND sous_famille_id = t_sous_familles_articles.oid
					AND categorie_id = t_categories_articles.oid
					AND sous_categorie_id = t_sous_categories_articles.oid
					AND classification_atc_id = t_classification_atc.oid
					AND unite_stockage_id = t_unites_stockage.oid
					AND unite_distribution_id = t_unites_distribution.oid
					AND compte_id = t_compte.oid
					AND (
						type_code IS DISTINCT FROM t_types_articles.code OR
						type_texte IS DISTINCT FROM t_types_articles.texte_court OR
						type_section_id IS DISTINCT FROM t_types_articles.section_id OR
						type_section_code IS DISTINCT FROM t_types_articles.section_code OR
						type_section_texte IS DISTINCT FROM t_types_articles.section_texte OR
						
						famille_code IS DISTINCT FROM t_familles_articles.code OR
						famille_texte IS DISTINCT FROM t_familles_articles.texte_court OR
						famille_section_id IS DISTINCT FROM t_familles_articles.section_id OR
						famille_section_code IS DISTINCT FROM t_familles_articles.section_code OR
						famille_section_texte IS DISTINCT FROM t_familles_articles.section_texte OR
						
						sous_famille_code IS DISTINCT FROM t_sous_familles_articles.code OR
						sous_famille_texte IS DISTINCT FROM t_sous_familles_articles.texte_court OR
						sous_famille_section_id IS DISTINCT FROM t_sous_familles_articles.section_id OR
						sous_famille_section_code IS DISTINCT FROM t_sous_familles_articles.section_code OR
						sous_famille_section_texte IS DISTINCT FROM t_sous_familles_articles.section_texte OR
						
						categorie_code IS DISTINCT FROM t_categories_articles.code OR
						categorie_texte IS DISTINCT FROM t_categories_articles.texte_court OR
						categorie_section_id IS DISTINCT FROM t_categories_articles.section_id OR
						categorie_section_code IS DISTINCT FROM t_categories_articles.section_code OR
						categorie_section_texte IS DISTINCT FROM t_categories_articles.section_texte OR
						
						sous_categorie_code IS DISTINCT FROM t_sous_categories_articles.code OR
						sous_categorie_texte IS DISTINCT FROM t_sous_categories_articles.texte_court OR
						sous_categorie_section_id IS DISTINCT FROM t_sous_categories_articles.section_id OR
						sous_categorie_section_code IS DISTINCT FROM t_sous_categories_articles.section_code OR
						sous_categorie_section_texte IS DISTINCT FROM t_sous_categories_articles.section_texte OR
						
						t_articles.classification_atc_code IS DISTINCT FROM t_classification_atc.code OR
						t_articles.classification_atc_texte IS DISTINCT FROM t_classification_atc.texte_court OR
						t_articles.classification_atc_section_id IS DISTINCT FROM t_classification_atc.section_id OR
						t_articles.classification_atc_section_code IS DISTINCT FROM t_classification_atc.section_code OR
						t_articles.classification_atc_section_texte IS DISTINCT FROM t_classification_atc.section_texte OR
						t_articles.classification_atc_1_id IS DISTINCT FROM t_classification_atc.classification_atc_1_id OR
						t_articles.classification_atc_1_code IS DISTINCT FROM t_classification_atc.classification_atc_1_code OR
						t_articles.classification_atc_1_texte IS DISTINCT FROM t_classification_atc.classification_atc_1_texte OR
						t_articles.classification_atc_2_id IS DISTINCT FROM t_classification_atc.classification_atc_2_id OR
						t_articles.classification_atc_2_code IS DISTINCT FROM t_classification_atc.classification_atc_2_code OR
						t_articles.classification_atc_2_texte IS DISTINCT FROM t_classification_atc.classification_atc_2_texte OR
						t_articles.classification_atc_3_id IS DISTINCT FROM t_classification_atc.classification_atc_3_id OR
						t_articles.classification_atc_3_code IS DISTINCT FROM t_classification_atc.classification_atc_3_code OR
						t_articles.classification_atc_3_texte IS DISTINCT FROM t_classification_atc.classification_atc_3_texte OR
						t_articles.classification_atc_4_id IS DISTINCT FROM t_classification_atc.classification_atc_4_id OR
						t_articles.classification_atc_4_code IS DISTINCT FROM t_classification_atc.classification_atc_4_code OR
						t_articles.classification_atc_4_texte IS DISTINCT FROM t_classification_atc.classification_atc_4_texte OR

						t_articles.unite_stockage_code IS DISTINCT FROM t_unites_stockage.code OR
						t_articles.unite_stockage_texte IS DISTINCT FROM t_unites_stockage.texte_court OR
						t_articles.unite_stockage_section_id IS DISTINCT FROM t_unites_stockage.section_id OR
						t_articles.unite_stockage_section_code IS DISTINCT FROM t_unites_stockage.section_code OR
						t_articles.unite_stockage_section_texte IS DISTINCT FROM t_unites_stockage.section_texte OR
						
						t_articles.unite_distribution_code IS DISTINCT FROM t_unites_distribution.code OR
						t_articles.unite_distribution_texte IS DISTINCT FROM t_unites_distribution.texte_court OR
						t_articles.unite_distribution_section_id IS DISTINCT FROM t_unites_distribution.section_id OR
						t_articles.unite_distribution_section_code IS DISTINCT FROM t_unites_distribution.section_code OR
						t_articles.unite_distribution_section_texte IS DISTINCT FROM t_unites_distribution.section_texte OR

						t_articles.compte_code IS DISTINCT FROM t_compte.code OR
						t_articles.compte_texte IS DISTINCT FROM t_compte.texte_court OR
						t_articles.compte_section_id IS DISTINCT FROM t_compte.section_id OR
						t_articles.compte_section_code IS DISTINCT FROM t_compte.section_code OR
						t_articles.compte_section_texte IS DISTINCT FROM t_compte.section_texte
					);



			]]></sqlcmd>
		</NODE>
		
		
		<NODE label="Tables Fournisseurs">
			<sqlcmd><![CDATA[
			
				-- Types de fournisseurs
				INSERT INTO eco.t_types_fournisseurs(code_original, code, texte, texte_court)
				SELECT CODE_CATEG_FOURN, CODE_CATEG_FOURN, MAX(LIB_CATEG_FOURN), MAX(LIB_CATEG_FOURN)
				FROM prod_evolucare_hygie.CATEGORIE_FOURN
				WHERE CODE_CATEG_FOURN NOT IN (SELECT code_original FROM eco.t_types_fournisseurs WHERE code_original IS NOT NULL)
				   GROUP BY 1,2
				ORDER BY 1
				;

				
				
				-- Fournisseurs (basé sur commandes)
				INSERT INTO eco.t_fournisseurs(code_original, code, texte, texte_court)
				SELECT CODE_FOURN, CODE_FOURN, MAX(NOM_FOURN), MAX(NOM_FOURN)
				FROM prod_evolucare_hygie.FOURNISSEURS
				WHERE CODE_FOURN NOT IN (SELECT code_original FROM eco.t_fournisseurs WHERE code_original IS NOT NULL)
				GROUP BY 1,2
				ORDER BY 1
				;
				
								
				
				UPDATE eco.t_fournisseurs
				SET code = CODE_FOURN,
					texte = NOM_FOURN,
					texte_court =  NOM_FOURN
				FROM prod_evolucare_hygie.FOURNISSEURS
				WHERE code_original = CODE_FOURN
				AND
					(
					code IS DISTINCT FROM CODE_FOURN OR
					texte IS DISTINCT FROM NOM_FOURN OR
					texte_court IS DISTINCT FROM NOM_FOURN
					)
				;


				UPDATE eco.t_fournisseurs
				SET type_id = COALESCE(t_types_fournisseurs.oid,0)
				FROM prod_evolucare_hygie.FOURNISSEURS
				LEFT JOIN eco.t_types_fournisseurs ON t_types_fournisseurs.code_original = FOURNISSEURS.CODE_CATEG_FOURN 
				WHERE t_fournisseurs.code_original = IDFOURNISSEURS AND
					type_id IS DISTINCT FROM COALESCE(t_types_fournisseurs.oid,0)
				;
				
				UPDATE eco.t_fournisseurs SET
					type_code = t_types_fournisseurs.code,
					type_texte = t_types_fournisseurs.texte_court,
					type_section_id = t_types_fournisseurs.section_id,
					type_section_code = t_types_fournisseurs.section_code,
					type_section_texte = t_types_fournisseurs.section_texte
				FROM eco.t_types_fournisseurs
				WHERE type_id = t_types_fournisseurs.oid
					AND (
						type_code IS DISTINCT FROM t_types_fournisseurs.code OR
						type_texte IS DISTINCT FROM t_types_fournisseurs.texte_court OR
						type_section_id IS DISTINCT FROM t_types_fournisseurs.section_id OR
						type_section_code IS DISTINCT FROM t_types_fournisseurs.section_code OR
						type_section_texte IS DISTINCT FROM t_types_fournisseurs.section_texte
					);

					
				UPDATE eco.t_articles SET
					fournisseur_principal_id = t_fournisseurs.oid
				FROM prod_evolucare_hygie.ARTICLES
				JOIN eco.t_fournisseurs ON ARTICLES.CODE_FOURN = t_fournisseurs.code_original
				WHERE t_articles.code_original = REF_ART_INT AND
					fournisseur_principal_id IS DISTINCT FROM t_fournisseurs.oid
				;
				
				UPDATE eco.t_articles SET
					ref_fournisseur_id = subview.ref_fournisseur_id,
					ref_fournisseur_texte = subview.ref_fournisseur_texte
				FROM
					(
						SELECT article_id,
							base.cti_group_array3(fournisseur_id) AS ref_fournisseur_id,
							base.cti_group_array3(ref_fournisseur_texte) AS ref_fournisseur_texte
						FROM 
						(
							SELECT 
								t_articles.oid AS article_id,
								t_fournisseurs.oid AS fournisseur_id,
								CASE WHEN REF_ART_FOURN <> '' THEN REF_ART_FOURN ELSE REF_ART_INT END AS ref_fournisseur_texte
							FROM eco.t_articles
								JOIN prod_evolucare_hygie.ARTICLES ON REF_ART_INT = t_articles.code_original
								JOIN eco.t_fournisseurs ON CODE_FOURN = t_fournisseurs.code
							GROUP BY 1,2,3
							ORDER BY 1,2,3
						) subview
						GROUP BY 1
					) subview
					WHERE t_articles.oid = subview.article_id AND
						(
						t_articles.ref_fournisseur_id IS DISTINCT FROM subview.ref_fournisseur_id OR
						t_articles.ref_fournisseur_texte IS DISTINCT FROM subview.ref_fournisseur_texte
						)
                ;

			]]></sqlcmd>
		</NODE>
		
		
		
		<NODE label="Autres tables">
			<sqlcmd><![CDATA[
			
				-- Gestionnaires
				
				
				
				-- Unités fonctionnelles
				INSERT INTO eco.t_unites_fonctionnelles(code_original, code, texte, texte_court)
				SELECT CODE_CLIENT, CODE_CLIENT, MAX(NOM_CLIENT), MAX(NOM_CLIENT)
				FROM prod_evolucare_hygie.CLIENTS
				WHERE CODE_CLIENT NOT IN (SELECT code_original FROM eco.t_unites_fonctionnelles WHERE code_original IS NOT NULL)
				GROUP BY 1,2
				;
				
				UPDATE eco.t_unites_fonctionnelles SET
					texte = NOM_CLIENT,
					texte_court = NOM_CLIENT
				FROM prod_evolucare_hygie.CLIENTS
				WHERE code_original = CODE_CLIENT AND
					(texte IS DISTINCT FROM NOM_CLIENT OR
					texte_court IS DISTINCT FROM NOM_CLIENT);
				
				-- Lieux
				
							
					
				-- Site
						
				
				
				-- Types mouvements
				DROP TABLE IF EXISTS w_types;
				CREATE TEMP TABLE w_types (code_original text, code text, texte text, texte_court text);
				INSERT INTO w_types VALUES
					('REC','REC','réception fournisseur','réception fournisseur'),
					('RET','RET','retour fournisseur','retour fournisseur'),
					('LIV','LIV','livraison client','livraison client'),
					('INV','INV','correction inventaire','correction inventaire')
					;
			
				INSERT INTO eco.t_types_mouvements(code_original, code, texte, texte_court)
				SELECT code_original, code, texte, texte_court
				FROM w_types
				WHERE code_original NOT IN (SELECT code_original FROM eco.t_types_mouvements)
				;
			]]></sqlcmd>
		</NODE>
		
		
	</NODE>
	<NODE name="PROD" label="RECUPERATION DES DONNEES DE PRODUCTION">
			
			
		<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');
				
				
			
				TRUNCATE eco.p_commandes;
				
				INSERT INTO eco.p_commandes(
					code_original,
					numero,
					date_commande,
					date_engagement,
					objet,
					reference,
					date_livraison_prevue,
					etat_reception,
					date_reception,
					etat_liquidation,
					fournisseur_id,
					gestionnaire_id,
					lieu_commande_id,
					lieu_facturation_id,
					lieu_livraison_id,
					unite_fonctionnelle_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)
				SELECT 
					NUM_CDE_FOURN AS code_original,
					NUM_CDE_FOURN AS numero,
					DATE_CDE_FOURN AS date_commande,
					DATE_CDE_FOURN AS date_engagement,
					''::text AS objet,
					NOM1_CDE_FOURN AS reference,
					DATE_LIVR_PREVUE_CDE AS date_livraison_prevue,
					'T' AS etat_reception,
					'20991231'::date AS date_reception,
					'T' AS etat_liquidation,
					COALESCE(t_fournisseurs.oid, 0) AS fournisseur_id,
					COALESCE(t_gestionnaires.oid,0) AS gestionnaire_id,
					0::bigint AS lieu_commande_id,
					COALESCE(t_lieux_facturation.oid,0) AS lieu_facturation_id,
					COALESCE(t_lieux_facturation.oid,0) AS lieu_livraison_id,
					0::bigint AS unite_fonctionnelle_id,
					TOTAL_HT_CDE_FOURN AS montant_commande_ht,
					TOTAL_REM_CDE_FOURN AS montant_commande_remise,
					TOTAL_TVA_CDE_FOURN AS montant_commande_tva,
					0::numeric AS montant_commande_port,
					TOTAL_TTC_CDE_FOURN AS montant_commande_total,
					0::numeric AS montant_liquidation_ht,
					0::numeric AS montant_liquidation_remise,
					0::numeric AS montant_liquidation_tva,
					0::numeric AS montant_liquidation_port,
					0::numeric AS montant_liquidation_total
				FROM prod_evolucare_hygie.CDE_FOURN
					LEFT JOIN eco.t_fournisseurs ON t_fournisseurs.code_original = CODE_FOURN
					LEFT JOIN eco.t_lieux t_lieux_commande ON t_lieux_commande.code_original = 'XXXX'
					LEFT JOIN eco.t_lieux t_lieux_livraison ON t_lieux_livraison.code_original = 'XXXX'
					LEFT JOIN eco.t_lieux t_lieux_facturation ON t_lieux_facturation.code_original = 'XXXX'
					LEFT JOIN eco.t_gestionnaires ON t_gestionnaires.code_original = 'XXXX'
				WHERE DATE_CDE_FOURN >= '[ENV_ECO_ANNEEDEBUT]-01-01'
				ORDER BY 1;
				
				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_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');
				
				DROP TABLE IF EXISTS w_LIGNECDE_FOURN;
				CREATE TEMP TABLE w_LIGNECDE_FOURN AS
				SELECT *,
					0::numeric AS QTE_LIGNELIV_FOURN
				FROM prod_evolucare_hygie.LIGNECDE_FOURN
				;

				UPDATE w_LIGNECDE_FOURN LIGNECDE_FOURN SET
					QTE_LIGNELIV_FOURN = subview.QTE_LIGNELIV_FOURN
				FROM
				(
					SELECT 
						LIGNELIV_FOURN.IDLIGNECDE_FOURN_INCHANGEABLE, SUM(QTE_LIGNELIV_FOURN) AS QTE_LIGNELIV_FOURN
					FROM prod_evolucare_hygie.LIV_FOURN
					JOIN prod_evolucare_hygie.LIGNELIV_FOURN ON LIV_FOURN.NUM_LIV_FOURN = LIGNELIV_FOURN.NUM_LIV_FOURN
					JOIN prod_evolucare_hygie.LIGNECDE_FOURN ON LIGNELIV_FOURN.IDLIGNECDE_FOURN_INCHANGEABLE = LIGNECDE_FOURN.IDLIGNECDE_FOURN_INCHANGEABLE
					GROUP BY 1
				) subview
				WHERE subview.IDLIGNECDE_FOURN_INCHANGEABLE = LIGNECDE_FOURN.IDLIGNECDE_FOURN_INCHANGEABLE
				;

				UPDATE w_LIGNECDE_FOURN LIGNECDE_FOURN SET
					QTE_LIGNELIV_FOURN = QTE_LIGNECDE_FOURN
				WHERE QTE_LIGNELIV_FOURN <> QTE_LIGNECDE_FOURN AND
					QTE_LIGNELIV_FOURN = QTE_LIGNECDE_FOURN * COND1_LIGNECDE_FOURN
				; 

				

				TRUNCATE eco.p_lignes_commandes;

				INSERT INTO eco.p_lignes_commandes(
					code_original,
					commande_id,
					ligne_commande,
					texte,
					article_id,
					unite_approvisionnement_id,
					compte_id,
					ucd_id,
					lpp_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,
					montant_livraison_ht,
					montant_livraison_ttc,
					etat_livraison,

					montant_liquidation_ht,
					montant_liquidation_remise,
					montant_liquidation_tva,
					montant_liquidation_port,
					montant_liquidation_ttc,
					etat_liquidation,
                    
                    taux_prorata_tva)
				SELECT
					IDLIGNECDE_FOURN_INCHANGEABLE AS code_original,
					p_commandes.oid AS commande_id,
					NUMLI_LIGNECDE_FOURN::numeric AS ligne_commande,
					LIB_LIGNECDE_FOURN AS texte,
					COALESCE(t_articles.oid, 0) AS article_id,
					0::bigint AS unite_approvisionnement_id,
					t_articles.compte_id, 
					COALESCE(t_ucd.oid, t_articles.ucd_id, 0) AS ucd_id,
					COALESCE(t_lpp.oid, t_articles.lpp_id, 0) AS lpp_id,
					
					QTE_LIGNECDE_FOURN AS quantite_approvisionnement,
					PA_LIGNECDE_FOURN AS prix_unitaire_approvisionnement,
					0::numeric AS multiplicateur_stockage,
					0::numeric AS quantite_stockage,
					0::numeric AS prix_unitaire_stockage,

					round(QTE_LIGNECDE_FOURN::numeric * PA_LIGNECDE_FOURN::numeric,2) AS montant_commande_ht,
					0::numeric AS montant_commande_remise,
					round(QTE_LIGNECDE_FOURN::numeric * PA_LIGNECDE_FOURN::numeric * VAL_TVA_LIGNECDE_FOURN / 100,2) AS montant_commande_tva,
					0::numeric AS montant_commande_port,
					round(QTE_LIGNECDE_FOURN::numeric * PA_LIGNECDE_FOURN::numeric,2) +
						round(QTE_LIGNECDE_FOURN::numeric * PA_LIGNECDE_FOURN::numeric * VAL_TVA_LIGNECDE_FOURN / 100,2) AS montant_commande_ttc,

					QTE_LIGNELIV_FOURN AS quantite_livraison_approvisionnement,
					QTE_LIGNELIV_FOURN AS quantite_livraison_stockage,
					round(QTE_LIGNELIV_FOURN::numeric * PA_LIGNECDE_FOURN::numeric,2) AS montant_livraison_ht,
					round(QTE_LIGNELIV_FOURN::numeric * PA_LIGNECDE_FOURN::numeric,2) +
						round(QTE_LIGNELIV_FOURN::numeric * PA_LIGNECDE_FOURN::numeric * VAL_TVA_LIGNECDE_FOURN / 100,2) AS montant_livraison_ttc,
					CASE WHEN QTE_LIGNELIV_FOURN = QTE_LIGNECDE_FOURN THEN 'T' WHEN QTE_LIGNELIV_FOURN > QTE_LIGNECDE_FOURN THEN 'S' WHEN QTE_LIGNELIV_FOURN = 0 THEN 'N' ELSE 'P' END AS etat_livraison,

					0::numeric AS montant_liquidation_ht,
					0::numeric AS montant_liquidation_remise,
					0::numeric AS montant_liquidation_tva,
					0::numeric AS montant_liquidation_port,
					0::numeric AS montant_liquidation_ttc,
					'T',
					0::numeric 
				FROM w_LIGNECDE_FOURN LIGNECDE_FOURN
					JOIN eco.p_commandes ON LIGNECDE_FOURN.NUM_CDE_FOURN = p_commandes.code_original
					LEFT JOIN eco.t_articles ON REF_ART_INT = t_articles.code_original AND t_articles.oid != 0
					LEFT JOIN base.t_ucd ON ucd_id = t_ucd.oid AND t_ucd.oid != 0
					LEFT JOIN base.t_lpp ON lpp_id = t_lpp.oid AND t_lpp.oid != 0
					LEFT JOIN eco.t_prorata_tva ON p_commandes.date_engagement BETWEEN t_prorata_tva.date_debut AND t_prorata_tva.date_fin
					;



                UPDATE eco.p_commandes SET
                    montant_commande_ht = subview.montant_commande_ht,
                    montant_commande_tva = subview.montant_commande_tva,
                    montant_commande_total = subview.montant_commande_ttc,
                    etat_reception = subview.etat_livraison
                FROM
                (
                SELECT 
                    commande_id,
                    SUM(p_lignes_commandes.montant_commande_ht) AS montant_commande_ht,
                    SUM(p_lignes_commandes.montant_commande_tva) AS montant_commande_tva,
                    SUM(p_lignes_commandes.montant_commande_ttc) AS montant_commande_ttc,
                    MIN(CASE WHEN etat_livraison NOT IN ('S','T') THEN 'N' ELSE 'T' END) AS etat_livraison
                FROM eco.p_lignes_commandes
                GROUP BY 1
                ) subview
                WHERE commande_id = p_commandes.oid
                ;

				
				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_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');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_10');

                TRUNCATE eco.p_mouvements_articles;
                
                INSERT INTO eco.p_mouvements_articles(
					date, 
					sens_mouvement,
					type_mouvement_id, 
					texte, 
					gestionnaire_id, 
					lieu_id, 
					unite_fonctionnelle_id, 
					article_id, 
					compte_id, 
					fournisseur_id,
					commande_id,
					ligne_commande,
					lpp_id,
					ucd_id,
					prix_unitaire, 
					entree_quantite, 
					entree_montant, 
					entree_montant_ht,
					sortie_quantite,
					sortie_montant,
					sortie_montant_ht)

				SELECT 
					DATE(LIV_FOURN.DATE_LIV_FOURN) AS date,
					'E' AS sens_mouvement,
					COALESCE(t_types_mouvements.oid,0) AS type_mouvement_id,
					t_types_mouvements.texte || ' ' || LIGNELIV_FOURN.NUM_LIV_FOURN || '-' || NUMLI_LIGNELIV_FOURN || ' ' || t_articles.code AS texte,
					COALESCE(t_gestionnaires.oid,0) AS gestionnaire_id,
					COALESCE(t_lieux.oid,0) AS lieu_id,
					COALESCE(t_unites_fonctionnelles.oid,0) AS unite_fonctionnelle_id,
					COALESCE(t_articles.oid,0) AS article_id,
					COALESCE(t_articles.compte_id,0) AS compte_id,
					COALESCE(t_fournisseurs.oid,t_articles.fournisseur_principal_id,0) AS fournisseur_id,
					COALESCE(p_commandes.oid,0) AS commande_id,
					COALESCE(p_lignes_commandes.ligne_commande,0) AS ligne_commande,
					COALESCE(t_articles.lpp_id,0) AS lppid,
					COALESCE(t_articles.ucd_id,0) AS ucd_id,
					PMP AS prix_unitaire,
					QTE_LIGNELIV_FOURN AS entree_quantite,
					(QTE_LIGNELIV_FOURN * PA_LIGNELIV_FOURN) + (QTE_LIGNELIV_FOURN * PA_LIGNELIV_FOURN * VAL_TVA_LIGNELIV_FOURN / 100) AS entree_montant,
					QTE_LIGNELIV_FOURN * PA_LIGNELIV_FOURN AS entree_montant_ht,
					0::numeric AS sortie_quantite,
					0::numeric AS sortie_montant,
					0::numeric AS sortie_montant_ht

				FROM prod_evolucare_hygie.LIGNELIV_FOURN
				JOIN prod_evolucare_hygie.LIV_FOURN ON LIGNELIV_FOURN.NUM_LIV_FOURN = LIV_FOURN.NUM_LIV_FOURN
					LEFT JOIN eco.p_commandes ON LIV_FOURN.NUM_CDE_FOURN = p_commandes.code_original
					LEFT JOIN eco.p_lignes_commandes ON LIGNELIV_FOURN.IDLIGNECDE_FOURN_INCHANGEABLE = p_lignes_commandes.code_original
					LEFT JOIN eco.t_types_mouvements ON 'REC' = t_types_mouvements.code_original
					LEFT JOIN eco.t_fournisseurs ON LIGNELIV_FOURN.CODE_FOURN = t_fournisseurs.code_original
					LEFT JOIN eco.t_gestionnaires ON 'XXX' = t_gestionnaires.code_original
					LEFT JOIN eco.t_lieux ON 'XXX' = t_lieux.code_original
					LEFT JOIN eco.t_unites_fonctionnelles ON 'XXXX' = t_unites_fonctionnelles.code_original 
					LEFT JOIN eco.t_articles ON LIGNELIV_FOURN.REF_ART_INT = t_articles.code_original AND t_articles.oid != 0
				WHERE DATE(LIV_FOURN.DATE_LIV_FOURN) >= date('[ENV_ECO_ANNEEDEBUT]-01-01')
				ORDER BY LIV_FOURN.DATE_LIV_FOURN
                ;

                
                INSERT INTO eco.p_mouvements_articles(
					date, 
					sens_mouvement,
					type_mouvement_id, 
					texte, 
					gestionnaire_id, 
					lieu_id, 
					unite_fonctionnelle_id, 
					article_id, 
					compte_id, 
					fournisseur_id,
					commande_id,
					ligne_commande,
					lpp_id,
					ucd_id,
					prix_unitaire, 
					entree_quantite, 
					entree_montant, 
					entree_montant_ht,
					sortie_quantite,
					sortie_montant,
					sortie_montant_ht)

				SELECT 
					DATE(LIV_FOURN.DATE_LIV_FOURN) AS date,
					'E' AS sens_mouvement,
					COALESCE(t_types_mouvements.oid,0) AS type_mouvement_id,
					t_types_mouvements.texte || ' ' || NUM_RETOUR_FOURN || '-' || t_articles.code AS texte,
					COALESCE(t_gestionnaires.oid,0) AS gestionnaire_id,
					COALESCE(t_lieux.oid,0) AS lieu_id,
					COALESCE(t_unites_fonctionnelles.oid,0) AS unite_fonctionnelle_id,
					COALESCE(t_articles.oid,0) AS article_id,
					COALESCE(t_articles.compte_id,0) AS compte_id,
					COALESCE(t_fournisseurs.oid,t_articles.fournisseur_principal_id,0) AS fournisseur_id,
					COALESCE(p_commandes.oid,0) AS commande_id,
					COALESCE(p_lignes_commandes.ligne_commande,0) AS ligne_commande,
					COALESCE(t_articles.lpp_id,0) AS lppid,
					COALESCE(t_articles.ucd_id,0) AS ucd_id,
					PMP AS prix_unitaire,
					QTE_LIGNELIV_FOURN AS entree_quantite,
					(QTE_LIGNELIV_FOURN * PA_LIGNELIV_FOURN) + (QTE_LIGNELIV_FOURN * PA_LIGNELIV_FOURN * VAL_TVA_LIGNELIV_FOURN / 100) AS entree_montant,
					QTE_LIGNELIV_FOURN * PA_LIGNELIV_FOURN AS entree_montant_ht,
					0::numeric AS sortie_quantite,
					0::numeric AS sortie_montant,
					0::numeric AS sortie_montant_ht
				FROM prod_evolucare_hygie.LIGNELIV_RETOUR_FOURN
				JOIN prod_evolucare_hygie.LIV_FOURN ON LIGNELIV_RETOUR_FOURN.NUM_LIV_FOURN = LIV_FOURN.NUM_LIV_FOURN
					LEFT JOIN eco.p_lignes_commandes ON 'XXX' = p_lignes_commandes.code_original
					LEFT JOIN eco.p_commandes ON 'XXX' = p_commandes.code_original
					LEFT JOIN eco.t_types_mouvements ON 'RET' = t_types_mouvements.code_original
					LEFT JOIN eco.t_fournisseurs ON LIGNELIV_RETOUR_FOURN.CODE_FOURN = t_fournisseurs.code_original
					LEFT JOIN eco.t_gestionnaires ON 'XXX' = t_gestionnaires.code_original
					LEFT JOIN eco.t_lieux ON 'XXX' = t_lieux.code_original
					LEFT JOIN eco.t_unites_fonctionnelles ON 'XXXX' = t_unites_fonctionnelles.code_original 
					LEFT JOIN eco.t_articles ON LIGNELIV_RETOUR_FOURN.REF_ART_INT = t_articles.code_original AND t_articles.oid != 0
				WHERE DATE(LIV_FOURN.DATE_LIV_FOURN) >= date('[ENV_ECO_ANNEEDEBUT]-01-01')
				ORDER BY LIV_FOURN.DATE_LIV_FOURN
                ;

				
                INSERT INTO eco.p_mouvements_articles(
					date, 
					sens_mouvement,
					type_mouvement_id, 
					texte, 
					gestionnaire_id, 
					lieu_id, 
					unite_fonctionnelle_id, 
					article_id, 
					compte_id, 
					fournisseur_id,
					commande_id,
					ligne_commande,
					lpp_id,
					ucd_id,
					prix_unitaire, 
					entree_quantite, 
					entree_montant, 
					entree_montant_ht,
					sortie_quantite,
					sortie_montant,
					sortie_montant_ht)

				SELECT 
					DATE(DATE_LIV_CLIENT) AS date,
					'S' AS sens_mouvement,
					COALESCE(t_types_mouvements.oid,0) AS type_mouvement_id,
					TITRE_LIV_CLIENT || ' ' || LIGNELIV_CLI.NUM_LIV_CLIENT || '-' || NUMLI_LIGNELIV_CLIENT AS texte,
					COALESCE(t_gestionnaires.oid,0) AS gestionnaire_id,
					COALESCE(t_lieux.oid,0) AS lieu_id,
					COALESCE(t_unites_fonctionnelles.oid,0) AS unite_fonctionnelle_id,
					COALESCE(t_articles.oid,0) AS article_id,
					COALESCE(t_articles.compte_id,0) AS compte_id,
					COALESCE(t_articles.fournisseur_principal_id,0) AS fournisseur_id,
					COALESCE(p_commandes.oid,0) AS commande_id,
					0 AS ligne_commande,
					COALESCE(t_articles.lpp_id,0) AS lppid,
					COALESCE(t_articles.ucd_id,0) AS ucd_id,
					PMP AS prix_unitaire,
					0::numeric AS entree_quantite,
					0::numeric AS entree_montant,
					0::numeric AS entree_montant_ht,
                    QTE_LIGNELIV_CLIENT AS sortie_quantite,
					(QTE_LIGNELIV_CLIENT * PMP) + (QTE_LIGNELIV_CLIENT * PMP * VAL_TVA_LIGNELIV_CLIENT / 100) AS sortie_montant,
					QTE_LIGNELIV_CLIENT * PMP AS sortie_montant_ht

				FROM prod_evolucare_hygie.LIGNELIV_CLI
				JOIN prod_evolucare_hygie.LIV_CLIENT ON LIGNELIV_CLI.NUM_LIV_CLIENT = LIV_CLIENT.NUM_LIV_CLIENT
					LEFT JOIN eco.p_commandes ON 'XXX' = p_commandes.code_original
					LEFT JOIN eco.t_types_mouvements ON 'LIV' = t_types_mouvements.code_original
					LEFT JOIN eco.t_gestionnaires ON 'XXX' = t_gestionnaires.code_original
					LEFT JOIN eco.t_lieux ON 'XXX' = t_lieux.code_original
					LEFT JOIN eco.t_unites_fonctionnelles ON CODE_CLIENT = t_unites_fonctionnelles.code_original 
					LEFT JOIN eco.t_articles ON LIGNELIV_CLI.REF_ART_INT = t_articles.code_original AND t_articles.oid != 0
					

				WHERE DATE(DATE_LIV_CLIENT) >= date('[ENV_ECO_ANNEEDEBUT]-01-01')
				ORDER BY DATE_LIV_CLIENT;
				
				INSERT INTO eco.p_mouvements_articles(
					date, 
					sens_mouvement,
					type_mouvement_id, 
					texte, 
					gestionnaire_id, 
					lieu_id, 
					unite_fonctionnelle_id, 
					article_id, 
					compte_id, 
					fournisseur_id,
					commande_id,
					ligne_commande,
					lpp_id,
					ucd_id,
					prix_unitaire, 
					entree_quantite, 
					entree_montant, 
					entree_montant_ht,
					sortie_quantite,
					sortie_montant,
					sortie_montant_ht)

				SELECT 
					DATE(LIGNELIV_CLI_SUPPR.DATE_LIV_CLIENT) AS date,
					'S' AS sens_mouvement,
					COALESCE(t_types_mouvements.oid,0) AS type_mouvement_id,
					TITRE_LIV_CLIENT || ' ' || LIGNELIV_CLI_SUPPR.NUM_LIV_CLIENT || '-' || NUMLI_LIGNELIV_CLIENT AS texte,
					COALESCE(t_gestionnaires.oid,0) AS gestionnaire_id,
					COALESCE(t_lieux.oid,0) AS lieu_id,
					COALESCE(t_unites_fonctionnelles.oid,0) AS unite_fonctionnelle_id,
					COALESCE(t_articles.oid,0) AS article_id,
					COALESCE(t_articles.compte_id,0) AS compte_id,
					COALESCE(t_articles.fournisseur_principal_id,0) AS fournisseur_id,
					COALESCE(p_commandes.oid,0) AS commande_id,
					0 AS ligne_commande,
					COALESCE(t_articles.lpp_id,0) AS lppid,
					COALESCE(t_articles.ucd_id,0) AS ucd_id,
					PA_LIGNELIV_CLIENT AS prix_unitaire,
					0::numeric AS entree_quantite,
					0::numeric AS entree_montant,
					0::numeric AS entree_montant_ht,
                    QTE_LIGNELIV_CLIENT AS sortie_quantite,
					(QTE_LIGNELIV_CLIENT * PA_LIGNELIV_CLIENT) + (QTE_LIGNELIV_CLIENT * PA_LIGNELIV_CLIENT * VAL_TVA_LIGNELIV_CLIENT / 100) AS sortie_montant,
					QTE_LIGNELIV_CLIENT * PA_LIGNELIV_CLIENT AS sortie_montant_ht
				FROM prod_evolucare_hygie.LIGNELIV_CLI_SUPPR
				JOIN prod_evolucare_hygie.LIV_CLIENT ON LIGNELIV_CLI_SUPPR.NUM_LIV_CLIENT = LIV_CLIENT.NUM_LIV_CLIENT
					LEFT JOIN eco.p_commandes ON 'XXX' = p_commandes.code_original
					LEFT JOIN eco.t_types_mouvements ON 'LIV' = t_types_mouvements.code_original
					LEFT JOIN eco.t_gestionnaires ON 'XXX' = t_gestionnaires.code_original
					LEFT JOIN eco.t_lieux ON 'XXX' = t_lieux.code_original
					LEFT JOIN eco.t_unites_fonctionnelles ON LIGNELIV_CLI_SUPPR.CODE_CLIENT = t_unites_fonctionnelles.code_original 
					LEFT JOIN eco.t_articles ON LIGNELIV_CLI_SUPPR.REF_ART_INT = t_articles.code_original AND t_articles.oid != 0
				WHERE DATE(LIGNELIV_CLI_SUPPR.DATE_LIV_CLIENT) >= date('[ENV_ECO_ANNEEDEBUT]-01-01')
				ORDER BY LIGNELIV_CLI_SUPPR.DATE_LIV_CLIENT;
				
				INSERT INTO eco.p_mouvements_articles(
					date, 
					sens_mouvement,
					type_mouvement_id, 
					texte, 
					gestionnaire_id, 
					lieu_id, 
					unite_fonctionnelle_id, 
					article_id, 
					compte_id, 
					fournisseur_id,
					commande_id,
					ligne_commande,
					lpp_id,
					ucd_id,
					prix_unitaire, 
					entree_quantite, 
					entree_montant, 
					entree_montant_ht,
					sortie_quantite,
					sortie_montant,
					sortie_montant_ht)

				SELECT 
					DATE(DATEHEUREREGUL) AS date,
					'S' AS sens_mouvement,
					COALESCE(t_types_mouvements.oid,0) AS type_mouvement_id,
					t_types_mouvements.texte || ' ' || t_articles.code AS texte,
					COALESCE(t_gestionnaires.oid,0) AS gestionnaire_id,
					COALESCE(t_lieux.oid,0) AS lieu_id,
					COALESCE(t_unites_fonctionnelles.oid,0) AS unite_fonctionnelle_id,
					COALESCE(t_articles.oid,0) AS article_id,
					COALESCE(t_articles.compte_id,0) AS compte_id,
					COALESCE(t_articles.fournisseur_principal_id,0) AS fournisseur_id,
					COALESCE(p_commandes.oid,0) AS commande_id,
					0 AS ligne_commande,
					COALESCE(t_articles.lpp_id,0) AS lppid,
					COALESCE(t_articles.ucd_id,0) AS ucd_id,
					PMP AS prix_unitaire,
					0::numeric AS entree_quantite,
					0::numeric AS entree_montant,
					0::numeric AS entree_montant_ht,
                    -DIFFERENCESTOCK AS sortie_quantite,
					(DIFFERENCESTOCK * PMP) + (DIFFERENCESTOCK * PMP * (1+TAUX_TVA) / 100) AS sortie_montant,
					DIFFERENCESTOCK * PMP AS sortie_montant_ht
				FROM prod_evolucare_hygie.REGULINVENTAIRE
					LEFT JOIN prod_evolucare_hygie.TVA ON REGULINVENTAIRE.CODETVA = TVA.CODE_TVA
					LEFT JOIN eco.p_commandes ON 'XXX' = p_commandes.code_original
					LEFT JOIN eco.t_types_mouvements ON 'INV' = t_types_mouvements.code_original
					LEFT JOIN eco.t_gestionnaires ON 'XXX' = t_gestionnaires.code_original
					LEFT JOIN eco.t_lieux ON 'XXX' = t_lieux.code_original
					LEFT JOIN eco.t_unites_fonctionnelles ON CODE_CLIENT = t_unites_fonctionnelles.code_original 
					LEFT JOIN eco.t_articles ON REF_ART_INT = t_articles.code_original AND t_articles.oid != 0
					

				WHERE DATE(DATEHEUREREGUL) >= date('[ENV_ECO_ANNEEDEBUT]-01-01')
				ORDER BY DATEHEUREREGUL;
				
				
				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');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_10');
				
				UPDATE eco.p_mouvements_articles SET
					entree_quantite = 0 - entree_quantite,
					sortie_quantite = 0 - sortie_quantite,
					entree_montant = 0 - entree_montant,
					sortie_montant = 0 - sortie_montant,
					entree_montant_ht = 0 - entree_montant_ht,
					sortie_montant_ht = 0 - sortie_montant_ht,
					sortie_montant_original_ttc = 0 - sortie_montant_original_ttc,
					sortie_montant_original_ht = 0 - sortie_montant_original_ht
				FROM eco.t_types_mouvements
				WHERE type_mouvement_id = t_types_mouvements.oid AND
					t_types_mouvements.particularite_inverser_signe = '1';
					
				UPDATE eco.p_commandes
				SET
					date_reception = date
				FROM (
					SELECT 
						commande_id,
						MAX(date) AS date
					FROM 
						eco.p_mouvements_articles
					GROUP BY 1) sub
				WHERE oid = commande_id
				;

			]]></sqlcmd>
		</NODE>
		
		
		<NODE label="Stocks">
			<sqlcmd><![CDATA[
			
			
				
				SELECT base.cti_disable_index('eco', 'i_stock_1');
				SELECT base.cti_disable_index('eco', 'i_stock_2');
				SELECT base.cti_disable_index('eco', 'i_stock_3');
				SELECT base.cti_disable_index('eco', 'i_stock_4');
				SELECT base.cti_disable_index('eco', 'i_stock_5');

				
				
				DROP TABLE IF EXISTS w_stock;
				CREATE TEMP TABLE w_stock AS
				SELECT 
					t_articles.code_original AS article_code_original,
					t_articles.oid AS article_id,
					t_articles.compte_id,
					t_articles.ucd_id,
					t_articles.lpp_id,
					t_articles.fournisseur_principal_id AS fournisseur_id,
					0 AS site_id,
					0 AS lieu_id,
					date(date_trunc('month',DATE_MODIF)) AS date_debut,
					date(date_trunc('month',DATE_MODIF) + interval '1 month' - interval '1 day') AS date_fin,
					date_part('year',DATE_MODIF)*12 + date_part('month',DATE_MODIF) AS mois_code,
					0 AS entree_quantite,
					0 AS entree_montant,
					0 AS sortie_quantite,
					0 AS sortie_montant,
					PMP AS pump,
					'1'::text AS ok_pump,
					0::numeric AS stock_quantite_debut,
					STK_REEL_QTE_ART AS stock_quantite_fin,
					0::numeric AS stock_valeur_debut,
					STK_REEL_QTE_ART*PMP AS stock_valeur_fin
				FROM prod_evolucare_hygie.ARTICLES
				JOIN eco.t_articles ON REF_ART_INT = code_original
				;

				UPDATE w_stock
				SET 
					stock_quantite_debut = stock_quantite_fin - qte,
					stock_valeur_debut = stock_valeur_fin - mnt
				FROM (
					SELECT
						article_id,
						date_trunc('month',date) AS date,
						SUM(entree_quantite) + SUM(sortie_quantite) As qte,
						SUM(entree_montant) + SUM(sortie_montant) AS mnt
					FROM
						eco.p_mouvements_articles
						JOIN eco.t_articles ON article_id = t_articles.oid
					GROUP BY 1,2 
					) sub
				WHERE
					sub.article_id = w_stock.article_id
					AND date = date_debut
				;

				INSERT INTO w_stock
				SELECT 
					t_articles.code_original AS article_code_original,
					p_mouvements_articles.article_id,
					t_articles.compte_id,
					t_articles.ucd_id,
					t_articles.lpp_id,
                    t_articles.fournisseur_principal_id AS fournisseur_id,
					0 AS site_id,
					0 AS lieu_id,
					date(date_trunc('month',date)) AS date_debut,
					date(date_trunc('month',date) + interval '1 month' - interval '1 day') AS date_fin,
					date_part('year',date)*12 + date_part('month',date) AS mois_code,
					SUM(p_mouvements_articles.entree_quantite) AS entree_quantite,
					SUM(p_mouvements_articles.entree_montant) AS entree_montant,
					SUM(p_mouvements_articles.sortie_quantite) AS sortie_quantite,
					SUM(p_mouvements_articles.sortie_montant) AS sortie_montant,
					0::numeric AS pump,
					'0'::text AS ok_pump,
					0::numeric AS stock_quantite_debut,
					0::numeric AS stock_quantite_fin,
					0::numeric AS stock_valeur_debut,
					0::numeric AS stock_valeur_fin
				FROM eco.p_mouvements_articles
				JOIN eco.t_articles ON p_mouvements_articles.article_id = t_articles.oid
				LEFT JOIN w_stock ON p_mouvements_articles.article_id = w_stock.article_id
					AND date(date_trunc('month',date)) = w_stock.date_debut
				WHERE
					w_stock.article_id IS NULL
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11
				;
				
				UPDATE w_stock
				SET stock_quantite_fin = subview.stock_quantite_fin
				FROM
				(
				SELECT  site_id, lieu_id, article_id, mois_code, 
					SUM(entree_quantite-sortie_quantite) OVER (PARTITION BY site_id, lieu_id, article_id ORDER BY date_fin) AS stock_quantite_fin
				FROM w_stock
				) subview
				WHERE w_stock.site_id = subview.site_id AND
					w_stock.lieu_id = subview.lieu_id AND
					w_stock.article_id = subview.article_id AND
					w_stock.mois_code = subview.mois_code AND
					subview.stock_quantite_fin <> 0
				;

				SELECT base.cti_execute('
				INSERT INTO w_stock
				SELECT 
					w_stock.article_code_original,
					w_stock.article_id,
					w_stock.compte_id,
					w_stock.ucd_id,
					w_stock.lpp_id,
                    w_stock.fournisseur_id,
					w_stock.site_id,
					w_stock.lieu_id,
					date(date_trunc(''month'',w_stock.date_debut) + interval ''1 month'') AS date_debut,
					date(date_trunc(''month'',w_stock.date_debut) + interval ''2 month'' - interval ''1 day'') AS date_fin,
					w_stock.mois_code + 1,
					0 AS entree_quantite,
					0 AS entree_montant,
					0 AS sortie_quantite,
					0 AS sortie_montant,
					0::numeric AS pump,
					''0''::text AS ok_pump,
					0::numeric AS stock_quantite_debut,
					w_stock.stock_quantite_fin,
					0::numeric AS stock_valeur_debut,
					0::numeric AS stock_valeur_fin
				FROM w_stock
				LEFT JOIN w_stock w_stock_next ON
					w_stock.site_id = w_stock_next.site_id AND
					w_stock.lieu_id = w_stock_next.lieu_id AND
					w_stock.article_id = w_stock_next.article_id AND
					w_stock.mois_code = w_stock_next.mois_code - 1
				WHERE w_stock.stock_quantite_fin <> 0 AND
					w_stock_next.mois_code IS NULL AND
					w_stock.date_debut <= date(now())
				',200)
				;
				
				

				UPDATE w_stock
				SET ok_pump = '1',
					pump = PMP,
					stock_valeur_fin = stock_quantite_fin * PMP
				FROM (
					SELECT 
						REF_ART_INT,
						date_trunc('month',DATE_LIV_CLIENT) as month,
						(MAX(ARRAY[DATE_LIV_CLIENT::text,PMP::text]))[2]::numeric AS PMP
					FROM 
						prod_evolucare_hygie.LIGNELIV_CLI
						JOIN prod_evolucare_hygie.LIV_CLIENT ON LIGNELIV_CLI.NUM_LIV_CLIENT = LIV_CLIENT.NUM_LIV_CLIENT
					GROUP  BY 1,2
					) sub
				WHERE 
					article_code_original = REF_ART_INT AND
					date_trunc('month',date_fin) = month AND
					ok_pump = 0
				;


				UPDATE w_stock
				SET ok_pump = '1',
					pump = PMP,
					stock_valeur_fin = stock_quantite_fin * PMP
				FROM (
					SELECT 
						REF_ART_INT,
						date_trunc('month',LIGNELIV_FOURN.DATE_LIV_FOURN) as month,
						(MAX(ARRAY[LIGNELIV_FOURN.DATE_LIV_FOURN::text,PMP::text]))[2]::numeric AS PMP
					FROM 
						prod_evolucare_hygie.LIGNELIV_FOURN
						JOIN prod_evolucare_hygie.LIV_FOURN ON LIGNELIV_FOURN.NUM_LIV_FOURN = LIV_FOURN.NUM_LIV_FOURN
					GROUP  BY 1,2
					) sub
				WHERE 
					article_code_original = REF_ART_INT AND
					date_trunc('month',date_fin) = month AND
					ok_pump = 0
				;
				
				UPDATE w_stock
				SET ok_pump = '1',
					pump = last_pump,
					stock_valeur_fin = stock_quantite_fin * last_pump
				FROM 
				(
					SELECT 
						article_id,
						date_fin AS date_pump,
						COALESCE(LEAD(pump) OVER (PARTITION BY article_id ORDER BY date_fin), 0) AS last_pump,
						COALESCE(LAG(date_fin) OVER (PARTITION BY article_id ORDER BY date_fin), '1900-01-01') AS last_date,
						COALESCE(LEAD(date_fin) OVER (PARTITION BY article_id ORDER BY date_fin), '2099-12-31') AS next_date
					FROM w_stock
					WHERE ok_pump != '0'
				) sub
				WHERE 
					sub.article_id = w_stock.article_id AND
					date_fin BETWEEN CASE WHEN last_date = '1900-01-01' THEN last_date ELSE date_pump END AND next_date AND
					ok_pump = 0
				;

				UPDATE w_stock SET
					stock_quantite_debut = w_stock_prec.stock_quantite_fin,
					stock_valeur_debut = w_stock_prec.stock_valeur_fin
				FROM w_stock w_stock_prec
				WHERE w_stock.site_id = w_stock_prec.site_id AND
					w_stock.lieu_id = w_stock_prec.lieu_id AND
					w_stock.article_id = w_stock_prec.article_id AND
					w_stock.mois_code = w_stock_prec.mois_code + 1
				;
				TRUNCATE eco.p_stock
				;
				
				INSERT INTO eco.p_stock
					(
					article_id,
					fournisseur_id,
					compte_id,
					ucd_id,
					lpp_id,
					site_id,
					lieu_id,
					date_debut,
					date_fin,
					entree_quantite,
					entree_montant,
					sortie_quantite,
					sortie_montant,
					pump,
					stock_quantite_debut,
					stock_quantite_fin,
					stock_valeur_debut,
					stock_valeur_fin
					)
				SELECT 
					article_id,
					fournisseur_id,
					compte_id,
					ucd_id,
					lpp_id,
					site_id,
					lieu_id,
					date_debut,
					date_fin,
					entree_quantite,
					entree_montant,
					sortie_quantite,
					sortie_montant,
					pump,
					stock_quantite_debut,
					stock_quantite_fin,
					stock_valeur_debut,
					stock_valeur_fin
				FROM w_stock
				;

			
				
				SELECT base.cti_enable_index('eco', 'i_stock_1');
				SELECT base.cti_enable_index('eco', 'i_stock_2');
				SELECT base.cti_enable_index('eco', 'i_stock_3');
				SELECT base.cti_enable_index('eco', 'i_stock_4');
				SELECT base.cti_enable_index('eco', 'i_stock_5');




			
			]]></sqlcmd>
		</NODE>
		
		
	
		<NODEx 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');
				
			
				TRUNCATE eco.p_facture;
				
				INSERT INTO eco.p_facture(
					code_original,
					no_facture,
					commande_id,
					date_facture,
					date_reglement,
					fournisseur_id,
					texte,
					montant_facture_article_ht,
					montant_facture_article_ttc,
					montant_facture_article_tva,
					montant_facture_port_ht,
					montant_facture_port_ttc,
					montant_facture_port_tva,
					montant_facture_ht,
					montant_facture_ttc,
					montant_facture_tva)
				SELECT 
					PINVOICED.NUM_0 AS code_original,
					CMMNUM_0 AS numero_facture,
					p_commandes.oid,
					date(BPRDATVCR_0) AS date_facture,
					date(GACCENTRY.ACCDAT_0) AS date_reglement,
					t_fournisseurs.oid,
					'Facture n°' || CMMNUM_0 as texte,
					SUM(AMTNOTLIN_0) AS montant_facture_article_ht,
					SUM(AMTATILIN_0) AS montant_facture_article_ttc,
					SUM(AMTTAXLIN1_0) AS montant_facture_article_tva,
					0::numeric,
					0::numeric,
					0::numeric,
					SUM(AMTNOTLIN_0) AS montant_facture_ht,
					SUM(AMTATILIN_0) AS montant_facture_tt,
					SUM(AMTTAXLIN1_0) AS montant_facture_tva
				FROM prod_sagex3.PINVOICED 
					JOIN eco.p_commandes ON POHNUM_0= p_commandes.code_original
					LEFT JOIN prod_sagex3.GACCENTRY ON GACCENTRY.NUM_0 = PINVOICED.NUM_0
					LEFT JOIN eco.t_fournisseurs ON t_fournisseurs.code_original = BPR_0
				WHERE PINVOICED.ACCDAT_0 >= '[ENV_ECO_ANNEEDEBUT]-01-01'
				GROUP BY 1,2,3,4,5,6,7
				ORDER BY 1;
			
				
				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_disable_index('eco', 'i_lignes_facture_1');
				SELECT base.cti_disable_index('eco', 'i_lignes_facture_2');
				SELECT base.cti_disable_index('eco', 'i_lignes_facture_3');
				SELECT base.cti_disable_index('eco', 'i_lignes_facture_4');
				
			
				TRUNCATE eco.p_lignes_facture;
				
				INSERT INTO eco.p_lignes_facture(
					article_id,
					code_original,
					compte_id,
					facture_id,
					ligne_commande,
					ligne_commande_id,
					montant_facture_article_ht,
					montant_facture_article_ttc,
					montant_facture_article_tva,
					montant_facture_port_ht,
					montant_facture_port_ttc,
					montant_facture_port_tva,
					montant_facture_ht,
					montant_facture_ttc,
					montant_facture_tva
					)
				SELECT 
					t_articles.oid,
					PINVOICED.NUM_0 || '-' || PINVOICED.PIDLIN_0 AS code_original,
					0 as compte_id,
					p_facture.oid,
					ligne_commande,
					p_lignes_commandes.oid,
					AMTNOTLIN_0 AS montant_facture_article_ht,
					AMTATILIN_0 AS montant_facture_article_ttc,
					AMTTAXLIN1_0 AS montant_facture_article_tva,
					0::numeric,
					0::numeric,
					0::numeric,
					AMTNOTLIN_0 AS montant_facture_ht,
					AMTATILIN_0 AS montant_facture_ttc,
					AMTTAXLIN1_0 AS montant_facture_tva
				FROM prod_sagex3.PINVOICED 
					JOIN eco.t_articles ON ITMREF_0 = t_articles.code_original
					JOIN eco.p_facture ON NUM_0 = p_facture.code_original
					JOIN eco.p_lignes_commandes ON POHNUM_0 || '-' || POPLIN_0 = p_lignes_commandes.code_original
					LEFT JOIN prod_sagex3.GACCENTRY ON GACCENTRY.NUM_0 = PINVOICED.NUM_0
				WHERE PINVOICED.ACCDAT_0 >= '[ENV_ECO_ANNEEDEBUT]-01-01'
				ORDER BY 2;
			
				SELECT base.cti_enable_index('eco', 'i_lignes_facture_1');
				SELECT base.cti_enable_index('eco', 'i_lignes_facture_2');
				SELECT base.cti_enable_index('eco', 'i_lignes_facture_3');
				SELECT base.cti_enable_index('eco', 'i_lignes_facture_4');
				
	
			]]></sqlcmd>
		</NODEx>
		
	</NODE>
	<NODE name="POST" label="POST-TRAITEMENTS">
		<sqlcmd><![CDATA[
			VACUUM ANALYSE eco.p_mouvements_articles;
			VACUUM ANALYSE eco.p_stock;
	
			SELECT eco.cti_update_mvt_qte_stock();
			SELECT eco.cti_update_mvt_mnt_stock();
		]]></sqlcmd>
	</NODE>
	
</ROOT>













