<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	
	
	<NODE name="INIT" label="INITIALISATIONS">
	
		<NODE label="RAZ tables">
			<sqlcmd><![CDATA[
				
				DELETE FROM compta.p_historique_ecritures WHERE ajustement = '2';
				DELETE FROM compta.p_historique_ecritures_total WHERE ajustement = '2';
				
			]]></sqlcmd>
		</NODE>
			
	
		<NODE label="Détection Mois modifiés">
			<sqlcmd><![CDATA[
            
                -- Correction CCP01 (chiffrier SHS) pour mois non clos
				UPDATE prod_shs.[COMPTASCHEMA]_CCP01
				SET CCNOR3 = -320
				WHERE CCNOR3 = 320 AND
					CCMLTT = 0 AND
					CCMLCL = 0 AND
					CCMLHO = 0 AND
					(CCANEE*100+CCMOIS) NOT IN (SELECT MCAMTC FROM prod_shs.[COMPTASCHEMA]_MCP01)
				;
			
				-- Attention, aux collectifs vides
				UPDATE prod_shs.[COMPTASCHEMA]_MCP01
				SET MCSANA = PESANA
				FROM prod_shs.[COMPTASCHEMA]_PEP01
				WHERE MCTYPE = PETYPE AND MCCPTE = PECPTE AND
					MCSANA = 0 AND PESANA <> 0 AND
					MCTYPE NOT IN (95,96);
					
				UPDATE prod_shs.[COMPTASCHEMA]F_MCP01F
				SET MCSANA = PESANA
				FROM prod_shs.[COMPTASCHEMA]_PEP01
				WHERE MCTYPE = PETYPE AND MCCPTE = PECPTE AND
					MCSANA = 0 AND PESANA <> 0 AND
					MCTYPE NOT IN (95,96);
					
				UPDATE prod_shs.[COMPTASCHEMA]I_MCP01I
				SET MCSANA = PESANA
				FROM prod_shs.[COMPTASCHEMA]_PEP01
				WHERE MCTYPE = PETYPE AND MCCPTE = PECPTE AND
					MCSANA = 0 AND PESANA <> 0 AND
					MCTYPE NOT IN (95,96);
			
				-- Ecritures exercice en cours
				DROP TABLE IF EXISTS w_CCP01_next;
				CREATE TEMP TABLE w_CCP01_next AS
				SELECT MAX(CCANEE*100+CCMOIS + CASE WHEN CCMOIS <> 12 THEN 1 ELSE 89 END) AS CCAMTC
				FROM prod_shs.[COMPTASCHEMA]_CCP01 
				WHERE CCMOIS <= 12 AND 
					CCNOR3 IN (320) ;
				
				-- Forcage mois sur non comptabilisé
				UPDATE prod_shs.[COMPTASCHEMA]_MCP01
				SET MCAMTC = CASE 
					WHEN to_number(substr(MCDTEC,1,6),'FM999999') < CCAMTC THEN CCAMTC
					ELSE to_number(substr(MCDTEC,1,6),'FM999999') END
				FROM w_CCP01_next
				WHERE MCPOIC <> '1' AND
					MCAMTC <> CASE 
					WHEN to_number(substr(MCDTEC,1,6),'FM999999') < CCAMTC THEN CCAMTC
					ELSE to_number(substr(MCDTEC,1,6),'FM999999') END AND
					MCTYMV = 0 AND MCCANN <> 'A';
					
				-- Forcage mois sur situation
				UPDATE prod_shs.[COMPTASCHEMA]I_MCP01I
				SET MCAMTC = to_number(substr(MCDTEC,1,6),'FM999999')
				WHERE MCPOIC <> '1' AND
					MCAMTC <> to_number(substr(MCDTEC,1,6),'FM999999') AND
					MCTYMV = 0 AND MCCANN <> 'A';
				
				-- Ecritures exercice précédent
				DROP TABLE IF EXISTS w_CCP01F_next;
				CREATE TEMP TABLE w_CCP01F_next AS
				SELECT MAX(MCAMTC) AS CCAMTC
				FROM prod_shs.[COMPTASCHEMA]F_MCP01F
				WHERE MCTYMV = '0' AND MCCANN <> 'A'
				;
				
				-- Forcage mois sur non comptabilisé	
				UPDATE prod_shs.[COMPTASCHEMA]F_MCP01F
				SET MCAMTC = CCAMTC
				FROM w_CCP01F_next
				WHERE MCPOIC <> '1' AND
					MCAMTC <> CCAMTC AND
					MCTYMV = 0 AND MCCANN <> 'A' AND (MCPOIC = '0' OR MCPOI5 = '1');
				
				
				
				-- Mois modifiés sur compte extra
				
				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(CASE WHEN fin_exercice <> '1' THEN montant_debit ELSE 0.00 END) as debit, 
					SUM(CASE WHEN fin_exercice <> '1' THEN montant_credit ELSE 0.00 END) as credit, 
					SUM(CASE WHEN fin_exercice = '1' THEN montant_debit ELSE 0.00 END) as debit_fin_exercice, 
					SUM(CASE WHEN fin_exercice = '1' THEN montant_credit ELSE 0.00 END) as credit_fin_exercice, 
					0.00 AS debit_situation, 
					0.00 AS credit_situation,
					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 ecriture_consolidee = '0' AND ajustement = '0' AND
					is_budget IS DISTINCT FROM '1'
				GROUP BY 1,2
				ORDER BY 1,2;

				

				DROP TABLE IF EXISTS w_ECRITURES;
				CREATE TEMP TABLE w_ECRITURES AS
				SELECT MCAMTC, 
						MCPOIC,
						MCCPTE,
						'0'::text AS exercice_precedent,
						'0'::text AS situation,
						Count(*) AS COUNT, 
						round(SUM(CASE WHEN MCPOI5 <> '1' AND MCSENS = 'D' THEN MCMONT ELSE 0.00 END),2) AS DEBIT, 
						round(SUM(CASE WHEN MCPOI5 <> '1' AND MCSENS = 'C' THEN MCMONT ELSE 0.00 END),2) AS CREDIT, 
						0.00 AS DEBIT_fin_exercice, 
						0.00 AS CREDIT_fin_exercice, 
						0.00 AS DEBIT_situation, 
						0.00 AS CREDIT_situation
				FROM prod_shs.[COMPTASCHEMA]_MCP01
				WHERE MCTYMV = '0' AND MCCANN <> 'A' 
				GROUP BY 1,2,3
				ORDER BY 1,2;

				-- Ecritures exercice précédent
				INSERT INTO w_ECRITURES
				SELECT MCAMTC, 
						MCPOIC,
						MCCPTE,
						'1'::text AS exercice_precedent,
						'0'::text AS situation,
						Count(*) AS COUNT, 
						round(SUM(CASE WHEN MCPOI5 <> '1' AND MCPOIC = '1' AND MCSENS = 'D' THEN MCMONT ELSE 0.00 END),2) AS DEBIT, 
						round(SUM(CASE WHEN MCPOI5 <> '1' AND MCPOIC = '1' AND MCSENS = 'C' THEN MCMONT ELSE 0.00 END),2) AS CREDIT, 
						round(SUM(CASE WHEN (MCPOI5 = '1' OR MCPOIC = '0') AND MCSENS = 'D' THEN MCMONT ELSE 0.00 END),2) AS DEBIT_fin_exercice, 
						round(SUM(CASE WHEN (MCPOI5 = '1' OR MCPOIC = '0') AND MCSENS = 'C' THEN MCMONT ELSE 0.00 END),2) AS CREDIT_fin_exercice, 
						0.00 AS DEBIT_situation, 
						0.00 AS CREDIT_situation
				FROM prod_shs.[COMPTASCHEMA]F_MCP01F
				WHERE MCTYMV = '0' AND MCCANN <> 'A' AND (MCPOIC = '1' OR MCPOIC = '0' OR MCPOI5 = '1')
				GROUP BY 1,2,3
				ORDER BY 1,2;
				
				-- Mois modifiés (date echeancec fournisseur)
				
				DROP TABLE IF EXISTS w_exercice_courant;
				CREATE TEMP TABLE w_exercice_courant AS
				SELECT substr(MAX(MCAMTC),1,4)::numeric AS exercice_comptable_courant
				FROM prod_shs.[COMPTASCHEMA]_MCP01
				;

				
				DROP TABLE IF EXISTS w_echeances_fournisseurs_cti;
				CREATE TEMP TABLE w_echeances_fournisseurs_cti AS	
				SELECT mois_comptable AS mois_comptable_cti,
					to_char(date_echeance,'YYYYMMDD') AS date_cti, 
					count(*) AS nb_cti
				FROM compta.p_historique_ecritures
				JOIN w_exercice_courant ON exercice_comptable >= exercice_comptable_courant
				WHERE date_echeance IS NOT NULL AND date_echeance <> '20991231'
				GROUP BY 1,2
				ORDER BY 1;

				DROP TABLE IF EXISTS w_echeances_fournisseurs_shs;
				CREATE TEMP TABLE w_echeances_fournisseurs_shs AS
				SELECT MCAMTC AS mois_comptable_shs,
					MCDTEH AS date_shs, 
					count(*) AS nb_shs
				FROM prod_shs.[COMPTASCHEMA]_MCP01
				WHERE MCDTEH <> 0 AND
					MCTYMV = '0' AND MCCANN <> 'A'
				GROUP BY 1,2
				ORDER BY 1;

				DROP TABLE IF EXISTS w_mois_modifies_date_echeance;
				CREATE TEMP TABLE w_mois_modifies_date_echeance AS
				SELECT mois_comptable_modifie_echeance
				FROM
				(
				SELECT mois_comptable_cti AS mois_comptable_modifie_echeance
				FROM w_echeances_fournisseurs_cti
				LEFT JOIN w_echeances_fournisseurs_shs ON date_shs = date_cti AND mois_comptable_shs = mois_comptable_cti
				WHERE nb_shs IS DISTINCT FROM nb_cti
				UNION
				SELECT mois_comptable_shs AS mois_comptable_modifie_echeance
				FROM w_echeances_fournisseurs_shs
				LEFT JOIN w_echeances_fournisseurs_cti ON date_shs = date_cti AND mois_comptable_shs = mois_comptable_cti
				WHERE nb_shs IS DISTINCT FROM nb_cti
				) subview
				GROUP BY 1
				;
				
				-- Mois modifiés
				DROP TABLE IF EXISTS w_mois_modifies;
				CREATE TEMP TABLE w_mois_modifies AS
				SELECT p_calendrier_mois.exercice_comptable,
					w_ECRITURES.exercice_precedent,
					w_ECRITURES.situation,
					w_ECRITURES.MCAMTC AS mois_comptable
				FROM w_ECRITURES
					LEFT JOIN w_historique_ecritures_total ON 
						w_historique_ecritures_total.mois_comptable = w_ECRITURES.MCAMTC AND
						w_historique_ecritures_total.numero = w_ECRITURES.MCCPTE
					LEFT JOIN w_mois_modifies_date_echeance ON w_ECRITURES.MCAMTC = mois_comptable_modifie_echeance
					JOIN base.p_calendrier_mois ON w_ECRITURES.MCAMTC = p_calendrier_mois.mois
				GROUP BY 1,2,3,4
				HAVING MAX(
					CASE WHEN 
						mois_comptable_modifie_echeance IS NOT NULL OR
						w_historique_ecritures_total.count IS DISTINCT FROM w_ECRITURES.count OR
						w_historique_ecritures_total.debit IS DISTINCT FROM w_ECRITURES.debit OR
						w_historique_ecritures_total.credit IS DISTINCT FROM w_ECRITURES.credit OR
						w_historique_ecritures_total.debit_fin_exercice IS DISTINCT FROM w_ECRITURES.debit_fin_exercice OR
						w_historique_ecritures_total.credit_fin_exercice IS DISTINCT FROM w_ECRITURES.credit_fin_exercice OR
						w_historique_ecritures_total.debit_situation IS DISTINCT FROM w_ECRITURES.debit_situation OR
						w_historique_ecritures_total.credit_situation IS DISTINCT FROM w_ECRITURES.credit_situation
					THEN 1 ELSE 0 END) = 1;

				ALTER TABLE w_mois_modifies ADD CONSTRAINT w_mois_modifies_pk PRIMARY KEY(mois_comptable, exercice_precedent, situation);
				
				
				-- Mois modifiés sur compte général
				
				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(CASE WHEN fin_exercice <> '1' THEN montant_debit ELSE 0.00 END) as debit, 
					SUM(CASE WHEN fin_exercice <> '1' THEN montant_credit ELSE 0.00 END) as credit, 
					SUM(CASE WHEN fin_exercice = '1' THEN montant_debit ELSE 0.00 END) as debit_fin_exercice, 
					SUM(CASE WHEN fin_exercice = '1' THEN montant_credit ELSE 0.00 END) as credit_fin_exercice, 
					0.00 AS debit_situation, 
					0.00 AS credit_situation,
					SUM(nombre_ecritures) as count
				FROM compta.p_historique_ecritures_total
					JOIN compta.t_comptes ON p_historique_ecritures_total.compte_id = t_comptes.oid
				WHERE mois_comptable >= 200801 AND ecriture_consolidee = '0' AND ajustement = '0' AND
					is_budget IS DISTINCT FROM '1'
				GROUP BY 1,2
				ORDER BY 1,2;

				

				DROP TABLE IF EXISTS w_ECRITURES;
				CREATE TEMP TABLE w_ECRITURES AS
				SELECT MCAMTC, 
						MCPOIC,
						COALESCE(CLCPTE,MCCPTE) AS MCCPTE,
						'0'::text AS exercice_precedent,
						'0'::text AS situation,
						Count(*) AS COUNT, 
						round(SUM(CASE WHEN MCPOI5 <> '1' AND MCSENS = 'D' THEN MCMONT ELSE 0.00 END),2) AS DEBIT, 
						round(SUM(CASE WHEN MCPOI5 <> '1' AND MCSENS = 'C' THEN MCMONT ELSE 0.00 END),2) AS CREDIT, 
						0.00 AS DEBIT_fin_exercice, 
						0.00 AS CREDIT_fin_exercice, 
						0.00 AS DEBIT_situation, 
						0.00 AS CREDIT_situation
				FROM prod_shs.[COMPTASCHEMA]_MCP01
				LEFT JOIN prod_shs.[COMPTASCHEMA]_CLP01 ON MCTYPE = CLTYPE AND MCSANA = CLCCOL
				WHERE MCTYMV = '0' AND MCCANN <> 'A' 
				GROUP BY 1,2,3
				ORDER BY 1,2;

				-- Ecritures exercice précédent
				INSERT INTO w_ECRITURES
				SELECT MCAMTC, 
						MCPOIC,
						COALESCE(CLCPTE,MCCPTE) AS MCCPTE,
						'1'::text AS exercice_precedent,
						'0'::text AS situation,
						Count(*) AS COUNT, 
						round(SUM(CASE WHEN MCPOI5 <> '1' AND MCPOIC = '1' AND MCSENS = 'D' THEN MCMONT ELSE 0.00 END),2) AS DEBIT, 
						round(SUM(CASE WHEN MCPOI5 <> '1' AND MCPOIC = '1' AND MCSENS = 'C' THEN MCMONT ELSE 0.00 END),2) AS CREDIT, 
						round(SUM(CASE WHEN (MCPOI5 = '1' OR MCPOIC = '0') AND MCSENS = 'D' THEN MCMONT ELSE 0.00 END),2) AS DEBIT_fin_exercice, 
						round(SUM(CASE WHEN (MCPOI5 = '1' OR MCPOIC = '0') AND MCSENS = 'C' THEN MCMONT ELSE 0.00 END),2) AS CREDIT_fin_exercice, 
						0.00 AS DEBIT_situation, 
						0.00 AS CREDIT_situation
				FROM prod_shs.[COMPTASCHEMA]F_MCP01F
				LEFT JOIN prod_shs.[COMPTASCHEMA]_CLP01 ON MCTYPE = CLTYPE AND MCSANA = CLCCOL
				WHERE MCTYMV = '0' AND MCCANN <> 'A' AND (MCPOIC = '1' OR MCPOIC = '0' OR MCPOI5 = '1')
				GROUP BY 1,2,3
				ORDER BY 1,2;
				
				-- Ecritures situation
				INSERT INTO w_ECRITURES
				SELECT MCAMTC, 
						MCPOIC,
						COALESCE(CLCPTE,MCCPTE) AS MCCPTE,
						'0'::text AS exercice_precedent,
						'1'::text AS situation,
						Count(*) AS COUNT, 
						0::numeric AS DEBIT, 
						0::numeric AS CREDIT, 
						0::numeric AS DEBIT_fin_exercice, 
						0::numeric AS CREDIT_fin_exercice, 
						round(SUM(CASE WHEN MCPOI5 <> '1' AND MCSENS = 'D' THEN MCMONT ELSE 0.00 END),2) AS DEBIT_situation, 
						round(SUM(CASE WHEN MCPOI5 <> '1' AND MCSENS = 'C' THEN MCMONT ELSE 0.00 END),2) AS CREDIT_situation
				FROM prod_shs.[COMPTASCHEMA]I_MCP01I
				LEFT JOIN prod_shs.[COMPTASCHEMA]_CLP01 ON MCTYPE = CLTYPE AND MCSANA = CLCCOL
				WHERE MCTYMV = '0' AND MCCANN <> 'A' AND MCPOIC = '0'
				GROUP BY 1,2,3
				ORDER BY 1,2;

				
				-- Mois modifiés
				INSERT INTO w_mois_modifies
				SELECT p_calendrier_mois.exercice_comptable,
					w_ECRITURES.exercice_precedent,
					w_ECRITURES.situation,
					w_ECRITURES.MCAMTC AS mois_comptable
				FROM w_ECRITURES
					LEFT JOIN w_historique_ecritures_total ON 
						w_historique_ecritures_total.mois_comptable = w_ECRITURES.MCAMTC AND
						w_historique_ecritures_total.numero = w_ECRITURES.MCCPTE
					JOIN base.p_calendrier_mois ON w_ECRITURES.MCAMTC = p_calendrier_mois.mois
					LEFT JOIN w_mois_modifies ON
						w_ECRITURES.exercice_precedent = w_mois_modifies.exercice_precedent AND
						w_ECRITURES.situation = w_mois_modifies.situation AND
						w_ECRITURES.MCAMTC = w_mois_modifies.mois_comptable
				WHERE w_mois_modifies.mois_comptable IS NULL
				GROUP BY 1,2,3,4
				HAVING MAX(CASE WHEN w_historique_ecritures_total.count IS DISTINCT FROM w_ECRITURES.count OR
						w_historique_ecritures_total.debit IS DISTINCT FROM w_ECRITURES.debit OR
						w_historique_ecritures_total.credit IS DISTINCT FROM w_ECRITURES.credit OR
						w_historique_ecritures_total.debit_fin_exercice IS DISTINCT FROM w_ECRITURES.debit_fin_exercice OR
						w_historique_ecritures_total.credit_fin_exercice IS DISTINCT FROM w_ECRITURES.credit_fin_exercice OR
						w_historique_ecritures_total.debit_situation IS DISTINCT FROM w_ECRITURES.debit_situation OR
						w_historique_ecritures_total.credit_situation IS DISTINCT FROM w_ECRITURES.credit_situation
					THEN 1 ELSE 0 END) = 1;

				
				
				CTISELECT_PROPERTY_READ 'MOISMODIFIES', COALESCE(base.cti_group_concat(mois_comptable),'0')
				FROM w_mois_modifies;
				
				CTISELECT_PROPERTY_READ 'MOISMODIFIESC', COALESCE(base.cti_group_concat(mois_comptable),'0')
				FROM w_mois_modifies
				WHERE exercice_precedent = '0';
				
				CTISELECT_PROPERTY_READ 'MOISMODIFIESP', COALESCE(base.cti_group_concat(mois_comptable),'0')
				FROM w_mois_modifies
				WHERE exercice_precedent = '1';
				
				CTISELECT_PROPERTY_READ 'MOISMODIFIESI', COALESCE(base.cti_group_concat(mois_comptable),'0')
				FROM w_mois_modifies
				WHERE situation = '1';
				
				echo Mois modifiés Exercice en cours  : [MOISMODIFIESC];
				echo Mois modifiés Exercice précédent : [MOISMODIFIESP];
				echo Mois modifiés Situation          : [MOISMODIFIESI];
										
			]]></sqlcmd>
		</NODE>

		
	</NODE>

	<NODE name="ECRITURES" label="RECUPERATION ECRITURES">
		<condition><![CDATA[
			"[MOISMODIFIESC]" != "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
					w_mois_modifies.exercice_precedent = '0' AND
					w_mois_modifies.situation = '0' 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[
			
				DROP TABLE IF EXISTS w_MCP01;
				CREATE TEMP TABLE w_MCP01 AS
				SELECT [COMPTASCHEMA]_MCP01.*, 
					w_mois_modifies.*,
					to_char(MCCJAL,'FM000') AS MCCJAL_A,
					to_char(MCJALP,'FM000') AS MCJALP_A,
					to_char(MCTYPE,'FM00') AS MCTYPE_A,
					to_char(COALESCE(CLTYPG, MCTYPE),'FM00') AS CLTYPG_A,
					to_char(MCTYCP,'FM00') AS MCTYCP_A,
					COALESCE(CLCPTE, MCCPTE) AS CLCPTE,
					to_char(MCSANA,'FM0000') AS MCSANA_A
				FROM prod_shs.[COMPTASCHEMA]_MCP01
					JOIN w_mois_modifies ON 	
						w_mois_modifies.mois_comptable = MCAMTC AND
						w_mois_modifies.situation = '0' AND
						w_mois_modifies.exercice_precedent = '0'
					LEFT JOIN prod_shs.[COMPTASCHEMA]_CLP01 ON MCTYPE = CLTYPE AND 
						MCSANA = CLCCOL AND
						MCTYPE NOT IN (95,96)
				;
					

				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,
					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,
					base.cti_to_date(to_char(MCDTEC,'FM00000000')) AS date_ecriture,
					w_MCP01.exercice_comptable,
					w_MCP01.mois_comptable,
					CASE WHEN MCCCLH = '2' THEN 'H' ELSE 'C' END AS clinique_honoraire,
					t_journaux.oid AS journal_id,
					COALESCE(t_sections_analytiques.oid, 0) AS section_analytique_id,
					t_comptes_extra.partenaire_id,
					t_comptes_extra.oid AS compte_extra_id,
					CASE WHEN MCPOIC = '1' THEN '1' ELSE '0' END::text AS comptabilisee,
					CASE WHEN MCPOI5 = '1' THEN '1' ELSE '0' END::text AS fin_exercice,
					'0'::text AS ajustement,
					'0'::text AS inter_site,
					to_char(exercice_comptable,'FM0000') || '-' || to_char(MCFOLI,'FM00000') || '-' || to_char(MCLIGN,'FM00') AS cle_originale,
					''::text AS identifiant_operation,
					COALESCE(t_comptes_contre.oid,0) AS compte_contrepartie_id,
					substr(MCINT3,1,100) AS texte,
					COALESCE(base.cti_to_date(to_char(MCDTFO,'FM00000000')),'20991231') AS date_facture_fournisseur,
					MCPIE1 AS piece,
					MCDOSA || CASE WHEN MCDCPA NOT IN ('', '00') THEN '.' || MCDCPA ELSE '' END AS dossier,
					COALESCE(base.cti_to_date(to_char(MCDTEH,'FM00000000')),'20991231') AS date_echeance,
					COALESCE(t_journaux_paiement.oid,0) AS journal_paiement_id,
					COALESCE(t_mode_paiement.oid,0) AS mode_paiement_id,
					MCNCHK AS numero_cheque,
					CASE WHEN MCSENS = 'C' THEN MCMONT ELSE 0 END AS montant_credit,
					CASE WHEN MCSENS = 'D' THEN MCMONT ELSE 0 END AS montant_debit
				FROM w_MCP01
					JOIN compta.t_journaux ON MCCJAL_A = t_journaux.code_original
					
					JOIN compta.t_types_compta t_types_compta_extra ON MCTYPE_A = t_types_compta_extra.code_original
					JOIN compta.t_comptes t_comptes_extra ON MCCPTE = t_comptes_extra.numero AND 
						t_types_compta_extra.oid = t_comptes_extra.type_compta_id

					JOIN compta.t_types_compta ON CLTYPG_A = t_types_compta.code_original
					JOIN compta.t_comptes ON CLCPTE = t_comptes.numero AND 
						t_types_compta.oid = t_comptes.type_compta_id
					
					LEFT JOIN compta.t_sites ON t_sites.code_original = MCCETS
					
					
					LEFT JOIN compta.t_journaux t_journaux_paiement ON MCJALP_A = t_journaux_paiement.code_original
					
					LEFT JOIN compta.t_types_compta t_types_compta_contre ON MCTYCP_A = t_types_compta_contre.code_original
					LEFT JOIN compta.t_comptes t_comptes_contre ON MCCPCT = t_comptes_contre.numero AND 
						t_types_compta_contre.oid = t_comptes_contre.type_compta_id
					LEFT JOIN compta.t_sections_analytiques ON MCSANA_A = t_sections_analytiques.code_original
					LEFT JOIN compta.t_mode_paiement ON MCMDPA = t_mode_paiement.code_original
				WHERE MCTYMV = 0 AND MCCANN <> 'A';
				
				-- Ecritures consolidées
				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,
					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,
					ecriture_consolidee,
					montant_credit,
					montant_debit)
				SELECT 
					COALESCE(t_sites.oid,0) AS site_id,
					t_comptes.type_compta_id,
					t_comptes.compte_general_id AS compte_id,
					base.cti_to_date(to_char(MCDTEC,'FM00000000')) AS date_ecriture,
					w_MCP01.exercice_comptable,
					w_MCP01.mois_comptable,
					CASE WHEN MCCCLH = '2' THEN 'H' ELSE 'C' END AS clinique_honoraire,
					t_journaux.oid AS journal_id,
					COALESCE(t_sections_analytiques.oid, 0) AS section_analytique_id,
					t_comptes.partenaire_id,
					t_comptes.oid AS compte_extra_id,
					CASE WHEN MCPOIC = '1' THEN '1' ELSE '0' END::text AS comptabilisee,
					CASE WHEN MCPOI5 = '1' THEN '1' ELSE '0' END::text AS fin_exercice,
					'0'::text AS ajustement,
					'0'::text AS inter_site,
					to_char(exercice_comptable,'FM0000') || '-' || to_char(MCFOLI,'FM00000') || '-' || to_char(MCLIGN,'FM00') AS cle_originale,
					''::text AS identifiant_operation,
					COALESCE(t_comptes_contre.oid,0) AS compte_contrepartie_id,
					substr(MCINT3,1,100) AS texte,
					COALESCE(base.cti_to_date(to_char(MCDTFO,'FM00000000')),'20991231') AS date_facture_fournisseur,
					MCPIE1 AS piece,
					MCDOSA || CASE WHEN MCDCPA NOT IN ('', '00') THEN '.' || MCDCPA ELSE '' END AS dossier,
					COALESCE(base.cti_to_date(to_char(MCDTEH,'FM00000000')),'20991231') AS date_echeance,
					COALESCE(t_journaux_paiement.oid,0) AS journal_paiement_id,
					COALESCE(t_mode_paiement.oid,0) AS mode_paiement_id,
					MCNCHK AS numero_cheque,
					'1' AS ecriture_consolidee,
					CASE WHEN MCSENS = 'C' THEN MCMONT ELSE 0 END AS montant_credit,
					CASE WHEN MCSENS = 'D' THEN MCMONT ELSE 0 END AS montant_debit
				FROM w_MCP01
					LEFT JOIN compta.t_sites ON t_sites.code_original = MCCETS
					JOIN compta.t_journaux ON MCCJAL_A = t_journaux.code_original
					LEFT JOIN compta.t_journaux t_journaux_paiement ON MCJALP_A = t_journaux_paiement.code_original
					JOIN compta.t_types_compta ON MCTYPE_A = t_types_compta.code_original
					JOIN compta.t_comptes ON MCCPTE = t_comptes.numero AND 
						t_types_compta.oid = t_comptes.type_compta_id
					LEFT JOIN compta.t_types_compta t_types_compta_contre ON MCTYCP_A = t_types_compta_contre.code_original
					LEFT JOIN compta.t_comptes t_comptes_contre ON MCCPCT = t_comptes_contre.numero AND 
						t_types_compta_contre.oid = t_comptes_contre.type_compta_id
					LEFT JOIN compta.t_sections_analytiques ON MCSANA_A = t_sections_analytiques.code_original
					LEFT JOIN compta.t_mode_paiement ON MCMDPA = t_mode_paiement.code_original
				WHERE MCTYMV = 1 AND MCCANN <> 'A' AND MCPOIC <> '1'  AND MCCJAL <> 0;
				
				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,
					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,
					ecriture_consolidee,
					montant_credit,
					montant_debit)
				SELECT 
					COALESCE(t_sites.oid,0) AS site_id,
					t_comptes.type_compta_id,
					t_comptes.compte_general_id AS compte_id,
					base.cti_to_date(to_char(MCDTEC,'FM00000000')) AS date_ecriture,
					w_MCP01.exercice_comptable,
					w_MCP01.mois_comptable,
					CASE WHEN MCCCLH = '2' THEN 'H' ELSE 'C' END AS clinique_honoraire,
					t_journaux.oid AS journal_id,
					COALESCE(t_sections_analytiques.oid, 0) AS section_analytique_id,
					t_comptes.partenaire_id,
					t_comptes.oid AS compte_extra_id,
					CASE WHEN MCPOIC = '1' THEN '1' ELSE '0' END::text AS comptabilisee,
					CASE WHEN MCPOI5 = '1' THEN '1' ELSE '0' END::text AS fin_exercice,
					'0'::text AS ajustement,
					'0'::text AS inter_site,
					to_char(exercice_comptable,'FM0000') || '-' || to_char(MCFOLI,'FM00000') || '-' || to_char(MCLIGN,'FM00') AS cle_originale,
					''::text AS identifiant_operation,
					COALESCE(t_comptes_contre.oid,0) AS compte_contrepartie_id,
					substr(MCINT3,1,100) AS texte,
					COALESCE(base.cti_to_date(to_char(MCDTFO,'FM00000000')),'20991231') AS date_facture_fournisseur,
					MCPIE1 AS piece,
					MCDOSA || CASE WHEN MCDCPA NOT IN ('', '00') THEN '.' || MCDCPA ELSE '' END AS dossier,
					COALESCE(base.cti_to_date(to_char(MCDTEH,'FM00000000')),'20991231') AS date_echeance,
					COALESCE(t_journaux_paiement.oid,0) AS journal_paiement_id,
					COALESCE(t_mode_paiement.oid,0) AS mode_paiement_id,
					MCNCHK AS numero_cheque,
					'2' AS ecriture_consolidee,
					SUM(CASE WHEN MCSENS = 'D' THEN MCMONT ELSE 0 END) AS montant_credit,
					SUM(CASE WHEN MCSENS = 'C' THEN MCMONT ELSE 0 END) AS montant_debit
				FROM w_MCP01
					LEFT JOIN compta.t_sites ON t_sites.code_original = MCCETS
					JOIN compta.t_journaux ON MCCJAL_A = t_journaux.code_original
					LEFT JOIN compta.t_journaux t_journaux_paiement ON MCJALP_A = t_journaux_paiement.code_original
					JOIN compta.t_types_compta ON MCTYPE_A = t_types_compta.code_original
					JOIN compta.t_comptes ON MCCPTE = t_comptes.numero AND 
						t_types_compta.oid = t_comptes.type_compta_id
					LEFT JOIN compta.t_types_compta t_types_compta_contre ON MCTYCP_A = t_types_compta_contre.code_original
					LEFT JOIN compta.t_comptes t_comptes_contre ON MCCPCT = t_comptes_contre.numero AND 
						t_types_compta_contre.oid = t_comptes_contre.type_compta_id
					LEFT JOIN compta.t_sections_analytiques ON MCSANA_A = t_sections_analytiques.code_original
					LEFT JOIN compta.t_mode_paiement ON MCMDPA = t_mode_paiement.code_original
				WHERE MCTYMV = 1 AND MCCANN <> 'A' AND MCPOIC <> '1'  AND MCCJAL <> 0
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27
				HAVING
					(
						SUM(CASE WHEN MCSENS = 'D' THEN MCMONT ELSE 0 END) <> 0 OR
						SUM(CASE WHEN MCSENS = 'C' THEN MCMONT ELSE 0 END) <> 0 
					);
				
				
				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="ECRITURES" label="RECUPERATION ECRITURES (exercice précédent)">
		<condition><![CDATA[
			"[MOISMODIFIESP]" != "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
					w_mois_modifies.exercice_precedent = '1' AND
					w_mois_modifies.situation = '0' 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[
			
				DROP TABLE IF EXISTS w_MCP01;
				CREATE TEMP TABLE w_MCP01 AS
				SELECT [COMPTASCHEMA]F_MCP01F.*, 
					w_mois_modifies.*,
					to_char(MCCJAL,'FM000') AS MCCJAL_A,
					to_char(MCJALP,'FM000') AS MCJALP_A,
					to_char(MCTYPE,'FM00') AS MCTYPE_A,
					to_char(COALESCE(CLTYPG, MCTYPE),'FM00') AS CLTYPG_A,
					to_char(MCTYCP,'FM00') AS MCTYCP_A,
					COALESCE(CLCPTE, MCCPTE) AS CLCPTE,
					to_char(MCSANA,'FM0000') AS MCSANA_A
				FROM prod_shs.[COMPTASCHEMA]F_MCP01F
					JOIN w_mois_modifies ON 	
						w_mois_modifies.mois_comptable = MCAMTC AND
						w_mois_modifies.situation = '0' AND
						w_mois_modifies.exercice_precedent = '1'
					LEFT JOIN prod_shs.[COMPTASCHEMA]_CLP01 ON MCTYPE = CLTYPE AND 
						MCSANA = CLCCOL AND
						MCTYPE NOT IN (95,96)
				;

				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,
					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,
					base.cti_to_date(to_char(MCDTEC,'FM00000000')) AS date_ecriture,
					w_MCP01.exercice_comptable,
					w_MCP01.mois_comptable,
					CASE WHEN MCCCLH = '2' THEN 'H' ELSE 'C' END AS clinique_honoraire,
					t_journaux.oid AS journal_id,
					COALESCE(t_sections_analytiques.oid, 0) AS section_analytique_id,
					t_comptes_extra.partenaire_id,
					t_comptes_extra.oid AS compte_extra_id,
					CASE WHEN MCPOIC = '1' THEN '1' ELSE '0' END::text AS comptabilisee,
					CASE WHEN MCPOI5 = '1' THEN '1' ELSE '0' END::text AS fin_exercice,
					'0'::text AS ajustement,
					'0'::text AS inter_site,
					to_char(exercice_comptable,'FM0000') || '-' || to_char(MCFOLI,'FM00000') || '-' || to_char(MCLIGN,'FM00') AS cle_originale,
					''::text AS identifiant_operation,
					COALESCE(t_comptes_contre.oid,0) AS compte_contrepartie_id,
					substr(MCINT3,1,100) AS texte,
					COALESCE(base.cti_to_date(to_char(MCDTFO,'FM00000000')),'20991231') AS date_facture_fournisseur,
					MCPIE1 AS piece,
					MCDOSA || CASE WHEN MCDCPA NOT IN ('', '00') THEN '.' || MCDCPA ELSE '' END AS dossier,
					COALESCE(base.cti_to_date(to_char(MCDTEH,'FM00000000')),'20991231') AS date_echeance,
					COALESCE(t_journaux_paiement.oid,0) AS journal_paiement_id,
					COALESCE(t_mode_paiement.oid,0) AS mode_paiement_id,
					MCNCHK AS numero_cheque,
					CASE WHEN MCSENS = 'C' THEN MCMONT ELSE 0 END AS montant_credit,
					CASE WHEN MCSENS = 'D' THEN MCMONT ELSE 0 END AS montant_debit
				FROM w_MCP01
					JOIN compta.t_journaux ON MCCJAL_A = t_journaux.code_original
					
					JOIN compta.t_types_compta t_types_compta_extra ON MCTYPE_A = t_types_compta_extra.code_original
					JOIN compta.t_comptes t_comptes_extra ON MCCPTE = t_comptes_extra.numero AND 
						t_types_compta_extra.oid = t_comptes_extra.type_compta_id

					JOIN compta.t_types_compta ON CLTYPG_A = t_types_compta.code_original
					JOIN compta.t_comptes ON CLCPTE = t_comptes.numero AND 
						t_types_compta.oid = t_comptes.type_compta_id
					
					LEFT JOIN compta.t_sites ON t_sites.code_original = MCCETS
					
					
					LEFT JOIN compta.t_journaux t_journaux_paiement ON MCJALP_A = t_journaux_paiement.code_original
					
					LEFT JOIN compta.t_types_compta t_types_compta_contre ON MCTYCP_A = t_types_compta_contre.code_original
					LEFT JOIN compta.t_comptes t_comptes_contre ON MCCPCT = t_comptes_contre.numero AND 
						t_types_compta_contre.oid = t_comptes_contre.type_compta_id
					LEFT JOIN compta.t_sections_analytiques ON MCSANA_A = t_sections_analytiques.code_original
					LEFT JOIN compta.t_mode_paiement ON MCMDPA = t_mode_paiement.code_original
				WHERE MCTYMV = 0 AND MCCANN <> 'A';
				
				
				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="ECRITURES" label="RECUPERATION ECRITURES (Situation intérmédiaire)">
		<condition><![CDATA[
			"[MOISMODIFIESI]" != "0"
		]]></condition>

				
		<NODE label="Génération détail">
			<sqlcmd><![CDATA[
			
				DROP TABLE IF EXISTS w_MCP01;
				CREATE TEMP TABLE w_MCP01 AS
				SELECT [COMPTASCHEMA]I_MCP01I.*, 
					w_mois_modifies.*,
					to_char(MCCJAL,'FM000') AS MCCJAL_A,
					to_char(MCJALP,'FM000') AS MCJALP_A,
					to_char(MCTYPE,'FM00') AS MCTYPE_A,
					to_char(COALESCE(CLTYPG, MCTYPE),'FM00') AS CLTYPG_A,
					to_char(MCTYCP,'FM00') AS MCTYCP_A,
					COALESCE(CLCPTE, MCCPTE) AS CLCPTE,
					to_char(MCSANA,'FM0000') AS MCSANA_A
				FROM prod_shs.[COMPTASCHEMA]I_MCP01I
					JOIN w_mois_modifies ON 	
						w_mois_modifies.mois_comptable = MCAMTC AND
						w_mois_modifies.situation = '1'
					LEFT JOIN prod_shs.[COMPTASCHEMA]_CLP01 ON 
						MCTYPE = CLTYPE AND 
						MCSANA = CLCCOL AND
						MCTYPE NOT IN (95,96)
				WHERE MCTYMV = '0' AND MCCANN <> 'A' AND MCPOIC = '0'
				;

				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,
					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,
					base.cti_to_date(to_char(MCDTEC,'FM00000000')) AS date_ecriture,
					w_MCP01.exercice_comptable,
					w_MCP01.mois_comptable,
					CASE WHEN MCCCLH = '2' THEN 'H' ELSE 'C' END AS clinique_honoraire,
					t_journaux.oid AS journal_id,
					COALESCE(t_sections_analytiques.oid, 0) AS section_analytique_id,
					t_comptes_extra.partenaire_id,
					t_comptes_extra.oid AS compte_extra_id,
					CASE WHEN MCPOIC = '1' THEN '1' ELSE '0' END::text AS comptabilisee,
					'0'::text AS fin_exercice,
					'2'::text AS ajustement,
					'0'::text AS inter_site,
					to_char(exercice_comptable,'FM0000') || '-' || to_char(MCFOLI,'FM00000') || '-' || to_char(MCLIGN,'FM00') AS cle_originale,
					''::text AS identifiant_operation,
					COALESCE(t_comptes_contre.oid,0) AS compte_contrepartie_id,
					substr(MCINT3,1,100) AS texte,
					COALESCE(base.cti_to_date(to_char(MCDTFO,'FM00000000')),'20991231') AS date_facture_fournisseur,
					MCPIE1 AS piece,
					MCDOSA || CASE WHEN MCDCPA NOT IN ('', '00') THEN '.' || MCDCPA ELSE '' END AS dossier,
					COALESCE(base.cti_to_date(to_char(MCDTEH,'FM00000000')),'20991231') AS date_echeance,
					COALESCE(t_journaux_paiement.oid,0) AS journal_paiement_id,
					COALESCE(t_mode_paiement.oid,0) AS mode_paiement_id,
					MCNCHK AS numero_cheque,
					CASE WHEN MCSENS = 'C' THEN MCMONT ELSE 0 END AS montant_credit,
					CASE WHEN MCSENS = 'D' THEN MCMONT ELSE 0 END AS montant_debit
				FROM w_MCP01
					JOIN compta.t_journaux ON MCCJAL_A = t_journaux.code_original
					
					JOIN compta.t_types_compta t_types_compta_extra ON MCTYPE_A = t_types_compta_extra.code_original
					JOIN compta.t_comptes t_comptes_extra ON MCCPTE = t_comptes_extra.numero AND 
						t_types_compta_extra.oid = t_comptes_extra.type_compta_id

					JOIN compta.t_types_compta ON CLTYPG_A = t_types_compta.code_original
					JOIN compta.t_comptes ON CLCPTE = t_comptes.numero AND 
						t_types_compta.oid = t_comptes.type_compta_id
					
					LEFT JOIN compta.t_sites ON t_sites.code_original = MCCETS
					
					
					LEFT JOIN compta.t_journaux t_journaux_paiement ON MCJALP_A = t_journaux_paiement.code_original
					
					LEFT JOIN compta.t_types_compta t_types_compta_contre ON MCTYCP_A = t_types_compta_contre.code_original
					LEFT JOIN compta.t_comptes t_comptes_contre ON MCCPCT = t_comptes_contre.numero AND 
						t_types_compta_contre.oid = t_comptes_contre.type_compta_id
					LEFT JOIN compta.t_sections_analytiques ON MCSANA_A = t_sections_analytiques.code_original
					LEFT JOIN compta.t_mode_paiement ON MCMDPA = t_mode_paiement.code_original
				WHERE MCTYMV = 0 AND MCCANN <> 'A';

				
			]]></sqlcmd>
		</NODE>
		
	</NODE>
	
	<NODE name="POST" label="TRAITEMENTS COMPLEMENTAIRES COMMUNS">
		
		<condition><![CDATA[
			"[MOISMODIFIES]" != "0"
		]]></condition>
		
		
		<NODE label="Compléments écritures">
		
			<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 = '001'
					WHERE exercice_comptable >= 2015 AND t_comptes_c_extra.type_compta_code='40'
				;

				
				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='40'
				;


				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,
							piece
						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.piece = subview.piece AND
						facture_soldee = '0'
					;

					UPDATE compta.p_ecriture_fournisseur
					SET facture_soldee = '1'
					FROM
						(
						SELECT 
							compte_extra_id,
							piece,
							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.piece = subview.piece 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 
						piece <> '';

					UPDATE compta.p_ecriture_fournisseur 
					SET ecriture_exclue = '1'
					WHERE facture_soldee = '0' AND 
						ecriture_exclue = '0' AND
						(date_echeance = '20991231' OR date_facture_fournisseur = '20991231');
						
			]]></sqlcmd>
		
		</NODE>
		
		
		
		
		
	</NODE>	

	<NODE name="CHIFFRIER" label="CREATION DU CHIFFRIER">
		
		<condition><![CDATA[
			"[MOISMODIFIES]" != "0"
		]]></condition>
		
		<NODE label="Récupération chiffrier">
			<sqlcmd><![CDATA[
			
				-- Dernier mois exercice pour 13eme compta
				DROP TABLE IF EXISTS w_exercice;
				CREATE TEMP TABLE w_exercice AS
				SELECT exercice_comptable,
					MAX(mois) AS dernier_mois
				FROM base.p_calendrier_mois
				GROUP BY 1;
			
				-- Chiffrier
				TRUNCATE compta.p_chiffrier_comptable;

				INSERT INTO compta.p_chiffrier_comptable
				(
					mois_comptable,
					site_id,
					clinique_honoraire,
					montant_debit,
					montant_credit
					)
				SELECT CASE WHEN CCMOIS <> 13 THEN CCANEE*100+CCMOIS ELSE dernier_mois END AS mois_comptable, 
					0::bigint AS site_id,
					'C'::text AS clinique_honoraire, 
					SUM(CCMLCL) AS montant_debit, 
					SUM(CCMLCL) AS montant_credit
				FROM prod_shs.[COMPTASCHEMA]_CCP01 
				JOIN w_exercice ON exercice_comptable = CCANEE
				WHERE CCCMON = 'E' AND 
					CCMOIS <> 99 AND 
					CCNOR3 IN (320) AND
					CCMLCL <> 0
				GROUP BY 1,2,3
				ORDER BY 1;

				INSERT INTO compta.p_chiffrier_comptable
				(
					mois_comptable,
					site_id,
					clinique_honoraire,
					montant_debit,
					montant_credit
					)
				SELECT CASE WHEN CCMOIS <> 13 THEN CCANEE*100+CCMOIS ELSE dernier_mois END AS mois_comptable, 
					0::bigint AS site_id,
					'H'::text AS clinique_honoraire, 
					SUM(CCMLHO) AS montant_debit, 
					SUM(CCMLHO) AS montant_credit
				FROM prod_shs.[COMPTASCHEMA]_CCP01 
				JOIN w_exercice ON exercice_comptable = CCANEE
				WHERE CCCMON = 'E' AND 
					CCMOIS <> 99 AND 
					CCNOR3 IN (320) AND
					CCMLHO <> 0
				GROUP BY 1,2,3
				ORDER BY 1;
				
				UPDATE compta.p_chiffrier_comptable
				SET site_id = (SELECT MAX(site_id) FROM compta.p_historique_ecritures)
				WHERE site_id = 0;
										
			]]></sqlcmd>
		</NODE>
		
		
	</NODE>
	
	<NODE name="PARAM" label="RECUPERATION DES PARAMETRES">

		<NODE label="Paramètres">
			<sqlcmd><![CDATA[
			

				-- Periode comptable
				UPDATE base.p_calendrier_mois SET
					exercice_comptable = subview.exercice_comptable
				FROM (
					SELECT
						p_calendrier_mois.mois, 
						p_calendrier_mois.date_debut,
						date_part('year',p_calendrier_mois.date_debut) + CASE 
							WHEN to_number(substr(p_calendrier_mois.mois,5,2),'FM00') >= subview.exercice_mois_comptable_1 AND subview.exercice_mois_comptable_1 > 1 THEN 1
							ELSE 0 END AS exercice_comptable
					FROM base.p_calendrier_mois
						JOIN (
							SELECT 
								MCAMTC_deb,
								MCAMTC_fin,
								to_number(substr(MCAMTC_fin,1,4),'FM0000') AS exercice_comptable,
								to_number(substr(MCAMTC_deb,5,2),'FM00') AS exercice_mois_comptable_1
							FROM
								(
								SELECT 
									MIN(MCAMTC) AS MCAMTC_deb,
									MIN(MCAMTC) + 99 AS MCAMTC_fin
								FROM prod_shs.[COMPTASCHEMA]_MCP01
								WHERE MCPOIC = '1' AND MCTYMV = 0 AND MCCANN <> 'A'
								) subview
						) subview ON 1 = 1
					) 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_comptable = subview.exercice_comptable
				FROM (
					SELECT
						p_calendrier_mois.mois, 
						p_calendrier_mois.date_debut,
						date_part('year',p_calendrier_mois.date_debut) + CASE 
							WHEN to_number(substr(p_calendrier_mois.mois,5,2),'FM00') >= subview.exercice_mois_comptable_1 AND subview.exercice_mois_comptable_1 > 1 THEN 1
							ELSE 0 END AS exercice_comptable
					FROM base.p_calendrier_mois
						JOIN (
							SELECT 
								MCAMTC_deb,
								MCAMTC_fin,
								to_number(substr(MCAMTC_fin,1,4),'FM0000') AS exercice_comptable,
								to_number(substr(MCAMTC_deb,5,2),'FM00') AS exercice_mois_comptable_1
							FROM
								(
								SELECT 
									MIN(MCAMTC) AS MCAMTC_deb,
									MIN(MCAMTC) + 99 AS MCAMTC_fin
								FROM prod_shs.[COMPTASCHEMA]F_MCP01F
								WHERE MCPOIC = '1' AND MCTYMV = 0 AND MCCANN <> 'A'
								) subview
						) subview ON 1 = 1
					) 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;

				-- Site = Etablissement
				INSERT INTO compta.t_sites(oid, code, texte, clinique_honoraire, code_original)  
				SELECT 0, chr(127) || '***', 'Non renseigné', 'C', '' WHERE 0 NOT IN (SELECT oid FROM compta.t_sites);
				;
				
				INSERT INTO compta.t_sites(code, code_original, texte)
				SELECT ETCETS, ETCETS, ETRSOC
				FROM prod_shs.[COMPTASCHEMA]_ETP01
				WHERE ETCETS NOT IN (SELECT code_original FROM compta.t_sites WHERE code_original IS NOT NULL) AND
					ETCETS IN (SELECT MCCETS FROM prod_shs.[COMPTASCHEMA]_MCP01)
				ORDER BY 1;
										
				-- Types de compte
				INSERT INTO compta.t_types_compta(code, code_original, texte, specialite)
				SELECT 
					CASE TYTYPE
						WHEN 95 THEN 'BILAN'
						WHEN 96 THEN 'GESTION'
						WHEN 97 THEN 'RESULTAT'
						ELSE to_char(TYTYPE,'FM00') END,
					to_char(TYTYPE,'FM00'),
					TYINT4,
					CASE TYTYPE
						WHEN 40 THEN 'FOU'
						WHEN 42 THEN 'CLI'
						WHEN 43 THEN 'CLI'
						WHEN 46 THEN 'HON'
						WHEN 47 THEN 'HON'
						WHEN 48 THEN 'HON'
						WHEN 91 THEN 'CLI'
						WHEN 92 THEN 'CLI'
						WHEN 95 THEN 'BIL'
						WHEN 96 THEN 'GES'
						WHEN 97 THEN 'RES'
						ELSE '' END
				FROM prod_shs.[COMPTASCHEMA]_TYP01
				WHERE to_char(TYTYPE,'FM00') NOT IN (SELECT code_original FROM compta.t_types_compta WHERE code_original IS NOT NULL)
				;
				
				-- Inexistants dans table des types
				INSERT INTO compta.t_types_compta(code, code_original, texte, specialite)
				SELECT 
					CASE PETYPE
						WHEN 95 THEN 'BILAN'
						WHEN 96 THEN 'GESTION'
						WHEN 97 THEN 'RESULTAT'
						ELSE to_char(PETYPE,'FM00') END,
					to_char(PETYPE,'FM00'),
					'Type ' || to_char(PETYPE,'FM00'),
					CASE PETYPE
						WHEN 40 THEN 'FOU'
						WHEN 42 THEN 'CLI'
						WHEN 43 THEN 'CLI'
						WHEN 46 THEN 'HON'
						WHEN 47 THEN 'HON'
						WHEN 48 THEN 'HON'
						WHEN 91 THEN 'CLI'
						WHEN 92 THEN 'CLI'
						WHEN 95 THEN 'BIL'
						WHEN 96 THEN 'GES'
						WHEN 97 THEN 'RES'
						ELSE '' END
				FROM prod_shs.[COMPTASCHEMA]_PEP01
				WHERE to_char(PETYPE,'FM00') NOT IN (SELECT code_original FROM compta.t_types_compta WHERE code_original IS NOT NULL)
				GROUP BY PETYPE
				;
				
				-- Inexistants dans table des types (13eme compta)
				INSERT INTO compta.t_types_compta(code, code_original, texte, specialite)
				SELECT 
					CASE PETYPE
						WHEN 95 THEN 'BILAN'
						WHEN 96 THEN 'GESTION'
						WHEN 97 THEN 'RESULTAT'
						ELSE to_char(PETYPE,'FM00') END,
					to_char(PETYPE,'FM00'),
					'Type ' || to_char(PETYPE,'FM00'),
					CASE PETYPE
						WHEN 40 THEN 'FOU'
						WHEN 42 THEN 'CLI'
						WHEN 43 THEN 'CLI'
						WHEN 46 THEN 'HON'
						WHEN 47 THEN 'HON'
						WHEN 48 THEN 'HON'
						WHEN 91 THEN 'CLI'
						WHEN 92 THEN 'CLI'
						WHEN 95 THEN 'BIL'
						WHEN 96 THEN 'GES'
						WHEN 97 THEN 'RES'
						ELSE '' END
				FROM prod_shs.[COMPTASCHEMA]F_PEP01F
				WHERE to_char(PETYPE,'FM00') NOT IN (SELECT code_original FROM compta.t_types_compta WHERE code_original IS NOT NULL)
				GROUP BY PETYPE
				;
				
				-- 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, code_original, texte)
				SELECT to_char(ANSANA,'FM0000'), to_char(ANSANA,'FM0000'), ANINT3
				FROM prod_shs.[COMPTASCHEMA]_ANP01
				WHERE to_char(ANSANA,'FM0000') NOT IN (SELECT code_original FROM compta.t_sections_analytiques WHERE code_original IS NOT NULL)
				ORDER BY 1;

				-- Journaux		
				INSERT INTO compta.t_journaux(code, code_original, texte, a_nouveaux, clinique_honoraire)
				SELECT to_char(JLCJAL,'FM000'), to_char(JLCJAL,'FM000'), JLINT7, CASE WHEN JLCJAL < 10 THEN '1' ELSE '0' END, CASE WHEN JLQSEP = '2' THEN 'H' ELSE 'C' END
				FROM prod_shs.[COMPTASCHEMA]_JLP01
				WHERE to_char(JLCJAL,'FM000') NOT IN (SELECT code_original FROM compta.t_journaux WHERE code_original IS NOT NULL)
				ORDER BY 1;
				
				-- Mode de paiement	
				INSERT INTO compta.t_mode_paiement(oid, code, code_original, texte)
				SELECT 0, '**', '**', 'Non renseigné'
				WHERE 0 NOT IN (SELECT oid FROM compta.t_mode_paiement);

				INSERT INTO compta.t_mode_paiement(code, code_original, texte)
				SELECT MPMDPA, MPMDPA, MAX(MPINTS)
				FROM prod_shs.[COMPTASCHEMA]_MPP01
				WHERE MPMDPA <> '' AND
					MPMDPA NOT IN (SELECT code_original FROM compta.t_mode_paiement WHERE code_original IS NOT NULL)
                GROUP BY 1,2
				ORDER BY 1;
				
				-- Classes de comptes
				INSERT INTO compta.t_classes_comptes(code, texte)
				SELECT PGCLAS, PGINT3
				FROM prod_shs.[COMPTASCHEMA]_PGP01
				WHERE PGCLAS NOT IN (SELECT code FROM compta.t_classes_comptes WHERE code IS NOT NULL)
				ORDER BY 1;
				
				UPDATE compta.t_classes_comptes
				SET texte = PGINT3
				FROM prod_shs.[COMPTASCHEMA]_PGP01
				WHERE PGCLAS = code AND
					texte IS DISTINCT FROM PGINT3;
				
				-- Comptes
				DROP TABLE IF EXISTS w_PEP01;
				CREATE TEMP TABLE w_PEP01 AS
				SELECT PETYPE,
					0::bigint AS type_compta_id,
					to_char(PECPTE,'FM00000000') AS PECPTE,
					PEINT3,
					PECCLH,
					CASE WHEN PETYPE IN (95,96,97) THEN to_char(PESANA,'FM0000') ELSE '0000' END AS PESANA,
					CASE WHEN PETYPE NOT IN (95,96,97) THEN COALESCE(CLTYPG,95) ELSE PETYPE END AS PETYPE_GENE,
					0::bigint AS type_compta_gene_id,
					CASE WHEN PETYPE NOT IN (95,96,97) THEN to_char(COALESCE(CLCPTE,PECPTE),'FM00000000') ELSE to_char(PECPTE,'FM00000000') END AS PECPTE_GENE
				FROM prod_shs.[COMPTASCHEMA]_PEP01
				LEFT JOIN prod_shs.[COMPTASCHEMA]_CLP01 ON
					PETYPE = CLTYPE AND
					PESANA = CLCCOL;

				INSERT INTO w_PEP01
				SELECT PETYPE,
					0::bigint AS type_compta_id,
					to_char(PECPTE,'FM00000000') AS PECPTE,
					PEINT3,
					PECCLH,
					CASE WHEN PETYPE IN (95,96,97) THEN to_char(PESANA,'FM0000') ELSE '0000' END AS PESANA,
					CASE WHEN PETYPE NOT IN (95,96,97) THEN COALESCE(CLTYPG,95) ELSE PETYPE END AS PETYPE_GENE,
					0::bigint AS type_compta_gene_id,
					CASE WHEN PETYPE NOT IN (95,96,97) THEN to_char(COALESCE(CLCPTE,PECPTE),'FM00000000') ELSE to_char(PECPTE,'FM00000000') END AS PECPTE_GENE
				FROM prod_shs.[COMPTASCHEMA]F_PEP01F
				LEFT JOIN prod_shs.[COMPTASCHEMA]_CLP01 ON
					PETYPE = CLTYPE AND
					PESANA = CLCCOL
				WHERE PETYPE||to_char(PECPTE,'FM00000000') NOT IN
					(SELECT PETYPE||PECPTE FROM w_PEP01);

				-- Comptes généraux non saisis (venant de CLP01)
				INSERT INTO w_PEP01
				SELECT PETYPE_GENE,
					0::bigint AS type_compta_gene_id,
					PECPTE_GENE,
					PEINT3,
					PECCLH,
					PESANA,
					PETYPE_GENE,
					0::bigint AS type_compta_gene_id,
					PECPTE_GENE
				FROM w_PEP01
				WHERE PETYPE_GENE||PECPTE_GENE NOT IN
					(SELECT PETYPE||PECPTE FROM w_PEP01);
					
					
				-- Adaptation texte avec *
				UPDATE w_PEP01
				SET PEINT3 = substr(PEINT3, 1, 23)  
				WHERE substr(PEINT3, 24, 7) = '*';

				UPDATE w_PEP01
				SET PEINT3 = substr(PEINT3, 1, 22)  
				WHERE substr(PEINT3, 23, 8) = '*';
				
				-- Affectation types de compta
				UPDATE w_PEP01
				SET type_compta_id = t_types_compta.oid
				FROM compta.t_types_compta
				WHERE t_types_compta.code_original = to_char(PETYPE,'FM00');

				UPDATE w_PEP01
				SET type_compta_gene_id = t_types_compta.oid
				FROM compta.t_types_compta
				WHERE t_types_compta.code_original = to_char(PETYPE_GENE,'FM00');


				UPDATE compta.t_comptes SET
					texte = PEINT3
				FROM w_PEP01
				WHERE t_comptes.type_compta_id = w_PEP01.type_compta_id AND
					t_comptes.numero = w_PEP01.PECPTE AND
					texte IS DISTINCT FROM PEINT3;

				
				-- Ajout des nouveaux comptes
				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 
						w_PEP01.type_compta_id,			
						PECPTE AS numero, 
						PEINT3 AS texte, 
						0 AS compte_general_id,
						CASE WHEN PECCLH = '2' THEN 'H' ELSE 'C' END AS clinique_honoraire,
						CASE WHEN PECPTE 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 w_PEP01
				LEFT JOIN compta.t_comptes ON
					w_PEP01.type_compta_id = t_comptes.type_compta_id AND
					PECPTE = t_comptes.numero
				WHERE t_comptes.oid IS NULL
				ORDER BY 2;
				
				
				
				-- Comptes généraux des extra comptable
				UPDATE compta.t_comptes SET
					compte_general_id = t_comptes_c.oid,
					collectif = CASE WHEN t_comptes.oid = t_comptes_c.oid THEN '1' ELSE '0' END
				FROM w_PEP01
				JOIN compta.t_comptes t_comptes_c ON type_compta_gene_id = t_comptes_c.type_compta_id AND
						PECPTE_GENE = t_comptes_c.numero	
				WHERE w_PEP01.PECPTE = t_comptes.numero AND
					w_PEP01.type_compta_id = t_comptes.type_compta_id AND
					(
					t_comptes.compte_general_id IS DISTINCT FROM t_comptes_c.oid OR
					t_comptes.collectif IS DISTINCT FROM (CASE WHEN t_comptes.oid = t_comptes_c.oid THEN '1' ELSE '0' END)
					);
					
				-- Partenaires
				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, code_original, texte, texte_court)
				SELECT code, code, texte, texte FROM
				(
				SELECT 'C' || substr(numero, 5, 5) as code, MAX(texte) as texte 
				FROM compta.t_comptes 
				WHERE type_compta_id IN (select oid FROM compta.t_types_compta where code IN ('42', '43', '91', '92') ) GROUP BY 1
				UNION
				SELECT 'F' || substr(numero, 4, 5) as code, MAX(texte) as texte 
				FROM compta.t_comptes 
				WHERE type_compta_id IN (select oid FROM compta.t_types_compta where code IN ('40') ) GROUP BY 1
				UNION
				SELECT 'M' || substr(numero, 5, 5) as code, MAX(texte) as texte 
				FROM compta.t_comptes 
				WHERE type_compta_id IN (select oid FROM compta.t_types_compta where code IN ('46', '47', '48') ) GROUP BY 1
				) subview
				WHERE subview.code NOT IN (select code_original FROM compta.t_partenaires WHERE code_original IS NOT NULL);
				
				UPDATE compta.t_comptes SET partenaire_id = t_partenaires.oid
				FROM compta.t_partenaires
				WHERE type_compta_id IN (select oid FROM compta.t_types_compta where code IN ('42', '43', '91', '92') )
				AND t_partenaires.code_original = 'C' || substr(t_comptes.numero, 5, 5)
				AND (partenaire_id <> t_partenaires.oid OR partenaire_id IS NULL);


				UPDATE compta.t_comptes SET partenaire_id = t_partenaires.oid
				FROM compta.t_partenaires
				WHERE type_compta_id IN (select oid FROM compta.t_types_compta where code IN ('40') )
				AND t_partenaires.code_original = 'F' || substr(t_comptes.numero, 4, 5)
				AND (partenaire_id <> t_partenaires.oid OR partenaire_id IS NULL);


				UPDATE compta.t_comptes SET partenaire_id = t_partenaires.oid
				FROM compta.t_partenaires
				WHERE type_compta_id IN (select oid FROM compta.t_types_compta where code IN ('46', '47', '48') )
				AND t_partenaires.code_original = 'M' || substr(t_comptes.numero, 5, 5)
				AND (partenaire_id <> t_partenaires.oid OR partenaire_id IS NULL);

				UPDATE compta.t_comptes SET partenaire_id = 0 WHERE partenaire_id IS NULL;

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













