return: text
lang: plpgsql
src: |
  DECLARE	--select * from eco.p_stock order by date_fin desc
  	result TEXT;
  BEGIN
  	DROP TABLE IF EXISTS w_mouvements_articles;
  	CREATE TABLE w_mouvements_articles AS SELECT * from eco.p_mouvements_articles;
  
  	ANALYSE w_mouvements_articles;
    ANALYSE eco.p_stock;
  
  	CREATE INDEX w_mvt_1 ON w_mouvements_articles using btree (article_id);
  	CREATE INDEX w_mvt_2 ON w_mouvements_articles using btree (site_id);
  	CREATE INDEX w_mvt_3 ON w_mouvements_articles using btree (lieu_id);
  	CREATE INDEX w_mvt_4 ON w_mouvements_articles using btree (date);
  	
  	PERFORM base.cti_execute(
    	'UPDATE w_mouvements_articles
    		SET
    		est_premier_jour = sub.est_premier_jour,
    		est_dernier_jour = sub.est_dernier_jour,
    		date_fin = sub.date_fin
    		FROM (SELECT
    		oid,
    		 CASE
                WHEN rank() OVER (PARTITION BY w_mouvements_articles.article_id, w_mouvements_articles.site_id, w_mouvements_articles.lieu_id,date ORDER BY w_mouvements_articles.oid) = 1 THEN 1
                ELSE 0
            END AS est_premier_jour,
            CASE
                WHEN cume_dist() OVER (PARTITION BY w_mouvements_articles.article_id,  w_mouvements_articles.site_id, w_mouvements_articles.lieu_id, w_mouvements_articles.date ORDER BY w_mouvements_articles.oid) = 1 THEN 1
                ELSE 0
            END AS est_dernier_jour,
            CASE
                WHEN cume_dist() OVER (PARTITION BY w_mouvements_articles.article_id,  w_mouvements_articles.site_id, w_mouvements_articles.lieu_id, w_mouvements_articles.date ORDER BY w_mouvements_articles.oid) = 1
    		THEN lead(w_mouvements_articles.date) OVER (PARTITION BY w_mouvements_articles.article_id,  w_mouvements_articles.site_id, w_mouvements_articles.lieu_id ORDER BY w_mouvements_articles.date) - interval ''1 day''
                ELSE date 
            END AS date_fin
            FROm w_mouvements_articles
            ) sub
            WHERE sub.oid = w_mouvements_articles.oid;
    
    	UPDATE w_mouvements_articles
    	SET date_fin = date_trunc(''month'',now()) - interval ''1 day'' + interval ''1 month''
    	WHERE date_fin IS NULL
    	;
    	
            UPDATE w_mouvements_articles
    	SET
    		stock_quantite_debut = 0,
    		stock_quantite_fin = 0
    	;
  
    	UPDATE w_mouvements_articles
    	SET
    		stock_quantite_debut = p_stock.stock_quantite_fin - w_stock_var.entree_var + w_stock_var.sortie_var,
    		stock_quantite_fin = p_stock.stock_quantite_fin - w_stock_var.entree_var + w_stock_var.sortie_var + w_mouvements_articles.entree_quantite - w_mouvements_articles.sortie_quantite
    	FROM
    	(
    		SELECT
    			oid,
    			site_id,
    			lieu_id,
    			article_id,
    			date,
    			date_trunc(''month'',date) as month,
    			sum(entree_quantite) OVER (PARTITION BY site_id, lieu_id, article_id, date_trunc(''month'',date) ORDER BY date DESC, oid DESC) AS entree_var,
    			sum(sortie_quantite) OVER (PARTITION BY site_id, lieu_id, article_id, date_trunc(''month'',date) ORDER BY date DESC, oid DESC) AS sortie_var
    		FROM w_mouvements_articles
    		ORDER BY 1,2,3,4,5
    		) w_stock_var
    		JOIN 
    		eco.p_stock
    		ON 1=1
    			AND w_stock_var.article_id = p_stock.article_id
    			AND w_stock_var.site_id = p_stock.site_id
    			AND w_stock_var.lieu_id = p_stock.lieu_id
    			AND date_trunc(''month'',date_fin) = month 
    		WHERE 1=1
    			AND w_mouvements_articles.article_id = w_stock_var.article_id
    			AND w_mouvements_articles.site_id = w_stock_var.site_id
    			AND w_mouvements_articles.lieu_id = w_stock_var.lieu_id
    			AND w_mouvements_articles.oid = w_stock_var.oid
    		',1)
    		WHERE (SELECT count(DISTINCT lieu_id) FROM eco.p_stock) > 1
    		;
    		
    	PERFORM base.cti_execute(
    	'UPDATE w_mouvements_articles
    		SET
    		est_premier_jour = sub.est_premier_jour,
    		est_dernier_jour = sub.est_dernier_jour,
    		date_fin = sub.date_fin
    		FROM (SELECT
    		oid,
    		 CASE
                WHEN rank() OVER (PARTITION BY w_mouvements_articles.article_id, w_mouvements_articles.site_id, date ORDER BY w_mouvements_articles.oid) = 1 THEN 1
                ELSE 0
            END AS est_premier_jour,
            CASE
                WHEN cume_dist() OVER (PARTITION BY w_mouvements_articles.article_id,  w_mouvements_articles.site_id, w_mouvements_articles.date ORDER BY w_mouvements_articles.oid) = 1 THEN 1
                ELSE 0
            END AS est_dernier_jour,
            CASE
                WHEN cume_dist() OVER (PARTITION BY w_mouvements_articles.article_id,  w_mouvements_articles.site_id, w_mouvements_articles.date ORDER BY w_mouvements_articles.oid) = 1
    		THEN lead(w_mouvements_articles.date) OVER (PARTITION BY w_mouvements_articles.article_id,  w_mouvements_articles.site_id ORDER BY w_mouvements_articles.date) - interval ''1 day''
                ELSE date 
            END AS date_fin
            FROm w_mouvements_articles
            ) sub
            WHERE sub.oid = w_mouvements_articles.oid;
    
    	UPDATE w_mouvements_articles
    	SET date_fin = date_trunc(''month'',now()) - interval ''1 day'' + interval ''1 month''
    	WHERE date_fin IS NULL
    	;
    	
            UPDATE w_mouvements_articles
    	SET
    		stock_quantite_debut = 0,
    		stock_quantite_fin = 0
    	;
  
  	UPDATE w_mouvements_articles
    	SET
    		stock_quantite_debut = p_stock.stock_quantite_fin - w_stock_var.entree_var + w_stock_var.sortie_var,
    		stock_quantite_fin = p_stock.stock_quantite_fin - w_stock_var.entree_var + w_stock_var.sortie_var + w_mouvements_articles.entree_quantite - w_mouvements_articles.sortie_quantite
    	FROM
    	(
    		SELECT
    			oid,
    			site_id,
    			lieu_id,
    			article_id,
    			date,
    			date_trunc(''month'',date) as month,
    			sum(entree_quantite) OVER (PARTITION BY site_id, article_id, date_trunc(''month'',date) ORDER BY date DESC, oid DESC) AS entree_var,
    			sum(sortie_quantite) OVER (PARTITION BY site_id, article_id, date_trunc(''month'',date) ORDER BY date DESC, oid DESC) AS sortie_var
    		FROM w_mouvements_articles
    		ORDER BY 1,2,3,4,5
    		) w_stock_var
    		JOIN 
    		eco.p_stock
    		ON 1=1
    			AND w_stock_var.article_id = p_stock.article_id
    			AND w_stock_var.site_id = p_stock.site_id
    			AND date_trunc(''month'',date_fin) = month 
    		WHERE 1=1
    			AND w_mouvements_articles.article_id = w_stock_var.article_id
    			AND w_mouvements_articles.site_id = w_stock_var.site_id
    			AND w_mouvements_articles.oid = w_stock_var.oid
    		',1)
    		WHERE (SELECT count(DISTINCT lieu_id) FROM eco.p_stock) = 1
    		;
    		
  	TRUNCATE eco.p_mouvements_articles;
  
  	INSERT INTO eco.p_mouvements_articles SELECT * FROM w_mouvements_articles;
  
  	ANALYSE eco.p_mouvements_articles;
  	REINDEX TABLE eco.p_mouvements_articles;
  	
  	RETURN 'OK';
  END;