<?xml version="1.0" encoding="ISO-8859-1"?>
<INDICATOR title="Intégrité tables" 
	name="iCTI_rh_integrite_tables" 
	softCode="iCTI_rh"
	eisOnly="false"
	type="CTIsql" 
	indicatorKey="" 
	linkView="RH000001">
	<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_contrats',(SELECT count(*) FROM rh.p_contrats))
                        ;
                        INSERT INTO w_count VALUES('v_contrats_1',(SELECT count(*) FROM rh.v_contrats_1))
                        ;
                        INSERT INTO w_count VALUES('p_contrats_mois',(SELECT count(*) FROM rh.p_contrats_mois))
                        ;
                        INSERT INTO w_count VALUES('v_contrats_mois_1',(SELECT count(DISTINCT contrat_mois_id) FROM rh.v_contrats_mois_1))
                        ;
                        INSERT INTO w_count VALUES('p_historique_paie',(SELECT count(*) FROM rh.p_historique_paie))
                        ;
                        INSERT INTO w_count VALUES('v_historique_paie_1',(SELECT count(DISTINCT CTID) FROM rh.v_historique_paie_1))
                        ;
                        INSERT INTO w_count VALUES('p_arrets_travail_mois',(SELECT count(*) FROM rh.p_arrets_travail_mois))
                        ;
                        INSERT INTO w_count VALUES('v_arrets_travail_mois_1',(SELECT count(*) FROM rh.v_arrets_travail_mois_1))
                        ;
                        INSERT INTO w_count VALUES('p_planning_mouvement',(SELECT count(*) FROM rh.p_planning_mouvement))
                        ;
                        INSERT INTO w_count VALUES('v_planning_mouvement_1',(SELECT count(*) FROM rh.v_planning_mouvement_1))
                        ;
                        



                        SELECT
                            now() AS date,
                            count_p_contrats - count_v_contrats +
                            count_p_contrats_mois - count_v_contrats_mois +
                            count_p_historique_paie - count_v_historique_paie +
                            count_p_arrets_travail_mois - count_v_arrets_travail_mois +
                            count_p_planning_mouvement - count_v_planning_mouvement
                                AS ecart_total,
                            count_p_contrats,
                            count_v_contrats,
                            count_p_contrats - count_v_contrats AS ecart_contrats,
                            count_p_contrats_mois,
                            count_v_contrats_mois,
                            count_p_contrats_mois - count_v_contrats_mois AS ecart_contrats_mois,
                            count_p_historique_paie,
                            count_v_historique_paie,
                            count_p_historique_paie - count_v_historique_paie AS ecart_historique_paie,
                            count_p_arrets_travail_mois,
                            count_v_arrets_travail_mois,
                            count_p_arrets_travail_mois - count_v_arrets_travail_mois AS ecart_arrets_travail_mois,
                            count_p_planning_mouvement,
                            count_v_planning_mouvement,
                            count_p_planning_mouvement - count_v_planning_mouvement AS ecart_planning_mouvement
                        FROM
                            (
                            SELECT 
                                MAX(CASE WHEN tablename = 'p_contrats' THEN nb ELSE 0 END) AS count_p_contrats,
                                MAX(CASE WHEN tablename = 'v_contrats_1' THEN nb ELSE 0 END) AS count_v_contrats,
                                MAX(CASE WHEN tablename = 'p_contrats_mois' THEN nb ELSE 0 END) AS count_p_contrats_mois,
                                MAX(CASE WHEN tablename = 'v_contrats_mois_1' THEN nb ELSE 0 END) AS count_v_contrats_mois,
                                MAX(CASE WHEN tablename = 'p_historique_paie' THEN nb ELSE 0 END) AS count_p_historique_paie,
                                MAX(CASE WHEN tablename = 'v_historique_paie_1' THEN nb ELSE 0 END) AS count_v_historique_paie,
                                MAX(CASE WHEN tablename = 'p_arrets_travail_mois' THEN nb ELSE 0 END) AS count_p_arrets_travail_mois,
                                MAX(CASE WHEN tablename = 'v_arrets_travail_mois_1' THEN nb ELSE 0 END) AS count_v_arrets_travail_mois,
                                MAX(CASE WHEN tablename = 'p_planning_mouvement' THEN nb ELSE 0 END) AS count_p_planning_mouvement,
                                MAX(CASE WHEN tablename = 'v_planning_mouvement_1' THEN nb ELSE 0 END) AS count_v_planning_mouvement
                            FROM w_count
                            ) subview

				]]></select>
	</SQL>
	<ALERT rule_compareField="ecart_contrats" rule_compareOperator=">" rule_compareValue="0" result="ERROR" resultText="ERREUR d'intégrité sur table p_contrats" />
	<ALERT rule_compareField="ecart_contrats_mois" rule_compareOperator=">" rule_compareValue="0" result="ERROR" resultText="ERREUR d'intégrité sur table p_contrats_mois" />
	<ALERT rule_compareField="ecart_historique_paie" rule_compareOperator=">" rule_compareValue="0" result="ERROR" resultText="ERREUR d'intégrité sur table p_historique_paie" />
	<ALERT rule_compareField="ecart_arrets_travail_mois" rule_compareOperator=">" rule_compareValue="0" result="ERROR" resultText="ERREUR d'intégrité sur table p_arrets_travail_mois" />
	<ALERT rule_compareField="ecart_planning_mouvement" rule_compareOperator=">" rule_compareValue="0" result="ERROR" resultText="ERREUR d'intégrité sur table p_planning_mouvement" />
	<ALERT rule_compareField="ecart_total" rule_compareOperator=">" rule_compareValue="0" result="ERROR" resultText="ERREUR d'intégrité" />
	<ALERT rule_compareField="ecart_total" rule_compareOperator="=" rule_compareValue="0" result="OK" resultText="Intégrité CORRECTE" />

</INDICATOR>
