<?xml version="1.0" encoding="ISO-8859-1" ?>
<ROOT>
	
	
	<NODE name="INIT" label="INITIALISATIONS">
	
		<NODE label="Détection Mois modifiés">
			<sqlcmd><![CDATA[

				-- Ecritures CTI
				DROP TABLE IF EXISTS w_historique_ecritures_total;
				CREATE TEMP TABLE w_historique_ecritures_total AS
				SELECT mois_comptable, 
					t_comptes.numero as numero, 
					SUM(montant_debit) as debit, 
					SUM(montant_credit) as credit, 
					SUM(nombre_ecritures) as count
				FROM compta.p_historique_ecritures_total
					JOIN compta.t_comptes ON p_historique_ecritures_total.compte_extra_id = t_comptes.oid
				WHERE mois_comptable >= 200801 AND ajustement = '0' AND
					is_budget IS DISTINCT FROM '1'
				GROUP BY 1,2
				ORDER BY 1,2;

				-- Ecritures prestataire
				DROP TABLE IF EXISTS w_ECRITUREC;
				CREATE TEMP TABLE w_ECRITUREC AS
				SELECT 
					date_part('year',JM_DATE) * 100 + date_part('month',JM_DATE) AS mois_comptable,
					COALESCE(w_COMPTET.CGCT_NUM,F_ECRITUREC.CG_NUM) AS CG_NUM,
					date_part('year',JM_DATE) AS exercice_comptable,
					date(JM_DATE) AS JM_DATE,
					Count(*) AS COUNT, 
					round(SUM(CASE WHEN EC_SENS <> 1 THEN EC_MONTANT ELSE 0 END),2) AS EC_MONTANT_debit, 
					round(SUM(CASE WHEN EC_SENS = 1 THEN EC_MONTANT ELSE 0 END),2) AS EC_MONTANT_credit
				FROM prod_sagei7compta.F_ECRITUREC
				LEFT JOIN w_COMPTET ON F_ECRITUREC.CG_NUM = w_COMPTET.CG_NUM AND
								F_ECRITUREC.CT_NUM = w_COMPTET.CT_NUM AND
								F_ECRITUREC.CT_NUM <> ''
				GROUP BY 1,2,3,4
				ORDER BY 1,2,3;
			

				DROP TABLE IF EXISTS w_mois_modifies;
				CREATE TEMP TABLE w_mois_modifies AS
				SELECT w_ECRITUREC.exercice_comptable,
					w_ECRITUREC.mois_comptable,
					JM_DATE
				FROM w_ECRITUREC
					LEFT JOIN w_historique_ecritures_total ON 
						w_historique_ecritures_total.mois_comptable = w_ECRITUREC.mois_comptable AND
						w_historique_ecritures_total.numero = w_ECRITUREC.CG_NUM 
				GROUP BY 1,2,3
				HAVING MAX(CASE WHEN w_historique_ecritures_total.count IS DISTINCT FROM w_ECRITUREC.count OR
						w_historique_ecritures_total.debit IS DISTINCT FROM w_ECRITUREC.EC_MONTANT_debit OR
						w_historique_ecritures_total.credit IS DISTINCT FROM w_ECRITUREC.EC_MONTANT_credit
					THEN 1 ELSE 0 END) = 1;

				ALTER TABLE w_mois_modifies ADD CONSTRAINT w_mois_modifies_pk PRIMARY KEY(mois_comptable);		
				
				CTISELECT_PROPERTY_READ 'MOISMODIFIES', COALESCE(base.cti_group_concat(mois_comptable),'0')
				FROM w_mois_modifies;
				
				
				
				echo Mois modifiés  : [MOISMODIFIES];
										
			]]></sqlcmd>
		</NODE>
		
		
		
	</NODE>

	<NODE name="ECRITURES" label="RECUPERATION ECRITURES">
		<condition><![CDATA[
			"[MOISMODIFIES]" != "0"
		]]></condition>
			
		<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');


				DELETE FROM compta.p_historique_ecritures
				USING w_mois_modifies
				WHERE p_historique_ecritures.mois_comptable = w_mois_modifies.mois_comptable AND
					is_budget IS DISTINCT FROM '1';	
				
				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 détail">
			<sqlcmd><![CDATA[

				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,
					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 
					t_sites.oid AS site_id,
					t_comptes.type_compta_id,
					t_comptes.compte_general_id AS compte_id,
					date(EC_DATE) AS date_ecriture,
					w_mois_modifies.exercice_comptable,
					w_mois_modifies.mois_comptable,
					t_sites.clinique_honoraire,
					t_journaux.oid AS journal_id,
					COALESCE(t_sections_analytiques.oid, 0) AS section_analytique_id,
					0::bigint AS section_analytique_2_id,
					COALESCE(w_COMPTET.partenaire_id,0) AS partenaire_id,
					COALESCE(w_COMPTET.compte_auxiliaire_id,t_comptes.oid) AS compte_extra_id,
					'1'::text AS comptabilisee,
					'0'::text AS fin_exercice,
					'0'::text AS ajustement,
					'0'::text AS inter_site,
					to_char(F_ECRITUREC.EC_NO,'FM0000000'),
					''::text AS identifiant_operation,
					COALESCE(t_comptes_contre.oid,0) AS compte_contrepartie_id,
					substr(EC_INTITULE,1,100) AS texte,
					'20991231'::date AS date_facture_fournisseur,
					EC_PIECE AS piece,
					EC_LETTRAGE AS dossier,
					CASE WHEN date(F_ECRITUREC.EC_ECHEANCE) >= '20000101' THEN F_ECRITUREC.EC_ECHEANCE ELSE '20991231' END AS date_echeance,
					0::bigint AS journal_paiement_id,
					0::bigint AS mode_paiement_id,
					''::text AS numero_cheque,
					round(CASE WHEN EC_SENS = 1 THEN
							CASE WHEN CA_Num IS NULL THEN EC_MONTANT ELSE EA_Montant END
							ELSE 0 END,2) AS montant_credit,
					round(CASE WHEN EC_SENS <> 1 THEN 
							CASE WHEN CA_Num IS NULL THEN EC_MONTANT ELSE EA_Montant END
							ELSE 0 END,2) AS montant_debit
				FROM prod_sagei7compta.F_ECRITUREC
					JOIN compta.t_divers ON t_divers.code = 'SAGEI7_ANA1'
					LEFT JOIN prod_sagei7compta.F_ECRITUREA ON F_ECRITUREC.EC_No = F_ECRITUREA.EC_No AND F_ECRITUREA.N_ANALYTIQUE::text = t_divers.valeur
					-- todo gérer plusieurs nivo
					JOIN w_mois_modifies ON 
						date(F_ECRITUREC.JM_DATE) = w_mois_modifies.JM_DATE
					JOIN compta.t_sites ON t_sites.oid::text = ANY(string_to_array('[SITES]'::text, ',')) OR '[SITES]' = '1' 
					JOIN compta.t_journaux ON JO_NUM = t_journaux.code_original
					JOIN compta.t_comptes ON CG_NUM = t_comptes.numero
					LEFT JOIN w_COMPTET ON F_ECRITUREC.CG_NUM = w_COMPTET.CG_NUM AND
								F_ECRITUREC.CT_NUM = w_COMPTET.CT_NUM AND
								F_ECRITUREC.CT_NUM <> ''
					LEFT JOIN compta.t_comptes t_comptes_contre ON CG_NUMCONT = t_comptes_contre.numero
					LEFT JOIN compta.t_sections_analytiques ON CA_NUM = t_sections_analytiques.code_original
				ORDER BY F_ECRITUREC.EC_NO
				;

				
				
				
				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">
		
		<condition><![CDATA[
			"[MOISMODIFIES]" != "0"
		]]></condition>
		
		
		<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 label="Echéancier fournisseur">
		
			<sqlcmd><![CDATA[
		
				-- Ecritures fournisseur
				
				DROP TABLE IF EXISTS w_exercice_fournisseur;
				CREATE TEMP TABLE w_exercice_fournisseur AS
				SELECT MAX(exercice_comptable) AS exercice_comptable_fournisseur
				FROM compta.p_historique_ecritures
					JOIN compta.t_comptes_c t_comptes_c_extra ON p_historique_ecritures.compte_extra_id = t_comptes_c_extra.oid
					JOIN compta.t_journaux ON t_journaux.oid = journal_id AND t_journaux.code = 'AN'
					WHERE exercice_comptable >= 2015 AND t_comptes_c_extra.type_compta_code='401'
				;

				
				TRUNCATE compta.p_ecriture_fournisseur;

				INSERT INTO compta.p_ecriture_fournisseur
					(
					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,
					ecriture_consolidee,
					facture_soldee,
					ecriture_exclue
					)
				SELECT
					compte_id,
					date_ecriture,
					mois_comptable,
					montant_credit,
					montant_debit,
					journal_id,
					section_analytique_id,
					p_historique_ecritures.clinique_honoraire,
					exercice_comptable,
					p_historique_ecritures.type_compta_id,
					compte_extra_id,
					comptabilisee,
					fin_exercice,
					ajustement,
					cle_originale,
					compte_contrepartie_id,
					p_historique_ecritures.texte,
					date_facture_fournisseur,
					piece,
					dossier,
					date_echeance,
					journal_paiement_id,
					mode_paiement_id,
					numero_cheque,
					site_id,
					partenaire_id,
					inter_site,
					identifiant_operation,
					ecriture_consolidee,
					'0'::text AS facture_soldee,
					'0'::text AS ecriture_exclue
				FROM compta.p_historique_ecritures
				JOIN w_exercice_fournisseur ON exercice_comptable >= exercice_comptable_fournisseur
				JOIN compta.t_comptes_c t_comptes_c_extra ON p_historique_ecritures.compte_extra_id = t_comptes_c_extra.oid AND
					t_comptes_c_extra.type_compta_code='401'
				;


				UPDATE compta.p_ecriture_fournisseur
				SET facture_soldee = '1'
				FROM
					(
					SELECT 
						compte_extra_id
					FROM compta.p_ecriture_fournisseur
					GROUP BY 1
					HAVING SUM(montant_credit) = SUM(montant_debit)
					) subview
				WHERE compta.p_ecriture_fournisseur.compte_extra_id = subview.compte_extra_id 
				;


				UPDATE compta.p_ecriture_fournisseur
				SET facture_soldee = '1'
				FROM
					(
					SELECT 
						compte_extra_id,
						dossier
					FROM compta.p_ecriture_fournisseur
					WHERE facture_soldee = '0'
					GROUP BY 1,2
					HAVING SUM(montant_credit) = SUM(montant_debit)
					) subview
				WHERE compta.p_ecriture_fournisseur.compte_extra_id = subview.compte_extra_id AND
					compta.p_ecriture_fournisseur.dossier = subview.dossier AND
					facture_soldee = '0'
				;

				UPDATE compta.p_ecriture_fournisseur
				SET facture_soldee = '1'
				FROM
					(
					SELECT 
						compte_extra_id,
						dossier,
						journal_id
					FROM compta.p_ecriture_fournisseur
					WHERE facture_soldee = '0' 
					GROUP BY 1,2,3
					HAVING SUM(montant_credit) = SUM(montant_debit)

					) subview
				WHERE compta.p_ecriture_fournisseur.compte_extra_id = subview.compte_extra_id AND
					compta.p_ecriture_fournisseur.dossier = subview.dossier AND
					compta.p_ecriture_fournisseur.journal_id = subview.journal_id AND
					compta.p_ecriture_fournisseur.facture_soldee = '0'
				;

				UPDATE compta.p_ecriture_fournisseur
				SET facture_soldee = '1'
				FROM
					(
					SELECT 
						compte_extra_id,
						journal_id
					FROM compta.p_ecriture_fournisseur
					WHERE facture_soldee = '0' 
					GROUP BY 1,2
					HAVING SUM(montant_credit) = SUM(montant_debit)

					) subview
				WHERE compta.p_ecriture_fournisseur.compte_extra_id = subview.compte_extra_id AND
					compta.p_ecriture_fournisseur.journal_id = subview.journal_id AND
					compta.p_ecriture_fournisseur.facture_soldee = '0'
				;

				

				UPDATE compta.p_ecriture_fournisseur
				SET facture_soldee = '1'
				FROM
					(
					SELECT 
						compte_extra_id,
						dossier,
						journal_id
					FROM compta.p_ecriture_fournisseur
					WHERE facture_soldee = '0' 
					GROUP BY 1,2,3
					HAVING SUM(montant_credit) = SUM(montant_debit)

					) subview
				WHERE compta.p_ecriture_fournisseur.compte_extra_id = subview.compte_extra_id AND
					compta.p_ecriture_fournisseur.journal_id = subview.journal_id AND
					compta.p_ecriture_fournisseur.facture_soldee = '0'
				;

				UPDATE compta.p_ecriture_fournisseur 
				SET ecriture_exclue = '1'
				WHERE facture_soldee = '0' AND 
					date_echeance = '20991231'
				;
				UPDATE compta.p_ecriture_fournisseur 
				SET ecriture_exclue = '1'
				WHERE facture_soldee = '0' AND 
					date_echeance < date(date_trunc('month',now()))
				;
						
			]]></sqlcmd>
		
		</NODE>
		
		
		
	</NODE>	

	<NODE name="CHIFFRIER" label="CREATION DU CHIFFRIER">
		
		<condition><![CDATA[
			"[MOISMODIFIES]" != "0"
		]]></condition>
		
		<NODE label="Récupération chiffrier">
			<sqlcmd><![CDATA[

				DELETE FROM compta.p_chiffrier_comptable
				USING w_mois_modifies
				WHERE p_chiffrier_comptable.mois_comptable = w_mois_modifies.mois_comptable;	
			
				INSERT INTO compta.p_chiffrier_comptable
					(
					mois_comptable,
					site_id,
					clinique_honoraire,
					montant_debit,
					montant_credit
					)
				SELECT 
					w_mois_modifies.mois_comptable,
					t_sites.oid,
					t_sites.clinique_honoraire,
					round(SUM(CASE WHEN EC_SENS <> 1 THEN EC_MONTANT ELSE 0 END),2) AS EC_MONTANT_debit, 
					round(SUM(CASE WHEN EC_SENS = 1 THEN EC_MONTANT ELSE 0 END),2) AS EC_MONTANT_credit
				FROM prod_sagei7compta.F_ECRITUREC
				JOIN w_mois_modifies ON date(F_ECRITUREC.JM_DATE) = w_mois_modifies.JM_DATE
				JOIN compta.t_sites ON t_sites.oid::text = ANY(string_to_array('[SITES]'::text, ',')) OR '[SITES]' = '1' 
				GROUP BY 1,2,3;
										
			]]></sqlcmd>
		</NODE>
		
		
	</NODE>
	
	<NODE name="PARAM" label="RECUPERATION DES PARAMETRES">

		<NODE label="Paramètres">
			<sqlcmd><![CDATA[
			
				-- Periode comptable
				
				-- Initialisation du niveau de section analytique à récupérer
                INSERT INTO compta.t_divers (code, texte, valeur, description)
				SELECT
					'SAGEI7_ANA1',
					'Niveau 1 de section analytique',
					(SELECT COALESCE(MAX(N_ANALYTIQUE),1)::text AS N_ANALYTIQUE FROM prod_sagei7compta.F_ECRITUREA),
					''
                WHERE 'SAGEI7_ANA1' NOT IN (SELECT code FROM compta.t_divers);
                ;
				

				-- Site = 
				INSERT INTO compta.t_sites(code_original, code, texte, clinique_honoraire)
				SELECT 1, '001', D_RAISONSOC, 'C'
				FROM prod_sagei7compta.P_DOSSIER
				WHERE 1 NOT IN (SELECT code_original FROM compta.t_sites WHERE code_original IS NOT NULL) ;
										
				-- Types de 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'
					UNION
					SELECT 'SPE', 'SPE', 'Comptes spéciaux', ''
					) subview
				WHERE code_original NOT IN (SELECT code_original FROM compta.t_types_compta WHERE code_original IS NOT NULL);
				
				INSERT INTO compta.t_types_compta(code, code_original, texte, specialite) 
				SELECT 
					rtrim(T_VAL01T_COMPTE, '0'),
					rtrim(T_VAL01T_COMPTE, '0'),
					T_VAL01T_INTITULE,
					CASE substr(T_VAL01T_COMPTE,1,2)
					WHEN '41' THEN 'CLI'
					WHEN '40' THEN 'FOU'
					ELSE 'DIV' END
				FROM prod_sagei7compta.P_TIERS
				WHERE T_VAL01T_COMPTE <> '' AND
					T_VAL01T_COMPTE IN (SELECT CG_NUM FROM prod_sagei7compta.F_ECRITUREC) AND
					rtrim(T_VAL01T_COMPTE, '0')  NOT IN (SELECT code_original FROM compta.t_types_compta WHERE code_original IS NOT NULL);


				
				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 
					CA_NUM, 
					CA_NUM,
					MAX(CA_INTITULE)
				FROM prod_sagei7compta.F_COMPTEA
				JOIN compta.t_divers ON t_divers.code = 'SAGEI7_ANA1'
				WHERE 1=1 AND
					N_ANALYTIQUE::text = t_divers.valeur AND
					CA_NUM NOT IN (SELECT code_original FROM compta.t_sections_analytiques WHERE code_original IS NOT NULL)
				GROUP BY 1,2
				;

				UPDATE compta.t_sections_analytiques SET
					texte = subview.CA_INTITULE
				FROM
				(
					SELECT 
						CA_NUM, 
						MAX(CA_INTITULE) AS CA_INTITULE
					FROM prod_sagei7compta.F_COMPTEA
					JOIN compta.t_divers ON t_divers.code = 'SAGEI7_ANA1'
					WHERE N_ANALYTIQUE::text = t_divers.valeur
					GROUP BY 1
				) subview
				WHERE code_original = CA_NUM AND
					texte IS DISTINCT FROM CA_INTITULE
				;
				
				INSERT INTO compta.t_sections_analytiques(code_original, code, texte) 
				SELECT 
					CA_NUM, 
					CA_NUM,
					CA_NUM
				FROM 
					prod_sagei7compta.F_ECRITUREA
				WHERE 1=1 AND
					CA_NUM NOT IN (SELECT code_original FROM compta.t_sections_analytiques WHERE code_original IS NOT NULL)
				GROUP BY 1,2,3
				;
				
				
				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);

		
				-- Journaux
				
				INSERT INTO compta.t_journaux(code_original, code, texte, a_nouveaux, clinique_honoraire)
				SELECT JO_NUM, JO_NUM, JO_INTITULE, '0', 'C'
				FROM prod_sagei7compta.F_JOURNAUX
				WHERE JO_NUM NOT IN (SELECT code_original FROM compta.t_journaux WHERE code_original IS NOT NULL) AND
					JO_NUM IN (SELECT JO_NUM FROM prod_sagei7compta.F_ECRITUREC)
				ORDER BY 1;
				
				-- Classes de comptes
				
				
				
				-- Comptes
				UPDATE compta.t_comptes SET
					type_compta_id = subview.type_compta_id,
					texte = subview.texte
				FROM
					(
					SELECT 
						COALESCE(t_types_compta.oid,0) AS type_compta_id,			
						F_COMPTEG.CG_NUM AS numero, 
						MAX(CG_INTITULE) AS texte
					FROM prod_sagei7compta.F_COMPTEG
					LEFT JOIN compta.t_types_compta ON 
						CASE 
						WHEN F_COMPTEG.CG_NUM LIKE '6%' OR F_COMPTEG.CG_NUM LIKE '7%' THEN 'GES' 
						WHEN F_COMPTEG.CG_NUM LIKE '8%' THEN 'SPE' 
						ELSE 'BIL' END 
							= t_types_compta.code_original
					GROUP BY 1,2
					) subview
				WHERE t_comptes.numero = subview.numero AND
					(
					t_comptes.type_compta_id IS DISTINCT FROM subview.type_compta_id OR
					t_comptes.texte IS DISTINCT FROM subview.texte
					);
									
			
				INSERT INTO compta.t_comptes(
						type_compta_id, numero, texte, compte_general_id, clinique_honoraire, 
						banque, collectif, type_compta_extra_id, numero_extra, partenaire_id)
				SELECT 
						COALESCE(t_types_compta.oid,0) AS type_compta_id,			
						F_COMPTEG.CG_NUM AS numero, 
						MAX(CG_INTITULE) AS texte, 
						0 AS compte_general_id,
						'C' AS clinique_honoraire,
						MAX(CASE WHEN F_COMPTEG.CG_NUM LIKE '512%' THEN '1' ELSE '0' END) AS banque,
						'0' AS collectif,
						0 AS type_compta_extra_id, 
						'' AS numero_extra,
						0::bigint AS partenaire_id
				FROM  prod_sagei7compta.F_COMPTEG
					LEFT JOIN compta.t_types_compta ON 
						CASE 
						WHEN F_COMPTEG.CG_NUM LIKE '6%' OR F_COMPTEG.CG_NUM LIKE '7%' THEN 'GES' 
						WHEN F_COMPTEG.CG_NUM LIKE '8%' THEN 'SPE' 
						ELSE 'BIL' END 
							= t_types_compta.code_original
				WHERE F_COMPTEG.CG_NUM NOT IN (SELECT numero FROM compta.t_comptes WHERE numero IS NOT NULL) AND
					(
						F_COMPTEG.CG_NUM IN (SELECT CG_NUM FROM prod_sagei7compta.F_ECRITUREC) OR 
						F_COMPTEG.CG_NUM IN (SELECT CG_NUMCONT FROM prod_sagei7compta.F_ECRITUREC)
					)
				GROUP BY 1,2
				ORDER BY 2;
				
				-- Partenaires
				INSERT INTO compta.t_partenaires(code_original, code, texte, texte_court)
				SELECT CT_NUM, CT_NUM, CT_INTITULE, substr(CT_INTITULE, 1, 50)
				FROM prod_sagei7compta.F_COMPTET
				WHERE CT_NUM IN (SELECT CT_NUM FROM prod_sagei7compta.F_ECRITUREC) AND
					CT_NUM NOT IN (SELECT code_original FROM compta.t_partenaires WHERE code_original IS NOT NULL)
				GROUP BY 1,2,3,4;
				
				UPDATE compta.t_partenaires SET
					texte = CT_INTITULE,
					texte_court = substr(CT_INTITULE, 1, 50)
				FROM prod_sagei7compta.F_COMPTET
				WHERE CT_NUM = code_original AND
					(
					t_partenaires.texte IS DISTINCT FROM CT_INTITULE OR
					t_partenaires.texte_court IS DISTINCT FROM CT_INTITULE
					);
					
				
				-- Comptes auxiliaires 
				
				DROP TABLE IF EXISTS w_COMPTET;
				CREATE TEMP TABLE w_COMPTET AS
				SELECT F_ECRITUREC.CT_NUM,
					F_ECRITUREC.CG_NUM,					
					F_ECRITUREC.CG_NUM || '.' || F_ECRITUREC.CT_NUM AS CGCT_NUM,
					CT_INTITULE,
					max(t_types_compta.oid) AS type_auxiliaire_id,
					0::bigint AS compte_auxiliaire_id,
					0::bigint AS partenaire_id
				FROM prod_sagei7compta.F_ECRITUREC
					JOIN prod_sagei7compta.F_COMPTEG ON F_COMPTEG.CG_NUM = F_ECRITUREC.CG_NUM
					JOIN prod_sagei7compta.F_COMPTET ON F_COMPTET.CT_NUM = F_ECRITUREC.CT_NUM
					JOIN compta.t_types_compta ON F_COMPTEG.CG_NUM LIKE (t_types_compta.code_original || '%')
				WHERE F_ECRITUREC.CT_NUM <> ''
				GROUP BY 1,2,3,4
				ORDER BY 3;



				
				UPDATE w_COMPTET
				SET partenaire_id = t_partenaires.oid
				FROM compta.t_partenaires
				WHERE CT_NUM = t_partenaires.code_original;
				
					
				UPDATE compta.t_comptes SET
					texte = subview.texte,
					compte_general_id = subview.compte_general_id,
					partenaire_id = subview.partenaire_id
				FROM
					(
					SELECT 
						type_auxiliaire_id AS type_compta_id,			
						CGCT_NUM AS numero, 
						MAX(CT_INTITULE) AS texte,
						MAX(COALESCE(t_comptes.oid,0)) AS compte_general_id,
						MAX(w_COMPTET.partenaire_id) AS partenaire_id
					FROM w_COMPTET
					LEFT JOIN compta.t_comptes ON CG_NUM = t_comptes.numero
					GROUP BY 1,2
					) subview
				WHERE t_comptes.numero = subview.numero AND
					(
					t_comptes.texte IS DISTINCT FROM subview.texte OR
					t_comptes.compte_general_id IS DISTINCT FROM subview.compte_general_id OR
					t_comptes.partenaire_id IS DISTINCT FROM subview.partenaire_id
					);
					
				INSERT INTO compta.t_comptes(
						type_compta_id, numero, texte, compte_general_id, clinique_honoraire, 
						banque, collectif, type_compta_extra_id, numero_extra, partenaire_id)
				SELECT 
						type_auxiliaire_id AS type_compta_id,			
						CGCT_NUM AS numero, 
						MAX(CT_INTITULE) AS texte, 
						MAX(COALESCE(t_comptes.oid,0)) AS compte_general_id,
						'C' AS clinique_honoraire,
						'0' AS banque,
						'0' AS collectif,
						0 AS type_compta_extra_id, 
						'' AS numero_extra,
						MAX(w_COMPTET.partenaire_id) AS partenaire_id
				FROM w_COMPTET
					LEFT JOIN compta.t_comptes ON CG_NUM = t_comptes.numero
				WHERE CGCT_NUM NOT IN (SELECT numero FROM compta.t_comptes WHERE numero IS NOT NULL)
				GROUP BY 1,2
				ORDER BY 2;
				
				
				UPDATE w_COMPTET
				SET compte_auxiliaire_id = t_comptes.oid
				FROM compta.t_comptes
				WHERE w_COMPTET.CGCT_NUM = t_comptes.numero;
				
				
				
				UPDATE compta.t_comptes SET
					compte_general_id = oid
				WHERE compte_general_id = 0 OR compte_general_id IS NULL;

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