<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	<NODE name="INIT" label="INITIALISATIONS">
		
		<NODE label="Tables Articles">
			
			<sqlcmd><![CDATA[
			
				-- 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;
			
				-- comptes
				INSERT INTO eco.t_compte(code, texte, texte_court, code_original)
				SELECT NUM_COMPTE, NUM_COMPTE, NUM_COMPTE, NUM_COMPTE
				FROM prod_qsp.PRODUIT
				WHERE NUM_COMPTE IS NOT NULL
					AND NUM_COMPTE NOT IN (SELECT code_original FROM eco.t_compte)
				GROUP BY 1,2,3,4
				;
				-- articles
				INSERT INTO eco.t_articles(code, texte, texte_court, code_original)
				SELECT N_PROD, LIBELLE, substr(LIBELLE,1,50), N_PROD
				FROM prod_qsp.PRODUIT
					LEFT JOIN eco.t_articles ON (code_original = N_PROD)
				WHERE code_original IS NULL;

				UPDATE eco.t_articles SET code = LEFT(REFERENCE,23)
				FROM 
					(
					SELECT 
						N_PROD,
						MIN(REFERENCE) AS REFERENCE
					FROM 
						prod_qsp.REF_PROD
					WHERE 
						TYPE_TIERS = 'S'
					GROUP BY 1
					) sub 
				WHERE 
					N_PROD = code_original
					AND code != REFERENCE
				;
				
				UPDATE eco.t_articles
				SET 
					code = LEFT(code,23)
				
				;
				-- famille
				INSERT INTO eco.t_familles_articles(code, texte, texte_court, code_original)
				SELECT CASE WHEN ABREGE != '' THEN ABREGE ELSE C_FAM::text END, LIBELLE, substr(LIBELLE,50), C_FAM	
				FROM prod_qsp.FAM_PROD
					LEFT JOIN eco.t_familles_articles ON (code_original = C_FAM)
				WHERE code_original IS NULL
				;
				
				UPDATE eco.t_familles_articles
				SET texte = LIBELLE,
					texte_court = substr(LIBELLE,1,50)
				FROM prod_qsp.FAM_PROD
				WHERE 
					code_original = C_FAM AND
					(texte IS DISTINCT FROM LIBELLE
					OR texte_court IS DISTINCT FROM substr(LIBELLE,1,50))
				;

				-- categorie
				INSERT INTO eco.t_categories_articles(code, texte, texte_court, code_original)
				SELECT C_THERA, LIBELLE, substr(LIBELLE,1,50), C_THERA
				FROM prod_qsp.CLASSE_THE
					LEFT JOIN eco.t_categories_articles ON (code_original = C_THERA)
				WHERE code_original IS NULL
				;
				
				UPDATE eco.t_categories_articles
				SET texte = LIBELLE,
					texte_court = substr(LIBELLE,50)
				FROM prod_qsp.CLASSE_THE
				WHERE 
					code_original = C_THERA AND
					(texte IS DISTINCT FROM LIBELLE 
					OR texte_court IS DISTINCT FROM substr(LIBELLE,1,50))
				;
					
				
				UPDATE eco.t_articles
				SET 
					texte = LIBELLE,
					texte_court = substr(LIBELLE,1,50)
				FROM prod_qsp.PRODUIT
				WHERE code_original = N_PROD
				AND (
					texte IS DISTINCT FROM LIBELLE OR
					texte_court IS DISTINCT FROM substr(LIBELLE,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_qsp.PRODUIT
					LEFT JOIN eco.t_familles_articles ON C_FAM = upper(t_familles_articles.code_original)
					LEFT JOIN eco.t_sous_familles_articles ON 0 = upper(t_sous_familles_articles.code_original)
					LEFT JOIN eco.t_types_articles ON upper(substr(NUM_TYP_PROD1,1,1)) = 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 C_THERA = upper(t_categories_articles.code_original)
					LEFT JOIN eco.t_sous_categories_articles ON 0 = upper(t_sous_categories_articles.code_original)
					LEFT JOIN eco.t_unites t_unites_stockage ON PCB = upper(t_unites_stockage.code_original)
					LEFT JOIN eco.t_unites t_unites_distribution ON SPCB = upper(t_unites_distribution.code_original)
					LEFT JOIN base.t_lpp ON CODE_TIPS = t_lpp.code
					LEFT JOIN base.t_ucd ON CODE_UCD = t_ucd.code
				WHERE
					t_articles.code_original = N_PROD 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 = t_compte.oid
				FROM
					prod_qsp.PRODUIT
					LEFT JOIN eco.t_compte ON t_compte.code_original = NUM_COMPTE
				WHERE N_PROD = t_articles.code_original
				;
			]]></sqlcmd>
		</NODE>
		
		
		<NODE label="Tables Fournisseurs">
			<sqlcmd><![CDATA[
			
			INSERT INTO eco.t_fournisseurs(code, texte, texte_court, code_original)
			SELECT NUM_TIERS, NOM_TIERS, NOM_TIERS, NUM_TIERS
			FROM prod_qsp.TIERS
			LEFT JOIN eco.t_fournisseurs ON (NUM_TIERS = t_fournisseurs.code_original)
			WHERE NUM_TIERS IN (SELECT NUM_TIERS FROM prod_qsp.COMMANDE WHERE TYPE_TIERS = 'F')
			AND t_fournisseurs.code_original IS NULL;
			
			
			UPDATE eco.t_fournisseurs
			SET 	texte = NOM_TIERS,
				texte_court = NOM_TIERS
			FROM prod_qsp.TIERS
			WHERE NUM_TIERS = t_fournisseurs.code_original
			AND (
				t_fournisseurs.texte IS DISTINCT FROM NOM_TIERS OR
				t_fournisseurs.texte_court IS DISTINCT FROM NOM_TIERS)
			;
		
			--
			--
			--UPDATE eco.t_fournisseurs SET
			--	type_code = t_types_fournisseurs.code,
			--	type_texte = t_types_fournisseurs.texte_court,
			--	type_section_id = t_types_fournisseurs.section_id,
			--	type_section_code = t_types_fournisseurs.section_code,
			--	type_section_texte = t_types_fournisseurs.section_texte
			--FROM eco.t_types_fournisseurs
			--WHERE type_id = t_types_fournisseurs.oid
			--	AND (
			--		type_code IS DISTINCT FROM t_types_fournisseurs.code OR
			--		type_texte IS DISTINCT FROM t_types_fournisseurs.texte_court OR
			--		type_section_id IS DISTINCT FROM t_types_fournisseurs.section_id OR
			--		type_section_code IS DISTINCT FROM t_types_fournisseurs.section_code OR
			--		type_section_texte IS DISTINCT FROM t_types_fournisseurs.section_texte
			--	);
				

			]]></sqlcmd>
		</NODE>
		
		
		
		<NODE label="Autres tables">
			<sqlcmd><![CDATA[
			
		
			-- Types mouvements
			
			
			INSERT INTO eco.t_types_mouvements(code, code_original, texte, texte_court)
			SELECT CODE_MOUVEMENT, CODE_MOUVEMENT, LIB_ETAT, LIB_ETAT
			FROM prod_qsp.MOUVEMENT_STOCK
				LEFT JOIN eco.t_types_mouvements ON (code_original = CODE_MOUVEMENT)
			WHERE code_original IS NULL AND CODE_MOUVEMENT <> ''
			;
			
			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)
			;
		

			]]></sqlcmd>
		</NODE>
		
		
	</NODE>
	<NODE name="PROD" label="RECUPERATION DES DONNEES DE PRODUCTION">
		<NODE label="Commandes">
			<sqlcmd><![CDATA[
			
						
				SELECT base.cti_disable_index('eco', 'i_commandes_1');
				SELECT base.cti_disable_index('eco', 'i_commandes_2');
				SELECT base.cti_disable_index('eco', 'i_commandes_3');
				SELECT base.cti_disable_index('eco', 'i_commandes_4');
				SELECT base.cti_disable_index('eco', 'i_commandes_5');
				SELECT base.cti_disable_index('eco', 'i_commandes_6');
				SELECT base.cti_disable_index('eco', 'i_commandes_7');
				SELECT base.cti_disable_index('eco', 'i_commandes_8');
				
					
				TRUNCATE eco.p_commandes;
						
				INSERT INTO eco.p_commandes(
					code_original,
					numero,
					date_commande,
					date_engagement,
					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,
					site_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
					NUMERO_COM AS code_original,
					NUMERO_COM AS numero,
					DATE_COM AS date_commande,
					DATE_COM AS date_engagement,
					''::text AS objet,
					''::text AS reference,
					DATE_COM AS date_livraison_prevue,
					CASE WHEN LIVRE = 'O' THEN 'T' ELSE 'N' END AS etat_reception,
					'20991231'::date AS date_reception,
					'N' etat_liquidation,
					COALESCE(t_fournisseurs.oid, 0) AS fournisseur_id,
					COALESCE(t_gestionnaires.oid,0) AS gestionnaire_id,
					0::bigint AS lieu_commande_id,
					COALESCE(t_lieux_facturation.oid,0) AS lieu_facturation_id,
					COALESCE(t_lieux_facturation.oid,0) AS lieu_livraison_id,
					0::bigint AS unite_fonctionnelle_id,
					COALESCE(t_sites.oid,0) AS site_id,
					0::numeric AS montant_commande_ht,
					0::numeric AS montant_commande_remise,
					0::numeric AS montant_commande_tva,
					0::numeric AS montant_commande_port,
					0::numeric AS montant_commande_total,
					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_total
					FROM prod_qsp.COMMANDE
						LEFT JOIN eco.t_fournisseurs ON t_fournisseurs.code_original = NUM_TIERS
						LEFT JOIN eco.t_lieux t_lieux_commande ON t_lieux_commande.code_original = 0
						LEFT JOIN eco.t_lieux t_lieux_livraison ON t_lieux_livraison.code_original = 0
						LEFT JOIN eco.t_lieux t_lieux_facturation ON t_lieux_facturation.code_original = 0
						LEFT JOIN eco.t_gestionnaires ON t_gestionnaires.code = USER_COM
						LEFT JOIN eco.t_sites ON t_sites.code_original = NUM_SOCIETE
					WHERE DATE_COM >= '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');
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Stocks">
			<sqlcmd><![CDATA[
				-- stocks
				SELECT base.cti_disable_index('eco', 'i_stock_1');
				SELECT base.cti_disable_index('eco', 'i_stock_2');
				SELECT base.cti_disable_index('eco', 'i_stock_3');
				SELECT base.cti_disable_index('eco', 'i_stock_4');
				SELECT base.cti_disable_index('eco', 'i_stock_5');

		
				
				DROP TABLE IF EXISTS w_prix;
				CREATE TEMP TABLE w_prix AS 
				SELECT 
					N_PROD,
					NUM_STOCK,
					NUM_COMPTE,
					PUMP,
					DEPOT,
					DATE_REF,
					COALESCE(LEAD(DATE_REF) OVER (PARTITION BY N_PROD, NUM_STOCK, DEPOT ORDER BY DATE_REF) - interval '1 day','2099-12-31') AS next_date
				FROM prod_qsp.QTE_MOIS
				WHERE PUMP != 0 AND DEPOT = 0
				;

				TRUNCATE eco.p_stock
				;
				
				INSERT INTO eco.p_stock
					(
					fournisseur_id,
					article_id,
					compte_id,
					ucd_id,
					lpp_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 
					t_articles.fournisseur_principal_id,
					t_articles.oid,
					COALESCE(compte_id, t_compte.oid,0) AS compte_id,
					ucd_id,
					lpp_id,
					t_sites.oid,
					t_lieux.oid,
					QTE_MOIS.DATE_REF,
					QTE_MOIS.DATE_REF + interval '1 month' - interval '1 day',
					QTE_ENTREE,
					QTE_ENTREE * w_prix.PUMP,
					QTE_SORTIE,
					QTE_SORTIE * w_prix.PUMP,
					w_prix.PUMP,
					SUM(QTE_ENTREE - QTE_SORTIE) OVER (PARTITION BY QTE_MOIS.N_PROD, QTE_MOIS.NUM_STOCK ORDER BY QTE_MOIS.DATE_REF rows unbounded preceding) - QTE_ENTREE + QTE_SORTIE AS stock_quantite_debut,
					SUM(QTE_ENTREE - QTE_SORTIE) OVER (PARTITION BY QTE_MOIS.N_PROD, QTE_MOIS.NUM_STOCK ORDER BY QTE_MOIS.DATE_REF rows unbounded preceding) AS stock_quantite_debut,
					(SUM(QTE_ENTREE - QTE_SORTIE) OVER (PARTITION BY QTE_MOIS.N_PROD, QTE_MOIS.NUM_STOCK ORDER BY QTE_MOIS.DATE_REF rows unbounded preceding) - QTE_ENTREE + QTE_SORTIE)* w_prix.PUMP AS stock_valeur_debut,
					(SUM(QTE_ENTREE - QTE_SORTIE) OVER (PARTITION BY QTE_MOIS.N_PROD, QTE_MOIS.NUM_STOCK ORDER BY QTE_MOIS.DATE_REF rows unbounded preceding))*w_prix.PUMP AS stock_valeur_fin
				FROM prod_qsp.QTE_MOIS
					JOIN eco.t_articles ON QTE_MOIS.N_PROD = t_articles.code_original
					LEFT JOIN w_prix ON QTE_MOIS.N_PROD = w_prix.N_PROD AND QTE_MOIS.DATE_REF BETWEEN w_prix.DATE_REF AND next_date AND QTE_MOIS.DEPOT = w_prix.DEPOT
					LEFT JOIN eco.t_compte ON w_prix.NUM_COMPTE = t_compte.code_original
					LEFT JOIN eco.t_lieux ON QTE_MOIS.NUM_STOCK = t_lieux.code_original
					LEFT JOIN prod_qsp.SOC_STOCK ON QTE_MOIS.NUM_STOCK = SOC_STOCK.NUM_STOCK
					LEFT JOIN eco.t_sites ON NUM_SOCIETE = t_sites.code_original
				WHERE TP_PERIO = 1
				;
				
				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');
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Mouvements">
			<sqlcmd><![CDATA[
				-- mouvements
							
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_1');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_2');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_3');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_4');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_5');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_6');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_7');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_8');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_9');
				SELECT base.cti_disable_index('eco', 'i_mouvements_articles_10');

				DROP TABLE IF EXISTS w_DETAIL_STOCK;
				CREATE TEMP TABLE w_DETAIL_STOCK AS
				SELECT
					DETAIL_STOCK.*,
					''::text AS no_com,
					''::text AS no_uf,
					''::text AS NUM_COMPTE
				FROM 
					prod_qsp.DETAIL_STOCK
				;


				UPDATE w_DETAIL_STOCK 
				SET 
					PRIX_REF = w_prix.PUMP,
					NUM_COMPTE = w_prix.NUM_COMPTE
				FROM
					w_prix
				WHERE 
					w_DETAIL_STOCK.N_PROD = w_prix.N_PROD AND
					w_DETAIL_STOCK.NUM_STOCK = w_prix.NUM_STOCK AND
					DATE_MVT BETWEEN DATE_REF AND COALESCE(next_date,'2099_12-31')
				;

				
				DROP TABLE IF EXISTS w_com;
				CREATE TEMP TABLE w_com AS
				SELECT 
					NUM_LIGNE,
					NUMERO_COM,
					LIV_LIGNE.NUM_TIERS,
					LIV_LIGNE.TYPE_TIERS
				FROM 
					prod_qsp.QUELLE_LIVRAISON_DETAIL
					LEFT JOIN prod_qsp.LIV_LIGNE on LIV_LIGNE.NOLIG_LIV = quelle_livraison_detail.NOLIG_LIV 
					LEFT JOIN prod_qsp.COMMANDES_LIVREES ON COMMANDES_LIVREES.NOLIG_LIV = LIV_LIGNE.NOLIG_LIV
					LEFT JOIN prod_qsp.COM_LIGNE ON COM_LIGNE.NOLIG_COM = COMMANDES_LIVREES.NOLIG_COM
				;
			
				UPDATE w_DETAIL_STOCK 
				SET 
					no_com = NUMERO_COM,
					no_uf = CASE WHEN TYPE_TIERS = 'V' THEN w_com.NUM_TIERS ELSE 0 END
				FROM
					w_com
				WHERE 
					w_DETAIL_STOCK.NUM_LIGNE = w_com.NUM_LIGNE
				;
				
				TRUNCATE eco.p_mouvements_articles;

				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,
					site_id,
					lpp_id,
					ucd_id,
					prix_unitaire, 
					entree_quantite, 
					sortie_quantite,
					entree_montant, 
					sortie_montant,
					stock_quantite_debut,
					stock_quantite_fin)
				SELECT 
					DATE_MVT AS date, --autre possible IPTDAT_0 mais imputation
					CASE WHEN QTE_MVT >= 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';
					


			
			]]></sqlcmd>
		</NODE>
		
	</NODE>
	<NODE name="POST" label="POST-TRAITEMENTS">
		<sqlcmd><![CDATA[
			VACUUM ANALYSE eco.p_mouvements_articles;
			VACUUM ANALYSE eco.p_stock;
				
			SELECT eco.cti_update_mvt_qte_stock();
			SELECT eco.cti_update_mvt_mnt_stock();
		]]></sqlcmd>
	</NODE>
	
</ROOT>
