<?xml version="1.0" encoding="ISO-8859-1"?>
<VUE name="PMSI000269" label="PMSI. Impact Théorique CMA" title="= 'Impact Théorique CMA par ' +  SELECT.GROUPBY_LABEL + ' (' + 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:sejours_analyse_globale:severites" helpDokuWIkiFile="pmsi000269"  helpFile="iCTI_PMSI_PMSI000269.HTML">
	<VIEWPROPERTIES componentName="PROPERTIES_RSS" />
	<VIEWPROPERTIES componentName="PROPERTIES_SELECT_TABTREE" />
	<VIEWPROPERTIES>
		<PROPERTY dataLink="VIEWPROPERTIES" nameField="name" valueField="value" />
		<PROPERTY name="FILTRE_DIAG_RSS_SELECT">
			<value><![CDATA[[EVAL 
			
				$select = "";
				
				$mode = substr('[DIAG]',0,2);
				$oid = substr('[DIAG]',2);
				if ($mode == 'LS') {
                    $section_id = PMSI_get_section_id('[DIAG]');
                    $oid = PMSI_get_classe_oid('[DIAG]');
				}
				$diag_select = '';
					
				if ('[DIAG]' != '-1') {
					if ($mode == 'LI') {$diag_select = "diagnostic_id IN (SELECT to_id FROM pmsi.t_listes_contenu WHERE liste_id = $oid)";}
					if ($mode == 'SL') {$diag_select = "diagnostic_id IN (SELECT to_id FROM pmsi.t_listes_contenu WHERE liste_id = $oid)";}
					if ($mode == 'DG') {$diag_select = "diagnostic_id = $oid ";}
					if ($mode == 'DR') {$diag_select = "diagnostic_id IN (SELECT oid FROM pmsi.t_diagnostics WHERE racine_id = $oid)";}
					if ($mode == 'LS') {$diag_select = "diagnostic_id IN (SELECT oid FROM pmsi.t_diagnostics WHERE $section_id = $oid)";}
					if ($mode == 'C*') {$diag_select = "diagnostic_id IN (SELECT oid FROM pmsi.t_diagnostics WHERE code ILIKE ANY (base.cti_select_codes('$oid')))";}
					
					$diag_select =  "$diag_select AND type_diagnostic IN ('DA', 'DS') ";

					if ($diag_select != '') {
						if ($mode != 'SL') {
							$select = $select. " AND v_rss_1.rss_id IN (SELECT rss_id FROM pmsi.p_rss_diagnostics WHERE $diag_select)";
						}
						else {
							$select = $select. " AND v_rss_1.rss_id NOT IN (SELECT rss_id FROM pmsi.p_rss_diagnostics WHERE $diag_select)";
						}
					}
				}
				else {
					$select = " AND v_rss_1.rss_id IS DISTINCT FROM v_rss_1.rss_id";
				}
				
				return $select;
					
				
			EVAL]]]></value>
		</PROPERTY>
		
		<PROPERTY name="FILTRE_DIAG_SELECT">
			<value><![CDATA[[EVAL 
			
				$select = "";
				
				$mode = substr('[DIAG]',0,2);
				$oid = substr('[DIAG]',2);
				if ($mode == 'LS') {
                    $section_id = PMSI_get_section_id('[DIAG]');
                    $oid = PMSI_get_classe_oid('[DIAG]');
				}
				$diag_select = '';
					
				if ('[DIAG]' != '-1') {
					if ($mode == 'LI') {$diag_select = "diagnostic_id IN (SELECT to_id FROM pmsi.t_listes_contenu WHERE liste_id = $oid)";}
					if ($mode == 'SL') {$diag_select = "diagnostic_id IN (SELECT to_id FROM pmsi.t_listes_contenu WHERE liste_id = $oid)";}
					if ($mode == 'DG') {$diag_select = "diagnostic_id = $oid ";}
					if ($mode == 'DR') {$diag_select = "diagnostic_id IN (SELECT oid FROM pmsi.t_diagnostics WHERE racine_id = $oid)";}
					if ($mode == 'LS') {$diag_select = "diagnostic_id IN (SELECT oid FROM pmsi.t_diagnostics WHERE $section_id = $oid)";}
					if ($mode == 'C*') {$diag_select = "diagnostic_id IN (SELECT oid FROM pmsi.t_diagnostics WHERE code ILIKE ANY (base.cti_select_codes('$oid')))";}
					

					
				}
				else {
					$diag_select = " diagnostic_id IS DISTINCT FROM diagnostic_id";
				}
				
				return $diag_select;
					
				
			EVAL]]]></value>
		</PROPERTY>
		
		<PROPERTY name="SORTBY">
			<value><![CDATA[[EVAL 
			
				if ('[GROUPBY_2]' != '-1' && '[GROUPBY]' != '-1' && '[GROUPBY_TAB]' != 'TAB') {
					return 'ORDER BY 5,4,7,6';
				}
				else {
					return 'ORDER BY 12,13 DESC,5,7';
				}
			EVAL]]]></value>
		</PROPERTY>
		
	</VIEWPROPERTIES>
	<SELECTIONS label="Paramètres de l'analyse" displayText="= var display:String = '';
					if (SELECT.DIAG  != '-1') {
						display = display + '#B#Analyse diagnostics#/B# : ' + SELECT.T_DIAG_LABEL + ' : #B#' + SELECT.DIAG_LABEL + '#/B#';
					}
					display = display + ' (Cette étude s appuie uniquement sur les sévérités des CMA sans tenir compte du groupeur)';
					return display;
					">
		<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" printable="false" globalListProvider="DATE_DEBUT" />
			<FIELD name="TOPERIODE" label="à" UI="combo" tree="true" width="200" default="2014-12-31" printable="false" globalListProvider="DATE_FIN" />
		</GROUP>
		<GROUP label="Diagnostic (CMA) analysé">
			<FIELD name="DIAG" label="" UI="combo" tree="true" width="400" default="" globalListProvider="DIAG" >
			</FIELD>
		</GROUP>
		<GROUP label="Regroupements">
			<FIELD name="GROUPBY" width="220" label="" UI="combo" default="4" tree="true">
				<OPTION label="Options" data="" globalListProvider="RSS_GROUPBY_OPTIONS" />
			</FIELD>
			<FIELD name="GROUPBY_TAB" width="100" label="Présentation" UI="combo" default="AUTO" visible="= SELECT.GROUPBY_2 != '-1' ">
				<OPTION label="Automatique" data="AUTO" />
				<OPTION label="Arbre" data="TREE" />
				<OPTION label="Onglet" data="TAB" />
			</FIELD>
		</GROUP>
		<GROUP label="">
			<FIELD name="GROUPBY_2" width="220" label="" UI="combo" default="-1" tree="true">
				<OPTION label="Pas de second niveau" data="-1" />
				<OPTION label="Options" data="" globalListProvider="RSS_GROUPBY_OPTIONS" />
			</FIELD>
		</GROUP>		
        <GROUP label="">
			<FIELD name="GROUPBY_3" width="220" label="" UI="combo" default="-1" tree="true">
				<OPTION label="Pas de troisième niveau" data="-1" />
				<OPTION label="Options" data="" globalListProvider="RSS_GROUPBY_OPTIONS" />
			</FIELD>
		</GROUP>

	</SELECTIONS>
	<SELECTIONS componentName="FILTRES_RSS_SOUSSELECTIONS" />
	<SELECTIONS componentName="FILTRES_RSS_SELECTIONS" />
	<QUERIES>
		<QUERY label="">
			<SQL optimizer="V_RSS_X">
				<select><![CDATA[ 
					
					DROP TABLE IF EXISTS w_analyse;
					CREATE TEMP TABLE w_analyse AS
					SELECT
						rss_id AS anz_rss_id,
						date_sortie AS anz_date_sortie,	
						date_sortie - date_entree AS anz_duree_sejour,
						ghm_id AS anz_ghm_id,
						ghm_code AS anz_ghm_code,
						ghs_id AS anz_ghs_id,
						ghs_code AS anz_ghs_code,
						'0'::text AS anz_max_diag_severite,
						0::numeric AS coefficient_mco,
						0::numeric AS anz_montant_ghs,
						0::numeric AS anz_prix_unitaire,

						ghm_id AS anz_ghm_id_sans,
						ghm_code AS anz_ghm_code_sans,
						ghs_id AS anz_ghs_id_sans,
						ghs_code AS anz_ghs_code_sans,
						0::numeric AS anz_montant_ghs_sans,
						'0'::text AS anz_max_diag_severite_sans,
						0::numeric AS anz_prix_unitaire_sans,
						0::numeric AS anz_borne_basse_sans,
						0::numeric AS anz_forfait_exb_sans,
						0::numeric AS anz_tarif_exb_sans
					FROM 
						#V_RSS_X#
					WHERE 
						mois_sortie IN ([VIEW.PERIODE_MOIS_LIST]) 
						AND date_sortie BETWEEN '[PERIODE]' AND '[TOPERIODE]'
						[VIEW.FILTRE_RSS_SELECT]
						[VIEW.FILTRE_DIAG_RSS_SELECT]
							
					;



					CREATE INDEX w_analyse_i1 ON w_analyse USING btree (anz_rss_id);



					UPDATE w_analyse
					SET anz_montant_ghs = subview.montant_ghs, anz_prix_unitaire = subview.prix_unitaire, coefficient_mco = subview.coefficient_mco
					FROM (
						SELECT
							rss_id,
							SUM(base_remboursement) AS montant_ghs,
							MAX(CASE WHEN p_rsf_detail.coefficient_mco <> 0 THEN p_rsf_detail.coefficient_mco ELSE 1 END) AS coefficient_mco,
							SUM(prix_unitaire) AS prix_unitaire
						FROM pmsi.p_rsf_detail
						JOIN pmsi.t_prestations ON t_prestations.oid = prestation_id AND t_prestations.code = 'GHS'
						JOIN w_analyse ON anz_rss_id = rss_id
						WHERE est_ligne_rss = '1'
						GROUP BY 1
					) subview
					WHERE rss_id = anz_rss_id;


					UPDATE w_analyse
					SET anz_max_diag_severite = subview.diagnostic_severite_code, anz_max_diag_severite_sans = subview.diagnostic_severite_code_sans
					FROM (
						SELECT
							rss_id,
							MAX(
								CASE 
								WHEN anz_date_sortie < '20100301' THEN
									CASE WHEN type_diagnostic_rss IN ('DA', 'DS') AND diagnostic_severite_fg11_code IN ('2', '3', '4') THEN diagnostic_severite_fg11_code ELSE '1' END
								WHEN anz_date_sortie < '20110301' THEN
									CASE WHEN type_diagnostic_rss IN ('DA', 'DS') AND diagnostic_severite_fg12_code IN ('2', '3', '4') THEN diagnostic_severite_fg12_code ELSE '1' END
								WHEN anz_date_sortie < '20120301' THEN
									CASE WHEN type_diagnostic_rss IN ('DA', 'DS') AND diagnostic_severite_fg13_code IN ('2', '3', '4') THEN diagnostic_severite_fg13_code ELSE '1' END
								WHEN anz_date_sortie < '20130301' THEN
									CASE WHEN type_diagnostic_rss IN ('DA', 'DS') AND diagnostic_severite_fg1311d_code IN ('2', '3', '4') THEN diagnostic_severite_fg1311d_code ELSE '1' END
								ELSE
									CASE WHEN type_diagnostic_rss IN ('DA', 'DS') AND diagnostic_severite_fg1311e_code IN ('2', '3', '4') THEN diagnostic_severite_fg1311e_code ELSE '1' END
								END) AS diagnostic_severite_code,

							MAX(
								CASE 
								WHEN [VIEW.FILTRE_DIAG_SELECT] THEN '1'
								WHEN anz_date_sortie < '20100301' THEN
									CASE WHEN type_diagnostic_rss IN ('DA', 'DS') AND diagnostic_severite_fg11_code IN ('2', '3', '4') THEN diagnostic_severite_fg11_code ELSE '1' END
								WHEN anz_date_sortie < '20110301' THEN
									CASE WHEN type_diagnostic_rss IN ('DA', 'DS') AND diagnostic_severite_fg12_code IN ('2', '3', '4') THEN diagnostic_severite_fg12_code ELSE '1' END
								WHEN anz_date_sortie < '20120301' THEN
									CASE WHEN type_diagnostic_rss IN ('DA', 'DS') AND diagnostic_severite_fg13_code IN ('2', '3', '4') THEN diagnostic_severite_fg13_code ELSE '1' END
								WHEN anz_date_sortie < '20130301' THEN
									CASE WHEN type_diagnostic_rss IN ('DA', 'DS') AND diagnostic_severite_fg1311d_code IN ('2', '3', '4') THEN diagnostic_severite_fg1311d_code ELSE '1' END
								ELSE
									CASE WHEN type_diagnostic_rss IN ('DA', 'DS') AND diagnostic_severite_fg1311e_code IN ('2', '3', '4') THEN diagnostic_severite_fg1311e_code ELSE '1' END
								END) AS diagnostic_severite_code_sans
						FROM pmsi.v_rss_diagnostics_1
						JOIN w_analyse ON anz_rss_id = rss_id
						WHERE type_diagnostic_rss IN ('DA', 'DS')
						GROUP BY 1
					) subview
					WHERE rss_id = anz_rss_id;


					UPDATE w_analyse 
					SET anz_ghm_code_sans = substr(anz_ghm_code,1,5) || anz_max_diag_severite_sans, anz_ghs_code_sans = 0, anz_ghs_id_sans = 0
					WHERE anz_max_diag_severite > anz_max_diag_severite_sans AND substr(anz_ghm_code,6,6) = anz_max_diag_severite;


					UPDATE w_analyse 
					SET anz_ghm_id_sans = t_ghm_c.oid
					FROM pmsi.t_ghm_c
					WHERE anz_ghm_code_sans = t_ghm_c.code AND anz_ghm_id_sans IS DISTINCT FROM t_ghm_c.oid;


					DROP TABLE IF EXISTS w_ghm_ghs;
					CREATE TEMP TABLE w_ghm_ghs AS
					SELECT ghm_id, (MAX(Array[nb,ghs_id]))[2] AS ghs_id
					FROM
					(
					SELECT ghm_id, ghs_id, count(*) AS nb
					FROM pmsi.p_rss
					GROUP BY 1,2
					)
					subview
					GROUP BY 1;

					
					UPDATE w_analyse 
					SET anz_ghs_id_sans = w_ghm_ghs.ghs_id, anz_ghs_code_sans = t_ghs.code
					FROM w_ghm_ghs 
					JOIN pmsi.t_ghs ON w_ghm_ghs.ghs_id = t_ghs.oid
					WHERE w_analyse.anz_ghm_id_sans = w_ghm_ghs.ghm_id AND 
						anz_ghs_id_sans = 0 ;


					UPDATE w_analyse 
					SET anz_ghs_id_sans = t_ghs.oid, anz_ghs_code_sans = t_ghs.code
					FROM pmsi.t_ghs
					WHERE w_analyse.anz_ghm_id_sans = t_ghs.ghm_id AND 
						anz_ghs_id_sans = 0 ;



					UPDATE w_analyse 
					SET 
						anz_prix_unitaire_sans = 
							CASE 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_1 AND anz_date_sortie <= t_ghs.date_fin_prive_1 THEN t_ghs.tarif_ghs_prive_1 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_2 AND anz_date_sortie <= t_ghs.date_fin_prive_2 THEN t_ghs.tarif_ghs_prive_2 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_3 AND anz_date_sortie <= t_ghs.date_fin_prive_3 THEN t_ghs.tarif_ghs_prive_3 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_4 AND anz_date_sortie <= t_ghs.date_fin_prive_4 THEN t_ghs.tarif_ghs_prive_4 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_5 AND anz_date_sortie <= t_ghs.date_fin_prive_5 THEN t_ghs.tarif_ghs_prive_5 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_6 AND anz_date_sortie <= t_ghs.date_fin_prive_6 THEN t_ghs.tarif_ghs_prive_6 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_7 AND anz_date_sortie <= t_ghs.date_fin_prive_7 THEN t_ghs.tarif_ghs_prive_7 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_8 AND anz_date_sortie <= t_ghs.date_fin_prive_8 THEN t_ghs.tarif_ghs_prive_8 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_9 AND anz_date_sortie <= t_ghs.date_fin_prive_9 THEN t_ghs.tarif_ghs_prive_9 
							ELSE 0::numeric 
							END * coefficient_mco,
						anz_borne_basse_sans = 
							CASE 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_1 AND anz_date_sortie <= t_ghs.date_fin_prive_1 THEN t_ghs.borne_basse_prive_1 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_2 AND anz_date_sortie <= t_ghs.date_fin_prive_2 THEN t_ghs.borne_basse_prive_2 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_3 AND anz_date_sortie <= t_ghs.date_fin_prive_3 THEN t_ghs.borne_basse_prive_3 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_4 AND anz_date_sortie <= t_ghs.date_fin_prive_4 THEN t_ghs.borne_basse_prive_4 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_5 AND anz_date_sortie <= t_ghs.date_fin_prive_5 THEN t_ghs.borne_basse_prive_5 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_6 AND anz_date_sortie <= t_ghs.date_fin_prive_6 THEN t_ghs.borne_basse_prive_6 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_7 AND anz_date_sortie <= t_ghs.date_fin_prive_7 THEN t_ghs.borne_basse_prive_7 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_8 AND anz_date_sortie <= t_ghs.date_fin_prive_8 THEN t_ghs.borne_basse_prive_8 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_9 AND anz_date_sortie <= t_ghs.date_fin_prive_9 THEN t_ghs.borne_basse_prive_9 
							ELSE 0::numeric 
							END,
						anz_forfait_exb_sans = 
							CASE 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_1 AND anz_date_sortie <= t_ghs.date_fin_prive_1 THEN t_ghs.forfait_exb_prive_1 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_2 AND anz_date_sortie <= t_ghs.date_fin_prive_2 THEN t_ghs.forfait_exb_prive_2 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_3 AND anz_date_sortie <= t_ghs.date_fin_prive_3 THEN t_ghs.forfait_exb_prive_3 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_4 AND anz_date_sortie <= t_ghs.date_fin_prive_4 THEN t_ghs.forfait_exb_prive_4 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_5 AND anz_date_sortie <= t_ghs.date_fin_prive_5 THEN t_ghs.forfait_exb_prive_5 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_6 AND anz_date_sortie <= t_ghs.date_fin_prive_6 THEN t_ghs.forfait_exb_prive_6 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_7 AND anz_date_sortie <= t_ghs.date_fin_prive_7 THEN t_ghs.forfait_exb_prive_7 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_8 AND anz_date_sortie <= t_ghs.date_fin_prive_8 THEN t_ghs.forfait_exb_prive_8 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_9 AND anz_date_sortie <= t_ghs.date_fin_prive_9 THEN t_ghs.forfait_exb_prive_9 
							ELSE 0::numeric 
							END * coefficient_mco,
						anz_tarif_exb_sans = 
							CASE 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_1 AND anz_date_sortie <= t_ghs.date_fin_prive_1 THEN t_ghs.tarif_exb_prive_1 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_2 AND anz_date_sortie <= t_ghs.date_fin_prive_2 THEN t_ghs.tarif_exb_prive_2 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_3 AND anz_date_sortie <= t_ghs.date_fin_prive_3 THEN t_ghs.tarif_exb_prive_3 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_4 AND anz_date_sortie <= t_ghs.date_fin_prive_4 THEN t_ghs.tarif_exb_prive_4 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_5 AND anz_date_sortie <= t_ghs.date_fin_prive_5 THEN t_ghs.tarif_exb_prive_5 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_6 AND anz_date_sortie <= t_ghs.date_fin_prive_6 THEN t_ghs.tarif_exb_prive_6 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_7 AND anz_date_sortie <= t_ghs.date_fin_prive_7 THEN t_ghs.tarif_exb_prive_7 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_8 AND anz_date_sortie <= t_ghs.date_fin_prive_8 THEN t_ghs.tarif_exb_prive_8 
							WHEN anz_date_sortie >= t_ghs.date_debut_prive_9 AND anz_date_sortie <= t_ghs.date_fin_prive_9 THEN t_ghs.tarif_exb_prive_9 
							ELSE 0::numeric 
							END * coefficient_mco 
					FROM pmsi.t_ghs
					WHERE w_analyse.anz_ghs_id_sans = t_ghs.oid AND  anz_ghm_code <> anz_ghm_code_sans ;



					UPDATE w_analyse
					SET anz_montant_ghs_sans = CASE WHEN anz_montant_ghs <> 0 AND anz_ghm_code <> anz_ghm_code_sans THEN anz_prix_unitaire_sans ELSE anz_montant_ghs END;

					UPDATE w_analyse
					SET anz_montant_ghs_sans =
						CASE 
							WHEN anz_forfait_exb_sans <> 0 THEN anz_montant_ghs_sans - anz_forfait_exb_sans
							WHEN anz_tarif_exb_sans <> 0 AND anz_duree_sejour = 0 THEN round(anz_tarif_exb_sans / 2,2)
							WHEN anz_tarif_exb_sans <> 0 THEN anz_tarif_exb_sans - ((anz_borne_basse_sans - anz_duree_sejour) * anz_tarif_exb_sans)
							ELSE anz_montant_ghs_sans END
					WHERE anz_ghm_code <> anz_ghm_code_sans AND anz_duree_sejour < anz_borne_basse_sans AND
						anz_montant_ghs_sans <> 0;
					
					
				]]></select>
				
			</SQL>
			<SQL optimizer="V_RSS_X">
				<select><![CDATA[ 
					DROP TABLE IF EXISTS w_tmp;
					CREATE TEMP TABLE w_tmp AS
						SELECT 
							[VIEW.SELECT_TABTREE_COLUMNS],
							0::bigint AS sort_field,
							SUM(v_rss_1.nb_rsa) AS nb_rsa,
							SUM(CASE WHEN anz_ghm_code = anz_ghm_code_sans THEN 1 ELSE 0 END) AS nb_rsa_0,
							SUM(CASE WHEN anz_ghm_code IS DISTINCT FROM anz_ghm_code_sans THEN 1 ELSE 0 END) AS nb_rsa_1,	
							SUM(anz_montant_ghs) AS montant_ghs,
							SUM(anz_montant_ghs_sans) AS montant_ghs_sans,
							SUM(anz_montant_ghs - anz_montant_ghs_sans) AS ecart_ghs
						FROM 
							#V_RSS_X#
							JOIN w_analyse ON v_rss_1.rss_id = w_analyse.anz_rss_id
							JOIN pmsi.t_ghm_indicateurs_nationaux ghm_nat ON ghm_nat.ghm_id = v_rss_1.ghm_id, 
							[VIEW.SELECT_TABTREE_VIEW]
						WHERE 
							mois_sortie IN ([VIEW.PERIODE_MOIS_LIST]) 
							[VIEW.SELECT_TABTREE_WHERE]
							
						GROUP BY 1,2,3,4,5,6,7,8,9,10,11
					;
					
					
					DROP TABLE IF EXISTS w_sort;
					CREATE TEMP TABLE w_sort AS
						SELECT 
							0::bigint as sort_sequence, 
							w_tmp.cod1 as sort_code, 
							w_tmp.nb_rsa as sort_value 
						FROM
							w_tmp
						WHERE cod = cod1 AND oid <> -9999999
						ORDER BY sort_value DESC
					;
					
					DROP SEQUENCE IF EXISTS w_sort_sequence;
					CREATE TEMP SEQUENCE w_sort_sequence;
					
					UPDATE w_sort
					SET 
						sort_sequence = nextval('w_sort_sequence'::regclass)
					;
					
					
					UPDATE w_tmp
					SET 
						sort_field = w_sort.sort_sequence
					FROM
						w_sort
					WHERE
						w_sort.sort_code = w_tmp.cod1
					;
					
					
					SELECT * FROM w_tmp
					[VIEW.SORTBY];
					
				]]></select>
				<FIELDS>
					<FIELD name="TABD" />
					<FIELD name="TABDL" />
					<FIELD name="TABDLL" />
					<FIELD name="OID1" />
					<FIELD name="COD1" />
					<FIELD name="OID2" />
					<FIELD name="COD2" />
					<FIELD name="OID" />
					<FIELD name="COD" />
					<FIELD name="TXT" />
					<FIELD name="LVL" />
					<FIELD name="SORT_FIELD" />
					<FIELD name="NB_RSA" />
					<FIELD name="NB_RSA0" />
					<FIELD name="NB_RSA1" />
					<FIELD name="MGHS" />
					<FIELD name="MGHSS" />
					<FIELD name="MGHSE" />
				</FIELDS>
			</SQL>
		</QUERY>
		<QUERY type="propertiesLink" name="VIEWPROPERTIES" forSelections="false">
			<SQL>
				<select><![CDATA[
						SELECT 'X_PERIODE_D', greatest(to_date(to_char('[PERIODE]'::date - INTERVAL  '1 year', 'YYYY-MM-DD'), 'YYYY-MM-DD'), (SELECT min(date_debut) FROM pmsi.p_imports))::text
					UNION ALL
						SELECT 'X_TOPERIODE_D', greatest(to_date(to_char('[TOPERIODE]'::date - INTERVAL  '1 year', 'YYYY-MM-DD'), 'YYYY-MM-DD'), (SELECT min(date_debut) FROM pmsi.p_imports))::text
					UNION ALL
						SELECT 'X_PERIODE', greatest(to_date(to_char('[PERIODE]'::date - INTERVAL  '3 year', 'YYYY-MM-DD'), 'YYYY-MM-DD'), (SELECT min(date_debut) FROM pmsi.p_imports))::text
				]]></select>
				<FIELDS>
					<FIELD name="name" />
					<FIELD name="value" />
				</FIELDS>
			</SQL>
		</QUERY>
	</QUERIES>
	<PRESENTATION>
		<VIEWLINKS>
			
			<VIEWLINK label="Liste des RSS" shortLabel="Liste RSS" view="PMSI000270.XML" componentName="RSS_BASE_VIEWLINK">
				
			</VIEWLINK>
		</VIEWLINKS>
		<ONGLET excelLabel="Impact CMA" label="Impact CMA" multiTab="true" multiTabBreakField="TABD" multiTabBreakLabelField="TABDL" multiTabBreakLonglabelField="TABDLL">
			
			<DATAGRID title="" total="true" showOthers="true" headerHeight="36" variableRowHeight="true" selectRowInRows="= ROW.OID == '-9999997'" otherRowInRows="= ROW.OID == '-9999998'" totalRowInRows="= ROW.OID == '-9999999'"
				sortable="= SELECT.GROUPBY_3 == '-1' &amp;&amp; (SELECT.GROUPBY_2 == '-1' || SELECT.GROUPBY_TAB == 'TAB')">
				<ROWSTYLE name="fontSize" value="11" />
				<FOOTERROWSTYLE name="fontSize" value="11" />
				<ROWSTYLE name="rowHeight" value="20" condition="ROW.LVL == '1' &amp;&amp; SELECT.GROUPBY_2 != '-1' &amp;&amp; SELECT.GROUPBY_TAB != 'TAB'" />
				<ROWSTYLE name="backgroundColor" value="0xFFD393" condition="ROW.LVL == '1' &amp;&amp; SELECT.GROUPBY_2 != '-1' &amp;&amp; SELECT.GROUPBY_TAB != 'TAB'" />
				<ROWSTYLE name="fontWeight" value="bold" condition="ROW.LVL == '1' &amp;&amp; SELECT.GROUPBY_2 != '-1' &amp;&amp; SELECT.GROUPBY_TAB != 'TAB'" />
				<ROWSTYLE name="borderSides" value="top bottom" condition="ROW.LVL == '1' &amp;&amp; SELECT.GROUPBY_2 != '-1' &amp;&amp; SELECT.GROUPBY_TAB != 'TAB'" />
				<ROWSTYLE name="borderStyle" value="solid" condition="ROW.LVL == '1' &amp;&amp; SELECT.GROUPBY_2 != '-1' &amp;&amp; SELECT.GROUPBY_TAB != 'TAB'" />
				<ROWSTYLE name="borderThickness" value="2" condition="ROW.LVL == '1' &amp;&amp; SELECT.GROUPBY_2 != '-1' &amp;&amp; SELECT.GROUPBY_TAB != 'TAB'" />
				<COLUMN dataField="LVL" width="50" type="Char" visible="false" headerText="Code" textAlign="left" otherFunction="text" totalFunction="text" />
				<COLUMN dataField="OID1" width="60" type="Char" visible="false" headerText="Oid" />
				<COLUMN dataField="COD1" width="60" type="Char" visible="false" headerText="Oid" />
				<COLUMN dataField="OID2" width="60" type="Char" visible="false" headerText="Oid" />
				<COLUMN dataField="COD2" width="60" type="Char" visible="false" headerText="Oid" />
				<COLUMN dataField="OID" width="60" type="Char" visible="false" headerText="Oid" />
				<COLUMN dataField="AGE" type="Char" visible="false" />
				<COLUMN dataField="COD" width="70" type="Char" visible="true" headerText="Code" textAlign="left" otherFunction="text" totalFunction="text" totalComplement="TOTAL">
					<CELLSTYLE name="fontSize" value="11" />
					<CELLSTYLE name="paddingLeft" value="15" condition="ROW.LVL == '2' &amp;&amp; SELECT.GROUPBY_TAB != 'TAB'" />
				</COLUMN>
				<COLUMN dataField="TXT" type="Char" minWidth="200" headerText="Texte" textAlign="left" totalFunction="text" otherFunction="text" otherComplement="Autres">
					<CELLSTYLE name="fontSize" value="11" />
				</COLUMN>
				
				<COLUMN dataField="NB_RSA" width="100" type="Number" outputFormat="#"  headerText="Séjours" textAlign="right" totalFunction="sum" otherFunction="sum" />
				<COLUMN dataField="NB_RSA0" width="100" type="Number" outputFormat="#"  headerText="Dont séjours sans impact CMA" textAlign="right" totalFunction="sum" otherFunction="sum" />
				<COLUMN dataField="NB_RSA1" width="100" type="Number" outputFormat="#"  headerText="Dont séjours avec impact CMA" textAlign="right" totalFunction="sum" otherFunction="sum" />
				
				<COLUMN dataField="MGHS" width="100" type="Number" outputFormat="#E"  headerText="Montant GHS facturé" textAlign="right" totalFunction="sum" otherFunction="sum" />
				<COLUMN dataField="MGHSS" width="100" type="Number" outputFormat="#E"  headerText="Montant GHS sans la CMA" textAlign="right" totalFunction="sum" otherFunction="sum" />
				<COLUMN dataField="MGHSE" width="100" type="Number" outputFormat="#E"  headerText="Impact financier CMA" textAlign="right" totalFunction="sum" otherFunction="sum" />
				
			</DATAGRID>
		</ONGLET>
		
	</PRESENTATION>
</VUE>
