<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
	
	
	<NODE name="INIT" label="INITIALISATIONS">
	
		<NODE label="Corrections tables">
			<sqlcmd><![CDATA[
			
				-- Tarif MA pour Sainte Marie Cambrai
				UPDATE prod_clinicom.FB_FAC_HISTOLETTRECLE
				SET CLI_DONN_TARIF = 150
				FROM base.t_finess 
				WHERE t_finess.code = '590052056' AND
					CLI_CODE_LETTRECLE = 'MA' AND
					CLI_DONN_TARIF = 0
				;
	 
	
				-- Numéros de factures en double
				UPDATE prod_clinicom.FD_FAC_DETFAC
				SET CLI_DONN_NOOBJETFAC = CLI_DONN_NOOBJETFAC_new
				FROM
				(
					SELECT CLI_DONN_EXERCOMPTA, CLI_DONN_NOOBJETFAC, MAX(FD_FAC_DETFAC.CTID) AS toctid,  CLI_DONN_NOOBJETFAC + round(count(*) / 10.0,1) AS CLI_DONN_NOOBJETFAC_new
					FROM prod_clinicom.FD_FAC_DETFAC JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT
					WHERE CLI_CODE_OBJETFACTURATION = 'T' AND CLI_DONN_NOOBJETFAC <> '0'
					GROUP BY 1,2
					HAVING count(*) > 1
				) subview
				WHERE FD_FAC_DETFAC.CTID = subview.toctid;
				
				-- Numéros de titres à 0
				UPDATE prod_clinicom.FD_FAC_DETFAC
				SET CLI_DONN_NOOBJETFAC = (9 * (SELECT 10 ^ length(max(CLI_DONN_NOOBJETFAC)) FROM prod_clinicom.FD_FAC_DETFAC WHERE CLI_CODE_OBJETFACTURATION = 'T')) + CLI_CODE_NOINTERNEOBJET
				FROM w_ADMISSIONS_select
				WHERE cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT AND
					CLI_CODE_OBJETFACTURATION = 'T' AND CLI_DONN_NOOBJETFAC = '0';
					
					
				-- Lit à la place de chambre dans mouvements
				DROP TABLE IF EXISTS w_MVTS_MICROMOUVEMENTS_lits;
				CREATE TEMP TABLE w_MVTS_MICROMOUVEMENTS_lits AS
				SELECT
					CLI_DONN_NOABSEPISODE, CLI_CODE_BOX, MAX(CLI_CODE_LIT) AS CLI_CODE_LIT
				FROM prod_clinicom.FD_MVTS_MICROMOUVEMENTS JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT
				WHERE CLI_CODE_LIT <> '' AND CLI_CODE_BOX <> ''
				GROUP BY 1,2;

				CREATE INDEX w_MVTS_MICROMOUVEMENTS_lits_i1
					ON w_MVTS_MICROMOUVEMENTS_lits
					USING btree
					(CLI_DONN_NOABSEPISODE);


				UPDATE prod_clinicom.FD_MVTS_MOUVEMENTS
				SET CLI_DONN_CHAMBRE = CLI_CODE_LIT
				FROM w_MVTS_MICROMOUVEMENTS_lits
				WHERE FD_MVTS_MOUVEMENTS.CLI_DONN_NOABSEPISODE = w_MVTS_MICROMOUVEMENTS_lits.CLI_DONN_NOABSEPISODE AND
					CLI_DONN_CHAMBRE = CLI_CODE_BOX
				;
				
				-- Chambre non renseignée (NR)
				UPDATE prod_clinicom.FD_MVTS_MOUVEMENTS
				SET CLI_DONN_CHAMBRE = '' 
				FROM w_ADMISSIONS_select
				WHERE CLI_DONN_CHAMBRE = 'NR' AND
					cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT
				
			]]></sqlcmd>
		</NODE>
	
		<NODE label="Identification des séjours">
			<sqlcmd><![CDATA[

					DROP TABLE IF EXISTS w_MVTS_EPISODES_chambre;
					CREATE TEMP TABLE w_MVTS_EPISODES_chambre AS
					SELECT FD_MVTS_MOUVEMENTS.CLI_DONN_NOABSEPISODE, 
						(MAX(ARRAY[FD_MVTS_MOUVEMENTS.CLI_DATE_MOUVEMENT::text, FD_MVTS_MOUVEMENTS.CLI_CODE_US]))[2] AS CLI_CODE_US, 
						(MAX(ARRAY[FD_MVTS_MOUVEMENTS.CLI_DATE_MOUVEMENT::text, FD_MVTS_MOUVEMENTS.CLI_DONN_CHAMBRE]))[2] AS CLI_DONN_CHAMBRE
					FROM prod_clinicom.FD_MVTS_MOUVEMENTS JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT
					WHERE CLI_DONN_CHAMBRE <> ''
					GROUP BY 1;

					ALTER TABLE w_MVTS_EPISODES_chambre ADD CONSTRAINT w_MVTS_EPISODES_chambre_pkey PRIMARY KEY(CLI_DONN_NOABSEPISODE);
					
					DROP TABLE IF EXISTS w_MVTS_EPISODES_medecinpmsi;
					CREATE TEMP TABLE w_MVTS_EPISODES_medecinpmsi AS
					SELECT FD_PMSI_RSS.CLI_DONN_NOABSEPISODE, (MAX(Array[CLI_Date_SortieUM::text,FD_PMSI_RUM.CLI_Code_MedecinResponsable]))[2] AS CLI_CODE_MEDECINPMSI
					FROM prod_clinicom.FD_PMSI_RSS JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT
					JOIN prod_clinicom.FD_PMSI_RUM ON FD_PMSI_RSS.CLI_Donn_NoRSS = FD_PMSI_RUM.CLI_Donn_NoRSS AND 
							FD_PMSI_RSS.CLI_Donn_NoAbsEpisode = FD_PMSI_RUM.CLI_Donn_NoAbsEpisode
					WHERE (FD_PMSI_RUM.CLI_Code_MedecinResponsable <> '' AND FD_PMSI_RUM.CLI_Code_MedecinResponsable <> 'NR')
					GROUP BY 1;
					
					ALTER TABLE w_MVTS_EPISODES_medecinpmsi ADD CONSTRAINT w_MVTS_EPISODES_medecinpmsi_pkey PRIMARY KEY(CLI_DONN_NOABSEPISODE);
					
					UPDATE prod_clinicom.FD_MVTS_EPISODES
					SET CLI_CODE_MEDECINRESPONSABLE = CLI_CODE_MEDECINPMSI
					FROM w_MVTS_EPISODES_medecinpmsi
					WHERE w_MVTS_EPISODES_medecinpmsi.CLI_DONN_NOABSEPISODE = FD_MVTS_EPISODES.CLI_DONN_NOABSEPISODE AND
					(CLI_CODE_MEDECINRESPONSABLE = '' OR CLI_CODE_MEDECINRESPONSABLE = 'NR');

					DROP TABLE IF EXISTS w_MVTS_EPISODES_medecin;
					CREATE TEMP TABLE w_MVTS_EPISODES_medecin AS
					SELECT FD_ACTES_ENTETE.CLI_DONN_NOABSEPISODE, 
						(MAX(Array[FD_ACTES_ENTETE.CLI_DATE_SAISIE::text, FD_ACTES_ENTETE.CLI_CODE_MEDECIN1]))[2] AS CLI_CODE_MEDECIN
					FROM prod_clinicom.FD_ACTES_ENTETE JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT
						JOIN prod_clinicom.FD_MVTS_EPISODES ON FD_ACTES_ENTETE.CLI_DONN_NOABSEPISODE = FD_MVTS_EPISODES.CLI_DONN_NOABSEPISODE AND 
							(CLI_CODE_MEDECINRESPONSABLE = '' OR CLI_CODE_MEDECINRESPONSABLE = 'NR')
					WHERE (FD_ACTES_ENTETE.CLI_CODE_MEDECIN1 <> '' AND FD_ACTES_ENTETE.CLI_CODE_MEDECIN1 <> 'NR')
					GROUP BY 1;

					ALTER TABLE w_MVTS_EPISODES_medecin ADD CONSTRAINT w_MVTS_EPISODES_medecin_pkey PRIMARY KEY(CLI_DONN_NOABSEPISODE);
					
					UPDATE prod_clinicom.FD_MVTS_EPISODES
					SET CLI_CODE_MEDECINRESPONSABLE = CLI_CODE_MEDECIN
					FROM w_MVTS_EPISODES_medecin
					WHERE w_MVTS_EPISODES_medecin.CLI_DONN_NOABSEPISODE = FD_MVTS_EPISODES.CLI_DONN_NOABSEPISODE AND
					(CLI_CODE_MEDECINRESPONSABLE = '' OR CLI_CODE_MEDECINRESPONSABLE = 'NR');
					
					UPDATE prod_clinicom.FD_MVTS_EPISODES
					SET CLI_CODE_MEDECINRESPONSABLE = ''
					FROM w_ADMISSIONS_select
					WHERE cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT AND
						CLI_CODE_MEDECINRESPONSABLE = 'NR';
					
					
					
					
					
					DROP TABLE IF EXISTS w_FB_MVTS_MOUVEMENTS_entree;
					CREATE TEMP TABLE w_FB_MVTS_MOUVEMENTS_entree AS
					SELECT CLI_CODE_MOUVEMENT FROM prod_clinicom.FD_MVTS_ADMISSIONS JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT GROUP BY 1;
					
					DROP TABLE IF EXISTS w_FB_MVTS_MOUVEMENTS_sortie;
					CREATE TEMP TABLE w_FB_MVTS_MOUVEMENTS_sortie AS
					SELECT CLI_CODE_MOUVEMENT FROM prod_clinicom.FD_MVTS_SORTIES JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT GROUP BY 1;


					DROP TABLE IF EXISTS w_FD_PMSI_RSS;
					CREATE TEMP TABLE w_FD_PMSI_RSS AS
					SELECT FD_PMSI_RSS.CLI_DONN_NOABSEPISODE,
						MAX(CLI_DATE_SIGNATURE) AS CLI_DATE_GROUPAGE,
						(MAX(Array[CLI_DATE_SIGNATURE::text, CLI_CODE_GHS::text]))[2]::numeric AS CLI_CODE_GHS,
						(MAX(Array[CLI_DATE_SIGNATURE::text, CLI_CODE_GHM::text]))[2]::text AS CLI_CODE_GHM
					FROM prod_clinicom.FD_PMSI_RSS JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT
					JOIN prod_clinicom.FD_PMSI_RUM ON FD_PMSI_RUM.CLI_DONN_NOABSEPISODE = FD_PMSI_RSS.CLI_DONN_NOABSEPISODE
					GROUP BY 1;

					ALTER TABLE w_FD_PMSI_RSS ADD CONSTRAINT w_FD_PMSI_RSS_pkey PRIMARY KEY(CLI_DONN_NOABSEPISODE);

			

					DROP TABLE IF EXISTS w_MVTS_EPISODES;
					CREATE TEMP TABLE w_MVTS_EPISODES AS 
					SELECT 
						FD_MVTS_ADMISSIONS.CLI_DONN_NOABSEPISODE,
						FD_MVTS_EPISODES.CLI_DONN_NOEXTEPISODE,
						FD_MVTS_EPISODES.CLI_CODE_NOIPP,
						FD_MVTS_PATIENTS.CLI_DONN_NOIPPEXTERNE,
						trim(FD_MVTS_PATIENTS.CLI_CODE_POSTAL) AS CLI_CODE_POSTAL,
						FD_MVTS_ADMISSIONS.CLI_CODE_MODEENTREE,
						FD_MVTS_ADMISSIONS.CLI_DATE_MOUVEMENT AS CLI_DATE_ENTREE,
						CLI_CODE_MODESORTIE,
						FD_MVTS_SORTIES.CLI_DATE_SORTIE,
						FD_MVTS_EPISODES.CLI_CODE_MEDECINRESPONSABLE,
						COALESCE(w_MVTS_EPISODES_chambre.CLI_CODE_US, FD_MVTS_SORTIES.CLI_CODE_US, FD_MVTS_ADMISSIONS.CLI_CODE_US) AS CLI_CODE_US,
						COALESCE(FD_MVTS_SORTIES.CLI_CODE_UF, FD_MVTS_ADMISSIONS.CLI_CODE_UF) AS CLI_CODE_UF,
						CASE WHEN COALESCE(CLI_DONN_CHAMBRE,'') <> '' THEN CLI_DONN_CHAMBRE ELSE '0' END AS CLI_DONN_CHAMBRE,
						FD_MVTS_ADMISSIONS.CLI_CODE_MOUVEMENT AS CLI_CODE_MOUVEMENT_ENTREE,
						FD_MVTS_SORTIES.CLI_CODE_MOUVEMENT AS CLI_CODE_MOUVEMENT_SORTIE,
						FD_MVTS_ADMISSIONS.CLI_FLAG_CHAMBREPARTICULIERE,
						CASE WHEN FD_MVTS_URGENCE.CLI_DONN_NOABSEPISODE IS NOT NULL THEN '1' ELSE '0' END AS CLI_FLAG_URGENCE,
						COALESCE(w_FD_PMSI_RSS.CLI_DATE_GROUPAGE, '20991231') AS CLI_DATE_GROUPAGE,
						COALESCE(w_FD_PMSI_RSS.CLI_CODE_GHS, 0) AS CLI_CODE_GHS,
						COALESCE(w_FD_PMSI_RSS.CLI_CODE_GHM, '') AS CLI_CODE_GHM,
						(
							COALESCE(w_MVTS_EPISODES_chambre.CLI_CODE_US, FD_MVTS_SORTIES.CLI_CODE_US, FD_MVTS_ADMISSIONS.CLI_CODE_US) || '|' ||
							COALESCE(FD_MVTS_SORTIES.CLI_CODE_UF, FD_MVTS_ADMISSIONS.CLI_CODE_UF) || '|' ||
							CASE WHEN COALESCE(CLI_DONN_CHAMBRE,'') <> '' THEN CLI_DONN_CHAMBRE ELSE '0' END || '|' ||
							FD_MVTS_EPISODES.CLI_CODE_MEDECINRESPONSABLE || '|' ||
							COALESCE(w_FD_PMSI_RSS.CLI_CODE_GHS, 0) || '|' ||
							COALESCE(w_FD_PMSI_RSS.CLI_CODE_GHM, '') || '|' ||
							trim(FD_MVTS_PATIENTS.CLI_CODE_POSTAL)
						) AS key,
						CASE 
							WHEN date(FD_MVTS_ADMISSIONS.CLI_DATE_MOUVEMENT) >= '[ENV_ADM_ANNEEDEBUT]0101' THEN '1'
							WHEN date(FD_MVTS_SORTIES.CLI_DATE_ENTREE) >= '[ENV_ADM_ANNEEDEBUT]0101' THEN '1'
							WHEN date(FD_MVTS_SORTIES.CLI_DATE_SORTIE) >= '[ENV_ADM_ANNEEDEBUT]0101' THEN '1'
							WHEN FD_MVTS_SORTIES.CLI_DATE_SORTIE IS NULL THEN '1'
							ELSE '0' 
						END AS CTI_OK
					FROM prod_clinicom.FD_MVTS_ADMISSIONS JOIN w_ADMISSIONS_select ON FD_MVTS_ADMISSIONS.cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT
						JOIN prod_clinicom.FD_MVTS_EPISODES ON FD_MVTS_ADMISSIONS.CLI_DONN_NOABSEPISODE = FD_MVTS_EPISODES.CLI_DONN_NOABSEPISODE
						JOIN prod_clinicom.FD_MVTS_PATIENTS ON FD_MVTS_EPISODES.CLI_CODE_NOIPP = FD_MVTS_PATIENTS.CLI_CODE_NOIPP
						LEFT JOIN prod_clinicom.FD_MVTS_SORTIES ON FD_MVTS_ADMISSIONS.CLI_DONN_NOABSEPISODE = FD_MVTS_SORTIES.CLI_DONN_NOABSEPISODE AND FD_MVTS_SORTIES.CLI_DATE_ENTREE IS NOT NULL
						LEFT JOIN prod_clinicom.FD_MVTS_URGENCE ON FD_MVTS_ADMISSIONS.CLI_DONN_NOABSEPISODE = FD_MVTS_URGENCE.CLI_DONN_NOABSEPISODE
						LEFT JOIN w_FD_PMSI_RSS ON FD_MVTS_ADMISSIONS.CLI_DONN_NOABSEPISODE = w_FD_PMSI_RSS.CLI_DONN_NOABSEPISODE
						LEFT JOIN w_MVTS_EPISODES_chambre ON FD_MVTS_ADMISSIONS.CLI_DONN_NOABSEPISODE = w_MVTS_EPISODES_chambre.CLI_DONN_NOABSEPISODE
					;	
						
					
					UPDATE w_MVTS_EPISODES
					SET CTI_OK = '1'
					WHERE CTI_OK <> '1' AND
						CLI_DONN_NOABSEPISODE IN (SELECT CLI_DONN_NOABSEPISODE FROM prod_clinicom.FD_FAC_DETFAC WHERE CLI_DONN_EXERCOMPTA >= [ENV_ADM_ANNEEDEBUT]);

					ALTER TABLE w_MVTS_EPISODES ADD CONSTRAINT w_MVTS_EPISODES_pkey PRIMARY KEY(CLI_DONN_NOABSEPISODE);


			]]></sqlcmd>
		</NODE>
					
		<NODE label="Détection modifications">
			<sqlcmd><![CDATA[
				
				
		
			]]></sqlcmd>
		</NODE>
		
	</NODE>

	<NODE name="SEJOUR" label="RECUPERATION DES SEJOURS">
	
		<NODE label="Patients">
			<sqlcmd><![CDATA[

				INSERT INTO activite[PX].p_patients(no_patient, nom, nom_naissance, prenom, code_sexe, date_naissance)
				SELECT 
					CLI_DONN_NOIPPEXTERNE, 
					CLI_DONN_NOM, 
					CLI_DONN_NOMNAISSANCE,
					CLI_DONN_PRENOM, 
					CASE WHEN FB_MVTS_SEXE.CLI_DONN_DESCRIPTIONCOURTE ILIKE 'F%' THEN '2' ELSE '1' END, 
					date(CLI_DATE_NAISSANCE)
				FROM prod_clinicom.FD_MVTS_PATIENTS
					JOIN prod_clinicom.FB_MVTS_SEXE ON FD_MVTS_PATIENTS.CLI_CODE_SEXE = FB_MVTS_SEXE.CLI_CODE_SEXE
				WHERE CLI_CODE_NOIPP IN (SELECT CLI_CODE_NOIPP FROM w_MVTS_EPISODES) AND
					CLI_DONN_NOIPPEXTERNE NOT IN (SELECT no_patient FROM activite[PX].p_patients)
				ORDER BY CLI_DONN_NOIPPEXTERNE;


					
				UPDATE activite[PX].p_patients SET
					nom = CLI_DONN_NOM, 
					nom_naissance = CLI_DONN_NOMNAISSANCE, 
					prenom = CLI_DONN_PRENOM, 
					code_sexe = CASE WHEN FB_MVTS_SEXE.CLI_DONN_DESCRIPTIONCOURTE ILIKE 'F%' THEN '2' ELSE '1' END, 
					date_naissance = date(CLI_DATE_NAISSANCE)
				FROM prod_clinicom.FD_MVTS_PATIENTS
					JOIN prod_clinicom.FB_MVTS_SEXE ON FD_MVTS_PATIENTS.CLI_CODE_SEXE = FB_MVTS_SEXE.CLI_CODE_SEXE
				WHERE p_patients.no_patient = CLI_DONN_NOIPPEXTERNE AND
					(
						p_patients.nom IS DISTINCT FROM CLI_DONN_NOM OR 
						p_patients.nom_naissance IS DISTINCT FROM CLI_DONN_NOMNAISSANCE OR 
						p_patients.prenom IS DISTINCT FROM CLI_DONN_PRENOM OR
						p_patients.code_sexe IS DISTINCT FROM CASE WHEN FB_MVTS_SEXE.CLI_DONN_DESCRIPTIONCOURTE ILIKE 'F%' THEN '2' ELSE '1' END OR
						p_patients.date_naissance IS DISTINCT FROM date(CLI_DATE_NAISSANCE)
					);
										
			]]></sqlcmd>
		</NODE>
				
		<NODE label="Séjours">
			<sqlcmd><![CDATA[
			
					
					-- Clé pour recherche oids
					
					DROP TABLE IF EXISTS w_MVTS_EPISODES_keys;
					CREATE TEMP TABLE w_MVTS_EPISODES_keys AS
					SELECT 
						key,
						CLI_CODE_US,
						CLI_CODE_UF,
						CLI_DONN_CHAMBRE,
						CLI_CODE_MEDECINRESPONSABLE,
						CLI_CODE_GHS,
						CLI_CODE_GHM,
						CLI_CODE_POSTAL,

						''::text AS service_facturation_code,
						''::text AS activite_code,
						''::text AS unite_fonctionnelle_code,
						''::text AS unite_medicale_code,
						''::text AS etage_code,
						''::text AS lit_code,

						''::text AS mode_traitement_code,
						''::text AS dmt_code,
						'0'::text AS type_sejour_force,
						
						0::bigint AS medecin_sejour_id,
						0::bigint AS service_facturation_id,
						0::bigint AS mode_traitement_id,
						0::bigint AS unite_fonctionnelle_id,
						0::bigint AS unite_medicale_id,
						0::bigint AS activite_id,
						0::bigint AS etage_id,
						0::bigint AS lit_id,
						0::bigint AS lieu_id,
						
						0::bigint as code_postal_id,
						
						0::bigint AS ghs_id,
						0::bigint AS ghm_id
						
					FROM w_MVTS_EPISODES
					GROUP BY 1,2,3,4,5,6,7,8;


					UPDATE w_MVTS_EPISODES_keys
					SET ghs_id = COALESCE(t_ghs.oid,0)
					FROM base.t_ghs
					WHERE t_ghs.code = CLI_CODE_GHS;
					
					UPDATE w_MVTS_EPISODES_keys
					SET ghm_id = COALESCE(t_ghm.oid,0)
					FROM base.t_ghm
					WHERE t_ghm.code = CLI_CODE_GHM;
					
					
					UPDATE w_MVTS_EPISODES_keys
					SET medecin_sejour_id = COALESCE(t_medecins_administratifs.oid,0)
					FROM activite[PX].t_medecins_administratifs
					WHERE t_medecins_administratifs.code_original = CLI_CODE_MEDECINRESPONSABLE;


					UPDATE w_MVTS_EPISODES_keys
					SET code_postal_id = COALESCE(t_codes_postaux.oid, 0)
					FROM base.t_codes_postaux
					WHERE t_codes_postaux.code = CLI_CODE_POSTAL;


					UPDATE w_MVTS_EPISODES_keys
					SET 
						unite_fonctionnelle_code = w_MVTS_EPISODES_keys.CLI_CODE_UF,
						unite_fonctionnelle_id = COALESCE(t_unites_fonctionnelles.oid,0),
						unite_medicale_code = COALESCE(w_MVTS_UF.CLI_CODE_UM,''),
						unite_medicale_id = COALESCE(t_unites_medicales.oid,0),
						service_facturation_code = w_MVTS_EPISODES_keys.CLI_CODE_UF,
						service_facturation_id = COALESCE(t_services_facturation.oid,0),
						activite_code = COALESCE(w_MVTS_UF.CLI_CODE_SERVICE,''),
						activite_id = COALESCE(t_activites.oid,0),
						mode_traitement_id = COALESCE(t_modes_traitement.oid,0),
						mode_traitement_code = COALESCE(t_modes_traitement.code,''),
						dmt_code = COALESCE(t_dmt.code,''),
						type_sejour_force = CASE WHEN t_services_facturation.type_sejour IN ('1', '2', '3', '4', '5') THEN COALESCE(t_services_facturation.type_sejour,'0') ELSE '0' END
					FROM w_MVTS_UF
						LEFT JOIN activite[PX].t_services_facturation ON CLI_CODE_UF = t_services_facturation.code_original
						LEFT JOIN activite[PX].t_unites_fonctionnelles ON CLI_CODE_UF = t_unites_fonctionnelles.code_original
						LEFT JOIN activite[PX].t_unites_medicales ON CLI_CODE_UM = t_unites_medicales.code_original
						LEFT JOIN activite[PX].t_activites ON CLI_CODE_SERVICE = t_activites.code_original
						LEFT JOIN base.t_modes_traitement ON t_services_facturation.mode_traitement_id = t_modes_traitement.oid
						LEFT JOIN base.t_dmt ON t_services_facturation.dmt_id = t_dmt.oid
					WHERE w_MVTS_UF.CLI_CODE_UF = w_MVTS_EPISODES_keys.CLI_CODE_UF;


					UPDATE w_MVTS_EPISODES_keys
					SET lit_id = COALESCE(t_lits.oid,0),
						lit_code = CLI_CODE_US || '|' || CLI_DONN_CHAMBRE,
						etage_id = COALESCE(t_lits.etage_id,0),
						etage_code = CLI_CODE_US
					FROM activite[PX].t_lits
						JOIN activite[PX].t_etages ON t_lits.etage_id = t_etages.oid
					WHERE t_lits.code_original = CLI_CODE_US || '|' || CLI_DONN_CHAMBRE;




					INSERT INTO activite[PX].t_lieux(
								code_original_1, 
								code_original_2, 
								code_original_3, 
								code_original_4,
								code_original_5, 
								code_original_6)
					SELECT 
						service_facturation_code,
						activite_code,
						etage_code,
						lit_code,
						unite_medicale_code,
						unite_fonctionnelle_code
					FROM w_MVTS_EPISODES_keys
						LEFT JOIN activite[PX].t_lieux ON (
							code_original_1 = service_facturation_code AND
							code_original_2 = activite_code AND
							code_original_3 = etage_code AND
							code_original_4 = lit_code AND
							code_original_5 = unite_medicale_code AND
							code_original_6 = unite_fonctionnelle_code
						)
					WHERE t_lieux.oid IS NULL
					GROUP BY 1,2,3,4,5,6;


					UPDATE activite[PX].t_lieux SET
						service_facturation_id = COALESCE(t_services_facturation.oid,0),
						mode_traitement_id = COALESCE(t_services_facturation.mode_traitement_id,0) 
					FROM activite[PX].t_services_facturation 
					WHERE code_original_1 = t_services_facturation.code_original
						AND (t_lieux.service_facturation_id <> COALESCE(t_services_facturation.oid,0) OR
							t_lieux.mode_traitement_id <> COALESCE(t_services_facturation.mode_traitement_id,0)
						)
						;



					UPDATE activite[PX].t_lieux SET
						activite_id = COALESCE(t_activites.oid,0)
					FROM activite[PX].t_activites
					WHERE code_original_2 = t_activites.code_original
						AND activite_id <> COALESCE(t_activites.oid,0);

						
					UPDATE activite[PX].t_lieux SET
						lit_id = COALESCE(t_lits.oid,0)
					FROM activite[PX].t_lits
					WHERE code_original_4 = t_lits.code_original AND
						t_lieux.lit_id <> COALESCE(t_lits.oid,0)
						;
						
					UPDATE activite[PX].t_lieux SET
						unite_medicale_id = COALESCE(t_unites_medicales.oid,0)
					FROM activite[PX].t_unites_medicales
					WHERE code_original_5 = t_unites_medicales.code_original
						AND unite_medicale_id <> COALESCE(t_unites_medicales.oid,0);

					
					UPDATE activite[PX].t_lieux SET
						unite_fonctionnelle_id = COALESCE(t_unites_fonctionnelles.oid,0)
					FROM activite[PX].t_unites_fonctionnelles
					WHERE code_original_6 = t_unites_fonctionnelles.code_original
						AND unite_fonctionnelle_id <> COALESCE(t_unites_fonctionnelles.oid,0);



					UPDATE w_MVTS_EPISODES_keys
						set lieu_id = t_lieux.oid
					FROM activite[PX].t_lieux
					WHERE code_original_1 = service_facturation_code AND
							code_original_2 = activite_code AND
							code_original_3 = etage_code AND
							code_original_4 = lit_code AND
							code_original_5 = unite_medicale_code AND
							code_original_6 = unite_fonctionnelle_code;



					DROP TABLE IF EXISTS w_MVTS_EPISODES_debiteurs;
					CREATE TEMP TABLE w_MVTS_EPISODES_debiteurs AS
					SELECT FD_FAC_DEBITEURS.CLI_DONN_NOABSEPISODE,
						MAX(w_FB_FAC_DEBITEURS.est_budget_global) AS est_budget_global,
						(SELECT oid FROM activite[PX].t_tiers_payant WHERE code_original = '0') AS tiers_payant_0_id,
						(MAX(CASE WHEN FD_FAC_DEBITEURS.CLI_CODE_ORGANISME <> '' AND FD_FAC_DEBITEURS.CLI_CODE_TYPEDEBITEUR IN (1) THEN ARRAY[FD_FAC_DEBITEURS.CLI_DONN_NUMSEQUENCE,COALESCE(tiers_payant_1_id,0)] ELSE NULL END))[2] AS tiers_payant_1_id,
						(MAX(CASE WHEN FD_FAC_DEBITEURS.CLI_CODE_ORGANISME <> '' AND FD_FAC_DEBITEURS.CLI_CODE_TYPEDEBITEUR NOT IN (1) THEN ARRAY[FD_FAC_DEBITEURS.CLI_DONN_NUMSEQUENCE,COALESCE(tiers_payant_2_id,0)] ELSE NULL END))[2] AS tiers_payant_2_id
					FROM prod_clinicom.FD_FAC_DEBITEURS JOIN w_ADMISSIONS_select ON FD_FAC_DEBITEURS.cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT
						JOIN w_MVTS_EPISODES ON w_MVTS_EPISODES.CLI_DONN_NOABSEPISODE = FD_FAC_DEBITEURS.CLI_DONN_NOABSEPISODE AND CTI_OK = '1'
						LEFT JOIN w_FB_FAC_DEBITEURS ON FD_FAC_DEBITEURS.CLI_CODE_ORGANISME = w_FB_FAC_DEBITEURS.CLI_CODE_ORGANISME
					GROUP BY 1
					ORDER BY 1;
					
					-- Sur les enfants, prendre eventuellement les débiteurs de la mere
					INSERT INTO w_MVTS_EPISODES_debiteurs
					SELECT FD_MVTS_NAISSANCE.CLI_DONN_NOABSEPISODEENFANT,
						MAX(w_FB_FAC_DEBITEURS.est_budget_global) AS est_budget_global,
						(SELECT oid FROM activite.t_tiers_payant WHERE code_original = '0') AS tiers_payant_0_id,
						(MAX(CASE WHEN FD_FAC_DEBITEURS.CLI_CODE_ORGANISME <> '' AND FD_FAC_DEBITEURS.CLI_CODE_TYPEDEBITEUR IN (1) THEN ARRAY[FD_FAC_DEBITEURS.CLI_DONN_NUMSEQUENCE,COALESCE(tiers_payant_1_id,0)] ELSE NULL END))[2] AS tiers_payant_1_id,
						(MAX(CASE WHEN FD_FAC_DEBITEURS.CLI_CODE_ORGANISME <> '' AND FD_FAC_DEBITEURS.CLI_CODE_TYPEDEBITEUR NOT IN (1) THEN ARRAY[FD_FAC_DEBITEURS.CLI_DONN_NUMSEQUENCE,COALESCE(tiers_payant_2_id,0)] ELSE NULL END))[2] AS tiers_payant_2_id
					FROM prod_clinicom.FD_FAC_DEBITEURS JOIN w_ADMISSIONS_select ON FD_FAC_DEBITEURS.cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT
						JOIN prod_clinicom.FD_MVTS_NAISSANCE ON FD_FAC_DEBITEURS.CLI_DONN_NOABSEPISODE = FD_MVTS_NAISSANCE.CLI_DONN_NOABSEPISODE
						JOIN w_MVTS_EPISODES ON w_MVTS_EPISODES.CLI_DONN_NOABSEPISODE = FD_MVTS_NAISSANCE.CLI_DONN_NOABSEPISODEENFANT AND CTI_OK = '1'
						LEFT JOIN w_FB_FAC_DEBITEURS ON FD_FAC_DEBITEURS.CLI_CODE_ORGANISME = w_FB_FAC_DEBITEURS.CLI_CODE_ORGANISME
					WHERE FD_MVTS_NAISSANCE.CLI_DONN_NOABSEPISODEENFANT NOT IN (SELECT CLI_DONN_NOABSEPISODE from w_MVTS_EPISODES_debiteurs)
					GROUP BY 1
					ORDER BY 1
					;



					DROP TABLE IF EXISTS w_MVTS_MOUVEMENTS_type_sejour;
					CREATE TEMP TABLE w_MVTS_MOUVEMENTS_type_sejour AS
					SELECT FD_MVTS_MOUVEMENTS.CLI_DONN_NOABSEPISODE,
						MAX(CASE 
							WHEN CLI_CODE_MOUVEMENT IN ('1','2') THEN '5' 
							WHEN CLI_CODE_MOUVEMENT IN ('13') THEN '3' 
							ELSE NULL 
						END) AS type_sejour_force
					FROM prod_clinicom.FD_MVTS_MOUVEMENTS JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT
						WHERE CLI_CODE_MOUVEMENT NOT IN ('7','11','12','15','52','53')
					GROUP BY 1
					ORDER BY 1;



					DROP TABLE IF EXISTS w_sejours;

					CREATE TEMP TABLE w_sejours AS
					SELECT 
						CLI_DONN_NOEXTEPISODE as no_sejour, 
						w_MVTS_EPISODES.CLI_DONN_NOABSEPISODE as code_original, 
						CLI_DONN_NOIPPEXTERNE as no_patient, 
						date(CLI_DATE_ENTREE) as date_entree, 
						extract('hour' FROM CLI_DATE_ENTREE) * 10000 + extract('minute' FROM CLI_DATE_ENTREE) * 100 + extract('second' FROM CLI_DATE_ENTREE) as heure_entree, 
						CASE WHEN CLI_DATE_SORTIE IS NOT NULL THEN date(CLI_DATE_SORTIE) ELSE '20991231' END::date as date_sortie, 
						CASE WHEN CLI_DATE_SORTIE IS NOT NULL THEN extract('hour' FROM CLI_DATE_SORTIE) * 10000 + extract('minute' FROM CLI_DATE_SORTIE) * 100 + extract('second' FROM CLI_DATE_SORTIE) ELSE 0 END as heure_sortie, 
						p_patients.code_sexe, 
						0 AS age, 
						CASE WHEN CLI_DATE_SORTIE IS NOT NULL THEN 1 ELSE 0 END as code_sorti, 
						0 as code_prevu,
						CASE 
							WHEN w_MVTS_EPISODES_keys.type_sejour_force <> '0' THEN w_MVTS_EPISODES_keys.type_sejour_force
							WHEN w_MVTS_MOUVEMENTS_type_sejour.type_sejour_force IS NOT NULL THEN w_MVTS_MOUVEMENTS_type_sejour.type_sejour_force
							WHEN mode_traitement_code IN ('07') OR dmt_code IN ('958') THEN '3' 
							WHEN mode_traitement_code IN ('19', '23') THEN '2' 
							WHEN mode_traitement_code IN ('04') AND date(CLI_DATE_SORTIE) = date(CLI_DATE_ENTREE) THEN '2' 
							WHEN mode_traitement_code IN ('03') AND date(CLI_DATE_SORTIE) = date(CLI_DATE_ENTREE) THEN '2' 
							WHEN mode_traitement_code IN ('10') AND date(CLI_DATE_SORTIE) <= date(CLI_DATE_ENTREE) + interval '1 day' THEN '3' 
							WHEN mode_traitement_code IN ('03') THEN '1' 
							ELSE '1' END as type_sejour,
						medecin_sejour_id,
						lieu_id AS lieu_sortie_id,
						CASE WHEN CLI_FLAG_CHAMBREPARTICULIERE > 0 THEN '1' ELSE '0' END as code_cp_demandee,
						mode_traitement_id,
						COALESCE(CLI_CODE_MODE_ENTREEPMSI,'8') as mode_entree,
						'0'::text AS provenance,
						COALESCE(CLI_CODE_MODESORTIEPMSI,'8') as mode_sortie,
						'0'::text AS destination,
						COALESCE(tiers_payant_0_id,0) AS tiers_payant_0_id,
						COALESCE(tiers_payant_1_id,0) AS tiers_payant_1_id,
						COALESCE(tiers_payant_2_id,0) AS tiers_payant_2_id,
						0 AS tiers_payant_22_id,
						1 AS est_budget_global,
						w_MVTS_EPISODES_keys.ghs_id,
						w_MVTS_EPISODES_keys.ghm_id,
						w_MVTS_EPISODES.CLI_DATE_GROUPAGE AS date_groupage,
						0::bigint AS ghs_bebe1_id,
						0::bigint AS ghs_bebe2_id,
						0::bigint AS ghs_bebe3_id,
						w_MVTS_EPISODES_keys.code_postal_id
					FROM w_MVTS_EPISODES
						JOIN activite[PX].p_patients ON CLI_DONN_NOIPPEXTERNE = p_patients.no_patient
						JOIN w_MVTS_EPISODES_keys ON w_MVTS_EPISODES.key = w_MVTS_EPISODES_keys.key
						LEFT JOIN w_MVTS_EPISODES_debiteurs ON w_MVTS_EPISODES.CLI_DONN_NOABSEPISODE = w_MVTS_EPISODES_debiteurs.CLI_DONN_NOABSEPISODE
						LEFT JOIN w_MVTS_MOUVEMENTS_type_sejour ON w_MVTS_EPISODES.CLI_DONN_NOABSEPISODE = w_MVTS_MOUVEMENTS_type_sejour.CLI_DONN_NOABSEPISODE
						LEFT JOIN prod_clinicom.FB_MVTS_MODESENTREE ON FB_MVTS_MODESENTREE.CLI_CODE_MODEENTREE = w_MVTS_EPISODES.CLI_CODE_MODEENTREE
						LEFT JOIN prod_clinicom.FB_MVTS_MODESSORTIE ON FB_MVTS_MODESSORTIE.CLI_CODE_MODESORTIE = w_MVTS_EPISODES.CLI_CODE_MODESORTIE
					WHERE CTI_OK = '1';
					
					-- Forcage type selon séjour
					UPDATE w_sejours
					SET type_sejour = t_sejour.type_sejour
					FROM activite.t_sejour
					WHERE t_sejour.no_sejour = w_sejours.no_sejour AND
						t_sejour.type_sejour IN ('1','2','3','4','5','6','9');
						
					-- Ajustement dates externes pour externes sur plusieurs jours
					
					UPDATE w_sejours
					SET 
						date_entree = subview.date_entree, 
						heure_entree = subview.heure_entree, 
						date_sortie = subview.date_sortie,
						heure_sortie = 235900
					FROM
					(
						SELECT no_sejour, 
							date_entree AS date_entree_sejour,
							date_sortie AS date_sortie_sejour,
							MIN(date(CLI_DATE_SAISIE)) AS date_entree, 
							MAX(date(CLI_DATE_SAISIE)) AS date_sortie, 
							extract('hour' FROM MIN(CLI_DATE_SAISIE)) * 10000 + extract('minute' FROM MIN(CLI_DATE_SAISIE)) * 100 + extract('second' FROM MIN(CLI_DATE_SAISIE)) AS heure_entree
						FROM prod_clinicom.fd_actes_entete 
						JOIN w_sejours ON cli_donn_noabsepisode = code_original AND
							w_sejours.type_sejour = '3' AND
							w_sejours.code_sorti = '1' AND
							w_sejours.date_entree <> w_sejours.date_sortie
						GROUP BY 1,2,3
						HAVING 
						MIN(date(CLI_DATE_SAISIE)) > date_entree OR
						MAX(date(CLI_DATE_SAISIE)) < date_sortie
					) subview
					WHERE w_sejours.no_sejour = subview.no_sejour;



					
					-- Ajustement heure sortie externe positionnée à 23h59 par défaut par clinicom
										
					DROP TABLE IF EXISTS w_duree;
					CREATE TEMP TABLE w_duree AS
					SELECT medecin_sejour_id, 
					GREATEST(round((avg(heure_sortie - heure_entree) / 10000)::numeric,0) * 10000,10000) AS duree_moyenne
					FROM w_sejours 
					WHERE type_sejour = '3' AND heure_sortie <> 235900 AND
						code_sorti = '1' AND date_entree = date_sortie AND heure_sortie > heure_entree
					GROUP BY 1;
										
					UPDATE w_sejours SET
						heure_sortie = heure_entree + duree_moyenne
					FROM w_duree
					WHERE
						w_sejours.medecin_sejour_id = w_duree.medecin_sejour_id AND
						type_sejour = '3' AND
						code_sorti = '1' AND
						heure_sortie = 235900 AND 
						heure_entree + duree_moyenne <= 240000;
					
					UPDATE w_sejours SET
						code_sorti = '1',
						date_sortie = date_entree,
						heure_sortie = heure_entree + duree_moyenne
					FROM w_duree
					WHERE
						w_sejours.medecin_sejour_id = w_duree.medecin_sejour_id AND
						type_sejour = '3' AND
						code_sorti <> '1' AND
						date_entree < date(now()) AND 
						heure_entree + duree_moyenne <= 240000;

					UPDATE w_sejours SET
						date_sortie = date(date_entree + interval '1 day'),
						heure_sortie = heure_entree + duree_moyenne - 240000
					FROM w_duree
					WHERE
						w_sejours.medecin_sejour_id = w_duree.medecin_sejour_id AND
						type_sejour = '3' AND
						code_sorti = '1' AND
						heure_sortie = 235900 AND 
						heure_entree + duree_moyenne > 240000;
						
					UPDATE w_sejours SET
						code_sorti = '1',
						date_sortie = date(date_entree + interval '1 day'),
						heure_sortie = heure_entree + duree_moyenne - 240000
					FROM w_duree
					WHERE
						w_sejours.medecin_sejour_id = w_duree.medecin_sejour_id AND
						type_sejour = '3' AND
						code_sorti <> '1' AND
						date_entree < date(now()) AND 
						heure_entree + duree_moyenne > 240000;
						
						
					DROP TABLE IF EXISTS w_duree;
					CREATE TEMP TABLE w_duree AS
					SELECT 
					GREATEST(round((avg(heure_sortie - heure_entree) / 10000)::numeric,0) * 10000,10000) AS duree_moyenne
					FROM w_sejours 
					WHERE type_sejour = '3' AND heure_sortie <> 235900 AND
						code_sorti = '1' AND date_entree = date_sortie AND heure_sortie > heure_entree;

					UPDATE w_sejours SET
						heure_sortie = heure_entree + duree_moyenne
					FROM w_duree
					WHERE
						type_sejour = '3' AND
						code_sorti = '1' AND
						heure_sortie = 235900 AND 
						heure_entree + duree_moyenne <= 240000;
					
					UPDATE w_sejours SET
						code_sorti = '1',
						date_sortie = date_entree,
						heure_sortie = heure_entree + duree_moyenne
					FROM w_duree
					WHERE
						type_sejour = '3' AND
						code_sorti <> '1' AND
						date_entree < date(now()) AND 
						heure_entree + duree_moyenne <= 240000;

					UPDATE w_sejours SET
						date_sortie = date(date_entree + interval '1 day'),
						heure_sortie = heure_entree + duree_moyenne - 240000
					FROM w_duree
					WHERE
						type_sejour = '3' AND
						code_sorti = '1' AND
						heure_sortie = 235900 AND 
						heure_entree + duree_moyenne > 240000;
					
					UPDATE w_sejours SET
						code_sorti = '1',
						date_sortie = date(date_entree + interval '1 day'),
						heure_sortie = heure_entree + duree_moyenne - 240000
					FROM w_duree
					WHERE
						type_sejour = '3' AND
						code_sorti <> '1' AND
						date_entree < date(now()) AND 
						heure_entree + duree_moyenne > 240000;
					
					
					-- Génération séjours

					SELECT base.cti_disable_index('activite', 'i_sejours_2');
					SELECT base.cti_disable_index('activite', 'i_sejours_3');
					SELECT base.cti_disable_index('activite', 'i_sejours_4');
					SELECT base.cti_disable_index('activite', 'i_sejours_5');
					SELECT base.cti_disable_index('activite', 'i_sejours_6');
					SELECT base.cti_disable_index('activite', 'i_sejours_7');
					SELECT base.cti_disable_index('activite', 'i_sejours_8');
					SELECT base.cti_disable_index('activite', 'i_sejours_9');
					SELECT base.cti_disable_index('activite', 'i_sejours_10');
					SELECT base.cti_disable_index('activite', 'i_sejours_11');
					SELECT base.cti_disable_index('activite', 'i_sejours_12');
					SELECT base.cti_disable_index('activite', 'i_sejours_13');

					UPDATE activite[PX].p_sejours SET
						code_original = w_sejours.code_original, 
						no_patient = w_sejours.no_patient, 
						date_entree = w_sejours.date_entree, 
						heure_entree = w_sejours.heure_entree, 
						date_sortie = w_sejours.date_sortie, 
						heure_sortie = w_sejours.heure_sortie, 
						code_sexe = w_sejours.code_sexe, 
						age = w_sejours.age, 
						code_sorti = w_sejours.code_sorti, 
						code_prevu = w_sejours.code_prevu, 
						type_sejour = w_sejours.type_sejour, 
						lieu_sortie_id = w_sejours.lieu_sortie_id,
						medecin_sejour_id = w_sejours.medecin_sejour_id,
						code_cp_demandee = w_sejours.code_cp_demandee,
						mode_traitement_id = w_sejours.mode_traitement_id,
						mode_entree = w_sejours.mode_entree,
						provenance = w_sejours.provenance,
						mode_sortie = w_sejours.mode_sortie,
						destination = w_sejours.destination,
						tiers_payant_0_id = w_sejours.tiers_payant_0_id,
						tiers_payant_1_id = w_sejours.tiers_payant_1_id,
						tiers_payant_2_id = w_sejours.tiers_payant_2_id,
						tiers_payant_22_id = w_sejours.tiers_payant_22_id,
						est_budget_global = w_sejours.est_budget_global,
						date_groupage = w_sejours.date_groupage,
						ghs_id = w_sejours.ghs_id,
						ghm_id = w_sejours.ghm_id,
						ghs_bebe1_id = w_sejours.ghs_bebe1_id,
						ghs_bebe2_id = w_sejours.ghs_bebe2_id,
						ghs_bebe3_id = w_sejours.ghs_bebe3_id,
						code_postal_id = w_sejours.code_postal_id
					FROM w_sejours
					WHERE w_sejours.no_sejour = p_sejours.no_sejour
					AND (
						w_sejours.code_original IS DISTINCT FROM p_sejours.code_original OR
						w_sejours.no_patient IS DISTINCT FROM p_sejours.no_patient OR
						w_sejours.date_entree IS DISTINCT FROM p_sejours.date_entree OR
						w_sejours.heure_entree IS DISTINCT FROM p_sejours.heure_entree OR
						w_sejours.date_sortie IS DISTINCT FROM p_sejours.date_sortie OR
						w_sejours.heure_sortie IS DISTINCT FROM p_sejours.heure_sortie OR
						w_sejours.code_sexe IS DISTINCT FROM p_sejours.code_sexe OR
						w_sejours.age IS DISTINCT FROM p_sejours.age OR
						w_sejours.code_sorti IS DISTINCT FROM p_sejours.code_sorti OR
						w_sejours.code_prevu IS DISTINCT FROM p_sejours.code_prevu OR
						w_sejours.type_sejour IS DISTINCT FROM p_sejours.type_sejour OR
						w_sejours.lieu_sortie_id IS DISTINCT FROM p_sejours.lieu_sortie_id OR
						w_sejours.medecin_sejour_id IS DISTINCT FROM p_sejours.medecin_sejour_id OR
						w_sejours.code_cp_demandee IS DISTINCT FROM p_sejours.code_cp_demandee OR
						w_sejours.mode_traitement_id IS DISTINCT FROM p_sejours.mode_traitement_id OR
						w_sejours.mode_entree IS DISTINCT FROM p_sejours.mode_entree OR
						w_sejours.provenance IS DISTINCT FROM p_sejours.provenance OR 
						w_sejours.mode_sortie IS DISTINCT FROM p_sejours.mode_sortie OR 
						w_sejours.destination IS DISTINCT FROM p_sejours.destination OR 
						w_sejours.tiers_payant_0_id IS DISTINCT FROM p_sejours.tiers_payant_0_id OR
						w_sejours.tiers_payant_1_id IS DISTINCT FROM p_sejours.tiers_payant_1_id OR
						w_sejours.tiers_payant_2_id IS DISTINCT FROM p_sejours.tiers_payant_2_id OR
						w_sejours.tiers_payant_22_id IS DISTINCT FROM p_sejours.tiers_payant_22_id OR
						w_sejours.est_budget_global IS DISTINCT FROM p_sejours.est_budget_global OR
						w_sejours.date_groupage IS DISTINCT FROM p_sejours.date_groupage OR
						w_sejours.ghs_id IS DISTINCT FROM p_sejours.ghs_id OR
						w_sejours.ghm_id IS DISTINCT FROM p_sejours.ghm_id OR
						w_sejours.ghs_bebe1_id IS DISTINCT FROM p_sejours.ghs_bebe1_id OR
						w_sejours.ghs_bebe2_id IS DISTINCT FROM p_sejours.ghs_bebe2_id OR
						w_sejours.ghs_bebe3_id IS DISTINCT FROM p_sejours.ghs_bebe3_id OR
						w_sejours.code_postal_id IS DISTINCT FROM p_sejours.code_postal_id
					);		


					INSERT INTO activite[PX].p_sejours (
						no_sejour, 
						code_original, 
						no_patient, 
						date_entree, 
						heure_entree, 
						date_sortie, 
						heure_sortie, 
						code_sexe, 
						age, 
						code_sorti, 
						code_prevu, 
						type_sejour, 
						lieu_sortie_id,
						medecin_sejour_id,
						ghm_id,
						ghs_id,
						ghs_bebe1_id,
						ghs_bebe2_id,
						ghs_bebe3_id,
						date_groupage,
						code_cp_demandee,
						mode_traitement_id,
						mode_entree,
						provenance,
						mode_sortie,
						destination,
						tiers_payant_0_id,
						tiers_payant_1_id,
						tiers_payant_2_id,
						tiers_payant_22_id,
						est_budget_global,
						code_postal_id
					) 
					SELECT 
						w_sejours.no_sejour, 
						w_sejours.code_original,
						w_sejours.no_patient, 
						w_sejours.date_entree, 
						w_sejours.heure_entree, 
						w_sejours.date_sortie, 
						w_sejours.heure_sortie, 
						w_sejours.code_sexe, 
						w_sejours.age, 
						w_sejours.code_sorti, 
						w_sejours.code_prevu, 
						w_sejours.type_sejour, 
						w_sejours.lieu_sortie_id,
						w_sejours.medecin_sejour_id,
						w_sejours.ghm_id,
						w_sejours.ghs_id,
						w_sejours.ghs_bebe1_id,
						w_sejours.ghs_bebe2_id,
						w_sejours.ghs_bebe3_id,
						w_sejours.date_groupage,
						w_sejours.code_cp_demandee,
						w_sejours.mode_traitement_id,
						w_sejours.mode_entree,
						w_sejours.provenance,
						w_sejours.mode_sortie,
						w_sejours.destination,
						w_sejours.tiers_payant_0_id,
						w_sejours.tiers_payant_1_id,
						w_sejours.tiers_payant_2_id,
						w_sejours.tiers_payant_22_id,
						w_sejours.est_budget_global,
						w_sejours.code_postal_id

					FROM w_sejours LEFT JOIN activite[PX].p_sejours ON (w_sejours.no_sejour = p_sejours.no_sejour)
					WHERE p_sejours.no_sejour IS NULL;

					DELETE FROM activite[PX].p_sejours WHERE no_sejour NOT IN (SELECT no_sejour FROM w_sejours);
					
					-- Séjours ignorés
					UPDATE activite[PX].p_sejours
					SET etat = 'I'
					FROM activite.t_sejour
					WHERE t_sejour.no_sejour = p_sejours.no_sejour AND
						est_ignore = '1' AND
						etat <> 'I' AND
						'[PX]' = '';

					UPDATE activite[PX].p_sejours
					SET etat = ''
					WHERE etat = 'I' AND 
						no_sejour NOT IN (SELECT no_sejour FROM activite.t_sejour WHERE est_ignore = '1') AND
						'[PX]' = '';
					
					-- Médecin séjour forcé
					UPDATE activite[PX].p_sejours
					SET medecin_sejour_id = t_sejour.medecin_sejour_id
					FROM activite.t_sejour
					WHERE t_sejour.no_sejour = p_sejours.no_sejour AND
						t_sejour.medecin_sejour_id <> 0 AND
						p_sejours.medecin_sejour_id IS DISTINCT FROM t_sejour.medecin_sejour_id AND
						'[PX]' = '';
				
				
					-- 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');
				
				
				
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Mouvements">
			<sqlcmd><![CDATA[

					DROP SEQUENCE IF EXISTS w_MVTS_MOUVEMENTS_seq;
					CREATE TEMP SEQUENCE w_MVTS_MOUVEMENTS_seq;


					DROP TABLE IF EXISTS w_MVTS_MOUVEMENTS;
					CREATE TEMP TABLE w_MVTS_MOUVEMENTS AS
					SELECT nextval('w_MVTS_MOUVEMENTS_seq'::regclass) AS sequence, 
						CLI_DATE_MOUVEMENT AS CLI_DATEHEURE_MOUVEMENT_debut,
						DATE(CLI_DATE_MOUVEMENT) AS CLI_DATE_MOUVEMENT_debut,
						extract('hour' FROM CLI_DATE_MOUVEMENT) * 10000 + extract('minute' FROM CLI_DATE_MOUVEMENT) * 100 + extract('second' FROM CLI_DATE_MOUVEMENT) AS CLI_HEURE_MOUVEMENT_debut,
						'20991231'::timestamp without time zone AS CLI_DATEHEURE_MOUVEMENT_fin,
						'20991231'::date AS CLI_DATE_MOUVEMENT_fin,
						240000::numeric AS CLI_HEURE_MOUVEMENT_fin,
						
						''::text AS CLI_CODE_MOUVEMENT_before,
						''::text AS CLI_CODE_UF_before,
						''::text AS CLI_CODE_UM_before,
						''::text AS CLI_CODE_SERVICE_before,
						''::text AS CLI_CODE_US_before,
						''::text AS CLI_DONN_CHAMBRE_before,

						''::text AS CLI_CODE_MOUVEMENT_next,
						''::text AS CLI_CODE_UF_next,
						''::text AS CLI_CODE_UM_next,
						''::text AS CLI_CODE_SERVICE_next,
						''::text AS CLI_CODE_US_next,
						''::text AS CLI_DONN_CHAMBRE_next,

						'0'::text AS est_entree,
						'0'::text AS est_sortie,
						
						'0'::text AS est_hospitalisation_jour,

						''::text AS KEY,


						*
					FROM
					(
						SELECT FD_MVTS_MOUVEMENTS.*, CLI_DATE_ENTREE, CLI_DATE_SORTIE, CLI_FLAG_URGENCE,
							COALESCE(w_MVTS_UF.CLI_CODE_SERVICE,'') AS CLI_CODE_SERVICE, COALESCE(w_MVTS_UF.CLI_CODE_UM,'') AS CLI_CODE_UM
						FROM w_MVTS_EPISODES
							JOIN prod_clinicom.FD_MVTS_MOUVEMENTS ON w_MVTS_EPISODES.CLI_DONN_NOABSEPISODE = FD_MVTS_MOUVEMENTS.CLI_DONN_NOABSEPISODE AND CTI_OK = '1'
							LEFT JOIN w_MVTS_UF ON FD_MVTS_MOUVEMENTS.CLI_CODE_UF = w_MVTS_UF.CLI_CODE_UF
						WHERE CLI_CODE_MOUVEMENT NOT IN ('5')
						ORDER BY w_MVTS_EPISODES.CLI_DONN_NOEXTEPISODE, CLI_DATE_MOUVEMENT
					) subview;


					CREATE INDEX w_MVTS_MOUVEMENTS_i1 ON w_MVTS_MOUVEMENTS USING btree (CLI_DONN_NOABSEPISODE);
					CREATE INDEX w_MVTS_MOUVEMENTS_i2 ON w_MVTS_MOUVEMENTS USING btree (sequence);
					
					
					-- Correction mouvement hospitalisation jour
					UPDATE w_MVTS_MOUVEMENTS SET
						est_hospitalisation_jour = '1'
					WHERE CLI_CODE_UF IN (SELECT
							CLI_CODE_UF
						FROM prod_clinicom.FD_MVTS_MOUVEMENTS
						WHERE CLI_CODE_MOUVEMENT IN ('1','2'));
					
					-- Correction mouvement externes (heure 235900)
					UPDATE w_MVTS_MOUVEMENTS SET
						CLI_DATEHEURE_MOUVEMENT_debut = w_sejours.date_sortie + to_char(w_sejours.heure_sortie,'FM000000')::time,
						CLI_DATE_MOUVEMENT_debut = w_sejours.date_sortie,
						CLI_HEURE_MOUVEMENT_debut = w_sejours.heure_sortie
					FROM w_sejours
					WHERE w_MVTS_MOUVEMENTS.CLI_DONN_NOABSEPISODE = w_sejours.code_original AND
						CLI_HEURE_MOUVEMENT_debut = 235900 AND
						w_sejours.type_sejour = '3' AND
						w_sejours.code_sorti = '1' AND
						CLI_CODE_MOUVEMENT = '15';


					-- Jours E/S
					UPDATE w_MVTS_MOUVEMENTS SET est_entree = '1' WHERE CLI_CODE_MOUVEMENT IN (SELECT CLI_CODE_MOUVEMENT FROM w_FB_MVTS_MOUVEMENTS_entree);


					-- report de la chambre 
					SELECT base.cti_execute('
						UPDATE w_MVTS_MOUVEMENTS SET
							CLI_DONN_CHAMBRE = w_MVTS_MOUVEMENTS_next.CLI_DONN_CHAMBRE
						FROM w_MVTS_MOUVEMENTS w_MVTS_MOUVEMENTS_next 
						WHERE w_MVTS_MOUVEMENTS.CLI_DONN_NOABSEPISODE = w_MVTS_MOUVEMENTS_next.CLI_DONN_NOABSEPISODE AND
							w_MVTS_MOUVEMENTS.sequence + 1 = w_MVTS_MOUVEMENTS_next.sequence AND
							w_MVTS_MOUVEMENTS.CLI_DONN_CHAMBRE = '''' AND
							w_MVTS_MOUVEMENTS_next.CLI_DONN_CHAMBRE <> ''''',100);

					SELECT base.cti_execute('	
					UPDATE w_MVTS_MOUVEMENTS SET
							CLI_DONN_CHAMBRE = w_MVTS_MOUVEMENTS_before.CLI_DONN_CHAMBRE
						FROM w_MVTS_MOUVEMENTS w_MVTS_MOUVEMENTS_before 
						WHERE w_MVTS_MOUVEMENTS.CLI_DONN_NOABSEPISODE = w_MVTS_MOUVEMENTS_before.CLI_DONN_NOABSEPISODE AND
							w_MVTS_MOUVEMENTS.sequence = w_MVTS_MOUVEMENTS_before.sequence + 1 AND
							w_MVTS_MOUVEMENTS.CLI_DONN_CHAMBRE = '''' AND
							w_MVTS_MOUVEMENTS_before.CLI_DONN_CHAMBRE <> ''''',100);



					-- Admission suspendue
					UPDATE w_MVTS_MOUVEMENTS SET 
						CLI_CODE_US = 'ADMSUSP',
						CLI_DONN_CHAMBRE = '0'
 					WHERE CLI_CODE_MOUVEMENT IN ('8');




					-- analyse mouvement suivant
					UPDATE w_MVTS_MOUVEMENTS SET
						CLI_DATEHEURE_MOUVEMENT_fin = w_MVTS_MOUVEMENTS_next.CLI_DATEHEURE_MOUVEMENT_debut - interval '1 second',
						CLI_DATE_MOUVEMENT_fin = date(w_MVTS_MOUVEMENTS_next.CLI_DATEHEURE_MOUVEMENT_debut - interval '1 second'),
						CLI_HEURE_MOUVEMENT_fin = extract('hour' FROM w_MVTS_MOUVEMENTS_next.CLI_DATEHEURE_MOUVEMENT_debut - interval '1 second') * 10000 + 
									extract('minute' FROM w_MVTS_MOUVEMENTS_next.CLI_DATEHEURE_MOUVEMENT_debut - interval '1 second') * 100 + 
									extract('second' FROM w_MVTS_MOUVEMENTS_next.CLI_DATEHEURE_MOUVEMENT_debut - interval '1 second'),
						CLI_CODE_MOUVEMENT_next = w_MVTS_MOUVEMENTS_next.CLI_CODE_MOUVEMENT,
						CLI_CODE_UF_next = w_MVTS_MOUVEMENTS_next.CLI_CODE_UF,
						CLI_CODE_UM_next = w_MVTS_MOUVEMENTS_next.CLI_CODE_UM,
						CLI_CODE_SERVICE_next = w_MVTS_MOUVEMENTS_next.CLI_CODE_SERVICE,
						CLI_CODE_US_next = w_MVTS_MOUVEMENTS_next.CLI_CODE_US,
						CLI_DONN_CHAMBRE_next = w_MVTS_MOUVEMENTS_next.CLI_DONN_CHAMBRE,
						est_sortie = CASE WHEN w_MVTS_MOUVEMENTS_next.CLI_CODE_MOUVEMENT IN (SELECT CLI_CODE_MOUVEMENT FROM w_FB_MVTS_MOUVEMENTS_sortie) THEN '1' ELSE '0' END
					FROM w_MVTS_MOUVEMENTS w_MVTS_MOUVEMENTS_next 
					WHERE w_MVTS_MOUVEMENTS.CLI_DONN_NOABSEPISODE = w_MVTS_MOUVEMENTS_next.CLI_DONN_NOABSEPISODE AND
						w_MVTS_MOUVEMENTS.sequence + 1 = w_MVTS_MOUVEMENTS_next.sequence ;

					DELETE FROM w_MVTS_MOUVEMENTS WHERE CLI_CODE_MOUVEMENT IN (SELECT CLI_CODE_MOUVEMENT FROM w_FB_MVTS_MOUVEMENTS_sortie);
					
					UPDATE w_MVTS_MOUVEMENTS SET
						CLI_DATEHEURE_MOUVEMENT_fin = CLI_DATEHEURE_MOUVEMENT_fin + interval '1 second',
						CLI_DATE_MOUVEMENT_fin = date(CLI_DATEHEURE_MOUVEMENT_fin + interval '1 second'),
						CLI_HEURE_MOUVEMENT_fin = extract('hour' FROM CLI_DATEHEURE_MOUVEMENT_fin + interval '1 second') * 10000 + 
									extract('minute' FROM CLI_DATEHEURE_MOUVEMENT_fin + interval '1 second') * 100 + 
									extract('second' FROM CLI_DATEHEURE_MOUVEMENT_fin + interval '1 second')
					WHERE est_sortie = '1';


					-- analyse mouvement precedent
					UPDATE w_MVTS_MOUVEMENTS SET
						CLI_CODE_MOUVEMENT_before = w_MVTS_MOUVEMENTS_before.CLI_CODE_MOUVEMENT,
						CLI_CODE_UF_before = w_MVTS_MOUVEMENTS_before.CLI_CODE_UF,
						CLI_CODE_UM_before = w_MVTS_MOUVEMENTS_before.CLI_CODE_UM,
						CLI_CODE_SERVICE_before = w_MVTS_MOUVEMENTS_before.CLI_CODE_SERVICE,
						CLI_CODE_US_before = w_MVTS_MOUVEMENTS_before.CLI_CODE_US,
						CLI_DONN_CHAMBRE_before = w_MVTS_MOUVEMENTS_before.CLI_DONN_CHAMBRE
					FROM w_MVTS_MOUVEMENTS w_MVTS_MOUVEMENTS_before
					WHERE w_MVTS_MOUVEMENTS.CLI_DONN_NOABSEPISODE = w_MVTS_MOUVEMENTS_before.CLI_DONN_NOABSEPISODE AND
						w_MVTS_MOUVEMENTS.sequence = w_MVTS_MOUVEMENTS_before.sequence + 1;

					
					-- Externes non sortis -> forcer la sortie
					UPDATE w_MVTS_MOUVEMENTS SET
						CLI_DATEHEURE_MOUVEMENT_fin = w_sejours.date_sortie + to_char(w_sejours.heure_sortie,'FM000000')::time,
						CLI_DATE_MOUVEMENT_fin = w_sejours.date_sortie,
						CLI_HEURE_MOUVEMENT_fin = w_sejours.heure_sortie,
						CLI_CODE_MOUVEMENT_next = '15',
						est_sortie = '1'
					FROM w_sejours
					WHERE w_MVTS_MOUVEMENTS.CLI_DONN_NOABSEPISODE = w_sejours.code_original AND 
						type_sejour = '3' AND
						cli_code_mouvement_next <> 15 AND
						w_sejours.code_sorti = '1';

					UPDATE w_MVTS_MOUVEMENTS SET CLI_DONN_CHAMBRE = '0' WHERE CLI_DONN_CHAMBRE = '';

					UPDATE w_MVTS_MOUVEMENTS 
					SET key = CLI_CODE_UF || '|' || CLI_CODE_UM || '|' || CLI_CODE_SERVICE || '|' || CLI_CODE_US || '|' || CLI_DONN_CHAMBRE;
					
					
					VACUUM FULL w_MVTS_MOUVEMENTS;
					
					CREATE INDEX w_MVTS_MOUVEMENTS_i3 ON w_MVTS_MOUVEMENTS USING btree (key);


					DROP TABLE IF EXISTS w_MVTS_MOUVEMENTS_keys;
					CREATE TEMP TABLE w_MVTS_MOUVEMENTS_keys AS
					SELECT 
						key,
						CLI_CODE_UF,
						CLI_CODE_UM,
						CLI_CODE_SERVICE,
						CLI_CODE_US,
						CLI_DONN_CHAMBRE,
				
						''::text AS mode_traitement_code,
						''::text AS dmt_code,
						'0'::text AS type_sejour_force,
						'0'::text AS chambre_particuliere,
						
						0::bigint AS service_facturation_id,
						0::bigint AS mode_traitement_id,
						0::bigint AS unite_fonctionnelle_id,
						0::bigint AS unite_medicale_id,
						0::bigint AS activite_id,
						0::bigint AS etage_id,
						0::bigint AS lit_id,
						0::bigint AS lieu_id
					FROM w_MVTS_MOUVEMENTS 
					GROUP BY 1,2,3,4,5,6,7;

					
					INSERT INTO activite[PX].t_etages(code_original, code, texte, texte_court)
					SELECT 
						CLI_CODE_US, 
						CLI_CODE_US, 
						CLI_CODE_US,
						CLI_CODE_US
					FROM 
						w_MVTS_MOUVEMENTS 
					WHERE CLI_CODE_US NOT IN (SELECT code_original FROM activite[PX].t_etages) AND CLI_CODE_US <> ''
					GROUP BY 1
					ORDER BY 1;

					INSERT INTO activite[PX].t_lits(code_original, code, texte, texte_court, etage_id, chambre_particuliere)
					SELECT code_original || '|0' , '', 'Non renseigné', 'Non renseigné', oid, 'N' 
					FROM activite[PX].t_etages
					WHERE oid > 0 AND
						code_original || '|0' NOT IN (SELECT code_original FROM activite[PX].t_lits );


					UPDATE activite[PX].t_lits SET code = '*****' WHERE oid = 0;
					
					UPDATE activite[PX].t_lits 
					SET code = t_etages.code || '***'
					FROM activite[PX].t_etages
					WHERE etage_id = t_etages.oid 
						AND t_lits.code = '' ;

					INSERT INTO activite[PX].t_lits(code_original, code, texte, texte_court, etage_id, chambre_particuliere)
					SELECT 
						CLI_CODE_US || '|' || CLI_DONN_CHAMBRE,
						CLI_DONN_CHAMBRE,
						'Chambre ' || CLI_DONN_CHAMBRE,
						'Chambre ' || CLI_DONN_CHAMBRE,
						t_etages.oid,
						'N'
					FROM 
						w_MVTS_MOUVEMENTS 
						JOIN activite[PX].t_etages ON CLI_CODE_US = t_etages.code_original
					WHERE CLI_DONN_CHAMBRE <> '' AND CLI_DONN_CHAMBRE <> '0' AND
						CLI_CODE_US || '|' || CLI_DONN_CHAMBRE NOT IN (SELECT code_original FROM activite[PX].t_lits)
					GROUP BY 1,2,3,4,5;



					UPDATE w_MVTS_MOUVEMENTS_keys
					SET 
						service_facturation_id = COALESCE(t_services_facturation.oid,0),
						mode_traitement_id = t_modes_traitement.oid,
						mode_traitement_code = t_modes_traitement.code,
						dmt_code = t_dmt.code,
						type_sejour_force = CASE WHEN t_services_facturation.type_sejour IN ('1', '2', '3', '4', '5') THEN t_services_facturation.type_sejour ELSE '0' END
					FROM activite[PX].t_services_facturation
						JOIN base.t_modes_traitement ON t_services_facturation.mode_traitement_id = t_modes_traitement.oid
						JOIN base.t_dmt ON t_services_facturation.dmt_id = t_dmt.oid
					WHERE t_services_facturation.code_original = CLI_CODE_UF;



					UPDATE w_MVTS_MOUVEMENTS_keys
					SET unite_fonctionnelle_id = COALESCE(t_unites_fonctionnelles.oid,0)
					FROM activite[PX].t_unites_fonctionnelles
					WHERE t_unites_fonctionnelles.code_original = CLI_CODE_UF;


					UPDATE w_MVTS_MOUVEMENTS_keys
					SET unite_medicale_id = COALESCE(t_unites_medicales.oid,0)
					FROM activite[PX].t_unites_medicales
					WHERE t_unites_medicales.code_original = CLI_CODE_UM;


					UPDATE w_MVTS_MOUVEMENTS_keys
					SET activite_id = COALESCE(t_activites.oid,0)
					FROM activite[PX].t_activites
					WHERE t_activites.code_original = CLI_CODE_SERVICE;


					UPDATE w_MVTS_MOUVEMENTS_keys
					SET lit_id = COALESCE(t_lits.oid,0),
						CLI_CODE_US = CASE WHEN t_lits.etage_id <> 0 THEN COALESCE(t_etages.code_original,'') ELSE '' END,
						etage_id = COALESCE(t_lits.etage_id,0),
						chambre_particuliere = t_lits.chambre_particuliere
					FROM activite[PX].t_lits
						JOIN activite[PX].t_etages ON t_lits.etage_id = t_etages.oid
					WHERE t_lits.code_original = CLI_CODE_US || '|' || CLI_DONN_CHAMBRE;


					INSERT INTO activite[PX].t_lieux(
								code_original_1, 
								code_original_2, 
								code_original_3, 
								code_original_4,
								code_original_5, 
								code_original_6)
					SELECT 
						CLI_CODE_UF,
						CLI_CODE_SERVICE,
						CLI_CODE_US,
						CLI_CODE_US || '|' || CLI_DONN_CHAMBRE,
						CLI_CODE_UM,
						CLI_CODE_UF
					FROM w_MVTS_MOUVEMENTS_keys
						LEFT JOIN activite[PX].t_lieux ON (
							code_original_1 = CLI_CODE_UF AND
							code_original_2 = CLI_CODE_SERVICE AND
							code_original_3 = CLI_CODE_US AND
							code_original_4 = CLI_CODE_US || '|' || CLI_DONN_CHAMBRE AND
							code_original_5 = CLI_CODE_UM AND
							code_original_6 = CLI_CODE_UF
						)
					WHERE t_lieux.oid IS NULL
					GROUP BY 1,2,3,4,5,6;
						
						
					UPDATE activite[PX].t_lieux SET
						service_facturation_id = COALESCE(t_services_facturation.oid,0),
						mode_traitement_id = COALESCE(t_services_facturation.mode_traitement_id,0) 
					FROM activite[PX].t_services_facturation 
					WHERE code_original_1 = t_services_facturation.code_original
						AND (t_lieux.service_facturation_id <> COALESCE(t_services_facturation.oid,0) OR
							t_lieux.mode_traitement_id <> COALESCE(t_services_facturation.mode_traitement_id,0)
						)
						;

					UPDATE activite[PX].t_lieux SET
						activite_id = COALESCE(t_activites.oid,0)
					FROM activite[PX].t_activites
					WHERE code_original_2 = t_activites.code_original
						AND activite_id <> COALESCE(t_activites.oid,0);
							
					UPDATE activite[PX].t_lieux SET
						lit_id = COALESCE(t_lits.oid,0)
					FROM activite[PX].t_lits
					WHERE code_original_4 = t_lits.code_original AND
						t_lieux.lit_id <> COALESCE(t_lits.oid,0)
						;
							
					UPDATE activite[PX].t_lieux SET
						unite_medicale_id = COALESCE(t_unites_medicales.oid,0)
					FROM activite[PX].t_unites_medicales
					WHERE code_original_5 = t_unites_medicales.code_original
						AND unite_medicale_id <> COALESCE(t_unites_medicales.oid,0);

					UPDATE activite[PX].t_lieux SET
						unite_fonctionnelle_id = COALESCE(t_unites_fonctionnelles.oid,0)
					FROM activite[PX].t_unites_fonctionnelles
					WHERE code_original_6 = t_unites_fonctionnelles.code_original
						AND unite_fonctionnelle_id <> COALESCE(t_unites_fonctionnelles.oid,0);

						
						


					UPDATE w_MVTS_MOUVEMENTS_keys
						set lieu_id = t_lieux.oid
					FROM activite[PX].t_lieux
					WHERE code_original_1 = CLI_CODE_UF AND
						code_original_2 = CLI_CODE_SERVICE AND
						code_original_3 = CLI_CODE_US AND
						code_original_4 = CLI_CODE_US || '|' || CLI_DONN_CHAMBRE AND
						code_original_5 = CLI_CODE_UM AND
						code_original_6 = CLI_CODE_UF;
						
					
					
					VACUUM FULL w_MVTS_MOUVEMENTS_keys;
					
					ALTER TABLE w_MVTS_MOUVEMENTS_keys ADD CONSTRAINT w_MVTS_MOUVEMENTS_keys_pkey PRIMARY KEY(key);

					

					DROP TABLE IF EXISTS w_mouvements_sejour;

					CREATE TEMP TABLE w_mouvements_sejour AS
					SELECT 
						w_MVTS_MOUVEMENTS.CLI_DONN_NOABSEPISODE, 
						p_sejours.no_sejour AS no_sejour,
						p_sejours.oid AS sejour_id,
						sequence,
						p_calendrier.date, 
						p_calendrier.jour_semaine,
						p_calendrier.is_weekend,
						CASE WHEN p_calendrier.date = CLI_DATE_MOUVEMENT_debut THEN CLI_HEURE_MOUVEMENT_debut ELSE 0 END AS heure_debut,
						CASE WHEN p_calendrier.date = CLI_DATE_MOUVEMENT_fin THEN CLI_HEURE_MOUVEMENT_fin ELSE 240000 END AS heure_fin,
						CASE WHEN p_calendrier.date = CLI_DATE_MOUVEMENT_debut AND est_entree = '1' AND p_sejours.type_sejour = '1' THEN 1 ELSE 0 END AS nb_entrees_directes,
						CASE WHEN p_calendrier.date = CLI_DATE_MOUVEMENT_fin AND est_sortie = '1' AND p_sejours.type_sejour = '1' THEN 1 ELSE 0 END AS nb_sorties_directes,
						CASE WHEN p_calendrier.date = CLI_DATE_MOUVEMENT_debut AND est_entree = '1' AND p_sejours.type_sejour = '1' THEN '1' ELSE '0' END AS est_jour_entree,
						CASE WHEN p_calendrier.date = CLI_DATE_MOUVEMENT_fin AND est_sortie = '1' AND p_sejours.type_sejour = '1' THEN '1' ELSE '0' END AS est_jour_sortie,
						CASE 
							WHEN p_sejours.type_sejour = '1' AND est_hospitalisation_jour <> '1' THEN '1' 
							WHEN p_sejours.type_sejour = '1' AND est_hospitalisation_jour = '1' AND CLI_CODE_MOUVEMENT = '1'  THEN '1' 
							ELSE '0' END AS est_jour_hospitalisation,
						CASE WHEN p_calendrier.date = CLI_DATE_MOUVEMENT_debut AND est_entree = '1' AND p_sejours.type_sejour = '2' THEN 1 ELSE 0 END AS nb_ambulatoires,
						CASE WHEN p_calendrier.date = CLI_DATE_MOUVEMENT_debut AND est_entree = '1' AND p_sejours.type_sejour = '3' THEN 1 ELSE 0 END AS nb_externes,
						CASE WHEN p_calendrier.date = CLI_DATE_MOUVEMENT_debut AND est_entree = '1' AND p_sejours.type_sejour <> '9' AND (dmt_code IN ('303', '307', '308', '401', '402', '403', '404', '405', '406', '407', '408', '409') OR CLI_FLAG_URGENCE = '1') THEN 1 ELSE 0 END AS nb_urgences,
						CASE WHEN p_sejours.type_sejour = '5' AND CLI_CODE_MOUVEMENT = '1' THEN 1 ELSE 0 END AS nb_seances,

						CASE WHEN p_calendrier.date = CLI_DATE_MOUVEMENT_debut AND p_sejours.type_sejour = '1' AND est_entree <> '1' AND w_MVTS_MOUVEMENTS.CLI_CODE_UF <> CLI_CODE_UF_before THEN 1 ELSE 0 END AS nb_entrees_mutation_service,
						CASE WHEN p_calendrier.date = CLI_DATE_MOUVEMENT_fin AND p_sejours.type_sejour = '1' AND est_sortie <> '1' AND w_MVTS_MOUVEMENTS.CLI_CODE_UF <> CLI_CODE_UF_next THEN 1 ELSE 0 END AS nb_sorties_mutation_service,
						CASE WHEN p_calendrier.date = CLI_DATE_MOUVEMENT_debut AND p_sejours.type_sejour = '1' AND est_entree <> '1' AND w_MVTS_MOUVEMENTS.CLI_CODE_US <> CLI_CODE_US_before THEN 1 ELSE 0 END AS nb_entrees_mutation_etage,
						CASE WHEN p_calendrier.date = CLI_DATE_MOUVEMENT_fin AND p_sejours.type_sejour = '1' AND est_sortie <> '1' AND w_MVTS_MOUVEMENTS.CLI_CODE_US <> CLI_CODE_US_next THEN 1 ELSE 0 END AS nb_sorties_mutation_etage,
						0 AS nb_entrees_mutation_activite,
						0 AS nb_sorties_mutation_activite,
						CASE WHEN p_calendrier.date = CLI_DATE_MOUVEMENT_debut AND p_sejours.type_sejour = '1' AND est_entree <> '1' AND w_MVTS_MOUVEMENTS.CLI_CODE_UM <> CLI_CODE_UM_before THEN 1 ELSE 0 END AS nb_entrees_mutation_unite_medicale,
						CASE WHEN p_calendrier.date = CLI_DATE_MOUVEMENT_fin AND p_sejours.type_sejour = '1' AND est_sortie <> '1' AND w_MVTS_MOUVEMENTS.CLI_CODE_UM <> CLI_CODE_UM_next THEN 1 ELSE 0 END AS nb_sorties_mutation_unite_medicale,

						CASE 
							WHEN p_sejours.type_sejour = '1' AND (p_calendrier.date <> CLI_DATE_MOUVEMENT_debut OR est_sortie = '1') AND est_hospitalisation_jour <> '1' THEN '1' 
							WHEN p_sejours.type_sejour = '1' AND est_hospitalisation_jour = '1' AND CLI_CODE_MOUVEMENT = '1'  THEN 1
							ELSE 0 END AS nb_jours_js_inclus,
						CASE 
							WHEN p_sejours.type_sejour = '1' AND p_calendrier.date <> CLI_DATE_MOUVEMENT_debut AND est_hospitalisation_jour <> '1' THEN '1' 
							WHEN p_sejours.type_sejour = '1' AND est_hospitalisation_jour = '1' AND CLI_CODE_MOUVEMENT = '1'  THEN 1
							ELSE 0 END AS nb_jours_js_non_inclus,
						CASE 
							WHEN p_sejours.type_sejour = '1' AND CLI_CODE_MOUVEMENT <> '8' AND chambre_particuliere = 'O' AND est_hospitalisation_jour <> '1' THEN '1' 
							WHEN p_sejours.type_sejour = '1' AND est_hospitalisation_jour = '1' AND chambre_particuliere = 'O' AND CLI_CODE_MOUVEMENT = '1'  THEN '1' 
							ELSE '0' END AS est_chambre_particuliere,
						CASE 
							WHEN p_sejours.type_sejour = '1' AND CLI_CODE_MOUVEMENT <> '8' AND chambre_particuliere = 'O' AND (p_calendrier.date <> CLI_DATE_MOUVEMENT_fin OR est_sortie = '1') AND est_hospitalisation_jour <> '1' THEN 1
							WHEN p_sejours.type_sejour = '1' AND est_hospitalisation_jour = '1' AND chambre_particuliere = 'O' AND p_calendrier.date <> CLI_DATE_MOUVEMENT_fin AND CLI_CODE_MOUVEMENT = '1'  THEN 1
							ELSE 0 END AS nb_chambres_particulieres,
						
						0::numeric AS nb_bebes,

						est_hospitalisation_jour,
						
						'0'::text AS est_dernier_mouvement_jour,
						'0'::text AS est_premier_mouvement_jour,
						
						lieu_id,

						p_sejours.medecin_sejour_id as medecin_sejour_id

					FROM w_MVTS_MOUVEMENTS 
						JOIN activite[PX].p_sejours ON w_MVTS_MOUVEMENTS.CLI_DONN_NOABSEPISODE = p_sejours.code_original AND p_sejours.etat = ''
						JOIN w_MVTS_MOUVEMENTS_keys ON w_MVTS_MOUVEMENTS.key = w_MVTS_MOUVEMENTS_keys.key
						JOIN base.p_calendrier ON 
							(p_calendrier.date BETWEEN CLI_DATE_MOUVEMENT_debut AND CLI_DATE_MOUVEMENT_fin AND p_calendrier.date <= now() OR (p_calendrier.date = p_sejours.date_entree AND est_entree = '1'))
					WHERE  (p_sejours.type_sejour NOT IN ('2', '3') OR (p_sejours.type_sejour IN ('2', '3') AND est_entree = '1' AND p_calendrier.date = CLI_DATE_MOUVEMENT_debut))
						AND p_sejours.code_prevu <> '1' 
					ORDER BY sequence, p_calendrier.date;
					
					CREATE INDEX w_mouvements_sejour_i1
					ON w_mouvements_sejour
					USING btree
					(CLI_DONN_NOABSEPISODE);
					
					CREATE INDEX w_mouvements_sejour_i2
					ON w_mouvements_sejour
					USING btree
					(no_sejour);
					
					
					-- bébés
					UPDATE w_mouvements_sejour
					SET nb_bebes = 1
					FROM prod_clinicom.FD_MVTS_NAISSANCE
					JOIN activite[PX].p_sejours ON FD_MVTS_NAISSANCE.CLI_Donn_NoAbsEpisodeEnfant = p_sejours.code_original
					WHERE w_mouvements_sejour.no_sejour = p_sejours.no_sejour AND date = date(CLI_DATE_NAISSANCE);
					
					
					-- Séances selon ACTES
					DROP TABLE IF EXISTS w_FD_ACTES_CCAM_seances;
					CREATE TEMP TABLE w_FD_ACTES_CCAM_seances AS
					SELECT FD_ACTES_CCAM.CLI_DONN_NOABSEPISODE, 
						date(CLI_DATE_SAISIE) AS date_seance, 
						extract('hour' FROM CLI_DATE_SAISIE) * 10000 + extract('minute' FROM CLI_DATE_SAISIE) * 100 + extract('second' FROM CLI_DATE_SAISIE) AS heure_seance,
						1 AS nb_seances
					FROM prod_clinicom.FD_ACTES_CCAM JOIN w_ADMISSIONS_select ON FD_ACTES_CCAM.cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT
					JOIN prod_clinicom.FD_ACTES_ENTETE ON FD_ACTES_CCAM.CLI_DONN_NOABSEPISODE = FD_ACTES_ENTETE.CLI_DONN_NOABSEPISODE AND
							FD_ACTES_CCAM.CLI_DONN_LIEN = FD_ACTES_ENTETE.CLI_DONN_LIEN 
					JOIN base.t_actes ON substring(CLI_CODE_CCAM, 1, 7) = t_actes.code 
					WHERE (CLI_CODE_CCAM LIKE 'ZZMK%' OR CLI_CODE_CCAM LIKE 'ZZMP%' OR (t_actes.texte ILIKE '%séance%' AND t_actes.code LIKE 'JV%'))
					GROUP BY 1,2,3
					ORDER BY 1,2,3;


					CREATE INDEX w_FD_ACTES_CCAM_seances_i1
					ON w_FD_ACTES_CCAM_seances
					USING btree
					(CLI_DONN_NOABSEPISODE);


					

					UPDATE w_mouvements_sejour
					SET nb_seances = w_FD_ACTES_CCAM_seances.nb_seances
					FROM w_FD_ACTES_CCAM_seances
					WHERE w_FD_ACTES_CCAM_seances.CLI_DONN_NOABSEPISODE = w_mouvements_sejour.CLI_DONN_NOABSEPISODE AND date = date_seance AND heure_seance BETWEEN heure_debut AND heure_fin AND
						w_mouvements_sejour.nb_seances IS DISTINCT FROM w_FD_ACTES_CCAM_seances.nb_seances;
					
					
					-- Séances selon GHS et DIAG
					DROP TABLE IF EXISTS w_FD_PMSI_RSS_seances;
					CREATE TEMP TABLE w_FD_PMSI_RSS_seances AS
					SELECT FD_PMSI_RSS.CLI_DONN_NOABSEPISODE, 
						date(CLI_DATE_ENTREE) AS date_seance, 
						extract('hour' FROM CLI_DATE_ENTREE) * 10000 + extract('minute' FROM CLI_DATE_ENTREE) * 100 + extract('second' FROM CLI_DATE_ENTREE) AS heure_seance,
						1 AS nb_seances
					FROM prod_clinicom.FD_PMSI_RSS JOIN w_ADMISSIONS_select ON FD_PMSI_RSS.cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT
					LEFT JOIN prod_clinicom.FD_PMSI_DIAG ON FD_PMSI_DIAG.CLI_DONN_NOABSEPISODE = FD_PMSI_RSS.CLI_DONN_NOABSEPISODE AND 
						CLI_CODE_TYPEDIAGNOSTIC = '1' AND CLI_CODE_DIAG IN ('Z510', 'Z5100', 'Z5101', 'Z511', 'Z512', 'Z5130', 'Z5131')
					JOIN base.t_ghs ON CLI_CODE_GHS = t_ghs.code 
					WHERE t_ghs.texte ILIKE '%séance%' OR 
						CLI_CODE_GHM LIKE '28%' OR
						FD_PMSI_DIAG.CLI_DONN_NOABSEPISODE IS NOT NULL
					GROUP BY 1,2,3
					ORDER BY 1,2,3;
					
					CREATE INDEX w_FD_PMSI_RSS_seances_i1
					ON w_FD_PMSI_RSS_seances
					USING btree
					(CLI_DONN_NOABSEPISODE);

						
					UPDATE w_mouvements_sejour
					SET nb_seances =  w_FD_PMSI_RSS_seances.nb_seances
					FROM  w_FD_PMSI_RSS_seances
					WHERE  w_FD_PMSI_RSS_seances.CLI_DONN_NOABSEPISODE = w_mouvements_sejour.CLI_DONN_NOABSEPISODE AND date = date_seance AND heure_seance BETWEEN heure_debut AND heure_fin AND
						w_mouvements_sejour.nb_seances IS DISTINCT FROM  w_FD_PMSI_RSS_seances.nb_seances;
					
			
				
					
					-- Si hopital de jour : Date entree dans mouvements = premier mouvement
					
					UPDATE w_mouvements_sejour
					SET nb_entrees_directes = CASE WHEN w_mouvements_sejour.heure_debut = heure_entree THEN 0
						WHEN w_mouvements_sejour.heure_debut = subview.heure_debut THEN 1
						ELSE 0 END
					FROM (
					SELECT w_mouvements_sejour.no_sejour, date,
						MIN(CASE WHEN est_jour_hospitalisation = '1' THEN heure_debut ELSE 999999 END) AS heure_debut,
						MIN(CASE WHEN nb_entrees_directes = 1 THEN heure_debut ELSE 999999 END) AS heure_entree
					FROM w_mouvements_sejour
					JOIN activite[PX].p_sejours ON w_mouvements_sejour.no_sejour = p_sejours.no_sejour AND w_mouvements_sejour.date = p_sejours.date_entree
					WHERE est_hospitalisation_jour = '1'
					GROUP BY 1,2
					HAVING MIN(CASE WHEN est_jour_hospitalisation = '1' THEN heure_debut ELSE 999999 END) IS DISTINCT FROM
						MIN(CASE WHEN nb_entrees_directes = 1 THEN heure_debut ELSE 999999 END)
					ORDER BY 1
					) subview
					WHERE est_hospitalisation_jour = '1' AND
						w_mouvements_sejour.no_sejour = subview.no_sejour AND
						w_mouvements_sejour.date = subview.date;



					UPDATE w_mouvements_sejour
					SET nb_sorties_directes = CASE WHEN w_mouvements_sejour.heure_fin = heure_sortie THEN 0
						WHEN w_mouvements_sejour.heure_fin = subview.heure_fin THEN 1
						ELSE 0 END
					FROM (
					SELECT w_mouvements_sejour.no_sejour, date,
						MAX(CASE WHEN est_jour_hospitalisation = '1' THEN heure_fin ELSE 0 END) AS heure_fin,
						MAX(CASE WHEN nb_sorties_directes = 1 THEN heure_fin ELSE 0 END) AS heure_sortie
					FROM w_mouvements_sejour
					JOIN activite[PX].p_sejours ON w_mouvements_sejour.no_sejour = p_sejours.no_sejour AND w_mouvements_sejour.date = p_sejours.date_sortie
					WHERE est_hospitalisation_jour = '1'
					GROUP BY 1,2
					HAVING MAX(CASE WHEN est_jour_hospitalisation = '1' THEN heure_fin ELSE 0 END) IS DISTINCT FROM
						MAX(CASE WHEN nb_sorties_directes = 1 THEN heure_fin ELSE 0 END)
					ORDER BY 1
					) subview
					WHERE est_hospitalisation_jour = '1' AND
						w_mouvements_sejour.no_sejour = subview.no_sejour AND
						w_mouvements_sejour.date = subview.date;



				
			
			
					-- Génération
					DELETE FROM w_mouvements_sejour 
					WHERE 
						nb_entrees_directes = 0 AND
						nb_sorties_directes = 0 AND
						est_jour_hospitalisation <> '1' AND
						nb_ambulatoires = 0 AND
						nb_externes = 0 AND
						nb_urgences = 0 AND
						nb_seances = 0 AND
						nb_entrees_mutation_service = 0 AND
						nb_sorties_mutation_service = 0 AND
						nb_entrees_mutation_etage = 0 AND
						nb_sorties_mutation_etage = 0 AND
						nb_entrees_mutation_activite = 0 AND
						nb_sorties_mutation_activite = 0 AND
						nb_entrees_mutation_unite_medicale = 0 AND
						nb_sorties_mutation_unite_medicale = 0 AND
						nb_jours_js_inclus = 0 AND
						nb_jours_js_non_inclus = 0 AND
						est_chambre_particuliere <> '1' AND
						nb_chambres_particulieres = 0 AND
						nb_bebes = 0;
						
					
					UPDATE w_mouvements_sejour
					SET est_premier_mouvement_jour = '1'
					FROM (
					SELECT no_sejour, date, MIN(heure_debut) AS heure_debut
					FROM w_mouvements_sejour
					GROUP BY 1,2
					HAVING  MIN(heure_debut) <> 0
					) subview
					WHERE w_mouvements_sejour.no_sejour = subview.no_sejour AND
						w_mouvements_sejour.heure_debut = subview.heure_debut;


					UPDATE w_mouvements_sejour
					SET est_dernier_mouvement_jour = '1'
					FROM (
					SELECT no_sejour, date, MAX(heure_fin) AS heure_fin
					FROM w_mouvements_sejour
					GROUP BY 1,2
					HAVING  MAX(heure_fin) <> 240000
					) subview
					WHERE w_mouvements_sejour.no_sejour = subview.no_sejour AND
						w_mouvements_sejour.heure_fin = subview.heure_fin;




					-- Mise en production des mouvements

					SELECT base.cti_disable_index('activite', 'i_mouvements_sejour_1');
					SELECT base.cti_disable_index('activite', 'i_mouvements_sejour_2');
					SELECT base.cti_disable_index('activite', 'i_mouvements_sejour_3');
					SELECT base.cti_disable_index('activite', 'i_mouvements_sejour_4');
					SELECT base.cti_disable_index('activite', 'i_mouvements_sejour_5');

					INSERT INTO activite[PX].p_mouvements_sejour(
								sejour_id, no_sejour, date, 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 sejour_id, no_sejour, date,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
					FROM w_mouvements_sejour
					WHERE heure_fin >= heure_debut
					ORDER BY no_sejour, sequence;

					SELECT base.cti_enable_index('activite', 'i_mouvements_sejour_1');
					SELECT base.cti_enable_index('activite', 'i_mouvements_sejour_2');
					SELECT base.cti_enable_index('activite', 'i_mouvements_sejour_3');
					SELECT base.cti_enable_index('activite', 'i_mouvements_sejour_4');
					SELECT base.cti_enable_index('activite', 'i_mouvements_sejour_5');

					

					UPDATE activite[PX].p_sejours
					SET code_sorti = '1'
					FROM activite[PX].p_mouvements_sejour 
					WHERE p_sejours.no_sejour = p_mouvements_sejour.no_sejour AND
						code_sorti = '0' AND
						(nb_ambulatoires > 0 OR nb_externes > 0) AND 
						date_sortie <= now();
						
					UPDATE activite[PX].p_sejours
					SET type_sejour = '5'
					WHERE type_sejour <> '5' AND 
						type_sejour <> '1' AND 
						no_sejour IN (
							SELECT no_sejour 
							FROM activite[PX].p_mouvements_sejour 
							WHERE nb_seances > 0);
							
					
					UPDATE activite[PX].p_mouvements_sejour
					SET nb_externes = 0, nb_ambulatoires = 0
					FROM activite[PX].p_sejours
					WHERE p_mouvements_sejour.no_sejour = p_sejours.no_sejour AND
						p_sejours.type_sejour = '5' AND
						(
							nb_externes <> 0 OR
							nb_ambulatoires <> 0
						);
										
										
			]]></sqlcmd>
		</NODE>
		
	</NODE>

	<NODE name="FACTURE" label="RECUPERATION DES FACTURES">
	
		<NODE label="Initialisation">

			<sqlcmd><![CDATA[
			
				-- Informations de séjour Hors PMSI
				DROP TABLE IF EXISTS w_FD_FAC_DETFAC_horspmsi;
				CREATE TEMP TABLE w_FD_FAC_DETFAC_horspmsi AS 
				SELECT FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE, FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET, FD_FAC_DETFAC.CLI_DONN_EXERCOMPTA
				FROM prod_clinicom.FD_FAC_DETFACLIG JOIN w_ADMISSIONS_select ON FD_FAC_DETFACLIG.cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT
				JOIN prod_clinicom.FD_FAC_DETFAC ON FD_FAC_DETFACLIG.CLI_DONN_NOABSEPISODE = FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE AND
										FD_FAC_DETFACLIG.CLI_DONN_NOOBJINTFAC = FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET AND
										FD_FAC_DETFACLIG.CLI_DONN_EXERCOMPTA = FD_FAC_DETFAC.CLI_DONN_EXERCOMPTA
				WHERE CLI_CODE_UF IN (SELECT CLI_CODE_UF FROM w_FS_MVTS_UF WHERE CLI_CODE_TYPERECUEIL = '2') AND CLI_CODE_OBJETFACTURATION = 'I'
				GROUP BY 1,2,3
				ORDER BY 1;
				
				CREATE INDEX w_FD_FAC_DETFAC_horspmsi_i1
				ON w_FD_FAC_DETFAC_horspmsi
				USING btree
				(CLI_DONN_NOABSEPISODE);


				-- extraction lignes facture
				
				DROP TABLE IF EXISTS w_FD_FAC_DETFAC;
				CREATE TEMP TABLE w_FD_FAC_DETFAC AS
				SELECT
					p_sejours.no_sejour,
					p_sejours.oid AS sejour_id,
					SUBSTR(FD_FAC_DETFAC.CLI_DONN_EXERCOMPTA,3,2) || '_' ||
					FD_FAC_DETFAC.CLI_CODE_OBJETFACTURATION || 
					CASE 
						WHEN CLI_DONN_NOOBJETFAC = '0' THEN p_sejours.no_sejour || '.' || FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET
						WHEN FD_FAC_DETFAC.CLI_CODE_OBJETFACTURATION = 'I' THEN p_sejours.no_sejour || '.' || FD_FAC_DETFAC.CLI_DONN_NOOBJETFAC
						ELSE FD_FAC_DETFAC.CLI_DONN_NOOBJETFAC::text 
					END::text AS no_facture,
					CASE 
						WHEN CLI_DONN_NOOBJETCTX <> 0 THEN 
							SUBSTR(CLI_DONN_EXERCTX,3,2) || '_' ||
							FD_FAC_DETFAC.CLI_CODE_OBJETFACTURATION || CLI_DONN_NOOBJETCTX
						ELSE '' END::text AS no_facture_avoir,
					p_sejours.ghs_id AS ghs_id,

					CASE 
						WHEN FD_FAC_DETFAC.CLI_CODE_TYPEDEBITEUR IN (1) THEN '1'
						WHEN FD_FAC_DETFAC.CLI_CODE_TYPEDEBITEUR = 7 THEN '0'
						ELSE '2' 
					END::text AS type_tiers_payant,
					w_MVTS_EPISODES_debiteurs.tiers_payant_0_id,
					CASE WHEN FD_FAC_DETFAC.CLI_CODE_TYPEDEBITEUR IN (1) THEN COALESCE(w_FB_FAC_DEBITEURS.tiers_payant_1_id,0) ELSE COALESCE(w_MVTS_EPISODES_debiteurs.tiers_payant_1_id,0) END AS tiers_payant_1_id,
					CASE WHEN FD_FAC_DETFAC.CLI_CODE_TYPEDEBITEUR NOT IN (1,2) AND FD_FAC_DETFAC.CLI_CODE_TYPEDEBITEUR <> 7 THEN COALESCE(w_FB_FAC_DEBITEURS.tiers_payant_2_id,0) ELSE COALESCE(w_MVTS_EPISODES_debiteurs.tiers_payant_2_id,0) END AS tiers_payant_2_id,
					
					CASE WHEN FD_FAC_DETFAC.CLI_CODE_OBJETFACTURATION = 'I' THEN '1'::text ELSE '0'::text END AS est_budget_global,
					'0'::text AS est_pmsi,
					0::bigint AS rss_id,
					FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE,
					FD_FAC_DETFAC.CLI_DONN_EXERCOMPTA,
					FD_FAC_DETFAC.CLI_DONN_NOOBJETFAC,
					FD_FAC_DETFAC.CLI_CODE_TYPEDEBITEUR,
					FD_FAC_DETFAC.CLI_CODE_ORGANISME,
					FD_FAC_DETFAC.CLI_FLAG_SENS,
					FD_FAC_DETFAC.CLI_CODE_OBJETFACTURATION,
					FD_FAC_DETFAC.CLI_DATE_OBJETFAC,
					date(FD_FAC_DETFAC.CLI_DATE_DEBPERIODE) AS CLI_DATE_DEBPERIODE,
					date(FD_FAC_DETFAC.CLI_DATE_FINPERIODE) AS CLI_DATE_FINPERIODE,
					FD_FAC_DETFAC.CLI_CODE_ETAT,
					CASE WHEN CLI_FLAG_SENS = 1 THEN FD_FAC_DETFAC.CLI_DONN_MONTANT ELSE 0 - FD_FAC_DETFAC.CLI_DONN_MONTANT END AS CLI_DONN_MONTANT,
					FD_FAC_DETFAC.CLI_DONN_EXERCTX,
					FD_FAC_DETFAC.CLI_DONN_NOOBJETCTX,
					FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET
				FROM prod_clinicom.FD_FAC_DETFAC
					JOIN activite[PX].p_sejours ON FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE = p_sejours.code_original AND p_sejours.etat = ''
					JOIN w_MVTS_EPISODES_debiteurs ON w_MVTS_EPISODES_debiteurs.CLI_DONN_NOABSEPISODE = FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE
					LEFT JOIN w_FB_FAC_DEBITEURS ON FD_FAC_DETFAC.CLI_CODE_ORGANISME = w_FB_FAC_DEBITEURS.CLI_CODE_ORGANISME
					LEFT JOIN w_FD_FAC_DETFAC_horspmsi ON w_FD_FAC_DETFAC_horspmsi.CLI_DONN_NOABSEPISODE = FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE AND
										w_FD_FAC_DETFAC_horspmsi.CLI_CODE_NOINTERNEOBJET = FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET AND
										w_FD_FAC_DETFAC_horspmsi.CLI_DONN_EXERCOMPTA = FD_FAC_DETFAC.CLI_DONN_EXERCOMPTA
				WHERE FD_FAC_DETFAC.CLI_CODE_OBJETFACTURATION <> 'P' AND (FD_FAC_DETFAC.CLI_CODE_OBJETFACTURATION <> 'I' OR w_FD_FAC_DETFAC_horspmsi.CLI_DONN_NOABSEPISODE IS NOT NULL);


				DROP SEQUENCE IF EXISTS w_FD_FAC_DETFACLIG_sequence;
				CREATE TEMP SEQUENCE w_FD_FAC_DETFACLIG_sequence;


				DROP TABLE IF EXISTS w_FD_FAC_DETFACLIG;
				CREATE TEMP TABLE w_FD_FAC_DETFACLIG AS
				SELECT
					nextval('w_FD_FAC_DETFACLIG_sequence'::regclass) AS oid,
					0::bigint AS FD_ACTES_CCAM_id,

					w_FD_FAC_DETFAC.no_sejour,
					w_FD_FAC_DETFAC.sejour_id,
					w_FD_FAC_DETFAC.no_facture,
					0::bigint AS rss_id,
					0::bigint AS no_rss,
					0::bigint AS ghs_id,
					'0'::text AS inclus_dans_sejour,

					w_FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE,
					w_FD_FAC_DETFAC.CLI_DONN_NOOBJETFAC,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET,
					w_FD_FAC_DETFAC.CLI_CODE_OBJETFACTURATION,
					w_FD_FAC_DETFAC.CLI_CODE_ETAT,
					w_FD_FAC_DETFAC.CLI_FLAG_SENS,
					w_FD_FAC_DETFAC.CLI_DONN_EXERCOMPTA,
					
					''::text AS CLI_CODE_MEDECINREALISATEUR,
					''::text AS CLI_CODE_CCAM,
					''::text AS CLI_CODE_PHASE,
					''::text AS CLI_CODE_ACTIVITE,
					''::text AS CLI_DONN_EXTDOC,
					''::text AS CLI_DONN_MODIFICATEUR,
					CLI_SEQUENCE1ACTE,
					CLI_SEQUENCE2ACTE,
					
					CLI_CODE_ORDONNATEUR::text,
					CASE WHEN CLI_CODE_PRESTATIONEXTERNE = '' THEN CLI_DONN_DESCRIPTIONCOURTE ELSE CLI_CODE_PRESTATIONEXTERNE END AS CLI_CODE_PRESTATION,
					date(CLI_DATE_DEBPREST) AS CLI_DATE_DEBPREST,
					date(COALESCE(CLI_DATE_FINPREST,CLI_DATE_DEBPREST)) AS CLI_DATE_FINPREST,	
					CASE WHEN CLI_CODE_PRESTATIONEXTERNE = '' THEN CLI_DONN_QTE ELSE 1 END * (CASE WHEN CLI_FLAG_SENS = 1 THEN 1 ELSE -1 END)  AS CLI_DONN_QTE,
					CASE WHEN CLI_CODE_PRESTATIONEXTERNE <> '' THEN CLI_DONN_QTE ELSE 1 END AS CLI_DONN_COEFF,
					CLI_DONN_TAUX,
					CLI_DONN_TU,
					CASE WHEN CLI_FLAG_SENS = 1 THEN CLI_DONN_MONTANTLIG ELSE 0 - CLI_DONN_MONTANTLIG END AS CLI_DONN_MONTANTLIG,
					CLI_CODE_PRATICIEN,
					CLI_CODE_PRATICIENINTERNE,
					CLI_DONN_NOOBJINTFAC
				FROM prod_clinicom.FD_FAC_DETFACLIG JOIN w_ADMISSIONS_select ON FD_FAC_DETFACLIG.cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT
					JOIN w_FD_FAC_DETFAC ON FD_FAC_DETFACLIG.CLI_DONN_NOABSEPISODE = w_FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE AND
						FD_FAC_DETFACLIG.CLI_DONN_NOOBJINTFAC = w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET AND
						FD_FAC_DETFACLIG.CLI_DONN_EXERCOMPTA = w_FD_FAC_DETFAC.CLI_DONN_EXERCOMPTA
					JOIN prod_clinicom.FB_FAC_PRESTATION ON FD_FAC_DETFACLIG.CLI_CODE_PRESTATION = FB_FAC_PRESTATION.CLI_CODE_PRESTATION;
					
					
				-- Suppression des lignes de forfait journalier en trop
								
				DELETE FROM w_FD_FAC_DETFACLIG
				USING
				(
					SELECT
						w_FD_FAC_DETFAC.no_facture,
						CLI_DONN_MONTANTLIG , CLI_DONN_MONTANT,
						CLI_DONN_MONTANTLIG - CLI_DONN_MONTANT AS CLI_DONN_MONTANTDIFF
					FROM w_FD_FAC_DETFAC
					JOIN (
						SELECT
							no_facture,
							SUM(CLI_DONN_MONTANTLIG) AS CLI_DONN_MONTANTLIG
						FROM w_FD_FAC_DETFACLIG
						GROUP BY 1
						) subview ON subview.no_facture = w_FD_FAC_DETFAC.no_facture
					WHERE CLI_DONN_MONTANTLIG <> CLI_DONN_MONTANT
				) subview 
				WHERE subview.no_facture = w_FD_FAC_DETFACLIG.no_facture AND subview.CLI_DONN_MONTANTDIFF = w_FD_FAC_DETFACLIG.CLI_DONN_MONTANTLIG;


						
		]]></sqlcmd>
		</NODE>
	
		<NODE label="Extraction PMSI">

			<sqlcmd><![CDATA[
			
				SELECT activite.cti_reorganize_sejour_pmsi()
				;
			
				INSERT INTO w_FD_FAC_DETFACLIG
				SELECT
					nextval('w_FD_FAC_DETFACLIG_sequence'::regclass) AS oid,
					0::bigint AS FD_ACTES_CCAM_id,

					p_sejours.no_sejour,
					p_sejours.oid AS sejour_id,
					p_sejours.no_sejour || '-BG' AS no_facture,
					p_rss.oid AS rss_id,
					p_rss.no_rss AS no_rss,
					t_ghs.oid AS ghs_id,
					'0'::text AS inclus_dans_sejour,

					p_sejours.code_original AS CLI_DONN_NOABSEPISODE,
					0::numeric AS CLI_DONN_NOOBJETFAC,
					0 - p_rss.oid AS CLI_CODE_NOINTERNEOBJET,
					'PMSI' AS CLI_CODE_OBJETFACTURATION,
					'LQ'::text AS CLI_CODE_ETAT,
					1::numeric AS CLI_FLAG_SENS,
					date_part('year', p_rss.date_sortie) AS CLI_DONN_EXERCOMPTA,
					
					''::text AS CLI_CODE_MEDECINREALISATEUR,
					''::text AS CLI_CODE_CCAM,
					''::text AS CLI_CODE_PHASE,
					''::text AS CLI_CODE_ACTIVITE,
					''::text AS CLI_DONN_EXTDOC,
					''::text AS CLI_DONN_MODIFICATEUR,
					''::text AS CLI_SEQUENCE1ACTE,
					''::text AS CLI_SEQUENCE2ACTE,
					
					''::text AS CLI_CODE_ORDONNATEUR,
					trim(t_prestations_pmsi.code) AS CLI_CODE_PRESTATION,
					p_rsf_detail.date_debut AS CLI_DATE_DEBPREST,
					p_rsf_detail.date_fin AS CLI_DATE_FINPREST,	
					p_rsf_detail.nombre AS CLI_DONN_QTE,
					p_rsf_detail.coefficient AS CLI_DONN_COEFF,
					p_rsf_detail.taux_remboursement AS CLI_DONN_TAUX,
					p_rsf_detail.prix_unitaire AS CLI_DONN_TU,
					round(p_rsf_detail.sejour_remboursable,2) AS CLI_DONN_MONTANTLIG,
					0::numeric AS CLI_CODE_PRATICIEN,
					''::text AS CLI_CODE_PRATICIENINTERNE,
					0 - p_rss.oid AS CLI_DONN_NOOBJINTFAC
				FROM activite[PX].p_sejours 
					JOIN activite.p_sejour_pmsi ON p_sejours.no_sejour = p_sejour_pmsi.no_sejour
					JOIN pmsi.p_rss ON p_sejour_pmsi.rss_id = p_rss.oid AND 
						p_rss.traitement_epmsi IN ('30','31','32','33') AND en_cours = '0' AND p_rss.etat = '' 
					JOIN pmsi.p_rsf_detail ON p_rss.oid = p_rsf_detail.rss_id AND est_ligne_rss = '1' AND p_rsf_detail.sejour_remboursable <> 0
					JOIN pmsi.t_prestations t_prestations_pmsi ON prestation_id = t_prestations_pmsi.oid  AND 
						t_prestations_pmsi.code NOT IN ('BHN', 'PHN', 'ELMA', 'HYP', 'PB', 'PB  F', 'PHU', 'PNDL', 'SAP', 'SCOL', 'SIND')
					LEFT JOIN pmsi.t_ghs t_ghs_pmsi ON p_rss.ghs_id = t_ghs_pmsi.oid
					LEFT JOIN base.t_ghs ON t_ghs_pmsi.code = t_ghs.code;


				-- Plusieurs RSS pour le meme sejour
				SELECT base.cti_execute('	
				UPDATE w_FD_FAC_DETFACLIG 
				SET no_facture = w_FD_FAC_DETFACLIG.no_facture || ''-'' || suffixe
				FROM (
					SELECT no_facture, MAX(rss_id) AS rss_id, count(*) AS suffixe
					FROM w_FD_FAC_DETFACLIG
					WHERE CLI_CODE_OBJETFACTURATION = ''PMSI''
					GROUP BY 1
					HAVING COUNT(DISTINCT rss_id) > 1
				) subview
				WHERE w_FD_FAC_DETFACLIG.no_facture = subview.no_facture AND
					w_FD_FAC_DETFACLIG.rss_id = subview.rss_id',50);

				


				UPDATE activite[PX].p_sejours SET ghs_id = w_FD_FAC_DETFACLIG.ghs_id
				FROM w_FD_FAC_DETFACLIG
				WHERE p_sejours.no_sejour = w_FD_FAC_DETFACLIG.no_sejour AND 
					CLI_CODE_OBJETFACTURATION = 'PMSI' AND w_FD_FAC_DETFACLIG.ghs_id <> 0 AND 
					CLI_CODE_PRESTATION = 'GHS' AND p_sejours.ghs_id IS DISTINCT FROM w_FD_FAC_DETFACLIG.ghs_id;



				INSERT INTO w_FD_FAC_DETFAC
				SELECT
					w_FD_FAC_DETFACLIG.no_sejour,
					w_FD_FAC_DETFACLIG.sejour_id,
					w_FD_FAC_DETFACLIG.no_facture,
					''::text AS no_facture_avoir,
					MAX(w_FD_FAC_DETFACLIG.ghs_id),

					'1'::text AS type_tiers_payant,
					MAX(w_MVTS_EPISODES_debiteurs.tiers_payant_0_id),
					MAX(w_MVTS_EPISODES_debiteurs.tiers_payant_1_id),
					MAX(w_MVTS_EPISODES_debiteurs.tiers_payant_2_id),

					'1'::text AS est_budget_global,
					'1'::text AS est_pmsi,
					rss_id,
					w_FD_FAC_DETFACLIG.CLI_DONN_NOABSEPISODE,
					MAX(CLI_DONN_EXERCOMPTA),
					MAX(CLI_DONN_NOOBJETFAC),
					1::numeric AS CLI_CODE_TYPEDEBITEUR,
					''::text AS CLI_CODE_ORGANISME,
					MAX(CLI_FLAG_SENS),
					MAX(CLI_CODE_OBJETFACTURATION),
					MAX(date_trunc('month',CLI_DATE_FINPREST) + interval '2 months' - interval '1 day') AS CLI_DATE_OBJETFAC,
					MIN(CLI_DATE_DEBPREST) AS CLI_DATE_DEBPERIODE,
					MAX(CLI_DATE_FINPREST) AS CLI_DATE_FINPERIODE,
					MAX(CLI_CODE_ETAT),
					SUM(CLI_DONN_MONTANTLIG),
					0::numeric AS CLI_DONN_EXERCTX,
					0::numeric AS CLI_DONN_NOOBJETCTX,
					MAX(CLI_CODE_NOINTERNEOBJET)
				FROM w_FD_FAC_DETFACLIG
					JOIN w_MVTS_EPISODES_debiteurs ON w_MVTS_EPISODES_debiteurs.CLI_DONN_NOABSEPISODE = w_FD_FAC_DETFACLIG.CLI_DONN_NOABSEPISODE
				WHERE CLI_CODE_OBJETFACTURATION= 'PMSI'
				GROUP BY 1,2,3,12,13;
				
						
		]]></sqlcmd>
		</NODE>
			
		<NODE label="Actes CCAM">

			<sqlcmd><![CDATA[
				CREATE INDEX w_FD_FAC_DETFAC_i1
				ON w_FD_FAC_DETFAC
				USING btree
				(no_sejour);

				CREATE INDEX w_FD_FAC_DETFAC_i2
				ON w_FD_FAC_DETFAC
				USING btree
				(no_facture);

				CREATE INDEX w_FD_FAC_DETFAC_i3
				ON w_FD_FAC_DETFAC
				USING btree
				(CLI_DONN_NOABSEPISODE);


				
				CREATE INDEX w_FD_FAC_DETFACLIG_i1
				ON w_FD_FAC_DETFACLIG
				USING btree
				(no_sejour);

				CREATE INDEX w_FD_FAC_DETFACLIG_i2
				ON w_FD_FAC_DETFACLIG
				USING btree
				(no_facture);

				CREATE INDEX w_FD_FAC_DETFACLIG_i3
				ON w_FD_FAC_DETFACLIG
				USING btree
				(CLI_DONN_NOABSEPISODE);

				CREATE INDEX w_FD_FAC_DETFACLIG_i4
				ON w_FD_FAC_DETFACLIG
				USING btree
				(CLI_CODE_PRESTATION);
								
				CREATE INDEX w_FD_FAC_DETFACLIG_i5
				ON w_FD_FAC_DETFACLIG
				USING btree
				(CLI_CODE_MEDECINREALISATEUR);
				
				CREATE INDEX w_FD_FAC_DETFACLIG_i6
				ON w_FD_FAC_DETFACLIG
				USING btree
				(CLI_CODE_CCAM);





				UPDATE w_FD_FAC_DETFAC SET CLI_DONN_NOOBJETCTX = w_FD_FAC_DETFAC_avoir.CLI_DONN_NOOBJETFAC
				FROM w_FD_FAC_DETFAC w_FD_FAC_DETFAC_avoir
				WHERE w_FD_FAC_DETFAC.CLI_DONN_NOOBJETFAC = w_FD_FAC_DETFAC_avoir.CLI_DONN_NOOBJETCTX AND
					w_FD_FAC_DETFAC_avoir.CLI_DONN_NOOBJETCTX <> 0;




				DROP TABLE IF EXISTS w_FD_ACTES_CCAM;
				CREATE TEMP TABLE w_FD_ACTES_CCAM AS
				SELECT 
					no_sejour,
					''::text AS no_facture_1,
					''::text AS no_facture_2,
					''::text AS no_facture_0,
					FD_ACTES_CCAM.CLI_DONN_NOABSEPISODE,
					FD_ACTES_CCAM.CLI_DONN_LIEN,
					FD_ACTES_CCAM.CLI_DONN_NUMSEQUENCE,
					FD_ACTES_ENTETE.CLI_DATE_SAISIE,
					FD_ACTES_CCAM.CLI_CODE_CCAM,
					FD_ACTES_CCAM.CLI_CODE_TABLECCAM,
					FD_ACTES_CCAM.CLI_DONN_NOMBRE,
					FD_ACTES_CCAM.CLI_CODE_PHASE,
					FD_ACTES_CCAM.CLI_CODE_ACTIVITE,
					FD_ACTES_CCAM.CLI_CODE_MEDECINREALISATEUR,
					FD_ACTES_CCAM.CLI_DONN_EXTDOC,
					FD_ACTES_CCAM.CLI_CODE_ASSNP,
					FD_ACTES_CCAM.CLI_FLAG_REMBEXC,
					FD_ACTES_CCAM.CLI_FLAG_PRIVE,
					FD_ACTES_CCAM.CLI_DONN_TAUXSECU,
					FD_ACTES_CCAM.CLI_FLAG_ACTEGRATUIT,
					FD_ACTES_CCAM.CLI_DONN_MODIFICATEUR,
					FD_ACTES_CCAM.CLI_FLAG_REGIE,
					FD_ACTES_CCAM.CLI_DONN_MNTDEPHONORAIRE,
					FD_ACTES_CCAM.CLI_FLAG_DISPOSITIFPREV,
					FD_ACTES_CCAM.CLI_CODE_CODEASSOC,
					FD_ACTES_CCAM.CLI_CODE_REGROUPEMENTFACTURATION,
					FD_ACTES_CCAM.CLI_FLAG_DEPASSEMENTAUTORISE,
					COALESCE(FB_FAC_ACTECCAMPHASEHISTO.CLI_DONN_PRIXUNITAIRE,0) AS CLI_DONN_PRIXUNITAIRE,
					CASE WHEN FD_ACTES_CCAM.CLI_CODE_CODEASSOC <> 'X' THEN COALESCE(FB_FAC_ASSONPCOEFF.CLI_DONN_COEFFICIENT,1) ELSE 0 END AS CLI_DONN_COEFFICIENT_ANP,
					COALESCE(FB_FAC_ASSOMODIFICATEURCOEFF_1.CLI_DONN_FORFAIT,0) AS CLI_DONN_FORFAIT_M1,
					COALESCE(FB_FAC_ASSOMODIFICATEURCOEFF_1.CLI_DONN_COEFFICIENT,1) AS CLI_DONN_COEFFICIENT_M1,
					COALESCE(FB_FAC_ASSOMODIFICATEURCOEFF_2.CLI_DONN_FORFAIT,0) AS CLI_DONN_FORFAIT_M2,
					COALESCE(FB_FAC_ASSOMODIFICATEURCOEFF_2.CLI_DONN_COEFFICIENT,1) AS CLI_DONN_COEFFICIENT_M2,
					COALESCE(FB_FAC_ASSOMODIFICATEURCOEFF_3.CLI_DONN_FORFAIT,0) AS CLI_DONN_FORFAIT_M3,
					COALESCE(FB_FAC_ASSOMODIFICATEURCOEFF_3.CLI_DONN_COEFFICIENT,1) AS CLI_DONN_COEFFICIENT_M3,
					COALESCE(FB_FAC_ASSOMODIFICATEURCOEFF_4.CLI_DONN_FORFAIT,0) AS CLI_DONN_FORFAIT_M4,
					COALESCE(FB_FAC_ASSOMODIFICATEURCOEFF_4.CLI_DONN_COEFFICIENT,1) AS CLI_DONN_COEFFICIENT_M4,
					1::numeric AS coefficient,
					CLI_DONN_PRIXUNITAIRE AS prix_unitaire,
					0::numeric AS taux_1,
					0::numeric AS taux_2,
					0::numeric AS taux_0,
					'0'::text AS acte_inclus_dans_sejour_0,
					'0'::text AS acte_inclus_dans_sejour_1,
					'0'::text AS acte_inclus_dans_sejour_2,
					0::numeric AS montant_facture,
					0::numeric AS montant_facture_0,
					0::numeric AS montant_facture_1,
					0::numeric AS montant_facture_2,
					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
				FROM prod_clinicom.FD_ACTES_CCAM JOIN w_ADMISSIONS_select ON FD_ACTES_CCAM.cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT 
				JOIN prod_clinicom.FD_ACTES_ENTETE ON FD_ACTES_CCAM.CLI_DONN_NOABSEPISODE = FD_ACTES_ENTETE.CLI_DONN_NOABSEPISODE AND
						FD_ACTES_CCAM.CLI_DONN_LIEN = FD_ACTES_ENTETE.CLI_DONN_LIEN 
				LEFT JOIN prod_clinicom.FB_FAC_ACTECCAMPHASEHISTO ON FD_ACTES_CCAM.CLI_CODE_CCAM = FB_FAC_ACTECCAMPHASEHISTO.CLI_CODE_ACTE AND
					FD_ACTES_CCAM.CLI_CODE_ACTIVITE = FB_FAC_ACTECCAMPHASEHISTO.CLI_CODE_ACTIVITE AND
					FD_ACTES_CCAM.CLI_CODE_PHASE = FB_FAC_ACTECCAMPHASEHISTO.CLI_CODE_PHASE AND
					FD_ACTES_ENTETE.CLI_DATE_SAISIE BETWEEN CLI_DATE_EFFET AND COALESCE(CLI_DATE_FINEFFET,'2099-12-31 00:00:00') - interval '1 second'
				LEFT JOIN prod_clinicom.FB_FAC_ASSONPCOEFF ON FD_ACTES_CCAM.CLI_CODE_CODEASSOC = FB_FAC_ASSONPCOEFF.CLI_CODE_ASSOCIATIONACTE AND
					FD_ACTES_ENTETE.CLI_DATE_SAISIE BETWEEN FB_FAC_ASSONPCOEFF.CLI_DATE_DEBUT AND COALESCE(FB_FAC_ASSONPCOEFF.CLI_DATE_FIN,'2099-12-31 00:00:00') - interval '1 second'
				LEFT JOIN prod_clinicom.FB_FAC_ASSOMODIFICATEURCOEFF FB_FAC_ASSOMODIFICATEURCOEFF_1 ON SUBSTR(FD_ACTES_CCAM.CLI_DONN_MODIFICATEUR,1,1) = FB_FAC_ASSOMODIFICATEURCOEFF_1.CLI_CODE_MODIFICATEUR AND
					FD_ACTES_ENTETE.CLI_DATE_SAISIE BETWEEN FB_FAC_ASSOMODIFICATEURCOEFF_1.CLI_DATE_DEBUT AND COALESCE(FB_FAC_ASSOMODIFICATEURCOEFF_1.CLI_DATE_FIN,'2099-12-31 00:00:00') - interval '1 second'
				LEFT JOIN prod_clinicom.FB_FAC_ASSOMODIFICATEURCOEFF FB_FAC_ASSOMODIFICATEURCOEFF_2 ON SUBSTR(FD_ACTES_CCAM.CLI_DONN_MODIFICATEUR,2,1) = FB_FAC_ASSOMODIFICATEURCOEFF_2.CLI_CODE_MODIFICATEUR AND
					FD_ACTES_ENTETE.CLI_DATE_SAISIE BETWEEN FB_FAC_ASSOMODIFICATEURCOEFF_2.CLI_DATE_DEBUT AND COALESCE(FB_FAC_ASSOMODIFICATEURCOEFF_2.CLI_DATE_FIN,'2099-12-31 00:00:00') - interval '1 second'
				LEFT JOIN prod_clinicom.FB_FAC_ASSOMODIFICATEURCOEFF FB_FAC_ASSOMODIFICATEURCOEFF_3 ON SUBSTR(FD_ACTES_CCAM.CLI_DONN_MODIFICATEUR,3,1) = FB_FAC_ASSOMODIFICATEURCOEFF_3.CLI_CODE_MODIFICATEUR AND
					FD_ACTES_ENTETE.CLI_DATE_SAISIE BETWEEN FB_FAC_ASSOMODIFICATEURCOEFF_3.CLI_DATE_DEBUT AND COALESCE(FB_FAC_ASSOMODIFICATEURCOEFF_3.CLI_DATE_FIN,'2099-12-31 00:00:00') - interval '1 second'
				LEFT JOIN prod_clinicom.FB_FAC_ASSOMODIFICATEURCOEFF FB_FAC_ASSOMODIFICATEURCOEFF_4 ON SUBSTR(FD_ACTES_CCAM.CLI_DONN_MODIFICATEUR,4,1) = FB_FAC_ASSOMODIFICATEURCOEFF_4.CLI_CODE_MODIFICATEUR AND
					FD_ACTES_ENTETE.CLI_DATE_SAISIE BETWEEN FB_FAC_ASSOMODIFICATEURCOEFF_4.CLI_DATE_DEBUT AND COALESCE(FB_FAC_ASSOMODIFICATEURCOEFF_4.CLI_DATE_FIN,'2099-12-31 00:00:00') - interval '1 second'
				JOIN activite[PX].p_sejours ON FD_ACTES_CCAM.CLI_DONN_NOABSEPISODE = p_sejours.code_original AND p_sejours.etat = '';



				CREATE INDEX w_FD_ACTES_CCAM_i1
				ON w_FD_ACTES_CCAM
				USING btree
				(no_sejour);

				CREATE INDEX w_FD_ACTES_CCAM_i2
				ON w_FD_ACTES_CCAM
				USING btree
				(CLI_DATE_SAISIE);

				
		

				UPDATE w_FD_ACTES_CCAM
				SET prix_unitaire = round(
						CLI_DONN_COEFFICIENT_ANP * 
						(CLI_DONN_PRIXUNITAIRE + 
						(CLI_DONN_PRIXUNITAIRE * CLI_DONN_COEFFICIENT_M1) - CLI_DONN_PRIXUNITAIRE + 
						(CLI_DONN_PRIXUNITAIRE * CLI_DONN_COEFFICIENT_M2) - CLI_DONN_PRIXUNITAIRE + 
						(CLI_DONN_PRIXUNITAIRE * CLI_DONN_COEFFICIENT_M3) - CLI_DONN_PRIXUNITAIRE + 
						(CLI_DONN_PRIXUNITAIRE * CLI_DONN_COEFFICIENT_M4) - CLI_DONN_PRIXUNITAIRE) +
						CLI_DONN_FORFAIT_M1 +
						CLI_DONN_FORFAIT_M2 +
						CLI_DONN_FORFAIT_M3 +
						CLI_DONN_FORFAIT_M4
						,2)
				WHERE CLI_DONN_PRIXUNITAIRE <> 0 AND
					(CLI_DONN_COEFFICIENT_ANP <> 1 OR 
					CLI_DONN_COEFFICIENT_M1 <> 1 OR 
					CLI_DONN_COEFFICIENT_M2 <> 1 OR 
					CLI_DONN_COEFFICIENT_M3 <> 1 OR 
					CLI_DONN_COEFFICIENT_M4 <> 1 OR
					CLI_DONN_FORFAIT_M1 <> 0 OR
					CLI_DONN_FORFAIT_M2 <> 0 OR
					CLI_DONN_FORFAIT_M3 <> 0 OR
					CLI_DONN_FORFAIT_M4 <> 0) ;


				DROP TABLE IF EXISTS w_FD_FAC_DETFAC_inclus;
				CREATE TEMP TABLE w_FD_FAC_DETFAC_inclus AS
				SELECT w_FD_FAC_DETFACLIG.no_sejour, 
					CLI_DATE_DEBPERIODE, 
					CLI_DATE_FINPERIODE,
					MAX(CASE WHEN CLI_CODE_PRESTATION IN ('GHS', 'TM', 'ESTH') AND CLI_DONN_TAUX > 0 THEN '1' ELSE '0' END) AS q_ghs,
					MAX(CASE WHEN type_tiers_payant = '0' THEN w_FD_FAC_DETFACLIG.no_facture ELSE '' END) AS no_facture_0, 
					MAX(CASE WHEN type_tiers_payant = '1' THEN w_FD_FAC_DETFACLIG.no_facture ELSE '' END) AS no_facture_1, 
					MAX(CASE WHEN type_tiers_payant = '2' THEN w_FD_FAC_DETFACLIG.no_facture ELSE '' END) AS no_facture_2, 
					MAX(CASE WHEN type_tiers_payant = '0' THEN CLI_DONN_TAUX ELSE 0 END) AS CLI_DONN_TAUX_0, 
					MAX(CASE WHEN type_tiers_payant = '1' THEN CLI_DONN_TAUX ELSE 0 END) AS CLI_DONN_TAUX_1, 
					MAX(CASE WHEN type_tiers_payant = '2' THEN CLI_DONN_TAUX ELSE 0 END) AS CLI_DONN_TAUX_2,
					MAX(CASE WHEN type_tiers_payant = '0' THEN '1' ELSE '0' END) AS acte_inclus_dans_sejour_0, 
					MAX(CASE WHEN type_tiers_payant = '1' THEN '1' ELSE '0' END) AS acte_inclus_dans_sejour_1, 
					MAX(CASE WHEN type_tiers_payant = '2' THEN '1' ELSE '0' END) AS acte_inclus_dans_sejour_2
				FROM w_FD_FAC_DETFACLIG
					JOIN w_FD_FAC_DETFAC ON w_FD_FAC_DETFACLIG.CLI_DONN_NOABSEPISODE = w_FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE AND
						w_FD_FAC_DETFACLIG.CLI_Donn_NoObjIntFac = w_FD_FAC_DETFAC.CLI_Code_NoInterneObjet 
				WHERE CLI_CODE_PRESTATION IN ('GHS', 'TM', 'ESTH', 'ACO', 'ADA', 'ADC', 'ADE', 'ADI', 'ATM', 'DEN') AND CLI_DONN_TAUX > 0
				GROUP BY 1,2,3;
				
				
				CREATE INDEX w_FD_FAC_DETFAC_inclus_i1
				ON w_FD_FAC_DETFAC_inclus
				USING btree
				(no_sejour);

				CREATE INDEX w_FD_FAC_DETFAC_inclus_i2
				ON w_FD_FAC_DETFAC_inclus
				USING btree
				(CLI_DATE_DEBPERIODE);





				-- Actes inclus dans séjours (budget global)
				UPDATE w_FD_ACTES_CCAM
				SET no_facture_1 = w_FD_FAC_DETFAC_inclus.no_facture_1,
					taux_1 = 100,
					acte_inclus_dans_sejour_1 = '1'
				FROM w_FD_FAC_DETFAC_inclus
				WHERE w_FD_FAC_DETFAC_inclus.no_sejour = w_FD_ACTES_CCAM.no_sejour AND
					date(CLI_DATE_SAISIE) BETWEEN CLI_DATE_DEBPERIODE AND CLI_DATE_FINPERIODE AND
					CLI_DATE_DEBPERIODE = CLI_DATE_FINPERIODE AND
					w_FD_FAC_DETFAC_inclus.acte_inclus_dans_sejour_1 = '1' AND
					w_FD_ACTES_CCAM.no_facture_1 = '' AND w_FD_ACTES_CCAM.no_facture_2 = '' AND w_FD_ACTES_CCAM.no_facture_0 = '';
					
				UPDATE w_FD_ACTES_CCAM
				SET no_facture_2 = w_FD_FAC_DETFAC_inclus.no_facture_2,
					taux_2 = 100,
					acte_inclus_dans_sejour_2 = '1'
				FROM w_FD_FAC_DETFAC_inclus
				WHERE w_FD_FAC_DETFAC_inclus.no_sejour = w_FD_ACTES_CCAM.no_sejour AND
					date(CLI_DATE_SAISIE) BETWEEN CLI_DATE_DEBPERIODE AND CLI_DATE_FINPERIODE AND
					CLI_DATE_DEBPERIODE = CLI_DATE_FINPERIODE AND
					w_FD_FAC_DETFAC_inclus.acte_inclus_dans_sejour_2 = '1' AND
					w_FD_ACTES_CCAM.no_facture_1 = '' AND w_FD_ACTES_CCAM.no_facture_2 = '' AND w_FD_ACTES_CCAM.no_facture_0 = '';

				UPDATE w_FD_ACTES_CCAM
				SET no_facture_0 = w_FD_FAC_DETFAC_inclus.no_facture_0,
					taux_0 = 100,
					acte_inclus_dans_sejour_0 = '1'
				FROM w_FD_FAC_DETFAC_inclus
				WHERE w_FD_FAC_DETFAC_inclus.no_sejour = w_FD_ACTES_CCAM.no_sejour AND
					date(CLI_DATE_SAISIE) BETWEEN CLI_DATE_DEBPERIODE AND CLI_DATE_FINPERIODE AND
					CLI_DATE_DEBPERIODE = CLI_DATE_FINPERIODE AND
					w_FD_FAC_DETFAC_inclus.acte_inclus_dans_sejour_0 = '1' AND
					w_FD_ACTES_CCAM.no_facture_1 = '' AND w_FD_ACTES_CCAM.no_facture_2 = '' AND w_FD_ACTES_CCAM.no_facture_0 = '';

					
				UPDATE w_FD_ACTES_CCAM
				SET no_facture_1 = w_FD_FAC_DETFAC_inclus.no_facture_1,
					taux_1 = 100,
					acte_inclus_dans_sejour_1 = '1'
				FROM w_FD_FAC_DETFAC_inclus
				WHERE w_FD_FAC_DETFAC_inclus.no_sejour = w_FD_ACTES_CCAM.no_sejour AND
					date(CLI_DATE_SAISIE) BETWEEN CLI_DATE_DEBPERIODE AND CLI_DATE_FINPERIODE AND
					w_FD_FAC_DETFAC_inclus.acte_inclus_dans_sejour_1 = '1' AND
					w_FD_ACTES_CCAM.no_facture_1 = '' AND w_FD_ACTES_CCAM.no_facture_2 = '' AND w_FD_ACTES_CCAM.no_facture_0 = '';
					
					
				UPDATE w_FD_ACTES_CCAM
				SET no_facture_2 = w_FD_FAC_DETFAC_inclus.no_facture_2,
					taux_2 = 100,
					acte_inclus_dans_sejour_2 = '1'
				FROM w_FD_FAC_DETFAC_inclus
				WHERE w_FD_FAC_DETFAC_inclus.no_sejour = w_FD_ACTES_CCAM.no_sejour AND
					date(CLI_DATE_SAISIE) BETWEEN CLI_DATE_DEBPERIODE AND CLI_DATE_FINPERIODE AND
					w_FD_FAC_DETFAC_inclus.acte_inclus_dans_sejour_2 = '1' AND
					w_FD_ACTES_CCAM.no_facture_1 = '' AND w_FD_ACTES_CCAM.no_facture_2 = '' AND w_FD_ACTES_CCAM.no_facture_0 = '';
					
				UPDATE w_FD_ACTES_CCAM
				SET no_facture_0 = w_FD_FAC_DETFAC_inclus.no_facture_0,
					taux_0 = 100,
					acte_inclus_dans_sejour_0 = '1'
				FROM w_FD_FAC_DETFAC_inclus
				WHERE w_FD_FAC_DETFAC_inclus.no_sejour = w_FD_ACTES_CCAM.no_sejour AND
					date(CLI_DATE_SAISIE) BETWEEN CLI_DATE_DEBPERIODE AND CLI_DATE_FINPERIODE AND
					w_FD_FAC_DETFAC_inclus.acte_inclus_dans_sejour_0 = '1' AND
					w_FD_ACTES_CCAM.no_facture_1 = '' AND w_FD_ACTES_CCAM.no_facture_2 = '' AND w_FD_ACTES_CCAM.no_facture_0 = '';



				UPDATE w_FD_ACTES_CCAM
				SET montant_facture_actes_inclus_dans_sejour = round(CLI_DONN_NOMBRE * prix_unitaire * taux_1 / 100,2),
					montant_facture_1_actes_inclus_dans_sejour = round(CLI_DONN_NOMBRE * prix_unitaire * taux_1 / 100,2)
				WHERE acte_inclus_dans_sejour_1 = '1';

				UPDATE w_FD_ACTES_CCAM
				SET montant_facture_actes_inclus_dans_sejour = montant_facture_actes_inclus_dans_sejour + round(CLI_DONN_NOMBRE * prix_unitaire * taux_2 / 100,2),
					montant_facture_2_actes_inclus_dans_sejour = round(CLI_DONN_NOMBRE * prix_unitaire * taux_2 / 100,2)
				WHERE acte_inclus_dans_sejour_2 = '1';

				UPDATE w_FD_ACTES_CCAM
				SET montant_facture_actes_inclus_dans_sejour = montant_facture_actes_inclus_dans_sejour + round(CLI_DONN_NOMBRE * prix_unitaire * taux_0 / 100,2),
					montant_facture_2_actes_inclus_dans_sejour = round(CLI_DONN_NOMBRE * prix_unitaire * taux_0 / 100,2)
				WHERE acte_inclus_dans_sejour_0 = '1';


				
				


			

				DROP TABLE IF EXISTS w_FD_FAC_DETFACLIG_taux;
				CREATE TEMP TABLE w_FD_FAC_DETFACLIG_taux AS
				SELECT w_FD_FAC_DETFACLIG.no_sejour, 
					CLI_CODE_PRESTATION, 
					CLI_DONN_TU, 
					MAX(CASE WHEN type_tiers_payant = '1' THEN w_FD_FAC_DETFACLIG.no_facture ELSE '' END) AS no_facture_1, 
					MAX(CASE WHEN type_tiers_payant = '2' THEN w_FD_FAC_DETFACLIG.no_facture ELSE '' END) AS no_facture_2, 
					MAX(CASE WHEN type_tiers_payant = '0' THEN w_FD_FAC_DETFACLIG.no_facture ELSE '' END) AS no_facture_0, 
					MAX(CASE WHEN type_tiers_payant = '1' THEN CLI_DONN_TAUX ELSE 0 END) AS CLI_DONN_TAUX_1, 
					MAX(CASE WHEN type_tiers_payant = '2' THEN CLI_DONN_TAUX ELSE 0 END) AS CLI_DONN_TAUX_2, 
					MAX(CASE WHEN type_tiers_payant = '0' THEN CLI_DONN_TAUX ELSE 0 END) AS CLI_DONN_TAUX_0
				FROM w_FD_FAC_DETFACLIG
						JOIN w_FD_FAC_DETFAC ON w_FD_FAC_DETFACLIG.CLI_DONN_NOABSEPISODE = w_FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE AND
							w_FD_FAC_DETFACLIG.CLI_Donn_NoObjIntFac = w_FD_FAC_DETFAC.CLI_Code_NoInterneObjet AND
							CLI_DONN_NOOBJETCTX = 0
				WHERE CLI_CODE_PRESTATION IN (SELECT CLI_CODE_REGROUPEMENTFACTURATION FROM w_FD_ACTES_CCAM)
				GROUP BY 1,2,3
				ORDER BY 1,2,3;


				DROP TABLE IF EXISTS w_FD_FAC_DETFACLIG_tauxb;
				CREATE TEMP TABLE w_FD_FAC_DETFACLIG_tauxb AS
				SELECT w_FD_FAC_DETFACLIG.no_sejour, 
					CLI_CODE_PRESTATION, 
					MAX(CASE WHEN type_tiers_payant = '1' THEN w_FD_FAC_DETFACLIG.no_facture ELSE '' END) AS no_facture_1, 
					MAX(CASE WHEN type_tiers_payant = '2' THEN w_FD_FAC_DETFACLIG.no_facture ELSE '' END) AS no_facture_2, 
					MAX(CASE WHEN type_tiers_payant = '0' THEN w_FD_FAC_DETFACLIG.no_facture ELSE '' END) AS no_facture_0, 
					MAX(CASE WHEN type_tiers_payant = '1' THEN CLI_DONN_TAUX ELSE 0 END) AS CLI_DONN_TAUX_1, 
					MAX(CASE WHEN type_tiers_payant = '2' THEN CLI_DONN_TAUX ELSE 0 END) AS CLI_DONN_TAUX_2, 
					MAX(CASE WHEN type_tiers_payant = '0' THEN CLI_DONN_TAUX ELSE 0 END) AS CLI_DONN_TAUX_0
				FROM w_FD_FAC_DETFACLIG
						JOIN w_FD_FAC_DETFAC ON w_FD_FAC_DETFACLIG.CLI_DONN_NOABSEPISODE = w_FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE AND
							w_FD_FAC_DETFACLIG.CLI_Donn_NoObjIntFac = w_FD_FAC_DETFAC.CLI_Code_NoInterneObjet AND
							CLI_DONN_NOOBJETCTX = 0
				WHERE CLI_CODE_PRESTATION IN (SELECT CLI_CODE_REGROUPEMENTFACTURATION FROM w_FD_ACTES_CCAM)
				GROUP BY 1,2
				ORDER BY 1,2;



				UPDATE w_FD_ACTES_CCAM
				SET no_facture_0 = w_FD_FAC_DETFACLIG_taux.no_facture_0,
					taux_0 = w_FD_FAC_DETFACLIG_taux.CLI_DONN_TAUX_0
				FROM w_FD_FAC_DETFACLIG_taux
				WHERE w_FD_FAC_DETFACLIG_taux.no_sejour = w_FD_ACTES_CCAM.no_sejour AND
					CLI_CODE_REGROUPEMENTFACTURATION = CLI_CODE_PRESTATION AND
					w_FD_ACTES_CCAM.prix_unitaire = CLI_DONN_TU AND 
					w_FD_FAC_DETFACLIG_taux.CLI_DONN_TAUX_0 <> 0 AND
					w_FD_ACTES_CCAM.no_facture_0 = '' ;


				UPDATE w_FD_ACTES_CCAM
				SET no_facture_1 = w_FD_FAC_DETFACLIG_taux.no_facture_1,
					taux_1 = w_FD_FAC_DETFACLIG_taux.CLI_DONN_TAUX_1
				FROM w_FD_FAC_DETFACLIG_taux
				WHERE w_FD_FAC_DETFACLIG_taux.no_sejour = w_FD_ACTES_CCAM.no_sejour AND
					CLI_CODE_REGROUPEMENTFACTURATION = CLI_CODE_PRESTATION AND
					w_FD_ACTES_CCAM.prix_unitaire = CLI_DONN_TU AND
					w_FD_ACTES_CCAM.no_facture_1 = '' AND 
					w_FD_FAC_DETFACLIG_taux.CLI_DONN_TAUX_1 <> 0;

				UPDATE w_FD_ACTES_CCAM
				SET no_facture_1 = w_FD_FAC_DETFACLIG_taux.no_facture_1,
					taux_1 = w_FD_FAC_DETFACLIG_taux.CLI_DONN_TAUX_1
				FROM w_FD_FAC_DETFACLIG_taux
				WHERE w_FD_FAC_DETFACLIG_taux.no_sejour = w_FD_ACTES_CCAM.no_sejour AND
					CLI_CODE_REGROUPEMENTFACTURATION = CLI_CODE_PRESTATION AND
					w_FD_ACTES_CCAM.no_facture_1 = '' AND 
					w_FD_FAC_DETFACLIG_taux.CLI_DONN_TAUX_1 <> 0 AND
					taux_0 + taux_1 + taux_2 <> 100;



				UPDATE w_FD_ACTES_CCAM
				SET no_facture_0 = w_FD_FAC_DETFACLIG_taux.no_facture_0,
					taux_0 = w_FD_FAC_DETFACLIG_taux.CLI_DONN_TAUX_0
				FROM w_FD_FAC_DETFACLIG_tauxb w_FD_FAC_DETFACLIG_taux
				WHERE w_FD_FAC_DETFACLIG_taux.no_sejour = w_FD_ACTES_CCAM.no_sejour AND
					CLI_CODE_REGROUPEMENTFACTURATION = CLI_CODE_PRESTATION AND 
					w_FD_FAC_DETFACLIG_taux.CLI_DONN_TAUX_0 <> 0 AND
					taux_0 + taux_1 + taux_2 <> 100 AND
					w_FD_ACTES_CCAM.no_facture_0 = '';

				UPDATE w_FD_ACTES_CCAM
				SET no_facture_2 = w_FD_FAC_DETFACLIG_taux.no_facture_2,
					taux_2 = w_FD_FAC_DETFACLIG_taux.CLI_DONN_TAUX_2
				FROM w_FD_FAC_DETFACLIG_taux
				WHERE w_FD_FAC_DETFACLIG_taux.no_sejour = w_FD_ACTES_CCAM.no_sejour AND
					CLI_CODE_REGROUPEMENTFACTURATION = CLI_CODE_PRESTATION AND
					w_FD_ACTES_CCAM.prix_unitaire = CLI_DONN_TU AND
					w_FD_ACTES_CCAM.no_facture_2 = '' AND 
					w_FD_FAC_DETFACLIG_taux.CLI_DONN_TAUX_2 <> 0 ;

				UPDATE w_FD_ACTES_CCAM
				SET no_facture_2 = w_FD_FAC_DETFACLIG_taux.no_facture_2,
					taux_2 = w_FD_FAC_DETFACLIG_taux.CLI_DONN_TAUX_2
				FROM w_FD_FAC_DETFACLIG_tauxb w_FD_FAC_DETFACLIG_taux
				WHERE w_FD_FAC_DETFACLIG_taux.no_sejour = w_FD_ACTES_CCAM.no_sejour AND
					CLI_CODE_REGROUPEMENTFACTURATION = CLI_CODE_PRESTATION AND
					w_FD_ACTES_CCAM.no_facture_2 = '' AND 
					w_FD_FAC_DETFACLIG_taux.CLI_DONN_TAUX_2 <> 0 AND
					taux_0 + taux_1 + taux_2 <> 100;

				
				-- mise à jour actes facturés (externes)
				UPDATE w_FD_FAC_DETFACLIG SET
					CLI_CODE_MEDECINREALISATEUR = w_FD_ACTES_CCAM.CLI_CODE_MEDECINREALISATEUR,
					CLI_CODE_CCAM = w_FD_ACTES_CCAM.CLI_CODE_CCAM,
					CLI_CODE_PHASE = w_FD_ACTES_CCAM.CLI_CODE_PHASE,
					CLI_CODE_ACTIVITE = w_FD_ACTES_CCAM.CLI_CODE_ACTIVITE,
					CLI_DONN_EXTDOC = w_FD_ACTES_CCAM.CLI_DONN_EXTDOC,
					CLI_DONN_MODIFICATEUR = w_FD_ACTES_CCAM.CLI_DONN_MODIFICATEUR
				FROM w_FD_ACTES_CCAM
				WHERE w_FD_ACTES_CCAM.CLI_DONN_NOABSEPISODE = w_FD_FAC_DETFACLIG.CLI_DONN_NOABSEPISODE AND
					w_FD_ACTES_CCAM.CLI_DONN_LIEN = w_FD_FAC_DETFACLIG.CLI_SEQUENCE1ACTE AND
					w_FD_ACTES_CCAM.CLI_DONN_NUMSEQUENCE = w_FD_FAC_DETFACLIG.CLI_SEQUENCE2ACTE AND
					w_FD_FAC_DETFACLIG.CLI_CODE_PRATICIENINTERNE <> '' AND
					w_FD_FAC_DETFACLIG.CLI_CODE_PRESTATION IN ('ACO', 'ADA', 'ADC', 'ADE', 'ADI', 'ATM', 'DEN');
					
				
				
					
				-- Ajout inclus dans sejour

				INSERT INTO w_FD_FAC_DETFACLIG
				SELECT
					nextval('w_FD_FAC_DETFACLIG_sequence'::regclass) AS oid,
					0::bigint AS FD_ACTES_CCAM_id,

					w_FD_FAC_DETFAC.no_sejour,
					w_FD_FAC_DETFAC.sejour_id,
					w_FD_FAC_DETFAC.no_facture,
					0::bigint AS rss_id,
					0::bigint AS no_rss,
					0::bigint AS ghs_id,
					'1'::text AS inclus_dans_sejour,

					w_FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE,
					w_FD_FAC_DETFAC.CLI_DONN_NOOBJETFAC,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET,
					w_FD_FAC_DETFAC.CLI_CODE_OBJETFACTURATION,
					w_FD_FAC_DETFAC.CLI_CODE_ETAT,
					w_FD_FAC_DETFAC.CLI_FLAG_SENS,
					w_FD_FAC_DETFAC.CLI_DONN_EXERCOMPTA,
					
					CLI_CODE_MEDECINREALISATEUR,
					CLI_CODE_CCAM,
					CLI_CODE_PHASE,
					CLI_CODE_ACTIVITE,
					CLI_DONN_EXTDOC,
					CLI_DONN_MODIFICATEUR,
					''::text AS CLI_SEQUENCE1ACTE,
					''::text AS CLI_SEQUENCE2ACTE,
					
					''::text AS CLI_CODE_ORDONNATEUR,
					CLI_CODE_REGROUPEMENTFACTURATION AS CLI_CODE_PRESTATION,
					date(CLI_DATE_SAISIE) AS CLI_DATE_DEBPREST,
					date(CLI_DATE_SAISIE) AS CLI_DATE_FINPREST,	
					CLI_DONN_NOMBRE AS CLI_DONN_QTE,
					1 AS CLI_DONN_COEFF,
					w_FD_ACTES_CCAM.taux_1,
					w_FD_ACTES_CCAM.prix_unitaire,
					round(w_FD_ACTES_CCAM.prix_unitaire * w_FD_ACTES_CCAM.taux_1 * CLI_DONN_NOMBRE / 100,2) AS CLI_DONN_MONTANTLIG,
					0::numeric AS CLI_CODE_PRATICIEN,
					'' AS CLI_CODE_PRATICIENINTERNE,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET
				FROM w_FD_ACTES_CCAM
					JOIN w_FD_FAC_DETFAC ON w_FD_ACTES_CCAM.no_facture_1 = w_FD_FAC_DETFAC.no_facture
				WHERE w_FD_ACTES_CCAM.no_facture_1 <> '' AND
					acte_inclus_dans_sejour_1 = '1';


				INSERT INTO w_FD_FAC_DETFACLIG
				SELECT
					nextval('w_FD_FAC_DETFACLIG_sequence'::regclass) AS oid,
					0::bigint AS FD_ACTES_CCAM_id,

					w_FD_FAC_DETFAC.no_sejour,
					w_FD_FAC_DETFAC.sejour_id,
					w_FD_FAC_DETFAC.no_facture,
					0::bigint AS rss_id,
					0::bigint AS no_rss,
					0::bigint AS ghs_id,
					'1'::text AS inclus_dans_sejour,

					w_FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE,
					w_FD_FAC_DETFAC.CLI_DONN_NOOBJETFAC,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET,
					w_FD_FAC_DETFAC.CLI_CODE_OBJETFACTURATION,
					w_FD_FAC_DETFAC.CLI_CODE_ETAT,
					w_FD_FAC_DETFAC.CLI_FLAG_SENS,
					w_FD_FAC_DETFAC.CLI_DONN_EXERCOMPTA,
					
					CLI_CODE_MEDECINREALISATEUR,
					CLI_CODE_CCAM,
					CLI_CODE_PHASE,
					CLI_CODE_ACTIVITE,
					CLI_DONN_EXTDOC,
					CLI_DONN_MODIFICATEUR,
					''::text AS CLI_SEQUENCE1ACTE,
					''::text AS CLI_SEQUENCE2ACTE,
					
					''::text AS CLI_CODE_ORDONNATEUR,
					CLI_CODE_REGROUPEMENTFACTURATION AS CLI_CODE_PRESTATION,
					date(CLI_DATE_SAISIE) AS CLI_DATE_DEBPREST,
					date(CLI_DATE_SAISIE) AS CLI_DATE_FINPREST,	
					CLI_DONN_NOMBRE AS CLI_DONN_QTE,
					1 AS CLI_DONN_COEFF,
					w_FD_ACTES_CCAM.taux_2,
					w_FD_ACTES_CCAM.prix_unitaire,
					round(w_FD_ACTES_CCAM.prix_unitaire * w_FD_ACTES_CCAM.taux_2 * CLI_DONN_NOMBRE / 100,2) AS CLI_DONN_MONTANTLIG,
					0::numeric AS CLI_CODE_PRATICIEN,
					'' AS CLI_CODE_PRATICIENINTERNE,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET
				FROM w_FD_ACTES_CCAM
					JOIN w_FD_FAC_DETFAC ON w_FD_ACTES_CCAM.no_facture_2 = w_FD_FAC_DETFAC.no_facture
				WHERE w_FD_ACTES_CCAM.no_facture_2 <> '' AND
					acte_inclus_dans_sejour_2 = '1';

				INSERT INTO w_FD_FAC_DETFACLIG
				SELECT
					nextval('w_FD_FAC_DETFACLIG_sequence'::regclass) AS oid,
					0::bigint AS FD_ACTES_CCAM_id,

					w_FD_FAC_DETFAC.no_sejour,
					w_FD_FAC_DETFAC.sejour_id,
					w_FD_FAC_DETFAC.no_facture,
					0::bigint AS rss_id,
					0::bigint AS no_rss,
					0::bigint AS ghs_id,
					'1'::text AS inclus_dans_sejour,

					w_FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE,
					w_FD_FAC_DETFAC.CLI_DONN_NOOBJETFAC,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET,
					w_FD_FAC_DETFAC.CLI_CODE_OBJETFACTURATION,
					w_FD_FAC_DETFAC.CLI_CODE_ETAT,
					w_FD_FAC_DETFAC.CLI_FLAG_SENS,
					w_FD_FAC_DETFAC.CLI_DONN_EXERCOMPTA,
					
					CLI_CODE_MEDECINREALISATEUR,
					CLI_CODE_CCAM,
					CLI_CODE_PHASE,
					CLI_CODE_ACTIVITE,
					CLI_DONN_EXTDOC,
					CLI_DONN_MODIFICATEUR,
					''::text AS CLI_SEQUENCE1ACTE,
					''::text AS CLI_SEQUENCE2ACTE,
					
					''::text AS CLI_CODE_ORDONNATEUR,
					CLI_CODE_REGROUPEMENTFACTURATION AS CLI_CODE_PRESTATION,
					date(CLI_DATE_SAISIE) AS CLI_DATE_DEBPREST,
					date(CLI_DATE_SAISIE) AS CLI_DATE_FINPREST,	
					CLI_DONN_NOMBRE AS CLI_DONN_QTE,
					1 AS CLI_DONN_COEFF,
					w_FD_ACTES_CCAM.taux_0,
					w_FD_ACTES_CCAM.prix_unitaire,
					round(w_FD_ACTES_CCAM.prix_unitaire * w_FD_ACTES_CCAM.taux_0 * CLI_DONN_NOMBRE / 100,2) AS CLI_DONN_MONTANTLIG,
					0::numeric AS CLI_CODE_PRATICIEN,
					'' AS CLI_CODE_PRATICIENINTERNE,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET
				FROM w_FD_ACTES_CCAM
					JOIN w_FD_FAC_DETFAC ON w_FD_ACTES_CCAM.no_facture_0 = w_FD_FAC_DETFAC.no_facture
				WHERE w_FD_ACTES_CCAM.no_facture_0 <> '' AND
					acte_inclus_dans_sejour_0 = '1';
					
			
		]]></sqlcmd>
		</NODE>
		
		
		
		<NODEx label="Actes ACTRHS (non implémenté)">

			<sqlcmd><![CDATA[
				

				DROP TABLE IF EXISTS w_FD_ACTES_ACTRHS;
				CREATE TEMP TABLE w_FD_ACTES_ACTRHS AS
				SELECT 
					no_sejour,
					''::text AS no_facture_1,
					''::text AS no_facture_2,
					''::text AS no_facture_0,
					FD_ACTES_ACTRHS.CLI_DONN_NOABSEPISODE,
					FD_ACTES_ACTRHS.CLI_DONN_LIEN,
					FD_ACTES_ACTRHS.CLI_DONN_NUMEROSEQUENCE,
					FD_ACTES_ENTETE.CLI_DATE_SAISIE,
					FD_ACTES_ACTRHS.CLI_CODE_ACTIVITESSR,
					FD_ACTES_ACTRHS.CLI_CODE_ACTE,
					FD_ACTES_ACTRHS.CLI_DONN_NBSEANCESARR AS CLI_DONN_NOMBRE,
					FD_ACTES_ACTRHS.CLI_CODE_INTERVENANT AS CLI_CODE_MEDECINREALISATEUR,
					1::numeric AS coefficient,
					0::numeric AS prix_unitaire,
					0::numeric AS taux_1,
					0::numeric AS taux_2,
					0::numeric AS taux_0,
					'0'::text AS acte_inclus_dans_sejour_0,
					'0'::text AS acte_inclus_dans_sejour_1,
					'0'::text AS acte_inclus_dans_sejour_2,
					0::numeric AS montant_facture,
					0::numeric AS montant_facture_0,
					0::numeric AS montant_facture_1,
					0::numeric AS montant_facture_2,
					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
				FROM prod_clinicom.FD_ACTES_ACTRHS JOIN w_ADMISSIONS_select ON FD_ACTES_ACTRHS.cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT  
				JOIN prod_clinicom.FD_ACTES_ENTETE ON FD_ACTES_ACTRHS.CLI_DONN_NOABSEPISODE = FD_ACTES_ENTETE.CLI_DONN_NOABSEPISODE AND
						FD_ACTES_ACTRHS.CLI_DONN_LIEN = FD_ACTES_ENTETE.CLI_DONN_LIEN 
						JOIN activite[PX].p_sejours ON FD_ACTES_ACTRHS.CLI_DONN_NOABSEPISODE = p_sejours.code_original AND p_sejours.etat = '';


				CREATE INDEX w_FD_ACTES_ACTRHS_i1
				ON w_FD_ACTES_ACTRHS
				USING btree
				(no_sejour);

				CREATE INDEX w_FD_ACTES_ACTRHS_i2
				ON w_FD_ACTES_ACTRHS
				USING btree
				(CLI_DATE_SAISIE);



				DROP TABLE IF EXISTS w_FD_FAC_DETFAC_inclus;
				CREATE TEMP TABLE w_FD_FAC_DETFAC_inclus AS
				SELECT w_FD_FAC_DETFACLIG.no_sejour, 
					CLI_DATE_DEBPERIODE, 
					CLI_DATE_FINPERIODE,
					MAX(CASE WHEN type_tiers_payant = '0' THEN w_FD_FAC_DETFACLIG.no_facture ELSE '' END) AS no_facture_0, 
					MAX(CASE WHEN type_tiers_payant = '1' THEN w_FD_FAC_DETFACLIG.no_facture ELSE '' END) AS no_facture_1, 
					MAX(CASE WHEN type_tiers_payant = '2' THEN w_FD_FAC_DETFACLIG.no_facture ELSE '' END) AS no_facture_2, 
					MAX(CASE WHEN type_tiers_payant = '0' THEN CLI_DONN_TAUX ELSE 0 END) AS CLI_DONN_TAUX_0, 
					MAX(CASE WHEN type_tiers_payant = '1' THEN CLI_DONN_TAUX ELSE 0 END) AS CLI_DONN_TAUX_1, 
					MAX(CASE WHEN type_tiers_payant = '2' THEN CLI_DONN_TAUX ELSE 0 END) AS CLI_DONN_TAUX_2,
					MAX(CASE WHEN type_tiers_payant = '0' THEN '1' ELSE '0' END) AS acte_inclus_dans_sejour_0, 
					MAX(CASE WHEN type_tiers_payant = '1' THEN '1' ELSE '0' END) AS acte_inclus_dans_sejour_1, 
					MAX(CASE WHEN type_tiers_payant = '2' THEN '1' ELSE '0' END) AS acte_inclus_dans_sejour_2
				FROM w_FD_FAC_DETFACLIG
					JOIN w_FD_FAC_DETFAC ON w_FD_FAC_DETFACLIG.CLI_DONN_NOABSEPISODE = w_FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE AND
						w_FD_FAC_DETFACLIG.CLI_Donn_NoObjIntFac = w_FD_FAC_DETFAC.CLI_Code_NoInterneObjet 
				WHERE CLI_CODE_PRESTATION IN ('GHS', 'TM', 'ESTH') AND CLI_DONN_TAUX > 0
				GROUP BY 1,2,3;




				-- Actes inclus dans séjours (budget global)
				UPDATE w_FD_ACTES_ACTRHS
				SET no_facture_1 = w_FD_FAC_DETFAC_inclus.no_facture_1,
					taux_1 = 100,
					acte_inclus_dans_sejour_1 = '1'
				FROM w_FD_FAC_DETFAC_inclus
				WHERE w_FD_FAC_DETFAC_inclus.no_sejour = w_FD_ACTES_ACTRHS.no_sejour AND
					date(CLI_DATE_SAISIE) BETWEEN CLI_DATE_DEBPERIODE AND CLI_DATE_FINPERIODE AND
					CLI_DATE_DEBPERIODE = CLI_DATE_FINPERIODE AND
					w_FD_FAC_DETFAC_inclus.acte_inclus_dans_sejour_1 = '1' AND
					w_FD_ACTES_ACTRHS.no_facture_1 = '' AND w_FD_ACTES_ACTRHS.no_facture_2 = '' AND w_FD_ACTES_ACTRHS.no_facture_0 = '';
					
				UPDATE w_FD_ACTES_ACTRHS
				SET no_facture_2 = w_FD_FAC_DETFAC_inclus.no_facture_2,
					taux_2 = 100,
					acte_inclus_dans_sejour_2 = '1'
				FROM w_FD_FAC_DETFAC_inclus
				WHERE w_FD_FAC_DETFAC_inclus.no_sejour = w_FD_ACTES_ACTRHS.no_sejour AND
					date(CLI_DATE_SAISIE) BETWEEN CLI_DATE_DEBPERIODE AND CLI_DATE_FINPERIODE AND
					CLI_DATE_DEBPERIODE = CLI_DATE_FINPERIODE AND
					w_FD_FAC_DETFAC_inclus.acte_inclus_dans_sejour_2 = '1' AND
					w_FD_ACTES_ACTRHS.no_facture_1 = '' AND w_FD_ACTES_ACTRHS.no_facture_2 = '' AND w_FD_ACTES_ACTRHS.no_facture_0 = '';

				UPDATE w_FD_ACTES_ACTRHS
				SET no_facture_0 = w_FD_FAC_DETFAC_inclus.no_facture_0,
					taux_0 = 100,
					acte_inclus_dans_sejour_0 = '1'
				FROM w_FD_FAC_DETFAC_inclus
				WHERE w_FD_FAC_DETFAC_inclus.no_sejour = w_FD_ACTES_ACTRHS.no_sejour AND
					date(CLI_DATE_SAISIE) BETWEEN CLI_DATE_DEBPERIODE AND CLI_DATE_FINPERIODE AND
					CLI_DATE_DEBPERIODE = CLI_DATE_FINPERIODE AND
					w_FD_FAC_DETFAC_inclus.acte_inclus_dans_sejour_0 = '1' AND
					w_FD_ACTES_ACTRHS.no_facture_1 = '' AND w_FD_ACTES_ACTRHS.no_facture_2 = '' AND w_FD_ACTES_ACTRHS.no_facture_0 = '';

					
				UPDATE w_FD_ACTES_ACTRHS
				SET no_facture_1 = w_FD_FAC_DETFAC_inclus.no_facture_1,
					taux_1 = 100,
					acte_inclus_dans_sejour_1 = '1'
				FROM w_FD_FAC_DETFAC_inclus
				WHERE w_FD_FAC_DETFAC_inclus.no_sejour = w_FD_ACTES_ACTRHS.no_sejour AND
					date(CLI_DATE_SAISIE) BETWEEN CLI_DATE_DEBPERIODE AND CLI_DATE_FINPERIODE AND
					w_FD_FAC_DETFAC_inclus.acte_inclus_dans_sejour_1 = '1' AND
					w_FD_ACTES_ACTRHS.no_facture_1 = '' AND w_FD_ACTES_ACTRHS.no_facture_2 = '' AND w_FD_ACTES_ACTRHS.no_facture_0 = '';
					
					
				UPDATE w_FD_ACTES_ACTRHS
				SET no_facture_2 = w_FD_FAC_DETFAC_inclus.no_facture_2,
					taux_2 = 100,
					acte_inclus_dans_sejour_2 = '1'
				FROM w_FD_FAC_DETFAC_inclus
				WHERE w_FD_FAC_DETFAC_inclus.no_sejour = w_FD_ACTES_ACTRHS.no_sejour AND
					date(CLI_DATE_SAISIE) BETWEEN CLI_DATE_DEBPERIODE AND CLI_DATE_FINPERIODE AND
					w_FD_FAC_DETFAC_inclus.acte_inclus_dans_sejour_2 = '1' AND
					w_FD_ACTES_ACTRHS.no_facture_1 = '' AND w_FD_ACTES_ACTRHS.no_facture_2 = '' AND w_FD_ACTES_ACTRHS.no_facture_0 = '';
					
				UPDATE w_FD_ACTES_ACTRHS
				SET no_facture_0 = w_FD_FAC_DETFAC_inclus.no_facture_0,
					taux_0 = 100,
					acte_inclus_dans_sejour_0 = '1'
				FROM w_FD_FAC_DETFAC_inclus
				WHERE w_FD_FAC_DETFAC_inclus.no_sejour = w_FD_ACTES_ACTRHS.no_sejour AND
					date(CLI_DATE_SAISIE) BETWEEN CLI_DATE_DEBPERIODE AND CLI_DATE_FINPERIODE AND
					w_FD_FAC_DETFAC_inclus.acte_inclus_dans_sejour_0 = '1' AND
					w_FD_ACTES_ACTRHS.no_facture_1 = '' AND w_FD_ACTES_ACTRHS.no_facture_2 = '' AND w_FD_ACTES_ACTRHS.no_facture_0 = '';




				
				-- mise à jour actes facturés (externes)
				UPDATE w_FD_FAC_DETFACLIG SET
					CLI_CODE_MEDECINREALISATEUR = CASE WHEN w_FD_FAC_DETFACLIG.CLI_CODE_MEDECINREALISATEUR = '' THEN w_FD_ACTES_ACTRHS.CLI_CODE_INTERVENANT ELSE w_FD_FAC_DETFACLIG.CLI_CODE_MEDECINREALISATEUR END,
					CLI_CODE_CCAM = CASE WHEN w_FD_FAC_DETFACLIG.CLI_CODE_CCAM = '' THEN w_FD_ACTES_ACTRHS.CLI_CODE_ACTE ELSE w_FD_FAC_DETFACLIG.CLI_CODE_CCAM END
				FROM w_FD_ACTES_ACTRHS
				WHERE w_FD_ACTES_ACTRHS.CLI_DONN_NOABSEPISODE = w_FD_FAC_DETFACLIG.CLI_DONN_NOABSEPISODE AND
					w_FD_ACTES_ACTRHS.CLI_DONN_LIEN = w_FD_FAC_DETFACLIG.CLI_SEQUENCE1ACTE AND
					w_FD_ACTES_ACTRHS.CLI_DONN_NUMSEQUENCE = w_FD_FAC_DETFACLIG.CLI_SEQUENCE2ACTE AND
					w_FD_FAC_DETFACLIG.CLI_CODE_PRATICIENINTERNE <> '' AND
					w_FD_FAC_DETFACLIG.CLI_CODE_PRESTATION NOT IN ('ACO', 'ADA', 'ADC', 'ADE', 'ADI', 'ATM', 'DEN');
					
					
				
					
				-- Ajout inclus dans sejour

				INSERT INTO w_FD_FAC_DETFACLIG
				SELECT
					nextval('w_FD_FAC_DETFACLIG_sequence'::regclass) AS oid,
					0::bigint AS FD_ACTES_ACTRHS_id,

					w_FD_FAC_DETFAC.no_sejour,
					w_FD_FAC_DETFAC.sejour_id,
					w_FD_FAC_DETFAC.no_facture,
					0::bigint AS rss_id,
					0::bigint AS no_rss,
					0::bigint AS ghs_id,
					'1'::text AS inclus_dans_sejour,

					w_FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE,
					w_FD_FAC_DETFAC.CLI_DONN_NOOBJETFAC,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET,
					w_FD_FAC_DETFAC.CLI_CODE_OBJETFACTURATION,
					w_FD_FAC_DETFAC.CLI_CODE_ETAT,
					w_FD_FAC_DETFAC.CLI_FLAG_SENS,
					w_FD_FAC_DETFAC.CLI_DONN_EXERCOMPTA,
					
					CLI_CODE_MEDECINREALISATEUR,
					CLI_CODE_ACTE,
					''::text AS CLI_CODE_PHASE,
					''::text AS CLI_CODE_ACTIVITE,
					''::text AS CLI_DONN_EXTDOC,
					''::text AS CLI_DONN_MODIFICATEUR,
					''::text AS CLI_SEQUENCE1ACTE,
					''::text AS CLI_SEQUENCE2ACTE,
					
					''::text AS CLI_CODE_ORDONNATEUR,
					CLI_CODE_ACTIVITESSR AS CLI_CODE_PRESTATION,
					date(CLI_DATE_SAISIE) AS CLI_DATE_DEBPREST,
					date(CLI_DATE_SAISIE) AS CLI_DATE_FINPREST,	
					CLI_DONN_NOMBRE AS CLI_DONN_QTE,
					1 AS CLI_DONN_COEFF,
					w_FD_ACTES_ACTRHS.taux_1,
					w_FD_ACTES_ACTRHS.prix_unitaire,
					round(w_FD_ACTES_ACTRHS.prix_unitaire * w_FD_ACTES_ACTRHS.taux_1 * CLI_DONN_NOMBRE / 100,2) AS CLI_DONN_MONTANTLIG,
					0::numeric AS CLI_CODE_PRATICIEN,
					'' AS CLI_CODE_PRATICIENINTERNE,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET
				FROM w_FD_ACTES_ACTRHS
					JOIN w_FD_FAC_DETFAC ON w_FD_ACTES_ACTRHS.no_facture_1 = w_FD_FAC_DETFAC.no_facture
				WHERE w_FD_ACTES_ACTRHS.no_facture_1 <> '' AND
					acte_inclus_dans_sejour_1 = '1';


				INSERT INTO w_FD_FAC_DETFACLIG
				SELECT
					nextval('w_FD_FAC_DETFACLIG_sequence'::regclass) AS oid,
					0::bigint AS FD_ACTES_ACTRHS_id,

					w_FD_FAC_DETFAC.no_sejour,
					w_FD_FAC_DETFAC.sejour_id,
					w_FD_FAC_DETFAC.no_facture,
					0::bigint AS rss_id,
					0::bigint AS no_rss,
					0::bigint AS ghs_id,
					'1'::text AS inclus_dans_sejour,

					w_FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE,
					w_FD_FAC_DETFAC.CLI_DONN_NOOBJETFAC,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET,
					w_FD_FAC_DETFAC.CLI_CODE_OBJETFACTURATION,
					w_FD_FAC_DETFAC.CLI_CODE_ETAT,
					w_FD_FAC_DETFAC.CLI_FLAG_SENS,
					w_FD_FAC_DETFAC.CLI_DONN_EXERCOMPTA,
					
					CLI_CODE_MEDECINREALISATEUR,
					CLI_CODE_ACTE,
					''::text AS CLI_CODE_PHASE,
					''::text AS CLI_CODE_ACTIVITE,
					''::text AS CLI_DONN_EXTDOC,
					''::text AS CLI_DONN_MODIFICATEUR,
					''::text AS CLI_SEQUENCE1ACTE,
					''::text AS CLI_SEQUENCE2ACTE,
					
					''::text AS CLI_CODE_ORDONNATEUR,
					CLI_CODE_ACTIVITESSR AS CLI_CODE_PRESTATION,
					date(CLI_DATE_SAISIE) AS CLI_DATE_DEBPREST,
					date(CLI_DATE_SAISIE) AS CLI_DATE_FINPREST,	
					CLI_DONN_NOMBRE AS CLI_DONN_QTE,
					1 AS CLI_DONN_COEFF,
					w_FD_ACTES_ACTRHS.taux_2,
					w_FD_ACTES_ACTRHS.prix_unitaire,
					round(w_FD_ACTES_ACTRHS.prix_unitaire * w_FD_ACTES_ACTRHS.taux_2 * CLI_DONN_NOMBRE / 100,2) AS CLI_DONN_MONTANTLIG,
					0::numeric AS CLI_CODE_PRATICIEN,
					'' AS CLI_CODE_PRATICIENINTERNE,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET
				FROM w_FD_ACTES_ACTRHS
					JOIN w_FD_FAC_DETFAC ON w_FD_ACTES_ACTRHS.no_facture_2 = w_FD_FAC_DETFAC.no_facture
				WHERE w_FD_ACTES_ACTRHS.no_facture_2 <> '' AND
					acte_inclus_dans_sejour_2 = '1';

				INSERT INTO w_FD_FAC_DETFACLIG
				SELECT
					nextval('w_FD_FAC_DETFACLIG_sequence'::regclass) AS oid,
					0::bigint AS FD_ACTES_ACTRHS_id,

					w_FD_FAC_DETFAC.no_sejour,
					w_FD_FAC_DETFAC.sejour_id,
					w_FD_FAC_DETFAC.no_facture,
					0::bigint AS rss_id,
					0::bigint AS no_rss,
					0::bigint AS ghs_id,
					'1'::text AS inclus_dans_sejour,

					w_FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE,
					w_FD_FAC_DETFAC.CLI_DONN_NOOBJETFAC,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET,
					w_FD_FAC_DETFAC.CLI_CODE_OBJETFACTURATION,
					w_FD_FAC_DETFAC.CLI_CODE_ETAT,
					w_FD_FAC_DETFAC.CLI_FLAG_SENS,
					w_FD_FAC_DETFAC.CLI_DONN_EXERCOMPTA,
					
					CLI_CODE_MEDECINREALISATEUR,
					CLI_CODE_ACTE,
					''::text AS CLI_CODE_PHASE,
					''::text AS CLI_CODE_ACTIVITE,
					''::text AS CLI_DONN_EXTDOC,
					''::text AS CLI_DONN_MODIFICATEUR,
					''::text AS CLI_SEQUENCE1ACTE,
					''::text AS CLI_SEQUENCE2ACTE,
					
					''::text AS CLI_CODE_ORDONNATEUR,
					CLI_CODE_ACTIVITESSR AS CLI_CODE_PRESTATION,
					date(CLI_DATE_SAISIE) AS CLI_DATE_DEBPREST,
					date(CLI_DATE_SAISIE) AS CLI_DATE_FINPREST,	
					CLI_DONN_NOMBRE AS CLI_DONN_QTE,
					1 AS CLI_DONN_COEFF,
					w_FD_ACTES_ACTRHS.taux_0,
					w_FD_ACTES_ACTRHS.prix_unitaire,
					round(w_FD_ACTES_ACTRHS.prix_unitaire * w_FD_ACTES_ACTRHS.taux_0 * CLI_DONN_NOMBRE / 100,2) AS CLI_DONN_MONTANTLIG,
					0::numeric AS CLI_CODE_PRATICIEN,
					'' AS CLI_CODE_PRATICIENINTERNE,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET
				FROM w_FD_ACTES_ACTRHS
					JOIN w_FD_FAC_DETFAC ON w_FD_ACTES_ACTRHS.no_facture_0 = w_FD_FAC_DETFAC.no_facture
				WHERE w_FD_ACTES_ACTRHS.no_facture_0 <> '' AND
					acte_inclus_dans_sejour_0 = '1';
					
			
		]]></sqlcmd>
		</NODEx>
			
			
			
		<NODE label="Actes NGAP">

			<sqlcmd><![CDATA[
						
				DROP TABLE IF EXISTS w_FD_ACTES_NGAP;
				CREATE TEMP TABLE w_FD_ACTES_NGAP AS
				SELECT 
					no_sejour,
					''::text AS no_facture_1,
					''::text AS no_facture_2,
					''::text AS no_facture_0,
					FD_ACTES_NGAP.CLI_DONN_NOABSEPISODE,
					FD_ACTES_NGAP.CLI_DONN_FACTUREINTERNE,
					FD_ACTES_NGAP.CLI_DONN_LIEN,
					FD_ACTES_NGAP.CLI_DONN_NUMEROSEQUENCE,
					FD_ACTES_ENTETE.CLI_DATE_SAISIE,
					FD_ACTES_NGAP.CLI_DONN_LETTRECLE,
					CASE WHEN FB_FAC_LETTRECLE.CLI_CODE_IMPRESSION <> '' THEN FB_FAC_LETTRECLE.CLI_CODE_IMPRESSION ELSE FD_ACTES_NGAP.CLI_DONN_LETTRECLE END AS CLI_CODE_PRESTATION,
					1::numeric AS CLI_DONN_NOMBRE,
					FD_ACTES_ENTETE.CLI_CODE_MEDECIN1 AS CLI_CODE_MEDECINREALISATEUR,
					FD_ACTES_NGAP.CLI_DONN_TAUXPEC AS CLI_DONN_TAUXSECU,
					FD_ACTES_NGAP.CLI_FLAG_ACTEGRATUIT,
					FD_ACTES_NGAP.CLI_FLAG_REGIE,
					GREATEST(FD_ACTES_NGAP.CLI_DONN_COEFF,0) AS CLI_DONN_COEFF,
					CASE WHEN FD_ACTES_NGAP.CLI_DONN_TARIF <> 0 THEN FD_ACTES_NGAP.CLI_DONN_TARIF ELSE COALESCE(FB_FAC_HISTOLETTRECLE.CLI_DONN_TARIF,0) END AS prix_unitaire,
					0::numeric AS taux_1,
					0::numeric AS taux_2,
					0::numeric AS taux_0,
					'0'::text AS acte_inclus_dans_sejour_0,
					'0'::text AS acte_inclus_dans_sejour_1,
					'0'::text AS acte_inclus_dans_sejour_2,
					0::numeric AS montant_facture,
					0::numeric AS montant_facture_0,
					0::numeric AS montant_facture_1,
					0::numeric AS montant_facture_2,
					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
				FROM prod_clinicom.FD_ACTES_NGAP JOIN w_ADMISSIONS_select ON FD_ACTES_NGAP.cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT   
				JOIN prod_clinicom.FB_FAC_LETTRECLE ON FD_ACTES_NGAP.CLI_DONN_LETTRECLE = FB_FAC_LETTRECLE.CLI_CODE_LETTRECLE
				JOIN prod_clinicom.FD_ACTES_ENTETE ON FD_ACTES_NGAP.CLI_DONN_NOABSEPISODE = FD_ACTES_ENTETE.CLI_DONN_NOABSEPISODE AND
						FD_ACTES_NGAP.CLI_DONN_LIEN = FD_ACTES_ENTETE.CLI_DONN_LIEN 
				JOIN activite[PX].p_sejours ON FD_ACTES_NGAP.CLI_DONN_NOABSEPISODE = p_sejours.code_original AND p_sejours.etat = ''
				LEFT JOIN prod_clinicom.FB_FAC_HISTOLETTRECLE ON FD_ACTES_NGAP.CLI_DONN_LETTRECLE = FB_FAC_HISTOLETTRECLE.CLI_CODE_LETTRECLE AND
						FD_ACTES_ENTETE.CLI_DATE_SAISIE BETWEEN CLI_DATE_EFFET AND COALESCE(CLI_DATE_FINEFFET,'2099-12-31 00:00:00') - interval '1 second'
				WHERE CASE WHEN FB_FAC_LETTRECLE.CLI_CODE_IMPRESSION <> '' THEN FB_FAC_LETTRECLE.CLI_CODE_IMPRESSION ELSE FD_ACTES_NGAP.CLI_DONN_LETTRECLE END
					NOT IN ('ATU', 'FPU', 'FTN', 'FTR', 'PHH', 'PHS', 'PII', 'PHU', 'PME', 'SE1', 'SE2', 'SE3', 'SE4','SE5', 'SE6', 'SE7', 'REA', 'SRC', 'STF', 'PJ', 'TM');



				CREATE INDEX w_FD_ACTES_NGAP_i1
				ON w_FD_ACTES_NGAP
				USING btree
				(no_sejour);

				CREATE INDEX w_FD_ACTES_NGAP_i2
				ON w_FD_ACTES_NGAP
				USING btree
				(CLI_DATE_SAISIE);



				DROP TABLE IF EXISTS w_FD_FAC_DETFAC_inclus;
				CREATE TEMP TABLE w_FD_FAC_DETFAC_inclus AS
				SELECT w_FD_FAC_DETFACLIG.no_sejour, 
					CLI_DATE_DEBPERIODE, 
					CLI_DATE_FINPERIODE,
					MAX(CASE WHEN type_tiers_payant = '0' THEN w_FD_FAC_DETFACLIG.no_facture ELSE '' END) AS no_facture_0, 
					MAX(CASE WHEN type_tiers_payant = '1' THEN w_FD_FAC_DETFACLIG.no_facture ELSE '' END) AS no_facture_1, 
					MAX(CASE WHEN type_tiers_payant = '2' THEN w_FD_FAC_DETFACLIG.no_facture ELSE '' END) AS no_facture_2, 
					MAX(CASE WHEN type_tiers_payant = '0' THEN CLI_DONN_TAUX ELSE 0 END) AS CLI_DONN_TAUX_0, 
					MAX(CASE WHEN type_tiers_payant = '1' THEN CLI_DONN_TAUX ELSE 0 END) AS CLI_DONN_TAUX_1, 
					MAX(CASE WHEN type_tiers_payant = '2' THEN CLI_DONN_TAUX ELSE 0 END) AS CLI_DONN_TAUX_2,
					MAX(CASE WHEN type_tiers_payant = '0' THEN '1' ELSE '0' END) AS acte_inclus_dans_sejour_0, 
					MAX(CASE WHEN type_tiers_payant = '1' THEN '1' ELSE '0' END) AS acte_inclus_dans_sejour_1, 
					MAX(CASE WHEN type_tiers_payant = '2' THEN '1' ELSE '0' END) AS acte_inclus_dans_sejour_2
				FROM w_FD_FAC_DETFACLIG
					JOIN w_FD_FAC_DETFAC ON w_FD_FAC_DETFACLIG.CLI_DONN_NOABSEPISODE = w_FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE AND
						w_FD_FAC_DETFACLIG.CLI_Donn_NoObjIntFac = w_FD_FAC_DETFAC.CLI_Code_NoInterneObjet 
				WHERE CLI_CODE_PRESTATION IN ('GHS', 'TM', 'ESTH') AND CLI_DONN_TAUX > 0
				GROUP BY 1,2,3;

				CREATE INDEX w_FD_FAC_DETFAC_inclus_i1
				ON w_FD_FAC_DETFAC_inclus
				USING btree
				(no_sejour);


				-- Actes inclus dans séjours (budget global)
				UPDATE w_FD_ACTES_NGAP
				SET no_facture_1 = w_FD_FAC_DETFAC_inclus.no_facture_1,
					taux_1 = 100,
					acte_inclus_dans_sejour_1 = '1'
				FROM w_FD_FAC_DETFAC_inclus
				WHERE w_FD_FAC_DETFAC_inclus.no_sejour = w_FD_ACTES_NGAP.no_sejour AND
					date(CLI_DATE_SAISIE) BETWEEN CLI_DATE_DEBPERIODE AND CLI_DATE_FINPERIODE AND
					CLI_DATE_DEBPERIODE = CLI_DATE_FINPERIODE AND
					w_FD_FAC_DETFAC_inclus.acte_inclus_dans_sejour_1 = '1' AND
					w_FD_ACTES_NGAP.no_facture_1 = '' AND w_FD_ACTES_NGAP.no_facture_2 = '' AND w_FD_ACTES_NGAP.no_facture_0 = '';
					
				UPDATE w_FD_ACTES_NGAP
				SET no_facture_2 = w_FD_FAC_DETFAC_inclus.no_facture_2,
					taux_2 = 100,
					acte_inclus_dans_sejour_2 = '1'
				FROM w_FD_FAC_DETFAC_inclus
				WHERE w_FD_FAC_DETFAC_inclus.no_sejour = w_FD_ACTES_NGAP.no_sejour AND
					date(CLI_DATE_SAISIE) BETWEEN CLI_DATE_DEBPERIODE AND CLI_DATE_FINPERIODE AND
					CLI_DATE_DEBPERIODE = CLI_DATE_FINPERIODE AND
					w_FD_FAC_DETFAC_inclus.acte_inclus_dans_sejour_2 = '1' AND
					w_FD_ACTES_NGAP.no_facture_1 = '' AND w_FD_ACTES_NGAP.no_facture_2 = '' AND w_FD_ACTES_NGAP.no_facture_0 = '';

				UPDATE w_FD_ACTES_NGAP
				SET no_facture_0 = w_FD_FAC_DETFAC_inclus.no_facture_0,
					taux_0 = 100,
					acte_inclus_dans_sejour_0 = '1'
				FROM w_FD_FAC_DETFAC_inclus
				WHERE w_FD_FAC_DETFAC_inclus.no_sejour = w_FD_ACTES_NGAP.no_sejour AND
					date(CLI_DATE_SAISIE) BETWEEN CLI_DATE_DEBPERIODE AND CLI_DATE_FINPERIODE AND
					CLI_DATE_DEBPERIODE = CLI_DATE_FINPERIODE AND
					w_FD_FAC_DETFAC_inclus.acte_inclus_dans_sejour_0 = '1' AND
					w_FD_ACTES_NGAP.no_facture_1 = '' AND w_FD_ACTES_NGAP.no_facture_2 = '' AND w_FD_ACTES_NGAP.no_facture_0 = '';

					
				UPDATE w_FD_ACTES_NGAP
				SET no_facture_1 = w_FD_FAC_DETFAC_inclus.no_facture_1,
					taux_1 = 100,
					acte_inclus_dans_sejour_1 = '1'
				FROM w_FD_FAC_DETFAC_inclus
				WHERE w_FD_FAC_DETFAC_inclus.no_sejour = w_FD_ACTES_NGAP.no_sejour AND
					date(CLI_DATE_SAISIE) BETWEEN CLI_DATE_DEBPERIODE AND CLI_DATE_FINPERIODE AND
					w_FD_FAC_DETFAC_inclus.acte_inclus_dans_sejour_1 = '1' AND
					w_FD_ACTES_NGAP.no_facture_1 = '' AND w_FD_ACTES_NGAP.no_facture_2 = '' AND w_FD_ACTES_NGAP.no_facture_0 = '';
					
					
				UPDATE w_FD_ACTES_NGAP
				SET no_facture_2 = w_FD_FAC_DETFAC_inclus.no_facture_2,
					taux_2 = 100,
					acte_inclus_dans_sejour_2 = '1'
				FROM w_FD_FAC_DETFAC_inclus
				WHERE w_FD_FAC_DETFAC_inclus.no_sejour = w_FD_ACTES_NGAP.no_sejour AND
					date(CLI_DATE_SAISIE) BETWEEN CLI_DATE_DEBPERIODE AND CLI_DATE_FINPERIODE AND
					w_FD_FAC_DETFAC_inclus.acte_inclus_dans_sejour_2 = '1' AND
					w_FD_ACTES_NGAP.no_facture_1 = '' AND w_FD_ACTES_NGAP.no_facture_2 = '' AND w_FD_ACTES_NGAP.no_facture_0 = '';
					
				UPDATE w_FD_ACTES_NGAP
				SET no_facture_0 = w_FD_FAC_DETFAC_inclus.no_facture_0,
					taux_0 = 100,
					acte_inclus_dans_sejour_0 = '1'
				FROM w_FD_FAC_DETFAC_inclus
				WHERE w_FD_FAC_DETFAC_inclus.no_sejour = w_FD_ACTES_NGAP.no_sejour AND
					date(CLI_DATE_SAISIE) BETWEEN CLI_DATE_DEBPERIODE AND CLI_DATE_FINPERIODE AND
					w_FD_FAC_DETFAC_inclus.acte_inclus_dans_sejour_0 = '1' AND
					w_FD_ACTES_NGAP.no_facture_1 = '' AND w_FD_ACTES_NGAP.no_facture_2 = '' AND w_FD_ACTES_NGAP.no_facture_0 = '';



				UPDATE w_FD_ACTES_NGAP
				SET montant_facture_actes_inclus_dans_sejour = round(CLI_DONN_NOMBRE * CLI_DONN_COEFF * prix_unitaire * taux_1 / 100,2),
					montant_facture_1_actes_inclus_dans_sejour = round(CLI_DONN_NOMBRE * CLI_DONN_COEFF * prix_unitaire * taux_1 / 100,2)
				WHERE acte_inclus_dans_sejour_1 = '1';

				UPDATE w_FD_ACTES_NGAP
				SET montant_facture_actes_inclus_dans_sejour = montant_facture_actes_inclus_dans_sejour + round(CLI_DONN_NOMBRE * CLI_DONN_COEFF * prix_unitaire * taux_2 / 100,2),
					montant_facture_2_actes_inclus_dans_sejour = round(CLI_DONN_NOMBRE * CLI_DONN_COEFF * prix_unitaire * taux_2 / 100,2)
				WHERE acte_inclus_dans_sejour_2 = '1';

				UPDATE w_FD_ACTES_NGAP
				SET montant_facture_actes_inclus_dans_sejour = montant_facture_actes_inclus_dans_sejour + round(CLI_DONN_NOMBRE * CLI_DONN_COEFF * prix_unitaire * taux_0 / 100,2),
					montant_facture_2_actes_inclus_dans_sejour = round(CLI_DONN_NOMBRE * CLI_DONN_COEFF * prix_unitaire * taux_0 / 100,2)
				WHERE acte_inclus_dans_sejour_0 = '1';
				
	
				-- mise à jour actes facturés (externes)
				UPDATE w_FD_FAC_DETFACLIG SET
					CLI_CODE_MEDECINREALISATEUR = CASE WHEN w_FD_FAC_DETFACLIG.CLI_CODE_MEDECINREALISATEUR = '' THEN w_FD_ACTES_NGAP.CLI_CODE_MEDECINREALISATEUR ELSE w_FD_FAC_DETFACLIG.CLI_CODE_MEDECINREALISATEUR END,
					CLI_CODE_CCAM = CASE WHEN w_FD_FAC_DETFACLIG.CLI_CODE_CCAM = '' THEN w_FD_ACTES_NGAP.CLI_DONN_LETTRECLE ELSE w_FD_FAC_DETFACLIG.CLI_CODE_CCAM END 
				FROM w_FD_ACTES_NGAP
				WHERE w_FD_ACTES_NGAP.CLI_DONN_NOABSEPISODE = w_FD_FAC_DETFACLIG.CLI_DONN_NOABSEPISODE AND
					w_FD_ACTES_NGAP.CLI_DONN_LIEN = w_FD_FAC_DETFACLIG.CLI_SEQUENCE1ACTE AND
					w_FD_ACTES_NGAP.CLI_DONN_NUMEROSEQUENCE = w_FD_FAC_DETFACLIG.CLI_SEQUENCE2ACTE AND
					w_FD_FAC_DETFACLIG.CLI_CODE_PRATICIENINTERNE <> '' AND
						(w_FD_FAC_DETFACLIG.CLI_CODE_MEDECINREALISATEUR = '' OR w_FD_FAC_DETFACLIG.CLI_CODE_CCAM  = '') AND
					w_FD_FAC_DETFACLIG.CLI_CODE_PRESTATION NOT IN ('ACO', 'ADA', 'ADC', 'ADE', 'ADI', 'ATM', 'DEN');
					
					
				UPDATE w_FD_FAC_DETFACLIG SET
					CLI_CODE_MEDECINREALISATEUR = CASE WHEN w_FD_FAC_DETFACLIG.CLI_CODE_MEDECINREALISATEUR = '' THEN w_FD_ACTES_NGAP.CLI_CODE_MEDECINREALISATEUR ELSE w_FD_FAC_DETFACLIG.CLI_CODE_MEDECINREALISATEUR END,
					CLI_CODE_CCAM = CASE WHEN w_FD_FAC_DETFACLIG.CLI_CODE_CCAM = '' THEN w_FD_ACTES_NGAP.CLI_DONN_LETTRECLE ELSE w_FD_FAC_DETFACLIG.CLI_CODE_CCAM END 
				FROM w_FD_ACTES_NGAP
				WHERE w_FD_ACTES_NGAP.CLI_DONN_NOABSEPISODE = w_FD_FAC_DETFACLIG.CLI_DONN_NOABSEPISODE AND
					w_FD_ACTES_NGAP.CLI_DONN_LETTRECLE = w_FD_FAC_DETFACLIG.CLI_CODE_PRESTATION AND
					w_FD_FAC_DETFACLIG.CLI_CODE_MEDECINREALISATEUR = '' AND
						(w_FD_FAC_DETFACLIG.CLI_CODE_MEDECINREALISATEUR = '' OR w_FD_FAC_DETFACLIG.CLI_CODE_CCAM  = '');
						
					
				UPDATE w_FD_FAC_DETFACLIG SET
					CLI_CODE_MEDECINREALISATEUR = CASE WHEN w_FD_FAC_DETFACLIG.CLI_CODE_MEDECINREALISATEUR = '' THEN w_FD_FAC_DETFACLIG.CLI_CODE_PRATICIENINTERNE ELSE w_FD_FAC_DETFACLIG.CLI_CODE_MEDECINREALISATEUR END,
					CLI_CODE_CCAM = CASE WHEN w_FD_FAC_DETFACLIG.CLI_CODE_CCAM = '' THEN w_FD_FAC_DETFACLIG.CLI_CODE_PRESTATION ELSE w_FD_FAC_DETFACLIG.CLI_CODE_CCAM END 
				WHERE w_FD_FAC_DETFACLIG.CLI_CODE_PRATICIENINTERNE <> '' AND
						(w_FD_FAC_DETFACLIG.CLI_CODE_MEDECINREALISATEUR = '' OR w_FD_FAC_DETFACLIG.CLI_CODE_CCAM  = '') AND
					w_FD_FAC_DETFACLIG.CLI_CODE_PRESTATION NOT IN ('ACO', 'ADA', 'ADC', 'ADE', 'ADI', 'ATM', 'DEN');
					


				-- Insertion actes inclus dans séjour
				INSERT INTO w_FD_FAC_DETFACLIG
				SELECT
					nextval('w_FD_FAC_DETFACLIG_sequence'::regclass) AS oid,
					0::bigint AS FD_ACTES_NGAP_id,

					w_FD_FAC_DETFAC.no_sejour,
					w_FD_FAC_DETFAC.sejour_id,
					w_FD_FAC_DETFAC.no_facture,
					0::bigint AS rss_id,
					0::bigint AS no_rss,
					0::bigint AS ghs_id,
					'1'::text AS inclus_dans_sejour,

					w_FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE,
					w_FD_FAC_DETFAC.CLI_DONN_NOOBJETFAC,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET,
					w_FD_FAC_DETFAC.CLI_CODE_OBJETFACTURATION,
					w_FD_FAC_DETFAC.CLI_CODE_ETAT,
					w_FD_FAC_DETFAC.CLI_FLAG_SENS,
					w_FD_FAC_DETFAC.CLI_DONN_EXERCOMPTA,
					
					CLI_CODE_MEDECINREALISATEUR,
					CLI_DONN_LETTRECLE AS CLI_CODE_CCAM,
					''::text AS CLI_CODE_PHASE,
					''::text AS CLI_CODE_ACTIVITE,
					''::text AS CLI_DONN_EXTDOC,
					''::text AS CLI_DONN_MODIFICATEUR,
					''::text AS CLI_SEQUENCE1ACTE,
					''::text AS CLI_SEQUENCE2ACTE,
					
					''::text AS CLI_CODE_ORDONNATEUR,
					CLI_CODE_PRESTATION,
					date(CLI_DATE_SAISIE) AS CLI_DATE_DEBPREST,
					date(CLI_DATE_SAISIE) AS CLI_DATE_FINPREST,	
					CLI_DONN_NOMBRE AS CLI_DONN_QTE,
					CLI_DONN_COEFF,
					w_FD_ACTES_NGAP.taux_1,
					w_FD_ACTES_NGAP.prix_unitaire,
					round(w_FD_ACTES_NGAP.prix_unitaire * w_FD_ACTES_NGAP.taux_1 * CLI_DONN_NOMBRE * CLI_DONN_COEFF / 100,2) AS CLI_DONN_MONTANTLIG,
					0::numeric AS CLI_CODE_PRATICIEN,
					'' AS CLI_CODE_PRATICIENINTERNE,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET
				FROM w_FD_ACTES_NGAP
					JOIN w_FD_FAC_DETFAC ON w_FD_ACTES_NGAP.no_facture_1 = w_FD_FAC_DETFAC.no_facture
				WHERE w_FD_ACTES_NGAP.no_facture_1 <> '' AND
					acte_inclus_dans_sejour_1 = '1';







				INSERT INTO w_FD_FAC_DETFACLIG
				SELECT
					nextval('w_FD_FAC_DETFACLIG_sequence'::regclass) AS oid,
					0::bigint AS FD_ACTES_NGAP_id,

					w_FD_FAC_DETFAC.no_sejour,
					w_FD_FAC_DETFAC.sejour_id,
					w_FD_FAC_DETFAC.no_facture,
					0::bigint AS rss_id,
					0::bigint AS no_rss,
					0::bigint AS ghs_id,
					'1'::text AS inclus_dans_sejour,

					w_FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE,
					w_FD_FAC_DETFAC.CLI_DONN_NOOBJETFAC,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET,
					w_FD_FAC_DETFAC.CLI_CODE_OBJETFACTURATION,
					w_FD_FAC_DETFAC.CLI_CODE_ETAT,
					w_FD_FAC_DETFAC.CLI_FLAG_SENS,
					w_FD_FAC_DETFAC.CLI_DONN_EXERCOMPTA,
					
					CLI_CODE_MEDECINREALISATEUR,
					CLI_DONN_LETTRECLE AS CLI_CODE_CCAM,
					''::text AS CLI_CODE_PHASE,
					''::text AS CLI_CODE_ACTIVITE,
					''::text AS CLI_DONN_EXTDOC,
					''::text AS CLI_DONN_MODIFICATEUR,
					''::text AS CLI_SEQUENCE1ACTE,
					''::text AS CLI_SEQUENCE2ACTE,
					
					''::text AS CLI_CODE_ORDONNATEUR,
					CLI_CODE_PRESTATION,
					date(CLI_DATE_SAISIE) AS CLI_DATE_DEBPREST,
					date(CLI_DATE_SAISIE) AS CLI_DATE_FINPREST,	
					CLI_DONN_NOMBRE AS CLI_DONN_QTE,
					1 AS CLI_DONN_COEFF,
					w_FD_ACTES_NGAP.taux_2,
					w_FD_ACTES_NGAP.prix_unitaire,
					round(w_FD_ACTES_NGAP.prix_unitaire * w_FD_ACTES_NGAP.taux_2 * CLI_DONN_NOMBRE * CLI_DONN_COEFF / 100,2) AS CLI_DONN_MONTANTLIG,
					0::numeric AS CLI_CODE_PRATICIEN,
					'' AS CLI_CODE_PRATICIENINTERNE,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET
				FROM w_FD_ACTES_NGAP
					JOIN w_FD_FAC_DETFAC ON w_FD_ACTES_NGAP.no_facture_2 = w_FD_FAC_DETFAC.no_facture
				WHERE w_FD_ACTES_NGAP.no_facture_2 <> '' AND
					acte_inclus_dans_sejour_2 = '1';

				INSERT INTO w_FD_FAC_DETFACLIG
				SELECT
					nextval('w_FD_FAC_DETFACLIG_sequence'::regclass) AS oid,
					0::bigint AS FD_ACTES_NGAP_id,

					w_FD_FAC_DETFAC.no_sejour,
					w_FD_FAC_DETFAC.sejour_id,
					w_FD_FAC_DETFAC.no_facture,
					0::bigint AS rss_id,
					0::bigint AS no_rss,
					0::bigint AS ghs_id,
					'1'::text AS inclus_dans_sejour,

					w_FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE,
					w_FD_FAC_DETFAC.CLI_DONN_NOOBJETFAC,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET,
					w_FD_FAC_DETFAC.CLI_CODE_OBJETFACTURATION,
					w_FD_FAC_DETFAC.CLI_CODE_ETAT,
					w_FD_FAC_DETFAC.CLI_FLAG_SENS,
					w_FD_FAC_DETFAC.CLI_DONN_EXERCOMPTA,
					
					CLI_CODE_MEDECINREALISATEUR,
					CLI_DONN_LETTRECLE AS CLI_CODE_CCAM,
					''::text AS CLI_CODE_PHASE,
					''::text AS CLI_CODE_ACTIVITE,
					''::text AS CLI_DONN_EXTDOC,
					''::text AS CLI_DONN_MODIFICATEUR,
					''::text AS CLI_SEQUENCE1ACTE,
					''::text AS CLI_SEQUENCE2ACTE,
					
					''::text AS CLI_CODE_ORDONNATEUR,
					CLI_CODE_PRESTATION,
					date(CLI_DATE_SAISIE) AS CLI_DATE_DEBPREST,
					date(CLI_DATE_SAISIE) AS CLI_DATE_FINPREST,	
					CLI_DONN_NOMBRE AS CLI_DONN_QTE,
					1 AS CLI_DONN_COEFF,
					w_FD_ACTES_NGAP.taux_0,
					w_FD_ACTES_NGAP.prix_unitaire,
					round(w_FD_ACTES_NGAP.prix_unitaire * w_FD_ACTES_NGAP.taux_0 * CLI_DONN_NOMBRE * CLI_DONN_COEFF / 100,2) AS CLI_DONN_MONTANTLIG,
					0::numeric AS CLI_CODE_PRATICIEN,
					'' AS CLI_CODE_PRATICIENINTERNE,
					w_FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET
				FROM w_FD_ACTES_NGAP
					JOIN w_FD_FAC_DETFAC ON w_FD_ACTES_NGAP.no_facture_0 = w_FD_FAC_DETFAC.no_facture
				WHERE w_FD_ACTES_NGAP.no_facture_0 <> '' AND
					acte_inclus_dans_sejour_0 = '1';		
						
						
			]]></sqlcmd>
		</NODE>
			
		<NODE label="Entêtes">

			<sqlcmd><![CDATA[
			
				DROP TABLE IF EXISTS w_FD_FAC_DETFAC_inclus;
				CREATE TEMP TABLE w_FD_FAC_DETFAC_inclus AS
				SELECT no_facture, SUM(CLI_DONN_MONTANTLIG) AS CLI_DONN_MONTANTLIG
				FROM w_FD_FAC_DETFACLIG
				WHERE inclus_dans_sejour = '1'
				GROUP BY 1;
			
				ALTER SEQUENCE activite[PX].s_factures RESTART WITH 1;

				DROP TABLE IF EXISTS w_factures;
				CREATE TEMP TABLE w_factures AS 
				SELECT 
					nextval('activite[PX].s_factures'::regclass) AS oid,

					CLI_DONN_NOABSEPISODE,
					CLI_CODE_NOINTERNEOBJET,
					CLI_DONN_EXERCOMPTA,
					CLI_CODE_ETAT,
					CLI_FLAG_SENS,
					
					w_FD_FAC_DETFAC.no_sejour,
					w_FD_FAC_DETFAC.sejour_id,
					p_sejours.lieu_sortie_id,
					
					w_FD_FAC_DETFAC.no_facture,
					date(CLI_DATE_DEBPERIODE) AS date_debut,
					date(CLI_DATE_FINPERIODE) AS date_fin,
					CASE 
						WHEN CLI_FLAG_SENS = '0' THEN '1'
						WHEN w_FD_FAC_DETFAC.est_budget_global = '1' THEN 'H'
						ELSE '0' END::text AS type_facture,
					p_sejours.code_cp_demandee, 
					'1'::text AS code_facture,
					CLI_DATE_OBJETFAC AS date_facture,
					'1'::text AS code_vente,
					CLI_DATE_OBJETFAC AS date_vente,
					extract('year' FROM CLI_DATE_OBJETFAC) * 100 + extract('month' FROM CLI_DATE_OBJETFAC) AS mois_vente,
					'0'::text as code_cloture,
					w_FD_FAC_DETFAC.est_budget_global,
					w_FD_FAC_DETFAC.est_pmsi,
					0::numeric AS nb_rejets,
					w_FD_FAC_DETFAC.no_facture AS no_facture_reference,
					0::bigint AS facture_reference_id,
					CASE WHEN CLI_FLAG_SENS = '0' THEN no_facture_avoir ELSE '' END AS no_facture_od_avoir,
					0::bigint AS facture_od_avoir_id,
					w_FD_FAC_DETFAC.ghs_id AS ghs_id,
					0::bigint AS ghs_bebe1_id, 
					0::bigint AS ghs_bebe2_id, 
					0::bigint AS ghs_bebe3_id,
					''::text as particularite_t2a, 
					w_FD_FAC_DETFAC.type_tiers_payant,
					w_FD_FAC_DETFAC.tiers_payant_1_id,
					w_FD_FAC_DETFAC.tiers_payant_2_id,
					0::bigint AS tiers_payant_22_id,
					w_FD_FAC_DETFAC.tiers_payant_0_id,
					
					'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,
					'0'::text AS code_expedie_0,
					'0'::text AS code_expedie_1,
					'0'::text AS code_expedie_2,
					'0'::text AS code_expedie_22,
					''::text AS no_bordereau_0,
					''::text AS no_bordereau_1,
					''::text AS no_bordereau_2,
					''::text AS no_bordereau_22,
					
					'20991231'::date AS date_solde,
					'20991231'::date AS date_solde_c,
					'20991231'::date AS date_solde_h,
					'20991231'::date AS date_solde_0_c,
					'20991231'::date AS date_solde_0_h,
					'20991231'::date AS date_solde_1_c,
					'20991231'::date AS date_solde_1_h,	
					'20991231'::date AS date_solde_2_c,
					'20991231'::date AS date_solde_2_h,	
					'20991231'::date AS date_solde_22_c,
					'20991231'::date AS date_solde_22_h,	
					CLI_DONN_MONTANT AS montant_facture_c,
					0::numeric AS montant_facture_h,
					0::numeric AS montant_facture_0_c,
					0::numeric AS montant_facture_0_h,
					0::numeric 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,
					COALESCE(CLI_DONN_MONTANTLIG,0) AS montant_facture_c_actes_inclus_dans_sejour,
					0::numeric AS montant_facture_h_actes_inclus_dans_sejour,
					0::numeric AS montant_comptabilise_c, 
					0::numeric AS montant_comptabilise_h, 
					0::numeric AS montant_comptabilise_0_c,
					0::numeric AS montant_comptabilise_0_h, 
					0::numeric 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_comptabilise_budget_global_c,
					0::numeric AS montant_regle_c,
					0::numeric AS 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::numeric AS delai_facture,
					
					0::numeric AS delai_expedition,
					0::numeric AS delai_expedition_0,
					0::numeric AS delai_expedition_1,
					0::numeric AS delai_expedition_2,
					0::numeric AS delai_expedition_22,
					0::numeric AS delai_solde,
					0::numeric AS delai_solde_0_c,
					0::numeric AS delai_solde_0_h,
					0::numeric AS delai_solde_1_c,
					0::numeric AS delai_solde_1_h,
					0::numeric AS delai_solde_2_c,
					0::numeric AS delai_solde_2_h,
					0::numeric AS delai_solde_22_c,
					0::numeric AS delai_solde_22_h

				FROM w_FD_FAC_DETFAC 
					JOIN activite[PX].p_sejours ON w_FD_FAC_DETFAC.no_sejour = p_sejours.no_sejour AND p_sejours.etat = ''
					LEFT JOIN w_FD_FAC_DETFAC_inclus ON w_FD_FAC_DETFAC_inclus.no_facture = w_FD_FAC_DETFAC.no_facture
				ORDER BY 1,2;


				UPDATE w_factures 
				SET date_vente = (CLI_DONN_EXERCOMPTA || '0101')::date
				WHERE date_vente < (CLI_DONN_EXERCOMPTA || '0101')::date;
				
				
				UPDATE w_factures 
				SET date_vente = (CLI_DONN_EXERCOMPTA || '1231')::date
				WHERE date_vente > (CLI_DONN_EXERCOMPTA || '1231')::date;
				
				UPDATE w_factures 
				SET mois_vente = extract('year' FROM date_vente) * 100 + extract('month' FROM date_vente)
				WHERE mois_vente IS DISTINCT FROM extract('year' FROM date_vente) * 100 + extract('month' FROM date_vente);

				UPDATE w_factures SET 
					montant_facture_1_c = montant_facture_c,
					montant_comptabilise_1_c = CASE WHEN est_budget_global <> '1' THEN montant_facture_c ELSE 0 END,
					montant_comptabilise_c = CASE WHEN est_budget_global <> '1' THEN montant_facture_c ELSE 0 END,
					montant_regle_c = CASE WHEN est_budget_global <> '1' AND CLI_CODE_ETAT IN ('LQ','RG') THEN montant_facture_c ELSE 0 END,
					montant_regle_1_c = CASE WHEN est_budget_global <> '1' AND CLI_CODE_ETAT IN ('LQ','RG') THEN montant_facture_c ELSE 0 END,
					montant_comptabilise_budget_global_c = CASE WHEN est_budget_global = '1' THEN montant_facture_c ELSE 0 END
				WHERE type_tiers_payant = '1';


				UPDATE w_factures SET 
					montant_facture_2_c = montant_facture_c,
					montant_comptabilise_2_c = montant_facture_c,
					montant_comptabilise_c = montant_facture_c,
					montant_regle_c = CASE WHEN CLI_CODE_ETAT IN ('LQ','RG') THEN montant_facture_c ELSE 0 END,
					montant_regle_2_c = CASE WHEN CLI_CODE_ETAT IN ('LQ','RG') THEN montant_facture_c ELSE 0 END
				WHERE type_tiers_payant = '2';
				
				UPDATE w_factures SET 
					montant_facture_0_c = montant_facture_c,
					montant_comptabilise_0_c = montant_facture_c,
					montant_comptabilise_c = montant_facture_c,
					montant_regle_c = CASE WHEN CLI_CODE_ETAT IN ('LQ','RG') THEN montant_facture_c ELSE 0 END,
					montant_regle_0_c = CASE WHEN CLI_CODE_ETAT IN ('LQ','RG') THEN montant_facture_c ELSE 0 END
				WHERE type_tiers_payant = '0';



				UPDATE w_factures SET 
					montant_facture_0_c = montant_facture_c,
					montant_comptabilise_0_c = montant_facture_c,
					montant_comptabilise_c = montant_facture_c,
					montant_regle_c = CASE WHEN CLI_CODE_ETAT IN ('LQ','RG') THEN montant_facture_c ELSE 0 END,
					montant_regle_0_c = CASE WHEN CLI_CODE_ETAT IN ('LQ','RG') THEN montant_facture_c ELSE 0 END
				WHERE type_tiers_payant = '0';


				UPDATE w_factures SET 
					no_facture_od_avoir = w_factures_od_avoir.no_facture_od_avoir,
					type_facture = 'X'
				FROM w_factures w_factures_od_avoir 
				WHERE w_factures_od_avoir.no_facture_od_avoir = w_factures.no_facture AND 
					 w_factures_od_avoir.no_facture_od_avoir <> '' AND 
					w_factures_od_avoir.type_facture = '1' AND 
					w_factures.type_facture <> '1';

				UPDATE w_factures
				SET facture_od_avoir_id = w_factures_od_avoir.oid
				FROM w_factures w_factures_od_avoir
				WHERE w_factures.no_facture_od_avoir = w_factures_od_avoir.no_facture AND
					w_factures.no_facture_od_avoir <> '';

				
				UPDATE w_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 w_factures.no_sejour = p_sejours.no_sejour AND
					CASE WHEN code_sorti = '1' THEN date_sortie ELSE date(now()) END - date_entree < 30 AND
					(
					w_factures.date_debut <> date_entree OR
					w_factures.date_fin <> CASE WHEN code_sorti = '1' THEN date_sortie ELSE date_fin END
					);
				
				
				DROP TABLE IF EXISTS w_factures_references;
				CREATE TEMP TABLE w_factures_references AS
				SELECT no_sejour, date_debut, 
					(MIN(CASE WHEN type_tiers_payant = '1' AND type_facture IN ('0','G','H') THEN Array[date_facture::text,no_facture] ELSE NULL END))[2] AS no_facture_1_0,
					(MIN(CASE WHEN type_tiers_payant = '1' AND type_facture = '2' THEN Array[date_facture::text,no_facture] ELSE NULL END))[2] AS no_facture_1_2,
					(MIN(CASE WHEN type_tiers_payant = '2' AND type_facture = '0' THEN Array[date_facture::text,no_facture] ELSE NULL END))[2] AS no_facture_2_0,
					(MIN(CASE WHEN type_tiers_payant = '2' AND type_facture = '2' THEN Array[date_facture::text,no_facture] ELSE NULL END))[2] AS no_facture_2_2,
					(MIN(CASE WHEN type_tiers_payant = '0' AND type_facture = '0' THEN Array[date_facture::text,no_facture] ELSE NULL END))[2] AS no_facture_0_0,
					(MIN(CASE WHEN type_tiers_payant = '0' AND type_facture = '2' THEN Array[date_facture::text,no_facture] ELSE NULL END))[2] AS no_facture_0_2,
					(MIN(Array[date_facture::text,no_facture]))[2] AS no_facture_x
				FROM w_factures
				GROUP BY 1,2
				ORDER BY 1,2;
				
				
				CREATE INDEX w_factures_references_i1
				ON w_factures_references
				USING btree
				(no_sejour);
				
				CREATE INDEX w_factures_i1
				ON w_factures
				USING btree
				(no_sejour);
				

				UPDATE w_factures
				SET no_facture_reference = COALESCE(no_facture_1_0,no_facture_1_2,no_facture_2_0,no_facture_2_2,no_facture_0_0,no_facture_0_2,no_facture_x)
				FROM w_factures_references
				WHERE w_factures.no_sejour = w_factures_references.no_sejour AND
					w_factures.date_debut = w_factures_references.date_debut AND
					no_facture_reference IS DISTINCT FROM COALESCE(no_facture_1_0,no_facture_1_2,no_facture_2_0,no_facture_2_2,no_facture_0_0,no_facture_0_2,no_facture_x);


				UPDATE w_factures
				SET type_facture = '2' 
				FROM w_factures_references
				WHERE w_factures.no_sejour = w_factures_references.no_sejour AND
					w_factures.date_debut = w_factures_references.date_debut AND
					type_facture = '0' AND
					type_tiers_payant = '1' AND
					no_facture IS DISTINCT FROM no_facture_1_0;

				UPDATE w_factures
				SET type_facture = '2' 
				FROM w_factures_references
				WHERE w_factures.no_sejour = w_factures_references.no_sejour AND
					w_factures.date_debut = w_factures_references.date_debut AND
					type_facture = '0' AND
					type_tiers_payant = '2' AND
					no_facture IS DISTINCT FROM no_facture_2_0;

				UPDATE w_factures
				SET type_facture = '2' 
				FROM w_factures_references
				WHERE w_factures.no_sejour = w_factures_references.no_sejour AND
					w_factures.date_debut = w_factures_references.date_debut AND
					type_facture = '0' AND
					type_tiers_payant = '0' AND
					no_facture IS DISTINCT FROM no_facture_0_0;
					
					
				UPDATE w_factures
				SET facture_reference_id = w_factures_references.oid
				FROM w_factures w_factures_references
				WHERE w_factures.no_facture_reference = w_factures_references.no_facture AND
					w_factures.facture_reference_id IS DISTINCT FROM w_factures_references.oid;
					
					

				
				UPDATE w_factures SET date_expedition_0 = date_facture WHERE date_expedition_0 < date_facture AND date_facture <> '2099-12-31';
				
				UPDATE w_factures
				SET
					date_solde_0_c = CASE WHEN montant_comptabilise_0_c = 0 THEN '00010101' WHEN montant_comptabilise_0_c = montant_regle_0_c THEN date_solde_0_c ELSE '20991231' END,
					date_solde_0_h = CASE WHEN montant_comptabilise_0_h = 0 THEN '00010101' WHEN montant_comptabilise_0_h = montant_regle_0_h THEN date_solde_0_h ELSE '20991231' END,
					date_solde_1_c = CASE WHEN montant_comptabilise_1_c = 0 THEN '00010101' WHEN montant_comptabilise_1_c = montant_regle_1_c THEN date_solde_1_c ELSE '20991231' END,
					date_solde_1_h = CASE WHEN montant_comptabilise_1_h = 0 THEN '00010101' WHEN montant_comptabilise_1_h = montant_regle_1_h THEN date_solde_1_h ELSE '20991231' END,
					date_solde_2_c = CASE WHEN montant_comptabilise_2_c = 0 THEN '00010101' WHEN montant_comptabilise_2_c = montant_regle_2_c THEN date_solde_2_c ELSE '20991231' END,
					date_solde_2_h = CASE WHEN montant_comptabilise_2_h = 0 THEN '00010101' WHEN montant_comptabilise_2_h = montant_regle_2_h THEN date_solde_2_h ELSE '20991231' END,
					date_solde_22_c = CASE WHEN montant_comptabilise_22_c = 0 THEN '00010101' WHEN montant_comptabilise_22_c = montant_regle_22_c THEN date_solde_22_c ELSE '20991231' END,
					date_solde_22_h = CASE WHEN montant_comptabilise_22_h = 0 THEN '00010101' WHEN montant_comptabilise_22_h = montant_regle_22_h THEN date_solde_22_h ELSE '20991231' END
				WHERE
				(
					date_solde_0_c <> CASE WHEN montant_comptabilise_0_c = 0 THEN '00010101' WHEN montant_comptabilise_0_c = montant_regle_0_c THEN date_solde_0_c ELSE '20991231' END OR
					date_solde_0_h <> CASE WHEN montant_comptabilise_0_h = 0 THEN '00010101' WHEN montant_comptabilise_0_h = montant_regle_0_h THEN date_solde_0_h ELSE '20991231' END OR
					date_solde_1_c <> CASE WHEN montant_comptabilise_1_c = 0 THEN '00010101' WHEN montant_comptabilise_1_c = montant_regle_1_c THEN date_solde_1_c ELSE '20991231' END OR
					date_solde_1_h <> CASE WHEN montant_comptabilise_1_h = 0 THEN '00010101' WHEN montant_comptabilise_1_h = montant_regle_1_h THEN date_solde_1_h ELSE '20991231' END OR
					date_solde_2_c <> CASE WHEN montant_comptabilise_2_c = 0 THEN '00010101' WHEN montant_comptabilise_2_c = montant_regle_2_c THEN date_solde_2_c ELSE '20991231' END OR
					date_solde_2_h <> CASE WHEN montant_comptabilise_2_h = 0 THEN '00010101' WHEN montant_comptabilise_2_h = montant_regle_2_h THEN date_solde_2_h ELSE '20991231' END OR
					date_solde_22_c <> CASE WHEN montant_comptabilise_22_c = 0 THEN '00010101' WHEN montant_comptabilise_22_c = montant_regle_22_c THEN date_solde_22_c ELSE '20991231' END OR
					date_solde_22_h <> CASE WHEN montant_comptabilise_22_h = 0 THEN '00010101' WHEN montant_comptabilise_22_h = montant_regle_22_h THEN date_solde_22_h ELSE '20991231' END
				);
			
				DROP TABLE IF EXISTS w_factures_soldes;
				CREATE TEMP TABLE w_factures_soldes AS
				SELECT 
					no_facture,
					GREATEST(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) AS date_solde,
					GREATEST(date_solde_0_c, date_solde_1_c, date_solde_2_c, date_solde_22_c) AS date_solde_c,
					GREATEST(date_solde_0_h, date_solde_1_h, date_solde_2_h, date_solde_22_h) AS date_solde_h
				FROM w_factures;
				
				UPDATE w_factures
				SET 
					date_solde = w_factures_soldes.date_solde,
					date_solde_c = w_factures_soldes.date_solde_c,
					date_solde_h = w_factures_soldes.date_solde_h
				FROM w_factures_soldes
				WHERE w_factures.no_facture = w_factures_soldes.no_facture AND 
					(
					w_factures.date_solde IS NULL OR w_factures.date_solde <> w_factures_soldes.date_solde OR 
					w_factures.date_solde_c IS NULL OR w_factures.date_solde_c <> w_factures_soldes.date_solde_c OR 
					w_factures.date_solde_h IS NULL OR w_factures.date_solde_h <> w_factures_soldes.date_solde_h
					);
					
					
				-- Mutuelle comme seul tiers payant
				UPDATE w_factures
				SET tiers_payant_2_id = tiers_payant_1_id
				FROM activite[PX].t_tiers_payant
				WHERE tiers_payant_1_id = t_tiers_payant.oid AND t_tiers_payant.type_tiers_payant = '2' AND
					montant_facture_2_c <> 0 AND tiers_payant_2_id = 0;



				-- Ajout des factures
				
				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,
					no_sejour,
					sejour_id,
					no_facture,
					date_debut,
					date_fin,
					type_facture,
					code_cp_demandee, 
					code_facture,
					date_facture,
					code_vente,
					date_vente,
					mois_vente,
					code_cloture,
					nb_rejets,
					no_facture_reference,
					facture_reference_id,
					no_facture_od_avoir,
					facture_od_avoir_id,
					ghs_id,
					ghs_bebe1_id, 
					ghs_bebe2_id, 
					ghs_bebe3_id,
					particularite_t2a, 
					tiers_payant_1_id,
					tiers_payant_2_id,
					tiers_payant_22_id,
					tiers_payant_0_id,
					date_expedition, 
					date_expedition_0,
					date_expedition_1,
					date_expedition_2,
					date_expedition_22,
					code_expedie_0,
					code_expedie_1,
					code_expedie_2,
					code_expedie_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,	
					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_facture_c_actes_inclus_dans_sejour,
					montant_facture_h_actes_inclus_dans_sejour,
					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_comptabilise_budget_global_c,
					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,
					delai_facture,
					delai_expedition,
					delai_expedition_0,
					delai_expedition_1,
					delai_expedition_2,
					delai_expedition_22,
					delai_solde,
					delai_solde_0_c,
					delai_solde_0_h,
					delai_solde_1_c,
					delai_solde_1_h,
					delai_solde_2_c,
					delai_solde_2_h,
					delai_solde_22_c,
					delai_solde_22_h
				)
				SELECT
					oid,
					no_sejour,
					sejour_id,
					no_facture,
					date_debut,
					date_fin,
					type_facture,
					code_cp_demandee, 
					code_facture,
					date_facture,
					code_vente,
					date_vente,
					mois_vente,
					code_cloture,
					nb_rejets,
					no_facture_reference,
					facture_reference_id,
					no_facture_od_avoir,
					facture_od_avoir_id,
					ghs_id,
					ghs_bebe1_id, 
					ghs_bebe2_id, 
					ghs_bebe3_id,
					particularite_t2a, 
					tiers_payant_1_id,
					tiers_payant_2_id,
					tiers_payant_22_id,
					tiers_payant_0_id,
					date_expedition, 
					date_expedition_0,
					date_expedition_1,
					date_expedition_2,
					date_expedition_22,
					code_expedie_0,
					code_expedie_1,
					code_expedie_2,
					code_expedie_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,	
					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_facture_c_actes_inclus_dans_sejour,
					montant_facture_h_actes_inclus_dans_sejour,
					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_comptabilise_budget_global_c,
					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,
					delai_facture,
					delai_expedition,
					delai_expedition_0,
					delai_expedition_1,
					delai_expedition_2,
					delai_expedition_22,
					delai_solde,
					delai_solde_0_c,
					delai_solde_0_h,
					delai_solde_1_c,
					delai_solde_1_h,
					delai_solde_2_c,
					delai_solde_2_h,
					delai_solde_22_c,
					delai_solde_22_h
				FROM w_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'
				GROUP BY 1,2,3,4,5,6,7,8,9,10,11;

			
				-- Séjours sans facture (hors BG)
				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, 
					'E' || no_sejour || CASE WHEN avec_facturation_intermediaire = '1' THEN '_' || p_calendrier.mois ELSE '' END, 
					'E' || no_sejour || 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_sejours
				SET est_sans_facturation = '1' 
				WHERE lieu_sortie_id IN 
					(SELECT oid FROM activite[PX].t_lieux WHERE 
						service_facturation_id IN
						(SELECT oid FROM activite[PX].t_services_facturation WHERE est_sans_facturation = '1') OR
						unite_fonctionnelle_id IN
						(SELECT oid FROM activite[PX].t_unites_fonctionnelles WHERE est_sans_facturation = '1') 
					);
				
				-- Séjours avec caisse BG mais facturés hors BG
				UPDATE activite[PX].p_sejours
				SET est_budget_global = subview.est_budget_global
				FROM (
				SELECT no_sejour,
					MAX(CASE WHEN montant_comptabilise_budget_global_c <> 0 THEN 1 ELSE 0 END) AS est_budget_global
				FROM activite[PX].p_factures
				WHERE montant_facture_1_c <> 0
				GROUP BY 1
				) subview
				WHERE p_sejours.no_sejour = subview.no_sejour AND p_sejours.est_budget_global <> subview.est_budget_global;
				
				
				
				
				-- Initialisation TP2 si pas connu
				UPDATE activite[PX].p_factures
				SET tiers_payant_2_id = (SELECT oid FROM activite.t_tiers_payant WHERE code_original = 'CTI_AMC')
				WHERE (tiers_payant_2_id = 0 OR tiers_payant_2_id IS NULL) AND
					montant_facture_2_c <> 0;

				UPDATE activite[PX].p_factures
				SET tiers_payant_2_id = 0
				WHERE tiers_payant_2_id IS NULL;

				UPDATE activite[PX].p_factures
				SET tiers_payant_1_id = (SELECT oid FROM activite.t_tiers_payant WHERE code_original = 'CTI_AMC')
				WHERE (tiers_payant_1_id = 0 OR tiers_payant_1_id IS NULL) AND
					montant_facture_1_c <> 0;

				UPDATE activite[PX].p_factures
				SET tiers_payant_1_id = 0
				WHERE tiers_payant_1_id IS NULL;

				UPDATE activite[PX].p_sejours
				SET tiers_payant_2_id = subview.tiers_payant_2_id
				FROM 
				(
					SELECT no_sejour, (MAX(Array[date_facture::text, tiers_payant_2_id::text]))[2]::bigint AS tiers_payant_2_id
					FROM activite[PX].p_factures
					WHERE tiers_payant_2_id <> 0
					GROUP BY 1
				) subview
				WHERE p_sejours.no_sejour = subview.no_sejour AND
					(p_sejours.tiers_payant_2_id = 0 OR p_sejours.tiers_payant_2_id IS NULL)
					;

										
										
			]]></sqlcmd>
		</NODE>
		
		<NODEx label="Rejets">
			<sqlcmd><![CDATA[

				
										
			]]></sqlcmd>
		</NODEx>
		
		<NODEx label="Lignes non facturées Clinique">
			<sqlcmd><![CDATA[
			
				TRUNCATE activite[PX].p_factures_lignes_non_facturees_c;
				
				
			]]></sqlcmd>
		</NODEx>
		
		<NODE label="Lignes facturées Clinique">
			<sqlcmd><![CDATA[
			

				INSERT INTO activite.t_prestations(code, code_original, texte, texte_court)
				SELECT CLI_CODE_PRESTATION, CLI_CODE_PRESTATION, CLI_CODE_PRESTATION, CLI_CODE_PRESTATION
				FROM w_FD_FAC_DETFACLIG
					LEFT JOIN activite.t_prestations ON CLI_CODE_PRESTATION = t_prestations.code
				WHERE t_prestations.code IS NULL
				GROUP BY CLI_CODE_PRESTATION;


				INSERT INTO activite[PX].t_rubriques_facturation(code, code_original, texte, texte_court)
				SELECT CLI_CODE_PRESTATION, CLI_CODE_PRESTATION, CLI_CODE_PRESTATION, CLI_CODE_PRESTATION
				FROM w_FD_FAC_DETFACLIG
					LEFT JOIN activite[PX].t_rubriques_facturation ON CLI_CODE_PRESTATION = t_rubriques_facturation.code
				WHERE t_rubriques_facturation.code IS NULL
				GROUP BY CLI_CODE_PRESTATION;
				
				INSERT INTO base.t_actes(code, texte, texte_court, nomenclature, ccam_regroupement_id_1, ccam_regroupement_id_4)
				SELECT substring(CLI_CODE_CCAM, 1, 7), 
					substring(CLI_CODE_CCAM, 1, 7), 
					substring(CLI_CODE_CCAM, 1, 7), 
					CASE WHEN length(CLI_CODE_CCAM) >= 7 THEN 'CCAM' ELSE 'NGAP' END,
					(SELECt oid FROM base.t_ccam_regroupements WHERE code = 'NGAP'), 
					(SELECt oid FROM base.t_ccam_regroupements WHERE code = 'NGAP')
								FROM w_FD_FAC_DETFACLIG
									LEFT JOIN base.t_actes ON substring(CLI_CODE_CCAM, 1, 7) = t_actes.code
								WHERE t_actes.code IS NULL AND CLI_CODE_CCAM <> ''
								GROUP BY 1,2,3,4,5,6;


				DROP SEQUENCE IF EXISTS w_factures_lignes_c_sequence;
				CREATE TEMP SEQUENCE w_factures_lignes_c_sequence;

				-- facturé
				
				DROP TABLE IF EXISTS temp.w_factures_lignes_c;
				CREATE TABLE temp.w_factures_lignes_c AS
				SELECT
					w_factures.no_sejour,
					w_factures.no_facture,
					w_factures.type_facture,
					w_factures.oid AS facture_id,
					w_factures.est_budget_global,
					w_factures.est_pmsi,

					w_FD_FAC_DETFACLIG.CLI_CODE_ETAT,
					w_FD_FAC_DETFACLIG.CLI_FLAG_SENS,
					w_factures.type_tiers_payant,
					w_FD_FAC_DETFACLIG.CLI_DATE_DEBPREST AS date_debut,
					w_FD_FAC_DETFACLIG.CLI_DATE_FINPREST AS date_fin,
					w_factures.lieu_sortie_id AS lieu_id,
					CASE WHEN inclus_dans_sejour <> '1' THEN 1::bigint ELSE 3::bigint END AS origine_facturation_id,
					COALESCE(t_compte.oid,0) AS compte_produit_id,
					t_rubriques_facturation.oid AS rubrique_facturation_id,
					t_rubriques_facturation.oid AS rubrique_comptabilisation_id,
					t_prestations.oid AS prestation_id,
					
					COALESCE(t_medecins_administratifs.oid,0) AS medecin_facture_id,
					COALESCE(t_medecins_administratifs.oid,0) AS medecin_comptabilise_id,
					COALESCE(t_actes.oid,0) AS acte_id,
					CLI_CODE_PHASE AS phase_ccam,
					CLI_CODE_ACTIVITE AS activite_ccam,
					CLI_DONN_EXTDOC AS extension_ccam,
					SUBSTR(CLI_DONN_MODIFICATEUR,1,1) AS modificateur_ccam_1,
					SUBSTR(CLI_DONN_MODIFICATEUR,2,1) AS modificateur_ccam_2,
					SUBSTR(CLI_DONN_MODIFICATEUR,3,1) AS modificateur_ccam_3,
					SUBSTR(CLI_DONN_MODIFICATEUR,4,1) AS modificateur_ccam_4,
					
					CLI_DONN_QTE AS nb_rubrique,
					CLI_DONN_QTE AS nb_prestation,
					CLI_DONN_COEFF AS coefficient,
					1::numeric AS coefficient_mco,
					CLI_DONN_TU AS prix_unitaire,
					nextval('w_factures_lignes_c_sequence'::regclass) AS oid,
					CASE WHEN type_tiers_payant = '0' THEN CLI_DONN_TAUX ELSE 0 END AS taux_0,
					CASE WHEN type_tiers_payant = '1' THEN CLI_DONN_TAUX ELSE 0 END AS taux_1,
					CASE WHEN type_tiers_payant = '2' THEN CLI_DONN_TAUX ELSE 0 END AS taux_2,
					0::numeric AS taux_22,
					CASE WHEN inclus_dans_sejour <> '1' THEN CLI_DONN_MONTANTLIG ELSE 0 END AS montant_facture,
					0::numeric AS montant_facture_0,
					0::numeric AS montant_facture_1,
					0::numeric 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 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,
					CASE WHEN inclus_dans_sejour = '1' THEN CLI_DONN_MONTANTLIG ELSE 0 END 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,
					0::numeric AS rubrique_facture_id
				FROM w_FD_FAC_DETFACLIG
					JOIN w_factures ON w_FD_FAC_DETFACLIG.CLI_DONN_NOABSEPISODE = w_factures.CLI_DONN_NOABSEPISODE AND
						w_FD_FAC_DETFACLIG.CLI_Donn_NoObjIntFac = w_factures.CLI_Code_NoInterneObjet AND
						w_FD_FAC_DETFACLIG.CLI_DONN_EXERCOMPTA = w_factures.CLI_DONN_EXERCOMPTA
					JOIN activite[PX].t_rubriques_facturation ON CLI_CODE_PRESTATION = t_rubriques_facturation.code_original
					JOIN activite.t_prestations ON CLI_CODE_PRESTATION = t_prestations.code
					LEFT JOIN activite[PX].t_compte ON CLI_CODE_ORDONNATEUR = t_compte.code
					LEFT JOIN activite[PX].t_medecins_administratifs ON CLI_CODE_MEDECINREALISATEUR = t_medecins_administratifs.code_original
					LEFT JOIN base.t_actes ON substring(CLI_CODE_CCAM, 1, 7) = t_actes.code
				;

				UPDATE temp.w_factures_lignes_c SET 
					montant_facture_1 = montant_facture,
					montant_comptabilise = CASE WHEN est_budget_global <> '1' THEN montant_facture ELSE 0 END,
					montant_comptabilise_1 = CASE WHEN est_budget_global <> '1' THEN montant_facture ELSE 0 END,
					montant_comptabilise_budget_global_1 = CASE WHEN est_budget_global = '1' THEN montant_facture ELSE 0 END,
					montant_facture_1_actes_inclus_dans_sejour = montant_facture_actes_inclus_dans_sejour
				WHERE type_tiers_payant = '1';


				UPDATE temp.w_factures_lignes_c SET 
					montant_facture_2 = montant_facture,
					montant_comptabilise = montant_facture,
					montant_comptabilise_2 = montant_facture,
					montant_facture_2_actes_inclus_dans_sejour = montant_facture_actes_inclus_dans_sejour
				WHERE type_tiers_payant = '2';
				
				UPDATE temp.w_factures_lignes_c SET 
					montant_facture_0 = montant_facture,
					montant_comptabilise = montant_facture,
					montant_comptabilise_0 = montant_facture,
					montant_facture_0_actes_inclus_dans_sejour = montant_facture_actes_inclus_dans_sejour
				WHERE type_tiers_payant = '0';
				

				ALTER TABLE temp.w_factures_lignes_c
					ADD CONSTRAINT w_factures_lignes_c_pk PRIMARY KEY(oid);

				CREATE INDEX w_factures_lignes_c_i1
				ON temp.w_factures_lignes_c
				USING btree
				(no_sejour);
				

				-- Nombre = coefficient pour rubriques à ventiler
				UPDATE temp.w_factures_lignes_c
				SET nb_rubrique = nb_rubrique * coefficient, nb_prestation = nb_prestation * coefficient, coefficient = 1
				FROM activite.t_prestations
				WHERE coefficient > 1 AND
					prestation_id = t_prestations.oid AND
					(t_prestations.code IN ('CP', 'CPC', 'SHO') OR
					t_prestations.type_ventilation_jour = '1')
				;
				
				-- dates = periode facture si date debut = date fin et nombre > 1
				UPDATE temp.w_factures_lignes_c
				SET date_debut = p_factures.date_debut, date_fin = p_factures.date_fin
				FROM activite.t_prestations, activite[PX].p_factures
				WHERE ABS(nb_rubrique) > 1 AND
					p_factures.no_facture = w_factures_lignes_c.no_facture AND
					prestation_id = t_prestations.oid AND
					(t_prestations.code IN ('CP', 'CPC', 'SHO') OR
					t_prestations.type_ventilation_jour = '1');


				-- Eclatement des supplements par jour
				DROP TABLE IF EXISTS w_factures_lignes_sup_c;
				CREATE TEMP TABLE w_factures_lignes_sup_c AS
				SELECT
					w_factures_lignes_c.oid AS from_oid,
					t_prestations.code AS prestation_code,
					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,
					NULL::date[] AS dates_prestation_ghs,
					w_factures_lignes_c.*
				FROM temp.w_factures_lignes_c
					JOIN activite[PX].p_sejours ON w_factures_lignes_c.no_sejour = p_sejours.no_sejour
					JOIN activite.t_prestations ON prestation_id = t_prestations.oid AND 
						(
						code IN ('PJ', 'FJ', 'TM', 'PHJ','SSM', 'GHS', 'EXH', 'SRC', 'REA', 'STF', 'NN1', 'NN2', 'NN3', 'CP', 'CPC', 'SHO')  OR
						t_prestations.type_ventilation_jour = '1'
						)
				WHERE (nb_prestation > 1 or nb_prestation < -1 OR (t_prestations.code = 'GHS' AND date(date_fin) - date(date_debut) > 1)) ;


				-- Nombre de jours GHS
				UPDATE w_factures_lignes_sup_c SET 
					date_fin_ghs = COALESCE(date(date_debut_exh - interval '1 day'), subview.date_fin_ghs),
					nb_prestation_ghs = COALESCE(date(date_debut_exh - interval '1 day'), subview.date_fin_ghs) - date_debut_ghs + 1
				FROM (
					SELECT no_facture, 
						date(MAX(CASE WHEN code = 'GHS' THEN date_fin ELSE NULL END)) AS date_fin_ghs,
						date(MIN(CASE WHEN code = 'EXH' THEN date_debut ELSE NULL END)) AS date_debut_exh
					FROM temp.w_factures_lignes_c
						JOIN activite.t_prestations ON prestation_id = t_prestations.oid AND code IN ('GHS', 'EXH')
					GROUP BY 1) subview
				WHERE w_factures_lignes_sup_c.no_facture = subview.no_facture;
				
				
				



				DROP TABLE IF EXISTS w_factures_lignes_sup_c_gen;
				CREATE TEMP TABLE w_factures_lignes_sup_c_gen AS
				SELECT
					from_oid,
					no_sejour,
					no_facture,
					type_facture,
					facture_id,
					est_budget_global,
					est_pmsi,
					CLI_CODE_ETAT,
					CLI_FLAG_SENS,
					type_tiers_payant,
					date AS date_debut,
					date AS date_fin,
					lieu_id,
					origine_facturation_id,
					compte_produit_id,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					prestation_id,
					medecin_facture_id,
					medecin_comptabilise_id,
					acte_id,
					phase_ccam,
					activite_ccam,
					extension_ccam,
					modificateur_ccam_1,
					modificateur_ccam_2,
					modificateur_ccam_3,
					modificateur_ccam_4,
					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,
					coefficient,
					coefficient_mco,
					prix_unitaire,
					oid,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					round(montant_facture / ABS(CASE WHEN prestation_code <> 'GHS' THEN nb_prestation ELSE nb_prestation_ghs END),2) AS montant_facture,
					round(montant_facture_0 / ABS(CASE WHEN prestation_code <> 'GHS' THEN nb_prestation ELSE nb_prestation_ghs END),2) AS montant_facture_0,
					round(montant_facture_1 / ABS(CASE WHEN prestation_code <> 'GHS' THEN nb_prestation ELSE nb_prestation_ghs END),2) AS montant_facture_1,
					round(montant_facture_2 / ABS(CASE WHEN prestation_code <> 'GHS' THEN nb_prestation ELSE nb_prestation_ghs END),2) AS montant_facture_2,
					round(montant_facture_22 / ABS(CASE WHEN prestation_code <> 'GHS' THEN nb_prestation ELSE nb_prestation_ghs END),2) AS montant_facture_22,
					round(montant_comptabilise / ABS(CASE WHEN prestation_code <> 'GHS' THEN nb_prestation ELSE nb_prestation_ghs END),2) AS montant_comptabilise,
					round(montant_comptabilise_0 / ABS(CASE WHEN prestation_code <> 'GHS' THEN nb_prestation ELSE nb_prestation_ghs END),2) AS montant_comptabilise_0,
					round(montant_comptabilise_1 / ABS(CASE WHEN prestation_code <> 'GHS' THEN nb_prestation ELSE nb_prestation_ghs END),2) AS montant_comptabilise_1,
					round(montant_comptabilise_2 / ABS(CASE WHEN prestation_code <> 'GHS' THEN nb_prestation ELSE nb_prestation_ghs END),2) AS montant_comptabilise_2,
					round(montant_comptabilise_22 / ABS(CASE WHEN prestation_code <> 'GHS' THEN nb_prestation ELSE nb_prestation_ghs END),2) AS 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,
					round(montant_comptabilise_budget_global_1 / ABS(CASE WHEN prestation_code <> 'GHS' THEN nb_prestation ELSE nb_prestation_ghs END),2) AS montant_comptabilise_budget_global_1
				FROM w_factures_lignes_sup_c
					JOIN base.p_calendrier ON date_debut BETWEEN p_calendrier.date - ABS(CASE WHEN prestation_code <> 'GHS' THEN nb_prestation ELSE nb_prestation_ghs END)::integer + 1 AND p_calendrier.date AND 
								p_calendrier.date <> date_debut ;



				DROP TABLE IF EXISTS w_factures_lignes_sup_c_gen_tot;
				CREATE TEMP TABLE w_factures_lignes_sup_c_gen_tot AS
				SELECT from_oid,
					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,
					SUM(montant_comptabilise_budget_global_1) AS montant_comptabilise_budget_global_1
				FROM w_factures_lignes_sup_c_gen
				GROUP BY 1;



				UPDATE temp.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_gen_tot.nb_rubrique,
					nb_prestation = w_factures_lignes_c.nb_prestation - w_factures_lignes_sup_c_gen_tot.nb_prestation,
					montant_facture = w_factures_lignes_c.montant_facture - w_factures_lignes_sup_c_gen_tot.montant_facture,
					montant_facture_0 = w_factures_lignes_c.montant_facture_0 - w_factures_lignes_sup_c_gen_tot.montant_facture_0,
					montant_facture_1 = w_factures_lignes_c.montant_facture_1 - w_factures_lignes_sup_c_gen_tot.montant_facture_1,
					montant_facture_2 = w_factures_lignes_c.montant_facture_2 - w_factures_lignes_sup_c_gen_tot.montant_facture_2,
					montant_facture_22 = w_factures_lignes_c.montant_facture_22 - w_factures_lignes_sup_c_gen_tot.montant_facture_22,
					montant_comptabilise = w_factures_lignes_c.montant_comptabilise - w_factures_lignes_sup_c_gen_tot.montant_comptabilise,
					montant_comptabilise_0 = w_factures_lignes_c.montant_comptabilise_0 - w_factures_lignes_sup_c_gen_tot.montant_comptabilise_0,
					montant_comptabilise_1 = w_factures_lignes_c.montant_comptabilise_1 - w_factures_lignes_sup_c_gen_tot.montant_comptabilise_1,
					montant_comptabilise_2 = w_factures_lignes_c.montant_comptabilise_2 - w_factures_lignes_sup_c_gen_tot.montant_comptabilise_2,
					montant_comptabilise_22 = w_factures_lignes_c.montant_comptabilise_22 - w_factures_lignes_sup_c_gen_tot.montant_comptabilise_22,
					montant_comptabilise_budget_global_1 = w_factures_lignes_c.montant_comptabilise_budget_global_1 - w_factures_lignes_sup_c_gen_tot.montant_comptabilise_budget_global_1
				FROM w_factures_lignes_sup_c_gen_tot
				WHERE w_factures_lignes_c.oid = from_oid;


				INSERT INTO temp.w_factures_lignes_c
				SELECT
					no_sejour,
					no_facture,
					type_facture,
					facture_id,
					est_budget_global,
					est_pmsi,
					CLI_CODE_ETAT,
					CLI_FLAG_SENS,
					type_tiers_payant,
					date_debut,
					date_fin,
					lieu_id,
					origine_facturation_id,
					compte_produit_id,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					prestation_id,
					medecin_facture_id,
					medecin_comptabilise_id,
					acte_id,
					phase_ccam,
					activite_ccam,
					extension_ccam,
					modificateur_ccam_1,
					modificateur_ccam_2,
					modificateur_ccam_3,
					modificateur_ccam_4,
					nb_rubrique,
					nb_prestation,
					coefficient,
					coefficient_mco,
					prix_unitaire,
					nextval('w_factures_lignes_c_sequence'::regclass) AS oid,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					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,
					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,
					0::numeric AS rubrique_facture_id
				FROM w_factures_lignes_sup_c_gen;
				
				
				-- Lieux d'exécution
				
				
				UPDATE temp.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_debut = p_mouvements_sejour.date AND 
					(p_mouvements_sejour.heure_fin = 240000 OR p_mouvements_sejour.nb_sorties_directes = 1) AND
					w_factures_lignes_c.lieu_id <> p_mouvements_sejour.lieu_id;
					
					
				
					
				-- Le code TM est basculé en BG ou HBG pour Coste Floret pour régimeeprincipal
				
				INSERT INTO activite[PX].t_rubriques_facturation(code, code_original, texte, texte_court)
				SELECT 'BG', 'CTI_BG', 'Séjour Budget Global',  'Séjour Budget Global'
				FROM activite.t_divers
				WHERE t_divers.code = 'CLINICOMBG' AND t_divers.valeur ='1' AND
					'CTI_BG' NOT IN (SELECT code_original FROM activite[PX].t_rubriques_facturation);
				
				
				INSERT INTO activite[PX].t_rubriques_facturation(code, code_original, texte, texte_court)
				SELECT 'HBG', 'CTI_HBG', 'Séjour Hors Budget Global',  'Séjour Hors Budget Global'
				FROM activite.t_divers
				WHERE t_divers.code = 'CLINICOMBG' AND t_divers.valeur ='1' AND
					'CTI_HBG' NOT IN (SELECT code_original FROM activite[PX].t_rubriques_facturation);
				




				UPDATE temp.w_factures_lignes_c
				SET rubrique_facturation_id = t_rubriques_facturation_bg.oid, rubrique_comptabilisation_id = t_rubriques_facturation_bg.oid
				FROM activite[PX].t_rubriques_facturation
				JOIN activite[PX].t_rubriques_facturation t_rubriques_facturation_bg ON t_rubriques_facturation_bg.code_original = 'CTI_BG'
				JOIN activite.t_divers ON t_divers.code = 'CLINICOMBG' AND t_divers.valeur ='1'
				WHERE w_factures_lignes_c.rubrique_facturation_id = t_rubriques_facturation.oid AND
					t_rubriques_facturation.code = 'TM' AND
					montant_comptabilise_budget_global_1 <> 0;



				UPDATE temp.w_factures_lignes_c
				SET rubrique_facturation_id = t_rubriques_facturation_bg.oid, rubrique_comptabilisation_id = t_rubriques_facturation_bg.oid
				FROM activite[PX].t_rubriques_facturation
				JOIN activite[PX].t_rubriques_facturation t_rubriques_facturation_bg ON t_rubriques_facturation_bg.code_original = 'CTI_HBG'
				JOIN activite.t_divers ON t_divers.code = 'CLINICOMBG' AND t_divers.valeur ='1'
				WHERE w_factures_lignes_c.rubrique_facturation_id = t_rubriques_facturation.oid AND
					t_rubriques_facturation.code = 'TM' AND
					montant_comptabilise_budget_global_1 = 0 AND
					taux_1 > 50;


				UPDATE temp.w_factures_lignes_c
				SET rubrique_facturation_id = t_rubriques_facturation_bg.oid, rubrique_comptabilisation_id = t_rubriques_facturation_bg.oid
				FROM activite[PX].t_rubriques_facturation
				JOIN activite[PX].t_rubriques_facturation t_rubriques_facturation_bg ON t_rubriques_facturation_bg.code_original = 'CTI_HBG'
				JOIN activite.t_divers ON t_divers.code = 'CLINICOMBG' AND t_divers.valeur ='1'
				WHERE w_factures_lignes_c.rubrique_facturation_id = t_rubriques_facturation.oid AND
					t_rubriques_facturation.code = 'TM' AND
					montant_comptabilise_budget_global_1 = 0 AND
					taux_2 > 50;


				UPDATE temp.w_factures_lignes_c
				SET rubrique_facturation_id = t_rubriques_facturation_bg.oid, rubrique_comptabilisation_id = t_rubriques_facturation_bg.oid
				FROM activite[PX].t_rubriques_facturation
				JOIN activite[PX].t_rubriques_facturation t_rubriques_facturation_bg ON t_rubriques_facturation_bg.code_original = 'CTI_HBG'
				JOIN activite.t_divers ON t_divers.code = 'CLINICOMBG' AND t_divers.valeur ='1'
				WHERE w_factures_lignes_c.rubrique_facturation_id = t_rubriques_facturation.oid AND
					t_rubriques_facturation.code = 'TM' AND
					montant_comptabilise_budget_global_1 = 0 AND
					taux_0 > 50;
					
					
				-- Les nombres TM sont supprimés sur la mutuellee et le patient (sauf coste floret)
				
				SELECT base.cti_execute(
				'UPDATE temp.w_factures_lignes_c
				SET nb_prestation = 0, nb_rubrique = 0
				FROM
				(
				SELECT 
					p_factures.no_sejour, 
					w_factures_lignes_c.date_debut,
					MAX(CASE WHEN taux_2 > 0 THEN w_factures_lignes_c.no_facture ELSE NULL END) AS no_facture,
					MAX(prestation_id) AS prestation_id
				FROM
					temp.w_factures_lignes_c
					JOIN activite[PX].p_factures ON w_factures_lignes_c.no_facture = p_factures.no_facture
					JOIN activite.t_prestations ON prestation_id = t_prestations.oid AND t_prestations.code = ''TM''
					LEFT JOIN activite.t_divers ON t_divers.code = ''CLINICOMBG'' AND t_divers.valeur = ''1''
				WHERE w_factures_lignes_c.date_debut = w_factures_lignes_c.date_fin AND nb_prestation <> 0 AND
					t_divers.code IS NULL
				GROUP BY 1,2
				HAVING count(*) > 1 AND MAX(taux_1) > 0 AND max(taux_2) > 0
				) subview
				WHERE w_factures_lignes_c.no_facture = subview.no_facture AND
					w_factures_lignes_c.date_debut = subview.date_debut AND
					w_factures_lignes_c.prestation_id = subview.prestation_id',50);



				SELECT base.cti_execute(
				'UPDATE temp.w_factures_lignes_c
				SET nb_prestation = 0, nb_rubrique = 0
				FROM
				(
				SELECT 
					p_factures.no_sejour, 
					w_factures_lignes_c.date_debut,
					MAX(CASE WHEN taux_0 > 0 THEN w_factures_lignes_c.no_facture ELSE NULL END) AS no_facture,
					MAX(prestation_id) AS prestation_id
				FROM
					temp.w_factures_lignes_c
					JOIN activite[PX].p_factures ON w_factures_lignes_c.no_facture = p_factures.no_facture
					JOIN activite.t_prestations ON prestation_id = t_prestations.oid AND t_prestations.code = ''TM''
					LEFT JOIN activite.t_divers ON t_divers.code = ''CLINICOMBG'' AND t_divers.valeur = ''1''
				WHERE w_factures_lignes_c.date_debut = w_factures_lignes_c.date_fin AND nb_prestation <> 0 AND
					t_divers.code IS NULL
				GROUP BY 1,2
				HAVING count(*) > 1 AND MAX(taux_1) > 0 AND max(taux_0) > 0
				) subview
				WHERE w_factures_lignes_c.no_facture = subview.no_facture AND
					w_factures_lignes_c.date_debut = subview.date_debut AND
					w_factures_lignes_c.prestation_id = subview.prestation_id',50);
					
				-- Rubriques internes cti
				SELECT activite[PX].cti_reorganize_rubrique_facture_c('temp.w_factures_lignes_c');
					
				-- Génération
			
				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(
					no_facture,
					facture_id,
					date_debut,
					date_fin,
					lieu_id,
					origine_facturation_id,
					rubrique_facture_id,
					compte_produit_id,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					prestation_id,
					nb_rubrique,
					nb_prestation,
					coefficient,
					coefficient_mco,
					prix_unitaire,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					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,
					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
					 )
				SELECT
					no_facture,
					facture_id,
					date_debut,
					date_fin,
					lieu_id,
					origine_facturation_id,
					rubrique_facture_id,
					compte_produit_id,
					rubrique_facturation_id,
					rubrique_comptabilisation_id,
					prestation_id,
					nb_rubrique,
					nb_prestation,
					coefficient,
					coefficient_mco,
					prix_unitaire,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					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,
					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
				FROM temp.w_factures_lignes_c;
				
				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');

				
	
				
				-- Facturee encours BG (sauf si TM AMC ou Patient
				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, 
					'E' || no_sejour || '-BG', 
					'E' || no_sejour || '-BG', 
					'E', 
					'0', 
					'20991231'::date,
					tiers_payant_0_id, tiers_payant_1_id, tiers_payant_2_id, tiers_payant_22_id, 
					code_cp_demandee , 
					p_sejours.date_entree,
					CASE WHEN code_sorti = '1' THEN p_sejours.date_sortie ELSE date(now()) END
				FROM activite.p_sejours
				WHERE est_budget_global = '1' AND
					date_sortie >= '20150101' AND
					type_sejour IN ('1', '2', '5') AND
					no_sejour NOT IN (
						SELECT no_sejour 
						FROM activite.p_factures
						WHERE no_facture LIKE '%BG'
						) AND
					no_sejour NOT IN (
						SELECT no_sejour
						FROM activite.p_factures_lignes_c
						JOIN activite.p_factures ON p_factures_lignes_c.no_facture = p_factures.no_facture
						JOIN activite.t_prestations ON p_factures_lignes_c.prestation_id = t_prestations.oid
						WHERE t_prestations.code = 'TM' AND
							(taux_1 >= 70 OR taux_2 >= 70 OR taux_0 >= 70) 
						GROUP BY 1
						HAVING SUM(p_factures_lignes_c.montant_facture) > 0
						) 
				;
				
				
										
			]]></sqlcmd>
		</NODE>
		
		
	
		<NODE label="Compte client Clinique">
			<sqlcmd><![CDATA[
			
				DROP TABLE IF EXISTS w_factures_soldes_c;
				CREATE TEMP TABLE w_factures_soldes_c AS
				SELECT
					w_factures_lignes_c.no_facture AS no_facture,
					COALESCE(p_factures.oid,0) AS facture_id,
					date_trunc('month',p_factures.date_vente) AS date_comptable,
					rubrique_facturation_id AS rubrique_comptabilisee_id,
					prestation_id,
					'0'::text AS od_avoir,
					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,
					0::numeric AS montant_regle,
					0::numeric AS montant_regle_0,
					0::numeric AS montant_regle_1,
					0::numeric AS montant_regle_2,
					0::numeric AS montant_regle_22
				FROM temp.w_factures_lignes_c
					JOIN activite[PX].p_factures ON p_factures.no_facture = w_factures_lignes_c.no_facture
				WHERE  
					montant_comptabilise <> 0 AND
					p_factures.code_vente = '1'
				GROUP BY 1,2,3,4,5,6;

				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 (
					no_facture,
					facture_id,
					date_comptable,
					od_avoir,
					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,
					facture_id,
					date_comptable,
					od_avoir,
					rubrique_comptabilisee_id,
					prestation_id,
					SUM(montant_comptabilise),
					SUM(montant_comptabilise_0),
					SUM(montant_comptabilise_1),
					SUM(montant_comptabilise_2),
					SUM(montant_comptabilise_22),
					SUM(montant_regle),
					SUM(montant_regle_0),
					SUM(montant_regle_1),
					SUM(montant_regle_2),
					SUM(montant_regle_22)
				FROM w_factures_soldes_c
				GROUP BY 1,2,3,4,5,6;

				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');
										
										
			]]></sqlcmd>
		</NODE>
	
		<NODEx label="Lignes non facturées Honoraires">
			<sqlcmd><![CDATA[

				
										
										
			]]></sqlcmd>
		</NODEx>
		
		<NODE label="Lignes factures Honoraires">
			<sqlcmd><![CDATA[

				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_1');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_2');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_3');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_4');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_5');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_6');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_7');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_8');
				SELECT base.cti_disable_index('activite', 'i_factures_lignes_h_9');


				INSERT INTO activite[PX].p_factures_lignes_h (
					no_facture,
					facture_id,
					date_debut,
					heure_debut,
					date_fin,
					lieu_id,
					acte_id,
					phase_ccam,
					activite_ccam,
					extension_ccam,
					modificateur_ccam_1,
					modificateur_ccam_2,
					modificateur_ccam_3,
					modificateur_ccam_4,
					medecin_facture_id,
					medecin_comptabilise_id,
					prestation_id,
					nb_rubrique,
					nb_prestation,
					coefficient,
					coefficient_mco,
					prix_unitaire,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					montant_facture,
					montant_facture_0,
					montant_facture_1,
					montant_facture_2,
					montant_facture_22,
					montant_depassement,
					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,
					facture_id,
					date_debut,
					0 AS heure_debut,
					date_fin,
					lieu_id,
					acte_id,
					phase_ccam,
					activite_ccam,
					extension_ccam,
					modificateur_ccam_1,
					modificateur_ccam_2,
					modificateur_ccam_3,
					modificateur_ccam_4,
					medecin_facture_id,
					medecin_comptabilise_id,
					prestation_id,
					nb_rubrique,
					nb_prestation,
					coefficient,
					coefficient_mco,
					prix_unitaire,
					taux_0,
					taux_1,
					taux_2,
					taux_22,
					montant_facture,
					montant_facture_0,
					montant_facture_1,
					montant_facture_2,
					montant_facture_22,
					0 AS montant_depassement,
					0 AS montant_comptabilise,
					0 AS montant_comptabilise_0,
					0 AS montant_comptabilise_1,
					0 AS montant_comptabilise_2,
					0 AS 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 temp.w_factures_lignes_c
				WHERE medecin_facture_id <> 0 OR montant_facture_actes_inclus_dans_sejour <> 0;

				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_1');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_2');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_3');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_4');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_5');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_6');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_7');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_8');
				SELECT base.cti_enable_index('activite', 'i_factures_lignes_h_9');

	
				UPDATE activite[PX].p_factures_lignes_h 
				SET lieu_id = p_mouvements_sejour.lieu_id
				FROM activite[PX].p_factures, activite[PX].p_mouvements_sejour
				WHERE p_factures_lignes_h.no_facture = p_factures.no_facture AND
					p_factures.no_sejour = p_mouvements_sejour.no_sejour AND
					activite[PX].p_factures_lignes_h.date_debut = p_mouvements_sejour.date AND 
					(p_mouvements_sejour.heure_fin = 240000 OR p_mouvements_sejour.nb_sorties_directes = 1) AND
					p_factures_lignes_h.lieu_id <> p_mouvements_sejour.lieu_id;
										
										
			]]></sqlcmd>
		</NODE>
	
		<NODEx label="Compte client Honoraires">
			<sqlcmd><![CDATA[

				
										
			]]></sqlcmd>
		</NODEx>
		
		<NODEx label="Validation">
			<sqlcmd><![CDATA[

				
										
			]]></sqlcmd>
		</NODEx>

	</NODE>
	
	<NODE name="CHIFFRIER" label="CREATION DU CHIFFRIER">
	
		<NODEx label="Récupération chiffrier occupation">
			<sqlcmd><![CDATA[
			
				TRUNCATE activite[PX].p_chiffrier_occupation;

				DROP TABLE IF EXISTS w_chiffrier;
				CREATE TEMP TABLE w_chiffrier AS
				SELECT
					date(DADM_DATEDEBUT) AS date,
					DADM_NDA AS no_sejour,
					1::numeric AS nb_entrees_directes,
					0::numeric AS nb_sorties_directes
				FROM prod_csis.SID_DOSSIERADMINISTRATIF_DADM
				WHERE DADM_ID_STDO NOT IN (1,2) AND
					DADM_ID_ETDO NOT IN (4,6) AND
					date(DADM_DATEDEBUT) >= '20090101';


				INSERT INTO w_chiffrier
				SELECT
					date(DADM_DATEFIN) AS date,
					DADM_NDA AS no_sejour,
					0::numeric AS nb_entrees_directes,
					1::numeric AS nb_sorties_directes
				FROM prod_csis.SID_DOSSIERADMINISTRATIF_DADM
				JOIN prod_csis.SNO_STATUTDOSSIER_STDO ON DADM_ID_STDO = STDO_ID AND STDO_NATUREENTREE = 'R'
				WHERE DADM_ID_STDO NOT IN (1,2) AND
					DADM_ID_ETDO NOT IN (4,6) AND
					DADM_DATEFIN IS NOT NULL AND
					date(DADM_DATEFIN) >= '20090101' AND
					STDO_CODE IN ('S', 'C') ;

				INSERT INTO activite[PX].p_chiffrier_occupation (
					date,
					no_sejour,
					nb_entrees_directes,
					nb_sorties_directes)
				SELECT 
					date,
					no_sejour,
					SUM(nb_entrees_directes),
					SUM(nb_sorties_directes)
				FROM w_chiffrier
				GROUP BY 1,2;

										
			]]></sqlcmd>
		</NODEx>
	
	
	
		<NODE label="Récupération chiffrier comptable">
			<sqlcmd><![CDATA[

				TRUNCATE activite[PX].p_factures_comptables;
				INSERT INTO activite[PX].p_factures_comptables(
					date_comptable,
					no_facture,
					montant_ventes_c, 
					montant_reglements_c, 
					montant_ventes_h, 
					montant_reglements_h
				)
				SELECT
					CASE
						WHEN date(CLI_DATE_OBJETFAC) < (CLI_DONN_EXERCOMPTA || '0101')::date THEN (CLI_DONN_EXERCOMPTA || '0101')::date
						WHEN date(CLI_DATE_OBJETFAC) < (CLI_DONN_EXERCOMPTA || '0101')::date THEN (CLI_DONN_EXERCOMPTA || '0101')::date
						ELSE date(CLI_DATE_OBJETFAC) END AS date_comptable,
					SUBSTR(CLI_DONN_EXERCOMPTA,3,2) || '_' ||
					CLI_CODE_OBJETFACTURATION || 
					CASE 
						WHEN CLI_DONN_NOOBJETFAC = '0' THEN CLI_DONN_NOEXTEPISODE || '.' || FD_FAC_DETFAC.CLI_CODE_NOINTERNEOBJET
						WHEN CLI_CODE_OBJETFACTURATION = 'I' THEN CLI_DONN_NOEXTEPISODE || '.' || FD_FAC_DETFAC.CLI_DONN_NOOBJETFAC
						ELSE FD_FAC_DETFAC.CLI_DONN_NOOBJETFAC::text 
					END::text AS no_facture,
					SUM(CASE WHEN CLI_FLAG_SENS = 1 THEN FD_FAC_DETFAC.CLI_DONN_MONTANT ELSE 0 - FD_FAC_DETFAC.CLI_DONN_MONTANT END) AS montant_ventes_c,
					0::numeric AS montant_reglements_c, 
					0::numeric AS montant_ventes_h, 
					0::numeric AS montant_reglements_h
				FROM prod_clinicom.FD_FAC_DETFAC JOIN w_ADMISSIONS_select ON FD_FAC_DETFAC.cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT   
					JOIN prod_clinicom.FD_MVTS_EPISODES ON FD_FAC_DETFAC.CLI_DONN_NOABSEPISODE = FD_MVTS_EPISODES.CLI_DONN_NOABSEPISODE
				WHERE CLI_CODE_OBJETFACTURATION <> 'P' AND CLI_CODE_OBJETFACTURATION <> 'I' AND CLI_DONN_EXERCOMPTA >= [ENV_ADM_ANNEEDEBUT]
				GROUP BY 1,2;


				TRUNCATE activite[PX].p_chiffrier_comptable;
				INSERT INTO activite[PX].p_chiffrier_comptable(
					date_comptable,
					montant_ventes_c, 
					montant_reglements_c, 
					montant_ventes_h, 
					montant_reglements_h
				)
				SELECT
					CASE
						WHEN date(CLI_DATE_OBJETFAC) < (CLI_DONN_EXERCOMPTA || '0101')::date THEN (CLI_DONN_EXERCOMPTA || '0101')::date
						WHEN date(CLI_DATE_OBJETFAC) < (CLI_DONN_EXERCOMPTA || '0101')::date THEN (CLI_DONN_EXERCOMPTA || '0101')::date
						ELSE date(CLI_DATE_OBJETFAC) END AS date_comptable,
					SUM(CASE WHEN CLI_FLAG_SENS = 1 THEN FD_FAC_DETFAC.CLI_DONN_MONTANT ELSE 0 - FD_FAC_DETFAC.CLI_DONN_MONTANT END) AS montant_ventes_c,
					0::numeric AS montant_reglements_c, 
					0::numeric AS montant_ventes_h, 
					0::numeric AS montant_reglements_h
				FROM prod_clinicom.FD_FAC_DETFAC JOIN w_ADMISSIONS_select ON FD_FAC_DETFAC.cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT  
				WHERE CLI_CODE_OBJETFACTURATION <> 'P' AND CLI_CODE_OBJETFACTURATION <> 'I' AND CLI_DONN_NOABSEPISODE <> '' AND CLI_DONN_EXERCOMPTA >= [ENV_ADM_ANNEEDEBUT]
				GROUP BY 1;

										
			]]></sqlcmd>
		</NODE>
		
	</NODE>
	
	<NODE name="PARAM" label="RECUPERATION DES PARAMETRES">
	
		<NODE label="Sélections selon établissement">
			<sqlcmd><![CDATA[
				CREATE TEMP TABLE w_FS_MVTS_UF AS
				SELECT FS_MVTS_UF.*
				FROM prod_clinicom.FS_MVTS_UF
				JOIN prod_clinicom.FS_MVTS_SERVICES ON FS_MVTS_UF.CLI_CODE_SERVICE = FS_MVTS_SERVICES.CLI_CODE_SERVICE
				JOIN prod_clinicom.FS_MVTS_DEPARTEMENTS ON FS_MVTS_SERVICES.CLI_CODE_DEPARTEMENT = FS_MVTS_DEPARTEMENTS.CLI_CODE_DEPARTEMENT
				WHERE CLI_CODE_ETABLISSEMENT = '[ID_ETAB]' OR 
					'[ID_ETAB]' = ''
				;

				CREATE INDEX w_FS_MVTS_UF_i1 ON w_FS_MVTS_UF USING btree (CLI_CODE_UF);	
				
				CREATE TEMP TABLE w_FS_MVTS_US AS
				SELECT FS_MVTS_US.*
				FROM prod_clinicom.FS_MVTS_US
				WHERE CLI_CODE_ETABLISSEMENT = '[ID_ETAB]' OR 
					'[ID_ETAB]' = ''
				;

				CREATE INDEX w_FS_MVTS_US_i1 ON w_FS_MVTS_US USING btree (CLI_CODE_US);	


				DROP TABLE IF EXISTS w_ADMISSIONS_select;
				CREATE TEMP TABLE w_ADMISSIONS_select AS
				SELECT CLI_Donn_NoAbsEpisode AS CLI_Donn_NoAbsEpisode_SELECT
				FROM prod_clinicom.FD_MVTS_ADMISSIONS
				JOIN w_FS_MVTS_UF ON w_FS_MVTS_UF.CLI_CODE_UF = FD_MVTS_ADMISSIONS.CLI_CODE_UF
				;
				
				CREATE INDEX w_ADMISSIONS_select_i1 ON w_ADMISSIONS_select USING btree (CLI_Donn_NoAbsEpisode_SELECT)
				;
			]]></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', '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 'PATIENTS', '0', '0', 'Patients', 'Patients'
				WHERE '0' NOT IN (SELECT code_original FROM activite[PX].t_tiers_payant);
				
				INSERT INTO activite[PX].t_tiers_payant(code, code_original, type_tiers_payant, texte, texte_court) 
				SELECT 'AMO', 'CTI_AMO', '1', 'Régime général', 'Régime général'
				WHERE 'CTI_AMO' 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, code_original, type_tiers_payant, texte, texte_court) 
				SELECT 'AMC', 'CTI_AMC', '2', 'Mutuelle', 'Mutuelle'
				WHERE 'CTI_AMC' NOT IN (SELECT code_original FROM activite[PX].t_tiers_payant WHERE code_original IS NOT NULL);

				
				DROP TABLE IF EXISTS w_FB_FAC_DEBITEURS;
				CREATE TEMP TABLE w_FB_FAC_DEBITEURS AS
				SELECT FB_FAC_DEBITEURS.CLI_CODE_ORGANISME, 
					FB_FAC_DEBITEURS.CLI_DONN_DESIGNATIONDEBITEUR, 
					FB_FAC_DEBITEURS.CLI_CODE_ORGANISME || CASE WHEN COUNT(DISTINCT FD_FAC_DEBITEURS.CLI_CODE_TYPEDEBITEUR) > 2 THEN '_2' ELSE '' END AS CLI_CODE_ORGANISME_2,
					MAX(CASE WHEN FB_FAC_DEBITEURS.CLI_CODE_BUDGETGLOBAL = 1 THEN 1 ELSE 0 END) AS est_budget_global,
					MIN(CASE WHEN FD_FAC_DEBITEURS.CLI_CODE_TYPEDEBITEUR IN (1) THEN '1' ELSE NULL END) AS type_tiers_payant_1,
					MAX(CASE WHEN FD_FAC_DEBITEURS.CLI_CODE_TYPEDEBITEUR IN (1) THEN NULL ELSE '2' END) AS type_tiers_payant_2,
					0::bigint AS tiers_payant_1_id,
					0::bigint AS tiers_payant_2_id
				FROM prod_clinicom.FB_FAC_DEBITEURS
					JOIN prod_clinicom.FD_FAC_DEBITEURS ON FB_FAC_DEBITEURS.CLI_CODE_ORGANISME = FD_FAC_DEBITEURS.CLI_CODE_ORGANISME
					JOIN w_ADMISSIONS_select ON FD_FAC_DEBITEURS.cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT 
				GROUP BY 1,2
				ORDER BY 1;
				
				INSERT INTO w_FB_FAC_DEBITEURS
				SELECT FD_FAC_DEBITEURS.CLI_CODE_ORGANISME, 
					FD_FAC_DEBITEURS.CLI_CODE_ORGANISME, 
					FD_FAC_DEBITEURS.CLI_CODE_ORGANISME || CASE WHEN COUNT(DISTINCT FD_FAC_DEBITEURS.CLI_CODE_TYPEDEBITEUR) > 2 THEN '_2' ELSE '' END AS CLI_CODE_ORGANISME_2,
					0 AS est_budget_global,
					MIN(CASE WHEN FD_FAC_DEBITEURS.CLI_CODE_TYPEDEBITEUR IN (1) THEN '1' ELSE NULL END) AS type_tiers_payant_1,
					MAX(CASE WHEN FD_FAC_DEBITEURS.CLI_CODE_TYPEDEBITEUR IN (1) THEN NULL ELSE '2' END) AS type_tiers_payant_2,
					0::bigint AS tiers_payant_1_id,
					0::bigint AS tiers_payant_2_id
				FROM prod_clinicom.FD_FAC_DEBITEURS
					JOIN w_ADMISSIONS_select ON FD_FAC_DEBITEURS.cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT 
					LEFT JOIN prod_clinicom.FB_FAC_DEBITEURS ON FB_FAC_DEBITEURS.CLI_CODE_ORGANISME = FD_FAC_DEBITEURS.CLI_CODE_ORGANISME
				WHERE FD_FAC_DEBITEURS.CLI_CODE_ORGANISME <> '' AND FB_FAC_DEBITEURS.CLI_CODE_ORGANISME IS NULL
				GROUP BY 1,2
				ORDER BY 1;
				
				INSERT INTO w_FB_FAC_DEBITEURS
				SELECT FD_FAC_DETFAC.CLI_CODE_ORGANISME, 
					FD_FAC_DETFAC.CLI_CODE_ORGANISME, 
					FD_FAC_DETFAC.CLI_CODE_ORGANISME || CASE WHEN COUNT(DISTINCT FD_FAC_DETFAC.CLI_CODE_TYPEDEBITEUR) > 2 THEN '_2' ELSE '' END AS CLI_CODE_ORGANISME_2,
					0 AS est_budget_global,
					MIN(CASE WHEN FD_FAC_DETFAC.CLI_CODE_TYPEDEBITEUR IN (1) THEN '1' ELSE NULL END) AS type_tiers_payant_1,
					MAX(CASE WHEN FD_FAC_DETFAC.CLI_CODE_TYPEDEBITEUR IN (1) THEN NULL ELSE '2' END) AS type_tiers_payant_2,
					0::bigint AS tiers_payant_1_id,
					0::bigint AS tiers_payant_2_id
				FROM prod_clinicom.FD_FAC_DETFAC
					JOIN w_ADMISSIONS_select ON FD_FAC_DETFAC.cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT 
					LEFT JOIN w_FB_FAC_DEBITEURS ON FD_FAC_DETFAC.CLI_CODE_ORGANISME = w_FB_FAC_DEBITEURS.CLI_CODE_ORGANISME
				WHERE FD_FAC_DETFAC.CLI_CODE_ORGANISME <> '' AND w_FB_FAC_DEBITEURS.CLI_CODE_ORGANISME IS NULL
				GROUP BY 1,2
				ORDER BY 1;
				
				ALTER TABLE w_FB_FAC_DEBITEURS ADD CONSTRAINT w_FB_FAC_DEBITEURS_pkey PRIMARY KEY(CLI_CODE_ORGANISME);

				INSERT INTO activite[PX].t_tiers_payant(code, code_original, texte, texte_court, type_tiers_payant) 
				SELECT CLI_CODE_ORGANISME, 
					CLI_CODE_ORGANISME, 
					CLI_DONN_DESIGNATIONDEBITEUR, 
					CLI_DONN_DESIGNATIONDEBITEUR, 
					type_tiers_payant_1
				FROM w_FB_FAC_DEBITEURS
				WHERE type_tiers_payant_1 IS NOT NULL AND
					CLI_CODE_ORGANISME NOT IN (SELECT code_original FROM activite[PX].t_tiers_payant)
				ORDER BY 1;

				INSERT INTO activite[PX].t_tiers_payant(code, code_original, texte, texte_court, type_tiers_payant) 
				SELECT CLI_CODE_ORGANISME_2, 
					CLI_CODE_ORGANISME_2, 
					CLI_DONN_DESIGNATIONDEBITEUR, 
					CLI_DONN_DESIGNATIONDEBITEUR, 
					type_tiers_payant_2
				FROM w_FB_FAC_DEBITEURS
				WHERE type_tiers_payant_2 IS NOT NULL AND
					CLI_CODE_ORGANISME_2 NOT IN (SELECT code_original FROM activite[PX].t_tiers_payant)
				ORDER BY 1;

				UPDATE w_FB_FAC_DEBITEURS 
				SET tiers_payant_1_id = t_tiers_payant.oid
				FROM activite[PX].t_tiers_payant 
				WHERE type_tiers_payant_1 IS NOT NULL AND
					CLI_CODE_ORGANISME = code_original;

				UPDATE w_FB_FAC_DEBITEURS 
				SET tiers_payant_2_id = t_tiers_payant.oid
				FROM activite[PX].t_tiers_payant 
				WHERE type_tiers_payant_2 IS NOT NULL AND
					CLI_CODE_ORGANISME_2 = code_original;
					
				UPDATE w_FB_FAC_DEBITEURS 
				SET tiers_payant_1_id = t_tiers_payant.oid
				FROM activite[PX].t_tiers_payant 
				WHERE type_tiers_payant_2 IS NOT NULL AND
					tiers_payant_1_id = 0 AND
					CLI_CODE_ORGANISME_2 = code_original;
					
				
										
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Médecins">
			<sqlcmd><![CDATA[
			
				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 base.t_specialites_medecin(code_original, code, texte, texte_court)
				SELECT CLI_CODE_SPECIALITE, 
					CLI_CODE_SPECIALITE, 
					CLI_DONN_DESCRIPTION, 
					CLI_DONN_DESCRIPTION
				FROM prod_clinicom.FB_MVTS_SPECIALITE
                    LEFT JOIN base.t_specialites_medecin ON CLI_CODE_SPECIALITE = t_specialites_medecin.code_original
				WHERE t_specialites_medecin.code_original IS NULL;
				
				


				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_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 CLI_CODE_MEDECIN, 
					base.cti_soundex_nom(CLI_DONN_NOM) AS CLI_DONN_NOM_SOUNDEX, 
					base.cti_soundex_nom(COALESCE(CLI_DONN_PRENOM,'')) AS CLI_DONN_PRENOM_SOUNDEX, 
					CLI_DONN_NOM, 
					COALESCE(CLI_DONN_PRENOM,'') AS CLI_DONN_PRENOM, 
					CASE WHEN COALESCE(CLI_CODE_ADELI,0) IS DISTINCT FROM 0 THEN to_char(CLI_CODE_ADELI,'FM000000000') ELSE '' END AS CLI_CODE_ADELI, 
					CLI_CODE_SPECIALITE, 
					COALESCE(t_medecins_administratifs.medecin_id,0) as medecin_id
				FROM prod_clinicom.FB_MVTS_MEDECETB
					LEFT JOIN activite[PX].t_medecins_administratifs ON CLI_CODE_MEDECIN = t_medecins_administratifs.code_original
				WHERE 
					CLI_CODE_MEDECIN IN (SELECT CLI_CODE_MEDECIN FROM prod_clinicom.FD_MVTS_ADMISSIONS JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT ) OR
					CLI_CODE_MEDECIN IN (SELECT CLI_CODE_MEDECIN FROM prod_clinicom.FD_MVTS_MOUVEMENTS JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT ) OR
					CLI_CODE_MEDECIN IN (SELECT CLI_CODE_MEDECIN FROM prod_clinicom.FD_MVTS_MUTATIONS JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT ) OR
					CLI_CODE_MEDECIN IN (SELECT CLI_CODE_MEDECIN FROM prod_clinicom.FD_ACTES_ENTETE JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT ) OR
					CLI_CODE_MEDECIN IN (SELECT CLI_CODE_MEDECIN1 FROM prod_clinicom.FD_ACTES_ENTETE JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT ) OR
					CLI_CODE_MEDECIN IN (SELECT CLI_CODE_MEDECINREALISATEUR FROM prod_clinicom.FD_ACTES_CCAM JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT )
				ORDER BY 2,3;


				INSERT INTO base.t_medecins(nom, prenom, numero_ordre, specialite_id)
				SELECT w_medecins.CLI_DONN_NOM, w_medecins.CLI_DONN_PRENOM, w_medecins.CLI_CODE_ADELI, 0 FROM
				(SELECT SUBSTR(CLI_DONN_NOM_SOUNDEX, 1, 8) AS CLI_DONN_NOM_SOUNDEX, SUBSTR(CLI_DONN_PRENOM_SOUNDEX, 1, 8) AS CLI_DONN_PRENOM_SOUNDEX, CLI_CODE_ADELI, MIN(CLI_CODE_MEDECIN) AS CLI_CODE_MEDECIN
				FROM w_medecins
				GROUP BY 1,2,3) subview, w_medecins
				WHERE w_medecins.CLI_CODE_MEDECIN = subview.CLI_CODE_MEDECIN
					AND subview.CLI_DONN_NOM_SOUNDEX || ',' || subview.CLI_DONN_PRENOM_SOUNDEX || ',' ||  subview.CLI_CODE_ADELI NOT IN 
					(SELECT substr(base.cti_soundex_nom(nom),1,8)  || ',' || substr(base.cti_soundex_nom(prenom),1,8) || ',' || numero_ordre FROM base.t_medecins);


				INSERT INTO base.t_medecins(nom, prenom, numero_ordre, specialite_id)
				SELECT w_medecins.CLI_DONN_NOM, w_medecins.CLI_DONN_PRENOM, w_medecins.CLI_CODE_ADELI, 0 FROM
				(SELECT CLI_DONN_NOM_SOUNDEX, CLI_DONN_PRENOM_SOUNDEX, MIN(CLI_CODE_MEDECIN) AS CLI_CODE_MEDECIN
				FROM w_medecins
				GROUP BY 1,2) subview, w_medecins
				WHERE w_medecins.CLI_CODE_MEDECIN = subview.CLI_CODE_MEDECIN
					AND subview.CLI_DONN_NOM_SOUNDEX || ',' || subview.CLI_DONN_PRENOM_SOUNDEX NOT IN 
					(SELECT base.cti_soundex_nom(nom)  || ',' || base.cti_soundex_nom(prenom) FROM base.t_medecins);


				UPDATE w_medecins SET medecin_id = subview.medecin_id
				FROM 
				(SELECT substr(base.cti_soundex_nom(nom),1,8) as nom , substr(base.cti_soundex_nom(prenom),1,8) as prenom, numero_ordre, MIN(oid) AS medecin_id FROM base.t_medecins GROUP BY 1,2,3) subview
				WHERE SUBSTR(CLI_DONN_NOM_SOUNDEX, 1, 8) = subview.nom
				AND SUBSTR(CLI_DONN_PRENOM_SOUNDEX, 1, 8) = subview.prenom
				AND CLI_CODE_ADELI = subview.numero_ordre
				AND w_medecins.medecin_id = 0;

				UPDATE w_medecins SET medecin_id = subview.medecin_id
				FROM 
				(SELECT base.cti_soundex_nom(nom) as nom , base.cti_soundex_nom(prenom) as prenom, MIN(oid) AS medecin_id FROM base.t_medecins GROUP BY 1,2) subview
				WHERE SUBSTR(CLI_DONN_NOM_SOUNDEX, 1, 8) = subview.nom
				AND SUBSTR(CLI_DONN_PRENOM_SOUNDEX, 1, 8) = subview.prenom
				AND w_medecins.medecin_id = 0;

				INSERT INTO activite[PX].t_medecins_administratifs(code_original, code, nom, prenom, specialite_id,  medecin_id)
				SELECT DISTINCT CLI_CODE_MEDECIN, CLI_CODE_MEDECIN, CLI_DONN_NOM, CLI_DONN_PRENOM, COALESCE(t_specialites_medecin.oid,0), medecin_id
				FROM w_medecins
					LEFT JOIN base.t_specialites_medecin ON (CLI_CODE_SPECIALITE = t_specialites_medecin.code_original)
				WHERE CLI_CODE_MEDECIN NOT IN (SELECT code_original FROM activite[PX].t_medecins_administratifs);

				UPDATE activite[PX].t_medecins_administratifs
				SET nom = CLI_DONN_NOM,
					prenom = CLI_DONN_PRENOM,
					specialite_id = COALESCE(t_specialites_medecin.oid,0),
					medecin_id = w_medecins.medecin_id,
					no_adeli = CLI_CODE_ADELI
				FROM w_medecins LEFT JOIN base.t_specialites_medecin ON (CLI_CODE_SPECIALITE = t_specialites_medecin.code_original)
				WHERE w_medecins.CLI_CODE_MEDECIN = t_medecins_administratifs.code_original
					AND (
					t_medecins_administratifs.nom IS DISTINCT FROM CLI_DONN_NOM OR
					t_medecins_administratifs.prenom IS DISTINCT FROM CLI_DONN_PRENOM OR
					t_medecins_administratifs.specialite_id IS DISTINCT FROM COALESCE(t_specialites_medecin.oid,0) OR
					t_medecins_administratifs.medecin_id IS DISTINCT FROM w_medecins.medecin_id OR
					t_medecins_administratifs.no_adeli <> w_medecins.CLI_CODE_ADELI
				);

				UPDATE base.t_medecins SET specialite_id = subview.specialite_id FROM 
				(SELECT t_medecins.oid, MIN(t_medecins_administratifs.specialite_id) as specialite_id 
				FROM base.t_medecins, activite[PX].t_medecins_administratifs , base.t_specialites_medecin 
				WHERE t_medecins.oid = t_medecins_administratifs.medecin_id 
				AND 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;					
										
			]]></sqlcmd>
		</NODE>
		
		<NODE label="Hébergement">
			<sqlcmd><![CDATA[
			
				-- Initialisations UF / Services
				DROP TABLE IF EXISTS w_MVTS_UF;
				CREATE TEMP TABLE w_MVTS_UF AS
				SELECT 
					FS_MVTS_UF.CLI_CODE_UF, 
					FS_MVTS_UF.CLI_DONN_DESCRIPTION,
					CLI_CODE_ACTIVITE,
					CLI_CODE_DMT,
					FS_MVTS_UF.CLI_CODE_UM,
					FS_MVTS_UF.CLI_CODE_SERVICE AS CLI_CODE_SERVICE,
					FS_MVTS_SERVICES.CLI_DONN_DESCRIPTION AS CLI_DONN_DESCRIPTION_SERVICE,
					COALESCE(t_modes_traitement.oid,0) AS mode_traitement_id,
					COALESCE(t_dmt.oid,0) AS dmt_id,
					0::integer AS service_distinct_activite,
					0::integer AS service_distinct_dmt
				FROM w_FS_MVTS_UF FS_MVTS_UF
					JOIN prod_clinicom.FS_MVTS_SERVICES ON FS_MVTS_SERVICES.CLI_CODE_SERVICE = FS_MVTS_UF.CLI_CODE_SERVICE
					LEFT JOIN base.t_modes_traitement ON CLI_CODE_ACTIVITE  = t_modes_traitement.code
					LEFT JOIN base.t_dmt ON CLI_CODE_DMT  = t_dmt.code
				WHERE 
					(
					CLI_CODE_UF IN (SELECT CLI_CODE_UF from prod_clinicom.FD_MVTS_MOUVEMENTS JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT) OR
					CLI_CODE_UF IN (SELECT CLI_CODE_UF from prod_clinicom.FD_MVTS_MUTATIONS JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT) OR
					CLI_CODE_UF IN (SELECT CLI_CODE_UF from prod_clinicom.FD_MVTS_ADMISSIONS JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT) OR
					CLI_CODE_UF IN (SELECT CLI_CODE_UF from prod_clinicom.FD_MVTS_SORTIES JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT) 
					)
				ORDER BY 1;


				UPDATE w_MVTS_UF SET 
					CLI_CODE_ACTIVITE = CODE_default,
					mode_traitement_id = oid_default
				FROM w_MVTS_UF w_MVTS_UF_2
					JOIN 	(
						SELECT
							CLI_CODE_SERVICE,
							(MAX(array[to_char(CODE_count,'FM00000'),CODE]))[2] AS CODE_default,
							(MAX(array[CODE_count,oid]))[2] AS oid_default
						FROM (
							SELECT 
								CLI_CODE_SERVICE, 
								CLI_CODE_ACTIVITE AS CODE,
								mode_traitement_id AS oid,
								COUNT(*) AS CODE_count
							FROM w_MVTS_UF
							WHERE CLI_CODE_ACTIVITE <> ''
							GROUP BY 1,2,3) subview
						GROUP BY 1
						) subview ON w_MVTS_UF_2.CLI_CODE_SERVICE = subview.CLI_CODE_SERVICE
				WHERE w_MVTS_UF.CLI_CODE_SERVICE = w_MVTS_UF_2.CLI_CODE_SERVICE AND w_MVTS_UF.CLI_CODE_ACTIVITE = '';


				UPDATE w_MVTS_UF SET 
					CLI_CODE_DMT = CODE_default,
					dmt_id = oid_default
				FROM w_MVTS_UF w_MVTS_UF_2
					JOIN 	(
						SELECT
							CLI_CODE_SERVICE,
							(MAX(array[to_char(CODE_count,'FM00000'),CODE]))[2] AS CODE_default,
							(MAX(array[CODE_count,oid]))[2] AS oid_default
						FROM (
							SELECT 
								CLI_CODE_SERVICE, 
								CLI_CODE_DMT AS CODE,
								dmt_id AS oid,
								COUNT(*) AS CODE_count
							FROM w_MVTS_UF
							WHERE CLI_CODE_DMT <> ''
							GROUP BY 1,2,3) subview
						GROUP BY 1
						) subview ON w_MVTS_UF_2.CLI_CODE_SERVICE = subview.CLI_CODE_SERVICE
				WHERE w_MVTS_UF.CLI_CODE_SERVICE = w_MVTS_UF_2.CLI_CODE_SERVICE AND w_MVTS_UF.CLI_CODE_DMT = '';


				ALTER TABLE w_MVTS_UF ADD CONSTRAINT w_MVTS_UF_pkey PRIMARY KEY(CLI_CODE_UF);

				
				-- Modes de traitement

				
				INSERT INTO base.t_modes_traitement(code, texte, texte_court)
				SELECT CLI_CODE_ACTIVITE, CLI_DONN_DESCRIPTION, CLI_DONN_DESCRIPTION
				FROM prod_clinicom.FS_MVTS_ACTIVITE
				WHERE CLI_CODE_ACTIVITE NOT IN (SELECT code FROM base.t_modes_traitement) AND
					CLI_CODE_ACTIVITE IN (SELECt CLI_CODE_ACTIVITE FROM w_MVTS_UF)
				ORDER BY CLI_CODE_ACTIVITE;
				
				-- DMT
				
				INSERT INTO base.t_modes_traitement(code, texte, texte_court)
				SELECT CLI_CODE_DMT, CLI_DONN_DESCRIPTION, CLI_DONN_DESCRIPTION
				FROM prod_clinicom.FS_MVTS_DMT
				WHERE CLI_CODE_DMT NOT IN (SELECT code FROM base.t_dmt) AND
					CLI_CODE_DMT IN (SELECt CLI_CODE_DMT FROM w_MVTS_UF)
				ORDER BY CLI_CODE_DMT;
				
				-- 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, type_t2a)
				SELECT 
					CLI_CODE_UF, 
					CLI_CODE_UF, 
					CLI_DONN_DESCRIPTION,
					CLI_DONN_DESCRIPTION,
					'0'
				FROM w_MVTS_UF
				WHERE CLI_CODE_UF NOT IN (SELECT code_original FROM activite[PX].t_services_facturation)
				GROUP BY 1,2,3,4
				ORDER BY 1;


				
				UPDATE activite[PX].t_services_facturation
				SET 
					mode_traitement_id = CASE WHEN t_services_facturation.mode_traitement_id = 0 THEN COALESCE(t_modes_traitement.oid,0) ELSE t_services_facturation.mode_traitement_id END,
					dmt_id = CASE WHEN t_services_facturation.dmt_id = 0 THEN COALESCE(t_dmt.oid,0) ELSE t_services_facturation.dmt_id END
				FROM w_MVTS_UF
					LEFT JOIN base.t_modes_traitement ON CLI_CODE_ACTIVITE  = t_modes_traitement.code
					LEFT JOIN base.t_dmt ON CLI_CODE_DMT  = t_dmt.code
				WHERE t_services_facturation.code_original = CLI_CODE_UF AND (t_services_facturation.mode_traitement_id = 0 OR t_services_facturation.dmt_id = 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 
					CLI_CODE_UF, 
					CLI_CODE_UF, 
					CLI_DONN_DESCRIPTION,
					CLI_DONN_DESCRIPTION
				FROM w_MVTS_UF
				WHERE CLI_CODE_UF NOT IN (SELECT code_original FROM activite[PX].t_unites_fonctionnelles)
				ORDER BY 1;
				
					
				-- 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);

				INSERT INTO activite[PX].t_unites_medicales(code_original, code, texte, texte_court)
				SELECT 
					CLI_CODE_UM, 
					CLI_CODE_UM, 
					CLI_DONN_DESCRIPTION,
					CLI_DONN_DESCRIPTION
				FROM 
					prod_clinicom.FS_MVTS_UM
				WHERE CLI_CODE_UM NOT IN (SELECT code_original FROM activite[PX].t_unites_medicales) AND
					CLI_CODE_UM IN (SELECT CLI_CODE_UM FROM w_MVTS_UF) 
				ORDER BY 1;



				-- 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 
					CLI_CODE_SERVICE, 
					CLI_CODE_SERVICE, 
					CLI_DONN_DESCRIPTION_SERVICE,
					CLI_DONN_DESCRIPTION_SERVICE
				FROM w_MVTS_UF
				WHERE CLI_CODE_SERVICE NOT IN (SELECT code_original FROM activite[PX].t_activites)
				GROUP BY 1,2,3,4
				ORDER BY 1;


				-- 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_etages(code_original, code, texte, texte_court)
				SELECT 
					CLI_CODE_US, 
					CLI_CODE_US, 
					CLI_DONN_DESCRIPTION,
					CLI_DONN_DESCRIPTION
				FROM 
					prod_clinicom.FS_MVTS_US
				WHERE CLI_CODE_US <> '' AND 
					CLI_CODE_US IN (SELECT CLI_CODE_US FROM w_FS_MVTS_US) AND
					CLI_CODE_US NOT IN (SELECT code_original FROM activite[PX].t_etages)
				ORDER BY 1;
				
				INSERT INTO activite[PX].t_etages(code_original, code, texte, texte_court)
				SELECT 
					CLI_CODE_US, 
					CLI_CODE_US, 
					CLI_CODE_US,
					CLI_CODE_US
				FROM 
					prod_clinicom.FD_MVTS_MOUVEMENTS JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT
				WHERE CLI_CODE_US <> '' AND 
					CLI_CODE_US NOT IN (SELECT code_original FROM activite[PX].t_etages)
				GROUP BY 1
				ORDER BY 1;

				INSERT INTO activite[PX].t_lits(code_original, code, texte, texte_court, etage_id, chambre_particuliere)
				SELECT 
					CLI_CODE_US || '|' || CLI_DONN_CHAMBRE,
					CLI_DONN_CHAMBRE,
					'Chambre ' || CLI_DONN_CHAMBRE,
					'Chambre ' || CLI_DONN_CHAMBRE,
					t_etages.oid,
					'O'
				FROM 
					prod_clinicom.FS_MVTS_USCHAMBRES
					JOIN activite[PX].t_etages ON CLI_CODE_US = t_etages.code_original
				WHERE CLI_CODE_US IN (SELECT CLI_CODE_US FROM w_FS_MVTS_US) AND
					CLI_CODE_US || '|' || CLI_DONN_CHAMBRE NOT IN (SELECT code_original FROM activite[PX].t_lits WHERE code_original IS NOT NULL);
				

				INSERT INTO activite[PX].t_lits(code_original, code, texte, texte_court, etage_id, chambre_particuliere)
				SELECT 
					CLI_CODE_US || '|' || CLI_DONN_LIT,
					CLI_DONN_LIT,
					'Lit ' || CLI_DONN_LIT,
					'Lit ' || CLI_DONN_LIT,
					t_etages.oid,
					'N'
				FROM 
					prod_clinicom.FS_MVTS_USLITS
					JOIN activite[PX].t_etages ON CLI_CODE_US = t_etages.code_original
				WHERE CLI_CODE_US IN (SELECT CLI_CODE_US FROM w_FS_MVTS_US) AND
					CLI_CODE_US || '|' || CLI_DONN_LIT NOT IN (SELECT code_original FROM activite[PX].t_lits WHERE code_original IS NOT NULL);
				
				
				INSERT INTO activite[PX].t_lits(code_original, code, texte, texte_court, etage_id, chambre_particuliere)
				SELECT 
					CLI_CODE_US || '|' || CLI_DONN_CHAMBRE,
					CLI_DONN_CHAMBRE,
					'Chambre ' || CLI_DONN_CHAMBRE,
					'Chambre ' || CLI_DONN_CHAMBRE,
					t_etages.oid,
					'N'
				FROM 
					prod_clinicom.FD_MVTS_MOUVEMENTS JOIN w_ADMISSIONS_select ON cli_donn_noabsepisode = cli_donn_noabsepisode_SElECT
					JOIN activite[PX].t_etages ON CLI_CODE_US = t_etages.code_original
				WHERE CLI_DONN_CHAMBRE <> '' AND
					CLI_CODE_US || '|' || CLI_DONN_CHAMBRE NOT IN (SELECT code_original FROM activite[PX].t_lits WHERE code_original IS NOT NULL)
				GROUP BY 1,2,3,4,5;
				
				
										
				INSERT INTO activite[PX].t_lits(code_original, code, texte, texte_court, etage_id, chambre_particuliere)
				SELECT code_original || '|0' , '', 'Non renseigné', 'Non renseigné', oid, 'N' 
				FROM activite[PX].t_etages
				WHERE oid > 0 AND
					code_original || '|0' NOT IN (SELECT code_original FROM activite[PX].t_lits WHERE code_original IS NOT NULL);


				UPDATE activite[PX].t_lits SET code = '*****' WHERE oid = 0;
				
				UPDATE activite[PX].t_lits 
				SET code = t_etages.code || '***'
				FROM activite[PX].t_etages
				WHERE etage_id = t_etages.oid 
					AND t_lits.code = '' ;
	
			]]></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);

				
				INSERT INTO activite[PX].t_rubriques_facturation(code_original, code, texte, texte_court)
				SELECT 
					CLI_CODE_LETTRECLE,
					CLI_CODE_LETTRECLE,
					CLI_DONN_DESCRIPTION,
					CLI_DONN_DESCRIPTION
				FROM prod_clinicom.FB_FAC_LETTRECLE
				WHERE CLI_CODE_LETTRECLE NOT IN (SELECT code_original FROM activite[PX].t_rubriques_facturation);

				INSERT INTO activite[PX].t_rubriques_facturation(code_original, code, texte, texte_court)
				SELECT 
					CLI_DONN_DESCRIPTIONCOURTE, 
					CLI_DONN_DESCRIPTIONCOURTE, 
					CLI_DONN_DESCRIPTION,
					CLI_DONN_DESCRIPTION
				FROM 
					prod_clinicom.FB_FAC_PRESTATION
				WHERE CLI_DONN_DESCRIPTIONCOURTE NOT IN (SELECT code_original FROM activite[PX].t_rubriques_facturation)
				ORDER BY 1;
				
				
				INSERT INTO activite[PX].t_rubriques_facturation(code_original, code, texte, texte_court)
				SELECT 
					CLI_CODE_PRESTATION,
					CLI_CODE_PRESTATION,
					CLI_DONN_DESCRIPTION,
					CLI_DONN_DESCRIPTION
				FROM prod_clinicom.FB_FAC_FRAISDIVERS
				WHERE CLI_CODE_PRESTATION NOT IN (SELECT code_original FROM activite[PX].t_rubriques_facturation);
				
				INSERT INTO activite[PX].t_rubriques_facturation(code_original, code, texte, texte_court)
				SELECT code, code, texte, texte
				FROM base.t_ccam_regroupements 
				WHERE oid <> 0 AND code NOT IN (SELECT code_original FROM activite[PX].t_rubriques_facturation);
				
				INSERT INTO activite[PX].t_rubriques_facturation(code_original, code, texte, texte_court)
				SELECT 
					CLI_CODE_PRESTATIONEXTERNE, 
					CLI_CODE_PRESTATIONEXTERNE, 
					CLI_CODE_PRESTATIONEXTERNE,
					CLI_CODE_PRESTATIONEXTERNE
				FROM 
					prod_clinicom.FD_FAC_DETFACLIG
				WHERE CLI_CODE_PRESTATIONEXTERNE <> '' AND CLI_CODE_PRESTATIONEXTERNE NOT IN (SELECT code_original FROM activite[PX].t_rubriques_facturation)
				GROUP BY 1
				ORDER BY 1;
				
				INSERT INTO activite.t_prestations(oid, code_original, code, texte, texte_court) 
				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 
					CLI_CODE_LETTRECLE,
					CLI_CODE_LETTRECLE,
					CLI_DONN_DESCRIPTION,
					CLI_DONN_DESCRIPTION
				FROM prod_clinicom.FB_FAC_LETTRECLE
				WHERE CLI_CODE_LETTRECLE NOT IN (SELECT code_original FROM activite.t_prestations);

				INSERT INTO activite.t_prestations(code_original, code, texte, texte_court)
				SELECT 
					CLI_DONN_DESCRIPTIONCOURTE, 
					CLI_DONN_DESCRIPTIONCOURTE, 
					CLI_DONN_DESCRIPTION,
					CLI_DONN_DESCRIPTION
				FROM 
					prod_clinicom.FB_FAC_PRESTATION
				WHERE CLI_DONN_DESCRIPTIONCOURTE NOT IN (SELECT code_original FROM activite.t_prestations)
				ORDER BY 1;
				
				
				INSERT INTO activite.t_prestations(code_original, code, texte, texte_court)
				SELECT 
					CLI_CODE_PRESTATION,
					CLI_CODE_PRESTATION,
					CLI_DONN_DESCRIPTION,
					CLI_DONN_DESCRIPTION
				FROM prod_clinicom.FB_FAC_FRAISDIVERS
				WHERE CLI_CODE_PRESTATION NOT IN (SELECT code_original FROM activite.t_prestations);
				
				INSERT INTO activite.t_prestations(code_original, code, texte, texte_court)
				SELECT code, code, texte, texte
				FROM base.t_ccam_regroupements 
				WHERE oid <> 0 AND code NOT IN (SELECT code_original FROM activite.t_prestations);
				
				INSERT INTO activite.t_prestations(code_original, code, texte, texte_court)
				SELECT 
					CLI_CODE_PRESTATIONEXTERNE, 
					CLI_CODE_PRESTATIONEXTERNE, 
					CLI_CODE_PRESTATIONEXTERNE,
					CLI_CODE_PRESTATIONEXTERNE
				FROM 
					prod_clinicom.FD_FAC_DETFACLIG
				WHERE CLI_CODE_PRESTATIONEXTERNE <> '' AND CLI_CODE_PRESTATIONEXTERNE NOT IN (SELECT code_original FROM activite.t_prestations)
				GROUP BY 1
				ORDER BY 1;
				
				-- Comptes
				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);
				
				INSERT INTO activite[PX].t_compte(code_original, code, texte, texte_court)
				SELECT CLI_CODE_ORDONNATEUR, CLI_CODE_ORDONNATEUR, CLI_CODE_ORDONNATEUR, CLI_CODE_ORDONNATEUR
				FROM prod_clinicom.FD_FAC_DETFACLIG				
				WHERE CLI_CODE_ORDONNATEUR <> 0 AND CLI_CODE_ORDONNATEUR NOT IN (SELECT code FROM activite[PX].t_compte)
				GROUP BY 1
				ORDER BY 1
				;
				
				
										
			]]></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';
				
				UPDATE activite[PX].p_factures
				SET
					date_solde_0_c = CASE WHEN montant_comptabilise_0_c = 0 THEN '00010101' WHEN montant_comptabilise_0_c = montant_regle_0_c THEN date_solde_0_c ELSE '20991231' END,
					date_solde_0_h = CASE WHEN montant_comptabilise_0_h = 0 THEN '00010101' WHEN montant_comptabilise_0_h = montant_regle_0_h THEN date_solde_0_h ELSE '20991231' END,
					date_solde_1_c = CASE WHEN montant_comptabilise_1_c = 0 THEN '00010101' WHEN montant_comptabilise_1_c = montant_regle_1_c THEN date_solde_1_c ELSE '20991231' END,
					date_solde_1_h = CASE WHEN montant_comptabilise_1_h = 0 THEN '00010101' WHEN montant_comptabilise_1_h = montant_regle_1_h THEN date_solde_1_h ELSE '20991231' END,
					date_solde_2_c = CASE WHEN montant_comptabilise_2_c = 0 THEN '00010101' WHEN montant_comptabilise_2_c = montant_regle_2_c THEN date_solde_2_c ELSE '20991231' END,
					date_solde_2_h = CASE WHEN montant_comptabilise_2_h = 0 THEN '00010101' WHEN montant_comptabilise_2_h = montant_regle_2_h THEN date_solde_2_h ELSE '20991231' END,
					date_solde_22_c = CASE WHEN montant_comptabilise_22_c = 0 THEN '00010101' WHEN montant_comptabilise_22_c = montant_regle_22_c THEN date_solde_22_c ELSE '20991231' END,
					date_solde_22_h = CASE WHEN montant_comptabilise_22_h = 0 THEN '00010101' WHEN montant_comptabilise_22_h = montant_regle_22_h THEN date_solde_22_h ELSE '20991231' END
				WHERE
				(
					date_solde_0_c <> CASE WHEN montant_comptabilise_0_c = 0 THEN '00010101' WHEN montant_comptabilise_0_c = montant_regle_0_c THEN date_solde_0_c ELSE '20991231' END OR
					date_solde_0_h <> CASE WHEN montant_comptabilise_0_h = 0 THEN '00010101' WHEN montant_comptabilise_0_h = montant_regle_0_h THEN date_solde_0_h ELSE '20991231' END OR
					date_solde_1_c <> CASE WHEN montant_comptabilise_1_c = 0 THEN '00010101' WHEN montant_comptabilise_1_c = montant_regle_1_c THEN date_solde_1_c ELSE '20991231' END OR
					date_solde_1_h <> CASE WHEN montant_comptabilise_1_h = 0 THEN '00010101' WHEN montant_comptabilise_1_h = montant_regle_1_h THEN date_solde_1_h ELSE '20991231' END OR
					date_solde_2_c <> CASE WHEN montant_comptabilise_2_c = 0 THEN '00010101' WHEN montant_comptabilise_2_c = montant_regle_2_c THEN date_solde_2_c ELSE '20991231' END OR
					date_solde_2_h <> CASE WHEN montant_comptabilise_2_h = 0 THEN '00010101' WHEN montant_comptabilise_2_h = montant_regle_2_h THEN date_solde_2_h ELSE '20991231' END OR
					date_solde_22_c <> CASE WHEN montant_comptabilise_22_c = 0 THEN '00010101' WHEN montant_comptabilise_22_c = montant_regle_22_c THEN date_solde_22_c ELSE '20991231' END OR
					date_solde_22_h <> CASE WHEN montant_comptabilise_22_h = 0 THEN '00010101' WHEN montant_comptabilise_22_h = montant_regle_22_h THEN date_solde_22_h ELSE '20991231' END
				);
			
				DROP TABLE IF EXISTS w_factures_soldes;
				CREATE TEMP TABLE w_factures_soldes AS
				SELECT 
					no_facture,
					GREATEST(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) AS date_solde,
					GREATEST(date_solde_0_c, date_solde_1_c, date_solde_2_c, date_solde_22_c) AS date_solde_c,
					GREATEST(date_solde_0_h, date_solde_1_h, date_solde_2_h, date_solde_22_h) AS date_solde_h
				FROM activite[PX].p_factures;
				
				UPDATE activite[PX].p_factures
				SET 
					date_solde = w_factures_soldes.date_solde,
					date_solde_c = w_factures_soldes.date_solde_c,
					date_solde_h = w_factures_soldes.date_solde_h
				FROM w_factures_soldes
				WHERE p_factures.no_facture = w_factures_soldes.no_facture AND 
					(
					p_factures.date_solde IS NULL OR p_factures.date_solde <> w_factures_soldes.date_solde OR 
					p_factures.date_solde_c IS NULL OR p_factures.date_solde_c <> w_factures_soldes.date_solde_c OR 
					p_factures.date_solde_h IS NULL OR p_factures.date_solde_h <> w_factures_soldes.date_solde_h
					);
					
				DROP TABLE IF EXISTS w_factures_references_soldes;
				
				CREATE TEMP TABLE w_factures_references_soldes AS
				SELECT
						no_facture_reference,
						MAX(date_solde) AS date_solde_reference,
						MAX(date_solde_c) AS date_solde_reference_c,
						MAX(date_solde_h) AS date_solde_reference_h,
						MAX(date_solde_0_c) AS date_solde_reference_0_c,
						MAX(date_solde_0_h) AS date_solde_reference_0_h,
						MAX(date_solde_1_c) AS date_solde_reference_1_c,
						MAX(date_solde_1_h) AS date_solde_reference_1_h,
						MAX(date_solde_2_c) AS date_solde_reference_2_c,
						MAX(date_solde_2_h) AS date_solde_reference_2_h,
						MAX(date_solde_22_c) AS date_solde_reference_22_c,
						MAX(date_solde_22_h) AS date_solde_reference_22_h
					FROM activite[PX].p_factures
					GROUP BY no_facture_reference;
			
				CREATE INDEX i_factures_references_soldes_1
				ON w_factures_references_soldes
				USING btree
				(no_facture_reference);



				UPDATE activite[PX].p_factures
				SET 
					date_solde_reference = w_factures_references_soldes.date_solde_reference,
					date_solde_reference_c = w_factures_references_soldes.date_solde_reference_c,
					date_solde_reference_h = w_factures_references_soldes.date_solde_reference_h,
					date_solde_reference_0_c = w_factures_references_soldes.date_solde_reference_0_c,
					date_solde_reference_0_h = w_factures_references_soldes.date_solde_reference_0_h,
					date_solde_reference_1_c = w_factures_references_soldes.date_solde_reference_1_c,
					date_solde_reference_1_h = w_factures_references_soldes.date_solde_reference_1_h,
					date_solde_reference_2_c = w_factures_references_soldes.date_solde_reference_2_c,
					date_solde_reference_2_h = w_factures_references_soldes.date_solde_reference_2_h,
					date_solde_reference_22_c = w_factures_references_soldes.date_solde_reference_22_c,
					date_solde_reference_22_h = w_factures_references_soldes.date_solde_reference_22_h
				FROM w_factures_references_soldes
				WHERE p_factures.no_facture_reference = w_factures_references_soldes.no_facture_reference
				AND (
					p_factures.date_solde_reference IS NULL OR p_factures.date_solde_reference <> w_factures_references_soldes.date_solde_reference OR 
					p_factures.date_solde_reference_c IS NULL OR p_factures.date_solde_reference_c <> w_factures_references_soldes.date_solde_reference_c OR 
					p_factures.date_solde_reference_h IS NULL OR p_factures.date_solde_reference_h <> w_factures_references_soldes.date_solde_reference_h OR 
					p_factures.date_solde_reference_0_c IS NULL OR p_factures.date_solde_reference_0_c <> w_factures_references_soldes.date_solde_reference_0_c OR 
					p_factures.date_solde_reference_0_h IS NULL OR p_factures.date_solde_reference_0_h <> w_factures_references_soldes.date_solde_reference_0_h OR
					p_factures.date_solde_reference_1_c IS NULL OR p_factures.date_solde_reference_1_c <> w_factures_references_soldes.date_solde_reference_1_c OR 
					p_factures.date_solde_reference_1_h IS NULL OR p_factures.date_solde_reference_1_h <> w_factures_references_soldes.date_solde_reference_1_h OR
					p_factures.date_solde_reference_2_c IS NULL OR p_factures.date_solde_reference_2_c <> w_factures_references_soldes.date_solde_reference_2_c OR 
					p_factures.date_solde_reference_2_h IS NULL OR p_factures.date_solde_reference_2_h <> w_factures_references_soldes.date_solde_reference_2_h OR
					p_factures.date_solde_reference_22_c IS NULL OR p_factures.date_solde_reference_22_c <> w_factures_references_soldes.date_solde_reference_22_c OR 
					p_factures.date_solde_reference_22_h IS NULL OR p_factures.date_solde_reference_22_h <> w_factures_references_soldes.date_solde_reference_22_h
				);

	
				
			]]></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 activite[PX].p_sejours 
				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 label="Tables répliquées">
			<sqlcmd><![CDATA[

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

</ROOT>
