


TRUNCATE eco.p_commandes;

INSERT INTO eco.p_commandes(
	code_original,
	numero,
	date_commande,
	objet,
	reference,
	date_livraison_prevue,
	etat_reception,
	date_reception,
	etat_liquidation,
	fournisseur_id,
	gestionnaire_id,
	lieu_commande_id,
	lieu_facturation_id,
	lieu_livraison_id,
	unite_fonctionnelle_id,
	montant_commande_ht,
	montant_commande_remise,
	montant_commande_tva,
	montant_commande_port,
	montant_commande_total,
	montant_liquidation_ht,
	montant_liquidation_remise,
	montant_liquidation_tva,
	montant_liquidation_port,
	montant_liquidation_total)

SELECT 
	FUNCDE,
	trim(to_char(FUNCDE,'000000')) AS numero,
	eco.cti_to_date(FUSCRE, FUACRE, FUMCRE, FUJCRE) AS date_commande,
	trim(FUOBFU) AS objet,
	trim(FUREFC) AS reference,
	eco.cti_to_date(FUSLIP, FUALIP, FUMLIP, FUJLIP) AS date_livraison_prevue,
	CASE WHEN FUTOP4 = '1' THEN 'T' ELSE 'N' END AS etat_reception,
	eco.cti_to_date(FUSLIV, FUALIV, FUMLIV, FUJLIV) AS date_reception,
	CASE WHEN FUTOP5 = '1' THEN 'T' ELSE 'N' END AS etat_liquidation,
	COALESCE(t_fournisseurs.oid,0) AS fournisseur_id,
	COALESCE(t_gestionnaires.oid,0) AS gestionnaire_id,
	COALESCE(t_lieux_commande.oid,0) AS lieu_commande_id,
	COALESCE(t_lieux_facturation.oid,0) AS lieu_facturation_id,
	COALESCE(t_lieux_livraison.oid,0) AS lieu_livraison_id,
	COALESCE(t_unites_fonctionnelles.oid,0)AS unite_fonctionnelle_id,
	FUMHTC AS montant_commande_ht,
	FUMREC AS montant_commande_remise,
	FUMTVC AS montant_commande_tva,
	FUMPOC AS montant_commande_port,
	FUMTCC AS montant_commande_total,
	FUMHTL AS montant_liquidation_ht,
	FUMREL AS montant_liquidation_remise,
	FUMTVL AS montant_liquidation_tva,
	FUMPOL AS montant_liquidation_port,
	FUMTCL AS montant_liquidation_total
FROM prod_shs.pigefi180_FUP01
	LEFT JOIN eco.t_fournisseurs ON FUIUFN = t_fournisseurs.code_original
	LEFT JOIN eco.t_gestionnaires ON FUKGEC = t_gestionnaires.code_original
	LEFT JOIN eco.t_lieux t_lieux_commande ON FULFAC = t_lieux_commande.code_original
	LEFT JOIN eco.t_lieux t_lieux_facturation ON FULFAC = t_lieux_facturation.code_original
	LEFT JOIN eco.t_lieux t_lieux_livraison ON FULLIV = t_lieux_livraison.code_original
	LEFT JOIN eco.t_unites_fonctionnelles ON CASE WHEN FUIUU2 <> '' THEN FUIUU2 ELSE FUIUU1 END  = t_unites_fonctionnelles.code_original
	
WHERE FUSCRE = 20 and FUACRE BETWEEN  08 AND 20 AND FUCANN = '';

