<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	<NODE name="INIT" label="INITIALISATIONS">
		
		<NODE label="Tables Etablissements">
			
			<sqlcmd><![CDATA[
				
				DROP TABLE IF EXISTS w_ETABLISSEMENT;
				CREATE TEMP TABLE w_ETABLISSEMENT AS
				SELECT EIDETABLISSEMENT AS EIDETABLISSEMENT_ok
				FROM prod_winpharm.GE_ETABLISSEMENT
				WHERE eidetablissement = ANY (string_to_array('[ETAB]',',')) OR 
					'[ETAB]' = ''
				;
			]]></sqlcmd>
		</NODE>
			
		<NODE label="Tables Articles">
			
			<sqlcmd><![CDATA[
			
				-- Paramétrage pour constitution du code
				INSERT INTO eco.t_divers (code, texte, valeur, description)
				SELECT
					'WINPHARM_CODE',
					'Ne pas préfixer les codes articles et fournisseur par le début du libellé',
					'0',
					'0=Préfixer le code par le début du libellé, 1=Ne pas préfixer les codes articles et fournisseur par le début du libellé'
				WHERE 'WINPHARM_CODE' NOT IN (SELECT code FROM eco.t_divers)
                ;
			
				-- LPP
				INSERT INTO base.t_lpp (code, texte, texte_court)
				SELECT SCODELPP, SLIBELLELPP, SLIBELLELPP
				FROM prod_winpharm.TH_LPP
				WHERE SCODELPP NOT IN (SELECT code FROM base.t_lpp);

				-- Comptes
				INSERT INTO eco.t_compte(code, texte, texte_court, code_original)
				SELECT GE_COMPTE.SNUMCOMPTE, GE_COMPTE.SLIBELLECOMPTE, GE_COMPTE.SLIBELLECOMPTE, GE_COMPTE.SNUMCOMPTE
				FROM prod_winpharm.GE_COMPTE
				JOIN
					(
						SELECT SNUMCOMPTE, MIN(EIDCOMPTE) AS EIDCOMPTE
						FROM prod_winpharm.GE_COMPTE
						JOIN w_ETABLISSEMENT ON EIDETABLISSEMENT = EIDETABLISSEMENT_ok
						GROUP BY 1
					) subview ON subview.EIDCOMPTE = GE_COMPTE.EIDCOMPTE
				WHERE GE_COMPTE.SNUMCOMPTE NOT IN (SELECT code FROM eco.t_compte);
				
				UPDATE eco.t_compte
				SET texte = GE_COMPTE.SLIBELLECOMPTE, texte_court = GE_COMPTE.SLIBELLECOMPTE
				FROM prod_winpharm.GE_COMPTE
				JOIN
					(
						SELECT SNUMCOMPTE, MIN(EIDCOMPTE) AS EIDCOMPTE
						FROM prod_winpharm.GE_COMPTE
						GROUP BY 1
					) subview ON subview.EIDCOMPTE = GE_COMPTE.EIDCOMPTE
				WHERE GE_COMPTE.SNUMCOMPTE = t_compte.code AND
					(texte IS DISTINCT FROM GE_COMPTE.SLIBELLECOMPTE OR 
					texte_court IS DISTINCT FROM GE_COMPTE.SLIBELLECOMPTE);
				
				-- unités
			
				INSERT INTO eco.t_unites(code_original, code, texte, texte_court )
				SELECT EIDCONDITIONNEMENT, SUBSTR(UPPER(translate(SLIBELLECONDITIONNEMENT,' .-+&()''','')),1,5), SLIBELLECONDITIONNEMENT, SLIBELLECONDITIONNEMENT
				FROM prod_winpharm.TH_CONDITIONNEMENT
				LEFT JOIN eco.t_unites ON code_original = EIDCONDITIONNEMENT
				WHERE SLIBELLECONDITIONNEMENT <> '' AND 
					code_original IS NULL AND
					EIDCONDITIONNEMENT IN (SELECT EIDCONDITIONNEMENT FROM prod_winpharm.GE_ARTICLE_LABORATOIRE)
				ORDER BY 1;
				
				UPDATE eco.t_unites
				SET 	code = SUBSTR(UPPER(translate(SLIBELLECONDITIONNEMENT,' .-+&()''','')),1,5),
					texte = SLIBELLECONDITIONNEMENT,
					texte_court = SLIBELLECONDITIONNEMENT
				FROM prod_winpharm.TH_CONDITIONNEMENT
				WHERE 
					code_original = EIDCONDITIONNEMENT AND
					(code IS DISTINCT FROM (SUBSTR(UPPER(translate(SLIBELLECONDITIONNEMENT,' .-+&()''','')),1,5)) OR
					texte IS DISTINCT FROM SLIBELLECONDITIONNEMENT OR
					texte_court IS DISTINCT FROM SLIBELLECONDITIONNEMENT);
					
				-- Familles
				INSERT INTO eco.t_familles_articles(code, texte, texte_court, code_original)
				SELECT SCODEFAMILLE, SLIBELLEFAMILLE, SLIBELLEFAMILLE, EIDFAMILLE
				FROM prod_winpharm.TH_FAMILLE
				WHERE EIDFAMILLE NOT IN (SELECT code_original FROM eco.t_familles_articles WHERE code_original IS NOT NULL)
				ORDER BY 1;

				UPDATE eco.t_familles_articles
				SET texte = SLIBELLEFAMILLE, texte_court = SLIBELLEFAMILLE
				FROM prod_winpharm.TH_FAMILLE
				WHERE EIDFAMILLE = code_original AND
					(texte IS DISTINCT FROM SLIBELLEFAMILLE OR 
					texte_court IS DISTINCT FROM SLIBELLEFAMILLE);	
				
				-- Sous familles
				INSERT INTO eco.t_sous_familles_articles(code, texte, texte_court, code_original)
				SELECT SCODECLASSE, SLIBELLECLASSE, SLIBELLECLASSE, EIDCLASSE
				FROM prod_winpharm.TH_CLASSE
				WHERE EIDCLASSE NOT IN (SELECT code_original FROM eco.t_sous_familles_articles WHERE code_original IS NOT NULL)
				ORDER BY 1;

				UPDATE eco.t_sous_familles_articles
				SET texte = SLIBELLECLASSE, texte_court = SLIBELLECLASSE
				FROM prod_winpharm.TH_CLASSE
				WHERE EIDCLASSE = code_original AND
					(texte IS DISTINCT FROM SLIBELLECLASSE OR 
					texte_court IS DISTINCT FROM SLIBELLECLASSE);
					
					
				-- ATC
				INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
				SELECT UPPER(SCODEATC), UPPER(SCODEATC), SLIBELLEATC, SLIBELLEATC
				FROM prod_winpharm.TH_CLASSEATC
				LEFT JOIN eco.t_classification_atc ON code_original = UPPER(SCODEATC)
				WHERE code_original IS NULL 
				ORDER BY 1;

				SELECT eco.cti_reorganize_classification_atc();

				
				-- Types articles
				INSERT INTO eco.t_types_articles(code, texte, texte_court, code_original)
				SELECT SCODETYPEPRODUIT, SNOMTYPE, SNOMTYPE, EIDTYPEPRODUIT
				FROM prod_winpharm.GE_TYPEPRODUIT
				WHERE EIDTYPEPRODUIT NOT IN (SELECT code_original FROM eco.t_types_articles WHERE code_original IS NOT NULL)
				ORDER BY 1;

				-- Articles
				
				DROP TABLE IF EXISTS w_ARTICLE;
				CREATE TEMP TABLE w_ARTICLE AS
				SELECT
					EIDARTICLE::text AS CODE_CTI,
					GE_ARTICLE.*
				FROM prod_winpharm.GE_ARTICLE
				JOIN w_ETABLISSEMENT ON EIDETABLISSEMENT = EIDETABLISSEMENT_ok;

				UPDATE w_ARTICLE
				SET SLIBELLECOURT = SLIBELLELONG 
				WHERE SLIBELLECOURT = '' AND SLIBELLELONG <> '';

				UPDATE w_ARTICLE
				SET SLIBELLELONG = SLIBELLECOURT
				WHERE SLIBELLECOURT <> '' AND SLIBELLELONG = '';

				UPDATE w_ARTICLE SET
					SLIBELLECOURT = SLIBELLECOMMERCIAL,
					SLIBELLELONG = SLIBELLECOMMERCIAL
				WHERE SLIBELLECOURT = '' AND SLIBELLELONG = '';

				UPDATE w_ARTICLE SET
					SLIBELLECOURT = EIDARTICLE::text,
					SLIBELLELONG = EIDARTICLE::text
				WHERE SLIBELLECOURT = '';

				UPDATE w_ARTICLE
				SET CODE_CTI = SUBSTR(UPPER(translate(SLIBELLELONG,' .-+&()''','')),1,5) || '-' || EIDARTICLE
				WHERE (SELECT MAX(valeur) FROM eco.t_divers WHERE code = 'WINPHARM_CODE') IS DISTINCT FROM '1' 
				;
					


				INSERT INTO eco.t_articles(code, texte, texte_court, code_original)
				SELECT 
					CODE_CTI, 
					SLIBELLELONG, 
					substr(SLIBELLECOURT,1,50), 
					EIDARTICLE
				FROM w_ARTICLE
					LEFT JOIN eco.t_articles ON (code_original = EIDARTICLE)
				WHERE code_original IS NULL;

				UPDATE eco.t_articles
				SET 
					code = CODE_CTI, 
					texte = SLIBELLELONG,
					texte_court = substr(SLIBELLECOURT,1,50)
				FROM w_ARTICLE
				WHERE code_original = EIDARTICLE
				AND (	
					code IS DISTINCT FROM CODE_CTI OR
					texte IS DISTINCT FROM SLIBELLELONG OR
					texte_court IS DISTINCT FROM substr(SLIBELLECOURT,1,50)
					);



				-- Attributs articles
				
				UPDATE eco.t_articles SET
					unite_stockage_id = COALESCE(t_unites.oid,0)
				FROM 

					(
					SELECT EIDARTICLE, MIN(GE_ARTICLE_LABORATOIRE.EIDCONDITIONNEMENT) AS EIDCONDITIONNEMENT
					FROM prod_winpharm.GE_ARTICLE_LABORATOIRE
					GROUP BY 1
					) subview
					JOIN eco.t_unites ON EIDCONDITIONNEMENT = t_unites.code_original
				WHERE t_articles.code_original = EIDARTICLE AND
					unite_stockage_id IS DISTINCT FROM COALESCE(t_unites.oid,0);

				UPDATE eco.t_articles SET
					type_id = COALESCE(t_types_articles.oid,0)
				FROM prod_winpharm.GE_ARTICLE
					JOIN prod_winpharm.TH_PRODUIT ON GE_ARTICLE.EIDPRODUIT = TH_PRODUIT.EIDPRODUIT
					LEFT JOIN eco.t_types_articles ON TH_PRODUIT.EIDTYPEPRODUIT = t_types_articles.code_original
				WHERE 
					t_articles.code_original = GE_ARTICLE.EIDARTICLE AND
					(t_articles.type_id IS DISTINCT FROM COALESCE(t_types_articles.oid,0) 
					);

				UPDATE eco.t_articles SET
					famille_id = COALESCE(t_familles_articles.oid,0)
				FROM prod_winpharm.GE_ARTICLE
					JOIN prod_winpharm.TH_PRODUIT ON GE_ARTICLE.EIDPRODUIT = TH_PRODUIT.EIDPRODUIT
					JOIN prod_winpharm.TH_PRODUIT_FAMILLE ON TH_PRODUIT_FAMILLE.EIDPRODUIT = TH_PRODUIT.EIDPRODUIT
					LEFT JOIN eco.t_familles_articles ON TH_PRODUIT_FAMILLE.EIDFAMILLE = t_familles_articles.code_original
				WHERE 
					t_articles.code_original = GE_ARTICLE.EIDARTICLE AND
					(t_articles.famille_id IS DISTINCT FROM COALESCE(t_familles_articles.oid,0)
					);

				UPDATE eco.t_articles SET
					sous_famille_id = COALESCE(t_sous_familles_articles.oid,0)
				FROM prod_winpharm.GE_ARTICLE
					JOIN prod_winpharm.TH_PRODUIT ON GE_ARTICLE.EIDPRODUIT = TH_PRODUIT.EIDPRODUIT
					JOIN prod_winpharm.TH_PRODUIT_CLASSE ON TH_PRODUIT_CLASSE.EIDPRODUIT = TH_PRODUIT.EIDPRODUIT
					LEFT JOIN eco.t_sous_familles_articles ON TH_PRODUIT_CLASSE.EIDCLASSE = t_sous_familles_articles.code_original
				WHERE 
					t_articles.code_original = GE_ARTICLE.EIDARTICLE AND
					(t_articles.sous_famille_id IS DISTINCT FROM COALESCE(t_sous_familles_articles.oid,0)
					);

				UPDATE eco.t_articles SET
					classification_atc_id = COALESCE(t_classification_atc.oid,0)
				FROM prod_winpharm.GE_ARTICLE
					JOIN prod_winpharm.TH_PRODUIT ON GE_ARTICLE.EIDPRODUIT = TH_PRODUIT.EIDPRODUIT
					JOIN prod_winpharm.TH_PRODUIT_CLASSEATC ON TH_PRODUIT_CLASSEATC.EIDPRODUIT = TH_PRODUIT.EIDPRODUIT
					JOIN prod_winpharm.TH_CLASSEATC ON TH_CLASSEATC.EIDCLASSEATC = TH_PRODUIT_CLASSEATC.EIDCLASSEATC 
					LEFT JOIN eco.t_classification_atc ON TH_CLASSEATC.SCODEATC = t_classification_atc.code_original
				WHERE 
					t_articles.code_original = GE_ARTICLE.EIDARTICLE AND
					(t_articles.classification_atc_id IS DISTINCT FROM COALESCE(t_classification_atc.oid,0)
					);

				UPDATE eco.t_articles SET
					lpp_id = COALESCE(t_lpp.oid,0)
				FROM prod_winpharm.GE_ARTICLE
					JOIN prod_winpharm.TH_PRODUIT ON GE_ARTICLE.EIDPRODUIT = TH_PRODUIT.EIDPRODUIT
					LEFT JOIN base.t_lpp ON TH_PRODUIT.SCODELPP = t_lpp.code
				WHERE 
					t_articles.code_original = GE_ARTICLE.EIDARTICLE AND
					(t_articles.lpp_id IS DISTINCT FROM COALESCE(t_lpp.oid,0)
					);

				UPDATE eco.t_articles SET
					ucd_id = COALESCE(t_ucd.oid,0)
				FROM prod_winpharm.GE_ARTICLE
					JOIN prod_winpharm.TH_PRODUIT ON GE_ARTICLE.EIDPRODUIT = TH_PRODUIT.EIDPRODUIT
					LEFT JOIN base.t_ucd ON TH_PRODUIT.SUCD = t_ucd.code
				WHERE 
					t_articles.code_original = GE_ARTICLE.EIDARTICLE AND
					(t_articles.ucd_id IS DISTINCT FROM COALESCE(t_ucd.oid,0)
					);

				UPDATE eco.t_articles SET
					compte_id = COALESCE(t_compte.oid,0),
					taux_tva_en_cours = COALESCE(TH_TAUXTVA.ETAUXTVA)
				FROM prod_winpharm.GE_ARTICLE
					LEFT JOIN prod_winpharm.GE_COMPTE ON GE_ARTICLE.EIDCOMPTE = GE_COMPTE.EIDCOMPTE
					LEFT JOIN prod_winpharm.TH_TAUXTVA ON GE_COMPTE.EIDTAUXTVA = TH_TAUXTVA.EIDTAUXTVA
					LEFT JOIN eco.t_compte ON GE_COMPTE.SNUMCOMPTE = t_compte.code AND t_compte.oid <> 0
				WHERE 
					t_articles.code_original = GE_ARTICLE.EIDARTICLE AND
					(
					t_articles.compte_id IS DISTINCT FROM COALESCE(t_compte.oid,0) OR
					taux_tva_en_cours IS DISTINCT FROM COALESCE(TH_TAUXTVA.ETAUXTVA)
					)
				;
				
				UPDATE eco.t_articles SET
					taux_tva_en_cours = ETAUXTVA
				FROM  
				(
				SELECT EIDARTICLE, 
					(MAX(ARRAY[EIDLIGNE,TH_TAUXTVA.ETAUXTVA]))[2] AS ETAUXTVA
				FROM prod_winpharm.GE_LIGNE
				JOIN prod_winpharm.TH_TAUXTVA ON GE_LIGNE.EIDTAUXTVA = TH_TAUXTVA.EIDTAUXTVA
				WHERE GE_LIGNE.EIDTAUXTVA <> 0
				GROUP BY 1
				) subview
				WHERE t_articles.code_original = subview.EIDARTICLE AND
					taux_tva_en_cours IS DISTINCT FROM ETAUXTVA
				;
				
				
					
				UPDATE eco.t_articles SET
					gere_en_stock = CASE WHEN BNONSTOCKE = 1 THEN '0' ELSE '1' END,
					stock_en_depot = CASE WHEN BENDEPOT = 1 THEN '1' ELSE '0' END
				FROM prod_winpharm.GE_ARTICLE
				WHERE 
					t_articles.code_original = GE_ARTICLE.EIDARTICLE AND
					(
					gere_en_stock IS DISTINCT FROM CASE WHEN BNONSTOCKE = 1 THEN '0' ELSE '1' END OR
					stock_en_depot IS DISTINCT FROM CASE WHEN BENDEPOT = 1 THEN '1' ELSE '0' END
					)
				;


				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[
			
				DROP TABLE IF EXISTS w_LABORATOIRE;
				CREATE TEMP TABLE w_LABORATOIRE AS
				SELECT
					EIDLABORATOIRE::text AS CODE_CTI,
					TH_LABORATOIRE.*
				FROM prod_winpharm.TH_LABORATOIRE;
				
				UPDATE w_LABORATOIRE
				SET CODE_CTI = SUBSTR(UPPER(translate(SRAISONSOCIALEBCB,' .-&()''','')),1,5) || '-' || EIDLABORATOIRE::text
				WHERE (SELECT MAX(valeur) FROM eco.t_divers WHERE code = 'WINPHARM_CODE') IS DISTINCT FROM '1' 
				;
				
				INSERT INTO base.t_codes_postaux(code, texte, texte_court, departement_id)
				SELECT SCODEPOSTAL, MAX(SVILLE), MAX(SVILLE), MAX(t_departements.oid)
				FROM w_LABORATOIRE
				LEFT JOIN base.t_codes_postaux ON SCODEPOSTAL = t_codes_postaux.code
				JOIN base.t_departements ON CASE WHEN SCODEPOSTAL BETWEEN '01' AND '95999' AND length(trim(SCODEPOSTAL)) = 5 THEN substr(SCODEPOSTAL,1,2) else '99' END = t_departements.code
				WHERE t_codes_postaux.oid IS NULL 
				GROUP BY 1
				;


				INSERT INTO eco.t_fournisseurs(code, texte, texte_court, code_original)
				SELECT CODE_CTI, 
					UPPER(SRAISONSOCIALEBCB), 
					UPPER(SRAISONSOCIALEBCB), 
					EIDLABORATOIRE
				FROM w_LABORATOIRE
					LEFT JOIN eco.t_fournisseurs ON code_original = EIDLABORATOIRE
				WHERE code_original IS NULL;

				UPDATE eco.t_fournisseurs
				SET code = CODE_CTI,
					texte = UPPER(SRAISONSOCIALEBCB),
					texte_court = UPPER(SRAISONSOCIALEBCB),
					adresse = trim(SADRESSE1 || ' ' || SADRESSE2),
					code_postal_id = COALESCE(t_codes_postaux.oid,0),
					ville = SVILLE,
					telephone = STELEPHONE,
					fax = SFAX,
					email = SEMAIL,
					siret = SSIRET
				FROM w_LABORATOIRE
				LEFT JOIN base.t_codes_postaux ON SCODEPOSTAL = t_codes_postaux.code
				WHERE code_original = EIDLABORATOIRE
				AND (
					t_fournisseurs.code IS DISTINCT FROM CODE_CTI OR
					t_fournisseurs.texte IS DISTINCT FROM UPPER(SRAISONSOCIALEBCB) OR
					t_fournisseurs.texte_court IS DISTINCT FROM UPPER(SRAISONSOCIALEBCB) OR
					t_fournisseurs.adresse IS DISTINCT FROM trim(SADRESSE1 || ' ' || SADRESSE2) OR
					t_fournisseurs.code_postal_id IS DISTINCT FROM COALESCE(t_codes_postaux.oid,0) OR
					t_fournisseurs.ville IS DISTINCT FROM SVILLE OR
					t_fournisseurs.telephone IS DISTINCT FROM STELEPHONE OR
					t_fournisseurs.fax IS DISTINCT FROM SFAX OR
					t_fournisseurs.email IS DISTINCT FROM SEMAIL OR
					t_fournisseurs.siret IS DISTINCT FROM SSIRET
					);
					
				UPDATE eco.t_fournisseurs SET
					code_comptable = SNUMCOMPTE,
					fournisseur_distributeur_id = subview.fournisseur_distributeur_id
				FROM
				(
				SELECT EIDLABORATOIRE,
					MAX(SNUMCOMPTE) AS SNUMCOMPTE,
					MAX(t_fournisseurs_distributeur.oid) AS fournisseur_distributeur_id
				FROM prod_winpharm.GE_LABORATOIRE_ETABLISSEMENT
				LEFT JOIN eco.t_fournisseurs AS t_fournisseurs_distributeur ON EIDLABORATOIREDIST = t_fournisseurs_distributeur.code_original
				GROUP BY 1
				) subview
				WHERE subview.EIDLABORATOIRE = t_fournisseurs.code_original AND
					(
					t_fournisseurs.code_comptable IS DISTINCT FROM SNUMCOMPTE OR
					t_fournisseurs.fournisseur_distributeur_id IS DISTINCT FROM subview.fournisseur_distributeur_id
					)
				;
					
				
				DROP TABLE IF EXISTS w_GE_ARTICLE_LABORATOIRE;
				CREATE TEMP TABLE w_GE_ARTICLE_LABORATOIRE AS
				SELECT GE_ARTICLE_LABORATOIRE.*,
					EIDARTICLELABORATOIRE::text AS code_original,
					COALESCE(t_articles.oid,0) AS article_id,
					COALESCE(t_fournisseurs.oid,0) AS fournisseur_id,
					COALESCE(t_fournisseurs_distributeur.oid,0) AS fournisseur_distributeur_id,
					CASE WHEN BFABRICANTP = 1 THEN '1' ELSE '0' END AS est_fournisseur_principal,
					CASE
					WHEN SREFERENCE <> '' THEN SREFERENCE
					WHEN SUCD <> '' THEN SUCD
					ELSE '' END AS code_reference_fournisseur,
					GE_ARTICLE.SLIBELLECOURT AS texte_reference_fournisseur,
					SCIP13 AS code_cip,
					''::text AS code_cahpp,
					COALESCE(t_unites.oid,0) AS unite_approvisionnement_id,
					GE_ARTICLE_LABORATOIRE.ENBUCD AS nombre_conditionnement_approvisionnement,
					EDPXACHATHT AS prix_unitaire_en_cours,
					0::bigint AS ucd_id,
					CASE 
						WHEN SUCD13 LIKE '34008%' AND length(SUCD13) = 13 THEN substr(SUCD13,6,7)
						WHEN SREFERENCE LIKE '34008%' AND length(SREFERENCE) = 13 THEN substr(SREFERENCE,6,7)
						WHEN TH_PRODUIT.SUCD LIKE '34008%' AND length(TH_PRODUIT.SUCD) = 13 THEN substr(TH_PRODUIT.SUCD,6,7)
						WHEN length(TH_PRODUIT.SUCD) = 7 AND TH_PRODUIT.SUCD LIKE '9%' THEN TH_PRODUIT.SUCD
						ELSE '' END AS code_ucd, 
					CASE 
						WHEN SUCD13 LIKE '34008%' AND length(SUCD13) = 13 THEN SUCD13
						WHEN SREFERENCE LIKE '34008%' AND length(SREFERENCE) = 13 THEN SREFERENCE
						WHEN TH_PRODUIT.SUCD LIKE '34008%' AND length(TH_PRODUIT.SUCD) = 13 THEN TH_PRODUIT.SUCD
						ELSE '' END AS code_ucd13,
					COALESCE(t_lpp.oid,0) AS lpp_id
				FROM prod_winpharm.GE_ARTICLE_LABORATOIRE
				JOIN prod_winpharm.GE_ARTICLE ON GE_ARTICLE_LABORATOIRE.EIDARTICLE = GE_ARTICLE.EIDARTICLE
				LEFT JOIN prod_winpharm.TH_PRODUIT ON GE_ARTICLE.EIDPRODUIT = TH_PRODUIT.EIDPRODUIT
				LEFT JOIN prod_winpharm.TH_LABORATOIRE ON GE_ARTICLE_LABORATOIRE.EIDLABORATOIRE = TH_LABORATOIRE.EIDLABORATOIRE AND GE_ARTICLE_LABORATOIRE.EIDLABORATOIRE > 0
				LEFT JOIN eco.t_articles ON GE_ARTICLE_LABORATOIRE.EIDARTICLE = t_articles.code_original
				LEFT JOIN eco.t_fournisseurs ON GE_ARTICLE_LABORATOIRE.EIDLABORATOIRE = t_fournisseurs.code_original
				LEFT JOIN eco.t_fournisseurs t_fournisseurs_distributeur ON TH_LABORATOIRE.EIDLABORATOIREDIST = t_fournisseurs_distributeur.code_original
				LEFT JOIN eco.t_unites ON GE_ARTICLE_LABORATOIRE.EIDCONDITIONNEMENT = t_unites.code_original
				LEFT JOIN base.t_lpp ON TH_PRODUIT.SCODELPP = t_lpp.code
				;
				
				
				
				-- References article laboratoire manquantes
				DROP TABLE IF EXISTS w_ARTICLE_LABORATOIRE_notexists;
				CREATE TEMP TABLE w_ARTICLE_LABORATOIRE_notexists AS
				SELECT GE_ENTETE.EIDLABORATOIRE,
					GE_LIGNE.EIDARTICLE,
					MAX(EIDLIGNE) AS EIDLIGNE
				FROM prod_winpharm.GE_ENTETE
				JOIN prod_winpharm.GE_LIGNE ON GE_ENTETE.EIDENTETE = GE_LIGNE.EIDENTETE
				LEFT JOIN prod_winpharm.GE_ARTICLE_LABORATOIRE ON
					GE_ARTICLE_LABORATOIRE.EIDARTICLE = GE_LIGNE.EIDARTICLE AND
					GE_ARTICLE_LABORATOIRE.EIDLABORATOIRE = GE_ENTETE.EIDLABORATOIRE
				WHERE GE_ARTICLE_LABORATOIRE.EIDARTICLE IS NULL AND
					GE_ENTETE.EDATECOMMANDE <> ''
				GROUP BY 1,2
				;

				ANALYSE w_ARTICLE_LABORATOIRE_notexists
				;
				
				INSERT INTO w_GE_ARTICLE_LABORATOIRE
				SELECT GE_ARTICLE_LABORATOIRE.*,
					GE_LIGNE.EIDARTICLE::text || '-'::text || GE_ENTETE.EIDLABORATOIRE::text AS code_original,
					COALESCE(t_articles.oid,0) AS article_id,
					COALESCE(t_fournisseurs.oid,0) AS fournisseur_id,
					COALESCE(t_fournisseurs_distributeur.oid,t_fournisseurs.oid,0) AS fournisseur_distributeur_id,
					CASE WHEN BFABRICANTP = 1 THEN '1' ELSE '0' END AS est_fournisseur_principal,
					CASE
					WHEN SREFERENCE <> '' THEN SREFERENCE
					WHEN SUCD <> '' THEN SUCD
					ELSE '' END AS code_reference_fournisseur,
					GE_ARTICLE.SLIBELLECOURT AS texte_reference_fournisseur,
					SCIP13 AS code_cip,
					''::text AS code_cahpp,
					COALESCE(t_unites.oid,0) AS unite_approvisionnement_id,
					GE_ARTICLE_LABORATOIRE.ENBUCD AS nombre_conditionnement_approvisionnement,
					GREATEST(EPXUNITHTCIP,EPXUNITHTUCD) AS prix_unitaire_en_cours,
					0::bigint AS ucd_id,
					CASE 
						WHEN SUCD13 LIKE '34008%' AND length(SUCD13) = 13 THEN substr(SUCD13,6,7)
						WHEN SREFERENCE LIKE '34008%' AND length(SREFERENCE) = 13 THEN substr(SREFERENCE,6,7)
						WHEN TH_PRODUIT.SUCD LIKE '34008%' AND length(TH_PRODUIT.SUCD) = 13 THEN substr(TH_PRODUIT.SUCD,6,7)
						WHEN length(TH_PRODUIT.SUCD) = 7 AND TH_PRODUIT.SUCD LIKE '9%' THEN TH_PRODUIT.SUCD
						ELSE '' END AS code_ucd, 
					CASE 
						WHEN SUCD13 LIKE '34008%' AND length(SUCD13) = 13 THEN SUCD13
						WHEN SREFERENCE LIKE '34008%' AND length(SREFERENCE) = 13 THEN SREFERENCE
						WHEN TH_PRODUIT.SUCD LIKE '34008%' AND length(TH_PRODUIT.SUCD) = 13 THEN TH_PRODUIT.SUCD
						ELSE '' END AS code_ucd13,
					COALESCE(t_lpp.oid,0) AS lpp_id
				FROM prod_winpharm.GE_ENTETE
				JOIN prod_winpharm.GE_LIGNE ON GE_ENTETE.EIDENTETE = GE_LIGNE.EIDENTETE
				JOIN w_ARTICLE_LABORATOIRE_notexists ON GE_LIGNE.EIDLIGNE = w_ARTICLE_LABORATOIRE_notexists.EIDLIGNE
				JOIN prod_winpharm.GE_ARTICLE ON GE_ARTICLE.EIDARTICLE = GE_LIGNE.EIDARTICLE
				LEFT JOIN prod_winpharm.GE_ARTICLE_LABORATOIRE ON
					GE_ARTICLE_LABORATOIRE.EIDARTICLE = GE_LIGNE.EIDARTICLE AND
					GE_ARTICLE_LABORATOIRE.EIDLABORATOIRE = GE_ENTETE.EIDLABORATOIRE
				LEFT JOIN prod_winpharm.TH_PRODUIT ON GE_ARTICLE.EIDPRODUIT = TH_PRODUIT.EIDPRODUIT
				LEFT JOIN prod_winpharm.TH_LABORATOIRE ON GE_ENTETE.EIDLABORATOIRE = TH_LABORATOIRE.EIDLABORATOIRE AND GE_ARTICLE_LABORATOIRE.EIDLABORATOIRE > 0
				LEFT JOIN eco.t_fournisseurs t_fournisseurs_distributeur ON TH_LABORATOIRE.EIDLABORATOIREDIST = t_fournisseurs_distributeur.code_original
				JOIN eco.t_articles ON GE_LIGNE.EIDARTICLE = t_articles.code_original
				JOIN eco.t_fournisseurs ON GE_ENTETE.EIDLABORATOIRE = t_fournisseurs.code_original 
				LEFT JOIN eco.t_unites ON '*' = t_unites.code_original
				LEFT JOIN base.t_lpp ON TH_PRODUIT.SCODELPP = t_lpp.code
				WHERE GE_ARTICLE_LABORATOIRE.EIDARTICLE IS NULL
				;
				
				-- Doublons
				DELETE FROM w_GE_ARTICLE_LABORATOIRE
				USING
				(
				SELECT EIDARTICLE, EIDLABORATOIRE, count(*), MAX(BFABRICANTP), MIN(BFABRICANTP), (MAX(ARRAY[BFABRICANTP,EIDARTICLELABORATOIRE]))[2]::bigint AS EIDARTICLELABORATOIRE_keep
				FROM w_GE_ARTICLE_LABORATOIRE
				WHERE EIDARTICLE IS NOT NULL
				GROUP BY 1,2
				HAVING count(*) > 1
				) subview
				WHERE w_GE_ARTICLE_LABORATOIRE.EIDARTICLE = subview.EIDARTICLE AND
					w_GE_ARTICLE_LABORATOIRE.EIDLABORATOIRE = subview.EIDLABORATOIRE AND
					w_GE_ARTICLE_LABORATOIRE.EIDARTICLELABORATOIRE <> subview. EIDARTICLELABORATOIRE_keep
				;


				INSERT INTO base.t_ucd (code, texte, texte_court)
				SELECT 
					code_ucd, 
					MIN(code_ucd) AS texte, 
					MIN(code_ucd) AS texte_court
				FROM w_GE_ARTICLE_LABORATOIRE
				WHERE code_ucd <> '' AND
					code_ucd NOT IN (SELECT code FROM base.t_ucd)
				GROUP BY 1
				;


				UPDATE w_GE_ARTICLE_LABORATOIRE
				SET ucd_id = t_ucd.oid
				FROm base.t_ucd
				WHERE t_ucd.code = code_ucd
				;
				
				INSERT INTO eco.t_article_fournisseur(
					code_original, 
					article_id, 
					fournisseur_id,
					fournisseur_distributeur_id, 
					est_fournisseur_principal, 
					code_reference_fournisseur, 
					texte_reference_fournisseur, 
					code_cip, 
					code_cahpp, 
					unite_approvisionnement_id, 
					nombre_conditionnement_approvisionnement, 
					prix_unitaire_en_cours, 
					ucd_id, 
					code_ucd13,
					lpp_id
					)
				SELECT 
					code_original, 
					article_id, 
					fournisseur_id,
					fournisseur_distributeur_id, 
					est_fournisseur_principal, 
					code_reference_fournisseur, 
					texte_reference_fournisseur, 
					code_cip, 
					code_cahpp, 
					unite_approvisionnement_id, 
					nombre_conditionnement_approvisionnement, 
					prix_unitaire_en_cours, 
					ucd_id, 
					code_ucd13,
					lpp_id
				FROM w_GE_ARTICLE_LABORATOIRE
				WHERE code_original NOT IN (SELECt code_original FROM eco.t_article_fournisseur)
				;
				
				UPDATE eco.t_article_fournisseur SET
					article_id =                                w_GE_ARTICLE_LABORATOIRE.article_id, 
					fournisseur_id =                            w_GE_ARTICLE_LABORATOIRE.fournisseur_id,
					fournisseur_distributeur_id =               w_GE_ARTICLE_LABORATOIRE.fournisseur_distributeur_id, 
					est_fournisseur_principal =                 w_GE_ARTICLE_LABORATOIRE.est_fournisseur_principal, 
					code_reference_fournisseur =                w_GE_ARTICLE_LABORATOIRE.code_reference_fournisseur, 
					texte_reference_fournisseur =               w_GE_ARTICLE_LABORATOIRE.texte_reference_fournisseur, 
					code_cip =                                  w_GE_ARTICLE_LABORATOIRE.code_cip, 
					code_cahpp =                                w_GE_ARTICLE_LABORATOIRE.code_cahpp, 
					unite_approvisionnement_id =                w_GE_ARTICLE_LABORATOIRE.unite_approvisionnement_id, 
					nombre_conditionnement_approvisionnement =  w_GE_ARTICLE_LABORATOIRE.nombre_conditionnement_approvisionnement, 
					prix_unitaire_en_cours =                    w_GE_ARTICLE_LABORATOIRE.prix_unitaire_en_cours, 
					ucd_id =                                    w_GE_ARTICLE_LABORATOIRE.ucd_id, 
					code_ucd13 =                                w_GE_ARTICLE_LABORATOIRE.code_ucd13,
					lpp_id =                                    w_GE_ARTICLE_LABORATOIRE.lpp_id
				FROM w_GE_ARTICLE_LABORATOIRE
				WHERE t_article_fournisseur.code_original = w_GE_ARTICLE_LABORATOIRE.code_original AND
					(
					t_article_fournisseur.article_id IS DISTINCT FROM                                w_GE_ARTICLE_LABORATOIRE.article_id OR 
						t_article_fournisseur.fournisseur_id IS DISTINCT FROM                            w_GE_ARTICLE_LABORATOIRE.fournisseur_id OR
						t_article_fournisseur.fournisseur_distributeur_id IS DISTINCT FROM               w_GE_ARTICLE_LABORATOIRE.fournisseur_distributeur_id OR 
						t_article_fournisseur.est_fournisseur_principal IS DISTINCT FROM                 w_GE_ARTICLE_LABORATOIRE.est_fournisseur_principal OR 
						t_article_fournisseur.code_reference_fournisseur IS DISTINCT FROM                w_GE_ARTICLE_LABORATOIRE.code_reference_fournisseur OR 
						t_article_fournisseur.texte_reference_fournisseur IS DISTINCT FROM               w_GE_ARTICLE_LABORATOIRE.texte_reference_fournisseur OR 
						t_article_fournisseur.code_cip IS DISTINCT FROM                                  w_GE_ARTICLE_LABORATOIRE.code_cip OR 
						t_article_fournisseur.code_cahpp IS DISTINCT FROM                                w_GE_ARTICLE_LABORATOIRE.code_cahpp OR 
						t_article_fournisseur.unite_approvisionnement_id IS DISTINCT FROM                w_GE_ARTICLE_LABORATOIRE.unite_approvisionnement_id OR 
						t_article_fournisseur.nombre_conditionnement_approvisionnement IS DISTINCT FROM  w_GE_ARTICLE_LABORATOIRE.nombre_conditionnement_approvisionnement OR 
						t_article_fournisseur.prix_unitaire_en_cours IS DISTINCT FROM                    w_GE_ARTICLE_LABORATOIRE.prix_unitaire_en_cours OR 
						t_article_fournisseur.ucd_id IS DISTINCT FROM                                    w_GE_ARTICLE_LABORATOIRE.ucd_id OR 
						t_article_fournisseur.code_ucd13 IS DISTINCT FROM                                w_GE_ARTICLE_LABORATOIRE.code_ucd13 OR
						t_article_fournisseur.lpp_id IS DISTINCT FROM                                    w_GE_ARTICLE_LABORATOIRE.lpp_id
					)
				;
				
				DELETE FROM eco.t_article_fournisseur
				WHERE code_original NOT IN (SELECT code_original FROM w_GE_ARTICLE_LABORATOIRE)
				;
				
				-- Suppression doublons
				DELETE FROM eco.t_article_fournisseur
				USING
				(
				SELECT fournisseur_id, article_id, count(*), 
					(MAX(ARRAY[BFABRICANTP,EIDARTICLELABORATOIRE]))[2]::text AS keep_code_original
				FROM eco.t_article_fournisseur
				JOIN prod_winpharm.GE_ARTICLE_LABORATOIRE ON code_original = EIDARTICLELABORATOIRE
				GROUP BY 1,2
				HAVING count(*) > 1
				) subview
				WHERE t_article_fournisseur.fournisseur_id = subview.fournisseur_id AND
					t_article_fournisseur.article_id = subview.article_id AND
					t_article_fournisseur.code_original <> subview.keep_code_original
				;
					

			]]></sqlcmd>
		</NODE>
		
		
		
		<NODE label="Autres tables">
			<sqlcmd><![CDATA[
			
				-- Lieux
				INSERT INTO eco.t_lieux(code, code_original, texte, texte_court)
				SELECT 
					EIDETABLISSEMENT, 
					EIDETABLISSEMENT, 
					SNOMETABLISSEMENT, 
					SNOMETABLISSEMENT
				FROM prod_winpharm.GE_ETABLISSEMENT
				JOIN w_ETABLISSEMENT ON EIDETABLISSEMENT = EIDETABLISSEMENT_ok
				WHERE EIDETABLISSEMENT NOT IN (SELECT code_original FROM eco.t_lieux)
				;
			
				-- Gestionnaires
				
				
				
				
							
				
				-- Unités fonctionnelles
				
				INSERT INTO eco.t_unites_fonctionnelles(code, texte, texte_court, code_original)
				SELECT GE_UNITEMEDICALE.EIDETABLISSEMENT || '-' || SCODEUM, SNOMUM, substr(SNOMUM,1,50), EIDUNITEMEDICALE
				FROM prod_winpharm.GE_UNITEMEDICALE 
				JOIN prod_winpharm.GE_ETABLISSEMENT ON GE_UNITEMEDICALE.EIDETABLISSEMENT = GE_ETABLISSEMENT.EIDETABLISSEMENT
				JOIN w_ETABLISSEMENT ON GE_UNITEMEDICALE.EIDETABLISSEMENT = EIDETABLISSEMENT_ok
				LEFT JOIN eco.t_unites_fonctionnelles ON (code_original = EIDUNITEMEDICALE)
				WHERE code_original IS NULL ;
				
				UPDATE eco.t_unites_fonctionnelles SET
					code = GE_UNITEMEDICALE.EIDETABLISSEMENT || '-' || SCODEUM,
					texte = SNOMUM,
					texte_court = substr(SNOMUM,1,50)
				FROM prod_winpharm.GE_UNITEMEDICALE
				WHERE code_original = EIDUNITEMEDICALE
				AND (
					code IS DISTINCT FROM (GE_UNITEMEDICALE.EIDETABLISSEMENT || '-' || SCODEUM) OR
					texte IS DISTINCT FROM SNOMUM OR
					texte_court IS DISTINCT FROM substr(SNOMUM,1,50)
				);
				
					
				-- Site

				
				
				-- Types mouvements
				INSERT INTO eco.t_types_mouvements(oid, code, texte, texte_court, code_original)
				SELECT ETYPEMOUVEMENT,
					to_char(ETYPEMOUVEMENT,'FM00'), 
					ETYPEMOUVEMENT, 
					ETYPEMOUVEMENT, 
					ETYPEMOUVEMENT
				FROM prod_winpharm.GE_MOUVEMENT
					LEFT JOIN eco.t_types_mouvements ON oid = ETYPEMOUVEMENT
				WHERE code_original IS NULL
				GROUP BY 1;
			

			]]></sqlcmd>
		</NODE>
		
		
	</NODE>
	<NODE name="PROD" label="RECUPERATION DES DONNEES DE PRODUCTION">
	
		<NODE label="Préparation">
			<sqlcmd><![CDATA[
			
				
			
			]]></sqlcmd>
		</NODE>
			
			
		<NODE label="Commandes">
			<sqlcmd><![CDATA[
			
				-- Création fictive d'un numéro de ligne de commande
				DROP TABLE IF EXISTS w_LIGNE_NUMERO;
				CREATE TEMP TABLE w_LIGNE_NUMERO AS
				SELECT EIDLIGNE, 
					row_number() OVER (PARTITION BY EIDENTETE ORDER BY EIDLIGNE) AS ligne_commande
				FROM prod_winpharm.GE_LIGNE
				;
			
				-- Table temporaire synthétique des lignes (commande, regroupant livraison, facturation)
				
				DROP TABLE IF EXISTS w_LIGNE;
				CREATE TEMP TABLE w_LIGNE AS
				SELECT 
					
					ge_ligne_commande.EIDLIGNE AS lc_EIDLIGNE,
					ge_ligne_commande.EIDARTICLE AS lc_EIDARTICLE,
					ge_ligne_commande.EIDCOMPTE AS lc_EIDCOMPTE,
					ge_ligne_commande.EETAT AS lc_EETAT,
					ge_ligne_commande.BCLOTURE AS lc_BCLOTURE,
					ge_ligne_commande.BSOLDE AS lc_BSOLDE,
					ge_ligne_commande.EQTEDEP AS lc_EQTEDEP,
					ge_ligne_commande.EQTEINIT AS lc_EQTEINIT,
					ge_ligne_commande.EQTEREST AS lc_EQTEREST,
					ge_ligne_commande.EQTEINIT AS lc_EQTE,
					ge_ligne_commande.EMONTHTCIP + ge_ligne_commande.EMONTHTUCD AS lc_ETOTALHT,
					ge_ligne_commande.EMONTTVACIP + ge_ligne_commande.EMONTTVAUCD AS lc_ETOTALTVA,
					ge_ligne_commande.EMONTTTCCIP + ge_ligne_commande.EMONTTTCUCD AS lc_ETOTALTTC,
					ge_ligne_commande.EPXUNITHTCIP AS lc_EPXUNITHTCIP,
					ge_ligne_commande.EPXUNITHTUCD AS lc_EPXUNITHTUCD,
					round(base.cti_division(ge_ligne_commande.EMONTTVACIP + ge_ligne_commande.EMONTTVAUCD,ge_ligne_commande.EMONTHTCIP + ge_ligne_commande.EMONTHTUCD)*100,1) AS ETAUXTVA_calc,
					0::NUMERIC AS ETAUXTVA,
					0::numeric AS lc_EPXUNITHT,
					0::numeric AS lc_EPXUNITTTC,

					ge_ligne_livraison.EIDLIGNE AS ll_EIDLIGNE,
					ge_ligne_livraison.EIDARTICLE AS ll_EIDARTICLE,
					COALESCE(ge_ligne_livraison.EIDCOMPTE,0) AS ll_EIDCOMPTE,
					COALESCE(ge_ligne_livraison.EETAT,'0') AS ll_EETAT,
					COALESCE(ge_ligne_livraison.BCLOTURE,0) AS ll_BCLOTURE,
					COALESCE(ge_ligne_livraison.EQTEDEP,0) AS ll_EQTEDEP,
					COALESCE(ge_ligne_livraison.EQTEINIT,0) AS ll_EQTEINIT,
					COALESCE(ge_ligne_livraison.EQTEREST,0) AS ll_EQTEREST,
					COALESCE(ge_ligne_livraison.EQTEINIT,0) AS ll_EQTE,
					COALESCE(ge_ligne_livraison.EMONTHTCIP + ge_ligne_livraison.EMONTHTUCD,0) AS ll_ETOTALHT,
					COALESCE(ge_ligne_livraison.EMONTTVACIP + ge_ligne_livraison.EMONTTVAUCD,0) AS ll_ETOTALTVA,
					COALESCE(ge_ligne_livraison.EMONTTTCCIP + ge_ligne_livraison.EMONTTTCUCD,0) AS ll_ETOTALTTC,
					ge_ligne_livraison.EPXUNITHTCIP AS ll_EPXUNITHTCIP,
					ge_ligne_livraison.EPXUNITHTUCD AS ll_EPXUNITHTUCD,
					0::numeric AS ll_EPXUNITHT,
					0::numeric AS ll_EPXUNITTTC,

					ge_ligne_facturation.EIDLIGNE AS lf_EIDLIGNE,
					ge_ligne_facturation.EIDARTICLE AS lf_EIDARTICLE,
					COALESCE(ge_ligne_facturation.EIDCOMPTE,0) AS lf_EIDCOMPTE,
					COALESCE(ge_ligne_facturation.EETAT,0) AS lf_EETAT,
					COALESCE(ge_ligne_facturation.BCLOTURE,0) AS lf_BCLOTURE,
					COALESCE(ge_ligne_facturation.EQTEDEP,0) AS lf_EQTEDEP,
					COALESCE(ge_ligne_facturation.EQTEINIT,0) AS lf_EQTEINIT,
					COALESCE(ge_ligne_facturation.EQTEREST,0) AS lf_EQTEREST,
					COALESCE(ge_ligne_facturation.EQTEINIT,0) AS lf_EQTE,
					COALESCE(ge_ligne_facturation.EMONTHTCIP,0) + ge_ligne_facturation.EMONTHTUCD AS lf_ETOTALHT,
					COALESCE(ge_ligne_facturation.EMONTTVACIP,0) + ge_ligne_facturation.EMONTTVAUCD AS lf_ETOTALTVA,
					COALESCE(ge_ligne_facturation.EMONTTTCCIP,0) + ge_ligne_facturation.EMONTTTCUCD AS lf_ETOTALTTC,
					ge_ligne_facturation.EPXUNITHTCIP AS lf_EPXUNITHTCIP,
					ge_ligne_facturation.EPXUNITHTUCD AS lf_EPXUNITHTUCD,
					0::numeric AS lf_EPXUNITHT,
					0::numeric AS lf_EPXUNITTTC,


					ge_entete_commande.EIDENTETE AS ec_EIDENTETE,
					ge_entete_commande.EIDETABLISSEMENT AS ec_EIDETABLISSEMENT,
					ge_entete_commande.EIDLABORATOIRE AS ec_EIDLABORATOIRE,
					ge_entete_commande.EETAT AS ec_EETAT,
					ge_entete_commande.BCLOTURE AS ec_BCLOTURE,
					ge_entete_commande.ENOBON AS ec_ENOBON,
					to_date(ge_entete_commande.EDATECOMMANDE,'YYYYMMDD') AS ec_EDATECOMMANDE,
					ge_entete_commande.ETOTALHT AS ec_ETOTALHT,
					ge_entete_commande.ETOTALTVA AS ec_ETOTALTVA,
					ge_entete_commande.ETOTALTTC AS ec_ETOTALTTC,

					ge_entete_livraison.EIDENTETE AS el_EIDENTETE,
					ge_entete_livraison.EIDETABLISSEMENT AS el_EIDETABLISSEMENT,
					ge_entete_livraison.EIDLABORATOIRE AS el_EIDLABORATOIRE,
					COALESCE(ge_entete_livraison.EETAT,0) AS el_EETAT,
					COALESCE(ge_entete_livraison.BCLOTURE,0) AS el_BCLOTURE,
					COALESCE(ge_entete_livraison.ENOBON,'') AS el_ENOBON,
					to_date(COALESCE(ge_entete_livraison.EDATELIVRAISON,'20991231'),'YYYYMMDD') AS el_EDATELIVRAISON,
					COALESCE(ge_entete_livraison.ETOTALHT,0) AS el_ETOTALHT,
					COALESCE(ge_entete_livraison.ETOTALTVA,0) AS el_ETOTALTVA,
					COALESCE(ge_entete_livraison.ETOTALTTC,0) AS el_ETOTALTTC,

					ge_entete_facturation.EIDENTETE AS ef_EIDENTETE,
					ge_entete_facturation.EIDETABLISSEMENT AS ef_EIDETABLISSEMENT,
					ge_entete_facturation.EIDLABORATOIRE AS ef_EIDLABORATOIRE,
					COALESCE(ge_entete_facturation.EETAT,0) AS ef_EETAT,
					COALESCE(ge_entete_facturation.BCLOTURE,0) AS ef_BCLOTURE,
					COALESCE(ge_entete_facturation.ENOBON,'') AS ef_ENOBON,
					to_date(COALESCE(ge_entete_facturation.EDATEFACTURATION,'20991231'),'YYYYMMDD') AS ef_EDATEFACTURATION,
					COALESCE(ge_entete_facturation.ETOTALHT,0) AS ef_ETOTALHT,
					COALESCE(ge_entete_facturation.ETOTALTVA,0) AS ef_ETOTALTVA,
					COALESCE(ge_entete_facturation.ETOTALTTC,0) AS ef_ETOTALTTC

					

					
				FROM prod_winpharm.ge_entete ge_entete_commande
					JOIN w_ETABLISSEMENT ON GE_ENTETE_COMMANDE.EIDETABLISSEMENT = EIDETABLISSEMENT_ok
					JOIN prod_winpharm.ge_ligne ge_ligne_commande ON ge_entete_commande.EIDENTETE = ge_ligne_commande.EIDENTETE
					LEFT JOIN prod_winpharm.ge_ligne ge_ligne_livraison ON ge_ligne_livraison.EIDLIGNECOMMANDE = ge_ligne_commande.EIDLIGNE
					LEFT JOIN prod_winpharm.ge_ligne ge_ligne_facturation ON ge_ligne_facturation.EIDLIGNELIVRAISON = ge_ligne_livraison.EIDLIGNE
					LEFT JOIN prod_winpharm.ge_entete ge_entete_livraison ON ge_ligne_livraison.EIDENTETE = ge_entete_livraison.EIDENTETE
					LEFT JOIN prod_winpharm.ge_entete ge_entete_facturation ON ge_ligne_facturation.EIDENTETE = ge_entete_facturation.EIDENTETE
				WHERE ge_entete_commande.EDATECOMMANDE <> '';
				
				UPDATE w_LIGNE
				SET ETAUXTVA = TH_TAUXTVA.ETAUXTVA
				FROM prod_winpharm.GE_COMPTE 
				JOIN prod_winpharm.TH_TAUXTVA ON GE_COMPTE.EIDTAUXTVA = TH_TAUXTVA.EIDTAUXTVA
				WHERE w_LIGNE.lf_EIDCOMPTE = GE_COMPTE.EIDCOMPTE AND
					w_LIGNE.ETAUXTVA = 0
				;

				UPDATE w_LIGNE
				SET ETAUXTVA = TH_TAUXTVA.ETAUXTVA
				FROM prod_winpharm.GE_COMPTE 
				JOIN prod_winpharm.TH_TAUXTVA ON GE_COMPTE.EIDTAUXTVA = TH_TAUXTVA.EIDTAUXTVA
				WHERE w_LIGNE.lc_EIDCOMPTE = GE_COMPTE.EIDCOMPTE AND
					w_LIGNE.ETAUXTVA = 0
				;

				UPDATE w_LIGNE
				SET ETAUXTVA = TH_TAUXTVA.ETAUXTVA
				FROM prod_winpharm.GE_ARTICLE
				JOIN prod_winpharm.GE_COMPTE ON GE_ARTICLE.EIDCOMPTE = GE_COMPTE.EIDCOMPTE
				JOIN prod_winpharm.TH_TAUXTVA ON GE_COMPTE.EIDTAUXTVA = TH_TAUXTVA.EIDTAUXTVA
				WHERE w_LIGNE.lc_EIDARTICLE = GE_ARTICLE.EIDARTICLE AND
					w_LIGNE.ETAUXTVA = 0
				;


				UPDATE w_LIGNE
				SET ETAUXTVA = 19.6
				WHERE ETAUXTVA = 20 AND
					ec_EDATECOMMANDE <= '20131231'
				;

				UPDATE w_LIGNE
				SET ETAUXTVA_calc = ETAUXTVA
				WHERE ETAUXTVA_calc BETWEEN ETAUXTVA - 0.3 AND ETAUXTVA+0.3 AND
					ETAUXTVA_calc <> ETAUXTVA
				;


				UPDATE w_LIGNE
				SET ETAUXTVA_calc =
					CASE 
					WHEN ETAUXTVA_calc BETWEEN 1 AND 3.9 THEN 2.1
					WHEN ETAUXTVA_calc BETWEEN 4 AND 7 THEN 5.5
					WHEN ETAUXTVA_calc BETWEEN 9 AND 11 THEN 10
					WHEN ETAUXTVA_calc BETWEEN 18 AND 21 AND ec_EDATECOMMANDE <= '20131231' THEN 19.6
					WHEN ETAUXTVA_calc BETWEEN 18 AND 21 THEN 20
					ELSE w_LIGNE.ETAUXTVA_calc END
				WHERE ETAUXTVA_calc <> 0 AND
					ETAUXTVA_calc NOT IN (SELECT ETAUXTVA FROM prod_winpharm.TH_TAUXTVA)
				;

				UPDATE w_LIGNE
				SET ETAUXTVA_calc = ETAUXTVA
				WHERE ETAUXTVA_calc <> 0 AND
					ETAUXTVA<> 0 AND
					ETAUXTVA_calc NOT IN (SELECT ETAUXTVA FROM prod_winpharm.TH_TAUXTVA)
				;


				UPDATE w_ligne
				SET lc_bsolde = 1 
				WHERE lc_bsolde = 0 AND ec_bcloture = 1;

				UPDATE w_ligne
				SET ll_EQTE = ll_EQTEDEP
				WHERE ll_EQTEDEP <> ll_EQTE AND
					ll_EQTEDEP <> 0 AND
					ll_ETOTALHT <> 0 AND
					ll_EPXUNITHTUCD <> 0 AND
					round(ll_EQTEDEP*ll_EPXUNITHTUCD,0) = round(ll_ETOTALHT,0);

				UPDATE w_ligne
				SET ll_EQTE = ll_EQTEDEP
				WHERE ll_EQTEDEP <> ll_EQTE AND
					ll_EQTEDEP <> 0 AND
					ll_ETOTALHT <> 0 AND
					ll_EPXUNITHTCIP <> 0 AND
					round(ll_EQTEDEP*ll_EPXUNITHTCIP,0) = round(ll_ETOTALHT,0);

				UPDATE w_ligne
				SET lf_EQTE = lf_EQTEDEP
				WHERE lf_EQTEDEP <> lf_EQTE AND
					lf_EQTEDEP <> 0 AND
					lf_ETOTALHT <> 0 AND
					lf_EPXUNITHTUCD <> 0 AND
					round(lf_EQTEDEP*lf_EPXUNITHTUCD,0) = round(lf_ETOTALHT,0);

				UPDATE w_ligne
				SET lf_EQTE = lf_EQTEDEP
				WHERE lf_EQTEDEP <> lf_EQTE AND
					lf_EQTEDEP <> 0 AND
					lf_ETOTALHT <> 0 AND
					lf_EPXUNITHTCIP <> 0 AND
					round(lf_EQTEDEP*lf_EPXUNITHTCIP,0) = round(lf_ETOTALHT,0);

				UPDATE w_ligne SET
					lc_EQTE = ll_EQTE,
					lc_ETOTALHT = ll_ETOTALHT,
					lc_ETOTALTVA = ll_ETOTALTVA,
					lc_ETOTALTTC = ll_ETOTALTTC,
					lc_EPXUNITHTCIP = ll_EPXUNITHTCIP,
					lc_EPXUNITHTUCD = ll_EPXUNITHTUCD,
					lc_EPXUNITHT = ll_EPXUNITHT,
					lc_EPXUNITTTC = ll_EPXUNITTTC
				WHERE LC_BSOLDE = 1 AND
					(
					lc_EQTE <> ll_EQTE OR
					lc_ETOTALHT <> ll_ETOTALHT OR
					lc_ETOTALTVA <> ll_ETOTALTVA OR
					lc_ETOTALTTC <> ll_ETOTALTTC OR
					lc_EPXUNITHTCIP <> ll_EPXUNITHTCIP OR
					lc_EPXUNITHTUCD <> ll_EPXUNITHTUCD OR
					lc_EPXUNITHT <> ll_EPXUNITHT OR
					lc_EPXUNITTTC <> ll_EPXUNITTTC
					)
					;


				UPDATE w_LIGNE
				SET lc_EIDCOMPTE = lf_EIDCOMPTE
				WHERE lc_EIDCOMPTE <> lf_EIDCOMPTE AND
					lf_EIDCOMPTE <> 0;
		
				-- Validation commandes
				
				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 
					ec_EIDENTETE AS code_original,
					ec_ENOBON AS numero,
					date(MAX(ec_EDATECOMMANDE)) AS date_commande,
					date(MAX(CASE WHEN lf_EETAT <> 0 THEN ef_EDATEFACTURATION ELSE ec_EDATECOMMANDE END)) AS date_engagement,
					substr(MAX(ec_ENOBON) || ' ' || base.cti_group_concat(DISTINCT el_ENOBON) || ' ' || base.cti_group_concat(DISTINCT ef_ENOBON),1,255) AS objet,
					''::text AS reference,
					'20991231'::date AS date_livraison_prevue,
					CASE 
						WHEN MIN(ll_EETAT) > 0 THEN 'T'
						WHEN MAX(ll_EETAT) = 0 THEN 'N'
						ELSE 'P' END AS etat_reception,
					MAX(el_EDATELIVRAISON) AS date_reception,
					CASE 
						WHEN MIN(lc_BSOLDE) > 0 AND MIN(lf_EETAT) <> 0 THEN 'T'
						WHEN MAX(lc_BSOLDE) = 0 THEN 'N'
						WHEN MAX(lf_EETAT) = 0 THEN 'N'
						ELSE 'P' END AS etat_liquidation,
					MAX(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,
					MAX(COALESCE(t_lieux_livraison.oid, 0)) AS lieu_livraison_id,
					0::bigint AS unite_fonctionnelle_id,
					SUM(lc_ETOTALHT) AS montant_commande_ht,
					0::numeric AS montant_commande_remise,
					SUM(lc_ETOTALTVA) AS montant_commande_tva,
					0::numeric AS montant_commande_port,
					SUM(lc_ETOTALTTC) AS montant_commande_total,
					SUM(lf_ETOTALHT) AS montant_liquidation_ht,
					0::numeric AS montant_liquidation_remise,
					SUM(lf_ETOTALTVA) AS montant_liquidation_tva,
					0::numeric AS montant_liquidation_port,
					SUM(lc_ETOTALTTC) AS montant_liquidation_total
				FROM w_LIGNE
					LEFT JOIN eco.t_fournisseurs ON t_fournisseurs.code_original = ec_EIDLABORATOIRE
					LEFT JOIN eco.t_lieux t_lieux_livraison ON ec_EIDETABLISSEMENT = t_lieux_livraison.code_original

				GROUP BY 1,2
				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');


				-- Validation lignes de commandes
				
				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,
					taux_tva)
				SELECT
					lc_EIDLIGNE AS code_original,
					MAX(p_commandes.oid) AS commande_id,
					MAX(w_LIGNE_NUMERO.ligne_commande) AS ligne_commande,
					substr(MAX(ec_ENOBON) || ' ' || base.cti_group_concat(el_ENOBON) || ' ' || base.cti_group_concat(ef_ENOBON),1,255) AS texte,
					MAX(COALESCE(t_articles.oid, 0)) AS article_id,
					MAX(COALESCE(t_articles.unite_stockage_id,0)) AS unite_approvisionnement_id,
					MAX(COALESCE(t_compte.oid, t_articles.compte_id, 0)) AS compte_id,
					MAX(COALESCE(t_ucd.oid, t_articles.ucd_id, 0)) AS ucd_id,
					MAX(COALESCE(t_lpp.oid, t_articles.lpp_id, 0)) AS lpp_id,
					
					MAX(lc_EQTE) AS quantite_approvisionnement,
					MAX(lc_EPXUNITHT) AS prix_unitaire_approvisionnement,
					0::numeric AS multiplicateur_stockage,
					0::numeric AS quantite_stockage,
					0::numeric AS prix_unitaire_stockage,

					MAX(lc_ETOTALHT) AS montant_commande_ht,
					0::numeric AS montant_commande_remise,
					MAX(lc_ETOTALTVA) AS montant_commande_tva,
					0::numeric AS montant_commande_port,
					MAX(lc_ETOTALTTC) AS montant_commande_ttc,

					SUM(ll_EQTE) AS quantite_livraison_approvisionnement,
					SUM(ll_EQTE) AS quantite_livraison_stockage,
					SUM(ll_ETOTALHT) AS montant_livraison_ht,
					SUM(ll_ETOTALTTC) AS montant_livraison_ttc,
					MIN(CASE WHEN ll_EETAT = 0 THEN 'N' ELSE 'T' END) AS etat_livraison,

					SUM(lf_ETOTALHT) AS montant_liquidation_ht,
					0::numeric AS montant_liquidation_remise,
					SUM(lf_ETOTALTVA) AS montant_liquidation_tva,
					0::numeric AS montant_liquidation_port,
					SUM(lf_ETOTALTTC) AS montant_liquidation_ttc,
					MIN(CASE WHEN lf_EETAT = 0 THEN 'N' ELSE 'T' END) AS etat_liquidation,
					MAX(ETAUXTVA_calc) AS taux_tva
				FROM w_LIGNE
					JOIN eco.p_commandes ON ec_EIDENTETE = p_commandes.code_original
					JOIN w_LIGNE_NUMERO ON w_LIGNE.lc_EIDLIGNE = w_LIGNE_NUMERO.EIDLIGNE
					LEFT JOIN eco.t_articles ON w_LIGNE.lc_EIDARTICLE = t_articles.code_original AND t_articles.oid <> 0
					LEFT JOIN prod_winpharm.GE_COMPTE ON w_LIGNE.lc_EIDCOMPTE = GE_COMPTE.EIDCOMPTE
					LEFT JOIN eco.t_compte ON GE_COMPTE.SNUMCOMPTE = t_compte.code_original AND t_compte.oid <> 0
					LEFT JOIN base.t_ucd ON 'x' = t_ucd.code AND t_ucd.oid <> 0
					LEFT JOIN base.t_lpp ON 'x' = t_lpp.code AND t_lpp.oid <> 0
				GROUP BY 1
					;
					

				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="Stocks">
			<sqlcmd><![CDATA[
			
				UPDATE prod_winpharm.GE_MOUVEMENT SET
					EQTEMOUVEMENT = 0 - EQTEMOUVEMENT,
					EMONTANTTTC = 0 - EMONTANTTTC,
					EMONTANTHT = 0 - EMONTANTHT,
					ENOBON = 'MODIFCTI'
				WHERE ETYPEMOUVEMENT = 3 AND
					ENOUVEAUSTOCK - EANCIENSTOCK <> EQTEMOUVEMENT AND
					EANCIENSTOCK - ENOUVEAUSTOCK = EQTEMOUVEMENT;

				DROP TABLE IF EXISTS w_STOCK;
				CREATE TEMP TABLE w_STOCK AS
				SELECT 
					EIDARTICLE,
					EIDETABLISSEMENT,
					date_part('year',EDATEMOUVEMENT) * 12 + date_part('month',EDATEMOUVEMENT) AS mois_sequence,
					MIN(date(date_trunc('month',EDATEMOUVEMENT))) AS date_debut,
					date(MAX(date(date_trunc('month',EDATEMOUVEMENT))) + interval '1 month' - interval '1 day') AS date_fin,
					(MIN(ARRAY[EIDMOUVEMENT,EANCIENSTOCK]))[2] AS stock_quantite_debut,
					0::numeric AS stock_valeur_debut_ht,
					0::numeric AS stock_valeur_debut_ttc,
					SUM(CASE WHEN ETYPEMOUVEMENT IN (1,3,5,11,12,14) THEN EQTEMOUVEMENT ELSE 0 END) AS entree_quantite,
					SUM(CASE WHEN ETYPEMOUVEMENT IN (1,3,5,11,12,14) THEN EMONTANTHT ELSE 0 END) AS entree_montant_ht,
					SUM(CASE WHEN ETYPEMOUVEMENT IN (1,3,5,11,12,14) THEN EMONTANTTTC ELSE 0 END) AS entree_montant_ttc,
					SUM(CASE WHEN ETYPEMOUVEMENT IN (1,3,5,11,12,14) AND EQTEMOUVEMENT > 0 THEN EQTEMOUVEMENT ELSE 0 END) AS entree_quantite_pump,
					SUM(CASE WHEN ETYPEMOUVEMENT IN (1,3,5,11,12,14) AND EQTEMOUVEMENT > 0 THEN EMONTANTHT ELSE 0 END) AS entree_montant_pump_ht,
					SUM(CASE WHEN ETYPEMOUVEMENT IN (1,3,5,11,12,14) AND EQTEMOUVEMENT > 0 THEN EMONTANTTTC ELSE 0 END) AS entree_montant_pump_ttc,
					SUM(CASE WHEN ETYPEMOUVEMENT IN (2,4) THEN EQTEMOUVEMENT ELSE 0 END) AS sortie_quantite,
					SUM(CASE WHEN ETYPEMOUVEMENT IN (2,4) THEN EMONTANTHT ELSE 0 END) AS sortie_montant_ht,
					SUM(CASE WHEN ETYPEMOUVEMENT IN (2,4) THEN EMONTANTTTC ELSE 0 END) AS sortie_montant_ttc,
					0::numeric AS sortie_quantite_valorise,
					0::numeric AS sortie_montant_valorise,
					0::numeric AS sortie_quantite_non_valorise,
					0::numeric AS sortie_montant_non_valorise,
					0::numeric AS stock_quantite_fin,
					0::numeric AS stock_valeur_fin_ht,
					0::numeric AS stock_valeur_fin_ttc,
					0::numeric AS pump_ht,
					0::numeric AS pump_ttc,
					'1'::text AS ok_stock_debut,
					'0'::text AS ok_stock_fin
				FROM prod_winpharm.GE_MOUVEMENT
				JOIN w_ETABLISSEMENT ON EIDETABLISSEMENT = EIDETABLISSEMENT_ok
				WHERE EIDARTICLE <> 0 AND
					ETYPEMOUVEMENT NOT IN (6) AND
					date(EDATEMOUVEMENT) <= now() AND
					(
						EANCIENSTOCK <> 0 OR
						ENOUVEAUSTOCK <> 0 OR
						EQTEMOUVEMENT <> 0
					)
				GROUP BY 1,2,3
				;

				UPDATE w_STOCK SET
					stock_quantite_fin = stock_quantite_debut + entree_quantite - sortie_quantite,
					ok_stock_fin = '1'
				;



				CREATE INDEX w_STOCK_i1 ON w_STOCK USING btree (EIDARTICLE);	


				DROP TABLE IF EXISTS w_STOCK_CURRENT;
				CREATE TEMP TABLE w_STOCK_CURRENT AS
				SELECT 
					MAX(mois_sequence) AS CURRENT_mois_sequence,
					MAX(date_debut) AS CURRENT_date_debut,
					MAX(date_fin) AS CURRENT_date_fin
				FROM w_STOCK;

				DROP TABLE IF EXISTS w_calendrier_mois;
				CREATE TEMP TABLE w_calendrier_mois AS
				SELECT 
					date_part('year',date_debut) * 12 + date_part('month',date_debut) AS mois_sequence,
					date_debut,
					date_fin
				FROM w_STOCK_CURRENT,
					base.p_calendrier_mois
				WHERE date_debut BETWEEN '[ENV_ECO_ANNEEDEBUT]0101' AND CURRENT_date_fin;

				CREATE INDEX w_calendrier_mois_i1 ON w_calendrier_mois USING btree (mois_sequence);


				DROP TABLE IF EXISTS w_STOCK_ARTICLE;
				CREATE TEMP TABLE w_STOCK_ARTICLE AS
				SELECT 
					EIDARTICLE,
					EIDETABLISSEMENT,
					MIN(mois_sequence) AS mois_sequence_debut,
					MAX(mois_sequence) AS mois_sequence_fin,
					(MAX(Array[mois_sequence,(stock_quantite_fin)]))[2] AS stock_quantite_fin
				FROM w_STOCK
				GROUP BY 1,2
				ORDER BY 1
				;

				UPDATE w_STOCK_ARTICLE
				SET mois_sequence_fin = CURRENT_mois_sequence
				FROM w_STOCK_CURRENT
				WHERE stock_quantite_fin <> 0 AND
					mois_sequence_fin <> CURRENT_mois_sequence ;

				CREATE INDEX w_STOCK_ARTICLE_i1 ON w_STOCK_ARTICLE USING btree (EIDARTICLE);

				INSERT INTO w_STOCK
					(
					EIDARTICLE,
					EIDETABLISSEMENT,
					mois_sequence,
					date_debut,
					date_fin,
					stock_quantite_debut,
					stock_valeur_debut_ht,
					stock_valeur_debut_ttc,
					entree_quantite,
					entree_montant_ht,
					entree_montant_ttc,
					entree_quantite_pump,
					entree_montant_pump_ht,
					entree_montant_pump_ttc,
					sortie_quantite,
					sortie_montant_ht,
					sortie_montant_ttc,
					sortie_quantite_valorise,
					sortie_montant_valorise,
					sortie_quantite_non_valorise,
					sortie_montant_non_valorise,
					stock_quantite_fin,
					stock_valeur_fin_ht,
					stock_valeur_fin_ttc,
					pump_ht,
					pump_ttc,
					ok_stock_debut,
					ok_stock_fin
					)
				SELECT
					w_STOCK_ARTICLE.EIDARTICLE,
					w_STOCK_ARTICLE.EIDETABLISSEMENT,
					w_calendrier_mois.mois_sequence,
					w_calendrier_mois.date_debut,
					w_calendrier_mois.date_fin,
					0::numeric AS stock_quantite_debut,
					0::numeric AS stock_valeur_debut_ht,
					0::numeric AS stock_valeur_debut_ttc,
					0::numeric AS entree_quantite,
					0::numeric AS entree_montant_ht,
					0::numeric AS entree_montant_ttc,
					0::numeric AS entree_quantite_pump,
					0::numeric AS entree_montant_pump_ht,
					0::numeric AS entree_montant_pump_ttc,
					0::numeric AS sortie_quantite,
					0::numeric AS sortie_montant_ht,
					0::numeric AS sortie_montant_ttc,
					0::numeric AS sortie_quantite_valorise,
					0::numeric AS sortie_montant_valorise,
					0::numeric AS sortie_quantite_non_valorise,
					0::numeric AS sortie_montant_non_valorise,
					0::numeric AS stock_quantite_fin,
					0::numeric AS stock_valeur_fin_ht,
					0::numeric AS stock_valeur_fin_ttc,
					0::numeric AS pump_ht,
					0::numeric AS pump_ttc,
					'0'::text AS ok_stock_debut,
					'0'::text AS ok_stock_fin
				FROM w_STOCK_ARTICLE
					JOIN w_calendrier_mois ON w_calendrier_mois.mois_sequence BETWEEN mois_sequence_debut AND mois_sequence_fin
					LEFT JOIN w_STOCK ON 
						w_STOCK_ARTICLE.EIDARTICLE = w_STOCK.EIDARTICLE AND
						w_STOCK.mois_sequence = w_calendrier_mois.mois_sequence
				WHERE w_STOCK.EIDARTICLE IS NULL;

				--VACUUM ANALYSE w_STOCK;


				SELECT base.cti_execute(
				'UPDATE w_STOCK SET
					stock_quantite_debut = 
						w_STOCK_BEFORE.stock_quantite_fin,
					stock_quantite_fin = 
						w_STOCK_BEFORE.stock_quantite_fin - 
						w_STOCK.entree_quantite + 
						w_STOCK.sortie_quantite,
					ok_stock_fin = ''1'',
					ok_stock_debut = ''1''
				FROM w_STOCK w_STOCK_BEFORE
				WHERE w_STOCK_BEFORE.EIDARTICLE = w_STOCK.EIDARTICLE AND
					w_STOCK_BEFORE.mois_sequence = w_STOCK.mois_sequence-1 AND
					w_STOCK_BEFORE.ok_stock_fin = ''1'' AND
					w_STOCK.ok_stock_fin = ''0''',100);


				DROP TABLE IF EXISTS w_STOCK_PUMP;
				CREATE TEMP TABLE w_STOCK_PUMP AS
				SELECT w_STOCK.EIDARTICLE,
					w_STOCK.mois_sequence,
					w_STOCK.date_fin, 
					base.cti_division(
						SUM(CASE WHEN w_STOCK.mois_sequence -  w_STOCK_before.mois_sequence < 6 THEN w_STOCK_before.entree_montant_pump_ht ELSE 0 END)::numeric
						,
						SUM(CASE WHEN w_STOCK.mois_sequence -  w_STOCK_before.mois_sequence < 6 THEN w_STOCK_before.entree_quantite_pump ELSE 0 END)::numeric
						) AS pump_ht,
					base.cti_division(
						SUM(CASE WHEN w_STOCK.mois_sequence -  w_STOCK_before.mois_sequence < 6 THEN w_STOCK_before.entree_montant_pump_ttc ELSE 0 END)::numeric
						,
						SUM(CASE WHEN w_STOCK.mois_sequence -  w_STOCK_before.mois_sequence < 6 THEN w_STOCK_before.entree_quantite_pump ELSE 0 END)::numeric
						) AS pump_ttc
				FROM w_STOCK
				JOIN w_STOCK w_STOCK_before ON 
					w_STOCK.EIDARTICLE = w_STOCK_before.EIDARTICLE AND
					w_STOCK.date_fin >= w_STOCK_before.date_fin
				GROUP BY 1,2,3;

					

				CREATE INDEX w_STOCK_PUMP_i1 ON w_STOCK_PUMP USING btree (EIDARTICLE);	


				SELECT base.cti_execute(
				'UPDATE w_STOCK_pump SET
					pump_ht = 
						w_STOCK_pump_BEFORE.pump_ht,
					pump_ttc = 
						w_STOCK_pump_BEFORE.pump_ttc
				FROM w_STOCK_pump w_STOCK_pump_BEFORE
				WHERE w_STOCK_pump_BEFORE.EIDARTICLE = w_STOCK_pump.EIDARTICLE AND
					w_STOCK_pump_BEFORE.mois_sequence = w_STOCK_pump.mois_sequence-1 AND
					w_STOCK_pump_BEFORE.pump_ht <> 0 AND
					w_STOCK_pump_BEFORE.pump_ttc <> 0 AND
					w_STOCK_pump.pump_ht = 0 AND
					w_STOCK_pump.pump_ttc = 0
					',100);

				SELECT base.cti_execute(
				'UPDATE w_STOCK_pump SET
					pump_ht = 
						w_STOCK_pump_AFTER.pump_ht,
					pump_ttc = 
						w_STOCK_pump_AFTER.pump_ttc
				FROM w_STOCK_pump w_STOCK_pump_AFTER
				WHERE w_STOCK_pump_AFTER.EIDARTICLE = w_STOCK_pump.EIDARTICLE AND
					w_STOCK_pump_AFTER.mois_sequence = w_STOCK_pump.mois_sequence+1 AND
					w_STOCK_pump_AFTER.pump_ht <> 0 AND
					w_STOCK_pump_AFTER.pump_ttc <> 0 AND
					w_STOCK_pump.pump_ht = 0 AND
					w_STOCK_pump.pump_ttc = 0
					',100);
						
				-- utilise le montant_sortie si cela ne suffit pas (pas de commande sur la période remontée)
				UPDATE w_STOCK_PUMP set
				pump_ht = base.cti_division(sortie_montant_ht,sortie_quantite),
				pump_ttc = base.cti_division(sortie_montant_ttc,sortie_quantite)
				FROM 
					w_stock
				WHERE 
					w_STOCK_PUMP.pump_ht = 0 AND w_STOCK_PUMP.pump_ttc = 0 AND
					w_STOCK.EIDARTICLE = w_STOCK_PUMP.EIDARTICLE AND
					w_STOCK.date_fin = w_STOCK_PUMP.date_fin;					

				UPDATE w_STOCK SET
					pump_ht = w_STOCK_PUMP.pump_ht,
					pump_ttc = w_STOCK_PUMP.pump_ttc,
					stock_valeur_fin_ht = stock_quantite_fin * w_STOCK_PUMP.pump_ht,
					stock_valeur_fin_ttc = stock_quantite_fin * w_STOCK_PUMP.pump_ttc
				FROM w_STOCK_PUMP
				WHERE w_STOCK_PUMP.EIDARTICLE = w_STOCK.EIDARTICLE AND
					w_STOCK_PUMP.mois_sequence = w_STOCK.mois_sequence;

				UPDATE w_STOCK SET
					stock_valeur_debut_ht = stock_quantite_debut * w_STOCK_PUMP.pump_ht,
					stock_valeur_debut_ttc = stock_quantite_debut * w_STOCK_PUMP.pump_ttc
				FROM w_STOCK_PUMP
				WHERE w_STOCK_PUMP.EIDARTICLE = w_STOCK.EIDARTICLE AND
					w_STOCK_PUMP.mois_sequence = w_STOCK.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 
					t_articles.oid AS article_id,
					date_fin,
					date_debut,
					t_articles.ucd_id AS ucd_id,
					t_articles.lpp_id AS lpp_id,
					t_articles.compte_id,
					COALESCE(t_lieux.oid,0) AS lieu_id,
					0::bigint AS provider_id,
					t_articles.fournisseur_principal_id AS fournisseur_id,
					stock_quantite_debut,
					stock_valeur_debut_ttc,
					entree_quantite,
					entree_montant_ttc,
					pump_ttc,
					sortie_quantite,
					sortie_montant_ttc,
					stock_quantite_fin,
					stock_valeur_fin_ttc
				FROM w_STOCK
				JOIN eco.t_articles ON EIDARTICLE = t_articles.code_original
				LEFT JOIN eco.t_lieux ON EIDETABLISSEMENT = t_lieux.code_original
				WHERE date_debut >= '[ENV_ECO_ANNEEDEBUT]0101' AND
					(
						stock_quantite_debut <> 0 OR
						entree_quantite <> 0 OR
						sortie_quantite <> 0 OR
						stock_quantite_fin <> 0
					)
				;
				
				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[
			
				-- Commande associée au mouvement
				DROP TABLE IF EXISTS w_MOUVEMENT_commande;
				CREATE TEMP TABLE w_MOUVEMENT_commande AS
				SELECT GE_MOUVEMENT.EIDMOUVEMENT,
					MAX(p_commandes.oid) AS commande_id, 
					MAX(ligne_commande) AS ligne_commande
					FROM prod_winpharm.GE_MOUVEMENT
					JOIN prod_winpharm.GE_LIGNE GE_LIGNE_RECEPTION ON GE_MOUVEMENT.EIDMOUVEMENT = GE_LIGNE_RECEPTION.EIDMOUVEMENT
					JOIN prod_winpharm.GE_LIGNE ON GE_LIGNE_RECEPTION.EIDLIGNECOMMANDE = GE_LIGNE.EIDLIGNE
					 
					JOIN prod_winpharm.GE_ENTETE ON GE_ENTETE.EIDENTETE = GE_LIGNE.EIDENTETE
					
					LEFT JOIN eco.p_commandes ON GE_ENTETE.EIDENTETE = p_commandes.code_original
					LEFT JOIN w_LIGNE_NUMERO ON GE_LIGNE.EIDLIGNE = w_LIGNE_NUMERO.EIDLIGNE
				GROUP BY 1
				;

				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(
					code_original,
					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 
					GE_MOUVEMENT.EIDMOUVEMENT::text AS code_original,
					EDATEMOUVEMENT AS date,
					CASE WHEN ETYPEMOUVEMENT IN (1,3,5,11,12,14) 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,0) AS fournisseur_id,
					COALESCE(w_MOUVEMENT_commande.commande_id,0) AS commande_id,
					COALESCE(w_MOUVEMENT_commande.ligne_commande,0) AS ligne_commande,
					COALESCE(t_ucd.oid, t_articles.ucd_id, 0) AS ucd_id,
					COALESCE(t_lpp.oid, t_articles.lpp_id, 0) AS lpp_id,
					base.cti_division(EMONTANTTTC,EQTEMOUVEMENT) AS prix_unitaire,
					CASE WHEN ETYPEMOUVEMENT IN (1,3,5,11,12,14) THEN EQTEMOUVEMENT ELSE 0 END AS entree_quantite,
					CASE WHEN ETYPEMOUVEMENT IN (2,4) THEN EQTEMOUVEMENT ELSE 0 END AS sortie_quantite,
					CASE WHEN ETYPEMOUVEMENT IN (1,3,5,11,12,14) THEN EMONTANTTTC ELSE 0 END AS entree_montant,
					CASE WHEN ETYPEMOUVEMENT IN (1,3,5,11,12,14) THEN EMONTANTHT ELSE 0 END AS entree_montant_ht,
					CASE WHEN ETYPEMOUVEMENT IN (2,4) THEN EMONTANTTTC ELSE 0 END AS sortie_montant,
					CASE WHEN ETYPEMOUVEMENT IN (2,4) THEN EMONTANTHT ELSE 0 END AS sortie_montant_ht,
					CASE WHEN ETYPEMOUVEMENT IN (2,4) THEN EMONTANTTTC ELSE 0 END  AS sortie_montant_original_ttc,
					CASE WHEN ETYPEMOUVEMENT IN (2,4) THEN EMONTANTHT ELSE 0 END AS sortie_montant_original_ht
				FROM prod_winpharm.GE_MOUVEMENT
					JOIN w_ETABLISSEMENT ON GE_MOUVEMENT.EIDETABLISSEMENT = EIDETABLISSEMENT_ok
					LEFT JOIN eco.t_types_mouvements ON ETYPEMOUVEMENT = t_types_mouvements.oid
					LEFT JOIN eco.t_unites_fonctionnelles ON EIDUNITEMEDICALE = t_unites_fonctionnelles.code_original
					LEFT JOIN eco.t_articles ON GE_MOUVEMENT.EIDARTICLE = t_articles.code_original AND t_articles.oid <> 0
					LEFT JOIN eco.t_lieux ON GE_MOUVEMENT.EIDETABLISSEMENT = t_lieux.code_original
					LEFT JOIN eco.t_fournisseurs ON EIDLABORATOIRE = t_fournisseurs.code_original
					LEFT JOIN eco.t_compte ON 'x' = t_compte.code_original AND t_compte.oid <> 0
					LEFT JOIN base.t_ucd ON 'x' = t_ucd.code AND t_ucd.oid <> 0
					LEFT JOIN base.t_lpp ON 'x' = t_lpp.code AND t_lpp.oid <> 0
					LEFT JOIN w_STOCK ON 
						GE_MOUVEMENT.EIDARTICLE = w_STOCK.EIDARTICLE AND 
						date(EDATEMOUVEMENT) BETWEEN w_STOCK.date_debut AND w_STOCK.date_fin 
					LEFT JOIN w_MOUVEMENT_commande ON w_MOUVEMENT_commande.EIDMOUVEMENT = GE_MOUVEMENT.EIDMOUVEMENT
				WHERE EDATEMOUVEMENT >= '[ENV_ECO_ANNEEDEBUT]0101' AND
					date(EDATEMOUVEMENT) <= now() AND
					ETYPEMOUVEMENT NOT IN (6) AND
					EQTEMOUVEMENT <> 0
				;

				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">
		
		
	</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>
	<NODE name="VACUUM" label="REORGANISATION BASE DE DONNEES">
		<NODE label="Réorganisation tables">
			<sqlcmd><![CDATA[
			
					
				
			]]></sqlcmd>
		</NODE>
	</NODE>
	<NODE name="RAZ" label="RAZ BASE">
		<NODE label="Nettoyage des tables">
			<sqlcmd><![CDATA[

				
										
			]]></sqlcmd>
		</NODE>
	</NODE>
	<NODE name="RAZ_ALL" label="RAZ ALL">
		<NODE label="Nettoyage COMPLET des tables">
			<sqlcmd><![CDATA[

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