<?xml version="1.0" encoding="ISO-8859-15"?>
<VUE name="ACTI000103" 
	label="Activité. Justificatif taux de facturation forfaits" 
	title="= 'Justificatif taux de facturation forfaits (' + SELECT.PERIODE_LABEL + ' à ' + SELECT.TOPERIODE_LABEL + ')'" 
	database="iCTI" swf="*CTI_view1" 
	softCode="iCTI_activite"
	globals="ACTI_globals.XML"
	componentsFile="ACTI_components.XML"
    helpDokuWIkiDir="activite:qualite:etudes:forfaits" 
    helpDokuWIkiFile="acti000102" 
    helpFile="iCTI_ACTI_ACTI000102.HTML"
>    
	
	<VIEWPROPERTIES>
		<PROPERTYx dataLink="VIEWPROPERTIES" nameField="name" valueField="value" />
		
		<PROPERTY name="RUBRIQUE_SELECT" >
				<value><![CDATA[
					[EVAL 

						$select_type = substr('[RUBRIQUE]',0,2);
						$select_oid = substr('[RUBRIQUE]',2);
						if ($select_type == "LS") {
							$section = ACTI_get_section_id('[RUBRIQUE]');
							$select_oid = ACTI_get_classe_oid('[RUBRIQUE]');
						}
						if ($select_type == 'LI') {return "AND rubrique_facturation_id IN (SELECT to_id FROM activite.t_listes_contenu WHERE liste_id = $select_oid)";}
						if ($select_type == 'LS') {return "AND rubrique_facturation_section_id[" . $section . "] = $select_oid";}
						if ($select_type == 'SL') {return "AND rubrique_facturation_id NOT IN (SELECT to_id FROM activite.t_listes_contenu WHERE liste_id = $select_oid)";}
						if ($select_type == 'RU') {return "AND rubrique_facturation_id = $select_oid";}
						if ('[RUBRIQUE]' == 'SAISIECODE') {
							return "AND rubrique_facturation_id IN (SELECT oid FROM activite.t_rubriques_facturation WHERE " . sql_codes_list_a('[RUBRIQUE_CODES]', 'code') . ")"; 
						}
						return "AND rubrique_facturation_id = -1";
					EVAL]
				]]></value>
		</PROPERTY>
		
		<PROPERTY name="JOURNEES_FIELD" value="[EVAL 
		
					$field = 'nb_jours';
					if ('[QJMUT]' == 'd') {$field = $field . '_d';}
					else {
						if ('[QJMUT]' == 'f') {$field = $field . '_f';}
						else {
							if ('[QJMUT]' == '12') {$field = $field . '_12';}
							else {
								$field = $field . '_f';
							}
						}
					}
					return $field;
				EVAL]" />
			
		<PROPERTY name="CP_FIELD" value="[EVAL 
		
					$field = 'nb_jours_cp';
					
					if ('[QJMUT]' == 'd') {$field = $field . '_d';}
					else {
						if ('[QJMUT]' == 'f') {$field = $field . '_f';}
						else {
							if ('[QJMUT]' == '12') {$field = $field . '_12';}
							else {
								$field = $field . '_f';
							}
						}
					}
					return $field;
				EVAL]" />
				
		<PROPERTY name="HAVING_CLAUSE" value="[EVAL 
		
					$clause = '';
					if ('[DATA]' == 'ECARTFF') {$clause = 'nb_jours_js_non_inclus';}
					
					return $clause;
				EVAL]" />

	</VIEWPROPERTIES>
	
	<SELECTIONS label="Paramètres de l'analyse" displayText="= var display:String = '';
						
						return display;
						" 
				>
		<GROUP>
			<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="2009-01-01" globalListProvider="DATE_DEBUT"   >
			</FIELD>
			<FIELD name="TOPERIODE" label="à" UI="combo" tree="true" width="200" default="2009-01-31" globalListProvider="DATE_FIN"  >
			</FIELD>
		</GROUP>
				
		
	</SELECTIONS>
	
	<SELECTIONS label="Modes de calcul" displayText="= var display:String = '';
						display = display + 'Rubriques : #B#' + SELECT.RUBRIQUE_LABEL + '#/B#\r';
						display = display + 'Modes de calcul : #B#' + VIEW.OPTADM_DAYOUT + '. ' + SELECT.QJMUT_LABEL  + '#/B#';
						return display;
						">
		<GROUP>
			<FIELD name="RUBRIQUE" label="Rubrique forfait" UI="combo" tree="true" width="400" default="-1" globalListProvider="RUBRIQUES" showIfNotDefault="true">
			</FIELD>
			<FIELD name="RUBRIQUE_CODES" label="Codes" UI="text" width="150" default="" showIfNotDefault="true" visible="= SELECT.RUBRIQUE == 'SAISIECODE' " />
		</GROUP>
		<GROUP label="Mode">
			<FIELD name="QJMUT" label="" UI="combo" width="300"  default="f" >
				<OPTION data="d" label="Si mutation, journée affectée à la premiere localisation" />
				<OPTION data="f" label="Si mutation, journée affectée à la dernière localisation" />
				<OPTION data="12" label="Si mutation, journée affectée à la localisation occupée à 12h" />
				<OPTION data="prorata" label="Si mutation, journée affectée au prorata du temps passé"/>
			</FIELD>
		</GROUP>
	</SELECTIONS>
	
	<SELECTIONS componentName="FILTRES_MOUVEMENTS_SELECTIONS" />
	
	<SELECTIONS componentName="FILTRES_SEJOURS_SELECTIONS" />
	
	<SELECTIONS componentName="FILTRES_PASSAGES_SELECTIONS" />
	

	<QUERIES>
	
		<QUERY label=""> 
			<SQL optimizer="V_SEJOURS_X" optimizer2="V_FACTURES_LIGNES_C_X">
				<select><![CDATA[
					DROP SEQUENCE IF EXISTS ACTI000102_facforf_rub_seq;
					CREATE TEMP SEQUENCE ACTI000102_facforf_rub_seq;
					
					DROP TABLE IF EXISTS acti000103_facforf_rub;
					CREATE TEMP TABLE acti000103_facforf_rub AS
					SELECT prestation_id, prestation_code, nextval('ACTI000102_facforf_rub_seq'::regclass) AS columnby_sequence, nb_forf_facturees
					FROM (	
						SELECT prestation_id, prestation_code,    						
							SUM(nb_rubrique * coefficient) AS nb_forf_facturees				
						FROM #V_FACTURES_LIGNES_C_X#
							JOIN #V_SEJOURS_X# ON v_sejours_1.no_sejour = v_factures_lignes_c_4.no_sejour
						WHERE 
							v_factures_lignes_c_4.date_fin BETWEEN '[PERIODE]' AND '[TOPERIODE]'
							[VIEW.SEJOURS_SELECT] 
							[VIEW.PASSAGES_SELECT]
							[VIEW.RUBRIQUE_SELECT]
						GROUP BY 1,2
						ORDER BY 2 
					) subview;
					
					CTISELECT_PROPERTY DISTINCT 'COLI' || columnby_sequence::text, CASE WHEN columnby_sequence <= 10 THEN prestation_id::text ELSE '-1' END FROM acti000103_facforf_rub				
					UNION 					
					CTISELECT_PROPERTY DISTINCT 'COLH' || columnby_sequence::text, prestation_code FROM acti000103_facforf_rub;
					
					DROP TABLE IF EXISTS acti000103_facforf;
					CREATE TEMP TABLE acti000103_facforf AS
					SELECT v_sejours_1.no_sejour, 
						v_factures_lignes_c_4.date_fin,
						SUM(nb_rubrique * coefficient) AS nb_forf_facturees,
						SUM(montant_facture + montant_encours) AS montant_forf_facturees,
						SUM(CASE WHEN acti000103_facforf_rub.columnby_sequence = 1 THEN nb_rubrique * coefficient ELSE 0 END) AS nb_forf_facturees_1,
						SUM(CASE WHEN acti000103_facforf_rub.columnby_sequence = 2 THEN nb_rubrique * coefficient ELSE 0 END) AS nb_forf_facturees_2,
						SUM(CASE WHEN acti000103_facforf_rub.columnby_sequence = 3 THEN nb_rubrique * coefficient ELSE 0 END) AS nb_forf_facturees_3,
						SUM(CASE WHEN acti000103_facforf_rub.columnby_sequence = 4 THEN nb_rubrique * coefficient ELSE 0 END) AS nb_forf_facturees_4,
						SUM(CASE WHEN acti000103_facforf_rub.columnby_sequence = 5 THEN nb_rubrique * coefficient ELSE 0 END) AS nb_forf_facturees_5 	
					FROM #V_FACTURES_LIGNES_C_X#
						JOIN #V_SEJOURS_X# ON v_sejours_1.no_sejour = v_factures_lignes_c_4.no_sejour
						JOIN acti000103_facforf_rub ON v_factures_lignes_c_4.prestation_id = acti000103_facforf_rub.prestation_id
					WHERE 
							v_factures_lignes_c_4.date_fin BETWEEN '[PERIODE]' AND '[TOPERIODE]'
							[VIEW.SEJOURS_SELECT]
							[VIEW.PASSAGES_SELECT]
					GROUP BY 1,2;
					
					ALTER TABLE acti000103_facforf ADD CONSTRAINT acti000103_facforf_pkey PRIMARY KEY(no_sejour, date_fin);
					
				]]></select>
				
			</SQL>
			<SQL optimizer="V_SEJOURS_X" optimizer2="V_MOUVEMENTS_SEJOUR_X">
				<select><![CDATA[	
				
					DROP TABLE IF EXISTS acti000103;
					CREATE TEMP TABLE acti000103 AS
					SELECT 
						v_mouvements_sejour_2.no_sejour, 
						nom || ' ' || prenom AS nom, 
						p_patients.date_naissance,
						date_entree, 
						CASE WHEN date_sortie<> '2099-12-31' THEN to_char(date_sortie,'YYYY-MM-DD') ELSE '' END::text AS date_sortie, 
						CASE WHEN date_sortie<> '2099-12-31' THEN heure_sortie / 10000 ELSE 0 END AS heure_sortie, 
						CASE WHEN v_sejours_1.date_sortie<> '2099-12-31' THEN v_sejours_1.date_sortie - v_sejours_1.date_entree ELSE 0 END AS duree_sejour, 
						lieu_sortie_etage_code,
						lieu_sortie_service_code,
						medecin_sejour_nom,

						SUM([VIEW.JOURNEES_FIELD] + nb_ambulatoires + nb_externes) AS nb_jou,
						
						SUM(nb_forf_facturees_1) AS nb_forf_facturees_1,
						SUM(nb_forf_facturees_2) AS nb_forf_facturees_2,
						SUM(nb_forf_facturees_3) AS nb_forf_facturees_3,
						SUM(nb_forf_facturees_4) AS nb_forf_facturees_4,
						SUM(nb_forf_facturees_5) AS nb_forf_facturees_5
						
					FROM #V_SEJOURS_X#
						JOIN #V_MOUVEMENTS_SEJOUR_X# ON v_mouvements_sejour_2.no_sejour = v_sejours_1.no_sejour
						JOIN activite.p_patients ON v_sejours_1.no_patient = p_patients.no_patient
						LEFT JOIN acti000103_facforf ON acti000103_facforf.no_sejour = v_mouvements_sejour_2.no_sejour AND
												acti000103_facforf.date_fin = v_mouvements_sejour_2.date
					WHERE 
							date BETWEEN '[PERIODE]' AND '[TOPERIODE]' AND
							[VIEW.JOURNEES_FIELD] + nb_ambulatoires + nb_externes <> 0
							[VIEW.SEJOURS_SELECT]
							[VIEW.PASSAGES_SELECT]
							[VIEW.MOUVEMENTS_SELECT] AND est_mouvement_previsionnel <> '1'
					GROUP BY 1,2,3,4,5,6,7,8,9,10
					ORDER BY no_sejour
						
				
				]]></select>
				
			</SQL>
			
			<SQL optimizer="V_SEJOURS_X" optimizer2="V_MOUVEMENTS_SEJOUR_X">
				<select><![CDATA[	
				
						(SELECT 
							'1'::text AS LVL,
							acti000103.*
						FROM acti000103
						ORDER BY no_sejour)
						
						UNION ALL
						
						(SELECT 
							'-9999999'::text AS LVL,
							'TOTAL', 
							COUNT(DISTINCT no_sejour)::text || ' séjours',
							null,
							null,
							null,
							0,
							0,
							'',
							'',
							'',

							SUM(nb_jou),
							
							SUM(nb_forf_facturees_1) AS nb_forf_facturees_1,
							SUM(nb_forf_facturees_2) AS nb_forf_facturees_2,
							SUM(nb_forf_facturees_3) AS nb_forf_facturees_3,
							SUM(nb_forf_facturees_4) AS nb_forf_facturees_4,
							SUM(nb_forf_facturees_5) AS nb_forf_facturees_5

						FROM acti000103
						)
						
				]]></select>
						
				
				<FIELDS>
					<FIELD name="LVL" />
					<FIELD name="SEJ" />
					<FIELD name="NOM" ifHideNames="Non autorisé" />
					<FIELD name="DNAT" />
					<FIELD name="DENT"/>
					<FIELD name="DSOR"/>
					<FIELD name="HSOR"/>
					<FIELD name="DUR"/>
					<FIELD name="ETA"/>
					<FIELD name="SER"/>
					<FIELD name="MED"/>
					<FIELD name="NBJRN"/>
					<FIELD name="NBFACF1"/>
					<FIELD name="NBFACF2"/>
					<FIELD name="NBFACF3"/>
					<FIELD name="NBFACF4"/>
					<FIELD name="NBFACF5"/>
				</FIELDS>
			</SQL>
			
		</QUERY>
		
		
		<QUERY type="propertiesLink" name="VIEWPROPERTIES" forRows="false"> 
			
			
			
		</QUERY>
		
		
	</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="48"
				selectRowInRows="= ROW.LVL == '-9999997'" otherRowInRows="= ROW.LVL == '-9999998'" totalRowInRows="= ROW.LVL == '-9999999'" >
				
				
				<COLUMN dataField="SEJ"
					width="100"
					type="Char"
					headerText="No séjour" 
					textAlign="left" 
					otherFunction="text"
					totalFunction="text"
					totalComplement="TOTAL" >
				</COLUMN>
				<COLUMN dataField="NOM" 
					type="Char"
					minWidth="150"
					headerText="Nom" 
					textAlign="left" 
					totalFunction="text" 
					totalComplement="*ROW">
				</COLUMN>
				<COLUMN dataField="DNAT" 
					width="60"
					type="Date" 
					inputFormat="AAAA-MM-JJ"
					outputFormat="DD/MM/YY"
					headerText="Date nais." 
					textAlign="right" 
					totalFunction="text">
				</COLUMN>
				<COLUMN dataField="DENT" 
					width="60"
					type="Date" 
					inputFormat="AAAA-MM-JJ"
					outputFormat="DD/MM/YY"
					headerText="Date entrée" 
					textAlign="right" 
					totalFunction="text">
				</COLUMN>
				<COLUMN dataField="DSOR" 
					width="60"
					type="Date" 
					inputFormat="AAAA-MM-JJ"
					outputFormat="DD/MM/YY"
					headerText="Date sortie" 
					textAlign="right" 
					totalFunction="text">
				</COLUMN>
				<COLUMN dataField="HSOR" 
					width="60"
					type="Number" 
					outputFormat="#,00"
					headerText="Heure sortie" 
					textAlign="right" 
					totalFunction="text">
				</COLUMN>
				<COLUMN dataField="DUR" 
					type="Number"
					width="50"
					outputFormat="#"
					fixed="false"
					headerText="Durée" 
					textAlign="right" 
					totalFunction="text" >
				</COLUMN>
				<COLUMN dataField="ETA" 
					type="Char"
					width="70"
					fixed="false"
					headerText="Etage" 
					textAlign="left" 
					totalFunction="text" >
				</COLUMN>
				
				<COLUMN dataField="SER" 
					type="Char"
					width="70"
					fixed="false"
					headerText="Serv. fact." 
					textAlign="left" 
					totalFunction="text" >
				</COLUMN>
				<COLUMN dataField="MED" 
					type="Char"
					minWidth="80"
					maxWidth="150"
					fixed="false"
					headerText="Médecin" 
					textAlign="left" 
					totalFunction="text"  >
				</COLUMN>
				

				<COLUMN dataField="NBJRN" width="60" type="Number" selectable="true" visible="true" outputFormat="#" fixed="false" headerText="Nb total jours" textAlign="right" totalFunction="sum" otherFunction="sum">
				</COLUMN>
				
				
				<COLUMN dataField="NBFACF1" width="80" type="Number" outputFormat="#" fixed="false" visibleCondition="VIEW.COLH1 != null" headerText="'NB facturé\r' + VIEW.COLH1" columnData="= VIEW.COLI1" headerTextCalc="true" textAlign="right" totalFunction="sum" otherFunction="sum">
				</COLUMN>
				<COLUMN dataField="NBFACF2" width="80" type="Number" outputFormat="#" fixed="false" visibleCondition="VIEW.COLH2 != null" headerText="'NB facturé\r' + VIEW.COLH2" columnData="= VIEW.COLI2" headerTextCalc="true" textAlign="right" totalFunction="sum" otherFunction="sum">
				</COLUMN>
				<COLUMN dataField="NBFACF3" width="80" type="Number" outputFormat="#" fixed="false" visibleCondition="VIEW.COLH3 != null" headerText="'NB facturé\r' + VIEW.COLH3" columnData="= VIEW.COLI3" headerTextCalc="true" textAlign="right" totalFunction="sum" otherFunction="sum">
				</COLUMN>
				<COLUMN dataField="NBFACF4" width="80" type="Number" outputFormat="#" fixed="false" visibleCondition="VIEW.COLH4 != null" headerText="'NB facturé\r' + VIEW.COLH4" columnData="= VIEW.COLI4" headerTextCalc="true" textAlign="right" totalFunction="sum" otherFunction="sum">
				</COLUMN>
				<COLUMN dataField="NBFACF5" width="80" type="Number" outputFormat="#" fixed="false" visibleCondition="VIEW.COLH5 != null" headerText="'NB facturé\r' + VIEW.COLH5" columnData="= VIEW.COLI5" headerTextCalc="true" textAlign="right" totalFunction="sum" otherFunction="sum">
				</COLUMN>
			</DATAGRID>
		
		</ONGLET>
		
				
				
	</PRESENTATION>
	
</VUE>

			
