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