<?xml version="1.0" encoding="ISO-8859-1"?>
<INDICATOR title="OVALIDE" 
	name="iCTI_pmsi_mat2a" 
	softCode="iCTI_pmsi"
	eisOnly="false"
	type="CTIsql" 
	indicatorKey=""
	linkView="PMSI000255">
	<reportTitle><![CDATA[OVALIDE [CONTEXT] [STATUT]]]></reportTitle>
	<reportDescription></reportDescription>
	<SQL database="iCTI">
		<select><![CDATA[		
			SELECT
				sum(import_manquant) AS import_manquant,
				sum(CASE WHEN mat2a_ok = 0 THEN ecart_rsa_transmis ELSE 0 END) AS ecart_rsa_transmis,
				sum(CASE WHEN mat2a_ok = 0 THEN ecart_rsa_traites ELSE 0 END) AS ecart_rsa_traites,
				sum(CASE WHEN mat2a_ok = 0 THEN ecart_rsa_0_nuits ELSE 0 END) AS ecart_rsa_0_nuits,
				sum(CASE WHEN mat2a_ok = 0 THEN ecart_rsa_seances ELSE 0 END) AS ecart_rsa_seances,
				sum(CASE WHEN mat2a_ok = 0 THEN ecart_rsa_br_prestations ELSE 0 END) AS ecart_rsa_br_prestations,
				sum(import_manquant + (CASE WHEN mat2a_ok = 0 THEN (ecart_rsa_transmis + ecart_rsa_traites + ecart_rsa_0_nuits + ecart_rsa_seances + ecart_rsa_br_prestations) ELSE 0 END)) AS total
			FROM
				(
				SELECT
					CASE WHEN last_mat2a_imports.annee IS NULL AND type_etablissement IN ('2', '3') THEN 1 ELSE 0 END AS import_manquant,
					coalesce(abs(mat2a_rsa_transmis - nb_rsa_transmis), 0) AS ecart_rsa_transmis,
					coalesce(abs(mat2a_rsa_traites - nb_rsa_traites), 0) AS ecart_rsa_traites,
					coalesce(abs(mat2a_rsa_0_nuits - nb_rsa_0_nuits), 0) AS ecart_rsa_0_nuits,
					coalesce(abs(mat2a_rsa_seances - nb_rsa_seances), 0) AS ecart_rsa_seances,
					coalesce(abs(mat2a_rsa_prestations_base_remb - br_total_rsa_prestations), 0) AS ecart_rsa_br_prestations,
					mat2a_ok
				FROM
						(
						SELECT
							p_imports.*,
							p_imports_stats.*,
							subq2.annee,
							subq2.mois,
                            t_finess.type_etablissement
						FROM pmsi.p_imports
							JOIN pmsi.p_imports_stats on p_imports_stats.import_id = p_imports.oid
							JOIN
								(
								SELECT 
									annee,
									max(mois) AS mois,
									(max(ARRAY [annee::text||lpad(mois, 2, '0'), oid::text]))[2] AS oid
								FROM
									(
									SELECT 
										to_char(date_debut, 'YYYY') AS annee,
										to_char(date_fin - '3 days'::interval, 'MM') AS mois,
										oid
									FROM pmsi.p_imports
									WHERE 1=1
										AND date_fin - date_debut < 400
										AND date_debut < now()
									) AS subq1
								GROUP BY 1
								) AS subq2 ON subq2.oid = p_imports.oid
                            LEFT JOIN base.t_finess ON t_finess.code = p_imports.finess
						) AS last_rss_imports
						LEFT JOIN
							(
							SELECT *
							FROM pmsi.p_mat2a_import
							JOIN 
								(
								SELECT
									mat2a_annee || lpad(mat2a_mois, 2, '0') AS annee,
									(max(ARRAY [mat2a_date::text, oid::text]))[2] AS oid
								FROM pmsi.p_mat2a_import
								GROUP BY 1
								) AS subq1 ON subq1.oid = p_mat2a_import.oid
							) AS last_mat2a_imports ON last_rss_imports.annee||lpad(last_rss_imports.mois, 2, '0') = last_mat2a_imports.annee
					WHERE 1=1
						AND last_rss_imports.annee >= 2016
				) AS subq_total
		]]></select>
	</SQL>
	<ALERT rule_compareField="import_manquant" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="WARNING Au moins un import RSS ou OVALIDE manquant." />
	<ALERT rule_compareField="ecart_rsa_transmis" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="WARNING Ecart sur le nombre de RSA transmis." />
	<ALERT rule_compareField="ecart_rsa_traites" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="WARNING Ecart sur le nombre de RSA traités." />
	<ALERT rule_compareField="ecart_rsa_0_nuits" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="WARNING Ecart sur le nombre de RSA 0 nuits." />
	<ALERT rule_compareField="ecart_rsa_seances" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="WARNING Ecart sur le nombre de RSA séances." />
	<ALERT rule_compareField="ecart_rsa_br_prestations" rule_compareOperator=">" rule_compareValue="1" result="WARNING" resultText="WARNING Ecart sur le montant total des prestations." />
	<ALERT rule_compareField="total" rule_compareOperator="=" rule_compareValue="0" result="OK" resultText="OK Les données PMSI MCO sont en adéquation avec les tableaux OVALIDE (Tableau 1.D.2.RTP et 1.V.1.CP)." />
</INDICATOR>
