<?xml version="1.0" encoding="ISO-8859-1"?>
<VUE name="PMSI000116" label="PMSI. Justificatif des ré-entrées" title="= 'Ré-entrées ' + ' (' + SELECT.PERIODE_LABEL + ' à ' + SELECT.TOPERIODE_LABEL + ') - ' + SELECT.VERSION_GROUPAGE_LABEL" database="iCTI" swf="*CTI_view1" softCode="iCTI_pmsi" globals="PMSI_globals.XML" componentsFile="PMSI_components.XML" helpDokuWIkiDir="pmsi:file_active" helpDokuWIkiFile="pmsi000114"  helpFile="iCTI_PMSI_PMSI000116.HTML">
	<VIEWPROPERTIES>
		<PROPERTYx dataLink="VIEWPROPERTIES" nameField="name" valueField="value" />
	</VIEWPROPERTIES>
	<SELECTIONS label="Paramètres de l'analyse" displayText="">
		<GROUP label="Période">
			<FIELD name="ALIAS_PERIODE" label="" UI="combo" width="220" default="CUM_LAST_MONTH" tree="true" globalListProvider="ALIAS_PERIODE" />
			<FIELD name="PERIODE" label="" UI="combo" tree="true" width="200" default="2014-01-01" globalListProvider="DATE_DEBUT" />
			<FIELD name="TOPERIODE" label="à" UI="combo" tree="true" width="200" default="2014-12-31" globalListProvider="DATE_FIN" />
		</GROUP>
	</SELECTIONS>
	<SELECTIONS componentName="FILTRES_RSS_SELECTIONS" />
	<SELECTIONS componentName="FILTRES_REENTREE_SELECTIONS" />
	<QUERIES>
		<QUERY label="">
			<SQL optimizer="V_RSS_X">
				<select><![CDATA[
					
					DROP TABLE IF EXISTS w_reentrees;
					CREATE TEMP TABLE w_reentrees AS
					SELECT 
						v_rss_1.patient_id, 
						v_rss_1.no_rss AS no_rss, 
						v_rss_1.rss_id AS rss_id, 
						v_rss_1.date_sortie AS date_sortie, 
						0::bigint AS apres_rss_id, 
						0::bigint AS apres_no_rss, 
						''::text AS apres_no_sejour_administratif,
						'20991231'::date AS apres_date_entree,
						'20991231'::date AS apres_date_sortie,
						'0'::numeric AS apres_duree_sejour,
						0::numeric AS delai,
						''::text AS apres_ghm_code,
						''::text AS apres_medecin_reference_rss_nom,
						''::text AS apres_diagnostic_principal
					FROM #V_RSS_X#
					WHERE
						v_rss_1.patient_id > 0 AND 
						v_rss_1.date_sortie BETWEEN '[PERIODE]' AND '[TOPERIODE]'  
						[VIEW.FILTRE_RSS_SELECT];
					
				]]></select>
			</SQL>
			<SQL optimizer="V_RSS_X">
				<select><![CDATA[
					
					DROP TABLE IF EXISTS w_apres;
					CREATE TEMP TABLE w_apres  AS
					SELECT 
						v_rss_1.patient_id, 
						v_rss_1.rss_id AS apres_rss_id, 
						v_rss_1.no_rss AS apres_no_rss, 
						v_rss_1.no_sejour_administratif AS apres_no_sejour_administratif,
						v_rss_1.date_entree AS apres_date_entree,
						v_rss_1.date_sortie AS apres_date_sortie,
						v_rss_1.duree_sejour AS apres_duree_sejour,
						v_rss_1.ghm_code AS apres_ghm_code,
						medecin_reference_rss_nom AS apres_medecin_reference_rss_nom,
						diagnostic_principal_code || ' ' || diagnostic_principal_texte AS apres_diagnostic_principal
					FROM #V_RSS_X#
					WHERE 
						v_rss_1.patient_id > 0 
						AND v_rss_1.date_entree >= '[PERIODE]'
						[VIEW.FILTRE_REENTREE_SELECT]
					ORDER BY patient_id, apres_date_entree;
					
				]]></select>
			</SQL>
			<SQL optimizer="V_RSS_X">
				<select><![CDATA[
					
					UPDATE w_reentrees
					SET apres_rss_id = subview.apres_rss_id,
						apres_no_rss = subview.apres_no_rss,
						apres_no_sejour_administratif = subview.apres_no_sejour_administratif,
						apres_date_entree = subview.apres_date_entree,
						apres_date_sortie = subview.apres_date_sortie,
						apres_duree_sejour = subview.apres_duree_sejour,
						delai = subview.apres_date_entree - w_reentrees.date_sortie,
						apres_ghm_code = subview.apres_ghm_code,
						apres_medecin_reference_rss_nom= subview.apres_medecin_reference_rss_nom,
						apres_diagnostic_principal = subview.apres_diagnostic_principal
					FROM
					(
						SELECT rss_id,	
							(MIN(ARRAY[w_apres.apres_date_entree::text,w_apres.apres_no_rss::text]))[2]::bigint AS apres_no_rss,
							(MIN(ARRAY[w_apres.apres_date_entree::text,w_apres.apres_no_sejour_administratif::text]))[2]::text AS apres_no_sejour_administratif,
							(MIN(ARRAY[w_apres.apres_date_entree::text,w_apres.apres_ghm_code::text]))[2] AS apres_ghm_code,
							(MIN(ARRAY[w_apres.apres_date_entree::text,w_apres.apres_medecin_reference_rss_nom::text]))[2] AS apres_medecin_reference_rss_nom,
							(MIN(ARRAY[w_apres.apres_date_entree::text,w_apres.apres_diagnostic_principal::text]))[2] AS apres_diagnostic_principal,
							(MIN(ARRAY[w_apres.apres_date_entree::text,w_apres.apres_rss_id::text]))[2]::bigint AS apres_rss_id,
							MIN(w_apres.apres_date_entree) AS apres_date_entree,
							(MIN(ARRAY[w_apres.apres_date_entree,w_apres.apres_date_sortie]))[2] AS apres_date_sortie,
							(MIN(ARRAY[w_apres.apres_date_entree::text,w_apres.apres_duree_sejour::text]))[2]::numeric AS apres_duree_sejour
						FROM w_reentrees
						JOIN w_apres ON
							w_reentrees.patient_id = w_apres.patient_id
							AND w_apres.apres_rss_id <> w_reentrees.rss_id
							AND w_apres.apres_date_entree >= w_reentrees.date_sortie
							GROUP BY 1
						) subview
						WHERE w_reentrees.rss_id = subview.rss_id
					;

						
					UPDATE w_reentrees
					SET apres_rss_id = 0
					WHERE NOT (1=1 [VIEW.DELAI_REENTREE_SELECT])
					;
					
					ANALYSE w_reentrees
					;
					
				]]></select>
			</SQL>
			<SQL optimizer="V_RSS_X">
				<select><![CDATA[
					
					(SELECT 
						v_rss_1.rss_id, 
						v_rss_1.no_rss,
						v_rss_1.no_sejour_administratif,
						CASE WHEN no_patient > 0 THEN no_patient ELSE 0 END,
						v_rss_1.finess, 
						identite_nom || ' ' || identite_prenom, 
						sexe_texte_court, 
						v_rss_1.date_naissance,
						v_rss_1.age,
						v_rss_1.date_entree,
						v_rss_1.date_sortie,
						v_rss_1.duree_sejour,
						v_rss_1.mode_sortie,
						v_rss_1.ghm_code, 
						medecin_reference_rss_nom,
						diagnostic_principal_code || ' ' || diagnostic_principal_texte, 
						w_reentrees.apres_rss_id, 
						w_reentrees.apres_no_rss,
						w_reentrees.apres_no_sejour_administratif,
						w_reentrees.apres_date_entree,
						w_reentrees.apres_date_sortie,
						w_reentrees.apres_duree_sejour,
						w_reentrees.delai,
						w_reentrees.apres_ghm_code, 
						w_reentrees.apres_medecin_reference_rss_nom,
						w_reentrees.apres_diagnostic_principal       
					FROM #V_RSS_X#
					JOIN w_reentrees ON v_rss_1.rss_id = w_reentrees.rss_id 
					WHERE  w_reentrees.apres_rss_id <> 0
					)
					
					UNION ALL
					
					(SELECT 
						-9999999, 
						COUNT(DISTINCT v_rss_1.no_rss),
						'',
						0,
						'', 
						COUNT(*)::text || ' ré-entrées' , 
						'',
						null,
						0, 
						null,
						null,
						0,
						'', 
						'',
						'', 
						'',

						0,
						0,
						null,
						null,
						null,
						0,
						0,
						'', 
						'', 
						''
					FROM #V_RSS_X#
					JOIN w_reentrees ON v_rss_1.rss_id = w_reentrees.rss_id 
					WHERE  w_reentrees.apres_rss_id <> 0
					)
					ORDER BY 2
					
				]]></select>
				<FIELDS>
					<FIELD name="OID_F" />
					<FIELD name="RSS_F" />
					<FIELD name="N_ADMIN_F" />
					<FIELD name="PAT" />
					<FIELD name="FIN" />
					<FIELD name="NOM" ifHideNames="Non autorisé" />
					<FIELD name="SEX" />
					<FIELD name="DATE_NAISSANCE_F" />
					<FIELD name="AGE" />
					<FIELD name="DATE_F" />
					<FIELD name="DATS_F" />
					<FIELD name="DUREE_SEJ_F" />
					<FIELD name="MODS_F" />
					<FIELD name="GHM_F" />
					<FIELD name="MED_F" />
					<FIELD name="DIAGP_F" />
					<FIELD name="OID_T" />
					<FIELD name="RSS_T" />
					<FIELD name="N_ADMIN_T" />
					<FIELD name="DATE_T" />
					<FIELD name="DATS_T" />
					<FIELD name="DUREE_SEJ_T" />
					<FIELD name="DELAI" />
					<FIELD name="GHM_T" />
					<FIELD name="MED_T" />
					<FIELD name="DIAGP_T" />
				</FIELDS>
			</SQL>
		</QUERY>
		<QUERY type="propertiesLink" name="VIEWPROPERTIES" forRows="false" />
	</QUERIES>
	<PRESENTATION>
		<VIEWLINKS>
			<VIEWLINK label="Détail RSS" shortLabel="Détail RSS" view="PMSI000006.XML">
				<ARG name="OID" value="	if (COLUMN == 'RSS_T') {return ROW.OID_T} return ROW.OID_F" />
			</VIEWLINK>
		</VIEWLINKS>
		<ONGLET excelLabel="Liste Ré-Entrées">
			<DATAGRID title="" total="true" key="RSS_F" headerHeight="36" totalRowInRows="= ROW.OID_F == '-9999999'">
				<ROWSTYLE name="fontSize" value="11" />
				<FOOTERROWSTYLE name="fontSize" value="11" />
				<COLUMN dataField="OID_F" type="Char" visible="false" />
				<COLUMN dataField="FIN" width="60" type="Char" visible="true" headerText="Etablissement" textAlign="left" visibleCondition="ENV.CONSOLIDATION == '1'" otherFunction="text" totalFunction="text" totalComplement="" />
				<COLUMN dataField="RSS_F" width="60" type="Number" visible="true" outputFormat="#" headerText="N° RSS" textAlign="center" otherFunction="text" totalFunction="text" totalComplement="TOTAL" />
				<COLUMN dataField="N_ADMIN_F" width="60" type="Char" visible="true" headerText="N° séjour" textAlign="center" otherFunction="text" totalFunction="text" totalComplement="" />
				<COLUMN dataField="PAT" width="80" type="Number" visible="true" outputFormat="#" headerText="N° Patient" textAlign="center" otherFunction="text" totalFunction="text" totalComplement="" />
				<COLUMN dataField="NOM" minWidth="150" maxWidth="200" type="Char" visible="true" headerText="Nom" textAlign="left" otherFunction="text" totalFunction="text" totalComplement="*ROW" />
				<COLUMN dataField="SEX" width="40" type="Char" visible="true" headerText="Sexe" fixed="false" textAlign="right" otherFunction="text" totalFunction="text">
					<CELLSTYLE name="cellIndicator" value="femme, 12,left middle , 0xEB76B1" condition="ROW.SEX == 'F'" /> 
					<CELLSTYLE name="cellIndicator" value="homme, 12,left middle , 0x75a0eb " condition="ROW.SEX == 'M'" /> 
					<CELLSTYLE name="cellIndicator" value="rectangle, 10,left middle , #FF5001 " condition="ROW.SEX == 'I'" /> 
					<CELLSTYLE name="cellIndicator" value="rectangle, 10, left middle, 0x666666" condition="ROW.SEX != 'F' &amp;&amp; ROW.SEX != 'M' &amp;&amp; ROW.SEX != 'I'" /> 
				</COLUMN>
				<COLUMN dataField="DATE_NAISSANCE_F" width="60" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" fixed="false" headerText="Date naissance" textAlign="left" otherFunction="text" totalFunction="text" />
				<COLUMN dataField="AGE" width="40" type="Number" outputFormat="#" fixed="false" visible="true" headerText="Age" textAlign="right" otherFunction="text" totalFunction="text" />
				<COLUMN dataField="DATE_F" width="60" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" fixed="false" headerText="Date entrée" textAlign="left" otherFunction="text" totalFunction="text" />
				<COLUMN dataField="DATS_F" width="60" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" fixed="false" headerText="Date sortie" textAlign="left" otherFunction="text" totalFunction="text" />
				<COLUMN dataField="DUREE_SEJ_F" width="50" type="Number" outputFormat="#j" visible="true" fixed="false" headerText="Durée Séjour" textAlign="left" otherFunction="text" totalFunction="text" />
				<COLUMN dataField="MODS_F" width="50" type="Char" visible="true" fixed="false" headerText="Mode sortie" textAlign="left" otherFunction="text" totalFunction="text" />
				<COLUMN dataField="GHM_F" width="60" type="Char" visible="true" fixed="false" headerText="GHM" textAlign="left" otherFunction="text" totalFunction="text" />
				<COLUMN dataField="MED_F" minWidth="60" maxWidth="150" type="Char" visible="true" fixed="false" headerText="Médecin" textAlign="left" otherFunction="text" totalFunction="text" />
				<COLUMN dataField="DIAGP_F" width="300" type="Char" fixed="false" visible="true" headerText="Diag P" textAlign="left" otherFunction="text" totalFunction="text" largeSeparator="true" />
				<COLUMN dataField="OID_T" type="Char" visible="false" />
				<COLUMN dataField="RSS_T" width="60" type="Number" visible="true" outputFormat="#" headerText="N° RSS R.E." selectable="true" textAlign="center" otherFunction="text" totalFunction="text" />
				<COLUMN dataField="N_ADMIN_T" width="60" type="Char" visible="true" headerText="N° séjour R.E." textAlign="center" otherFunction="text" totalFunction="text" totalComplement="" />
				<COLUMN dataField="DATE_T" width="60" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" fixed="false" headerText="Date entrée R.E." textAlign="left" otherFunction="text" totalFunction="text" />
				<COLUMN dataField="DATS_T" width="60" type="Date" inputFormat="AAAA-MM-JJ" outputFormat="DD/MM/YY" visible="true" fixed="false" headerText="Date sortie R.E." textAlign="left" otherFunction="text" totalFunction="text" />
				<COLUMN dataField="DUREE_SEJ_T" width="50" type="Number" outputFormat="#j" visible="true" fixed="false" headerText="Durée Séjour R.E." textAlign="left" otherFunction="text" totalFunction="text" />
				<COLUMN dataField="DELAI" width="50" type="Number" outputFormat="#j" visible="true" fixed="false" headerText="Délai R.E." textAlign="left" otherFunction="text" totalFunction="text" />
				<COLUMN dataField="GHM_T" width="60" type="Char" visible="true" fixed="false" headerText="GHM R.E." textAlign="left" otherFunction="text" totalFunction="text" />
				<COLUMN dataField="MED_T" minWidth="60" maxWidth="150" type="Char" visible="true" fixed="false" headerText="Médecin R.E." textAlign="left" otherFunction="text" totalFunction="text" />
				<COLUMN dataField="DIAGP_T" width="300" type="Char" fixed="false" visible="true" headerText="Diag P R.E." textAlign="left" otherFunction="text" totalFunction="text" />
			</DATAGRID>
		</ONGLET>
	</PRESENTATION>
</VUE>
