0 AND
IDUNITE::text NOT IN (SELECT code_original FROM eco.t_unites WHERE code_original IS NOT NULL);
-- LPP
-- UCD
INSERT INTO base.t_ucd (code, texte, texte_court, laboratoire_texte, conditionnement_texte)
SELECT CODE_UCD_7, CODE_UCD_7, CODE_UCD_7, '', ''
FROM prod_evolucare_hygie.ARTICLES
WHERE CODE_UCD_7 <> '' AND
CODE_UCD_7 NOT IN (SELECT code FROM base.t_ucd WHERE code IS NOT NULL)
GROUP BY 1;
-- ATC
INSERT INTO eco.t_classification_atc(code_original, code, texte, texte_court )
SELECT UPPER(CODE_ATC), UPPER(CODE_ATC), MAX(LIBELLE_ATC), MAX(LIBELLE_ATC)
FROM prod_evolucare_hygie.ATC
LEFT JOIN eco.t_classification_atc ON code_original = UPPER(CODE_ATC)
WHERE code_original IS NULL
GROUP BY 1
ORDER BY 1;
-- Standard ATC
SELECT eco.cti_reorganize_classification_atc();
-- Comptes
INSERT INTO eco.t_compte(code_original, code, texte, texte_court)
SELECT
CPTE_ACHAT_FAMI_COMPTA code_original,
CPTE_ACHAT_FAMI_COMPTA AS code,
MAX(LIB_FAMI_COMPTA) AS texte,
MAX(LIB_FAMI_COMPTA) AS texte_court
FROM prod_evolucare_hygie.FAMILLES_COMPTA
WHERE length(CPTE_ACHAT_FAMI_COMPTA) > 5 AND
CPTE_ACHAT_FAMI_COMPTA NOT IN (SELECT code_original FROM eco.t_compte WHERE code_original IS NOT NULL) AND
CODE_FAMI_COMPTA IN (SELECT CODE_FAMI_COMPTA FROM prod_evolucare_hygie.ARTICLES)
GROUP BY 1
ORDER BY 1;
-- Types d'articles
INSERT INTO eco.t_types_articles(code_original, code, texte, texte_court)
SELECT NOMENCLATURE, NOMENCLATURE,NOMENCLATURE, NOMENCLATURE
FROM prod_evolucare_hygie.ARTICLES
WHERE NOMENCLATURE NOT IN (SELECT code_original FROM eco.t_types_articles WHERE code_original IS NOT NULL)
GROUP BY 1,2,3,4
ORDER BY 1;
-- Familles d'articles
INSERT INTO eco.t_familles_articles(code_original, code, texte, texte_court)
SELECT COD_FAMILLE_ART, COD_FAMILLE_ART,
MAX(CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END),
MAX(CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END)
FROM prod_evolucare_hygie.FAMILLE_ARTICLES
WHERE COD_FAMILLE_ART <> 0 AND
COD_FAMILLE_ART NOT IN (SELECT code_original FROM eco.t_familles_articles WHERE code_original IS NOT NULL)
GROUP BY 1,2
ORDER BY 1;
UPDATE eco.t_familles_articles
SET texte = CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END,
texte_court = CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END
FROM prod_evolucare_hygie.FAMILLE_ARTICLES
WHERE COD_FAMILLE_ART = code_original AND
(texte IS DISTINCT FROM CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END OR
texte_court IS DISTINCT FROM CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END);
-- Sous familles
INSERT INTO eco.t_sous_familles_articles(code_original, code, texte, texte_court)
SELECT COD_FAMILLE_ART, COD_FAMILLE_ART,
MAX(CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END),
MAX(CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END)
FROM prod_evolucare_hygie.FAMILLE_ARTICLES
WHERE COD_FAMILLE_ART <> 0 AND
LIB_FAMILLE_ART <> '' AND
COD_FAMILLE_ART NOT IN (SELECT code_original FROM eco.t_sous_familles_articles WHERE code_original IS NOT NULL)
GROUP BY 1,2
ORDER BY 1;
UPDATE eco.t_sous_familles_articles
SET texte = CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END,
texte_court = CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END
FROM prod_evolucare_hygie.FAMILLE_ARTICLES
WHERE COD_FAMILLE_ART = code_original AND
(texte IS DISTINCT FROM CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END OR
texte_court IS DISTINCT FROM CASE WHEN LIB_FAMILLE_ART <> '' THEN LIB_FAMILLE_ART ELSE COD_FAMILLE_ART END);
-- Catégories
-- Sous catégories
-- Articles
INSERT INTO eco.t_articles(code_original, code, texte, texte_court)
SELECT REF_ART_INT,
REF_ART_INT,
DESIGN_USUELLE,
substr(DESIGN_USUELLE,1,50)
FROM prod_evolucare_hygie.ARTICLES
LEFT JOIN eco.t_articles ON (code_original = REF_ART_INT)
WHERE code_original IS NULL;
UPDATE eco.t_articles
SET
texte = DESIGN_USUELLE,
texte_court = substr(DESIGN_USUELLE,1,50)
FROM prod_evolucare_hygie.ARTICLES
WHERE code_original = REF_ART_INT
AND (
texte IS DISTINCT FROM DESIGN_USUELLE OR
texte_court IS DISTINCT FROM substr(DESIGN_USUELLE,1,50))
;
UPDATE eco.t_articles SET
type_id = COALESCE(t_types_articles.oid,0),
famille_id = COALESCE(t_familles_articles.oid,0),
sous_famille_id = COALESCE(t_sous_familles_articles.oid,0),
categorie_id = COALESCE(t_categories_articles.oid,0),
sous_categorie_id = COALESCE(t_sous_categories_articles.oid,0),
classification_atc_id = COALESCE(t_classification_atc.oid,0),
unite_stockage_id = COALESCE(t_unites_stockage.oid,0),
unite_distribution_id = COALESCE(t_unites_distribution.oid,0),
lpp_id = COALESCE(t_lpp.oid,0),
ucd_id = COALESCE(t_ucd.oid,0)
FROM prod_evolucare_hygie.ARTICLES
LEFT JOIN prod_evolucare_hygie.FAMILLE_ARTICLES ON ARTICLES.COD_FAMILLE_ART::text = FAMILLE_ARTICLES.COD_FAMILLE_ART
LEFT JOIN eco.t_familles_articles ON CASE WHEN ARTICLES.SOUS_FAMILLE <> '' THEN ARTICLES.SOUS_FAMILLE ELSE FAMILLE_ARTICLES.COD_FAMILLE_ART::text END = t_familles_articles.code_original
LEFT JOIN eco.t_sous_familles_articles ON FAMILLE_ARTICLES.COD_FAMILLE_ART = t_sous_familles_articles.code_original
LEFT JOIN eco.t_types_articles ON NOMENCLATURE = upper(t_types_articles.code_original)
LEFT JOIN eco.t_classification_atc ON CODE_ATC = upper(t_classification_atc.code_original)
LEFT JOIN eco.t_categories_articles ON t_categories_articles.oid = 0
LEFT JOIN eco.t_sous_categories_articles ON t_sous_categories_articles.oid = 0
LEFT JOIN eco.t_unites t_unites_stockage ON t_unites_stockage.oid = 0
LEFT JOIN eco.t_unites t_unites_distribution ON t_unites_distribution.oid = 0
LEFT JOIN base.t_lpp ON t_lpp.oid = 0
LEFT JOIN base.t_ucd ON CODE_UCD_7 = t_ucd.code
WHERE
t_articles.code_original = REF_ART_INT AND
(t_articles.type_id IS DISTINCT FROM COALESCE(t_types_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.categorie_id IS DISTINCT FROM COALESCE(t_categories_articles.oid,0) OR
t_articles.sous_categorie_id IS DISTINCT FROM COALESCE(t_sous_categories_articles.oid,0) OR
t_articles.classification_atc_id IS DISTINCT FROM COALESCE(t_classification_atc.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) OR
lpp_id IS DISTINCT FROM COALESCE(t_lpp.oid,0) OR
ucd_id IS DISTINCT FROM COALESCE(t_ucd.oid,0)
);
UPDATE eco.t_articles SET
compte_id = COALESCE(t_compte.oid,0)
FROM prod_evolucare_hygie.ARTICLES
LEFT JOIN prod_evolucare_hygie.FAMILLES_COMPTA ON ARTICLES.CODE_FAMI_COMPTA = FAMILLES_COMPTA.CODE_FAMI_COMPTA
LEFT JOIN eco.t_compte ON CPTE_ACHAT_FAMI_COMPTA = t_compte.code_original
WHERE
t_articles.code_original = REF_ART_INT AND
(
t_articles.compte_id IS DISTINCT FROM COALESCE(t_compte.oid,0)
);
UPDATE eco.t_articles SET compte_id = 0 WHERE compte_id IS NULL;
UPDATE eco.t_articles
SET gere_en_stock = '1'
FROM prod_evolucare_hygie.ARTICLES
WHERE REF_ART_INT = code_original AND
gere_en_stock != '1'
;
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
);
]]>
'' THEN REF_ART_FOURN ELSE REF_ART_INT END AS ref_fournisseur_texte
FROM eco.t_articles
JOIN prod_evolucare_hygie.ARTICLES ON REF_ART_INT = t_articles.code_original
JOIN eco.t_fournisseurs ON CODE_FOURN = t_fournisseurs.code
GROUP BY 1,2,3
ORDER BY 1,2,3
) subview
GROUP BY 1
) subview
WHERE t_articles.oid = subview.article_id AND
(
t_articles.ref_fournisseur_id IS DISTINCT FROM subview.ref_fournisseur_id OR
t_articles.ref_fournisseur_texte IS DISTINCT FROM subview.ref_fournisseur_texte
)
;
]]>
'' AND
TCATEGORIECOMMERCIALPRODUIT.OID IN (SELECT OIDCATEGORIECOMMERCIAL FROM w_TPRODUIT) AND
TCATEGORIECOMMERCIALPRODUIT.OID NOT IN (SELECT code_original FROM eco.t_familles_articles WHERE code_original IS NOT NULL)
GROUP BY 1,2
ORDER BY 2;
UPDATE eco.t_familles_articles
SET texte = CAPTION,
texte_court = CAPTION
FROM prod_sage100compta.TCATEGORIECOMMERCIALPRODUIT
WHERE TCATEGORIECOMMERCIALPRODUIT.OID = code_original AND
(texte IS DISTINCT FROM CAPTION OR
texte_court IS DISTINCT FROM SUBSTR(TCATEGORIECOMMERCIALPRODUIT.CAPTION,50))
;
-- Sous familles
-- Catégories
INSERT INTO eco.t_categories_articles(code_original, code, texte, texte_court)
SELECT TCATEGORIECOMPTAPRODUIT.OID,
TCATEGORIECOMPTAPRODUIT.CODE,
MAX(TCATEGORIECOMPTAPRODUIT.CAPTION),
MAX(TCATEGORIECOMPTAPRODUIT.CAPTION)
FROM prod_sage100compta.TCATEGORIECOMPTAPRODUIT
WHERE TCATEGORIECOMPTAPRODUIT.OID <> '' AND
TCATEGORIECOMPTAPRODUIT.OID IN (SELECT OIDCATEGORIECOMPTAPRODUIT FROM w_TPRODUIT) AND
TCATEGORIECOMPTAPRODUIT.OID NOT IN (SELECT code_original FROM eco.t_categories_articles WHERE code_original IS NOT NULL)
GROUP BY 1,2
ORDER BY 2;
UPDATE eco.t_categories_articles
SET texte = CAPTION,
texte_court = CAPTION
FROM prod_sage100compta.TCATEGORIECOMPTAPRODUIT
WHERE TCATEGORIECOMPTAPRODUIT.OID = code_original AND
(texte IS DISTINCT FROM CAPTION OR
texte_court IS DISTINCT FROM CAPTION);
-- Sous catégories
-- Articles
INSERT INTO eco.t_articles(code_original, code, texte, texte_court)
SELECT TPRODUIT.OID,
substr(TPRODUIT.CODE,1,30),
TPRODUIT.CAPTION,
substr(TPRODUIT.CAPTION,1,50)
FROM w_TPRODUIT TPRODUIT
LEFT JOIN eco.t_articles ON TPRODUIT.OID = code_original
WHERE TPRODUIT.OID IN
(SELECT DISTINCT TPRODUITCOMMERCIAL.OIDPRODUIT
FROM prod_sage100compta.TDETAILLIVRAISON
JOIN prod_sage100compta.TPRODUITCOMMERCIAL ON OIDPRODUITCOMMERCIAL = TPRODUITCOMMERCIAL.OID
) AND
code_original IS NULL;
UPDATE eco.t_articles
SET
texte = CAPTION,
texte_court = substr(CAPTION,1,50)
FROM prod_sage100compta.TPRODUIT
WHERE code_original = TPRODUIT.OID
AND (
texte IS DISTINCT FROM CAPTION OR
texte_court IS DISTINCT FROM substr(CAPTION,1,50))
;
UPDATE eco.t_articles SET
type_id = COALESCE(t_types_articles.oid,0),
famille_id = COALESCE(t_familles_articles.oid,0),
sous_famille_id = COALESCE(t_sous_familles_articles.oid,0),
categorie_id = COALESCE(t_categories_articles.oid,0),
sous_categorie_id = COALESCE(t_sous_categories_articles.oid,0),
classification_atc_id = COALESCE(t_classification_atc.oid,0),
unite_stockage_id = COALESCE(t_unites_stockage.oid,0),
unite_distribution_id = COALESCE(t_unites_distribution.oid,0),
lpp_id = COALESCE(t_lpp.oid,0),
ucd_id = COALESCE(t_ucd.oid,0)
FROM w_TPRODUIT TPRODUIT
LEFT JOIN eco.t_familles_articles ON OIDCATEGORIECOMMERCIAL = t_familles_articles.code_original
LEFT JOIN eco.t_sous_familles_articles ON t_sous_familles_articles.oid = 0
LEFT JOIN eco.t_types_articles ON NOMENCLATURE = upper(t_types_articles.code_original)
LEFT JOIN eco.t_classification_atc ON t_classification_atc.oid = 0
LEFT JOIN eco.t_categories_articles ON OIDCATEGORIECOMPTAPRODUIT = t_categories_articles.code_original
LEFT JOIN eco.t_sous_categories_articles ON t_sous_categories_articles.oid = 0
LEFT JOIN eco.t_unites t_unites_stockage ON t_unites_stockage.oid = 0
LEFT JOIN eco.t_unites t_unites_distribution ON t_unites_distribution.oid = 0
LEFT JOIN base.t_lpp ON t_lpp.oid = 0
LEFT JOIN base.t_ucd ON t_ucd.oid = 0
WHERE
t_articles.code_original = TPRODUIT.OID AND
(t_articles.type_id IS DISTINCT FROM COALESCE(t_types_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.categorie_id IS DISTINCT FROM COALESCE(t_categories_articles.oid,0) OR
t_articles.sous_categorie_id IS DISTINCT FROM COALESCE(t_sous_categories_articles.oid,0) OR
t_articles.classification_atc_id IS DISTINCT FROM COALESCE(t_classification_atc.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) OR
lpp_id IS DISTINCT FROM COALESCE(t_lpp.oid,0) OR
ucd_id IS DISTINCT FROM COALESCE(t_ucd.oid,0)
);
UPDATE eco.t_articles SET
compte_id = COALESCE(t_compte.oid,0)
FROM w_TPRODUIT TPRODUIT
LEFT JOIN eco.t_compte ON compte_numero = t_compte.code_original
WHERE
t_articles.code_original = TPRODUIT.OID AND
(
t_articles.compte_id IS DISTINCT FROM COALESCE(t_compte.oid,0)
);
UPDATE eco.t_articles SET compte_id = 0 WHERE compte_id IS NULL
;
UPDATE eco.t_articles
SET gere_en_stock = '1'
FROM w_TPRODUIT TPRODUIT
WHERE TPRODUIT.OID = code_original AND
gere_en_stock != '1'
;
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
);
]]>
''
GROUP BY 1;
UPDATE w_REFERENCE
SET code = upper(translate(REFERENCE,'éčôŕ-./°,.,''()?','eeoa '))
;
UPDATE w_REFERENCE
SET code = replace(code,' ',' ')
WHERE code LIKE '% %'
;
UPDATE w_REFERENCE
SET code = replace(code,' ',' ')
WHERE code LIKE '% %'
;
UPDATE w_REFERENCE
SET code = replace(code,' ',' ')
WHERE code LIKE '% %'
;
UPDATE w_REFERENCE
SET code =
substr(split_part(code,' ',1),1,5) ||
substr(split_part(code,' ',2),1,5)
WHERE split_part(code,' ',3) = ''
;
UPDATE w_REFERENCE
SET code =
substr(split_part(code,' ',1),1,2) ||
substr(split_part(code,' ',2),1,2) ||
substr(split_part(code,' ',3),1,2) ||
substr(split_part(code,' ',4),1,2) ||
substr(split_part(code,' ',5),1,2)
WHERE split_part(code,' ',3) <> ''
;
INSERT INTO eco.t_unites_fonctionnelles(code_original, code, texte, texte_court)
SELECT OID, CODE, CAPTION, CAPTION
FROM prod_sage100compta.TSITECOMMERCIALE
WHERE OID IN (SELECT OIDSITELIVRAISON FROM prod_sage100compta.TPIECELIVRAISON) AND
OID NOT IN (SELECT code_original FROM eco.t_unites_fonctionnelles WHERE code_original IS NOT NULL)
;
-- Lieux
-- Site
-- Types mouvements
INSERT INTO eco.t_types_mouvements(code_original, code, texte, texte_court)
SELECT TTYPEPIECEGC.OID, TTYPEPIECEGC.CODE, MAX(TTYPEPIECEGC.CAPTION), MAX(TTYPEPIECEGC.CAPTION)
FROM prod_sage100compta.TTYPEPIECEGC
WHERE TTYPEPIECEGC.OID IN
(
SELECT DISTINCT OIDTYPEPIECEGC
FROM prod_sage100compta.TPIECELIVRAISON
) AND
TTYPEPIECEGC.OID NOT IN (SELECT code_original FROM eco.t_types_mouvements WHERE code_original IS NOT NULL)
GROUP BY 1,2
ORDER BY 2
;
]]>
= '[ENV_ECO_ANNEEDEBUT]-01-01'
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_LIGNECDE_FOURN;
CREATE TEMP TABLE w_LIGNECDE_FOURN AS
SELECT *,
0::numeric AS QTE_LIGNELIV_FOURN
FROM prod_evolucare_hygie.LIGNECDE_FOURN
;
UPDATE w_LIGNECDE_FOURN LIGNECDE_FOURN SET
QTE_LIGNELIV_FOURN = subview.QTE_LIGNELIV_FOURN
FROM
(
SELECT
LIGNELIV_FOURN.IDLIGNECDE_FOURN_INCHANGEABLE, SUM(QTE_LIGNELIV_FOURN) AS QTE_LIGNELIV_FOURN
FROM prod_evolucare_hygie.LIV_FOURN
JOIN prod_evolucare_hygie.LIGNELIV_FOURN ON LIV_FOURN.NUM_LIV_FOURN = LIGNELIV_FOURN.NUM_LIV_FOURN
JOIN prod_evolucare_hygie.LIGNECDE_FOURN ON LIGNELIV_FOURN.IDLIGNECDE_FOURN_INCHANGEABLE = LIGNECDE_FOURN.IDLIGNECDE_FOURN_INCHANGEABLE
GROUP BY 1
) subview
WHERE subview.IDLIGNECDE_FOURN_INCHANGEABLE = LIGNECDE_FOURN.IDLIGNECDE_FOURN_INCHANGEABLE
;
UPDATE w_LIGNECDE_FOURN LIGNECDE_FOURN SET
QTE_LIGNELIV_FOURN = QTE_LIGNECDE_FOURN
WHERE QTE_LIGNELIV_FOURN <> QTE_LIGNECDE_FOURN AND
QTE_LIGNELIV_FOURN = QTE_LIGNECDE_FOURN * COND1_LIGNECDE_FOURN
;
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,
taux_prorata_tva)
SELECT
IDLIGNECDE_FOURN_INCHANGEABLE AS code_original,
p_commandes.oid AS commande_id,
NUMLI_LIGNECDE_FOURN::numeric AS ligne_commande,
LIB_LIGNECDE_FOURN AS texte,
COALESCE(t_articles.oid, 0) AS article_id,
0::bigint AS unite_approvisionnement_id,
t_articles.compte_id,
COALESCE(t_ucd.oid, t_articles.ucd_id, 0) AS ucd_id,
COALESCE(t_lpp.oid, t_articles.lpp_id, 0) AS lpp_id,
QTE_LIGNECDE_FOURN AS quantite_approvisionnement,
PA_LIGNECDE_FOURN AS prix_unitaire_approvisionnement,
0::numeric AS multiplicateur_stockage,
0::numeric AS quantite_stockage,
0::numeric AS prix_unitaire_stockage,
round(QTE_LIGNECDE_FOURN::numeric * PA_LIGNECDE_FOURN::numeric,2) AS montant_commande_ht,
0::numeric AS montant_commande_remise,
round(QTE_LIGNECDE_FOURN::numeric * PA_LIGNECDE_FOURN::numeric * VAL_TVA_LIGNECDE_FOURN / 100,2) AS montant_commande_tva,
0::numeric AS montant_commande_port,
round(QTE_LIGNECDE_FOURN::numeric * PA_LIGNECDE_FOURN::numeric,2) +
round(QTE_LIGNECDE_FOURN::numeric * PA_LIGNECDE_FOURN::numeric * VAL_TVA_LIGNECDE_FOURN / 100,2) AS montant_commande_ttc,
QTE_LIGNELIV_FOURN AS quantite_livraison_approvisionnement,
QTE_LIGNELIV_FOURN AS quantite_livraison_stockage,
round(QTE_LIGNELIV_FOURN::numeric * PA_LIGNECDE_FOURN::numeric,2) AS montant_livraison_ht,
round(QTE_LIGNELIV_FOURN::numeric * PA_LIGNECDE_FOURN::numeric,2) +
round(QTE_LIGNELIV_FOURN::numeric * PA_LIGNECDE_FOURN::numeric * VAL_TVA_LIGNECDE_FOURN / 100,2) AS montant_livraison_ttc,
CASE WHEN QTE_LIGNELIV_FOURN = QTE_LIGNECDE_FOURN THEN 'T' WHEN QTE_LIGNELIV_FOURN > QTE_LIGNECDE_FOURN THEN 'S' WHEN QTE_LIGNELIV_FOURN = 0 THEN 'N' ELSE 'P' END AS etat_livraison,
0::numeric AS montant_liquidation_ht,
0::numeric AS montant_liquidation_remise,
0::numeric AS montant_liquidation_tva,
0::numeric AS montant_liquidation_port,
0::numeric AS montant_liquidation_ttc,
'T',
0::numeric
FROM w_LIGNECDE_FOURN LIGNECDE_FOURN
JOIN eco.p_commandes ON LIGNECDE_FOURN.NUM_CDE_FOURN = p_commandes.code_original
LEFT JOIN eco.t_articles ON REF_ART_INT = 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
LEFT JOIN eco.t_prorata_tva ON p_commandes.date_engagement BETWEEN t_prorata_tva.date_debut AND t_prorata_tva.date_fin
;
UPDATE eco.p_commandes SET
montant_commande_ht = subview.montant_commande_ht,
montant_commande_tva = subview.montant_commande_tva,
montant_commande_total = subview.montant_commande_ttc,
etat_reception = subview.etat_livraison
FROM
(
SELECT
commande_id,
SUM(p_lignes_commandes.montant_commande_ht) AS montant_commande_ht,
SUM(p_lignes_commandes.montant_commande_tva) AS montant_commande_tva,
SUM(p_lignes_commandes.montant_commande_ttc) AS montant_commande_ttc,
MIN(CASE WHEN etat_livraison NOT IN ('S','T') THEN 'N' ELSE 'T' END) AS etat_livraison
FROM eco.p_lignes_commandes
GROUP BY 1
) subview
WHERE commande_id = p_commandes.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');
]]>
= date('[ENV_ECO_ANNEEDEBUT]-01-01')
ORDER BY LIV_FOURN.DATE_LIV_FOURN
;
INSERT INTO eco.p_mouvements_articles(
date,
sens_mouvement,
type_mouvement_id,
texte,
gestionnaire_id,
lieu_id,
unite_fonctionnelle_id,
article_id,
compte_id,
fournisseur_id,
commande_id,
ligne_commande,
lpp_id,
ucd_id,
prix_unitaire,
entree_quantite,
entree_montant,
entree_montant_ht,
sortie_quantite,
sortie_montant,
sortie_montant_ht)
SELECT
DATE(DATE_LIV_CLIENT) AS date,
'S' AS sens_mouvement,
COALESCE(t_types_mouvements.oid,0) AS type_mouvement_id,
t_types_mouvements.texte || ' ' || t_articles.code 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_articles.fournisseur_principal_id,0) AS fournisseur_id,
COALESCE(p_commandes.oid,0) AS commande_id,
0 AS ligne_commande,
COALESCE(t_articles.lpp_id,0) AS lppid,
COALESCE(t_articles.ucd_id,0) AS ucd_id,
PMP AS prix_unitaire,
0::numeric AS entree_quantite,
0::numeric AS entree_montant,
0::numeric AS entree_montant_ht,
QTE_LIGNELIV_CLIENT AS sortie_quantite,
(QTE_LIGNELIV_CLIENT * PMP) + (QTE_LIGNELIV_CLIENT * PMP * VAL_TVA_LIGNELIV_CLIENT / 100) AS sortie_montant,
QTE_LIGNELIV_CLIENT * PMP AS sortie_montant_ht
FROM prod_evolucare_hygie.LIGNELIV_CLI
JOIN prod_evolucare_hygie.LIV_CLIENT ON LIGNELIV_CLI.NUM_LIV_CLIENT = LIV_CLIENT.NUM_LIV_CLIENT
LEFT JOIN eco.p_commandes ON 'XXX' = p_commandes.code_original
LEFT JOIN eco.t_types_mouvements ON 'XXX' = t_types_mouvements.code_original
LEFT JOIN eco.t_gestionnaires ON 'XXX' = t_gestionnaires.code_original
LEFT JOIN eco.t_lieux ON 'XXX' = t_lieux.code_original
LEFT JOIN eco.t_unites_fonctionnelles ON CODE_CLIENT = t_unites_fonctionnelles.code_original
LEFT JOIN eco.t_articles ON LIGNELIV_CLI.REF_ART_INT = t_articles.code_original AND t_articles.oid != 0
WHERE DATE(DATE_LIV_CLIENT) >= date('[ENV_ECO_ANNEEDEBUT]-01-01')
ORDER BY DATE_LIV_CLIENT;
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';
]]>
0
;
SELECT base.cti_execute('
INSERT INTO w_stock
SELECT
w_stock.site_code_original,
w_stock.article_code_original,
w_stock.article_id,
w_stock.compte_id,
w_stock.ucd_id,
w_stock.lpp_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_code + 1,
0 AS entree_quantite,
0 AS entree_montant,
0 AS sortie_quantite,
0 AS sortie_montant,
0::numeric AS pump,
''0''::text AS ok_pump,
0::numeric AS stock_quantite_debut,
w_stock.stock_quantite_fin,
0::numeric AS stock_valeur_debut,
0::numeric AS 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_id = w_stock_next.article_id AND
w_stock.mois_code = w_stock_next.mois_code - 1
WHERE w_stock.stock_quantite_fin <> 0 AND
w_stock_next.mois_code IS NULL AND
w_stock.date_debut <= date(now())
',200)
;
UPDATE w_stock
SET ok_pump = '1',
pump = X_PMPFINP_0,
stock_valeur_fin = stock_quantite_fin * X_PMPFINP_0
FROM prod_sagex3.XMVTSART
WHERE article_code_original = X_ITMREF_0 AND
site_code_original = X_STOFCY_0 AND
date_part('year',date_fin) = X_YEA_0 AND
date_part('month',date_fin) = X_MON_0
;
UPDATE w_stock
SET ok_pump = '1',
pump = AVC_0,
stock_valeur_fin = stock_quantite_fin * AVC_0
FROM prod_sagex3.ITMMVT
WHERE article_code_original = ITMMVT.ITMREF_0 AND
site_code_original = ITMMVT.STOFCY_0 AND
ok_pump = '0'
;
UPDATE w_stock SET
stock_quantite_debut = w_stock_prec.stock_quantite_fin,
stock_valeur_debut = w_stock_prec.stock_valeur_fin
FROM w_stock w_stock_prec
WHERE w_stock.site_id = w_stock_prec.site_id AND
w_stock.lieu_id = w_stock_prec.lieu_id AND
w_stock.article_id = w_stock_prec.article_id AND
w_stock.mois_code = w_stock_prec.mois_code + 1
;
TRUNCATE eco.p_stock
;
INSERT INTO eco.p_stock
(
article_id,
compte_id,
ucd_id,
lpp_id,
fournisseur_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,
ucd_id,
lpp_id,
fournisseur_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
;
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');
]]>
= '[ENV_ECO_ANNEEDEBUT]-01-01'
GROUP BY 1,2,3,4,5,6,7
ORDER BY 1;
SELECT base.cti_enable_index('eco', 'i_facture_1');
SELECT base.cti_enable_index('eco', 'i_facture_2');
SELECT base.cti_enable_index('eco', 'i_facture_3');
SELECT base.cti_enable_index('eco', 'i_facture_4');
SELECT base.cti_enable_index('eco', 'i_facture_5');
SELECT base.cti_disable_index('eco', 'i_lignes_facture_1');
SELECT base.cti_disable_index('eco', 'i_lignes_facture_2');
SELECT base.cti_disable_index('eco', 'i_lignes_facture_3');
SELECT base.cti_disable_index('eco', 'i_lignes_facture_4');
TRUNCATE eco.p_lignes_facture;
INSERT INTO eco.p_lignes_facture(
article_id,
code_original,
compte_id,
facture_id,
ligne_commande,
ligne_commande_id,
montant_facture_article_ht,
montant_facture_article_ttc,
montant_facture_article_tva,
montant_facture_port_ht,
montant_facture_port_ttc,
montant_facture_port_tva,
montant_facture_ht,
montant_facture_ttc,
montant_facture_tva
)
SELECT
t_articles.oid,
PINVOICED.NUM_0 || '-' || PINVOICED.PIDLIN_0 AS code_original,
0 as compte_id,
p_facture.oid,
ligne_commande,
p_lignes_commandes.oid,
AMTNOTLIN_0 AS montant_facture_article_ht,
AMTATILIN_0 AS montant_facture_article_ttc,
AMTTAXLIN1_0 AS montant_facture_article_tva,
0::numeric,
0::numeric,
0::numeric,
AMTNOTLIN_0 AS montant_facture_ht,
AMTATILIN_0 AS montant_facture_ttc,
AMTTAXLIN1_0 AS montant_facture_tva
FROM prod_sagex3.PINVOICED
JOIN eco.t_articles ON ITMREF_0 = t_articles.code_original
JOIN eco.p_facture ON NUM_0 = p_facture.code_original
JOIN eco.p_lignes_commandes ON POHNUM_0 || '-' || POPLIN_0 = p_lignes_commandes.code_original
LEFT JOIN prod_sagex3.GACCENTRY ON GACCENTRY.NUM_0 = PINVOICED.NUM_0
WHERE PINVOICED.ACCDAT_0 >= '[ENV_ECO_ANNEEDEBUT]-01-01'
ORDER BY 2;
SELECT base.cti_enable_index('eco', 'i_lignes_facture_1');
SELECT base.cti_enable_index('eco', 'i_lignes_facture_2');
SELECT base.cti_enable_index('eco', 'i_lignes_facture_3');
SELECT base.cti_enable_index('eco', 'i_lignes_facture_4');
]]>
= '[ENV_ECO_ANNEEDEBUT]-01-01'
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');
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,
taux_prorata_tva)
SELECT
TDETAILTAXABLE.oid AS code_original,
p_commandes.oid AS commande_id,
TDETAILTAXABLE.NUMERO AS ligne_commande,
DESIGNATIONPRODUIT AS texte,
COALESCE(t_articles.oid, 0) AS article_id,
0::bigint AS unite_approvisionnement_id,
t_articles.compte_id,
COALESCE(t_ucd.oid, t_articles.ucd_id, 0) AS ucd_id,
COALESCE(t_lpp.oid, t_articles.lpp_id, 0) AS lpp_id,
QUANTITETARIFEE AS quantite_approvisionnement,
PRIXNET AS prix_unitaire_approvisionnement,
0::numeric AS multiplicateur_stockage,
0::numeric AS quantite_stockage,
0::numeric AS prix_unitaire_stockage,
round(QUANTITETARIFEE::numeric * PRIXNET::numeric,2) AS montant_commande_ht,
0::numeric AS montant_commande_remise,
round(QUANTITETARIFEE::numeric * PRIXNET::numeric * TTAUXADATE.TAUX::numeric / 100,2) AS montant_commande_tva,
0::numeric AS montant_commande_port,
round(QUANTITETARIFEE::numeric * PRIXNET::numeric,2) +
round(QUANTITETARIFEE::numeric * PRIXNET::numeric * TTAUXADATE.TAUX::numeric / 100,2) AS montant_commande_ttc,
QUANTITETARIFEE AS quantite_livraison_approvisionnement,
QUANTITETARIFEE AS quantite_livraison_stockage,
round(QUANTITETARIFEE::numeric * PRIXNET::numeric,2) AS montant_livraison_ht,
round(QUANTITETARIFEE::numeric * PRIXNET::numeric,2) +
round(QUANTITETARIFEE::numeric * PRIXNET::numeric * TTAUXADATE.TAUX::numeric / 100,2) AS montant_livraison_ttc,
CASE WHEN STATUTLIVRAISON = 3 THEN 'T' WHEN STATUTLIVRAISON = 2 THEN 'S' WHEN STATUTLIVRAISON = 0 THEN 'N' ELSE 'P' END AS etat_livraison,
0::numeric AS montant_liquidation_ht,
0::numeric AS montant_liquidation_remise,
0::numeric AS montant_liquidation_tva,
0::numeric AS montant_liquidation_port,
0::numeric AS montant_liquidation_ttc,
'T',
0::numeric
FROM prod_sage100compta.TDETAILTAXABLE
JOIN eco.p_commandes ON TDETAILTAXABLE.OIDCONTRATCOMMERCIAL = p_commandes.code_original
JOIN (SELECT
oidmodetva, oidtva
FROM prod_sage100compta.TAPPLICATIONREGIMETVA
GROUP BY 1,2 ) TAPPLICATIONREGIMETVA ON TAPPLICATIONREGIMETVA.oidmodetva = TDETAILTAXABLE.oidmodetva
JOIN (SELECT
oidtva,
tdate,
(COALESCE (lead(tdate) OVER (PARTITION BY oidtva ORDER by tdate), '2099-12-31'))::date - interval '1 day' AS t_date_max,
taux
FROM
prod_sage100compta.TTAUXADATE)
TTAUXADATE ON TAPPLICATIONREGIMETVA.oidtva = TTAUXADATE.oidtva AND date_engagement between tdate and t_date_max
JOIN prod_sage100compta.TPRODUITCOMMERCIAL ON OIDPRODUITCOMMERCIAL = TPRODUITCOMMERCIAL.OID
LEFT JOIN eco.t_articles ON TPRODUITCOMMERCIAL.OIDPRODUIT = 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
LEFT JOIN eco.t_prorata_tva ON p_commandes.date_engagement BETWEEN t_prorata_tva.date_debut AND t_prorata_tva.date_fin
;
UPDATE eco.p_commandes SET
montant_commande_ht = subview.montant_commande_ht,
montant_commande_tva = subview.montant_commande_tva,
montant_commande_total = subview.montant_commande_ttc,
etat_reception = subview.etat_livraison
FROM
(
SELECT
commande_id,
SUM(p_lignes_commandes.montant_commande_ht) AS montant_commande_ht,
SUM(p_lignes_commandes.montant_commande_tva) AS montant_commande_tva,
SUM(p_lignes_commandes.montant_commande_ttc) AS montant_commande_ttc,
MIN(CASE WHEN etat_livraison NOT IN ('S','T') THEN 'N' ELSE 'T' END) AS etat_livraison
FROM eco.p_lignes_commandes
GROUP BY 1
) subview
WHERE commande_id = p_commandes.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');
]]>
= '20140101' THEN taux_tva_1 ELSE taux_tva_2 END AS entree_montant,
CASE WHEN TTYPEPIECEGC.CODE = 'PLA' THEN MONTANTNET ELSE 0 END AS entree_montant_ht,
CASE WHEN TTYPEPIECEGC.CODE = 'PLV' THEN QUANTITE ELSE 0 END AS sortie_quantite,
CASE WHEN TTYPEPIECEGC.CODE = 'PLV' THEN MONTANTNET ELSE 0 END * CASE WHEN DATE(TDETAILLIVRAISON.DATELIVRAISONPREVUE) >= '20140101' THEN taux_tva_1 ELSE taux_tva_2 END AS sortie_montant,
CASE WHEN TTYPEPIECEGC.CODE = 'PLV' THEN MONTANTNET ELSE 0 END AS sortie_montant_ht
FROM prod_sage100compta.TDETAILLIVRAISON
JOIN prod_sage100compta.TPIECELIVRAISON ON TDETAILLIVRAISON.OIDPIECELIVRAISON = TPIECELIVRAISON.OID
JOIN prod_sage100compta.TPRODUITCOMMERCIAL ON OIDPRODUITCOMMERCIAL = TPRODUITCOMMERCIAL.OID
JOIN prod_sage100compta.TTYPEPIECEGC ON OIDTYPEPIECEGC = TTYPEPIECEGC.OID
JOIN w_TPRODUIT TPRODUIT ON TPRODUITCOMMERCIAL.OIDPRODUIT = TPRODUIT.OID
LEFT JOIN prod_sage100compta.TROLETIERS ON TPIECELIVRAISON.OIDROLETIERSRO = TROLETIERS.OID
LEFT JOIN eco.p_commandes ON p_commandes.oid = 0
LEFT JOIN eco.p_lignes_commandes ON p_lignes_commandes.oid = 0
LEFT JOIN eco.t_types_mouvements ON OIDTYPEPIECEGC = t_types_mouvements.code_original
LEFT JOIN eco.t_fournisseurs ON TROLETIERS.OIDTIERS = t_fournisseurs.code_original
LEFT JOIN eco.t_gestionnaires ON 'XXX' = t_gestionnaires.code_original
LEFT JOIN eco.t_lieux ON 'XXX' = t_lieux.code_original
LEFT JOIN eco.t_unites_fonctionnelles ON TPIECELIVRAISON.OIDSITELIVRAISON = t_unites_fonctionnelles.code_original
LEFT JOIN eco.t_articles ON TPRODUITCOMMERCIAL.OIDPRODUIT = t_articles.code_original AND t_articles.oid != 0
WHERE DATE(TDETAILLIVRAISON.DATELIVRAISONPREVUE) >= date('[ENV_ECO_ANNEEDEBUT]-01-01')
ORDER BY TDETAILLIVRAISON.DATELIVRAISONPREVUE
;
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');
]]>
= '[ENV_ECO_ANNEEDEBUT]-01-01'
GROUP BY 1,2,3,4,5,6,7
ORDER BY 1;
SELECT base.cti_enable_index('eco', 'i_facture_1');
SELECT base.cti_enable_index('eco', 'i_facture_2');
SELECT base.cti_enable_index('eco', 'i_facture_3');
SELECT base.cti_enable_index('eco', 'i_facture_4');
SELECT base.cti_enable_index('eco', 'i_facture_5');
SELECT base.cti_disable_index('eco', 'i_lignes_facture_1');
SELECT base.cti_disable_index('eco', 'i_lignes_facture_2');
SELECT base.cti_disable_index('eco', 'i_lignes_facture_3');
SELECT base.cti_disable_index('eco', 'i_lignes_facture_4');
TRUNCATE eco.p_lignes_facture;
INSERT INTO eco.p_lignes_facture(
article_id,
code_original,
compte_id,
facture_id,
ligne_commande,
ligne_commande_id,
montant_facture_article_ht,
montant_facture_article_ttc,
montant_facture_article_tva,
montant_facture_port_ht,
montant_facture_port_ttc,
montant_facture_port_tva,
montant_facture_ht,
montant_facture_ttc,
montant_facture_tva
)
SELECT
t_articles.oid,
PINVOICED.NUM_0 || '-' || PINVOICED.PIDLIN_0 AS code_original,
0 as compte_id,
p_facture.oid,
ligne_commande,
p_lignes_commandes.oid,
AMTNOTLIN_0 AS montant_facture_article_ht,
AMTATILIN_0 AS montant_facture_article_ttc,
AMTTAXLIN1_0 AS montant_facture_article_tva,
0::numeric,
0::numeric,
0::numeric,
AMTNOTLIN_0 AS montant_facture_ht,
AMTATILIN_0 AS montant_facture_ttc,
AMTTAXLIN1_0 AS montant_facture_tva
FROM prod_sagex3.PINVOICED
JOIN eco.t_articles ON ITMREF_0 = t_articles.code_original
JOIN eco.p_facture ON NUM_0 = p_facture.code_original
JOIN eco.p_lignes_commandes ON POHNUM_0 || '-' || POPLIN_0 = p_lignes_commandes.code_original
LEFT JOIN prod_sagex3.GACCENTRY ON GACCENTRY.NUM_0 = PINVOICED.NUM_0
WHERE PINVOICED.ACCDAT_0 >= '[ENV_ECO_ANNEEDEBUT]-01-01'
ORDER BY 2;
SELECT base.cti_enable_index('eco', 'i_lignes_facture_1');
SELECT base.cti_enable_index('eco', 'i_lignes_facture_2');
SELECT base.cti_enable_index('eco', 'i_lignes_facture_3');
SELECT base.cti_enable_index('eco', 'i_lignes_facture_4');
]]>