<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	<NODE name="INIT" label="INITIALISATIONS">
		
		
		<NODE label="Tables Articles">
			
			<sqlcmd><![CDATA[
			
			-- comptes
			INSERT INTO eco.t_compte(code, texte, texte_court, code_original)
			SELECT CCODECOMPTABLE,CDESIGN, LEFT(CDESIGN,50), CNOID
			FROM prod_hestia.TCODECOMPTABLE
			WHERE
				CCODECOMPTABLE IN (SELECT CCOMPTEGEF FROM prod_hestia.TSTOCKPRODUITGEF) AND
				CNOID NOT IN (SELECT code_original FROM eco.t_compte)
			GROUP BY 1,2,3,4
			ORDER BY 1;

			UPDATE eco.t_compte
			SET
				code = CCODECOMPTABLE,
				texte = CDESIGN,
				texte_court = LEFT(CDESIGN,50)
			FROM prod_hestia.TCODECOMPTABLE
			WHERE CNOID = code_original
				AND (
					code != CCODECOMPTABLE OR
					texte != CDESIGN OR
					texte_court != LEFT(CDESIGN,50)
				)
			;
			-- unités
			--avec grammage
			--SELECT CNOID, CASE WHEN UPPER(CABREGE) != 'UN' THEN UPPER(CABREGE) ELSE UPPER(CABREGE) || base.cti_division() || ' (Kg)' END, CDESIGN, LEFT(CDESIGN,50)
			--FROM prod_hestia.TUNITESGESTION JOIN prod_hesti.TDENREEFOURNISS ON UNICNOIDUNITEGESTION = TUNITEGESTION.OID
			--LEFT JOIN eco.t_unites ON (code_original = CNOID)
			----WHERE code_original IS NULL
			--;
			
			
			INSERT INTO eco.t_unites(code_original, code, texte, texte_court )
				SELECT 
				TUNITESGESTION.CNOID || '-' || CGRAMME, 
				CASE WHEN UPPER(CABREGE) != 'UN' THEN UPPER(CABREGE) ELSE UPPER(CABREGE) || '-' || CGRAMME END,
				CDESIGN || ' (' || CGRAMME || 'g)',
				LEFT(CDESIGN || ' (' || CGRAMME || 'g)',50)
			FROM prod_hestia.TUNITESGESTION 
				JOIN prod_hestia.TDENREEFOURNIS ON CNOIDUNITEGESTION = TUNITESGESTION.CNOID
			LEFT JOIN eco.t_unites ON code = CASE WHEN UPPER(CABREGE) != 'UN' THEN UPPER(CABREGE) ELSE UPPER(CABREGE) || '-' || CGRAMME END
			WHERE code IS NULL
			GROUP BY 1,2,3,4
			;
			
			UPDATE eco.t_unites
			SET 	
				code =  CASE WHEN UPPER(CABREGE) != 'UN' THEN UPPER(CABREGE) ELSE UPPER(CABREGE) || '-' || CGRAMME END,
				texte = CDESIGN || ' (' || CGRAMME || 'g)',
				texte_court = LEFT(CDESIGN || ' (' || CGRAMME || 'g)',50)
			FROM prod_hestia.TUNITESGESTION
				JOIN prod_hestia.TDENREEFOURNIS ON CNOIDUNITEGESTION = TUNITESGESTION.CNOID
			WHERE 
				code_original = TUNITESGESTION.CNOID || '-' || CGRAMME AND
				(code IS DISTINCT FROM CASE WHEN UPPER(CABREGE) != 'UN' THEN UPPER(CABREGE) ELSE UPPER(CABREGE) || '-' || CGRAMME END OR
				texte IS DISTINCT FROM CDESIGN || ' (' || CGRAMME || 'g)' OR
				texte_court IS DISTINCT FROM LEFT(CDESIGN || ' (' || CGRAMME || 'g)',50))
			;
			
			-- Familles
			INSERT INTO eco.t_familles_articles(code_original, code, texte, texte_court )
			SELECT CNOID, CNOID, CDESIGN,  LEFT(CDESIGN,50)
			FROM prod_hestia.TFAMILDENR
			LEFT JOIN eco.t_familles_articles ON (code_original = CNOID)
			WHERE code_original IS NULL 
			;
			
			UPDATE eco.t_familles_articles
			SET texte = CDESIGN,
				texte_court =  LEFT(CDESIGN,50)
			FROM prod_hestia.TFAMILDENR
			WHERE code_original = CNOID AND
				(texte IS DISTINCT FROM CDESIGN OR
				texte_court IS DISTINCT FROM  LEFT(CDESIGN,50));
			
			-- Types articles
			
			INSERT INTO eco.t_types_articles(code_original, code, texte, texte_court )
			SELECT 'C', 'C','Cuisine','Cuisine'
			WHERE 'C' NOT IN (SELECT code_original FROM eco.t_types_articles)
			GROUP BY 1;
			
			-- Catégories
			
			
			INSERT INTO eco.t_categories_articles(code_original, code, texte, texte_court )
			SELECT CNOID, CDESIGN, LEFT(CDESIGN,50), CNOID
			FROM prod_hestia.tcategoriefournisseur 
			LEFT JOIN eco.t_categories_articles ON (CNOID = t_categories_articles.code_original)
			WHERE code_original IS NULL
			;
			
			UPDATE eco.t_categories_articles
			SET texte = CDESIGN,
				texte_court = LEFT(CDESIGN,50)
			FROM prod_hestia.tcategoriefournisseur 
			WHERE 
				 CNOID = t_categories_articles.code_original
			AND (
				t_categories_articles.texte IS DISTINCT FROM CDESIGN OR
				t_categories_articles.texte_court IS DISTINCT FROM LEFT(CDESIGN,50))
			;
			
			
			-- Lieux
			INSERT INTO eco.t_lieux(code_original, code, texte, texte_court )
			SELECT CNOID, CNOID, CDESIGN, LEFT(CDESIGN,50)
			FROM prod_hestia.TLIEUSTOCKAGE
			LEFT JOIN eco.t_lieux ON (code_original = CNOID)
			WHERE code_original IS NULL
			;
			
			UPDATE eco.t_lieux
			SET 	
				texte = CDESIGN,
				texte_court = LEFT(CDESIGN,50)
			FROM prod_hestia.TLIEUSTOCKAGE
			WHERE 
				code_original = CNOID AND
				(
				texte IS DISTINCT FROM CDESIGN OR
				texte_court IS DISTINCT FROM LEFT(CDESIGN,50))
			;
			
			-- Sites
			-- sites = magasin
			
			-- Articles
			DROP TABLE IF EXISTS w_PRODUIT;
			CREATE TEMP TABLE w_PRODUIT AS
			SELECT
				CNOID AS code_original,
				COALESCE(NULLIF(CCODEGP,0),to_char(CNOID,'FM000000')) AS code,
				CDESIGN AS texte,
				LEFT(CDESIGN,50) AS texte_court,*
			FROM prod_hestia.TDENREE
			;
				
		
			INSERT INTO eco.t_articles(code_original, code, texte, texte_court )
			SELECT w_PRODUIT.code_original, w_PRODUIT.code, w_PRODUIT.texte, substr(w_PRODUIT.texte_court,1,50)
			FROM w_PRODUIT 
			LEFT JOIN eco.t_articles ON (w_PRODUIT.code_original = t_articles.code_original)
			WHERE t_articles.code_original IS NULL;
			
			UPDATE eco.t_articles
			SET 
				code = w_PRODUIT.code,
				texte = w_PRODUIT.texte,
				texte_court = w_PRODUIT.texte_court
			FROM w_PRODUIT 
			WHERE w_PRODUIT.code_original = t_articles.code_original
			AND (t_articles.code IS DISTINCT FROM w_PRODUIT.code OR
				t_articles.texte IS DISTINCT FROM w_PRODUIT.texte OR
				t_articles.texte_court IS DISTINCT FROM w_PRODUIT.texte_court)
			;
			
			UPDATE eco.t_articles
			SET gere_en_stock = ''
			WHERE gere_en_stock IS NULL;
			
			UPDATE eco.t_articles
			SET compte_id = t_compte.oid
			FROM prod_hestia.tstockproduitgef 
				JOIN eco.t_compte ON CCOMPTEGEF = code
			WHERE  CDENREEGEF = t_articles.code
			;
			
			UPDATE eco.t_articles SET
				type_id = COALESCE(t_types_articles.oid,0),
				famille_id = COALESCE(t_types_articles.oid,0),
				sous_famille_id = COALESCE(t_types_articles.oid,0),
				categorie_id = COALESCE(t_categories_articles.oid,0),
				sous_categorie_id = 0,
				unite_stockage_id = COALESCE(t_unites_stockage.oid,0),
				unite_distribution_id = COALESCE(t_unites_distribution.oid,0)
			FROM w_PRODUIT
				LEFT JOIN eco.t_types_articles ON 'C' = t_types_articles.code_original
				LEFT JOIN eco.t_familles_articles ON CNOFAMILDENR = t_familles_articles.code_original
				LEFT JOIN eco.t_sous_familles_articles ON 'XXX' = t_familles_articles.code_original
				LEFT JOIN eco.t_categories_articles ON 'XXX' = t_categories_articles.code_original
				LEFT JOIN eco.t_unites t_unites_stockage ON 'g' = t_unites_stockage.code
				LEFT JOIN eco.t_unites t_unites_distribution ON 'g' = t_unites_distribution.code_original
			WHERE 
				t_articles.code_original = w_PRODUIT.CNOID AND
				(
					t_articles.type_id IS DISTINCT FROM COALESCE(t_types_articles.oid,0) OR
					t_articles.categorie_id IS DISTINCT FROM COALESCE(t_categories_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.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
				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 eco.t_article_fournisseur (
					code_original,
					article_id,
					fournisseur_id,
					code_reference_fournisseur,
					texte_reference_fournisseur,
					unite_approvisionnement_id,
					nombre_conditionnement_approvisionnement,
					prix_unitaire_en_cours
					)
				SELECT 
					CNOID,
					t_articles.oid, 
					t_fournisseurs.oid,
					'',
					CNOMDENRFOURNISSEUR,
					CNOIDUNITEGESTION,
					CQTEGRSTOCK,
					CPRIXDEFAUT
				FROM prod_hestia.TDENREEFOURNIS
					LEFT JOIN eco.t_fournisseurs ON CNOIDFOURNISSEUR = t_fournisseurs.code_original
					LEFT JOIN eco.t_articles ON CNODENREE = t_articles.code_original
				WHERE CNOID NOT IN (SELECT t_article_fournisseur.code_original FROM eco.t_article_fournisseur WHERE code_original IS NOT NULL)
				;

		]]></sqlcmd>
		</NODE>
		
		
		<NODE label="Tables Fournisseurs">
			<sqlcmd><![CDATA[
		
			-- Fournisseurs
			INSERT INTO eco.t_fournisseurs(code, texte, texte_court, code_original)
			SELECT CCODEGEF, CDESIGN, LEFT(CDESIGN,50), CNOID
			FROM prod_hestia.tfournisseur 
			LEFT JOIN eco.t_fournisseurs ON (CNOID = t_fournisseurs.code_original)
			WHERE t_fournisseurs.code_original IS NULL;
			
			
			UPDATE eco.t_fournisseurs
			SET 	
				code = CCODEGEF,
				texte = CDESIGN,
				texte_court = LEFT(CDESIGN,50)
			FROM prod_hestia.tfournisseur 
			WHERE CNOID = t_fournisseurs.code_original
			AND (
				t_fournisseurs.code IS DISTINCT FROM CCODEGEF OR
				t_fournisseurs.texte IS DISTINCT FROM CDESIGN OR
				t_fournisseurs.texte_court IS DISTINCT FROM LEFT(CDESIGN,50));
			
			
			
			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[
			
			
			-- Unités fonctionnelles
			
			INSERT INTO eco.t_unites_fonctionnelles(code, texte, texte_court, code_original)
				SELECT CNOID, CDESIGN, LEFT(CDESIGN,50), CNOID
			FROM prod_hestia.TSERVICE 
			LEFT JOIN eco.t_unites_fonctionnelles ON (code_original = CNOID)
			WHERE code_original IS NULL;
			
			UPDATE eco.t_unites_fonctionnelles
			SET texte = CDESIGN,
				texte_court = substr(CDESIGN,1,50)
			FROM prod_hestia.TSERVICE
			WHERE code_original = CNOID
			AND (texte IS DISTINCT FROM CDESIGN OR
				texte_court IS DISTINCT FROM substr(CDESIGN,1,50));
			
			
			-- Types mouvements
			
			
			INSERT INTO eco.t_types_mouvements(code_original, code, texte, texte_court )
			SELECT 
				CTYPE, 
				CTYPE, 
				CTYPE, 
				CTYPE
			FROM 
				prod_hestia.tstockmouvement
				LEFT JOIN eco.t_types_mouvements ON code_original = CTYPE
			WHERE code_original IS NULL 
			GROUP BY 1,2,3,4
			ORDER BY 1;
			
			

			]]></sqlcmd>
		</NODE>
		
		
	</NODE>
	<NODE name="PROD" label="RECUPERATION DES DONNEES DE PRODUCTION">
		<NODE label="Commandes">
			<sqlcmd><![CDATA[
				
				TRUNCATE eco.p_commandes;

				INSERT INTO eco.p_commandes(
					code_original,
					numero,
					date_commande,
					objet,
					reference,
					date_livraison_prevue,
					etat_reception,
					date_reception,
					etat_liquidation,
					fournisseur_id,
					gestionnaire_id,
					lieu_commande_id,
					lieu_facturation_id,
					lieu_livraison_id,
					site_id,
					unite_fonctionnelle_id,
					montant_commande_ht,
					montant_commande_remise,
					montant_commande_tva,
					montant_commande_port,
					montant_commande_total,
					montant_liquidation_ht,
					montant_liquidation_remise,
					montant_liquidation_tva,
					montant_liquidation_port,
					montant_liquidation_total)

				SELECT 
					CNOID,
					CNOID AS numero,
					date(coalesce(NULLIF(CDATECOMMANDE,''),'1900-01-01')) AS date_commande,
					trim('') AS objet,
					trim('') AS reference,
					date(coalesce(NULLIF(CDATELIVRAISON,''),'2099-12-31'))  AS date_livraison_prevue,
					CASE WHEN CDATELIVRAISON IS NOT NULL THEN 'T' ELSE 'N' END AS etat_reception,
					date(coalesce(NULLIF(CDATEFACTUREFOURNISSEUR,''),'2099-12-31')) AS date_reception,
					CASE WHEN CDATEFACTUREFOURNISSEUR IS NOT NULL THEN 'T' ELSE 'N' END AS etat_liquidation,
					COALESCE(t_fournisseurs.oid,0) AS fournisseur_id,
					COALESCE(t_gestionnaires.oid,0) AS gestionnaire_id,
					COALESCE(t_lieux_commande.oid,0) AS lieu_commande_id,
					COALESCE(t_lieux_facturation.oid,0) AS lieu_facturation_id,
					COALESCE(t_lieux_livraison.oid,0) AS lieu_livraison_id,
					COALESCE(t_sites.oid,0) AS site_id,
					COALESCE(t_unites_fonctionnelles.oid,0) AS unite_fonctionnelle_id,
					CTOTALHT::numeric  AS montant_commande_ht,
					0 AS montant_commande_remise,
					CTOTALTVA::numeric AS montant_commande_tva,
					0 AS montant_commande_port,
					CTOTALTTC AS montant_commande_total,
					CASE WHEN CDATEFACTUREFOURNISSEUR IS NOT NULL THEN CTOTALHT::numeric ELSE 0 END AS montant_liquidation_ht,
					0 AS montant_liquidation_remise,
					CASE WHEN CDATEFACTUREFOURNISSEUR IS NOT NULL THEN CTOTALTVA::numeric ELSE 0 END AS montant_liquidation_tva,
					CASE WHEN CDATEFACTUREFOURNISSEUR IS NOT NULL THEN 0 ELSE 0 END AS montant_liquidation_port,
					CASE WHEN CDATEFACTUREFOURNISSEUR IS NOT NULL THEN CTOTALFACTUREFOURNISSEUR::numeric ELSE 0 END AS montant_liquidation_total
				FROM prod_hestia.TSTOCKCOMMANDE
					LEFT JOIN eco.t_fournisseurs ON CNOIDFOURNISSEUR = t_fournisseurs.code_original
					LEFT JOIN eco.t_gestionnaires ON '0' = t_gestionnaires.code_original
					LEFT JOIN eco.t_sites ON 'XXX' = t_sites.code_original
					LEFT JOIN eco.t_lieux t_lieux_commande ON 'XXX' = t_lieux_commande.code_original
					LEFT JOIN eco.t_lieux t_lieux_facturation ON 'XXX' = t_lieux_facturation.code_original
					LEFT JOIN eco.t_lieux t_lieux_livraison ON 'XXX'  = t_lieux_livraison.code_original
					LEFT JOIN eco.t_unites_fonctionnelles ON '0' = t_unites_fonctionnelles.code_original
				WHERE (CDATECOMMANDE BETWEEN '[ENV_ECO_ANNEEDEBUT]0101' AND now() OR 
					CDATELIVRAISON BETWEEN '[ENV_ECO_ANNEEDEBUT]0101' AND now()
					)
					AND TSTOCKCOMMANDE.CTYPE = 14
					;



				TRUNCATE  eco.p_lignes_commandes;


				INSERT INTO eco.p_lignes_commandes(
					code_original,
					commande_id,
					ligne_commande,
					texte,
					article_id,
					unite_approvisionnement_id,
					compte_id,
					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_prorata_tva)
				SELECT
					TSTOCKDETAILCOMMANDE.CNOID AS code_original,
					p_commandes.oid AS commande_id,
					CNOLIGNE AS ligne_commande,
					t_articles.texte AS texte,
					t_articles.oid AS article_id,
					COALESCE(t_unites.oid,t_articles.unite_stockage_id) AS unite_approvisionnement_id,
					t_articles.compte_id AS compte_id,
					CQTECOMMANDER::numeric AS quantite_approvisionnement,
					CPRIXHT::numeric AS prix_unitaire_approvisionnement,
					1 AS multiplicateur_stockage,
					CQTECOMMANDER::numeric AS quantite_stockage,
					CPRIXHT::numeric AS prix_unitaire_stockage,

					CQTECOMMANDER::numeric * CPRIXHT::numeric AS montant_commande_ht,
					0 AS montant_commande_remise,
					CPRIXTTC::numeric - ( CQTECOMMANDER::numeric * CPRIXHT::numeric ) AS montant_commande_tva,
					0 AS montant_commande_port,
					CPRIXTTC AS montant_commande_ttc,

					CASE WHEN CNOLOTINTERNE != 0 THEN CQTECOMMANDER ELSE 0 END AS quantite_livraison_approvisionnement,
					CASE WHEN CNOLOTINTERNE != 0 THEN CQTECOMMANDER * CEQGRAMMEUNITEGESTION ELSE 0 END AS quantite_livraison_stockage,
					CASE WHEN CNOLOTINTERNE != 0 THEN CQTECOMMANDER * CPRIXHT ELSE 0 END AS montant_livraison_ht,
					CASE WHEN CNOLOTINTERNE != 0 THEN CPRIXTTC ELSE 0 END AS montant_livraison_ttc,
					CASE WHEN CNOLOTINTERNE != 0 THEN 'N' ELSE 'T' END AS etat_livraison,

					CASE WHEN TSTOCKDETAILCOMMANDE.CETAT = 60 THEN CQTECOMMANDER * CPRIXHT ELSE 0 END AS montant_liquidation_ht,
					CASE WHEN TSTOCKDETAILCOMMANDE.CETAT = 60 THEN 0 ELSE 0 END AS montant_liquidation_remise,
					CASE WHEN TSTOCKDETAILCOMMANDE.CETAT = 60 THEN CPRIXTTC - CPRIXHT*CQTECOMMANDER ELSE 0 END AS montant_liquidation_tva,
					CASE WHEN TSTOCKDETAILCOMMANDE.CETAT = 60 THEN 0 ELSE 0 END AS montant_liquidation_port,
					CASE WHEN TSTOCKDETAILCOMMANDE.CETAT = 60 THEN CPRIXTTC ELSE 0 END AS montant_liquidation_ttc,
					CASE WHEN TSTOCKDETAILCOMMANDE.CETAT = 60 THEN 'T' ELSE 'N' END AS etat_liquidation,
					
					CASE WHEN t_articles.type_prorata_tva IS DISTINCT FROM '1' THEN COALESCE(t_prorata_tva.taux,0) ELSE 0 END
					
				FROM prod_hestia.TSTOCKDETAILCOMMANDE
					JOIN prod_hestia.TSTOCKCOMMANDE ON CNOIDSTOCKCOMMANDE = TSTOCKCOMMANDE.CNOID
					JOIN eco.p_commandes ON CNOIDSTOCKCOMMANDE = p_commandes.code_original
					JOIN prod_hestia.TDENREEFOURNIS ON CNOIDDENREEFOURNISSEUR = TDENREEFOURNIS.CNOID
					JOIN eco.t_articles ON CNODENREE = t_articles.code_original
					LEFT JOIN eco.t_unites ON CABREGEUNITEGESTION || '-' || CGRAMME = t_unites.code
					LEFT JOIN eco.t_prorata_tva ON t_prorata_tva.oid = 0
				--WHERE ETATCMDE > 1
				;

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

				TRUNCATE eco.p_mouvements_articles;

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


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

				SELECT 
					TSTOCKMOUVEMENT.CNOID,
					date(LEFT(CDATEHEUREMOUVEMENT,8)) AS date,
					CASE WHEN CQTEGRAMME > 0 THEN 'E' ELSE 'S' END AS sens_mouvement,
					COALESCE(t_types_mouvements.oid,0) AS type_mouvement_id,
					COALESCE(t_types_mouvements.texte,'') || t_articles.texte 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_articles.oid,0) AS article_id,
					COALESCE(t_articles.compte_id,0) AS compte_id,
					COALESCE(t_unites_fonctionnelles.oid,0) AS unite_fonctionnelle_id,
					COALESCE(t_fournisseurs.oid,t_fournisseurs_articles.oid,0) AS fournisseur_id,
					COALESCE(p_lignes_commandes.commande_id,0) AS commande_id,
					COALESCE(p_lignes_commandes.ligne_commande,0) AS commande_id,
					CQTEGRAMME/(TSTOCKDETAILCOMMANDE.CQTEUNITEGESTION*CEQGRAMMEUNITEGESTION) AS prix_unitaire,
					CASE WHEN CQTEGRAMME >= 0 THEN CQTEGRAMME ELSE 0 END AS entree_quantite,
					CASE WHEN CQTEGRAMME < 0  THEN -CQTEGRAMME ELSE 0 END AS sortie_quantite,
					CASE WHEN CQTEGRAMME >= 0 THEN (CPRIXHT*CQTEGRAMME*(1+CTVADEF)/(TSTOCKDETAILCOMMANDE.CQTEUNITEGESTION*CEQGRAMMEUNITEGESTION)) ELSE 0 END AS entree_montant,
					CASE WHEN CQTEGRAMME < 0  THEN -(CPRIXHT*CQTEGRAMME*(1+CTVADEF)/(TSTOCKDETAILCOMMANDE.CQTEUNITEGESTION*CEQGRAMMEUNITEGESTION)) ELSE 0 END AS sortie_montant,
					CASE WHEN CQTEGRAMME >= 0 THEN (CPRIXHT*CQTEGRAMME/(TSTOCKDETAILCOMMANDE.CQTEUNITEGESTION*CEQGRAMMEUNITEGESTION)) ELSE 0 END AS entree_montant_ht,
					CASE WHEN CQTEGRAMME < 0  THEN -(CPRIXHT*CQTEGRAMME/(TSTOCKDETAILCOMMANDE.CQTEUNITEGESTION*CEQGRAMMEUNITEGESTION)) ELSE 0 END AS sortie_montant,
					CASE WHEN CQTEGRAMME >= 0  THEN (CPRIXHT*CQTEGRAMME*(1+CTVADEF)/(TSTOCKDETAILCOMMANDE.CQTEUNITEGESTION*CEQGRAMMEUNITEGESTION)) ELSE 0 END AS sortie_montant_original_ttc,
					CASE WHEN CQTEGRAMME < 0  THEN -(CPRIXHT*CQTEGRAMME/(TSTOCKDETAILCOMMANDE.CQTEUNITEGESTION*CEQGRAMMEUNITEGESTION)) ELSE 0 END AS sortie_montant_original_ht,
					0 AS stock_quantite_debut,
					0 AS stock_quantite_fin
				FROM prod_hestia.TSTOCKMOUVEMENT
					LEFT JOIN prod_hestia.TDENREEFOURNIS ON CNOIDDENREEFOURNISSEUR = TDENREEFOURNIS.CNOID
					LEFT JOIN prod_hestia.TSTOCKDETAILCOMMANDE ON CNOIDSTOCKDETAILCOMMANDE = TSTOCKDETAILCOMMANDE.CNOID
					LEFT JOIN eco.t_types_mouvements ON CTYPE = t_types_mouvements.code_original
					LEFT JOIN eco.t_gestionnaires ON '0' = t_gestionnaires.code_original
					LEFT JOIN eco.t_sites ON CNOIDMAGASIN = t_sites.code_original
					LEFT JOIN eco.t_lieux ON CNOLIEUSTOCKAGEDFT = t_lieux.code_original
					JOIN eco.t_articles ON CNODENREE = t_articles.code_original
					LEFT JOIN eco.t_fournisseurs ON CNOIDFOURNISSEUR = t_fournisseurs.code_original
					LEFT JOIN eco.t_fournisseurs t_fournisseurs_articles ON TDENREEFOURNIS.CNOIDFOURNISSEUR = t_fournisseurs_articles.code_original
					LEFT JOIN eco.p_lignes_commandes ON CNOIDSTOCKDETAILCOMMANDE = p_lignes_commandes.code_original
					LEFT JOIN eco.t_unites_fonctionnelles ON 'XXX' = t_unites_fonctionnelles.code_original
					LEFT JOIN prod_hestia.TCLASSETVA ON CNOIDTVA = TCLASSETVA.CNOID
				WHERE 	date(LEFT(CDATEHEUREMOUVEMENT,8)) BETWEEN '[ENV_ECO_ANNEEDEBUT]0101' AND now()
				;

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


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

			
				-- cumul ES d'après mouvements
				DROP TABLE IF EXISTS w_stock;
				CREATE TEMP TABLE w_stock AS
				SELECT 
					COALESCE(t_sites.code_original,'') AS site_code_original,
					COALESCE(t_lieux.code_original,'') AS lieu_code_original,
					t_articles.code_original AS article_code_original,
					p_mouvements_articles.article_id,
					t_articles.compte_id,
					site_id,
					lieu_id,
					date(date_trunc('month',date)) AS date_debut,
					date(date_trunc('month',date) + interval '1 month' - interval '1 day') AS date_fin,
					date_part('year',date)*12 + date_part('month',date) AS mois_sequence,
					SUM(entree_quantite) AS entree_quantite,
					SUM(entree_montant) AS entree_montant,
					SUM(sortie_quantite) AS sortie_quantite,
					SUM(sortie_montant) AS sortie_montant,
					0::numeric as pump,
					'0'::text AS ok_pump,
					0::numeric AS stock_quantite_debut,
					0::numeric AS stock_quantite_fin,
					0::numeric AS stock_valeur_debut,
					0::numeric AS stock_valeur_fin
				FROM eco.p_mouvements_articles
				JOIN eco.t_articles ON p_mouvements_articles.article_id = t_articles.oid
				LEFt JOIN eco.t_sites ON p_mouvements_articles.site_id = t_sites.oid
				LEFt JOIN eco.t_lieux ON p_mouvements_articles.lieu_id = t_lieux.oid
				GROUP BY 1,2,3,4,5,6,7,8,9,10
				ORDER BY article_id,date_fin
				;

				-- inventaires
				DROP TABLE IF EXISTS w_STOCK_FIN;
				CREATE TEMP TABLE w_STOCK_FIN AS
				SELECT 
					TDENREEFOURNIS.CNOID,
					CNODENREE,
					CNOLIEUSTOCKAGEDFT,
					TDENREEFOURNIS.CQTEGRSTOCK,
					CPRIXGEF,
					CPRIXDEFAUT,
					CGRAMME,
					CQTEUNITEGESTION
				FROM prod_hestia.TSTOCKPRODUITMAGASIN 
					JOIN prod_hestia.TDENREEFOURNIS on CNOIDPRODUIT = TDENREEFOURNIS.CNOID  
					LEFT JOIN prod_hestia.TSTOCKPRODUITGEF ON TSTOCKPRODUITGEF.CCODEGEF = TDENREEFOURNIS.CCODEGEF
					;
					
				UPDATE w_stock_fin set cprixgef = null;

				INSERT INTO w_stock
				SELECT 
					coalesce(t_sites.code_original,''),
					coalesce(t_lieux.code_original,''),
					t_articles.code_original,
					t_articles.oid,
					t_articles.compte_id,
					COALESCE(t_sites.oid,0),
					coalesce(t_lieux.oid,0),
					date_trunc('month',now()) AS date_debut,
					date_trunc('month',now()) + interval '1 month' - interval '1 day' AS date_fin,
					date_part('year',now())*12 + date_part('month',now()) AS mois_sequence,
					0 AS entree_quantite,
					0 AS entree_montant,
					0 AS sortie_quantite,
					0 AS sortie_montant,
					base.cti_division(SUM(CQTEGRSTOCK*COALESCE(CPRIXGEF,CPRIXDEFAUT)/CGRAMME),SUM(CQTEGRSTOCK)),
					1 AS ok_pump,
					0 AS stock_quantite_debut,
					SUM(CQTEGRSTOCK) AS stock_quantite_fin,
					0 AS stock_valeur_debut,
					SUM(CQTEGRSTOCK *COALESCE(CPRIXGEF,CPRIXDEFAUT)/CGRAMME) AS stock_valeur_fin
				FROM w_STOCK_FIN
					JOIN eco.t_articles ON CNODENREE = t_articles.code_original
					LEFT JOIN eco.t_sites ON 'XXX' = t_sites.code_original
					LEFT JOIN eco.t_lieux ON CNOLIEUSTOCKAGEDFT = t_lieux.code_original
					LEFT JOIN w_stock ON 1=1 AND 
						t_articles.code_original = article_code_original AND
						date_trunc('month',now()) = date_debut AND
						COALESCE(t_lieux.oid,0) = lieu_id
				WHERE date_debut IS NULL 
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11
				;
				
					
				UPDATE w_stock
				SET 
					ok_pump = sub.ok_pump,
					--stock_quantite_debut = stock - entree_quantite + sortie_quantite,
					stock_quantite_fin = stock,
					stock_valeur_debut = (stock - entree_quantite + sortie_quantite) * sub.pump,
					stock_valeur_fin = stock * sub.pump,
					pump = sub.pump
				FROM  (
					SELECT CNODENREE,
					CNOLIEUSTOCKAGEDFT ,
					base.cti_division(SUM(CQTEGRSTOCK * COALESCE(CPRIXGEF,CPRIXDEFAUT)/CGRAMME),SUM(CQTEGRSTOCK)) as pump,
					1 as ok_pump,
					SUM(CQTEGRSTOCK) as stock
					 FROM w_STOCK_FIN
					 GROUP BY 1,2) sub
					 LEFT JOIN eco.t_lieux ON CNOLIEUSTOCKAGEDFT = t_lieux.code_original
				WHERE 1=1 
					AND CNODENREE = article_code_original
					AND COALESCE(t_lieux.oid,0) = lieu_id
					AND date_trunc('month',now()) = date_debut
					AND w_stock.ok_pump = '0'
				;
			
			
				-- calcul quantités
				-- maj décroissante
				UPDATE w_stock
				SET ok_pump = '0.5',
					stock_quantite_fin = subview.stock_quantite_fin
					--,stock_quantite_debut = subview.stock_quantite_debut
				FROM
				(
				SELECT  w_stock.site_id, w_stock.lieu_id, w_stock.article_id, w_stock.mois_sequence, w_stock.date_fin,
					--stock_ref.stock_quantite_fin - SUM(w_stock.entree_quantite - w_stock.sortie_quantite) OVER (PARTITION BY w_stock.site_id, w_stock.lieu_id, w_stock.article_id ORDER BY w_stock.date_fin desc) AS stock_quantite_debut,
					w_stock_ref.stock_quantite_fin - w_stock_ref.entree_quantite + w_stock_ref.sortie_quantite + w_stock.entree_quantite - w_stock.sortie_quantite - SUM(w_stock.entree_quantite 
					- w_stock.sortie_quantite) OVER (PARTITION BY w_stock.site_id, w_stock.lieu_id, w_stock.article_id, next_ref  ORDER BY w_stock.date_fin desc) AS stock_quantite_fin
				FROM w_stock
					JOIN (SELECT 
						site_id,
						lieu_id,
						article_id,
						mois_sequence,
						stock_quantite_fin,
						entree_quantite,
						sortie_quantite,
						COALESCE(lead(mois_sequence,1) OVER (PARTITION BY article_id, site_id, lieu_id ORDER BY mois_sequence),100000000) AS next_ref,
						COALESCE(lag(mois_sequence,1) OVER (PARTITION BY article_id, site_id, lieu_id ORDER BY mois_sequence),-100000000) AS last_ref
						FROM w_stock WHERE ok_pump != '0'
					) w_stock_ref  ON  1=1
						AND w_stock.lieu_id = w_stock_ref.lieu_id
						AND w_stock.site_id = w_stock_ref.site_id
						AND w_stock.article_id = w_stock_ref.article_id
						AND w_stock.mois_sequence BETWEEN last_ref ANd w_stock_ref.mois_sequence
				ORDER BY w_stock.date_fin
				) subview
				WHERE ok_pump = '0' AND
					w_stock.article_id = subview.article_id AND
					w_stock.site_id = subview.site_id AND
					w_stock.lieu_id = subview.lieu_id AND
					w_stock.mois_sequence = subview.mois_sequence
				;
				
				-- maj croissante des quantités
				UPDATE w_stock
				SET ok_pump = '0.5',
					stock_quantite_fin = subview.stock_quantite_fin--,
					--stock_quantite_debut = subview.stock_quantite_debut
				FROM
				(
				SELECT  w_stock.site_id, w_stock.lieu_id, w_stock.article_id, w_stock.mois_sequence, w_stock.date_fin,
					w_stock_ref.stock_quantite_fin + SUM(w_stock.entree_quantite - w_stock.sortie_quantite) OVER (PARTITION BY w_stock.site_id, w_stock.lieu_id, w_stock.article_id, last_ref ORDER BY w_stock.date_fin asc) AS stock_quantite_fin--,
					--stock_ref.stock_quantite_fin - w_stock.entree_quantite + w_stock.sortie_quantite + SUM(w_stock.entree_quantite - w_stock.sortie_quantite) OVER (PARTITION BY w_stock.site_id, w_stock.lieu_id, w_stock.article_id ORDER BY w_stock.date_fin asc) AS stock_quantite_debut
				FROM w_stock JOIN 
					(SELECT 
						site_id,
						lieu_id,
						article_id,
						mois_sequence,
						stock_quantite_fin,
						COALESCE(lead(mois_sequence,1) OVER (PARTITION BY article_id, site_id, lieu_id ORDER BY mois_sequence),100000000) AS next_ref,
						COALESCE(lag(mois_sequence,1) OVER (PARTITION BY article_id, site_id, lieu_id ORDER BY mois_sequence),-100000000) AS last_ref
						FROM w_stock WHERE ok_pump != '0'
					) w_stock_ref ON  1=1
						AND w_stock.lieu_id = w_stock_ref.lieu_id
						AND w_stock.site_id = w_stock_ref.site_id
						AND w_stock.article_id = w_stock_ref.article_id
						AND w_stock.mois_sequence BETWEEN w_stock_ref.mois_sequence AND next_ref
						AND w_stock_ref.mois_sequence < w_stock.mois_sequence
						
				ORDER BY w_stock.date_fin
				) subview
				WHERE ok_pump = '0' AND
					w_stock.article_id = subview.article_id AND
					w_stock.site_id = subview.site_id AND
					w_stock.lieu_id = subview.lieu_id AND
					w_stock.mois_sequence = subview.mois_sequence
				;

			/*	-- forcage pump avec dernières réceptions
				DROP TABLE IF EXISTS w_rec;
				CREATE TEMP TABLE w_rec AS 
				SELECT 
				'1' as ok_pump,
				ITMMVT.ITMREF_0,ITMMVT.STOFCY_0,X_PMPPER_0,
				 date_trunc('month',X_DATPMP_0) AS date_ref
				FROM prod_sagex3.ITMMVT
				;

                 -- Prix distri historisé si pas de pump
                DROP SEQUENCE IF EXISTS w_prix_distri_seq;
                CREATE TEMP SEQUENCE w_prix_distri_seq;
                
                DROP TABLE IF EXISTS w_prix_distri;
                CREATE TEMP TABLE w_prix_distri AS
                SELECT subview.*, '20991231'::date AS date_fin, nextval('w_prix_distri_seq'::regclass) AS prix_sequence
                FROM
                (
                SELECT STOFCY_0,ITMREF_0, date(date_trunc('month',IPTDAT_0)) AS date_debut,
                    (MAX(ARRAY[IPTDAT_0::text,PRIORD_0::text]))[2]::numeric AS prix_distri_ht,
                    0 AS prix_distri_ttc
                FROM prod_sagex3.STOJOU
                WHERE QTYSTU_0 < 0
					AND PRINAT_0 = 5  AND TRSTYP_0 = 4
                GROUP BY 1,2,3
                ORDER BY 1,2,3
                ) subview;

                UPDATE w_prix_distri
                SET date_fin = w_prix_distri_next.date_debut - interval '1 day'
                FROM w_prix_distri w_prix_distri_next
                WHERE w_prix_distri.ITMREF_0 = w_prix_distri_next.ITMREF_0 AND
			w_prix_distri.STOFCY_0 = w_prix_distri_next.STOFCY_0 AND
                    w_prix_distri.prix_sequence = w_prix_distri_next.prix_sequence - 1
                ;
                
                CREATE INDEX w_prix_distri_i1 ON w_prix_distri USING btree (ITMREF_0)
                ;	
                 CREATE INDEX w_prix_distri_i2 ON w_prix_distri USING btree (STOFCY_0)
                ;

				UPDATE w_stock
				SET 
				ok_pump = '1',
				pump = X_PMPPER_0,
				stock_valeur_fin = stock_quantite_fin * X_PMPPER_0,
				stock_valeur_debut = stock_quantite_debut * X_PMPPER_0
				FROM w_rec
				WHERE 1=1 AND
					ITMREF_0 = article_code_original AND
					STOFCY_0 = site_code_original AND
					date_ref <= date_debut AND
					w_stock.ok_pump = '0.5' AND
					X_PMPPER_0 != 0
				;	


				UPDATE w_STOCK SET
					pump = prix_distri_ht,
					stock_valeur_fin = w_STOCK.stock_quantite_fin * prix_distri_ht,
					ok_pump = 1
				FROM w_prix_distri
				WHERE w_prix_distri.ITMREF_0 = w_STOCK.article_code_original AND
				w_prix_distri.STOFCY_0 = w_STOCK.site_code_original AND
					w_STOCK.date_fin BETWEEN  w_prix_distri.date_debut AND w_prix_distri.date_fin
					AND ok_pump != '1'
					AND prix_distri_ht != 0
                ;


				
                 -- Prix reception historisé si toujours pas de pump
                DROP SEQUENCE IF EXISTS w_prix_rec_seq;
                CREATE TEMP SEQUENCE w_prix_rec_seq;
                
                DROP TABLE IF EXISTS w_prix_rec;
                CREATE TEMP TABLE w_prix_rec AS
                SELECT subview.*, '20991231'::date AS date_fin, nextval('w_prix_rec_seq'::regclass) AS prix_sequence
                FROM
                (
                SELECT STOFCY_0,ITMREF_0, date(date_trunc('month',IPTDAT_0)) AS date_debut,
                    (MAX(ARRAY[IPTDAT_0::text,PRIORD_0::text]))[2]::numeric AS prix_distri_ht,
                    0 AS prix_distri_ttc
                FROM prod_sagex3.STOJOU
                WHERE QTYSTU_0 > 0
					AND PRINAT_0 = 8
                GROUP BY 1,2,3
                ORDER BY 1,2,3
                ) subview;

                UPDATE w_prix_rec
                SET date_fin = w_prix_rec_next.date_debut - interval '1 day'
                FROM w_prix_rec w_prix_rec_next
                WHERE w_prix_rec.ITMREF_0 = w_prix_rec_next.ITMREF_0 AND
                w_prix_rec.STOFCY_0 = w_prix_rec_next.STOFCY_0 AND
                    w_prix_rec.prix_sequence = w_prix_rec_next.prix_sequence - 1
                ;
                
                CREATE INDEX w_prix_rec_i1 ON w_prix_rec USING btree (ITMREF_0)
                ;	
              
				CREATE INDEX w_prix_rec_i2 ON w_prix_rec USING btree (STOFCY_0)
                ;
				
				UPDATE w_STOCK SET
					pump = prix_distri_ht,
					stock_valeur_fin = w_STOCK.stock_quantite_fin * prix_distri_ht
				FROM w_prix_rec
				WHERE w_prix_rec.ITMREF_0 = w_STOCK.article_code_original AND
					w_prix_rec.STOFCY_0 = w_STOCK.site_code_original AND
					w_STOCK.date_fin BETWEEN  w_prix_rec.date_debut AND w_prix_rec.date_fin
					AND ok_pump != '1'
                ;

				UPDATE w_STOCK SET
					pump = prix_distri_ht,
					stock_valeur_fin = w_STOCK.stock_quantite_fin * prix_distri_ht
				FROM w_prix_rec
				WHERE w_prix_rec.ITMREF_0 = w_STOCK.article_code_original AND
					w_prix_rec.STOFCY_0 = w_STOCK.site_code_original AND
					w_STOCK.date_fin BETWEEN  w_prix_rec.date_debut AND w_prix_rec.date_fin
					AND ok_pump != '1'
                ;

*/

				-- ajout des stocks mensuels manquants
				SELECT base.cti_execute('
				INSERT INTO w_stock
				SELECT 
					w_stock.site_code_original,
					w_stock.lieu_code_original,
					w_stock.article_code_original,
					w_stock.article_id,
					w_stock.compte_id,
					w_stock.site_id,
					w_stock.lieu_id,
					date(date_trunc(''month'',w_stock.date_debut) + interval ''1 month'') AS date_debut,
					date(date_trunc(''month'',w_stock.date_debut) + interval ''2 month'' - interval ''1 day'') AS date_fin,
					w_stock.mois_sequence + 1,
					0 AS entree_quantite,
					0 AS entree_montant,
					0 AS sortie_quantite,
					0 AS sortie_montant,
					w_stock.pump,
					w_stock.ok_pump,
					w_stock.stock_quantite_debut,
					w_stock.stock_quantite_fin,
					w_stock.stock_valeur_debut,
					w_stock.stock_valeur_fin
				FROM w_stock
				LEFT JOIN w_stock w_stock_next ON
					w_stock.site_id = w_stock_next.site_id AND
					w_stock.lieu_id = w_stock_next.lieu_id AND
					w_stock.article_code_original = w_stock_next.article_code_original AND
					w_stock.mois_sequence = w_stock_next.mois_sequence - 1
				WHERE 
					w_stock_next.mois_sequence IS NULL AND
					w_stock.date_debut <= date(now()) AND
					w_stock.date_fin >= ''[ENV_ECO_ANNEEDEBUT]-01-01'' AND
					w_stock.stock_quantite_fin != 0
				',200)
				;

                -- mise à jour des débuts
				UPDATE w_STOCK SET
					stock_quantite_debut = stock_quantite_fin - entree_quantite + sortie_quantite,
					stock_valeur_debut = stock_valeur_fin - entree_montant + sortie_montant
				;

				-- insertion 
				TRUNCATE eco.p_stock
				;
				
				INSERT INTO eco.p_stock
					(
					article_id,
					compte_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_fin
					)
				SELECT 
					article_id,
					compte_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_fin
				FROM w_stock
				WHERE stock_quantite_debut != 0 OR stock_quantite_fin != 0
				;
				
				SELECT base.cti_enable_index('eco', 'i_stock_1');
				SELECT base.cti_enable_index('eco', 'i_stock_2');
				SELECT base.cti_enable_index('eco', 'i_stock_3');
				SELECT base.cti_enable_index('eco', 'i_stock_4');
				SELECT base.cti_enable_index('eco', 'i_stock_5');
			]]></sqlcmd>
		</NODE>
		
	
		<NODE label="Factures">
			<sqlcmd><![CDATA[
			
			
			]]></sqlcmd>
		</NODE>
	</NODE>
	<NODE name="POST" label="POST-TRAITEMENTS">
		
	</NODE>
	
</ROOT>
