<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	<NODE name="INIT" label="INITIALISATIONS COMMUNES">
		
		<NODE label="Initialisation tables">
			<sqlcmd><![CDATA[
			
				SELECT eco.cti_initialize_tables();
                
                -- Suppression des codes NOW et NOWCLOTURE.
                -- De cette façon, si l'import du prestataire ne les ajoute pas, le SHARE le fera.
                DELETE FROM eco.t_divers
                WHERE code IN ('NOW', 'NOWCLOTURE')
                ;
		
			]]></sqlcmd>
		</NODE>
        
        <NODE label="RAZ">
			<sqlcmd><![CDATA[
			
				TRUNCATE eco.p_commandes;
                TRUNCATE eco.p_lignes_commandes;
                TRUNCATE eco.p_mouvements_articles;
                TRUNCATE eco.p_stock;
                TRUNCATE eco.p_chiffrier_production;
                TRUNCATE eco.p_facture;
                TRUNCATE eco.p_lignes_facture;
		
			]]></sqlcmd>
		</NODE>
		
	</NODE>
	
    <NODE name="POST" label="POST-TRAITEMENTS COMMUNS">
    
        <NODE label="Sens forcé">
			<sqlcmd><![CDATA[
					
				UPDATE eco.p_mouvements_articles SET
					sens_mouvement = 'S',
					sortie_montant = 0 - entree_montant,
					sortie_montant_ht = 0 - entree_montant_ht,
					sortie_quantite = 0 - entree_quantite,
					entree_montant = 0,
					entree_montant_ht = 0,
					entree_quantite = 0
				FROM eco.t_types_mouvements
				WHERE type_mouvement_id = t_types_mouvements.oid AND
					t_types_mouvements.sens_force = 'S' AND
					p_mouvements_articles.sens_mouvement <> 'S'
				;
                
                UPDATE eco.p_mouvements_articles SET
					sens_mouvement = 'E',
					entree_montant = 0 - sortie_montant,
					entree_montant_ht = 0 - sortie_montant_ht,
					entree_quantite = 0 -sortie_quantite,
					sortie_montant = 0,
					sortie_montant_ht = 0,
					sortie_quantite = 0
				FROM eco.t_types_mouvements
				WHERE type_mouvement_id = t_types_mouvements.oid AND
					t_types_mouvements.sens_force = 'E' AND
					p_mouvements_articles.sens_mouvement <> 'E'
				;
	
			
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Mouvements avec montants farfelus">
			<sqlcmd><![CDATA[
			
				INSERT INTO eco.t_types_mouvements(code_original, code, texte, texte_court)
				SELECT '*CTIERR','ERREUR','Erreur','Erreur'
				WHERE '*CTIERR' NOT IN (SELECT code_original FROM eco.t_types_mouvements WHERE code_original IS NOT NULL);

					

				UPDATE eco.p_mouvements_articles
				SET type_mouvement_id = t_types_mouvements.oid
				FROM eco.t_types_mouvements,
				(SELECT *
				FROM
				(SELECT base.cti_division(abs(entree_montant)+abs(sortie_montant), SUM(abs(entree_montant)+abs(sortie_montant)) OVER (PARTITION BY date_trunc('month',date))) AS pourcentage_ligne, 
					 oid
					
				FROM eco.p_mouvements_articles
				) subview
				WHERE pourcentage_ligne > 0.15
				) subview
				WHERE p_mouvements_articles.oid = subview.oid AND
					t_types_mouvements.code_original = '*CTIERR'
				;
	
			]]></sqlcmd>
		</NODE>
    
	
		<NODE label="Lignes commandes sans code">
			<sqlcmd><![CDATA[
				-- remise à jour de la sequence des oids de t_articles et t_article_fournisseur
				SELECT setval('eco.s_articles'::regClass,(SELECT max(oid) + 1 FROM eco.t_articles));	
				SELECT setval('eco.s_article_fournisseur'::regClass,(SELECT max(oid) + 1 FROM eco.t_article_fournisseur));	
				
				INSERT INTO eco.t_articles(code_original, code, texte, texte_court)
				SELECT '*F'||t_fournisseurs.code,'F'||t_fournisseurs.code, 'Articles sans code de ' || t_fournisseurs.texte, substr('Art. sans code de ' || t_fournisseurs.texte_court,1,50)
				FROM eco.p_lignes_commandes
				JOIN eco.p_commandes ON commande_id = p_commandes.oid
				JOIN eco.t_fournisseurs On fournisseur_id = t_fournisseurs.oid
				LEFT JOIN eco.t_articles ON t_articles.code_original = ('*F'||t_fournisseurs.code)
				WHERE article_id = 0 AND t_articles.code_original IS NULL
				GROUP BY 1,2,3,4
				ORDER BY 1
				;
				
				INSERT INTO eco.t_article_fournisseur(
				    code_original, 
				    article_id, 
				    fournisseur_id, 
				    code_reference_fournisseur, 
				    texte_reference_fournisseur, 
				    code_cip, 
				    code_cahpp, 
				    unite_approvisionnement_id, 
				    nombre_conditionnement_approvisionnement, 
				    prix_unitaire_en_cours, 
				    est_fournisseur_principal, 
				    fournisseur_distributeur_id, 
				    ucd_id, 
				    lpp_id, 
				    code_ucd13
				)
				SELECT 
				    '*F'||t_fournisseurs.code AS code_original, 
				    t_articles.oid AS article_id, 
				    p_commandes.fournisseur_id, 
				    ''::text AS code_reference_fournisseur, 
				    'Articles sans code de ' || t_fournisseurs.texte AS texte_reference_fournisseur, 
				    ''::text AS code_cip, 
				    ''::text AS code_cahpp, 
				    0::bigint AS unite_approvisionnement_id, 
				    0::numeric AS nombre_conditionnement_approvisionnement, 
				    0::numeric AS prix_unitaire_en_cours, 
				    '1'::text AS est_fournisseur_principal, 
				    p_commandes.fournisseur_id AS fournisseur_distributeur_id, 
				    0::bigint AS ucd_id, 
				    0::bigint AS lpp_id, 
				    ''::text AS code_ucd13
				FROM eco.p_lignes_commandes
				JOIN eco.p_commandes ON commande_id = p_commandes.oid
				JOIN eco.t_fournisseurs On fournisseur_id = t_fournisseurs.oid
				JOIN eco.t_articles ON t_articles.code_original = ('*F'||t_fournisseurs.code)
				LEFT JOIN eco.t_article_fournisseur ON t_article_fournisseur.code_original = ('*F'||t_fournisseurs.code)
				WHERE p_lignes_commandes.article_id = 0 AND t_article_fournisseur.code_original IS NULL
				GROUP BY 1,2,3,5,12
				ORDER BY 1
				;

				UPDATE eco.p_lignes_commandes
				SET article_id = t_articles.oid
				FROM eco.p_commandes 
				JOIN eco.t_fournisseurs On fournisseur_id = t_fournisseurs.oid
				JOIN eco.t_articles ON t_articles.code_original = ('*F'||t_fournisseurs.code)
				WHERE p_lignes_commandes.article_id = 0 AND
					commande_id = p_commandes.oid
				;
                
                UPDATE eco.p_lignes_commandes
                SET compte_id = 0
                WHERE compte_id IS NULL
                ;
			
			]]></sqlcmd>
		</NODE>
	
		<NODE label="Lignes commandes sans date de réception">
			<sqlcmd><![CDATA[
				 
                UPDATE eco.p_lignes_commandes
				SET date_reception = CASE WHEN etat_reception = 'N' THEN '2099-12-31' ELSE p_commandes.date_reception END
				FROM eco.p_commandes
                WHERE commande_id = p_commandes.oid AND p_lignes_commandes.date_reception IS NULL
                ;
			]]></sqlcmd>
		</NODE>
	

		<NODE label="Forcage des commandes soldées à livrés en totalité">
			<condition><![CDATA[
				
				SELECT count(*) > 0 
				FROM eco.t_divers
				WHERE 1=1
					AND code = 'FORCE_LIV_LIQ'
					AND valeur = '1'
            ;
				
			]]></condition>
				<sqlcmd><![CDATA[
				
				UPDATE eco.p_commandes
                SET 
					etat_reception = 'T'
                WHERE 
					etat_liquidation ='T'
                ;	
				UPDATE eco.p_lignes_commandes
                SET 
					etat_livraison = 'T'
                FROM eco.p_commandes
				WHERE 
					p_commandes.etat_liquidation ='T' AND
					commande_id = p_commandes.oid
                ;
			]]></sqlcmd>
		</NODE>
	
		<NODE label="Corrections lignes de commandes sans montant">
			<sqlcmd><![CDATA[
				-- montant dans les mouvements
				UPDATE eco.p_lignes_commandes 
				SET
					prix_unitaire_approvisionnement = entree_montant_ht/entree_quantite,
					montant_commande_ht = quantite_approvisionnement * entree_montant_ht/entree_quantite,
					montant_commande_ttc = quantite_approvisionnement * entree_montant/entree_quantite,
					montant_commande_tva = quantite_approvisionnement * (entree_montant - entree_montant_ht)/entree_quantite
				FROM eco.p_mouvements_articles
				WHERE 
					p_mouvements_articles.commande_id = p_lignes_commandes.commande_id
					AND (p_mouvements_articles.ligne_commande = p_lignes_commandes.ligne_commande OR p_mouvements_articles.ligne_commande = 0)
					AND p_mouvements_articles.article_id = p_lignes_commandes.article_id
					AND montant_commande_ht = 0
					AND quantite_livraison_approvisionnement != 0
					AND entree_montant != 0 
					AND entree_quantite != 0
					AND etat_liquidation <> 'T'
				;
				
				-- commandes soldées non livrées
				UPDATE eco.p_lignes_commandes SET
					quantite_approvisionnement = 0,
					quantite_stockage = 0
				WHERE 1=1
					AND montant_commande_ht = 0
					AND etat_liquidation = 'T'
					AND quantite_livraison_approvisionnement = 0
					AND etat_livraison = 'N'
					AND (SELECT valeur = '1' FROM eco.t_divers WHERE code = 'FORCE_QTE_APR_STK')
				;

			]]></sqlcmd>
		</NODE>

		<NODE label="Corrections mouvements sans fournisseur">
			<sqlcmd><![CDATA[
				-- Fournisseur principal
				UPDATE eco.t_article_fournisseur
				SET est_fournisseur_principal = CASE WHEN fournisseur_id = fournisseur_principal_id THEN '1' ELSE '0' END
				FROM
				(
				SELECT article_id,
					(MAX(ARRAY[date_derniere_commande::text,fournisseur_id::text]))[2]::bigint AS fournisseur_principal_id
				FROM
				(
				SELECT t_articles.oid AS article_id,
					COALESCE(fournisseur_id,0) AS fournisseur_id,
					count(*) AS nb,
					MIN(date_commande) AS date_premiere_commande,
					MAX(date_commande) AS date_derniere_commande
				FROM eco.t_articles
				JOIN eco.p_lignes_commandes ON p_lignes_commandes.article_id = t_articles.oid
				JOIN eco.p_commandes ON p_commandes.oid = p_lignes_commandes.commande_id
				GROUP BY 1,2
				ORDER BY 1
				) subview
				GROUP BY 1
				) subview
				WHERE t_article_fournisseur.article_id = subview.article_id AND
					est_fournisseur_principal IS DISTINCT FROM CASE WHEN fournisseur_id = fournisseur_principal_id THEN '1' ELSE '0' END
				;
				
				UPDATE eco.t_articles
				SET fournisseur_principal_id = t_article_fournisseur.fournisseur_id
				FROM eco.t_article_fournisseur
				WHERE t_articles.oid = t_article_fournisseur.article_id AND
					t_article_fournisseur.est_fournisseur_principal = '1' AND
					t_articles.fournisseur_principal_id IS DISTINCT FROM t_article_fournisseur.fournisseur_id
				;
			
			
		        UPDATE eco.p_mouvements_articles
				SET fournisseur_id = subview.fournisseur_id
				FROM
				(
				SELECT article_id, 
					MAX(fournisseur_id) AS fournisseur_id
				FROM eco.p_mouvements_articles
				WHERE fournisseur_id <> 0
				GROUP BY 1
				HAVING count(DISTINCT fournisseur_id) = 1
				) subview
				WHERE p_mouvements_articles.article_id = subview.article_id AND
					p_mouvements_articles.fournisseur_id = 0
				;


				UPDATE eco.p_mouvements_articles
				SET fournisseur_id = subview.fournisseur_id
				FROM
				(
				SELECT article_id, 
					MAX(fournisseur_id) AS fournisseur_id
				FROM eco.t_article_fournisseur
				WHERE fournisseur_id <> 0
				GROUP BY 1
				HAVING count(DISTINCT fournisseur_id) = 1
				) subview
				WHERE p_mouvements_articles.article_id = subview.article_id AND
					p_mouvements_articles.fournisseur_id = 0
				;

				UPDATE eco.p_mouvements_articles
				SET 
					fournisseur_id = sub.fournisseur_id
					
				FROM 
					(SELECT article_id, fournisseur_id, date, date(COALESCE(lead(date) OVER (PARTITION BY article_id ORDER BY date),'2099-12-31')- interval '1 day') AS next_date
					FROM eco.p_mouvements_articles
					WHERE fournisseur_id != 0
					AND entree_quantite > 0
					) sub
				WHERE
					p_mouvements_articles.fournisseur_id = 0 AND
					p_mouvements_articles.article_id = sub.article_id AND
					p_mouvements_articles.date between sub.date AND next_date
				;

				UPDATE eco.p_mouvements_articles
				SET fournisseur_id = subview.fournisseur_id
				FROM
				(
				SELECT article_id, MIN(date) AS date_min, (MIN(Array[date::text,fournisseur_id::text]))[2]::bigint AS fournisseur_id
				FROM eco.p_mouvements_articles
				WHERE fournisseur_id <> 0
				GROUP BY 1
				) subview
				WHERE p_mouvements_articles.article_id =subview.article_id AND
					p_mouvements_articles.fournisseur_id = 0 AND
					p_mouvements_articles.date < date_min
				;

				UPDATE eco.p_mouvements_articles
				SET fournisseur_id = subview.fournisseur_id
				FROM
				(
				SELECT article_id, 
					MAX(fournisseur_id) AS fournisseur_id
				FROM eco.t_article_fournisseur
				WHERE fournisseur_id <> 0 AND
					est_fournisseur_principal = '1'
				GROUP BY 1
				HAVING count(DISTINCT fournisseur_id) = 1
				) subview
				WHERE p_mouvements_articles.article_id = subview.article_id AND
					p_mouvements_articles.fournisseur_id = 0
				;
				
				UPDATE eco.t_articles SET ref_fournisseur_id[1] = sub.fournisseur_id
				FROM (
					SELECT article_id, MAX(fournisseur_id) AS fournisseur_id
					FROM eco.p_commandes
						LEFT JOIN eco.p_lignes_commandes on commande_id = p_commandes.oid
						WHERE fournisseur_id != 0
					GROUP BY 1
					HAVING max(fournisseur_id) > 0
				) sub
				WHERE
					oid = sub.article_id AND 
					ref_fournisseur_id[1] = 0 
				;
				
				UPDATE eco.p_mouvements_articles
				SET 
					fournisseur_id = t_articles.ref_fournisseur_id[1]
				FROM 
					eco.t_articles
				WHERE
					p_mouvements_articles.article_id = t_articles.oid AND 
					fournisseur_id = 0 AND
					ref_fournisseur_id[1] != 0 AND
					ref_fournisseur_id[1] IS NOT NULL
				;
			]]></sqlcmd>
		</NODE>
	
		<NODE label="Corrections articles LPP et UCD">
			<sqlcmd><![CDATA[
			
				UPDATE eco.t_articles SET ucd_id = p_lignes_commandes.ucd_id
				FROM eco.p_lignes_commandes 
				WHERE article_id = t_articles.oid AND t_articles.ucd_id = 0 AND p_lignes_commandes.ucd_id != 0;

				UPDATE eco.t_articles SET ucd_id = p_mouvements_articles.ucd_id
				FROM eco.p_mouvements_articles 
				WHERE article_id = t_articles.oid AND t_articles.ucd_id = 0 AND p_mouvements_articles.ucd_id != 0;;

				UPDATE eco.t_articles SET lpp_id = p_lignes_commandes.lpp_id
				FROM eco.p_lignes_commandes 
				WHERE article_id = t_articles.oid AND t_articles.lpp_id = 0 AND p_lignes_commandes.lpp_id != 0;

				UPDATE eco.t_articles SET lpp_id = p_mouvements_articles.lpp_id
				FROM eco.p_mouvements_articles 
				WHERE article_id = t_articles.oid AND t_articles.lpp_id = 0 AND p_mouvements_articles.lpp_id != 0;;
	
			]]></sqlcmd>
		</NODE>
        
		<NODE label="Mise a jour des codes articles fournisseurs non alimentes">
			<sqlcmd><![CDATA[
				UPDATE eco.t_articles 
				SET 
					ref_fournisseur_code = ref_codes,
					ref_fournisseur_texte = ref_textes
				FROM
				(
				SELECT 
					oid, 
					array_agg(ref_id) as ref_ids,
					array_agg(ref_code) as ref_codes,
					array_agg(ref_texte) as ref_textes
					FROM (
						SELECT 
							oid,
							ref_id, 
							regexp_replace(ref_texte,'.*\(([^\)]*)\)$',E'\\1') as ref_code,
							regexp_replace(ref_texte,'(.*)\([^\)]*\)$',E'\\1') as ref_texte
						FROM (
							SELECT 
								oid, 
								unnest(ref_fournisseur_id) as ref_id,
								unnest(ref_fournisseur_texte) as ref_texte
							FROM eco.t_articles
							WHERE ref_fournisseur_code[1] = '' OR ref_fournisseur_code[1] IS NULL
						) sub
					) sub2
					GROUP BY 1
				) sub3
				WHERE
					sub3.oid = t_articles.oid
				;
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Corrections code TVA">
			<sqlcmd><![CDATA[
			
				UPDATE eco.p_lignes_commandes
				SET taux_tva = 20
				FROM eco.p_commandes
				where commande_id = p_commandes.oid AND
					taux_tva = 19.6 AND
					date_commande >= '20140101'
				;
				
				UPDATE eco.t_articles
				SET taux_tva_en_cours = subview.taux_tva
				FROM
				(
				SELECT article_id, (MAX(Array[date_commande::text,taux_tva::text]))[2]::numeric AS taux_tva
				FROM eco.p_lignes_commandes 
				JOIN eco.p_commandes ON commande_id = p_commandes.oid
				WHERE taux_tva <> 0
				GROUP BY 1
				) subview
				WHERE t_articles.oid = subview.article_id AND
					t_articles.taux_tva_en_cours <> subview.taux_tva
				;
			
				UPDATE eco.t_articles
				SET taux_tva_en_cours = 20
				WHERE taux_tva_en_cours = 19.6
				;
	
			]]></sqlcmd>
		</NODE>
		
		<NODE label="ATC selon association UCD et LPP">
			<condition><![CDATA[
				SELECT count(*) > 0 
				FROM eco.t_divers
				WHERE 1=1
					AND code = 'FORCE_ATC'
					AND valeur = '1'
            ;
			]]></condition>
			<sqlcmd><![CDATA[
				
				UPDATE eco.t_articles
				SET classification_atc_id = t_classification_atc.oid
				FROM base.t_ucd b_ucd JOIN pmsi.t_ucd p_ucd ON b_ucd.code = p_ucd.code
					JOIN eco.t_classification_atc ON p_ucd.atc_code = t_classification_atc.code
				WHERE t_articles.ucd_id = b_ucd.oid AND
					ucd_id <> 0 AND
					t_articles.classification_atc_id <> t_classification_atc.oid;
					
				UPDATE eco.t_articles
				SET classification_atc_id = t_classification_atc.oid
				FROM base.t_lpp b_lpp JOIN pmsi.t_lpp p_lpp ON b_lpp.code = p_lpp.code
					JOIN (
						SELECT
							t_classes_sections.code as atc_code,
							to_id
						FROM 
							pmsi.t_classes 
							JOIN pmsi.t_classes_sections ON classe_id = t_classes.oid
							JOIN pmsi.t_classes_sections_elements ON section_id = t_classes_sections.oid
						WHERE t_classes.sequence = 14
						) atc ON to_id = b_lpp.oid
					JOIN eco.t_classification_atc ON atc_code = t_classification_atc.code
				WHERE t_articles.lpp_id = b_lpp.oid AND
					lpp_id <> 0 AND
					t_articles.classification_atc_id <> t_classification_atc.oid
				;
				
				UPDATE eco.t_articles SET
					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

				FROM eco.t_classification_atc
					
				WHERE classification_atc_id = t_classification_atc.oid
					AND (
						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
					);
			
			]]></sqlcmd>
		</NODE>
        
		
		<NODE label="Compléments articles">
			<sqlcmd><![CDATA[
			
			-- Type selon compte
			UPDATE eco.t_articles
			SET type_id = subview.type_force_id,
				type_code = subview.type_force_code,
				type_texte = subview.type_force_texte,
				type_section_id = subview.type_force_section_id,
				type_section_code = subview.type_force_section_code,
				type_section_texte = subview.type_force_section_texte
			FROM
			(
			SELECT t_articles.oid, 
				MAX(t_types_articles.oid) AS type_force_id,
				MAX(t_types_articles.code) AS type_force_code,
				MAX(t_types_articles.texte_court) AS type_force_texte,
				MAX(t_types_articles.section_id) AS type_force_section_id,
				MAX(t_types_articles.section_code) AS type_force_section_code,
				MAX(t_types_articles.section_texte) AS type_force_section_texte
			FROM eco.t_articles
			JOIN eco.t_compte ON t_compte.oid = compte_id AND compte_id > 0
			JOIN eco.t_types_articles ON 
				t_types_articles.defaut_selon = 'CPT' AND
				t_compte.code LIKE ANY(string_to_array(t_types_articles.defaut_selon_codes,','))
			GROUP BY 1
			) subview
			WHERE t_articles.oid = subview.oid AND
				type_id = 0 AND
				(
				type_id IS DISTINCT FROM subview.type_force_id OR
				type_code IS DISTINCT FROM subview.type_force_code OR
				type_texte IS DISTINCT FROM subview.type_force_texte OR
				type_section_id IS DISTINCT FROM subview.type_force_section_id OR
				type_section_code IS DISTINCT FROM subview.type_force_section_code OR
				type_section_texte IS DISTINCT FROM subview.type_force_section_texte
				)
			;
			
			-- Type selon catégorie
			UPDATE eco.t_articles
			SET type_id = subview.type_force_id,
				type_code = subview.type_force_code,
				type_texte = subview.type_force_texte,
				type_section_id = subview.type_force_section_id,
				type_section_code = subview.type_force_section_code,
				type_section_texte = subview.type_force_section_texte
			FROM
			(
			SELECT t_articles.oid, 
				MAX(t_types_articles.oid) AS type_force_id,
				MAX(t_types_articles.code) AS type_force_code,
				MAX(t_types_articles.texte_court) AS type_force_texte,
				MAX(t_types_articles.section_id) AS type_force_section_id,
				MAX(t_types_articles.section_code) AS type_force_section_code,
				MAX(t_types_articles.section_texte) AS type_force_section_texte
			FROM eco.t_articles
			JOIN eco.t_categories_articles ON t_categories_articles.oid = categorie_id AND categorie_id > 0
			JOIN eco.t_types_articles ON 
				t_types_articles.defaut_selon = 'CAT' AND
				t_categories_articles.code LIKE ANY(string_to_array(t_types_articles.defaut_selon_codes,','))
			GROUP BY 1
			) subview
			WHERE t_articles.oid = subview.oid AND
				type_id = 0 AND
				(
				type_id IS DISTINCT FROM subview.type_force_id OR
				type_code IS DISTINCT FROM subview.type_force_code OR
				type_texte IS DISTINCT FROM subview.type_force_texte OR
				type_section_id IS DISTINCT FROM subview.type_force_section_id OR
				type_section_code IS DISTINCT FROM subview.type_force_section_code OR
				type_section_texte IS DISTINCT FROM subview.type_force_section_texte
				)
			;

			
				
			UPDATE eco.p_lignes_commandes
			SET fournisseur_distributeur_id = t_article_fournisseur.fournisseur_distributeur_id
			FROM eco.p_commandes,
				eco.t_article_fournisseur
			WHERE p_lignes_commandes.commande_id = p_commandes.oid AND
				t_article_fournisseur.article_id = p_lignes_commandes.article_id AND
				t_article_fournisseur.fournisseur_id = p_commandes.fournisseur_id AND
				p_lignes_commandes.fournisseur_distributeur_id IS DISTINCT from t_article_fournisseur.fournisseur_distributeur_id AND
					t_article_fournisseur.fournisseur_distributeur_id <> 0
			;
			
			UPDATE eco.p_commandes
			SET fournisseur_distributeur_id = fournisseur_id 
			WHERE fournisseur_distributeur_id = 0
			;

			UPDATE eco.p_commandes
			SET fournisseur_distributeur_id = subview.fournisseur_distributeur_id
			FROM
			(
			SELECT commande_id,
				(MAX(ARRAY[to_char(subview.montant_commande_ht,'FM000000000000000000.00'),subview.fournisseur_distributeur_id::text]))[2]::bigint AS fournisseur_distributeur_id
			FROM
			(
			SELECT commande_id, p_lignes_commandes.fournisseur_distributeur_id, SUM(p_lignes_commandes.montant_commande_ht) AS montant_commande_ht
			FROM eco.p_lignes_commandes
			JOIN eco.p_commandes ON p_lignes_commandes.commande_id = p_commandes.oid
			group by 1,2
			) subview
			GROUP BY 1
			) subview
			WHERE p_commandes.oid = subview.commande_id AND
				p_commandes.fournisseur_distributeur_id IS DISTINCT FROM subview.fournisseur_distributeur_id
			;
			
			INSERT INTO base.t_ucd (code, texte, texte_court)
			SELECT code_reference_fournisseur,
				MAX(texte_reference_fournisseur),
				MAX(texte_reference_fournisseur)
			FROM eco.t_article_fournisseur
			WHERE length(code_reference_fournisseur) = 7 AND
				code_reference_fournisseur BETWEEN '9000000' AND '9899999' AND
				base.cti_to_number(code_reference_fournisseur)::text = code_reference_fournisseur AND
				code_reference_fournisseur NOT IN (SELECT code FROM base.t_ucd)
			GROUP BY 1;

			UPDATE eco.t_article_fournisseur
			SET ucd_id = t_ucd.oid
			FROM base.t_ucd
			WHERE code_reference_fournisseur = t_ucd.code AND
				length(code_reference_fournisseur) = 7 AND
				code_reference_fournisseur BETWEEN '9000000' AND '9899999' AND
				t_article_fournisseur.ucd_id IS DISTINCT FROM t_ucd.oid
			;

			INSERT INTO base.t_ucd (code, texte, texte_court)
			SELECT substr(code_reference_fournisseur,6,7),
				MAX(texte_reference_fournisseur),
				MAX(texte_reference_fournisseur)
			FROM eco.t_article_fournisseur
			WHERE length(code_reference_fournisseur) = 13 AND
				substr(code_reference_fournisseur,6,7) BETWEEN '9000000' AND '9899999' AND
				code_reference_fournisseur LIKE '34008%' AND
				base.cti_to_number(code_reference_fournisseur)::text = code_reference_fournisseur AND
				substr(code_reference_fournisseur,6,7) NOT IN (SELECT code FROM base.t_ucd)
			GROUP BY 1;

			UPDATE eco.t_article_fournisseur
			SET ucd_id = t_ucd.oid
			FROM base.t_ucd
			WHERE substr(code_reference_fournisseur,6,7) = t_ucd.code AND
				length(code_reference_fournisseur) = 13 AND
				code_reference_fournisseur LIKE '34008%' AND
				substr(code_reference_fournisseur,6,7) BETWEEN '9000000' AND '9899999' AND
				t_article_fournisseur.ucd_id IS DISTINCT FROM t_ucd.oid
			;
			
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Affectation unités selon activité">
			<condition><![CDATA[
				
				SELECT count(*) > 0
				FROM pg_tables
				WHERE schemaname = 'activite' AND 
					tablename = 'p_mouvements_sejour'
				;
				
			]]></condition>
			<sqlcmd><![CDATA[
			
				INSERT INTO eco.t_divers (code, texte, valeur, description)
				SELECT
					'UFSELONETAGEACTIVITE',
					'Si UF non renseignee ou dans la liste, chercher etage dans activite',
					'',
					'Liste des UF separees par une virgule (#0 pour UF non renseignee'
				WHERE 'UFSELONETAGEACTIVITE' NOT IN (SELECT code FROM eco.t_divers)
				;


				DROP TABLE IF EXISTS w_mouvement_etage;
				CREATE TEMP TABLE w_mouvement_etage AS
				SELECT p_mouvements_articles.oid, 
					'*CTIACT-'||(MAX(ARRAY[to_char(p_mouvements_sejour.heure_debut,'FM000000'), t_etages.code]))[2] AS etage_code_original, 
					(MAX(ARRAY[to_char(p_mouvements_sejour.heure_debut,'FM000000'), t_etages.code]))[2] AS etage_code, 
					(MAX(ARRAY[to_char(p_mouvements_sejour.heure_debut,'FM000000'), t_etages.texte]))[2] AS etage_texte
				FROM eco.p_mouvements_articles
				JOIN eco.t_unites_fonctionnelles ON p_mouvements_articles.unite_fonctionnelle_id = t_unites_fonctionnelles.oid
				JOIN activite.p_mouvements_sejour ON 
					p_mouvements_articles.no_sejour = p_mouvements_sejour.no_sejour AND
					p_mouvements_sejour.date = p_mouvements_articles.date
				JOIN activite.t_lieux ON p_mouvements_sejour.lieu_id = t_lieux.oid
				JOIN activite.t_lits ON t_lieux.lit_id= t_lits.oid
				JOIN activite.t_etages ON t_lits.etage_id = t_etages.oid
				JOIN eco.t_divers ON t_divers.code = 'UFSELONETAGEACTIVITE'
				WHERE sens_mouvement = 'S' AND
					(
					p_mouvements_articles.unite_fonctionnelle_id = 0 AND '#0' = ANY(string_to_array(t_divers.valeur,',')) OR
					p_mouvements_articles.unite_fonctionnelle_id > 0 AND t_unites_fonctionnelles.code ILIKE ANY(string_to_array(t_divers.valeur,','))
					) AND
					p_mouvements_articles.no_sejour <> '' AND
					t_etages.oid <> 0
					GROUP BY 1
				;

				ANALYSE w_mouvement_etage
				;

				DROP TABLE IF EXISTS w_etage;
				CREATE TEMP TABLE w_etage AS 
				SELECT
					etage_code_original,
					etage_code,
					etage_texte,
					0::bigint AS unite_fonctionnelle_id
				FROM w_mouvement_etage
				GROUP BY 1,2,3
				;

				ANALYSE w_etage
				;

				INSERT INTO eco.t_unites_fonctionnelles(code_original, code, texte, texte_court )
				SELECT etage_code_original, MAX(etage_code), MAX(etage_texte) || ' (GAP)', MAX(etage_texte) || ' (GAP)'
				FROM w_etage
				LEFT JOIN eco.t_unites_fonctionnelles ON 
					etage_code_original = t_unites_fonctionnelles.code_original OR
					etage_code = t_unites_fonctionnelles.code
				WHERE code_original IS NULL 
				GROUP BY 1
				ORDER BY 2
				;

				UPDATE w_etage
				SET unite_fonctionnelle_id = t_unites_fonctionnelles.oid
				FROM eco.t_unites_fonctionnelles
				WHERE w_etage.etage_code ILIKE t_unites_fonctionnelles.code
				;

				UPDATE eco.p_mouvements_articles
				SET unite_fonctionnelle_id = w_etage.unite_fonctionnelle_id
				FROM w_mouvement_etage
				JOIN w_etage on w_mouvement_etage.etage_code_original = w_etage.etage_code_original 
				WHERE w_mouvement_etage.oid = p_mouvements_articles.oid
				;
	
		]]></sqlcmd>
		</NODE>
		
        
        <NODE label="Poles">
			<sqlcmd><![CDATA[

            SELECT eco.cti_reorganize_pole()
            ;
			]]></sqlcmd>
		</NODE>
	

	
		<NODE label="Référencements">
			<sqlcmd><![CDATA[

				TRUNCATE eco.p_oids;
	
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'articles', article_id
				FROM eco.p_mouvements_articles
				WHERE article_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'articles')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'articles', article_id
				FROM eco.p_lignes_commandes
				WHERE article_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'articles')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'categories_articles', categorie_id
				FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE categorie_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'categories_articles')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'categories_articles', categorie_id
				FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE categorie_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'categories_articles')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'sous_categories_articles', sous_categorie_id
				FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE sous_categorie_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'sous_categories_articles')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'sous_categories_articles', sous_categorie_id
				FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE sous_categorie_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'sous_categories_articles')
				GROUP BY 1,2;
																
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'classification_atc', classification_atc_id
				FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE classification_atc_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'classification_atc')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'classification_atc', classification_atc_id
				FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE classification_atc_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'classification_atc')
				GROUP BY 1,2;
								
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'familles_articles', famille_id
				FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE famille_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'familles_articles')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'familles_articles', famille_id
				FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE famille_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'familles_articles')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'sous_familles_articles', sous_famille_id
				FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE sous_famille_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'sous_familles_articles')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'sous_familles_articles', sous_famille_id
				FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE sous_famille_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'sous_familles_articles')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'compte', compte_id
				FROM eco.p_mouvements_articles
				WHERE compte_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'compte')
				GROUP BY 1,2;
				
				
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'compte', compte_id
				FROM eco.p_lignes_commandes 
				WHERE compte_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'compte')
				GROUP BY 1,2;
                
                INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'pole', pole_id
				FROM eco.p_mouvements_articles
				WHERE pole_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'pole')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'types_articles', t_articles.type_id
				FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE t_articles.type_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'types_articles')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'types_articles', t_articles.type_id
				FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE t_articles.type_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'types_articles')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'unites_stockage_articles', t_articles.unite_stockage_id
				FROM eco.p_mouvements_articles JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE t_articles.unite_stockage_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'unites_stockage_articles')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'unites_stockage_articles', t_articles.unite_stockage_id
				FROM eco.p_lignes_commandes JOIN eco.t_articles ON article_id = t_articles.oid
				WHERE t_articles.unite_stockage_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'unites_stockage_articles')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'gestionnaires', gestionnaire_id
				FROM eco.p_mouvements_articles
				WHERE gestionnaire_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'gestionnaires')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'gestionnaires', gestionnaire_id
				FROM eco.p_commandes
				WHERE gestionnaire_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'gestionnaires')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'sites', site_id
				FROM eco.p_mouvements_articles
				WHERE site_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'sites')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'sites', site_id
				FROM eco.p_commandes
				WHERE site_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'sites')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'unites_fonctionnelles', unite_fonctionnelle_id
				FROM eco.p_mouvements_articles
				WHERE unite_fonctionnelle_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'unites_fonctionnelles')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'unites_fonctionnelles', unite_fonctionnelle_id
				FROM eco.p_commandes
				WHERE unite_fonctionnelle_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'unites_fonctionnelles')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'unites_fonctionnelles', unite_fonctionnelle_id
				FROM eco.p_mouvements_articles
				WHERE unite_fonctionnelle_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'unites_fonctionnelles')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'fournisseurs', fournisseur_id
				FROM eco.p_mouvements_articles
				WHERE fournisseur_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'fournisseurs')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'fournisseurs', fournisseur_id
				FROM eco.p_commandes
				WHERE fournisseur_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'fournisseurs')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'types_fournisseurs', t_fournisseurs.type_id
				FROM eco.p_mouvements_articles JOIN eco.t_fournisseurs ON fournisseur_id = t_fournisseurs.oid
				WHERE t_fournisseurs.type_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'types_fournisseurs')
				GROUP BY 1,2;
				
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'types_fournisseurs', t_fournisseurs.type_id
				FROM eco.p_commandes JOIN eco.t_fournisseurs ON fournisseur_id = t_fournisseurs.oid
				WHERE t_fournisseurs.type_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'types_fournisseurs')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'lieux_mouvements', lieu_id
				FROM eco.p_mouvements_articles
				WHERE lieu_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'lieux_mouvements')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'lieux_commandes', lieu_livraison_id
				FROM eco.p_commandes
				WHERE lieu_livraison_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'lieux_commandes')
				GROUP BY 1,2;

				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'types_mouvements', type_mouvement_id
				FROM eco.p_mouvements_articles
				WHERE type_mouvement_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'types_mouvements')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'ucd', ucd_id
				FROM eco.p_mouvements_articles
				WHERE ucd_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'ucd')
				GROUP BY 1,2;
				
				INSERT INTO eco.p_oids (code_table, oid) 
				SELECT 'lpp', lpp_id
				FROM eco.p_mouvements_articles
				WHERE lpp_id NOT IN (SELECT oid FROM eco.p_oids WHERE code_table = 'lpp')
				GROUP BY 1,2;
				
			
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Réplication données">
			<sqlcmd><![CDATA[
			
				TRUNCATE eco.t_profils;
				
				INSERT INTO eco.t_profils (categorie_article_id, article_id, classification_atc_id, compte_id, famille_article_id, sous_famille_article_id, sous_categorie_article_id, type_article_id, fournisseur_id, lieu_id, gestionnaire_id, unite_fonctionnelle_id)
				SELECT categorie_id, article_id, classification_atc_id, t_articles.compte_id, famille_id, sous_famille_id, sous_categorie_id, type_id, fournisseur_id, lieu_id, gestionnaire_id, unite_fonctionnelle_id
				FROM eco.p_mouvements_articles
					JOIN eco.t_articles ON article_id = t_articles.oid
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12
				UNION
				SELECT categorie_id, article_id, classification_atc_id, t_articles.compte_id, famille_id, sous_famille_id, sous_categorie_id, type_id, fournisseur_id, lieu_livraison_id, gestionnaire_id, p_lignes_commandes.unite_fonctionnelle_id
				FROM eco.p_lignes_commandes
					JOIN eco.p_commandes ON commande_id = p_commandes.oid
					JOIN eco.t_articles ON article_id = t_articles.oid
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12;
				
				
				SELECT eco.cti_reorganize_ucd_lpp_c();
				
				SELECT eco.cti_reorganize_fournisseur();
				
				SELECT eco.cti_reorganize_type_article_cti();
				
				SELECT * FROM eco.cti_update_schema_classes('*ALL');
	
				
			]]></sqlcmd>
		</NODE>
        
        <NODE label="Mise à jour des dates">
            <sqlcmd><![CDATA[

                -- Initialisation mois en cours/mois clôturé (NOW/NOWCLOTURE) lorsque pas déjà renseigné lors de l'import.
                SELECT eco.cti_now('NOW','');
                SELECT eco.cti_now('NOWCLOTURE','');
                
            ]]></sqlcmd>
        </NODE>
		
		<NODE label="Calcul chiffrier">
            <sqlcmd><![CDATA[

                TRUNCATE eco.p_chiffrier_production;
	
				INSERT INTO eco.p_chiffrier_production (provider_id,mois)
				SELECT provider_id, extract('year' FROM date) * 100 + extract('month' FROM date) 
				FROM eco.p_mouvements_articles
				GROUP BY 1,2;

				UPDATE eco.p_chiffrier_production
				SET montant_commande = montant_commande_ht
				FROM 
				(
				SELECT provider_id,extract('year' FROM date_commande) * 100 + extract('month' FROM date_commande) AS mois  ,
				sum(montant_commande_ht) AS montant_commande_ht
				FROM eco.p_commandes
				GROUP BY 1,2
				) as sub
				WHERE sub.mois = p_chiffrier_production.mois
					AND sub.provider_id = p_chiffrier_production.provider_id
				;

				UPDATE eco.p_chiffrier_production
				SET montant_livraison = montant_entrees
				FROM 
				(
				SELECT provider_id, extract('year' FROM date) * 100 + extract('month' FROM date) AS mois  ,
				sum(entree_montant_ht) AS montant_entrees
				FROM eco.p_mouvements_articles
				WHERE type_mouvement_id IN (
					SELECT
						to_id
					FROM 
						eco.t_classes_sections_elements
					WHERE
						section_id IN (SELECT oid FROM eco.t_classes_sections WHERE code = '00' AND classe_id = (SELECT oid from eco.t_classes WHERE code = 'CTI_TYPMVT')))
				GROUP BY 1,2
				) as sub
				WHERE sub.mois = p_chiffrier_production.mois
					AND sub.provider_id = p_chiffrier_production.provider_id
				;


				UPDATE eco.p_chiffrier_production
				SET montant_consommation = montant_sorties
				FROM 
				(
				SELECT provider_id, extract('year' FROM date) * 100 + extract('month' FROM date) AS mois  ,
				sum(sortie_montant_ht) AS montant_sorties
				FROM eco.p_mouvements_articles
				WHERE type_mouvement_id IN (
					SELECT
						to_id
					FROM 
						eco.t_classes_sections_elements
					WHERE
						section_id IN (SELECT oid FROM eco.t_classes_sections WHERE code = '01' AND classe_id = (SELECT oid from eco.t_classes WHERE code = 'CTI_TYPMVT')))
				GROUP BY 1,2
				) as sub
				WHERE sub.mois = p_chiffrier_production.mois
					AND sub.provider_id = p_chiffrier_production.provider_id
				;
                
            ]]></sqlcmd>
        </NODE>
		
	</NODE>
	<NODE name="VACUUM" label="REORGANISATION BASE DE DONNEES">
		<NODE label="Réorganisation tables">
			<sqlcmd><![CDATA[
			
                VACUUM ANALYSE eco.p_commandes;
				REINDEX TABLE eco.p_commandes;
				
                VACUUM ANALYSE eco.p_lignes_commandes;
                VACUUM ANALYSE eco.p_facture;
                VACUUM ANALYSE eco.p_lignes_facture;
                VACUUM ANALYSE eco.p_mouvements_articles;
				REINDEX TABLE eco.p_mouvements_articles;
                VACUUM ANALYSE eco.p_stock;
				
				VACUUM ANALYSE eco.p_chiffrier_production;
				VACUUM ANALYSE eco.p_sejours;
				VACUUM FULL eco.t_articles;
				REINDEX TABLE eco.t_articles;
				VACUUM ANALYSE eco.t_article_fournisseur;
				VACUUM ANALYSE eco.t_classes_sections_elements;
				VACUUM ANALYSE eco.t_listes_contenu;
				VACUUM ANALYSE eco.t_rapport_group;
				VACUUM ANALYSE eco.t_rapports;
				VACUUM ANALYSE eco.t_unites_fonctionnelles;
			]]></sqlcmd>
		</NODE>
	</NODE>
	<NODE name="RAZ" label="RAZ BASE">
		<NODE label="Nettoyage des tables">
			<sqlcmd><![CDATA[

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

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