



TRUNCATE econom.p_commandes;

INSERT INTO econom.p_commandes(
	numero,
	date_commande,
	objet,
	reference,
	date_livraison_prevue,
	etat_reception,
	date_reception,
	etat_liquidation,
	fournisseur_id,
	gestionnaire_id,
	lieu_facturation_id,
	lieu_livraison_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 
	trim(to_char(FUNCDE,'000000')) AS numero,
	econom.cti_to_date(FUSCRE, FUACRE, FUMCRE, FUJCRE) AS date_commande,
	trim(FUOBFU) AS objet,
	trim(FUREFC) AS reference,
	econom.cti_to_date(FUSLIP, FUALIP, FUMLIP, FUJLIP) AS date_livraison_prevue,
	CASE WHEN FUTOP4 = '1' THEN 'T' ELSE 'N' END AS etat_reception,
	econom.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_facturation.oid,0) AS lieu_facturation_id,
	COALESCE(t_lieux_livraison.oid,0) AS lieu_livraison_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 econom.t_fournisseurs ON FUIUFN = t_fournisseurs.code_original
	LEFT JOIN econom.t_gestionnaires ON FUKGEC = t_gestionnaires.code_original
	LEFT JOIN econom.t_lieux t_lieux_facturation ON FULFAC = t_lieux_facturation.code_original
	LEFT JOIN econom.t_lieux t_lieux_livraison ON FULLIV = t_lieux_livraison.code_original
	
WHERE FUSCRE = 20 and FUACRE BETWEEN  08 AND 20



select * FROM econom.p_commandes order by article_id, date

limit 1000

