<?xml version="1.0" encoding="ISO-8859-1"?>
<VUE name="ACTI000097" 
	label="Chiffrier occupation CTI" 
	database="iCTI" 
	swf="*CTI_view1" 
	softCode="iCTI_activite"
	globals="ACTI_globals.XML"
	admProvider="">
	<CACHE cachable="false" />
	<title><![CDATA[= 
			var title:String = 'Chiffrier occupation CTI';
			
			if (SELECT.PERIODE != '-1') {
				if (SELECT.TOPERIODE != '-1') {
					title = title + ' de ' + SELECT.PERIODE_LABEL + ' à ' + SELECT.TOPERIODE_LABEL;
				}
				else {
					title = title + ' à partir de ' + SELECT.PERIODE_LABEL;
				}
			}
			else {
				if (SELECT.TOPERIODE != '-1') {
					title = title + ' jusque ' + SELECT.TOPERIODE_LABEL;
				}
			}
				
			if (SELECT.PROVIDER != '-1') {
				title = title + ' (' + SELECT.PROVIDER_LABEL + ')'
			}
			return title;
		]]></title>
	<VIEWPROPERTIES>
		
		<PROPERTY name="PERIODE" value="[EVAL 
												if ('[PERIODE]' != '-1') {return '[PERIODE]';}
												return '20070101';
											EVAL]"/>
		<PROPERTY name="TOPERIODE" value="[EVAL 
												if ('[TOPERIODE]' != '-1') {return '[TOPERIODE]';}
												return '20201231';
											EVAL]"/>
											
		<PROPERTY name="PROVIDER_SELECT" value="[EVAL 
												if ('[PROVIDER]' != '-1') {return ' AND provider_id = ' . substr('[PROVIDER]',2);}
												return '';
											EVAL]"/>

		
	</VIEWPROPERTIES>
	
	<SELECTIONS label="Période">
	
		<GROUP label="Periode comptable">
			<FIELD name="PERIODE" label="" UI="combo" tree="true" width="200" default="2010-01-01">
				<OPTION label="Pas de sélection" data="-1"/>
				<OPTION dataLink="MOIS" dataField="date" data2Field="mois" treeLevelField="lvl" labelField="txt"/>
			</FIELD>
			<FIELD name="TOPERIODE" label="à" UI="combo" tree="true" width="200" default="-1">
				<OPTION label="Pas de sélection" data="-1"/>
				<OPTION dataLink="TOMOIS" dataField="date" data2Field="mois" treeLevelField="lvl" labelField="txt"/>
			</FIELD>
		</GROUP>
		
		<GROUP label="Logiciel administratif">
			<FIELD name="PROVIDER" label="" UI="combo" width="200" default="-1" globalListProvider="PROVIDERS">
			</FIELD>
		</GROUP>
		
	</SELECTIONS>
	
	<QUERIES> 
		
		<QUERY> 
			<SQL>
				<select><![CDATA[
							DROP TABLE IF EXISTS w_chiffrier_occupation;
							CREATE TEMP TABLE w_chiffrier_occupation
							(
								date date,
								provider_id bigint,
								nb_entrees_directes_s numeric(13,2) DEFAULT 0,
								nb_entrees_directes_m numeric(13,2) DEFAULT 0,
								nb_entrees_directes_c numeric(13,2) DEFAULT 0,
								nb_sorties_directes_s numeric(13,2) DEFAULT 0,
								nb_sorties_directes_m numeric(13,2) DEFAULT 0,
								nb_sorties_directes_c numeric(13,2) DEFAULT 0,
								valide text DEFAULT '0'
							) ;
					
							INSERT INTO w_chiffrier_occupation(date, provider_id, nb_entrees_directes_s)
							SELECT 
								date_trunc('month',p_sejours.date_entree) AS date, 
								provider_id, 
								count(*) AS nb_entrees_directes_s
							FROM activite.p_sejours
							WHERE p_sejours.date_entree BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]'
								[VIEW.PROVIDER_SELECT]
							GROUP BY 1,2;
							
							INSERT INTO w_chiffrier_occupation(date, provider_id, nb_sorties_directes_s)
							SELECT 
								date_trunc('month',p_sejours.date_sortie) AS date, 
								provider_id, 
								count(*) AS nb_sorties_directes_s
							FROM activite.p_sejours
							WHERE p_sejours.code_sorti = '1' AND p_sejours.date_sortie BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]'
								[VIEW.PROVIDER_SELECT]
							GROUP BY 1,2;
					
							
					
							INSERT INTO w_chiffrier_occupation(
								date, 
								provider_id, 
								nb_entrees_directes_c, 
								nb_sorties_directes_c)
							SELECT 
								date_trunc('month',date) AS date, 
								provider_id, 
								SUM(nb_entrees_directes) AS nb_entrees_directes_c, 
								SUM(nb_sorties_directes) AS nb_sorties_directes_c
							FROM activite.p_chiffrier_occupation
							WHERE date BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]'
								[VIEW.PROVIDER_SELECT]
							GROUP BY 1,2 ;
							
							
							
							DROP TABLE IF EXISTS w_chiffrier_occupation_erreurs;
							CREATE TEMP TABLE w_chiffrier_occupation_erreurs AS
							SELECT 
								date_trunc('month',date) AS date,
								provider_id AS w_provider_id
							FROM w_chiffrier_occupation
							GROUP BY 1,2
							HAVING
								SUM(nb_entrees_directes_s) <> SUM(nb_entrees_directes_c) OR
								SUM(nb_sorties_directes_s) <> SUM(nb_sorties_directes_c)
							ORDER BY 1 DESC;
							
							CREATE INDEX w_chiffrier_occupation_erreurs_i1
							ON w_chiffrier_occupation_erreurs
							USING btree
							(date);
					
					
					
							SELECT 
								date ,
								SUM(nb_entrees_directes_s),
								SUM(nb_entrees_directes_c),
								SUM(nb_sorties_directes_s),
								SUM(nb_sorties_directes_c)
							FROM w_chiffrier_occupation
							GROUP BY 1
							ORDER BY 1 DESC;
					]]></select>
				<FIELDS>
					<FIELD name="DATE"  />
					<FIELD name="NE_S" />
					<FIELD name="NE_C" />
					<FIELD name="NS_S" />
					<FIELD name="NS_C" />
				</FIELDS>
			</SQL>
			
			
		</QUERY>
		
		<QUERY> 
			<SQL>
				<select><![CDATA[
							DROP TABLE IF EXISTS w_chiffrier_occupation;
							CREATE TEMP TABLE w_chiffrier_occupation
							(
								date date,
								no_sejour text,
								provider_id bigint,
								nb_entrees_directes_s numeric(13,2) DEFAULT 0,
								nb_entrees_directes_m numeric(13,2) DEFAULT 0,
								nb_entrees_directes_c numeric(13,2) DEFAULT 0,
								nb_sorties_directes_s numeric(13,2) DEFAULT 0,
								nb_sorties_directes_m numeric(13,2) DEFAULT 0,
								nb_sorties_directes_c numeric(13,2) DEFAULT 0
							) ;
					
							INSERT INTO w_chiffrier_occupation(date, no_sejour, provider_id, nb_entrees_directes_s)
							SELECT 
								date(date_trunc('month',p_sejours.date_entree)) AS date, 
								no_sejour,
								provider_id, 
								count(*) AS nb_entrees_directes_s
							FROM activite.p_sejours
								JOIN w_chiffrier_occupation_erreurs ON date(date_trunc('month',p_sejours.date_entree)) = w_chiffrier_occupation_erreurs.date
							WHERE p_sejours.date_entree BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]'
								[VIEW.PROVIDER_SELECT]
							GROUP BY 1,2,3;
							
							INSERT INTO w_chiffrier_occupation(date, no_sejour, provider_id, nb_sorties_directes_s)
							SELECT 
								date(date_trunc('month',p_sejours.date_sortie)) AS date, 
								no_sejour,
								provider_id, 
								count(*) AS nb_sorties_directes_s
							FROM activite.p_sejours
								JOIN w_chiffrier_occupation_erreurs ON date(date_trunc('month',p_sejours.date_sortie)) = w_chiffrier_occupation_erreurs.date
							WHERE p_sejours.code_sorti = '1' AND p_sejours.date_sortie BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]'
								[VIEW.PROVIDER_SELECT]
							GROUP BY 1,2,3;
					
							
					
							INSERT INTO w_chiffrier_occupation(
								date,
								no_sejour,
								provider_id, 
								nb_entrees_directes_c, 
								nb_sorties_directes_c)
							SELECT 
								date(date_trunc('month',p_chiffrier_occupation.date)) AS date, 
								no_sejour,
								provider_id, 
								SUM(nb_entrees_directes) AS nb_entrees_directes_c, 
								SUM(nb_sorties_directes) AS nb_sorties_directes_c
							FROM activite.p_chiffrier_occupation
								JOIN w_chiffrier_occupation_erreurs ON date(date_trunc('month',p_chiffrier_occupation.date)) = w_chiffrier_occupation_erreurs.date
							WHERE p_chiffrier_occupation.date BETWEEN '[VIEW.PERIODE]' AND '[VIEW.TOPERIODE]'
								[VIEW.PROVIDER_SELECT]
							GROUP BY 1,2,3 ;
							
					
							SELECT 
								w_chiffrier_occupation.date,
								w_chiffrier_occupation.no_sejour,
								MIN(p_sejours.date_entree) AS date_entree_s,
								MAX(CASE WHEN code_sorti = '1' THEN p_sejours.date_sortie ELSE NULL END) AS date_sortie_s,
								MIN(CASE WHEN nb_entrees_directes = 1 THEN p_chiffrier_occupation.date ELSE NULL END) AS date_entree_c,
								MAX(CASE WHEN nb_sorties_directes = 1 THEN p_chiffrier_occupation.date ELSE NULL END) AS date_sortie_c
							FROM w_chiffrier_occupation
								LEFT JOIN activite.p_sejours ON w_chiffrier_occupation.no_sejour = p_sejours.no_sejour
								LEFT JOIN activite.p_chiffrier_occupation ON w_chiffrier_occupation.no_sejour = p_chiffrier_occupation.no_sejour
							GROUP BY 1,2
							HAVING MIN(p_sejours.date_entree) IS DISTINCT FROM MIN(CASE WHEN nb_entrees_directes = 1 THEN p_chiffrier_occupation.date ELSE NULL END) OR 
								MAX(CASE WHEN code_sorti = '1' THEN p_sejours.date_sortie ELSE NULL END) IS DISTINCT FROM MAX(CASE WHEN nb_sorties_directes = 1 THEN p_chiffrier_occupation.date ELSE NULL END)
							ORDER BY 1 DESC, 2
							LIMIT 1000;
							
					]]></select>
				<FIELDS>
					<FIELD name="DATE"  />
					<FIELD name="SEJ"  />
					<FIELD name="DE_S" />
					<FIELD name="DS_S" />
					<FIELD name="DE_C" />
					<FIELD name="DS_C" />
				</FIELDS>
			</SQL>
			
			
		</QUERY>
		
		
		
		<QUERY type="comboLink" name="MOIS" forRows="false">
			<SQL>
				<select><![CDATA[
						SELECT date, texte, mois, level
						FROM activite.v_calendrier_1 
						WHERE date >= '20070101' AND
							date <= (SELECT MAX(date) FROM activite.p_chiffrier_occupation) AND
							date <= date(date_trunc('month', now() + interval '1 month' - interval '1 day')) AND
							level IN ('1', '2');
				]]></select>
				<FIELDS>
					<FIELD name="date"/>
					<FIELD name="txt"/>
					<FIELD name="mois"/>
					<FIELD name="lvl"/>
				</FIELDS>
			</SQL>
		</QUERY>
		
		<QUERY type="comboLink" name="TOMOIS" forRows="false">
			<SQL>
				<select><![CDATA[
						SELECT date, texte, mois, level 
						FROM activite.v_calendrier_2
						WHERE date >= '20070101' AND
							date <= (SELECT MAX(date) FROM activite.p_chiffrier_occupation) AND
							date <= date(date_trunc('month', now() + interval '1 month' - interval '1 day')) AND
							level IN ('1', '2');
				]]></select>
				<FIELDS>
					<FIELD name="date"/>
					<FIELD name="txt"/>
					<FIELD name="mois"/>
					<FIELD name="lvl"/>
				</FIELDS>
			</SQL>
		</QUERY>
		
		
			
	</QUERIES> 
	
	<CALCFIELDS>
	</CALCFIELDS>
	
	<PRESENTATION>
	
		<VIEWLINKS>
			
			<VIEWLINK label="Valider Mois" shortLabel="Valider Mois" view="ACTI000093.XML">
				<ARG name="PERIODE" value="ROW.DATE" />
				<ARG name="TOPERIODE" value="ROW.DATF" />
				<ARG name="VAL_OPTION" value="'V'" />
			</VIEWLINK>
			<VIEWLINK label="INValider Mois" shortLabel="INValider Mois" view="ACTI000093.XML">
				<ARG name="PERIODE" value="ROW.DATE" />
				<ARG name="TOPERIODE" value="ROW.DATF" />
				<ARG name="VAL_OPTION" value="'IV'" />
			</VIEWLINK>


		</VIEWLINKS>
			
				
		
		
		<ONGLET excelLabel="Chiffrier occupation" label="Chiffrier occupation" queryNumber="0">
				
			<DATAGRID title="" total="false" headerHeight="54" printRatio="1">
														
				<COLUMN dataField="DATE" 
					type="Date"
					inputFormat="AAAA-MM-JJ"
					outputFormat="MMMM YYYY"
					minWidth="80"
					visible="true"
					headerText="Mois" 
					textAlign="left"
					totalFunction="text"
					totalComplement="TOTAL"
				/>
				
				
				<COLUMN dataField="NE_S"
					type="Number"
					outputFormat="#"
					width="80"
					fixed="false"
					headerText="CTI Entrées"
					multiLine="sum"	
					totalFunction="sum"
					align="right">
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.NE_S != ROW.NE_C" />
				</COLUMN>
				<COLUMN dataField="NE_C"
					type="Number"
					outputFormat="#"
					width="80"
					fixed="false"
					headerText="PRO Entrées"
					multiLine="sum"	
					totalFunction="sum"
					align="right">
				</COLUMN>
				<COLUMN dataField="NS_S"
					type="Number"
					outputFormat="#"
					width="80"
					fixed="false"
					headerText="CTI Sorties"
					multiLine="sum"	
					totalFunction="sum"
					align="right">
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.NS_S != ROW.NS_C" />
				</COLUMN>
				<COLUMN dataField="NS_C"
					type="Number"
					outputFormat="#"
					width="80"
					fixed="false"
					headerText="PRO Sorties"
					multiLine="sum"	
					totalFunction="sum"
					align="right">
				</COLUMN>
					
						
			</DATAGRID>
		
		</ONGLET>
		
		
		<ONGLET excelLabel="Chiffrier occupation - Séjours" label="Chiffrier occupation - Séjours" queryNumber="1">
				
			<DATAGRID title="" total="false" headerHeight="36" printRatio="1">
														
				<COLUMN dataField="DATE" 
					type="Date"
					inputFormat="AAAA-MM-JJ"
					outputFormat="MMMM YYYY"
					width="100"
					visible="true"
					headerText="Mois" 
					textAlign="left"
					totalFunction="text"
					totalComplement="TOTAL"
				/>
				
				
				<COLUMN dataField="SEJ" 
					type="Char"
					width="100"
					visible="true"
					headerText="Séjour" 
					textAlign="left"
					totalFunction="text"
					totalComplement="TOTAL"
				/>
				
				
				<COLUMN dataField="DE_S"
					type="Date"
					inputFormat="AAAA-MM-JJ"
					outputFormat="DD MMM YYYY"
					width="100"
					fixed="false"
					headerText="CTI Date Entrée"
					multiLine="sum"	
					totalFunction="sum"
					align="right">
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.DE_S != ROW.DE_C" />
				</COLUMN>
				<COLUMN dataField="DE_C"
					type="Date"
					inputFormat="AAAA-MM-JJ"
					outputFormat="DD MMM YYYY"
					width="100"
					fixed="false"
					headerText="PRO Date Entrée"
					multiLine="sum"	
					totalFunction="sum"
					align="right">
				</COLUMN>
				<COLUMN dataField="DS_S"
					type="Date"
					inputFormat="AAAA-MM-JJ"
					outputFormat="DD MMM YYYY"
					width="100"
					fixed="false"
					headerText="CTI Date Sortie"
					multiLine="sum"	
					totalFunction="sum"
					align="right">
					<CELLSTYLE name="cellIndicator" value="circle, 5, right top, 0xFF6600" 
							condition="ROW.DS_S != ROW.DS_C" />
				</COLUMN>
				<COLUMN dataField="DS_C"
					type="Date"
					inputFormat="AAAA-MM-JJ"
					outputFormat="DD MMM YYYY"
					width="100"
					fixed="false"
					headerText="PRO Date Sortie"
					multiLine="sum"	
					totalFunction="sum"
					align="right">
				</COLUMN>
					
						
			</DATAGRID>
		
		</ONGLET>
		
		
				
	</PRESENTATION>
	
</VUE>

			
