'' ; UPDATE eco.t_types_mouvements SET texte = LIB_ETAT, texte_court = LIB_ETAT FROM prod_qsp.MOUVEMENT_STOCK WHERE CODE_MOUVEMENT <> '' AND code_original = CODE_MOUVEMENT AND (texte IS DISTINCT FROM LIB_ETAT OR texte_court IS DISTINCT FROM LIB_ETAT); INSERT INTO eco.t_types_mouvements(code_original, code, texte, texte_court) SELECT 'CTI_STOCK', 'CTI_STOCK', 'Stock début de mois', 'Stock début de mois' WHERE 'CTI_STOCK' NOT IN (SELECT code_original FROM eco.t_types_mouvements); -- Site INSERT INTO eco.t_sites(code, code_original,texte, texte_court) SELECT NUM_SOCIETE, NUM_SOCIETE, RAISON_SOCIALE, RAISON_SOCIALE FROM prod_qsp.SOCIETE WHERE NUM_SOCIETE IN (SELECT NUM_SOCIETE FROM prod_qsp.COMMANDE) AND NUM_SOCIETE NOT IN (SELECT code_original FROM eco.t_sites WHERE code_original IS NOT NULL) ORDER BY NUM_SOCIETE; -- Gestionnaires -- Achats INSERT INTO eco.t_gestionnaires(code_original, code, texte, texte_court) SELECT USERNAME, USERNAME, USERNAME, USERNAME FROM prod_qsp.PRODUIT LEFT JOIN eco.t_gestionnaires ON (code_original = USERNAME) WHERE code_original IS NULL GROUP BY 1,2,3,4 ; -- Stocks INSERT INTO eco.t_gestionnaires(code_original, code, texte, texte_court) SELECT USERNAME, USERNAME, USERNAME, USERNAME FROM prod_qsp.USER_STOCK LEFT JOIN eco.t_gestionnaires ON (code_original = USERNAME) WHERE code_original IS NULL GROUP BY 1,2,3,4; -- Unites de stockage INSERT INTO eco.t_unites(code, code_original, texte, texte_court) SELECT PCB, PCB, LIB_PCB, LIB_PCB FROM prod_qsp.PRODUIT WHERE PCB NOT IN (SELECT code_original from eco.t_unites) GROUP BY 1,2,3,4; -- unites fonctionnelles INSERT INTO eco.t_unites_fonctionnelles(code, texte, texte_court, code_original) SELECT NUM_TIERS, NOM_TIERS, NOM_TIERS, NUM_TIERS FROM prod_qsp.TIERS LEFT JOIN eco.t_unites_fonctionnelles ON (NUM_TIERS = t_unites_fonctionnelles.code_original) WHERE NUM_TIERS IN (SELECT NUM_TIERS FROM prod_qsp.LIVRAISON WHERE TYPE_TIERS = 'V') AND t_unites_fonctionnelles.code_original IS NULL; UPDATE eco.t_unites_fonctionnelles SET texte = NOM_TIERS, texte_court = NOM_TIERS FROM prod_qsp.TIERS WHERE NUM_TIERS = t_unites_fonctionnelles.code_original AND ( t_unites_fonctionnelles.texte IS DISTINCT FROM NOM_TIERS OR t_unites_fonctionnelles.texte_court IS DISTINCT FROM NOM_TIERS) ; ]]> = '2015-01-01' AND TYPE_TIERS = 'F' ORDER BY 1; SELECT base.cti_enable_index('eco', 'i_commandes_1'); SELECT base.cti_enable_index('eco', 'i_commandes_2'); SELECT base.cti_enable_index('eco', 'i_commandes_3'); SELECT base.cti_enable_index('eco', 'i_commandes_4'); SELECT base.cti_enable_index('eco', 'i_commandes_5'); SELECT base.cti_enable_index('eco', 'i_commandes_6'); SELECT base.cti_enable_index('eco', 'i_commandes_7'); SELECT base.cti_enable_index('eco', 'i_commandes_8'); SELECT base.cti_disable_index('eco', 'i_ligne_commandes_1'); SELECT base.cti_disable_index('eco', 'i_ligne_commandes_2'); SELECT base.cti_disable_index('eco', 'i_ligne_commandes_3'); DROP TABLE IF EXISTS w_tva; CREATE TEMP TABLE w_tva AS SELECT CODE_TVA ,round(SUM(MONTANT_TVA)/sum(MONTANT_HT),3) AS tx_tva FROM prod_qsp.pied_facture group by 1; DROP TABLE IF EXISTS w_COMMANDES_LIVREES; CREATE TEMP TABLE w_COMMANDES_LIVREES AS SELECT COMMANDES_LIVREES.NOLIG_COM, SUM(LIV_LIGNE.QTE_SPCB)::numeric AS quantite_stockage, MAX(LIV_LIGNE.PRIX_REF)::numeric AS prix_unitaire_stockage, MAX(LIVRAISON.DATE_LIV) AS date_livraison, SUM(LIV_LIGNE.QTE_PCB) AS quantite_livraison_approvisionnement, SUM(COALESCE(LIV_LIGNE.QTE_SPCB,0)) AS quantite_livraison_stockage, SUM(COALESCE(LIV_LIGNE.PRIX_REF*LIV_LIGNE.QTE_SPCB,0)) AS montant_livraison_ht, SUM(COALESCE(LIV_LIGNE.PRIX_REF*(LIV_LIGNE.QTE_SPCB-QTE_GRAT)*(1+tx_tva),0)) AS montant_livraison_ttc, CASE WHEN SUM(COALESCE(LIV_LIGNE.QTE_SPCB,0)) = COM_LIGNE.QTE_SPCB THEN 'T' WHEN SUM(COALESCE(LIV_LIGNE.QTE_SPCB,0)) > COM_LIGNE.QTE_SPCB THEN 'P' WHEN SUM(COALESCE(LIV_LIGNE.QTE_SPCB,0)) != 0 THEN 'P' ELSE 'N' END AS etat_livraison, SUM(COALESCE(PRIX_FACTURE_HT,0)) AS montant_liquidation_ht, SUM(COALESCE(LIGNE_FACTURE.TAUX_REMISE*LIGNE_FACTURE.PRIX_UNITAIRE*LIVRAISONS_FACTUREES.QTE_PCB,0)) AS montant_liquidation_remise, SUM(COALESCE(TVA_FACTURE,0)) AS montant_liquidation_tva, --montant_liquidation_port, COALESCE(SUM(PRIX_FACTURE_HT) + SUM(TVA_FACTURE),0) AS montant_liquidation_ttc, CASE WHEN MAX(COALESCE(LIV_LIGNE.FACTURE,0)) = 1 THEN 'T' ELSE 'N' END AS etat_liquidation FROM prod_qsp.COM_LIGNE LEFT JOIN prod_qsp.PRODUIT ON COM_LIGNE.N_PROD = PRODUIT.N_PROD LEFT JOIN prod_qsp.COMMANDES_LIVREES ON COMMANDES_LIVREES.NOLIG_COM = COM_LIGNE.NOLIG_COM LEFT JOIN prod_qsp.LIV_LIGNE on LIV_LIGNE.NOLIG_LIV = COMMANDES_LIVREES.NOLIG_LIV LEFT JOIN prod_qsp.LIVRAISON on LIVRAISON.NUMERO_LIV = LIV_LIGNE.NUMERO_LIV LEFT JOIN prod_qsp.LIVRAISONS_FACTUREES on LIV_LIGNE.NOLIG_LIV = LIVRAISONS_FACTUREES.NOLIG_LIV LEFT JOIN prod_qsp.LIGNE_FACTURE on LIGNE_FACTURE.NOLIG_FACT = LIVRAISONS_FACTUREES.NOLIG_FACT LEFT JOIN w_tva ON COALESCE(PRODUIT.CODE_TVA,LIGNE_FACTURE.CODE_TVA) = w_tva.CODE_TVA GROUP BY 1,COM_LIGNE.QTE_SPCB; 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, ucd_id, lpp_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) SELECT p_commandes.code_original || '-' || COM_LIGNE.NOLIG_COM AS code_original, p_commandes.oid AS commande_id, rank() OVER (PARTITION BY p_commandes.oid ORDER BY COM_LIGNE.NOLIG_COM) AS ligne_commande, COM_LIGNE.LIB_PROD AS texte, COALESCE(t_articles.oid, 0) AS article_id, 0::bigint AS unite_approvisionnement_id, compte_id, --possibilite de remonter le compte present dans le reglement COALESCE(t_ucd.oid, t_articles.ucd_id, 0) AS ucd_id, COALESCE(t_lpp.oid, t_articles.lpp_id, 0) AS lpp_id, COM_LIGNE.QTE_SPCB AS quantite_approvisionnement, COM_LIGNE.PRIX_REF AS prix_unitaire_approvisionnement, COM_LIGNE.QTE_PCB AS multiplicateur_stockage, quantite_stockage, prix_unitaire_stockage, COM_LIGNE.PRIX_REF*COM_LIGNE.QTE_SPCB AS montant_commande_ht, COM_LIGNE.PRIX_REF*COM_LIGNE.QTE_SPCB*TAUX_REMISE/100 AS montant_commande_remise, (1-base.cti_division(w_COMMANDES_LIVREES.montant_liquidation_ttc,w_COMMANDES_LIVREES.montant_liquidation_ht)) * COM_LIGNE.PRIX_REF*COM_LIGNE.QTE_SPCB AS montant_commande_tva, 0::numeric AS montant_commande_port, base.cti_division(w_COMMANDES_LIVREES.montant_liquidation_ttc,w_COMMANDES_LIVREES.montant_liquidation_ht) * COM_LIGNE.PRIX_REF*COM_LIGNE.QTE_SPCB AS montant_commande_ttc, quantite_livraison_approvisionnement, quantite_livraison_stockage, montant_livraison_ht, 0::numeric AS montant_livraison_ttc, etat_livraison, w_COMMANDES_LIVREES.montant_liquidation_ht, w_COMMANDES_LIVREES.montant_liquidation_remise, w_COMMANDES_LIVREES.montant_liquidation_tva, 0::numeric AS montant_liquidation_port, w_COMMANDES_LIVREES.montant_liquidation_ttc, w_COMMANDES_LIVREES.etat_liquidation FROM prod_qsp.COM_LIGNE JOIN eco.p_commandes ON NUMERO_COM = p_commandes.code_original JOIN w_COMMANDES_LIVREES ON COM_LIGNE.NOLIG_COM = w_COMMANDES_LIVREES.NOLIG_COM LEFT JOIN eco.t_articles ON N_PROD = t_articles.code_original AND t_articles.oid != 0 LEFT JOIN base.t_ucd ON ucd_id = t_ucd.oid AND t_ucd.oid != 0 LEFT JOIN base.t_lpp ON lpp_id = t_lpp.oid AND t_lpp.oid != 0 ; DROP TABLE IF EXISTS W_COM_TOT; CREATE TEMP TABLE w_COM_TOT AS SELECT commande_id, SUM(p_lignes_commandes.montant_commande_ht) AS montant_commande_ht, SUM(p_lignes_commandes.montant_commande_remise) AS montant_commande_remise, SUM(p_lignes_commandes.montant_commande_tva) AS montant_commande_tva, SUM(p_lignes_commandes.montant_commande_ttc) AS montant_commande_total, SUM(p_lignes_commandes.montant_liquidation_ht) AS montant_liquidation_ht, SUM(p_lignes_commandes.montant_liquidation_remise) AS montant_liquidation_remise, SUM(p_lignes_commandes.montant_liquidation_tva) AS montant_liquidation_tva, SUM(p_lignes_commandes.montant_liquidation_ttc) AS montant_liquidation_total FROM eco.p_lignes_commandes GROUP BY 1 ; VACUUM ANALYSE w_COM_TOT; CREATE INDEX commande_id_i ON w_COM_TOT using btree(commande_id); UPDATE eco.p_commandes SET date_reception = date_livraison FROM ( SELECT COM_LIGNE.NUMERO_COM, MAX(date_livraison) AS date_livraison FROM prod_qsp.COM_LIGNE JOIN eco.p_commandes ON NUMERO_COM = p_commandes.code_original JOIN w_COMMANDES_LIVREES ON COM_LIGNE.NOLIG_COM = w_COMMANDES_LIVREES.NOLIG_COM GROUP BY 1 ) sub WHERE NUMERO_COM = numero ; -- maj des commandes UPDATE eco.p_commandes SET montant_commande_ht = w_COM_TOT.montant_commande_ht, montant_commande_remise = w_COM_TOT.montant_commande_remise, montant_commande_tva = w_COM_TOT.montant_commande_tva, montant_commande_total = w_COM_TOT.montant_commande_total, montant_liquidation_ht = w_COM_TOT.montant_liquidation_ht, montant_liquidation_remise = w_COM_TOT.montant_liquidation_remise, montant_liquidation_tva = w_COM_TOT.montant_liquidation_tva, montant_liquidation_total = w_COM_TOT.montant_liquidation_total FROM w_COM_TOT WHERE commande_id = p_commandes.oid ; -- maj des données fournisseur article UPDATE eco.t_articles SET fournisseur_principal_id = last_fou, ref_fournisseur_id = ref_ids, ref_fournisseur_texte = ref_textes FROM ( SELECT article_id, article_code, ref_fournisseur_id, ref_fournisseur_texte, array_agg(fournisseur_id) AS ref_ids, array_agg(article_texte || ' (' || article_code || ')') as ref_textes, (MAX(ARRAY[date_commande::text, fournisseur_id::text]))[2]::bigint AS last_fou FROM ( SELECT article_id, t_articles.code AS article_code, t_articles.texte AS article_texte, ref_fournisseur_id, ref_fournisseur_texte, fournisseur_id, max(date_commande) AS date_commande FROM eco.p_commandes JOIN eco.p_lignes_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 ) sub GROUP BY 1,2,3,4 ORDER BY 1 ) sub WHERE article_id = t_articles.oid ; SELECT base.cti_enable_index('eco', 'i_ligne_commandes_1'); SELECT base.cti_enable_index('eco', 'i_ligne_commandes_2'); SELECT base.cti_enable_index('eco', 'i_ligne_commandes_3'); ]]> = 0 THEN 'E' ELSE 'S' END AS sens_mouvement, COALESCE(t_types_mouvements.oid,0) AS type_mouvement_id, t_types_mouvements.texte || ' (' || t_articles.code || ' - ' || t_articles.texte || ')' AS texte, COALESCE(t_gestionnaires.oid,0) AS gestionnaire_id, COALESCE(t_lieux.oid,0) AS lieu_id, COALESCE(t_unites_fonctionnelles.oid,0) AS unite_fonctionnelle_id, COALESCE(t_articles.oid,0) AS article_id, COALESCE(t_articles.compte_id,0) AS compte_id, COALESCE(t_fournisseurs.oid,t_articles.fournisseur_principal_id,0) AS fournisseur_id, COALESCE(commande_id,0) AS commande_id, COALESCE(ligne_commande,0) AS ligne_commande, COALESCE(t_sites.oid,0) AS site_id, COALESCE(t_articles.lpp_id,0) AS lppid, COALESCE(t_articles.ucd_id,0) AS ucd_id, PRIX_REF AS prix_unitaire, CASE WHEN QTE_MVT >= 0 THEN QTE_MVT ELSE 0 END AS entree_quantite, CASE WHEN QTE_MVT < 0 THEN 0-QTE_MVT ELSE 0 END AS sortie_quantite, 0 AS entree_montant, 0 AS sortie_montant, 0 AS stock_quantite_debut, 0 AS stock_quantite_fin FROM w_DETAIL_STOCK LEFT JOIN eco.t_types_mouvements ON CODE_MOUVEMENT = t_types_mouvements.code_original LEFT JOIN eco.t_gestionnaires ON USER_DET_STOCK = t_gestionnaires.code_original LEFT JOIN eco.t_lieux ON NUM_STOCK = t_lieux.code_original LEFT JOIN eco.t_unites_fonctionnelles ON no_uf = t_unites_fonctionnelles.code_original LEFT JOIN eco.t_articles ON N_PROD = t_articles.code_original AND t_articles.oid != 0 LEFT JOIN eco.t_sites ON NUM_SOCIETE = t_sites.code_original LEFT JOIN eco.t_fournisseurs ON N_TIERS = t_fournisseurs.code_original LEFT JOIN eco.p_commandes ON no_com = p_commandes.code_original LEFT JOIN eco.p_lignes_commandes ON p_commandes.oid = commande_id AND p_lignes_commandes.article_id = t_articles.oid --LEFT JOIN eco.t_sections_analytiques ON CCE_1 = t_sections_analytiques.code_original WHERE DATE_MVT >= date('2015-01-01') ORDER BY DATE_MVT; UPDATE eco.p_mouvements_articles SET entree_montant_ht = p_mouvements_articles.entree_quantite * prix_unitaire, sortie_montant_ht = p_mouvements_articles.sortie_quantite * prix_unitaire ; -- mise à jour montants ht entrées si pas de commande (à partir des articles) UPDATE eco.p_mouvements_articles SET entree_montant = entree_montant_ht * (1 + tx_tva) FROM eco.t_articles JOIN prod_qsp.PRODUIT ON N_PROD = code_original JOIN w_tva ON PRODUIT.CODE_TVA = w_tva.CODE_TVA WHERE article_id = t_articles.oid AND entree_quantite != 0 AND entree_montant_ht != 0 AND entree_montant = 0 ; -- mise à jour montants ht sortie (à partir des articles) UPDATE eco.p_mouvements_articles SET sortie_montant = sortie_montant * (1 + tx_tva) FROM eco.t_articles JOIN prod_qsp.PRODUIT ON N_PROD = code_original JOIN w_tva ON PRODUIT.CODE_TVA = w_tva.CODE_TVA WHERE article_id = t_articles.oid AND sortie_quantite != 0 AND sortie_montant_ht != 0 AND entree_montant = 0 ; UPDATE eco.p_mouvements_articles SET entree_montant_ht = entree_quantite * base.cti_division(montant_liquidation_ht,quantite_livraison_approvisionnement), entree_montant = entree_quantite * base.cti_division(montant_liquidation_ttc,quantite_livraison_approvisionnement) FROM eco.p_lignes_commandes WHERE p_mouvements_articles.article_id = p_lignes_commandes.article_id AND p_mouvements_articles.commande_id = p_lignes_commandes.commande_id ; SELECT base.cti_enable_index('eco', 'i_mouvements_articles_1'); SELECT base.cti_enable_index('eco', 'i_mouvements_articles_2'); SELECT base.cti_enable_index('eco', 'i_mouvements_articles_3'); SELECT base.cti_enable_index('eco', 'i_mouvements_articles_4'); SELECT base.cti_enable_index('eco', 'i_mouvements_articles_5'); SELECT base.cti_enable_index('eco', 'i_mouvements_articles_6'); SELECT base.cti_enable_index('eco', 'i_mouvements_articles_7'); SELECT base.cti_enable_index('eco', 'i_mouvements_articles_8'); SELECT base.cti_enable_index('eco', 'i_mouvements_articles_9'); SELECT base.cti_enable_index('eco', 'i_mouvements_articles_10'); UPDATE eco.p_mouvements_articles SET entree_quantite = 0 - entree_quantite, sortie_quantite = 0 - sortie_quantite, entree_montant = 0 - entree_montant, sortie_montant = 0 - sortie_montant, entree_montant_ht = 0 - entree_montant_ht, sortie_montant_ht = 0 - sortie_montant_ht, sortie_montant_original_ttc = 0 - sortie_montant_original_ttc, sortie_montant_original_ht = 0 - sortie_montant_original_ht FROM eco.t_types_mouvements WHERE type_mouvement_id = t_types_mouvements.oid AND t_types_mouvements.particularite_inverser_signe = '1'; ]]>