<?xml version="1.0" encoding="ISO-8859-1"?>
<INDICATOR title="Fraicheur" 
	name="iCTI_activite_fraicheur" 
	softCode="iCTI_activite"
	eisOnly="false"
	type="CTIsql" 
	indicatorKey="" >
	<reportTitle><![CDATA[Fraicheur [CONTEXT] [STATUT]]]></reportTitle>
	<reportDescription></reportDescription>
	<SQL database="iCTI">
		<select><![CDATA[		
			
			INSERT INTO activite.t_divers (code, texte, valeur, description)
			SELECT
				'CTLFRAICHEUR'||dow,
				'Controle fraicheur des données '||texte,
				CASE WHEN dow = '' THEN '1' WHEN dow = '1' THEN '2' ELSE '' END,
				'Retard des données accepté en jours ' || CASE WHEN dow <> '' THEN ' ' || texte || ' (rien = valeur par défaut)' ELSE 'Par défaut' END 
			FROM
			(
			SELECT '1' AS dow,'Lundi' AS texte
			UNION
			SELECT '2','Mardi'
			UNION
			SELECT '3','Mercredi'
			UNION
			SELECT '4','Jeudi'
			UNION
			SELECT '5','Vendredi'
			UNION
			SELECT '6','Samedi'
			UNION
			SELECT '7','Dimanche'
			UNION
			SELECT '','Par défaut'
			ORDER BY 1
			) subview
			WHERE 'CTLFRAICHEUR'||dow  NOT IN (SELECT code FROM activite.t_divers);
				

			SELECT date(now()) AS date,
				COALESCE((date(now()) - MAX(date_now_activite)) - base.cti_to_number(MAX(COALESCE(t_divers_1.valeur,t_divers_2.valeur,t_divers_3.valeur,t_divers_4.valeur,t_divers_5.valeur,t_divers_6.valeur,t_divers_7.valeur,t_divers_gen.valeur,'1'))),MAX(CASE WHEN t_divers_gen.valeur <> '999' THEN 999 ELSE 0 END)) AS anciennete_jours
			FROM
			(
			SELECT MAX(date_facture) AS date_now_activite FROM activite.p_factures WHERE date_facture <= now()
			UNION
			SELECT MAX(date) AS date_now_activite FROM activite.p_mouvements_sejour 
			WHERE (nb_entrees_directes = 1 OR nb_sorties_directes = 1 OR nb_entrees_directes = 1 OR nb_ambulatoires = 1 OR nb_externes = 1) AND 
				est_mouvement_previsionnel <> '1'
			) subview
			LEFT JOIN activite.t_divers t_divers_gen ON t_divers_gen.code = 'CTLFRAICHEUR' AND t_divers_gen.valeur <> ''
			LEFT JOIN activite.t_divers t_divers_1 ON t_divers_1.code = 'CTLFRAICHEUR1' AND date_part('DOW',now()) = 1 AND t_divers_1.valeur <> ''
			LEFT JOIN activite.t_divers t_divers_2 ON t_divers_2.code = 'CTLFRAICHEUR2' AND date_part('DOW',now()) = 2 AND t_divers_2.valeur <> ''
			LEFT JOIN activite.t_divers t_divers_3 ON t_divers_3.code = 'CTLFRAICHEUR3' AND date_part('DOW',now()) = 3 AND t_divers_3.valeur <> ''
			LEFT JOIN activite.t_divers t_divers_4 ON t_divers_4.code = 'CTLFRAICHEUR4' AND date_part('DOW',now()) = 4 AND t_divers_4.valeur <> ''
			LEFT JOIN activite.t_divers t_divers_5 ON t_divers_5.code = 'CTLFRAICHEUR5' AND date_part('DOW',now()) = 5 AND t_divers_5.valeur <> ''
			LEFT JOIN activite.t_divers t_divers_6 ON t_divers_6.code = 'CTLFRAICHEUR6' AND date_part('DOW',now()) = 6 AND t_divers_6.valeur <> ''
			LEFT JOIN activite.t_divers t_divers_7 ON t_divers_7.code = 'CTLFRAICHEUR7' AND date_part('DOW',now()) = 0 AND t_divers_7.valeur <> ''
		]]></select>
	</SQL>
	<ALERT rule_compareField="anciennete_jours" rule_compareOperator="&gt;" rule_compareValue="1" result="ERROR" resultText="Les données dans iCTI ne sont pas à jour" />
	<ALERT rule_compareField="anciennete_jours" rule_compareOperator="=" rule_compareValue="1" result="WARNING" resultText="Les données dans iCTI ne sont peut-etre pas à jour" />
	<ALERT rule_compareField="anciennete_jours" rule_compareOperator="&lt;" rule_compareValue="1" result="OK" resultText="Les données dans iCTI sont à jour" />

</INDICATOR>