<?xml version="1.0" encoding="ISO-8859-1"?>
<VUE 
	name="ACTIS00122" 
	label="Actvité. Feuille de facturation PY CORWIN" 
	title="= 'Feuille de facturation PY CORWIN (' + SELECT.PERIODE_LABEL + ' à ' + SELECT.TOPERIODE_LABEL + ')'" 
	database="iCTI"
	swf="*CTI_view1"
	softCode="iCTI_activite"
	globals="ACTI_globals.XML"
	componentsFile="ACTI_components.XML">
		

	<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="Période" UI="combo" tree="true" width="200" default="2013-03-01" globalListProvider="DATE_DEBUT" >
			</FIELD>
			<FIELD name="TOPERIODE" label="à" UI="combo" tree="true" width="200" default="2013-03-01" globalListProvider="DATE_FIN" >
			</FIELD>
		</GROUP>
		
		<GROUP label="Regroupement">
			<FIELD name="GROUPBY_TAB" width="100" label="" UI="combo" default="AUTO" >
				<OPTION label="Automatique" data="AUTO"/>
				<OPTION label="Arbre" data="TREE"/>
				<OPTION label="Onglet" data="TAB"/>
			</FIELD>
		</GROUP>
	</SELECTIONS>
	
	
	<QUERIES>
		<QUERY label="">
			<SQL>
				<select><![CDATA[
			
					-- Extraction des activités
					DROP SEQUENCE IF EXISTS w_PVCOMM_sequence;
					CREATE TEMP SEQUENCE w_PVCOMM_sequence;

					DROP TABLE IF EXISTS w_PVCOMM;
					CREATE TEMP TABLE w_PVCOMM AS
					SELECT 
						*,
						nextval('w_PVCOMM_sequence'::regclass) AS sequence,
						0::bigint AS sequence_jour
					FROM
					(
						SELECT PVCOMM.CODE,
							date(PVCOMM.DATE) AS DATE,
							PVCOMM.HEUREDEB,
							PVCOMM.MINDEB,
							PVCOMM.HEUREFIN,
							PVCOMM.MINFIN,
							CASE 
								WHEN PVCOMM.HEUREDEB < 13 AND PVCOMM.HEUREFIN < 13 THEN '1'
								WHEN PVCOMM.HEUREDEB >= 13 AND PVCOMM.HEUREFIN >= 13 THEN '2'
								ELSE '3' END AS PERIODE_TYPE,
							to_char(PVCOMM.HEUREDEB,'FM00') || 'h' || to_char(PVCOMM.MINDEB,'FM00') || ' à ' || to_char(PVCOMM.HEUREFIN,'FM00') || 'h' || to_char(PVCOMM.MINFIN,'FM00') AS PERIODE,
							C_PVATEL + C_PVACTI AS ACTI_CODE,
							COALESCE(PVACTI.LIBELLE, PVATEL.LIBELLE, (C_PVATEL + C_PVACTI)::text) AS ACTI_LIBELLE,
							0::numeric AS NB_INTERVENANTS_PREVUS,
							NULL::BIGINT[] AS INTERVENANTS_PREVUS,
							0::numeric AS NB_RESIDENTS_PREVUS,
							NULL::BIGINT[] AS RESIDENTS_PREVUS,
							0::numeric AS NB_INTERVENANTS_PRESENTS,
							NULL::BIGINT[] AS INTERVENANTS_PRESENTS,
							0::numeric AS NB_RESIDENTS_PRESENTS,
							NULL::BIGINT[] AS RESIDENTS_PRESENTS,
							''::text AS PYx
						FROM prod_corwin.PVCOMM	
						LEFT JOIN prod_corwin.PVACTI ON C_PVACTI = PVACTI.code
						LEFT JOIN prod_corwin.PVATEL ON C_PVATEL = PVATEL.code
						WHERE C_PVACTI <> 0 AND date(date) BETWEEN '[PERIODE]' AND '[TOPERIODE]'
						ORDER BY date, PVCOMM.CODE
					) subview;



					UPDATE w_PVCOMM
					SET sequence_jour = sequence - sequence_first + 1
					FROM (
						SELECT 
							date,
							periode_type,
							MIN(sequence) AS sequence_first
						FROM w_PVCOMM
						GROUP BY 1,2
					) subview
					WHERE w_PVCOMM.date = subview.date AND w_PVCOMM.periode_type = subview.periode_type;

					
					UPDATE w_PVCOMM
					SET 
						NB_INTERVENANTS_PREVUS = subview.NB_INTERVENANTS,
						INTERVENANTS_PREVUS = subview.INTERVENANTS
					FROM
						(SELECT C_PVCOMM, 
							base.cti_group_array3(C_INTERVEN) AS INTERVENANTS,
							count(DISTINCT C_INTERVEN) AS NB_INTERVENANTS
						FROM prod_corwin.PVCOMM2
						WHERE C_INTERVEN <> 0 AND PREVU = 1
						GROUP BY 1
						) subview
					WHERE w_PVCOMM.CODE = C_PVCOMM;


					UPDATE w_PVCOMM
					SET 
						NB_INTERVENANTS_PRESENTS = subview.NB_INTERVENANTS,
						INTERVENANTS_PRESENTS = subview.INTERVENANTS
					FROM
						(SELECT C_PVCOMM, 
							base.cti_group_array3(C_INTERVEN) AS INTERVENANTS,
							count(DISTINCT C_INTERVEN) AS NB_INTERVENANTS
						FROM prod_corwin.PVCOMM2
						WHERE C_INTERVEN <> 0 AND PRESENT = 1
						GROUP BY 1
						) subview
					WHERE w_PVCOMM.CODE = C_PVCOMM;


					UPDATE w_PVCOMM
					SET 
						NB_RESIDENTS_PREVUS = subview.NB_RESIDENTS,
						RESIDENTS_PREVUS = subview.RESIDENTS
					FROM
						(SELECT C_PVCOMM, 
							base.cti_group_array3(NUMID) AS RESIDENTS,
							count(DISTINCT NUMID) AS NB_RESIDENTS
						FROM prod_corwin.PVCOMM2
						WHERE NUMID <> 0 AND PREVU = 1
						GROUP BY 1
						) subview
					WHERE w_PVCOMM.CODE = C_PVCOMM;


					UPDATE w_PVCOMM
					SET 
						NB_RESIDENTS_PRESENTS = subview.NB_RESIDENTS,
						RESIDENTS_PRESENTS = subview.RESIDENTS
					FROM
						(SELECT C_PVCOMM, 
							base.cti_group_array3(NUMID) AS RESIDENTS,
							count(DISTINCT NUMID) AS NB_RESIDENTS
						FROM prod_corwin.PVCOMM2
						WHERE NUMID <> 0 AND PRESENT = 1
						GROUP BY 1
						) subview
					WHERE w_PVCOMM.CODE = C_PVCOMM;



					UPDATE w_PVCOMM
					SET PYx =
						CASE 
							WHEN PERIODE_TYPE <> '3' AND NB_RESIDENTS_PRESENTS > 1 AND NB_INTERVENANTS_PRESENTS = 1 THEN 'PY0'
							WHEN PERIODE_TYPE <> '3' AND NB_RESIDENTS_PRESENTS > 1 AND NB_INTERVENANTS_PRESENTS > 1 THEN 'PY2'
							WHEN PERIODE_TYPE <> '3' AND NB_RESIDENTS_PRESENTS = 1 AND NB_INTERVENANTS_PRESENTS = 1 THEN 'PY1'
							WHEN PERIODE_TYPE <> '3' AND NB_RESIDENTS_PRESENTS = 1 AND NB_INTERVENANTS_PRESENTS > 1 THEN 'PY3'
							WHEN PERIODE_TYPE = '3' AND NB_RESIDENTS_PRESENTS > 1 AND NB_INTERVENANTS_PRESENTS = 1 THEN 'PY4'
							WHEN PERIODE_TYPE = '3' AND NB_RESIDENTS_PRESENTS > 1 AND NB_INTERVENANTS_PRESENTS > 1 THEN 'PY6'
							WHEN PERIODE_TYPE = '3' AND NB_RESIDENTS_PRESENTS = 1 AND NB_INTERVENANTS_PRESENTS = 1 THEN 'PY5'
							WHEN PERIODE_TYPE = '3' AND NB_RESIDENTS_PRESENTS = 1 AND NB_INTERVENANTS_PRESENTS > 1 THEN 'PY7'
							ELSE '' END;





					DROP TABLE IF EXISTS w_releve;
					CREATE TEMP TABLE w_releve AS
					SELECT subview.NUMID AS resident_id, NDA AS resident_code, NOM AS resident_texte, 
						p_calendrier.date, to_char(p_calendrier.date,'DD/MM/YYYY') AS date_code, 
						p_calendrier.texte_court AS date_texte, 
						to_char(p_calendrier.date,'YYYYDDD')::bigint AS DATE_id ,
						p_calendrier.jour_semaine,
						COALESCE(nb_py0_1,0) AS nb_py0_1, 
						COALESCE(nb_py1_1,0) AS nb_py1_1, 
						COALESCE(nb_py2_1,0) AS nb_py2_1, 
						COALESCE(nb_py3_1,0) AS nb_py3_1, 
						COALESCE(nb_py0_2,0) AS nb_py0_2, 
						COALESCE(nb_py1_2,0) AS nb_py1_2, 
						COALESCE(nb_py2_2,0) AS nb_py2_2, 
						COALESCE(nb_py3_2,0) AS nb_py3_2
					FROM base.p_calendrier
					JOIN (
						SELECT
							PVCOMM2.numid, NDA,  CASE WHEN RESIDENT.NOMARI <> '' THEN RESIDENT.NOMARI ELSE RESIDENT.NOM END || ' ' || RESIDENT.PRENOM1 || '  (Date entrée : ' || to_char(DATE_ENTRE, 'DD/MM/YYYY') || ')' AS NOM
						FROM w_PVCOMM
						JOIN prod_corwin.PVCOMM2 ON C_PVCOMM = w_PVCOMM .code
						JOIN prod_corwin.RESIDENT ON PVCOMM2.NUMID = RESIDENT.NUMID
						WHERE PRESENT = 1 AND PVCOMM2.NUMID <> 0
						GROUP BY 1,2,3
						) subview ON 1 = 1
					LEFT JOIN 
						(
						SELECT 
							PVCOMM2.NUMID, DATE,
							SUM(CASE WHEN periode_type = '1' AND Pyx = 'PY0' THEN 1 ELSE 0 END) AS nb_py0_1,
							SUM(CASE WHEN periode_type = '1' AND Pyx = 'PY1' THEN 1 ELSE 0 END) AS nb_py1_1,
							SUM(CASE WHEN periode_type = '1' AND Pyx = 'PY2' THEN 1 ELSE 0 END) AS nb_py2_1,
							SUM(CASE WHEN periode_type = '1' AND Pyx = 'PY3' THEN 1 ELSE 0 END) AS nb_py3_1,
							SUM(CASE WHEN periode_type = '2' AND Pyx = 'PY0' THEN 1 ELSE 0 END) AS nb_py0_2,
							SUM(CASE WHEN periode_type = '2' AND Pyx = 'PY1' THEN 1 ELSE 0 END) AS nb_py1_2,
							SUM(CASE WHEN periode_type = '2' AND Pyx = 'PY2' THEN 1 ELSE 0 END) AS nb_py2_2,
							SUM(CASE WHEN periode_type = '2' AND Pyx = 'PY3' THEN 1 ELSE 0 END) AS nb_py3_2
						FROM w_PVCOMM
						JOIN prod_corwin.PVCOMM2 ON C_PVCOMM = w_PVCOMM .code
						WHERE PRESENT = 1 AND PVCOMM2.NUMID <> 0
						GROUP BY 1,2
						ORDER BY 1,2
						) subview2 ON p_calendrier.date = subview2.date AND subview.NUMID = subview2.NUMID
					WHERE p_calendrier.date BETWEEN '[PERIODE]' AND '[TOPERIODE]'
					ORDER BY 1,2



					]]></select>
				<FIELDS>
					
				</FIELDS>
			</SQL>
			
			<SQL>
				<select><![CDATA[
			
				
					CTISELECT_TREE
						GROUPBY1{date_id,, date_code,, date_texte}
						OPTIONS{}
						INDICATORS{
							SUM(nb_py0_1),,
							SUM(nb_py1_1),,
							SUM(nb_py2_1),,
							SUM(nb_py3_1),,
							SUM(nb_py0_2),,
							SUM(nb_py1_2),,
							SUM(nb_py2_2),,
							SUM(nb_py3_2),,
						}	
						INDICATORS_TEXT{
							MAX(jour_semaine::text),,
						}	
					FROM w_releve
					
							
					AFTER{
						
					}
					
					

					
				
					]]></select>
				<FIELDS>
					<INSERT name="CTISELECT_TREE_FIELDS"/>
					<FIELD name="PY01"/>
					<FIELD name="PY11"/>
					<FIELD name="PY21"/>
					<FIELD name="PY31"/>
					<FIELD name="PY02"/>
					<FIELD name="PY12"/>
					<FIELD name="PY22"/>
					<FIELD name="PY32"/>
					<FIELD name="JOUR"/>
				</FIELDS>
			</SQL>
			
	
		</QUERY>
		
		<QUERY label="">
			<SQL>
				<select><![CDATA[
			
				
					CTISELECT_TREE
						GROUPBY1{resident_id,, resident_code,, resident_texte}
						GROUPBY1MODE{[GROUPBY_TAB]}
						GROUPBY2{date_id,, date_code,, date_texte}
						OPTIONS{}
						INDICATORS{
							SUM(nb_py0_1),,
							SUM(nb_py1_1),,
							SUM(nb_py2_1),,
							SUM(nb_py3_1),,
							SUM(nb_py0_2),,
							SUM(nb_py1_2),,
							SUM(nb_py2_2),,
							SUM(nb_py3_2),,
						}	
						INDICATORS_TEXT{
							MAX(CASE 
								WHEN nb_py0_1 > 0 THEN 'PY0' 
								WHEN nb_py1_1 > 0 THEN 'PY1' 
								WHEN nb_py2_1 > 0 THEN 'PY2' 
								WHEN nb_py3_1 > 0 THEN 'PY3' 
								ELSE '' END),,
							MAX(CASE 
								WHEN nb_py0_2 > 0 THEN 'PY0' 
								WHEN nb_py1_2 > 0 THEN 'PY1' 
								WHEN nb_py2_2 > 0 THEN 'PY2' 
								WHEN nb_py3_2 > 0 THEN 'PY3' 
								ELSE '' END),,
							MAX(jour_semaine::text),,
						}	
					FROM w_releve
					
							
					AFTER{
						UPDATE w_table
						SET 
							indicateurs_text[1] = '',
							indicateurs_text[2] = '',
							indicateurs_text[3] = ''
						WHERE oid2 = -9999999
					}
					
					

				
				
					]]></select>
				<FIELDS>
					<INSERT name="CTISELECT_TREE_FIELDS"/>
					<FIELD name="PY01"/>
					<FIELD name="PY11"/>
					<FIELD name="PY21"/>
					<FIELD name="PY31"/>
					<FIELD name="PY02"/>
					<FIELD name="PY12"/>
					<FIELD name="PY22"/>
					<FIELD name="PY32"/>
					<FIELD name="FAC1"/>
					<FIELD name="FAC2"/>
					<FIELD name="JOUR"/>
				</FIELDS>
			</SQL>
	
		</QUERY>
		
		<QUERY type="propertiesLink" name="VIEWPROPERTIES" forRows="false">
		</QUERY>
	</QUERIES>
	<PRESENTATION>
	
		
		<ONGLET excelLabel="Synthèse période" label="Synthèse période"
			multiTab="true" multiTabBreakField="TABD" multiTabBreakLabelField="TABDL" multiTabBreakLonglabelField="TABDLL">
			<DATAGRID title="" total="true" sortable="false" showOthers="false" headerHeight="48"
						selectRowInRows="= ROW.OID == '-9999997'" 
						otherRowInRows="= ROW.OID == '-9999998'" 
						totalRowInRows="= ROW.OID == '-9999999'">

				<ROWSTYLE name="backgroundColor" value="0xbbbbbb" condition="ROW.LVL == '2' &amp;&amp; ROW.JOUR == '7'"/>
				<COLUMN dataField="JOUR" width="60" type="Char" visible="false" headerText="Oid">
				</COLUMN>
				<COLUMN dataField="OID" width="60" type="Char" visible="false" headerText="Oid">
				</COLUMN>
				<COLUMN dataField="COD" width="80" type="Char" visible="true" headerText="Code" textAlign="left" otherFunction="text" totalFunction="text">
					<INSERT name="COLUMN_COD_MULTITAB_STYLES"/>
				</COLUMN>
				<COLUMN dataField="TXT" type="Char" visible="true" minWidth="50" headerText="Texte"  textAlign="left"  >
				</COLUMN>

				<COLUMN dataField="PY01" 
					type="Number" 
					width="80"
					outputFormat="#"
					visible="true"
					headerText="Réalisé@LF@Matin@LF@PY0" 
					textAlign="right"
					totalComplement="*ROW" >
				</COLUMN>
				<COLUMN dataField="PY11" 
					type="Number" 
					width="80"
					outputFormat="#"
					visible="true"
					headerText="Réalisé@LF@Matin@LF@PY1" 
					textAlign="right"
					totalComplement="*ROW" >
				</COLUMN>
				<COLUMN dataField="PY21" 
					type="Number" 
					width="80"
					outputFormat="#"
					visible="true"
					headerText="Réalisé@LF@Matin@LF@PY2" 
					textAlign="right"
					totalComplement="*ROW" >
				</COLUMN>
				<COLUMN dataField="PY31" 
					type="Number" 
					width="80"
					outputFormat="#"
					visible="true"
					headerText="Réalisé@LF@Matin@LF@PY3" 
					textAlign="right"
					totalComplement="*ROW" >
				</COLUMN>
				<COLUMN dataField="PY02" 
					type="Number" 
					width="80"
					outputFormat="#"
					visible="true"
					headerText="Réalisé@LF@Après-midi@LF@PY0" 
					textAlign="right"
					totalComplement="*ROW" >
				</COLUMN>
				<COLUMN dataField="PY12" 
					type="Number" 
					width="80"
					outputFormat="#"
					visible="true"
					headerText="Réalisé@LF@Après-midi@LF@PY1" 
					textAlign="right"
					totalComplement="*ROW" >
				</COLUMN>
				<COLUMN dataField="PY22" 
					type="Number" 
					width="80"
					outputFormat="#"
					visible="true"
					headerText="Réalisé@LF@Après-midi@LF@PY2" 
					textAlign="right"
					totalComplement="*ROW" >
				</COLUMN>
				<COLUMN dataField="PY32" 
					type="Number" 
					width="80"
					outputFormat="#"
					visible="true"
					headerText="Réalisé@LF@Après-midi@LF@PY3" 
					textAlign="right"
					totalComplement="*ROW" >
				</COLUMN>
				
			</DATAGRID>
		</ONGLET>

		
		<ONGLET excelLabel="Relevé par résident" label="Relevé par résident"  queryNumber="1"
			multiTab="true" multiTabBreakField="TABD" multiTabBreakLabelField="TABDL" multiTabBreakLonglabelField="TABDLL">
			<DATAGRID title="" total="true" sortable="false" showOthers="false" headerHeight="48"
						selectRowInRows="= ROW.OID == '-9999997'" 
						otherRowInRows="= ROW.OID == '-9999998'" 
						totalRowInRows="= ROW.OID == '-9999999'">
				<INSERT name="DATAGRID_MULTITAB_STYLES"/>
				<ROWSTYLE name="backgroundColor" value="0xbbbbbb" condition="ROW.LVL == '2' &amp;&amp; ROW.JOUR == '7'"/>
				<COLUMN dataField="JOUR" width="60" type="Char" visible="false" headerText="Oid">
				</COLUMN>
				<COLUMN dataField="OID" width="60" type="Char" visible="false" headerText="Oid">
				</COLUMN>
				<COLUMN dataField="COD" width="80" type="Char" visible="true" headerText="Code" textAlign="left" otherFunction="text" totalFunction="text">
					<INSERT name="COLUMN_COD_MULTITAB_STYLES"/>
				</COLUMN>
				<COLUMN dataField="TXT" type="Char" visible="true" minWidth="50" headerText="Texte"  textAlign="left"  >
				</COLUMN>

				<COLUMN dataField="PY01" 
					type="Number" 
					width="80"
					outputFormat="#"
					visible="true"
					headerText="Réalisé@LF@Matin@LF@PY0" 
					textAlign="right"
					totalComplement="*ROW" >
				</COLUMN>
				<COLUMN dataField="PY11" 
					type="Number" 
					width="80"
					outputFormat="#"
					visible="true"
					headerText="Réalisé@LF@Matin@LF@PY1" 
					textAlign="right"
					totalComplement="*ROW" >
				</COLUMN>
				<COLUMN dataField="PY21" 
					type="Number" 
					width="80"
					outputFormat="#"
					visible="true"
					headerText="Réalisé@LF@Matin@LF@PY2" 
					textAlign="right"
					totalComplement="*ROW" >
				</COLUMN>
				<COLUMN dataField="PY31" 
					type="Number" 
					width="80"
					outputFormat="#"
					visible="true"
					headerText="Réalisé@LF@Matin@LF@PY3" 
					textAlign="right"
					totalComplement="*ROW" >
				</COLUMN>
				<COLUMN dataField="PY02" 
					type="Number" 
					width="80"
					outputFormat="#"
					visible="true"
					headerText="Réalisé@LF@Après-midi@LF@PY0" 
					textAlign="right"
					totalComplement="*ROW" >
				</COLUMN>
				<COLUMN dataField="PY12" 
					type="Number" 
					width="80"
					outputFormat="#"
					visible="true"
					headerText="Réalisé@LF@Après-midi@LF@PY1" 
					textAlign="right"
					totalComplement="*ROW" >
				</COLUMN>
				<COLUMN dataField="PY22" 
					type="Number" 
					width="80"
					outputFormat="#"
					visible="true"
					headerText="Réalisé@LF@Après-midi@LF@PY2" 
					textAlign="right"
					totalComplement="*ROW" >
				</COLUMN>
				<COLUMN dataField="PY32" 
					type="Number" 
					width="80"
					outputFormat="#"
					visible="true"
					headerText="Réalisé@LF@Après-midi@LF@PY3" 
					textAlign="right"
					totalComplement="*ROW" >
				</COLUMN>
				<COLUMN dataField="FAC1" 
					type="Char" 
					width="80"
					visible="true"
					headerText="A facturer@LF@Matin" 
					textAlign="center"
					totalComplement="*ROW" >
				</COLUMN>
				<COLUMN dataField="FAC2" 
					type="Char" 
					width="80"
					visible="true"
					headerText="A facturer@LF@Après-midi" 
					textAlign="center"
					totalComplement="*ROW" >
				</COLUMN>
			</DATAGRID>
		</ONGLET>
		
		
	</PRESENTATION>
</VUE>