select * from eco.p_commandes
limit 100



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,
	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
	FVNCDE || '|' || FVLICD AS code_original,
	p_commandes.oid AS commande_id,
	FVLICD AS ligne_commande,
	trim(FVLIA1 || ' ' || FVLIA2) AS texte,
	t_articles.oid AS article_id,
	COALESCE(t_unites.oid,t_articles.unite_stockage_id) AS unite_approvisionnement_id,
	0 AS compte_id,
	FVQCD1 AS quantite_approvisionnement,
	FVPUN1 AS prix_unitaire_approvisionnement,

	FVUSUA AS multiplicateur_stockage,

	FVQCD2 AS quantite_stockage,
	FVPUN2 AS prix_unitaire_stockage,

	

	FVMHT1 AS montant_commande_ht,
	FVMRE1 AS montant_commande_remise,
	FVMTV1 AS montant_commande_tva,
	FVMPO1 AS montant_commande_port,
	FVMTC1 AS montant_commande_ttc,

	FVQRE1 AS quantite_livraison_approvisionnement,
	FVQRE1 AS quantite_livraison_stockage,
	
	CASE WHEN FVQRE1 = FVQCD1 THEN FVMHT1 WHEN FVQCD1 = 0 THEN 0 ELSE FVMHT1 / FVQCD1 * FVQRE1 END AS montant_livraison_ht,
	CASE WHEN FVQRE1 = FVQCD1 THEN FVMTC1 WHEN FVQCD1 = 0 THEN 0 ELSE FVMTC1 / FVQCD1 * FVQRE1 END AS montant_livraison_ht,
	CASE WHEN FVQRE1 = FVQCD1 THEN 'T' WHEN FVQRE1 > FVQCD1 THEN 'S' WHEN FVQRE1 = 0 THEN 'N' ELSE 'P' END AS etat_livraison,

	CASE WHEN FVTOP3 = '1' THEN FVMHT1 ELSE 0 END AS montant_liquidation_ht,
	CASE WHEN FVTOP3 = '1' THEN FVMRE1 ELSE 0 END AS montant_liquidation_remise,
	CASE WHEN FVTOP3 = '1' THEN FVMTV1 ELSE 0 END AS montant_liquidation_tva,
	CASE WHEN FVTOP3 = '1' THEN FVMPO1 ELSE 0 END AS montant_liquidation_port,
	CASE WHEN FVTOP3 = '1' THEN FVMTC1 ELSE 0 END AS montant_liquidation_ttc,

	FVTOP3 AS etat_liquidation
	

FROM prod_shs.pigefi180_FVP01
	JOIN prod_shs.pigefi180_FUP01 on FVNCDE = FUNCDE
	JOIN eco.p_commandes ON FVNCDE = p_commandes.code_original
	JOIN eco.t_articles ON FVIUAR = t_articles.code_original
	LEFT JOIN eco.t_unites ON FVKUAP = t_unites.code_original
WHERE FUSCRE = 20 and FUACRE BETWEEN  08 AND 20 AND FUCANN = '' AND FVCANN = ''




TRUNCATE eco.t_profils_comptables;


INSERT INTO eco.t_profils_comptables
	(code_original,
	centre_responsabilite_id,
	unite_fonctionnelle_id)
SELECT
	SYIUCR || '|' || SYIUUF AS code_original,
	COALESCE(t_centres_responsabilites.oid,0) AS centre_responsabilite_id,
	COALESCE(t_unites_fonctionnelles.oid,0) AS unite_fonctionnelle_id
FROM prod_shs.pigefi180_syp01
	LEFT JOIN eco.t_centres_responsabilites ON SYIUCR = t_centres_responsabilites.code_original
	LEFT JOIN eco.t_unites_fonctionnelles ON SYIUUF = t_unites_fonctionnelles.code_original
GROUP BY 1,2,3;



UPDATE eco.t_profils_comptables SET
	centre_responsabilite_code = t_centres_responsabilites.code,
	centre_responsabilite_texte = t_centres_responsabilites.texte_court,
	centre_responsabilite_section_id = t_centres_responsabilites.section_id,
	centre_responsabilite_section_code = t_centres_responsabilites.section_code,
	centre_responsabilite_section_texte = t_centres_responsabilites.section_texte,
	unite_fonctionnelle_code = t_unites_fonctionnelles.code,
	unite_fonctionnelle_texte = t_unites_fonctionnelles.texte_court,
	unite_fonctionnelle_section_id = t_unites_fonctionnelles.section_id,
	unite_fonctionnelle_section_code = t_unites_fonctionnelles.section_code,
	unite_fonctionnelle_section_texte = t_unites_fonctionnelles.section_texte
FROM 
	eco.t_centres_responsabilites,
	eco.t_unites_fonctionnelles
