<?xml version="1.0" encoding="ISO-8859-15"?>
<VUE name="ACTI000144" 
	label="Activité. Liste des coordonnées patients" 
	title="= 'Liste des coordonnées patients (' + SELECT.PERIODE_LABEL + ' à ' + SELECT.TOPERIODE_LABEL + ')'" 
	database="iCTI" swf="*CTI_view1" 
	softCode="iCTI_activite"
	globals="ACTI_globals.XML"
	componentsFile="ACTI_components.XML">
	
	<VIEWPROPERTIES>
		
		<PROPERTY name="NO_SEJOUR_SELECT">
			<value>
				<![CDATA[
					[EVAL 
						if (('[NO_SEJOUR]') != '') {return " AND v_sejours_1.no_sejour = '[NO_SEJOUR]' "; }
						return '';
					EVAL]
				]]>
			</value>
		</PROPERTY>
		
		<PROPERTY name="NOM_SELECT">
			<value>
				<![CDATA[
					[EVAL 
						if (('[NOM]') != '') {return " AND p_patients.nom ILIKE '[NOM]%' "; }
						return '';
					EVAL]
				]]>
			</value>
		</PROPERTY>
		
		<PROPERTY name="DATE_SEJOUR_SELECT">
			<value>
				<![CDATA[
					[EVAL 
						if ('[QUELLEDATE]' == 'PR') {
							return " AND v_sejours_1.date_entree <= '[TOPERIODE]' AND v_sejours_1.date_sortie >='[PERIODE]' ";
						}
						if ('[QUELLEDATE]' == 'EN') {
							return " AND v_sejours_1.date_entree BETWEEN '[PERIODE]' AND '[TOPERIODE]' ";
						}
						if ('[QUELLEDATE]' == 'SO') {
							return " AND code_sorti = '1' AND v_sejours_1.date_sortie BETWEEN '[PERIODE]' AND '[TOPERIODE]' ";
						}
					EVAL]
				]]>
			</value>
		</PROPERTY>
	
		<!--PROPERTY dataLink="VIEWPROPERTIES" nameField="name" valueField="value"></PROPERTY>
		<PROPERTY name="CSVFILE_ID" value="[EVAL               $ctidir = substr(strtoupper(iCTI_dir()),5);             if (substr('[ENV.LOCALKEYS:TRIGRAMME]',0,1) != '[') {              if (trim('[ENV.LOCALKEYS:TRIGRAMME]') != '') {               return '[ENV.LOCALKEYS:TRIGRAMME]';              }             }             else{              return $ctidir;             }             EVAL]"></PROPERTY-->
	</VIEWPROPERTIES>
	<SELECTIONS label="Paramètres de l'analyse">
		<GROUP>
			<FIELD name="QUELLEDATE" label="" UI="combo" default="PR">
				<OPTION label="Date présence" data="PR" />
				<OPTION label="Date entrée" data="EN" />
				<OPTION label="Date sortie" data="SO" />
			</FIELD>
			<FIELD name="PERIODE" label="Période E/S " UI="combo" tree="true" width="200" default="2007-01-01" globalListProvider="DATE_DEBUT">
			</FIELD>
			<FIELD name="TOPERIODE" label="à" UI="combo" tree="true" width="200" default="2007-01-31" globalListProvider="DATE_FIN">
			</FIELD>
		</GROUP>
		
		<GROUP label="N° de séjour">
			<FIELD name="NO_SEJOUR" label="" UI="text" width="100" default="" showIfNotDefault="true">
			</FIELD>
		</GROUP>
		
		<GROUP label="Ou Nom">
			<FIELD name="NOM" label="" UI="text" width="200" default="" showIfNotDefault="true">
			</FIELD>
		</GROUP>
	</SELECTIONS>
	
	<SELECTIONS componentName="FILTRES_PATIENTS_SELECTIONS" />
	
	<SELECTIONS componentName="FILTRES_SEJOURS_SELECTIONS" />
	
	<SELECTIONS componentName="FILTRES_MOUVEMENTS_SELECTIONS" />
	
	<!-- <SELECTIONS componentName="FILTRES_CLIENTS_SELECTIONS" /> -->
	
	<QUERIES>
		<QUERY label="">
			<SQL optimizer="V_SEJOURS_X" optimizer2="V_MOUVEMENTS_SEJOUR_X">
				<select>

					<![CDATA[
						DROP TABLE IF EXISTS w_DATA;
						
						CREATE TEMP TABLE w_DATA (finess_texte text, no_sejour_administratif text, exoneration_tm_code text);
						
						SELECT base.cti_execute('
							INSERT INTO w_DATA 
							SELECT t_finess.texte as finess_texte,
								no_sejour_administratif,
								exoneration_tm_code
							FROM 
								pmsi.p_rss
								LEFT JOIN pmsi.v_rsf_total_1 ON rss_id = p_rss.oid
								LEFT JOIN base.t_finess ON p_rss.finess = t_finess.code
							WHERE 
								etat != ''S''
								AND date_entree <= ''[TOPERIODE]''
								AND date_sortie >=''[PERIODE]''',1)
						WHERE EXISTS (SELECT 1 FROM information_schema.views WHERE  table_schema = 'pmsi' AND table_name = 'v_rsf_total_1');
						
						SELECT base.cti_execute('
							INSERT INTO w_DATA
							SELECT t_finess.texte as finess_texte,
								num_admin_sejour as numero_sejour_administratif,
								t_exoneration_tm.code as exoneration_tm_code
							FROM 
								pmsissr.p_ssr
								LEFT JOIN pmsissr.p_rsf_a ON ssr_id = p_ssr.num_admin_sejour
								LEFT JOIN base.t_finess ON p_ssr.finess_id = t_finess.oid
								LEFT JOIN base.t_exoneration_tm ON justif_exo_tm = t_exoneration_tm.oid
							WHERE 
								p_ssr.date_entree <= ''[TOPERIODE]''
								AND p_ssr.date_sortie >=''[PERIODE]''',1)
						WHERE EXISTS (SELECT 1 FROM information_schema.tables WHERE  table_schema = 'pmsissr' AND table_name = 'p_rsf_a');
						
						SELECT base.cti_execute('
							INSERT INTO w_DATA
							SELECT t_finess.texte as finess_texte,
								num_admin_sejour as numero_sejour_administratif,
								t_exoneration_tm.code as exoneration_tm_code
							FROM 
								pmsissr.p_ssr_fil_eau
								LEFT JOIN pmsissr.p_rsf_a_fil_eau ON ssr_id = p_ssr_fil_eau.oid
								LEFT JOIN base.t_finess ON p_ssr_fil_eau.finess_id = t_finess.oid
								LEFT JOIN base.t_exoneration_tm ON justif_exo_tm = t_exoneration_tm.oid
							WHERE 
								p_ssr_fil_eau.date_entree <= ''[TOPERIODE]''
								AND p_ssr_fil_eau.date_sortie >=''[PERIODE]''',1)
						WHERE EXISTS (SELECT 1 FROM information_schema.tables WHERE  table_schema = 'pmsissr' AND table_name = 'p_rsf_a_fil_eau');
						
						
						SELECT DISTINCT 
							v_sejours_1.no_sejour AS "Code séjour"
							,COALESCE(w_DATA.finess_texte,t_finess.texte) AS "Nom Etablissement"
							,v_sejours_1.no_patient AS "IPP"
							,prenom AS "Prénom"
							,nom AS "Nom"
							,p_coordonnees_patient.adresse AS "Adresse postale rue"
							,code_postal_code AS "Adresse postale CP"
							,code_postal_texte AS "Adresse postale ville"
							,telephone_fixe AS "N° de téléphone fixe"
							,telephone_portable AS "N° de téléphone portable"
							,p_coordonnees_patient.email AS "Mail"
							,sexe_texte_court AS "Sexe"
							,age_id AS "Age"
							,type_sejour_texte AS "MDT (Ambu,Hosp,externe,...)"
							,lieu_sortie_service_texte AS "Service de facturation"
							,exoneration_tm_code AS "Code exonération sécu"
							,t_tiers_payant.texte || CASE 
								WHEN tiers_payant_22_id != 0
									THEN ', ' || t_tiers_payant_22.code
								ELSE ''
								END AS "Codes mutuelles" 
							--lieu_id 
						FROM 
							#V_SEJOURS_X# LEFT JOIN w_DATA ON no_sejour_administratif = no_sejour
							LEFT JOIN activite.p_patients ON v_sejours_1.no_patient = p_patients.no_patient
							LEFT JOIN activite.p_coordonnees_patient ON p_coordonnees_patient.sejour_id = v_sejours_1.sejour_id
							LEFT JOIN activite.t_tiers_payant ON t_tiers_payant.oid = tiers_payant_2_id AND tiers_payant_2_id != 0
							LEFT JOIN activite.t_tiers_payant t_tiers_payant_22 ON t_tiers_payant_22.oid = tiers_payant_2_id AND tiers_payant_22_id != 0
							JOIN base.t_finess ON t_finess.oid = (SELECT min(oid) FROM base.t_finess)
						WHERE 
							v_sejours_1.date_entree <= '[TOPERIODE]' 
							AND v_sejours_1.date_sortie >='[PERIODE]'
							
							[VIEW.NO_SEJOUR_SELECT]
							[VIEW.NOM_SELECT]
							[VIEW.SEJOURS_SELECT]
							[VIEW.DATE_SEJOUR_SELECT]
							[VIEW.PATIENTS_SELECT]
							
							
							AND no_sejour = ANY(ARRAY(SELECT v_mouvements_sejour_2.no_sejour 
											FROM #V_MOUVEMENTS_SEJOUR_X#
											JOIN #V_SEJOURS_X# ON v_mouvements_sejour_2.no_sejour = v_sejours_1.no_sejour
											WHERE v_mouvements_sejour_2.date BETWEEN '[PERIODE]' AND '[TOPERIODE]'
											[VIEW.NO_SEJOUR_SELECT]
											[VIEW.MOUVEMENTS_SELECT]
											[VIEW.PATIENTS_SELECT])::text[])
					UNION ALL
						SELECT 
							'-9999999', 
							COUNT(*)::text || ' séjours, ' || count(DISTINCT v_sejours_1.no_patient)::text || ' patients', 
							null, 
							null, 
							null, 
							null, 
							null,
							null,
							null,
							null,
							null,
							null,
							null,
							null,
							null,
							null,
							null
						FROM 
							#V_SEJOURS_X#, activite.p_patients
						WHERE 
							v_sejours_1.date_entree <= '[TOPERIODE]' 
							AND v_sejours_1.date_sortie >='[PERIODE]'
							AND v_sejours_1.no_patient = p_patients.no_patient
							[VIEW.NO_SEJOUR_SELECT]
							[VIEW.NOM_SELECT]
							[VIEW.SEJOURS_SELECT]
							[VIEW.DATE_SEJOUR_SELECT]
							

							AND no_sejour = ANY(ARRAY(SELECT v_mouvements_sejour_2.no_sejour 
											FROM #V_MOUVEMENTS_SEJOUR_X#
											JOIN #V_SEJOURS_X# ON v_mouvements_sejour_2.no_sejour = v_sejours_1.no_sejour
											WHERE v_mouvements_sejour_2.date BETWEEN '[PERIODE]' AND '[TOPERIODE]'
											[VIEW.NO_SEJOUR_SELECT]
											[VIEW.MOUVEMENTS_SELECT]
											[VIEW.PATIENTS_SELECT])::text[])
											;
					]]></select>
				<FIELDS>
					<FIELD name="NO_SEJOUR"></FIELD>
					<FIELD name="ETAB"></FIELD>
					<FIELD name="IPP"></FIELD>
					<FIELD name="PRENOM" ifHideNames="Non autorisé"></FIELD>
					<FIELD name="NOM" ifHideNames="Non autorisé"></FIELD>
					<FIELD name="ADRESSE" ifHideNames="Non autorisé"></FIELD>
					<FIELD name="CP"></FIELD>
					<FIELD name="VILLE"></FIELD>
					<FIELD name="TEL_FIXE" ifHideNames="Non autorisé"></FIELD>
					<FIELD name="TEL_PORTABLE" ifHideNames="Non autorisé"></FIELD>
					<FIELD name="EMAIL" ifHideNames="Non autorisé"></FIELD>
					<FIELD name="SEXE"></FIELD>
					<FIELD name="AGE"></FIELD>
					<FIELD name="TYPE_SEJOUR"></FIELD>
					<FIELD name="SERVICE"></FIELD>
					<FIELD name="CODE_EXO"></FIELD>
					<FIELD name="CODES_MUT"></FIELD>
				</FIELDS>
			</SQL>
		</QUERY>
		
		<QUERY type="propertiesLink" name="VIEWPROPERTIES" forRows="false" />
	</QUERIES>
	<PRESENTATION>
		<VIEWLINKS>		
			<VIEWLINK label="Détail séjour" shortLabel="Détail séjour" view="ACTI000020.XML">
				<ARG name="SEJOUR" value="ROW.NO_SEJOUR" />
			</VIEWLINK>
		
		</VIEWLINKS>
	<ONGLET excelLabel="Liste séjours" label="Liste séjours">
			
			<DATAGRID title="" total="true" key="NO_SEJOUR" headerHeight="36" totalRowInRows="= ROW.NO_SEJOUR == '-9999999'">
				
				<COLUMN dataField="ETAB"
					minWidth="110"
					type="Char"
					headerText="Etablissement" 
					textAlign="left" 
					otherFunction="text"
					totalFunction="text"
					/>
				<COLUMN dataField="IPP"
					width="50"
					type="Char"
					headerText="IPP" 
					textAlign="center" 
					otherFunction="text"
					totalFunction="text"
					/>
				<COLUMN dataField="NO_SEJOUR" 
					width="60"
					type="Char"
					headerText="Code séjour" 
					textAlign="left" 
					totalFunction="text" 
					/>
				<COLUMN dataField="NOM" 
					width="100"
					type="Char" 
					headerText="Nom" 
					textAlign="left" 
					totalFunction="text" />
				<COLUMN dataField="PRENOM" 
					width="100"
					type="Char" 
					headerText="Prénom" 
					textAlign="left" 
					totalFunction="text" />
				<COLUMN dataField="ADRESSE" 
					minWidth="160"
					type="Char" 
					headerText="Adresse" 
					textAlign="left" 
					totalFunction="text" />
				<COLUMN dataField="CP" 
					width="40"
					type="Char" 
					headerText="Code postal" 
					textAlign="center" 
					totalFunction="text" />
				<COLUMN dataField="VILLE" 
					type="Char"
					minWidth="100"
					headerText="Ville" 
					textAlign="left" 
					totalFunction="text" />
				<COLUMN dataField="TEL_FIXE" 
					type="Char"
					width="80"
					headerText="Téléphone fixe" 
					textAlign="center" 
					totalFunction="text" />
				<COLUMN dataField="TEL_PORTABLE" 
					type="Char"
					width="80"
					headerText="Téléphone portable" 
					textAlign="center" 
					totalFunction="text" />
				<COLUMN dataField="EMAIL" 
					type="Char"
					minWidth="150"
					headerText="E-Mail" 
					textAlign="left" 
					totalFunction="text" />
				<COLUMN dataField="SEXE" 
					width="30"
					type="Char"
					headerText="Sexe" 
					textAlign="center" 
					totalFunction="text" />
				<COLUMN dataField="AGE" 
					width="30"
					type="Char"
					headerText="Age" 
					textAlign="center" 
					totalFunction="text" />
				<COLUMN dataField="TYPE_SEJOUR" 
					width="80"
					type="Char"
					headerText="Type séjour" 
					textAlign="left" 
					totalFunction="text" />
				<COLUMN dataField="SERVICE" 
					minWidth="70"
					type="Char"
					headerText="Serv.Fact." 
					textAlign="left" 
					totalFunction="text" />
				<COLUMN dataField="CODE_EXO" 
					width="35"
					type="Char"
					headerText="Code Exo." 
					textAlign="center" 
					totalFunction="text" />
				<COLUMN dataField="CODES_MUT" 
					width="200"
					type="Char"

					headerText="Codes Mut." 
					textAlign="left" 
					totalFunction="text" />
			</DATAGRID>
		</ONGLET>
	</PRESENTATION>
</VUE>
