<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	<NODE name="INIT" label="INITIALISATIONS">

		<NODE label="Tables Articles">

			<sqlcmd><![CDATA[
			
			
				-- UCD
				
				-- LPP
					
				-- Catégories
				INSERT INTO eco.t_divers(code, valeur, texte, description)
				SELECT 'W1_CAT_SRC','0','Source catégorie/sous catégorie.','0=DEF_TYPE_ARTICLE 1=DEF_CLASSIF '
				WHERE 'W1_CAT_SRC' NOT IN (SELECT code FROM eco.t_divers)
				;

				DROP TABLE IF EXISTS w_DAT_ARTICLE;
				CREATE TEMP TABLE w_DAT_ARTICLE AS
				SELECT
					ART_OID,
					MAX(ART_CLASSE) AS ART_CLASSE,
					MAX(ART_CODE) AS ART_CODE,
					MAX(ART_LIB) AS ART_LIB,
					MAX(ART_LIBELLE_COURT) AS ART_LIBELLE_COURT,
					MAX(ART_TYPE_ARTICLE) AS ART_TYPE_ARTICLE,
					MAX(ART_UNIT_DISTRIB) AS ART_UNIT_DISTRIB,
					MAX(ART_COMPTE) AS ART_COMPTE,

					COALESCE(MAX(CASE
					WHEN t_divers.valeur = '1' THEN def_classif_parent2.cls_oid::text
					ELSE ART_CLASSE::text END),'') AS categorie_code_original,
					COALESCE(MAX(CASE
					WHEN t_divers.valeur = '1' THEN def_classif_parent2.CLS_CODE
					ELSE DEF_TYPE_ARTICLE_cat.TA_IDENT::text END),'') AS categorie_code,
					COALESCE(MAX(CASE
					WHEN t_divers.valeur = '1' THEN def_classif_parent2.CLS_LIB
					ELSE DEF_TYPE_ARTICLE_cat.TA_LIB::text END),'') AS categorie_texte,
					COALESCE(MAX(CASE
					WHEN t_divers.valeur = '1' THEN def_classif_parent.cls_oid::text
					ELSE ART_TYPE_ARTICLE::text END),'') AS sous_categorie_code_original,
					COALESCE(MAX(CASE
					WHEN t_divers.valeur = '1' THEN def_classif_parent.CLS_CODE
					ELSE DEF_TYPE_ARTICLE_ssc.TA_IDENT::text END),'') AS sous_categorie_code,
					COALESCE(MAX(CASE
					WHEN t_divers.valeur = '1' THEN def_classif_parent.CLS_LIB
					ELSE DEF_TYPE_ARTICLE_ssc.TA_LIB::text END),'') AS sous_categorie_texte
				FROM prod_web100t.DAT_ARTICLE
				JOIN eco.t_divers ON t_divers.code = 'W1_CAT_SRC'
				LEFT JOIN prod_web100t.DEF_TYPE_ARTICLE DEF_TYPE_ARTICLE_cat ON ART_CLASSE = DEF_TYPE_ARTICLE_cat.TA_OID
				LEFT JOIN prod_web100t.DEF_TYPE_ARTICLE DEF_TYPE_ARTICLE_ssc ON ART_TYPE_ARTICLE = DEF_TYPE_ARTICLE_ssc.TA_OID
				LEFT JOIN prod_web100t.def_classif ON def_classif.CLS_ARTICLE = ART_OID
				LEFT join prod_web100t.def_classif def_classif_parent ON  def_classif.cls_parent = def_classif_parent.cls_oid
				LEFT join prod_web100t.def_classif def_classif_parent2 ON  def_classif_parent.cls_parent = def_classif_parent2.cls_oid
				LEFT join prod_web100t.def_classif def_classif_parent3 ON  def_classif_parent2.cls_parent = def_classif_parent3.cls_oid
				WHERE (
					def_classif.CLS_ARTICLE IS NULL OR
					def_classif_parent3.CLS_ORDER = 0
					)
				GROUP BY 1
				;


				ANALYSE w_DAT_ARTICLE
				;

				DROP TABLE IF EXISTS w_DAT_ARTICLE_cat;
				CREATE TEMP TABLE w_DAT_ARTICLE_cat AS
				SELECT categorie_code_original,
					MAX(categorie_code) AS categorie_code,
					MAX(categorie_texte) AS categorie_texte
				FROM w_DAT_ARTICLE
				WHERE categorie_code_original <> ''
				GROUP BY 1
				;

				INSERT INTO eco.t_categories_articles(code, texte, texte_court, code_original)
				SELECT categorie_code, categorie_texte, LEFT(categorie_texte,50), categorie_code_original
				FROM w_DAT_ARTICLE_cat
					LEFT JOIN eco.t_categories_articles ON code_original = categorie_code_original
				WHERE code_original IS NULL
				;

				UPDATE eco.t_categories_articles
				SET code = categorie_code,
					texte = categorie_texte,
					texte_court = LEFT(categorie_texte,50)
				FROM w_DAT_ARTICLE_cat
				WHERE
					code_original = categorie_code_original AND
					(
					code IS DISTINCT FROM categorie_code OR
					texte IS DISTINCT FROM categorie_texte OR
					texte_court IS DISTINCT FROM LEFT(categorie_texte,50)
					)
				;

				-- Sous catégories

				DROP TABLE IF EXISTS w_DAT_ARTICLE_ssc;
				CREATE TEMP TABLE w_DAT_ARTICLE_ssc AS
				SELECT sous_categorie_code_original,
					MAX(sous_categorie_code) AS sous_categorie_code,
					MAX(sous_categorie_texte) AS sous_categorie_texte
				FROM w_DAT_ARTICLE
				WHERE sous_categorie_code_original <> ''
				GROUP BY 1
				;

				INSERT INTO eco.t_sous_categories_articles(code, texte, texte_court, code_original)
				SELECT sous_categorie_code, sous_categorie_texte, LEFT(sous_categorie_texte,50), sous_categorie_code_original
				FROM w_DAT_ARTICLE_ssc
					LEFT JOIN eco.t_sous_categories_articles ON code_original = sous_categorie_code_original
				WHERE code_original IS NULL
				;

				UPDATE eco.t_sous_categories_articles
				SET code = sous_categorie_code,
					texte = sous_categorie_texte,
					texte_court = LEFT(sous_categorie_texte,50)
				FROM w_DAT_ARTICLE_ssc
				WHERE
					code_original = sous_categorie_code_original AND
					(
					code IS DISTINCT FROM sous_categorie_code OR
					texte IS DISTINCT FROM sous_categorie_texte OR
					texte_court IS DISTINCT FROM LEFT(sous_categorie_texte,50)
					)
				;


				-- Lieux

				INSERT INTO eco.t_lieux(code, texte, texte_court, code_original)
				SELECT LIEU_CODE, LIEU_LIB, LIEU_LIB, LIEU_OID
				FROM prod_web100t.DEF_LIEU_STOCKAGE LEFT JOIN eco.t_lieux ON (code_original = LIEU_OID)
						WHERE code_original IS NULL;

				UPDATE eco.t_lieux
				SET texte = LIEU_LIB,
					texte_court = LIEU_LIB
				FROM prod_web100t.DEF_LIEU_STOCKAGE
				WHERE code_original = LIEU_OID
				AND (texte IS DISTINCT FROM LIEU_LIB OR
					texte_court IS DISTINCT FROM LIEU_LIB);

				-- Comptes

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

				INSERT INTO eco.t_compte(code_original, code, texte, texte_court)
				SELECT CPT_CODE, CPT_CODE, MAX(CPT_LIB), MAX(CPT_LIB)
				FROM PROD_WEB100T.CPT_COMPTES
				WHERE CPT_CODE <> '' AND
					(CPT_OID IN (SELECT DISTINCT ART_COMPTE FROM prod_web100t.DAT_ARTICLE) OR
					CPT_OID IN (SELECT DISTINCT SE_COMPTE FROM prod_web100t.STK_ETAT))
					AND
					CPT_CODE NOT IN (SELECT code FROM eco.t_compte)
				GROUP BY 1
				ORDER BY 1;

				UPDATE eco.t_compte
				SET texte = CPT_LIB, texte_court = CPT_LIB
				FROM PROD_WEB100T.CPT_COMPTES
				WHERE t_compte.code = CPT_CODE AND
						(
						texte IS DISTINCT FROM CPT_LIB OR
						texte_court IS DISTINCT FROM CPT_LIB
						);


				-- Familles d'articles

				-- Sous familles

				-- ATC / Cladimed

				INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court, est_cladimed )
				SELECT CLS_OID, CLS_CODE, CLS_LIB, CLS_LIB, cladimed
				FROM prod_web100t.DEF_CLASSIF
					JOIN (
						SELECT
							CLS_OID AS ROOT_OID,
							CASE WHEN CLS_CODE = 'CLADIMED' THEN 1 ELSE 0 END AS cladimed
						FROM prod_web100t.DEF_CLASSIF
						WHERE CLS_LIB IN ('ATC', 'CLADIMED')
						) AS sub ON CLS_ROOT = ROOT_OID
				LEFT JOIN eco.t_classification_atc ON (code_original = CLS_OID)
				WHERE CLS_PARENT != 0 AND
					code_original IS NULL
				;

				UPDATE eco.t_classification_atc
				SET texte = CLS_LIB,
					texte_court = CLS_LIB,
					est_cladimed = cladimed
				FROM prod_web100t.DEF_CLASSIF
					JOIN (
						SELECT
							CLS_OID AS ROOT_OID,
							CASE WHEN CLS_CODE = 'CLADIMED' THEN 1 ELSE 0 END AS cladimed
						FROM prod_web100t.DEF_CLASSIF
						WHERE CLS_LIB IN ('ATC', 'CLADIMED')
						) AS sub ON CLS_ROOT = ROOT_OID
				WHERE
					code_original = CLS_OID AND
					(texte IS DISTINCT FROM CLS_LIB OR
					texte_court IS DISTINCT FROM CLS_LIB);


					-- Standard ATC
				SELECT eco.cti_reorganize_classification_atc();


				DROP TABLE IF EXISTS w_SGN_NMCL_DATA;
				CREATE TEMP TABLE W_SGN_NMCL_DATA AS
				SELECT
					NMCL_IDENT,
					ND_INVARIANT_OID,
					(MAX(Array[ND_VALIDE_DE::text,ND_CODE]))[2] AS ND_CODE,
					(MAX(Array[ND_VALIDE_DE::text,ND_LIB]))[2] AS ND_LIB
				FROM prod_web100t.SGN_NMCL_DATA
					JOIN prod_web100t.SGN_NMCLS ON ND_NMCL_OID = NMCL_OID
				WHERE NMCL_IDENT IN
					(
					'UNITES',
					'UCD',
					'LPP',
					'TYPEARTICLEGEF',
					'TAUXTVA'
					'LIEUSTOCKAGE',
					'LIEULIVRAISONFACTURATION',
					'ETABLISSEMENT',
					'ENTITEFACTURATION',
					'INTERVENANT',
					'UNITEFONCTIONNELLE')
				GROUP BY 1,2;


				-- Site
				INSERT INTO eco.t_sites (code, texte, texte_court, code_original)
				SELECT ND_CODE, ND_LIB, ND_LIB, ND_INVARIANT_OID
				FROM prod_web100t.DEF_UF_GESTION
					JOIN w_SGN_NMCL_DATA ON NMCL_IDENT = 'ENTITEFACTURATION' AND ND_INVARIANT_OID = UF_EF
										 AND (ND_INVARIANT_OID = ANY (string_to_array('[ETAB]',',')) OR '[ETAB]' = '')
					LEFT JOIN eco.t_sites ON (code_original = ND_INVARIANT_OID)
				WHERE code_original IS NULL
				GROUP BY 1,2,3,4;

				UPDATE eco.t_sites
				SET texte = ND_LIB,
					texte_court = ND_LIB
				FROM prod_web100t.DEF_UF_GESTION
					JOIN w_SGN_NMCL_DATA ON NMCL_IDENT = 'ENTITEFACTURATION' AND ND_INVARIANT_OID = UF_ETS
				WHERE code_original = ND_CODE
				AND (texte IS DISTINCT FROM ND_LIB OR
					texte_court IS DISTINCT FROM ND_LIB);


				-- unités

				INSERT INTO eco.t_unites(code, texte, texte_court, code_original)
				SELECT ND_CODE, ND_LIB, ND_LIB, ND_INVARIANT_OID
				FROM W_SGN_NMCL_DATA LEFT JOIN eco.t_unites ON (code = ND_CODE)
				WHERE NMCL_IDENT = 'UNITES' AND code IS NULL
				;

				UPDATE eco.t_unites
				SET texte = ND_LIB,
					texte_court = ND_LIB
				FROM W_SGN_NMCL_DATA
				WHERE
					NMCL_IDENT = 'UNITES' AND
					code = ND_CODE AND
					(texte IS DISTINCT FROM ND_LIB OR
					texte_court IS DISTINCT FROM ND_LIB);



				-- Types articles = Catégories articles
				INSERT INTO eco.t_types_articles(code, texte, texte_court, code_original)
				SELECT TA_IDENT, TA_LIB, LEFT(TA_LIB,50), TA_OID
				FROM prod_web100t.DEF_TYPE_ARTICLE
					LEFT JOIN eco.t_types_articles ON (code_original = TA_OID)
				WHERE code_original IS NULL AND TA_IDENT <> ''
				;

				UPDATE eco.t_types_articles
				SET texte = TA_LIB,
					texte_court = LEFT(TA_LIB,50)
				FROM prod_web100t.DEF_TYPE_ARTICLE
				WHERE
					TA_IDENT <> '' AND
					code_original = TA_OID AND
					(texte IS DISTINCT FROM TA_LIB OR
					texte_court IS DISTINCT FROM LEFT(TA_LIB,50))
				;



				-- Articles

				INSERT INTO eco.t_articles(code, texte, texte_court, code_original)
				SELECT DISTINCT ART_CODE, ART_LIB, ART_LIBELLE_COURT, ART_OID
				FROM prod_web100t.DAT_ARTICLE
				JOIN prod_web100t.DEF_TYPE_ARTICLE ON ta_oid = art_type_article
				JOIN eco.t_sites ON t_sites.code_original = ta_ef
				LEFT JOIN eco.t_articles ON (t_articles.code_original = ART_OID)
				WHERE t_articles.code_original IS NULL;

				UPDATE eco.t_articles
				SET
					code = ART_CODE,
					texte = ART_LIB,
					texte_court = ART_LIBELLE_COURT,
					gere_en_stock = CASE WHEN ART_TYPE_STOCK = '4' THEN '1' ELSE '0' END,
					stock_en_depot = CASE WHEN ART_TYPE_STOCK NOT IN ('3','4') THEN '1' ELSE '0' END
				FROM prod_web100t.DAT_ARTICLE
				WHERE code_original = ART_OID
					AND (
					code IS DISTINCT FROM ART_CODE OR
					texte IS DISTINCT FROM ART_LIB OR
					texte_court IS DISTINCT FROM ART_LIBELLE_COURT OR
					gere_en_stock IS DISTINCT FROM CASE WHEN ART_TYPE_STOCK = '4' THEN '1' ELSE '0' END OR
					stock_en_depot IS DISTINCT FROM CASE WHEN ART_TYPE_STOCK NOT IN ('3','4') THEN '1' ELSE '0' END
					)
				;

				UPDATE eco.t_articles SET
					type_id = COALESCE(t_types_articles.oid,0),
					famille_id = COALESCE(t_familles_articles.oid,0),
					sous_famille_id = COALESCE(t_sous_familles_articles.oid,0),
					categorie_id = COALESCE(t_categories_articles.oid,0),
					sous_categorie_id = COALESCE(t_sous_categories_articles.oid,0),
					unite_stockage_id = COALESCE(t_unites_stockage.oid,0),
					unite_distribution_id = COALESCE(t_unites_distribution.oid,0)
				FROM w_DAT_ARTICLE
					LEFT JOIN eco.t_types_articles ON ART_CLASSE = t_types_articles.code_original
					LEFT JOIN eco.t_familles_articles ON 0 = t_familles_articles.code_original
					LEFT JOIN eco.t_sous_familles_articles ON 0 = t_sous_familles_articles.code_original
					LEFT JOIN eco.t_categories_articles ON categorie_code_original = t_categories_articles.code_original
					LEFT JOIN eco.t_sous_categories_articles ON sous_categorie_code_original = t_sous_categories_articles.code_original
					LEFT JOIN eco.t_unites t_unites_stockage ON ART_UNIT_DISTRIB = t_unites_stockage.code_original
					LEFT JOIN eco.t_unites t_unites_distribution ON ART_UNIT_DISTRIB = t_unites_distribution.code_original
				WHERE
					t_articles.code_original = ART_OID AND
					(
					t_articles.type_id IS DISTINCT FROM COALESCE(t_types_articles.oid,0) OR
					t_articles.famille_id IS DISTINCT FROM COALESCE(t_familles_articles.oid,0) OR
					t_articles.sous_famille_id IS DISTINCT FROM COALESCE(t_sous_familles_articles.oid,0) OR
					t_articles.categorie_id IS DISTINCT FROM COALESCE(t_categories_articles.oid,0) OR
					t_articles.sous_categorie_id IS DISTINCT FROM COALESCE(t_sous_categories_articles.oid,0) OR
					t_articles.unite_stockage_id IS DISTINCT FROM COALESCE(t_unites_stockage.oid,0) OR
					t_articles.unite_distribution_id IS DISTINCT FROM COALESCE(t_unites_distribution.oid,0)
					)
				;



				--UPDATE eco.t_articles SET
				--	classification_atc_id = COALESCE(t_classification_atc.oid,0)
				--FROM prod_web100t.
				--	LEFT JOIN eco.t_classification_atc ON  = t_classification_atc.code
				--WHERE 
				--	t_articles.code_original = ARIUAR AND
				--	classification_atc_id IS DISTINCT FROM COALESCE(t_classification_atc.oid,0);

				
				UPDATE eco.t_articles SET classification_atc_id = 0 WHERE classification_atc_id IS NULL;
					
				UPDATE eco.t_articles SET
					compte_id = COALESCE(t_compte.oid,0)
				FROM prod_web100t.DAT_ARTICLE
					JOIN prod_web100t.CPT_COMPTES ON ART_COMPTE = CPT_OID
					LEFT JOIN eco.t_compte ON CPT_CODE = t_compte.code_original
				WHERE 
					t_articles.code_original = ART_OID AND
					compte_id IS DISTINCT FROM COALESCE(t_compte.oid,0);

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



				UPDATE eco.t_articles SET
					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
					);
					
				-- LPP
				INSERT INTO base.t_lpp (code, texte, texte_court)
				SELECT ND_CODE,ND_LIB, LEFT(ND_LIB,50)
				FROM w_SGN_NMCL_DATA
				WHERE ND_CODE NOT IN (SELECT code FROM base.t_lpp)
					AND ND_INVARIANT_OID IN (SELECT AL_ARTICLE FROM prod_web100t.DAT_ART_LPP)
				;
				
				UPDATE eco.t_articles 
				SET lpp_id = subview.lpp_id
				FROM
					(
						SELECT AL_ARTICLE,(MAX(Array[AL_DEBUT::text,t_lpp.oid::text]))[2]::bigint AS lpp_id
						FROM prod_web100t.DAT_ART_LPP
						JOIN w_SGN_NMCL_DATA ON NMCL_IDENT = 'LPP' AND ND_INVARIANT_OID = AL_LPP
						JOIN base.t_lpp ON t_lpp.code = substr(ND_CODE,1,7)
						WHERE AL_DELETED = 0
						GROUP BY 1
					) subview
				WHERE t_articles.code_original = AL_ARTICLE AND
					t_articles.lpp_id IS DISTINCT FROM subview.lpp_id
				;

				-- UCD			
				INSERT INTO base.t_ucd (code, texte, texte_court)
				SELECT CASE WHEN ND_CODE LIKE '34008%' AND LENGTH(ND_CODE) = 13 THEN SUBSTR(ND_CODE,6,7) ELSE ND_CODE END, MIN(ND_LIB), LEFT(MIN(ND_LIB),50)
				FROM w_SGN_NMCL_DATA
				WHERE 
					(LENGTH(ND_CODE) = 13 AND ND_CODE LIKE '34008%' OR LENGTH(ND_CODE) = 9)
					AND CASE WHEN LENGTH(ND_CODE) = 13 THEN SUBSTR(ND_CODE,6,7) ELSE ND_CODE END NOT IN (SELECT code FROM base.t_ucd)
					AND ND_INVARIANT_OID IN (SELECT ART_UCD FROM prod_web100t.DAT_ARTICLE)
				GROUP BY 1
				;
				
				UPDATE eco.t_articles SET
					ucd_id = t_ucd.oid,
					code_ucd13 = CASE WHEN ND_CODE LIKE '34008%' AND LENGTH(ND_CODE) = 13 THEN ND_CODE ELSE t_articles.code_ucd13 END
				FROM
					prod_web100t.DAT_ARTICLE
					JOIN w_SGN_NMCL_DATA ON NMCL_IDENT = 'UCD' AND ND_INVARIANT_OID = ART_UCD
					JOIN base.t_ucd ON t_ucd.code = CASE WHEN ND_CODE LIKE '34008%' AND LENGTH(ND_CODE) = 13 THEN SUBSTR(ND_CODE,6,7) ELSE ND_CODE END
				WHERE t_articles.code_original = ART_OID AND
					(
					t_articles.ucd_id IS DISTINCT FROM t_ucd.oid OR
					t_articles.code_ucd13 IS DISTINCT FROM CASE WHEN ND_CODE LIKE '34008%' AND LENGTH(ND_CODE) = 13 THEN ND_CODE ELSE t_articles.code_ucd13 END
					)
				;
					
				
				-- 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,
					CASE WHEN count(*) < 50 THEN
						base.cti_group_array3(COALESCE(AFLIFA,''))::text[]
						ELSE
						base.cti_group_array3(COALESCE(substr(AFLIFA,20),''))::text[]
						END AS ref_fournisseur_texte
				FROM eco.t_articles 
					LEFT JOIN (
						SELECT t_articles.oid AS article_id, 
							t_fournisseurs.oid AS fournisseur_id,
							AF_LIBELLE_COURT || CASE WHEN AF_CODE_REF <> '' THEN ' (' || AF_CODE_REF || ')' ELSE '' END AS AFLIFA
						FROM prod_web100t.DAT_ART_FOURN
						JOIN eco.t_articles ON AF_ARTICLE = t_articles.code_original
						JOIN eco.t_fournisseurs ON AF_FOURNISSEUR = t_fournisseurs.code_original
						) 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
					);
					
				DROP TABLE IF EXISTS w_ART_TVA;
				CREATE TEMP TABLE w_ART_TVA AS
				SELECT ART_OID, FLD_TAUX
				FROM prod_web100t.DAT_ARTICLE
				JOIN prod_web100t.MWS_NMCL_TAUXTVA ON OID = ART_TVA
				;
				
				UPDATE eco.t_articles SET
					taux_tva_en_cours = CASE WHEN FLD_TAUX <> 0.196 THEN FLD_TAUX*100 ELSE 20 END
				FROM w_ART_TVA
				WHERE ART_OID = t_articles.code_original AND
					taux_tva_en_cours IS DISTINCT FROM (CASE WHEN FLD_TAUX <> 0.196 THEN FLD_TAUX*100 ELSE 20 END)
				;
				
			]]></sqlcmd>
		</NODE>

		<NODE label="Tables Fournisseurs">
			<sqlcmd><![CDATA[
			
			-- Types fournisseurs
			
			INSERT INTO eco.t_fournisseurs(code, texte, texte_court, code_original)
			SELECT FRN_CODE, FRN_LIB, FRN_LIB_COURT, FRN_OID
			FROM prod_web100t.DAT_FOURNISSEUR
				LEFT JOIN eco.t_fournisseurs ON (code_original = FRN_OID)
			WHERE code_original IS NULL AND FRN_OID <> 0
			;
			
			INSERT INTO base.t_codes_postaux(code, texte, texte_court, departement_id)
			SELECT FRN_CODEPOSTAL, MAX(FRN_VILLE), MAX(FRN_VILLE), MAX(t_departements.oid)
			FROM prod_web100t.DAT_FOURNISSEUR
			LEFT JOIN base.t_codes_postaux ON FRN_CODEPOSTAL = t_codes_postaux.code
			JOIN base.t_departements ON CASE WHEN FRN_CODEPOSTAL BETWEEN '01' AND '95999' AND length(FRN_CODEPOSTAL) = 5 THEN substr(FRN_CODEPOSTAL,1,2) else '99' END = t_departements.code
			WHERE t_codes_postaux.oid IS NULL 
			GROUP BY 1
			;
			
			UPDATE eco.t_fournisseurs SET
				texte = FRN_LIB,
				texte_court = FRN_LIB_COURT,
				adresse = trim(FRN_ADRESSE1 || ' ' || FRN_ADRESSE2 || ' ' || FRN_ADRESSE3),
				code_postal_id = COALESCE(t_codes_postaux.oid,0),
				ville = FRN_VILLE,
				telephone = FRN_TELEPHONE,
				fax = FRN_FAX,
				email = FRN_EMAIL,
				code_edi = FRN_CODE_EXTERNE,
				siret = FRN_SIRET
			FROM prod_web100t.DAT_FOURNISSEUR
			LEFT JOIN base.t_codes_postaux ON FRN_CODEPOSTAL = t_codes_postaux.code
			WHERE 
				t_fournisseurs.code_original = FRN_OID AND
				(
				t_fournisseurs.texte IS DISTINCT FROM FRN_LIB OR
				t_fournisseurs.texte_court IS DISTINCT FROM FRN_LIB_COURT OR
				t_fournisseurs.adresse IS DISTINCT FROM trim(FRN_ADRESSE1 || ' ' || FRN_ADRESSE2 || ' ' || FRN_ADRESSE3) OR
				t_fournisseurs.code_postal_id IS DISTINCT FROM COALESCE(t_codes_postaux.oid,0) OR
				t_fournisseurs.ville IS DISTINCT FROM FRN_VILLE OR
				t_fournisseurs.telephone IS DISTINCT FROM FRN_TELEPHONE OR
				t_fournisseurs.fax IS DISTINCT FROM FRN_FAX OR
				t_fournisseurs.email IS DISTINCT FROM FRN_EMAIL OR
				t_fournisseurs.code_edi IS DISTINCT FROM FRN_CODE_EXTERNE OR
				t_fournisseurs.siret IS DISTINCT FROM FRN_SIRET
				)
			;
			
			UPDATE eco.t_fournisseurs
			SET code_comptable = CPT_AUXILIAIRE
			FROM
			(
			SELECT CPT_REF, (MAX(ARRAY[CPT_CREATED::text,CPT_AUXILIAIRE::text]))[2] AS CPT_AUXILIAIRE, count(*)
			FROM prod_web100t.CPT_COMPTES
			WHERE CPT_CODE LIKE '40%' AND
				CPT_AUXILIAIRE <> '' AND
				CPT_REF <> 0
			GROUP BY 1
			) subview
			WHERE t_fournisseurs.code_original = subview.CPT_REF AND
				code_comptable IS DISTINCT FROM CPT_AUXILIAIRE
			;
				
				
				
			-- Lien article fournisseur
			DROP TABLE IF EXISTS w_ART_FOURN;
			CREATE TEMP TABLE w_ART_FOURN AS
			SELECT DAT_ART_FOURN.*,
				AF_OID::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 AF_PRINCIPAL = 1 THEN '1' ELSE '0' END AS est_fournisseur_principal,
				AF_CODE_REF AS code_reference_fournisseur,
				AF_DESIGNATION AS texte_reference_fournisseur,
				ART_CIP AS code_cip,
				''::text AS code_cahpp,
				COALESCE(t_unites.oid,0) AS unite_approvisionnement_id,
				AF_NB_US_PAR_UA AS nombre_conditionnement_approvisionnement,
				COALESCE(TAF_TARIF,CA_PU,0) AS prix_unitaire_en_cours,
				0::bigint AS ucd_id,
				CASE 
					WHEN AF_CODE_REF LIKE '34008%' AND length(AF_CODE_REF) = 13 THEN substr(AF_CODE_REF,6,7)
					WHEN ND_CODE LIKE '34008%' AND length(ND_CODE) = 13 THEN substr(ND_CODE,6,7)
					WHEN length(ND_CODE) = 7 AND ND_CODE LIKE '9%' THEN ND_CODE
					ELSE '' END AS code_ucd, 
				CASE 
					WHEN AF_CODE_REF LIKE '34008%' AND length(AF_CODE_REF) = 13 THEN AF_CODE_REF
					WHEN ND_CODE LIKE '34008%' AND length(ND_CODE) = 13 THEN ND_CODE
					ELSE '' END AS code_ucd13,
				COALESCE(subview_lpp.lpp_id,0) AS lpp_id
			FROM prod_web100t.DAT_ART_FOURN
			JOIN prod_web100t.DAT_ARTICLE ON AF_ARTICLE = ART_OID
			JOIN prod_web100t.DEF_TYPE_ARTICLE ON DEF_TYPE_ARTICLE.TA_OID = art_type_article
			JOIN eco.t_sites ON t_sites.code_original = DEF_TYPE_ARTICLE.TA_EF
			LEFT JOIN w_SGN_NMCL_DATA ON NMCL_IDENT = 'UCD' AND ND_INVARIANT_OID = ART_UCD
			LEFT JOIN eco.t_articles ON AF_ARTICLE = t_articles.code_original
			LEFT JOIN eco.t_fournisseurs ON AF_FOURNISSEUR = t_fournisseurs.code_original
			LEFT JOIN eco.t_fournisseurs t_fournisseurs_distributeur ON AF_DISTRIBUTEUR = t_fournisseurs_distributeur.code_original
			LEFT JOIN eco.t_unites ON AF_UNIT_APPRO = t_unites.code_original
			LEFT JOIN (
				SELECT AL_ARTICLE,(MAX(Array[AL_DEBUT::text,t_lpp.oid::text]))[2]::bigint AS lpp_id
				FROM prod_web100t.DAT_ART_LPP
				JOIN w_SGN_NMCL_DATA ON NMCL_IDENT = 'LPP' AND ND_INVARIANT_OID = AL_LPP
				JOIN base.t_lpp ON t_lpp.code = substr(ND_CODE,1,7)
				WHERE AL_DELETED = 0
				GROUP BY 1
				) subview_lpp ON AF_ARTICLE = AL_ARTICLE
			LEFT JOIN (
				SELECT TAF_ART_FOURNISSEUR, (MAX(ARRAY[TAF_DEBUT::text,TAF_TARIF::text]))[2]::numeric AS TAF_TARIF
				FROM prod_web100t.DAT_TARIF_ART_FOURN
				GROUP BY 1
				) subview_taf ON AF_OID = TAF_ART_FOURNISSEUR
			LEFT JOIN (
				SELECT CA_ARTICLE, CDE_FOURNISSEUR, (MAX(Array[CDE_DATE_CDE::text,CA_PU::text]))[2]::numeric AS CA_PU
				FROM prod_web100t.CDE_COMMANDE_ART
				JOIN prod_web100t.CDE_COMMANDE ON CA_COMMANDE = CDE_OID
				GROUP BY 1,2
				) subview_cde ON
					AF_ARTICLE = CA_ARTICLE AND
					AF_FOURNISSEUR = CDE_FOURNISSEUR
			;

			UPDATE w_ART_FOURN
			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_ART_FOURN
			WHERE code_original NOT IN (SELECt code_original FROM eco.t_article_fournisseur)
			;
			
			UPDATE eco.t_article_fournisseur SET
			    article_id =                                w_ART_FOURN.article_id, 
			    fournisseur_id =                            w_ART_FOURN.fournisseur_id,
			    fournisseur_distributeur_id =               w_ART_FOURN.fournisseur_distributeur_id, 
			    est_fournisseur_principal =                 w_ART_FOURN.est_fournisseur_principal, 
			    code_reference_fournisseur =                w_ART_FOURN.code_reference_fournisseur, 
			    texte_reference_fournisseur =               w_ART_FOURN.texte_reference_fournisseur, 
			    code_cip =                                  w_ART_FOURN.code_cip, 
			    code_cahpp =                                w_ART_FOURN.code_cahpp, 
			    unite_approvisionnement_id =                w_ART_FOURN.unite_approvisionnement_id, 
			    nombre_conditionnement_approvisionnement =  w_ART_FOURN.nombre_conditionnement_approvisionnement, 
			    prix_unitaire_en_cours =                    w_ART_FOURN.prix_unitaire_en_cours, 
			    ucd_id =                                    w_ART_FOURN.ucd_id, 
			    code_ucd13 =                                w_ART_FOURN.code_ucd13,
			    lpp_id =                                    w_ART_FOURN.lpp_id
			FROM w_ART_FOURN
			WHERE t_article_fournisseur.code_original = w_ART_FOURN.code_original AND
				(
				t_article_fournisseur.article_id IS DISTINCT FROM                                w_ART_FOURN.article_id OR 
				    t_article_fournisseur.fournisseur_id IS DISTINCT FROM                            w_ART_FOURN.fournisseur_id OR
				    t_article_fournisseur.fournisseur_distributeur_id IS DISTINCT FROM               w_ART_FOURN.fournisseur_distributeur_id OR 
				    t_article_fournisseur.est_fournisseur_principal IS DISTINCT FROM                 w_ART_FOURN.est_fournisseur_principal OR 
				    t_article_fournisseur.code_reference_fournisseur IS DISTINCT FROM                w_ART_FOURN.code_reference_fournisseur OR 
				    t_article_fournisseur.texte_reference_fournisseur IS DISTINCT FROM               w_ART_FOURN.texte_reference_fournisseur OR 
				    t_article_fournisseur.code_cip IS DISTINCT FROM                                  w_ART_FOURN.code_cip OR 
				    t_article_fournisseur.code_cahpp IS DISTINCT FROM                                w_ART_FOURN.code_cahpp OR 
				    t_article_fournisseur.unite_approvisionnement_id IS DISTINCT FROM                w_ART_FOURN.unite_approvisionnement_id OR 
				    t_article_fournisseur.nombre_conditionnement_approvisionnement IS DISTINCT FROM  w_ART_FOURN.nombre_conditionnement_approvisionnement OR 
				    t_article_fournisseur.prix_unitaire_en_cours IS DISTINCT FROM                    w_ART_FOURN.prix_unitaire_en_cours OR 
				    t_article_fournisseur.ucd_id IS DISTINCT FROM                                    w_ART_FOURN.ucd_id OR 
				    t_article_fournisseur.code_ucd13 IS DISTINCT FROM                                w_ART_FOURN.code_ucd13 OR
				    t_article_fournisseur.lpp_id IS DISTINCT FROM                                    w_ART_FOURN.lpp_id
				)
			;
			
			
			]]></sqlcmd>
		</NODE>

		<NODE label="Autres tables">
			<sqlcmd><![CDATA[
			
				
			-- Unités fonctionnelles
			
			--INSERT INTO eco.t_unites_fonctionnelles(code, texte, texte_court, code_original)
			--SELECT UF_CODE, UF_LIB, UF_LIB, UF_OID 
			--FROM prod_web100t.DEF_UF_GESTION
			--	LEFT JOIN eco.t_unites_fonctionnelles ON (code_original = UF_OID)
			--WHERE code_original IS NULL;
			--
			--UPDATE eco.t_unites_fonctionnelles
			--SET texte = UF_LIB,
			--	texte_court = UF_LIB
			--FROM prod_web100t.DEF_UF_GESTION
			--WHERE code_original = UF_CODE
			--AND (texte IS DISTINCT FROM UF_LIB OR
			--	texte_court IS DISTINCT FROM UF_LIB);
				
			INSERT INTO eco.t_unites_fonctionnelles (code, texte, texte_court, code_original)
			SELECT ND_CODE, ND_LIB, ND_LIB, ND_INVARIANT_OID 
			FROM w_SGN_NMCL_DATA 
				LEFT JOIN eco.t_unites_fonctionnelles ON (code_original = ND_INVARIANT_OID)
			WHERE NMCL_IDENT = 'UNITEFONCTIONNELLE' AND code_original IS NULL
			GROUP BY 1,2,3,4;
			
			UPDATE eco.t_unites_fonctionnelles
			SET texte = ND_LIB,
				texte_court = ND_LIB
			FROM w_SGN_NMCL_DATA 
			WHERE NMCL_IDENT = 'UNITEFONCTIONNELLE' AND code_original = ND_CODE
			AND (texte IS DISTINCT FROM ND_LIB OR
				texte_court IS DISTINCT FROM ND_LIB);
				

			-- Types mouvements
						
			INSERT INTO eco.t_types_mouvements(code, texte, texte_court, code_original)
			SELECT TM_CODE, TM_LIB, TM_LIB, TM_OID
			FROM prod_web100t.STK_TYPE_MVT
				LEFT JOIN eco.t_types_mouvements ON (code_original = TM_OID)
			WHERE code_original IS NULL AND TM_CODE <> ''
			;
			
			UPDATE eco.t_types_mouvements
			SET texte = TM_LIB,
				texte_court = TM_LIB
			FROM prod_web100t.STK_TYPE_MVT
			WHERE 
				TM_CODE <> '' AND
				code_original = TM_OID AND
				(texte IS DISTINCT FROM TM_LIB OR
				texte_court IS DISTINCT FROM TM_LIB);
			
			INSERT INTO eco.t_types_mouvements(code_original, code, texte, texte_court) 
			SELECT 'CTI_STOCK', 'CTI_STOCK', 'Stock début de mois', 'Stock début de mois'
			WHERE 'CTI_STOCK' NOT IN (SELECT code_original FROM eco.t_types_mouvements);
			
			
			-- Gestionnaires
			
			INSERT INTO eco.t_gestionnaires(code, texte, texte_court, code_original)
			SELECT USR_OID, USR_NOM || ' ' || USR_PRENOM, USR_NOM || ' ' || USR_PRENOM, USR_OID
			FROM prod_web100t.SO_USERS LEFT JOIN eco.t_gestionnaires ON (code_original = USR_OID)
			WHERE code_original IS NULL;
			
			UPDATE eco.t_gestionnaires
			SET texte = USR_NOM || ' ' || USR_PRENOM,
				texte_court = USR_NOM || ' ' || USR_PRENOM
			FROM prod_web100t.SO_USERS
			WHERE code_original = USR_OID
			AND (texte IS DISTINCT FROM (USR_NOM || ' ' || USR_PRENOM) OR
				texte_court IS DISTINCT FROM (USR_NOM || ' ' || USR_PRENOM));
			
			

			]]></sqlcmd>
		</NODE>


	</NODE>
	<NODE name="PROD" label="RECUPERATION DES DONNEES DE PRODUCTION">
		<NODE label="Commandes">
			<sqlcmd><![CDATA[
				
				SELECT base.cti_execute('INSERT INTO eco.t_divers(code,valeur, description) 
					VALUES (''W1_SAUF_TYPE_CDE'','''',''valeurs de type de commande à ne pas inclure dans CTI (séparées par ,)'')',1)
				WHERE (SELECT count(*) FROM eco.t_divers WHERE code = 'W1_SAUF_TYPE_CDE') = 0;

			
				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,
					site_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 
					CDE_OID,
					(case when (select max(length(CDE_NUMERO)) from prod_web100t.CDE_COMMANDE)<=6 then trim(to_char(CDE_NUMERO,'000000')) 
					when (select max(length(CDE_NUMERO)) from prod_web100t.CDE_COMMANDE)=7 then trim(to_char(CDE_NUMERO,'0000000'))
					when (select max(length(CDE_NUMERO)) from prod_web100t.CDE_COMMANDE)=8 then trim(to_char(CDE_NUMERO,'00000000')) end) as numero,
					CDE_DATE_CDE AS date_commande,
					CDE_DATE_ENGAGEMENT AS date_engagement,
					trim(CDE_OBJET) AS objet,
					trim(CDE_REFERENCE) AS reference,
					CDE_DATE_LIVRAISON AS date_livraison_prevue,
					CASE WHEN CDE_RECEPTION != 0 THEN 'T' ELSE 'N' END AS etat_reception,
					CDE_DATE_RECEPTION AS date_reception,
					CASE 
						WHEN CDE_ETAT_LIQUIDATION = 3 THEN 'T'
						WHEN CDE_ETAT_LIQUIDATION = 2 THEN 'P'
						ELSE 'N' END AS etat_liquidation,
					COALESCE(t_fournisseurs.oid,0) AS fournisseur_id,
					COALESCE(t_gestionnaires.oid,0) AS gestionnaire_id,
					COALESCE(t_lieux_commande.oid,0) AS lieu_commande_id,
					COALESCE(t_lieux_facturation.oid,0) AS lieu_facturation_id,
					COALESCE(t_lieux_livraison.oid,0) AS lieu_livraison_id,
					COALESCE(t_sites.oid,0) AS site_id,
					COALESCE(t_unites_fonctionnelles.oid,0)AS unite_fonctionnelle_id,
					0 AS montant_commande_ht,
					0 AS montant_commande_remise,
					0 AS montant_commande_tva,
					0 AS montant_commande_port,
					CDE_MONTANT_TTC AS montant_commande_total,
					0 AS montant_liquidation_ht,
					0 AS montant_liquidation_remise,
					0 AS montant_liquidation_tva,
					0 AS montant_liquidation_port,
					CDE_MONTANT_LIQUIDE AS montant_liquidation_total
				FROM prod_web100t.CDE_COMMANDE
					JOIN eco.t_sites ON CDE_EF = t_sites.code_original
					LEFT JOIN eco.t_fournisseurs ON CDE_FOURNISSEUR = t_fournisseurs.code_original
					LEFT JOIN eco.t_gestionnaires ON CDE_CREATOR = t_gestionnaires.code_original
					LEFT JOIN eco.t_lieux t_lieux_commande ON CDE_LIEU_STOCKAGE = t_lieux_commande.code_original
					LEFT JOIN eco.t_lieux t_lieux_facturation ON CDE_LIEU_FACTURATION = t_lieux_facturation.code_original
					LEFT JOIN eco.t_lieux t_lieux_livraison ON CDE_LIEU_LIVRAISON = t_lieux_livraison.code_original
					LEFT JOIN eco.t_unites_fonctionnelles ON CDE_UF_CDE = t_unites_fonctionnelles.code_original
					LEFT JOIN eco.t_divers ON t_divers.code = 'W100T_SAUF_TYPE_CDE'
				WHERE CDE_DATE_CDE >= '[ENV_ECO_ANNEEDEBUT]-01-01' AND
					-- type N  hors commande renouvellement R ? et dépot RD ?
					CASE WHEN t_divers.valeur IS NOT NULL THEN CDE_TYPE != ALL(string_to_array(t_divers.valeur,',')) ELSE TRUE END
					-- test de desactivation
			    AND CDE_DATE_VALIDATION is not null
				;
				
				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,
					lpp_id,
					ucd_id,
					unite_approvisionnement_id,
					compte_id,
					quantite_approvisionnement,
					prix_unitaire_approvisionnement,
					multiplicateur_stockage,
					quantite_stockage,
					prix_unitaire_stockage,
					
					montant_commande_ht,
					montant_commande_remise,
					montant_commande_tva,
					montant_commande_port,
					montant_commande_ttc,

					quantite_livraison_approvisionnement,
					quantite_livraison_stockage,
					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,
					fournisseur_distributeur_id)
				SELECT
					CA_COMMANDE || '|' || CA_OID AS code_original,
					p_commandes.oid AS commande_id,
					rank() OVER (PARTITION BY CA_COMMANDE ORDER BY CA_OID) AS ligne_commande,
					trim(CA_LIB) AS texte,
					COALESCE(t_articles.oid, 0) AS article_id,
					COALESCE(t_articles.lpp_id, 0) AS lpp_id,
					COALESCE(t_articles.ucd_id, 0) AS ucd_id,
					COALESCE(t_unites.oid,t_articles.unite_stockage_id,0) AS unite_approvisionnement_id,
					COALESCE(t_articles.compte_id, 0) AS compte_id,
					CA_QUANTITE AS quantite_approvisionnement,
					CA_PU AS prix_unitaire_approvisionnement,
					CA_NB_US_PAR_UA AS multiplicateur_stockage,
					CA_QUANTITE AS quantite_stockage,
					COALESCE(PEF_PUMP,0) AS prix_unitaire_stockage,

					CA_MONTANT_HT AS montant_commande_ht,
					CA_REMISE_VAL AS montant_commande_remise,
					CASE WHEN CA_MONTANT_TTC != 0 THEN  CA_MONTANT_TTC - CA_MONTANT_HT ELSE w_ART_TVA.FLD_TAUX * CA_MONTANT_HT END AS montant_commande_tva,
					0 AS montant_commande_port,
					CASE WHEN CA_MONTANT_TTC != 0 THEN CA_MONTANT_TTC ELSE CA_MONTANT_HT * (1+w_ART_TVA.FLD_TAUX) END AS montant_commande_ttc,
					
					CA_QTE_RECEPTIONNEE AS quantite_livraison_approvisionnement,
					CA_QTE_RECEPTIONNEE AS quantite_livraison_stockage,
					CA_MONTANT_HT_REC AS montant_livraison_ht,
					CA_MONTANT_TTC_REC AS montant_livraison_ttc,
					CASE 
						WHEN CA_QTE_RECEPTIONNEE >= CA_QUANTITE THEN 'T' 
						WHEN CA_RECEPTIONNEE = '1' THEN 'P'
						ELSE 'N' END AS	etat_livraison,
					
					0,
					0,
					0,
					0,
					0,
					0,
					
					COALESCE(MWS_NMCL_TAUXTVA.FLD_TAUX*100,0) AS taux_tva,
					p_commandes.fournisseur_id AS fournisseur_distributeur_id

				FROM prod_web100t.CDE_COMMANDE_ART
					JOIN eco.p_commandes ON CA_COMMANDE = p_commandes.code_original
					LEFT JOIN prod_web100t.STK_PUMP_EF ON PEF_ARTICLE = CA_ARTICLE AND date_reception = PEF_DATE
					LEFT JOIN w_ART_TVA ON CA_ARTICLE = ART_OID
					LEFT JOIN eco.t_articles ON CA_ARTICLE = t_articles.code_original AND t_articles.oid <> 0
					LEFT JOIN eco.t_unites ON CA_UNIT_APPRO = t_unites.code_original
					LEFT JOIN prod_web100t.MWS_NMCL_TAUXTVA ON CA_TVA = MWS_NMCL_TAUXTVA.OID
				;
				
				-- liquidation 
				DROP TABLE IF EXISTS w_liquidation;
				CREATE TEMP TABLE w_liquidation AS
				SELECT
					LL_COMMANDE_LIGNE,
					SUM((CASE WHEN LQD_SIGNE = 'P' THEN 1 ELSE -1 END) * LL_MONTANT_HT_HORS_REMISE) AS montant_liquidation_ht,
					SUM((CASE WHEN LQD_SIGNE = 'P' THEN 1 ELSE -1 END) * LL_REMISE_VAL) AS montant_liquidation_remise,
					SUM((CASE WHEN LQD_SIGNE = 'P' THEN 1 ELSE -1 END) * LL_MONTANT_TVA) AS montant_liquidation_tva,
					0 AS montant_liquidation_port,
					SUM((CASE WHEN LQD_SIGNE = 'P' THEN 1 ELSE -1 END) * LL_MONTANT_TTC) AS montant_liquidation_total
				FROM 
					prod_web100t.LQD_LIQUIDATION_LIGNE 
					JOIN prod_web100t.LQD_LIQUIDATION ON LL_LIQUIDATION = LQD_OID
				WHERE	LQD_TYPE = 1 AND LQD_ETAT = 2
				GROUP BY 1
				ORDER BY 1
				;
				
				UPDATE eco.p_lignes_commandes SET
					montant_liquidation_ht = w_liquidation.montant_liquidation_ht,
					montant_liquidation_remise = w_liquidation.montant_liquidation_remise,
					montant_liquidation_tva = w_liquidation.montant_liquidation_tva,
					montant_liquidation_port = 0,
					montant_liquidation_ttc = w_liquidation.montant_liquidation_total,
					etat_liquidation = 'T'
				FROM 
					w_liquidation
				WHERE 
					LL_COMMANDE_LIGNE = split_part(code_original,'|',2) ;
				;				
							
				-- forcage des commandes et livraisons en fonction de la liquidation ??
				
				-- maj des montants de commandes par rapport aux lignes
				UPDATE eco.p_commandes
				SET
					montant_commande_ht = total_ht,
					montant_commande_remise = total_remise,
					montant_commande_tva = total_tva,
					montant_commande_port = total_port,
					montant_commande_total= total_ttc,
					montant_liquidation_ht = total_liquidation,
					montant_liquidation_remise = total_liquidation_remise,
					montant_liquidation_tva = total_liquidation_tva,
					montant_liquidation_port = total_liquidation_port,
					montant_liquidation_total = total_liquidation_ttc
				FROM
					(SELECT 
						commande_id,
						SUM(montant_commande_ht) AS total_ht,
						SUM(montant_commande_remise) AS total_remise,
						SUM(montant_commande_tva) AS total_tva,
						SUM(montant_commande_port) AS total_port,
						SUM(montant_commande_ttc) AS total_ttc,
						SUM(montant_liquidation_ht) AS total_liquidation,
						SUM(montant_liquidation_remise) AS total_liquidation_remise,
						SUM(montant_liquidation_tva) AS total_liquidation_tva,
						SUM(montant_liquidation_port) AS total_liquidation_port,
						SUM(montant_liquidation_ttc) AS total_liquidation_ttc
					FROM eco.p_lignes_commandes
					GROUP BY 1
					) sub
				WHERE commande_id = oid
				;
				
				SELECT base.cti_enable_index('eco', 'i_ligne_commandes_1');
				SELECT base.cti_enable_index('eco', 'i_ligne_commandes_2');
				SELECT base.cti_enable_index('eco', 'i_ligne_commandes_3');
				
				
			--	UPDATE eco.t_articles
            --
			--	SET ucd_id = subview.ucd_id
			--	FROM
			--		(
            --
            --
			--		SELECT article_id, count(distinct ucd_id), (MAX(Array[date_commande::text, ucd_id::text]))[2]::bigint AS ucd_id
			--		FROM eco.p_lignes_commandes
			--		JOIN eco.p_commandes ON commande_id = p_commandes.oid
			--		WHERE ucd_id <> 0
			--		GROUP BY 1
            --
			--		) subview
			--	WHERE t_articles.oid = subview.article_id AND
			--		subview.ucd_id IS DISTINCT FROM t_articles.ucd_id;
					

				UPDATE eco.p_lignes_commandes
				SET ucd_id = t_articles.ucd_id
				FROM eco.t_articles
				WHERE article_id = t_articles.oid AND
					p_lignes_commandes.ucd_id = 0 AND
					t_articles.ucd_id <> 0;

			]]></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,
					date, 
					sens_mouvement,
					type_mouvement_id, 
					texte, 
					gestionnaire_id, 
					lieu_id, 
					site_id, 
					unite_fonctionnelle_id, 
					article_id, 
					compte_id, 
					fournisseur_id,
					commande_id,
					ligne_commande,
					lpp_id,
					ucd_id,
					prix_unitaire, 
					entree_quantite, 
					sortie_quantite,
					entree_montant, 
					entree_montant_ht, 
					sortie_montant,
					sortie_montant_ht,
					stock_quantite_debut,
					stock_quantite_fin,
					no_sejour)
				SELECT 
					MVT_OID::text AS code_original,
					MVT_DATE AS date,
					CASE WHEN TM_SENS = 1 THEN 'E' ELSE 'S' END AS sens_mouvement,
					COALESCE(t_types_mouvements.oid,0) AS type_mouvement_id,
					t_lieux.texte || ' - ' || t_types_mouvements.texte || ' ' || t_articles.texte_court AS texte,
					COALESCE(t_gestionnaires.oid,0) AS gestionnaire_id,
					COALESCE(t_lieux.oid,0) AS lieu_id,
					COALESCE(t_sites.oid,0) AS site_id,
					COALESCE(t_unites_fonctionnelles.oid,0) AS unite_fonctionnelle_id,
					COALESCE(t_articles.oid,0) AS article_id,
					COALESCE(t_articles.compte_id,0) AS compte_id,
					COALESCE(p_commandes.fournisseur_id,p_commandes_2.fournisseur_id,fournisseur_principal_id,0) AS fournisseur_id,
					COALESCE(p_lignes_commandes.commande_id,p_commandes.oid,0) AS commande_id,
					COALESCE(p_lignes_commandes.ligne_commande,0),
					COALESCE(t_articles.lpp_id,0) AS lpp_id,
					COALESCE(t_articles.ucd_id,0) AS ucd_id,
					MVT_SIGNE * base.cti_division((MVT_MONTANT/(1 + FLD_TAUX))::numeric,MVT_QTE::numeric) AS prix_unitaire,
					CASE WHEN TM_SENS = 1 THEN MVT_QTE ELSE 0 END AS entree_quantite,
					CASE WHEN TM_SENS = -1 THEN MVT_QTE ELSE 0 END AS sortie_quantite,
					CASE WHEN TM_SENS = 1 THEN MVT_MONTANT ELSE 0 END AS entree_montant,
					CASE WHEN TM_SENS = 1 THEN MVT_MONTANT/(1 + FLD_TAUX) ELSE 0 END AS entree_montant_ht,
					CASE WHEN TM_SENS = -1 THEN MVT_MONTANT ELSE 0 END AS sortie_montant,
					CASE WHEN TM_SENS = -1 THEN MVT_MONTANT/(1 + FLD_TAUX) ELSE 0 END AS sortie_montant_ht,
					0 AS stock_quantite_debut,
					0 AS stock_quantite_fin,
					DA_NUMERO AS no_sejour
				FROM prod_web100t.STK_MVT
					JOIN eco.t_sites ON MVT_EF = t_sites.code_original
					LEFT JOIN prod_web100t.STK_TYPE_MVT ON MVT_TYPE = TM_OID
					LEFT JOIN eco.t_types_mouvements ON MVT_TYPE = t_types_mouvements.code_original
					LEFT JOIN eco.t_gestionnaires ON 0 = t_gestionnaires.code_original
					LEFT JOIN eco.t_lieux ON MVT_LIEU = t_lieux.code_original
					LEFT JOIN eco.t_unites_fonctionnelles ON CASE WHEN TM_SENS = 1 THEN MVT_UF ELSE MVT_UF_DEST  END = t_unites_fonctionnelles.code_original
					LEFT JOIN eco.t_articles ON MVT_ARTICLE = t_articles.code_original AND t_articles.oid <> 0
					LEFT JOIN w_ART_TVA ON t_articles.code_original = ART_OID
					LEFT JOIN prod_web100t.CDE_COMMANDE_ART ON CA_OID = MVT_DATA_REF AND MVT_DATA_REF <> 0
					LEFT JOIN eco.p_commandes ON MVT_COMMANDE = p_commandes.code_original AND MVT_COMMANDE <> 0
					LEFT JOIN eco.p_lignes_commandes ON CDE_COMMANDE_ART.CA_COMMANDE||'|'||CA_OID = p_lignes_commandes.code_original
					LEFT JOIN eco.p_commandes p_commandes_2 ON p_lignes_commandes.commande_id = p_commandes_2.oid
					LEFT JOIN prod_web100t.DAT_DA ON DA_VENUE = MVT_VENUE AND MVT_VENUE != 0
				WHERE MVT_DATE > '[ENV_ECO_ANNEEDEBUT]-01-01'
					--AND MVT_CANCELATION != 1
				ORDER BY MVT_OID;

				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_1');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_2');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_3');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_4');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_5');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_6');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_7');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_8');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_9');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_10');
				
				UPDATE eco.p_mouvements_articles SET
					entree_quantite = 0 - entree_quantite,
					sortie_quantite = 0 - sortie_quantite,
					entree_montant = 0 - entree_montant,
					sortie_montant = 0 - sortie_montant,
					entree_montant_ht = 0 - entree_montant_ht,
					sortie_montant_ht = 0 - sortie_montant_ht,
					sortie_montant_original_ttc = 0 - sortie_montant_original_ttc,
					sortie_montant_original_ht = 0 - sortie_montant_original_ht
				FROM eco.t_types_mouvements
				WHERE type_mouvement_id = t_types_mouvements.oid AND
					t_types_mouvements.particularite_inverser_signe = '1';
					
				UPDATE eco.p_mouvements_articles SET
					entree_quantite = sortie_quantite,
					sortie_quantite = entree_quantite,
					entree_montant = sortie_montant,
					sortie_montant = entree_montant,
					entree_montant_ht = sortie_montant_ht,
					sortie_montant_ht = entree_montant_ht,
					sortie_montant_original_ttc = sortie_montant_original_ht,
					sortie_montant_original_ht = sortie_montant_original_ttc,
					sens_mouvement = sens_force
				FROM eco.t_types_mouvements
				WHERE type_mouvement_id = t_types_mouvements.oid AND
					t_types_mouvements.sens_force IN ('E','S') AND t_types_mouvements.sens_force != p_mouvements_articles.sens_mouvement
				;
				
				
			
			]]></sqlcmd>
		</NODE>


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

				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,
					sortie_quantite,
					sortie_montant,
					stock_quantite_fin,
					stock_valeur_fin,
					pump)
				SELECT 
					COALESCE(t_articles.oid,0) AS article_id,
					date(date_trunc('month',SE_DATE + interval '1 month') - interval '1 day') AS date_fin,
					date(date_trunc('month',SE_DATE)) AS date_debut,
					COALESCE(t_articles.ucd_id,0) AS ucd_id,
					COALESCE(t_articles.lpp_id,0) AS lpp_id,
					COALESCE(t_compte.oid,t_articles.compte_id) AS compte_id,
					COALESCE(t_lieux.oid,0) AS lieu_id,
					0::bigint AS provider_id,
					t_articles.fournisseur_principal_id AS fournisseur_id,                
					(MIN(ARRAY[SE_DATE::text,SE_QTE_DEBUT::text]))[2]::numeric AS stock_quantite_debut,
					(MIN(ARRAY[SE_DATE::text,SE_VALO_DEBUT::text]))[2]::numeric AS stock_valeur_debut,
					SUM(SE_QTE_ENTREE) AS entree_quantite,
					SUM(SE_VALO_ENTREE) AS entree_montant,
					SUM(SE_QTE_SORTIE) AS sortie_quantite,
					SUM(SE_VALO_SORTIE) AS sortie_montant,
					(MAX(ARRAY[SE_DATE::text,SE_QTE_FIN::text]))[2]::numeric AS stock_quantite_fin,
					(MAX(ARRAY[SE_DATE::text,SE_VALO_FIN::text]))[2]::numeric AS stock_valeur_fin,
					(MAX(ARRAY[SE_DATE::text,PEF_PUMP::text]))[2]::numeric
				FROM prod_web100t.STK_ETAT
					LEFT JOIN eco.t_lieux ON SE_LIEU = t_lieux.code_original
					LEFT JOIN eco.t_unites_fonctionnelles ON SE_UF = t_unites_fonctionnelles.code_original
					LEFT JOIN prod_web100t.CPT_COMPTES ON CPT_OID = SE_COMPTE
					LEFT JOIN eco.t_compte ON CPT_CODE = t_compte.code_original
					LEFT JOIN eco.t_articles ON SE_ARTICLE = t_articles.code_original AND t_articles.oid <> 0
					LEFT JOIN prod_web100t.STK_PUMP_EF ON PEF_ARTICLE = SE_ARTICLE AND PEF_DATE = SE_DATE
				GROUP BY 1,2,3,4,5,6,7,8,9
				;
				
				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');

			-- stocks journaliers			
			UPDATE eco.p_mouvements_articles 
				SET
				est_premier_jour = sub.est_premier_jour,
				est_dernier_jour = sub.est_dernier_jour,
				date_fin = sub.date_fin
				FROM (SELECT
				oid,
				 CASE
					WHEN rank() OVER (PARTITION BY p_mouvements_articles.article_id, p_mouvements_articles.site_id, p_mouvements_articles.lieu_id,date ORDER BY p_mouvements_articles.oid) = 1 THEN 1
					ELSE 0
				END AS est_premier_jour,
				CASE
					WHEN cume_dist() OVER (PARTITION BY p_mouvements_articles.article_id,  p_mouvements_articles.site_id, p_mouvements_articles.lieu_id, p_mouvements_articles.date ORDER BY p_mouvements_articles.oid) = 1 THEN 1
					ELSE 0
				END AS est_dernier_jour,
				CASE
					WHEN cume_dist() OVER (PARTITION BY p_mouvements_articles.article_id,  p_mouvements_articles.site_id, p_mouvements_articles.lieu_id, p_mouvements_articles.date ORDER BY p_mouvements_articles.oid) = 1
				THEN lead(p_mouvements_articles.date) OVER (PARTITION BY p_mouvements_articles.article_id,  p_mouvements_articles.site_id, p_mouvements_articles.lieu_id ORDER BY p_mouvements_articles.date) - interval '1 day'
					ELSE date 
				END AS date_fin
				FROm eco.p_mouvements_articles 
				) sub
				WHERE sub.oid = p_mouvements_articles.oid;

			UPDATE eco.p_mouvements_articles 
			SET date_fin = date_trunc('month',now()) - interval '1 day' + interval '1 month'
			WHERE date_fin IS NULL
			;
			
			DROP TABLE IF EXISTS w_STK_ETAT;
			CREATE TEMP TABLE w_STK_ETAT AS
			SELECT date(SE_DATE) AS SE_DATE,
				SE_QTE_DEBUT,
				SE_QTE_FIN,
				SE_VALO_FIN,
				COALESCE(t_articles.oid,0) AS article_id,
				COALESCE(t_lieux.oid,0) AS lieu_id,
				COALESCE(t_sites.oid,0) AS site_id
			FROM prod_web100t.STK_ETAT
			JOIN eco.t_sites ON SE_EF = t_sites.code_original AND t_sites.oid <> 0
			LEFT JOIN eco.t_lieux ON SE_LIEU = t_lieux.code_original AND t_lieux.oid <> 0
			LEFT JOIN eco.t_articles ON SE_ARTICLE = t_articles.code_original AND t_articles.oid <> 0
			WHERE SE_QTE_DEBUT <> 0 OR
					SE_QTE_FIN <> 0 OR
					SE_VALO_FIN <> 0 
			;

			ANALYSE w_STK_ETAT
			;

			CREATE INDEX w_STK_ETAT_i1
				ON w_STK_ETAT
				USING btree
				(article_id);

			UPDATE eco.p_mouvements_articles 
			SET
				stock_quantite_debut = CASE WHEN est_premier_jour = 1 THEN SE_QTE_DEBUT ELSE 0 END,
				stock_quantite_fin = CASE WHEN est_dernier_jour = 1 THEN SE_QTE_FIN ELSE 0 END,
				prix_unitaire_calcule = base.cti_division(SE_VALO_FIN::numeric,SE_QTE_FIN::numeric)
			FROM w_STK_ETAT
			WHERE 
				p_mouvements_articles.article_id = w_STK_ETAT.article_id AND
				p_mouvements_articles.lieu_id = w_STK_ETAT.lieu_id AND
				p_mouvements_articles.site_id = w_STK_ETAT.site_id AND 
				SE_DATE = date AND
				(
				p_mouvements_articles.stock_quantite_debut IS DISTINCT FROM CASE WHEN est_premier_jour = 1 THEN SE_QTE_DEBUT ELSE 0 END OR
				p_mouvements_articles.stock_quantite_fin IS DISTINCT FROM CASE WHEN est_dernier_jour = 1 THEN SE_QTE_FIN ELSE 0 END OR
				p_mouvements_articles.prix_unitaire_calcule IS DISTINCT FROM base.cti_division(SE_VALO_FIN::numeric,SE_QTE_FIN::numeric)
				)
			;

			--UPDATE eco.p_mouvements_articles
			--SET
			--	stock_quantite_debut = CASE WHEN est_premier_jour = 0 THEN w_stock_var.stock_quantite_fin - w_stock_var.entree_var + w_stock_var.sortie_var ELSE w_stock_var.stock_quantite_debut END,
			--	stock_quantite_fin = CASE WHEN est_dernier_jour = 0 THEN w_stock_var.stock_quantite_fin - w_stock_var.entree_var + w_stock_var.sortie_var + p_mouvements_articles.entree_quantite - p_mouvements_articles.sortie_quantite ELSE w_stock_var.stock_quantite_fin END
			--FROM
			--(
			--	SELECT
			--		oid,
			--		site_id,
			--		lieu_id,
			--		article_id,
			--		date,
			--		first_value(stock_quantite_debut) OVER (PARTITION BY lieu_id, article_id, date ORDER BY oid) AS stock_quantite_debut,
			--		last_value(stock_quantite_fin) OVER (PARTITION BY lieu_id, article_id, date ORDER BY oid) AS stock_quantite_fin,
			--		sum(entree_quantite) OVER (PARTITION BY lieu_id, article_id, date ORDER BY oid) AS entree_var,
			--		sum(sortie_quantite) OVER (PARTITION BY lieu_id, article_id, date ORDER BY oid) AS sortie_var
			--	FROM eco.p_mouvements_articles
			--	ORDER BY 1,2,3,4,5
			--) w_stock_var
			--WHERE 1=1
			--	AND p_mouvements_articles.article_id = w_stock_var.article_id
			--	AND p_mouvements_articles.site_id = w_stock_var.site_id
			--	AND p_mouvements_articles.lieu_id = w_stock_var.lieu_id
			--	AND p_mouvements_articles.oid = w_stock_var.oid
			--	AND p_mouvements_articles.date = w_stock_var.date
			--	AND p_mouvements_articles.stock_quantite_debut = 0 OR p_mouvements_articles.stock_quantite_fin = 0
			--;
			
			
			]]></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();
			
			
			SELECT eco.cti_reorganize_sejour();
			SELECT eco.cti_reorganize_sejour_ucd_lpp();
		]]></sqlcmd>

	</NODE>

</ROOT>
