<?xml version="1.0" encoding="ISO-8859-15"?>
<VUE name="ACTI000033" 
	label="Activité. Liste des séjours" 
	title="= 'Liste des séjours (' + SELECT.PERIODE_LABEL + ' à ' + SELECT.TOPERIODE_LABEL + ')'" 
	database="iCTI" swf="*CTI_view1" 
	softCode="iCTI_activite"
	globals="ACTI_globals.XML"
	componentsFile="ACTI_components.XML"
    helpFile="iCTI_ACTI_ACTI000033.HTML"
    helpDokuWIkiDir="activite:recherche_par_criteres" 
    helpDokuWIkiFile="acti000033" 
>    
	
	<VIEWPROPERTIES>
		
		<PROPERTY name="NO_SEJOUR_SELECT">
			<value>
				<![CDATA[
					[EVAL 
						if (('[NO_SEJOUR]') != '') {return " AND (v_sejours_1.no_sejour = '[NO_SEJOUR]' OR substr(v_sejours_1.no_sejour,3,1) = '_' AND substr(v_sejours_1.no_sejour,4) = '[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="NO_FACTURE_SELECT">
			<value>
				<![CDATA[
					[EVAL 
						if (('[NO_FACTURE]') != '') {return " AND v_sejours_1.no_sejour IN (SELECT no_sejour FROM activite.p_factures WHERE no_facture = '[NO_FACTURE]' OR substr(no_facture,3,1) = '_' AND substr(no_facture,4) = '[NO_FACTURE]') "; }
						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>
		
		<PROPERTYx dataLink="VIEWPROPERTIES" nameField="name" valueField="value" />
		
	</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="ALIAS_PERIODE" label="" UI="combo" width="220" default="CUM_LAST_MONTH" tree="true" globalListProvider="ALIAS_PERIODE" />
			<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>
        
        <GROUP label="Ou N° de facture">
			<FIELD name="NO_FACTURE" label="" UI="text" width="100" default="" showIfNotDefault="true">
			</FIELD>
		</GROUP>
		
	</SELECTIONS>
	
	<SELECTIONS componentName="FILTRES_SEJOURS_SELECTIONS" />
	
	<SELECTIONS componentName="FILTRES_MOUVEMENTS_SELECTIONS" />

	<QUERIES>
	
		<QUERY label=""> 
			<SQL optimizer="V_SEJOURS_X" optimizer2="V_MOUVEMENTS_SEJOUR_X">
				<select>
					<![CDATA[
						SELECT 
							v_sejours_1.no_sejour, 
							nom || ' ' || prenom,
                            v_sejours_1.no_patient,
							v_sejours_1.date_entree, 
							substr(lpad(v_sejours_1.heure_entree, 6, '0'), 1, 2) || ':' || substr(lpad(v_sejours_1.heure_entree, 6, '0'), 3, 2) as heure_entree,
							CASE WHEN v_sejours_1.date_sortie <> '2099-12-31' THEN to_char(date_sortie,'YYYY-MM-DD') ELSE '' END::text, 
							CASE WHEN code_sorti = '1' THEN substr(lpad(v_sejours_1.heure_sortie, 6, '0'), 1, 2) || ':' || substr(lpad(v_sejours_1.heure_sortie, 6, '0'), 3, 2) ELSE NULL END as heure_sortie,
							CASE WHEN v_sejours_1.date_entree <= date(now())  THEN LEAST(v_sejours_1.date_sortie, date(now())) - v_sejours_1.date_entree ELSE 0 END, 
							lieu_sortie_etage_code,
							lieu_sortie_lit_code,
							lieu_sortie_service_code,
							medecin_sejour_nom,
                            medecin_traitant_nom,
							CASE
								WHEN type_sejour = '9' THEN 'FICTIF'
								WHEN code_prevu = '1' THEN 'PREVU'
								WHEN code_sorti <> '1' THEN 'PRESENT'
								WHEN date_sortie >= date(now()) THEN 'PRESENT'
								WHEN date_facture NOT IN ('2099-12-31','0001-01-01') AND date_solde NOT IN ('2099-12-31','0001-01-01') THEN 'SOLDE'
								WHEN date_expedition NOT IN ('2099-12-31','0001-01-01') THEN 'EXPEDIE'
								WHEN date_facture NOT IN ('2099-12-31','0001-01-01') THEN 'FACTURE'
								WHEN date_sortie NOT IN ('2099-12-31') THEN 'SORTI'
								ELSE 'SORTI'
							END
						FROM 
							#V_SEJOURS_X#, activite.p_patients
						WHERE 
							v_sejours_1.no_patient = p_patients.no_patient
							AND v_sejours_1.date_entree <= '[TOPERIODE]' 
							AND v_sejours_1.date_sortie >='[PERIODE]'
							
							[VIEW.NO_SEJOUR_SELECT]
							[VIEW.NOM_SELECT]
                            [VIEW.NO_FACTURE_SELECT]
							[VIEW.SEJOURS_SELECT]
							[VIEW.DATE_SEJOUR_SELECT]

							AND no_sejour IN (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]
											)
						
						ORDER BY 1
                        LIMIT 5000;
					]]>
				</select>
				<FIELDS>
					<FIELD name="SEJ" />
					<FIELD name="NOM" ifHideNames="Non autorisé" />
					<FIELD name="NPAT" />
					<FIELD name="DENT" />
					<FIELD name="HENT" />
					<FIELD name="DSOR" />
					<FIELD name="HSOR" />
					<FIELD name="DUR" />
					<FIELD name="ETA" />
					<FIELD name="LIT" />
					<FIELD name="SER" />
					<FIELD name="MED" />
					<FIELD name="MEDT" />
					<FIELD name="STS" />
				</FIELDS>
			</SQL>
			<SQL optimizer="V_SEJOURS_X" optimizer2="V_MOUVEMENTS_SEJOUR_X">
				<select>
					<![CDATA[
						
						SELECT 
							'-9999999', 
							COUNT(*)::text || ' séjours, ' || count(DISTINCT v_sejours_1.no_patient)::text || ' patients', 
							null, 
							null, 
							null, 
							null,
							0, 
							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.NO_FACTURE_SELECT]
							[VIEW.SEJOURS_SELECT]
							[VIEW.DATE_SEJOUR_SELECT]

							AND no_sejour IN (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]
											);

					]]>
				</select>
				<FIELDS>
					<FIELD name="SEJ" />
					<FIELD name="NOM" ifHideNames="Non autorisé" />
					<FIELD name="DENT" />
					<FIELD name="HENT" />
					<FIELD name="DSOR" />
					<FIELD name="HSOR" />
					<FIELD name="DUR" />
					<FIELD name="ETA" />
					<FIELD name="LIT" />
					<FIELD name="SER" />
					<FIELD name="MED" />
					<FIELD name="MEDT" />
					<FIELD name="STS" />
				</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.SEJ" />
				
			</VIEWLINK>
		
		</VIEWLINKS>
	
		<ONGLET excelLabel="Liste séjours" label="Liste séjours">
			
			<DATAGRID title="" total="true" key="SEJ" headerHeight="36" totalRowInRows="= ROW.SEJ == '-9999999'">
				
				<COLUMN dataField="SEJ"
					width="100"
					type="Char"
					headerText="No séjour" 
					textAlign="left" 
					otherFunction="text"
					totalFunction="text"
					totalComplement="TOTAL" />
				<COLUMN dataField="NOM" 
					type="Char"
					minWidth="50"
					headerText="Nom" 
					textAlign="left" 
					totalFunction="text" 
					totalComplement="*ROW" />
				<COLUMN dataField="NPAT" 
					type="Char"
					width="80"
					fixed="false"
					headerText="N° Patient" 
					textAlign="right" 
					totalFunction="text" />
				<COLUMN dataField="DENT" 
					width="60"
					type="Date" 
					inputFormat="AAAA-MM-JJ"
					outputFormat="DD/MM/YY"
					headerText="Date entrée" 
					textAlign="right" 
					totalFunction="text" />
				<COLUMN dataField="HENT" 
					width="60"
					type="Char" 
					headerText="Heure entrée" 
					textAlign="right" 
					totalFunction="text" />
				<COLUMN dataField="DSOR" 
					width="60"
					type="Date" 
					inputFormat="AAAA-MM-JJ"
					outputFormat="DD/MM/YY"
					headerText="Date sortie" 
					textAlign="right" 
					totalFunction="text" />
				<COLUMN dataField="HSOR" 
					width="60"
					type="Char" 
					headerText="Heure sortie" 
					textAlign="right" 
					totalFunction="text" />
				<COLUMN dataField="DUR" 
					type="Number"
					width="50"
					outputFormat="#"
					fixed="false"
					headerText="Durée" 
					textAlign="right" 
					totalFunction="text" />
				<COLUMN dataField="STS" 
					type="Char"
					width="80"
					fixed="false"
					headerText="Etat" 
					textAlign="left" 
					totalFunction="text" />
				<COLUMN dataField="ETA" 
					type="Char"
					width="80"
					fixed="false"
					headerText="Etage" 
					textAlign="left" 
					totalFunction="text" />
				<COLUMN dataField="LIT" 
					type="Char"
					width="50"
					fixed="false"
					headerText="Lit" 
					textAlign="left" 
					totalFunction="text" />
				<COLUMN dataField="SER" 
					type="Char"
					width="80"
					fixed="false"
					headerText="Serv. fact." 
					textAlign="left" 
					totalFunction="text" />
				<COLUMN dataField="MED" 
					type="Char"
					minWidth="50"
					maxWidth="150"
					fixed="false"
					headerText="Médecin" 
					textAlign="left" 
					totalFunction="text" />				
                <COLUMN dataField="MEDT" 
					type="Char"
					minWidth="50"
					maxWidth="150"
					fixed="false"
					headerText="Médecin traitant"
					textAlign="left" 
					totalFunction="text" />
			</DATAGRID>
		</ONGLET>
	</PRESENTATION>
</VUE>
