<?xml version="1.0" encoding="ISO-8859-1"?>
<INDICATOR title="Intégrité tables" 
	name="iCTI_activite_integrite_tables" 
	softCode="iCTI_activite"
	eisOnly="false"
	type="CTIsql" 
	indicatorKey="" 
	linkView="ACTI000030">
	<reportTitle><![CDATA[Intégrité tables [CONTEXT] [STATUT]]]></reportTitle>
	<reportDescription></reportDescription>
	<SQL database="iCTI">
		<select><![CDATA[		
			DROP TABLE IF EXISTS w_count;
			CREATE TEMP TABLE w_count (tablename text, nb numeric)
			;
			INSERT INTO w_count VALUES('p_sejours',(SELECT count(*) FROM activite.p_sejours WHERE etat = ''))
			;
			INSERT INTO w_count VALUES('v_sejours_1',(SELECT count(*) FROM activite.v_sejours_1))
			;
			INSERT INTO w_count VALUES('p_mouvements_sejour',(SELECT count(*) FROM activite.p_mouvements_sejour))
			;
			INSERT INTO w_count VALUES('v_mouvements_sejour_4',(SELECT count(*) FROM activite.v_mouvements_sejour_4))
			;
			INSERT INTO w_count VALUES('p_factures',(SELECT count(*) FROM activite.p_factures))
			;
			INSERT INTO w_count VALUES('v_factures_1',(SELECT count(*) FROM activite.v_factures_1))
			;
			INSERT INTO w_count VALUES('p_factures_lignes_c',(SELECT count(*) FROM activite.p_factures_lignes_c))
			;
			INSERT INTO w_count VALUES('v_factures_lignes_c_3',(SELECT count(*) FROM activite.v_factures_lignes_c_3))
			;
			INSERT INTO w_count VALUES('p_facture_solde_tiers_c',(SELECT count(*) FROM activite.p_facture_solde_tiers_c))
			;
			INSERT INTO w_count VALUES('v_facture_solde_tiers_c_1',(SELECT count(*) FROM activite.v_facture_solde_tiers_c_1))
			;
			INSERT INTO w_count VALUES('p_factures_lignes_h',(SELECT count(*) FROM activite.p_factures_lignes_h))
			;
			INSERT INTO w_count VALUES('v_factures_lignes_h_1',(SELECT count(*) FROM activite.v_factures_lignes_h_1))
			;
			INSERT INTO w_count VALUES('p_factures_soldes_h',(SELECT count(*) FROM activite.p_factures_soldes_h))
			;
			INSERT INTO w_count VALUES('v_factures_soldes_h_1',(SELECT count(*) FROM activite.v_factures_soldes_h_1))
			;



			SELECT
				now() AS date,
				count_p_sejours - count_v_sejours +
				count_p_mouvements_sejour - count_v_mouvements_sejour +
				count_p_factures - count_v_factures +
				count_p_factures_lignes_c - count_v_factures_lignes_c +
				count_p_factures_lignes_h - count_v_factures_lignes_h +
				count_p_facture_solde_tiers_c - count_v_facture_solde_tiers_c +
				count_p_factures_soldes_h - count_v_factures_soldes_h 
					AS ecart_total,
				count_p_sejours,
				count_v_sejours,
				count_p_sejours - count_v_sejours AS ecart_sejours,
				count_p_mouvements_sejour,
				count_v_mouvements_sejour,
				count_p_mouvements_sejour - count_v_mouvements_sejour AS ecart_mouvements_sejour,
				count_p_factures,
				count_v_factures,
				count_p_factures - count_v_factures AS ecart_factures,
				count_p_factures_lignes_c,
				count_v_factures_lignes_c,
				count_p_factures_lignes_c - count_v_factures_lignes_c AS ecart_factures_lignes_c,
				count_p_facture_solde_tiers_c,
				count_v_facture_solde_tiers_c,
				count_p_facture_solde_tiers_c - count_v_facture_solde_tiers_c AS ecart_facture_solde_tiers_c,
				count_p_factures_lignes_h,
				count_v_factures_lignes_h,
				count_p_factures_lignes_h - count_v_factures_lignes_h AS ecart_factures_lignes_h,
				count_p_factures_soldes_h,
				count_v_factures_soldes_h,
				count_p_factures_soldes_h - count_v_factures_soldes_h AS ecart_factures_soldes_h
			FROM
				(
				SELECT 
					MAX(CASE WHEN tablename = 'p_sejours' THEN nb ELSE 0 END) AS count_p_sejours,
					MAX(CASE WHEN tablename = 'v_sejours_1' THEN nb ELSE 0 END) AS count_v_sejours,
					MAX(CASE WHEN tablename = 'p_mouvements_sejour' THEN nb ELSE 0 END) AS count_p_mouvements_sejour,
					MAX(CASE WHEN tablename = 'v_mouvements_sejour_4' THEN nb ELSE 0 END) AS count_v_mouvements_sejour,
					MAX(CASE WHEN tablename = 'p_factures' THEN nb ELSE 0 END) AS count_p_factures,
					MAX(CASE WHEN tablename = 'v_factures_1' THEN nb ELSE 0 END) AS count_v_factures,
					MAX(CASE WHEN tablename = 'p_factures_lignes_c' THEN nb ELSE 0 END) AS count_p_factures_lignes_c,
					MAX(CASE WHEN tablename = 'v_factures_lignes_c_3' THEN nb ELSE 0 END) AS count_v_factures_lignes_c,
					MAX(CASE WHEN tablename = 'p_facture_solde_tiers_c' THEN nb ELSE 0 END) AS count_p_facture_solde_tiers_c,
					MAX(CASE WHEN tablename = 'v_facture_solde_tiers_c_1' THEN nb ELSE 0 END) AS count_v_facture_solde_tiers_c,
					MAX(CASE WHEN tablename = 'p_factures_lignes_h' THEN nb ELSE 0 END) AS count_p_factures_lignes_h,
					MAX(CASE WHEN tablename = 'v_factures_lignes_h_1' THEN nb ELSE 0 END) AS count_v_factures_lignes_h,
					MAX(CASE WHEN tablename = 'p_factures_soldes_h' THEN nb ELSE 0 END) AS count_p_factures_soldes_h,
					MAX(CASE WHEN tablename = 'v_factures_soldes_h_1' THEN nb ELSE 0 END) AS count_v_factures_soldes_h
				FROM w_count
				) subview
		]]></select>
	</SQL>
	
	<ALERT rule_compareField="ecart_sejours" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="écart d'intégrité sur table p_sejours" />	
	<ALERT rule_compareField="ecart_mouvements_sejour" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="écart d'intégrité sur table p_mouvements_sejour (moins de 10)" />
	<ALERT rule_compareField="ecart_factures" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="écart d'intégrité sur table p_factures" />
	<ALERT rule_compareField="ecart_factures_lignes_c" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="écart d'intégrité sur table p_factures_lignes_c" />
	<ALERT rule_compareField="ecart_facture_solde_tiers_c" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="écart d'intégrité sur table p_facture_solde_tiers_c" />
	<ALERT rule_compareField="ecart_factures_lignes_h" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="écart d'intégrité sur table p_factures_lignes_h" />
	<ALERT rule_compareField="ecart_factures_soldes_h" rule_compareOperator=">" rule_compareValue="0" result="WARNING" resultText="écart d'intégrité sur table p_factures_soldes_h" />

	<ALERT rule_compareField="ecart_sejours" rule_compareOperator=">" rule_compareValue="10" result="ERROR" resultText="ERREUR d'intégrité sur table p_sejours" />	
	<ALERT rule_compareField="ecart_mouvements_sejour" rule_compareOperator=">" rule_compareValue="10" result="ERROR" resultText="ERREUR d'intégrité sur table p_mouvements_sejour" />
	<ALERT rule_compareField="ecart_factures" rule_compareOperator=">" rule_compareValue="10" result="ERROR" resultText="ERREUR d'intégrité sur table p_factures" />
	<ALERT rule_compareField="ecart_factures_lignes_c" rule_compareOperator=">" rule_compareValue="10" result="ERROR" resultText="ERREUR d'intégrité sur table p_factures_lignes_c" />
	<ALERT rule_compareField="ecart_facture_solde_tiers_c" rule_compareOperator=">" rule_compareValue="10" result="ERROR" resultText="ERREUR d'intégrité sur table p_facture_solde_tiers_c" />
	<ALERT rule_compareField="ecart_factures_lignes_h" rule_compareOperator=">" rule_compareValue="10" result="ERROR" resultText="ERREUR d'intégrité sur table p_factures_lignes_h" />
	<ALERT rule_compareField="ecart_factures_soldes_h" rule_compareOperator=">" rule_compareValue="10" result="ERROR" resultText="ERREUR d'intégrité sur table p_factures_soldes_h" />
	
	<ALERT rule_compareField="ecart_total" rule_compareOperator="=" rule_compareValue="0" result="OK" resultText="Intégrité CORRECTE" />
	
	

</INDICATOR>
