<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	<NODE name="INIT" label="INITIALISATIONS">
		<sqlcmd><![CDATA[
			SET DATESTYLE TO ISO, European
			;
		]]></sqlcmd>
	</NODE>

	<NODE name="ECRITURES" label="RECUPERATION ECRITURES">			
		<NODE label="Suppression avant regénération">
			<sqlcmd><![CDATA[
									
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_2');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_3');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_4');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_5');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_6');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_7');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_8');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_9');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_10');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_11');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_12');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_13');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_1');
				
				
				
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_2');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_3');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_4');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_5');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_6');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_7');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_8');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_9');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_10');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_11');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_12');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_13');
				SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_1');
														
			]]></sqlcmd>
		</NODE>
				
		<NODE label="Génération écritures">
			<sqlcmd><![CDATA[

				-- Ecritures générales
				DROP TABLE IF EXISTS w_ecritures_generales
				;

				CREATE TEMP TABLE w_ecritures_generales AS
					SELECT
						ecr_gen.uk_numero_ordre AS ordre_numero,
						ecr_gen.fk_numero_piece AS piece_numero,
						ecr_gen.fk_societe AS societe_code,
						ecr_gen.fk_etablissement AS etablissement_code,
						ecr_gen.fk_societe|| ecr_gen.fk_etablissement AS site_code,
						ecr_gen.fk_compte AS compte_numero,
						ecr_gen.fk_centralisateur AS centralisateur_code,
						CASE WHEN ecr_gen.fk_centralisateur = ecr_gen.fk_compte THEN ecr_gen.fk_compte ELSE ecr_gen.fk_centralisateur || '-' || ecr_gen.fk_compte END AS cti_compte_numero,
						ecr_gen.date_comptable::date AS date_ecriture,
						substr(to_char(ecr_gen.date_comptable::date, 'YYYYMM'), 1, 4) AS exercice_comptable,
						substr(to_char(ecr_gen.date_comptable::date, 'YYYYMM'), 1, 6) AS mois_comptable,
						'C'::text AS clinique_honoraire,
						ecr_gen.fk_origine AS journal_code,
						CASE 
							WHEN fk_type_tiers_contrepartie = 'FOURN'
							THEN fk_compte_contrepartie
							ELSE ''::text 
						END AS partenaire_code,
						'0'::text AS comptabilisee,
						'0'::text AS fin_exercice,
						'0'::text AS ajustement,
						'0'::text AS inter_site,
						ecr_gen.fk_origine || '-' || ecr_gen.fk_numero_piece || '-' || ecr_gen.uk_numero_ordre AS cle_originale,
						ecr_gen.fk_origine || '-' || ecr_gen.fk_numero_piece || '-' || ecr_gen.uk_numero_ordre AS identifiant_operation,
						ecr_gen.fk_compte_contrepartie AS compte_contrepartie_numero,
						'0'::text AS cti_compte_contrepartie_numero,
						'2099-12-31'::date AS date_facture_fournisseur,
						''::text AS dossier,
						'2099-12-31'::date AS date_echeance,
						0::bigint AS journal_paiement_id,
						0::bigint AS mode_paiement_id,
						''::text AS numero_cheque,
						-- Ecritures analytiques
						COALESCE(ecr_ana.libelle_ecriture, ecr_gen.libelle_ecriture) AS texte,
						CASE
							WHEN ecr_ana.ecriture_gene_id IS NULL AND ecr_gen.fk_sens = '-1' THEN ecr_gen.montant_m1::numeric
							WHEN ecr_ana.ecriture_gene_id IS NOT NULL AND ecr_gen.fk_sens = '-1' AND ecr_ana.sens = '-1' THEN ecr_ana.montant_ana_monnaie1::numeric
							WHEN ecr_ana.ecriture_gene_id IS NOT NULL AND ecr_gen.fk_sens = '-1' AND ecr_ana.sens = '1' THEN -ecr_ana.montant_ana_monnaie1::numeric
							ELSE 0::numeric
						END AS montant_credit,
						CASE
							WHEN ecr_ana.ecriture_gene_id IS NULL AND ecr_gen.fk_sens = '1' THEN ecr_gen.montant_m1::numeric
							WHEN ecr_ana.ecriture_gene_id IS NOT NULL AND ecr_gen.fk_sens = '1' AND ecr_ana.sens = '1' THEN ecr_ana.montant_ana_monnaie1::numeric
							WHEN ecr_ana.ecriture_gene_id IS NOT NULL AND ecr_gen.fk_sens = '1' AND ecr_ana.sens = '-1' THEN -ecr_ana.montant_ana_monnaie1::numeric
							ELSE 0::numeric
						END AS montant_debit,
						CASE WHEN ecr_ana.fk_axe0 <> '' THEN ecr_ana.fk_axe0 || '-0' ELSE '' END AS analytique_0_code,
						CASE WHEN ecr_ana.fk_axe1 <> '' THEN ecr_ana.fk_axe1 || '-1' ELSE '' END AS analytique_1_code,
						CASE WHEN ecr_ana.fk_axe2 <> '' THEN ecr_ana.fk_axe2 || '-2' ELSE '' END AS analytique_2_code,
						CASE WHEN ecr_ana.fk_axe3 <> '' THEN ecr_ana.fk_axe3 || '-3' ELSE '' END AS analytique_3_code,
						CASE WHEN ecr_ana.fk_axe4 <> '' THEN ecr_ana.fk_axe4 || '-4' ELSE '' END AS analytique_4_code,
						CASE WHEN ecr_ana.fk_axe5 <> '' THEN ecr_ana.fk_axe5 || '-5' ELSE '' END AS analytique_5_code
					FROM prod_talentia_finance_csv.fi_gen_ecriture_gene AS ecr_gen
					LEFT JOIN prod_talentia_finance_csv.fi_gen_ecriture_analytique AS ecr_ana ON ecr_ana.ecriture_gene_id = ecr_gen.id
					WHERE ecr_gen.date_comptable::date >= '[ENV_COMPTA_ANNEEDEBUT]0101'
				;

				UPDATE w_ecritures_generales
				SET cti_compte_contrepartie_numero = subview.CPTC
				FROM
				(
				SELECT compte_numero, (MAX(Array[to_char(nb,'FM000000000000'), centralisateur_code]))[2] AS CPTC
				FROM
				(
				SELECT compte_numero, centralisateur_code, count(*) AS nb
				FROM w_ecritures_generales
				GROUP BY 1,2
				ORDER BY 1,2,3 
				) subview
				GROUP BY 1
				) subview
				WHERE w_ecritures_generales.compte_contrepartie_numero <> '' AND 
					w_ecritures_generales.compte_contrepartie_numero = subview.compte_numero
				;

				-- Ecritures analytiques
				DROP TABLE IF EXISTS w_ecritures_analytiques
				;

				CREATE TEMP TABLE w_ecritures_analytiques AS
					SELECT
						ufk_numero_ordre AS ordre_numero,
						fk_numero_piece AS piece_numero,
						fk_societe AS societe_code,
						fk_etablissement AS etablissement_code,
						fk_societe|| fk_etablissement AS site_code,
						fk_compte AS compte_numero,
						fk_centralisateur AS centralisateur_code,
						CASE WHEN fk_centralisateur = fk_compte THEN fk_compte ELSE fk_centralisateur || '-' || fk_compte END AS cti_compte_numero,
						date_comptable::date AS date_ecriture,
						substr(to_char(date_comptable::date, 'YYYYMM'), 1, 4) AS exercice_comptable,
						substr(to_char(date_comptable::date, 'YYYYMM'), 1, 6) AS mois_comptable,
						'C'::text AS clinique_honoraire,
						fk_origine AS journal_code,
						CASE WHEN fk_centralisateur = fk_compte THEN '' ELSE fk_compte END AS partenaire_code,
						'0'::text AS comptabilisee,
						'0'::text AS fin_exercice,
						'0'::text AS ajustement,
						'0'::text AS inter_site,
						fk_origine || '-' || fk_numero_piece || '-' || ufk_numero_ordre AS cle_originale,
						fk_origine || '-' || fk_numero_piece || '-' || ufk_numero_ordre AS identifiant_operation,
						'0'::text AS compte_contrepartie_numero,
						'0'::text AS cti_compte_contrepartie_numero,
						'2099-12-31'::date AS date_facture_fournisseur,
						''::text AS dossier,
						'2099-12-31'::date AS date_echeance,
						0::bigint AS journal_paiement_id,
						0::bigint AS mode_paiement_id,
						''::text AS numero_cheque,
						-- Ecritures analytiques
						libelle_ecriture AS texte,
						CASE WHEN sens = '-1' THEN montant_ana_monnaie1::numeric ELSE 0::numeric END AS montant_credit,
						CASE WHEN sens = '1' THEN montant_ana_monnaie1::numeric ELSE 0::numeric END AS montant_debit,
						CASE WHEN fk_axe0 <> '' THEN fk_axe0 || '-0' ELSE '' END AS analytique_0_code,
						CASE WHEN fk_axe1 <> '' THEN fk_axe1 || '-1' ELSE '' END AS analytique_1_code,
						CASE WHEN fk_axe2 <> '' THEN fk_axe2 || '-2' ELSE '' END AS analytique_2_code,
						CASE WHEN fk_axe3 <> '' THEN fk_axe3 || '-3' ELSE '' END AS analytique_3_code,
						CASE WHEN fk_axe4 <> '' THEN fk_axe4 || '-4' ELSE '' END AS analytique_4_code,
						CASE WHEN fk_axe5 <> '' THEN fk_axe5 || '-5' ELSE '' END AS analytique_5_code
					FROM prod_talentia_finance_csv.fi_gen_ecriture_analytique 
					WHERE 
						ecriture_gene_id IS NULL
						AND date_comptable::date >= '[ENV_COMPTA_ANNEEDEBUT]0101'
				;

			-- Regroupement de toutes les écritures comptables
			DROP TABLE IF EXISTS w_ecritures
			;

			CREATE TEMP TABLE w_ecritures AS
				SELECT * 
				FROM w_ecritures_generales
			;

			INSERT INTO w_ecritures SELECT * FROM w_ecritures_analytiques
			;


			]]></sqlcmd>
		</NODE>
		
		<NODE label="Insertion des écritures en base de données">
			<sqlcmd><![CDATA[

				TRUNCATE compta.p_historique_ecritures
				;

				-- Ecritures comptables
				INSERT INTO compta.p_historique_ecritures 
					(site_id,
					type_compta_id,
					compte_id,
					date_ecriture,
					exercice_comptable,
					mois_comptable,
					clinique_honoraire,
					journal_id,
					section_analytique_id,
					section_analytique_2_id,
					section_analytique_3_id,
					section_analytique_4_id,
					section_analytique_5_id,
					section_analytique_6_id,
					partenaire_id,
					compte_extra_id,
					comptabilisee,
					fin_exercice,
					ajustement,
					inter_site,
					cle_originale,
					identifiant_operation,
					compte_contrepartie_id,
					texte,
					date_facture_fournisseur,
					piece,
					dossier,
					date_echeance,
					journal_paiement_id,
					mode_paiement_id,
					numero_cheque,
					montant_credit,
					montant_debit)
				SELECT 
					COALESCE(t_sites.oid,0) AS site_id,
					t_comptes.type_compta_id,
					t_comptes.oid AS compte_id,
					w_ecritures.date_ecriture,
					w_ecritures.exercice_comptable::numeric,
					w_ecritures.mois_comptable::numeric,
					w_ecritures.clinique_honoraire,
					t_journaux.oid AS journal_id,
					COALESCE(t_sections_analytiques_1.oid,0) AS section_analytique_1_id,
					COALESCE(t_sections_analytiques_2.oid,0) AS section_analytique_2_id,
					COALESCE(t_sections_analytiques_3.oid,0) AS section_analytique_3_id,
					COALESCE(t_sections_analytiques_4.oid,0) AS section_analytique_4_id,
					COALESCE(t_sections_analytiques_5.oid,0) AS section_analytique_5_id,
					COALESCE(t_sections_analytiques_6.oid,0) AS section_analytique_6_id,
					COALESCE(t_partenaires.oid,0) AS partenaire_id,
					t_comptes_extra.oid AS compte_extra_id,
					w_ecritures.comptabilisee,
					w_ecritures.fin_exercice,
					w_ecritures.ajustement,
					w_ecritures.inter_site,
					w_ecritures.cle_originale,
					w_ecritures.identifiant_operation,
					COALESCE(t_comptes_contrepartie.oid,0) AS compte_contrepartie_id,
					w_ecritures.texte,
					w_ecritures.date_facture_fournisseur,
					w_ecritures.piece_numero,
					w_ecritures.dossier,
					w_ecritures.date_echeance,
					w_ecritures.journal_paiement_id,
					w_ecritures.mode_paiement_id,
					w_ecritures.numero_cheque,
					montant_credit,
					montant_debit
				FROM w_ecritures
				LEFT JOIN compta.t_sites ON t_sites.code_original = w_ecritures.site_code
				JOIN compta.t_comptes ON t_comptes.numero = w_ecritures.centralisateur_code
				JOIN compta.t_comptes t_comptes_extra ON t_comptes_extra.numero = w_ecritures.cti_compte_numero
				LEFT JOIN compta.t_comptes t_comptes_contrepartie ON t_comptes_contrepartie.numero = w_ecritures.cti_compte_contrepartie_numero
				JOIN compta.t_journaux ON t_journaux.code_original = w_ecritures.journal_code
				LEFT JOIN compta.t_sections_analytiques t_sections_analytiques_1 ON t_sections_analytiques_1.code_original = analytique_0_code 
				LEFT JOIN compta.t_sections_analytiques t_sections_analytiques_2 ON t_sections_analytiques_2.code_original = analytique_1_code 
				LEFT JOIN compta.t_sections_analytiques t_sections_analytiques_3 ON t_sections_analytiques_3.code_original = analytique_2_code 
				LEFT JOIN compta.t_sections_analytiques t_sections_analytiques_4 ON t_sections_analytiques_4.code_original = analytique_3_code 
				LEFT JOIN compta.t_sections_analytiques t_sections_analytiques_5 ON t_sections_analytiques_5.code_original = analytique_4_code 
				LEFT JOIN compta.t_sections_analytiques t_sections_analytiques_6 ON t_sections_analytiques_6.code_original = analytique_5_code 
				LEFT JOIN compta.t_partenaires ON t_partenaires.code_original = w_ecritures.partenaire_code
				;

				SELECT base.cti_enable_index('compta', 'i_historique_ecritures_1');				
				SELECT base.cti_enable_index('compta', 'i_historique_ecritures_2');
				SELECT base.cti_enable_index('compta', 'i_historique_ecritures_3');
				SELECT base.cti_enable_index('compta', 'i_historique_ecritures_4');
				SELECT base.cti_enable_index('compta', 'i_historique_ecritures_5');
				SELECT base.cti_enable_index('compta', 'i_historique_ecritures_6');
				SELECT base.cti_enable_index('compta', 'i_historique_ecritures_7');
				SELECT base.cti_enable_index('compta', 'i_historique_ecritures_8');
				SELECT base.cti_enable_index('compta', 'i_historique_ecritures_9');
				SELECT base.cti_enable_index('compta', 'i_historique_ecritures_10');
				SELECT base.cti_enable_index('compta', 'i_historique_ecritures_11');
				SELECT base.cti_enable_index('compta', 'i_historique_ecritures_12');
				SELECT base.cti_enable_index('compta', 'i_historique_ecritures_13');
				
			]]></sqlcmd>
		</NODE>
		
	</NODE>
	
	<NODE name="POST" label="TRAITEMENTS COMPLEMENTAIRES COMMUNS">
	</NODE>	

	<NODE name="CHIFFRIER" label="RECUPERATION CHIFFRIERS">	
		<NODE label="Génération chiffrier des soldes généraux">
			<sqlcmd><![CDATA[
			
				-- Chiffrier 
				DROP TABLE IF EXISTS w_chiffrier_soldes_generaux
				;

				CREATE TEMP TABLE w_chiffrier_soldes_generaux AS
					SELECT 
						(fi_exp_soldes_generaux.exercice || fi_exp_soldes_generaux_detail.fk_periode)::numeric AS mois_comptable,
						fk_societe || fk_etablissement AS site_code,
						fk_numero_compte AS compte_numero,
						CASE WHEN fk_centralisateur = fk_numero_compte THEN fk_numero_compte ELSE fk_numero_compte || '-' || fk_centralisateur END AS cti_compte_code,
						'C'::text AS clinique_honoraire,
						fi_exp_soldes_generaux_detail.solde_debit_monnaie1::numeric AS montant_debit,
						fi_exp_soldes_generaux_detail.solde_credit_monnaie1::numeric AS montant_credit
					FROM prod_talentia_finance_csv.fi_exp_soldes_generaux
					JOIN prod_talentia_finance_csv.fi_exp_soldes_generaux_detail ON fi_exp_soldes_generaux_detail.soldes_generaux_id = fi_exp_soldes_generaux.id
					WHERE 
						fk_numero_compte BETWEEN '1' AND '9'
						AND (fi_exp_soldes_generaux.exercice || fi_exp_soldes_generaux_detail.fk_periode)::numeric >= '[ENV_COMPTA_ANNEEDEBUT]01'
				;

			]]></sqlcmd>
		</NODE>
		<NODE label="Génération chiffrier des soldes analytiques">
			<sqlcmd><![CDATA[
			
				-- Chiffrier 
				DROP TABLE IF EXISTS w_chiffrier_soldes_analytiques
				;

				CREATE TEMP TABLE w_chiffrier_soldes_analytiques AS
					SELECT 
						(fi_ana_solde_analytique.exercice || fi_ana_solde_analytique_detail.fk_periode)::numeric AS mois_comptable,
						fk_societe || fk_etablissement AS site_code,
						fk_numero_compte AS compte_numero,
						CASE WHEN fk_centralisateur = fk_numero_compte THEN fk_numero_compte ELSE fk_numero_compte || '-' || fk_centralisateur END AS cti_compte_code,
						'C'::text AS clinique_honoraire,
						fi_ana_solde_analytique_detail.solde_debit_monnaie1::numeric AS montant_debit,
						fi_ana_solde_analytique_detail.solde_credit_monnaie1::numeric AS montant_credit
					FROM prod_talentia_finance_csv.fi_ana_solde_analytique
					JOIN prod_talentia_finance_csv.fi_ana_solde_analytique_detail ON fi_ana_solde_analytique_detail.solde_analytique_id = fi_ana_solde_analytique.id
					WHERE 
						fk_numero_compte BETWEEN '1' AND '9'
						AND (fi_ana_solde_analytique.exercice || fi_ana_solde_analytique_detail.fk_periode)::numeric >= '[ENV_COMPTA_ANNEEDEBUT]01'
				;

			]]></sqlcmd>
		</NODE>
	
		<NODE label="Insertion des chiffriers en base de données">
			<sqlcmd><![CDATA[
			
				TRUNCATE compta.p_chiffrier_comptable
				;

				-- Insertion du chiffrier des soldes généraux en base de données
				INSERT INTO compta.p_chiffrier_comptable
					(
					mois_comptable,
					site_id,
					clinique_honoraire,
					montant_debit,
					montant_credit
					)
					SELECT
						w_chiffrier_soldes_generaux.mois_comptable,
						COALESCE(t_sites.oid,0) AS site_id,
						w_chiffrier_soldes_generaux.clinique_honoraire,
						w_chiffrier_soldes_generaux.montant_debit,
						w_chiffrier_soldes_generaux.montant_credit
					FROM w_chiffrier_soldes_generaux
					LEFT JOIN compta.t_sites ON t_sites.code_original = w_chiffrier_soldes_generaux.site_code
				;

				-- Insertion du chiffrier des soldes analytiques en base de données
				INSERT INTO compta.p_chiffrier_comptable
					(
					mois_comptable,
					site_id,
					clinique_honoraire,
					montant_debit,
					montant_credit
					)
					SELECT
						w_chiffrier_soldes_analytiques.mois_comptable,
						COALESCE(t_sites.oid,0) AS site_id,
						w_chiffrier_soldes_analytiques.clinique_honoraire,
						w_chiffrier_soldes_analytiques.montant_debit,
						w_chiffrier_soldes_analytiques.montant_credit
					FROM w_chiffrier_soldes_analytiques
					LEFT JOIN compta.t_sites ON t_sites.code_original = w_chiffrier_soldes_analytiques.site_code
				;

				TRUNCATE compta.p_chiffrier_comptable_compte
				;

				INSERT INTO compta.p_chiffrier_comptable_compte
					(
					mois_comptable,
					compte_id,
					site_id,
					clinique_honoraire,
					montant_debit,
					montant_credit
					)
					SELECT
						w_chiffrier_soldes_generaux.mois_comptable,
						COALESCE(t_comptes.oid,0) AS compte_id,
						COALESCE(t_sites.oid,0) AS site_id,
						w_chiffrier_soldes_generaux.clinique_honoraire,
						w_chiffrier_soldes_generaux.montant_debit,
						w_chiffrier_soldes_generaux.montant_credit
					FROM w_chiffrier_soldes_generaux
					LEFT JOIN compta.t_sites ON t_sites.code_original = w_chiffrier_soldes_generaux.site_code
					LEFT JOIN compta.t_comptes ON t_comptes.numero = w_chiffrier_soldes_generaux.compte_numero
				;

				INSERT INTO compta.p_chiffrier_comptable_compte
					(
					mois_comptable,
					compte_id,
					site_id,
					clinique_honoraire,
					montant_debit,
					montant_credit
					)
					SELECT
						w_chiffrier_soldes_analytiques.mois_comptable,
						COALESCE(t_comptes.oid,0) AS compte_id,
						COALESCE(t_sites.oid,0) AS site_id,
						w_chiffrier_soldes_analytiques.clinique_honoraire,
						w_chiffrier_soldes_analytiques.montant_debit,
						w_chiffrier_soldes_analytiques.montant_credit
					FROM w_chiffrier_soldes_analytiques
					LEFT JOIN compta.t_sites ON t_sites.code_original = w_chiffrier_soldes_analytiques.site_code
					LEFT JOIN compta.t_comptes ON t_comptes.numero = w_chiffrier_soldes_analytiques.compte_numero
				;

			]]></sqlcmd>
		</NODE>
	
	</NODE>
	
	<NODE name="PARAM" label="RECUPERATION DES PARAMETRES">	
		<NODE label="Paramètres">
			<sqlcmd><![CDATA[

				-- Sites (établissements)
				INSERT INTO compta.t_sites(code_original, code, texte, clinique_honoraire)
					SELECT 
						fk_societe||uk_code, 
						fk_societe||'-'||uk_code, 
						LIB_ETABLISSEMENT, 
						'C'
					FROM prod_talentia_finance_csv.fi_par_etablissement
				WHERE fk_societe||uk_code NOT IN (SELECT code_original FROM compta.t_sites WHERE code_original IS NOT NULL)
				ORDER BY fk_societe||uk_code
				;


				-- Types compta
				INSERT INTO compta.t_types_compta(code, code_original, texte, specialite) 
				SELECT code, code_original, texte, specialite
				FROM
					(
					SELECT 'GES' AS code, 'GES' AS code_original, 'Gestion' AS texte, 'GES' AS specialite
					UNION
					SELECT 'BIL', 'BIL', 'Bilan', 'BIL'
					) subview
				WHERE code_original NOT IN (SELECT code_original FROM compta.t_types_compta WHERE code_original IS NOT NULL);

				DROP TABLE IF EXISTS w_comptes_tiers
				;
				
				CREATE TEMP TABLE w_comptes_tiers AS
					SELECT 
						uk_numero_compte AS compte_numero, 
						lib_compte AS compte_texte, 
						substr(uk_numero_compte,1,2) AS cti_type_compte
					FROM prod_talentia_finance_csv.fi_tie_tiers
				;
				
				INSERT INTO compta.t_types_compta(code, code_original, texte, specialite) 
					SELECT 
						cti_type_compte,
						cti_type_compte,
						MAX(
							CASE 
								WHEN cti_type_compte LIKE 'C%' THEN 'Clients ' || cti_type_compte
								WHEN cti_type_compte LIKE 'F%' THEN 'Fournisseurs ' || cti_type_compte
								ELSE 'Autres' 
							END),
						MAX(
							CASE 
								WHEN cti_type_compte LIKE 'C%' THEN 'CLI '
								WHEN cti_type_compte LIKE 'F%' THEN 'FOU '
								ELSE '' 
							END)
					FROM w_comptes_tiers
					WHERE cti_type_compte NOT IN (SELECT code_original FROM compta.t_types_compta WHERE code_original IS NOT NULL)
					GROUP BY 1,2
				;

				-- Sections analytiques
				INSERT INTO compta.t_sections_analytiques(oid, code, code_original, texte) 
				SELECT 0, '0000', '', 'Non renseignée'
				WHERE 0 NOT IN (SELECT oid FROM compta.t_sections_analytiques);

				INSERT INTO compta.t_sections_analytiques(code_original, code, texte)
				SELECT AXE, AXE, LIB_AXE
				FROM
				(
				SELECT uk_code||'-0' AS AXE, LIB_AXE
				FROM prod_talentia_finance_csv.fi_ana_axe_analytique0
				UNION
				SELECT uk_code||'-1', LIB_AXE
				FROM prod_talentia_finance_csv.fi_ana_axe_analytique1
				UNION
				SELECT uk_code||'-2', LIB_AXE
				FROM prod_talentia_finance_csv.fi_ana_axe_analytique2
				UNION
				SELECT uk_code||'-3', LIB_AXE
				FROM prod_talentia_finance_csv.fi_ana_axe_analytique3
				UNION
				SELECT uk_code||'-4', LIB_AXE
				FROM prod_talentia_finance_csv.fi_ana_axe_analytique4
				UNION
				SELECT uk_code||'-5', LIB_AXE
				FROM prod_talentia_finance_csv.fi_ana_axe_analytique5
				) subview
				WHERE AXE NOT IN (SELECT code_original FROM compta.t_sections_analytiques WHERE code_original IS NOT NULL)
				ORDER BY AXE
				;

				-- Partenaires (tiers)
				INSERT INTO compta.t_partenaires(oid, code, code_original, texte, texte_court) 
					SELECT 0, chr(127) || '***', '', 'Non renseigné', 'Non renseigné'
					WHERE 0 NOT IN (SELECT oid FROM compta.t_partenaires)
				;

				INSERT INTO compta.t_partenaires(code_original, code, texte, texte_court)
					SELECT 
						uk_numero_compte,
						uk_numero_compte,
						(MAX(ARRAY[LAST_MODIFIED_DATE::text, lib_compte::text]))[2]::text,
						(MAX(ARRAY[LAST_MODIFIED_DATE::text, lib_compte::text]))[2]::text
					FROM prod_talentia_finance_csv.fi_tie_tiers
					WHERE uk_numero_compte NOT IN (SELECT code_original FROM compta.t_partenaires WHERE code_original IS NOT NULL)
					GROUP BY uk_numero_compte
				;

				-- Journaux
				INSERT INTO compta.t_journaux(oid, code, code_original, texte) 
				SELECT 0, chr(127) || '***', '', 'Non renseigné'
				WHERE 0 NOT IN (SELECT oid FROM compta.t_journaux);

				INSERT INTO compta.t_journaux(code, code_original, texte, a_nouveaux, clinique_honoraire)
				SELECT 
					uk_code,
					uk_code,
					libelle_journal,
					'0',
					'C'
				FROM prod_talentia_finance_csv.fi_par_journal
				WHERE uk_code NOT IN (SELECT code_original FROM compta.t_journaux WHERE code_original IS NOT NULL)
				ORDER BY uk_code
				;
				
				INSERT INTO compta.t_journaux(code, code_original, texte, a_nouveaux, clinique_honoraire)
				SELECT 
					fk_origine,
					fk_origine,
					CASE 
						WHEN lib_origine NOT LIKE '*****%' 
						THEN lib_origine 
						ELSE fk_origine 
					END,
					'0', 
					'C'
				FROM prod_talentia_finance_csv.fi_gen_origine
				WHERE fk_origine NOT IN (SELECT code_original FROM compta.t_journaux WHERE code_original IS NOT NULL)
				;

				-- Comptes
				INSERT INTO compta.t_comptes(
					type_compta_id, numero, texte, compte_general_id, clinique_honoraire, 
					banque, collectif, type_compta_extra_id, numero_extra)
				SELECT 
					COALESCE(t_types_compta.oid,0) AS type_compta_id,			
					uk_numero_compte AS numero,
					MAX(lib_compte) AS texte,
					0 AS compte_general_id,
					'C' AS clinique_honoraire,
					MAX(CASE WHEN uk_numero_compte LIKE '512%' IS NOT NULL THEN '1' ELSE '0' END) AS banque,
					'0' AS collectif,
					0 AS type_compta_extra_id, 
					'' AS numero_extra
				FROM prod_talentia_finance_csv.fi_gen_plan_comptable
				LEFT JOIN compta.t_types_compta ON 
					CASE WHEN 
						uk_numero_compte LIKE '6%' OR 
						uk_numero_compte LIKE '7%' OR 
						uk_numero_compte LIKE '8%' OR
						uk_numero_compte LIKE '9%' 
					THEN 'GES' 
					ELSE 'BIL' 
					END = t_types_compta.code_original
				WHERE uk_numero_compte NOT IN (SELECT numero FROM compta.t_comptes WHERE numero IS NOT NULL)
				GROUP BY 1,2
				;

				UPDATE compta.t_comptes SET compte_general_id = oid 
				WHERE type_compta_id IN (SELECT oid FROM compta.t_types_compta 
				WHERE code IN ('BIL', 'GES')) AND compte_general_id <> oid AND partenaire_id = 0;

				-- Comptes des tiers
				INSERT INTO compta.t_comptes(
					type_compta_id, numero, texte, compte_general_id, clinique_honoraire, 
					banque, collectif, type_compta_extra_id, numero_extra)
					with comptes_prestataires_ecritures AS (
						SELECT 
							CASE WHEN fk_centralisateur = fk_compte THEN fk_compte ELSE fk_centralisateur || '-' || fk_compte END AS cti_compte_numero,
							fk_centralisateur AS centralisateur_code,  -- CPTC
							fk_compte AS compte_numero -- NCPT
						FROM prod_talentia_finance_csv.fi_gen_ecriture_gene
						WHERE fk_compte <> fk_centralisateur
						GROUP BY 1,2,3
					)
					SELECT 
						COALESCE(t_types_compta.oid,0) AS type_compta_id,			
						comptes_prestataires_ecritures.cti_compte_numero, 
						MAX(fi_gen_plan_comptable.lib_compte || ' - ' || w_comptes_tiers.compte_texte) AS texte, 
						MAX(t_comptes.oid) AS compte_general_id,
						'C' AS clinique_honoraire,
						'0' AS banque,
						'0' AS collectif,
						0 AS type_compta_extra_id, 
						'' AS numero_extra
					FROM comptes_prestataires_ecritures
					JOIN prod_talentia_finance_csv.fi_gen_plan_comptable ON fi_gen_plan_comptable.uk_numero_compte = comptes_prestataires_ecritures.centralisateur_code
					JOIN w_comptes_tiers ON w_comptes_tiers.compte_numero = comptes_prestataires_ecritures.compte_numero
					JOIN compta.t_types_compta ON w_comptes_tiers.cti_type_compte = t_types_compta.code_original
					JOIN compta.t_comptes ON centralisateur_code = t_comptes.numero
					WHERE comptes_prestataires_ecritures.cti_compte_numero NOT IN (SELECT numero FROM compta.t_comptes WHERE numero IS NOT NULL)
					GROUP BY 1,2
				;

			]]></sqlcmd>
		</NODE>
		
	</NODE>
	
</ROOT>