<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	
	
	<NODE name="INIT" label="INITIALISATIONS">
	
		<NODE label="Détection Mois modifiés">
			<sqlcmd><![CDATA[
			
				
				-- Pour l'instant, tout est remonté sans test
			
							
			]]></sqlcmd>
		</NODE>
		
		
		
	</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');

				-- Pour l'instant, tout est remonté sans test, à terme ne remonter que le dernier exercice comptable (dernier de acpcliex)
				TRUNCATE compta.p_historique_ecritures;
				--DELETE FROM compta.p_historique_ecritures
				--USING (SELECT to_char(MIN(DEXE),'YYYYMM') as date_debut_acropole, MAX(FEXE) as date_fin_acropole FROM prod_acropole.acpcliex) sub
				--WHERE mois_comptable >= date_debut_acropole;
				
				
				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="Préparation">
			<sqlcmd><![CDATA[
			
				DROP TABLE IF EXISTS w_cpt;
				CREATE TEMP TABLE w_cpt AS
				SELECT COMPTE_PC,	LIBELLE_PC, t_comptes.oid AS compte_id
				FROM prod_acropole."0808377P" JOIN compta.t_comptes ON (COMPTE_PC = t_comptes.numero)
				GROUP BY 1,2,3;
				
				DROP TABLE IF EXISTS w_cpt_aux;
				CREATE TEMP TABLE w_cpt_aux AS
				SELECT PLC_COMPTE, t_types_compta.oid AS type_compte_extra_id, t_comptes_aux.oid AS compte_extra_id, coalesce(t_partenaires.oid,0) AS partenaire_id
				FROM prod_acropole."0808377P" 
				JOIN compta.t_comptes ON (w_plx_plc.compte_id = t_comptes.oid)
				JOIN compta.t_comptes t_comptes_aux ON (t_comptes.numero || '_'::text || plx_compte = t_comptes_aux.numero)
				LEFT JOIN compta.t_partenaires ON (plx_idtiers = t_partenaires.code_original);
		
			]]></sqlcmd>
		</NODE> -->
		
		<NODE label="Génération détail">
			<sqlcmd><![CDATA[

				INSERT INTO compta.p_historique_ecritures(
					compte_id, 
					date_ecriture, 
					mois_comptable, 
					montant_credit, 
					montant_debit, 
					journal_id, 
					section_analytique_id, 
					clinique_honoraire, 
					exercice_comptable, 
					type_compta_id, 
					compte_extra_id, 
					comptabilisee, 
					fin_exercice, 
					ajustement, 
					cle_originale, 
					compte_contrepartie_id, 
					texte, 
					date_facture_fournisseur, 
					piece, dossier, 
					date_echeance, 
					journal_paiement_id, 
					mode_paiement_id, 
					numero_cheque, 
					site_id, 
					partenaire_id, 
					inter_site, 
					identifiant_operation
				)
				SELECT
					t_comptes.oid AS compte_id, 
					ECR_DATH AS date_ecriture, 
					ECR_AAMM AS mois_comptable, 
					ECR_CREDEURO AS montant_credit, 
					ECR_DEBEURO AS montant_debit, 
					t_journaux.oid AS journal_id, 
					0 AS section_analytique_id, 
					t_sites.clinique_honoraire, 
					exercice_comptable, 
					t_comptes.type_compta_id, 
					COALESCE(w_cpt_aux.oid,t_comptes.oid), 
					'0' AS comptabilisee, 
					'0' AS fin_exercice, 
					'0' AS ajustement, 
					HICLEUNIK AS cle_originale, 
					COALESCE(t_comptes_contre.oid,0) AS compte_contrepartie_id, 
					ECR_LIB AS texte, 
					'20991231' AS date_facture_fournisseur, 
					'' AS piece, 
					ECR_DOCH AS dossier, 
					'20991231' AS date_echeance, 
					0 AS journal_paiement_id, 
					0 AS mode_paiement_id, 
					'' AS numero_cheque, 
					t_sites.oid AS site_id, 
					COALESCE(w_cpt_aux.partenaire_id,0), 
					'0' AS inter_site, 
					ECR_DOCH::text || '-'::text || ECR_ORDRE::text AS identifiant_operation
				FROM temp.w_08083a7h
					JOIN compta.t_comptes ON (ECR_AFF = t_comptes.numero)
					JOIN compta.t_journaux ON (ECR_CPF = t_journaux.code_original)
					JOIN compta.t_sites ON (ECR_CLI = t_sites.code)
					JOIN base.p_calendrier_mois ON (ECR_AAMM = mois)
					LEFT JOIN compta.t_comptes w_cpt_aux ON (ECR_AFF || '_' || ECR_REP = w_cpt_aux.numero)
					LEFT JOIN compta.t_comptes t_comptes_contre ON t_comptes_contre.oid = -1
				WHERE ECR_AAMM::bigint >= 200701
				;
			
				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 label="Compléments écritures">
		
			<sqlcmd><![CDATA[
		
				UPDATE compta.p_historique_ecritures
				SET clinique_honoraire = 'C'
				FROM compta.t_sites
				WHERE p_historique_ecritures.site_id = t_sites.oid
					AND p_historique_ecritures.clinique_honoraire <> 'C'
					AND site_id IN (SELECT oid FROM compta.t_sites WHERE clinique_honoraire <> 'H');
					
					
				UPDATE compta.p_historique_ecritures
				SET clinique_honoraire = 'H'
				FROM compta.t_sites
				WHERE p_historique_ecritures.site_id = t_sites.oid
					AND p_historique_ecritures.clinique_honoraire <> 'H'
					AND site_id IN (SELECT oid FROM compta.t_sites WHERE clinique_honoraire = 'H');
					
					
				UPDATE compta.p_historique_ecritures 
				SET compte_id = t_comptes.compte_general_id 
				FROM compta.t_comptes 
				WHERE comptabilisee <> '1' 
				AND compte_id = t_comptes.oid 
				AND t_comptes.oid <> t_comptes.compte_general_id 
				AND compte_id = compte_extra_id;
				
				-- Précision cli/hon (cli prioritaire)
				
				UPDATE compta.t_comptes
				SET clinique_honoraire = subview.clinique_honoraire
				FROM 
				(
				SELECT
					compte_id AS compte_id, 
					MIN(CASE WHEN p_historique_ecritures.clinique_honoraire = 'H' THEN 'H' ELSE 'C' END) AS clinique_honoraire
				FROM compta.p_historique_ecritures
				JOIN compta.t_comptes ON compte_id = t_comptes.oid
				GROUP BY 1
				) subview
				WHERE t_comptes.oid = subview.compte_id AND
					t_comptes.clinique_honoraire <> subview.clinique_honoraire;

				UPDATE compta.t_comptes
				SET clinique_honoraire = subview.clinique_honoraire
				FROM 
				(
				SELECT
					t_comptes.oid AS compte_id, t_comptes.numero,
					MIN(CASE WHEN p_historique_ecritures.clinique_honoraire = 'H' THEN 'H' ELSE 'C' END) AS clinique_honoraire
				FROM compta.p_historique_ecritures
				JOIN compta.t_comptes ON compte_extra_id = t_comptes.oid 
				GROUP BY 1,2
				) subview
				WHERE t_comptes.oid = subview.compte_id AND
					t_comptes.clinique_honoraire <> subview.clinique_honoraire;
					
				
				UPDATE compta.t_journaux
				SET clinique_honoraire = subview.clinique_honoraire
				FROM 
				(
				SELECT
					journal_id, 
					MIN(CASE WHEN p_historique_ecritures.clinique_honoraire = 'H' THEN 'H' ELSE 'C' END) AS clinique_honoraire
				FROM compta.p_historique_ecritures
				GROUP BY 1
				) subview
				WHERE t_journaux.oid = subview.journal_id AND
					t_journaux.clinique_honoraire <> subview.clinique_honoraire;

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

	<NODE name="CHIFFRIER" label="CREATION DU CHIFFRIER">
		
		
		<NODE label="Récupération chiffrier">
			<sqlcmd><![CDATA[

				
										
			]]></sqlcmd>
		</NODE>
		
		
	</NODE>
	
	<NODE name="PARAM" label="RECUPERATION DES PARAMETRES">

		<NODE label="Regroupement des tables source">
			<sqlcmd><![CDATA[

				--INSERT INTO prod_acropole."08083a7h" SELECT * FROM prod_acropole."08083a8h";
				--INSERT INTO prod_acropole."08083a7p" SELECT * FROM prod_acropole."08083a8p";
				--INSERT INTO prod_acropole."08083a7s" SELECT * FROM prod_acropole."08083a8s";
				--INSERT INTO prod_acropole."08083a7h" SELECT * FROM prod_acropole."08083a9h";
				--INSERT INTO prod_acropole."08083a7p" SELECT * FROM prod_acropole."08083a9p";
				--INSERT INTO prod_acropole."08083a7s" SELECT * FROM prod_acropole."08083a9s";
				
				DROP TABLE IF EXISTS temp.w_08083a7h;
				DROP TABLE IF EXISTS temp.w_08083a7p;
				DROP TABLE IF EXISTS temp.w_08083a7s;
				CREATE TABLE temp.w_08083a7h AS SELECT * FROM prod_acropole."08083a7h";
				CREATE TABLE temp.w_08083a7p AS SELECT * FROM prod_acropole."08083a7p";
				CREATE TABLE temp.w_08083a7s AS SELECT * FROM prod_acropole."08083a7s";
				INSERT INTO temp.w_08083a7h     SELECT * FROM prod_acropole."08083a8h";
				INSERT INTO temp.w_08083a7p     SELECT * FROM prod_acropole."08083a8p";
				INSERT INTO temp.w_08083a7s     SELECT * FROM prod_acropole."08083a8s";
				INSERT INTO temp.w_08083a7h     SELECT * FROM prod_acropole."08083a9h";
				INSERT INTO temp.w_08083a7p     SELECT * FROM prod_acropole."08083a9p";
				INSERT INTO temp.w_08083a7s     SELECT * FROM prod_acropole."08083a9s";
				INSERT INTO temp.w_08083a7h     SELECT * FROM prod_acropole."08083a0h";
				INSERT INTO temp.w_08083a7p     SELECT * FROM prod_acropole."08083a0p";
				INSERT INTO temp.w_08083a7s     SELECT * FROM prod_acropole."08083a0s";
								
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Paramètres">
			<sqlcmd><![CDATA[
			
				-- Initialisation paramètres
				UPDATE compta.t_sites SET code = chr(127) || '***', texte = 'Non renseigné' WHERE oid = 0;
				INSERT INTO compta.t_sites(oid, code, clinique_honoraire, code_original, texte) 
				SELECT 0, chr(127) || '***', '', '', 'Non renseigné' WHERE 0 NOT IN (SELECT oid FROM compta.t_sites);
				
				UPDATE compta.t_classes_comptes SET code = '0', texte = 'Non renseigné' WHERE oid = 0;
				INSERT INTO compta.t_classes_comptes(oid, code, texte, clinique, honoraire) 
				SELECT 0, '0', 'Non renseigné', '0', '0' WHERE 0 NOT IN (SELECT oid FROM compta.t_classes_comptes);
				
				UPDATE compta.t_partenaires SET code = chr(127) || '****', texte = 'Non renseigné', texte_court = 'Non renseigné' WHERE oid = 0;
				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);
				
				UPDATE compta.t_journaux SET code = chr(127) || '***', texte = 'Non renseigné' WHERE oid = 0;
				INSERT INTO compta.t_journaux(oid, code, clinique_honoraire, code_original, texte) 
				SELECT 0, chr(127) || '***', '', '', 'Non renseigné' WHERE 0 NOT IN (SELECT oid FROM compta.t_journaux);
				
				UPDATE compta.t_sections_analytiques SET code = chr(127) || '***', texte = 'Non renseigné' WHERE oid = 0;
				INSERT INTO compta.t_sections_analytiques(oid, code, code_original, texte) 
				SELECT 0, chr(127) || '***', '', 'Non renseigné' WHERE 0 NOT IN (SELECT oid FROM compta.t_sections_analytiques);
				
				
				
				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_comptes(oid, type_compta_id, numero, texte, compte_general_id, clinique_honoraire)
				SELECT 0, 0, '', 'Non renseigné', 0, '' WHERE 0 NOT IN (SElECT oid FROM compta.t_comptes);
			
				-- Periode comptable
				
				UPDATE base.p_calendrier_mois SET
					exercice_comptable = subview.exercice_comptable::int
				FROM (
					SELECT 
						p_calendrier_mois.mois, 
						p_calendrier_mois.date_debut,
						subview.exercice_comptable
					FROM base.p_calendrier_mois
						JOIN (
							SELECT 
								DEXE,
								FEXE,
								to_char(DEXE,'YYYY') AS exercice_comptable,
								to_char(DEXE,'MM') AS exercice_mois_comptable_1
							FROM
								prod_acropole.acpcliex
						) subview ON date_debut BETWEEN DEXE AND FEXE
					) subview
					WHERE 	p_calendrier_mois.mois = subview.mois AND
						p_calendrier_mois.exercice_comptable IS DISTINCT FROM subview.exercice_comptable;

				UPDATE base.p_calendrier_mois
				SET exercice_mois_comptable_1 = subview.exercice_mois_comptable_1
				FROM
					(
					SELECT exercice_comptable,
						MIN(mois) AS exercice_mois_comptable_1
					FROM base.p_calendrier_mois
					GROUP BY 1
					) subview
				WHERE p_calendrier_mois.exercice_comptable = subview.exercice_comptable AND
					p_calendrier_mois.exercice_mois_comptable_1 IS DISTINCT FROM subview.exercice_mois_comptable_1;
			

				-- Sites
				INSERT INTO compta.t_sites(code_original, code, texte, clinique_honoraire)
				SELECT CLI, CLI, NOMCLI, 'C'
				FROM prod_acropole.ACPCLIEX
				WHERE CLI IN (SELECT DISTINCT SUPP_CLI FROM temp.w_08083a7s)
					AND CLI NOT IN (SELECT code_original FROM compta.t_sites)
				GROUP BY 1,2,3;
					
										
				-- Types de compta
				INSERT INTO compta.t_types_compta(oid, code_original, code, texte) 
				SELECT 0, '', '', 'Non renseigné'
				WHERE 0 NOT IN (SELECT oid FROM compta.t_types_compta);
	
				INSERT INTO compta.t_types_compta(code, code_original, texte, specialite) 
				SELECT * FROM
				(
				SELECT 'GES', 'GES' AS code_original, 'GESTION',  'GES'
				UNION
				SELECT 'BIL', 'BIL' AS code_original, 'BILAN',  'BIL'
				UNION
				SELECT 'RES', 'RES' AS code_original, 'RESULTAT',  'RES'
				) subview
				WHERE code_original NOT IN (SELECT code_original FROM compta.t_types_compta);
	
				-- Journaux
				INSERT INTO compta.t_journaux(code, code_original, texte, a_nouveaux)
				SELECT JRN, JRN, LIBJ, CASE WHEN TYPJO = 5 THEN '1' ELSE '0' END
				FROM prod_acropole.ACPEXJRN
				WHERE JRN NOT IN (SELECT code_original FROM compta.t_journaux)
				GROUP BY 1,2,3,4
				;
				
				
				-- Partenaires
				INSERT INTO compta.t_partenaires(code, code_original, texte, texte_court)
				SELECT PCP_TIE, PCP_TIE, MAX(PCP_NOM), MAX(PCP_NOM)
				FROM temp.w_08083a7p
				WHERE PCP_TIE NOT IN (SELECT code_original FROM compta.t_partenaires)
				AND PCP_TIE IS NOT NULL
				GROUP BY 1 
				ORDER BY 1;
				
				-- Classes de comptes
				
				-- Comptes génériques
				INSERT INTO compta.t_comptes (type_compta_id, numero, texte, clinique_honoraire, banque)
				SELECT t_types_compta.oid, subview.COMPTE_PC, subview.LIBELLE_PC, '',
					CASE WHEN subviewj.JRN IS NOT NULL THEN '1' ELSE '0' END
				FROM
				(SELECT 
					CASE WHEN COMPTE_PC LIKE '6%' THEN 'GES'
						WHEN COMPTE_PC LIKE '7%' THEN 'GES'
						WHEN COMPTE_PC LIKE '8%' THEN 'RES'
						ELSE 'BIL' END AS plc_type,
					COMPTE_PC, 
					MAX(LIBELLE_PC  || CASE WHEN LIBELLE2_PC IS NOT NULL THEN ' ' || LIBELLE2_PC ELSE '' END) AS LIBELLE_PC
				FROM prod_acropole.ACPPLANC WHERE TYP_PC != '' GROUP BY 1,2 ORDER BY 2) subview 
				JOIN compta.t_types_compta ON (plc_type = t_types_compta.code_original)
				LEFT JOIN 
				(
				SELECT DISTINCT JRN FROM prod_acropole.ACPEXJRN WHERE TYPJO = 3
				) subviewj  ON JRN = COMPTE_PC
				LEFT JOIN (SELECT ECR_AFF FROM temp.w_08083a7h GROUP BY 1 order by 1) compte_util ON COMPTE_PC = ECR_AFF
				WHERE ECR_AFF IS NOT NULL AND subview.COMPTE_PC NOT IN (SELECT numero FROM compta.t_comptes) 
				ORDER BY subview.COMPTE_PC;

				-- Comptes spécifiques
				INSERT INTO compta.t_comptes (type_compta_id, numero, texte, clinique_honoraire, banque)
				SELECT t_types_compta.oid, subview.PCP_AFF, subview.PCP_NOM, '',
					CASE WHEN subviewj.JRN IS NOT NULL THEN '1' ELSE '0' END
				FROM
				(SELECT 
					CASE WHEN PCP_AFF LIKE '6%' THEN 'GES'
						WHEN PCP_AFF LIKE '7%' THEN 'GES'
						WHEN PCP_AFF LIKE '8%' THEN 'RES'
						ELSE 'BIL' END AS plc_type,
					PCP_AFF, 
					PCP_NOM
				FROM temp.w_08083a7p WHERE PCP_TIE IS NULL) subview 
				JOIN compta.t_types_compta ON (plc_type = t_types_compta.code_original)
				LEFT JOIN 
				(
				SELECT DISTINCT JRN FROM prod_acropole.ACPEXJRN WHERE TYPJO = 3
				) subviewj  ON JRN = PCP_AFF
				LEFT JOIN (SELECT ECR_AFF FROM temp.w_08083a7h GROUP BY 1) compte_util ON PCP_AFF = ECR_AFF
				WHERE subview.PCP_AFF NOT IN (SELECT numero FROM compta.t_comptes) 
				ORDER BY subview.PCP_AFF;

				UPDATE compta.t_comptes SET texte = PCP_NOM FROM temp.w_08083a7p WHERE numero = PCP_AFF AND PCP_TIE IS NULL;

				UPDATE compta.t_comptes SET compte_general_id = oid WHERE compte_general_id IS NULL OR (compte_general_id = 0 AND oid <> 0);
				
				-- Comptes auxiliaires
				DROP TABLE IF EXISTS w_plx_plc;
				CREATE TEMP TABLE w_plx_plc AS
				SELECT ECR_AFF, ECR_REP, CASE WHEN ECR_AFF LIKE '6%' THEN 'GES'
						WHEN ECR_AFF LIKE '7%' THEN 'GES'
						WHEN ECR_AFF LIKE '8%' THEN 'RES'
						ELSE 'BIL' END AS plc_type
				FROM temp.w_08083a7h 
				WHERE ECR_REP IS NOT NULL
				GROUP BY 1,2,3;

				INSERT INTO compta.t_comptes(
							type_compta_id, numero, texte, compte_general_id, clinique_honoraire, partenaire_id)
				SELECT t_types_compta.oid, t_comptes.numero::text || '_'::text || t_partenaires.code::text, t_partenaires.texte, t_comptes.oid, '', coalesce(t_partenaires.oid,0)
				FROM w_plx_plc
					JOIN compta.t_comptes ON (w_plx_plc.ECR_AFF = t_comptes.numero)
					JOIN compta.t_types_compta ON (plc_type = t_types_compta.code_original)
					LEFT JOIN compta.t_partenaires ON (ECR_REP = t_partenaires.code_original)
				WHERE t_comptes.numero::text || '_'::text || t_partenaires.code::text NOT IN (SELECT numero::text FROM compta.t_comptes);

				-- comptes non présents 
				INSERT INTO compta.t_comptes(type_compta_id, numero, texte, clinique_honoraire, banque)
				SELECT t_types_compta.oid, ECR_AFF, MAX(ECR_LIB), '', 0
				FROM temp.w_08083a7h 
					JOIN compta.t_types_compta ON (CASE WHEN ECR_AFF LIKE '6%' THEN 'GES'
						WHEN ECR_AFF LIKE '7%' THEN 'GES'
						WHEN ECR_AFF LIKE '8%' THEN 'RES'
						ELSE 'BIL' END =  t_types_compta.code_original)
				WHERE ECR_AFF NOT IN (SELECT numero::text FROM compta.t_comptes)
				GROUP BY 1,2;
				
				-- Classes de comptes
				INSERT into compta.t_classes_comptes (code, texte) 
				(SELECT DISTINCT substr(numero, 1 , 1), '' FROM compta.t_comptes  
				WHERE compte_general_id = oid AND numero <> '' AND substr(numero, 1 , 1) NOT IN (SELECT code FROM compta.t_classes_comptes)) 
				UNION 
				SELECT DISTINCT substr(numero, 1 , 2), '' FROM compta.t_comptes  
				WHERE compte_general_id = oid AND numero <> '' AND substr(numero, 1 , 2) NOT IN (SELECT code FROM compta.t_classes_comptes) 
				UNION 
				SELECT DISTINCT substr(numero, 1 , 3), '' FROM compta.t_comptes  
				WHERE compte_general_id = oid AND numero <> '' AND substr(numero, 1 , 3) NOT IN (SELECT code FROM compta.t_classes_comptes) 
				UNION 
				SELECT DISTINCT substr(numero, 1 , 4), '' FROM compta.t_comptes  
				WHERE compte_general_id = oid AND numero <> '' AND substr(numero, 1 , 4) NOT IN (SELECT code FROM compta.t_classes_comptes) 
				UNION 
				SELECT DISTINCT substr(numero, 1 , 5), '' FROM compta.t_comptes  
				WHERE compte_general_id = oid AND numero <> '' AND substr(numero, 1 , 5) NOT IN (SELECT code FROM compta.t_classes_comptes) 
				UNION 
				SELECT DISTINCT substr(numero, 1 , 6), '' FROM compta.t_comptes  
				WHERE compte_general_id = oid AND numero <> '' AND substr(numero, 1 , 6) NOT IN (SELECT code FROM compta.t_classes_comptes) 
				;
				
				UPDATE compta.t_classes_comptes SET texte = '' WHERE texte IS NULL
	
			]]></sqlcmd>
		</NODE>
		
	</NODE>
	
</ROOT>
