<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	
	
	<NODE name="INIT" label="INITIALISATIONS">


					
		
	</NODE>

	<NODE name="SEJOUR" label="RECUPERATION DES SEJOURS">
	
		<NODE label="Patients">
			<sqlcmd><![CDATA[
			
				-- Table des séjours pour ajouter numereo patient
				DROP TABLE IF EXISTS w_S_P_PCHA;
				CREATE TEMP TABLE w_S_P_PCHA AS
				SELECT S_P_PCHA.*, 
					''::text AS NIPP,
					''::text AS C_I_INDI_CODE
				FROM prod_anthadine.S_P_PCHA 
				WHERE PCHA_NODOS <> ''
				;

				UPDATE w_S_P_PCHA SET
					INDI_CODE = CASE WHEN w_S_P_PCHA.INDI_CODE <> '' THEN w_S_P_PCHA.INDI_CODE ELSE R_E_HIST.INDI_CODE END,
					C_I_INDI_CODE = R_E_HIST.C_I_INDI_CODE
				FROM prod_anthadine.R_E_HIST
				WHERE R_E_HIST.HIST_CODE = w_S_P_PCHA.HIST_CODE
				;
				UPDATE w_S_P_PCHA
				SET INDI_CODE = R_E_HIST.INDI_CODE
				FROM prod_anthadine.R_E_HIST
				WHERE R_E_HIST.PCHA_CODE = w_S_P_PCHA.PCHA_CODE AND
					w_S_P_PCHA.INDI_CODE = ''
				;
				UPDATE w_S_P_PCHA
				SET INDI_CODE = A_F_FSEJ.S_M_INDI_CODE
				FROM prod_anthadine.A_F_FSEJ
				WHERE A_F_FSEJ.PCHA_CODE = w_S_P_PCHA.PCHA_CODE AND
					w_S_P_PCHA.INDI_CODE = ''
				;

				
				DROP TABLE IF EXISTS w_S_P_PCHA_pat;
				CREATE TEMP TABLE w_S_P_PCHA_pat AS
				SELECT
					INDI_CODE,
					SUBSTR(PCHA_NODOS,0,LENGTH(PCHA_NODOS)-2) AS NIPP
				FROM w_S_P_PCHA
				GROUP BY 1,2
				ORDER BY 1,2
				;

				-- NIPP en double
				UPDATE w_S_P_PCHA_pat
				SET NIPP = NIPP || INDI_CODE
				WHERE NIPP IN (SELECT NIPP FROM w_S_P_PCHA_pat GROUP BY 1 HAVING count(*) > 1)
				;

				UPDATE w_S_P_PCHA
				SET NIPP = w_S_P_PCHA_pat.NIPP
				FROM w_S_P_PCHA_pat
				WHERE w_S_P_PCHA.INDI_CODE = w_S_P_PCHA_pat.INDI_CODE
				;
				
				-- Séjours en double
				UPDATE w_S_P_PCHA
				SET PCHA_NODOS = PCHA_NODOS || '-' || PCHA_CODE
				WHERE PCHA_NODOS IN
					(
					SELECT PCHA_NODOS
					FROM w_S_P_PCHA
					GROUP BY 1
					HAVING count(*) > 1
					)	
				;


				-- Ajout des patients
				DROP TABLE IF EXISTS w_patients;
				CREATE TEMP TABLE w_patients AS
				SELECT 
					'' as FINESS,
					NIPP,
					INDI_NOM,
					INDI_NOMJF,
					INDI_PRNOM,
					INDI_DNAIS,
					CASE WHEN INDI_SEXE = 'M' THEN '1' ELSE '2' END AS INDI_SEXE,
					IDENT
				FROM w_S_P_PCHA_pat
					JOIN prod_anthadine.S_M_INDI ON w_S_P_PCHA_pat.INDI_CODE = S_M_INDI.INDI_CODE
				;

			
				INSERT INTO activite[PX].p_patients(finess, no_patient, nom, nom_naissance, prenom, date_naissance, code_sexe)
				SELECT 
					'' as FINESS,
					NIPP,
					INDI_NOM,
					INDI_NOMJF,
					INDI_PRNOM,
					INDI_DNAIS,
					INDI_SEXE				
				FROM w_patients
					LEFT JOIN activite[PX].p_patients ON NIPP = p_patients.no_patient
				WHERE p_patients.no_patient IS NULL
				;
				
				UPDATE activite[PX].p_patients
				SET 
					nom = INDI_NOM, 
					nom_naissance = INDI_NOMJF, 
					prenom = INDI_PRNOM, 
					date_naissance = INDI_DNAIS,
					code_sexe = INDI_SEXE
				FROM w_patients
				WHERE p_patients.no_patient = NIPP
					AND (
						nom IS DISTINCT FROM INDI_NOM OR 
						nom_naissance IS DISTINCT FROM INDI_NOMJF OR 
						prenom IS DISTINCT FROM INDI_PRNOM OR 
						date_naissance IS DISTINCT FROM INDI_DNAIS OR
						code_sexe IS DISTINCT FROM INDI_SEXE
					);
								
			
			]]></sqlcmd>
		</NODE>
				
		<NODE label="Séjours">
			<sqlcmd><![CDATA[
				-- Les mouvements s'appuient sur la facturation, 
				-- Il faut repousser la date de la dernière facture 
				DROP TABLE IF EXISTS w_A_F_FSEJ_mvt;
				CREATE TEMP TABLE w_A_F_FSEJ_mvt AS
				SELECT *,
					FSEJ_DFIN AS FSEJ_DFIN_mvt
				FROM prod_anthadine.A_F_FSEJ
				;

				ANALYSE w_A_F_FSEJ_mvt
				;

				UPDATE w_A_F_FSEJ_mvt
				SET FSEJ_DFIN_mvt = PCHA_DFIN
				FROM
				(
				SELECT w_A_F_FSEJ_mvt.PCHA_CODE, (MAX(ARRAY[FSEJ_DFIN::text,FSEJ_CODE::text]))[2]::bigint AS FSEJ_CODE, MAX(PCHA_DFIN) AS PCHA_DFIN
				FROM w_A_F_FSEJ_mvt
				JOIN w_S_P_PCHA ON
					w_A_F_FSEJ_mvt.PCHA_CODE = w_S_P_PCHA.PCHA_CODE
				GROUP BY 1
				HAVING MAX(FSEJ_DFIN_mvt) < MAX(PCHA_DFIN)
				) subview
				WHERE w_A_F_FSEJ_mvt.FSEJ_CODE = subview.FSEJ_CODE
				;

				INSERT INTO w_A_F_FSEJ_mvt (
					PCHA_CODE,
					FSEJ_CODE,
					SERV_CODE,
					S_M_INDI_CODE,
					FSEJ_NUM,
					FSEJ_DDEBU,
					FSEJ_DFIN,
					FSEJ_DFIN_MVT,
					IDENT,
					CAIP_CODE,
					CAIC_CODE,
					FSEJ_NSEQ,
					FSEJ_NSSEQ
				)
				SELECT 
					PCHA_CODE,
					0-PCHA_CODE AS FSEJ_CODE,
					SERV_CODE,
					INDI_CODE AS S_M_INDI_CODE,
					1::numeric AS FSEJ_NUM,
					PCHA_DDEBU AS FSEJ_DDEBU,
					PCHA_DFIN AS FSEJ_DFIN,
					PCHA_DFIN AS FSEJ_DFIN_mvt,
					0::BIGINT AS IDENT,
					0::BIGINT AS CAIP_CODE,
					0::BIGINT AS CAIC_CODE,
					0::BIGINT AS FSEJ_NSEQ,
					0::BIGINT AS FSEJ_NSSEQ
				FROM w_S_P_PCHA
				WHERE PCHA_CODE NOT IN (SELECT PCHA_CODE FROM w_A_F_FSEJ_mvt)
				;


				-- lieux

				-- Récupération lieux depuis factures 
				DROP TABLE IF EXISTS w_lieu_sej;
				CREATE TEMP TABLE w_lieu_sej AS
				SELECT 0::bigint AS lieu_id,
						A_F_FSEJ.FSEJ_CODE,
						SERV_CODE,
						MAX(CASE WHEN UOTYPE_CODE = 1 THEN UO_CODE::text ELSE '0'::text END) AS UOACT_CODE,
						MAX(CASE WHEN UOTYPE_CODE = 2 THEN UO_CODE::text ELSE '0'::text END) AS UOGEO_CODE
					FROM w_A_F_FSEJ_mvt A_F_FSEJ
						LEFT JOIN prod_anthadine.C_S_UOFSEJ ON C_S_UOFSEJ.FSEJ_CODE = A_F_FSEJ.IDENT
					GROUP BY 1,2,3
				;

				ANALYSE w_lieu_sej
				;

				-- Complément depuis table séjours
				-- Récupération lieux depuis les séjours pour récupération séjours non facturés
				DROP TABLE IF EXISTS w_lieu_sej2;
				CREATE TEMP TABLE w_lieu_sej2 AS
				SELECT 0::bigint AS lieu_id,
						A_F_FSEJ.FSEJ_CODE,
						SERV_CODE,
						MAX(CASE WHEN c_s_uoappa.UOTYPE_CODE = 1 THEN C_S_UOAPPA.UO_CODE::text ELSE '0'::text END) AS UOACT_CODE,
						MAX(CASE WHEN c_s_uoappa.UOTYPE_CODE = 2 THEN C_S_UOAPPA.UO_CODE::text ELSE '0'::text END) AS UOGEO_CODE
					FROM w_A_F_FSEJ_mvt A_F_FSEJ
						JOIN prod_anthadine.C_S_UOAPPA ON A_F_FSEJ.S_M_INDI_CODE = C_S_UOAPPA.INDI_CODE
					GROUP BY 1,2,3
				;

				ANALYSE w_lieu_sej2
				;

				UPDATE w_lieu_sej SET
					UOACT_CODE = CASE WHEN w_lieu_sej.UOACT_CODE = '0' THEN w_lieu_sej2.UOACT_CODE ELSE w_lieu_sej.UOACT_CODE END,
					UOGEO_CODE = CASE WHEN w_lieu_sej.UOGEO_CODE = '0' THEN w_lieu_sej2.UOGEO_CODE ELSE w_lieu_sej.UOGEO_CODE END
				FROM w_lieu_sej2
				WHERE w_lieu_sej.FSEJ_CODE = w_lieu_sej2.FSEJ_CODE AND
					w_lieu_sej.SERV_CODE = w_lieu_sej2.SERV_CODE AND
					(
					w_lieu_sej.UOACT_CODE = '0' OR
					w_lieu_sej.UOGEO_CODE = '0'
					)
				;
				
				INSERT INTO activite[PX].t_lieux(
					code_original_1, -- service
					code_original_2, -- activite
					code_original_3, -- etage
					code_original_4, -- lit
					code_original_5, -- um
					code_original_6, -- uf
					code_original_7,
					service_facturation_id, 
					activite_id, 
					lit_id, 
					unite_medicale_id, 
					unite_fonctionnelle_id, 
					mode_traitement_id)
				SELECT DISTINCT 
					SERV_CODE, 
					UOACT_CODE,
					'',
					'',
					'',
					UOGEO_CODE,
					'',
					t_services_facturation.oid,
					t_activites.oid,
					0,
					0,
					t_unites_fonctionnelles.oid,
					t_services_facturation.mode_traitement_id
				FROM (
						SELECT SERV_CODE, UOACT_CODE, UOGEO_CODE 
						FROM w_lieu_sej
						GROUP BY 1,2,3) sub
					LEFT JOIN activite[PX].t_lieux ON (
						t_lieux.code_original_1 = SERV_CODE AND
						t_lieux.code_original_2 = UOACT_CODE AND 
						t_lieux.code_original_6 = UOGEO_CODE)
				, activite[PX].t_services_facturation, activite[PX].t_unites_fonctionnelles, activite[PX].t_activites
				WHERE 
					SERV_CODE = t_services_facturation.code_original AND
					UOACT_CODE = t_activites.code_original AND
					UOGEO_CODE = t_unites_fonctionnelles.code_original AND
					t_lieux.oid IS NULL;

										
				UPDATE w_lieu_sej
				SET lieu_id = t_lieux.oid
				FROM activite[PX].t_lieux
				WHERE
					t_lieux.code_original_1 = SERV_CODE AND
					t_lieux.code_original_2 = UOACT_CODE AND 
					t_lieux.code_original_6 = UOGEO_CODE
				;

				
				SELECT activite[PX].cti_reorganize_lieux_c();
				
				SELECT base.cti_disable_index('activite[PX]', 'i_sejours_2');
				SELECT base.cti_disable_index('activite[PX]', 'i_sejours_3');
				SELECT base.cti_disable_index('activite[PX]', 'i_sejours_4');
				SELECT base.cti_disable_index('activite[PX]', 'i_sejours_5');
				SELECT base.cti_disable_index('activite[PX]', 'i_sejours_6');
				SELECT base.cti_disable_index('activite[PX]', 'i_sejours_7');
				SELECT base.cti_disable_index('activite[PX]', 'i_sejours_8');
				SELECT base.cti_disable_index('activite[PX]', 'i_sejours_9');
				SELECT base.cti_disable_index('activite[PX]', 'i_sejours_10');
				SELECT base.cti_disable_index('activite[PX]', 'i_sejours_11');
				SELECT base.cti_disable_index('activite[PX]', 'i_sejours_12');
				SELECT base.cti_disable_index('activite[PX]', 'i_sejours_13');
				
				DROP TABLE IF EXISTS w_sej;
				CREATE TEMP TABLE w_sej AS
				SELECT A_F_FSEJ.PCHA_CODE,
					MAX(CASE WHEN UOTYPE_CODE = 1 THEN UO_CODE::text ELSE '0'::text END) AS UOACT_CODE,
					MAX(INDI_CODE) AS S_M_INDI_CODE,
					MAX(CASE WHEN UOTYPE_CODE = 2 THEN UO_CODE::text ELSE '0'::text END) AS UOGEO_CODE,
					(MAX(ARRAY[FSEJ_NSEQ::text || FSEJ_NSSEQ::text, CAIP_CODE::text]))[2] AS CAIP_CODE,
					(MAX(ARRAY[FSEJ_NSEQ::text || FSEJ_NSSEQ::text, CAIC_CODE::text]))[2] AS CAIC_CODE
				FROM w_S_P_PCHA S_P_PCHA 
					JOIN w_A_F_FSEJ_mvt A_F_FSEJ ON A_F_FSEJ.PCHA_CODE = S_P_PCHA.PCHA_CODE 
					LEFT JOIN prod_anthadine.C_S_UOFSEJ ON C_S_UOFSEJ.FSEJ_CODE = A_F_FSEJ.IDENT
				WHERE FSEJ_DANNUL IS NULL
				GROUP BY 1
				;
				
				INSERT INTO activite[PX].p_sejours (
					finess,
					code_original,
					no_sejour,
					no_patient,
					code_sexe,
					date_entree,
					heure_entree,
					mode_entree,
					provenance,
					date_sortie,
					heure_sortie,
					mode_sortie,
					destination,
					code_sorti,
					code_prevu,
					type_sejour,
					mode_traitement_id,
					lieu_sortie_id,
					medecin_sejour_id,
					medecin_traitant_id,
					tiers_payant_1_id,
					tiers_payant_2_id,
					tiers_payant_0_id,
					
					date_groupage,
					
					code_postal_id
				) 
					SELECT 
					'' AS finess,
					S_P_PCHA.PCHA_CODE AS code_original,
					S_P_PCHA.PCHA_NODOS AS code,
					S_P_PCHA.NIPP AS no_patient,
					p_patients.code_sexe,
					PCHA_DDEBU AS date_entree,
					COALESCE(REPLACE(PCHA_HDEBU,':','')::int,0) AS heure_entree,
					ENTR_ENSO_CODE AS mode_entree,
					CASE WHEN ENTR_PRDE_CODE >= 0 THEN ENTR_PRDE_CODE ELSE 0 END AS provenance,
					PCHA_DFIN AS date_sortie,
					COALESCE(REPLACE(PCHA_HFSEJ,':','')::int,0) AS heure_sortie,
					SORT_ENSO_CODE AS mode_sortie,
					CASE WHEN SORT_PRDE_CODE >= 0 THEN SORT_PRDE_CODE ELSE 0 END AS destination,
					PCHA_CLOTU AS code_sorti,
					CASE WHEN PCHA_DDEBU < now() THEN 0 ELSE 1 END as code_prevu,
					'1' AS type_sejour,
					t_services_facturation.mode_traitement_id,
					t_lieux.oid AS lieu_sortie_id,
					COALESCE(t_medecins_administratifs.oid,0) as medecin_sejour_id,
					COALESCE(t_medecins_traitants_administratifs.oid,0) as medecin_sejour_id,
					COALESCE(t_tiers_payant_1.oid,0),
					COALESCE(t_tiers_payant_2.oid,0),
					COALESCE(t_tiers_payant_0.oid,0),
		
					'2099-12-31' AS date_groupage,
					t_codes_postaux.oid AS code_postal_id
				FROM w_sej
					JOIN w_S_P_PCHA S_P_PCHA ON  S_P_PCHA.PCHA_CODE =  w_sej.PCHA_CODE
					JOIN prod_anthadine.S_M_INDI ON S_M_INDI_CODE = S_M_INDI.INDI_CODE
					LEFT JOIN (
						SELECT PCHA_CODE,
							(MAX(ARRAY[ROWID::text,S_O_INDI_CODE::text]))[2] as S_O_INDI_CODE
						FROM prod_anthadine.R_E_HIST GROUP BY 1)
						w_R_E_HIST ON w_R_E_HIST.PCHA_CODE = w_sej.PCHA_CODE
					LEFT JOIN prod_anthadine.S_M_MALA ON S_M_INDI_CODE = S_M_MALA.INDI_CODE 
					JOIN activite[PX].t_lieux ON 
						t_lieux.code_original_1 = S_P_PCHA.SERV_CODE AND
						t_lieux.code_original_2 = UOACT_CODE AND 
						t_lieux.code_original_6 = UOGEO_CODE
					LEFT JOIN activite[PX].t_services_facturation ON S_P_PCHA.SERV_CODE = t_services_facturation.code
					LEFT JOIN activite[PX].p_patients ON SUBSTR(PCHA_NODOS,0,GREATEST(LENGTH(PCHA_NODOS)-2,0)) = no_patient
					LEFT JOIN activite[PX].t_medecins_administratifs ON COALESCE(NULLIF(w_R_E_HIST.S_O_INDI_CODE,''),S_M_MALA.S_O_INDI_CODE,'') = t_medecins_administratifs.code_original
					LEFT JOIN activite[PX].t_medecins_traitants_administratifs ON  COALESCE(S_M_MALA.S_O_INDI_CODE,'') = t_medecins_traitants_administratifs.code_original
					LEFT JOIN activite[PX].t_tiers_payant t_tiers_payant_1 ON CAIP_CODE = t_tiers_payant_1.code_original
					LEFT JOIN activite[PX].t_tiers_payant t_tiers_payant_2 ON CAIC_CODE = t_tiers_payant_2.code_original
					LEFT JOIN activite[PX].t_tiers_payant t_tiers_payant_0 ON 'PATIENT' = t_tiers_payant_0.code_original
					LEFT JOIN (
							SELECT  
								S_M_HABI.INDI_CODE, 
								(MAX(ARRAY[COALESCE(HABI_DDEBUT,'1900-01-01'::date)::text,ADRS_CPOST::text]))[2] AS ADRS_CPOST
								--COALESCE(HABI_DDEBUT,'1900-01-01'::date) AS HABI_DDEBUT,
								--COALESCE(HABI_DFIN,'2099-12-31'::date) AS HABI_DFIN,
								
							FROM 
								prod_anthadine.S_M_HABI
								JOIN prod_anthadine.S_M_ADRS ON S_M_HABI.ADRS_CODE = S_M_ADRS.ADRS_CODE
							WHERE adrs_cpost != ''
							GROUP BY 1--,2,3
							) sub ON sub.INDI_CODE = S_M_INDI_CODE -- AND PCHA_DDEBU between HABI_DDEBUT AND HABI_DFIN
					LEFT JOIN base.t_codes_postaux ON t_codes_postaux.code = ADRS_CPOST
				;
				
				-- Report date sortie pour les présents
				UPDATE activite[PX].p_sejours
				SET date_sortie = date(now())
				WHERE date_sortie < date(now()) AND
					code_sorti = '0'
				;
				
				-- Les heures E/S ne sont pas toujours saisies
				UPDATE activite[PX].p_sejours
				SET heure_entree = 080000
				WHERE heure_entree = 0
				;

				UPDATE activite[PX].p_sejours
				SET heure_sortie = 180000
				WHERE code_sorti = '1' AND
					heure_sortie = 0
				;
			
				-- recréation index
				
				SELECT base.cti_enable_index('activite', 'i_sejours_2');
				SELECT base.cti_enable_index('activite', 'i_sejours_3');
				SELECT base.cti_enable_index('activite', 'i_sejours_4');
				SELECT base.cti_enable_index('activite', 'i_sejours_5');
				SELECT base.cti_enable_index('activite', 'i_sejours_6');
				SELECT base.cti_enable_index('activite', 'i_sejours_7');
				SELECT base.cti_enable_index('activite', 'i_sejours_8');
				SELECT base.cti_enable_index('activite', 'i_sejours_9');
				SELECT base.cti_enable_index('activite', 'i_sejours_10');
				SELECT base.cti_enable_index('activite', 'i_sejours_11');
				SELECT base.cti_enable_index('activite', 'i_sejours_12');
				SELECT base.cti_enable_index('activite', 'i_sejours_13');
				
				-- Provenance
				DROP TABLE IF EXISTS w_ETAB_PROV;
				CREATE TEMP TABLE w_ETAB_PROV AS
				SELECT w_S_P_PCHA.C_I_INDI_CODE ,
					COALESCE(S_M_INDI_3.INDI_NOM,S_M_INDI_2.INDI_NOM,S_M_INDI.INDI_NOM) AS INDI_NOM, 
					CASE 
					WHEN S_I_INTC_3.INTC_FINES <> '' THEN S_I_INTC_3.INTC_FINES||S_I_INTC_3.INTC_CFINE
					WHEN S_I_INTC_2.INTC_FINES <> '' THEN S_I_INTC_2.INTC_FINES||S_I_INTC_2.INTC_CFINE
					WHEN S_I_INTC.INTC_FINES <> '' THEN S_I_INTC.INTC_FINES||S_I_INTC.INTC_CFINE
					ELSE '' END AS finess,
					''::text AS INDI_NOM_SYN,
					0::bigint AS etablissement_id
				FROM w_S_P_PCHA
				JOIN prod_anthadine.S_M_INDI ON S_M_INDI.INDI_CODE = w_S_P_PCHA.C_I_INDI_CODE 
				LEFT JOIN prod_anthadine.S_I_INTC ON S_I_INTC.INDI_CODE = S_M_INDI.INDI_CODE  
				LEFT JOIN prod_anthadine.S_M_INDI S_M_INDI_2 ON S_M_INDI_2.INDI_CODE = S_I_INTC.INTC_INDI_CODE
				LEFT JOIN prod_anthadine.S_I_INTC S_I_INTC_2 ON S_I_INTC_2.INDI_CODE = S_M_INDI_2.INDI_CODE  
				LEFT JOIN prod_anthadine.S_M_INDI S_M_INDI_3 ON S_M_INDI_3.INDI_CODE = S_I_INTC_2.INTC_INDI_CODE
				LEFT JOIN prod_anthadine.S_I_INTC S_I_INTC_3 ON S_I_INTC_3.INDI_CODE = S_M_INDI_3.INDI_CODE  
				GROUP BY 1,2,3
				;
				
				ANALYSE w_ETAB_PROV
				;


				UPDATE w_ETAB_PROV
				SET etablissement_id = t_etablissements.oid
				FROM base.t_etablissements 
				WHERE t_etablissements.code = finess AND
					finess <> ''
				;

				-- Les finess ne sont pas toujours renseignes. Tentative par le texte
				UPDATE w_ETAB_PROV SET
					finess = t_etablissements.code,
					etablissement_id = t_etablissements.oid
				FROM base.t_etablissements 
				WHERE t_etablissements.texte ILIKE INDI_NOM AND
					etablissement_id = 0
				;
				UPDATE w_ETAB_PROV
				SET INDI_NOM_SYN = trim(REPLACE(UPPER(INDI_NOM),'CENTRE HOSPITALIER','CH'))
				WHERE INDI_NOM ILIKE '%CENTRE HOSPITALIER%'
				;
				UPDATE w_ETAB_PROV
				SET INDI_NOM_SYN = trim(REPLACE(UPPER(INDI_NOM),'CLINIQUE','POLYCLINIQUE'))
				WHERE INDI_NOM ILIKE 'CLINIQUE%'
				;
				UPDATE w_ETAB_PROV SET
					finess = t_etablissements.code,
					etablissement_id = t_etablissements.oid
				FROM base.t_etablissements 
				WHERE t_etablissements.texte ILIKE INDI_NOM_SYN AND
					INDI_NOM_SYN <> '' AND
					etablissement_id = 0
				;
				UPDATE w_ETAB_PROV SET
					finess = t_etablissements.code,
					etablissement_id = t_etablissements.oid
				FROM base.t_etablissements 
				WHERE t_etablissements.texte ILIKE '%'||INDI_NOM||'%' AND
					length(INDI_NOM) > 10 AND
					etablissement_id = 0
				;


				-- Finess non trouve, code provisoire
				UPDATE w_ETAB_PROV
				SET finess = 'X'||substr(upper(md5(INDI_NOM)),1,8)
				WHERE finess = ''
				;

				INSERT INTO base.t_etablissements (code, texte, texte_court)
				SELECT finess, MAX(INDI_NOM), MAX(INDI_NOM)
				FROM w_ETAB_PROV
				WHERE finess NOT IN (SELECT code FROM base.t_etablissements)
				GROUP BY 1
				;

				UPDATE w_ETAB_PROV
				SET etablissement_id = t_etablissements.oid
				FROM base.t_etablissements 
				WHERE t_etablissements.code = finess AND
					finess <> ''
				;

				INSERT INTO activite[PX].p_sejours_transferts (sejour_id, provenance_id, destination_id)
				SELECT p_sejours.oid,
					MAX(etablissement_id),
					MAX(0)
				FROM w_S_P_PCHA
				LEFT JOIN w_ETAB_PROV ON w_S_P_PCHA.C_I_INDI_CODE = w_ETAB_PROV.C_I_INDI_CODE
				JOIN activite[PX].p_sejours ON w_S_P_PCHA.PCHA_CODE = p_sejours.code_original
				GROUP BY 1
				;
									
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Mouvements">
			<sqlcmd><![CDATA[
			
				
				-- Mise en production des mouvements
				SELECT base.cti_disable_index('activite[PX]', 'i_mouvements_sejour_1');
				SELECT base.cti_disable_index('activite[PX]', 'i_mouvements_sejour_2');
				SELECT base.cti_disable_index('activite[PX]', 'i_mouvements_sejour_3');
				SELECT base.cti_disable_index('activite[PX]', 'i_mouvements_sejour_4');
				SELECT base.cti_disable_index('activite[PX]', 'i_mouvements_sejour_5');
				
				INSERT INTO activite[PX].p_mouvements_sejour(
							sejour_id, no_sejour, date, est_mouvement_previsionnel, nb_jours_js_non_inclus, 
							lieu_id, nb_entrees_directes, nb_sorties_directes, 
							nb_urgences, nb_externes, nb_ambulatoires, medecin_sejour_id, 
							nb_jours_js_inclus, nb_seances, nb_entrees_mutation_service, 
							nb_sorties_mutation_service, nb_entrees_mutation_activite, nb_sorties_mutation_activite, 
							nb_entrees_mutation_etage, nb_sorties_mutation_etage, nb_chambres_particulieres, 
							heure_debut, heure_fin, est_jour_entree, est_jour_sortie, est_jour_hospitalisation, 
							est_chambre_particuliere, 
							nb_entrees_mutation_unite_medicale, nb_sorties_mutation_unite_medicale, nb_bebes,
							est_premier_mouvement_jour, est_dernier_mouvement_jour, jour_semaine, is_weekend)
				SELECT 
						oid, 
						no_sejour, 
						date, 
						MAX(CASE WHEN p_sejours.code_prevu = '1' OR p_calendrier.date > now() - interval '1 day' THEN '1' ELSE '0' END) AS est_mouvement_previsionnel ,
						MAX(CASE WHEN p_sejours.type_sejour = '1' AND (p_calendrier.date <> date_sortie) THEN 1 ELSE 0 END) AS nb_jours_js_non_inclus, 
						MAX(lieu_id), 
						MAX(CASE WHEN date = date_entree THEN 1 ELSE 0 END) AS nb_entrees_directes,
						MAX(CASE WHEN date = date_sortie THEN 1 ELSE 0 END) nb_sorties_directes, 
						0 AS nb_urgences,
						0 AS nb_externes, 
						0 AS nb_ambulatoires, 
						MAX(medecin_sejour_id), 
						1 AS nb_jours_js_inclus,
						0 AS nb_seances, 
						0 AS nb_entrees_mutation_service, 
						0 AS nb_sorties_mutation_service,
						0 AS nb_entrees_mutation_activite,
						0 AS nb_sorties_mutation_activite, 
						0 AS nb_entrees_mutation_etage, 
						0 AS nb_sorties_mutation_etage,
						0 AS nb_chambres_particulieres, 
						MAX(CASE WHEN p_calendrier.date = date_entree THEN heure_entree ELSE 0 END) AS heure_debut,
						MAX(CASE WHEN p_calendrier.date = date_sortie THEN heure_sortie ELSE 240000 END) AS heure_fin,
						MAX(CASE WHEN p_calendrier.date = date_entree THEN 1 ELSE 0 END) AS est_jour_entree,
						MAX(CASE WHEN p_calendrier.date = date_sortie THEN 1 ELSE 0 END) AS est_jour_sortie,
						1 AS est_jour_hospitalisation, 
						0 AS est_chambre_particuliere, 
						0 AS nb_entrees_mutation_unite_medicale,
						0 AS nb_sorties_mutation_unite_medicale,
						0 AS nb_bebes,
						1 AS est_premier_mouvement_jour,
						1 AS est_dernier_mouvement_jour,
						MAX(jour_semaine),
						MAX(is_weekend)
				FROM activite[PX].p_sejours
					JOIN w_A_F_FSEJ_mvt A_F_FSEJ ON code_original = PCHA_CODE
					JOIN w_lieu_sej ON w_lieu_sej.FSEJ_CODE = A_F_FSEJ.FSEJ_CODE
				JOIN base.p_calendrier ON p_calendrier.date BETWEEN FSEJ_DDEBU AND FSEJ_DFIN_mvt
				WHERE FSEJ_DANNUL IS NULL
				GROUP BY 1,2,3
				;
	
				SELECT base.cti_enable_index('activite[PX]', 'i_mouvements_sejour_1');
				SELECT base.cti_enable_index('activite[PX]', 'i_mouvements_sejour_2');
				SELECT base.cti_enable_index('activite[PX]', 'i_mouvements_sejour_3');
				SELECT base.cti_enable_index('activite[PX]', 'i_mouvements_sejour_4');
				SELECT base.cti_enable_index('activite[PX]', 'i_mouvements_sejour_5');

						
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lien PMSI">
		
			<condition><![CDATA[
				"[TYPE_TRAITEMENT]" == "E"
			]]></condition>
			
			<sqlcmd><![CDATA[
			
					SELECT activite[PX].cti_reorganize_sejour_pmsi()

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

	<NODE name="FACTURE" label="RECUPERATION DES FACTURES">
	
		<NODE label="Entêtes">
			<sqlcmd><![CDATA[
			
				DROP TABLE IF EXISTS w_factures;
				CREATE TEMP TABLE w_factures AS 
				SELECT
					A_F_FSEJ.PCHA_CODE,
					A_F_FSEJ.FSEJ_CODE,
					nextval('activite[PX].s_factures'::regclass) AS oid,
					p_sejours.oid AS sejour_id,
					p_sejours.no_sejour,
					FSEJ_NUM AS no_facture,
					''::text AS no_facture_reference,
					0::bigint AS facture_reference_id,
					0::text AS no_facture_od_avoir,
					0::bigint AS facture_od_avoir_id,
					FSEJ_DDEBU AS date_debut,
					--T2AM_DFIN AS date_fin,
					FSEJ_T2AM_DFFAC AS date_fin,
					0 AS ghs_id,
					'0' AS type_facture,
					0 AS nb_rejets,
					'1' AS code_facture,
					FSEJ_T2AM_DFFAC AS date_facture,
					'1' AS code_vente,
					to_char(FSEJ_T2AM_DFFAC,'YYYYMM')::numeric AS mois_vente,
					FSEJ_T2AM_DFFAC AS date_vente,
					'' AS code_cloture,

					'' AS particularite_t2a,

					
					p_sejours.tiers_payant_0_id,
					p_sejours.tiers_payant_1_id,
					p_sejours.tiers_payant_2_id,
					02 as tiers_payant_22_id,
					FSEJ_MPCRO AS montant_facture_c,
					0::numeric AS montant_facture_h,
					FSEJ_MPCPA AS montant_facture_0_c,
					FSEJ_MHFPA AS montant_facture_0_h,
					FSEJ_MPCRO AS montant_facture_1_c,
					FSEJ_MHFRO AS montant_facture_1_h,
					--SUM(T2AM_TGHT * (1+t2am_dfin::date- t2am_ddeb::date) * (1 - (T2AM_CMCO * FSEJ_PCTXS / 100)) AS montant_facture_2_c,
					FSEJ_MPCRC AS montant_facture_2_c,
					FSEJ_MHFRC AS montant_facture_2_h,
					0::numeric AS montant_facture_22_c,
					0::numeric AS montant_facture_22_h,
					FSEJ_MPCRO AS montant_comptabilise_c,
					FSEJ_MHFPA AS montant_comptabilise_h,
					FSEJ_MPCPA AS montant_comptabilise_0_c,
					FSEJ_MHFPA AS montant_comptabilise_0_h,
					FSEJ_MPCRO AS montant_comptabilise_1_c,
					FSEJ_MHFRO AS montant_comptabilise_1_h,
					FSEJ_MPCRC AS montant_comptabilise_2_c,
					FSEJ_MHFRC AS montant_comptabilise_2_h,
					0::numeric AS montant_comptabilise_22_c,
					0::numeric AS montant_comptabilise_22_h,
					0::numeric AS montant_regle_c,
					0::numeric montant_regle_h,
					0::numeric AS montant_regle_0_c,
					0::numeric AS montant_regle_0_h,
					0::numeric AS montant_regle_1_c,
					0::numeric AS montant_regle_1_h,
					0::numeric AS montant_regle_2_c,
					0::numeric AS montant_regle_2_h,
					0::numeric AS montant_regle_22_c,
					0::numeric AS montant_regle_22_h,
					'1' AS code_expedie_0,
					'1' AS code_expedie_1,
					'1' AS code_expedie_2,
					'0' AS code_expedie_22,
					FSEJ_T2AM_DFFAC AS date_expedition,
					FSEJ_T2AM_DFFAC AS date_expedition_0,
					FSEJ_T2AM_DFFAC AS date_expedition_1,
					FSEJ_T2AM_DFFAC AS date_expedition_2,
					'20991231'::date AS date_expedition_22,
					''::text AS no_bordereau_0,
					''::text AS no_bordereau_1,
					''::text AS no_bordereau_2,
					''::text AS no_bordereau_22,
					'0001-01-01'::date AS date_solde,
					'0001-01-01'::date AS date_solde_c,
					'0001-01-01'::date AS date_solde_h,
					'0001-01-01'::date AS date_solde_0_c,
					'0001-01-01'::date AS date_solde_0_h,
					'0001-01-01'::date AS date_solde_1_c,
					'0001-01-01'::date AS date_solde_1_h,
					'0001-01-01'::date AS date_solde_2_c,
					'0001-01-01'::date AS date_solde_2_h,
					'0001-01-01'::date AS date_solde_22_c,
					'0001-01-01'::date AS date_solde_22_h
				FROM prod_anthadine.A_F_FSEJ
					JOIN activite[PX].p_sejours ON A_F_FSEJ.PCHA_CODE = p_sejours.code_original
				WHERE FSEJ_DANNUL IS NULL
				
				;
				
				ANALYSE w_factures
				;
				
				UPDATE w_factures
					SET montant_regle_0_c = REGL_MONT,
						date_solde_0_c = REGL_DATE
				FROM
					(SELECT FSEJ_CODE, SUM(REGL_MONT) AS REGL_MONT, MAX(REGL_DATE) AS REGL_DATE FROM prod_anthadine.V_F_REGP GROUP BY 1) sub
				WHERE sub.FSEJ_CODE = w_factures.FSEJ_CODE
				;
				
				UPDATE w_factures
					SET montant_regle_1_c = REGL_MONT,
						date_solde_1_c = REGL_DATE
				FROM
					(SELECT FSEJ_CODE, SUM(REGL_MONT) AS REGL_MONT, MAX(REGL_DATE) AS REGL_DATE FROM prod_anthadine.V_F_REGS GROUP BY 1) sub
				WHERE sub.FSEJ_CODE = w_factures.FSEJ_CODE
				;
					
				UPDATE w_factures
					SET montant_regle_2_c = REGL_MONT,
						date_solde_2_c = REGL_DATE
				FROM
					(SELECT FSEJ_CODE, SUM(REGL_MONT) AS REGL_MONT, MAX(REGL_DATE) AS REGL_DATE FROM prod_anthadine.V_F_REGC GROUP BY 1) sub
				WHERE sub.FSEJ_CODE = w_factures.FSEJ_CODE
				;
				
				UPDATE w_factures
					SET montant_regle_c = montant_regle_0_c + montant_regle_1_c + montant_regle_2_c,
						date_solde_2_c = GREATEST(date_solde_0_c,date_solde_1_c,date_solde_2_c)
				;
				
				CREATE INDEX w_factures_i1
					ON w_factures
					USING btree
					(FSEJ_CODE);

				CREATE INDEX w_factures_i2
					ON w_factures
					USING btree
					(sejour_id);
					
				-- Factures Molecules Onereuses
				DROP TABLE IF EXISTS w_factures_mo;
				CREATE TEMP TABLE w_factures_mo AS 
				SELECT
					
					F_A_PIEC.PIEC_CODE,
					MAX(S_P_PCHA.PCHA_CODE) AS PCHA_CODE,
					MAX(nextval('activite.s_factures'::regclass)) AS oid,
					MAX(p_sejours.oid) AS sejour_id,
					MAX(p_sejours.no_sejour) AS no_sejour,
					MAX('MO'||F_A_PIEC.PIEC_CODE) AS no_facture,
					''::text AS no_facture_reference,
					0::bigint AS facture_reference_id,
					0::text AS no_facture_od_avoir,
					0::bigint AS facture_od_avoir_id,
					MIN(COALESCE(LGFA_DADMI,PIEC_DDACT)) AS date_debut,
					MAX(COALESCE(LGFA_DADMI,PIEC_DDACT)) AS date_fin,
					0 AS ghs_id,
					'1' AS type_facture,
					0 AS nb_rejets,
					'1' AS code_facture,
					MAX(PIEC_DATE) AS date_facture,
					'1' AS code_vente,
					MAX(to_char(PIEC_DCOMPT,'YYYYMM')::numeric) AS mois_vente,
					MAX(PIEC_DCOMPT) AS date_vente,
					'' AS code_cloture,

					'' AS particularite_t2a,

					
					MAX(p_sejours.tiers_payant_0_id) AS tiers_payant_0_id,
					MAX(p_sejours.tiers_payant_1_id) AS tiers_payant_1_id,
					MAX(p_sejours.tiers_payant_2_id) AS tiers_payant_2_id,
					0 as tiers_payant_22_id,
					SUM(LGFA_MONT) AS montant_facture_c,
					0::numeric AS montant_facture_h,
					0::numeric AS montant_facture_0_c,
					0::numeric AS montant_facture_0_h,
					SUM(LGFA_MONT) AS montant_facture_1_c,
					0::numeric AS montant_facture_1_h,
					0::numeric AS montant_facture_2_c,
					0::numeric AS montant_facture_2_h,
					0::numeric AS montant_facture_22_c,
					0::numeric AS montant_facture_22_h,
					SUM(LGFA_MONREM) AS montant_comptabilise_c,
					0::numeric AS montant_comptabilise_h,
					0::numeric AS montant_comptabilise_0_c,
					0::numeric AS montant_comptabilise_0_h,
					SUM(LGFA_MONREM) AS montant_comptabilise_1_c,
					0::numeric AS montant_comptabilise_1_h,
					0::numeric AS montant_comptabilise_2_c,
					0::numeric AS montant_comptabilise_2_h,
					0::numeric AS montant_comptabilise_22_c,
					0::numeric AS montant_comptabilise_22_h,
					0::numeric AS montant_regle_c,
					0::numeric montant_regle_h,
					0::numeric AS montant_regle_0_c,
					0::numeric AS montant_regle_0_h,
					0::numeric AS montant_regle_1_c,
					0::numeric AS montant_regle_1_h,
					0::numeric AS montant_regle_2_c,
					0::numeric AS montant_regle_2_h,
					0::numeric AS montant_regle_22_c,
					0::numeric AS montant_regle_22_h,
					'0' AS code_expedie_0,
					'0' AS code_expedie_1,
					'0' AS code_expedie_2,
					'0' AS code_expedie_22,
					'20991231'::date AS date_expedition,
					'20991231'::date AS date_expedition_0,
					'20991231'::date AS date_expedition_1,
					'20991231'::date AS date_expedition_2,
					'20991231'::date AS date_expedition_22,
					''::text AS no_bordereau_0,
					''::text AS no_bordereau_1,
					''::text AS no_bordereau_2,
					''::text AS no_bordereau_22,
					'0001-01-01'::date AS date_solde,
					'0001-01-01'::date AS date_solde_c,
					'0001-01-01'::date AS date_solde_h,
					'0001-01-01'::date AS date_solde_0_c,
					'0001-01-01'::date AS date_solde_0_h,
					'0001-01-01'::date AS date_solde_1_c,
					'0001-01-01'::date AS date_solde_1_h,
					'0001-01-01'::date AS date_solde_2_c,
					'0001-01-01'::date AS date_solde_2_h,
					'0001-01-01'::date AS date_solde_22_c,
					'0001-01-01'::date AS date_solde_22_h
				FROM prod_anthadine.F_A_PIEC
				JOIN prod_anthadine.A_F_LGFA ON F_A_PIEC.PIEC_CODE = A_F_LGFA.PIEC_CODE
				JOIN prod_anthadine.S_P_PCHA ON f_a_piec.serv_code = s_p_pcha.serv_code
							AND f_a_piec.mala_indi_code = s_p_pcha.indi_code
							AND s_p_pcha.pcha_ddebu <= f_a_piec.piec_dpact
							AND s_p_pcha.pcha_dfsej >= f_a_piec.piec_dpact
				JOIN activite[PX].p_sejours ON S_P_PCHA.PCHA_CODE = p_sejours.code_original
				WHERE LGFA_ONERE = '1'
				GROUP BY 1
				;
				
				ANALYSE w_factures_mo
				;


				SELECT base.cti_disable_index('activite', 'i_factures_3');
				SELECT base.cti_disable_index('activite', 'i_factures_4');
				SELECT base.cti_disable_index('activite', 'i_factures_5');
				SELECT base.cti_disable_index('activite', 'i_factures_6');
				SELECT base.cti_disable_index('activite', 'i_factures_7');
				SELECT base.cti_disable_index('activite', 'i_factures_8');
				SELECT base.cti_disable_index('activite', 'i_factures_9');
				SELECT base.cti_disable_index('activite', 'i_factures_10');
				SELECT base.cti_disable_index('activite', 'i_factures_11');
				SELECT base.cti_disable_index('activite', 'i_factures_12');
				SELECT base.cti_disable_index('activite', 'i_factures_13');
				SELECT base.cti_disable_index('activite', 'i_factures_14');
				

				INSERT INTO activite[PX].p_factures (
					oid,
					sejour_id,
					no_sejour,
					no_facture,
					no_facture_reference,
					facture_reference_id,
					no_facture_od_avoir,
					facture_od_avoir_id,
					date_debut,
					date_fin,
					ghs_id,
					type_facture,
					nb_rejets,
					code_facture,
					date_facture,
					code_vente,
					mois_vente,
					date_vente,
					code_cloture,

					particularite_t2a,
					
					tiers_payant_0_id,
					tiers_payant_1_id,
					tiers_payant_2_id,
					tiers_payant_22_id,
					montant_facture_c,
					montant_facture_h,
					montant_facture_0_c,
					montant_facture_0_h,
					montant_facture_1_c,
					montant_facture_1_h,
					montant_facture_2_c,
					montant_facture_2_h,
					montant_facture_22_c,
					montant_facture_22_h,
					montant_comptabilise_c,
					montant_comptabilise_h,
					montant_comptabilise_0_c,
					montant_comptabilise_0_h,
					montant_comptabilise_1_c,
					montant_comptabilise_1_h,
					montant_comptabilise_2_c,
					montant_comptabilise_2_h,
					montant_comptabilise_22_c,
					montant_comptabilise_22_h,
					montant_regle_c,
					montant_regle_h,
					montant_regle_0_c,
					montant_regle_0_h,
					montant_regle_1_c,
					montant_regle_1_h,
					montant_regle_2_c,
					montant_regle_2_h,
					montant_regle_22_c,
					montant_regle_22_h,
					code_expedie_0,
					code_expedie_1,
					code_expedie_2,
					code_expedie_22,
					date_expedition,
					date_expedition_0,
					date_expedition_1,
					date_expedition_2,
					date_expedition_22,
					no_bordereau_0,
					no_bordereau_1,
					no_bordereau_2,
					no_bordereau_22,

					date_solde,
					date_solde_c,
					date_solde_h,
					date_solde_0_c,
					date_solde_0_h,
					date_solde_1_c,
					date_solde_1_h,
					date_solde_2_c,
					date_solde_2_h,
					date_solde_22_c,
					date_solde_22_h


					)

				SELECT
					oid,
					sejour_id,
					no_sejour,
					no_facture,
					w_factures.no_facture_reference,
					facture_reference_id,
					no_facture_od_avoir,
					facture_od_avoir_id,
					date_debut,
					date_fin,
					ghs_id,
					type_facture,
					nb_rejets,
					code_facture,
					date_facture,
					code_vente,
					mois_vente,
					date_vente,
					code_cloture,

					particularite_t2a,
					
					tiers_payant_0_id,
					tiers_payant_1_id,
					tiers_payant_2_id,
					tiers_payant_22_id,
					montant_facture_c,
					montant_facture_h,
					montant_facture_0_c,
					montant_facture_0_h,
					montant_facture_1_c,
					montant_facture_1_h,
					montant_facture_2_c,
					montant_facture_2_h,
					montant_facture_22_c,
					montant_facture_22_h,
					montant_comptabilise_c,
					montant_comptabilise_h,
					montant_comptabilise_0_c,
					montant_comptabilise_0_h,
					montant_comptabilise_1_c,
					montant_comptabilise_1_h,
					montant_comptabilise_2_c,
					montant_comptabilise_2_h,
					montant_comptabilise_22_c,
					montant_comptabilise_22_h,
					montant_regle_c,
					montant_regle_h,
					montant_regle_0_c,
					montant_regle_0_h,
					montant_regle_1_c,
					montant_regle_1_h,
					montant_regle_2_c,
					montant_regle_2_h,
					montant_regle_22_c,
					montant_regle_22_h,
					code_expedie_0,
					code_expedie_1,
					code_expedie_2,
					code_expedie_22,
					date_expedition ,
					date_expedition_0 ,
					date_expedition_1 ,
					date_expedition_2 ,
					date_expedition_22 ,
					no_bordereau_0,
					no_bordereau_1,
					no_bordereau_2,
					no_bordereau_22,

					date_solde,
					date_solde_c,
					date_solde_h,
					date_solde_0_c,
					date_solde_0_h,
					date_solde_1_c,
					date_solde_1_h,
					date_solde_2_c,
					date_solde_2_h,
					date_solde_22_c,
					date_solde_22_h

				FROM w_factures 
				WHERE oid NOT IN (SELECT oid FROM activite[PX].p_factures);
				
				
				INSERT INTO activite[PX].p_factures (
					oid,
					sejour_id,
					no_sejour,
					no_facture,
					no_facture_reference,
					facture_reference_id,
					no_facture_od_avoir,
					facture_od_avoir_id,
					date_debut,
					date_fin,
					ghs_id,
					type_facture,
					nb_rejets,
					code_facture,
					date_facture,
					code_vente,
					mois_vente,
					date_vente,
					code_cloture,

					particularite_t2a,
					
					tiers_payant_0_id,
					tiers_payant_1_id,
					tiers_payant_2_id,
					tiers_payant_22_id,
					montant_facture_c,
					montant_facture_h,
					montant_facture_0_c,
					montant_facture_0_h,
					montant_facture_1_c,
					montant_facture_1_h,
					montant_facture_2_c,
					montant_facture_2_h,
					montant_facture_22_c,
					montant_facture_22_h,
					montant_comptabilise_c,
					montant_comptabilise_h,
					montant_comptabilise_0_c,
					montant_comptabilise_0_h,
					montant_comptabilise_1_c,
					montant_comptabilise_1_h,
					montant_comptabilise_2_c,
					montant_comptabilise_2_h,
					montant_comptabilise_22_c,
					montant_comptabilise_22_h,
					montant_regle_c,
					montant_regle_h,
					montant_regle_0_c,
					montant_regle_0_h,
					montant_regle_1_c,
					montant_regle_1_h,
					montant_regle_2_c,
					montant_regle_2_h,
					montant_regle_22_c,
					montant_regle_22_h,
					code_expedie_0,
					code_expedie_1,
					code_expedie_2,
					code_expedie_22,
					date_expedition,
					date_expedition_0,
					date_expedition_1,
					date_expedition_2,
					date_expedition_22,
					no_bordereau_0,
					no_bordereau_1,
					no_bordereau_2,
					no_bordereau_22,

					date_solde,
					date_solde_c,
					date_solde_h,
					date_solde_0_c,
					date_solde_0_h,
					date_solde_1_c,
					date_solde_1_h,
					date_solde_2_c,
					date_solde_2_h,
					date_solde_22_c,
					date_solde_22_h


					)

				SELECT
					oid,
					sejour_id,
					no_sejour,
					no_facture,
					w_factures_mo.no_facture_reference,
					facture_reference_id,
					no_facture_od_avoir,
					facture_od_avoir_id,
					date_debut,
					date_fin,
					ghs_id,
					type_facture,
					nb_rejets,
					code_facture,
					date_facture,
					code_vente,
					mois_vente,
					date_vente,
					code_cloture,

					particularite_t2a,
					
					tiers_payant_0_id,
					tiers_payant_1_id,
					tiers_payant_2_id,
					tiers_payant_22_id,
					montant_facture_c,
					montant_facture_h,
					montant_facture_0_c,
					montant_facture_0_h,
					montant_facture_1_c,
					montant_facture_1_h,
					montant_facture_2_c,
					montant_facture_2_h,
					montant_facture_22_c,
					montant_facture_22_h,
					montant_comptabilise_c,
					montant_comptabilise_h,
					montant_comptabilise_0_c,
					montant_comptabilise_0_h,
					montant_comptabilise_1_c,
					montant_comptabilise_1_h,
					montant_comptabilise_2_c,
					montant_comptabilise_2_h,
					montant_comptabilise_22_c,
					montant_comptabilise_22_h,
					montant_regle_c,
					montant_regle_h,
					montant_regle_0_c,
					montant_regle_0_h,
					montant_regle_1_c,
					montant_regle_1_h,
					montant_regle_2_c,
					montant_regle_2_h,
					montant_regle_22_c,
					montant_regle_22_h,
					code_expedie_0,
					code_expedie_1,
					code_expedie_2,
					code_expedie_22,
					date_expedition ,
					date_expedition_0 ,
					date_expedition_1 ,
					date_expedition_2 ,
					date_expedition_22 ,
					no_bordereau_0,
					no_bordereau_1,
					no_bordereau_2,
					no_bordereau_22,

					date_solde,
					date_solde_c,
					date_solde_h,
					date_solde_0_c,
					date_solde_0_h,
					date_solde_1_c,
					date_solde_1_h,
					date_solde_2_c,
					date_solde_2_h,
					date_solde_22_c,
					date_solde_22_h

				FROM w_factures_mo 
				WHERE oid NOT IN (SELECT oid FROM activite[PX].p_factures);

				SELECT base.cti_enable_index('activite', 'i_factures_3');
				SELECT base.cti_enable_index('activite', 'i_factures_4');
				SELECT base.cti_enable_index('activite', 'i_factures_5');
				SELECT base.cti_enable_index('activite', 'i_factures_6');
				SELECT base.cti_enable_index('activite', 'i_factures_7');
				SELECT base.cti_enable_index('activite', 'i_factures_8');
				SELECT base.cti_enable_index('activite', 'i_factures_9');
				SELECT base.cti_enable_index('activite', 'i_factures_10');
				SELECT base.cti_enable_index('activite', 'i_factures_11');
				SELECT base.cti_enable_index('activite', 'i_factures_12');
				SELECT base.cti_enable_index('activite', 'i_factures_13');
				SELECT base.cti_enable_index('activite', 'i_factures_14');


				
				-- Séjours sans facture
				
				DROP TABLE IF EXISTS w_factures_trous;
				CREATE TEMP TABLE w_factures_trous AS
				SELECT
					p_sejours.finess,
					p_sejours.no_sejour,
					p_sejours.date_entree,
					p_sejours.tiers_payant_0_id, 
					p_sejours.tiers_payant_1_id, 
					p_sejours.tiers_payant_2_id, 
					p_sejours.tiers_payant_22_id,
					p_sejours.code_cp_demandee,
					COALESCE(t_services_facturation.avec_facturation_intermediaire,'') AS avec_facturation_intermediaire,
					p_sejours.code_sorti,
					CASE WHEN code_sorti = '1' THEN date_sortie ELSE date(now()) END AS date_sortie,
					MAX(CASE WHEN p_factures.date_fin IS NOT NULL AND date_sortie - date_entree < 30 THEN date_sortie ELSE p_factures.date_fin END) AS date_fin_facture,
					date(MAX(COALESCE(CASE WHEN p_factures.date_fin IS NOT NULL AND date_sortie - date_entree < 30 THEN date_sortie ELSE p_factures.date_fin END + interval '1 day', p_sejours.date_entree))) AS date_debut_encours,
					CASE WHEN code_sorti = '1' THEN date_sortie ELSE date(now()) END AS date_fin_encours
				FROM activite[PX].p_sejours
					LEFT JOIN activite[PX].p_factures ON p_sejours.no_sejour = p_factures.no_sejour AND no_facture NOT LIKE 'E%'
					JOIN activite[PX].t_lieux ON lieu_sortie_id = t_lieux.oid
					JOIN activite[PX].t_services_facturation ON t_lieux.service_facturation_id = t_services_facturation.oid
				WHERE p_sejours.etat = '' AND p_sejours.est_sans_facturation IS DISTINCT FROM '1' AND date_entree <= now() AND p_sejours.type_sejour <> '6' AND p_sejours.type_sejour <> '9' AND code_prevu <> 1
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11;
				
				INSERT INTO activite[PX].p_factures(
					finess, 
					no_sejour, 
					no_facture, 
					no_facture_reference, 
					type_facture, 
					code_facture, 
					date_facture, 
					tiers_payant_0_id, tiers_payant_1_id, tiers_payant_2_id, tiers_payant_22_id,
					code_cp_demandee, 
					date_debut, 
					date_fin)
				SELECT finess, 
					no_sejour, 
					no_sejour || '.00' || CASE WHEN avec_facturation_intermediaire = '1' THEN '_' || p_calendrier.mois ELSE '' END, 
					no_sejour || '.00' || CASE WHEN avec_facturation_intermediaire = '1' THEN '_' || p_calendrier.mois ELSE '' END, 
					'E', 
					'0', 
					'20991231'::date,
					tiers_payant_0_id, tiers_payant_1_id, tiers_payant_2_id, tiers_payant_22_id, 
					code_cp_demandee , 
					MIN(GREATEST(date_debut_encours, p_calendrier.date)), 
					MAX(LEAST(date_fin_encours, p_calendrier.date))
				FROM w_factures_trous 
					JOIN base.p_calendrier ON p_calendrier.date  BETWEEN date_debut_encours AND date_fin_encours
				WHERE (date_debut_encours <= date_fin_encours or date_fin_facture is null) 
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12;

					
				UPDATE activite[PX].p_factures SET
					date_debut = date_entree,
					date_fin = CASE WHEN code_sorti = '1' THEN date_sortie ELSE date_fin END
				FROM activite[PX].p_sejours 
					JOIN activite[PX].t_lieux ON lieu_sortie_id = t_lieux.oid
					JOIN activite[PX].t_services_facturation ON t_lieux.service_facturation_id = t_services_facturation.oid AND 
						avec_facturation_intermediaire IS DISTINCT FROM '1'
				WHERE p_factures.no_sejour = p_sejours.no_sejour AND
					CASE WHEN code_sorti = '1' THEN date_sortie ELSE date(now()) END - date_entree < 30 AND
					(
					p_factures.date_debut <> date_entree OR
					p_factures.date_fin <> CASE WHEN code_sorti = '1' THEN date_sortie ELSE date_fin END
					);
					
					
				-- Date de fin pour les factures Molecules
				UPDATE activite[PX].p_factures
				SET date_fin = p_sejours.date_sortie
				FROM activite[PX].p_sejours
				WHERE no_facture NOT LIKE 'MO%' AND
					p_factures.no_sejour = p_sejours.no_sejour AND
					p_sejours.code_sorti = '1' AND
					p_factures.date_fin = date(p_sejours.date_sortie - interval '1 day')
				;
				
				UPDATE activite[PX].p_factures SET
					date_fin = p_factures_ght.date_fin,
					date_debut = p_factures_ght.date_debut
				FROM activite[PX].p_factures p_factures_ght
				WHERE p_factures.no_facture LIKE 'MO%' AND
					p_factures.no_sejour = p_factures_ght.no_sejour AND
					p_factures_ght.type_facture = '0' AND
					p_factures.date_fin BETWEEN p_factures_ght.date_debut AND p_factures_ght.date_fin AND
					p_factures.date_fin <> p_factures_ght.date_fin
				;

								
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lignes non facturées Clinique">
			<sqlcmd><![CDATA[

				
				
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lignes factures Clinique">
			<sqlcmd><![CDATA[
			
				DROP SEQUENCE IF EXISTS w_factures_lignes_c_seq;
				CREATE TEMP SEQUENCE w_factures_lignes_c_seq;
			
				DROP TABLE IF EXISTS w_factures_lignes_c;
				CREATE TEMP TABLE w_factures_lignes_c AS 
				SELECT
					nextval('w_factures_lignes_c_seq'::regclass) AS sequence, 
					w_factures.no_sejour,
					w_factures.oid AS facture_id,
					w_factures.no_facture AS no_facture,
					w_factures.date_vente,

					''::text AS FAAVFA,
					
					FSEJ_CGEO AS coefficient,
					T2AM_CMCO AS coefficient_mco,
					t_rubriques_facturation.oid AS rubrique_facturation_id,
					t_rubriques_facturation.oid AS rubrique_comptabilisation_id,
					t_prestations.oid AS prestation_id,
					t_prestations.code AS prestation_code,
					t_prestations.type_ventilation_jour AS prestation_type_ventilation_jour,
					COALESCE(t_compte.oid,0) AS compte_produit_id,
					p_sejours.lieu_sortie_id AS lieu_id,
					1::bigint AS origine_facturation_id,

					T2AM_TGHT AS prix_unitaire,
					100 - CASE WHEN FSEJ_PCTXS != 0 THEN FSEJ_PCTXS ELSE 100 END - FSEJ_PCTXC AS taux_0,
					CASE WHEN FSEJ_PCTXS != 0 THEN FSEJ_PCTXS ELSE 100 END AS taux_1,
					FSEJ_PCTXC  AS taux_2,
					0::numeric AS taux_22,

					T2AM_DDEB AS date_debut,
					T2AM_DFIN AS date_fin,
					1 + (LEAST(T2AM_DFIN,T2AM_DFFAC)::date - T2AM_DDEB::date) AS nb_rubrique,
					1 + (LEAST(T2AM_DFIN,T2AM_DFFAC)::date - T2AM_DDEB::date) AS nb_prestation,
					T2AM_TGHT * FSEJ_CGEO * T2AM_CMCO *  (1 + (LEAST(T2AM_DFIN,T2AM_DFFAC)::date - T2AM_DDEB::date)) AS montant_facture,
					T2AM_TGHT * FSEJ_CGEO * T2AM_CMCO * (100 - (CASE WHEN FSEJ_PCTXS != 0 THEN FSEJ_PCTXS ELSE 100 END)) * (1 + (LEAST(T2AM_DFIN,T2AM_DFFAC)::date - T2AM_DDEB::date)) / 100  AS montant_facture_0,
					T2AM_TGHT * FSEJ_CGEO * T2AM_CMCO * (CASE WHEN FSEJ_PCTXS != 0 THEN FSEJ_PCTXS ELSE 100 END) * (1 + (LEAST(T2AM_DFIN,T2AM_DFFAC)::date - T2AM_DDEB::date)) / 100 AS montant_facture_1,
					T2AM_TGHT * FSEJ_CGEO * T2AM_CMCO * FSEJ_PCTXC * (1 + (LEAST(T2AM_DFIN,T2AM_DFFAC)::date - T2AM_DDEB::date)) / 100 AS montant_facture_2,
					0::numeric AS montant_facture_22,
					0::numeric AS montant_comptabilise,
					0::numeric AS montant_comptabilise_0,
					0::numeric AS montant_comptabilise_1,
					0::numeric AS montant_comptabilise_2,
					0::numeric AS montant_comptabilise_22,
					0::numeric AS rubrique_facture_id
				FROM prod_anthadine.A_C_T2AM
					JOIN prod_anthadine.A_F_FSEJ ON A_F_FSEJ.FSEJ_CODE = A_C_T2AM.FSEJ_CODE
					JOIN w_factures ON w_factures.FSEJ_CODE = A_C_T2AM.FSEJ_CODE
					JOIN activite[PX].p_sejours ON w_factures.no_sejour = p_sejours.no_sejour
					JOIN activite[PX].t_rubriques_facturation ON T2AM_GHT = t_rubriques_facturation.code_original 
					JOIN activite.t_prestations ON 'GHT' = t_prestations.code
					LEFT JOIN activite[PX].t_compte ON 0 = t_compte.code
				WHERE T2AM_DFFAC IS NOT NULL AND T2AM_DANNUL IS NULL
				;
				
				UPDATE w_factures_lignes_c
				SET
					montant_comptabilise	= montant_facture,
					montant_comptabilise_0	= montant_facture_0,
					montant_comptabilise_1	= montant_facture_1,
					montant_comptabilise_2	= montant_facture_2, 
                    montant_comptabilise_22	= montant_facture_22
				;
					
				CREATE INDEX w_factures_lignes_c_i1
				  ON w_factures_lignes_c
				  USING btree
				  (sequence);
				  
				CREATE INDEX w_factures_lignes_c_i2
				  ON w_factures_lignes_c
				  USING btree
				  (prestation_id);


				-- Eclatement des supplements par jour
				DROP TABLE IF EXISTS w_factures_lignes_sup_c;

				CREATE TEMP TABLE w_factures_lignes_sup_c AS
				SELECT
					date(p_sejours.date_entree) AS date_debut_ghs,
					date(p_sejours.date_sortie - interval '1 day') AS date_fin_ghs,
					(date(date_sortie) - date(date_entree)) * CASE WHEN nb_prestation < 0 THEN -1 ELSE 1 END AS nb_prestation_ghs,
					CASE WHEN prestation_code = 'GHS' THEN date(date_entree + interval '1 day') ELSE date(date_debut + interval '1 day') END AS date_debut_det,
					CASE WHEN prestation_code = 'GHS' THEN date(p_sejours.date_sortie - interval '1 day') ELSE date(date_debut + ((ABS(nb_prestation)-1)|| ' days')::interval)  END AS date_fin_det,
					ABS(CASE WHEN prestation_code = 'GHS' THEN date(date_sortie) - date(date_entree) ELSE nb_prestation END) AS nb_det,
					w_factures_lignes_c.*
				FROM w_factures_lignes_c
					JOIN activite[PX].p_sejours ON w_factures_lignes_c.no_sejour = p_sejours.no_sejour
				WHERE 
					(
						prestation_code IN ('PJ', 'FJ', 'SSM', 'GHS', 'GHT', 'EXH', 'SRC', 'REA', 'STF', 'NN1', 'NN2', 'NN3', 'CP', 'CPC', 'SHO') OR
						prestation_type_ventilation_jour = '1'
					)
						AND
					(
					(nb_prestation > 1 or nb_prestation < -1 OR 
					(prestation_code = 'GHS' AND date(date_sortie) - date(date_entree) > 1))
					) ;

				
				UPDATE w_factures_lignes_sup_c SET 
					date_fin_ghs = date(date_debut_exh - interval '1 day'),
					date_fin_det = date(date_debut_exh - interval '1 day'),
					nb_prestation_ghs = date(date_debut_exh - interval '1 day') - date_debut_ghs + 1,
					nb_det = date(date_debut_exh - interval '1 day') - date_debut_ghs + 1
				FROM (
					SELECT no_facture, date(MIN(date_debut)) AS date_debut_exh
					FROM w_factures_lignes_c
						JOIN activite.t_prestations ON prestation_id = t_prestations.oid AND code = 'EXH'
					GROUP BY 1) subview
				WHERE w_factures_lignes_sup_c.no_facture = subview.no_facture AND
					w_factures_lignes_sup_c.prestation_code = 'GHS';

				CREATE INDEX w_factures_lignes_sup_c_i1
				  ON w_factures_lignes_sup_c
				  USING btree
				  (date_debut);

				DROP TABLE IF EXISTS w_factures_lignes_sup_c_sup;

				CREATE TEMP TABLE w_factures_lignes_sup_c_sup AS
				SELECT
					sequence, 
					no_sejour,
					facture_id,
					no_facture,
					date_vente,

					FAAVFA,
					
					coefficient,
					coefficient_mco,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					prestation_id,
					prestation_code,
					prestation_type_ventilation_jour,
					compte_produit_id,
					lieu_id,
					origine_facturation_id,

					prix_unitaire,
					taux_0,
					taux_1,
					taux_2,
					taux_22,

					date AS date_debut,
					date AS date_fin,
					CASE WHEN prestation_code = 'GHS' AND date <> date_fin_ghs THEN 0 WHEN nb_prestation < 0 THEN -1 ELSE 1 END AS nb_rubrique,
					CASE WHEN prestation_code = 'GHS' AND date <> date_fin_ghs THEN 0 WHEN nb_prestation < 0 THEN -1 ELSE 1 END AS nb_prestation,
					round(montant_facture / ABS(nb_det),2) AS montant_facture,
					round(montant_facture_0 / ABS(nb_det),2) AS montant_facture_0,
					round(montant_facture_1 / ABS(nb_det),2) AS montant_facture_1,
					round(montant_facture_2 / ABS(nb_det),2) AS montant_facture_2,
					round(montant_facture_22 / ABS(nb_det),2) AS montant_facture_22,
					round(montant_comptabilise / ABS(nb_det),2) AS montant_comptabilise,
					round(montant_comptabilise_0 / ABS(nb_det),2) AS montant_comptabilise_0,
					round(montant_comptabilise_1 / ABS(nb_det),2) AS montant_comptabilise_1,
					round(montant_comptabilise_2 / ABS(nb_det),2) AS montant_comptabilise_2,
					round(montant_comptabilise_22 / ABS(nb_det),2) AS montant_comptabilise_22,
					rubrique_facture_id
				FROM w_factures_lignes_sup_c
					JOIN base.p_calendrier ON p_calendrier.date BETWEEN date_debut_det AND date_fin_det 
				;

				DROP TABLE IF EXISTS w_factures_lignes_sup_c_sup_tot;

				CREATE TEMP TABLE w_factures_lignes_sup_c_sup_tot AS

				SELECT sequence,
					SUM(nb_rubrique) AS nb_rubrique,
					SUM(nb_prestation) AS nb_prestation,
					SUM(montant_facture) AS montant_facture,
					SUM(montant_facture_0) AS montant_facture_0,
					SUM(montant_facture_1) AS montant_facture_1,
					SUM(montant_facture_2) AS montant_facture_2,
					SUM(montant_facture_22) AS montant_facture_22,
					SUM(montant_comptabilise) AS montant_comptabilise,
					SUM(montant_comptabilise_0) AS montant_comptabilise_0,
					SUM(montant_comptabilise_1) AS montant_comptabilise_1,
					SUM(montant_comptabilise_2) AS montant_comptabilise_2,
					SUM(montant_comptabilise_22) AS montant_comptabilise_22
				FROM w_factures_lignes_sup_c_sup
				GROUP BY 1;


				UPDATE w_factures_lignes_c
				SET
					date_fin = w_factures_lignes_c.date_debut,
					nb_rubrique = w_factures_lignes_c.nb_rubrique - w_factures_lignes_sup_c_sup_tot.nb_rubrique,
					nb_prestation = w_factures_lignes_c.nb_prestation - w_factures_lignes_sup_c_sup_tot.nb_prestation,
					montant_facture = w_factures_lignes_c.montant_facture - w_factures_lignes_sup_c_sup_tot.montant_facture,
					montant_facture_0 = w_factures_lignes_c.montant_facture_0 - w_factures_lignes_sup_c_sup_tot.montant_facture_0,
					montant_facture_1 = w_factures_lignes_c.montant_facture_1 - w_factures_lignes_sup_c_sup_tot.montant_facture_1,
					montant_facture_2 = w_factures_lignes_c.montant_facture_2 - w_factures_lignes_sup_c_sup_tot.montant_facture_2,
					montant_facture_22 = w_factures_lignes_c.montant_facture_22 - w_factures_lignes_sup_c_sup_tot.montant_facture_22,
					montant_comptabilise = w_factures_lignes_c.montant_comptabilise - w_factures_lignes_sup_c_sup_tot.montant_comptabilise,
					montant_comptabilise_0 = w_factures_lignes_c.montant_comptabilise_0 - w_factures_lignes_sup_c_sup_tot.montant_comptabilise_0,
					montant_comptabilise_1 = w_factures_lignes_c.montant_comptabilise_1 - w_factures_lignes_sup_c_sup_tot.montant_comptabilise_1,
					montant_comptabilise_2 = w_factures_lignes_c.montant_comptabilise_2 - w_factures_lignes_sup_c_sup_tot.montant_comptabilise_2,
					montant_comptabilise_22 = w_factures_lignes_c.montant_comptabilise_22 - w_factures_lignes_sup_c_sup_tot.montant_comptabilise_22
				FROM w_factures_lignes_sup_c_sup_tot
				WHERE w_factures_lignes_c.sequence = w_factures_lignes_sup_c_sup_tot.sequence;
				
				
		
				-- Ajout des lignes
				INSERT INTO w_factures_lignes_c(
					sequence, 
					no_sejour,
					facture_id,
					no_facture,
					date_vente,

					FAAVFA,
					
					coefficient,
					coefficient_mco,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					prestation_id,
					prestation_code,
					prestation_type_ventilation_jour,
					compte_produit_id,
					lieu_id,
					origine_facturation_id,

					prix_unitaire,
					taux_0,
					taux_1,
					taux_2,
					taux_22,

					date_debut,
					date_fin,
					nb_rubrique,
					nb_prestation,
					montant_facture,
					montant_facture_0,
					montant_facture_1,
					montant_facture_2,
					montant_facture_22,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					rubrique_facture_id)
				SELECT 
					sequence, 
					no_sejour,
					facture_id,
					no_facture,
					date_vente,

					FAAVFA,
					
					coefficient,
					coefficient_mco,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					prestation_id,
					prestation_code,
					prestation_type_ventilation_jour,
					compte_produit_id,
					lieu_id,
					origine_facturation_id,

					prix_unitaire,
					taux_0,
					taux_1,
					taux_2,
					taux_22,

					date_debut,
					date_fin,
					nb_rubrique,
					nb_prestation,
					montant_facture,
					montant_facture_0,
					montant_facture_1,
					montant_facture_2,
					montant_facture_22,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					rubrique_facture_id
				FROM w_factures_lignes_sup_c_sup;
				
				-- Inversion signe pour avoirs
				UPDATE w_factures_lignes_c SET
					nb_rubrique = 0-nb_rubrique,
					nb_prestation = 0-nb_prestation,
					montant_facture = 0-montant_facture,
					montant_facture_0 = 0-montant_facture_0,
					montant_facture_1 = 0-montant_facture_1,
					montant_facture_2 = 0-montant_facture_2,
					montant_facture_22 = 0-montant_facture_22,
					montant_comptabilise = 0-montant_comptabilise,
					montant_comptabilise_0 = 0-montant_comptabilise_0,
					montant_comptabilise_1 = 0-montant_comptabilise_1,
					montant_comptabilise_2 = 0-montant_comptabilise_2,
					montant_comptabilise_22 = 0-montant_comptabilise_22
				WHERE FAAVFA = 'A'
				;
				
				
				-- Lieu exécution
				
				
				UPDATE w_factures_lignes_c
				SET lieu_id = p_mouvements_sejour.lieu_id
				FROM 
					activite[PX].p_mouvements_sejour
				WHERE
					w_factures_lignes_c.no_sejour = p_mouvements_sejour.no_sejour AND
					w_factures_lignes_c.date_fin = p_mouvements_sejour.date AND
					(230000 BETWEEN p_mouvements_sejour.heure_debut AND p_mouvements_sejour.heure_fin OR 
					nb_entrees_directes = 1 AND heure_fin = 240000 OR 
					nb_sorties_directes = 1 ) AND
					w_factures_lignes_c.lieu_id <> p_mouvements_sejour.lieu_id;
					
				-- Rubriques internes cti
				SELECT activite[PX].cti_reorganize_rubrique_facture_c('w_factures_lignes_c');




				-- Lignes molecules onereuses
				DROP TABLE IF EXISTS w_factures_lignes_c_mo;
				CREATE TEMP TABLE w_factures_lignes_c_mo AS 
				SELECT
					nextval('w_factures_lignes_c_seq'::regclass) AS sequence, 
					w_factures_mo.no_sejour,
					w_factures_mo.oid AS facture_id,
					w_factures_mo.no_facture AS no_facture,
					w_factures_mo.date_vente,

					''::text AS FAAVFA,
					
					1::numeric AS coefficient,
					1::numeric AS coefficient_mco,
					t_rubriques_facturation.oid AS rubrique_facturation_id,
					t_rubriques_facturation.oid AS rubrique_comptabilisation_id,
					t_prestations.oid AS prestation_id,
					t_prestations.code AS prestation_code,
					t_prestations.type_ventilation_jour AS prestation_type_ventilation_jour,
					COALESCE(t_compte.oid,0) AS compte_produit_id,
					COALESCE(t_ucd.oid,0) AS ucd_id,
					p_sejours.lieu_sortie_id AS lieu_id,
					1::bigint AS origine_facturation_id,

					LGFA_PU AS prix_unitaire,
					0::numeric AS taux_0,
					LGFA_TXFINA AS taux_1,
					0::numeric AS taux_2,
					0::numeric AS taux_22,

					COALESCE(LGFA_DADMI,PIEC_DDACT) AS date_debut,
					COALESCE(LGFA_DADMI,PIEC_DDACT) AS date_fin,
					LGFA_QT AS nb_rubrique,
					LGFA_QT AS nb_prestation,
					LGFA_MONT AS montant_facture,
					0::numeric AS montant_facture_0,
					LGFA_MONT AS montant_facture_1,
					0::numeric AS montant_facture_2,
					0::numeric AS montant_facture_22,
					LGFA_MONREM AS montant_comptabilise,
					0::numeric AS montant_comptabilise_0,
					LGFA_MONREM AS montant_comptabilise_1,
					0::numeric AS montant_comptabilise_2,
					0::numeric AS montant_comptabilise_22,
					0::numeric AS rubrique_facture_id
				FROM prod_anthadine.F_A_PIEC
				JOIN prod_anthadine.A_F_LGFA ON F_A_PIEC.PIEC_CODE = A_F_LGFA.PIEC_CODE
				JOIN prod_anthadine.S_P_PCHA ON f_a_piec.serv_code = s_p_pcha.serv_code
							AND f_a_piec.mala_indi_code = s_p_pcha.indi_code
							AND s_p_pcha.pcha_ddebu <= f_a_piec.piec_dpact
							AND s_p_pcha.pcha_dfsej >= f_a_piec.piec_dpact
				LEFT JOIN prod_anthadine.S_O_RECO ON A_F_LGFA.S_O_RECO_IDENT = S_O_RECO.IDENT
				LEFT JOIN prod_anthadine.S_O_TRAI ON S_O_RECO.TRAI_CODE = S_O_TRAI.TRAI_CODE
				LEFT JOIN base.t_ucd ON TRAI_UCD = t_ucd.code
				JOIN w_factures_mo ON w_factures_mo.PIEC_CODE = F_A_PIEC.PIEC_CODE
				JOIN activite[PX].p_sejours ON w_factures_mo.no_sejour = p_sejours.no_sejour
				JOIN activite[PX].t_rubriques_facturation ON 'MO' = t_rubriques_facturation.code_original 
				JOIN activite.t_prestations ON 'MO' = t_prestations.code
				LEFT JOIN activite.t_compte ON 0 = t_compte.code
				WHERE LGFA_ONERE = '1'
				;
				
				UPDATE w_factures_lignes_c_mo w_factures_lignes_c
				SET lieu_id = p_mouvements_sejour.lieu_id
				FROM 
					activite[PX].p_mouvements_sejour
				WHERE
					w_factures_lignes_c.no_sejour = p_mouvements_sejour.no_sejour AND
					w_factures_lignes_c.date_fin = p_mouvements_sejour.date AND
					(230000 BETWEEN p_mouvements_sejour.heure_debut AND p_mouvements_sejour.heure_fin OR 
					nb_entrees_directes = 1 AND heure_fin = 240000 OR 
					nb_sorties_directes = 1 ) AND
					w_factures_lignes_c.lieu_id <> p_mouvements_sejour.lieu_id;
				
				-- Rubriques internes cti
				SELECT activite[PX].cti_reorganize_rubrique_facture_c('w_factures_lignes_c_mo');



				-- Validation des lignes de facture  
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_1');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_2');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_3');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_4');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_5');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_6');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_7');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_c_8');

				INSERT INTO activite[PX].p_factures_lignes_c (
					facture_id,
					no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					nb_prestation,
					coefficient,
					coefficient_mco,
					rubrique_facture_id,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					prestation_id,
					compte_produit_id,
					montant_facture,
					montant_facture_0,
					montant_facture_1,
					montant_facture_2,
					montant_facture_22,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					prix_unitaire,
					lieu_id,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					montant_encours,
					montant_encours_0,
					montant_encours_1,
					montant_encours_2,
					montant_encours_22,
					montant_facture_actes_inclus_dans_sejour,
					montant_facture_0_actes_inclus_dans_sejour,
					montant_facture_1_actes_inclus_dans_sejour,
					montant_facture_2_actes_inclus_dans_sejour,
					montant_facture_22_actes_inclus_dans_sejour,
					montant_comptabilise_budget_global_1,
					origine_facturation_id
				)
				SELECT
					facture_id,
					no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					nb_prestation,
					coefficient,
					coefficient_mco,
					rubrique_facture_id,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					prestation_id,
					compte_produit_id,
					montant_facture,
					montant_facture_0,
					montant_facture_1,
					montant_facture_2,
					montant_facture_22,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					prix_unitaire,
					lieu_id,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					0::numeric AS montant_encours,
					0::numeric AS montant_encours_0,
					0::numeric AS montant_encours_1,
					0::numeric AS montant_encours_2,
					0::numeric AS montant_encours_22,
					0::numeric AS montant_facture_actes_inclus_dans_sejour,
					0::numeric AS montant_facture_0_actes_inclus_dans_sejour,
					0::numeric AS montant_facture_1_actes_inclus_dans_sejour,
					0::numeric AS montant_facture_2_actes_inclus_dans_sejour,
					0::numeric AS montant_facture_22_actes_inclus_dans_sejour,
					0::numeric AS montant_comptabilise_budget_global_1,
					origine_facturation_id
				FROM w_factures_lignes_c
				WHERE montant_facture <> 0;
				
				
				INSERT INTO activite[PX].p_factures_lignes_c (
					facture_id,
					no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					nb_prestation,
					coefficient,
					coefficient_mco,
					rubrique_facture_id,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					prestation_id,
					compte_produit_id,
					ucd_id,
					montant_facture,
					montant_facture_0,
					montant_facture_1,
					montant_facture_2,
					montant_facture_22,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					prix_unitaire,
					lieu_id,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					montant_encours,
					montant_encours_0,
					montant_encours_1,
					montant_encours_2,
					montant_encours_22,
					montant_facture_actes_inclus_dans_sejour,
					montant_facture_0_actes_inclus_dans_sejour,
					montant_facture_1_actes_inclus_dans_sejour,
					montant_facture_2_actes_inclus_dans_sejour,
					montant_facture_22_actes_inclus_dans_sejour,
					montant_comptabilise_budget_global_1,
					origine_facturation_id
				)
				SELECT
					facture_id,
					no_facture,
					date_debut,
					date_fin,
					nb_rubrique,
					nb_prestation,
					coefficient,
					coefficient_mco,
					rubrique_facture_id,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					prestation_id,
					compte_produit_id,
					ucd_id,
					montant_facture,
					montant_facture_0,
					montant_facture_1,
					montant_facture_2,
					montant_facture_22,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					prix_unitaire,
					lieu_id,
					montant_comptabilise,
					montant_comptabilise_0,
					montant_comptabilise_1,
					montant_comptabilise_2,
					montant_comptabilise_22,
					0::numeric AS montant_encours,
					0::numeric AS montant_encours_0,
					0::numeric AS montant_encours_1,
					0::numeric AS montant_encours_2,
					0::numeric AS montant_encours_22,
					0::numeric AS montant_facture_actes_inclus_dans_sejour,
					0::numeric AS montant_facture_0_actes_inclus_dans_sejour,
					0::numeric AS montant_facture_1_actes_inclus_dans_sejour,
					0::numeric AS montant_facture_2_actes_inclus_dans_sejour,
					0::numeric AS montant_facture_22_actes_inclus_dans_sejour,
					0::numeric AS montant_comptabilise_budget_global_1,
					origine_facturation_id
				FROM w_factures_lignes_c_mo
				WHERE montant_facture <> 0 OR 
					montant_comptabilise <> 0;
				
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_1');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_2');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_3');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_4');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_5');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_6');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_7');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_c_8');
										
										
			]]></sqlcmd>
		</NODE>
	
		<NODE label="Compte client Clinique">
			<sqlcmd><![CDATA[
			
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_c_1');
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_c_2');
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_c_3');
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_c_4');
				SELECT base.cti_disable_index('activite', 'i_factures_soldes_c_8');
				
				
				INSERT INTO activite[PX].p_factures_soldes_c(
					facture_id,
					no_facture, 
					date_comptable, 
					rubrique_comptabilisee_id, 
					prestation_id, 
					montant_comptabilise, 
					montant_comptabilise_0, 
					montant_comptabilise_1, 
					montant_comptabilise_2, 
					montant_comptabilise_22, 
					montant_regle, 
					montant_regle_0, 
					montant_regle_1, 
					montant_regle_2, 
					montant_regle_22
				)
				SELECT
					facture_id,
					no_facture,
					date_trunc('month',date_vente) + interval '1 month' - interval '1 day',
					rubrique_comptabilisation_id,
					prestation_id,
					SUM(montant_comptabilise),
					SUM(montant_comptabilise_0),
					SUM(montant_comptabilise_1),
					SUM(montant_comptabilise_2),
					SUM(montant_comptabilise_22),
					0,
					0,
					0,
					0,
					0
				FROM w_factures_lignes_c
				WHERE date_vente >= '20120101' AND
					date_vente IS NOT NULL  AND
					(montant_comptabilise <> 0 OR 
					montant_comptabilise_0 <> 0 OR 
					montant_comptabilise_1 <> 0 OR 
					montant_comptabilise_2 <> 0 OR 
					montant_comptabilise_22 <> 0 
					)
				GROUP BY 1,2,3,4,5;
				
				INSERT INTO activite[PX].p_factures_soldes_c(
					facture_id,
					no_facture, 
					date_comptable, 
					rubrique_comptabilisee_id, 
					prestation_id, 
					montant_comptabilise, 
					montant_comptabilise_0, 
					montant_comptabilise_1, 
					montant_comptabilise_2, 
					montant_comptabilise_22, 
					montant_regle, 
					montant_regle_0, 
					montant_regle_1, 
					montant_regle_2, 
					montant_regle_22
				)
				SELECT
					facture_id,
					no_facture,
					date_trunc('month',date_vente) + interval '1 month' - interval '1 day',
					rubrique_comptabilisation_id,
					prestation_id,
					SUM(montant_comptabilise),
					SUM(montant_comptabilise_0),
					SUM(montant_comptabilise_1),
					SUM(montant_comptabilise_2),
					SUM(montant_comptabilise_22),
					0,
					0,
					0,
					0,
					0
				FROM w_factures_lignes_c_mo w_factures_lignes_c
				WHERE date_vente >= '20120101' AND
					date_vente IS NOT NULL  AND
					(montant_comptabilise <> 0 OR 
					montant_comptabilise_0 <> 0 OR 
					montant_comptabilise_1 <> 0 OR 
					montant_comptabilise_2 <> 0 OR 
					montant_comptabilise_22 <> 0 
					)
				GROUP BY 1,2,3,4,5;


				INSERT INTO activite[PX].p_factures_soldes_c(
					facture_id,
					no_facture, 
					date_comptable, 
					rubrique_comptabilisee_id, 
					prestation_id, 
					montant_comptabilise, 
					montant_comptabilise_0, 
					montant_comptabilise_1, 
					montant_comptabilise_2, 
					montant_comptabilise_22, 
					montant_regle, 
					montant_regle_0, 
					montant_regle_1, 
					montant_regle_2, 
					montant_regle_22
				)
				SELECT
					w_factures.oid AS facture_id,
					no_facture,
					REGL_DATE::date,
					0::bigint AS rubrique_comptabilisation_id,
					0::bigint AS prestation_id,
					0,
					0,
					0,
					0,
					0,
					SUM(REGL_MONT) AS montant_regle,
					SUM(CASE WHEN type = 0 THEN REGL_MONT ELSE 0 END) AS montant_regle_0,
					SUM(CASE WHEN type = 1 THEN REGL_MONT ELSE 0 END) AS montant_regle_1,
					SUM(CASE WHEN type = 2 THEN REGL_MONT ELSE 0 END) AS montant_regle_2,
					0 AS montant_regle_22
				FROM (
					SELECT 0 AS type, FSEJ_CODE, REGL_MONT, REGL_DATE FROM prod_anthadine.V_F_REGP
					UNION ALL
					SELECT 1, FSEJ_CODE, REGL_MONT, REGL_DATE FROM prod_anthadine.V_F_REGS
					UNION ALL
					SELECT 2, FSEJ_CODE, REGL_MONT, REGL_DATE FROM prod_anthadine.V_F_REGC
				) sub
				JOIN prod_anthadine.A_F_FSEJ ON A_F_FSEJ.FSEJ_CODE = sub.FSEJ_CODE
				JOIN w_factures ON FSEJ_NUM = w_factures.no_facture 
				GROUP BY 1,2,3,4,5
				;
				
				
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_c_1');
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_c_2');
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_c_3');
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_c_4');
				SELECT base.cti_enable_index('activite', 'i_factures_soldes_c_8');
				
				-- Si sejour solde, solde des factures lignes à lignes
				DROP TABLE IF EXISTS w_sejour_solde;
				CREATE TEMP TABLE w_sejour_solde AS
				SELECT no_sejour,
					MAX(CASE WHEN montant_regle <> 0 THEN date_comptable ELSE NULL END) AS date_solde
				FROM activite[PX].p_factures_soldes_c
				JOIN activite[PX].p_factures ON p_factures_soldes_c.no_facture = p_factures.no_facture
				GROUP BY 1
				HAVING SUM(montant_comptabilise) = SUM(montant_regle)
				;

				ANALYSE w_sejour_solde
				;
				
				DROP TABLE IF EXISTS w_facture_solde;
				CREATE TEMP TABLE w_facture_solde AS
				SELECT p_factures_soldes_c.no_facture,
					p_factures_soldes_c.facture_id,
					p_factures_soldes_c.rubrique_comptabilisee_id, 
					p_factures_soldes_c.prestation_id, 
					MAX(w_sejour_solde.date_solde) AS date_solde,
					SUM(montant_comptabilise) - SUM(montant_regle) AS montant_solde,
					SUM(montant_comptabilise_0) - SUM(montant_regle_0) AS montant_solde_0,
					SUM(montant_comptabilise_1) - SUM(montant_regle_1) AS montant_solde_1,
					SUM(montant_comptabilise_2) - SUM(montant_regle_2) AS montant_solde_2
				FROM activite[PX].p_factures_soldes_c
				JOIN activite[PX].p_factures ON p_factures_soldes_c.no_facture = p_factures.no_facture
				JOIN w_sejour_solde ON p_factures.no_sejour = w_sejour_solde.no_sejour
				GROUP BY 1,2,3,4
				HAVING SUM(montant_comptabilise) IS DISTINCT FROM SUM(montant_regle)
				;

				ANALYSE w_facture_solde
				;

				INSERT INTO activite[PX].p_factures_soldes_c(
					facture_id,
					no_facture, 
					date_comptable, 
					rubrique_comptabilisee_id, 
					prestation_id, 
					montant_comptabilise, 
					montant_comptabilise_0, 
					montant_comptabilise_1, 
					montant_comptabilise_2, 
					montant_comptabilise_22, 
					montant_regle, 
					montant_regle_0, 
					montant_regle_1, 
					montant_regle_2, 
					montant_regle_22,
					od_non_comptabilise
				)
				SELECT
					facture_id,
					no_facture,
					date_solde,
					rubrique_comptabilisee_id,
					prestation_id,
					0::numeric AS montant_comptabilise,
					0::numeric AS montant_comptabilise_0,
					0::numeric AS montant_comptabilise_1,
					0::numeric AS montant_comptabilise_2,
					0::numeric AS montant_comptabilise_22,
					montant_solde AS montant_regle, 
					montant_solde_0 AS montant_regle_0, 
					montant_solde_1 AS montant_regle_1, 
					montant_solde_2 AS montant_regle_2, 
					0::numeric AS montant_regle_22,
					'1'::text AS od_non_comptabilise
				FROM w_facture_solde
				;

				UPDATE activite[PX].p_factures SET
					montant_regle_c = montant_comptabilise_c,
					montant_regle_0_c = montant_comptabilise_0_c,
					montant_regle_1_c = montant_comptabilise_1_c,
					montant_regle_2_c = montant_comptabilise_2_c
				FROM w_sejour_solde
				WHERE p_factures.no_sejour = w_sejour_solde.no_sejour AND
					(
					montant_regle_c <> montant_comptabilise_c OR
					montant_regle_0_c <> montant_comptabilise_0_c OR
					montant_regle_1_c <> montant_comptabilise_1_c OR
					montant_regle_2_c <> montant_comptabilise_2_c
					)
				;
				
				
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Lignes factures Honoraires">
			<sqlcmd><![CDATA[
			
				DROP TABLE IF EXISTS w_factures_lignes_h;
				CREATE TEMP TABLE w_factures_lignes_h AS 
				SELECT
					w_factures.no_sejour,
					w_factures.oid AS facture_id,
					w_factures.no_facture AS no_facture,
					w_factures.date_vente,
					date(S_R_ACTE.ACTE_DATE) AS date_debut, 
					date(S_R_ACTE.ACTE_DATE) AS date_fin, 
					ACTE_NBACT AS nb_rubrique, 
					ACTE_NBACT AS nb_prestation, 
					1::numeric AS coefficient, 
					1::numeric AS coefficient_mco, 
					COALESCE(t_medecins_administratifs.oid,0) AS medecin_facture_id, 
					COALESCE(t_medecins_administratifs.oid,0) AS medecin_comptabilise_id, 
					w_S_N_VCAL.prestation_id,
					w_S_N_VCAL.rubrique_facturation_id,
					w_S_N_VCAL.rubrique_facturation_id AS rubrique_comptabilisation_id, 
					w_S_N_VCAL.acte_id,
					0::bigint AS protocole_id,
					ACTE_MTACT + ACTE_MTDEP AS montant_facture, 
					0::numeric AS montant_facture_0, 
					ACTE_MTACT + ACTE_MTDEP AS montant_facture_1, 
					0::numeric AS montant_facture_2, 
					0::numeric AS montant_facture_22, 
					ACTE_MTDEP AS montant_depassement,
					0::numeric AS taux_0, 
					100::numeric AS taux_1, 
					0::numeric AS taux_2, 
					0::numeric AS taux_22, 
					ACTE_MTACT + ACTE_MTDEP AS prix_unitaire, 
					lieu_sortie_id AS lieu_id, 
					
					ACTE_MTACT + ACTE_MTDEP AS montant_comptabilise, 
					0::numeric AS montant_comptabilise_0, 
					ACTE_MTACT + ACTE_MTDEP AS montant_comptabilise_1, 
					0::numeric AS montant_comptabilise_2,
					0::numeric AS montant_comptabilise_22, 
					
					0::numeric AS montant_encours, 
					0::numeric AS montant_encours_0, 
					0::numeric AS montant_encours_1, 
					0::numeric AS montant_encours_2, 
					0::numeric AS montant_encours_22,

					0::numeric AS montant_facture_actes_inclus_dans_sejour, 
					0::numeric AS montant_facture_0_actes_inclus_dans_sejour, 
					0::numeric AS montant_facture_1_actes_inclus_dans_sejour, 
					0::numeric AS montant_facture_2_actes_inclus_dans_sejour, 
					0::numeric AS montant_facture_22_actes_inclus_dans_sejour

				FROM prod_anthadine.A_F_FSACTE
				JOIN prod_anthadine.S_R_ACTE ON S_R_ACTE_IDENT = S_R_ACTE.IDENT
				JOIN prod_anthadine.A_F_FSEJ ON A_F_FSEJ.FSEJ_CODE = A_F_FSACTE.FSEJ_CODE
				JOIN w_S_N_VCAL ON TACT_CODE = VCAL_CODE
				JOIN w_factures ON w_factures.FSEJ_CODE = A_F_FSEJ.FSEJ_CODE
				JOIN activite[PX].p_sejours ON w_factures.no_sejour = p_sejours.no_sejour
				LEFT JOIN activite[PX].t_medecins_administratifs ON PRO_INDI_CODE = t_medecins_administratifs.code_original
				;
				
				
				UPDATE w_factures_lignes_h w_factures_lignes_h
				SET lieu_id = p_mouvements_sejour.lieu_id
				FROM activite[PX].p_mouvements_sejour
				WHERE
					w_factures_lignes_h.no_sejour = p_mouvements_sejour.no_sejour AND
					w_factures_lignes_h.date_fin = p_mouvements_sejour.date AND
					(230000 BETWEEN p_mouvements_sejour.heure_debut AND p_mouvements_sejour.heure_fin OR 
					nb_entrees_directes = 1 AND heure_fin = 240000 OR 
					nb_sorties_directes = 1 ) AND
					w_factures_lignes_h.lieu_id <> p_mouvements_sejour.lieu_id
				;
				
				
				INSERT INTO activite[PX].p_factures_lignes_h(
					no_facture, 
					date_debut, 
					date_fin, 
					nb_rubrique, 
					nb_prestation, 
					coefficient, 
					coefficient_mco, 
					medecin_facture_id, 
					medecin_comptabilise_id, 
					prestation_id,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					acte_id,
					protocole_id, 
					montant_facture, 
					montant_facture_0, 
					montant_facture_1, 
					montant_facture_2, 
					montant_facture_22, 
					montant_depassement,
					taux_0, 
					taux_1, 
					taux_2, 
					taux_22, 
					prix_unitaire, 
					lieu_id, 
					
					montant_comptabilise, 
					montant_comptabilise_0, 
					montant_comptabilise_1, 
					montant_comptabilise_2,
					montant_comptabilise_22, 
					
					montant_encours, 
					montant_encours_0, 
					montant_encours_1, 
					montant_encours_2, 
					montant_encours_22,

					montant_facture_actes_inclus_dans_sejour, 
					montant_facture_0_actes_inclus_dans_sejour, 
					montant_facture_1_actes_inclus_dans_sejour, 
					montant_facture_2_actes_inclus_dans_sejour, 
					montant_facture_22_actes_inclus_dans_sejour
				)
				SELECT 
					no_facture, 
					date_debut, 
					date_fin, 
					nb_rubrique, 
					nb_prestation, 
					coefficient, 
					coefficient_mco, 
					medecin_facture_id, 
					medecin_comptabilise_id, 
					prestation_id,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					acte_id,
					protocole_id, 
					montant_facture, 
					montant_facture_0, 
					montant_facture_1, 
					montant_facture_2, 
					montant_facture_22, 
					montant_depassement,
					taux_0, 
					taux_1, 
					taux_2, 
					taux_22, 
					prix_unitaire, 
					lieu_id, 
					
					montant_comptabilise, 
					montant_comptabilise_0, 
					montant_comptabilise_1, 
					montant_comptabilise_2,
					montant_comptabilise_22, 
					
					montant_encours, 
					montant_encours_0, 
					montant_encours_1, 
					montant_encours_2, 
					montant_encours_22,

					montant_facture_actes_inclus_dans_sejour, 
					montant_facture_0_actes_inclus_dans_sejour, 
					montant_facture_1_actes_inclus_dans_sejour, 
					montant_facture_2_actes_inclus_dans_sejour, 
					montant_facture_22_actes_inclus_dans_sejour
				FROM w_factures_lignes_h
				;
				
				UPDATE activite[PX].p_factures SET
					montant_facture_h = subview.montant_facture,
					montant_facture_0_h = subview.montant_facture_0,
					montant_facture_1_h = subview.montant_facture_1,
					montant_facture_2_h = subview.montant_facture_2,
					montant_facture_22_h = subview.montant_facture_22,
					montant_comptabilise_h = subview.montant_comptabilise,
					montant_comptabilise_0_h = subview.montant_comptabilise_0,
					montant_comptabilise_1_h = subview.montant_comptabilise_1,
					montant_comptabilise_2_h = subview.montant_comptabilise_2,
					montant_comptabilise_22_h = subview.montant_comptabilise_22
				FROM 
				(
				SELECT no_facture,
					SUM(montant_facture) AS montant_facture,
					SUM(montant_facture_0) AS montant_facture_0,
					SUM(montant_facture_1) AS montant_facture_1,
					SUM(montant_facture_2) AS montant_facture_2,
					SUM(montant_facture_22) AS montant_facture_22,
					SUM(montant_comptabilise) AS montant_comptabilise,
					SUM(montant_comptabilise_0) AS montant_comptabilise_0,
					SUM(montant_comptabilise_1) AS montant_comptabilise_1,
					SUM(montant_comptabilise_2) AS montant_comptabilise_2,
					SUM(montant_comptabilise_22) AS montant_comptabilise_22
				FROM w_factures_lignes_h
				GROUP BY 1
				) subview
				WHERE p_factures.no_facture = subview.no_facture
				;
			
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Compte client Honoraires">
			<sqlcmd><![CDATA[
			
				
				INSERT INTO activite[PX].p_factures_soldes_h(
					no_facture, 
					date_comptable, 
					medecin_comptabilise_id, 
					rubrique_comptabilisee_id,
					prestation_id,
					montant_comptabilise, 
					montant_comptabilise_0, 
					montant_comptabilise_1, 
					montant_comptabilise_2, 
					montant_comptabilise_22, 
					montant_regle, 
					montant_regle_0, 
					montant_regle_1, 
					montant_regle_2, 
					montant_regle_22
				)
				SELECT
					no_facture,
					date_vente,
					medecin_comptabilise_id,
					rubrique_comptabilisation_id,
					prestation_id,
					SUM(montant_comptabilise),
					SUM(montant_comptabilise_0),
					SUM(montant_comptabilise_1),
					SUM(montant_comptabilise_2),
					SUM(montant_comptabilise_22),
					0,
					0,
					0,
					0,
					0
				FROM w_factures_lignes_h
				WHERE date_vente IS NOT NULL AND
					(montant_comptabilise <> 0 OR 
					montant_comptabilise_0 <> 0 OR 
					montant_comptabilise_1 <> 0 OR 
					montant_comptabilise_2 <> 0 OR 
					montant_comptabilise_22 <> 0 
					)
				GROUP BY 1,2,3,4,5;
			
			]]></sqlcmd>
		</NODE>

	</NODE>
	
	<NODE name="PARAM" label="RECUPERATION DES PARAMETRES">
	
		<NODE label="Type d'environnement">
			<sqlcmd><![CDATA[
				CTISELECT_PROPERTY_READ 'ENV_WITHPMSI', MAX(CASE WHEN schemaname = 'pmsi' AND tablename = 'p_rss' THEN 1 ELSE 0 END) 
				FROM pg_tables 
				WHERE schemaname = 'pmsi' AND tablename = 'p_rss'
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Tiers payant">
			<sqlcmd><![CDATA[

				INSERT INTO activite[PX].t_types_tiers_payant(code, texte, texte_court)
				SELECT code, texte, texte FROM
				(
				SELECT '0' AS code, 'Patients' AS texte
				UNION ALL
				SELECT '1','Régime général'
				UNION ALL
				SELECT '2','Mutuelles'
				) subview
				WHERE code NOT IN (SELECT code FROM activite[PX].t_types_tiers_payant);

				INSERT INTO activite[PX].t_tiers_payant(oid, code, code_original, type_tiers_payant, texte, texte_court) 
				SELECT 0, '', '0', '0', 'Non saisi', 'Non saisi'
				WHERE 0 NOT IN (SELECT oid FROM activite[PX].t_tiers_payant);

				INSERT INTO activite[PX].t_tiers_payant(code, code_original, type_tiers_payant, texte, texte_court) 
				SELECT 'PATIENT', 'PATIENT', '0', 'PATIENTS', 'PATIENTS'
				WHERE 'PATIENT' NOT IN (SELECT code_original FROM activite[PX].t_tiers_payant WHERE code_original IS NOT NULL);

				INSERT INTO activite[PX].t_tiers_payant(code_original, code, type_tiers_payant, texte, texte_court) 
				SELECT IDENT, CAIP_CODE, '1', CAIP_NOM, LEFT(CAIP_NOM,50)
				FROM prod_anthadine.S_M_CAIP
				WHERE IDENT NOT IN (SELECT code_original FROM activite[PX].t_tiers_payant WHERE code_original IS NOT NULL)
				;

				UPDATE activite[PX].t_tiers_payant
				SET grand_regime_id = t_grands_regimes.oid
				FROM prod_anthadine.S_M_CAIP
				JOIN base.t_grands_regimes ON REGI_CODE = t_grands_regimes.code AND
					t_grands_regimes.oid <> 0
				WHERE t_tiers_payant.code_original = IDENT AND
					(grand_regime_id = 0 OR grand_regime_id IS NULL);


				INSERT INTO activite[PX].t_tiers_payant(code_original, code, type_tiers_payant, texte, texte_court) 
				SELECT IDENT, CAIC_CODE, '2', CAIC_NOM, LEFT(CAIC_NOM,50)
				FROM prod_anthadine.S_S_CAIC
				WHERE IDENT NOT IN (SELECT code_original FROM activite[PX].t_tiers_payant WHERE code_original IS NOT NULL)
				;
		
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Médecins">
			<sqlcmd><![CDATA[

				INSERT INTO activite[PX].t_specialites_medecin(oid, code, code_original, texte, texte_court) 
				SELECT 0, '**', '0', 'Non saisie', 'Non saisie'
				WHERE 0 NOT IN (SELECT oid FROM activite[PX].t_specialites_medecin);
				
				INSERT INTO base.t_specialites_medecin(oid, code, code_original, texte, texte_court) 
				SELECT 0, '', '0', 'Non saisie', 'Non saisie'
				WHERE 0 NOT IN (SELECT oid FROM base.t_specialites_medecin);

				INSERT INTO activite[PX].t_specialites_medecin(code, code_original, texte, texte_court) 
				SELECT SPPS_CODE, SPPS_CODE, SPPS_LIBE, LEFT(SPPS_LIBE,50)
				FROM prod_anthadine.C_N_SPPS
				WHERE SPPS_CODE NOT IN (SELECT code FROM activite[PX].t_specialites_medecin);
				
				INSERT INTO base.t_specialites_medecin(code, code_original, texte, texte_court) 
				SELECT SPPS_CODE, SPPS_CODE, SPPS_LIBE, LEFT(SPPS_LIBE,50)
				FROM prod_anthadine.C_N_SPPS
				WHERE SPPS_CODE NOT IN (SELECT code FROM base.t_specialites_medecin);

				INSERT INTO base.t_medecins(oid, nom, prenom, numero_ordre, specialite_id ) 
				SELECT 0, 'Non renseigné', '', '', 0 WHERE 0 NOT IN (SELECT oid FROM base.t_medecins);

				INSERT INTO activite[PX].t_medecins_administratifs(oid, code_original, code, nom, prenom, specialite_id, medecin_id) 
				SELECT 0, '0', '****', 'Non renseigné', '', 0, 0 WHERE 0 NOT IN (SELECT oid FROM activite[PX].t_medecins_administratifs);

				DROP TABLE IF EXISTS w_medecins;
				CREATE TEMP TABLE w_medecins AS
				SELECT 
					S_O_MEDT.INDI_CODE,
					base.cti_soundex_nom(INDI_NOM) AS NOM_SOUNDEX, 
					base.cti_soundex_nom(INDI_PRNOM) AS PRNOM_SOUNDEX, 
					''::text AS NOM_SOUNDEX8,
					''::text AS PRNOM_SOUNDEX8,
					INDI_NOM AS NOM, 
					INDI_PRNOM AS PRNOM, 
					MEDT_NRPPS, 
					SPPS_CODE,
					COALESCE(t_medecins_administratifs.medecin_id,0) as medecin_id
				FROM prod_anthadine.S_M_INDI JOIN prod_anthadine.S_O_MEDT ON S_O_MEDT.INDI_CODE = S_M_INDI.INDI_CODE
					LEFT JOIN activite[PX].t_medecins_administratifs ON S_O_MEDT.INDI_CODE = t_medecins_administratifs.code_original
				;
				
				UPDATE w_medecins
				SET 
					NOM_SOUNDEX8 = trim(substr(NOM_SOUNDEX,1,8)),
					PRNOM_SOUNDEX8 = trim(substr(PRNOM_SOUNDEX,1,8))
				;
			

				INSERT INTO base.t_medecins(code, nom, prenom, numero_ordre, specialite_id)
				SELECT w_medecins.INDI_CODE, w_medecins.NOM, w_medecins.PRNOM, w_medecins.MEDT_NRPPS, 0 
				FROM w_medecins
				WHERE trim(w_medecins.NOM_SOUNDEX8 || ',' || w_medecins.PRNOM_SOUNDEX8 || ',' ||  w_medecins.MEDT_NRPPS) NOT IN 
					(SELECT trim(substr(base.cti_soundex_nom(nom),1,8)  || ',' || substr(base.cti_soundex_nom(prenom),1,8) || ',' || numero_ordre) FROM base.t_medecins WHERE nom IS NOT NULL AND prenom IS NOT NULL AND numero_ordre IS NOT NULL);
				;


				INSERT INTO base.t_medecins(code, nom, prenom, numero_ordre, specialite_id)
				SELECT w_medecins.INDI_CODE, w_medecins.NOM, w_medecins.PRNOM, w_medecins.MEDT_NRPPS, 0 
				FROM w_medecins
				WHERE w_medecins.NOM_SOUNDEX || ',' || w_medecins.PRNOM_SOUNDEX NOT IN 
					(SELECT base.cti_soundex_nom(nom)  || ',' || base.cti_soundex_nom(prenom) FROM base.t_medecins WHERE nom IS NOT NULL AND prenom IS NOT NULL);


				UPDATE w_medecins 
				SET medecin_id = t_medecins.oid
				FROM base.t_medecins
				WHERE NOM_SOUNDEX8 = trim(substr(base.cti_soundex_nom(t_medecins.nom),1,8)) AND
					PRNOM_SOUNDEX8 = trim(substr(base.cti_soundex_nom(t_medecins.prenom),1,8)) AND
					numero_ordre = MEDT_NRPPS
				;

				UPDATE w_medecins 
				SET medecin_id = t_medecins.oid
				FROM base.t_medecins
				WHERE NOM_SOUNDEX = base.cti_soundex_nom(t_medecins.nom) AND
					PRNOM_SOUNDEX = base.cti_soundex_nom(t_medecins.prenom) AND
					medecin_id = 0
				;


				INSERT INTO activite[PX].t_medecins_administratifs(code_original, code, nom, prenom, specialite_id,  medecin_id)
				SELECT DISTINCT INDI_CODE, substr(translate(NOM,' -=()''',''),1,5) || substr(translate(PRNOM,' -=()''',''),1,2), NOM, PRNOM, COALESCE(t_specialites_medecin.oid,0), medecin_id
				FROM w_medecins
					LEFT JOIN base.t_specialites_medecin ON SPPS_CODE = t_specialites_medecin.code_original
				WHERE INDI_CODE NOT IN (SELECT code_original FROM activite[PX].t_medecins_administratifs);

				UPDATE activite[PX].t_medecins_administratifs
				SET nom = w_medecins.NOM,
					prenom = PRNOM,
					specialite_id = COALESCE(t_specialites_medecin.oid,0),
					medecin_id = w_medecins.medecin_id
				FROM w_medecins LEFT JOIN base.t_specialites_medecin ON SPPS_CODE = t_specialites_medecin.code_original
				WHERE w_medecins.INDI_CODE = t_medecins_administratifs.code_original
					AND (
					t_medecins_administratifs.nom <> w_medecins.NOM OR
					t_medecins_administratifs.prenom <> PRNOM OR
					t_medecins_administratifs.specialite_id <> COALESCE(t_specialites_medecin.oid,0) OR
					t_medecins_administratifs.medecin_id <> w_medecins.medecin_id
				);
				

				INSERT INTO activite[PX].t_medecins_traitants_administratifs(code_original, code, nom, prenom, specialite_id,  medecin_id)
				SELECT DISTINCT INDI_CODE, substr(translate(NOM,' -=()''',''),1,5) || substr(translate(PRNOM,' -=()''',''),1,2), NOM, PRNOM, COALESCE(t_specialites_medecin.oid,0), medecin_id
				FROM w_medecins
					LEFT JOIN base.t_specialites_medecin ON SPPS_CODE = t_specialites_medecin.code_original
				WHERE INDI_CODE NOT IN (SELECT code_original FROM activite[PX].t_medecins_traitants_administratifs);

				UPDATE activite[PX].t_medecins_traitants_administratifs
				SET nom = w_medecins.NOM,
					prenom = PRNOM,
					specialite_id = COALESCE(t_specialites_medecin.oid,0),
					medecin_id = w_medecins.medecin_id
				FROM w_medecins LEFT JOIN base.t_specialites_medecin ON SPPS_CODE = t_specialites_medecin.code_original
				WHERE w_medecins.INDI_CODE = t_medecins_traitants_administratifs.code_original
					AND (
					t_medecins_traitants_administratifs.nom <> w_medecins.NOM OR
					t_medecins_traitants_administratifs.prenom <> PRNOM OR
					t_medecins_traitants_administratifs.specialite_id <> COALESCE(t_specialites_medecin.oid,0) OR
					t_medecins_traitants_administratifs.medecin_id <> w_medecins.medecin_id
				);

				UPDATE base.t_medecins SET 
					code = CASE WHEN t_medecins.code <> '' THEN t_medecins.code ELSE subview.code END,
					specialite_id = CASE WHEN t_medecins.specialite_id <> 0 THEN  t_medecins.specialite_id ELSE subview.specialite_id END
				FROM 
					(
					SELECT t_medecins.oid, 
						MAX(t_medecins_administratifs.code) AS code,
						MIN(t_medecins_administratifs.specialite_id) as specialite_id
					FROM base.t_medecins
					JOIN activite[PX].t_medecins_administratifs ON t_medecins.oid = t_medecins_administratifs.medecin_id
					JOIN base.t_specialites_medecin ON t_medecins_administratifs.specialite_id = t_specialites_medecin.oid 
					GROUP BY t_medecins.oid
					) subview 
				WHERE t_medecins.oid = subview.oid AND 
					t_medecins.oid <> 0 AND 
					(
					t_medecins.specialite_id = 0 OR
					t_medecins.code = '' OR
					t_medecins.code IS NULL
					)
				;					
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Hébergement">
			<sqlcmd><![CDATA[
				
				-- Services
				
				INSERT INTO activite[PX].t_services_facturation(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '****', 'Non renseigné', 'Non renseigné' WHERE 0 NOT IN (SELECt oid FROM activite[PX].t_services_facturation);

				
				
				INSERT INTO activite[PX].t_services_facturation(code_original, code, texte, texte_court, nb_lits, type_t2a)
				SELECT SERV_CODE, SERV_CODE, SERV_NOM, LEFT(SERV_NOM,50), 0 AS nb_lits, '0' AS type_t2a
				FROM prod_anthadine.S_S_SERV
				WHERE SERV_CODE NOT IN (SELECT code_original FROM activite[PX].t_services_facturation WHERE code_original IS NOT NULL)
				ORDER BY SERV_CODE;
				

				UPDATE activite[PX].t_services_facturation SET
					texte = SERV_NOM,
					texte_court = LEFT(SERV_NOM,50)
				FROM prod_anthadine.S_S_SERV
				WHERE IDENT = code_original AND
					(
						texte IS DISTINCT FROM SERV_NOM
					);
				
				UPDATE activite[PX].t_services_facturation
				SET mode_traitement_id = COALESCE(t_modes_traitement.oid,0)
				FROM base.t_modes_traitement 
				WHERE t_modes_traitement.code = '06';
				
				UPDATE activite[PX].t_services_facturation
				SET dmt_id = COALESCE(t_dmt.oid,0)
				FROM prod_anthadine.S_S_SERV 
					LEFT JOIN base.t_dmt ON t_dmt.oid != 0 AND LPAD(SERV_DMT,3,0) = t_dmt.code
				WHERE t_services_facturation.code_original = IDENT AND 
					COALESCE(dmt_id,0) = 0 AND
					COALESCE(dmt_id,0) <> COALESCE(t_dmt.oid,0);
					
				
				-- Unités fonctionnelles

				INSERT INTO activite[PX].t_unites_fonctionnelles(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '', 'Non renseignée', 'Non renseignée' WHERE 0 NOT IN (SELECt oid FROM activite[PX].t_unites_fonctionnelles);

				INSERT INTO activite[PX].t_unites_fonctionnelles(code_original, code, texte, texte_court) 
				SELECT UO_CODE, UO_CODE, UO_LIBE, LEFT(UO_LIBE,50)
				FROM prod_anthadine.C_S_UO
				LEFT JOIN activite[PX].t_unites_fonctionnelles ON UO_CODE = code_original
				WHERE UOTYPE_CODE = 2 AND
					t_unites_fonctionnelles.oid IS NULL
				;
				
				-- Unités médicales

				INSERT INTO activite[PX].t_unites_medicales(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '', 'Non renseignée', 'Non renseignée' WHERE 0 NOT IN (SELECt oid FROM activite[PX].t_unites_medicales);

				

				-- Activités

				INSERT INTO activite[PX].t_activites(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '', 'Non renseignée', 'Non renseignée' WHERE 0 NOT IN (SELECt oid FROM activite[PX].t_activites);

				INSERT INTO activite[PX].t_activites(code_original, code, texte, texte_court) 
				SELECT UO_CODE, UO_CODE, UO_LIBE, LEFT(UO_LIBE,50)
				FROM prod_anthadine.C_S_UO
				LEFT JOIN activite[PX].t_activites ON UO_CODE = code_original
				WHERE UOTYPE_CODE = 1  AND
					t_activites.oid IS NULL
				;
				
				-- Etages et lits
				
				INSERT INTO activite[PX].t_etages(oid, code_original, code, texte, texte_court, nb_lits) 
				SELECT 0, 0, '', 'Non renseigné', 'Non renseigné', 0 WHERE 0 NOT IN (SELECT oid FROM activite[PX].t_etages);
				
				INSERT INTO activite[PX].t_lits(oid, code_original, code, texte, texte_court, etage_id, chambre_particuliere) 
				SELECT 0, 0, '', 'Non renseigné', 'Non renseigné', 0, 'N' WHERE 0 NOT IN (SELECT oid FROM activite[PX].t_lits);
					
			
			]]></sqlcmd>
		</NODE>
				
		<NODE label="Facturation">
			<sqlcmd><![CDATA[
				
				-- Rubriques et prestations

				INSERT INTO activite[PX].t_rubriques_facturation(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '', 'Non renseignée', 'Non renseignée' WHERE 0 NOT IN (SELECT oid FROM activite[PX].t_rubriques_facturation)
				;

				DROP TABLE IF EXISTS w_S_N_VCAL;
				CREATE TEMP TABLE w_S_N_VCAL AS
				SELECT *,
					'VCAL'||VCAL_CODE AS rubrique_code_original,
					CASE 
					WHEN PRES_CODE <> '' THEN PRES_CODE
					ELSE CPRE_CODE END AS rubrique_code,
					CASE
					WHEN PRES_CODE <> '' THEN PRES_CODE
					ELSE CPRE_CODE END AS prestation_code,
					0::bigint AS rubrique_facturation_id,
					0::bigint AS prestation_id,
					0::bigint AS acte_id
				FROM prod_anthadine.S_N_VCAL
				ORDER BY PRES_CODE, CPRE_CODE
				;

				UPDATE w_S_N_VCAL
				SET rubrique_code = rubrique_code || '-' || VCAL_CODE
				WHERE rubrique_code IN
					(SELECt rubrique_code FROM w_S_N_VCAL GROUp BY 1 HAVING count(*) > 1)
				;




				INSERT INTO activite[PX].t_rubriques_facturation(code_original, code, texte, texte_court)
				SELECT 
					rubrique_code_original,
					rubrique_code,
					VCAL_LIBEL,
					VCAL_LIBEL
				FROM w_S_N_VCAL
				WHERE rubrique_code_original NOT IN (SELECT code_original FROM activite[PX].t_rubriques_facturation WHERE code_original IS NOT NULL)
				;

				UPDATE w_S_N_VCAL
				SET rubrique_facturation_id = t_rubriques_facturation.oid
				FROM activite[PX].t_rubriques_facturation
				WHERE t_rubriques_facturation.code_original = rubrique_code_original
				;


				INSERT INTO activite.t_prestations(code_original, code, texte, texte_court)
				SELECT 
					prestation_code,
					prestation_code,
					prestation_code,
					prestation_code
				FROM w_S_N_VCAL
				WHERE prestation_code NOT IN (SELECT code FROM activite.t_prestations WHERE code IS NOT NULL) AND
					prestation_code <> ''
				GROUP BY 1
				;

				UPDATE w_S_N_VCAL
				SET prestation_id = t_prestations.oid
				FROM activite.t_prestations
				WHERE t_prestations.code = prestation_code
				;


				INSERT INTO base.t_actes(code, texte, texte_court, nomenclature)
				SELECT 
					prestation_code,
					prestation_code,
					prestation_code,
					'NGAP'
				FROM w_S_N_VCAL
				WHERE prestation_code NOT IN (SELECT code FROM base.t_actes WHERE nomenclature = 'NGAP') AND
					prestation_code <> ''
				GROUP BY 1
				;

				UPDATE w_S_N_VCAL
				SET acte_id = t_actes.oid
				FROM base.t_actes
				WHERE t_actes.code = prestation_code
				;
				
				ANALYSE w_S_N_VCAL
				;
				
				INSERT INTO activite[PX].t_rubriques_facturation(code_original, code, texte, texte_court)
				SELECT 
					LGHT_CODE,
					LGHT_CODE,
					LGHT_LIBE,
					LEFT(LGHT_LIBE,50)
				FROM prod_anthadine.A_C_LGHT
				WHERE LGHT_CODE NOT IN (SELECT code_original FROM activite[PX].t_rubriques_facturation WHERE code IS NOT NULL)
				;
				
				INSERT INTO activite[PX].t_rubriques_facturation(code_original, code, texte, texte_court)
				SELECT 
					'MO'::text,
					'MO'::text,
					'Molecules Onereuses'::text,
					'Molecules Onereuses'::text
				WHERE 'MO'::text NOT IN (SELECT code_original FROM activite[PX].t_rubriques_facturation WHERE code_original IS NOT NULL)
				;


				INSERT INTO activite.t_prestations(oid, code_original, code, texte, texte_court, clinique_honoraire) 
				SELECT 0, 0, '', 'Non renseignée', 'Non renseignée', '' WHERE 0 NOT IN (SELECT oid FROM activite.t_prestations);
				
				
				INSERT INTO activite.t_prestations(code_original, code, texte, texte_court)
				SELECT 
					'MO'::text,
					'MO'::text,
					'Molecules Onereuses'::text,
					'Molecules Onereuses'::text
				WHERE 'MO'::text NOT IN (SELECT code FROM activite.t_prestations WHERE code IS NOT NULL)
				;

				INSERT INTO activite.t_prestations(code_original, code, texte, texte_court)
				SELECT 'CTI_GHT', 'GHT', 'Groupe homogène de tarifs', 'Groupe homogène de tarifs'
				WHERE 'GHT' NOT IN (SELECT code FROM activite[PX].t_prestations);
				
				INSERT INTO activite[PX].t_compte(oid, code_original, code, texte, texte_court) 
				SELECT 0, 0, '', 'Non renseigné', 'Non renseigné' WHERE 0 NOT IN (SELECt oid FROM activite[PX].t_compte);
										
			]]></sqlcmd>
		</NODE>
		
	</NODE>
		
	<NODE name="POST" label="TRAITEMENTS COMPLEMENTAIRES">
	
		
	
		<NODE label="Compléments facture">
			<sqlcmd><![CDATA[
							
				
			
				UPDATE activite[PX].p_factures SET date_expedition_0 = date_facture WHERE date_expedition_0 < date_facture AND date_facture <> '2099-12-31';

				
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Compléments séjours">
			<sqlcmd><![CDATA[

				UPDATE activite[PX].p_sejours 
				SET date_groupage = date_facture 
				WHERE (date_groupage IS null OR date_groupage = '20991231') AND 
					date_facture <> '2099-12-31';
				
				
				DROP TABLE IF EXISTS w_sejours_total;
				CREATE TEMP TABLE w_sejours_total AS
				SELECT   
					p_sejours.no_sejour AS no_sejour, 
					p_sejours.code_sorti,
					p_sejours.date_sortie,
					p_sejours.date_groupage,   
					p_sejours.ghs_id,
	
					MAX(COALESCE(p_factures.date_facture,'20991231')) AS date_facture,     
					MAX(COALESCE(p_factures.date_expedition,'20991231')) AS date_expedition,  
					MAX(COALESCE(p_factures.date_solde,'20991231')) AS date_solde,  
					SUM(COALESCE(p_factures.montant_facture_c,0)) AS montant_facture_c,  
					SUM(COALESCE(p_factures.montant_facture_h,0)) AS montant_facture_h,  
					SUM(COALESCE(p_factures.montant_regle_c,0)) AS montant_regle_c,  
					SUM(COALESCE(p_factures.montant_regle_h,0)) AS montant_regle_h,  
					SUM(COALESCE(CASE WHEN p_factures.type_facture <> 'P' AND p_factures.type_facture <> 'E' AND p_factures.type_facture <> 'G' THEN 1 ELSE 0 END,0)) AS nb_factures,  
					SUM(COALESCE(CASE WHEN p_factures.type_facture = '0' THEN 0 ELSE 1 END,0)) AS nb_factures_regularisation,  
					SUM(COALESCE(p_factures.nb_rejets,0)) AS nb_rejets,  

					SUM(COALESCE(p_factures.montant_facture_0_c,0)) AS montant_facture_0_c,  
					SUM(COALESCE(p_factures.montant_facture_0_h,0)) AS montant_facture_0_h,  
					SUM(COALESCE(p_factures.montant_regle_0_c,0)) AS montant_regle_0_c,  
					SUM(COALESCE(p_factures.montant_regle_0_h,0)) AS montant_regle_0_h,  
					MAX(COALESCE(CASE WHEN (p_factures.montant_facture_0_c <> 0 OR p_factures.montant_facture_0_h <> 0)  THEN p_factures.date_expedition_0 ELSE '0001-01-01' END,'20991231' )) AS date_expedition_0,   
					MAX(COALESCE(CASE WHEN p_factures.date_solde_0_c < '2099-12-31' AND p_factures.date_solde_0_c > p_factures.date_solde_0_h THEN p_factures.date_solde_0_c ELSE p_factures.date_solde_0_h END,'20991231' )) AS date_solde_0,  
					

					SUM(COALESCE(p_factures.montant_facture_1_c,0)) AS montant_facture_1_c,  
					SUM(COALESCE(p_factures.montant_facture_1_h,0)) AS montant_facture_1_h,  
					SUM(COALESCE(p_factures.montant_regle_1_c,0)) AS montant_regle_1_c,  
					SUM(COALESCE(p_factures.montant_regle_1_h,0)) AS montant_regle_1_h,  
					MAX(COALESCE(CASE WHEN (p_factures.montant_facture_1_c <> 0 OR p_factures.montant_facture_1_h <> 0)  THEN p_factures.date_expedition_1 ELSE '0001-01-01' END,'20991231' )) AS date_expedition_1,  
					MAX(COALESCE(CASE WHEN p_factures.date_solde_1_c < '2099-12-31' AND p_factures.date_solde_1_c > p_factures.date_solde_1_h THEN p_factures.date_solde_1_c ELSE p_factures.date_solde_1_h END,'20991231' )) AS date_solde_1,  

					SUM(COALESCE(p_factures.montant_facture_2_c,0)) AS montant_facture_2_c,  
					SUM(COALESCE(p_factures.montant_facture_2_h,0)) AS montant_facture_2_h,  
					SUM(COALESCE(p_factures.montant_regle_2_c,0)) AS montant_regle_2_c,  
					SUM(COALESCE(p_factures.montant_regle_2_h,0)) AS montant_regle_2_h,  
					MAX(COALESCE(CASE WHEN (p_factures.montant_facture_2_c <> 0 OR p_factures.montant_facture_2_h <> 0)  THEN p_factures.date_expedition_2 ELSE '0001-01-01' END,'20991231' )) AS date_expedition_2,  
					MAX(COALESCE(CASE WHEN p_factures.date_solde_2_c < '2099-12-31' AND p_factures.date_solde_2_c > p_factures.date_solde_2_h THEN p_factures.date_solde_2_c ELSE p_factures.date_solde_2_h END,'20991231' )) AS date_solde_2,  

					SUM(COALESCE(p_factures.montant_facture_22_c,0)) AS montant_facture_22_c,  
					SUM(COALESCE(p_factures.montant_facture_22_h,0)) AS montant_facture_22_h,  
					SUM(COALESCE(p_factures.montant_regle_22_c,0)) AS montant_regle_22_c,  
					SUM(COALESCE(p_factures.montant_regle_22_h,0)) AS montant_regle_22_h,  
					MAX(COALESCE(CASE WHEN (p_factures.montant_facture_22_c <> 0 OR p_factures.montant_facture_22_h <> 0)  THEN p_factures.date_expedition_22 ELSE '0001-01-01' END,'20991231' )) AS date_expedition_22,  
					MAX(COALESCE(CASE WHEN p_factures.date_solde_22_c < '2099-12-31' AND p_factures.date_solde_22_c > p_factures.date_solde_22_h THEN p_factures.date_solde_22_c ELSE p_factures.date_solde_22_h END,'20991231' )) AS date_solde_22 , 

					
					SUM(COALESCE(p_factures.montant_comptabilise_c,0)) AS montant_comptabilise_c,  
					SUM(COALESCE(p_factures.montant_comptabilise_h,0)) AS montant_comptabilise_h,  
					SUM(COALESCE(p_factures.montant_comptabilise_0_c,0)) AS montant_comptabilise_0_c,  
					SUM(COALESCE(p_factures.montant_comptabilise_0_h,0)) AS montant_comptabilise_0_h,  
					SUM(COALESCE(p_factures.montant_comptabilise_1_c,0)) AS montant_comptabilise_1_c,  
					SUM(COALESCE(p_factures.montant_comptabilise_1_h,0)) AS montant_comptabilise_1_h,  
					SUM(COALESCE(p_factures.montant_comptabilise_2_c,0)) AS montant_comptabilise_2_c,  
					SUM(COALESCE(p_factures.montant_comptabilise_2_h,0)) AS montant_comptabilise_2_h,  
					SUM(COALESCE(p_factures.montant_comptabilise_22_c,0)) AS montant_comptabilise_22_c,  
					SUM(COALESCE(p_factures.montant_comptabilise_22_h,0)) AS montant_comptabilise_22_h,

					SUM(COALESCE(p_factures.montant_comptabilise_budget_global_c,0)) AS montant_comptabilise_budget_global_c,  

					SUM(COALESCE(p_factures.montant_facture_c_actes_inclus_dans_sejour,0)) AS montant_facture_c_actes_inclus_dans_sejour,  
					SUM(COALESCE(p_factures.montant_facture_h_actes_inclus_dans_sejour,0)) AS montant_facture_h_actes_inclus_dans_sejour,

					0::numeric AS delai_groupage, 
					0::numeric AS delai_facture, 
					0::numeric AS delai_expedition, 
					0::numeric AS delai_solde, 
					0::numeric AS delai_expedition_0, 
					0::numeric AS delai_solde_0, 
					0::numeric AS delai_expedition_1, 
					0::numeric AS delai_solde_1, 
					0::numeric AS delai_expedition_2, 
					0::numeric AS delai_solde_2, 
					0::numeric AS delai_expedition_22, 
					0::numeric AS delai_solde_22
				
				FROM activite[PX].p_sejours LEFT JOIN activite[PX].p_factures ON p_sejours.no_sejour = p_factures.no_sejour AND type_facture <> 'P'
				GROUP BY 1,2,3,4,5;
				
				
				UPDATE w_sejours_total
				SET date_groupage = date_facture 
				WHERE (date_groupage IS null OR date_groupage = '20991231' OR date_groupage = '00010101') AND 
					date_facture <> '2099-12-31';


				UPDATE w_sejours_total SET  
					date_groupage = CASE WHEN date_groupage > '00010101' THEN date_groupage ELSE '20991231' END,
					date_facture = CASE WHEN date_facture> '00010101' THEN date_facture ELSE '20991231' END,
					date_expedition = CASE WHEN date_expedition > '00010101' THEN date_expedition ELSE '20991231' END,
					date_solde = CASE WHEN date_solde > '00010101' THEN date_solde ELSE '20991231' END,
					date_expedition_0 = CASE WHEN date_expedition_0 > '00010101' THEN date_expedition_0 ELSE '20991231' END,
					date_solde_0 = CASE WHEN date_solde_0 > '00010101' THEN date_solde_0 ELSE '20991231' END,
					date_expedition_1 = CASE WHEN date_expedition_1 > '00010101' THEN date_expedition_1 ELSE '20991231' END,
					date_solde_1 = CASE WHEN date_solde_1 > '00010101' THEN date_solde_1 ELSE '20991231' END,
					date_expedition_2 = CASE WHEN date_expedition_2 > '00010101' THEN date_expedition_2 ELSE '20991231' END,
					date_solde_2 = CASE WHEN date_solde_2 > '00010101' THEN date_solde_2 ELSE '20991231' END,
					date_expedition_22 = CASE WHEN date_expedition_22 > '00010101' THEN date_expedition_22 ELSE '20991231' END,
					date_solde_22 = CASE WHEN date_solde_22 > '00010101' THEN date_solde_22 ELSE '20991231' END;

					
				UPDATE w_sejours_total SET  
					delai_groupage = CASE WHEN code_sorti = '1' AND date_groupage > date_sortie AND date_groupage < '2099-12-31' AND ghs_id <> 0
								THEN date_groupage  - date_sortie ELSE 0 END, 
					delai_facture = CASE WHEN code_sorti = '1' AND date_facture  > date_sortie AND date_facture < '2099-12-31' AND date_facture > date_sortie  
								THEN date_facture  - date_sortie ELSE 0 END, 
					delai_expedition = CASE WHEN code_sorti = '1' AND date_expedition  > date_sortie AND date_facture < '2099-12-31' AND date_expedition < '2099-12-31'  
								THEN date_expedition  - date_sortie ELSE 0 END, 
					delai_solde = CASE WHEN code_sorti = '1' AND date_solde > date_sortie AND date_facture < '2099-12-31' AND date_solde < '2099-12-31'  
								THEN date_solde - date_sortie ELSE 0 END, 
					delai_expedition_0 = CASE WHEN code_sorti = '1' AND date_expedition_0 > date_sortie AND date_facture < '2099-12-31' AND date_expedition_0 < '2099-12-31'  
								THEN date_expedition_0  - date_sortie ELSE 0 END, 
					delai_solde_0 = CASE WHEN code_sorti = '1'AND date_solde_0 > date_sortie AND date_facture < '2099-12-31' AND date_solde_0 < '2099-12-31'  
								THEN date_solde_0 - date_sortie ELSE 0 END, 
					delai_expedition_1 = CASE WHEN code_sorti = '1' AND date_expedition_1 > date_sortie AND date_facture < '2099-12-31' AND date_expedition_1 < '2099-12-31'  
								THEN date_expedition_1  - date_sortie ELSE 0 END, 
					delai_solde_1 = CASE WHEN code_sorti = '1' AND date_solde_1 > date_sortie AND date_facture < '2099-12-31' AND date_solde_1 < '2099-12-31'  
								THEN date_solde_1 - date_sortie ELSE 0 END, 
					delai_expedition_2 = CASE WHEN code_sorti = '1' AND date_expedition_2 > date_sortie AND date_facture < '2099-12-31' AND date_expedition_2 < '2099-12-31'  
								THEN date_expedition_2  - date_sortie ELSE 0 END, 
					delai_solde_2 = CASE WHEN code_sorti = '1' AND date_solde_2 > date_sortie AND date_facture < '2099-12-31' AND date_solde_2 < '2099-12-31'  
								THEN date_solde_2 - date_sortie ELSE 0 END, 
					delai_expedition_22 = CASE WHEN code_sorti = '1' AND date_expedition_22 > date_sortie AND date_facture < '2099-12-31' AND date_expedition_22 < '2099-12-31'  
								THEN date_expedition_22  - date_sortie ELSE 0 END, 
					delai_solde_22 = CASE WHEN code_sorti = '1' AND date_solde_22 > date_sortie AND date_facture < '2099-12-31' AND date_solde_22 < '2099-12-31'  
								THEN date_solde_22 - date_sortie ELSE 0 END;

				ALTER TABLE w_sejours_total  ADD CONSTRAINT w_sejours_total_pkey PRIMARY KEY(no_sejour);



				UPDATE activite[PX].p_sejours SET

					date_facture = w_sejours_total.date_facture,  
					date_expedition = w_sejours_total.date_expedition,  
					date_solde = w_sejours_total.date_solde,  
					montant_facture_c = w_sejours_total.montant_facture_c,  
					montant_facture_h = w_sejours_total.montant_facture_h,  
					montant_regle_c = w_sejours_total.montant_regle_c,  
					montant_regle_h = w_sejours_total.montant_regle_h,  
					nb_factures = w_sejours_total.nb_factures,  
					nb_factures_regularisation = w_sejours_total.nb_factures_regularisation,  
					nb_factures_rejet = w_sejours_total.nb_rejets,  

					montant_facture_0_c = w_sejours_total.montant_facture_0_c,  
					montant_facture_0_h = w_sejours_total.montant_facture_0_h,  
					montant_regle_0_c = w_sejours_total.montant_regle_0_c,  
					montant_regle_0_h = w_sejours_total.montant_regle_0_h,  
					date_expedition_0 = w_sejours_total.date_expedition_0,  
					date_solde_0 = w_sejours_total.date_solde_0,  

					montant_facture_1_c = w_sejours_total.montant_facture_1_c,  
					montant_facture_1_h = w_sejours_total.montant_facture_1_h,  
					montant_regle_1_c = w_sejours_total.montant_regle_1_c,  
					montant_regle_1_h = w_sejours_total.montant_regle_1_h,  
					date_expedition_1 = w_sejours_total.date_expedition_1,   
					date_solde_1 = w_sejours_total.date_solde_1,  
		 
					montant_facture_2_c = w_sejours_total.montant_facture_2_c,  
					montant_facture_2_h = w_sejours_total.montant_facture_2_h,  
					montant_regle_2_c = w_sejours_total.montant_regle_2_c,  
					montant_regle_2_h = w_sejours_total.montant_regle_2_h,  
					date_expedition_2 = w_sejours_total.date_expedition_2, 
					date_solde_2 = w_sejours_total.date_solde_2,  
		 
					montant_facture_22_c = w_sejours_total.montant_facture_22_c,  
					montant_facture_22_h = w_sejours_total.montant_facture_22_h,  
					montant_regle_22_c = w_sejours_total.montant_regle_22_c,  
					montant_regle_22_h = w_sejours_total.montant_regle_22_h,  
					date_expedition_22 = w_sejours_total.date_expedition_22, 
					date_solde_22 = w_sejours_total.date_solde_22,
					
					montant_comptabilise_c = w_sejours_total.montant_comptabilise_c,  
					montant_comptabilise_h = w_sejours_total.montant_comptabilise_h,  
					montant_comptabilise_0_c = w_sejours_total.montant_comptabilise_0_c,  
					montant_comptabilise_0_h = w_sejours_total.montant_comptabilise_0_h,  
					montant_comptabilise_1_c = w_sejours_total.montant_comptabilise_1_c,  
					montant_comptabilise_1_h = w_sejours_total.montant_comptabilise_1_h,  
					montant_comptabilise_2_c = w_sejours_total.montant_comptabilise_2_c,  
					montant_comptabilise_2_h = w_sejours_total.montant_comptabilise_2_h,  
					montant_comptabilise_22_c = w_sejours_total.montant_comptabilise_22_c,  
					montant_comptabilise_22_h = w_sejours_total.montant_comptabilise_22_h,

					montant_comptabilise_budget_global_c = w_sejours_total.montant_comptabilise_budget_global_c,

					montant_facture_c_actes_inclus_dans_sejour = w_sejours_total.montant_facture_c_actes_inclus_dans_sejour,
					montant_facture_h_actes_inclus_dans_sejour = w_sejours_total.montant_facture_h_actes_inclus_dans_sejour,


					delai_groupage = w_sejours_total.delai_groupage, 
					delai_facture = w_sejours_total.delai_facture, 
					delai_expedition = w_sejours_total.delai_expedition, 
					delai_solde = w_sejours_total.delai_solde, 
					delai_expedition_0 = w_sejours_total.delai_expedition_0, 
					delai_solde_0 = w_sejours_total.delai_solde_0, 
					delai_expedition_1 = w_sejours_total.delai_expedition_1, 
					delai_solde_1 = w_sejours_total.delai_solde_1, 
					delai_expedition_2 = w_sejours_total.delai_expedition_2, 
					delai_solde_2 = w_sejours_total.delai_solde_2, 
					delai_expedition_22 = w_sejours_total.delai_expedition_22, 
					delai_solde_22 = w_sejours_total.delai_solde_22
					
				FROM w_sejours_total
				WHERE w_sejours_total.no_sejour =  p_sejours.no_sejour

				AND (
					p_sejours.date_facture IS DISTINCT FROM w_sejours_total.date_facture OR   
					p_sejours.date_expedition IS DISTINCT FROM w_sejours_total.date_expedition OR   
					p_sejours.date_solde IS DISTINCT FROM w_sejours_total.date_solde OR   
					p_sejours.montant_facture_c IS DISTINCT FROM w_sejours_total.montant_facture_c OR   
					p_sejours.montant_facture_h IS DISTINCT FROM w_sejours_total.montant_facture_h OR   
					p_sejours.montant_regle_c IS DISTINCT FROM w_sejours_total.montant_regle_c OR   
					p_sejours.montant_regle_h IS DISTINCT FROM w_sejours_total.montant_regle_h OR   
					p_sejours.nb_factures IS DISTINCT FROM w_sejours_total.nb_factures OR   
					p_sejours.nb_factures_regularisation IS DISTINCT FROM w_sejours_total.nb_factures_regularisation OR   
					p_sejours.nb_factures_rejet IS DISTINCT FROM w_sejours_total.nb_rejets OR   

					p_sejours.montant_facture_0_c IS DISTINCT FROM w_sejours_total.montant_facture_0_c OR   
					p_sejours.montant_facture_0_h IS DISTINCT FROM w_sejours_total.montant_facture_0_h OR   
					p_sejours.montant_regle_0_c IS DISTINCT FROM w_sejours_total.montant_regle_0_c OR   
					p_sejours.montant_regle_0_h IS DISTINCT FROM w_sejours_total.montant_regle_0_h OR   
					p_sejours.date_expedition_0 IS DISTINCT FROM w_sejours_total.date_expedition_0 OR   
					p_sejours.date_solde_0 IS DISTINCT FROM w_sejours_total.date_solde_0 OR   

					p_sejours.montant_facture_1_c IS DISTINCT FROM w_sejours_total.montant_facture_1_c OR   
					p_sejours.montant_facture_1_h IS DISTINCT FROM w_sejours_total.montant_facture_1_h OR   
					p_sejours.montant_regle_1_c IS DISTINCT FROM w_sejours_total.montant_regle_1_c OR   
					p_sejours.montant_regle_1_h IS DISTINCT FROM w_sejours_total.montant_regle_1_h OR   
					p_sejours.date_expedition_1 IS DISTINCT FROM w_sejours_total.date_expedition_1 OR    
					p_sejours.date_solde_1 IS DISTINCT FROM w_sejours_total.date_solde_1 OR   
		 
					p_sejours.montant_facture_2_c IS DISTINCT FROM w_sejours_total.montant_facture_2_c OR   
					p_sejours.montant_facture_2_h IS DISTINCT FROM w_sejours_total.montant_facture_2_h OR   
					p_sejours.montant_regle_2_c IS DISTINCT FROM w_sejours_total.montant_regle_2_c OR   
					p_sejours.montant_regle_2_h IS DISTINCT FROM w_sejours_total.montant_regle_2_h OR   
					p_sejours.date_expedition_2 IS DISTINCT FROM w_sejours_total.date_expedition_2 OR  
					p_sejours.date_solde_2 IS DISTINCT FROM w_sejours_total.date_solde_2 OR   
		 
					p_sejours.montant_facture_22_c IS DISTINCT FROM w_sejours_total.montant_facture_22_c OR   
					p_sejours.montant_facture_22_h IS DISTINCT FROM w_sejours_total.montant_facture_22_h OR   
					p_sejours.montant_regle_22_c IS DISTINCT FROM w_sejours_total.montant_regle_22_c OR   
					p_sejours.montant_regle_22_h IS DISTINCT FROM w_sejours_total.montant_regle_22_h OR   
					p_sejours.date_expedition_22 IS DISTINCT FROM w_sejours_total.date_expedition_22 OR  
					p_sejours.date_solde_22 IS DISTINCT FROM w_sejours_total.date_solde_22 OR 
					
					p_sejours.montant_comptabilise_c IS DISTINCT FROM w_sejours_total.montant_comptabilise_c OR   
					p_sejours.montant_comptabilise_h IS DISTINCT FROM w_sejours_total.montant_comptabilise_h OR   
					p_sejours.montant_comptabilise_0_c IS DISTINCT FROM w_sejours_total.montant_comptabilise_0_c OR   
					p_sejours.montant_comptabilise_0_h IS DISTINCT FROM w_sejours_total.montant_comptabilise_0_h OR   
					p_sejours.montant_comptabilise_1_c IS DISTINCT FROM w_sejours_total.montant_comptabilise_1_c OR   
					p_sejours.montant_comptabilise_1_h IS DISTINCT FROM w_sejours_total.montant_comptabilise_1_h OR   
					p_sejours.montant_comptabilise_2_c IS DISTINCT FROM w_sejours_total.montant_comptabilise_2_c OR   
					p_sejours.montant_comptabilise_2_h IS DISTINCT FROM w_sejours_total.montant_comptabilise_2_h OR   
					p_sejours.montant_comptabilise_22_c IS DISTINCT FROM w_sejours_total.montant_comptabilise_22_c OR   
					p_sejours.montant_comptabilise_22_h IS DISTINCT FROM w_sejours_total.montant_comptabilise_22_h OR   

					p_sejours.montant_comptabilise_budget_global_c IS DISTINCT FROM w_sejours_total.montant_comptabilise_budget_global_c OR   

					p_sejours.montant_facture_c_actes_inclus_dans_sejour IS DISTINCT FROM w_sejours_total.montant_facture_c_actes_inclus_dans_sejour OR   
					p_sejours.montant_facture_h_actes_inclus_dans_sejour IS DISTINCT FROM w_sejours_total.montant_facture_h_actes_inclus_dans_sejour OR

					p_sejours.delai_groupage IS DISTINCT FROM w_sejours_total.delai_groupage OR   
					p_sejours.delai_facture IS DISTINCT FROM w_sejours_total.delai_facture OR   
					p_sejours.delai_expedition IS DISTINCT FROM w_sejours_total.delai_expedition OR   
					p_sejours.delai_solde IS DISTINCT FROM w_sejours_total.delai_solde OR   
					p_sejours.delai_expedition_0 IS DISTINCT FROM w_sejours_total.delai_expedition_0 OR   
					p_sejours.delai_solde_0 IS DISTINCT FROM w_sejours_total.delai_solde_0 OR   
					p_sejours.delai_expedition_1 IS DISTINCT FROM w_sejours_total.delai_expedition_1 OR   
					p_sejours.delai_solde_1 IS DISTINCT FROM w_sejours_total.delai_solde_1 OR   
					p_sejours.delai_expedition_2 IS DISTINCT FROM w_sejours_total.delai_expedition_2 OR   
					p_sejours.delai_solde_2 IS DISTINCT FROM w_sejours_total.delai_solde_2 OR   
					p_sejours.delai_expedition_22 IS DISTINCT FROM w_sejours_total.delai_expedition_22 OR   
					p_sejours.delai_solde_22 IS DISTINCT FROM w_sejours_total.delai_solde_22

				);
														
										
			]]></sqlcmd>
		</NODE>
		
	
	</NODE>
	
	
</ROOT>
