<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	<NODE name="INIT" label="INITIALISATIONS">
			
		<NODE label="Tables Articles">
			
			<sqlcmd><![CDATA[
			
				-- Préparation
				

				DROP TABLE IF EXISTS w_classification_articles;
				CREATE TEMP TABLE w_classification_articles AS
				SELECT 
					SXFAMILLES_1.DOS_IDE, 
					SXFAMILLES_1.FAM_IDE AS FAM_IDE_1, SXFAMILLES_1.FAM_COD AS FAM_COD_1, SXFAMILLES_1.FAM_LIB AS FAM_LIB_1, 
					COALESCE(SXFAMILLES_2.FAM_IDE,0) AS FAM_IDE_2, COALESCE(SXFAMILLES_2.FAM_COD,'') AS FAM_COD_2, COALESCE(SXFAMILLES_2.FAM_LIB,'') AS FAM_LIB_2, 
					COALESCE(SXFAMILLES_3.FAM_IDE,0) AS FAM_IDE_3, COALESCE(SXFAMILLES_3.FAM_COD,'') AS FAM_COD_3, COALESCE(SXFAMILLES_3.FAM_LIB,'') AS FAM_LIB_3, 
					COALESCE(SXFAMILLES_4.FAM_IDE,0) AS FAM_IDE_4, COALESCE(SXFAMILLES_4.FAM_COD,'') AS FAM_COD_4, COALESCE(SXFAMILLES_4.FAM_LIB,'') AS FAM_LIB_4, 
					COALESCE(SXFAMILLES_5.FAM_IDE,0) AS FAM_IDE_5, COALESCE(SXFAMILLES_5.FAM_COD,'') AS FAM_COD_5, COALESCE(SXFAMILLES_5.FAM_LIB,'') AS FAM_LIB_5, 
					COALESCE(SXFAMILLES_6.FAM_IDE,0) AS FAM_IDE_6, COALESCE(SXFAMILLES_6.FAM_COD,'') AS FAM_COD_6, COALESCE(SXFAMILLES_6.FAM_LIB,'') AS FAM_LIB_6
				FROM prod_cegi.SXFAMILLES SXFAMILLES_1
				LEFT JOIN prod_cegi.SXFAMILLES SXFAMILLES_2 ON SXFAMILLES_2.FAM_FAM = SXFAMILLES_1.FAM_IDE AND SXFAMILLES_2.DOS_IDE = SXFAMILLES_1.DOS_IDE
				LEFT JOIN prod_cegi.SXFAMILLES SXFAMILLES_3 ON SXFAMILLES_3.FAM_FAM = SXFAMILLES_2.FAM_IDE AND SXFAMILLES_3.DOS_IDE = SXFAMILLES_2.DOS_IDE
				LEFT JOIN prod_cegi.SXFAMILLES SXFAMILLES_4 ON SXFAMILLES_4.FAM_FAM = SXFAMILLES_3.FAM_IDE AND SXFAMILLES_4.DOS_IDE = SXFAMILLES_3.DOS_IDE
				LEFT JOIN prod_cegi.SXFAMILLES SXFAMILLES_5 ON SXFAMILLES_5.FAM_FAM = SXFAMILLES_4.FAM_IDE AND SXFAMILLES_5.DOS_IDE = SXFAMILLES_4.DOS_IDE
				LEFT JOIN prod_cegi.SXFAMILLES SXFAMILLES_6 ON SXFAMILLES_6.FAM_FAM = SXFAMILLES_5.FAM_IDE AND SXFAMILLES_6.DOS_IDE = SXFAMILLES_5.DOS_IDE
				WHERE SXFAMILLES_1.FAM_NIV = 1
				ORDER BY 1,3,6,9,12,15,18;


				UPDATE w_classification_articles
				SET FAM_LIB_2 = fam_lib_1 
				WHERE FAM_LIB_2 = '???';

				UPDATE w_classification_articles
				SET FAM_COD_1 = '?'||FAM_IDE_1
				WHERE FAM_COD_1 = '' AND FAM_IDE_1 <> 0;

				INSERT INTO w_classification_articles 
				SELECT
					w_class_1.DOS_IDE, 
					w_class_1.FAM_IDE_1, w_class_1.FAM_COD_1, w_class_1.FAM_LIB_1, 
					w_class_1.FAM_IDE_2, w_class_1.FAM_COD_2, w_class_1.FAM_LIB_2, 
					w_class_1.FAM_IDE_3, w_class_1.FAM_COD_3, w_class_1.FAM_LIB_3, 
					w_class_1.FAM_IDE_4, w_class_1.FAM_COD_4, w_class_1.FAM_LIB_4, 
					w_class_1.FAM_IDE_5, w_class_1.FAM_COD_5, w_class_1.FAM_LIB_5, 
					0, '', ''
				FROM w_classification_articles w_class_1
				LEFT JOIN w_classification_articles w_class_2 ON
					w_class_1.DOS_IDE = w_class_2.DOS_IDE AND
					w_class_1.FAM_IDE_1 = w_class_2.FAM_IDE_1 AND
					w_class_1.FAM_IDE_2 = w_class_2.FAM_IDE_2 AND
					w_class_1.FAM_IDE_3 = w_class_2.FAM_IDE_3 AND
					w_class_1.FAM_IDE_4 = w_class_2.FAM_IDE_4 AND
					w_class_1.FAM_IDE_5 = w_class_2.FAM_IDE_5 AND
					w_class_2.FAM_IDE_6 = 0
				WHERE w_class_2.FAM_IDE_1 IS NULL
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19;

				INSERT INTO w_classification_articles 
				SELECT
					w_class_1.DOS_IDE, 
					w_class_1.FAM_IDE_1, w_class_1.FAM_COD_1, w_class_1.FAM_LIB_1, 
					w_class_1.FAM_IDE_2, w_class_1.FAM_COD_2, w_class_1.FAM_LIB_2, 
					w_class_1.FAM_IDE_3, w_class_1.FAM_COD_3, w_class_1.FAM_LIB_3, 
					w_class_1.FAM_IDE_4, w_class_1.FAM_COD_4, w_class_1.FAM_LIB_4, 
					0, '', '', 
					0, '', ''
				FROM w_classification_articles w_class_1
				LEFT JOIN w_classification_articles w_class_2 ON
					w_class_1.DOS_IDE = w_class_2.DOS_IDE AND
					w_class_1.FAM_IDE_1 = w_class_2.FAM_IDE_1 AND
					w_class_1.FAM_IDE_2 = w_class_2.FAM_IDE_2 AND
					w_class_1.FAM_IDE_3 = w_class_2.FAM_IDE_3 AND
					w_class_1.FAM_IDE_4 = w_class_2.FAM_IDE_4 AND
					w_class_2.FAM_IDE_5 = 0 AND
					w_class_2.FAM_IDE_6 = 0
				WHERE w_class_2.FAM_IDE_1 IS NULL
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19;

				INSERT INTO w_classification_articles 
				SELECT
					w_class_1.DOS_IDE, 
					w_class_1.FAM_IDE_1, w_class_1.FAM_COD_1, w_class_1.FAM_LIB_1, 
					w_class_1.FAM_IDE_2, w_class_1.FAM_COD_2, w_class_1.FAM_LIB_2, 
					w_class_1.FAM_IDE_3, w_class_1.FAM_COD_3, w_class_1.FAM_LIB_3, 
					0, '', '', 
					0, '', '', 
					0, '', ''
				FROM w_classification_articles w_class_1
				LEFT JOIN w_classification_articles w_class_2 ON
					w_class_1.DOS_IDE = w_class_2.DOS_IDE AND
					w_class_1.FAM_IDE_1 = w_class_2.FAM_IDE_1 AND
					w_class_1.FAM_IDE_2 = w_class_2.FAM_IDE_2 AND
					w_class_1.FAM_IDE_3 = w_class_2.FAM_IDE_3 AND
					w_class_2.FAM_IDE_4 = 0 AND
					w_class_2.FAM_IDE_5 = 0 AND
					w_class_2.FAM_IDE_6 = 0
				WHERE w_class_2.FAM_IDE_1 IS NULL
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19;

				INSERT INTO w_classification_articles 
				SELECT
					w_class_1.DOS_IDE, 
					w_class_1.FAM_IDE_1, w_class_1.FAM_COD_1, w_class_1.FAM_LIB_1, 
					w_class_1.FAM_IDE_2, w_class_1.FAM_COD_2, w_class_1.FAM_LIB_2, 
					0, '', '', 
					0, '', '', 
					0, '', '', 
					0, '', ''
				FROM w_classification_articles w_class_1
				LEFT JOIN w_classification_articles w_class_2 ON
					w_class_1.DOS_IDE = w_class_2.DOS_IDE AND
					w_class_1.FAM_IDE_1 = w_class_2.FAM_IDE_1 AND
					w_class_1.FAM_IDE_2 = w_class_2.FAM_IDE_2 AND
					w_class_2.FAM_IDE_3 = 0 AND
					w_class_2.FAM_IDE_4 = 0 AND
					w_class_2.FAM_IDE_5 = 0 AND
					w_class_2.FAM_IDE_6 = 0
				WHERE w_class_2.FAM_IDE_1 IS NULL
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19;

				INSERT INTO w_classification_articles 
				SELECT
					w_class_1.DOS_IDE, 
					w_class_1.FAM_IDE_1, w_class_1.FAM_COD_1, w_class_1.FAM_LIB_1, 
					0, '', '', 
					0, '', '', 
					0, '', '', 
					0, '', '', 
					0, '', ''
				FROM w_classification_articles w_class_1
				LEFT JOIN w_classification_articles w_class_2 ON
					w_class_1.DOS_IDE = w_class_2.DOS_IDE AND
					w_class_1.FAM_IDE_1 = w_class_2.FAM_IDE_1 AND
					w_class_2.FAM_IDE_2 = 0 AND
					w_class_2.FAM_IDE_3 = 0 AND
					w_class_2.FAM_IDE_4 = 0 AND
					w_class_2.FAM_IDE_5 = 0 AND
					w_class_2.FAM_IDE_6 = 0
				WHERE w_class_2.FAM_IDE_1 IS NULL
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19;
				
				ANALYSE w_classification_articles
				;

				DROP TABLE IF EXISTS w_ARTICLE;
				CREATE TEMP TABLE w_ARTICLE AS
				SELECT 
					SXART_GEN.DOS_IDE::text || '-'::text || SXARTICLE.ART_IDE::text AS ART_IDE_CTI,
					ART_GEN_COD::text AS ART_COD_CTI,
					0::bigint AS article_id,
					SXART_GEN.DOS_IDE, SXDOSSIER.DOS_COD,
					SXARTICLE.ART_IDE, SXART_GEN.ART_GEN_IDE, SXART_GEN.ART_GEN_COD, SXART_GEN.ART_GEN_LIB, SXART_GEN.FAM_IDE, 
					SXARTICLE.ART_SUPP,
					SXART_GEN.PLC_COMPTE,
					0::bigint AS lpp_id,
					0::bigint AS ucd_id,
					0::bigint AS compte_id,
					SXART_GEN.TVA_CODE AS TVA_CODE_article,
					0::numeric AS taux_tva_en_cours,
					0::numeric AS FAM_IDE_1, 
					''::text AS FAM_COD_1, 
					''::text AS FAM_LIB_1,
					0::numeric AS FAM_IDE_2, 
					''::text AS FAM_COD_2, 
					''::text AS FAM_LIB_2,
					0::numeric AS FAM_IDE_3, 
					''::text AS FAM_COD_3, 
					''::text AS FAM_LIB_3,
					0::numeric AS FAM_IDE_4, 
					''::text AS FAM_COD_4, 
					''::text AS FAM_LIB_4,
					0::numeric AS FAM_IDE_5, 
					''::text AS FAM_COD_5, 
					''::text AS FAM_LIB_5,
					0::numeric AS FAM_IDE_6, 
					''::text AS FAM_COD_6, 
					''::text AS FAM_LIB_6,
					''::text AS ATC_COD,
					COALESCE(SXART_SUP.DOS_IDE::text || '-'::text || SXART_SUP.FORME_IDE::text) AS FORME_IDE_CTI,
					ART_GEN_STO,
					SXART_GEN.TSK_IDE,
					TSK_COD,
					TSK_LIB,
					0::bigint AS unite_id
				FROM prod_cegi.SXART_GEN
				JOIN prod_cegi.SXARTICLE ON SXART_GEN.ART_GEN_IDE = SXARTICLE.ART_GEN_IDE AND
					SXART_GEN.DOS_IDE = SXARTICLE.DOS_IDE
				LEFT JOIN prod_cegi.SXART_SUP ON SXART_SUP.ART_IDE = SXARTICLE.ART_IDE AND
					SXART_SUP.DOS_IDE = SXARTICLE.DOS_IDE
				LEFT JOIN prod_cegi.SXTYPE_STOCK ON SXTYPE_STOCK.TSK_IDE = SXART_GEN.TSK_IDE AND
					SXTYPE_STOCK.DOS_IDE = SXARTICLE.DOS_IDE
				JOIN prod_cegi.SXDOSSIER ON SXDOSSIER.DOS_IDE = SXART_GEN.DOS_IDE 
					AND (ETAB_ID::text = ANY (string_to_array('[ETAB_ID]'::text,',')) OR '[ETAB_ID]' = '' OR '[ETAB_ID]' = '-1')
				;
				
				ANALYSE w_ARTICLE
				;
				
				UPDATE w_ARTICLE SET
					FAM_IDE_1 = w_classification_articles.FAM_IDE_1, 
					FAM_COD_1 = w_classification_articles.FAM_COD_1, 
					FAM_LIB_1 = w_classification_articles.FAM_LIB_1,
					FAM_IDE_2 = w_classification_articles.FAM_IDE_2, 
					FAM_COD_2 = w_classification_articles.FAM_COD_2, 
					FAM_LIB_2 = w_classification_articles.FAM_LIB_2,
					FAM_IDE_3 = w_classification_articles.FAM_IDE_3, 
					FAM_COD_3 = w_classification_articles.FAM_COD_3, 
					FAM_LIB_3 = w_classification_articles.FAM_LIB_3,
					FAM_IDE_4 = w_classification_articles.FAM_IDE_4, 
					FAM_COD_4 = w_classification_articles.FAM_COD_4, 
					FAM_LIB_4 = w_classification_articles.FAM_LIB_4,
					FAM_IDE_5 = w_classification_articles.FAM_IDE_5, 
					FAM_COD_5 = w_classification_articles.FAM_COD_5, 
					FAM_LIB_5 = w_classification_articles.FAM_LIB_5,
					FAM_IDE_6 = w_classification_articles.FAM_IDE_6, 
					FAM_COD_6 = w_classification_articles.FAM_COD_6, 
					FAM_LIB_6 = w_classification_articles.FAM_LIB_6
				FROM w_classification_articles 
				WHERE w_classification_articles.DOS_IDE = w_ARTICLE.DOS_IDE AND
					w_classification_articles.FAM_IDE_1 = FAM_IDE AND
					FAM_IDE <> 0 AND
					w_classification_articles.FAM_IDE_2 = 0
				;

				UPDATE w_ARTICLE SET
					FAM_IDE_1 = w_classification_articles.FAM_IDE_1, 
					FAM_COD_1 = w_classification_articles.FAM_COD_1, 
					FAM_LIB_1 = w_classification_articles.FAM_LIB_1,
					FAM_IDE_2 = w_classification_articles.FAM_IDE_2, 
					FAM_COD_2 = w_classification_articles.FAM_COD_2, 
					FAM_LIB_2 = w_classification_articles.FAM_LIB_2,
					FAM_IDE_3 = w_classification_articles.FAM_IDE_3, 
					FAM_COD_3 = w_classification_articles.FAM_COD_3, 
					FAM_LIB_3 = w_classification_articles.FAM_LIB_3,
					FAM_IDE_4 = w_classification_articles.FAM_IDE_4, 
					FAM_COD_4 = w_classification_articles.FAM_COD_4, 
					FAM_LIB_4 = w_classification_articles.FAM_LIB_4,
					FAM_IDE_5 = w_classification_articles.FAM_IDE_5, 
					FAM_COD_5 = w_classification_articles.FAM_COD_5, 
					FAM_LIB_5 = w_classification_articles.FAM_LIB_5,
					FAM_IDE_6 = w_classification_articles.FAM_IDE_6, 
					FAM_COD_6 = w_classification_articles.FAM_COD_6, 
					FAM_LIB_6 = w_classification_articles.FAM_LIB_6
				FROM w_classification_articles 
				WHERE w_classification_articles.DOS_IDE = w_ARTICLE.DOS_IDE AND
					w_classification_articles.FAM_IDE_2 = FAM_IDE AND
					FAM_IDE <> 0 AND
					w_classification_articles.FAM_IDE_3 = 0
				;

				UPDATE w_ARTICLE SET
					FAM_IDE_1 = w_classification_articles.FAM_IDE_1, 
					FAM_COD_1 = w_classification_articles.FAM_COD_1, 
					FAM_LIB_1 = w_classification_articles.FAM_LIB_1,
					FAM_IDE_2 = w_classification_articles.FAM_IDE_2, 
					FAM_COD_2 = w_classification_articles.FAM_COD_2, 
					FAM_LIB_2 = w_classification_articles.FAM_LIB_2,
					FAM_IDE_3 = w_classification_articles.FAM_IDE_3, 
					FAM_COD_3 = w_classification_articles.FAM_COD_3, 
					FAM_LIB_3 = w_classification_articles.FAM_LIB_3,
					FAM_IDE_4 = w_classification_articles.FAM_IDE_4, 
					FAM_COD_4 = w_classification_articles.FAM_COD_4, 
					FAM_LIB_4 = w_classification_articles.FAM_LIB_4,
					FAM_IDE_5 = w_classification_articles.FAM_IDE_5, 
					FAM_COD_5 = w_classification_articles.FAM_COD_5, 
					FAM_LIB_5 = w_classification_articles.FAM_LIB_5,
					FAM_IDE_6 = w_classification_articles.FAM_IDE_6, 
					FAM_COD_6 = w_classification_articles.FAM_COD_6, 
					FAM_LIB_6 = w_classification_articles.FAM_LIB_6
				FROM w_classification_articles 
				WHERE w_classification_articles.DOS_IDE = w_ARTICLE.DOS_IDE AND
					w_classification_articles.FAM_IDE_3 = FAM_IDE AND
					FAM_IDE <> 0 AND
					w_classification_articles.FAM_IDE_4 = 0
				;

				UPDATE w_ARTICLE SET
					FAM_IDE_1 = w_classification_articles.FAM_IDE_1, 
					FAM_COD_1 = w_classification_articles.FAM_COD_1, 
					FAM_LIB_1 = w_classification_articles.FAM_LIB_1,
					FAM_IDE_2 = w_classification_articles.FAM_IDE_2, 
					FAM_COD_2 = w_classification_articles.FAM_COD_2, 
					FAM_LIB_2 = w_classification_articles.FAM_LIB_2,
					FAM_IDE_3 = w_classification_articles.FAM_IDE_3, 
					FAM_COD_3 = w_classification_articles.FAM_COD_3, 
					FAM_LIB_3 = w_classification_articles.FAM_LIB_3,
					FAM_IDE_4 = w_classification_articles.FAM_IDE_4, 
					FAM_COD_4 = w_classification_articles.FAM_COD_4, 
					FAM_LIB_4 = w_classification_articles.FAM_LIB_4,
					FAM_IDE_5 = w_classification_articles.FAM_IDE_5, 
					FAM_COD_5 = w_classification_articles.FAM_COD_5, 
					FAM_LIB_5 = w_classification_articles.FAM_LIB_5,
					FAM_IDE_6 = w_classification_articles.FAM_IDE_6, 
					FAM_COD_6 = w_classification_articles.FAM_COD_6, 
					FAM_LIB_6 = w_classification_articles.FAM_LIB_6
				FROM w_classification_articles 
				WHERE w_classification_articles.DOS_IDE = w_ARTICLE.DOS_IDE AND
					w_classification_articles.FAM_IDE_4 = FAM_IDE AND
					FAM_IDE <> 0 AND
					w_classification_articles.FAM_IDE_5 = 0
				;

				UPDATE w_ARTICLE SET
					FAM_IDE_1 = w_classification_articles.FAM_IDE_1, 
					FAM_COD_1 = w_classification_articles.FAM_COD_1, 
					FAM_LIB_1 = w_classification_articles.FAM_LIB_1,
					FAM_IDE_2 = w_classification_articles.FAM_IDE_2, 
					FAM_COD_2 = w_classification_articles.FAM_COD_2, 
					FAM_LIB_2 = w_classification_articles.FAM_LIB_2,
					FAM_IDE_3 = w_classification_articles.FAM_IDE_3, 
					FAM_COD_3 = w_classification_articles.FAM_COD_3, 
					FAM_LIB_3 = w_classification_articles.FAM_LIB_3,
					FAM_IDE_4 = w_classification_articles.FAM_IDE_4, 
					FAM_COD_4 = w_classification_articles.FAM_COD_4, 
					FAM_LIB_4 = w_classification_articles.FAM_LIB_4,
					FAM_IDE_5 = w_classification_articles.FAM_IDE_5, 
					FAM_COD_5 = w_classification_articles.FAM_COD_5, 
					FAM_LIB_5 = w_classification_articles.FAM_LIB_5,
					FAM_IDE_6 = w_classification_articles.FAM_IDE_6, 
					FAM_COD_6 = w_classification_articles.FAM_COD_6, 
					FAM_LIB_6 = w_classification_articles.FAM_LIB_6
				FROM w_classification_articles 
				WHERE w_classification_articles.DOS_IDE = w_ARTICLE.DOS_IDE AND
					w_classification_articles.FAM_IDE_5 = FAM_IDE AND
					FAM_IDE <> 0 AND
					w_classification_articles.FAM_IDE_6 = 0
				;

				UPDATE w_ARTICLE SET
					FAM_IDE_1 = w_classification_articles.FAM_IDE_1, 
					FAM_COD_1 = w_classification_articles.FAM_COD_1, 
					FAM_LIB_1 = w_classification_articles.FAM_LIB_1,
					FAM_IDE_2 = w_classification_articles.FAM_IDE_2, 
					FAM_COD_2 = w_classification_articles.FAM_COD_2, 
					FAM_LIB_2 = w_classification_articles.FAM_LIB_2,
					FAM_IDE_3 = w_classification_articles.FAM_IDE_3, 
					FAM_COD_3 = w_classification_articles.FAM_COD_3, 
					FAM_LIB_3 = w_classification_articles.FAM_LIB_3,
					FAM_IDE_4 = w_classification_articles.FAM_IDE_4, 
					FAM_COD_4 = w_classification_articles.FAM_COD_4, 
					FAM_LIB_4 = w_classification_articles.FAM_LIB_4,
					FAM_IDE_5 = w_classification_articles.FAM_IDE_5, 
					FAM_COD_5 = w_classification_articles.FAM_COD_5, 
					FAM_LIB_5 = w_classification_articles.FAM_LIB_5,
					FAM_IDE_6 = w_classification_articles.FAM_IDE_6, 
					FAM_COD_6 = w_classification_articles.FAM_COD_6, 
					FAM_LIB_6 = w_classification_articles.FAM_LIB_6
				FROM w_classification_articles 
				WHERE w_classification_articles.DOS_IDE = w_ARTICLE.DOS_IDE AND
					w_classification_articles.FAM_IDE_6 = FAM_IDE AND
					FAM_IDE <> 0 
				;

				UPDATE W_ARTICLE
				SET ART_GEN_COD = '?'||ART_IDE, ART_COD_CTI='?'||ART_IDE
				WHERE ART_GEN_COD = '';


				UPDATE w_ARTICLE
				SET ART_COD_CTI = w_ARTICLE.ART_GEN_COD || '-' || DOS_COD
				FROM
				(
					SELECT ART_GEN_COD, count(*) AS nb
					FROM w_ARTICLE
					WHERE ART_SUPP = 0
					GROUP BY 1
					HAVING count(*) > 1
				) subview
				WHERE w_ARTICLE.ART_GEN_COD = subview.ART_GEN_COD
				
				;
				UPDATE w_ARTICLE
				SET ART_COD_CTI = w_ARTICLE.ART_GEN_COD || '(SUPP)'
				WHERE ART_SUPP = 1
				;
				
	
				UPDATE W_ARTICLE
				SET ATC_COD = 
					CASE
						WHEN FAM_IDE_6 <> 0 THEN FAM_COD_6
						WHEN FAM_IDE_5 <> 0 THEN FAM_COD_5
						WHEN FAM_IDE_4 <> 0 THEN FAM_COD_4
						WHEN FAM_IDE_3 <> 0 THEN FAM_COD_3
						WHEN FAM_IDE_2 <> 0 THEN FAM_COD_2
						ELSE '' END
				WHERE FAM_COD_1 = 'ATC' ;
				
				-- ATC
				
				INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
				SELECT UPPER(FAM_COD_6), UPPER(MAX(FAM_COD_6)), MAX(FAM_LIB_6), MAX(FAM_LIB_6) 
				FROM w_classification_articles
				LEFT JOIN eco.t_classification_atc ON code_original = UPPER(FAM_COD_6)
				WHERE FAM_COD_1 = 'ATC' AND
					FAM_COD_6 <> '' AND
					code_original IS NULL 
				GROUP BY 1
				ORDER BY 1;

				INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
				SELECT UPPER(FAM_COD_5), UPPER(MAX(FAM_COD_5)), MAX(FAM_LIB_5), MAX(FAM_LIB_5) 
				FROM w_classification_articles
				LEFT JOIN eco.t_classification_atc ON code_original = UPPER(FAM_COD_5)
				WHERE FAM_COD_1 = 'ATC' AND
					FAM_COD_5 <> '' AND
					code_original IS NULL 
				GROUP BY 1
				ORDER BY 1;

				INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
				SELECT UPPER(FAM_COD_4), UPPER(MAX(FAM_COD_4)), MAX(FAM_LIB_4), MAX(FAM_LIB_4) 
				FROM w_classification_articles
				LEFT JOIN eco.t_classification_atc ON code_original = UPPER(FAM_COD_4)
				WHERE FAM_COD_1 = 'ATC' AND
					FAM_COD_4 <> '' AND
					code_original IS NULL 
				GROUP BY 1
				ORDER BY 1;

				INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
				SELECT UPPER(FAM_COD_3), UPPER(MAX(FAM_COD_3)), MAX(FAM_LIB_3), MAX(FAM_LIB_3) 
				FROM w_classification_articles
				LEFT JOIN eco.t_classification_atc ON code_original = UPPER(FAM_COD_3)
				WHERE FAM_COD_1 = 'ATC' AND
					FAM_COD_3 <> '' AND
					code_original IS NULL 
				GROUP BY 1
				ORDER BY 1;

				INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
				SELECT UPPER(FAM_COD_2), UPPER(MAX(FAM_COD_2)), MAX(FAM_LIB_2), MAX(FAM_LIB_2) 
				FROM w_classification_articles
				LEFT JOIN eco.t_classification_atc ON code_original = UPPER(FAM_COD_2)
				WHERE FAM_COD_1 = 'ATC' AND
					FAM_COD_2 <> '' AND
					code_original IS NULL 
				GROUP BY 1
				ORDER BY 1;

				
				-- Standard ATC
				SELECT eco.cti_reorganize_classification_atc();
			
			
				-- Comptes
				INSERT INTO eco.t_compte(code, texte, texte_court, code_original)
				SELECT PLC_COMPTE, PLC_LIBELLE, LEFT(PLC_LIBELLE,50), PLC_COMPTE
				FROM prod_cegi.CMPLANCPT
				WHERE PLC_COMPTE IN (SELECT PLC_COMPTE FROM prod_cegi.SXART_GEN) AND
					PLC_COMPTE NOT IN (SELECT code_original FROM eco.t_compte WHERE code_original IS NOT NULL);


				INSERT INTO eco.t_compte(code, texte, texte_court, code_original)
				SELECT PLC_COMPTE, PLC_LIBELLE, LEFT(PLC_LIBELLE,50), PLC_COMPTE
				FROM prod_cegi.CMPLANCPT
				WHERE PLC_COMPTE IN (SELECT PLC_COMPTE FROM prod_cegi.SXARTICLE_COMPTE) AND
					PLC_COMPTE NOT IN (SELECT code_original FROM eco.t_compte WHERE code_original IS NOT NULL);
				
				UPDATE eco.t_compte
				SET
					texte = PLC_LIBELLE,
					texte_court = LEFT(PLC_LIBELLE,50)
				FROM prod_cegi.CMPLANCPT
				WHERE PLC_COMPTE = code_original
					AND (
						texte != PLC_LIBELLE
						OR texte_court != LEFT(PLC_LIBELLE,50)
						);
					
				UPDATE w_ARTICLE
				SET compte_id = t_compte.oid
				FROM eco.t_compte
				WHERE t_compte.code_original = PLC_COMPTE;
				
				-- tva
				UPDATE w_ARTICLE
				SET taux_tva_en_cours = subview.TVV_TAUX
				FROM
				(
					SELECT TVA_CODE, (MAX(ARRAY[COALESCE(TVV_DATFIN,'20991231')::text,TVV_TAUX::text]))[2]::numeric AS TVV_TAUX
					FROM prod_cegi.CMVALEURTVA 
					GROUP BY 1
				) subview
				WHERE TVA_CODE_ARTICLE = TVA_CODE AND
					taux_tva_en_cours <> subview.TVV_TAUX
				;
					
				-- unités
				DROP TABLE IF EXISTS w_SXFORME;
				CREATE TEMP TABLE w_SXFORME AS
				SELECT 
					(SXFORME.DOS_IDE::text||'-'::text||FORME_IDE::text) AS FORME_IDE_CTI,
					SXFORME.DOS_IDE,
					DOS_COD,
					FORME_IDE,
					FORME_COD::text,
					FORME_LIB,
					0::bigint AS unite_id
				FROM prod_cegi.SXFORME 
					JOIN prod_cegi.SXDOSSIER ON SXDOSSIER.DOS_IDE = SXFORME.DOS_IDE 
						AND (ETAB_ID::text = ANY (string_to_array('[ETAB_ID]'::text,',')) OR '[ETAB_ID]' = '' OR '[ETAB_ID]' = '-1')
				ORDER BY FORME_IDE, FORME_COD;

				UPDATE w_SXFORME
				SET FORME_COD = w_SXFORME.FORME_COD || '-' || DOS_COD
				FROM
					(
					SELECT
						FORME_COD
					FROM w_SXFORME
					GROUP BY 1
					HAVING count(DISTINCT FORME_LIB) > 1
					) subview
				WHERE w_SXFORME.FORME_COD = subview.FORME_COD;

				INSERT INTO eco.t_unites(code_original, code, texte, texte_court)
				SELECT FORME_IDE_CTI, FORME_COD, MAX(FORME_LIB), MAX(FORME_LIB) 
				FROM w_SXFORME
					LEFT JOIN eco.t_unites ON FORME_IDE_CTI = code_original
				WHERE FORME_COD <> '' AND 
					code_original IS NULL
				GROUP BY 1,2;

				UPDATE w_SXFORME
				SET unite_id = t_unites.oid
				FROM eco.t_unites
				WHERE w_SXFORME.FORME_IDE_CTI = code_original;

				UPDATE w_SXFORME
				SET unite_id = subview.unite_id
				FROM
					(
					SELECT 
						FORME_COD,
						FORME_LIB,
						MIN(unite_id) AS unite_id
					FROM w_SXFORME
					GROUP BY 1,2
					) subview
				WHERE w_SXFORME.FORME_COD = subview.FORME_COD AND
					w_SXFORME.FORME_LIB = subview.FORME_LIB AND
					w_SXFORME.unite_id <> subview.unite_id;



							
				
				UPDATE w_ARTICLE
				SET unite_id = t_unites.oid
				FROM eco.t_unites
				WHERE t_unites.code_original = FORME_IDE_CTI;
					
				
				-- Familles d'articles
				DROP TABLE IF EXISTS w_SX_ARTFAM;
				CREATE TEMP TABLE w_SX_ARTFAM AS
				SELECT 
					DOS_IDE::text||'-'||FAM_IDE_1||'-'||FAM_IDE_2 AS FAM_IDE_CTI,
					DOS_IDE,
					DOS_COD,
					FAM_IDE_1,
					FAM_IDE_2,
					FAM_COD_1||CASE WHEN FAM_IDE_2 != 0 THEN '-'||FAM_COD_2::text ELSE '' END AS FAM_COD_2,
					FAM_LIB_1||CASE WHEN FAM_IDE_2 != 0 THEN ' - '||FAM_LIB_2 ELSE '' END AS FAM_LIB_2,
					0::bigint AS famille_id
				FROM w_ARTICLE
				--WHERE FAM_IDE_2 <> 0
				GROUP BY 1,2,3,4,5,6,7
				ORDER BY DOS_IDE, FAM_IDE_2;


				DROP TABLE IF EXISTS w_SX_ARTFAM_doubles;
				CREATE TEMP TABLE w_SX_ARTFAM_doubles AS
				SELECT
					FAM_COD_2
				FROM w_SX_ARTFAM
				GROUP BY 1
				HAVING count(DISTINCT FAM_LIB_2) > 1;
				
				UPDATE w_SX_ARTFAM
				SET FAM_COD_2 = w_SX_ARTFAM.FAM_COD_2 || '-' || no
				FROM 
					(
					SELECT
						w_SX_ARTFAM.FAM_COD_2, MAX(FAM_IDE_CTI) AS FAM_IDE_CTI, count(*) AS no
					FROM w_SX_ARTFAM
					JOIN w_SX_ARTFAM_doubles ON w_SX_ARTFAM_doubles.FAM_COD_2 = w_SX_ARTFAM.FAM_COD_2
					GROUP BY 1
					HAVING count(*) > 1
					) subview
				WHERE w_SX_ARTFAM.FAM_COD_2 = subview.FAM_COD_2 AND
					w_SX_ARTFAM.FAM_IDE_CTI = subview.FAM_IDE_CTI;
				
				INSERT INTO eco.t_familles_articles(code_original, code, texte, texte_court)
				SELECT FAM_IDE_CTI, FAM_COD_2, MAX(FAM_LIB_2), MAX(FAM_LIB_2)
				FROM w_SX_ARTFAM
					LEFT JOIN eco.t_familles_articles ON FAM_IDE_CTI = code_original
				WHERE FAM_IDE_CTI IS NOT NULL AND 
					code_original IS NULL
				GROUP BY 1,2;

				UPDATE w_SX_ARTFAM
				SET famille_id = t_familles_articles.oid
				FROM eco.t_familles_articles
				WHERE w_SX_ARTFAM.FAM_IDE_CTI = code_original;

				UPDATE w_SX_ARTFAM
				SET famille_id = subview.famille_id
				FROM
					(
					SELECT 
						FAM_COD_2,
						FAM_LIB_2,
						MIN(famille_id) AS famille_id
					FROM w_SX_ARTFAM
					GROUP BY 1,2
					) subview
				WHERE w_SX_ARTFAM.FAM_COD_2 = subview.FAM_COD_2 AND
					w_SX_ARTFAM.FAM_LIB_2 = subview.FAM_LIB_2 AND
					w_SX_ARTFAM.famille_id <> subview.famille_id;


					
				-- Sous familles
				DROP TABLE IF EXISTS w_SX_ARTSFA;
				CREATE TEMP TABLE w_SX_ARTSFA AS
				SELECT 
					DOS_IDE::text||'-'||FAM_IDE_1||'-'||FAM_IDE_3 AS FAM_IDE_CTI,
					DOS_IDE,
					DOS_COD,
					FAM_IDE_1,
					FAM_IDE_3,
					FAM_COD_1||'-'||FAM_COD_3::text AS FAM_COD_3,
					FAM_LIB_1||' - '||FAM_LIB_3 AS FAM_LIB_3,
					0::bigint AS sous_famille_id
				FROM w_ARTICLE
				WHERE FAM_IDE_3 <> 0
				GROUP BY 1,2,3,4,5,6,7
				ORDER BY DOS_IDE, FAM_IDE_3;


				DROP TABLE IF EXISTS w_SX_ARTSFA_doubles;
				CREATE TEMP TABLE w_SX_ARTSFA_doubles AS
				SELECT
					FAM_COD_3
				FROM w_SX_ARTSFA
				GROUP BY 1
				HAVING count(DISTINCT FAM_LIB_3) > 1;

				
				UPDATE w_SX_ARTSFA
				SET FAM_COD_3 = w_SX_ARTSFA.FAM_COD_3 || '-' || no
				FROM 
					(
					SELECT
						w_SX_ARTSFA.FAM_COD_3, MAX(FAM_IDE_CTI) AS FAM_IDE_CTI, count(*) AS no
					FROM w_SX_ARTSFA
					JOIN w_SX_ARTSFA_doubles ON w_SX_ARTSFA_doubles.FAM_COD_3 = w_SX_ARTSFA.FAM_COD_3
					GROUP BY 1
					HAVING count(*) > 1
					) subview
				WHERE w_SX_ARTSFA.FAM_COD_3 = subview.FAM_COD_3 AND
					w_SX_ARTSFA.FAM_IDE_CTI = subview.FAM_IDE_CTI;
				
				INSERT INTO eco.t_sous_familles_articles(code_original, code, texte, texte_court)
				SELECT FAM_IDE_CTI, FAM_COD_3, MAX(FAM_LIB_3), MAX(FAM_LIB_3)
				FROM w_SX_ARTSFA
					LEFT JOIN eco.t_sous_familles_articles ON FAM_IDE_CTI = code_original
				WHERE FAM_COD_3 <> '' AND 
					code_original IS NULL
				GROUP BY 1,2;

				UPDATE w_SX_ARTSFA
				SET sous_famille_id = t_sous_familles_articles.oid
				FROM eco.t_sous_familles_articles
				WHERE w_SX_ARTSFA.FAM_IDE_CTI = code_original;

				UPDATE w_SX_ARTSFA
				SET sous_famille_id = subview.sous_famille_id
				FROM
					(
					SELECT 
						FAM_COD_3,
						FAM_LIB_3,
						MIN(sous_famille_id) AS sous_famille_id
					FROM w_SX_ARTSFA
					GROUP BY 1,2
					) subview
				WHERE w_SX_ARTSFA.FAM_COD_3 = subview.FAM_COD_3 AND
					w_SX_ARTSFA.FAM_LIB_3 = subview.FAM_LIB_3 AND
					w_SX_ARTSFA.sous_famille_id <> subview.sous_famille_id;
				
				
				-- Types d'articles
				SELECT base.cti_execute('
					UPDATE	w_ARTICLE
					SET FAM_IDE_1 = TSK_IDE,
						FAM_COD_1 = TSK_COD,
						FAM_LIB_1 = TSK_LIB
				',1)
				WHERE (SELECT count(*) FROM eco.t_divers WHERE code = 'CEGI_STO_TYP' AND valeur = '1') > 0
				;
				
				DROP TABLE IF EXISTS w_SX_ARTTYP;
				CREATE TEMP TABLE w_SX_ARTTYP AS
				SELECT 
					DOS_IDE::text||'-'||FAM_IDE_1 AS FAM_IDE_CTI,
					DOS_IDE,
					DOS_COD,
					FAM_IDE_1,
					FAM_COD_1::text,
					FAM_LIB_1,
					0::bigint AS type_article_id
				FROM w_ARTICLE
				WHERE FAM_IDE_1 <> 0
				GROUP BY 1,2,3,4,5,6
				ORDER BY DOS_IDE, FAM_IDE_1;

				UPDATE w_SX_ARTTYP
				SET FAM_COD_1 = w_SX_ARTTYP.FAM_COD_1 || '-' || DOS_COD
				FROM
					(
					SELECT
						FAM_COD_1
					FROM w_SX_ARTTYP
					GROUP BY 1
					HAVING count(DISTINCT FAM_LIB_1) > 1
					) subview
				WHERE w_SX_ARTTYP.FAM_COD_1 = subview.FAM_COD_1;

				INSERT INTO eco.t_types_articles(code_original, code, texte, texte_court)
				SELECT FAM_IDE_CTI, FAM_COD_1, MAX(FAM_LIB_1), MAX(FAM_LIB_1) 
				FROM w_SX_ARTTYP
					LEFT JOIN eco.t_types_articles ON FAM_IDE_CTI = code_original
				WHERE FAM_COD_1 <> '' AND 
					code_original IS NULL
				GROUP BY 1,2;

				UPDATE w_SX_ARTTYP
				SET type_article_id = t_types_articles.oid
				FROM eco.t_types_articles
				WHERE w_SX_ARTTYP.FAM_IDE_CTI = code_original;

				UPDATE w_SX_ARTTYP
				SET type_article_id = subview.type_article_id
				FROM
					(
					SELECT 
						FAM_COD_1,
						FAM_LIB_1,
						MIN(type_article_id) AS type_article_id
					FROM w_SX_ARTTYP
					GROUP BY 1,2
					) subview
				WHERE w_SX_ARTTYP.FAM_COD_1 = subview.FAM_COD_1 AND
					w_SX_ARTTYP.FAM_LIB_1 = subview.FAM_LIB_1 AND
					w_SX_ARTTYP.type_article_id <> subview.type_article_id;

				-- categories = type de stock cegi
				INSERT INTO eco.t_categories_articles(code_original, code, texte, texte_court)
				SELECT DOS_IDE || '-' || TSK_IDE, TSK_COD, MAX(TSK_LIB), MAX(LEFT(TSK_LIB,50))
				FROM prod_cegi.SXTYPE_STOCK
					LEFT JOIN eco.t_categories_articles ON  DOS_IDE || '-' || TSK_IDE = code_original
				WHERE code_original IS NULL
				GROUP BY 1,2;
				
				UPDATE eco.t_categories_articles
				SET	
					code = TSK_COD,
					texte = TSK_LIB,
					texte_court = LEFT(TSK_LIB,50)
				FROM (
					SELECT 
						DOS_IDE,
						TSK_IDE,
						TSK_COD,
						MAX(TSK_LIB) AS TSK_LIB
					FROM prod_cegi.SXTYPE_STOCK
					GROUP BY 1,2,3
					 ) sub
				WHERE DOS_IDE|| '-' || TSK_COD = code_original
					AND (TSK_COD != code OR 
						TSK_LIB != texte OR
						LEFT(TSK_LIB,50) != texte_court
						)
				;

				-- Articles
				INSERT INTO eco.t_articles(code_original, code, texte, texte_court)
				SELECT ART_IDE_CTI, ART_COD_CTI, ART_GEN_LIB, LEFT(ART_GEN_LIB,50)
				FROM W_ARTICLE
					LEFT JOIN eco.t_articles ON code_original = ART_IDE_CTI
				WHERE code_original IS NULL
				ORDER by 1;
				
				UPDATE eco.t_articles
				SET 
					code = ART_COD_CTI,
					texte = ART_GEN_LIB,
					texte_court = LEFT(ART_GEN_LIB,50),
					gere_en_stock = CASE WHEN ART_GEN_STO NOT IN ('N', 'D') THEN '1' ELSE '0' END
				FROM W_ARTICLE
				WHERE code_original = ART_IDE_CTI
				AND (code IS DISTINCT FROM ART_COD_CTI OR
					texte IS DISTINCT FROM ART_GEN_LIB OR
					texte_court IS DISTINCT FROM LEFT(ART_GEN_LIB,50) OR
					gere_en_stock IS DISTINCT FROM CASE WHEN ART_GEN_STO NOT IN ('N', 'D') THEN '1' ELSE '0' END);
					
					
				UPDATE w_ARTICLE
				SET article_id = t_articles.oid
				FROM eco.t_articles
				WHERE t_articles.code_original = ART_IDE_CTI;


				UPDATE eco.t_articles SET
					type_id = COALESCE(w_SX_ARTTYP.type_article_id,0),
					famille_id = COALESCE(w_SX_ARTFAM.famille_id,0),
					sous_famille_id = COALESCE(w_SX_ARTSFA.sous_famille_id,0),
					categorie_id =  COALESCE(t_categories_articles.oid,0),
					sous_categorie_id = 0,
					classification_atc_id = COALESCE(t_classification_atc.oid,0),
					unite_stockage_id = w_ARTICLE.unite_id,
					unite_distribution_id = w_ARTICLE.unite_id,
					compte_id = w_ARTICLE.compte_id,
					taux_tva_en_cours = w_ARTICLE.taux_tva_en_cours
				FROM w_ARTICLE
					LEFT JOIN w_SX_ARTTYP ON w_ARTICLE.DOS_IDE = w_SX_ARTTYP.DOS_IDE AND w_ARTICLE.FAM_IDE_1 = w_SX_ARTTYP.FAM_IDE_1
					LEFT JOIN w_SX_ARTFAM ON w_ARTICLE.DOS_IDE = w_SX_ARTFAM.DOS_IDE AND w_ARTICLE.FAM_IDE_1 = w_SX_ARTFAM.FAM_IDE_1 AND w_ARTICLE.FAM_IDE_2 = w_SX_ARTFAM.FAM_IDE_2
					LEFT JOIN w_SX_ARTSFA ON w_ARTICLE.DOS_IDE = w_SX_ARTSFA.DOS_IDE AND w_ARTICLE.FAM_IDE_1 = w_SX_ARTSFA.FAM_IDE_1 AND w_ARTICLE.FAM_IDE_3 = w_SX_ARTSFA.FAM_IDE_3
					LEFT JOIN eco.t_classification_atc ON ATC_COD = t_classification_atc.code_original
					LEFT JOIN eco.t_categories_articles ON w_ARTICLE.DOS_IDE || '-' || w_ARTICLE.TSK_IDE = t_categories_articles.code_original
				WHERE 
					t_articles.code_original = ART_IDE_CTI AND
					(t_articles.type_id IS DISTINCT FROM COALESCE(w_SX_ARTTYP.type_article_id,0) OR
					t_articles.famille_id IS DISTINCT FROM COALESCE(w_SX_ARTFAM.famille_id,0) OR
					t_articles.sous_famille_id IS DISTINCT FROM COALESCE(w_SX_ARTSFA.sous_famille_id,0) OR
					t_articles.categorie_id IS DISTINCT FROM COALESCE(t_categories_articles.oid,0) OR
					t_articles.sous_categorie_id IS DISTINCT FROM 0 OR
					t_articles.classification_atc_id IS DISTINCT FROM COALESCE(t_classification_atc.oid,0) OR
					t_articles.unite_stockage_id IS DISTINCT FROM w_ARTICLE.unite_id OR
					t_articles.unite_distribution_id IS DISTINCT FROM w_ARTICLE.unite_id OR
					t_articles.compte_id IS DISTINCT FROM w_ARTICLE.compte_id OR
					t_articles.taux_tva_en_cours IS DISTINCT FROM w_ARTICLE.taux_tva_en_cours
					);

				


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

					INSERT INTO base.t_lpp (code, texte, texte_court)
					SELECT 
						ART_SUP_LPP, MAX(LPP_COURT), LEFT(MAX(LPP_COURT),50) 
					FROM 
						prod_cegi.SXART_SUP 
					JOIN 
						prod_cegi.R_LPP ON ART_SUP_LPP = LPP_ACTE
					WHERE 1=1 AND
						ART_SUP_LPP NOT IN (SELECT code FROM base.t_lpp) AND
						ART_SUP_LPP != ''
					GROUP BY 1
					;
					
					UPDATE eco.t_articles
					SET 
						lpp_id = t_lpp.oid 
					FROM
						prod_cegi.SXART_SUP
						JOIN base.t_lpp ON ART_SUP_LPP = t_lpp.code
						
					WHERE 1=1 AND
						ART_SUP_LPP != '' AND
						DOS_IDE || '-' || ART_IDE = t_articles.code_original AND
						lpp_id != t_lpp.oid
					;

					UPDATE w_ARTICLE
					SET
						lpp_id = t_articles.lpp_id
					FROM eco.t_articles
					WHERE t_articles.code_original = ART_IDE_CTI
					;
						
					INSERT INTO base.t_ucd (code, texte, texte_court)
					SELECT CASE 
						WHEN LENGTH(ART_SUP_UCD) = 13 AND LEFT(ART_SUP_UCD,1) != '0' THEN LEFT(RIGHT(ART_SUP_UCD,8),7)::text
						ELSE split_part(ART_SUP_UCD,'.',1)::text
						 END, MAX(ART_SUP_LIB), LEFT(MAX(ART_SUP_LIB),50)
					FROM prod_cegi.SXART_SUP 
					WHERE CASE 
						WHEN LENGTH(ART_SUP_UCD) = 13 AND LEFT(ART_SUP_UCD,1) != '0' THEN LEFT(RIGHT(ART_SUP_UCD,8),7)::text
						ELSE split_part(ART_SUP_UCD,'.',1)::text
						 END NOT IN (SELECT code FROM base.t_ucd) AND ART_SUP_UCD != ''
						AND NOT ART_SUP_UCD ~ '[A-Z]' AND LENGTH(ART_SUP_UCD) < 14
						GROUP BY 1
					;
						
					UPDATE eco.t_articles
					SET 
						ucd_id = t_ucd.oid 
					FROM
						prod_cegi.SXART_SUP
						JOIN base.t_ucd ON (ART_SUP_UCD != '' AND NOT ART_SUP_UCD ~'[A-Z]' AND LENGTH(ART_SUP_UCD) < 14 AND CASE 
						WHEN LENGTH(ART_SUP_UCD) = 13 AND LEFT(ART_SUP_UCD,1) != '0' THEN LEFT(RIGHT(ART_SUP_UCD,8),7)::text
						ELSE split_part(ART_SUP_UCD,'.',1)::text
						 END = t_ucd.code)
					WHERE 1=1 AND
						DOS_IDE || '-' || ART_IDE = t_articles.code_original AND
						ucd_id != t_ucd.oid
					;
					
					UPDATE eco.t_articles
					SET 
						code_ucd13 = ART_SUP_UCD
					FROM prod_cegi.SXART_SUP
					WHERE DOS_IDE || '-' || ART_IDE = t_articles.code_original AND
						code_ucd13 IS DISTINCT FROM ART_SUP_UCD AND
						ART_SUP_UCD LIKE '34008%'
					;


					UPDATE w_ARTICLE
					SET
						ucd_id = t_articles.ucd_id
					FROM eco.t_articles
					WHERE t_articles.code_original = ART_IDE_CTI
					;
				
				-- Référence article fournisseur
                DROP TABLE IF EXISTS w_ARTICLES_FOURNISSEUR;
                CREATE TEMP TABLE w_ARTICLES_FOURNISSEUR AS
                SELECT 
					SX_CDD.DOS_IDE,
                    SX_CDD.ART_IDE, 
                    SX_CDE.TIE_ID, 
                    SX_CDD_REF, 
                    SX_CDD_ART_LIB, 
                    SX_CDE_DAT AS DATE_COMMANDE, 
                    MAX(COALESCE(ART_SUP_LPP,'')) AS ART_SUP_LPP,
                    MAX(COALESCE(ART_SUP_UCD,'')) AS ART_SUP_UCD,
					MAX(COALESCE(ART_SUP_UCD,'')) AS ART_SUP_UCD13,
					''::text AS SX_CIP_COD,
					''::text AS SX_LAF_REF,
					0::numeric AS SX_LAF_CON,
                    MAX(SX_CDD_PHT)AS SX_CDD_PHT,
                    MAX(CASE WHEN SX_CDD.FORME_IDE <> 0 THEN SX_CDD.DOS_IDE::text||'-'::text||SX_CDD.FORME_IDE::text ELSE '' END) AS unite_code_original,
                    count(*) AS nb,
                    0::bigint AS article_id,
                    0::bigint AS fournisseur_id,
					0::bigint AS fournisseur_distributeur_id,
                    0::bigint AS unite_id,
					0::bigint AS ucd_id,
                    0::bigint AS lpp_id
                FROM prod_cegi.SX_CDD
                JOIN prod_cegi.SX_CDE ON SX_CDD.SX_CDE_IDE =  SX_CDE.SX_CDE_IDE AND SX_CDD.DOS_IDE =  SX_CDE.DOS_IDE
                LEFT JOIN prod_cegi.SXART_SUP ON
					SXART_SUP.ART_IDE = SX_CDD.ART_IDE AND
					SXART_SUP.DOS_IDE = SX_CDD.DOS_IDE
                GROUP BY 1,2,3,4,5,6
                ORDER BY 1,2
                ;
				
				ANALYSE w_ARTICLES_FOURNISSEUR
				;
				
				UPDATE w_ARTICLES_FOURNISSEUR
				SET SX_LAF_REF = SX_LAF.SX_LAF_REF
				FROM 
					(
					SELECT DOS_IDE, ART_IDE, TIE_ID,
					(MAX(ARRAY[SX_LAF_DEB::text, SX_LAF_REF::text]))[2] AS SX_LAF_REF
					FROM prod_cegi.SX_LAF
					WHERE SX_LAF_SUPP = 0
					GROUP BY 1,2,3
					) SX_LAF
				WHERE w_ARTICLES_FOURNISSEUR.DOS_IDE = SX_LAF.DOS_IDE AND
					w_ARTICLES_FOURNISSEUR.ART_IDE = SX_LAF.ART_IDE AND
					w_ARTICLES_FOURNISSEUR.TIE_ID = SX_LAF.TIE_ID
				;
				
				UPDATE w_ARTICLES_FOURNISSEUR
				SET SX_LAF_CON = SX_LAF.SX_LAF_CON
				FROM 
					(
					SELECT DOS_IDE, ART_IDE, TIE_ID,
					(MAX(ARRAY[SX_LAF_DEB::text, SX_LAF_CON::text]))[2]::numeric AS SX_LAF_CON
					FROM prod_cegi.SX_LAF
					WHERE SX_LAF_SUPP = 0
					GROUP BY 1,2,3
					) SX_LAF
				WHERE w_ARTICLES_FOURNISSEUR.DOS_IDE = SX_LAF.DOS_IDE AND
					w_ARTICLES_FOURNISSEUR.ART_IDE = SX_LAF.ART_IDE AND
					w_ARTICLES_FOURNISSEUR.TIE_ID = SX_LAF.TIE_ID
				;

				UPDATE w_ARTICLES_FOURNISSEUR
				SET SX_LAF_CON = SX_LAF.SX_LAF_CON
				FROM 
					(
					SELECT DOS_IDE, ART_IDE, TIE_ID,
					(MAX(ARRAY[SX_LAF_DEB::text, SX_LAF_CON::text]))[2]::numeric AS SX_LAF_CON
					FROM prod_cegi.SX_LAF
					GROUP BY 1,2,3
					) SX_LAF
				WHERE w_ARTICLES_FOURNISSEUR.DOS_IDE = SX_LAF.DOS_IDE AND
					w_ARTICLES_FOURNISSEUR.ART_IDE = SX_LAF.ART_IDE AND
					w_ARTICLES_FOURNISSEUR.TIE_ID = SX_LAF.TIE_ID AND
					w_ARTICLES_FOURNISSEUR.SX_LAF_CON = 0
				;

				UPDATE w_ARTICLES_FOURNISSEUR
				SET SX_LAF_CON = SX_LAF.SX_LAF_CON
				FROM 
					(
					SELECT DOS_IDE, ART_IDE, 
					(MAX(ARRAY[SX_LAF_DEB::text, SX_LAF_CON::text]))[2]::numeric AS SX_LAF_CON
					FROM prod_cegi.SX_LAF
					WHERE SX_LAF_SUPP = 0
					GROUP BY 1,2
					HAVING count(DISTINCT SX_LAF_CON) = 1 
					) SX_LAF
				WHERE w_ARTICLES_FOURNISSEUR.DOS_IDE = SX_LAF.DOS_IDE AND
					w_ARTICLES_FOURNISSEUR.ART_IDE = SX_LAF.ART_IDE AND
					w_ARTICLES_FOURNISSEUR.SX_LAF_CON = 0
				;

				UPDATE w_ARTICLES_FOURNISSEUR
				SET SX_LAF_CON = SX_LAF.SX_LAF_CON
				FROM 
					(
					SELECT DOS_IDE, ART_IDE, 
					(MAX(ARRAY[SX_LAF_DEB::text, SX_LAF_CON::text]))[2]::numeric AS SX_LAF_CON
					FROM prod_cegi.SX_LAF
					WHERE SX_LAF_SUPP = 0
					GROUP BY 1,2
					HAVING count(DISTINCT SX_LAF_CON) > 1 
					) SX_LAF
				WHERE w_ARTICLES_FOURNISSEUR.DOS_IDE = SX_LAF.DOS_IDE AND
					w_ARTICLES_FOURNISSEUR.ART_IDE = SX_LAF.ART_IDE AND
					w_ARTICLES_FOURNISSEUR.SX_LAF_CON = 0
				;
				
				UPDATE w_ARTICLES_FOURNISSEUR
				SET SX_LAF_CON = 1
				WHERE SX_LAF_CON = 0
				;
				
				UPDATE w_ARTICLES_FOURNISSEUR
				SET SX_CIP_COD = subview.SX_CIP_COD
				FROM 
				(
				SELECT DOS_IDE, ART_IDE,  (MAX(ARRAY[(1-SX_CIP_SUPP)::text, SX_CIP_DEB::text,SX_CIP_COD::text]))[3] AS SX_CIP_COD
				FROM prod_cegi.SX_CIP
				GROUP BY 1,2
				) subview
				WHERE w_ARTICLES_FOURNISSEUR.DOS_IDE = subview.DOS_IDE AND
					w_ARTICLES_FOURNISSEUR.ART_IDE = subview.ART_IDE
				;

				
				UPDATE w_ARTICLES_FOURNISSEUR SET
					ART_SUP_UCD13 = CASE WHEN ART_SUP_UCD13 LIKE '34008%' THEN w_ARTICLES_FOURNISSEUR.ART_SUP_UCD13 ELSE '' END,
					ART_SUP_UCD = CASE WHEN ART_SUP_UCD LIKE '34008%' THEN substr(w_ARTICLES_FOURNISSEUR.ART_SUP_UCD,6,7) ELSE w_ARTICLES_FOURNISSEUR.ART_SUP_UCD END
				WHERE 
					(
						ART_SUP_UCD13 IS DISTINCT FROM CASE WHEN ART_SUP_UCD13 LIKE '34008%' THEN w_ARTICLES_FOURNISSEUR.ART_SUP_UCD13 ELSE '' END OR
						ART_SUP_UCD IS DISTINCT FROM CASE WHEN ART_SUP_UCD LIKE '34008%' THEN substr(w_ARTICLES_FOURNISSEUR.ART_SUP_UCD,6,7) ELSE w_ARTICLES_FOURNISSEUR.ART_SUP_UCD END
					)
				;
				
				UPDATE w_ARTICLES_FOURNISSEUR
				SET ucd_id = t_ucd.oid
				FROM base.t_ucd
				WHERE ART_SUP_UCD <> '' AND
					ART_SUP_UCD = t_ucd.code
				;

				UPDATE w_ARTICLES_FOURNISSEUR
				SET lpp_id = t_lpp.oid
				FROM base.t_lpp
				WHERE ART_SUP_LPP <> '' AND
					ART_SUP_LPP = t_lpp.code
				;

                UPDATE w_ARTICLES_FOURNISSEUR
                SET article_id = t_articles.oid
                FROM eco.t_articles
                WHERE DOS_IDE::text||'-'::text||ART_IDE::text = t_articles.code_original
                ;

                UPDATE w_ARTICLES_FOURNISSEUR
                SET fournisseur_id = t_fournisseurs.oid
                FROM eco.t_fournisseurs
                WHERE TIE_ID = t_fournisseurs.code_original
                ;

                UPDATE w_ARTICLES_FOURNISSEUR
                SET unite_id = t_unites.oid
                FROM eco.t_unites
                WHERE unite_code_original = t_unites.code_original AND
					unite_code_original <> ''
                ;
				
                UPDATE eco.t_articles SET
					ref_fournisseur_id = subview.ref_fournisseur_id,
					ref_fournisseur_code = subview.ref_fournisseur_code,
					ref_fournisseur_texte = subview.ref_fournisseur_texte
                FROM
                (
                SELECT article_id,
                    base.cti_group_array3(fournisseur_id) AS ref_fournisseur_id,
                    base.cti_group_array3(subview.ref_fournisseur_code) AS ref_fournisseur_code,
                    base.cti_group_array3(CASE WHEN subview.ref_fournisseur_texte != '' THEN subview.ref_fournisseur_texte ELSE texte END || CASE WHEN subview.ref_fournisseur_code != '' AND subview.ref_fournisseur_code != subview.ref_fournisseur_texte THEN '(' || subview.ref_fournisseur_code || ')' ELSE '' END) AS ref_fournisseur_texte
                FROM 
                (
                    SELECT 
                        article_id,
                        fournisseur_id,
                        (MAX(Array[CASE WHEN SX_CDD_ART_LIB != '' THEN DATE_COMMANDE::text ELSE '1900-01-01' END, SX_CDD_ART_LIB]))[2] AS ref_fournisseur_texte,
                        (MAX(Array[CASE 
				WHEN SX_CDD_REF != '' OR split_part(split_part(SX_CDD_ART_LIB,' REF ',2),' ',1) != '' THEN DATE_COMMANDE::text 
				ELSE '1900-01-01' 
				END, 
				CASE 
				WHEN SX_CDD_REF != '' THEN SX_CDD_REF 
				WHEN ART_SUP_UCD != '' THEN ART_SUP_UCD 
				WHEN ART_SUP_LPP != '' THEN ART_SUP_LPP 
				WHEN SX_CDD_ART_LIB LIKE '% REF%' THEN split_part(split_part(SX_CDD_ART_LIB,' REF ',2),' ',1)  
				ELSE SX_CDD_ART_LIB
				END]))[2] AS ref_fournisseur_code
                    FROM w_ARTICLES_FOURNISSEUR
                    WHERE fournisseur_id <> 0 
                    GROUP BY 1,2
                    ORDER BY 1,2
                ) subview
                JOIN eco.t_articles ON article_id = t_articles.oid
                GROUP BY 1
                ORDER BY 2,1
                ) subview
                WHERE t_articles.oid = subview.article_id AND
                    (
                    t_articles.ref_fournisseur_id IS DISTINCT FROM subview.ref_fournisseur_id OR
                    t_articles.ref_fournisseur_code IS DISTINCT FROM subview.ref_fournisseur_code OR
                    t_articles.ref_fournisseur_texte IS DIStINCT FROM subview.ref_fournisseur_texte
                    )
				;


				DROP TABLE IF EXISTS w_articles_fournisseur_eco;
				CREATE TEMP TABLE w_articles_fournisseur_eco  AS
				SELECT
					DOS_IDE::text||'-'::text||ART_IDE::text||'-'||TIE_ID AS code_original2,
					DOS_IDE::text||'-'::text||ART_IDE::text||'-'||TIE_ID AS code_original,
					MAX(DOS_IDE) AS DOS_IDE,
                    MAX(ART_IDE) AS ART_IDE,
                    MAX(TIE_ID) AS TIE_ID,
					MAX(article_id) AS article_id, 
					MAX(fournisseur_id) AS fournisseur_id,
					(MAX(Array[CASE WHEN SX_CDD_ART_LIB != '' THEN DATE_COMMANDE::text ELSE '1900-01-01' END, SX_CDD_ART_LIB]))[2] AS texte_reference_fournisseur,
					(MAX(Array[CASE 
						WHEN SX_CDD_REF != '' OR split_part(split_part(SX_CDD_ART_LIB,' REF ',2),' ',1) != '' THEN DATE_COMMANDE::text 
						ELSE '1900-01-01' 
						END, 
						CASE
						WHEN SX_LAF_REF != '' THEN SX_LAF_REF 

						WHEN SX_CDD_REF != '' THEN SX_CDD_REF 
						WHEN ART_SUP_UCD13 != '' THEN ART_SUP_UCD13
						WHEN ART_SUP_UCD != '' THEN ART_SUP_UCD 
						WHEN ART_SUP_LPP != '' THEN ART_SUP_LPP 
						WHEN SX_CDD_ART_LIB LIKE '% REF%' THEN split_part(split_part(SX_CDD_ART_LIB,' REF ',2),' ',1)  
						ELSE SX_CDD_ART_LIB
						END]))[2] AS code_reference_fournisseur,
					(MAX(Array[CASE WHEN SX_CIP_COD != '' THEN DATE_COMMANDE::text ELSE '1900-01-01' END, SX_CIP_COD]))[2] AS code_cip,
					(MAX(Array[CASE WHEN ucd_id <> 0 THEN DATE_COMMANDE::text ELSE '1900-01-01' END, ucd_id::text]))[2]::bigint AS ucd_id,
					(MAX(Array[CASE WHEN lpp_id <> 0 THEN DATE_COMMANDE::text ELSE '1900-01-01' END, lpp_id::text]))[2]::bigint AS lpp_id,
					(MAX(Array[CASE WHEN ART_SUP_UCD13 != '' THEN DATE_COMMANDE::text ELSE '1900-01-01' END, ART_SUP_UCD13]))[2] AS code_ucd13,

					''::text AS code_cahpp,
					MAX(unite_id) AS unite_approvisionnement_id,
					MAX(SX_LAF_CON) AS nombre_conditionnement_approvisionnement,
					(MAX(ARRAY[DATE_COMMANDE::text,SX_CDD_PHT::text]))[2]::numeric AS prix_unitaire_en_cours
				FROM w_ARTICLES_FOURNISSEUR
				GROUP BY 1
				;
				
				UPDATE w_ARTICLES_FOURNISSEUR_eco
				SET prix_unitaire_en_cours = SX_LAF_PUN
				FROM (SELECT DOS_IDE, TIE_ID, ART_IDE,
							(MAX(Array[SX_LAF_IDE,SX_LAF_PUN]))[2] AS SX_LAF_PUN
						FROM prod_cegi.SX_LAF
						WHERE SX_LAF_SUPP = 0
							AND (SX_LAF_APP IS NULL OR SX_LAF_APP <= now())
							AND (SX_LAF_MAR_DAR IS NULL OR SX_LAF_MAR_DAR >= now())
						GROUP BY 1,2,3
					) SX_LAF
				WHERE  w_ARTICLES_FOURNISSEUR_eco.DOS_IDE = SX_LAF.DOS_IDE AND
					w_ARTICLES_FOURNISSEUR_eco.ART_IDE = SX_LAF.ART_IDE AND
					w_ARTICLES_FOURNISSEUR_eco.TIE_ID = SX_LAF.TIE_ID AND
					prix_unitaire_en_cours <> SX_LAF_PUN
				;
				
				UPDATE w_ARTICLES_FOURNISSEUR_eco SET
					code_original = w_ARTICLES_FOURNISSEUR_eco.DOS_IDE ||'-'::text|| SX_LAF.SX_LAF_IDE
				FROM prod_cegi.SX_LAF
				WHERE  w_ARTICLES_FOURNISSEUR_eco.DOS_IDE = SX_LAF.DOS_IDE AND
					w_ARTICLES_FOURNISSEUR_eco.ART_IDE = SX_LAF.ART_IDE AND
					w_ARTICLES_FOURNISSEUR_eco.TIE_ID = SX_LAF.TIE_ID
				;

				-- Suppression des doublons éventuels (avant maj)
				DELETE FROM eco.t_article_fournisseur
				USING
				(
				SELECT article_id, 
					fournisseur_id,
					max(oid) AS keep_oid
				FROM eco.t_article_fournisseur
				GROUP BY 1,2
				HAVING count(*) > 1
				) subview
				WHERE t_article_fournisseur.article_id = subview.article_id AND
					t_article_fournisseur.fournisseur_id = subview.fournisseur_id AND
					t_article_fournisseur.oid <> subview.keep_oid
				;

				-- Le code original prend par défaut l'identifiant de CJ_LAF
				UPDATE eco.t_article_fournisseur
				SET code_original = w_ARTICLES_FOURNISSEUR_eco.code_original
				FROM w_ARTICLES_FOURNISSEUR_eco
				WHERE w_ARTICLES_FOURNISSEUR_eco.code_original = t_article_fournisseur.code_original AND
					w_ARTICLES_FOURNISSEUR_eco.code_original2 <> w_ARTICLES_FOURNISSEUR_eco.code_original
				;

				INSERT INTO eco.t_article_fournisseur (
					code_original,
					article_id,
					fournisseur_id,
					code_reference_fournisseur,
					texte_reference_fournisseur,
					code_cip,
					ucd_id,
					code_ucd13,
					lpp_id,
					code_cahpp,
					unite_approvisionnement_id,
					nombre_conditionnement_approvisionnement,
					prix_unitaire_en_cours
					)
				SELECT 
					code_original,
					article_id, 
					fournisseur_id,
					code_reference_fournisseur,
					texte_reference_fournisseur,
					code_cip,
					ucd_id,
					code_ucd13,
					lpp_id,
					code_cahpp,
					unite_approvisionnement_id,
					nombre_conditionnement_approvisionnement,
					prix_unitaire_en_cours
				FROM w_articles_fournisseur_eco
				WHERE w_articles_fournisseur_eco.code_original NOT IN (SELECT t_article_fournisseur.code_original FROM eco.t_article_fournisseur WHERE code_original IS NOT NULL)
				;
				
				-- Suppression des doublons éventuels
				DELETE FROM eco.t_article_fournisseur
				USING
				(
				SELECT article_id, 
					fournisseur_id,
					max(oid) AS keep_oid
				FROM eco.t_article_fournisseur
				GROUP BY 1,2
				HAVING count(*) > 1
				) subview
				WHERE t_article_fournisseur.article_id = subview.article_id AND
					t_article_fournisseur.fournisseur_id = subview.fournisseur_id AND
					t_article_fournisseur.oid <> subview.keep_oid
				;

				UPDATE eco.t_article_fournisseur SET
					article_id = w_articles_fournisseur_eco.article_id,
					fournisseur_id = w_articles_fournisseur_eco.fournisseur_id,
					code_reference_fournisseur = w_articles_fournisseur_eco.code_reference_fournisseur,
					texte_reference_fournisseur = w_articles_fournisseur_eco.texte_reference_fournisseur,
					code_cip = w_articles_fournisseur_eco.code_cip,
					code_ucd13 = w_articles_fournisseur_eco.code_ucd13,
					ucd_id = w_articles_fournisseur_eco.ucd_id,
					lpp_id = w_articles_fournisseur_eco.lpp_id,
					code_cahpp = w_articles_fournisseur_eco.code_cahpp,
					unite_approvisionnement_id = w_articles_fournisseur_eco.unite_approvisionnement_id,
					nombre_conditionnement_approvisionnement = w_articles_fournisseur_eco.nombre_conditionnement_approvisionnement,
					prix_unitaire_en_cours = w_articles_fournisseur_eco.prix_unitaire_en_cours
				FROM w_articles_fournisseur_eco
				WHERE t_article_fournisseur.code_original = w_articles_fournisseur_eco.code_original AND
					(
						t_article_fournisseur.article_id IS DISTINCT FROM w_articles_fournisseur_eco.article_id OR
						t_article_fournisseur.fournisseur_id IS DISTINCT FROM w_articles_fournisseur_eco.fournisseur_id OR
						t_article_fournisseur.code_reference_fournisseur IS DISTINCT FROM w_articles_fournisseur_eco.code_reference_fournisseur OR
						t_article_fournisseur.texte_reference_fournisseur IS DISTINCT FROM w_articles_fournisseur_eco.texte_reference_fournisseur OR
						t_article_fournisseur.code_cip IS DISTINCT FROM w_articles_fournisseur_eco.code_cip OR
						t_article_fournisseur.code_ucd13 IS DISTINCT FROM w_articles_fournisseur_eco.code_ucd13 OR
						t_article_fournisseur.ucd_id IS DISTINCT FROM w_articles_fournisseur_eco.ucd_id OR
						t_article_fournisseur.lpp_id IS DISTINCT FROM w_articles_fournisseur_eco.lpp_id OR
						t_article_fournisseur.code_cahpp IS DISTINCT FROM w_articles_fournisseur_eco.code_cahpp OR
						t_article_fournisseur.unite_approvisionnement_id IS DISTINCT FROM w_articles_fournisseur_eco.unite_approvisionnement_id OR
						t_article_fournisseur.nombre_conditionnement_approvisionnement IS DISTINCT FROM w_articles_fournisseur_eco.nombre_conditionnement_approvisionnement OR
						t_article_fournisseur.prix_unitaire_en_cours IS DISTINCT FROM w_articles_fournisseur_eco.prix_unitaire_en_cours
					)
				;
				
				-- Suppression des doublons éventuels (après maj, cas de SX_LAF qui change)
				DELETE FROM eco.t_article_fournisseur
				USING
				(
				SELECT article_id, 
					fournisseur_id,
					max(oid) AS keep_oid
				FROM eco.t_article_fournisseur
				GROUP BY 1,2
				HAVING count(*) > 1
				) subview
				WHERE t_article_fournisseur.article_id = subview.article_id AND
					t_article_fournisseur.fournisseur_id = subview.fournisseur_id AND
					t_article_fournisseur.oid <> subview.keep_oid
				;
			]]></sqlcmd>
		</NODE>
		
		
		<NODE label="Tables Fournisseurs">
			<sqlcmd><![CDATA[
			
				-- Types de fournisseurs
				
					
				-- Fournisseurs
				INSERT INTO eco.t_fournisseurs(code, texte, texte_court, code_original)
				SELECT TIE_CODE, 
					CASE WHEN COALESCE(TIE_NOM,'') <> '' THEN TIE_NOM ELSE '?'||SX_FOURNISSEUR.TIE_ID END, 
					CASE WHEN COALESCE(TIE_NOM,'') <> '' THEN TIE_NOM ELSE '?'||SX_FOURNISSEUR.TIE_ID END, 
					SX_FOURNISSEUR.TIE_ID
				FROM prod_cegi.SX_FOURNISSEUR
					LEFT JOIN eco.t_fournisseurs ON code_original = SX_FOURNISSEUR.TIE_ID
					LEFT JOIN prod_cegi.CMTIERS ON SX_FOURNISSEUR.TIE_ID = CMTIERS.TIE_ID
				WHERE code_original IS NULL;
				
				-- Fournisseurs qui ne sont que dans la table des tiers
				INSERT INTO eco.t_fournisseurs(code, texte, texte_court, code_original)
				SELECT TIE_CODE, 
					CASE WHEN COALESCE(TIE_NOM,'') <> '' THEN TIE_NOM ELSE '?'||SX_CDE.TIE_ID END, 
					CASE WHEN COALESCE(TIE_NOM,'') <> '' THEN TIE_NOM ELSE '?'||SX_CDE.TIE_ID END, 
					SX_CDE.TIE_ID
				FROM prod_cegi.SX_CDE
				LEFT JOIN prod_cegi.SX_FOURNISSEUR ON SX_CDE.TIE_ID = SX_FOURNISSEUR.TIE_ID
				JOIN prod_cegi.CMTIERS ON SX_CDE.TIE_ID = CMTIERS.TIE_ID
				LEFT JOIN eco.t_fournisseurs ON code_original = SX_CDE.TIE_ID
				WHERE SX_FOURNISSEUR.TIE_ID IS NULL AND
					code_original IS NULL
				GROUP BY 1,2,3,4
				;


				UPDATE eco.t_fournisseurs
				SET code = TIE_CODE,
					texte = CASE WHEN COALESCE(TIE_NOM,'') <> '' THEN TIE_NOM ELSE '?'||CMTIERS.TIE_ID END,
					texte_court = CASE WHEN COALESCE(TIE_NOM,'') <> '' THEN TIE_NOM ELSE '?'||CMTIERS.TIE_ID END
				FROM prod_cegi.CMTIERS 
				WHERE code_original = CMTIERS.TIE_ID
				AND (code IS DISTINCT FROM TIE_CODE OR
					texte IS DISTINCT FROM CASE WHEN COALESCE(TIE_NOM,'') <> '' THEN TIE_NOM ELSE '?'||CMTIERS.TIE_ID END OR
					texte_court IS DISTINCT FROM CASE WHEN COALESCE(TIE_NOM,'') <> '' THEN TIE_NOM ELSE '?'||CMTIERS.TIE_ID END)
				;
				
				UPDATE eco.t_fournisseurs
				SET fournisseur_distributeur_id = COALESCE(t_fournisseurs_distributeur.oid, t_fournisseurs.oid)
				FROM prod_cegi.SX_FOURNISSEUR
					LEFT JOIN eco.t_fournisseurs t_fournisseurs_distributeur ON SX_FOU_DIS_IDE = t_fournisseurs_distributeur.code_original AND SX_FOU_DIS_IDE > 0
				WHERE t_fournisseurs.code_original = SX_FOURNISSEUR.TIE_ID AND
					t_fournisseurs.fournisseur_distributeur_id IS DISTINCT FROM COALESCE(t_fournisseurs_distributeur.oid, t_fournisseurs.oid)
				;
				UPDATE eco.t_fournisseurs
				SET fournisseur_distributeur_id = oid
				WHERE fournisseur_distributeur_id = 0
				;
				
					
				INSERT INTO base.t_codes_postaux(code, texte, texte_court, departement_id)
				SELECT TIES_CODEP, UPPER(MAX(TIES_VILLE)), UPPER(MAX(TIES_VILLE)), MAX(t_departements.oid)
				FROM prod_cegi.SX_FOURNISSEUR
				JOIN prod_cegi.CMTIESERV ON SX_FOURNISSEUR.TIE_ID = CMTIESERV.TIE_ID AND CMTIESERV.TIES_SERV = 0
				LEFT JOIN base.t_codes_postaux ON TIES_CODEP = t_codes_postaux.code
				JOIN base.t_departements ON 
					TIES_CODEP NOT LIKE '97%' AND substr(TIES_CODEP,1,2) = t_departements.code OR
					TIES_CODEP LIKE '97%' AND substr(TIES_CODEP,1,3) = t_departements.code 
				WHERE t_codes_postaux.oid IS NULL AND 
					length(TIES_CODEP) = 5
				GROUP BY 1
				;


				-- Adresse
				UPDATE eco.t_fournisseurs SET
					code = TIE_CODE,
					texte = CASE WHEN COALESCE(TIE_NOM,'') <> '' THEN TIE_NOM ELSE '?'||SX_FOURNISSEUR.TIE_ID END,
					texte_court = CASE WHEN COALESCE(TIE_NOM,'') <> '' THEN TIE_NOM ELSE '?'||SX_FOURNISSEUR.TIE_ID END,
					adresse = COALESCE(trim(TIES_ADR1 || ' ' || TIES_ADR2),''),
					code_postal_id = COALESCE(t_codes_postaux.oid,0),
					ville = COALESCE(TIES_VILLE,''),
					cedex = '',
					telephone = COALESCE(TIES_TEL,''),
					fax = COALESCE(TIES_FAX,''),
					email = COALESCE(TIES_EMAIL,''),
					code_cahpp = '',
					siret = COALESCE(TIE_SIRET,''),
					iban = COALESCE(substr(RIB_IBAN,1,4) || ' ' || substr(RIB_IBAN,5,4) || ' ' || substr(RIB_IBAN,9,4) || ' ' || substr(RIB_IBAN,13,4) || ' ' || substr(RIB_IBAN,17,4) || ' ' || substr(RIB_IBAN,21,4) || ' ' || substr(RIB_IBAN,25,4),''),
					bic = COALESCE(RIB_BIC,''),
					code_comptable = COALESCE(PLX_COMPTE,TIE_CODE)
				FROM prod_cegi.SX_FOURNISSEUR
					LEFT JOIN prod_cegi.CMTIERS ON SX_FOURNISSEUR.TIE_ID = CMTIERS.TIE_ID
					LEFT JOIN prod_cegi.CMPLANAUX ON CMTIERS.PLX_ID = CMPLANAUX.PLX_ID
					LEFT JOIN prod_cegi.CMTIESERV ON SX_FOURNISSEUR.TIE_ID = CMTIESERV.TIE_ID AND CMTIESERV.TIES_SERV = 0
					LEFT JOIN 
						(
						SELECT CMTIERIB.*
						FROM prod_cegi.CMTIERIB
						JOIN
							(
							SELECT TIE_ID,
							(MAX(ARRAY[COALESCE(RIB_DATECLOT,'20991231'::date)::text,RIB_DEF::text, RIB_NUMORD::text]))[3]::numeric AS RIB_NUMORD
							FROM prod_cegi.CMTIERIB
							GROUP BY 1
							) subview ON CMTIERIB.TIE_ID = subview.TIE_ID AND CMTIERIB.RIB_NUMORD = subview.RIB_NUMORD
						) CMTIERIB ON SX_FOURNISSEUR.TIE_ID = CMTIERIB.TIE_ID 
					LEFT JOIN base.t_codes_postaux ON TIES_CODEP = t_codes_postaux.code
				WHERE code_original = SX_FOURNISSEUR.TIE_ID AND
					(
					t_fournisseurs.code IS DISTINCT FROM TIE_CODE OR
					t_fournisseurs.texte IS DISTINCT FROM CASE WHEN COALESCE(TIE_NOM,'') <> '' THEN TIE_NOM ELSE '?'||SX_FOURNISSEUR.TIE_ID END OR
					t_fournisseurs.texte_court IS DISTINCT FROM CASE WHEN COALESCE(TIE_NOM,'') <> '' THEN TIE_NOM ELSE '?'||SX_FOURNISSEUR.TIE_ID END OR
					t_fournisseurs.adresse IS DISTINCT FROM COALESCE(trim(TIES_ADR1 || ' ' || TIES_ADR2),'') OR
					t_fournisseurs.code_postal_id IS DISTINCT FROM COALESCE(t_codes_postaux.oid,0) OR
					t_fournisseurs.ville IS DISTINCT FROM COALESCE(TIES_VILLE,'') OR
					t_fournisseurs.cedex IS DISTINCT FROM '' OR
					t_fournisseurs.telephone IS DISTINCT FROM COALESCE(TIES_TEL,'') OR
					t_fournisseurs.fax IS DISTINCT FROM COALESCE(TIES_FAX,'') OR
					t_fournisseurs.email IS DISTINCT FROM COALESCE(TIES_EMAIL,'') OR
					t_fournisseurs.code_cahpp IS DISTINCT FROM '' OR
					t_fournisseurs.siret IS DISTINCT FROM COALESCE(TIE_SIRET,'') OR
					t_fournisseurs.iban IS DISTINCT FROM COALESCE(substr(RIB_IBAN,1,4) || ' ' || substr(RIB_IBAN,5,4) || ' ' || substr(RIB_IBAN,9,4) || ' ' || substr(RIB_IBAN,13,4) || ' ' || substr(RIB_IBAN,17,4) || ' ' || substr(RIB_IBAN,21,4) || ' ' || substr(RIB_IBAN,25,4),'') OR
					t_fournisseurs.bic IS DISTINCT FROM COALESCE(RIB_BIC,'') OR
					t_fournisseurs.code_comptable IS DISTINCT FROM COALESCE(PLX_COMPTE,TIE_CODE)
					)
				;



				UPDATE eco.t_fournisseurs
				SET type_id = 0
				WHERE type_id IS NULL;	
				
				
				UPDATE eco.t_fournisseurs SET
					type_code = t_types_fournisseurs.code,
					type_texte = t_types_fournisseurs.texte_court,
					type_section_id = t_types_fournisseurs.section_id,
					type_section_code = t_types_fournisseurs.section_code,
					type_section_texte = t_types_fournisseurs.section_texte
				FROM eco.t_types_fournisseurs
				WHERE type_id = t_types_fournisseurs.oid
					AND (
						type_code IS DISTINCT FROM t_types_fournisseurs.code OR
						type_texte IS DISTINCT FROM t_types_fournisseurs.texte_court OR
						type_section_id IS DISTINCT FROM t_types_fournisseurs.section_id OR
						type_section_code IS DISTINCT FROM t_types_fournisseurs.section_code OR
						type_section_texte IS DISTINCT FROM t_types_fournisseurs.section_texte
					);
					
					
					
				
				-- Référence article fournisseur
					

			]]></sqlcmd>
		</NODE>
		
		
		
		<NODE label="Autres tables">
			<sqlcmd><![CDATA[
			
				-- Gestionnaires
				INSERT INTO eco.t_divers (code, texte, valeur, description)
				SELECT
					'CEGI_GEST_SRC',
					'Source du gestionnaire',
					'UTI',
					'UTI=Utilisateur, DOS=Dossier'
				WHERE 'CEGI_GEST_SRC' NOT IN (SELECT code FROM eco.t_divers)
                ;
				
				INSERT INTO eco.t_gestionnaires(code_original, code, texte, texte_court)
				SELECT UTL_ID::text, UTL_CODE, UTL_LIB, UTL_LIB
				FROM prod_cegi.CJ_UTIL 
				JOIN eco.t_divers ON t_divers.code = 'CEGI_GEST_SRC' AND t_divers.valeur <> 'DOS'
				LEFT JOIN eco.t_gestionnaires ON (code_original = UTL_ID::text)
				WHERE code_original IS NULL AND
					UTL_ID IN (
						SELECT SX_CDE_SIG FROM prod_cegi.SX_CDE 
							JOIN prod_cegi.SXDOSSIER ON SXDOSSIER.DOS_IDE = SX_CDE.DOS_IDE
						WHERE SXDOSSIER.ETAB_ID::text = ANY (string_to_array('[ETAB_ID]'::text,',')) OR '[ETAB_ID]' = '' OR '[ETAB_ID]' = '-1')
				;
				
				
                INSERT INTO eco.t_gestionnaires(code_original, code, texte, texte_court)
                SELECT UTL_ID::text, UTL_CODE, UTL_LIB, UTL_LIB
                FROM prod_cegi.CJ_UTIL 
				JOIN eco.t_divers ON t_divers.code = 'CEGI_GEST_SRC' AND t_divers.valeur <> 'DOS'
                LEFT JOIN eco.t_gestionnaires ON (code_original = UTL_ID::text)
                WHERE code_original IS NULL AND
                    UTL_ID IN (SELECT SX_CDE_UTI FROM prod_cegi.SX_CDE WHERE (ETAB_ID = ANY(string_to_array('[ETAB_ID]',',')) OR '[ETAB_ID]' = ''))
				;
					
				INSERT INTO eco.t_gestionnaires(code_original, code, texte, texte_court)
                SELECT DOS_IDE::text, DOS_COD, DOS_LIB, DOS_LIB
                FROM prod_cegi.SXDOSSIER
				JOIN eco.t_divers ON t_divers.code = 'CEGI_GEST_SRC' AND t_divers.valeur = 'DOS'
                LEFT JOIN eco.t_gestionnaires ON (code_original = DOS_IDE::text)
                WHERE code_original IS NULL AND
					(SXDOSSIER.ETAB_ID = ANY(string_to_array('[ETAB_ID]',',')) OR '[ETAB_ID]' = '')
				;
				
				
                UPDATE eco.t_gestionnaires SET
					code = DOS_COD,
					texte = DOS_LIB,
					texte_court = DOS_LIB
				FROM prod_cegi.SXDOSSIER
						JOIN eco.t_divers ON t_divers.code = 'CEGI_GEST_SRC' AND t_divers.valeur = 'DOS'
				WHERE t_gestionnaires.code_original = DOS_IDE::text AND
					(
					t_gestionnaires.code IS DISTINCT FROM DOS_COD OR
					t_gestionnaires.texte IS DISTINCT FROM DOS_LIB OR
					t_gestionnaires.texte_court IS DISTINCT FROM DOS_LIB
					)
				;
				
							
				
				-- Unités fonctionnelles
				INSERT INTO eco.t_unites_fonctionnelles(code_original, code, texte, texte_court)
				SELECT TII_IDE::text, MAX(NVE_LIBC), MAX(NVE_LIBELLE), LEFT(MAX(NVE_LIBELLE),50)
				FROM prod_cegi.SXTIERS_INTERNES 
					JOIN prod_cegi.CJ_NVE ON TII_IDE = NVE_ID AND (ETB_ID = ANY (string_to_array('[ETAB_ID]'::text,',')) OR '[ETAB_ID]' = '' OR '[ETAB_ID]' = '-1')
					LEFT JOIN eco.t_unites_fonctionnelles ON (code_original = TII_IDE::text)
				WHERE code_original IS NULL
				GROUP BY 1;
				
				UPDATE eco.t_unites_fonctionnelles SET
					code = NVE_LIBC,
					texte = NVE_LIBELLE,
					texte_court = LEFT(NVE_LIBELLE,50)
				FROM prod_cegi.SXTIERS_INTERNES 
					JOIN prod_cegi.CJ_NVE ON TII_IDE = NVE_ID AND (ETB_ID = ANY (string_to_array('[ETAB_ID]'::text,',')) OR '[ETAB_ID]' = '' OR '[ETAB_ID]' = '-1')
				WHERE code_original = TII_IDE
				AND (code IS DISTINCT FROM NVE_LIBC OR
					texte IS DISTINCT FROM NVE_LIBELLE OR
					texte_court IS DISTINCT FROM LEFT(NVE_LIBELLE,50));
				
				-- Lieux
				INSERT INTO eco.t_lieux(code_original, code, texte, texte_court)
				SELECT NVE_ID::text, MAX(NVE_LIBC), MAX(NVE_LIBELLE), MAX(NVE_LIBELLE)
				FROM prod_cegi.CJ_NVE
					LEFT JOIN eco.t_lieux ON (code_original = NVE_ID::text)
				WHERE NVE_ID IN (SELECT MVT_STO FROM prod_cegi.SXMOUVEMENT) AND
					code_original IS NULL AND
					(ETB_ID = ANY (string_to_array('[ETAB_ID]'::text,',')) OR '[ETAB_ID]' = '' OR '[ETAB_ID]' = '-1')
				GROUP BY 1;
				
					
				-- Site
				INSERT INTO eco.t_sites(code_original, code, texte, texte_court)
				SELECT DOS_IDE::text, DOS_COD, DOS_LIB, DOS_LIB
				FROM prod_cegi.SXDOSSIER
					LEFT JOIN eco.t_sites ON (code_original = DOS_IDE::text)
				WHERE code_original IS NULL
					AND (ETAB_ID = ANY (string_to_array('[ETAB_ID]'::text,',')) OR '[ETAB_ID]' = '' OR '[ETAB_ID]' = '-1');
				
				
				-- Types mouvements
				
				DROP TABLE IF EXISTS w_types_mouvements;
				CREATE TEMP TABLE w_types_mouvements AS 
				SELECT 
					MVT_CAT::text||MVT_TY1||MVT_TY2||MVT_TY3 AS MVT_TYP,
					CASE MVT_TY1 
						WHEN 'D' THEN 'Distribution'
						WHEN 'E' THEN 'Echange'
						WHEN 'I' THEN 'Inventaire'
						WHEN 'L' THEN 'Livraison'
						WHEN 'M' THEN 'Mise à jour stock'
						WHEN 'R' THEN 'Retour'
						WHEN 'X' THEN 'Destruction'
						ELSE MVT_TY1 END 
					|| ' (' || MVT_CAT::text || ' ' || MVT_TY2 || ' ' || MVT_TY3 || ')' AS MVT_TYP_LIB,
					MVT_CAT::text,
					MVT_TY1,
					MVT_TY2,
					MVT_TY3
				FROM prod_cegi.SXMOUVEMENT
				GROUP BY 1,2,3,4,5,6
					ORDER BY 1;

				INSERT INTO eco.t_types_mouvements(code, texte, texte_court, code_original)
				SELECT MVT_TYP, MVT_TYP_LIB, MVT_TYP_LIB, MVT_TYP
				FROM w_types_mouvements
					LEFT JOIN eco.t_types_mouvements ON code_original = MVT_TYP
				WHERE MVT_TYP<> '' AND t_types_mouvements.oid IS NULL
				GROUP BY 1,2,3,4;
			
			

			]]></sqlcmd>
		</NODE>
		
		
	</NODE>
	<NODE name="PROD" label="RECUPERATION DES DONNEES DE PRODUCTION">
	
		<NODE label="Préparation">
			<sqlcmd><![CDATA[
			
				DROP TABLE IF EXISTS w_SX_CDE;
				CREATE TEMP TABLE w_SX_CDE AS
				SELECT 
					SX_CDE.DOS_IDE,
					SX_CDE_IDE,
					SX_CDE.DOS_IDE::text||'-'::text||SX_CDE_IDE::text AS SX_CDE_IDE_CTI,
					SX_CDE_NUM,
					SX_CDE_DAT,
					substr(SX_CDE_COM,1,255) AS SX_CDE_COM,
					SX_CDE_REF,
					SX_CDE_LIV,
					TIE_ID,
					SX_CDE_UTI,
					SX_CDE_SIG,
					CASE 
					WHEN t_divers.valeur = 'DOS' THEN SX_CDE.DOS_IDE::text
					WHEN SX_CDE_SIG <> 0 THEN SX_CDE_SIG::text
					ELSE SX_CDE_UTI::text END AS gestionnaire_code_original,
					SX_CDE_MNT_HT,
					SX_CDE_MNT_TVA AS SX_CDE_MNT_TTC,
					SX_CDE_MNT_TVA - SX_CDE_MNT_HT AS SX_CDE_MNT_TVA,
					'20991231'::date AS SX_LVE_DAT,
					0::numeric AS SX_CDE_MNL_HT,
					0::numeric AS SX_CDE_MNL_TTC,
					0::numeric AS SX_CDE_MNL_TVA,
					'20991231'::date AS SX_FCE_DAT,
					0::numeric AS SX_CDE_MNF_HT,
					0::numeric AS SX_CDE_MNF_TTC,
					0::numeric AS SX_CDE_MNF_TVA
				FROM prod_cegi.SX_CDE
				JOIN prod_cegi.SXDOSSIER ON SXDOSSIER.DOS_IDE = SX_CDE.DOS_IDE
				JOIN eco.t_divers ON t_divers.code = 'CEGI_GEST_SRC'
				WHERE SX_CDE_DAT >= '[ENV_ECO_ANNEEDEBUT]0101' AND
					SX_CDE_SUPP = 0 AND
					(SXDOSSIER.ETAB_ID = ANY (string_to_array('[ETAB_ID]'::text,',')) OR '[ETAB_ID]' = '' OR '[ETAB_ID]' = '-1')
				;


				DROP TABLE IF EXISTS w_SX_CDD;
				CREATE TEMP TABLE w_SX_CDD AS
				SELECT 
					SX_CDD.DOS_IDE,
					SX_CDD.SX_CDE_IDE,
					SX_CDD.DOS_IDE::text||'-'::text||SX_CDD.SX_CDE_IDE::text AS SX_CDE_IDE_CTI,
					SX_CDD.DOS_IDE::text||'-'::text||SX_CDD.SX_CDD_IDE::text AS SX_CDD_IDE_CTI,
					SX_CDD.SX_CDD_IDE,
					SX_CDD.ART_IDE,
					SX_CDD.DOS_IDE::text||'-'::text||SX_CDD.ART_IDE::text AS ART_IDE_CTI,
					SX_CDD.FORME_IDE,
					SX_CDD.DOS_IDE::text||'-'::text||SX_CDD.FORME_IDE::text AS FORME_IDE_CTI,
					SX_CDD_QTE,
					SX_CDD_QTL,
					SX_CDD_PHT,
					SX_CDD_PTC,
					SX_CDD_SOL,
					SX_CDD_OND,
					round((SX_CDD_PHT * SX_CDD_QTE)::numeric,2) AS SX_CDD_MNT_HT,
					round((SX_CDD_PTC * SX_CDD_QTE)::numeric,2) AS SX_CDD_MNT_TTC,
					round((SX_CDD_PTC * SX_CDD_QTE)::numeric,2) - round((SX_CDD_PHT * SX_CDD_QTE)::numeric,2) AS SX_CDD_MNT_TVA,
					'20991231'::date AS SX_LVE_DAT,
					round((SX_CDD_PHT * SX_CDD_QTL)::numeric,2) AS SX_CDD_MNL_HT,
					round((SX_CDD_PTC * SX_CDD_QTL)::numeric,2) AS SX_CDD_MNL_TTC,
					round((SX_CDD_PTC * SX_CDD_QTL)::numeric,2) - round((SX_CDD_PHT * SX_CDD_QTL)::numeric,2) AS SX_CDD_MNL_TVA,
					'20991231'::date AS SX_FCE_DAT,
					SX_CDD_QTF,
					0::numeric AS SX_CDD_MNF_HT,
					0::numeric AS SX_CDD_MNF_TTC,
					0::numeric AS SX_CDD_MNF_TVA,
					SX_CDE_DAT AS TVA_DATE,
					SX_CDD.TVA_CODE,
					base.cti_division((round((SX_CDD_PTC)::numeric,2)-round((SX_CDD_PHT)::numeric,2)), round((SX_CDD_PHT)::numeric,2)) * 100 AS TVV_TAUX
				FROM prod_cegi.SX_CDD
				JOIN w_SX_CDE ON SX_CDD.DOS_IDE = w_SX_CDE.DOS_IDE AND SX_CDD.SX_CDE_IDE = w_SX_CDE.SX_CDE_IDE
				WHERE SX_CDD_SUPP = 0;	
				
				UPDATE w_SX_CDD
				SET TVV_TAUX = CMVALEURTVA.TVV_TAUX
				FROM prod_cegi.CMVALEURTVA 
				WHERE w_SX_CDD.TVA_CODE = CMVALEURTVA.TVA_CODE AND
					TVA_DATE BETWEEN TVV_DATDEB AND COALESCE(TVV_DATFIN,'20991231') AND
					w_SX_CDD.TVV_TAUX IS DISTINCT FROM CMVALEURTVA.TVV_TAUX
				;
				



				DROP TABLE IF EXISTS w_SX_LVD;
				CREATE TEMP TABLE w_SX_LVD AS
				SELECT 
					w_SX_CDD.DOS_IDE,
					w_SX_CDD.SX_CDD_IDE,
					SUM(COALESCE(SX_LVD_QTE,0)) AS SX_LVD_QTE,
					MAX(COALESCE(SX_LVE.SX_LVE_DAT,'20991231')) AS SX_LVE_DAT,
					base.cti_group_array3(SX_LVD.DOS_IDE::text||'-'::text||SX_LVD.MVT_IDE::text) AS MVT_IDE_array
				FROM w_SX_CDD 
				LEFT JOIN prod_cegi.SX_LVD ON SX_LVD.DOS_IDE = w_SX_CDD.DOS_IDE AND SX_LVD.SX_CDD_IDE = w_SX_CDD.SX_CDD_IDE AND SX_LVD_SUPP = 0
				LEFT JOIN prod_cegi.SX_LVE ON SX_LVD.DOS_IDE = SX_LVE.DOS_IDE AND SX_LVD.SX_LVE_IDE = SX_LVE.SX_LVE_IDE
				GROUP BY 1,2;


				UPDATE prod_cegi.SX_FCD
				SET SX_CDD_IDE = SX_LVD.SX_CDD_IDE
				FROM prod_cegi.SX_LVD
				WHERE SX_LVD.DOS_IDE = SX_FCD.DOS_IDE AND 
					SX_LVD.SX_LVD_IDE = SX_FCD.SX_LVD_IDE AND 
					SX_FCD.SX_CDD_IDE IS DISTINCT FROM SX_LVD.SX_CDD_IDE;



				
				UPDATE w_SX_CDD SET
					SX_CDD_QTL = SX_LVD_QTE,
					SX_LVE_DAT = w_SX_LVD.SX_LVE_DAT,
					SX_CDD_MNL_HT = round((SX_CDD_PHT * SX_LVD_QTE)::numeric,2),
					SX_CDD_MNL_TTC = round((SX_CDD_PTC * SX_LVD_QTE)::numeric,2),
					SX_CDD_MNL_TVA = round((SX_CDD_PTC * SX_LVD_QTE)::numeric,2) - round((SX_CDD_PHT * SX_CDD_QTL)::numeric,2)
				FROM w_SX_LVD
				WHERE w_SX_LVD.DOS_IDE = w_SX_CDD.DOS_IDE AND 
					w_SX_LVD.SX_CDD_IDE = w_SX_CDD.SX_CDD_IDE;

				SELECT base.cti_execute('
				UPDATE w_SX_CDD SET
					SX_CDD_QTE = SX_CDD_QTL,
					SX_CDD_MNT_HT = SX_CDD_MNL_HT,
					SX_CDD_MNT_TTC = SX_CDD_MNL_TTC,
					SX_CDD_MNT_TVA = SX_CDD_MNL_TVA
				WHERE SX_CDD_SOL = 1 AND 
					SX_CDD_OND IS DISTINCT FROM ''D'' AND
					(SX_CDD_QTL IS DISTINCT FROM SX_CDD_QTE OR
					SX_CDD_MNT_HT IS DISTINCT FROM SX_CDD_MNL_HT OR
					SX_CDD_MNT_TTC IS DISTINCT FROM SX_CDD_MNL_TTC OR
					SX_CDD_MNT_TVA IS DISTINCT FROM SX_CDD_MNL_TVA)
				',1);

				DROP TABLE IF EXISTS w_SX_FCD;
				CREATE TEMP TABLE w_SX_FCD AS
				SELECT 
					w_SX_CDD.DOS_IDE,
					w_SX_CDD.SX_CDD_IDE,
					SUM(COALESCE(SX_FCD_QTE,0)) AS SX_FCD_QTE,
					SUM(COALESCE(SX_FCD_MNT,0)) AS SX_FCD_MNT_HT,
					round(SUM(COALESCE(SX_FCD_MNT * CMVALEURTVA.TVV_TAUX / 100.00,0))::numeric,2) AS SX_FCD_MNT_TVA,
					SUM(COALESCE(SX_FCD_MNT,0)) + round(SUM(COALESCE(SX_FCD_MNT * CMVALEURTVA.TVV_TAUX / 100.00,0))::numeric,2) AS SX_FCD_MNT_TTC,
					MAX(COALESCE(SX_FCE.SX_FCE_DAT,'20991231')) AS SX_FCE_DAT
				FROM w_SX_CDD 
				JOIN prod_cegi.SX_FCD ON SX_FCD.DOS_IDE = w_SX_CDD.DOS_IDE AND SX_FCD.SX_CDD_IDE = w_SX_CDD.SX_CDD_IDE AND SX_FCD_SUPP = 0
				JOIN prod_cegi.SX_FCE ON SX_FCD.DOS_IDE = SX_FCE.DOS_IDE AND SX_FCD.SX_FCE_IDE = SX_FCE.SX_FCE_IDE AND SX_FCE_SUPP = 0
				LEFT JOIN prod_cegi.CMVALEURTVA ON SX_FCD.TVA_CODE = CMVALEURTVA.TVA_CODE AND 
					SX_FCE.SX_FCE_DAT BETWEEN TVV_DATDEB AND COALESCE(TVV_DATFIN,'20991231')
				GROUP BY 1,2;



				UPDATE w_SX_CDD SET
					SX_CDD_QTF = SX_FCD_QTE,
					SX_FCE_DAT = w_SX_FCD.SX_FCE_DAT,
					SX_CDD_MNF_HT = SX_FCD_MNT_HT,
					SX_CDD_MNF_TTC = SX_FCD_MNT_TTC,
					SX_CDD_MNF_TVA = SX_FCD_MNT_TVA
				FROM w_SX_FCD
				WHERE w_SX_FCD.DOS_IDE = w_SX_CDD.DOS_IDE AND 
					w_SX_FCD.SX_CDD_IDE = w_SX_CDD.SX_CDD_IDE;

				SELECT base.cti_execute('UPDATE w_SX_CDD SET
					SX_CDD_MNT_HT = SX_CDD_QTE * base.cti_division(SX_FCD_MNT_HT::numeric,SX_FCD_QTE::numeric),
					SX_CDD_MNT_TTC = SX_CDD_QTE * base.cti_division(SX_FCD_MNT_TTC::numeric,SX_FCD_QTE::numeric),
					SX_CDD_MNT_TVA = SX_CDD_QTE * base.cti_division(SX_FCD_MNT_TVA::numeric,SX_FCD_QTE::numeric)
				FROM w_SX_FCD
				WHERE w_SX_FCD.DOS_IDE = w_SX_CDD.DOS_IDE AND 
					w_SX_FCD.SX_CDD_IDE = w_SX_CDD.SX_CDD_IDE AND
					SX_CDD_MNT_HT = 0.,
				',1)
				WHERE (SELECT valeur = 1 FROM eco.t_divers WHERE code = 'CEGI_FORCE_FAC_COM')
				;

				SELECT base.cti_execute('
				UPDATE w_SX_CDD SET
					SX_CDD_MNL_HT = SX_CDD_QTL * base.cti_division(SX_FCD_MNT_HT::numeric,SX_FCD_QTE::numeric),
					SX_CDD_MNL_TTC = SX_CDD_QTL * base.cti_division(SX_FCD_MNT_TTC::numeric,SX_FCD_QTE::numeric),
					SX_CDD_MNL_TVA = SX_CDD_QTL * base.cti_division(SX_FCD_MNT_TVA::numeric,SX_FCD_QTE::numeric)
				FROM w_SX_FCD
				WHERE w_SX_FCD.DOS_IDE = w_SX_CDD.DOS_IDE AND 
					w_SX_FCD.SX_CDD_IDE = w_SX_CDD.SX_CDD_IDE AND
					SX_CDD_MNL_HT = 0',1)
				WHERE (SELECT valeur = 1 FROM eco.t_divers WHERE code = 'CEGI_FORCE_FAC_LIV')
				;


				UPDATE w_SX_CDE SET
					SX_CDE_MNT_HT = subview.SX_CDD_MNT_HT,
					SX_CDE_MNT_TTC = subview.SX_CDD_MNT_TTC,
					SX_CDE_MNT_TVA = subview.SX_CDD_MNT_TVA,
					SX_LVE_DAT = subview.SX_LVE_DAT,
					SX_CDE_MNL_HT = subview.SX_CDD_MNL_HT,
					SX_CDE_MNL_TTC = subview.SX_CDD_MNL_TTC,
					SX_CDE_MNL_TVA = subview.SX_CDD_MNL_TVA,
					SX_FCE_DAT = subview.SX_FCE_DAT,
					SX_CDE_MNF_HT = subview.SX_CDD_MNF_HT,
					SX_CDE_MNF_TTC = subview.SX_CDD_MNF_TTC,
					SX_CDE_MNF_TVA = subview.SX_CDD_MNF_TVA
				FROM 
					(
					SELECT 
						DOS_IDE, 
						SX_CDE_IDE, 
						SUM(SX_CDD_MNT_HT) AS SX_CDD_MNT_HT,
						SUM(SX_CDD_MNT_TTC) AS SX_CDD_MNT_TTC,
						SUM(SX_CDD_MNT_TVA) AS SX_CDD_MNT_TVA,
						MAX(SX_LVE_DAT) AS SX_LVE_DAT,
						SUM(SX_CDD_MNL_HT) AS SX_CDD_MNL_HT,
						SUM(SX_CDD_MNL_TTC) AS SX_CDD_MNL_TTC,
						SUM(SX_CDD_MNL_TVA) AS SX_CDD_MNL_TVA,
						MAX(SX_FCE_DAT) AS SX_FCE_DAT,
						SUM(SX_CDD_MNF_HT) AS SX_CDD_MNF_HT,
						SUM(SX_CDD_MNF_TTC) AS SX_CDD_MNF_TTC,
						SUM(SX_CDD_MNF_TVA) AS SX_CDD_MNF_TVA
					FROM w_SX_CDD
					GROUP BY 1,2
					) subview
				WHERE w_SX_CDE.DOS_IDE = subview.DOS_IDE AND 
					w_SX_CDE.SX_CDE_IDE = subview.SX_CDE_IDE;
					
					
					
				DROP TABLE IF EXISTS w_SX_LAF;
				CREATE TEMP TABLE w_SX_LAF AS
				SELECT 
					SX_LAF.DOS_IDE,
					SX_LAF.ART_IDE,
					SX_LAF.TIE_ID,
					date(SX_LAF_DEB) AS SX_LAF_DEB,
					'20991231'::date AS SX_LAF_FIN,
					MAX(SX_LAF_PUN) AS SX_LAF_PUN,
					MAX(TVA_CODE) AS TVA_CODE
				FROM prod_cegi.SX_LAF
				JOIN prod_cegi.SXARTICLE ON SXARTICLE.ART_IDE = SX_LAF.ART_IDE AND
						SXARTICLE.DOS_IDE = SX_LAF.DOS_IDE
				JOIN prod_cegi.SXART_GEN ON SXART_GEN.ART_GEN_IDE = SXARTICLE.ART_GEN_IDE AND
						SXART_GEN.DOS_IDE = SXARTICLE.DOS_IDE
				GROUP BY 1,2,3,4;


				UPDATE w_SX_LAF
				SET SX_LAF_FIN = subview.SX_LAF_FIN
				FROM
				(
					SELECT 
						w_SX_LAF.DOS_IDE,
						w_SX_LAF.ART_IDE,
						w_SX_LAF.TIE_ID,
						w_SX_LAF.SX_LAF_DEB,
						MIN(date(w_SX_LAF_next.SX_LAF_DEB - interval '1 day')) AS SX_LAF_FIN
					FROM w_SX_LAF
					JOIN w_SX_LAF w_SX_LAF_next ON
						w_SX_LAF.DOS_IDE = w_SX_LAF_next.DOS_IDE AND
						w_SX_LAF.ART_IDE = w_SX_LAF_next.ART_IDE AND
						w_SX_LAF.TIE_ID = w_SX_LAF_next.TIE_ID AND
						w_SX_LAF.SX_LAF_DEB < w_SX_LAF_next.SX_LAF_DEB
					GROUP BY 1,2,3,4
				) subview
				WHERE w_SX_LAF.DOS_IDE = subview.DOS_IDE AND
					w_SX_LAF.ART_IDE = subview.ART_IDE AND
					w_SX_LAF.TIE_ID = subview.TIE_ID AND
					w_SX_LAF.SX_LAF_DEB = subview.SX_LAF_DEB;

				UPDATE w_SX_LAF
				SET SX_LAF_DEB = '00010101'
				FROM
				(
					SELECT 
						DOS_IDE,
						ART_IDE,
						TIE_ID,
						MIN(SX_LAF_DEB) AS SX_LAF_DEB
					FROM w_SX_LAF
					GROUP BY 1,2,3
				)subview
				WHERE w_SX_LAF.DOS_IDE = subview.DOS_IDE AND
					w_SX_LAF.ART_IDE = subview.ART_IDE AND
					w_SX_LAF.TIE_ID = subview.TIE_ID AND
					w_SX_LAF.SX_LAF_DEB = subview.SX_LAF_DEB;


				UPDATE w_SX_LAF
				SET SX_LAF_PUN = subview.SX_LAF_PUN
				FROM
				(
					SELECT SX_FCD.DOS_IDE, SX_FCD.ART_IDE, SX_FCE.TIE_ID, SX_LAF_DEB, 
						(MAX(Array[date(SX_FCE_DAT)::text, (base.cti_division(SX_FCD_MNT::numeric,SX_FCD_QTE::numeric*GREATEST(SX_FCD_CON,1)::numeric))::text]))[2]::numeric AS SX_LAF_PUN
					from prod_cegi.SX_FCD 
						JOIN prod_cegi.SX_FCE ON SX_FCD.DOS_IDE = SX_FCE.DOS_IDE AND SX_FCD.SX_FCE_IDE = SX_FCE.SX_FCE_IDE AND SX_FCE_SUPP = 0
						JOIN w_SX_LAF ON 
							w_SX_LAF.DOS_IDE = SX_FCD.DOS_IDE AND
							w_SX_LAF.ART_IDE = SX_FCD.ART_IDE AND
							w_SX_LAF.TIE_ID = SX_FCE.TIE_ID AND
							date(SX_FCE_DAT) BETWEEN SX_LAF_DEB AND SX_LAF_FIN
					WHERE SX_LAF_PUN = 0 AND SX_FCD_MNT <> 0
					GROUP BY 1,2,3,4
					ORDER BY 1,2,3,4
				) subview
				WHERE w_SX_LAF.DOS_IDE = subview.DOS_IDE AND
					w_SX_LAF.ART_IDE = subview.ART_IDE AND
					w_SX_LAF.TIE_ID = subview.TIE_ID AND
					w_SX_LAF.SX_LAF_DEB = subview.SX_LAF_DEB;

				INSERT INTO w_SX_LAF
					(
					DOS_IDE,
					ART_IDE, 
					TIE_ID, 
					SX_LAF_DEB, 
					SX_LAF_FIN, 
					SX_LAF_PUN,
					TVA_CODE
					)
				SELECT SX_FCD.DOS_IDE, 
					SX_FCD.ART_IDE, 
					SX_FCE.TIE_ID, 
					'00010101'::date AS SX_LAF_DEB, 
					'20991231'::date AS SX_LAF_FIN, 
					(MAX(Array[date(SX_FCE_DAT)::text, (base.cti_division(SX_FCD_MNT::numeric,SX_FCD_QTE::numeric*GREATEST(SX_FCD_CON,1)::numeric))::text]))[2]::numeric AS SX_LAF_PUN,
					SX_FCD.TVA_CODE
				FROM prod_cegi.SX_FCD 
					JOIN prod_cegi.SX_FCE ON SX_FCD.DOS_IDE = SX_FCE.DOS_IDE AND SX_FCD.SX_FCE_IDE = SX_FCE.SX_FCE_IDE AND SX_FCE_SUPP = 0
					JOIN prod_cegi.SXDOSSIER ON SXDOSSIER.DOS_IDE = SX_FCE.DOS_IDE
						AND (SXDOSSIER.ETAB_ID = ANY (string_to_array('[ETAB_ID]'::text,',')) OR '[ETAB_ID]' = '' OR '[ETAB_ID]' = '-1')
					LEFT JOIN w_SX_LAF ON 
						w_SX_LAF.DOS_IDE = SX_FCD.DOS_IDE AND
						w_SX_LAF.ART_IDE = SX_FCD.ART_IDE AND
						w_SX_LAF.TIE_ID = SX_FCE.TIE_ID 
				WHERE SX_FCD_MNT <> 0 AND w_SX_LAF.ART_IDE IS NULL
				GROUP BY 1,2,3,4,7
				ORDER BY 1,2,3,4;

				INSERT INTO w_SX_LAF
					(
					DOS_IDE,
					ART_IDE, 
					TIE_ID, 
					SX_LAF_DEB, 
					SX_LAF_FIN, 
					SX_LAF_PUN,
					TVA_CODE
					)
				SELECT SXMOUVEMENT.DOS_IDE, 
					SXMOUVEMENT.ART_IDE, 
					SXMOUVEMENT.TIE_ID, 
					'00010101'::date AS SX_LAF_DEB, 
					'20991231'::date AS SX_LAF_FIN, 
					0::numeric AS SX_LAF_PUN,
					SXART_GEN.TVA_CODE
				FROM prod_cegi.SXMOUVEMENT
				JOIN prod_cegi.SXARTICLE ON SXARTICLE.ART_IDE = SXMOUVEMENT.ART_IDE AND
						SXARTICLE.DOS_IDE = SXMOUVEMENT.DOS_IDE
				JOIN prod_cegi.SXART_GEN ON SXART_GEN.ART_GEN_IDE = SXARTICLE.ART_GEN_IDE AND
						SXART_GEN.DOS_IDE = SXARTICLE.DOS_IDE
					LEFT JOIN w_SX_LAF ON 
						w_SX_LAF.DOS_IDE = SXMOUVEMENT.DOS_IDE AND
						w_SX_LAF.ART_IDE = SXMOUVEMENT.ART_IDE AND
						w_SX_LAF.TIE_ID = SXMOUVEMENT.TIE_ID 
				WHERE SXMOUVEMENT.TIE_ID <> 0 AND
					SXMOUVEMENT.ART_IDE <> 0 AND
					w_SX_LAF.ART_IDE IS NULL AND
					MVT_STO IN (SELECT NVE_ID FROM prod_cegi.CJ_NVE WHERE ETB_ID::text = ANY (string_to_array('[ETAB_ID]'::text,',')) OR '[ETAB_ID]' = '' OR '[ETAB_ID]' = '-1')
				GROUP BY 1,2,3,4,7
				ORDER BY 1,2,3,4;

				UPDATE w_SX_LAF
				SET SX_LAF_PUN = subview.SX_LAF_PUN
				FROM
				(
					SELECT SX_FCD.DOS_IDE, SX_FCD.ART_IDE, w_SX_LAF.TIE_ID, SX_LAF_DEB, 
						(MAX(Array[date(SX_FCE_DAT)::text, (base.cti_division(SX_FCD_MNT::numeric,SX_FCD_QTE::numeric*GREATEST(SX_FCD_CON,1)::numeric))::text]))[2]::numeric AS SX_LAF_PUN
					FROM prod_cegi.SX_FCD 
						JOIN prod_cegi.SX_FCE ON SX_FCD.DOS_IDE = SX_FCE.DOS_IDE AND SX_FCD.SX_FCE_IDE = SX_FCE.SX_FCE_IDE AND SX_FCE_SUPP = 0
						JOIN w_SX_LAF ON 
							w_SX_LAF.DOS_IDE = SX_FCD.DOS_IDE AND
							w_SX_LAF.ART_IDE = SX_FCD.ART_IDE AND
							date(SX_FCE_DAT) BETWEEN SX_LAF_DEB AND SX_LAF_FIN
					WHERE SX_LAF_PUN = 0 AND SX_FCD_MNT <> 0
					GROUP BY 1,2,3,4
					ORDER BY 1,2,3,4
				) subview
				WHERE w_SX_LAF.DOS_IDE = subview.DOS_IDE AND
					w_SX_LAF.ART_IDE = subview.ART_IDE AND
					w_SX_LAF.TIE_ID = subview.TIE_ID AND
					w_SX_LAF.SX_LAF_DEB = subview.SX_LAF_DEB AND
					w_SX_LAF.SX_LAF_PUN = 0;


				
				
				

				DROP TABLE IF EXISTS w_SXMOUVEMENT;
				CREATE TEMP TABLE w_SXMOUVEMENT AS
				SELECT
					DOS_IDE,
					MVT_IDE,
					DOS_IDE::text||'-'::text||MVT_IDE::text AS MVT_IDE_CTI,
					date(MVT_DAT) AS MVT_DAT,
					MVT_CAT,
					ART_IDE,
					DOS_IDE::text||'-'::text||ART_IDE::text AS ART_IDE_CTI,
					TIE_ID,
					MVT_TII,
					MVT_STO,
					MVT_UTI,
					CASE 
					WHEN t_divers.valeur = 'DOS' THEN SXMOUVEMENT.DOS_IDE::text
					ELSE MVT_UTI::text END AS gestionnaire_code_original,
					MVT_ENT,
					MVT_QTE,
					MVT_MNT,
					0::numeric AS MVT_MNT_HT,
					0::numeric AS MVT_MNT_TTC,
					MVT_QTI AS MVT_STO_DEB,
					0 AS MVT_STO_FIN,
					MVT_TY1,
					MVT_TY2,
					MVT_TY3,
					MVT_CAT::text || MVT_TY1 || MVT_TY2 || MVT_TY3 AS MVT_TYP,
					MVT_LIB::text,
					MVT_NOM,
					MVT_NOM_TYP,
					LOT_IDE,
					'0'::text AS MVT_FCD,
					'0'::text AS MVT_LVD,
					''::text AS no_sejour,
					''::text AS no_patient,
					0::bigint As commande_id,
					0::numeric AS ligne_commande,
					''::text AS livraison_code_original
				FROM prod_cegi.SXMOUVEMENT
				JOIN eco.t_divers ON t_divers.code = 'CEGI_GEST_SRC'
				WHERE  (MVT_QTE <> 0 OR MVT_MNT <> 0)-- AND MVT_ENT <> 0
					AND MVT_STO IN (SELECT NVE_ID FROM prod_cegi.CJ_NVE WHERE ETB_ID = ANY (string_to_array('[ETAB_ID]'::text,',')) OR '[ETAB_ID]' = '' OR '[ETAB_ID]' = '-1')
					-- todo: enlever les mouevemnts de facture et mettre à jour les quantités avec les quantiré
					ORDER BY MVT_DAT, MVT_IDE
				;
				
				ANALYSE w_SXMOUVEMENT
				;
				
				CREATE INDEX w_SXMOUVEMENT_MVT_IDE ON w_SXMOUVEMENT USING btree (MVT_IDE)
				;
				
				-- Recherche séjour selon type de lien vers séjour
				UPDATE w_SXMOUVEMENT
				SET no_sejour = SEJ_NUMDOS, no_patient = COALESCE(p_sejours.no_patient,'')
				FROM prod_cegi.CJ_SEJOUR
				LEFT JOIN activite.p_sejours ON SEJ_NUMDOS = no_sejour
				WHERE MVT_NOM_TYP = 2 AND
					MVT_NOM = SEJ_ID
				;

				-- Recherche séjour dans libellé
				UPDATE w_SXMOUVEMENT
				SET no_sejour = SEJ_NUMDOS, no_patient = COALESCE(p_sejours.no_patient,'')
				FROM prod_cegi.CJ_SEJOUR
				LEFT JOIN activite.p_sejours ON SEJ_NUMDOS = no_sejour
				WHERE split_part(MVT_LIB,' ',2) = SEJ_NUMDOS
				;
				
				


				DROP TABLE IF EXISTS w_SX_FCD_montant;
				CREATE TEMP TABLE w_SX_FCD_montant AS
				SELECT SX_FCD.DOS_IDE, SX_FCD.MVT_IDE, 
					SUM(SX_FCD_QTE * CASE WHEN SX_FCD_UNI = 0 THEN SX_FCD_CON ELSE 1 END) AS SX_FCD_QTE,
					SUM(SX_FCD_MNT) AS SX_FCD_MNT_HT,
					SUM(COALESCE(SX_FCD_MNT,0)) + round(SUM(COALESCE(SX_FCD_MNT * TVV_TAUX / 100.00,0))::numeric,2) AS SX_FCD_MNT_TTC
				FROM prod_cegi.SX_FCD
				JOIN prod_cegi.SX_FCE ON SX_FCD.DOS_IDE = SX_FCE.DOS_IDE AND SX_FCD.SX_FCE_IDE = SX_FCE.SX_FCE_IDE AND SX_FCE_SUPP = 0
				LEFT JOIN prod_cegi.CMVALEURTVA ON SX_FCD.TVA_CODE = CMVALEURTVA.TVA_CODE AND 
					SX_FCE.SX_FCE_DAT BETWEEN TVV_DATDEB AND COALESCE(TVV_DATFIN,'20991231')
				WHERE SX_FCD_QTE <> 0
				GROUP BY 1,2;

				DROP TABLE IF EXISTS w_SX_FCD_MVT;
				CREATE TEMP TABLE w_SX_FCD_MVT AS
				SELECT 
					SX_FCD.DOS_IDE, 
					SX_FCD.MVT_IDE,
					SX_LVD.MVT_IDE AS MVT_IDE_LVD 
				FROM prod_cegi.SX_FCD
				JOIN prod_cegi.SX_FCE ON SX_FCD.DOS_IDE = SX_FCE.DOS_IDE AND SX_FCD.SX_FCE_IDE = SX_FCE.SX_FCE_IDE AND SX_FCE_SUPP = 0
				JOIN prod_cegi.SX_LVD ON SX_FCD.DOS_IDE = SX_LVD.DOS_IDE AND SX_FCD.SX_CDD_IDE = SX_LVD.SX_CDD_IDE AND SX_LVD_SUPP = 0
				WHERE SX_FCD.MVT_IDE <> SX_LVD.MVT_IDE AND SX_FCD.SX_CDD_IDE <> 0
				GROUP BY 1,2,3;


				INSERT INTO w_SX_FCD_montant (
					DOS_IDE, 
					MVT_IDE, 
					SX_FCD_QTE,
					SX_FCD_MNT_HT,
					SX_FCD_MNT_TTC
					)
				SELECT
					w_SX_FCD_montant.DOS_IDE, 
					w_SX_FCD_MVT.MVT_IDE_LVD,
					w_SX_FCD_montant.SX_FCD_QTE,
					w_SX_FCD_montant.SX_FCD_MNT_HT,
					w_SX_FCD_montant.SX_FCD_MNT_TTC
				FROM w_SX_FCD_montant
				JOIN w_SX_FCD_MVT ON 
					w_SX_FCD_montant.DOS_IDE = w_SX_FCD_MVT.DOS_IDE AND w_SX_FCD_montant.MVT_IDE = w_SX_FCD_MVT.MVT_IDE
				LEFT JOIN w_SX_FCD_montant w_SX_FCD_montant_deja ON
					w_SX_FCD_MVT.DOS_IDE = w_SX_FCD_montant_deja.DOS_IDE AND w_SX_FCD_MVT.MVT_IDE_LVD = w_SX_FCD_montant_deja.MVT_IDE
				WHERE w_SX_FCD_montant_deja.DOS_IDE IS NULL;


				
				DROP TABLE IF EXISTS w_SX_LVD_montant;
				CREATE TEMP TABLE w_SX_LVD_montant AS
				SELECT SX_LVD.DOS_IDE, SX_LVD.MVT_IDE, 
					SUM(SX_LVD_QTE * CASE WHEN SX_LVD_UNI = 0 THEN SX_LVD_CON ELSE 1 END) AS SX_LVD_QTE,
					round(SUM(SX_LVD_PUN * SX_LVD_QTE)::numeric,2) AS SX_LVD_MNT_HT,
					round(SUM(SX_LVD_PUN * SX_LVD_QTE)::numeric,2) + round(SUM(COALESCE((SX_LVD_PUN * SX_LVD_QTE)::numeric * TVV_TAUX / 100.00,0))::numeric,2) AS SX_LVD_MNT_TTC
				FROM prod_cegi.SX_LVD 
				LEFT JOIN prod_cegi.SX_LVE ON SX_LVD.DOS_IDE = SX_LVE.DOS_IDE AND SX_LVD.SX_LVE_IDE = SX_LVE.SX_LVE_IDE AND SX_LVE_SUPP = 0
				LEFT JOIN prod_cegi.CMVALEURTVA ON SX_LVD.TVA_CODE = CMVALEURTVA.TVA_CODE AND 
					SX_LVE.SX_LVE_DAT BETWEEN TVV_DATDEB AND COALESCE(TVV_DATFIN,'20991231')
				WHERE SX_LVD_PUN <> 0
				GROUP BY 1,2;
				
				ANALYSE w_SX_LVD_montant
				;
				
				DROP TABLE IF EXISTS w_SX_LVD_code_original;
				CREATE TEMP TABLE w_SX_LVD_code_original AS
				SELECT SX_LVD.DOS_IDE, SX_LVD.MVT_IDE, 
					SX_LVD.DOS_IDE::text || '-' || (MAX(ARRAY[SX_CDD_IDE,SX_LVD_IDE]))[2]::text AS livraison_code_original
				FROM prod_cegi.SX_LVD
				WHERE MVT_IDE <> 0
				GROUP BY 1,2;
				
				ANALYSE w_SX_LVD_code_original
				;


				

				UPDATE w_SXMOUVEMENT SET
					MVT_FCD = '1',
					MVT_MNT_HT = SX_FCD_MNT_HT,
					MVT_MNT_TTC = SX_FCD_MNT_TTC
				FROM w_SX_FCD_montant
				WHERE w_SXMOUVEMENT.DOS_IDE = w_SX_FCD_montant.DOS_IDE AND
					w_SXMOUVEMENT.MVT_IDE = w_SX_FCD_montant.MVT_IDE AND
					SX_FCD_QTE = w_SXMOUVEMENT.MVT_QTE;

				UPDATE w_SXMOUVEMENT SET
					MVT_FCD = '1',
					MVT_MNT_HT = round((SX_FCD_MNT_HT / SX_FCD_QTE * w_SXMOUVEMENT.MVT_QTE)::numeric,2),
					MVT_MNT_TTC = round((SX_FCD_MNT_TTC / SX_FCD_QTE * w_SXMOUVEMENT.MVT_QTE)::numeric,2)
				FROM w_SX_FCD_montant
				WHERE w_SXMOUVEMENT.DOS_IDE = w_SX_FCD_montant.DOS_IDE AND
					w_SXMOUVEMENT.MVT_IDE = w_SX_FCD_montant.MVT_IDE AND
					SX_FCD_QTE <> w_SXMOUVEMENT.MVT_QTE AND
					SX_FCD_QTE <> 0 AND
					w_SXMOUVEMENT.MVT_FCD <> '1';

				UPDATE w_SXMOUVEMENT SET
					MVT_LVD = '1',
					MVT_MNT_HT = SX_LVD_MNT_HT,
					MVT_MNT_TTC = SX_LVD_MNT_TTC
				FROM w_SX_LVD_montant
				WHERE w_SXMOUVEMENT.DOS_IDE = w_SX_LVD_montant.DOS_IDE AND
					w_SXMOUVEMENT.MVT_IDE = w_SX_LVD_montant.MVT_IDE AND
					SX_LVD_QTE = w_SXMOUVEMENT.MVT_QTE AND
					w_SXMOUVEMENT.MVT_FCD <> '1' AND
					MVT_MNT_HT = 0;

				UPDATE w_SXMOUVEMENT SET
					MVT_LVD = '1',
					MVT_MNT_HT = round((SX_LVD_MNT_HT / SX_LVD_QTE * w_SXMOUVEMENT.MVT_QTE)::numeric,2),
					MVT_MNT_TTC = round((SX_LVD_MNT_TTC / SX_LVD_QTE * w_SXMOUVEMENT.MVT_QTE)::numeric,2)
				FROM w_SX_LVD_montant
				WHERE w_SXMOUVEMENT.DOS_IDE = w_SX_LVD_montant.DOS_IDE AND
					w_SXMOUVEMENT.MVT_IDE = w_SX_LVD_montant.MVT_IDE AND
					SX_LVD_QTE <> w_SXMOUVEMENT.MVT_QTE AND
					SX_LVD_QTE <> 0 AND
					w_SXMOUVEMENT.MVT_FCD <> '1' AND
					w_SXMOUVEMENT.MVT_LVD <> '1' AND
					w_SXMOUVEMENT.MVT_QTE <> 0;
					
				UPDATE w_SXMOUVEMENT SET
					livraison_code_original = w_SX_LVD_code_original.livraison_code_original
				FROM w_SX_LVD_code_original
				WHERE w_SXMOUVEMENT.DOS_IDE = w_SX_LVD_code_original.DOS_IDE AND
					w_SXMOUVEMENT.MVT_IDE = w_SX_LVD_code_original.MVT_IDE
				;

				

				DROP TABLE IF EXISTS w_SXMOUVEMENT_montant;
				CREATE TEMP TABLE w_SXMOUVEMENT_montant AS
				SELECT w_SXMOUVEMENT.DOS_IDE, 
					w_SXMOUVEMENT.MVT_IDE,
					w_SXMOUVEMENT.ART_IDE,
					w_SXMOUVEMENT.MVT_DAT,
					round((SX_LAF_PUN * MVT_QTE)::numeric,2) AS MVT_MNT_HT,
					round((SX_LAF_PUN * MVT_QTE)::numeric,2) + round(COALESCE((SX_LAF_PUN * MVT_QTE)::numeric * TVV_TAUX / 100.00,0)::numeric,2) AS MVT_MNT_TTC
				FROM w_SXMOUVEMENT
					JOIN w_SX_LAF ON
						w_SXMOUVEMENT.DOS_IDE = w_SX_LAF.DOS_IDE AND
						w_SXMOUVEMENT.ART_IDE = w_SX_LAF.ART_IDE AND
						w_SXMOUVEMENT.TIE_ID = w_SX_LAF.TIE_ID AND
						date(w_SXMOUVEMENT.MVT_DAT) BETWEEN w_SX_LAF.SX_LAF_DEB AND w_SX_LAF.SX_LAF_FIN 
					LEFT JOIN prod_cegi.CMVALEURTVA ON w_SX_LAF.TVA_CODE = CMVALEURTVA.TVA_CODE AND 
						w_SXMOUVEMENT.MVT_DAT BETWEEN TVV_DATDEB AND COALESCE(TVV_DATFIN,'20991231')
				WHERE 	MVT_CAT = 1 AND
					SX_LAF_PUN <> 0 AND
					w_SXMOUVEMENT.MVT_FCD <> '1' AND
					MVT_MNT_HT = 0 AND
					w_SXMOUVEMENT.MVT_QTE <> 0;

				-- montant des entrées
				UPDATE w_SXMOUVEMENT SET
					MVT_MNT_HT = w_SXMOUVEMENT_montant.MVT_MNT_HT,
					MVT_MNT_TTC = w_SXMOUVEMENT_montant.MVT_MNT_TTC
				FROM w_SXMOUVEMENT_montant
				WHERE w_SXMOUVEMENT.DOS_IDE = w_SXMOUVEMENT_montant.DOS_IDE AND
					w_SXMOUVEMENT.MVT_IDE = w_SXMOUVEMENT_montant.MVT_IDE
				;

				-- montant à partir du prix calculé et de la dernière TVA connue
				DROP TABLE IF EXISTS w_SXMOUVEMENT_tva;
				CREATE TEMP TABLE w_SXMOUVEMENT_tva AS
				SELECT w_SXMOUVEMENT.DOS_IDE, 
					w_SXMOUVEMENT.MVT_IDE,
					w_SXMOUVEMENT.ART_IDE,
					w_SXMOUVEMENT.MVT_DAT,
					base.cti_division(MVT_MNT_TTC, MVT_MNT_HT) AS TX_TVA_INV,
					COALESCE(lead(MVT_IDE,1) OVER (PARTITION BY w_SXMOUVEMENT.DOS_IDE, w_SXMOUVEMENT.ART_IDE ORDER BY MVT_DAT,MVT_IDE),9999999999999) AS next_mvt
				FROM w_SXMOUVEMENT
				WHERE
					MVT_MNT_HT != 0
					;

				UPDATE w_SXMOUVEMENT SET
					MVT_MNT_HT = base.cti_division(MVT_MNT::numeric,TX_TVA_INV),
					MVT_MNT_TTC = MVT_MNT
				FROM w_SXMOUVEMENT_tva
				WHERE w_SXMOUVEMENT.DOS_IDE = w_SXMOUVEMENT_tva.DOS_IDE AND
					w_SXMOUVEMENT.ART_IDE = w_SXMOUVEMENT_tva.ART_IDE AND
					w_SXMOUVEMENT.MVT_IDE BETWEEN w_SXMOUVEMENT_tva.MVT_IDE AND next_mvt AND
					MVT_MNT_HT = 0
				;

				-- montant des sorties à partir du prix calculé et de la première TVA connue
				UPDATE w_SXMOUVEMENT SET
					MVT_MNT_HT = base.cti_division(MVT_MNT::numeric, TX_TVA_INV),
					MVT_MNT_TTC = MVT_MNT
				FROM 
					(SELECT 
						DOS_IDE,
						ART_IDE,
						min(MVT_IDE) AS MVT_IDE,
						(MIN(ARRAY[MVT_IDE,TX_TVA_INV]))[2] AS TX_TVA_INV
					FROM w_SXMOUVEMENT_tva
					GROUP BY 1,2) w_SXMOUVEMENT_tva
				WHERE w_SXMOUVEMENT.DOS_IDE = w_SXMOUVEMENT_tva.DOS_IDE AND
					w_SXMOUVEMENT.ART_IDE = w_SXMOUVEMENT_tva.ART_IDE AND
					w_SXMOUVEMENT.MVT_IDE < w_SXMOUVEMENT_tva.MVT_IDE AND
					MVT_MNT_HT = 0
				;

				-- montant des sorties à partir du prix calculé et de la première TVA connue
				UPDATE w_SXMOUVEMENT SET
					MVT_MNT_HT = base.cti_division(MVT_MNT::numeric, (100.0+taux_tva_en_cours)/100.0),
					MVT_MNT_TTC = MVT_MNT
				FROM 
					eco.t_articles
				WHERE w_SXMOUVEMENT.DOS_IDE || '-' || ART_IDE = code_original AND
					MVT_MNT_HT = 0 AND
					MVT_MNT != 0 AND
					taux_tva_en_cours != 0
				;

				
				-- autres cas (?)
				UPDATE w_SXMOUVEMENT SET
					MVT_MNT_TTC = MVT_MNT
				WHERE MVT_MNT_HT = 0 AND MVT_MNT != 0
				;

			]]></sqlcmd>
		</NODE>
			
			
		<NODE label="Commandes">
			<sqlcmd><![CDATA[
			
				SELECT base.cti_disable_index('eco', 'i_commandes_1');
				SELECT base.cti_disable_index('eco', 'i_commandes_2');
				SELECT base.cti_disable_index('eco', 'i_commandes_3');
				SELECT base.cti_disable_index('eco', 'i_commandes_4');
				SELECT base.cti_disable_index('eco', 'i_commandes_5');
				SELECT base.cti_disable_index('eco', 'i_commandes_6');
				SELECT base.cti_disable_index('eco', 'i_commandes_7');
				SELECT base.cti_disable_index('eco', 'i_commandes_8');
				

				TRUNCATE eco.p_commandes;
				
				INSERT INTO eco.p_commandes(
					code_original,
					numero,
					date_commande,
					date_engagement,
					objet,
					reference,
					date_livraison_prevue,
					etat_reception,
					date_reception,
					etat_liquidation,
					fournisseur_id,
					fournisseur_distributeur_id,
					gestionnaire_id,
					site_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 
					SX_CDE_IDE_CTI,
					SX_CDE_NUM::text AS numero,
					SX_CDE_DAT AS date_commande,
					date_trunc('month',SX_CDE_DAT)  + interval '1 month' - interval '1 day' AS date_engagement,
					SX_CDE_COM AS objet,
					SX_CDE_REF AS reference,
					'20991231'::date AS date_livraison_prevue,
					CASE WHEN SX_CDE_LIV = 1 THEN 'T' ELSE 'N' END AS etat_reception,
					'20991231'::date AS date_reception,
					CASE WHEN SX_CDE_LIV = 1 THEN 'T' ELSE 'N' END AS etat_liquidation,
					COALESCE(t_fournisseurs.oid,0) AS fournisseur_id,
					COALESCE(t_fournisseurs.fournisseur_distributeur_id, 0) AS fournisseur_distributeur_id,
					COALESCE(t_gestionnaires.oid,0) AS gestionnaire_id,
					COALESCE(t_sites.oid,0),
					0 AS lieu_commande_id,
					0 AS lieu_facturation_id,
					0 AS lieu_livraison_id,
					0::bigint AS unite_fonctionnelle_id,
					SX_CDE_MNT_HT AS montant_commande_ht,
					0::numeric AS montant_commande_remise,
					SX_CDE_MNT_TVA AS montant_commande_tva,
					0::numeric AS montant_commande_port,
					SX_CDE_MNT_TTC AS montant_commande_total,
					SX_CDE_MNF_HT AS montant_liquidation_ht,
					0::numeric AS montant_liquidation_remise,
					SX_CDE_MNF_TVA AS montant_liquidation_tva,
					0::numeric AS montant_liquidation_port,
					SX_CDE_MNF_TTC AS montant_liquidation_total
				FROM w_SX_CDE
					LEFT JOIN eco.t_fournisseurs ON TIE_ID::text = t_fournisseurs.code_original
					LEFT JOIN eco.t_gestionnaires ON w_SX_CDE.gestionnaire_code_original = t_gestionnaires.code_original
					LEFT JOIN eco.t_sites ON w_SX_CDE.DOS_IDE = t_sites.code_original
				;
				
				
				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,
					fournisseur_distributeur_id,
					article_id,
					unite_approvisionnement_id,
					lpp_id,
					ucd_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)
				SELECT
					SX_CDD_IDE_CTI AS code_original,
					p_commandes.oid AS commande_id,
					rank() OVER (PARTITION BY SX_CDE_IDE_CTI ORDER BY SX_CDD_IDE) AS ligne_commande,
					''::text AS texte,
					p_commandes.fournisseur_distributeur_id,
					COALESCE(w_ARTICLE.article_id, 0) AS article_id,
					COALESCE(t_unites.oid,w_ARTICLE.unite_id,0) AS unite_approvisionnement_id,
					COALESCE(lpp_id,0) AS lpp_id,
					COALESCE(ucd_id,0) AS ucd_id,
					COALESCE(w_ARTICLE.compte_id, 0) AS compte_id,
					SX_CDD_QTE AS quantite_approvisionnement,
					SX_CDD_PHT AS prix_unitaire_approvisionnement,
					1 AS multiplicateur_stockage,
					SX_CDD_QTL AS quantite_stockage,
					SX_CDD_PHT AS prix_unitaire_stockage,

					SX_CDD_MNT_HT AS montant_commande_ht,
					0 AS montant_commande_remise,
					SX_CDD_MNT_TVA AS montant_commande_tva,
					0 AS montant_commande_port,
					SX_CDD_MNT_TTC AS montant_commande_ttc,

					SX_CDD_QTL AS quantite_livraison_approvisionnement,
					SX_CDD_QTL AS quantite_livraison_stockage,
					SX_CDD_MNL_HT AS montant_livraison_ht,
					SX_CDD_MNL_TTC AS montant_livraison_ttc,
					CASE WHEN SX_CDD_QTL = SX_CDD_QTE THEN 'T' WHEN SX_CDD_QTL > SX_CDD_QTE THEN 'S' WHEN SX_CDD_QTl = 0 THEN 'N' ELSE 'P' END AS etat_livraison,

					SX_CDD_MNF_HT AS montant_liquidation_ht,
					0::numeric AS montant_liquidation_remise,
					SX_CDD_MNF_TVA AS montant_liquidation_tva,
					0::numeric AS montant_liquidation_port,
					SX_CDD_MNF_TTC AS montant_liquidation_ttc,
					CASE WHEN SX_CDD_SOL::text = 1 THEN 'T' ELSE 'N' END AS etat_liquidation,
					
					TVV_TAUX
				FROM w_SX_CDD
					JOIN eco.p_commandes ON SX_CDE_IDE_CTI = p_commandes.code_original
					LEFT JOIN w_ARTICLE ON w_SX_CDD.ART_IDE_CTI = w_ARTICLE.ART_IDE_CTI 
					LEFT JOIN eco.t_unites ON w_SX_CDD.FORME_IDE_CTI = t_unites.code_original AND t_unites.oid <> 0;
				
				UPDATE eco.p_commandes
				SET date_reception = date_liv
				FROM
				(
					SELECT SX_CDE.DOS_IDE || '-' || SX_CDE.SX_CDE_IDE AS commande, MAX(SX_LVD.SX_LVD_CRE) as date_liv
					FROM prod_cegi.SX_LVD
						JOIN prod_cegi.SX_CDD ON SX_CDD.SX_CDD_IDE = SX_LVD.SX_CDD_IDE AND SX_CDD.DOS_IDE = SX_LVD.DOS_IDE
						JOIN prod_cegi.SX_CDE ON SX_CDE.SX_CDE_IDE = SX_CDD.SX_CDE_IDE AND SX_CDD.DOS_IDE = SX_LVD.DOS_IDE
					GROUP BY 1
				) sub
				WHERE commande = code_original
				;
		
				UPDATE eco.p_lignes_commandes
				SET date_reception = date_liv
				FROM
				(SELECT 
					commande,
					ligne,
					max(date_liv) as date_liv 
				FROM
				(
					SELECT p_commandes.oid AS commande,
						rank() OVER (PARTITION BY SX_CDE.DOS_IDE || '-' || SX_CDE.SX_CDE_IDE ORDER BY SX_CDD.SX_CDD_IDE) As ligne,
						SX_LVD.SX_LVD_CRE as date_liv
					FROM prod_cegi.SX_LVD
						JOIN prod_cegi.SX_CDD ON SX_CDD.SX_CDD_IDE = SX_LVD.SX_CDD_IDE AND SX_CDD.DOS_IDE = SX_LVD.DOS_IDE
						JOIN prod_cegi.SX_CDE ON SX_CDE.SX_CDE_IDE = SX_CDD.SX_CDE_IDE AND SX_CDD.DOS_IDE = SX_LVD.DOS_IDE
						JOIN eco.p_commandes ON SX_CDE.DOS_IDE || '-' || SX_CDE.SX_CDE_IDE = code_original
				) sub
				GROUP BY 1,2
				) sub_grouped
				WHERE commande_id = commande AND ligne = ligne_commande
				;

				SELECT base.cti_enable_index('eco', 'i_ligne_commandes_1');
				SELECT base.cti_enable_index('eco', 'i_ligne_commandes_2');
				SELECT base.cti_enable_index('eco', 'i_ligne_commandes_3');
			
			]]></sqlcmd>
		</NODE>
		<NODE label="Mouvements">
			<sqlcmd><![CDATA[
			
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_1');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_2');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_3');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_4');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_5');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_6');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_7');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_8');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_9');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_10');

				-- DELETE FROM w_SXMOUVEMENT
				-- USING 
				-- prod_cegi.SX_FCD
				-- WHERE 1=1 
				-- 	AND SX_FCD.MVT_IDE = w_SXMOUVEMENT.MVT_IDE 
				-- 	AND SX_FCD.DOS_IDE = w_SXMOUVEMENT.DOS_IDE 
				-- 	AND SX_LVD_IDE = 0
				-- ;	
					
				UPDATE w_SXMOUVEMENT 
				SET commande_id = sub.commande_id,
					ligne_commande = sub.ligne_commande
				FROM
					(SELECT commande_id, ligne_commande, MVT_IDE, DOS_IDE
					FROM prod_cegi.SX_FCD 
					JOIN eco.p_lignes_commandes ON DOS_IDE::text || '-' || SX_CDD_IDE::text = code_original) sub
				WHERE sub.MVT_IDE = w_SXMOUVEMENT.MVT_IDE AND
					sub.DOS_IDE = w_SXMOUVEMENT.DOS_IDE
				;
				
				UPDATE w_SXMOUVEMENT 
				SET commande_id = sub.commande_id,
					ligne_commande = sub.ligne_commande
				FROM
					(SELECT commande_id, ligne_commande, MVT_IDE, DOS_IDE
					FROM prod_cegi.SX_LVD 
					JOIN eco.p_lignes_commandes ON DOS_IDE::text || '-' || SX_CDD_IDE::text = code_original) sub
				WHERE sub.MVT_IDE = w_SXMOUVEMENT.MVT_IDE AND
					sub.DOS_IDE = w_SXMOUVEMENT.DOS_IDE AND 
					w_SXMOUVEMENT.commande_id = 0
				;
				
				ANALYSE w_SXMOUVEMENT
				;
				
				-- Séjour en réappro dépot
				UPDATE w_SXMOUVEMENT SET
					no_sejour = subview.no_sejour,
					no_patient = subview.no_patient
				FROM
				(
					SELECT p_lignes_commandes.commande_id, p_lignes_commandes.ligne_commande, MAX(no_sejour) AS no_sejour, MAX(no_patient) AS no_patient
					FROM prod_cegi.SX_PTH
					JOIN prod_cegi.SX_CDD ON SX_PTH.DOS_IDE = SX_CDD.DOS_IDE AND SX_PTH.SX_CDD_IDE = SX_CDD.SX_CDD_IDE
					JOIN prod_cegi.SX_CDE ON SX_CDD.DOS_IDE = SX_CDE.DOS_IDE AND SX_CDD.SX_CDE_IDE = SX_CDE.SX_CDE_IDE
					JOIN eco.p_lignes_commandes ON (SX_CDD.DOS_IDE::text || '-' || SX_CDD.SX_CDD_IDE::text = code_original)
					JOIN w_SXMOUVEMENT ON 
						SX_PTH.MVT_IDE = w_SXMOUVEMENT.MVT_IDE AND
						sx_PTH.DOS_IDE = w_SXMOUVEMENT.DOS_IDE
					WHERE w_SXMOUVEMENT.no_sejour <> ''
					GROUP BY 1,2
					HAVING count(*) = 1
				) subview
				WHERE w_SXMOUVEMENT.commande_id = subview.commande_id AND
					w_SXMOUVEMENT.ligne_commande = subview.ligne_commande
				;

				UPDATE w_SXMOUVEMENT
				SET	MVT_LIB = 'Commande ' || p_commandes.numero || ' - ' || MVT_LIB
				FROM eco.p_commandes
				WHERE commande_id = p_commandes.oid
				;
				
				-- Pour les dépots, créer un mouvement fictif qui relie à la commande
				DROP TABLE IF EXISTS w_SXMOUVEMENT_NOM1;
				CREATE TEMP TABLE w_SXMOUVEMENT_NOM1 AS
				SELECT MVT_NOM, MAX(no_sejour) AS no_sejour, max(no_patient) AS no_patient
				FROM w_SXMOUVEMENT
				WHERE MVT_NOM_TYP = '1'
				GROUP BY 1;

				ANALYSE w_SXMOUVEMENT_NOM1
				;

				DROP TABLE IF EXISTS w_SX_CDD_depot;
				CREATE TEMP TABLE w_SX_CDD_depot AS
				SELECT SX_CDD.*,
					date(SX_CDE_DAT) AS SX_CDE_DAT,
					SX_CDE_NOM,
					SX_CDE_NUM,
					TIE_ID,
					COALESCE(no_sejour,''::text) AS no_sejour,
					COALESCE(no_patient,''::text) AS no_patient,
					p_lignes_commandes.commande_id,
					p_lignes_commandes.ligne_commande,
					montant_commande_ht,
					montant_commande_ttc,
					montant_commande_tva,
					0::numeric AS MVT_ENT_IDE,
					''::text AS MVT_LIB
				FROM prod_cegi.SX_CDD
				JOIN prod_cegi.SX_CDE ON 
					SX_CDD.DOS_IDE = SX_CDE.DOS_IDE AND 
					SX_CDD.SX_CDE_IDE = SX_CDE.SX_CDE_IDE
				JOIN prod_cegi.SXARTICLE ON
					SX_CDD.DOS_IDE = SXARTICLE.DOS_IDE AND 
					SX_CDD.ART_IDE = SXARTICLE.ART_IDE
				JOIN eco.p_lignes_commandes ON p_lignes_commandes.code_original = (SX_CDD.DOS_IDE::text||'-'||SX_CDD.SX_CDD_IDE)
				LEFT JOIN w_SXMOUVEMENT_NOM1 ON SX_CDE_NOM = MVT_NOM
				JOIN prod_cegi.SXART_GEN ON
					SXARTICLE.DOS_IDE = SXART_GEN.DOS_IDE AND 
					SXARTICLE.ART_GEN_IDE = SXART_GEN.ART_GEN_IDE
				WHERE SX_CDD_SUPP = 0 AND 
					SX_CDE_NOM <> 0 AND 
					SX_CDE_LIV <> 0 AND
					SX_CDD.SX_CDD_SUPP = 0 AND
						(SX_CDD_OND = 'D' OR SX_CDD_OND IS NULL) AND
						SXART_GEN.ART_GEN_STO = 'D' 
				;

				ANALYSE w_SX_CDD_depot
				;
				
				UPDATE w_SX_CDD_depot
				SET SX_CDD_QTL = SX_CDD_QTE
				FROM
				(
				SELECT DOS_IDE, SX_CDE_IDE
				FROM w_SX_CDD_depot
				GROUP BY 1,2
				HAVING SUM(SX_CDD_QTL) = 0
				) subview
				WHERE w_SX_CDD_depot.DOS_IDE = subview.DOS_IDE AND
					w_SX_CDD_depot.SX_CDE_IDE = subview.SX_CDE_IDE
				;

				-- Si le mouvement d'entrée est déjà là, il ne faudra rien faire
				UPDATE w_SX_CDD_depot
				SET MVT_ENT_IDE = w_SXMOUVEMENT.MVT_IDE,
					MVT_LIB = w_SXMOUVEMENT.MVT_LIB
				FROM w_SXMOUVEMENT
				WHERE w_SXMOUVEMENT.commande_id = w_SX_CDD_depot.commande_id AND
					w_SXMOUVEMENT.ligne_commande = w_SX_CDD_depot.ligne_commande AND
					w_SXMOUVEMENT.MVT_ENT = 1
				;

				UPDATE w_SXMOUVEMENT SET
					no_sejour = w_SX_CDD_depot.no_sejour,
					no_patient = w_SX_CDD_depot.no_patient
				FROM w_SX_CDD_depot
				WHERE w_SX_CDD_depot.MVT_ENT_IDE = MVT_ENT and
					w_SXMOUVEMENT.no_sejour = ''
				;
				
				

				-- Lieu par défaut par article
				DROP TABLE IF EXISTS w_SXMOUVEMENT_STO;
				CREATE TEMP TABLE w_SXMOUVEMENT_STO AS 
				SELECT DOS_IDE, ART_IDE, (MAX(ARRAY[nb, MVT_STO]))[2] AS MVT_STO_dft
				FROM
				(
				SELECT DOS_IDE, ART_IDE, MVT_STO, count(*) AS nb
				FROM w_SXMOUVEMENT
				WHERE DOS_IDE::text || '-' || ART_IDE IN (SELECT DOS_IDE::text || '-' || ART_IDE FROM w_SX_CDD_depot)
				GROUP BY 1,2,3
				) subview
				GROUP BY 1,2
				;

				-- Ajout des entrées avec commande et ligne
				INSERT INTO w_SXMOUVEMENT (
					DOS_IDE,
					MVT_IDE,
					MVT_IDE_CTI,
					MVT_DAT,
					MVT_CAT,
					ART_IDE,
					ART_IDE_CTI,
					TIE_ID,
					MVT_TII,
					MVT_STO,
					MVT_UTI,
					MVT_ENT,
					MVT_QTE,
					MVT_MNT,
					MVT_MNT_HT,
					MVT_MNT_TTC,
					MVT_STO_DEB,
					MVT_STO_FIN,
					MVT_TY1,
					MVT_TY2,
					MVT_TY3,
					MVT_TYP,
					MVT_LIB,
					MVT_NOM,
					MVT_NOM_TYP,
					LOT_IDE,
					MVT_FCD,
					MVT_LVD,
					no_sejour,
					no_patient,
					commande_id,
					ligne_commande
				)
				SELECT
					w_SX_CDD_depot.DOS_IDE,
					SX_CDD_IDE AS MVT_IDE,
					w_SX_CDD_depot.DOS_IDE::text || '-CDD' || SX_CDD_IDE || 'E' AS MVT_IDE_CTI,
					SX_CDE_DAT AS MVT_DAT,
					1::numeric AS MVT_CAT,
					w_SX_CDD_depot.ART_IDE,
					w_SX_CDD_depot.DOS_IDE::text||'-'::text||w_SX_CDD_depot.ART_IDE::text AS ART_IDE_CTI,
					TIE_ID,
					0::numeric AS MVT_TII,
					COALESCE(MVT_STO_dft,0) AS MVT_STO,
					SX_CDD_UTI AS MVT_UTI,
					1::numeric AS MVT_ENT,
					SX_CDD_QTL AS MVT_QTE,
					base.cti_division(montant_commande_ht,SX_CDD_QTE::numeric)*SX_CDD_QTL AS MVT_MNT,
					base.cti_division(montant_commande_ht,SX_CDD_QTE::numeric)*SX_CDD_QTL AS MVT_MNT_HT,
					base.cti_division(montant_commande_ttc,SX_CDD_QTE::numeric)*SX_CDD_QTL AS MVT_MNT_TTC,
					0::numeric AS MVT_STO_DEB,
					0::numeric AS MVT_STO_FIN,
					'L'::text AS MVT_TY1,
					'F'::text AS MVT_TY2,
					'S'::text AS MVT_TY3,
					'1LFS'::text AS MVT_TYP,
					'Commande reappro '||SX_CDE_NUM AS MVT_LIB,
					SX_CDE_NOM AS MVT_NOM,
					1::numeric AS MVT_NOM_TYP,
					0::numeric AS LOT_IDE,
					0::numeric AS MVT_FCD,
					0::numeric AS MVT_LVD,
					no_sejour,
					no_patient,
					commande_id,
					ligne_commande
				FROM w_SX_CDD_depot
				LEFT JOIN w_SXMOUVEMENT_STO ON
					w_SX_CDD_depot.DOS_IDE = w_SXMOUVEMENT_STO.DOS_IDE AND
					w_SX_CDD_depot.ART_IDE = w_SXMOUVEMENT_STO.ART_IDE	
				WHERE MVT_ENT_IDE = 0 AND
					SX_CDD_QTL <> 0
				;
				
				
				-- Annulation des entrées sans commande et ligne
				INSERT INTO w_SXMOUVEMENT (
					DOS_IDE,
					MVT_IDE,
					MVT_IDE_CTI,
					MVT_DAT,
					MVT_CAT,
					ART_IDE,
					ART_IDE_CTI,
					TIE_ID,
					MVT_TII,
					MVT_STO,
					MVT_UTI,
					MVT_ENT,
					MVT_QTE,
					MVT_MNT,
					MVT_MNT_HT,
					MVT_MNT_TTC,
					MVT_STO_DEB,
					MVT_STO_FIN,
					MVT_TY1,
					MVT_TY2,
					MVT_TY3,
					MVT_TYP,
					MVT_LIB,
					MVT_NOM,
					MVT_NOM_TYP,
					LOT_IDE,
					MVT_FCD,
					MVT_LVD,
					no_sejour,
					no_patient,
					commande_id,
					ligne_commande
				)
				SELECT
					w_SX_CDD_depot.DOS_IDE,
					SX_CDD_IDE AS MVT_IDE,
					w_SX_CDD_depot.DOS_IDE::text || '-CDD' || SX_CDD_IDE || 'A' AS MVT_IDE_CTI,
					SX_CDE_DAT AS MVT_DAT,
					1::numeric AS MVT_CAT,
					w_SX_CDD_depot.ART_IDE,
					w_SX_CDD_depot.DOS_IDE::text||'-'::text||w_SX_CDD_depot.ART_IDE::text AS ART_IDE_CTI,
					TIE_ID,
					0::numeric AS MVT_TII,
					COALESCE(MVT_STO_dft,0) AS MVT_STO,
					SX_CDD_UTI AS MVT_UTI,
					1::numeric AS MVT_ENT,
					0-SX_CDD_QTL AS MVT_QTE,
					0-base.cti_division(montant_commande_ht,SX_CDD_QTE::numeric)*SX_CDD_QTL AS MVT_MNT,
					0-base.cti_division(montant_commande_ht,SX_CDD_QTE::numeric)*SX_CDD_QTL AS MVT_MNT_HT,
					0-base.cti_division(montant_commande_ttc,SX_CDD_QTE::numeric)*SX_CDD_QTL AS MVT_MNT_TTC,
					0::numeric AS MVT_STO_DEB,
					0::numeric AS MVT_STO_FIN,
					'L'::text AS MVT_TY1,
					'F'::text AS MVT_TY2,
					'S'::text AS MVT_TY3,
					'1LFS'::text AS MVT_TYP,
					'Commande reappro '||SX_CDE_NUM::text || ' (contrepasse)' AS MVT_LIB,
					SX_CDE_NOM AS MVT_NOM,
					1::numeric AS MVT_NOM_TYP,
					0::numeric AS LOT_IDE,
					0::numeric AS MVT_FCD,
					0::numeric AS MVT_LVD,
					no_sejour,
					no_patient,
					0::bigint AS commande_id,
					0::numeric AS ligne_commande
				FROM w_SX_CDD_depot
				LEFT JOIN w_SXMOUVEMENT_STO ON
					w_SX_CDD_depot.DOS_IDE = w_SXMOUVEMENT_STO.DOS_IDE AND
					w_SX_CDD_depot.ART_IDE = w_SXMOUVEMENT_STO.ART_IDE	
				WHERE MVT_ENT_IDE = 0 AND
					SX_CDD_QTL <> 0
				;
				
				TRUNCATE eco.p_mouvements_articles;
			
				INSERT INTO eco.p_mouvements_articles(
					code_original,
					date, 
					sens_mouvement,
					type_mouvement_id, 
					texte, 
					gestionnaire_id, 
					site_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,
					prix_unitaire_calcule,
					stock_quantite_debut,
					stock_quantite_fin,
					date_fin,
					est_premier_jour,
					est_dernier_jour,
					no_sejour,
					no_patient,
					livraison_code_original
					)
				SELECT 
					MVT_IDE_CTI AS code_original,
					MVT_DAT AS date,
					CASE WHEN MVT_ENT = 1 THEN 'E' ELSE 'S' END AS sens_mouvement,
					COALESCE(t_types_mouvements.oid,0) AS type_mouvement_id,
					MVT_LIB AS texte,
					COALESCE(t_gestionnaires.oid,0) AS gestionnaire_id,
					COALESCE(t_sites.oid,0) AS site_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,
					commande_id,
					ligne_commande,
					ucd_id,
					lpp_id,
					base.cti_division(MVT_MNT_HT::numeric ,  MVT_QTE::numeric) AS prix_unitaire,
					CASE WHEN MVT_ENT = 1 THEN MVT_QTE ELSE 0 END AS entree_quantite,
					CASE WHEN MVT_ENT <> 1 THEN MVT_QTE ELSE 0 END AS sortie_quantite,
					CASE WHEN MVT_ENT = 1 THEN MVT_MNT_TTC ELSE 0 END AS entree_montant,
					CASE WHEN MVT_ENT = 1 THEN MVT_MNT_HT ELSE 0 END AS entree_montant_ht,
					CASE WHEN MVT_ENT <> 1 THEN MVT_MNT_TTC ELSE 0 END AS sortie_montant,
					CASE WHEN MVT_ENT <> 1 THEN MVT_MNT_HT ELSE 0 END AS sortie_montant_ht,
					CASE WHEN MVT_ENT <> 1 THEN MVT_MNT_TTC ELSE 0 END AS sortie_montant_original_ttc,
					CASE WHEN MVT_ENT <> 1 THEN MVT_MNT_HT ELSE 0 END AS sortie_montant_original_ht,
					--ajuster PU calc que sur entrées
					CASE WHEN MVT_QTE = 0 AND MVT_MNT != 0 THEN LAG(base.cti_division(MVT_MNT_HT::numeric,MVT_QTE::numeric)) OVER (PARTITION BY ART_IDE_CTI, MVT_STO ORDER BY MVT_DAT,MVT_IDE)
						ELSE base.cti_division(MVT_MNT_HT::numeric,MVT_QTE::numeric) END
					AS prix_unitaire_calcule,
					CASE WHEN MVT_QTE = 0 AND MVT_MNT != 0 THEN LAG(MVT_STO_DEB) OVER (PARTITION BY ART_IDE_CTI, MVT_STO ORDER BY MVT_DAT,MVT_IDE) +
					LAG(MVT_ENT*MVT_QTE) OVER (PARTITION BY ART_IDE_CTI, MVT_STO ORDER BY MVT_DAT,MVT_IDE) ELSE MVT_STO_DEB
					END,
					CASE WHEN MVT_QTE = 0 AND MVT_MNT != 0 THEN LAG(MVT_STO_DEB) OVER (PARTITION BY ART_IDE_CTI, MVT_STO ORDER BY MVT_DAT,MVT_IDE) +
					LAG(MVT_ENT*MVT_QTE) OVER (PARTITION BY ART_IDE_CTI, MVT_STO ORDER BY MVT_DAT,MVT_IDE) ELSE MVT_STO_DEB
					END + MVT_ENT*MVT_QTE,
					CASE WHEN COALESCE(LEAD(MVT_DAT) OVER (PARTITION BY ART_IDE_CTI, MVT_STO ORDER BY MVT_DAT,MVT_IDE),'2100-01-01') = MVT_DAT THEN
					MVT_DAT ELSE COALESCE(LEAD(MVT_DAT) OVER (PARTITION BY ART_IDE_CTI, MVT_STO ORDER BY MVT_DAT,MVT_IDE),'2100-01-01') - interval '1 day'
					END as date_fin,
					CASE  WHEN rank() OVER (PARTITION BY ART_IDE_CTI, MVT_STO,MVT_DAT ORDER BY MVT_DAT,MVT_IDE) = 1 THEN 1
					ELSE 0
					END AS est_premier_jour,
					CASE
					WHEN cume_dist() OVER (PARTITION BY ART_IDE_CTI, MVT_STO,MVT_DAT ORDER BY MVT_DAT,MVT_IDE) = 1 THEN 1
					ELSE 0
					END AS est_dernier_jour,
					w_SXMOUVEMENT.no_sejour,
					w_SXMOUVEMENT.no_patient,
					w_SXMOUVEMENT.livraison_code_original
				FROM w_SXMOUVEMENT
					LEFT JOIN eco.t_types_mouvements ON MVT_TYP = t_types_mouvements.code_original
					LEFT JOIN eco.t_unites_fonctionnelles ON t_unites_fonctionnelles.code_original = MVT_TII
					LEFT JOIN eco.t_articles ON ART_IDE_CTI::text = t_articles.code_original AND t_articles.oid <> 0
					LEFT JOIN eco.t_fournisseurs ON TIE_ID::text = t_fournisseurs.code_original
					LEFT JOIN eco.t_gestionnaires ON w_SXMOUVEMENT.gestionnaire_code_original = t_gestionnaires.code_original
					LEFT JOIN eco.t_lieux ON MVT_STO = t_lieux.code_original
					LEFT JOIN eco.t_sites ON DOS_IDE = t_sites.code_original
					LEFT JOIN eco.t_compte ON t_compte.oid = -1
				WHERE MVT_DAT >= '[ENV_ECO_ANNEEDEBUT]0101' ;

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

				VACUUM ANALYSE eco.p_mouvements_articles;

				--SELECT eco.cti_update_mvt_mnt_stock();
			
			]]></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,
					site_id,
					lieu_id,
					provider_id,
					fournisseur_id,
					stock_quantite_debut,
					stock_valeur_debut,
					stock_valeur_debut_ttc,
					entree_quantite,
					entree_montant,
					pump,
					sortie_quantite,
					sortie_montant,
					stock_quantite_fin,
					stock_valeur_fin,
					stock_valeur_fin_ttc)

				SELECT 
					article_id,
					date(date_trunc('month',date) + interval '1 month' - interval '1 day') AS date_fin,
					date(date_trunc('month',date)) AS date_debut,
					0::bigint AS ucd_id,
					0::bigint AS lpp_id,
					compte_id,
					site_id,
					lieu_id,
					0::bigint AS provider_id,
					p_mouvements_articles.fournisseur_id AS fournisseur_id,
					(MIN(ARRAY[(to_char(date,'YYYMMDD') || code_original),stock_quantite_debut::text]))[2]::numeric,
					(MIN(ARRAY[(to_char(date,'YYYMMDD') || code_original),(stock_quantite_debut*prix_unitaire_calcule)::text]))[2]::numeric,
					(MIN(ARRAY[(to_char(date,'YYYMMDD') || code_original),(stock_quantite_debut*prix_unitaire_calcule*base.cti_division(entree_montant+sortie_montant,entree_montant_ht+sortie_montant_ht))::text]))[2]::numeric,
					SUM(entree_quantite),
					SUM(entree_montant),
					(MAX(ARRAY[(to_char(date,'YYYMMDD') || code_original),prix_unitaire_calcule::text]))[2]::numeric,
					SUM(sortie_quantite),
					SUM(sortie_montant),
					(MAX(ARRAY[(to_char(date,'YYYMMDD') || code_original),stock_quantite_fin::text]))[2]::numeric,
					(MAX(ARRAY[(to_char(date,'YYYMMDD') || code_original),(stock_quantite_fin*prix_unitaire_calcule)::text]))[2]::numeric,
					(MAX(ARRAY[(to_char(date,'YYYMMDD') || code_original),(stock_quantite_fin*prix_unitaire_calcule*base.cti_division(entree_montant+sortie_montant,entree_montant_ht+sortie_montant_ht))::text]))[2]::numeric
				FROM eco.p_mouvements_articles
				WHERE date >= '[ENV_ECO_ANNEEDEBUT]0101'
				GROUP BY 1,2,3,4,5,6,7,8,10;
				
				--maj des pump à 0
				DROP TABLE IF EXISTS w_pump;
				CREATE TEMP TABLE w_pump AS
				SELECT 
					article_id,
					site_id,
					lieu_id,
					COALESCE(lead(date) OVER (PARTITION BY article_id, site_id, lieu_id ORDER BY oid),'1900-01-01') AS prev_date,
					date,
					COALESCE(lead(date) OVER (PARTITION BY article_id, site_id, lieu_id ORDER BY oid),'2099-12-31')  AS next_date,
					prix_unitaire_calcule AS pump_cur,
					prix_unitaire_calcule*base.cti_division(entree_montant+sortie_montant,entree_montant_ht+sortie_montant_ht) AS pump_ttc_cur
				FROM eco.p_mouvements_articles
				WHERE sortie_quantite > 0 AND est_dernier_jour = 1;
				
				UPDATE eco.p_stock
				SET 
					pump = pump_cur,
					stock_valeur_fin = stock_quantite_fin * pump_cur,
					stock_valeur_fin_ttc = stock_quantite_fin * pump_ttc_cur
				FROM w_pump
				WHERE 1=1 AND
					p_stock.article_id =  w_pump.article_id AND
					p_stock.site_id = w_pump.site_id AND
					p_stock.lieu_id = w_pump.lieu_id AND
					date_fin BETWEEN date AND next_date AND
					stock_valeur_fin = 0 AND
					stock_quantite_fin != 0
				;

				UPDATE eco.p_stock
				SET 
					stock_valeur_debut = stock_quantite_debut * pump_cur,
					stock_valeur_debut_ttc = stock_quantite_debut * pump_ttc_cur
				FROM w_pump
				WHERE 1=1 AND
					p_stock.article_id =  w_pump.article_id AND
					p_stock.site_id = w_pump.site_id AND
					p_stock.lieu_id = w_pump.lieu_id AND
					date_debut BETWEEN prev_date AND date AND
					stock_valeur_debut = 0 AND
					stock_quantite_debut != 0
				;

				DROP TABLE IF EXISTS w_pump;
				CREATE TEMP TABLE w_pump AS
				SELECT 
					article_id,
					site_id,
					lieu_id,
					COALESCE(lead(date) OVER (PARTITION BY article_id, site_id, lieu_id ORDER BY oid),'1900-01-01') AS prev_date,
					date,
					COALESCE(lead(date) OVER (PARTITION BY article_id, site_id, lieu_id ORDER BY oid),'2099-12-31')  AS next_date,
					prix_unitaire_calcule AS pump_cur,
					prix_unitaire_calcule*base.cti_division(entree_montant+sortie_montant,entree_montant_ht+sortie_montant_ht) AS pump_ttc_cur
				FROM eco.p_mouvements_articles
				WHERE entree_montant != 0 AND entree_montant_ht != 0 AND est_dernier_jour = 1;
				
				UPDATE eco.p_stock
				SET 
					pump = pump_cur,
					stock_valeur_fin = stock_quantite_fin * pump_cur,
					stock_valeur_fin_ttc = stock_quantite_fin * pump_ttc_cur
				FROM w_pump
				WHERE 1=1 AND
					p_stock.article_id =  w_pump.article_id AND
					p_stock.site_id = w_pump.site_id AND
					p_stock.lieu_id = w_pump.lieu_id AND
					date_fin BETWEEN date AND next_date AND
					stock_valeur_fin = 0 AND
					stock_quantite_fin != 0
				;

				UPDATE eco.p_stock
				SET 
					stock_valeur_debut = stock_quantite_debut * pump_cur,
					stock_valeur_debut_ttc = stock_quantite_debut * pump_ttc_cur
				FROM w_pump
				WHERE 1=1 AND
					p_stock.article_id =  w_pump.article_id AND
					p_stock.site_id = w_pump.site_id AND
					p_stock.lieu_id = w_pump.lieu_id AND
					date_debut BETWEEN prev_date AND date AND
					stock_valeur_debut = 0 AND
					stock_quantite_debut != 0
				;

				-- ajout des stocks mensuels manquants
				SELECT base.cti_execute('
				INSERT INTO eco.p_stock
				(
					article_id,
					compte_id,
					ucd_id,
					lpp_id,
					site_id,
					lieu_id,
					date_debut,
					date_fin,
					entree_quantite,
					entree_montant,
					sortie_quantite,
					sortie_montant,
					pump,
					stock_quantite_debut,
					stock_quantite_fin,
					stock_valeur_debut,
					stock_valeur_debut_ttc,
					stock_valeur_fin,
					stock_valeur_fin_ttc
					)
				SELECT 
					p_stock.article_id,
					p_stock.compte_id,
					p_stock.ucd_id,
					p_stock.lpp_id,
					p_stock.site_id,
					p_stock.lieu_id,
					date(date_trunc(''month'',p_stock.date_debut) - interval ''1 month'') AS date_debut,
					date(date_trunc(''month'',p_stock.date_debut) - interval ''1 day'') AS date_fin,
					0 AS entree_quantite,
					0 AS entree_montant,
					0 AS sortie_quantite,
					0 AS sortie_montant,
					p_stock.pump,
					p_stock.stock_quantite_debut,
					p_stock.stock_quantite_debut,
					p_stock.stock_valeur_debut,
					p_stock.stock_valeur_debut_ttc,
					p_stock.stock_valeur_debut,
					p_stock.stock_valeur_debut_ttc
				FROM eco.p_stock
				LEFT JOIN eco.p_stock p_stock_prev ON
					p_stock.site_id = p_stock_prev.site_id AND
					p_stock.lieu_id = p_stock_prev.lieu_id AND
					p_stock.article_id = p_stock_prev.article_id AND
					date(date_trunc(''month'',p_stock.date_debut) - interval ''1 day'') = p_stock_prev.date_fin
				WHERE 
					p_stock.date_debut <= now() AND
					p_stock.date_fin >= ''[ENV_ECO_ANNEEDEBUT]0101'' AND
					p_stock.stock_quantite_debut != 0 AND
					p_stock_prev.article_id IS NULL
				',200)
				;

				SELECT base.cti_execute('
				INSERT INTO eco.p_stock
				(
					article_id,
					compte_id,
					ucd_id,
					lpp_id,
					site_id,
					lieu_id,
					date_debut,
					date_fin,
					entree_quantite,
					entree_montant,
					sortie_quantite,
					sortie_montant,
					pump,
					stock_quantite_debut,
					stock_quantite_fin,
					stock_valeur_debut,
					stock_valeur_debut_ttc,
					stock_valeur_fin,
					stock_valeur_fin_ttc
					)
				SELECT 
					p_stock.article_id,
					p_stock.compte_id,
					p_stock.ucd_id,
					p_stock.lpp_id,
					p_stock.site_id,
					p_stock.lieu_id,
					date(date_trunc(''month'',p_stock.date_debut) + interval ''1 month'') AS date_debut,
					date(date_trunc(''month'',p_stock.date_debut) + interval ''2 month'' - interval ''1 day'') AS date_fin,
					0 AS entree_quantite,
					0 AS entree_montant,
					0 AS sortie_quantite,
					0 AS sortie_montant,
					p_stock.pump,
					p_stock.stock_quantite_fin,
					p_stock.stock_quantite_fin,
					p_stock.stock_valeur_fin,
					p_stock.stock_valeur_fin_ttc,
					p_stock.stock_valeur_fin,
					p_stock.stock_valeur_fin_ttc
				FROM eco.p_stock
				LEFT JOIN eco.p_stock p_stock_next ON
					p_stock.site_id = p_stock_next.site_id AND
					p_stock.lieu_id = p_stock_next.lieu_id AND
					p_stock.article_id = p_stock_next.article_id AND
					p_stock.date_fin + interval ''1 day'' = p_stock_next.date_debut
				WHERE 
					p_stock.date_debut <= now() AND
					p_stock.date_fin >= ''[ENV_ECO_ANNEEDEBUT]0101'' AND
					p_stock.stock_quantite_fin != 0 AND
					p_stock_next.article_id IS NULL
				',200)
				;
					
				SELECT base.cti_enable_index('eco', 'i_stock_1');
				SELECT base.cti_enable_index('eco', 'i_stock_2');
				SELECT base.cti_enable_index('eco', 'i_stock_3');
				SELECT base.cti_enable_index('eco', 'i_stock_4');
				SELECT base.cti_enable_index('eco', 'i_stock_5');

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

			
			]]></sqlcmd>
		</NODEx>
		
		</NODE>
	<NODE name="POST" label="POST-TRAITEMENTS">
		<sqlcmd><![CDATA[
		]]></sqlcmd>
	</NODE>
	
</ROOT>