<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	<NODE name="INIT" label="INITIALISATIONS">
		
		<NODE label="Tables Articles">
			
			<sqlcmd><![CDATA[
			
				DROP TABLE IF EXISTS w_ARP08;
				CREATE TEMP TABLE w_ARP08 AS
				SELECT UCD, MAX(LIBUCD) AS LIBUCD
				FROM prod_shs.[ECOLIB]_ARP08
				WHERE UCD <> ''
				GROUP BY 1
				;

			
			
				-- UCD
				INSERT INTO base.t_ucd(
					code, 
					texte, 
					texte_court, 
					laboratoire_texte, 
					conditionnement_texte, 
					atc_code, 
					atc_texte, 
					ephmra_code, ephmra_texte, 
					prestation_defaut_id)
				SELECT 
					UCD,
					MAX(LIBUCD),
					'',
					'',
					'', 
					'', 
					'', 
					'', 
					'',
					0 
				FROM w_ARP08
				WHERE UCD <> '' AND 
					UCD NOT IN (SELECT code FROM base.t_ucd)
				GROUP BY 1;
			
				-- LPP
				INSERT INTO base.t_lpp(
					code, 
					texte, 
					texte_court)
				SELECT 
					ARCIA1,
					MAX(ARLI80),
					MAX(ARLI80)
				FROM prod_shs.[ECOLIB]_ARP03
				WHERE ARCIA1 <> '' AND 
					ARCIA1 NOT IN (SELECT code FROM base.t_lpp)
				GROUP BY 1;
				
				
				-- Lettres budgetaires
				INSERT INTO eco.t_lettre_budgetaire(code, texte, texte_court, code_original)
				SELECT BCLBUD, 
					BCLBUD, 
					BCLBUD, 
					BCLBUD
				FROM prod_shs.[ECOLIB]_BCP01
				WHERE BCLBUD NOT IN (SELECT code_original FROM eco.t_lettre_budgetaire)
				GROUP BY 1
				ORDER BY 1;
				
				
				-- comptes
				INSERT INTO eco.t_compte(code, texte, texte_court, code_original)
				SELECT BCCPT1 || BCCPT2 || BCCPT3 || BCCPT4 || BCCPT5 || BCCPT6 || BCCPT7 || BCCPT8, 
					BCLI50, 
					BCLI50, 
					BCIUBC
				FROM prod_shs.[ECOLIB]_BCP01
				WHERE BCIUBC IN (SELECT ARIUBC FROM prod_shs.[ECOLIB]_ARP01) AND
					BCIUBC NOT IN (SELECT code_original FROM eco.t_compte)
				ORDER BY 1;
				
				INSERT INTO eco.t_compte(code, texte, texte_court, code_original)
				SELECT BCCPT1 || BCCPT2 || BCCPT3 || BCCPT4 || BCCPT5 || BCCPT6 || BCCPT7 || BCCPT8, 
					BCLI50, 
					BCLI50, 
					BCIUBC
				FROM prod_shs.[ECOLIB]_BCP01
				WHERE BCIUBC IN (SELECT FVIUBC FROM prod_shs.[ECOLIB]_FVP01) AND
					BCIUBC NOT IN (SELECT code_original FROM eco.t_compte)
				ORDER BY 1;
				
				INSERT INTO eco.t_compte(code, texte, texte_court, code_original)
				SELECT BCCPT1 || BCCPT2 || BCCPT3 || BCCPT4 || BCCPT5 || BCCPT6 || BCCPT7 || BCCPT8, 
					BCLI50, 
					BCLI50, 
					BCIUBC
				FROM prod_shs.[ECOLIB]_BCP01
				WHERE BCIUBC IN (
					SELECT BCIDV1 
					FROM prod_shs.[ECOLIB]_BCP01
					JOIN eco.t_compte ON BCIUBC = t_compte.code_original
					) AND
					BCIUBC NOT IN (SELECT code_original FROM eco.t_compte)
				ORDER BY 1;

				INSERT INTO eco.t_compte(code, texte, texte_court, code_original)
				SELECT BCCPT1 || BCCPT2 || BCCPT3 || BCCPT4 || BCCPT5 || BCCPT6 || BCCPT7 || BCCPT8, 
					BCLI50, 
					BCLI50, 
					BCIUBC
				FROM prod_shs.[ECOLIB]_BCP01
				WHERE BCIUBC IN (
					SELECT BCIDR1 
					FROM prod_shs.[ECOLIB]_BCP01
					JOIN eco.t_compte ON BCIUBC = t_compte.code_original
					) AND
					BCIUBC NOT IN (SELECT code_original FROM eco.t_compte)
				ORDER BY 1;
				
				UPDATE eco.t_compte SET
					texte = BCLI50,
					texte_court = BCLI50
				FROM prod_shs.[ECOLIB]_BCP01
				WHERE BCIUBC = t_compte.code_original AND
					(
					t_compte.texte IS DISTINCT FROM BCLI50 OR
					t_compte.texte_court IS DISTINCT FROM BCLI50
					);
				
				
			
				-- Lettres budgétaires (classes)
				
				DROP TABLE IF EXISTS w_BCP01_classes;
				CREATE TEMP TABLE w_BCP01_classes AS
				SELECT 
					count(DISTINCT CASE WHEN BCLBUD <> '' THEN BCLBUD ELSE NULL END) AS CTI_CPTLETBUD,
					count(DISTINCT CASE WHEN BCIDV1 <> '' THEN BCIDV1 ELSE NULL END) AS CTI_CPTVOTE,
					count(DISTINCT CASE WHEN BCIDR1 <> '' THEN BCIDR1 ELSE NULL END) AS CTI_CPTRCV
				FROM prod_shs.[ECOLIB]_BCP01;

				INSERT INTO eco.t_classes(
					code, texte, table_id, is_cti, sequence)
				SELECT
					'CTI_CPTLETBUD',
					'Lettres budgétaires',
					t_listes_tables.oid,
					'0',
					6
				FROM w_BCP01_classes, eco.t_listes_tables
				WHERE CTI_CPTLETBUD > 1 AND
					t_listes_tables.code = 'CPT' AND
					6 NOT IN (SELECT sequence 
						FROM eco.t_classes
						JOIn eco.t_listes_tables ON t_classes.table_id = t_listes_tables.oid AND t_listes_tables.code = 'CPT');

				DROP TABLE IF EXISTS w_classes;
				CREATE TEMP TABLE w_classes AS
				SELECT t_classes.oid AS current_classe_id
				FROM eco.t_classes
					JOIN eco.t_listes_tables ON t_classes.table_id = t_listes_tables.oid AND t_listes_tables.code = 'CPT'
				WHERE t_classes.sequence = 6;

				INSERT INTO eco.t_classes_sections (code, texte, classe_id)
				SELECT BCLBUD, BCLBUD, current_classe_id
				FROM w_classes ,
					prod_shs.[ECOLIB]_BCP01
					JOIN eco.t_compte ON BCIUBC = t_compte.code_original
				WHERE BCLBUD NOT IN 
						(SELECT t_classes_sections.code 
						FROM eco.t_classes_sections
						JOIN w_classes ON t_classes_sections.classe_id = current_classe_id
						)
				GROUP BY 1,2,3
				ORDER BY 1,2,3;

				DROP TABLE IF EXISTS w_classes_sections_elements;
				CREATE TEMP TABLE w_classes_sections_elements AS
				SELECT t_classes_sections.oid AS section_id, t_compte.oid AS to_id
						FROM eco.t_classes_sections
						JOIN prod_shs.[ECOLIB]_BCP01 ON BCLBUD = t_classes_sections.code
						JOIN eco.t_compte ON code_original = BCIUBC
						JOIN w_classes ON t_classes_sections.classe_id = current_classe_id
				GROUP BY 1,2;		

				DELETE FROM eco.t_classes_sections_elements
				USING w_classes, eco.t_classes_sections
				WHERE t_classes_sections_elements.section_id = t_classes_sections.oid AND 
					t_classes_sections.classe_id = current_classe_id;

				INSERT INTO eco.t_classes_sections_elements(section_id, to_id)
				SELECT 
					section_id, to_id
				FROM w_classes_sections_elements;

				-- Comptes de vote (classes)

				INSERT INTO eco.t_classes(
					code, texte, table_id, is_cti, sequence)
				SELECT
					'CTI_CPTVOTE',
					'Compte de vote',
					t_listes_tables.oid,
					'0',
					7
				FROM w_BCP01_classes, eco.t_listes_tables
				WHERE CTI_CPTVOTE > 1 AND
					t_listes_tables.code = 'CPT' AND
					7 NOT IN (SELECT sequence 
						FROM eco.t_classes
						JOIn eco.t_listes_tables ON t_classes.table_id = t_listes_tables.oid AND t_listes_tables.code = 'CPT');

				DROP TABLE IF EXISTS w_classes;
				CREATE TEMP TABLE w_classes AS
				SELECT t_classes.oid AS current_classe_id
				FROM eco.t_classes
					JOIN eco.t_listes_tables ON t_classes.table_id = t_listes_tables.oid AND t_listes_tables.code = 'CPT'
				WHERE t_classes.sequence = 7;

				INSERT INTO eco.t_classes_sections (code, texte, classe_id)
				SELECT trim(BCP01_2.BCCPT1 || BCP01_2.BCCPT2 || BCP01_2.BCCPT3 || BCP01_2.BCCPT4 || BCP01_2.BCCPT5 || BCP01_2.BCCPT6 || BCP01_2.BCCPT7 || BCP01_2.BCCPT8) , 
					MAX(BCP01_2.BCLI50), current_classe_id
				FROM w_classes ,
					prod_shs.[ECOLIB]_BCP01 BCP01
					JOIN eco.t_compte ON BCIUBC = t_compte.code_original
					JOIN prod_shs.[ECOLIB]_BCP01 BCP01_2 ON BCP01.BCIDV1 = BCP01_2.BCIUBC
				WHERE (BCP01_2.BCCPT1 || BCP01_2.BCCPT2 || BCP01_2.BCCPT3 || BCP01_2.BCCPT4 || BCP01_2.BCCPT5 || BCP01_2.BCCPT6 || BCP01_2.BCCPT7 || BCP01_2.BCCPT8) NOT IN 
						(SELECT t_classes_sections.code 
						FROM eco.t_classes_sections
						JOIN w_classes ON t_classes_sections.classe_id = current_classe_id
						)
				GROUP BY 1,3
				ORDER BY 1,2,3;

				DROP TABLE IF EXISTS w_classes_sections_elements;
				CREATE TEMP TABLE w_classes_sections_elements AS
				SELECT t_classes_sections.oid AS section_id, t_compte.oid AS to_id
						FROM eco.t_classes_sections
						JOIN prod_shs.[ECOLIB]_BCP01 BCP01_2 ON (BCP01_2.BCCPT1 || BCP01_2.BCCPT2 || BCP01_2.BCCPT3 || BCP01_2.BCCPT4 || BCP01_2.BCCPT5 || BCP01_2.BCCPT6 || BCP01_2.BCCPT7 || BCP01_2.BCCPT8) = t_classes_sections.code
						JOIN prod_shs.[ECOLIB]_BCP01 BCP01 ON BCP01.BCIDV1 = BCP01_2.BCIUBC
						JOIN eco.t_compte ON code_original = BCP01.BCIUBC
						JOIN w_classes ON t_classes_sections.classe_id = current_classe_id
				GROUP BY 1,2;		

				DELETE FROM eco.t_classes_sections_elements
				USING w_classes, eco.t_classes_sections
				WHERE t_classes_sections_elements.section_id = t_classes_sections.oid AND 
					t_classes_sections.classe_id = current_classe_id;

				INSERT INTO eco.t_classes_sections_elements(section_id, to_id)
				SELECT 
					section_id, to_id
				FROM w_classes_sections_elements;

				-- Comptes receveur (classes)

				INSERT INTO eco.t_classes(
					code, texte, table_id, is_cti, sequence)
				SELECT
					'CTI_CPTRCV',
					'Compte receveur',
					t_listes_tables.oid,
					'0',
					8
				FROM w_BCP01_classes, eco.t_listes_tables
				WHERE CTI_CPTRCV > 1 AND
					t_listes_tables.code = 'CPT' AND
					8 NOT IN (SELECT sequence 
						FROM eco.t_classes
						JOIn eco.t_listes_tables ON t_classes.table_id = t_listes_tables.oid AND t_listes_tables.code = 'CPT');

				DROP TABLE IF EXISTS w_classes;
				CREATE TEMP TABLE w_classes AS
				SELECT t_classes.oid AS current_classe_id
				FROM eco.t_classes
					JOIN eco.t_listes_tables ON t_classes.table_id = t_listes_tables.oid AND t_listes_tables.code = 'CPT'
				WHERE t_classes.sequence = 8;

				INSERT INTO eco.t_classes_sections (code, texte, classe_id)
				SELECT trim(BCP01_2.BCCPT1 || BCP01_2.BCCPT2 || BCP01_2.BCCPT3 || BCP01_2.BCCPT4 || BCP01_2.BCCPT5 || BCP01_2.BCCPT6 || BCP01_2.BCCPT7 || BCP01_2.BCCPT8) , 
					MAX(BCP01_2.BCLI50), current_classe_id
				FROM w_classes ,
					prod_shs.[ECOLIB]_BCP01 BCP01
					JOIN eco.t_compte ON BCIUBC = t_compte.code_original
					JOIN prod_shs.[ECOLIB]_BCP01 BCP01_2 ON BCP01.BCIDR1 = BCP01_2.BCIUBC
				WHERE (BCP01_2.BCCPT1 || BCP01_2.BCCPT2 || BCP01_2.BCCPT3 || BCP01_2.BCCPT4 || BCP01_2.BCCPT5 || BCP01_2.BCCPT6 || BCP01_2.BCCPT7 || BCP01_2.BCCPT8) NOT IN 
						(SELECT t_classes_sections.code 
						FROM eco.t_classes_sections
						JOIN w_classes ON t_classes_sections.classe_id = current_classe_id
						)
				GROUP BY 1,3
				ORDER BY 1,2,3;

				DROP TABLE IF EXISTS w_classes_sections_elements;
				CREATE TEMP TABLE w_classes_sections_elements AS
				SELECT t_classes_sections.oid AS section_id, t_compte.oid AS to_id
						FROM eco.t_classes_sections
						JOIN prod_shs.[ECOLIB]_BCP01 BCP01_2 ON (BCP01_2.BCCPT1 || BCP01_2.BCCPT2 || BCP01_2.BCCPT3 || BCP01_2.BCCPT4 || BCP01_2.BCCPT5 || BCP01_2.BCCPT6 || BCP01_2.BCCPT7 || BCP01_2.BCCPT8) = t_classes_sections.code
						JOIN prod_shs.[ECOLIB]_BCP01 BCP01 ON BCP01.BCIDR1 = BCP01_2.BCIUBC
						JOIN eco.t_compte ON code_original = BCP01.BCIUBC
						JOIN w_classes ON t_classes_sections.classe_id = current_classe_id
				GROUP BY 1,2;		

				DELETE FROM eco.t_classes_sections_elements
				USING w_classes, eco.t_classes_sections
				WHERE t_classes_sections_elements.section_id = t_classes_sections.oid AND 
					t_classes_sections.classe_id = current_classe_id;

				INSERT INTO eco.t_classes_sections_elements(section_id, to_id)
				SELECT 
					section_id, to_id
				FROM w_classes_sections_elements;
					
					
				
				-- Familles d'articles
				INSERT INTO eco.t_familles_articles(code, texte, texte_court, code_original)
				SELECT DISTINCT THCODE, MAX(THLI40), MAX(THLI40), THCODE 
				FROM prod_shs.[ECOLIB]_THP02GE 
					LEFT JOIN eco.t_familles_articles ON (code_original = THCODE)
				WHERE THCENR = 'FS1' AND code_original IS NULL AND THCODE <> ''
				GROUP BY 1;
				
				UPDATE eco.t_familles_articles
				SET texte = THLI40,
					texte_court = THLI40
				FROM prod_shs.[ECOLIB]_THP02GE
				WHERE 
				THCENR = 'FS1' AND THCODE <> '' AND
					code_original = THCODE AND
					(texte IS DISTINCT FROM THLI40 OR
					texte_court IS DISTINCT FROM THLI40);
					
					
				-- Sous familles
				
				INSERT INTO eco.t_sous_familles_articles(code, texte, texte_court, code_original)
				SELECT DISTINCT THCODE, MAX(THLI40), MAX(THLI40), THCODE 
				FROM prod_shs.[ECOLIB]_THP02GE 
					LEFT JOIN eco.t_sous_familles_articles ON (code_original = THCODE)
				WHERE THCENR = 'FS2' AND code_original IS NULL AND THCODE <> ''
				GROUP BY 1;
				
				UPDATE eco.t_sous_familles_articles
				SET texte = THLI40,
					texte_court = THLI40
				FROM prod_shs.[ECOLIB]_THP02GE
				WHERE 
				THCENR = 'FS2' AND THCODE <> '' AND
					code_original = THCODE AND
					(texte IS DISTINCT FROM THLI40 OR
					texte_court IS DISTINCT FROM THLI40);

				
				
				
				INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
				SELECT UWCATC, UWCATC, MAX(UWLIBE), MAX(UWLIBE) 
				FROM prod_shs.[ECOLIB]_UWA01
				LEFT JOIN eco.t_classification_atc ON (code_original = UWCATC)
				WHERE code_original IS NULL 
				GROUP BY 1
				ORDER BY 1;
				
				UPDATE eco.t_classification_atc
				SET texte = UWLIBE,
					texte_court = UWLIBE
				FROM prod_shs.[ECOLIB]_UWA01
				WHERE 
					code_original = UWCATC AND
					(texte IS DISTINCT FROM UWLIBE OR
					texte_court IS DISTINCT FROM UWLIBE);
					
				INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
				SELECT UWCATC, UWCATC, MAX(UWLIBE), MAX(UWLIBE) 
				FROM prod_shs.[ECOLIB]_UWA03
				LEFT JOIN eco.t_classification_atc ON (code_original = UWCATC)
				WHERE code_original IS NULL 
				GROUP BY 1
				ORDER BY 1;


				-- Standard ATC
				SELECT eco.cti_reorganize_classification_atc();
				
					
					
				-- unités
				
				INSERT INTO eco.t_unites(code, texte, texte_court, code_original)
				SELECT DISTINCT THCODE, MAX(THLI40), MAX(THLI40), THCODE 
				FROM prod_shs.[ECOLIB]_THP02GE LEFT JOIN eco.t_unites ON (code_original = THCODE)
				WHERE THCENR IN ( 'UDI', 'UST') AND code_original IS NULL AND THCODE <> ''
				GROUP BY 1;
				
				UPDATE eco.t_unites
				SET texte = THLI40,
					texte_court = THLI40
				FROM prod_shs.[ECOLIB]_THP02GE
				WHERE 
				THCENR IN ( 'UDI', 'UST') AND THCODE <> '' AND
					code_original = THCODE AND
					(texte IS DISTINCT FROM THLI40 OR
					texte_court IS DISTINCT FROM THLI40);
				
				
				
				-- Types articles
				
				INSERT INTO eco.t_types_articles(code, texte, texte_court, code_original)
				SELECT DISTINCT THCODE, MAX(THLI40), MAX(THLI40), THCODE 
				FROM prod_shs.[ECOLIB]_THP02GE 
					LEFT JOIN eco.t_types_articles ON (code_original = THCODE)
				WHERE THCENR = 'TAG' AND code_original IS NULL AND THCODE <> ''
				GROUP BY 1;
				
				UPDATE eco.t_types_articles
				SET texte = THLI40,
					texte_court = THLI40
				FROM prod_shs.[ECOLIB]_THP02GE
				WHERE 
				THCENR = 'TAG' AND THCODE <> '' AND
					code_original = THCODE AND
					(texte IS DISTINCT FROM THLI40 OR
					texte_court IS DISTINCT FROM THLI40);
				
				
				-- Catégories
				
				INSERT INTO eco.t_categories_articles(code, texte, texte_court, code_original)
				SELECT DISTINCT THCODE, MAX(THLI40), MAX(THLI40), THCODE 
				FROM prod_shs.[ECOLIB]_THP02GE 
					LEFT JOIN eco.t_categories_articles ON (code_original = THCODE)
				WHERE THCENR = 'CAT' AND code_original IS NULL AND THCODE <> ''
				GROUP BY 1;
				
				UPDATE eco.t_categories_articles
				SET texte = THLI40,
					texte_court = THLI40
				FROM prod_shs.[ECOLIB]_THP02GE
				WHERE 
				THCENR = 'CAT' AND THCODE <> '' AND
					code_original = THCODE AND
					(texte IS DISTINCT FROM THLI40 OR
					texte_court IS DISTINCT FROM THLI40);
					
				-- Sous catégories
				
				INSERT INTO eco.t_sous_categories_articles(code, texte, texte_court, code_original)
				SELECT DISTINCT THCODE, MAX(THLI40), MAX(THLI40), THCODE 
				FROM prod_shs.[ECOLIB]_THP02GE LEFT JOIN eco.t_sous_categories_articles ON (code_original = THCODE)
				WHERE THCENR = 'SCA' AND code_original IS NULL AND THCODE <> ''
				GROUP BY 1;
				
				UPDATE eco.t_sous_categories_articles
				SET texte = THLI40,
					texte_court = THLI40
				FROM prod_shs.[ECOLIB]_THP02GE
				WHERE 
				THCENR = 'SCA' AND THCODE <> '' AND
					code_original = THCODE AND
					(texte IS DISTINCT FROM THLI40 OR
					texte_court IS DISTINCT FROM THLI40);
					
				
				-- Lieux
				
				INSERT INTO eco.t_lieux(code, texte, texte_court, code_original)
				SELECT ZLLIEU, ZLNLIE, ZLNLIE, ZLLIEU 
				FROM prod_shs.[ECOLIB]_ZLP01 LEFT JOIN eco.t_lieux ON (code_original = ZLLIEU)
						WHERE code_original IS NULL;
						
				UPDATE eco.t_lieux
				SET texte = ZLNLIE,
					texte_court = ZLNLIE
				FROM prod_shs.[ECOLIB]_ZLP01
				WHERE code_original = ZLLIEU
				AND (texte IS DISTINCT FROM ZLNLIE OR
					texte_court IS DISTINCT FROM ZLNLIE);
				
				INSERT INTO eco.t_lieux(code, texte, texte_court, code_original)
				SELECT ZYLIEU, ZYNLIE, ZYNLIE, ZYLIEU 
				FROM prod_shs.[ECOLIB]_ZYP01 
					LEFT JOIN eco.t_lieux ON (code_original = ZYLIEU)
				WHERE code_original IS NULL;
				

				-- Articles

				INSERT INTO eco.t_articles(code, texte, texte_court, code_original)
				SELECT DISTINCT ARARTI, ARLI40, ARLI15, ARIUAR 
				FROM prod_shs.[ECOLIB]_ARP01 LEFT JOIN eco.t_articles ON (code_original = ARIUAR)
				WHERE code_original IS NULL;
				
				UPDATE eco.t_articles
				SET 
					code = ARARTI,
					texte = ARLI40,
					texte_court = ARLI15
				FROM prod_shs.[ECOLIB]_ARP01
				WHERE code_original = ARIUAR
				AND (code IS DISTINCT FROM ARARTI OR
					texte IS DISTINCT FROM ARLI40 OR
					texte_court IS DISTINCT FROM ARLI15);
				
					


				UPDATE eco.t_articles SET
					type_id = COALESCE(t_types_articles.oid,0),
					famille_id = COALESCE(t_familles_articles.oid,0),
					sous_famille_id = COALESCE(t_sous_familles_articles.oid,0),
					categorie_id = COALESCE(t_categories_articles.oid,0),
					sous_categorie_id = COALESCE(t_sous_categories_articles.oid,0),
					unite_stockage_id = COALESCE(t_unites_stockage.oid,0),
					unite_distribution_id = COALESCE(t_unites_distribution.oid,0)
				FROM prod_shs.[ECOLIB]_ARP01
					LEFT JOIN eco.t_types_articles ON ARTYAG = t_types_articles.code_original
					LEFT JOIN eco.t_familles_articles ON ARKFS1 = t_familles_articles.code_original
					LEFT JOIN eco.t_sous_familles_articles ON ARKFS2 = t_sous_familles_articles.code_original
					LEFT JOIN eco.t_categories_articles ON ARKCAT = t_categories_articles.code_original
					LEFT JOIN eco.t_sous_categories_articles ON ARKSCA = t_sous_categories_articles.code_original
					LEFT JOIN eco.t_unites t_unites_stockage ON ARKUST = t_unites_stockage.code_original
					LEFT JOIN eco.t_unites t_unites_distribution ON ARKUST = t_unites_distribution.code_original
				WHERE 
					t_articles.code_original = ARIUAR AND
					(t_articles.type_id IS DISTINCT FROM COALESCE(t_types_articles.oid,0) OR
					t_articles.famille_id IS DISTINCT FROM COALESCE(t_familles_articles.oid,0) OR
					t_articles.sous_famille_id IS DISTINCT FROM COALESCE(t_sous_familles_articles.oid,0) OR
					t_articles.categorie_id IS DISTINCT FROM COALESCE(t_categories_articles.oid,0) OR
					t_articles.sous_categorie_id IS DISTINCT FROM COALESCE(t_sous_categories_articles.oid,0) OR
					t_articles.unite_stockage_id IS DISTINCT FROM COALESCE(t_unites_stockage.oid,0) OR
					t_articles.unite_distribution_id IS DISTINCT FROM COALESCE(t_unites_distribution.oid,0)
					);



				UPDATE eco.t_articles SET
					classification_atc_id = COALESCE(t_classification_atc.oid,0)
				FROM prod_shs.[ECOLIB]_ARP01
					LEFT JOIN eco.t_classification_atc ON ARCATC = t_classification_atc.code
				WHERE 
					t_articles.code_original = ARIUAR AND
					classification_atc_id IS DISTINCT FROM COALESCE(t_classification_atc.oid,0);

				
				UPDATE eco.t_articles SET classification_atc_id = 0 WHERE classification_atc_id IS NULL;
					
				UPDATE eco.t_articles SET
					compte_id = COALESCE(t_compte.oid,0)
				FROM prod_shs.[ECOLIB]_ARP01
					LEFT JOIN eco.t_compte ON ARIUBC = t_compte.code_original
				WHERE 
					t_articles.code_original = ARIUAR AND
					compte_id IS DISTINCT FROM COALESCE(t_compte.oid,0);

				UPDATE eco.t_articles SET compte_id = 0 WHERE compte_id IS NULL;
				
				
				UPDATE eco.t_articles SET
					taux_tva_en_cours = TZTXTV
				FROM prod_shs.[ECOLIB]_ARP01
				JOIN prod_shs.[ECOLIB]_TZP01 ON ARCTVA = TZCTVA
				WHERE t_articles.code_original = ARIUAR AND
					taux_tva_en_cours IS DISTINCT FROM TZTXTV
				;



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

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

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

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

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

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

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

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

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

						t_articles.compte_code IS DISTINCT FROM t_compte.code OR
						t_articles.compte_texte IS DISTINCT FROM t_compte.texte_court OR
						t_articles.compte_section_id IS DISTINCT FROM t_compte.section_id OR
						t_articles.compte_section_code IS DISTINCT FROM t_compte.section_code OR
						t_articles.compte_section_texte IS DISTINCT FROM t_compte.section_texte
					);
					
				-- LPP
				UPDATE eco.t_articles 
				SET lpp_id = subview.lpp_id
				FROM
					(
						SELECT ARIUAR,(MAX(Array[ARDMEA,t_lpp.oid]))[2] AS lpp_id
						FROM prod_shs.[ECOLIB]_ARP06 ARP06
						JOIN prod_shs.[ECOLIB]_ARP03 ARP03 ON ARIUA1 = ARIULP
						JOIN base.t_lpp ON t_lpp.code = substr(ARP03.ARCIA1,1,7)
						GROUP BY 1
					) subview
				WHERE t_articles.code_original = ARIUAR AND
					t_articles.lpp_id IS DISTINCT FROM subview.lpp_id;


				-- Référence article fournisseur
				DROP TABLE IF EXISTS w_articles;
				CREATE TEMP TABLE w_articles AS
				SELECT t_articles.oid AS article_id,
					t_articles.code AS article_code,
					base.cti_group_array3(COALESCE(fournisseur_id,0))::bigint[] AS ref_fournisseur_id,
					CASE WHEN count(*) < 50 THEN
						base.cti_group_array3(COALESCE(AFLIFA,''))::text[]
						ELSE
						base.cti_group_array3(COALESCE(substr(AFLIFA,20),''))::text[]
						END AS ref_fournisseur_texte,
					base.cti_group_array3(COALESCE(AFREFA,''))::text[] AS ref_fournisseur_code
				FROM eco.t_articles 
					LEFT JOIN (
						SELECT t_articles.oid AS article_id, 
							t_fournisseurs.oid AS fournisseur_id,
							CASE WHEN AFLIFA <> '' THEN AFLIFA ELSE ARLI40 END || CASE WHEN AFREFA <> '' THEN ' (' || AFREFA || ')' ELSE '' END AS AFLIFA,
							AFREFA
						FROM prod_shs.[ECOLIB]_AFP01
						JOIN prod_shs.[ECOLIB]_ARP01 ON AFIUAR = ARIUAR
						JOIN eco.t_articles ON AFIUAR = t_articles.code_original
						JOIN eco.t_fournisseurs ON AFIUFN = t_fournisseurs.code_original
						WHERE AFIUAR <> ''
						) subview ON subview.article_id = t_articles.oid
				GROUP BY 1,2
				ORDER BY 1;

				UPDATE eco.t_articles
				SET 
					ref_fournisseur_id = w_articles.ref_fournisseur_id,
					ref_fournisseur_code = w_articles.ref_fournisseur_code,
					ref_fournisseur_texte = w_articles.ref_fournisseur_texte
				FROM w_articles
				WHERE w_articles.article_id = t_articles.oid AND
					(
						t_articles.ref_fournisseur_id IS DISTINCT FROM w_articles.ref_fournisseur_id OR
						t_articles.ref_fournisseur_code IS DISTINCT FROM w_articles.ref_fournisseur_code OR
						t_articles.ref_fournisseur_texte IS DISTINCT FROM w_articles.ref_fournisseur_texte
					);
					
					
				INSERT INTO eco.t_article_fournisseur (
					code_original,
					article_id,
					fournisseur_id,
					fournisseur_distributeur_id,
					code_reference_fournisseur,
					texte_reference_fournisseur,
					code_cip,
					code_cahpp,
					unite_approvisionnement_id,
					nombre_conditionnement_approvisionnement,
					prix_unitaire_en_cours
					)
				SELECT 
					AFIUAF,
					t_articles.oid AS article_id, 
					t_fournisseurs.oid AS fournisseur_id,
					COALESCE(t_fournisseurs_distributeur.oid,t_fournisseurs.oid) AS fournisseur_distributeur_id,
					AFREFA AS code_reference_fournisseur,
					AFLIFA AS texte_reference_fournisseur,
					AFCBAR,
					AFCAEX,
					COALESCE(t_unites.oid,0) AS unite_approvisionnement_id,
					AFUSUA AS nombre_conditionnement_approvisionnement,
					base.cti_division(AFPUTA::numeric,CASE WHEN AFQTPU > 0 THEN AFQTPU ELSE 1 END::numeric) AS prix_unitaire_en_cours
				FROM prod_shs.[ECOLIB]_AFP01
				JOIN prod_shs.[ECOLIB]_ARP01 ON AFIUAR = ARIUAR
				JOIN eco.t_articles ON AFIUAR = t_articles.code_original
				JOIN eco.t_fournisseurs ON AFIUFN = t_fournisseurs.code_original
				LEFT JOIN eco.t_fournisseurs t_fournisseurs_distributeur ON AFIUF1 = t_fournisseurs_distributeur.code_original
				LEFT JOIN eco.t_unites ON AFKUAP = t_unites.code_original
				WHERE AFIUAR <> '' AND AFIUFN <> '' AND
					AFIUAF NOT IN (SELECT code_original FROM eco.t_article_fournisseur WHERE code_original <> '')
				;

				UPDATE eco.t_article_fournisseur SET
					article_id = t_articles.oid,
					fournisseur_id = t_fournisseurs.oid,
					fournisseur_distributeur_id = COALESCE(t_fournisseurs_distributeur.oid,t_fournisseurs.oid),
					code_reference_fournisseur = AFREFA,
					texte_reference_fournisseur = AFLIFA,
					code_cip = AFCBAR,
					code_cahpp = AFCAEX,
					unite_approvisionnement_id = COALESCE(t_unites.oid,0),
					nombre_conditionnement_approvisionnement = AFUSUA,
					prix_unitaire_en_cours = base.cti_division(AFPUTA::numeric,CASE WHEN AFQTPU > 0 THEN AFQTPU ELSE 1 END::numeric)
				FROM prod_shs.[ECOLIB]_AFP01
				JOIN prod_shs.[ECOLIB]_ARP01 ON AFIUAR = ARIUAR
				JOIN eco.t_articles ON AFIUAR = t_articles.code_original
				JOIN eco.t_fournisseurs ON AFIUFN = t_fournisseurs.code_original
				LEFT JOIN eco.t_fournisseurs t_fournisseurs_distributeur ON AFIUF1 = t_fournisseurs_distributeur.code_original
				LEFT JOIN eco.t_unites ON AFKUAP = t_unites.code_original
				WHERE t_article_fournisseur.code_original = AFIUAF AND
					AFIUAR <> '' AND AFIUFN <> '' AND
					(
						t_article_fournisseur.article_id IS DISTINCT FROM t_articles.oid OR
						t_article_fournisseur.fournisseur_id IS DISTINCT FROM t_fournisseurs.oid OR
						t_article_fournisseur.fournisseur_distributeur_id IS DISTINCT FROM COALESCE(t_fournisseurs_distributeur.oid,t_fournisseurs.oid) OR
						t_article_fournisseur.code_reference_fournisseur IS DISTINCT FROM AFREFA OR
						t_article_fournisseur.texte_reference_fournisseur IS DISTINCT FROM AFLIFA OR
						t_article_fournisseur.code_cip IS DISTINCT FROM AFCBAR OR
						t_article_fournisseur.code_cahpp IS DISTINCT FROM AFCAEX OR
						t_article_fournisseur.unite_approvisionnement_id IS DISTINCT FROM COALESCE(t_unites.oid,0) OR
						t_article_fournisseur.nombre_conditionnement_approvisionnement IS DISTINCT FROM AFUSUA OR
						t_article_fournisseur.prix_unitaire_en_cours IS DISTINCT FROM base.cti_division(AFPUTA::numeric,CASE WHEN AFQTPU > 0 THEN AFQTPU ELSE 1 END::numeric)
					)
				;
					
				-- géré en stock (a améliorer avec D dépot et P pret)
				UPDATE eco.t_articles
				SET 
					gere_en_stock = CASE WHEN ARTARB IN('D','P') THEN 0 ELSE 1 END
				FROM prod_shs.[ECOLIB]_ARP01
				WHERE code_original = ARIUAR;
			]]></sqlcmd>
		</NODE>
		
		
		<NODE label="Tables Fournisseurs">
			<sqlcmd><![CDATA[
			
			-- Types fournisseurs
			
			INSERT INTO eco.t_types_fournisseurs(code, texte, texte_court, code_original)
			SELECT DISTINCT THCODE, MAX(THLI40), MAX(THLI40), THCODE 
			FROM prod_shs.[ECOLIB]_THP02GE 
				LEFT JOIN eco.t_types_fournisseurs ON (code_original = THCODE)
			WHERE THCENR = 'TYF' AND code_original IS NULL AND THCODE <> ''
			GROUP BY 1;
			
			UPDATE eco.t_types_fournisseurs
			SET texte = THLI40,
				texte_court = THLI40
			FROM prod_shs.[ECOLIB]_THP02GE
			WHERE 
			THCENR IN ( 'TYF') AND THCODE <> '' AND
				code_original = THCODE AND
				(texte IS DISTINCT FROM THLI40 OR
				texte_court IS DISTINCT FROM THLI40);
				
				
			INSERT INTO eco.t_types_fournisseurs(code, texte, texte_court, code_original)
			SELECT DISTINCT THCODE, MAX(THLI40), MAX(THLI40), THCODE 
			FROM prod_shs.[ECOLIB]_THP01GE 
				LEFT JOIN eco.t_types_fournisseurs ON (code_original = THCODE)
			WHERE THCENR = 'TYF' AND code_original IS NULL AND THCODE <> ''
			GROUP BY 1;
			
			UPDATE eco.t_types_fournisseurs
			SET texte = THLI40,
				texte_court = THLI40
			FROM prod_shs.[ECOLIB]_THP01GE
			WHERE 
			THCENR IN ( 'TYF') AND THCODE <> '' AND
				code_original = THCODE AND
				(texte IS DISTINCT FROM THLI40 OR
				texte_court IS DISTINCT FROM THLI40);
				
				
			-- Fournisseurs
			
			INSERT INTO eco.t_fournisseurs(code, texte, texte_court, code_original, code_cahpp, siret)
			SELECT DISTINCT FNFOUR, FNNOMF, FNNOMF, FNIUFN, FNCBAR, FNNFIN
			FROM prod_shs.[ECOLIB]_FNP01 
				LEFT JOIN eco.t_fournisseurs ON (code_original = FNIUFN)
			WHERE code_original IS NULL;
			
			
			
			INSERT INTO base.t_codes_postaux(code, texte, texte_court, departement_id)
			SELECT VLCPOS, MAX(VLNOMV), MAX(VLNOMV), MAX(t_departements.oid)
			FROM prod_shs.[ECOLIB]_FNP01
			JOIN prod_shs.[ECOLIB]_VLP01GE ON FNIUVI = VLIUVI
			LEFT JOIN base.t_codes_postaux ON VLCPOS = t_codes_postaux.code
			JOIN base.t_departements ON CASE WHEN VLIPAY = '001' THEN substr(VLCPOS,1,2) else '99' END = t_departements.code
			WHERE t_codes_postaux.oid IS NULL 
			GROUP BY 1
			;
			
			UPDATE eco.t_fournisseurs SET
				texte = FNNOMF,
				texte_court = FNNOMF,
				adresse = trim(FNADRA || ' ' || FNADRB),
				code_postal_id = COALESCE(t_codes_postaux.oid,0),
				ville = VLNOMV,
				cedex = FNCDEX,
				telephone = FNNTEL,
				fax = FNNFAX,
				code_cahpp = FNCBAR,
				siret = FNNFIN
			FROM prod_shs.[ECOLIB]_FNP01
			JOIN prod_shs.[ECOLIB]_VLP01GE ON FNIUVI = VLIUVI
			LEFT JOIN base.t_codes_postaux ON VLCPOS = t_codes_postaux.code
			WHERE code_original = FNIUFN
			AND (
				t_fournisseurs.texte IS DISTINCT FROM FNNOMF OR
				t_fournisseurs.texte_court IS DISTINCT FROM FNNOMF OR
				t_fournisseurs.adresse IS DISTINCT FROM trim(FNADRA || ' ' || FNADRB) OR
				t_fournisseurs.code_postal_id IS DISTINCT FROM COALESCE(t_codes_postaux.oid,0) OR
				t_fournisseurs.ville IS DISTINCT FROM VLNOMV OR
				t_fournisseurs.cedex IS DISTINCT FROM FNCDEX OR
				t_fournisseurs.telephone IS DISTINCT FROM FNNTEL OR
				t_fournisseurs.fax IS DISTINCT FROM FNNFAX OR
				t_fournisseurs.code_cahpp IS DISTINCT FROM FNCBAR OR
				t_fournisseurs.siret IS DISTINCT FROM FNNFIN
				);
				
			UPDATE eco.t_fournisseurs SET
				code_comptable = substr(code,2,5)
			WHERE oid <> 0 AND
				length(t_fournisseurs.code) = 6 AND
				code_comptable IS DISTINCT FROM substr(code,2,5)
			;
			
			UPDATE eco.t_fournisseurs SET
				iban = CASE WHEN FOIBAX <> '' THEN FOIBAX ELSE iban END,
				bic = CASE WHEN FOBICX <> '' THEN FOBICX ELSE bic END
			FROM
				(
				SELECT to_char(FOCFOU,'FM00000') AS FOCFOU, 
					CASE WHEN FOBICC <> '' THEN FOBICC ELSE FOBICP END AS FOBICX,
					CASE WHEN FOIBAC <> '' THEN FOIBAC ELSE FOIBAP END AS FOIBAX
				FROM prod_shs.[COMPTALIB]_FOP02
				WHERE FOBICC <> '' OR FOBICP <> '' OR FOIBAC <> '' OR FOIBAP <> ''
			) subview
			WHERE code_comptable = FOCFOU AND 
				(
				iban IS DISTINCT FROM CASE WHEN FOIBAX <> '' THEN FOIBAX ELSE iban END OR
				bic IS DISTINCT FROM CASE WHEN FOBICX <> '' THEN FOBICX ELSE bic END
				)
			;
			
			UPDATE eco.t_fournisseurs
			SET iban = 'FR76'||' '||substr(FOCBQE,1,4)||' '||substr(FOCBQE,5,4)||' '||substr(FOCBQE,9,4)||' '||substr(FOCBQE,13,4)||' '||substr(FOCBQE,17,4)||' '||substr(FOCBQE,21,3)
			FROM
				(
				SELECT to_char(FOCFOU,'FM00000') AS FOCFOU, 
					FORSOF, 
					CASE 
						WHEN FOBQEF <> '' THEN FOBQEF || FOGUIF || FONCPF || FOCRIF 
						WHEN FOBQEP <> '' THEN FOBQEP || FOGUIP || FONCPP || FOCRIP
						ELSE '' END AS FOCBQE
				FROM prod_shs.[COMPTALIB]_FOP01
				WHERE FOBQEF <> '' OR FOBQEP <> ''
			) subview
			WHERE code_comptable = FOCFOU AND 
				length(FOCBQE) = 23 AND
				iban = ''
			;
			
			UPDATE eco.t_fournisseurs SET
				type_id = COALESCE(t_types_fournisseurs.oid,0)
			FROM prod_shs.[ECOLIB]_FNP01
				LEFT JOIN eco.t_types_fournisseurs ON FNKTYF = t_types_fournisseurs.code_original
				
			WHERE 
				t_fournisseurs.code_original = FNIUFN AND
				(t_fournisseurs.type_id IS DISTINCT FROM COALESCE(t_types_fournisseurs.oid,0));
				
			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
				);
				

			]]></sqlcmd>
		</NODE>
		
		
		
		<NODE label="Autres tables">
			<sqlcmd><![CDATA[
			
			-- Gestionnaires
			
			INSERT INTO eco.t_gestionnaires(code, texte, texte_court, code_original)
			SELECT GEKGEC, GENOMG, GENOMG, GEKGEC 
			FROM prod_shs.[ECOLIB]_GEP01 LEFT JOIN eco.t_gestionnaires ON (code_original = GEKGEC)
			WHERE code_original IS NULL;
			
			UPDATE eco.t_gestionnaires
			SET texte = GENOMG,
				texte_court = GENOMG
			FROM prod_shs.[ECOLIB]_GEP01 
			WHERE code_original = GEKGEC
			AND (texte IS DISTINCT FROM GENOMG OR
				texte_court IS DISTINCT FROM GENOMG);
				
			
			
			
						
			
			-- Unités fonctionnelles
			
			INSERT INTO eco.t_unites_fonctionnelles(code, texte, texte_court, code_original)
			SELECT UFCIUF, UFNMUF, UFNMUF, UFIUUF 
			FROM prod_shs.[ECOLIB]_UFP01GE 
				LEFT JOIN eco.t_unites_fonctionnelles ON (code_original = UFIUUF)
			WHERE code_original IS NULL;
			
			UPDATE eco.t_unites_fonctionnelles
			SET texte = UFNMUF,
				texte_court = UFNMUF
			FROM prod_shs.[ECOLIB]_UFP01GE
			WHERE code_original = UFCIUF
			AND (texte IS DISTINCT FROM UFNMUF OR
				texte_court IS DISTINCT FROM UFNMUF);
				
				
			-- Site

			DROP TABLE IF EXISTS w_UFP01_classes;
			CREATE TEMP TABLE w_UFP01_classes AS
			SELECT 
				count(DISTINCT CASE WHEN UFIUCR <> '' THEN UFIUCR ELSE NULL END) AS CTI_UFCR,
				count(DISTINCT CASE WHEN UFIUSE <> '' THEN UFIUSE ELSE NULL END) AS CTI_UFUM,
				count(DISTINCT CASE WHEN UFSERS <> '' THEN UFSERS ELSE NULL END) AS CTI_UFSERS,
				count(DISTINCT CASE WHEN UFETBF <> '' THEN UFETBF ELSE NULL END) AS CTI_UFSITE
			FROM prod_shs.[ECOLIB]_UFP01GE;



			INSERT INTO eco.t_classes(
				code, texte, table_id, is_cti, sequence)
			SELECT
				'CTI_UFSITE',
				'Site',
				t_listes_tables.oid,
				'0',
				6
			FROM w_UFP01_classes, eco.t_listes_tables
			WHERE CTI_UFSITE > 1 AND
				t_listes_tables.code = 'UFO' AND
				6 NOT IN (SELECT sequence 
					FROM eco.t_classes
					JOIN eco.t_listes_tables ON t_classes.table_id = t_listes_tables.oid AND t_listes_tables.code = 'UFO');


			DROP TABLE IF EXISTS w_classes;
			CREATE TEMP TABLE w_classes AS
			SELECT t_classes.oid AS current_classe_id
			FROM eco.t_classes
				JOIN eco.t_listes_tables ON t_classes.table_id = t_listes_tables.oid AND t_listes_tables.code = 'UFO'
			WHERE t_classes.sequence = 6 AND t_classes.code = 'CTI_UFSITE';


			INSERT INTO eco.t_classes_sections (code, texte, classe_id)
			SELECT LHIUSI , 
				MAX(LHNSIT), current_classe_id
			FROM w_classes ,
				prod_shs.[ECOLIB]_UFP01GE
				JOIN eco.t_unites_fonctionnelles ON UFIUUF = t_unites_fonctionnelles.code_original
				JOIN prod_shs.[ECOLIB]_LHP01GE ON UFETBF = LHIUSI
			WHERE LHIUSI NOT IN 
					(SELECT t_classes_sections.code 
					FROM eco.t_classes_sections
					JOIN w_classes ON t_classes_sections.classe_id = current_classe_id
					)
			GROUP BY 1,3
			ORDER BY 1,2,3;


			DROP TABLE IF EXISTS w_classes_sections_elements;
			CREATE TEMP TABLE w_classes_sections_elements AS
			SELECT t_classes_sections.oid AS section_id, t_unites_fonctionnelles.oid AS to_id
					FROM eco.t_classes_sections
					JOIN prod_shs.[ECOLIB]_LHP01GE ON LHIUSI = t_classes_sections.code
					JOIN prod_shs.[ECOLIB]_UFP01GE ON UFETBF = LHIUSI
					JOIN eco.t_unites_fonctionnelles ON code_original = UFIUUF
					JOIN w_classes ON t_classes_sections.classe_id = current_classe_id
			GROUP BY 1,2;		

			DELETE FROM eco.t_classes_sections_elements
			USING w_classes, eco.t_classes_sections
			WHERE t_classes_sections_elements.section_id = t_classes_sections.oid AND 
				t_classes_sections.classe_id = current_classe_id;


			INSERT INTO eco.t_classes_sections_elements(section_id, to_id)
			SELECT 
				section_id, to_id
			FROM w_classes_sections_elements;

			-- Centre responsabilité

			INSERT INTO eco.t_classes(
				code, texte, table_id, is_cti, sequence)
			SELECT
				'CTI_UFCR',
				'Centre de responsabilité',
				t_listes_tables.oid,
				'0',
				7
			FROM w_UFP01_classes, eco.t_listes_tables
			WHERE CTI_UFCR > 1 AND
				t_listes_tables.code = 'UFO' AND
				7 NOT IN (SELECT sequence 
					FROM eco.t_classes
					JOIN eco.t_listes_tables ON t_classes.table_id = t_listes_tables.oid AND t_listes_tables.code = 'UFO');


			DROP TABLE IF EXISTS w_classes;
			CREATE TEMP TABLE w_classes AS
			SELECT t_classes.oid AS current_classe_id
			FROM eco.t_classes
				JOIN eco.t_listes_tables ON t_classes.table_id = t_listes_tables.oid AND t_listes_tables.code = 'UFO'
			WHERE t_classes.sequence = 7 AND t_classes.code = 'CTI_UFCR';


			INSERT INTO eco.t_classes_sections (code, texte, classe_id)
			SELECT RCCICR , 
				MAX(RCLI25), current_classe_id
			FROM w_classes ,
				prod_shs.[ECOLIB]_UFP01GE UFP01GE
				JOIN eco.t_unites_fonctionnelles ON UFIUUF = t_unites_fonctionnelles.code_original
				JOIN prod_shs.[ECOLIB]_RCP01 ON UFIUCR = RCIURC
			WHERE RCCICR NOT IN 
					(SELECT t_classes_sections.code 
					FROM eco.t_classes_sections
					JOIN w_classes ON t_classes_sections.classe_id = current_classe_id
					)
			GROUP BY 1,3
			ORDER BY 1,2,3;


			DROP TABLE IF EXISTS w_classes_sections_elements;
			CREATE TEMP TABLE w_classes_sections_elements AS
			SELECT t_classes_sections.oid AS section_id, t_unites_fonctionnelles.oid AS to_id
					FROM eco.t_classes_sections
					JOIN prod_shs.[ECOLIB]_RCP01 ON RCCICR = t_classes_sections.code
					JOIN prod_shs.[ECOLIB]_UFP01GE ON UFIUCR = RCIURC
					JOIN eco.t_unites_fonctionnelles ON code_original = UFIUUF
					JOIN w_classes ON t_classes_sections.classe_id = current_classe_id
			GROUP BY 1,2;		

			DELETE FROM eco.t_classes_sections_elements
			USING w_classes, eco.t_classes_sections
			WHERE t_classes_sections_elements.section_id = t_classes_sections.oid AND 
				t_classes_sections.classe_id = current_classe_id;


			INSERT INTO eco.t_classes_sections_elements(section_id, to_id)
			SELECT 
				section_id, to_id
			FROM w_classes_sections_elements;



			-- Unité médicale

			INSERT INTO eco.t_classes(
				code, texte, table_id, is_cti, sequence)
			SELECT
				'CTI_UFUM',
				'Unité médicale',
				t_listes_tables.oid,
				'0',
				8
			FROM w_UFP01_classes, eco.t_listes_tables
			WHERE CTI_UFUM > 1 AND
				t_listes_tables.code = 'UFO' AND
				8 NOT IN (SELECT sequence 
					FROM eco.t_classes
					JOIN eco.t_listes_tables ON t_classes.table_id = t_listes_tables.oid AND t_listes_tables.code = 'UFO');


			DROP TABLE IF EXISTS w_classes;
			CREATE TEMP TABLE w_classes AS
			SELECT t_classes.oid AS current_classe_id
			FROM eco.t_classes
				JOIN eco.t_listes_tables ON t_classes.table_id = t_listes_tables.oid AND t_listes_tables.code = 'UFO'
			WHERE t_classes.sequence = 8 AND t_classes.code = 'CTI_UFUM';


			INSERT INTO eco.t_classes_sections (code, texte, classe_id)
			SELECT UMCSER , 
				MAX(UMNSER), current_classe_id
			FROM w_classes ,
				prod_shs.[ECOLIB]_UFP01GE
				JOIN eco.t_unites_fonctionnelles ON UFIUUF = t_unites_fonctionnelles.code_original
				JOIN prod_shs.[ECOLIB]_UMP01GE ON UFIUSE = UMIUSE
			WHERE UMCSER NOT IN 
					(SELECT t_classes_sections.code 
					FROM eco.t_classes_sections
					JOIN w_classes ON t_classes_sections.classe_id = current_classe_id
					)
			GROUP BY 1,3
			ORDER BY 1,2,3;


			DROP TABLE IF EXISTS w_classes_sections_elements;
			CREATE TEMP TABLE w_classes_sections_elements AS
			SELECT t_classes_sections.oid AS section_id, t_unites_fonctionnelles.oid AS to_id
					FROM eco.t_classes_sections
					JOIN prod_shs.[ECOLIB]_UMP01GE ON UMCSER = t_classes_sections.code
					JOIN prod_shs.[ECOLIB]_UFP01GE ON UFIUSE = UMIUSE
					JOIN eco.t_unites_fonctionnelles ON code_original = UFIUUF
					JOIN w_classes ON t_classes_sections.classe_id = current_classe_id
			GROUP BY 1,2;		

			DELETE FROM eco.t_classes_sections_elements
			USING w_classes, eco.t_classes_sections
			WHERE t_classes_sections_elements.section_id = t_classes_sections.oid AND 
				t_classes_sections.classe_id = current_classe_id;


			INSERT INTO eco.t_classes_sections_elements(section_id, to_id)
			SELECT 
				section_id, to_id
			FROM w_classes_sections_elements;






			-- Service de soins

			INSERT INTO eco.t_classes(
				code, texte, table_id, is_cti, sequence)
			SELECT
				'CTI_UFSERS',
				'Service de soins',
				t_listes_tables.oid,
				'0',
				9
			FROM w_UFP01_classes, eco.t_listes_tables
			WHERE CTI_UFSERS > 1 AND
				t_listes_tables.code = 'UFO' AND
				9 NOT IN (SELECT sequence 
					FROM eco.t_classes
					JOIN eco.t_listes_tables ON t_classes.table_id = t_listes_tables.oid AND t_listes_tables.code = 'UFO');


			DROP TABLE IF EXISTS w_classes;
			CREATE TEMP TABLE w_classes AS
			SELECT t_classes.oid AS current_classe_id
			FROM eco.t_classes
				JOIN eco.t_listes_tables ON t_classes.table_id = t_listes_tables.oid AND t_listes_tables.code = 'UFO'
			WHERE t_classes.sequence = 9 AND t_classes.code = 'CTI_UFSERS';


			INSERT INTO eco.t_classes_sections (code, texte, classe_id)
			SELECT SVKSER , 
				MAX(SVLIBE), current_classe_id
			FROM w_classes ,
				prod_shs.[ECOLIB]_UFP01GE
				JOIN eco.t_unites_fonctionnelles ON UFIUUF = t_unites_fonctionnelles.code_original
				JOIN prod_shs.[ECOLIB]_SVP01GE ON UFSERS = SVKSER
			WHERE SVKSER NOT IN 
					(SELECT t_classes_sections.code 
					FROM eco.t_classes_sections
					JOIN w_classes ON t_classes_sections.classe_id = current_classe_id
					)
			GROUP BY 1,3
			ORDER BY 1,2,3;


			DROP TABLE IF EXISTS w_classes_sections_elements;
			CREATE TEMP TABLE w_classes_sections_elements AS
			SELECT t_classes_sections.oid AS section_id, t_unites_fonctionnelles.oid AS to_id
					FROM eco.t_classes_sections
					JOIN prod_shs.[ECOLIB]_SVP01GE ON SVKSER = t_classes_sections.code
					JOIN prod_shs.[ECOLIB]_UFP01GE ON UFSERS = SVKSER
					JOIN eco.t_unites_fonctionnelles ON code_original = UFIUUF
					JOIN w_classes ON t_classes_sections.classe_id = current_classe_id
			GROUP BY 1,2;		

			DELETE FROM eco.t_classes_sections_elements
			USING w_classes, eco.t_classes_sections
			WHERE t_classes_sections_elements.section_id = t_classes_sections.oid AND 
				t_classes_sections.classe_id = current_classe_id;


			INSERT INTO eco.t_classes_sections_elements(section_id, to_id)
			SELECT 
				section_id, to_id
			FROM w_classes_sections_elements;
			
			
			-- Types mouvements
						
			INSERT INTO eco.t_types_mouvements(code, texte, texte_court, code_original)
			SELECT DISTINCT THCODE, MAX(THLI40), MAX(THLI40), THCODE 
			FROM prod_shs.[ECOLIB]_THP01GE 
				LEFT JOIN eco.t_types_mouvements ON (code_original = THCODE)
			WHERE THCENR = 'CMV' AND code_original IS NULL AND THCODE <> ''
			GROUP BY 1;
			
			UPDATE eco.t_types_mouvements
			SET texte = THLI40,
				texte_court = THLI40
			FROM prod_shs.[ECOLIB]_THP01GE
			WHERE 
				THCENR = 'CMV' AND THCODE <> '' AND
				code_original = THCODE AND
				(texte IS DISTINCT FROM THLI40 OR
				texte_court IS DISTINCT FROM THLI40);
			
			INSERT INTO eco.t_types_mouvements(code_original, code, texte, texte_court) 
			SELECT 'CTI_STOCK', 'CTI_STOCK', 'Stock début de mois', 'Stock début de mois'
			WHERE 'CTI_STOCK' NOT IN (SELECT code_original FROM eco.t_types_mouvements);
			
			

			]]></sqlcmd>
		</NODE>
		
		
	</NODE>
	<NODE name="PROD" label="RECUPERATION DES DONNEES DE PRODUCTION">
		<NODE label="Commandes">
			<sqlcmd><![CDATA[
			
				SELECT base.cti_disable_index('eco', 'i_commandes_1');
				SELECT base.cti_disable_index('eco', 'i_commandes_2');
				SELECT base.cti_disable_index('eco', 'i_commandes_3');
				SELECT base.cti_disable_index('eco', 'i_commandes_4');
				SELECT base.cti_disable_index('eco', 'i_commandes_5');
				SELECT base.cti_disable_index('eco', 'i_commandes_6');
				SELECT base.cti_disable_index('eco', 'i_commandes_7');
				SELECT base.cti_disable_index('eco', 'i_commandes_8');
				
				
			
				TRUNCATE eco.p_commandes;
				
				INSERT INTO eco.p_commandes(
					code_original,
					numero,
					date_commande,
					date_engagement,
					objet,
					reference,
					date_livraison_prevue,
					etat_reception,
					date_reception,
					etat_liquidation,
					fournisseur_id,
					fournisseur_distributeur_id,
					gestionnaire_id,
					lieu_commande_id,
					lieu_facturation_id,
					lieu_livraison_id,
					unite_fonctionnelle_id,
					montant_commande_ht,
					montant_commande_remise,
					montant_commande_tva,
					montant_commande_port,
					montant_commande_total,
					montant_liquidation_ht,
					montant_liquidation_remise,
					montant_liquidation_tva,
					montant_liquidation_port,
					montant_liquidation_total)

				SELECT 
					FUNCDE,
					trim(to_char(FUNCDE,'000000')) AS numero,
					eco.cti_to_date(FUSCRE, FUACRE, FUMCRE, FUJCRE) AS date_commande,
					date((CASE WHEN FUMENG <> 0 THEN eco.cti_to_date(FUSENG, FUAENG, FUMENG, 01::numeric) ELSE eco.cti_to_date(FUSCRE, FUACRE, FUMCRE, 01::numeric) END)  + interval '1 month' - interval '1 day') AS date_engagement,
					trim(FUOBFU) AS objet,
					trim(FUREFC) AS reference,
					eco.cti_to_date(FUSLIP, FUALIP, FUMLIP, FUJLIP) AS date_livraison_prevue,
					CASE WHEN FUTOP4 = '1' THEN 'T' ELSE 'N' END AS etat_reception,
					eco.cti_to_date(FUSLIV, FUALIV, FUMLIV, FUJLIV) AS date_reception,
					CASE WHEN FUTOP5 = '1' THEN 'T' ELSE 'N' END AS etat_liquidation,
					COALESCE(t_fournisseurs.oid,0) AS fournisseur_id,
					COALESCE(t_fournisseurs.oid,0) AS fournisseur_distributeur_id,
					COALESCE(t_gestionnaires.oid,0) AS gestionnaire_id,
					COALESCE(t_lieux_commande.oid,0) AS lieu_commande_id,
					COALESCE(t_lieux_facturation.oid,0) AS lieu_facturation_id,
					COALESCE(t_lieux_livraison.oid,0) AS lieu_livraison_id,
					COALESCE(t_unites_fonctionnelles.oid,0)AS unite_fonctionnelle_id,
					FUMHTC AS montant_commande_ht,
					FUMREC AS montant_commande_remise,
					FUMTVC AS montant_commande_tva,
					FUMPOC AS montant_commande_port,
					FUMTCC AS montant_commande_total,
					FUMHTL AS montant_liquidation_ht,
					FUMREL AS montant_liquidation_remise,
					FUMTVL AS montant_liquidation_tva,
					FUMPOL AS montant_liquidation_port,
					FUMTCL AS montant_liquidation_total
				FROM prod_shs.[ECOLIB]_FUP01
					LEFT JOIN eco.t_fournisseurs ON FUIUFN = t_fournisseurs.code_original
					LEFT JOIN eco.t_gestionnaires ON FUKGEC = t_gestionnaires.code_original
					LEFT JOIN eco.t_lieux t_lieux_commande ON FULFAC = t_lieux_commande.code_original
					LEFT JOIN eco.t_lieux t_lieux_facturation ON FULFAC = t_lieux_facturation.code_original
					LEFT JOIN eco.t_lieux t_lieux_livraison ON FULLIV = t_lieux_livraison.code_original
					LEFT JOIN eco.t_unites_fonctionnelles ON CASE WHEN FUIUU2 <> '' THEN FUIUU2 ELSE FUIUU1 END  = t_unites_fonctionnelles.code_original
					
				WHERE FUSCRE = 20 AND 
					100*FUSCRE + FUACRE >= [ENV_ECO_ANNEEDEBUT] AND 
					FUCANN = '' AND
					FUTOP3 <> '0';

				
				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');
				
				-- Si ligne commande soldée en réception et si quantité réceptionnée <> 0 (etat_livraison<>'N'), modification des quantités commandées
				UPDATE prod_shs.[ECOLIB]_FVP01 SET
					FVQCD1 = FVQRE1,
					FVQCD2 = FVQRE2
				WHERE 1=1
					AND FVQCD1 != FVQRE1
					AND FVTOP1 = '1'
					AND FVQRE1 != 0
				;   			
				
				-- UCD par fournisseur
				DROP TABLE IF EXISTS w_ARAF_ucd;
				CREATE TEMP TABLE w_ARAF_ucd AS
				SELECT AFIUAR, AFIUFN, AFREFA, COALESCE(t_ucd_fournisseur.oid, t_ucd.oid, 0) AS ucd_id
				FROM prod_shs.[ECOLIB]_ARP01 
				LEFT JOIN base.t_ucd ON (t_ucd.code = substr(ARMOT2,1,7) AND length(ARMOT2) = 7) OR (t_ucd.code = substr(ARMOT2,6,7) AND length(ARMOT2) = 13)
				JOIN prod_shs.[ECOLIB]_AFP01 ON AFIUAR = ARIUAR 
				LEFT JOIN base.t_ucd t_ucd_fournisseur ON (t_ucd_fournisseur.code = substr(AFREFA,1,7) AND length(AFREFA) = 7) OR (t_ucd_fournisseur.code = substr(AFREFA,6,7) AND length(AFREFA) = 13)
				WHERE AFIUAR NOT IN (SELECT ARIUAR FROM prod_shs.[ECOLIB]_ARP06) AND
					(ARMOT2 <> '' OR AFREFA <> '') AND
					(t_ucd.oid IS NOT NULL OR t_ucd_fournisseur.oid IS NOT NULL);
					
				CREATE INDEX w_ARAF_ucd_i1 ON w_ARAF_ucd USING btree (AFIUAR);	


				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,
					lpp_id,
					ucd_id,
					unite_approvisionnement_id,
					compte_id,
					quantite_approvisionnement,
					prix_unitaire_approvisionnement,
					multiplicateur_stockage,
					quantite_stockage,
					prix_unitaire_stockage,
					taux_tva,
					unite_fonctionnelle_id,
					
					montant_commande_ht,
					montant_commande_remise,
					montant_commande_tva,
					montant_commande_port,
					montant_commande_ttc,

					quantite_livraison_approvisionnement,
					quantite_livraison_stockage,
					montant_livraison_ht,
					montant_livraison_ttc,
					etat_livraison,

					montant_liquidation_ht,
					montant_liquidation_remise,
					montant_liquidation_tva,
					montant_liquidation_port,
					montant_liquidation_ttc,
					etat_liquidation)
				SELECT
					FVNCDE || '|' || FVLICD AS code_original,
					p_commandes.oid AS commande_id,
					FVLICD AS ligne_commande,
					trim(FVLIA1 || ' ' || FVLIA2) AS texte,
					CASE WHEN t_fournisseurs.fournisseur_distributeur_id <> 0 THEN t_fournisseurs.fournisseur_distributeur_id ELSE t_fournisseurs.oid END AS fournisseur_distributeur_id,
					COALESCE(t_articles.oid, 0) AS article_id,
					COALESCE(t_articles.lpp_id, 0) AS lpp_id,
					COALESCE(w_ARAF_ucd.ucd_id, 0) AS ucd_id,
					COALESCE(t_unites.oid,t_articles.unite_stockage_id,0) AS unite_approvisionnement_id,
					COALESCE(t_compte.oid, t_articles.compte_id, 0) AS compte_id,
					FVQCD1 AS quantite_approvisionnement,
					FVPUN1 AS prix_unitaire_approvisionnement,
					FVUSUA AS multiplicateur_stockage,
					FVQCD2 AS quantite_stockage,
					FVPUN2 AS prix_unitaire_stockage,
					FVTXTV AS taux_tva,
					p_commandes.unite_fonctionnelle_id,

					FVMHT1 AS montant_commande_ht,
					FVMRE1 AS montant_commande_remise,
					FVMTV1 AS montant_commande_tva,
					FVMPO1 AS montant_commande_port,
					FVMTC1 AS montant_commande_ttc,

					FVQRE1 AS quantite_livraison_approvisionnement,
					FVQRE1 AS quantite_livraison_stockage,
					CASE WHEN FVQRE1 = FVQCD1 THEN FVMHT1 WHEN FVQCD1 = 0 THEN 0 ELSE FVMHT1 / FVQCD1 * FVQRE1 END AS montant_livraison_ht,
					CASE WHEN FVQRE1 = FVQCD1 THEN FVMTC1 WHEN FVQCD1 = 0 THEN 0 ELSE FVMTC1 / FVQCD1 * FVQRE1 END AS montant_livraison_ht,
					CASE WHEN FVQRE1 = FVQCD1 THEN 'T' WHEN FVQRE1 > FVQCD1 THEN 'S' WHEN FVQRE1 = 0 THEN 'N' ELSE 'P' END AS etat_livraison,

					COALESCE(YXMHT5,0) AS montant_liquidation_ht,
					COALESCE(YXMRE5,0) AS montant_liquidation_remise,
					COALESCE(YXMTV5,0) AS montant_liquidation_tva,
					COALESCE(YXMPO5,0) AS montant_liquidation_port,
					COALESCE(YXMTC5,0) AS montant_liquidation_ttc,
					CASE WHEN FVTOP3 = '1' THEN 'T' ELSE 'N' END AS etat_liquidation
					
				FROM prod_shs.[ECOLIB]_FVP01
					JOIN prod_shs.[ECOLIB]_FUP01 ON FVNCDE = FUNCDE
					LEFT JOIN prod_shs.[ECOLIB]_YXP01 ON FVNLIQ = YXNLIQ and FVLILI = YXLILI
					JOIN eco.p_commandes ON FVNCDE = p_commandes.code_original
					JOIN eco.t_fournisseurs ON p_commandes.fournisseur_id = t_fournisseurs.oid
					LEFT JOIN eco.t_articles ON FVIUAR = t_articles.code_original AND t_articles.oid <> 0
					LEFT JOIN w_ARAF_ucd ON FVIUAR = AFIUAR AND FUIUFN = AFIUFN
					LEFT JOIN eco.t_unites ON FVKUAP = t_unites.code_original
					LEFT JOIN eco.t_compte ON FVIUBC = t_compte.code_original AND t_compte.oid <> 0
				WHERE FUSCRE = 20 AND 
					100*FUSCRE + FUACRE >=  [ENV_ECO_ANNEEDEBUT] AND 
					FUCANN = '' AND
					FUTOP3 <> '0' AND 
					FVCANN = ''
				;
				
				-- forcage des commandes et livraisons en fonction de la liquidation => dans share
				
				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');
				
				-- UCD par défaut article
				UPDATE eco.t_articles
				SET ucd_id = 0
				WHERE ucd_id <> 0 AND
					code_original NOT IN (SELECT AFIUAR FROM w_ARAF_ucd);
					
				UPDATE eco.t_articles
				SET ucd_id = subview.ucd_id
				FROM
					(
					SELECT article_id, count(distinct ucd_id), (MAX(Array[date_commande::text, ucd_id::text]))[2]::bigint AS ucd_id
					FROM eco.p_lignes_commandes
					JOIN eco.p_commandes ON commande_id = p_commandes.oid
					WHERE ucd_id <> 0
					GROUP BY 1
					) subview
				WHERE t_articles.oid = subview.article_id AND
					subview.ucd_id IS DISTINCT FROM t_articles.ucd_id;
					
				UPDATE eco.t_articles
				SET ucd_id = subview.ucd_id
				FROM
				(
				SELECT t_articles.oid AS article_id,  max(COALESCE(t_ucd_fournisseur.oid, t_ucd.oid, 0)) AS ucd_id
				FROM prod_shs.[ECOLIB]_ARP01 
				JOIN eco.t_articles ON ARIUAR = t_articles.code_original
				LEFT JOIN base.t_ucd ON (t_ucd.code = substr(ARMOT2,1,7) AND length(ARMOT2) = 7) OR (t_ucd.code = substr(ARMOT2,6,7) AND length(ARMOT2) = 7)
				JOIN prod_shs.[ECOLIB]_AFP01 ON AFIUAR = ARIUAR 
				LEFT JOIN base.t_ucd t_ucd_fournisseur ON (t_ucd_fournisseur.code = substr(AFREFA,1,7) AND length(AFREFA) = 7) OR (t_ucd_fournisseur.code = substr(AFREFA,6,7) AND length(AFREFA) = 13)
				WHERE AFIUAR NOT IN (SELECT ARIUAR FROM prod_shs.[ECOLIB]_ARP06) AND
					(ARMOT2 <> '' OR AFREFA <> '') AND
					(t_ucd.oid IS NOT NULL OR t_ucd_fournisseur.oid IS NOT NULL)
				GROUP BY 1
				) subview
				WHERE t_articles.oid = subview.article_id
				;


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

			]]></sqlcmd>
		</NODE>
		
		<NODE label="Mouvements">
			<sqlcmd><![CDATA[
			
				-- Détection séjour et facture par numéro de mouvements.
				DROP TABLE IF EXISTS w_UBA01_DOSS;
				CREATE TEMP TABLE w_UBA01_DOSS AS
				SELECT UBNMVT,
					UBNCDE,
					UBLICD,
					eco.cti_to_date(SYSIEC, SYANNE, SYMOIS, SYJOUR) AS date_mouvement,
					UBDOSS::text AS no_sejour, 
					UBIUHN::text AS no_patient
				FROM prod_shs.[ECOLIB]_UBA01
				JOIN prod_shs.[ECOLIB]_SYP01 ON SYNMVT = UBNMVT
				WHERE UBNMVT <> 0 AND
					(base.cti_to_number(UBDOSS) <> 0 OR base.cti_to_number(UBIUHN) <> 0)
				;


				INSERT INTO w_UBA01_DOSS 
				SELECT UBNMVL, 
					UBNCDE,
					UBLICD,
					eco.cti_to_date(SYSIEC, SYANNE, SYMOIS, SYJOUR) AS date_mouvement,
					UBDOSS::text AS no_sejour, 
					UBIUHN::text AS no_patient
				FROM prod_shs.[ECOLIB]_UBA01
				JOIN prod_shs.[ECOLIB]_SYP01 ON SYNMVT = UBNMVL
				WHERE UBNMVL <> 0 AND
					UBNMVL NOT IN (SELECT UBNMVT FROM w_UBA01_DOSS ) AND
					(base.cti_to_number(UBDOSS) <> 0 OR base.cti_to_number(UBIUHN) <> 0)
				;

				INSERT INTO w_UBA01_DOSS 
				SELECT SYNMVT, 
					UBNCDE,
					UBLICD,
					eco.cti_to_date(SYSIEC, SYANNE, SYMOIS, SYJOUR) AS date_mouvement,
					UBDOSS::text AS no_sejour, 
					UBIUHN::text AS no_patient
				FROM prod_shs.[ECOLIB]_UBA01
				JOIN prod_shs.[ECOLIB]_SYP01 ON SYNCDE = UBNCDE AND SYLICD = UBLICD
				WHERE UBNCDE <> 0 AND
					SYNMVT NOT IN (SELECT UBNMVT FROM w_UBA01_DOSS) AND
					(base.cti_to_number(UBDOSS) <> 0 OR base.cti_to_number(UBIUHN) <> 0)
				;

				UPDATE w_UBA01_DOSS 
				SET no_sejour = NULL
				WHERE no_sejour = ''
				;


				UPDATE w_UBA01_DOSS 
				SET no_patient = NULL
				WHERE no_patient = ''
				;
				
				-- Uniquement les mouvements qui n'ont qu'un seul séjour
				DROP TABLE IF EXISTS w_UBA01_DOSS_U;
				CREATE TEMP TABLE w_UBA01_DOSS_U AS
				SELECT UBNMVT, COALESCE(MAX(no_sejour),'') AS no_sejour, COALESCE(MAX(no_patient),'') AS no_patient
				FROM w_UBA01_DOSS 
				WHERE no_sejour IS NOT NULL OR no_patient IS NOT NULL
				GROUP BY 1
				HAVING count(DISTINCT no_sejour) <= 1 AND count(DISTINCT no_patient) <= 1 
				;
				
				CREATE INDEX iw_UBA01_DOSS_U_UBNMVT ON w_UBA01_DOSS_U USING btree (UBNMVT)
				;



				TRUNCATE eco.p_mouvements_articles;

				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_1');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_2');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_3');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_4');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_5');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_6');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_7');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_8');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_9');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_10');

				-- TVA articles
				DROP SEQUENCE IF EXISTS w_MVT_sequence;
				CREATE TEMP SEQUENCE w_mvt_sequence; 

				DROP TABLE IF EXISTS w_MVT_ARTICLES;
				CREATE TEMP TABLE w_MVT_ARTICLES AS
				SELECT subview.*, nextval(' w_MVT_sequence'::regclass) AS sequence
				FROM
				(
				SELECT 
					SYNMVT AS code_original,
					eco.cti_to_date(SYSIEC, SYANNE, SYMOIS, SYJOUR) AS date,
					CASE WHEN SYKNMV = 'E' OR (SYKNMV = 'T' AND SYKCMV = 'TE') THEN 'E' ELSE 'S' END AS sens_mouvement,
					COALESCE(t_types_mouvements.oid,0) AS type_mouvement_id,
					TRIM(SYLI25) AS texte,
					COALESCE(t_gestionnaires.oid,0) AS gestionnaire_id,
					COALESCE(t_lieux.oid,0) AS lieu_id,
					COALESCE(t_unites_fonctionnelles.oid,0) AS unite_fonctionnelle_id,
					COALESCE(t_articles.oid,0) AS article_id,
					COALESCE(t_compte.oid,t_articles.compte_id,0) AS compte_id,
					COALESCE(t_fournisseurs.oid,0) AS fournisseur_id,
					COALESCE(p_commandes.oid,0) AS commande_id,
					SYLICD AS ligne_commande,
					COALESCE(t_articles.lpp_id,0) AS lpp_id,
					COALESCE(t_ucd.oid,0) AS ucd_id,
					SYPRMV AS prix_unitaire,
					CASE WHEN SYKNMV = 'E' OR (SYKNMV = 'T' AND SYKCMV = 'TE') THEN SYQMVT ELSE 0 END AS entree_quantite,
					CASE WHEN SYKNMV = 'S' OR (SYKNMV = 'T' AND SYKCMV = 'TS') THEN CASE WHEN SYKCMV <> 'NS' AND SYKCMV <> 'IN' THEN SYQMVT ELSE 0 - SYQMVT END ELSE 0 END AS sortie_quantite,
					CASE WHEN SYKNMV = 'E' OR (SYKNMV = 'T' AND SYKCMV = 'TE') THEN 
						CASE 
							WHEN SYKNMV = 'E' AND SYKCMV IN ('EE','IE') AND SYMTC5 <> 0 THEN SYMTC5
							WHEN SYKNMV = 'E' AND SYKCMV IN ('EE','IE') AND SYMTC5 = 0 THEN SYMTC1
							WHEN SYKNMV = 'E' AND SYKCMV = 'EL' AND SYMTC1 = 0 THEN SYMTC5
							WHEN SYKNMV = 'E' AND SYKCMV = 'EL' AND SYMTC5 = 0 THEN SYMTC1
							WHEN SYKNMV = 'E' AND SYKCMV = 'EL' THEN SYMTC5 - SYMTC1
							WHEN SYKNMV = 'E' AND SYKCMV = 'AE' THEN SYMTC1
							WHEN SYKNMV = 'E' AND SYKCMV IN('EN', 'EP') THEN SYMTC5
							WHEN SYMTC5 <> 0 THEN SYMTC5
							WHEN SYMTC5 = 0 THEN SYMTC1
							ELSE SYQMVT * SYPRMV 
						END
					ELSE 0 END AS entree_montant,
					CASE WHEN SYKNMV = 'S' OR (SYKNMV = 'T' AND SYKCMV = 'TS') THEN 
						CASE 
							WHEN SYKNMV = 'S' AND SYKCMV IN ('IS') AND SYMTC5 <> 0 THEN SYMTC5
							WHEN SYKNMV = 'S' AND SYKCMV IN ('IS') AND SYMTC5 = 0 THEN SYMTC1
							WHEN SYKNMV = 'S' AND SYKCMV = 'SL' AND SYMTC1 = 0 THEN SYMTC5
							WHEN SYKNMV = 'S' AND SYKCMV = 'SL' AND SYMTC5 = 0 THEN SYMTC1
							WHEN SYKNMV = 'S' AND SYKCMV = 'SL' THEN SYMTC5 - SYMTC1
							WHEN SYKNMV = 'S' AND SYKCMV IN ('AS','IP','PS') AND SYMTC1 <> 0 THEN SYMTC1
							WHEN SYKNMV = 'S' AND SYKCMV IN ('AS','IP','PS') THEN SYMTC5
							WHEN SYKNMV = 'S' AND SYKCMV IN ('IN','NS') THEN 0-SYMTC1
							WHEN SYMTC5 <> 0 THEN SYMTC5
							WHEN SYMTC5 = 0 THEN SYMTC1
							ELSE SYQMVT * SYPRMV 
						END 
					ELSE 0 END AS sortie_montant,
					0 AS stock_quantite_debut,
					0 AS stock_quantite_fin,
					COALESCE(w_UBA01_DOSS_U.no_patient,'') AS no_patient,
					COALESCE(w_UBA01_DOSS_U.no_sejour,'') AS no_sejour
				FROM prod_shs.[ECOLIB]_SYP01
					LEFT JOIN prod_shs.[ECOLIB]_AFP01 ON SYIUAF = AFIUAF
					LEFT JOIN w_ARP08 ON AFREFA = UCD
					LEFT JOIN prod_shs.[ECOLIB]_FUP01 ON SYNCDE  = FUNCDE 
					LEFT JOIN eco.t_types_mouvements ON SYKCMV = t_types_mouvements.code_original
					LEFT JOIN eco.t_gestionnaires ON SYKGEC = t_gestionnaires.code_original
					LEFT JOIN eco.t_lieux ON SYLIEU = t_lieux.code_original
					LEFT JOIN eco.t_unites_fonctionnelles ON SYIUUF = t_unites_fonctionnelles.code_original
					LEFT JOIN eco.t_articles ON SYIUAR = t_articles.code_original AND t_articles.oid <> 0
					LEFT JOIN eco.t_compte ON SYIUBC = t_compte.code_original AND t_compte.oid <> 0
					LEFT JOIN eco.t_fournisseurs ON CASE
						WHEN FUIUFN <> '' THEN FUIUFN
						WHEN AFIUFN <> '' THEN AFIUFN 
						ELSE SYIUFN END
						= t_fournisseurs.code_original
					LEFT JOIN eco.p_commandes ON SYNCDE = p_commandes.code_original
					LEFT JOIN base.t_ucd ON ((substr(UCD,1,7) = t_ucd.code AND length(UCD) = 7) OR (substr(UCD,1,7) = t_ucd.code AND length(UCD) = 7)) AND t_ucd.oid <> 0
					LEFT JOIN w_UBA01_DOSS_U ON UBNMVT = SYNMVT
				WHERE SYSIEC = 20 AND 100*SYSIEC + SYANNE >= [ENV_ECO_ANNEEDEBUT] AND SYCANN = ''
				ORDER BY eco.cti_to_date(SYSIEC, SYANNE, SYMOIS, SYJOUR),SYNMVT
				) subview
				;
				
				INSERT INTO eco.p_mouvements_articles(
					oid,
					code_original,
					date, 
					sens_mouvement,
					type_mouvement_id, 
					texte, 
					gestionnaire_id, 
					lieu_id, 
					unite_fonctionnelle_id, 
					article_id, 
					compte_id, 
					fournisseur_id,
					commande_id,
					ligne_commande,
					lpp_id,
					ucd_id,
					prix_unitaire, 
					entree_quantite, 
					sortie_quantite,
					entree_montant, 
					sortie_montant,
					stock_quantite_debut,
					stock_quantite_fin,
					no_patient,
					no_sejour)
				SELECT 
					sequence,
					code_original,
					date, 
					sens_mouvement,
					type_mouvement_id, 
					texte, 
					gestionnaire_id, 
					lieu_id, 
					unite_fonctionnelle_id, 
					article_id, 
					compte_id, 
					fournisseur_id,
					commande_id,
					ligne_commande,
					lpp_id,
					ucd_id,
					prix_unitaire, 
					entree_quantite, 
					sortie_quantite,
					entree_montant, 
					sortie_montant,
					stock_quantite_debut,
					stock_quantite_fin,
					no_patient,
					no_sejour
				FROM 
					w_MVT_ARTICLES
				;
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_1');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_2');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_3');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_4');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_5');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_6');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_7');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_8');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_9');
				SELECT base.cti_enable_index('eco', 'i_mouvements_articles_10');
				
				UPDATE eco.p_mouvements_articles SET
					entree_quantite = 0 - entree_quantite,
					sortie_quantite = 0 - sortie_quantite,
					entree_montant = 0 - entree_montant,
					sortie_montant = 0 - sortie_montant,
					entree_montant_ht = 0 - entree_montant_ht,
					sortie_montant_ht = 0 - sortie_montant_ht,
					sortie_montant_original_ttc = 0 - sortie_montant_original_ttc,
					sortie_montant_original_ht = 0 - sortie_montant_original_ht
				FROM eco.t_types_mouvements
				WHERE type_mouvement_id = t_types_mouvements.oid AND
					t_types_mouvements.particularite_inverser_signe = '1';
					
				UPDATE eco.p_mouvements_articles SET
					entree_quantite = sortie_quantite,
					sortie_quantite = entree_quantite,
					entree_montant = sortie_montant,
					sortie_montant = entree_montant,
					entree_montant_ht = sortie_montant_ht,
					sortie_montant_ht = entree_montant_ht,
					sortie_montant_original_ttc = sortie_montant_original_ht,
					sortie_montant_original_ht = sortie_montant_original_ttc,
					sens_mouvement = sens_force
				FROM eco.t_types_mouvements
				WHERE type_mouvement_id = t_types_mouvements.oid AND
					t_types_mouvements.sens_force IN ('E','S') AND t_types_mouvements.sens_force != p_mouvements_articles.sens_mouvement
				;

				UPDATE eco.p_mouvements_articles SET
					entree_montant_ht = base.cti_division(entree_montant, (1 + tx_tva )),
					sortie_montant_ht = base.cti_division(sortie_montant, (1 + tx_tva ))
				FROM (
				SELECT 
					article_id,
					date_commande,
					round(base.cti_division(p_lignes_commandes.montant_commande_ttc - p_lignes_commandes.montant_commande_ht + p_lignes_commandes.montant_commande_remise, p_lignes_commandes.montant_commande_ht),3) AS tx_tva,
					COALESCE(lead(date_commande,1) OVER (PARTITION BY article_id ORDER BY date_commande) - interval '1 day', '2099-12-31') AS date_next
					FROM eco.p_lignes_commandes 
						JOIN eco.p_commandes ON commande_id = p_commandes.oid
					WHERE p_lignes_commandes.montant_commande_ttc != 0
					ORDER BY 1,2
				) sub
				WHERE 1=1
					AND sub.article_id = p_mouvements_articles.article_id
					AND p_mouvements_articles.date between date_commande and date_next
				;
			
			
				UPDATE eco.p_mouvements_articles
				SET ucd_id = t_articles.ucd_id
				FROM eco.t_articles
				WHERE article_id = t_articles.oid AND
					p_mouvements_articles.ucd_id = 0 AND
					t_articles.ucd_id <> 0;
			]]></sqlcmd>
		</NODE>
		
		
		<NODE label="Stocks">
			<sqlcmd><![CDATA[
			
				-- Table temporaire pour modification stock entrées si différent stock fin mois précédent
				DROP TABLE IF EXISTS w_STP01;
				CREATE TEMP TABLE w_STP01 AS
				SELECT *,
					(STSIEC*1200+STANNE*12+STMOIS) AS STSYMD,
					date(eco.cti_to_date(STSIEC, STANNE, STMOIS, 01::numeric) + interval '1 month' - Interval '1 day') AS STDATE,
					STQSEP+STQTEP AS STQENT,
					STQSSP+STQTSP AS STQSOR,
					0 AS stock_ok
				FROM prod_shs.[ECOLIB]_STP01
				WHERE to_char(STSIEC*10000+STANNE*100+STMOIS,'FM000000') <= to_char(now(),'YYYYMM')
				;

				CREATE INDEX w_STP01_i1 ON w_STP01 USING btree (STIUAR);	
				CREATE INDEX w_STP01_i2 ON w_STP01 USING btree (STLIEU);
				CREATE INDEX w_STP01_i3 ON w_STP01 USING btree (STSYMD);
				CREATE INDEX w_STP01_i4 ON w_STP01 USING btree (STDATE);
				
				ALTER TABLE w_STP01 ALTER COLUMN STQSPH TYPE numeric;

				UPDATE w_STP01
				SET STQSPH = STQSDP + STQENT - STQSOR
				WHERE STQSDP + STQENT - STQSOR <> STQSPH AND
					w_STP01.STDATE >= date(now() - interval '1 year')
				;

				-- Traitement des mois non clos
				DROP TABLE IF EXISTS w_STP01_cloture;
				CREATE TEMP TABLE w_STP01_cloture AS
				SELECT STLIEU, MIN(STSYMD) AS STSYMD_cloture
				FROM
				(
				SELECT w_STP01.STSYMD, w_STP01.STLIEU,
					SUM(case when w_STP01.STQSPH IS DISTINCT FROM w_STP01_apres.STQSDP THEN 1 ELSE 0 END),
					SUM(case when w_STP01.STQSPH IS NOT DISTINCT FROM w_STP01_apres.STQSDP THEN 1 ELSE 0 END),
					MAX(case when w_STP01.STQSPH IS DISTINCT FROM w_STP01_apres.STQSDP THEN w_STP01.STIUAR  ELSE '' END)
				FROM w_STP01
				LEFT JOIN w_STP01 w_STP01_apres ON 
					w_STP01.STLIEU = w_STP01_apres.STLIEU AND
					w_STP01.STIUAR = w_STP01_apres.STIUAR AND
					w_STP01.STSYMD = w_STP01_apres.STSYMD -1
				WHERE w_STP01.STDATE >= date(now() - interval '1 year') AND
					(w_STP01.STQSPH <> 0)
				GROUP BY 1,2
				HAVING SUM(case when w_STP01.STQSPH IS DISTINCT FROM w_STP01_apres.STQSDP THEN 1 ELSE 0 END) > 0
				ORDER BY 1 DESC, 2
				) subview
				GROUP BY 1
				;

				DROP TABLE IF EXISTS w_STP01_nonclos;
				CREATE TEMP TABLE w_STP01_nonclos AS
				SELECT STLIEU, mois, (ANNEE*12+numero_mois) AS STSYMD_nonclos, (ANNEE*12+numero_mois)-1 AS STSYMD_nonclos_prec
				FROM base.p_calendrier_mois
				JOIN w_STP01_cloture ON (ANNEE*12+numero_mois) >= STSYMD_cloture AND date_debut <= date(now())
				ORDER BY 1,2
				;
				
				SELECT base.cti_execute('
					DROP TABLE IF EXISTS w_STP01_manquant
					.,
					CREATE TEMP TABLE w_STP01_manquant AS
					SELECT w_STP01.*
					FROM w_STP01
					JOIN w_STP01_nonclos ON 
						w_STP01.STLIEU = w_STP01_nonclos.STLIEU AND
						w_STP01.STSYMD = w_STP01_nonclos.STSYMD_nonclos -1
					LEFT JOIN w_STP01 w_STP01_apres ON 
						w_STP01.STLIEU = w_STP01_apres.STLIEU AND
						w_STP01.STIUAR = w_STP01_apres.STIUAR AND
						w_STP01_nonclos.STSYMD_nonclos = w_STP01_apres.STSYMD
					WHERE w_STP01.STQSPH <> 0 AND
						w_STP01_apres IS NULL
					.,
					UPDATE w_STP01_manquant SET
						STSYMD = STSYMD+1,
						STDATE = date(STDATE+interval ''1 month''),
						STQSDP = STQSPH,
						STPUMD = STPUMF,
						STQENT = 0,
						STQSOR = 0
					.,
					INSERT INTO w_STP01
					SELECT *
					FROM w_STP01_manquant
				',20)
				;

				SELECT base.cti_execute('
				
				ALTER TABLE w_STP01 ALTER COLUMN STQSDP TYPE numeric.,
				
				UPDATE w_STP01 SET
					STQSDP = w_STP01_avant.STQSPH,
					STPUMD = CASE WHEN w_STP01.STPUMD = 0 THEN w_STP01_avant.STPUMF ELSE w_STP01.STPUMD END,
					STQSPH = w_STP01_avant.STQSPH + w_STP01.STQENT - w_STP01.STQSOR,
					STPUMF = CASE WHEN w_STP01.STPUMF = 0 THEN w_STP01_avant.STPUMF ELSE w_STP01.STPUMF END
				FROM w_STP01 w_STP01_avant
				WHERE 
					w_STP01.STLIEU = w_STP01_avant.STLIEU AND
					w_STP01.STIUAR = w_STP01_avant.STIUAR AND
					w_STP01.STSYMD = '||STSYMD_nonclos||' AND
					w_STP01_avant.STSYMD = '||STSYMD_nonclos-1||' AND
					(
					w_STP01.STQSDP <> w_STP01_avant.STQSPH OR
					w_STP01.STPUMD <> w_STP01_avant.STPUMF OR
					w_STP01.STQSPH <> w_STP01_avant.STQSPH + w_STP01.STQENT - w_STP01.STQSOR OR
					w_STP01.STPUMF <> w_STP01_avant.STPUMF
					)
				',1)
				FROM
				(
					SELECT STSYMD_nonclos
					FROM w_STP01_nonclos
					GROUP BY 1
					ORDER BY 1
				) subview
				
				;

				

				
				ANALYSE w_STP01
				;
				
				-- Ajout à la base
				
			
				SELECT base.cti_disable_index('eco', 'i_stock_1');
				SELECT base.cti_disable_index('eco', 'i_stock_2');
				SELECT base.cti_disable_index('eco', 'i_stock_3');
				SELECT base.cti_disable_index('eco', 'i_stock_4');
				SELECT base.cti_disable_index('eco', 'i_stock_5');
			

				TRUNCATE eco.p_stock;
				
				INSERT INTO eco.p_stock(
						article_id,
						date_fin,
						date_debut,
						ucd_id,
						lpp_id,
						compte_id,
						lieu_id,
						provider_id,
						fournisseur_id,
						stock_quantite_debut,
						stock_valeur_debut,
						entree_quantite,
						entree_montant,
						sortie_quantite,
						sortie_montant,
						stock_quantite_fin,
						stock_valeur_fin,
						pump)
				SELECT 
					COALESCE(t_articles.oid,0) AS article_id,
					STDATE AS date_fin,
					date(date_trunc('month',STDATE)) AS date_debut,
					COALESCE(t_articles.ucd_id,0) AS ucd_id,
					COALESCE(t_articles.lpp_id,0) AS lpp_id,
					t_articles.compte_id AS compte_id,
					COALESCE(t_lieux.oid,0) AS lieu_id,
					0::bigint AS provider_id,
					t_articles.fournisseur_principal_id AS fournisseur_id,
					STQSDP AS stock_quantite_debut,
					STQSDP * STPUMD AS stock_valeur_debut,
					STQENT AS entree_quantite,
					STVSEP+STVTEP AS entree_montant,
					STQSOR AS sortie_quantite,
					STVSSP+STVTSP AS sortie_montant,
					STQSDP + STQENT - STQSOR AS stock_quantite_fin,
					(STQSDP + STQENT - STQSOR) * STPUMF AS stock_valeur_fin,
					STPUMF
				FROM w_STP01
					LEFT JOIN eco.t_lieux ON STLIEU = t_lieux.code_original
					LEFT JOIN eco.t_articles ON STIUAR = t_articles.code_original AND t_articles.oid <> 0
				WHERE STSIEC = 20 AND 100*STSIEC + STANNE  >= [ENV_ECO_ANNEEDEBUT] AND STCANN = '' AND
					(
					STQSDP IS DISTINCT FROM 0 OR
					STQENT IS DISTINCT FROM 0 OR
					STVSEP IS DISTINCT FROM 0 OR
					STVTEP IS DISTINCT FROM 0 OR
					STQSOR IS DISTINCT FROM 0 OR
					STVSSP IS DISTINCT FROM 0 OR
					STVTSP IS DISTINCT FROM 0 OR
					STQTSP IS DISTINCT FROM 0 
					);
					
				SELECT base.cti_enable_index('eco', 'i_stock_1');
				SELECT base.cti_enable_index('eco', 'i_stock_2');
				SELECT base.cti_enable_index('eco', 'i_stock_3');
				SELECT base.cti_enable_index('eco', 'i_stock_4');
				SELECT base.cti_enable_index('eco', 'i_stock_5');
				
				
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Factures">
			<sqlcmd><![CDATA[
			
				

			
			]]></sqlcmd>
		</NODE>
		
	</NODE>
	<NODE name="POST" label="POST-TRAITEMENTS">
	
		<NODE label="Compléments fournisseurs">
			<sqlcmd><![CDATA[
			
				UPDATE eco.t_fournisseurs
				SET fournisseur_distributeur_id = subview.fournisseur_distributeur_id
				FROM
				(
				SELECT fournisseur_id, (MAX(Array[nb,fournisseur_distributeur_id]))[2] AS fournisseur_distributeur_id
				FROM
				(
				SELECT fournisseur_id,fournisseur_distributeur_id, count(*) AS nb, max(date_commande)
				FROM eco.p_commandes
				GROUP BY 1,2
				) subview
				GROUP BY 1
				) subview
				WHERE t_fournisseurs.oid = subview.fournisseur_id AND
					t_fournisseurs.fournisseur_distributeur_id IS DISTINCT FROM subview.fournisseur_distributeur_id
				;

			
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Calcul des stocks journaliers">
			<sqlcmd><![CDATA[
				SELECT eco.cti_update_mvt_qte_stock();
				SELECT eco.cti_update_mvt_mnt_stock();
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Liens séjours">
			<sqlcmd><![CDATA[		
				SELECT eco.cti_reorganize_sejour();
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Compléments séjours">
			<sqlcmd><![CDATA[		
				SELECT eco.cti_reorganize_sejour_ucd_lpp();
			]]></sqlcmd>
		</NODE>
	</NODE>
	
</ROOT>