WHERE  centre_responsabilite_id = t_centres_responsabilites.oid
	AND unite_fonctionnelle_id = t_unites_fonctionnelles.oid
	AND (
		centre_responsabilite_code IS DISTINCT FROM t_centres_responsabilites.code OR
		centre_responsabilite_texte IS DISTINCT FROM t_centres_responsabilites.texte_court OR
		centre_responsabilite_section_id IS DISTINCT FROM t_centres_responsabilites.section_id OR
		centre_responsabilite_section_code IS DISTINCT FROM t_centres_responsabilites.section_code OR
		centre_responsabilite_section_texte IS DISTINCT FROM t_centres_responsabilites.section_texte OR
		unite_fonctionnelle_code IS DISTINCT FROM t_unites_fonctionnelles.code OR
		unite_fonctionnelle_texte IS DISTINCT FROM t_unites_fonctionnelles.texte_court OR
		unite_fonctionnelle_section_id IS DISTINCT FROM t_unites_fonctionnelles.section_id OR
		unite_fonctionnelle_section_code IS DISTINCT FROM t_unites_fonctionnelles.section_code OR
		unite_fonctionnelle_section_texte IS DISTINCT FROM t_unites_fonctionnelles.section_texte
	);





TRUNCATE eco.p_mouvements_articles;

INSERT INTO eco.p_mouvements_articles(
	date, 
	sens_mouvement,
	type_mouvement_id, 
	texte, 
	gestionnaire_id, 
	lieu_id, 
	profil_comptable_id, 
	article_id, 
	fournisseur_id,
	commande_id,
	ligne_commande,
	prix_unitaire, 
	entree_quantite, 
	sortie_quantite,
	entree_montant, 
	sortie_montant,
	stock_quantite_debut,
	stock_quantite_fin)

SELECT 
	eco.cti_to_date(SYSIEC, SYANNE, SYMOIS, SYJOUR) AS date,
	CASE WHEN SYKNMV = 'E' OR (SYKNMV = 'T' AND SYKCMV = 'TE') THEN 'E' ELSE 'S' END AS sens_mouvement,
	COALESCE(t_types_mouvements.oid,0) AS type_mouvement_id,
	TRIM(SYLI25) AS texte,
	COALESCE(t_gestionnaires.oid,0) AS gestionnaire_id,
	COALESCE(t_lieux.oid,0) AS lieu_id,
	COALESCE(t_profils_comptables.oid,0) AS profil_comptable,
	COALESCE(t_articles.oid,0) AS article_id,
	COALESCE(t_fournisseurs.oid,0) AS fournisseur_id,
	COALESCE(p_commandes.oid,0) AS commande_id,
	SYLICD AS ligne_commande,
	SYPRMV AS prix_unitaire,
	CASE WHEN SYKNMV = 'E' OR (SYKNMV = 'T' AND SYKCMV = 'TE') THEN SYQMVT ELSE 0 END AS entree_quantite,
	CASE WHEN SYKNMV = 'S' OR (SYKNMV = 'T' AND SYKCMV = 'TS') THEN SYQMVT ELSE 0 END AS sortie_quantite,
	CASE WHEN SYKNMV = 'E' OR (SYKNMV = 'T' AND SYKCMV = 'TE') THEN SYQMVT * SYPRMV ELSE 0 END AS entree_montant,
	CASE WHEN SYKNMV = 'S' OR (SYKNMV = 'T' AND SYKCMV = 'TS') THEN SYQMVT * SYPRMV ELSE 0 END AS sortie_montant,
	0 AS stock_quantite_debut,
	0 AS stock_quantite_fin

FROM prod_shs.pigefi180_syp01
	LEFT JOIN prod_shs.pigefi180_AFP01 ON SYIUAF = AFIUAF
	LEFT JOIN prod_shs.pigefi180_FUP01 ON SYNCDE  = FUNCDE 
	LEFT JOIN eco.t_types_mouvements ON SYKCMV = t_types_mouvements.code_original
	LEFT JOIN eco.t_gestionnaires ON SYKGEC = t_gestionnaires.code_original
	LEFT JOIN eco.t_lieux ON SYLIEU = t_lieux.code_original
	LEFT JOIN eco.t_profils_comptables ON SYIUCR || '|' || SYIUUF = t_profils_comptables.code_original
	LEFT JOIN eco.t_articles ON SYIUAR = t_articles.code_original
	LEFT JOIN eco.t_fournisseurs ON CASE WHEN FUIUFN <> '' THEN AFIUFN WHEN AFIUFN <> '' THEN AFIUFN ELSE SYIUFN END = t_fournisseurs.code_original
	LEFT JOIN eco.p_commandes ON SYNCDE = p_commandes.code_original

WHERE sysiec = 20 and syanne BETWEEN  08 AND 20 AND SYCANN = ''
ORDER BY SYNMVT;

select * from eco.p_mouvements_articles limit 1000

TRUNCATE eco.p_chiffrier_production;

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



select * FROM eco.p_mouvements_articles order by article_id, date

limit 1000

