<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	<NODE name="INIT" label="INITIALISATIONS">
		<NODE label="Tables Articles">
			
			<sqlcmd><![CDATA[
			
				
				-- Alimentation des différents providers.
				INSERT INTO eco.t_providers(oid, code, texte)
					SELECT
						row_number() over () as oid,
						provider_code,
						provider_code
					FROM prod_cliwin.F_500
					WHERE provider_code NOT IN (SELECT code FROM eco.t_providers WHERE code IS NOT NULL)
					GROUP BY 2,3
				;

				-- Source des paramètres
				INSERT INTO eco.t_divers (code, texte, valeur, description)
				SELECT
					'CLIWIN_ARTCPT_SOURCE',
					'Champ source pour compte sur article',
					'SSFA1',
					'SSFA1, SSFA2'
				WHERE 'CLIWIN_ARTCPT_SOURCE' NOT IN (SELECT code FROM eco.t_divers)
                ;
				INSERT INTO eco.t_divers (code, texte, valeur, description)
				SELECT
					'CLIWIN_ARTFAM_SOURCE',
					'Champ source pour famille sur article',
					'FAMI2',
					'FAMI2, FAMI1'
				WHERE 'CLIWIN_ARTFAM_SOURCE' NOT IN (SELECT code FROM eco.t_divers)
                ;
				INSERT INTO eco.t_divers (code, texte, valeur, description)
				SELECT
					'CLIWIN_ARTSFA_SOURCE',
					'Champ source pour famille sur article',
					'SSFA2',
					'SSFA2, SSFA1'
				WHERE 'CLIWIN_ARTSFA_SOURCE' NOT IN (SELECT code FROM eco.t_divers)
                ;
			
			
				DROP TABLE IF EXISTS w_F200
				;
				CREATE TEMP TABLE w_F200 AS
					SELECT *,
						F20_SSFA1 AS compte_code_original,
						F20_UFAMI2 AS famille_code_original,
						F20_FAMI2 AS famille_code,
						F20_USSFA2 AS sous_famille_code_original,
						F20_SSFA2 AS sous_famille_code
					FROM prod_cliwin.F_200
				;
				

				DROP TABLE IF EXISTS w_F500;
				CREATE TEMP TABLE w_F500 AS
				SELECT *,
						F50_SSFA1 AS compte_code_original
				FROM prod_cliwin.F_500
				;

				
				DROP TABLE IF EXISTS w_F810;
				CREATE TEMP TABLE w_F810 AS
				SELECT *,
						F81_SSFA1 AS compte_code_original,
						F81_REFUCD AS ucd_code
				FROM prod_cliwin.F_810
				;

				UPDATE w_F200 SET
					compte_code_original = F20_SSFA2
				FROM eco.t_divers
				WHERE t_divers.code = 'CLIWIN_ARTCPT_SOURCE' AND 
					t_divers.valeur = 'SSFA2'
				;
				
				UPDATE w_F200 SET
					famille_code_original = F20_UFAMI1,
					famille_code = F20_FAMI1
				FROM eco.t_divers
				WHERE t_divers.code = 'CLIWIN_ARTFAM_SOURCE' AND 
					t_divers.valeur = 'FAMI1'
				;

				UPDATE w_F200 SET
					sous_famille_code_original = F20_USSFA1,
					sous_famille_code = F20_SSFA1
				FROM eco.t_divers
				WHERE t_divers.code = 'CLIWIN_ARTSFA_SOURCE' AND 
					t_divers.valeur = 'SSFA1'
				;

				UPDATE w_F500 SET
					compte_code_original = F50_SSFA2
				FROM eco.t_divers
				WHERE t_divers.code = 'CLIWIN_ARTCPT_SOURCE' AND 
					t_divers.valeur = 'SSFA2'
				;

				UPDATE w_F810 SET
					compte_code_original = F81_SSFA2
				FROM eco.t_divers
				WHERE t_divers.code = 'CLIWIN_ARTCPT_SOURCE' AND 
					t_divers.valeur = 'SSFA2'
				;
				
				UPDATE w_F810
				SET ucd_code = substr(ucd_code,6,7)
				WHERE ucd_code LIKE '34008%'
				;

				ANALYSE w_F200
				;

				ANALYSE w_F500
				;

				ANALYSE w_F810
				;
				
				-- Taux de TVA
				DROP TABLE IF EXISTS w_TVA;
				CREATE TEMP TABLE w_TVA AS 
				SELECT
					'1'::text AS codtva, '00010101'::date AS date_debut, '20991231'::date AS date_fin, 0.021::numeric AS taux
				UNION
				SELECT
					'2', '00010101', '20991231', 0.055
				UNION
				SELECT
					'3', '00010101', '20131231', 0.196
				UNION
				SELECT
					'3', '20140101', '20991231', 0.20
				UNION
				SELECT
					'4', '00010101', '20991231', 0.0
				UNION
				SELECT
					'5', '00010101', '20131231', 0.07
				UNION
				SELECT
					'5', '20140101', '20991231', 0.10
				;

				-- Articles qui n'existes pas (pb intégrité cliwin)
				INSERT INTO w_f200 (
					ARCLEUNIK, F20_LIBEL, 
					F20_FAMI1, F20_UFAMI1, F20_SSFA1, F20_USSFA1,
					F20_FAMI2, F20_UFAMI2, F20_SSFA2, F20_USSFA2,
					F20_CODTVA, F20_PRMPT
				)
				SELECT F_810.ARCLEUNIK, MAX(F81_LIBEL), 
					MAX(F81_FAMI1), MAX(F81_UFAMI1), MAX(F81_SSFA1), MAX(F81_USSFA1),
					MAX(F81_FAMI2), MAX(F81_UFAMI2), MAX(F81_SSFA2), MAX(F81_USSFA2),
					'3' AS F81_CODTVA, '*' AS F20_PRMPT
				FROM prod_cliwin.F_810
					LEFT JOIN w_f200 AS F_200 ON F_200.ARCLEUNIK = F_810.ARCLEUNIK 
				WHERE F_200.ARCLEUNIK IS NULL
				GROUP BY 1;
				
				
				-- ATC
				INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
				SELECT F180_CODE, MAX(F180_CODE), MAX(F180_LIBELLE), MAX(F180_LIBELLE) 
				FROM prod_CLIWIN.F_180
				LEFT JOIN eco.t_classification_atc ON code_original = F180_CODE
				WHERE code_original IS NULL 
				GROUP BY 1
				ORDER BY 1;
				
				-- Standard ATC
				SELECT eco.cti_reorganize_classification_atc();
			
			
				-- Comptes
				DROP TABLE IF EXISTS w_compte;
				CREATE TEMP TABLE w_compte AS 
				SELECT F40_CSSFA AS compte_code, 
					MAX(F40_LSSFA) AS compte_texte
				FROM prod_cliwin.F_400
				JOIN w_f200 AS F_200 ON F40_CSSFA = F_200.compte_code_original
				JOIN eco.t_divers ON t_divers.code = 'CLIWIN_ARTCPT_SOURCE' 
				WHERE 
					(
					F40_TYPCLA = '1' AND t_divers.valeur IS DISTINCT FROM 'SSFA2' OR
					F40_TYPCLA = '2' AND t_divers.valeur = 'SSFA2'
					)
				GROUP BY 1
				;
				
				INSERT INTO eco.t_compte(code, code_original, texte, texte_court)
				SELECT compte_code, compte_code, compte_texte, compte_texte
				FROM w_compte
				WHERE compte_code NOT IN (SELECT code FROM eco.t_compte WHERE code IS NOT NULL)
				ORDER BY 2
				;
				
				UPDATE eco.t_compte
				SET texte = compte_texte, texte_court = compte_texte
				FROM w_compte
				WHERE compte_code = t_compte.code AND
					(
					texte IS DISTINCT FROM compte_texte OR 
					texte_court IS DISTINCT FROM compte_texte
					)
				;
					
					
				-- Types d'articles
				INSERT INTO eco.t_types_articles(code, texte, texte_court, code_original)
				SELECT F10_CODE, F10_LIBEL, F10_LIBEL, F10_CODE
				FROM prod_cliwin.F_100
				JOIN w_f200 AS F_200 ON F10_CODE = F20_STOCK
				WHERE F10_TYPE = 'STOCK' AND
					F10_CODE NOT IN (SELECT code_original FROM eco.t_types_articles WHERE code_original IS NOT NULL)
				GROUP BY 1,2,3,4
				ORDER BY 2;
					
				-- Familles d'articles
				DROP TABLE IF EXISTS w_famille;
				CREATE TEMP TABLE w_famille AS 
				SELECT 
					FACLEUNIK AS famille_code_original,
					MAX(F40_CFAMI) AS famille_code, 
					MAX(F40_LFAMI) AS famille_texte
				FROM prod_cliwin.F_400
				JOIN w_f200 AS F_200 ON FACLEUNIK = F_200.famille_code_original
				JOIN eco.t_divers ON t_divers.code = 'CLIWIN_ARTFAM_SOURCE' 
				WHERE 
					(
					F40_TYPCLA = '2' AND t_divers.valeur IS DISTINCT FROM 'FAMI1' OR
					F40_TYPCLA = '1' AND t_divers.valeur = 'FAMI1'
					)
				GROUP BY 1
				;

				INSERT INTO eco.t_familles_articles( code_original, code, texte, texte_court)
				SELECT famille_code_original, famille_code, famille_texte, famille_texte
				FROM w_famille
				WHERE famille_code_original NOT IN (SELECT code_original FROM eco.t_familles_articles WHERE code_original IS NOT NULL)
				;

				UPDATE eco.t_familles_articles SET
					code = famille_code,
					texte = famille_texte,
					texte_court = famille_texte
				FROM w_famille
				WHERE famille_code_original = code_original AND
					(
					code IS DISTINCT FROM famille_code OR
					texte IS DISTINCT FROM famille_texte OR
					texte_court IS DISTINCT FROM famille_texte
					)
				;
					

				DROP TABLE IF EXISTS w_sous_famille;
				CREATE TEMP TABLE w_sous_famille AS 
				SELECT 
					FACLEUNIK AS sous_famille_code_original,
					MAX(F40_CSSFA) AS sous_famille_code, 
					MAX(F40_LSSFA) AS sous_famille_texte
				FROM prod_cliwin.F_400
				JOIN w_f200 AS F_200 ON FACLEUNIK = F_200.sous_famille_code_original
				JOIN eco.t_divers ON t_divers.code = 'CLIWIN_ARTSFA_SOURCE' 
				WHERE 
					(
					F40_TYPCLA = '2' AND t_divers.valeur IS DISTINCT FROM 'SSFA1' OR
					F40_TYPCLA = '1' AND t_divers.valeur = 'SSFA1'
					)
				GROUP BY 1
				;

				INSERT INTO eco.t_sous_familles_articles( code_original, code, texte, texte_court)
				SELECT sous_famille_code_original, sous_famille_code, sous_famille_texte, sous_famille_texte
				FROM w_sous_famille
				WHERE sous_famille_code_original NOT IN (SELECT code_original FROM eco.t_sous_familles_articles WHERE code_original IS NOT NULL)
				;

				UPDATE eco.t_sous_familles_articles SET
					code = sous_famille_code,
					texte = sous_famille_texte,
					texte_court = sous_famille_texte
				FROM w_sous_famille
				WHERE sous_famille_code_original = code_original AND
					(
					code IS DISTINCT FROM sous_famille_code OR
					texte IS DISTINCT FROM sous_famille_texte OR
					texte_court IS DISTINCT FROM sous_famille_texte
					)
				;

				-- Articles
				DROP TABLE IF EXISTS w_200;
				CREATE TEMP TABLE w_200 AS
				SELECT ARCLEUNIK::text AS w20_CODE, F20_LIBEL AS w20_LIBEL, F_200.*
				FROM w_f200 AS F_200;

				-- UPDATE w_200 SET w20_LIBEL = replace(w20_LIBEL, '.', '') WHERE W20_LIBEL LIKE '%.%';
				-- UPDATE w_200 SET w20_LIBEL = replace(w20_LIBEL, ',', '') WHERE W20_LIBEL LIKE '%,%';
				-- UPDATE w_200 SET w20_LIBEL = replace(w20_LIBEL, '/', '') WHERE W20_LIBEL LIKE '%/%';
				-- UPDATE w_200 SET w20_LIBEL = replace(w20_LIBEL, '%', '') ;
				-- UPDATE w_200 SET w20_LIBEL = replace(w20_LIBEL, '*', '') WHERE W20_LIBEL LIKE '%*%';
				-- UPDATE w_200 SET w20_LIBEL = replace(w20_LIBEL, '(', ' ') WHERE W20_LIBEL LIKE '%(%';
				-- UPDATE w_200 SET w20_LIBEL = replace(w20_LIBEL, ')', ' ') WHERE W20_LIBEL LIKE '%)%';
				-- UPDATE w_200 SET w20_LIBEL = replace(w20_LIBEL, '+', ' ') WHERE W20_LIBEL LIKE '%+%';
				-- UPDATE w_200 SET w20_LIBEL = replace(w20_LIBEL, '''', '') WHERE W20_LIBEL LIKE '%''%';
				-- UPDATE w_200 SET w20_LIBEL = replace(w20_LIBEL, '-', ' ') WHERE W20_LIBEL LIKE '%-%';
				-- UPDATE w_200 SET w20_LIBEL = replace(w20_LIBEL, ' A ', ' ') WHERE W20_LIBEL LIKE '% A %';
				-- UPDATE w_200 SET w20_LIBEL = replace(w20_LIBEL, ' DE ', ' ') WHERE W20_LIBEL LIKE '% DE %';
				-- UPDATE w_200 SET w20_LIBEL = replace(w20_LIBEL, '  ', ' ') WHERE W20_LIBEL LIKE '%  %';
				-- UPDATE w_200 SET w20_LIBEL = replace(w20_LIBEL, '  ', ' ') WHERE W20_LIBEL LIKE '%  %';
				-- UPDATE w_200 SET w20_LIBEL = replace(w20_LIBEL, '  ', ' ') WHERE W20_LIBEL LIKE '%  %';
				-- UPDATE w_200 SET w20_LIBEL = replace(w20_LIBEL, '  ', ' ') WHERE W20_LIBEL LIKE '%  %';

				-- UPDATE w_200
				-- SET W20_CODE =
				-- 	trim(substr(split_part(w20_LIBEL,' ',1),1,4)) ||
				-- 	trim(substr(split_part(w20_LIBEL,' ',2),1,1)) ||
				-- 	trim(substr(split_part(w20_LIBEL,' ',3),1,1)) ||
				-- 	ARCLEUNIK::text;


				INSERT INTO eco.t_articles(code, texte, texte_court, code_original)
				SELECT ARCLEUNIK::text, F20_LIBEL, F20_LIBEL, ARCLEUNIK::text
				FROM W_200 
					LEFT JOIN eco.t_articles ON (code_original = ARCLEUNIK::text)
				WHERE code_original IS NULL;
				
				UPDATE eco.t_articles
				SET 
					code = w20_CODE,
					texte = F20_LIBEL,
					texte_court = F20_LIBEL
				FROM w_200
				WHERE code_original = ARCLEUNIK::text
				AND (code IS DISTINCT FROM w20_CODE OR
					texte IS DISTINCT FROM F20_LIBEL OR
					texte_court IS DISTINCT FROM F20_LIBEL);


				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 = 0,
					sous_categorie_id = 0,
					unite_stockage_id = 0,
					unite_distribution_id = 0
				FROM w_200
					LEFT JOIN eco.t_familles_articles ON famille_code_original = t_familles_articles.code_original
					LEFT JOIN eco.t_sous_familles_articles ON sous_famille_code_original::text = t_sous_familles_articles.code_original
					LEFT JOIN eco.t_types_articles ON F20_STOCK::text = t_types_articles.code_original
				WHERE 
					t_articles.code_original = ARCLEUNIK 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 0 OR
					t_articles.sous_categorie_id IS DISTINCT FROM 0 OR
					t_articles.unite_stockage_id IS DISTINCT FROM 0 OR
					t_articles.unite_distribution_id IS DISTINCT FROM 0
					);
					
					
				UPDATE eco.t_articles SET
					compte_id = COALESCE(t_compte.oid,0),
					taux_tva_en_cours = COALESCE(w_TVA.taux*100,0)
				FROM W_200
					LEFT JOIN eco.t_compte ON W_200.compte_code_original = t_compte.code_original
					LEFT JOIN w_TVA ON F20_CODTVA = w_TVA.codtva AND date(now()) BETWEEN w_TVA.date_debut AND w_TVA.date_fin
				WHERE 
					t_articles.code_original = ARCLEUNIK AND
					(
					t_articles.compte_id IS DISTINCT FROM COALESCE(t_compte.oid,0) OR
					taux_tva_en_cours IS DISTINCT FROM COALESCE(w_TVA.taux*100,0)
					)
				;

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



				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
					);


				-- Référence article fournisseur
				DROP TABLE IF EXISTS w_articles;
				CREATE TEMP TABLE w_articles AS
				SELECT t_articles.oid AS article_id,
					t_articles.code AS article_code,
					base.cti_group_array3(COALESCE(fournisseur_id,0))::bigint[] AS ref_fournisseur_id,
					base.cti_group_array3(COALESCE(AFLIFA,''))::text[] AS ref_fournisseur_texte
				FROM eco.t_articles 
					LEFT JOIN (
						SELECT t_articles.oid AS article_id, 
							t_fournisseurs.oid AS fournisseur_id,
							t_articles.texte || (MAX(ARRAY[F21_DEBUT::text,CASE WHEN F21_REFER <> '' AND F21_REFER <> t_articles.texte THEN ' (' || F21_REFER || ')' ELSE '' END]))[2] AS AFLIFA
						FROM prod_cliwin.F_210
						JOIN eco.t_articles ON ARCLEUNIK::text = t_articles.code_original
						JOIN eco.t_fournisseurs ON F21_IDDIST::text = t_fournisseurs.code_original
						WHERE ARCLEUNIK <> 0
						GROUP BY 1,2
						) subview ON subview.article_id = t_articles.oid
				GROUP BY 1,2
				ORDER BY 1;

				UPDATE eco.t_articles
				SET 
					ref_fournisseur_id = w_articles.ref_fournisseur_id,
					ref_fournisseur_texte = w_articles.ref_fournisseur_texte
				FROM w_articles
				WHERE w_articles.article_id = t_articles.oid AND
					(
						t_articles.ref_fournisseur_id IS DISTINCT FROM w_articles.ref_fournisseur_id OR
						t_articles.ref_fournisseur_texte IS DISTINCT FROM w_articles.ref_fournisseur_texte
					);
			]]></sqlcmd>
		</NODE>
		<NODE label="Tables Fournisseurs">
			<sqlcmd><![CDATA[
			
			-- Types d'fournisseurs
			INSERT INTO eco.t_types_fournisseurs(code, texte, texte_court, code_original)
			SELECT F10_CODE, F10_LIBEL, F10_LIBEL, F10_CODE
			FROM prod_cliwin.F_100
			JOIN prod_cliwin.F_300 ON F10_CODE = F30_NATUR
			WHERE F10_TYPE = 'NATUR' AND
				F10_CODE NOT IN (SELECT code_original FROM eco.t_types_fournisseurs WHERE code_original IS NOT NULL)
			GROUP BY 1,2,3,4
			ORDER BY 2;
				
			-- Fournisseurs
			
			DROP TABLE IF EXISTS w_300;
			CREATE TEMP TABLE w_300 AS
			SELECT FOCLEUNIK::text AS w30_CODE, F30_LIBEL AS w30_LIBEL, F_300.*
			FROM prod_cliwin.F_300;

			-- UPDATE w_300 SET w30_LIBEL = replace(w30_LIBEL, '.', '') WHERE W30_LIBEL LIKE '%.%';
			-- UPDATE w_300 SET w30_LIBEL = replace(w30_LIBEL, ',', '') WHERE W30_LIBEL LIKE '%,%';
			-- UPDATE w_300 SET w30_LIBEL = replace(w30_LIBEL, '/', '') WHERE W30_LIBEL LIKE '%/%';
			-- UPDATE w_300 SET w30_LIBEL = replace(w30_LIBEL, '%', '') ;
			-- UPDATE w_300 SET w30_LIBEL = replace(w30_LIBEL, '*', '') WHERE W30_LIBEL LIKE '%*%';
			-- UPDATE w_300 SET w30_LIBEL = replace(w30_LIBEL, '(', ' ') WHERE W30_LIBEL LIKE '%(%';
			-- UPDATE w_300 SET w30_LIBEL = replace(w30_LIBEL, ')', ' ') WHERE W30_LIBEL LIKE '%)%';
			-- UPDATE w_300 SET w30_LIBEL = replace(w30_LIBEL, '+', ' ') WHERE W30_LIBEL LIKE '%+%';
			-- UPDATE w_300 SET w30_LIBEL = replace(w30_LIBEL, '''', '') WHERE W30_LIBEL LIKE '%''%';
			-- UPDATE w_300 SET w30_LIBEL = replace(w30_LIBEL, '-', ' ') WHERE W30_LIBEL LIKE '%-%';
			-- UPDATE w_300 SET w30_LIBEL = replace(w30_LIBEL, ' A ', ' ') WHERE W30_LIBEL LIKE '% A %';
			-- UPDATE w_300 SET w30_LIBEL = replace(w30_LIBEL, ' DE ', ' ') WHERE W30_LIBEL LIKE '% DE %';
			-- UPDATE w_300 SET w30_LIBEL = replace(w30_LIBEL, '  ', ' ') WHERE W30_LIBEL LIKE '%  %';
			-- UPDATE w_300 SET w30_LIBEL = replace(w30_LIBEL, '  ', ' ') WHERE W30_LIBEL LIKE '%  %';
			-- UPDATE w_300 SET w30_LIBEL = replace(w30_LIBEL, '  ', ' ') WHERE W30_LIBEL LIKE '%  %';
			-- UPDATE w_300 SET w30_LIBEL = replace(w30_LIBEL, '  ', ' ') WHERE W30_LIBEL LIKE '%  %';

			-- UPDATE w_300
			-- SET W30_CODE =
			-- 	trim(substr(split_part(w30_LIBEL,' ',1),1,4)) ||
			-- 	trim(substr(split_part(w30_LIBEL,' ',2),1,1)) ||
			-- 	trim(substr(split_part(w30_LIBEL,' ',3),1,1)) ||
			-- 	FOCLEUNIK::text;


			INSERT INTO eco.t_fournisseurs(code, texte, texte_court, code_original)
			SELECT w30_code, F30_LIBEL, F30_LIBEL, FOCLEUNIK::text
			FROM w_300
				LEFT JOIN eco.t_fournisseurs ON (code_original = FOCLEUNIK::text)
			WHERE code_original IS NULL;


			UPDATE eco.t_fournisseurs
			SET code = W30_CODE,
				texte = F30_LIBEL,
				texte_court = F30_LIBEL,
				adresse = trim(F30_ADR1 || ' ' || F30_ADR2),
				code_postal_id = COALESCE(t_codes_postaux.oid,0),
				ville = F30_VILLE,
				telephone = F30_TEL1,
				fax = F30_FAX1,
				siret = F30_SIRET,
				code_comptable = F30_COMPTA
			FROM w_300
			LEFT JOIN base.t_codes_postaux ON F30_CPOST = t_codes_postaux.code
			WHERE code_original = FOCLEUNIK::text AND
				(
				t_fournisseurs.code IS DISTINCT FROM w30_CODE OR
				t_fournisseurs.texte IS DISTINCT FROM F30_LIBEL OR
				t_fournisseurs.texte_court IS DISTINCT FROM F30_LIBEL OR
				adresse IS DISTINCT FROM trim(F30_ADR1 || ' ' || F30_ADR2) OR
				code_postal_id IS DISTINCT FROM COALESCE(t_codes_postaux.oid,0) OR
				ville IS DISTINCT FROM F30_VILLE OR
				telephone IS DISTINCT FROM F30_TEL1 OR
				fax IS DISTINCT FROM F30_FAX1 OR
				siret IS DISTINCT FROM F30_SIRET OR
				code_comptable IS DISTINCT FROM F30_COMPTA
				)
			;

				
			UPDATE eco.t_fournisseurs SET
				type_id = COALESCE(t_types_fournisseurs.oid,0)
			FROM w_300
				LEFT JOIN eco.t_types_fournisseurs ON F30_NATUR = t_types_fournisseurs.code_original
			WHERE 1=1
				AND t_fournisseurs.code_original = FOCLEUNIK::text
				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
				);
				
			-- Article fournisseur
			DROP TABLE IF EXISTS w_article_fournisseur_eco
			;
			CREATE TEMP TABLE w_article_fournisseur_eco AS
				SELECT
					F_200.ARCLEUNIK::text || '|' ||F_300.focleunik AS code_original,
					t_articles.oid AS article_id,
					t_fournisseurs.oid AS fournisseur_id,
					t_fournisseurs.oid AS fournisseur_distributeur_id,
					max(coalesce(t_ucd.oid, 0::bigint)) AS ucd_id,
					max(coalesce(t_lpp.oid, 0::bigint)) AS lpp_id,
					max(f21_refer) AS code_reference_fournisseur,
					max(f21_refer) AS texte_reference_fournisseur,
					max(substr(ucd13, 6, 7)) AS ucd_code,
					max(F20_CODTIPS) AS lpp_code,
					max(ucd13) AS code_ucd13,
					''::text AS code_cip,
					''::text AS code_cahpp,
					0::bigint AS unite_approvisionnement_id,
					1 AS nombre_conditionnement_approvisionnement,
					coalesce(max(f_210.f21_prix), 0::numeric) AS prix_unitaire_en_cours
				FROM prod_cliwin.F_200
					JOIN prod_cliwin.F_210 ON F_210.ARCLEUNIK = F_200.ARCLEUNIK
					-- JOIN prod_cliwin.F_810 ON F_810.ARCLEUNIK = F_200.ARCLEUNIK
					JOIN prod_cliwin.F_300 ON F_300.focleunik = F_210.f21_iddist
					JOIN eco.t_articles ON t_articles.code_original = F_210.ARCLEUNIK::text
					JOIN eco.t_fournisseurs ON t_fournisseurs.code_original = F_210.f21_iddist
					LEFT JOIN base.t_ucd ON t_ucd.code = substr(ucd13, 6, 7) -- F81_REFUCD
					LEFT JOIN base.t_lpp ON t_lpp.code = F20_CODTIPS
				group by 1,2,3,4 --,5,6 --,7,8,9,10,11,12,13,14,15,16
			;

			INSERT INTO eco.t_article_fournisseur (
					code_original,
					article_id,
					fournisseur_id,
					code_reference_fournisseur,
					texte_reference_fournisseur,
					code_cip,
					ucd_id,
					code_ucd13,
					lpp_id,
					code_cahpp,
					unite_approvisionnement_id,
					nombre_conditionnement_approvisionnement,
					prix_unitaire_en_cours)
				SELECT 
					w_article_fournisseur_eco.code_original,
					w_article_fournisseur_eco.article_id, 
					w_article_fournisseur_eco.fournisseur_id,
					w_article_fournisseur_eco.code_reference_fournisseur,
					w_article_fournisseur_eco.texte_reference_fournisseur,
					w_article_fournisseur_eco.code_cip,
					w_article_fournisseur_eco.ucd_id,
					w_article_fournisseur_eco.code_ucd13,
					w_article_fournisseur_eco.lpp_id,
					w_article_fournisseur_eco.code_cahpp,
					w_article_fournisseur_eco.unite_approvisionnement_id,
					w_article_fournisseur_eco.nombre_conditionnement_approvisionnement,
					w_article_fournisseur_eco.prix_unitaire_en_cours
				FROM w_article_fournisseur_eco
					LEFT JOIN eco.t_article_fournisseur ON t_article_fournisseur.code_original = w_article_fournisseur_eco.code_original
				WHERE t_article_fournisseur.code_original IS NULL
			;

			UPDATE eco.t_article_fournisseur SET
				article_id = w_article_fournisseur_eco.article_id,
				fournisseur_id = w_article_fournisseur_eco.fournisseur_id,
				code_reference_fournisseur = w_article_fournisseur_eco.code_reference_fournisseur,
				texte_reference_fournisseur = w_article_fournisseur_eco.texte_reference_fournisseur,
				code_cip = w_article_fournisseur_eco.code_cip,
				code_ucd13 = w_article_fournisseur_eco.code_ucd13,
				ucd_id = w_article_fournisseur_eco.ucd_id,
				lpp_id = w_article_fournisseur_eco.lpp_id,
				code_cahpp = w_article_fournisseur_eco.code_cahpp,
				unite_approvisionnement_id = w_article_fournisseur_eco.unite_approvisionnement_id,
				nombre_conditionnement_approvisionnement = w_article_fournisseur_eco.nombre_conditionnement_approvisionnement,
				prix_unitaire_en_cours = w_article_fournisseur_eco.prix_unitaire_en_cours
			FROM w_article_fournisseur_eco
			WHERE 1=1
				AND t_article_fournisseur.code_original = w_article_fournisseur_eco.code_original
				AND (1!=1
					OR t_article_fournisseur.article_id IS DISTINCT FROM w_article_fournisseur_eco.article_id
					OR t_article_fournisseur.fournisseur_id IS DISTINCT FROM w_article_fournisseur_eco.fournisseur_id
					OR t_article_fournisseur.code_reference_fournisseur IS DISTINCT FROM w_article_fournisseur_eco.code_reference_fournisseur
					OR t_article_fournisseur.texte_reference_fournisseur IS DISTINCT FROM w_article_fournisseur_eco.texte_reference_fournisseur
					OR t_article_fournisseur.code_cip IS DISTINCT FROM w_article_fournisseur_eco.code_cip
					OR t_article_fournisseur.code_ucd13 IS DISTINCT FROM w_article_fournisseur_eco.code_ucd13
					OR t_article_fournisseur.ucd_id IS DISTINCT FROM w_article_fournisseur_eco.ucd_id
					OR t_article_fournisseur.lpp_id IS DISTINCT FROM w_article_fournisseur_eco.lpp_id
					OR t_article_fournisseur.code_cahpp IS DISTINCT FROM w_article_fournisseur_eco.code_cahpp
					OR t_article_fournisseur.unite_approvisionnement_id IS DISTINCT FROM w_article_fournisseur_eco.unite_approvisionnement_id
					OR t_article_fournisseur.nombre_conditionnement_approvisionnement IS DISTINCT FROM w_article_fournisseur_eco.nombre_conditionnement_approvisionnement
					OR t_article_fournisseur.prix_unitaire_en_cours IS DISTINCT FROM w_article_fournisseur_eco.prix_unitaire_en_cours)
			;

			]]></sqlcmd>
		</NODE>
		<NODE label="Autres tables">
			<sqlcmd><![CDATA[
			
			-- Lieux
			INSERT INTO eco.t_lieux(code_original, code, texte, texte_court)
			SELECT F50_STOCK, F50_STOCK, F50_STOCK , F50_STOCK
			FROM prod_cliwin.F_500
				LEFT JOIN eco.t_lieux ON (code_original = F50_STOCK)
			WHERE F50_STOCK <> '' AND
				code_original IS NULL
			GROUP BY 1,2,3,4
			;
			
			-- Gestionnaires
			
			
			
			
						
			
			-- Unités fonctionnelles
			
			DROP TABLE IF EXISTS w_130;
			CREATE TEMP TABLE w_130 AS
			SELECT F50_DEPT || '-' || F50_SERVI AS F13_code, 
				MAX(COALESCE(F_130_DEPT.F13_LIBEL1, 'Département ' || F50_DEPT) || ', ' || COALESCE(F_130_SERVI.F13_LIBEL2,'Service ' || F50_SERVI)) AS F13_texte
			FROM prod_cliwin.F_500
				LEFT JOIN prod_cliwin.F_130 F_130_DEPT ON
					F50_DEPT = F_130_DEPT.F13_CODE1 AND COALESCE(F_130_DEPT.F13_CODE2,'') = ''
				LEFT JOIN prod_cliwin.F_130 F_130_SERVI ON
					F50_DEPT = F_130_SERVI.F13_CODE1 AND F50_SERVI = F_130_SERVI.F13_CODE2
			WHERE F50_DEPT <> '' AND F50_DEPT IS NOT NULL
			GROUP BY 1
			ORDER BY 1;
			
			INSERT INTO eco.t_unites_fonctionnelles(code, texte, texte_court, code_original)
			SELECT F13_code,
				F13_texte,
				substr(F13_texte,1,50),
				F13_code
			FROM w_130
				LEFT JOIN eco.t_unites_fonctionnelles ON code_original = F13_code
			WHERE code_original IS NULL 
			GROUP BY 1,2
			ORDER BY 1;


			UPDATE eco.t_unites_fonctionnelles SET
				texte = F13_texte,
				texte_court = substr(F13_texte,1,50)
			FROM w_130
			WHERE code_original = F13_code AND
				(
				texte IS DISTINCT FROM F13_texte OR
				texte_court IS DISTINCT FROM substr(F13_texte,1,50)
				);
			
			
				
			-- Site

			
			
			-- Types mouvements

			INSERT INTO eco.t_types_mouvements(code, texte, texte_court, code_original)
			SELECT F50_TYPE, F50_TYPE, F50_TYPE, F50_TYPE
			FROM prod_cliwin.F_500
				LEFT JOIN eco.t_types_mouvements ON (code_original = F50_TYPE)
			WHERE F50_TYPE <> '' AND t_types_mouvements.oid IS NULL
			GROUP BY 1;
			
			

			]]></sqlcmd>
		</NODE>
	</NODE>
	<NODE name="PROD" label="RECUPERATION DES DONNEES DE PRODUCTION">
		<NODE label="Préparation">
			<sqlcmd><![CDATA[
			
				
				-- Date de fin dans table F_210 (articles/fournisseurs)
				DROP SEQUENCE IF EXISTS w_210_sequence;
				CREATE TEMP SEQUENCE w_210_sequence;

				DROP TABLE IF EXISTS w_210;
				CREATE TEMP TABLE w_210 AS
				SELECT nextval('w_210_sequence'::regclass) AS sequence, *
				FROM (
					SELECT ARCLEUNIK, F21_IDDIST, F21_DEBUT, F21_FIN, LICLEUNIK, F21_UCD, F21_PRIX
					FROM prod_cliwin.F_210
					ORDER BY 1,2,3
					) subview;
					
				UPDATE w_210
				SET 
					F21_FIN = w_210_2.F21_DEBUT - interval '1 day'
				FROM w_210 w_210_2
				WHERE w_210.ARCLEUNIK = w_210_2.ARCLEUNIK AND
					w_210.F21_IDDIST = w_210_2.F21_IDDIST AND
					w_210.sequence = w_210_2.sequence -1; 

				UPDATE w_210 SET
					F21_FIN = COALESCE(F21_FIN, '20991231')
				;
				
				-- UCD par article
				DROP SEQUENCE IF EXISTS w_210_ucd_sequence;
				CREATE TEMP SEQUENCE w_210_ucd_sequence;

				DROP TABLE IF EXISTS w_210_ucd;
				CREATE TEMP TABLE w_210_ucd AS	 
				SELECT  nextval('w_210_ucd_sequence'::regclass) AS sequence, *
				FROM (
					SELECT F_210.ARCLEUNIK,
						F21_UCD,
						t_ucd.oid AS ucd_id,
						MIN(COALESCE(F50_DATE,'00010101')) AS date_debut,
						'20991231'::date AS date_fin
					FROM w_210 as F_210
					JOIN base.t_ucd ON F21_UCD = t_ucd.code
					LEFT JOIN prod_cliwin.F_500 ON F_210.ARCLEUNIK = F_500.ARCLEUNIK AND F_210.F21_IDDIST = F_500.FOCLEUNIK AND
						F50_TYPE IN ('E', 'R')
					WHERE base.cti_is_num(F21_UCD) AND 
						F21_UCD <> ''
					GROUP BY 1,2,3
					ORDER BY 1,4,2
					) subview;

				UPDATE w_210_ucd
				SET date_fin = w_210_ucd_2.date_debut - interval '1 day'
				FROM w_210_ucd w_210_ucd_2
				WHERE w_210_ucd.ARCLEUNIK = w_210_ucd_2.ARCLEUNIK AND
					w_210_ucd.sequence = w_210_ucd_2.sequence -1;



				DROP SEQUENCE IF EXISTS w_210_fournisseur_sequence;
				CREATE TEMP SEQUENCE w_210_fournisseur_sequence;

				DROP TABLE IF EXISTS w_210_fournisseur;
				CREATE TEMP TABLE w_210_fournisseur AS	 
				SELECT  nextval('w_210_fournisseur_sequence'::regclass) AS sequence, *
				FROM (
					SELECT F_210.ARCLEUNIK,
						F21_IDDIST,
						t_fournisseurs.oid AS fournisseur_id,
						MIN(COALESCE(F50_DATE,'00010101')) AS date_debut,
						'20991231'::date AS date_fin
					FROM w_210 as F_210
					JOIN eco.t_fournisseurs ON F21_IDDIST = t_fournisseurs.code_original
					LEFT JOIN prod_cliwin.F_500 ON F_210.ARCLEUNIK = F_500.ARCLEUNIK AND F_210.F21_IDDIST = F_500.FOCLEUNIK AND
						F50_TYPE IN ('E', 'R') AND 
						F_500.FOCLEUNIK <> 0
					GROUP BY 1,2,3
					ORDER BY 1,4,2
					) subview;


				UPDATE w_210_fournisseur
				SET date_fin = w_210_fournisseur_2.date_debut - interval '1 day'
				FROM w_210_fournisseur w_210_fournisseur_2
				WHERE w_210_fournisseur.ARCLEUNIK = w_210_fournisseur_2.ARCLEUNIK AND
					w_210_fournisseur.sequence = w_210_fournisseur_2.sequence -1;

			
			]]></sqlcmd>
		</NODE>
		<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(
					provider_id,
					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
					coalesce(t_providers.oid, 0::bigint) AS provider_id,
					C1CLEUNIK::text,
					F80_NOCOM AS numero,
					F80_DACOM AS date_commande,
					date(F80_DACOM  + interval '1 month' - interval '1 day') AS date_engagement,
					''::text AS objet,
					''::text AS reference,
					'20991231'::date 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,
					0::bigint AS gestionnaire_id,
					0::bigint AS lieu_commande_id,
					0::bigint AS lieu_facturation_id,
					0::bigint AS lieu_livraison_id,
					0::bigint AS unite_fonctionnelle_id,
					F80_TOTHT AS montant_commande_ht,
					0::numeric AS montant_commande_remise,
					F80_TOTTC - F80_TOTHT AS montant_commande_tva,
					0::numeric AS montant_commande_port,
					F80_TOTTC AS montant_commande_total,
					F80_TOTHT AS montant_liquidation_ht,
					0::numeric AS montant_liquidation_remise,
					F80_TOTTC - F80_TOTHT AS montant_liquidation_tva,
					0::numeric AS montant_liquidation_port,
					F80_TOTTC AS montant_liquidation_total
				FROM prod_cliwin.F_800
					LEFT JOIN eco.t_fournisseurs ON F80_IDDIS::text = t_fournisseurs.code_original
					LEFT JOIN eco.t_providers on t_providers.code = F_800.provider_code
				WHERE F80_DACOM >= '[ENV_ECO_ANNEEDEBUT]0101';
				
				
				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');
				
				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)
				SELECT
					C2CLEUNIK::text AS code_original,
					p_commandes.oid AS commande_id,
					0::numeric AS ligne_commande,
					F81_LIBEL AS texte,
					COALESCE(t_articles.oid, 0) AS article_id,
					0::bigint AS unite_approvisionnement_id,
					COALESCE(t_compte.oid, t_articles.compte_id, 0) AS 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,
					
					F81_QTECOM * CASE WHEN F80_TOTHT >= 0 THEN 1 ELSE -1 END AS quantite_approvisionnement,
					F81_PRICOM AS prix_unitaire_approvisionnement,
					0::numeric AS multiplicateur_stockage,
					0::numeric AS quantite_stockage,
					0::numeric AS prix_unitaire_stockage,

					(F81_PRICOM * F81_QTECOM) * CASE WHEN F80_TOTHT >= 0 THEN 1 ELSE -1 END AS montant_commande_ht,
					(round(F81_PRICOM * (F81_TXTVAI/100),5) * F81_QTECOM) * CASE WHEN F80_TOTHT >= 0 THEN 1 ELSE -1 END::numeric AS montant_commande_remise,
					round(F81_PRICOM * (1 - F81_TXTVAI/100),5) * F81_QTECOM * COALESCE(w_TVA.taux,0) * CASE WHEN F80_TOTHT >= 0 THEN 1 ELSE -1 END AS montant_commande_tva,
					0::numeric AS montant_commande_port,
					round(F81_PRICOM * (1 - F81_TXTVAI/100),5) * F81_QTECOM + round(F81_PRICOM * (1 - F81_TXTVAI/100),5) * F81_QTECOM * COALESCE(w_TVA.taux,0) * CASE WHEN F80_TOTHT >= 0 THEN 1 ELSE -1 END AS montant_commande_ttc,

					F81_QTELIV * CASE WHEN F80_TOTHT >= 0 THEN 1 ELSE -1 END AS quantite_livraison_approvisionnement,
					F81_QTELIV * CASE WHEN F80_TOTHT >= 0 THEN 1 ELSE -1 END AS quantite_livraison_stockage,
					round(F81_PRICOM * (1 - F81_TXTVAI/100),5) * F81_QTELIV * CASE WHEN F80_TOTHT >= 0 THEN 1 ELSE -1 END AS montant_livraison_ht,
					round(F81_PRICOM * (1 - F81_TXTVAI/100),5) * F81_QTELIV + round(F81_PRICOM * (1 - F81_TXTVAI/100),5) * F81_QTELIV * COALESCE(w_TVA.taux,0) * CASE WHEN F80_TOTHT >= 0 THEN 1 ELSE -1 END AS montant_livraison_ttc,
					CASE WHEN F81_QTELIV = F81_QTECOM THEN 'T' WHEN F81_QTELIV > F81_QTECOM THEN 'S' WHEN F81_QTELIV = 0 THEN 'N' ELSE 'P' END AS etat_livraison,

					(F81_PRIFAC * F81_QTEFAC) * CASE WHEN F80_TOTHT >= 0 THEN 1 ELSE -1 END AS montant_liquidation_ht,
					round(F81_PRIFAC * (F81_TXTVAI/100),5) * F81_QTEFAC AS montant_liquidation_remise,
					round(F81_PRIFAC * (1- F81_TXTVAI/100),5) * F81_QTEFAC * COALESCE(w_TVA.taux,0) * CASE WHEN F80_TOTHT >= 0 THEN 1 ELSE -1 END AS montant_liquidation_tva,
					0::numeric AS montant_liquidation_port,
					round(F81_PRIFAC * (1- F81_TXTVAI/100),5) * F81_QTEFAC + round(F81_PRIFAC * (1- F81_TXTVAI/100),5) * F81_QTEFAC * COALESCE(w_TVA.taux,0) * CASE WHEN F80_TOTHT >= 0 THEN 1 ELSE -1 END AS montant_liquidation_ttc,
					CASE WHEN F81_FACSOL = 'O' THEN '1' ELSE '0' END AS etat_liquidation
				FROM w_F810 F_810
					JOIN prod_cliwin.F_800 ON F80_NOCOM = F81_NOCOM AND F80_DACOM = F81_DACOM
					JOIN eco.p_commandes ON C1CLEUNIK::text = p_commandes.code_original
					JOIN w_f200 AS F_200 ON F_200.ARCLEUNIK = F_810.ARCLEUNIK 
					LEFT JOIN w_TVA ON F20_CODTVA = w_TVA.codtva AND date(F80_DACOM) BETWEEN w_TVA.date_debut AND w_TVA.date_fin
					-- LEFT JOIN w_210 as F_210 ON F_210.LICLEUNIK = F_810.LICLEUNIK
					LEFT JOIN eco.t_articles ON F_810.ARCLEUNIK::text = t_articles.code_original AND t_articles.oid <> 0
					LEFT JOIN eco.t_compte ON F_810.compte_code_original = t_compte.code_original AND t_compte.oid <> 0
					LEFT JOIN base.t_ucd ON ucd_code = t_ucd.code AND t_ucd.oid <> 0
					LEFT JOIN base.t_lpp ON F20_CODTIPS = t_lpp.code
				WHERE F81_DACOM >= '[ENV_ECO_ANNEEDEBUT]0101';
				
				
				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="Réapprovisionnement DMI">
			<sqlcmd><![CDATA[
				-- écrasement des quantite (voir pourquoi avec qualif)
				DROP TABLE IF EXISTS w_R500
				;

				CREATE TEMP TABLE w_R500 AS
					select *
					from prod_cliwin.F_R500
				;

				UPDATE w_R500 SET
					FR5_QTE = 1
				;
			
				INSERT INTO eco.p_commandes(
					provider_id,
					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
					coalesce(t_providers.oid, 0::bigint) AS provider_id,
					FR5_NUMDEM::text,
					FR5_NUMDEM AS numero,
					MAX(FR5_DDEMAN) AS date_commande,
					date(MAX(FR5_DATE)  + interval '1 month' - interval '1 day') AS date_engagement,
					''::text AS objet,
					''::text AS reference,
					MAX(FR5_DATE) AS date_livraison_prevue,
					'T' AS etat_reception,
					MAX(FR5_DAREN)::date AS date_reception,
					'T' AS etat_liquidation,
					COALESCE(MIN(t_fournisseurs.oid),0) AS fournisseur_id,
					0::bigint AS gestionnaire_id,
					0::bigint AS lieu_commande_id,
					0::bigint AS lieu_facturation_id,
					0::bigint AS lieu_livraison_id,
					0::bigint AS unite_fonctionnelle_id,
					SUM(FR5_QTE * FR5_PRIXHT) AS montant_commande_ht,
					0::numeric AS montant_commande_remise,
					SUM(FR5_QTE * (FR5_PRIXTC - FR5_PRIXHT)) AS montant_commande_tva,
					0::numeric AS montant_commande_port,
					SUM(FR5_QTE * FR5_PRIXTC) AS montant_commande_total,
					SUM(CASE WHEN FR5_DAFAC IS NOT NULL THEN FR5_QTE * FR5_PRIXHT ELSE 0 END) AS montant_liquidation_ht,
					0::numeric AS montant_liquidation_remise,
					SUM(CASE WHEN FR5_DAFAC IS NOT NULL THEN FR5_QTE * (FR5_PRIXTC - FR5_PRIXHT) ELSE 0 END) AS montant_liquidation_tva,
					0::numeric AS montant_liquidation_port,
					SUM(CASE WHEN FR5_DAFAC IS NOT NULL THEN FR5_QTE * FR5_PRIXTC ELSE 0 END) AS montant_liquidation_total
				FROM W_R500 as F_R500
					LEFT JOIN eco.t_providers on t_providers.code = F_R500.provider_code
					LEFT JOIN eco.t_fournisseurs ON FOCLEUNIK::text = t_fournisseurs.code_original
				WHERE FR5_DDEMAN >= '[ENV_ECO_ANNEEDEBUT]0101'
				AND FR5_NUMDEM IS NOT NULL
				GROUP BY 1,2;


				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)
				SELECT
					FR5_NUMDEM || R5CLEUNIK::text AS code_original,
					p_commandes.oid AS commande_id,
					0::numeric AS ligne_commande,
					'' AS texte,
					COALESCE(t_articles.oid, 0) AS article_id,
					0::bigint AS unite_approvisionnement_id,
					COALESCE(t_compte.oid, t_articles.compte_id, 0) AS compte_id,
					COALESCE(t_ucd.oid, 0) AS ucd_id,
					COALESCE(t_lpp.oid, 0) AS lpp_id,
					
					FR5_QTE * CASE WHEN FR5_PRIXHT >= 0 THEN 1 ELSE -1 END AS quantite_approvisionnement,
					FR5_PRIXHT AS prix_unitaire_approvisionnement,
					0::numeric AS multiplicateur_stockage,
					0::numeric AS quantite_stockage,
					0::numeric AS prix_unitaire_stockage,

					(FR5_PRIXHT * FR5_QTE) * CASE WHEN FR5_PRIXHT >= 0 THEN 1 ELSE -1 END AS montant_commande_ht,
					0::numeric AS montant_commande_remise,
					(FR5_PRIXTC - FR5_PRIXHT) * FR5_QTE AS montant_commande_tva,
					0::numeric AS montant_commande_port, 
					(FR5_PRIXTC - FR5_PRIXHT) * FR5_QTE AS montant_commande_ttc,

					FR5_QTE * CASE WHEN FR5_PRIXHT >= 0 THEN 1 ELSE -1 END AS quantite_livraison_approvisionnement,
					FR5_QTE * CASE WHEN FR5_PRIXHT >= 0 THEN 1 ELSE -1 END AS quantite_livraison_stockage,
					(FR5_PRIXHT * FR5_QTE) * CASE WHEN FR5_PRIXHT >= 0 THEN 1 ELSE -1 END AS montant_livraison_ht,
					(FR5_PRIXTC - FR5_PRIXHT) * FR5_QTE  AS montant_livraison_ttc,
					CASE WHEN FR5_QTE = FR5_QTE THEN 'T' WHEN FR5_QTE > FR5_QTE THEN 'S' WHEN FR5_QTE = 0 THEN 'N' ELSE 'P' END AS etat_livraison,

					(CASE WHEN FR5_DAFAC IS NOT NULL THEN FR5_PRIXHT * FR5_QTE ELSE 0 END) AS montant_liquidation_ht,
					0::numeric AS montant_liquidation_remise,
					CASE WHEN FR5_DAFAC IS NOT NULL THEN (FR5_PRIXTC - FR5_PRIXHT) * FR5_QTE ELSE 0 END AS montant_liquidation_tva,
					0::numeric AS montant_liquidation_port,
					CASE WHEN FR5_DAFAC IS NOT NULL THEN (FR5_PRIXTC * FR5_QTE) ELSE 0 END AS montant_liquidation_ttc,
					CASE WHEN FR5_DAFAC IS NOT NULL THEN '1' ELSE '0' END AS etat_liquidation
				FROM w_r500 as F_R500
					JOIN eco.p_commandes ON FR5_NUMDEM = p_commandes.code_original
					JOIN w_f200 AS F_200 ON F_200.ARCLEUNIK = F_R500.ARCLEUNIK 
					LEFT JOIN eco.t_articles ON F_R500.ARCLEUNIK::text = t_articles.code_original AND t_articles.oid <> 0
					LEFT JOIN eco.t_compte ON ''::text = t_compte.code_original AND t_compte.oid <> 0
					LEFT JOIN base.t_ucd ON '' = t_ucd.code
					LEFT JOIN base.t_lpp ON F20_CODTIPS = t_lpp.code
				WHERE FR5_DDEMAN >= '[ENV_ECO_ANNEEDEBUT]0101';


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

				UPDATE w_f200 SET
					F20_PUMP = F21_PRIX
				FROM w_210 as F_210
				WHERE w_f200.ARCLEUNIK = F_210.ARCLEUNIK AND
					F20_PUMP = 0 AND F21_PRIX <> 0
				;

				DROP TABLE IF EXISTS w_stocks;
				CREATE TEMP TABLE w_stocks AS
				SELECT 
					t_articles.oid AS article_id,
					F_500.ARCLEUNIK,
					F_200.F20_DEPRIX AS F20_PRIX,
					F_200.F20_PUMP,
					date(date_trunc('month',F50_DATE) + interval '1 month' - interval '1 day') AS date_fin,
					date(date_trunc('month',F50_DATE)) AS date_debut,
					date_part('year',F50_DATE) * 12 + date_part('month',F50_DATE) AS mois_sequence,
					COALESCE(t_ucd.oid,w_210_ucd.ucd_id,w_210_ucd_def.ucd_id,0) AS ucd_id,
					COALESCE(t_lpp.oid,0) AS lpp_id,
					COALESCE(t_compte.oid,t_articles.compte_id,0) AS compte_id,
					0::bigint AS lieu_id,
					coalesce(t_providers.oid, 0::bigint) AS provider_id,
					MAX(t_fournisseurs.oid) AS fournisseur_id,					
					0.00 AS stock_quantite_debut,
					0.00 AS stock_valeur_debut,

					MAX(COALESCE(F_210.F21_PRIX,0)) AS F21_PRIX,
					SUM(CASE WHEN F50_TYPE IN ('E', 'F', 'R') THEN F50_QTE ELSE 0 END) AS entree_quantite,
					SUM(CASE WHEN F50_TYPE IN ('E', 'F', 'R') THEN 
						CASE 
							WHEN F50_PUTTC <> 0 THEN F50_QTE * F50_PUTTC
							WHEN F50_PRIX <> 0 THEN F50_QTE * F50_PRIX * (1+COALESCE(w_TVA.taux,0))
							ELSE F50_QTE * F50_PUMP * (1+COALESCE(w_TVA.taux,0))
						END 
					ELSE 0 END) AS entree_montant,
					SUM(CASE WHEN F50_TYPE IN ('E', 'F', 'R') AND F50_QTE > 0 THEN F50_QTE ELSE 0 END) AS entree_quantite_pump,
					SUM(CASE WHEN F50_TYPE IN ('E', 'F', 'R') AND F50_QTE > 0 THEN 
						CASE 
							WHEN F50_PUTTC <> 0 THEN F50_QTE * F50_PUTTC
							WHEN F50_PRIX <> 0 THEN F50_QTE * F50_PRIX * (1+COALESCE(w_TVA.taux,0))
							ELSE F50_QTE * F50_PUMP * (1+COALESCE(w_TVA.taux,0))
						END 
					ELSE 0 END) AS entree_montant_pump,
					0.00 AS pump,
					SUM(CASE WHEN F50_TYPE NOT IN ('E', 'F', 'R', 'I') THEN F50_QTE ELSE 0 END) AS sortie_quantite,
					0.00 AS sortie_montant,
					(MAX(Array[
						F50_DATE::text || MOCLEUNIK::text,
						(CASE WHEN F50_TYPE IN ('I') THEN F50_QTE ELSE F50_ASTK + CASE  WHEN F50_TYPE IN ('E', 'F', 'R') THEN F50_QTE ELSE 0-F50_QTE END END)::text
					]))[2]::numeric AS stock_quantite_fin,
					0.00 AS stock_valeur_fin
				FROM w_F500 F_500
					JOIN w_f200 as F_200 ON F_200.ARCLEUNIK = F_500.ARCLEUNIK
					LEFT JOIN w_TVA ON F20_CODTVA = w_TVA.codtva AND date(F_500.F50_DATE) BETWEEN w_TVA.date_debut AND w_TVA.date_fin
					LEFT JOIN w_210 as F_210 ON 
						F_210.ARCLEUNIK = F_500.ARCLEUNIK AND 
						F_210.F21_IDDIST = F_500.FOCLEUNIK AND 
						date(F_500.F50_DATE) BETWEEN F21_DEBUT AND F21_FIN
					LEFT JOIN eco.t_providers on t_providers.code = F_500.provider_code
					LEFT JOIN eco.t_types_mouvements ON F50_TYPE = t_types_mouvements.code_original
					LEFT JOIN eco.t_unites_fonctionnelles ON (t_unites_fonctionnelles.code_original = (F50_DEPT || '-' || F50_SERVI))
					LEFT JOIN eco.t_articles ON F_500.ARCLEUNIK::text = t_articles.code_original AND t_articles.oid <> 0
					LEFT JOIN eco.t_fournisseurs ON F_500.FOCLEUNIK::text = t_fournisseurs.code_original
					LEFT JOIN eco.t_compte ON F_500.compte_code_original = t_compte.code_original AND t_compte.oid <> 0
					LEFT JOIN base.t_ucd ON F21_UCD = t_ucd.code
					LEFT JOIN base.t_lpp ON F20_CODTIPS = t_lpp.code
					LEFT JOIN w_210_ucd ON F_500.ARCLEUNIK = w_210_ucd.ARCLEUNIK AND date(F_500.F50_DATE) BETWEEN w_210_ucd.date_debut AND w_210_ucd.date_fin
					LEFT JOIN w_210_ucd w_210_ucd_def ON F_500.ARCLEUNIK = w_210_ucd_def.ARCLEUNIK AND w_210_ucd_def.date_fin = '20991231'
					LEFT JOIN w_210_fournisseur ON F_500.ARCLEUNIK = w_210_fournisseur.ARCLEUNIK AND date(F_500.F50_DATE) BETWEEN w_210_fournisseur.date_debut AND w_210_fournisseur.date_fin
					LEFT JOIN w_210_fournisseur w_210_fournisseur_2 ON F_500.ARCLEUNIK = w_210_fournisseur_2.ARCLEUNIK AND w_210_fournisseur_2.date_fin = '20991231'
				WHERE date(F50_DATE) < date(date_trunc('month',now()) + interval '1 month')
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12
				ORDER BY 1,2;

				CREATE INDEX w_stocks_i1 ON w_stocks USING btree (article_id);	

				UPDATE w_stocks
				SET entree_quantite_pump = 0, entree_montant_pump = 0
				WHERE entree_quantite_pump < 0 OR entree_montant_pump < 0; 

			
					
					
				SELECT base.cti_execute(
				'INSERT INTO w_stocks (
					article_id,
					ARCLEUNIK,
					F20_PRIX,
					F20_PUMP,
					F21_PRIX,
					date_fin,
					date_debut,
					mois_sequence,
					ucd_id,
					lpp_id,
					compte_id,
					lieu_id,
					provider_id,
					fournisseur_id,
					stock_quantite_debut,
					stock_valeur_debut,
					entree_quantite,
					entree_montant,
					entree_quantite_pump,
					entree_montant_pump,
					pump,
					sortie_quantite,
					sortie_montant,
					stock_quantite_fin,
					stock_valeur_fin
				)
				SELECT 
					w_stocks.article_id,
					w_stocks.ARCLEUNIK,
					w_stocks.F20_PRIX,
					w_stocks.F20_PUMP,
					w_stocks.F21_PRIX,
					date(w_stocks.date_debut + interval ''2 month'' - interval ''1 day'') AS date_fin,
					date(w_stocks.date_debut + interval ''1 month'') AS date_debut,
					w_stocks.mois_sequence + 1 AS mois_sequence,
					w_stocks.ucd_id,
					w_stocks.lpp_id,
					w_stocks.compte_id,
					w_stocks.lieu_id,
					w_stocks.provider_id,
					w_stocks.fournisseur_id,
					0.00 AS stock_quantite_debut,
					0.00 AS stock_valeur_debut,
					0.00 AS entree_quantite,
					0.00 AS entree_montant,
					0.00 AS entree_quantite_pump,
					0.00 AS entree_montant_pump,
					0.00 AS pump,
					0.00 AS sortie_quantite,
					0.00 AS sortie_montant,
					w_stocks.stock_quantite_fin,
					0.00 AS stock_valeur_fin
				FROM w_stocks
				LEFT JOIN w_stocks w_stocks_after ON 
					w_stocks_after.article_id = w_stocks.article_id AND
					w_stocks_after.mois_sequence = w_stocks.mois_sequence + 1
				WHERE w_stocks_after.article_id IS NULL AND
					w_stocks.date_debut < date(date_trunc(''month'',now()))
				ORDER BY 1,2',300);

	
				UPDATE w_stocks
				SET stock_quantite_fin = F20_QTESTO
				FROM w_f200 AS F_200
				WHERE w_stocks.ARCLEUNIK = F_200.ARCLEUNIK AND
					w_stocks.date_fin >= date(date_trunc('month',now())) AND
					F20_QTESTO <> stock_quantite_fin
					;

			



				DROP TABLE IF EXISTS w_pump;
				CREATE TEMP TABLE w_pump AS
				SELECT w_stocks.article_id, 
					w_stocks.ARCLEUNIK,
					w_stocks.date_fin, 
					MAX(w_stocks.F20_PRIX) AS F20_PRIX,
					MAX(w_stocks.F20_PUMP) AS F20_PUMP,
					MAX(w_stocks.F21_PRIX) AS F21_PRIX,
					base.cti_division(
						SUM(w_stocks_before.entree_montant_pump)::numeric
						,
						SUM(w_stocks_before.entree_quantite_pump)::numeric
						) AS pump,
					base.cti_division(
						SUM(CASE WHEN w_stocks.mois_sequence -  w_stocks_before.mois_sequence < 6 THEN w_stocks_before.entree_montant_pump ELSE 0 END)::numeric
						,
						SUM(CASE WHEN w_stocks.mois_sequence -  w_stocks_before.mois_sequence < 6 THEN w_stocks_before.entree_quantite_pump ELSE 0 END)::numeric
						) AS pump6,
					base.cti_division(
						SUM(CASE WHEN w_stocks.mois_sequence -  w_stocks_before.mois_sequence < 12 THEN w_stocks_before.entree_montant_pump ELSE 0 END)::numeric
						,
						SUM(CASE WHEN w_stocks.mois_sequence -  w_stocks_before.mois_sequence < 12 THEN w_stocks_before.entree_quantite_pump ELSE 0 END)::numeric
						) AS pump12
				FROM w_stocks
				JOIN w_stocks w_stocks_before ON 
					w_stocks.article_id = w_stocks_before.article_id AND
					w_stocks.date_fin >= w_stocks_before.date_fin
				GROUP BY 1,2,3;

				

				CREATE INDEX w_pump_i1 ON w_pump USING btree (article_id);	

				

				UPDATE w_stocks SET
					pump = CASE WHEN pump6 <> 0 THEN pump6 WHEN pump12 <> 0 THEN pump12 WHEN w_pump.pump <> 0 THEN w_pump.pump WHEN w_pump.F21_PRIX <> 0 THEN w_pump.F21_PRIX ELSE w_pump.F20_PUMP END,
					sortie_montant = sortie_quantite * CASE WHEN pump6 <> 0 THEN pump6 WHEN pump12 <> 0 THEN pump12 WHEN w_pump.pump <> 0 THEN w_pump.pump WHEN w_pump.F21_PRIX <> 0 THEN w_pump.F21_PRIX ELSE w_pump.F20_PUMP END,
					stock_valeur_fin = w_stocks.stock_quantite_fin * CASE WHEN pump6 <> 0 THEN pump6 WHEN pump12 <> 0 THEN pump12 WHEN w_pump.pump <> 0 THEN w_pump.pump WHEN w_pump.F21_PRIX <> 0 THEN w_pump.F21_PRIX ELSE w_pump.F20_PUMP END
				FROM w_pump
				WHERE w_pump.article_id = w_stocks.article_id AND
					w_pump.date_fin = w_stocks.date_fin;




				DELETE FROM w_stocks 
				WHERE article_id IN (
					SELECT article_id
					FROM w_stocks
					WHERE stock_quantite_fin <> 0
					GROUP BY 1
					HAVING MAX(date_debut) <= '20091231'
					);


				

				UPDATE w_stocks SET
					stock_quantite_debut = w_stocks_before.stock_quantite_fin,
					stock_valeur_debut = w_stocks_before.stock_valeur_fin
				FROM w_stocks w_stocks_before
				WHERE w_stocks_before.article_id = w_stocks.article_id AND
					w_stocks_before.mois_sequence = w_stocks.mois_sequence - 1;


				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');
			

				TRUNCATE eco.p_stock;
				
				INSERT INTO eco.p_stock(
					article_id,
					date_fin,
					date_debut,
					ucd_id,
					lpp_id,
					compte_id,
					lieu_id,
					provider_id,
					fournisseur_id,
					stock_quantite_debut,
					stock_valeur_debut,
					entree_quantite,
					entree_montant,
					pump,
					sortie_quantite,
					sortie_montant,
					stock_quantite_fin,
					stock_valeur_fin)

				SELECT 
					article_id,
					date_fin,
					date_debut,
					ucd_id,
					lpp_id,
					compte_id,
					lieu_id,
					provider_id,
					fournisseur_id,
					stock_quantite_debut,
					stock_valeur_debut,
					entree_quantite,
					entree_montant,
					pump,
					sortie_quantite,
					sortie_montant,
					stock_quantite_fin,
					stock_valeur_fin
				FROM w_stocks
				WHERE date_debut >= '[ENV_ECO_ANNEEDEBUT]0101';
					
				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>
		
		
		<NODE label="Mouvements">
			<sqlcmd><![CDATA[
			

				TRUNCATE eco.p_mouvements_articles;

				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');


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

				SELECT
					'M'::text || F_500.MOCLEUNIK::text AS code_original,
					coalesce(t_providers.oid, 0::bigint) AS provider_id,
					F50_DATE AS date,
					CASE WHEN F50_TYPE IN ('E', 'F', 'R') THEN 'E' ELSE 'S' END AS sens_mouvement,
					COALESCE(t_types_mouvements.oid,0) AS type_mouvement_id,
					''::text AS texte,
					0::bigint 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_compte.oid,t_articles.compte_id,0) AS compte_id,
					COALESCE(t_fournisseurs.oid,w_210_fournisseur.fournisseur_id, w_210_fournisseur_2.fournisseur_id,0) AS fournisseur_id,
					0::bigint AS commande_id,
					0::numeric AS ligne_commande,
					COALESCE(t_ucd.oid,w_210_ucd.ucd_id,w_210_ucd_def.ucd_id,0) AS ucd_id,
					COALESCE(t_lpp.oid,0) AS lpp_id,
					CASE 
						WHEN F50_TYPE IN ('E', 'F', 'R') AND F50_PUTTC <> 0 THEN F50_PUTTC / (1+COALESCE(w_TVA.taux,0))
						WHEN F50_TYPE IN ('E', 'F', 'R') AND F50_PRIX <> 0 THEN F50_PRIX
						WHEN F50_TYPE IN ('E', 'F', 'R') THEN F50_PUMP
						WHEN F50_TYPE NOT IN ('E', 'F', 'R') AND F50_PUMP <> 0 THEN F50_PUMP
						WHEN F50_TYPE NOT IN ('E', 'F', 'R') THEN F50_PRIX
						ELSE 0
					END  AS prix_unitaire,
					CASE WHEN F50_TYPE IN ('E', 'F', 'R') THEN F50_QTE ELSE 0 END AS entree_quantite,
					CASE WHEN F50_TYPE NOT IN ('E', 'F', 'R') THEN F50_QTE ELSE 0 END AS sortie_quantite,
					CASE WHEN F50_TYPE IN ('E', 'F', 'R') THEN 
						CASE 
							WHEN F50_PUTTC <> 0 THEN F50_QTE * F50_PUTTC
							WHEN F50_PRIX <> 0 THEN F50_QTE * F50_PRIX * (1+COALESCE(w_TVA.taux,0))
							ELSE F50_QTE * F50_PUMP * (1+COALESCE(w_TVA.taux,0))
						END 
					ELSE 0 END AS entree_montant,
					round(CASE WHEN F50_TYPE IN ('E', 'F', 'R') THEN 
						CASE 
							WHEN F50_PUTTC <> 0 THEN F50_QTE * F50_PUTTC / (1+COALESCE(w_TVA.taux,0))
							WHEN F50_PRIX <> 0 THEN F50_QTE * F50_PRIX
							ELSE F50_QTE * F50_PUMP 
						END 
					ELSE 0 END,2) AS entree_montant_ht,
					CASE WHEN F50_TYPE NOT IN ('E', 'F', 'R') THEN
						CASE 
							WHEN F50_PUTTC <> 0 THEN F50_QTE * F50_PUTTC
							WHEN F50_PRIX <> 0 THEN F50_QTE * F50_PRIX * (1+COALESCE(w_TVA.taux,0))
							WHEN F50_PUMP <> 0 THEN F50_QTE * F50_PUMP * (1+COALESCE(w_TVA.taux,0))
							ELSE F50_QTE * p_stock.pump * (1+COALESCE(w_TVA.taux,0))
						END 
					ELSE 0 END AS sortie_montant,
					CASE WHEN F50_TYPE NOT IN ('E', 'F', 'R') THEN 
						CASE 
							WHEN F50_PUTTC <> 0 THEN F50_QTE * F50_PUTTC / (1+COALESCE(w_TVA.taux,0))
							WHEN F50_PRIX <> 0 THEN F50_QTE * F50_PRIX
							WHEN F50_PUMP <> 0 THEN F50_QTE * F50_PUMP
							ELSE F50_QTE * p_stock.pump
						END 
					ELSE 0 END AS sortie_montant_ht,
					CASE WHEN F50_TYPE NOT IN ('E', 'F', 'R') THEN 
						CASE 
							WHEN F50_PUMP <> 0 THEN F50_QTE * F50_PUMP * (1+COALESCE(w_TVA.taux,0))
							ELSE F50_QTE * F50_PRIX * (1+COALESCE(w_TVA.taux,0))
						END 
					ELSE 0 END AS sortie_montant_original_ttc,
					CASE WHEN F50_TYPE NOT IN ('E', 'F', 'R') THEN 
						CASE 
							WHEN F50_PUMP <> 0 THEN F50_QTE * F50_PUMP
							ELSE F50_QTE * F50_PRIX
						END 
					ELSE 0 END AS sortie_montant_original_ht

				FROM w_F500 F_500
					JOIN w_f200 AS F_200 ON F_200.ARCLEUNIK = F_500.ARCLEUNIK
					LEFT JOIN eco.t_providers on t_providers.code = F_500.provider_code
					LEFT JOIN w_TVA ON F20_CODTVA = w_TVA.codtva AND date(F_500.F50_DATE) BETWEEN w_TVA.date_debut AND w_TVA.date_fin
					LEFT JOIN w_210 as F_210 ON 
						F_210.ARCLEUNIK = F_500.ARCLEUNIK AND 
						F_210.F21_IDDIST = F_500.FOCLEUNIK AND 
						date(F_500.F50_DATE) BETWEEN F21_DEBUT AND F21_FIN
					LEFT JOIN eco.t_types_mouvements ON F50_TYPE = t_types_mouvements.code_original
					LEFT JOIN eco.t_unites_fonctionnelles ON (t_unites_fonctionnelles.code_original = (F50_DEPT || '-' || F50_SERVI))
					LEFT JOIN eco.t_articles ON F_500.ARCLEUNIK::text = t_articles.code_original AND t_articles.oid <> 0
					LEFT JOIN eco.t_fournisseurs ON F_500.FOCLEUNIK::text = t_fournisseurs.code_original
					LEFT JOIN eco.t_compte ON F_500.compte_code_original = t_compte.code_original AND t_compte.oid <> 0
					LEFT JOIN eco.t_lieux ON F_500.F50_STOCK = t_lieux.code_original AND t_lieux.oid <> 0
					LEFT JOIN base.t_ucd ON F21_UCD = t_ucd.code
					LEFT JOIN base.t_lpp ON F20_CODTIPS = t_lpp.code
					LEFT JOIN w_210_ucd ON F_500.ARCLEUNIK = w_210_ucd.ARCLEUNIK AND date(F_500.F50_DATE) BETWEEN w_210_ucd.date_debut AND w_210_ucd.date_fin
					LEFT JOIN w_210_ucd w_210_ucd_def ON F_500.ARCLEUNIK = w_210_ucd_def.ARCLEUNIK AND w_210_ucd_def.date_fin = '20991231'
					LEFT JOIN w_210_fournisseur ON F_500.ARCLEUNIK = w_210_fournisseur.ARCLEUNIK AND date(F_500.F50_DATE) BETWEEN w_210_fournisseur.date_debut AND w_210_fournisseur.date_fin
					LEFT JOIN w_210_fournisseur w_210_fournisseur_2 ON F_500.ARCLEUNIK = w_210_fournisseur_2.ARCLEUNIK AND w_210_fournisseur_2.date_fin = '20991231'
					LEFT JOIN eco.p_stock ON p_stock.article_id =t_articles.oid AND 
						date(F50_DATE) BETWEEN p_stock.date_debut AND p_stock.date_fin
				WHERE F50_DATE >= '[ENV_ECO_ANNEEDEBUT]0101' AND F50_TYPE <> 'I';

				INSERT INTO eco.p_mouvements_articles(
					code_original,
					provider_id,
					date, 
					sens_mouvement,
					type_mouvement_id, 
					texte, 
					gestionnaire_id, 
					lieu_id, 
					unite_fonctionnelle_id, 
					article_id, 
					compte_id, 
					fournisseur_id,
					commande_id,
					ligne_commande,
					ucd_id,
					lpp_id,
					prix_unitaire, 
					entree_quantite, 
					sortie_quantite,
					entree_montant, 
					entree_montant_ht,
					sortie_montant,
					sortie_montant_ht,
					sortie_montant_original_ttc,
					sortie_montant_original_ht)
				SELECT
					'R'::text || F_R500.R5CLEUNIK::text AS code_original,
					coalesce(t_providers.oid, 0::bigint) AS provider_id,
					FR5_DATE AS date,
					'E' AS sens_mouvement,
					COALESCE(t_types_mouvements.oid,0) AS type_mouvement_id,
					''::text AS texte,
					0::bigint AS gestionnaire_id,
					0::bigint AS lieu_id,
					COALESCE(t_unites_fonctionnelles.oid,0) AS unite_fonctionnelle_id,
					COALESCE(t_articles.oid,0) AS article_id,
					COALESCE(t_compte.oid,t_articles.compte_id,0) AS compte_id,
					COALESCE(t_fournisseurs.oid,w_210_fournisseur.fournisseur_id, w_210_fournisseur_2.fournisseur_id,0) AS fournisseur_id,
					0::bigint AS commande_id,
					0::numeric AS ligne_commande,
					COALESCE(t_ucd.oid,w_210_ucd.ucd_id,w_210_ucd_def.ucd_id,0) AS ucd_id,
					COALESCE(t_lpp.oid,0) AS lpp_id,
					CASE 
						WHEN FR5_PRIXTC <> 0 THEN FR5_PRIXTC / (1+COALESCE(w_TVA.taux,0))
						WHEN FR5_PRIXHT <> 0 THEN FR5_PRIXHT
						ELSE 0
					END  AS prix_unitaire,
					FR5_QTE AS entree_quantite,
					0 AS sortie_quantite,
					CASE 
						WHEN FR5_PRIXTC <> 0 THEN FR5_QTE * FR5_PRIXTC
						WHEN FR5_PRIXHT <> 0 THEN FR5_QTE * FR5_PRIXHT * (1+COALESCE(w_TVA.taux,0))
						ELSE 0	END 
					AS entree_montant,
					round(
						CASE 
							WHEN FR5_PRIXTC <> 0 THEN FR5_QTE * FR5_PRIXTC / (1+COALESCE(w_TVA.taux,0))
							WHEN FR5_PRIXHT <> 0 THEN FR5_QTE * FR5_PRIXHT
							ELSE 0
						END,2) AS entree_montant_ht,
					0 AS sortie_montant,
					0 AS sortie_montant_ht,
					0 AS sortie_montant_original_ttc,
					0 AS sortie_montant_original_ht

				FROM w_r500 as F_R500
					JOIN w_f200 AS F_200 ON F_200.ARCLEUNIK = F_R500.ARCLEUNIK
					LEFT JOIN eco.t_providers on t_providers.code = F_R500.provider_code
					LEFT JOIN w_TVA ON F20_CODTVA = w_TVA.codtva AND date(F_R500.FR5_DATE) BETWEEN w_TVA.date_debut AND w_TVA.date_fin
					LEFT JOIN w_210 as F_210 ON 
						F_210.ARCLEUNIK = F_R500.ARCLEUNIK AND 
						F_210.F21_IDDIST = F_R500.FOCLEUNIK AND 
						date(F_R500.FR5_DATE) BETWEEN F21_DEBUT AND F21_FIN
					LEFT JOIN eco.t_types_mouvements ON 'E' = t_types_mouvements.code_original
					LEFT JOIN eco.t_unites_fonctionnelles ON (t_unites_fonctionnelles.code_original = (FR5_CDEPT || '-' || FR5_CSERVI))
					LEFT JOIN eco.t_articles ON F_R500.ARCLEUNIK::text = t_articles.code_original AND t_articles.oid <> 0
					LEFT JOIN eco.t_fournisseurs ON F_R500.FOCLEUNIK::text = t_fournisseurs.code_original
					LEFT JOIN eco.t_compte ON ''::text = t_compte.code_original AND t_compte.oid <> 0
					LEFT JOIN base.t_ucd ON F21_UCD = t_ucd.code
					LEFT JOIN base.t_lpp ON F20_CODTIPS = t_lpp.code
					LEFT JOIN w_210_ucd ON F_R500.ARCLEUNIK = w_210_ucd.ARCLEUNIK AND date(F_R500.FR5_DATE) BETWEEN w_210_ucd.date_debut AND w_210_ucd.date_fin
					LEFT JOIN w_210_ucd w_210_ucd_def ON F_R500.ARCLEUNIK = w_210_ucd_def.ARCLEUNIK AND w_210_ucd_def.date_fin = '20991231'
					LEFT JOIN w_210_fournisseur ON F_R500.ARCLEUNIK = w_210_fournisseur.ARCLEUNIK AND date(F_R500.FR5_DATE) BETWEEN w_210_fournisseur.date_debut AND w_210_fournisseur.date_fin
					LEFT JOIN w_210_fournisseur w_210_fournisseur_2 ON F_R500.ARCLEUNIK = w_210_fournisseur_2.ARCLEUNIK AND w_210_fournisseur_2.date_fin = '20991231'
					LEFT JOIN eco.p_stock ON p_stock.article_id =t_articles.oid AND 
						date(FR5_DATE) BETWEEN p_stock.date_debut AND p_stock.date_fin
				WHERE FR5_DATE >= '[ENV_ECO_ANNEEDEBUT]0101' AND
					date(FR5_DATE) <= now();

				--INSERT INTO eco.p_mouvements_articles(
				--	code_original,
				--	provider_id,
				--	date, 
				--	sens_mouvement,
				--	type_mouvement_id, 
				--	texte, 
				--	gestionnaire_id, 
				--	lieu_id, 
				--	unite_fonctionnelle_id, 
				--	article_id, 
				--	compte_id, 
				--	fournisseur_id,
				--	commande_id,
				--	ligne_commande,
				--	ucd_id,
				--	lpp_id,
				--	prix_unitaire, 
				--	entree_quantite, 
				--	sortie_quantite,
				--	entree_montant, 
				--	entree_montant_ht,
				--	sortie_montant,
				--	sortie_montant_ht,
				--	sortie_montant_original_ttc,
				--	sortie_montant_original_ht)
				--SELECT
				--	'R2'::text || F_R500.R5CLEUNIK::text AS code_original,
				--	coalesce(t_providers.oid, 0::bigint) AS provider_id,
				--	FR5_DATE AS date,
				--	'E' AS sens_mouvement,
				--	COALESCE(t_types_mouvements.oid,0) AS type_mouvement_id,
				--	''::text AS texte,
				--	0::bigint AS gestionnaire_id,
				--	0::bigint AS lieu_id,
				--	COALESCE(t_unites_fonctionnelles.oid,0) AS unite_fonctionnelle_id,
				--	COALESCE(t_articles.oid,0) AS article_id,
				--	COALESCE(t_compte.oid,t_articles.compte_id,0) AS compte_id,
				--	COALESCE(t_fournisseurs.oid,w_210_fournisseur.fournisseur_id, w_210_fournisseur_2.fournisseur_id,0) AS fournisseur_id,
				--	0::bigint AS commande_id,
				--	0::numeric AS ligne_commande,
				--	COALESCE(t_ucd.oid,w_210_ucd.ucd_id,w_210_ucd_def.ucd_id,0) AS ucd_id,
				--	COALESCE(t_lpp.oid,0) AS lpp_id,
				--	CASE 
				--		WHEN FR5_PRIXTC <> 0 THEN FR5_PRIXTC / (1+COALESCE(w_TVA.taux,0))
				--		WHEN FR5_PRIXHT <> 0 THEN FR5_PRIXHT
				--		ELSE 0
				--	END  AS prix_unitaire,
				--	FR5_QTE AS entree_quantite,
				--	0 AS sortie_quantite,
				--	CASE 
				--		WHEN FR5_PRIXTC <> 0 THEN FR5_QTE * FR5_PRIXTC
				--		WHEN FR5_PRIXHT <> 0 THEN FR5_QTE * FR5_PRIXHT * (1+COALESCE(w_TVA.taux,0))
				--		ELSE 0	END 
				--	AS entree_montant,
				--	round(
				--		CASE 
				--			WHEN FR5_PRIXTC <> 0 THEN FR5_QTE * FR5_PRIXTC / (1+COALESCE(w_TVA.taux,0))
				--			WHEN FR5_PRIXHT <> 0 THEN FR5_QTE * FR5_PRIXHT
				--			ELSE 0
				--		END,2) AS entree_montant_ht,
				--	0 AS sortie_montant,
				--	0 AS sortie_montant_ht,
				--	0 AS sortie_montant_original_ttc,
				--	0 AS sortie_montant_original_ht
				--FROM w_r500 as F_R500
				--	JOIN w_f200 AS F_200 ON F_200.ARCLEUNIK = F_R500.ARCLEUNIK
				--	LEFT JOIN eco.t_providers on t_providers.code = F_R500.provider_code
				--	LEFT JOIN w_TVA ON F20_CODTVA = w_TVA.codtva AND date(F_R500.FR5_DATE) BETWEEN w_TVA.date_debut AND w_TVA.date_fin
				--	LEFT JOIN w_210 as F_210 ON 
				--		F_210.ARCLEUNIK = F_R500.ARCLEUNIK AND 
				--		F_210.F21_IDDIST = F_R500.FOCLEUNIK AND 
				--		date(F_R500.FR5_DATE) BETWEEN F21_DEBUT AND F21_FIN
				--	LEFT JOIN eco.t_types_mouvements ON '' = t_types_mouvements.code_original
				--	LEFT JOIN eco.t_unites_fonctionnelles ON (t_unites_fonctionnelles.code_original = (FR5_CDEPT || '-' || FR5_CSERVI))
				--	LEFT JOIN eco.t_articles ON F_R500.ARCLEUNIK::text = t_articles.code_original AND t_articles.oid <> 0
				--	LEFT JOIN eco.t_fournisseurs ON F_R500.FOCLEUNIK::text = t_fournisseurs.code_original
				--	LEFT JOIN eco.t_compte ON ''::text = t_compte.code_original AND t_compte.oid <> 0
				--	LEFT JOIN base.t_ucd ON F21_UCD = t_ucd.code
				--	LEFT JOIN base.t_lpp ON F20_CODTIPS = t_lpp.code
				--	LEFT JOIN w_210_ucd ON F_R500.ARCLEUNIK = w_210_ucd.ARCLEUNIK AND date(F_R500.FR5_DATE) BETWEEN w_210_ucd.date_debut AND w_210_ucd.date_fin
				--	LEFT JOIN w_210_ucd w_210_ucd_def ON F_R500.ARCLEUNIK = w_210_ucd_def.ARCLEUNIK AND w_210_ucd_def.date_fin = '20991231'
				--	LEFT JOIN w_210_fournisseur ON F_R500.ARCLEUNIK = w_210_fournisseur.ARCLEUNIK AND date(F_R500.FR5_DATE) BETWEEN w_210_fournisseur.date_debut AND w_210_fournisseur.date_fin
				--	LEFT JOIN w_210_fournisseur w_210_fournisseur_2 ON F_R500.ARCLEUNIK = w_210_fournisseur_2.ARCLEUNIK AND w_210_fournisseur_2.date_fin = '20991231'
				--	LEFT JOIN eco.p_stock ON p_stock.article_id =t_articles.oid AND 
				--		date(FR5_DATE) BETWEEN p_stock.date_debut AND p_stock.date_fin
				--WHERE FR5_DATE >= '[ENV_ECO_ANNEEDEBUT]0101' AND
				--	date(FR5_DATE) <= now();

				
				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');


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

			
			]]></sqlcmd>
		</NODE>
		
	</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>