					SELECT 
					a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
					a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
						IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
						IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
						scan_start_date AS incident_date_in_seconds
							
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_GroupId)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.GroupId = par_group_id
						AND a.confident_data_inc_count <> 0
						ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c , rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY scan_start_date DESC;			
				ELSEIF par_content_type_id = 3 THEN
					SELECT 
					a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
					a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
						IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
						IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
						scan_start_date AS incident_date_in_seconds
							
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_GroupId)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.GroupId = par_group_id
						AND a.data_dictionary_inc_count <> 0
						ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c , rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY scan_start_date DESC;			
				ELSE
					SELECT 
					a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
					a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
						IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
						IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
						scan_start_date AS incident_date_in_seconds
							
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_GroupId)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.GroupId = par_group_id
						ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c , rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY scan_start_date DESC;
				END IF;
			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					IF par_content_type_id = 2 THEN
						SELECT 
						a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
						a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
							IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
							IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
							scan_start_date AS incident_date_in_seconds
							
							FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id)
							WHERE
							a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND FIND_IN_SET(a.client_id, var_client_ids_str)
							AND a.confident_data_inc_count <> 0
							ORDER BY a.scan_start_date DESC
							LIMIT par_start_index,par_max_records
						) AS a, client_master AS c , rpt_user_master AS d
						WHERE 
						a.client_id=c.client_id
						AND a.user_id = d.user_id
						ORDER BY scan_start_date DESC;				
					ELSEIF par_content_type_id = 3 THEN
						SELECT 
						a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
						a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
							IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
							IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
							scan_start_date AS incident_date_in_seconds
							
							FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id)
							WHERE
							a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND FIND_IN_SET(a.client_id, var_client_ids_str)
							AND a.data_dictionary_inc_count <> 0
							ORDER BY a.scan_start_date DESC
							LIMIT par_start_index,par_max_records
						) AS a, client_master AS c , rpt_user_master AS d
						WHERE 
						a.client_id=c.client_id
						AND a.user_id = d.user_id
						ORDER BY scan_start_date DESC;				
					ELSE
						SELECT 
						a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
						a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
							IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
							IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
							scan_start_date AS incident_date_in_seconds
							
							FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id)
							WHERE
							a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND FIND_IN_SET(a.client_id, var_client_ids_str)
							ORDER BY a.scan_start_date DESC
							LIMIT par_start_index,par_max_records
						) AS a, client_master AS c , rpt_user_master AS d
						WHERE 
						a.client_id=c.client_id
						AND a.user_id = d.user_id
						ORDER BY scan_start_date DESC;
					END IF;
				ELSEIF var_client_count = 1 THEN
					IF par_content_type_id = 2 THEN
						SELECT 
						a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
						a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
							IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
							IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
							scan_start_date AS incident_date_in_seconds
							
							FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id)
							WHERE
							a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND a.client_id = var_client_ids_str
							AND a.confident_data_inc_count <> 0
							ORDER BY a.scan_start_date DESC
							LIMIT par_start_index,par_max_records
						) AS a, client_master AS c , rpt_user_master AS d
						WHERE 
						a.client_id=c.client_id
						AND a.user_id = d.user_id
						ORDER BY scan_start_date DESC;				
					ELSEIF par_content_type_id = 3 THEN
						SELECT 
						a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
						a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
							IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
							IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
							scan_start_date AS incident_date_in_seconds
							
							FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id)
							WHERE
							a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND a.client_id = var_client_ids_str
							AND a.data_dictionary_inc_count <> 0
							ORDER BY a.scan_start_date DESC
							LIMIT par_start_index,par_max_records
						) AS a, client_master AS c , rpt_user_master AS d
						WHERE 
						a.client_id=c.client_id
						AND a.user_id = d.user_id
						ORDER BY scan_start_date DESC;				
					ELSE
						SELECT 
						a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
						a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
							IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
							IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
							scan_start_date AS incident_date_in_seconds
							
							FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id)
							WHERE
							a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND a.client_id = var_client_ids_str
							ORDER BY a.scan_start_date DESC
							LIMIT par_start_index,par_max_records
						) AS a, client_master AS c , rpt_user_master AS d
						WHERE 
						a.client_id=c.client_id
						AND a.user_id = d.user_id
						ORDER BY scan_start_date DESC;
					END IF;
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
				IF par_content_type_id = 2 THEN
					SELECT 
					a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
					a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
						IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
						IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
						scan_start_date AS incident_date_in_seconds
							
						FROM rpt_dlp_od_summarized_fact AS a FORCE INDEX(idx_scan_start_date) 
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.confident_data_inc_count <> 0 
						ORDER BY a.scan_start_date DESC
						LIMIT par_start_index,par_max_records
					) AS a, client_master AS c , rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY scan_start_date DESC;			
				ELSEIF par_content_type_id = 3 THEN
					SELECT 
					a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
					a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
						IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
						IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
						scan_start_date AS incident_date_in_seconds
							
						FROM rpt_dlp_od_summarized_fact AS a FORCE INDEX(idx_scan_start_date) 
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.data_dictionary_inc_count <> 0
						ORDER BY a.scan_start_date DESC
						LIMIT par_start_index,par_max_records
					) AS a, client_master AS c , rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY scan_start_date DESC;			
				ELSE
					SELECT 
					a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
					a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
						IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
						IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
						scan_start_date AS incident_date_in_seconds
							
						FROM rpt_dlp_od_summarized_fact AS a FORCE INDEX(idx_scan_start_date) 
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						ORDER BY a.scan_start_date DESC
						LIMIT par_start_index,par_max_records
					) AS a, client_master AS c , rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY scan_start_date DESC;
				END IF;
			END IF;
		ELSE
		
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
		
			IF par_client_name<> '' THEN
				
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master 
				WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					IF par_content_type_id = 2 THEN
						SELECT 
						a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
						a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
							IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
							IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
							scan_start_date AS incident_date_in_seconds
							
							FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_user_id, idx_GroupId)
							WHERE
							a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND FIND_IN_SET(a.client_id, var_client_ids_str)
							AND (par_group_id = 0 OR a.GroupId=par_group_id)
							AND a.user_id = var_user_id
							AND a.confident_data_inc_count <> 0
							ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
						) AS a, client_master AS c, rpt_user_master AS d
						WHERE 
						a.client_id=c.client_id
						AND a.user_id = d.user_id
						ORDER BY scan_start_date DESC;
					ELSEIF par_content_type_id = 3 THEN
						SELECT 
						a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
						a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
							IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
							IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
							scan_start_date AS incident_date_in_seconds
							
							FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_user_id, idx_GroupId)
							WHERE
							a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND FIND_IN_SET(a.client_id, var_client_ids_str)
							AND (par_group_id = 0 OR a.GroupId=par_group_id)
							AND a.user_id = var_user_id
							AND a.data_dictionary_inc_count <> 0
							ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
						) AS a, client_master AS c, rpt_user_master AS d
						WHERE 
						a.client_id=c.client_id
						AND a.user_id = d.user_id
						ORDER BY scan_start_date DESC;
					ELSE
						SELECT 
						a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
						a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
							IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
							IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
							scan_start_date AS incident_date_in_seconds
							
							FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_user_id, idx_GroupId)
							WHERE
							a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND FIND_IN_SET(a.client_id, var_client_ids_str)
							AND (par_group_id = 0 OR a.GroupId=par_group_id)
							AND a.user_id = var_user_id
							ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
						) AS a, client_master AS c, rpt_user_master AS d
						WHERE 
						a.client_id=c.client_id
						AND a.user_id = d.user_id
						ORDER BY scan_start_date DESC;
					END IF;
				ELSEIF var_client_count = 1 THEN
					IF par_content_type_id = 2 THEN
						SELECT 
						a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
						a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
							IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
							IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
							scan_start_date AS incident_date_in_seconds
							
							FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_user_id, idx_GroupId)
							WHERE
							a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND a.client_id = var_client_ids_str
							AND (par_group_id = 0 OR a.GroupId=par_group_id)
							AND a.user_id = var_user_id
							AND a.confident_data_inc_count <> 0
							ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
						) AS a, client_master AS c, rpt_user_master AS d
						WHERE 
						a.client_id=c.client_id
						AND a.user_id = d.user_id
						ORDER BY scan_start_date DESC;
					ELSEIF par_content_type_id = 3 THEN
						SELECT 
						a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
						a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
							IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
							IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
							scan_start_date AS incident_date_in_seconds
							
							FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_user_id, idx_GroupId)
							WHERE
							a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND a.client_id = var_client_ids_str
							AND (par_group_id = 0 OR a.GroupId=par_group_id)
							AND a.user_id = var_user_id
							AND a.data_dictionary_inc_count <> 0
							ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
						) AS a, client_master AS c, rpt_user_master AS d
						WHERE 
						a.client_id=c.client_id
						AND a.user_id = d.user_id
						ORDER BY scan_start_date DESC;
						
					ELSE
					
						SELECT 
						a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
						a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
							IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
							IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
							scan_start_date AS incident_date_in_seconds
							
							FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_user_id, idx_GroupId)
							WHERE
							a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND a.client_id = var_client_ids_str
							AND (par_group_id = 0 OR a.GroupId=par_group_id)
							AND a.user_id = var_user_id
							ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
						) AS a, client_master AS c, rpt_user_master AS d
						WHERE 
						a.client_id=c.client_id
						AND a.user_id = d.user_id
						ORDER BY scan_start_date DESC;
						
					END IF;
				END IF;
				
			ELSE
			
				IF par_content_type_id = 2 THEN
					SELECT 
					a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
					a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
						IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
						IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
						scan_start_date AS incident_date_in_seconds
						
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_user_id, idx_GroupId)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND (par_group_id = 0 OR a.GroupId=par_group_id)
						AND a.user_id = var_user_id
						AND a.confident_data_inc_count <> 0
						ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY scan_start_date DESC;
				ELSEIF par_content_type_id = 3 THEN
					SELECT 
					a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
					a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
						IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
						IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
						scan_start_date AS incident_date_in_seconds
						
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_user_id, idx_GroupId)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND (par_group_id = 0 OR a.GroupId=par_group_id)
						AND a.user_id = var_user_id
						AND a.data_dictionary_inc_count <> 0
						ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY scan_start_date DESC;
				ELSE
					SELECT 
					a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
					a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
						IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
						IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
						scan_start_date AS incident_date_in_seconds
						
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_user_id, idx_GroupId)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND (par_group_id = 0 OR a.GroupId=par_group_id)
						AND a.user_id = var_user_id
						ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY scan_start_date DESC;
				END IF;
				
			END IF;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `fam_rpt_get_chart_details_count_pr` ;
DELIMITER $$
CREATE PROCEDURE `fam_rpt_get_chart_details_count_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone='+00:00';
	IF par_username='' THEN
		IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
		
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT IFNULL(COUNT(DISTINCT file_ext_chksum), 0) AS total_records
				FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND FIND_IN_SET(a.client_id, var_client_ids_str)
				AND a.GroupId = par_group_id;
			ELSEIF var_client_count = 1 THEN
				SELECT IFNULL(COUNT(DISTINCT file_ext_chksum), 0) AS total_records
				FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.client_id = var_client_ids_str
				AND a.GroupId = par_group_id;	
			END IF;
			
		ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
		
			SELECT IFNULL(COUNT(DISTINCT file_ext_chksum), 0) AS total_records
			FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_GroupId)
			WHERE
			a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
			AND a.GroupId = par_group_id;
			
		ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
		
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT IFNULL(COUNT(DISTINCT file_ext_chksum), 0) AS total_records
				FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND FIND_IN_SET(a.client_id, var_client_ids_str);
			ELSEIF var_client_count = 1 THEN
				SELECT IFNULL(COUNT(DISTINCT file_ext_chksum), 0) AS total_records
				FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.client_id = var_client_ids_str;	
			END IF;
		
		ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX00
		
			SELECT IFNULL(COUNT(DISTINCT file_ext_chksum), 0) AS total_records
			FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
			WHERE
			a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second);
		
		END IF;
	ELSE
		SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username=par_username LIMIT 1;
		IF par_client_name<> '' THEN		
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
			FROM client_master WHERE (par_group_id=0 OR GroupId=par_group_id) AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT IFNULL(COUNT(DISTINCT file_ext_chksum), 0) AS total_records
				FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND FIND_IN_SET(a.client_id, var_client_ids_str)
				AND a.user_id=var_user_id
				AND (par_group_id=0 OR GroupId=par_group_id);
			ELSEIF var_client_count = 1 THEN
				SELECT IFNULL(COUNT(DISTINCT file_ext_chksum), 0) AS total_records
				FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.client_id = var_client_ids_str
				AND a.user_id=var_user_id
				AND (par_group_id=0 OR GroupId=par_group_id);	
			END IF;
		ELSE
			SELECT IFNULL(COUNT(DISTINCT file_ext_chksum), 0) AS total_records
			FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only)
			WHERE
			a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
			AND a.user_id=var_user_id
			AND (par_group_id=0 OR GroupId=par_group_id);
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `fam_rpt_get_chart_details_pr`;
DELIMITER $$
CREATE PROCEDURE `fam_rpt_get_chart_details_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_event_id INT(11),
	IN par_location_id INT(11),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone='+00:00';
	
	SELECT user_id INTO var_user_id FROM rpt_user_master WHERE par_username<>'' AND username=par_username LIMIT 1;
	IF par_event_id <> 0 THEN
		IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				IF par_event_id = 1 THEN -- Copy Event			
					SELECT file_extension, 
					IFNULL(SUM(files_copied_on_local), 0) AS files_count1,					
					IFNULL(SUM(files_copied_on_removable), 0) AS files_count2,					
					IFNULL(SUM(files_copied_on_network), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				ELSEIF par_event_id = 2 THEN -- Delete Event
					
					SELECT file_extension, 
					IFNULL(SUM(files_deleted_on_local), 0) AS files_count1,
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_count2,
					IFNULL(SUM(files_deleted_on_network), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				ELSE -- Rename Event
				
					SELECT file_extension, 					
					IFNULL(SUM(files_renamed_on_local), 0) AS files_count1,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_count2,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				END IF;
			ELSEIF var_client_count = 1 THEN
				IF par_event_id = 1 THEN -- Copy Event			
					SELECT file_extension, 
					IFNULL(SUM(files_copied_on_local), 0) AS files_count1,					
					IFNULL(SUM(files_copied_on_removable), 0) AS files_count2,					
					IFNULL(SUM(files_copied_on_network), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				ELSEIF par_event_id = 2 THEN -- Delete Event
					
					SELECT file_extension, 
					IFNULL(SUM(files_deleted_on_local), 0) AS files_count1,
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_count2,
					IFNULL(SUM(files_deleted_on_network), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				ELSE -- Rename Event
				
					SELECT file_extension, 					
					IFNULL(SUM(files_renamed_on_local), 0) AS files_count1,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_count2,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				END IF;
			END IF;
		ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
			
			IF par_event_id = 1 THEN -- Copy Event
			
					SELECT file_extension, 
					IFNULL(SUM(files_copied_on_local), 0) AS files_count1,					
					IFNULL(SUM(files_copied_on_removable), 0) AS files_count2,					
					IFNULL(SUM(files_copied_on_network), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
				
			ELSEIF par_event_id = 2 THEN -- Delete Event			
				
				SELECT file_extension, 
				IFNULL(SUM(files_deleted_on_local), 0) AS files_count1,
				IFNULL(SUM(files_deleted_on_removable), 0) AS files_count2,
				IFNULL(SUM(files_deleted_on_network), 0) AS files_count3
				
				FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
				ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
				LIMIT par_start_index, par_max_records;
				
			ELSE -- Rename Event
			
				SELECT file_extension, 					
				IFNULL(SUM(files_renamed_on_local), 0) AS files_count1,
				IFNULL(SUM(files_renamed_on_removable), 0) AS files_count2,
				IFNULL(SUM(files_renamed_on_network), 0) AS files_count3
				
				FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
				ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
				LIMIT par_start_index, par_max_records;
				
			END IF;
			
		ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				IF par_event_id = 1 THEN -- Copy Event
				
					SELECT file_extension, 
					IFNULL(SUM(files_copied_on_local), 0) AS files_count1,					
					IFNULL(SUM(files_copied_on_removable), 0) AS files_count2,					
					IFNULL(SUM(files_copied_on_network), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				ELSEIF par_event_id = 2 THEN -- Delete Event
					
						SELECT file_extension, 
						IFNULL(SUM(files_deleted_on_local), 0) AS files_count1,
						IFNULL(SUM(files_deleted_on_removable), 0) AS files_count2,
						IFNULL(SUM(files_deleted_on_network), 0) AS files_count3
						
						FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
						WHERE
						a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
						ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
						LIMIT par_start_index, par_max_records;
					
				ELSE -- Rename Event
					
					SELECT file_extension, 					
					IFNULL(SUM(files_renamed_on_local), 0) AS files_count1,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_count2,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				END IF;
				
			ELSEIF var_client_count = 1 THEN
				IF par_event_id = 1 THEN -- Copy Event
				
					SELECT file_extension, 
					IFNULL(SUM(files_copied_on_local), 0) AS files_count1,					
					IFNULL(SUM(files_copied_on_removable), 0) AS files_count2,					
					IFNULL(SUM(files_copied_on_network), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				ELSEIF par_event_id = 2 THEN -- Delete Event
					
						SELECT file_extension, 
						IFNULL(SUM(files_deleted_on_local), 0) AS files_count1,
						IFNULL(SUM(files_deleted_on_removable), 0) AS files_count2,
						IFNULL(SUM(files_deleted_on_network), 0) AS files_count3
						
						FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
						WHERE
						a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
						ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
						LIMIT par_start_index, par_max_records;
					
				ELSE -- Rename Event
					
					SELECT file_extension, 					
					IFNULL(SUM(files_renamed_on_local), 0) AS files_count1,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_count2,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				END IF;
				
			END IF;			
		ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX00
			
			IF par_event_id = 1 THEN -- Copy Event
				
				SELECT file_extension, 
				IFNULL(SUM(files_copied_on_local), 0) AS files_count1,					
				IFNULL(SUM(files_copied_on_removable), 0) AS files_count2,					
				IFNULL(SUM(files_copied_on_network), 0) AS files_count3
				
				FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
				ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
				LIMIT par_start_index, par_max_records;
				
			ELSEIF par_event_id = 2 THEN -- Delete Event
				
				SELECT file_extension, 
				IFNULL(SUM(files_deleted_on_local), 0) AS files_count1,
				IFNULL(SUM(files_deleted_on_removable), 0) AS files_count2,
				IFNULL(SUM(files_deleted_on_network), 0) AS files_count3
				
				FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
				ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
				LIMIT par_start_index, par_max_records;
				
			ELSE -- Rename Event
				
				SELECT file_extension, 					
				IFNULL(SUM(files_renamed_on_local), 0) AS files_count1,
				IFNULL(SUM(files_renamed_on_removable), 0) AS files_count2,
				IFNULL(SUM(files_renamed_on_network), 0) AS files_count3
				
				FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
				ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
				LIMIT par_start_index, par_max_records;
				
			END IF;
			
		END IF;
	ELSEIF par_location_id <> 0 THEN
		IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				IF par_location_id = 1 THEN -- Local			
					SELECT file_extension, 
					IFNULL(SUM(files_copied_on_local), 0) AS files_count1,					
					IFNULL(SUM(files_deleted_on_local), 0) AS files_count2,					
					IFNULL(SUM(files_renamed_on_local), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				ELSEIF par_location_id = 2 THEN -- Removable
					
					SELECT file_extension, 
					IFNULL(SUM(files_copied_on_removable), 0) AS files_count1,
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_count2,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				ELSE -- Network
				
					SELECT file_extension, 					
					IFNULL(SUM(files_copied_on_network), 0) AS files_count1,
					IFNULL(SUM(files_deleted_on_network), 0) AS files_count2,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				END IF;
			ELSEIF var_client_count = 1 THEN
				IF par_location_id = 1 THEN -- Local			
					SELECT file_extension, 
					IFNULL(SUM(files_copied_on_local), 0) AS files_count1,					
					IFNULL(SUM(files_deleted_on_local), 0) AS files_count2,					
					IFNULL(SUM(files_renamed_on_local), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				ELSEIF par_location_id = 2 THEN -- Removable
					
					SELECT file_extension, 
					IFNULL(SUM(files_copied_on_removable), 0) AS files_count1,
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_count2,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				ELSE -- Network
				
					SELECT file_extension, 					
					IFNULL(SUM(files_copied_on_network), 0) AS files_count1,
					IFNULL(SUM(files_deleted_on_network), 0) AS files_count2,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				END IF;
			END IF;
		ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
			
			IF par_location_id = 1 THEN -- Local
			
					SELECT file_extension, 
					IFNULL(SUM(files_copied_on_local), 0) AS files_count1,					
					IFNULL(SUM(files_deleted_on_local), 0) AS files_count2,					
					IFNULL(SUM(files_renamed_on_local), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
				
			ELSEIF par_location_id = 2 THEN -- Removable			
				
				SELECT file_extension, 
				IFNULL(SUM(files_copied_on_removable), 0) AS files_count1,
				IFNULL(SUM(files_deleted_on_removable), 0) AS files_count2,
				IFNULL(SUM(files_renamed_on_removable), 0) AS files_count3
				
				FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
				ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
				LIMIT par_start_index, par_max_records;
				
			ELSE -- Network
			
				SELECT file_extension, 					
				IFNULL(SUM(files_copied_on_network), 0) AS files_count1,
				IFNULL(SUM(files_deleted_on_network), 0) AS files_count2,
				IFNULL(SUM(files_renamed_on_network), 0) AS files_count3
				
				FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
				ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
				LIMIT par_start_index, par_max_records;
				
			END IF;
			
		ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				IF par_location_id = 1 THEN -- Local
				
					SELECT file_extension, 
					IFNULL(SUM(files_copied_on_local), 0) AS files_count1,					
					IFNULL(SUM(files_deleted_on_local), 0) AS files_count2,					
					IFNULL(SUM(files_renamed_on_local), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				ELSEIF par_location_id = 2 THEN -- Removable
					
						SELECT file_extension, 
						IFNULL(SUM(files_copied_on_removable), 0) AS files_count1,
						IFNULL(SUM(files_deleted_on_removable), 0) AS files_count2,
						IFNULL(SUM(files_renamed_on_removable), 0) AS files_count3
						
						FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
						WHERE
						a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
						ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
						LIMIT par_start_index, par_max_records;
					
				ELSE -- Network
					
					SELECT file_extension, 					
					IFNULL(SUM(files_copied_on_network), 0) AS files_count1,
					IFNULL(SUM(files_deleted_on_network), 0) AS files_count2,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				END IF;
			ELSEIF var_client_count = 1 THEN
				IF par_location_id = 1 THEN -- Local
				
					SELECT file_extension, 
					IFNULL(SUM(files_copied_on_local), 0) AS files_count1,					
					IFNULL(SUM(files_deleted_on_local), 0) AS files_count2,					
					IFNULL(SUM(files_renamed_on_local), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				ELSEIF par_location_id = 2 THEN -- Removable
					
						SELECT file_extension, 
						IFNULL(SUM(files_copied_on_removable), 0) AS files_count1,
						IFNULL(SUM(files_deleted_on_removable), 0) AS files_count2,
						IFNULL(SUM(files_renamed_on_removable), 0) AS files_count3
						
						FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
						WHERE
						a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_strs_str
						AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
						ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
						LIMIT par_start_index, par_max_records;
					
				ELSE -- Network
					
					SELECT file_extension, 					
					IFNULL(SUM(files_copied_on_network), 0) AS files_count1,
					IFNULL(SUM(files_deleted_on_network), 0) AS files_count2,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_count3
					
					FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_strs_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
					ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
					LIMIT par_start_index, par_max_records;
					
				END IF;
			END IF;
		ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX00
			
			IF par_location_id = 1 THEN -- Local
				
				SELECT file_extension, 
				IFNULL(SUM(files_copied_on_local), 0) AS files_count1,					
				IFNULL(SUM(files_deleted_on_local), 0) AS files_count2,					
				IFNULL(SUM(files_renamed_on_local), 0) AS files_count3
				
				FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
				ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
				LIMIT par_start_index, par_max_records;
				
			ELSEIF par_location_id = 2 THEN -- Removable
				
				SELECT file_extension, 
				IFNULL(SUM(files_copied_on_removable), 0) AS files_count1,
				IFNULL(SUM(files_deleted_on_removable), 0) AS files_count2,
				IFNULL(SUM(files_renamed_on_removable), 0) AS files_count3
				
				FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
				ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
				LIMIT par_start_index, par_max_records;
				
			ELSE -- Network
				
				SELECT file_extension, 					
				IFNULL(SUM(files_copied_on_network), 0) AS files_count1,
				IFNULL(SUM(files_deleted_on_network), 0) AS files_count2,
				IFNULL(SUM(files_renamed_on_network), 0) AS files_count3
				
				FROM rpt_fam_extn_summary AS a USE INDEX(idx_incident_date_only, idx_file_ext_chksum)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id) GROUP BY a.file_ext_chksum
				ORDER BY files_count3 DESC, files_count2 DESC, files_count1 DESC
				LIMIT par_start_index, par_max_records;
				
			END IF;
			
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `fam_rpt_get_chart_pr`;
DELIMITER $$
CREATE PROCEDURE `fam_rpt_get_chart_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_event_id INT(11),
	IN par_location_id INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT 
		IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,				
		IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,				
		IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network
		
		FROM rpt_fam_chart_summary AS a;
	ELSE
		SELECT user_id INTO var_user_id FROM rpt_user_master WHERE par_username<>'' AND username=par_username LIMIT 1;
		-- CONDITION : 1
		IF par_event_id = 1 AND par_location_id = 0 THEN -- 10
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,				
					IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,				
					IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,				
					IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,				
					IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X			
				SELECT 
				IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,				
				IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,				
				IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,				
					IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,				
					IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,				
					IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,				
					IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT 
				IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,				
				IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,				
				IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
			
		-- CONDITION : 2
		ELSEIF par_event_id = 2 AND par_location_id = 0 THEN -- 20
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local,				
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable,				
					IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local,				
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable,				
					IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
				SELECT 
				IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local,				
				IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable,				
				IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local,				
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable,				
					IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local,				
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable,				
					IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT 
				IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local,				
				IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable,				
				IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 3
		ELSEIF par_event_id = 3 AND par_location_id = 0 THEN -- 30
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
				SELECT 
				IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local,
				IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable,
				IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT 
					IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 4
		ELSEIF par_event_id = 0 AND par_location_id = 1 THEN -- 01
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,
					IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local,
					IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,
					IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local,
					IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
				SELECT 
				IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,
				IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local,
				IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,
					IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local,
					IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,
					IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local,
					IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT 
					IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,
					IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local,
					IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 5
		ELSEIF par_event_id = 0 AND par_location_id = 2 THEN -- 02
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
				SELECT 
				IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,
				IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable,
				IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT 
				IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,
				IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable,
				IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 6
		ELSEIF par_event_id = 0 AND par_location_id = 3 THEN -- 03
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network,
					IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network,
					IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
				SELECT 
				IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network,
				IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network,
				IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network,
					IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network,
					IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT 
				IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network,
				IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network,
				IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 7
		ELSEIF par_event_id = 1 AND par_location_id = 1 THEN -- 11
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
				SELECT 
				IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT 
				IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 8
		ELSEIF par_event_id = 1 AND par_location_id = 2 THEN -- 12
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
				SELECT 
					IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT 
				IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 9
		ELSEIF par_event_id = 1 AND par_location_id = 3 THEN -- 13
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
				SELECT 
				IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT 
				IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 10
		ELSEIF par_event_id = 2 AND par_location_id = 1 THEN -- 21
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
				SELECT 
				IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT 
				IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 11
		ELSEIF par_event_id = 2 AND par_location_id = 2 THEN -- 22
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
				SELECT 
				IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT 
				IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 12
		ELSEIF par_event_id = 2 AND par_location_id = 3 THEN -- 23
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
				SELECT 
				IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT 
				IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 13
		ELSEIF par_event_id = 3 AND par_location_id = 1 THEN -- 31
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
				SELECT 
				IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT 
				IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 14
		ELSEIF par_event_id = 3 AND par_location_id = 2 THEN -- 32
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
				SELECT 
				IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT 
				IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 15
		ELSEIF par_event_id = 3 AND par_location_id = 3 THEN -- 33
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
				SELECT 
				IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT 
				IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 16
		ELSEIF par_event_id = 0 AND par_location_id = 0 THEN -- 00
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,
					IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,
					IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network,
					
					IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local,
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable,
					IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network,
					
					IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,
					IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,
					IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network,
					
					IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local,
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable,
					IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network,
					
					IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
				SELECT 
				IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,
				IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,
				IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network,
				
				IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local,
				IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable,
				IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network,
				
				IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local,
				IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable,
				IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,
					IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,
					IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network,
					
					IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local,
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable,
					IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network,
					
					IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,
					IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,
					IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network,
					
					IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local,
					IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable,
					IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network,
					
					IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local,
					IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable,
					IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT 
				IFNULL(SUM(files_copied_on_local), 0) AS files_copied_on_local,
				IFNULL(SUM(files_copied_on_removable), 0) AS files_copied_on_removable,
				IFNULL(SUM(files_copied_on_network), 0) AS files_copied_on_network,
				
				IFNULL(SUM(files_deleted_on_local), 0) AS files_deleted_on_local,
				IFNULL(SUM(files_deleted_on_removable), 0) AS files_deleted_on_removable,
				IFNULL(SUM(files_deleted_on_network), 0) AS files_deleted_on_network,
				
				IFNULL(SUM(files_renamed_on_local), 0) AS files_renamed_on_local,
				IFNULL(SUM(files_renamed_on_removable), 0) AS files_renamed_on_removable,
				IFNULL(SUM(files_renamed_on_network), 0) AS files_renamed_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		END IF;
	
	END IF;
END$$
DELIMITER ;

DROP PROCEDURE IF EXISTS `fam_rpt_get_chart_summ_pr`;
DELIMITER $$
CREATE PROCEDURE `fam_rpt_get_chart_summ_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_event_id INT(11),
	IN par_location_id INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	
	SELECT user_id INTO var_user_id FROM rpt_user_master WHERE par_username<>'' AND username=par_username LIMIT 1;
	
	-- CONDITION : 1
	IF par_event_id = 1 AND par_location_id = 0 THEN -- 10
	
		IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_local,
				a.files_copied_on_removable,
				a.files_copied_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_local) AS files_copied_on_local,				
					SUM(files_copied_on_removable) AS files_copied_on_removable,				
					SUM(files_copied_on_network) AS files_copied_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_copied_on_network DESC, a.files_copied_on_removable DESC, a.files_copied_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_copied_on_network DESC, a.files_copied_on_removable DESC, a.files_copied_on_local DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_local,
				a.files_copied_on_removable,
				a.files_copied_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_local) AS files_copied_on_local,				
					SUM(files_copied_on_removable) AS files_copied_on_removable,				
					SUM(files_copied_on_network) AS files_copied_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_strs_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_copied_on_network DESC, a.files_copied_on_removable DESC, a.files_copied_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_copied_on_network DESC, a.files_copied_on_removable DESC, a.files_copied_on_local DESC;
			END IF;
		ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_copied_on_local,
			a.files_copied_on_removable,
			a.files_copied_on_network
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_copied_on_local) AS files_copied_on_local,				
				SUM(files_copied_on_removable) AS files_copied_on_removable,				
				SUM(files_copied_on_network) AS files_copied_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_copied_on_network DESC, a.files_copied_on_removable DESC, a.files_copied_on_local DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_copied_on_network DESC, a.files_copied_on_removable DESC, a.files_copied_on_local DESC;
			
		ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_local,
				a.files_copied_on_removable,
				a.files_copied_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_local) AS files_copied_on_local,				
					SUM(files_copied_on_removable) AS files_copied_on_removable,				
					SUM(files_copied_on_network) AS files_copied_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_copied_on_network DESC, a.files_copied_on_removable DESC, a.files_copied_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id			
				ORDER BY a.files_copied_on_network DESC, a.files_copied_on_removable DESC, a.files_copied_on_local DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_local,
				a.files_copied_on_removable,
				a.files_copied_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_local) AS files_copied_on_local,				
					SUM(files_copied_on_removable) AS files_copied_on_removable,				
					SUM(files_copied_on_network) AS files_copied_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_copied_on_network DESC, a.files_copied_on_removable DESC, a.files_copied_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id			
				ORDER BY a.files_copied_on_network DESC, a.files_copied_on_removable DESC, a.files_copied_on_local DESC;
			END IF;
		ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_copied_on_local,
			a.files_copied_on_removable,
			a.files_copied_on_network
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_copied_on_local) AS files_copied_on_local,				
				SUM(files_copied_on_removable) AS files_copied_on_removable,				
				SUM(files_copied_on_network) AS files_copied_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_copied_on_network DESC, a.files_copied_on_removable DESC, a.files_copied_on_local DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id			
			ORDER BY a.files_copied_on_network DESC, a.files_copied_on_removable DESC, a.files_copied_on_local DESC;
		END IF;
		
	-- CONDITION : 2
	ELSEIF par_event_id = 2 AND par_location_id = 0 THEN -- 20
	
		IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_deleted_on_local,
				a.files_deleted_on_removable,
				a.files_deleted_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_deleted_on_local) AS files_deleted_on_local,				
					SUM(files_deleted_on_removable) AS files_deleted_on_removable,				
					SUM(files_deleted_on_network) AS files_deleted_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_deleted_on_network DESC, a.files_deleted_on_removable DESC, a.files_deleted_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_deleted_on_network DESC, a.files_deleted_on_removable DESC, a.files_deleted_on_local DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_deleted_on_local,
				a.files_deleted_on_removable,
				a.files_deleted_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_deleted_on_local) AS files_deleted_on_local,				
					SUM(files_deleted_on_removable) AS files_deleted_on_removable,				
					SUM(files_deleted_on_network) AS files_deleted_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_deleted_on_network DESC, a.files_deleted_on_removable DESC, a.files_deleted_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_deleted_on_network DESC, a.files_deleted_on_removable DESC, a.files_deleted_on_local DESC;
			END IF;
			
		ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_deleted_on_local,
			a.files_deleted_on_removable,
			a.files_deleted_on_network
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_deleted_on_local) AS files_deleted_on_local,				
				SUM(files_deleted_on_removable) AS files_deleted_on_removable,				
				SUM(files_deleted_on_network) AS files_deleted_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_deleted_on_network DESC, a.files_deleted_on_removable DESC, a.files_deleted_on_local DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_deleted_on_network DESC, a.files_deleted_on_removable DESC, a.files_deleted_on_local DESC;
			
		ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_deleted_on_local,
				a.files_deleted_on_removable,
				a.files_deleted_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_deleted_on_local) AS files_deleted_on_local,				
					SUM(files_deleted_on_removable) AS files_deleted_on_removable,				
					SUM(files_deleted_on_network) AS files_deleted_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_deleted_on_network DESC, a.files_deleted_on_removable DESC, a.files_deleted_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id			
				ORDER BY a.files_deleted_on_network DESC, a.files_deleted_on_removable DESC, a.files_deleted_on_local DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_deleted_on_local,
				a.files_deleted_on_removable,
				a.files_deleted_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_deleted_on_local) AS files_deleted_on_local,				
					SUM(files_deleted_on_removable) AS files_deleted_on_removable,				
					SUM(files_deleted_on_network) AS files_deleted_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					AND a.client_id = var_client_ids_str
					GROUP BY a.user_id
					ORDER BY a.files_deleted_on_network DESC, a.files_deleted_on_removable DESC, a.files_deleted_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id			
				ORDER BY a.files_deleted_on_network DESC, a.files_deleted_on_removable DESC, a.files_deleted_on_local DESC;
			END IF;
			
		ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_deleted_on_local,
			a.files_deleted_on_removable,
			a.files_deleted_on_network
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_deleted_on_local) AS files_deleted_on_local,				
				SUM(files_deleted_on_removable) AS files_deleted_on_removable,				
				SUM(files_deleted_on_network) AS files_deleted_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_deleted_on_network DESC, a.files_deleted_on_removable DESC, a.files_deleted_on_local DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id			
			ORDER BY a.files_deleted_on_network DESC, a.files_deleted_on_removable DESC, a.files_deleted_on_local DESC;
		END IF;
	
	-- CONDITION : 3
	ELSEIF par_event_id = 3 AND par_location_id = 0 THEN -- 30
	
		IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_renamed_on_local,
				a.files_renamed_on_removable,
				a.files_renamed_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_renamed_on_local) AS files_renamed_on_local,
					SUM(files_renamed_on_removable) AS files_renamed_on_removable,
					SUM(files_renamed_on_network) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_network DESC, a.files_renamed_on_removable DESC, a.files_renamed_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_network DESC, a.files_renamed_on_removable DESC, a.files_renamed_on_local DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_renamed_on_local,
				a.files_renamed_on_removable,
				a.files_renamed_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_renamed_on_local) AS files_renamed_on_local,
					SUM(files_renamed_on_removable) AS files_renamed_on_removable,
					SUM(files_renamed_on_network) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_network DESC, a.files_renamed_on_removable DESC, a.files_renamed_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_network DESC, a.files_renamed_on_removable DESC, a.files_renamed_on_local DESC;
			END IF;
			
		ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_renamed_on_local,
			a.files_renamed_on_removable,
			a.files_renamed_on_network
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_renamed_on_local) AS files_renamed_on_local,
				SUM(files_renamed_on_removable) AS files_renamed_on_removable,
				SUM(files_renamed_on_network) AS files_renamed_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_renamed_on_network DESC, a.files_renamed_on_removable DESC, a.files_renamed_on_local DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_renamed_on_network DESC, a.files_renamed_on_removable DESC, a.files_renamed_on_local DESC;
			
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_renamed_on_local,
				a.files_renamed_on_removable,
				a.files_renamed_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_renamed_on_local) AS files_renamed_on_local,
					SUM(files_renamed_on_removable) AS files_renamed_on_removable,
					SUM(files_renamed_on_network) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_network DESC, a.files_renamed_on_removable DESC, a.files_renamed_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id			
				ORDER BY a.files_renamed_on_network DESC, a.files_renamed_on_removable DESC, a.files_renamed_on_local DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_renamed_on_local,
				a.files_renamed_on_removable,
				a.files_renamed_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_renamed_on_local) AS files_renamed_on_local,
					SUM(files_renamed_on_removable) AS files_renamed_on_removable,
					SUM(files_renamed_on_network) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_network DESC, a.files_renamed_on_removable DESC, a.files_renamed_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id			
				ORDER BY a.files_renamed_on_network DESC, a.files_renamed_on_removable DESC, a.files_renamed_on_local DESC;
			END IF;
			
		ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_renamed_on_local,
			a.files_renamed_on_removable,
			a.files_renamed_on_network
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_renamed_on_local) AS files_renamed_on_local,
				SUM(files_renamed_on_removable) AS files_renamed_on_removable,
				SUM(files_renamed_on_network) AS files_renamed_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_renamed_on_network DESC, a.files_renamed_on_removable DESC, a.files_renamed_on_local DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id			
			ORDER BY a.files_renamed_on_network DESC, a.files_renamed_on_removable DESC, a.files_renamed_on_local DESC;
		END IF;
	
	-- CONDITION : 4
	ELSEIF par_event_id = 0 AND par_location_id = 1 THEN -- 01
	
		IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_local,
				a.files_deleted_on_local,
				a.files_renamed_on_local
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_local) AS files_copied_on_local,
					SUM(files_deleted_on_local) AS files_deleted_on_local,
					SUM(files_renamed_on_local) AS files_renamed_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_local DESC, a.files_deleted_on_local DESC, a.files_copied_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_local DESC, a.files_deleted_on_local DESC, a.files_copied_on_local DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_local,
				a.files_deleted_on_local,
				a.files_renamed_on_local
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_local) AS files_copied_on_local,
					SUM(files_deleted_on_local) AS files_deleted_on_local,
					SUM(files_renamed_on_local) AS files_renamed_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_local DESC, a.files_deleted_on_local DESC, a.files_copied_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_local DESC, a.files_deleted_on_local DESC, a.files_copied_on_local DESC;
			END IF;
			
		ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_copied_on_local,
			a.files_deleted_on_local,
			a.files_renamed_on_local
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_copied_on_local) AS files_copied_on_local,
				SUM(files_deleted_on_local) AS files_deleted_on_local,
				SUM(files_renamed_on_local) AS files_renamed_on_local
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_renamed_on_local DESC, a.files_deleted_on_local DESC, a.files_copied_on_local DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_renamed_on_local DESC, a.files_deleted_on_local DESC, a.files_copied_on_local DESC;
			
		ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_local,
				a.files_deleted_on_local,
				a.files_renamed_on_local
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_local) AS files_copied_on_local,
					SUM(files_deleted_on_local) AS files_deleted_on_local,
					SUM(files_renamed_on_local) AS files_renamed_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_local DESC, a.files_deleted_on_local DESC, a.files_copied_on_local DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_renamed_on_local DESC, a.files_deleted_on_local DESC, a.files_copied_on_local DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_local,
				a.files_deleted_on_local,
				a.files_renamed_on_local
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_local) AS files_copied_on_local,
					SUM(files_deleted_on_local) AS files_deleted_on_local,
					SUM(files_renamed_on_local) AS files_renamed_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_local DESC, a.files_deleted_on_local DESC, a.files_copied_on_local DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_renamed_on_local DESC, a.files_deleted_on_local DESC, a.files_copied_on_local DESC;
			END IF;
			
		ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_copied_on_local,
			a.files_deleted_on_local,
			a.files_renamed_on_local
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_copied_on_local) AS files_copied_on_local,
				SUM(files_deleted_on_local) AS files_deleted_on_local,
				SUM(files_renamed_on_local) AS files_renamed_on_local
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_renamed_on_local DESC, a.files_deleted_on_local DESC, a.files_copied_on_local DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_renamed_on_local DESC, a.files_deleted_on_local DESC, a.files_copied_on_local DESC;
		END IF;
	
	-- CONDITION : 5
	ELSEIF par_event_id = 0 AND par_location_id = 2 THEN -- 02
	
		IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_removable,
				a.files_deleted_on_removable,
				a.files_renamed_on_removable
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_removable) AS files_copied_on_removable,
					SUM(files_deleted_on_removable) AS files_deleted_on_removable,
					SUM(files_renamed_on_removable) AS files_renamed_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_removable DESC, a.files_deleted_on_removable DESC, a.files_copied_on_removable DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_removable DESC, a.files_deleted_on_removable DESC, a.files_copied_on_removable DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_removable,
				a.files_deleted_on_removable,
				a.files_renamed_on_removable
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_removable) AS files_copied_on_removable,
					SUM(files_deleted_on_removable) AS files_deleted_on_removable,
					SUM(files_renamed_on_removable) AS files_renamed_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_removable DESC, a.files_deleted_on_removable DESC, a.files_copied_on_removable DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_removable DESC, a.files_deleted_on_removable DESC, a.files_copied_on_removable DESC;
			END IF;
			
		ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_copied_on_removable,
			a.files_deleted_on_removable,
			a.files_renamed_on_removable
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_copied_on_removable) AS files_copied_on_removable,
				SUM(files_deleted_on_removable) AS files_deleted_on_removable,
				SUM(files_renamed_on_removable) AS files_renamed_on_removable
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_renamed_on_removable DESC, a.files_deleted_on_removable DESC, a.files_copied_on_removable DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_renamed_on_removable DESC, a.files_deleted_on_removable DESC, a.files_copied_on_removable DESC;
			
		ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_removable,
				a.files_deleted_on_removable,
				a.files_renamed_on_removable
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_removable) AS files_copied_on_removable,
					SUM(files_deleted_on_removable) AS files_deleted_on_removable,
					SUM(files_renamed_on_removable) AS files_renamed_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_removable DESC, a.files_deleted_on_removable DESC, a.files_copied_on_removable DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_removable DESC, a.files_deleted_on_removable DESC, a.files_copied_on_removable DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_removable,
				a.files_deleted_on_removable,
				a.files_renamed_on_removable
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_removable) AS files_copied_on_removable,
					SUM(files_deleted_on_removable) AS files_deleted_on_removable,
					SUM(files_renamed_on_removable) AS files_renamed_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_removable DESC, a.files_deleted_on_removable DESC, a.files_copied_on_removable DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_removable DESC, a.files_deleted_on_removable DESC, a.files_copied_on_removable DESC;
			END IF;
			
		ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_copied_on_removable,
			a.files_deleted_on_removable,
			a.files_renamed_on_removable
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_copied_on_removable) AS files_copied_on_removable,
				SUM(files_deleted_on_removable) AS files_deleted_on_removable,
				SUM(files_renamed_on_removable) AS files_renamed_on_removable
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_renamed_on_removable DESC, a.files_deleted_on_removable DESC, a.files_copied_on_removable DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_renamed_on_removable DESC, a.files_deleted_on_removable DESC, a.files_copied_on_removable DESC;
		END IF;
	
	-- CONDITION : 6
	ELSEIF par_event_id = 0 AND par_location_id = 3 THEN -- 03
	
		IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_network,
				a.files_deleted_on_network,
				a.files_renamed_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_network) AS files_copied_on_network,
					SUM(files_deleted_on_network) AS files_deleted_on_network,
					SUM(files_renamed_on_network) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_network DESC, a.files_deleted_on_network DESC, a.files_copied_on_network DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_network DESC, a.files_deleted_on_network DESC, a.files_copied_on_network DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_network,
				a.files_deleted_on_network,
				a.files_renamed_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_network) AS files_copied_on_network,
					SUM(files_deleted_on_network) AS files_deleted_on_network,
					SUM(files_renamed_on_network) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_network DESC, a.files_deleted_on_network DESC, a.files_copied_on_network DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_network DESC, a.files_deleted_on_network DESC, a.files_copied_on_network DESC;
			END IF;
			
		ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_copied_on_network,
			a.files_deleted_on_network,
			a.files_renamed_on_network
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_copied_on_network) AS files_copied_on_network,
				SUM(files_deleted_on_network) AS files_deleted_on_network,
				SUM(files_renamed_on_network) AS files_renamed_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_renamed_on_network DESC, a.files_deleted_on_network DESC, a.files_copied_on_network DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_renamed_on_network DESC, a.files_deleted_on_network DESC, a.files_copied_on_network DESC;
			
		ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_network,
				a.files_deleted_on_network,
				a.files_renamed_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_network) AS files_copied_on_network,
					SUM(files_deleted_on_network) AS files_deleted_on_network,
					SUM(files_renamed_on_network) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_network DESC, a.files_deleted_on_network DESC, a.files_copied_on_network DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_network DESC, a.files_deleted_on_network DESC, a.files_copied_on_network DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_network,
				a.files_deleted_on_network,
				a.files_renamed_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_network) AS files_copied_on_network,
					SUM(files_deleted_on_network) AS files_deleted_on_network,
					SUM(files_renamed_on_network) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_network DESC, a.files_deleted_on_network DESC, a.files_copied_on_network DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_network DESC, a.files_deleted_on_network DESC, a.files_copied_on_network DESC;
			END IF;
			
		ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_copied_on_network,
			a.files_deleted_on_network,
			a.files_renamed_on_network
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_copied_on_network) AS files_copied_on_network,
				SUM(files_deleted_on_network) AS files_deleted_on_network,
				SUM(files_renamed_on_network) AS files_renamed_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_renamed_on_network DESC, a.files_deleted_on_network DESC, a.files_copied_on_network DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_renamed_on_network DESC, a.files_deleted_on_network DESC, a.files_copied_on_network DESC;
		END IF;
	
	-- CONDITION : 7
	ELSEIF par_event_id = 1 AND par_location_id = 1 THEN -- 11
	
		IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_local
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_local) AS files_copied_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_copied_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_copied_on_local DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_local
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_local) AS files_copied_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_copied_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_copied_on_local DESC;
			END IF;
			
		ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_copied_on_local
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_copied_on_local) AS files_copied_on_local
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_copied_on_local DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_copied_on_local DESC;
			
		ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_local
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_local) AS files_copied_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_copied_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_copied_on_local DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_local
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_local) AS files_copied_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_copied_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_copied_on_local DESC;
			END IF;
			
		ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_copied_on_local
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_copied_on_local) AS files_copied_on_local
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_copied_on_local DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_copied_on_local DESC;
		END IF;
	
	-- CONDITION : 8
	ELSEIF par_event_id = 1 AND par_location_id = 2 THEN -- 12
	
		IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_removable
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_removable) AS files_copied_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_copied_on_removable DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_copied_on_removable DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_removable
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_removable) AS files_copied_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_copied_on_removable DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_copied_on_removable DESC;
			END IF;
			
		ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_copied_on_removable
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_copied_on_removable) AS files_copied_on_removable
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_copied_on_removable DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_copied_on_removable DESC;
			
		ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_removable
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_removable) AS files_copied_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_copied_on_removable DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_copied_on_removable DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_removable
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_removable) AS files_copied_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_copied_on_removable DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_copied_on_removable DESC;
			END IF;
			
		ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_copied_on_removable
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_copied_on_removable) AS files_copied_on_removable
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_copied_on_removable DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_copied_on_removable DESC;
		END IF;
	
	-- CONDITION : 9
	ELSEIF par_event_id = 1 AND par_location_id = 3 THEN -- 13
	
		IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_network) AS files_copied_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_copied_on_network DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_copied_on_network DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_network) AS files_copied_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_copied_on_network DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_copied_on_network DESC;
			END IF;
			
		ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_copied_on_network
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_copied_on_network) AS files_copied_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_copied_on_network DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_copied_on_network DESC;
			
		ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_network) AS files_copied_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_copied_on_network DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_copied_on_network DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_copied_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_copied_on_network) AS files_copied_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_copied_on_network DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_copied_on_network DESC;
			END IF;
			
		ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_copied_on_network
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_copied_on_network) AS files_copied_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_copied_on_network DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_copied_on_network DESC;
		END IF;
	
	-- CONDITION : 10
	ELSEIF par_event_id = 2 AND par_location_id = 1 THEN -- 21
	
		IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_deleted_on_local
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_deleted_on_local) AS files_deleted_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_deleted_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_deleted_on_local DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_deleted_on_local
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_deleted_on_local) AS files_deleted_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_deleted_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_deleted_on_local DESC;
			END IF;
			
		ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_deleted_on_local
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_deleted_on_local) AS files_deleted_on_local
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_deleted_on_local DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_deleted_on_local DESC;
			
		ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_deleted_on_local
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_deleted_on_local) AS files_deleted_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_deleted_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_deleted_on_local DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_deleted_on_local
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_deleted_on_local) AS files_deleted_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_deleted_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_deleted_on_local DESC;
			END IF;
			
		ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_deleted_on_local
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_deleted_on_local) AS files_deleted_on_local
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_deleted_on_local DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_deleted_on_local DESC;
		END IF;
	
	-- CONDITION : 11
	ELSEIF par_event_id = 2 AND par_location_id = 2 THEN -- 22
	
		IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_deleted_on_removable
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_deleted_on_removable) AS files_deleted_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_deleted_on_removable DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_deleted_on_removable DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_deleted_on_removable
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_deleted_on_removable) AS files_deleted_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_deleted_on_removable DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_deleted_on_removable DESC;
			END IF;
			
		ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_deleted_on_removable
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_deleted_on_removable) AS files_deleted_on_removable
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_deleted_on_removable DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_deleted_on_removable DESC;
			
		ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_deleted_on_removable
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_deleted_on_removable) AS files_deleted_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_deleted_on_removable DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_deleted_on_removable DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_deleted_on_removable
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_deleted_on_removable) AS files_deleted_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_deleted_on_removable DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_deleted_on_removable DESC;
			END IF;
			
		ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_deleted_on_removable
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_deleted_on_removable) AS files_deleted_on_removable
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_deleted_on_removable DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_deleted_on_removable DESC;
		END IF;
	
	-- CONDITION : 12
	ELSEIF par_event_id = 2 AND par_location_id = 3 THEN -- 23
	
		IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_deleted_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_deleted_on_network) AS files_deleted_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_deleted_on_network DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_deleted_on_network DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_deleted_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_deleted_on_network) AS files_deleted_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_deleted_on_network DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_deleted_on_network DESC;
			END IF;
			
		ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_deleted_on_network
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_deleted_on_network) AS files_deleted_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_deleted_on_network DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_deleted_on_network DESC;
			
		ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_deleted_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_deleted_on_network) AS files_deleted_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_deleted_on_network DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_deleted_on_network DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_deleted_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_deleted_on_network) AS files_deleted_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_deleted_on_network DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_deleted_on_network DESC;
			END IF;
			
		ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_deleted_on_network
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_deleted_on_network) AS files_deleted_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_deleted_on_network DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_deleted_on_network DESC;
		END IF;
	
	-- CONDITION : 13
	ELSEIF par_event_id = 3 AND par_location_id = 1 THEN -- 31
	
		IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_renamed_on_local
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_renamed_on_local) AS files_renamed_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_local DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_renamed_on_local
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_renamed_on_local) AS files_renamed_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_local DESC;
			END IF;
			
		ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_renamed_on_local
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_renamed_on_local) AS files_renamed_on_local
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_renamed_on_local DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_renamed_on_local DESC;
			
		ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_renamed_on_local
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_renamed_on_local) AS files_renamed_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_local DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_renamed_on_local
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_renamed_on_local) AS files_renamed_on_local
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_local DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_local DESC;
			END IF;
			
		ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_renamed_on_local
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_renamed_on_local) AS files_renamed_on_local
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_renamed_on_local DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_renamed_on_local DESC;
		END IF;
	
	-- CONDITION : 14
	ELSEIF par_event_id = 3 AND par_location_id = 2 THEN -- 32
	
		IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_renamed_on_removable
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_renamed_on_removable) AS files_renamed_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_removable DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_removable DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_renamed_on_removable
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_renamed_on_removable) AS files_renamed_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_removable DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_removable DESC;
			END IF;
			
		ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_renamed_on_removable
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_renamed_on_removable) AS files_renamed_on_removable
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_renamed_on_removable DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_renamed_on_removable DESC;
			
		ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_renamed_on_removable
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_renamed_on_removable) AS files_renamed_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_removable DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_removable DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_renamed_on_removable
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_renamed_on_removable) AS files_renamed_on_removable
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_removable DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_removable DESC;
			END IF;
			
		ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_renamed_on_removable
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_renamed_on_removable) AS files_renamed_on_removable
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_renamed_on_removable DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_renamed_on_removable DESC;
		END IF;
	
	-- CONDITION : 15
	ELSEIF par_event_id = 3 AND par_location_id = 3 THEN -- 33
	
		IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_renamed_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_renamed_on_network) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_network DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_network DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_renamed_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_renamed_on_network) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_network DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_network DESC;
			END IF;
			
		ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_renamed_on_network
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_renamed_on_network) AS files_renamed_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_renamed_on_network DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_renamed_on_network DESC;
			
		ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_renamed_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_renamed_on_network) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_network DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_network DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT b.username, c.computer_name, a.user_id, a.client_id,
				a.files_renamed_on_network
				FROM
				(
					SELECT `user_id`, client_id,
					SUM(files_renamed_on_network) AS files_renamed_on_network
					
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (var_user_id IS NULL OR a.user_id=var_user_id)
					GROUP BY a.user_id
					ORDER BY a.files_renamed_on_network DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.client_id=c.client_id AND a.user_id=b.user_id
				ORDER BY a.files_renamed_on_network DESC;
			END IF;
			
		ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
			SELECT b.username, c.computer_name, a.user_id, a.client_id,
			a.files_renamed_on_network
			FROM
			(
				SELECT `user_id`, client_id,
				SUM(files_renamed_on_network) AS files_renamed_on_network
				
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (var_user_id IS NULL OR a.user_id=var_user_id)
				GROUP BY a.user_id
				ORDER BY a.files_renamed_on_network DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.client_id=c.client_id AND a.user_id=b.user_id
			ORDER BY a.files_renamed_on_network DESC;
		END IF;
	
	END IF;
END$$
DELIMITER ;

DROP PROCEDURE IF EXISTS `fam_rpt_get_count_pr`;
DELIMITER $$
CREATE PROCEDURE `fam_rpt_get_count_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_event_id INT(11),
	IN par_location_id INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone='+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT IFNULL(
			SUM(files_copied_on_local)+SUM(files_copied_on_removable)+SUM(files_copied_on_network)+
			SUM(files_deleted_on_local)+SUM(files_deleted_on_removable)+SUM(files_deleted_on_network)+
			SUM(files_renamed_on_local)+SUM(files_renamed_on_removable)+SUM(files_renamed_on_network)
		, 0) AS record_count
		FROM rpt_fam_chart_summary;
	ELSE
		SELECT user_id INTO var_user_id FROM rpt_user_master WHERE par_username<>'' AND username=par_username LIMIT 1;
		-- CONDITION : 1
		IF par_event_id = 1 AND par_location_id = 0 THEN -- 10
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00	
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_copied_on_local)+SUM(files_copied_on_removable)+SUM(files_copied_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_copied_on_local)+SUM(files_copied_on_removable)+SUM(files_copied_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
				SELECT IFNULL(SUM(files_copied_on_local)+SUM(files_copied_on_removable)+SUM(files_copied_on_network), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_copied_on_local)+SUM(files_copied_on_removable)+SUM(files_copied_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_copied_on_local)+SUM(files_copied_on_removable)+SUM(files_copied_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT IFNULL(SUM(files_copied_on_local)+SUM(files_copied_on_removable)+SUM(files_copied_on_network), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second) 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
			
		-- CONDITION : 2
		ELSEIF par_event_id = 2 AND par_location_id = 0 THEN -- 20
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00	
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_deleted_on_local)+SUM(files_deleted_on_removable)+SUM(files_deleted_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_deleted_on_local)+SUM(files_deleted_on_removable)+SUM(files_deleted_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
				SELECT IFNULL(SUM(files_deleted_on_local)+SUM(files_deleted_on_removable)+SUM(files_deleted_on_network), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_deleted_on_local)+SUM(files_deleted_on_removable)+SUM(files_deleted_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_deleted_on_local)+SUM(files_deleted_on_removable)+SUM(files_deleted_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT IFNULL(SUM(files_deleted_on_local)+SUM(files_deleted_on_removable)+SUM(files_deleted_on_network), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second) 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 3
		ELSEIF par_event_id = 3 AND par_location_id = 0 THEN -- 30
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00	
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_renamed_on_local)+SUM(files_renamed_on_removable)+SUM(files_renamed_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_renamed_on_local)+SUM(files_renamed_on_removable)+SUM(files_renamed_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
				SELECT IFNULL(SUM(files_renamed_on_local)+SUM(files_renamed_on_removable)+SUM(files_renamed_on_network), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_renamed_on_local)+SUM(files_renamed_on_removable)+SUM(files_renamed_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_renamed_on_local)+SUM(files_renamed_on_removable)+SUM(files_renamed_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT IFNULL(SUM(files_renamed_on_local)+SUM(files_renamed_on_removable)+SUM(files_renamed_on_network), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second) 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 4
		ELSEIF par_event_id = 0 AND par_location_id = 1 THEN -- 01
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00	
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_copied_on_local)+SUM(files_deleted_on_local)+SUM(files_renamed_on_local), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_copied_on_local)+SUM(files_deleted_on_local)+SUM(files_renamed_on_local), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
				SELECT IFNULL(SUM(files_copied_on_local)+SUM(files_deleted_on_local)+SUM(files_renamed_on_local), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_copied_on_local)+SUM(files_deleted_on_local)+SUM(files_renamed_on_local), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_copied_on_local)+SUM(files_deleted_on_local)+SUM(files_renamed_on_local), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT IFNULL(SUM(files_copied_on_local)+SUM(files_deleted_on_local)+SUM(files_renamed_on_local), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second) 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 5
		ELSEIF par_event_id = 0 AND par_location_id = 2 THEN -- 02
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00	
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_copied_on_removable)+SUM(files_deleted_on_removable)+SUM(files_renamed_on_removable), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_copied_on_removable)+SUM(files_deleted_on_removable)+SUM(files_renamed_on_removable), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
				SELECT IFNULL(SUM(files_copied_on_removable)+SUM(files_deleted_on_removable)+SUM(files_renamed_on_removable), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_copied_on_removable)+SUM(files_deleted_on_removable)+SUM(files_renamed_on_removable), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_copied_on_removable)+SUM(files_deleted_on_removable)+SUM(files_renamed_on_removable), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT IFNULL(SUM(files_copied_on_removable)+SUM(files_deleted_on_removable)+SUM(files_renamed_on_removable), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second) 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 6
		ELSEIF par_event_id = 0 AND par_location_id = 3 THEN -- 03
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00	
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_copied_on_network)+SUM(files_deleted_on_network)+SUM(files_renamed_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_copied_on_network)+SUM(files_deleted_on_network)+SUM(files_renamed_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
				SELECT IFNULL(SUM(files_copied_on_network)+SUM(files_deleted_on_network)+SUM(files_renamed_on_network), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_copied_on_network)+SUM(files_deleted_on_network)+SUM(files_renamed_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_copied_on_network)+SUM(files_deleted_on_network)+SUM(files_renamed_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT IFNULL(SUM(files_copied_on_network)+SUM(files_deleted_on_network)+SUM(files_renamed_on_network), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second) 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 7
		ELSEIF par_event_id = 1 AND par_location_id = 1 THEN -- 11
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00	
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_copied_on_local), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_copied_on_local), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
				SELECT IFNULL(SUM(files_copied_on_local), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_copied_on_local), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_copied_on_local), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT IFNULL(SUM(files_copied_on_local), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second) 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 8
		ELSEIF par_event_id = 1 AND par_location_id = 2 THEN -- 12
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00	
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_copied_on_removable), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_copied_on_removable), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
				SELECT IFNULL(SUM(files_copied_on_removable), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_copied_on_removable), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_copied_on_removable), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT IFNULL(SUM(files_copied_on_removable), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second) 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 9
		ELSEIF par_event_id = 1 AND par_location_id = 3 THEN -- 13
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00	
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_copied_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_copied_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
				SELECT IFNULL(SUM(files_copied_on_network), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_copied_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_copied_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT IFNULL(SUM(files_copied_on_network), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second) 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 10
		ELSEIF par_event_id = 2 AND par_location_id = 1 THEN -- 21
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00	
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_deleted_on_local), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_deleted_on_local), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
				SELECT IFNULL(SUM(files_deleted_on_local), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_deleted_on_local), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_deleted_on_local), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT IFNULL(SUM(files_deleted_on_local), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second) 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 11
		ELSEIF par_event_id = 2 AND par_location_id = 2 THEN -- 22
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00	
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_deleted_on_removable), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_deleted_on_removable), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
				SELECT IFNULL(SUM(files_deleted_on_removable), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_deleted_on_removable), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_deleted_on_removable), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT IFNULL(SUM(files_deleted_on_removable), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second) 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 12
		ELSEIF par_event_id = 2 AND par_location_id = 3 THEN -- 23
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00	
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_deleted_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_deleted_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
				SELECT IFNULL(SUM(files_deleted_on_network), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_deleted_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_deleted_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT IFNULL(SUM(files_deleted_on_network), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second) 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 13
		ELSEIF par_event_id = 3 AND par_location_id = 1 THEN -- 31
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00	
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_renamed_on_local), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_renamed_on_local), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
				SELECT IFNULL(SUM(files_renamed_on_local), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_renamed_on_local), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_renamed_on_local), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT IFNULL(SUM(files_renamed_on_local), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second) 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 14
		ELSEIF par_event_id = 3 AND par_location_id = 2 THEN -- 32
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00	
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_renamed_on_removable), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_renamed_on_removable), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
				SELECT IFNULL(SUM(files_renamed_on_removable), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_renamed_on_removable), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_renamed_on_removable), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT IFNULL(SUM(files_renamed_on_removable), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second) 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 15
		ELSEIF par_event_id = 3 AND par_location_id = 3 THEN -- 33
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00	
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_renamed_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_renamed_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
				SELECT IFNULL(SUM(files_renamed_on_network), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(files_renamed_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(files_renamed_on_network), 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT IFNULL(SUM(files_renamed_on_network), 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second) 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		-- CONDITION : 16
		ELSEIF par_event_id = 0 AND par_location_id = 0 THEN -- 00
		
			IF par_group_id <> 0 AND par_client_name<> '' THEN -- 00	
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(
						SUM(files_copied_on_local)+SUM(files_copied_on_removable)+SUM(files_copied_on_network)+
						SUM(files_deleted_on_local)+SUM(files_deleted_on_removable)+SUM(files_deleted_on_network)+
						SUM(files_renamed_on_local)+SUM(files_renamed_on_removable)+SUM(files_renamed_on_network)
					, 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(
						SUM(files_copied_on_local)+SUM(files_copied_on_removable)+SUM(files_copied_on_network)+
						SUM(files_deleted_on_local)+SUM(files_deleted_on_removable)+SUM(files_deleted_on_network)+
						SUM(files_renamed_on_local)+SUM(files_renamed_on_removable)+SUM(files_renamed_on_network)
					, 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id<>0 AND par_client_name='' THEN -- 0X		
				SELECT IFNULL(
					SUM(files_copied_on_local)+SUM(files_copied_on_removable)+SUM(files_copied_on_network)+
					SUM(files_deleted_on_local)+SUM(files_deleted_on_removable)+SUM(files_deleted_on_network)+
					SUM(files_renamed_on_local)+SUM(files_renamed_on_removable)+SUM(files_renamed_on_network)
				, 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
				
			ELSEIF par_group_id=0 AND par_client_name<>'' THEN -- X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(
						SUM(files_copied_on_local)+SUM(files_copied_on_removable)+SUM(files_copied_on_network)+
						SUM(files_deleted_on_local)+SUM(files_deleted_on_removable)+SUM(files_deleted_on_network)+
						SUM(files_renamed_on_local)+SUM(files_renamed_on_removable)+SUM(files_renamed_on_network)
					, 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(
						SUM(files_copied_on_local)+SUM(files_copied_on_removable)+SUM(files_copied_on_network)+
						SUM(files_deleted_on_local)+SUM(files_deleted_on_removable)+SUM(files_deleted_on_network)+
						SUM(files_renamed_on_local)+SUM(files_renamed_on_removable)+SUM(files_renamed_on_network)
					, 0) AS record_count
					FROM rpt_fam_chart_summary AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (var_user_id IS NULL OR a.user_id=var_user_id);
				END IF;
				
			ELSEIF par_group_id=0 AND par_client_name='' THEN -- XX
				SELECT IFNULL(
					SUM(files_copied_on_local)+SUM(files_copied_on_removable)+SUM(files_copied_on_network)+
					SUM(files_deleted_on_local)+SUM(files_deleted_on_removable)+SUM(files_deleted_on_network)+
					SUM(files_renamed_on_local)+SUM(files_renamed_on_removable)+SUM(files_renamed_on_network)
				, 0) AS record_count
				FROM rpt_fam_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second) 
				AND (var_user_id IS NULL OR a.user_id=var_user_id);
			END IF;
		
		END IF;
	
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `fam_rpt_get_summ_details_pr`;
DELIMITER $$
CREATE PROCEDURE `fam_rpt_get_summ_details_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id BIGINT(11),
	IN par_client_id BIGINT(20),
	IN par_event_id BIGINT(11),
	IN par_location_id BIGINT(11),
	IN par_user_id BIGINT(11),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN

	SET time_zone='+00:00';
	
	IF par_group_id <> 0 THEN -- 0
		SELECT incident_date,
		location_id,event_id,event_details,b.file_name, d.username,
		t1.client_id,computer_name,domain_name, incident_date_in_seconds
		FROM
		(
			SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
			`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
			FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
			WHERE
			a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
			AND (par_client_id = -1 OR a.client_id = par_client_id)
			AND a.GroupId = par_group_id
			AND a.event_id = par_event_id
			AND a.location_id = par_location_id
			AND a.user_id = par_user_id
			ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
		) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
		WHERE 
		t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
		ORDER BY incident_date DESC;
	ELSEIF par_group_id=0 THEN -- X
		SELECT incident_date,
		location_id,event_id,event_details,b.file_name, d.username,
		t1.client_id,computer_name,domain_name, incident_date_in_seconds
		FROM
		(
			SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
			`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
			FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id)
			WHERE
			a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
			AND (par_client_id = -1 OR a.client_id = par_client_id)
			AND a.event_id = par_event_id
			AND a.location_id = par_location_id
			AND a.user_id = par_user_id
			ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
		) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
		WHERE 
		t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
		ORDER BY incident_date DESC;
	
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `fam_rpt_get_table_pr`;
DELIMITER $$
CREATE PROCEDURE `fam_rpt_get_table_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_event_id INT(11),
	IN par_location_id INT(11),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone='+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT incident_date,
		location_id,event_id,event_details,b.file_name, d.username,
		t1.client_id,computer_name,domain_name, incident_date_in_seconds
		FROM
		(
			SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
			`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
			FROM rpt_fam_incident_fact AS a
			ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
		) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
		WHERE 
		t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
		ORDER BY incident_date DESC;
	ELSE
		IF par_username='' THEN
			IF par_group_id <> 0 AND par_client_name<> '' AND par_event_id <> 0 AND par_location_id <> 0 THEN -- 0000
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date,
					location_id,event_id,event_details,b.file_name, d.username,
					t1.client_id,computer_name,domain_name, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
						`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
						FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						AND a.event_id = par_event_id
						AND a.location_id = par_location_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date,
					location_id,event_id,event_details,b.file_name, d.username,
					t1.client_id,computer_name,domain_name, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
						`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
						FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.GroupId = par_group_id
						AND a.event_id = par_event_id
						AND a.location_id = par_location_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSEIF par_group_id<>0 AND par_client_name<>'' AND par_event_id<>0 AND par_location_id=0 THEN -- 000X
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date,
					location_id,event_id,event_details,b.file_name, d.username,
					t1.client_id,computer_name,domain_name, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
						`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
						FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						AND a.event_id = par_event_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date,
					location_id,event_id,event_details,b.file_name, d.username,
					t1.client_id,computer_name,domain_name, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
						`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
						FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.GroupId = par_group_id
						AND a.event_id = par_event_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSEIF par_group_id<>0 AND par_client_name<>'' AND par_event_id=0 AND par_location_id<>0 THEN -- 00X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date,
					location_id,event_id,event_details,b.file_name, d.username,
					t1.client_id,computer_name,domain_name, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
						`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
						FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						AND a.location_id = par_location_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date,
					location_id,event_id,event_details,b.file_name, d.username,
					t1.client_id,computer_name,domain_name, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
						`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
						FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.GroupId = par_group_id
						AND a.location_id = par_location_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSEIF par_group_id<>0 AND par_client_name='' AND par_event_id<>0 AND par_location_id<>0 THEN -- 0X00		
				SELECT incident_date,
				location_id,event_id,event_details,b.file_name, d.username,
				t1.client_id,computer_name,domain_name, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
					`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
					FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					AND a.event_id = par_event_id
					AND a.location_id = par_location_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
				WHERE 
				t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
				ORDER BY incident_date DESC;
			ELSEIF par_group_id=0 AND par_client_name<>'' AND par_event_id<>0 AND par_location_id<>0 THEN -- X000
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date,
					location_id,event_id,event_details,b.file_name, d.username,
					t1.client_id,computer_name,domain_name, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
						`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
						FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.event_id = par_event_id
						AND a.location_id = par_location_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date,
					location_id,event_id,event_details,b.file_name, d.username,
					t1.client_id,computer_name,domain_name, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
						`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
						FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.event_id = par_event_id
						AND a.location_id = par_location_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSEIF par_group_id<>0 AND par_client_name<>'' AND par_event_id=0 AND par_location_id=0 THEN -- 00XX
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date,
					location_id,event_id,event_details,b.file_name, d.username,
					t1.client_id,computer_name,domain_name, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
						`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
						FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date,
					location_id,event_id,event_details,b.file_name, d.username,
					t1.client_id,computer_name,domain_name, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
						`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
						FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.GroupId = par_group_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSEIF par_group_id<>0 AND par_client_name='' AND par_event_id=0 AND par_location_id<>0 THEN -- 0XX0
				SELECT incident_date,
				location_id,event_id,event_details,b.file_name, d.username,
				t1.client_id,computer_name,domain_name, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
					`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
					FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					AND a.location_id = par_location_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
				WHERE 
				t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
				ORDER BY incident_date DESC;
			ELSEIF par_group_id=0 AND par_client_name='' AND par_event_id<>0 AND par_location_id<>0 THEN -- XX00
				SELECT incident_date,
				location_id,event_id,event_details,b.file_name, d.username,
				t1.client_id,computer_name,domain_name, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
					`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
					FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.event_id = par_event_id
					AND a.location_id = par_location_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
				WHERE 
				t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
				ORDER BY incident_date DESC;
			ELSEIF par_group_id=0 AND par_client_name<>'' AND par_event_id<>0 AND par_location_id=0 THEN -- X00X
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date,
					location_id,event_id,event_details,b.file_name, d.username,
					t1.client_id,computer_name,domain_name, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
						`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
						FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.event_id = par_event_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date,
					location_id,event_id,event_details,b.file_name, d.username,
					t1.client_id,computer_name,domain_name, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
						`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
						FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.event_id = par_event_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSEIF par_group_id<>0 AND par_client_name='' AND par_event_id<>0 AND par_location_id=0 THEN -- 0X0X
				SELECT incident_date,
				location_id,event_id,event_details,b.file_name, d.username,
				t1.client_id,computer_name,domain_name, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
					`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
					FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					AND a.event_id = par_event_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
				WHERE 
				t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
				ORDER BY incident_date DESC;
			ELSEIF par_group_id=0 AND par_client_name<>'' AND par_event_id=0 AND par_location_id<>0 THEN -- X0X0
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date,
					location_id,event_id,event_details,b.file_name, d.username,
					t1.client_id,computer_name,domain_name, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
						`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
						FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.location_id = par_location_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date,
					location_id,event_id,event_details,b.file_name, d.username,
					t1.client_id,computer_name,domain_name, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
						`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
						FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.location_id = par_location_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSEIF par_group_id<>0 AND par_client_name='' AND par_event_id=0 AND par_location_id=0 THEN -- 0XXX
				SELECT incident_date,
				location_id,event_id,event_details,b.file_name, d.username,
				t1.client_id,computer_name,domain_name, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
					`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
					FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
				WHERE 
				t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
				ORDER BY incident_date DESC;
			ELSEIF par_group_id=0 AND par_client_name<>'' AND par_event_id=0 AND par_location_id=0 THEN -- X0XX
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date,
					location_id,event_id,event_details,b.file_name, d.username,
					t1.client_id,computer_name,domain_name, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
						`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
						FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date,
					location_id,event_id,event_details,b.file_name, d.username,
					t1.client_id,computer_name,domain_name, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
						`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
						FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSEIF par_group_id=0 AND par_client_name='' AND par_event_id<>0 AND par_location_id=0 THEN -- XX0X
				SELECT incident_date,
				location_id,event_id,event_details,b.file_name, d.username,
				t1.client_id,computer_name,domain_name, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
					`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
					FROM rpt_fam_incident_fact AS a FORCE INDEX(idx_incident_date)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.event_id = par_event_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
				WHERE 
				t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
				ORDER BY incident_date DESC;
			ELSEIF par_group_id=0 AND par_client_name='' AND par_event_id=0 AND par_location_id<>0 THEN -- XXX0
				SELECT incident_date,
				location_id,event_id,event_details,b.file_name, d.username,
				t1.client_id,computer_name,domain_name, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
					`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
					FROM rpt_fam_incident_fact AS a FORCE INDEX(idx_incident_date)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.location_id = par_location_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
				WHERE 
				t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
				ORDER BY incident_date DESC;
			ELSEIF par_group_id=0 AND par_client_name='' AND par_event_id=0 AND par_location_id=0 THEN -- XXXX
				SELECT incident_date,
				location_id,event_id,event_details,b.file_name, d.username,
				t1.client_id,computer_name,domain_name, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
					`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
					FROM rpt_fam_incident_fact AS a FORCE INDEX(idx_incident_date)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
				WHERE 
				t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
				ORDER BY incident_date DESC;
			END IF;
		ELSE
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username=par_username LIMIT 1;
			IF par_client_name <> '' THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master WHERE (par_group_id=0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date,
					location_id,event_id,event_details,b.file_name, d.username,
					t1.client_id,computer_name,domain_name, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
						`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
						FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_user_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.user_id=var_user_id
						AND (par_group_id=0 OR GroupId=par_group_id)
						AND (par_event_id=0 OR a.event_id = par_event_id)
						AND (par_location_id=0 OR a.location_id = par_location_id)
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date,
					location_id,event_id,event_details,b.file_name, d.username,
					t1.client_id,computer_name,domain_name, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
						`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
						FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_user_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.user_id=var_user_id
						AND (par_group_id=0 OR GroupId=par_group_id)
						AND (par_event_id=0 OR a.event_id = par_event_id)
						AND (par_location_id=0 OR a.location_id = par_location_id)
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSE
				SELECT incident_date,
				location_id,event_id,event_details,b.file_name, d.username,
				t1.client_id,computer_name,domain_name, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(`incident_date`) AS incident_date, `fam_file_id`,`location_id`, `event_id`,
					`event_details`,`client_id`,`user_id`,`GroupId`, incident_date AS incident_date_in_seconds
					FROM rpt_fam_incident_fact AS a USE INDEX(idx_incident_date, idx_user_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.user_id=var_user_id
					AND (par_group_id=0 OR GroupId=par_group_id)
					AND (par_event_id=0 OR a.event_id = par_event_id)
					AND (par_location_id=0 OR a.location_id = par_location_id)
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS t1, rpt_fam_file_master_dim AS b, client_master AS c, rpt_user_master AS d 
				WHERE 
				t1.fam_file_id=b.fam_file_id AND t1.client_id=c.client_id AND t1.user_id=d.user_id
				ORDER BY incident_date DESC;
			END IF;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `firewall_rpt_get_count_pr`;
DELIMITER $$
CREATE PROCEDURE `firewall_rpt_get_count_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT IFNULL(COUNT(*), 0) AS record_count FROM rpt_firewall_fact;
	ELSE
		IF par_username='' THEN
			IF par_client_name<> '' AND par_group_id <> 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(COUNT(*), 0) AS record_count
					FROM rpt_firewall_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id;
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(COUNT(*), 0) AS record_count
					FROM rpt_firewall_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id;
				END IF;

			ELSEIF par_client_name='' AND par_group_id<>0 THEN
				SELECT IFNULL(COUNT(*), 0) AS record_count
				FROM rpt_firewall_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id;

			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(COUNT(*), 0) AS record_count
					FROM rpt_firewall_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(COUNT(*), 0) AS record_count
					FROM rpt_firewall_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str;
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
				SELECT IFNULL(COUNT(*), 0) AS record_count
				FROM rpt_firewall_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				ORDER BY a.incident_date DESC;
			END IF;
		ELSE
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username=par_username LIMIT 1;
			IF par_client_name<> '' THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master WHERE (par_group_id=0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(COUNT(*), 0) AS record_count
					FROM rpt_firewall_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (par_group_id=0 OR a.GroupId = par_group_id)
					AND a.user_id = var_user_id;
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(COUNT(*), 0) AS record_count
					FROM rpt_firewall_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (par_group_id=0 OR a.GroupId = par_group_id)
					AND a.user_id = var_user_id;
				END IF;
			ELSE
				SELECT IFNULL(COUNT(*), 0) AS record_count
				FROM rpt_firewall_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_group_id=0 OR a.GroupId = par_group_id)
				AND a.user_id = var_user_id;
			END IF;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `firewall_rpt_get_table_pr`;
DELIMITER $$
CREATE PROCEDURE `firewall_rpt_get_table_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN	
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT incident_date,
		local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, exception_name,
		a.client_id, b.computer_name, b.domain_name, c.username, incident_date_in_seconds
		FROM
		(
			SELECT from_unixtime(incident_date) AS incident_date, client_id, a.user_id, 
			local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, 
			exception_name, incident_date AS incident_date_in_seconds
			FROM rpt_firewall_fact AS a
			ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
		) AS a, client_master AS b, rpt_user_master AS c
		WHERE 
		a.client_id=b.client_id AND a.user_id=c.user_id
		ORDER BY incident_date DESC;
	ELSE
		IF par_username='' THEN
			IF par_client_name<> '' AND par_group_id <> 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date,
					local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, exception_name,
					a.client_id, b.computer_name, b.domain_name, c.username, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, a.user_id, 
						local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, 
						exception_name, incident_date AS incident_date_in_seconds
						FROM rpt_firewall_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS b, rpt_user_master AS c
					WHERE 
					a.client_id=b.client_id AND a.user_id=c.user_id
					ORDER BY incident_date DESC;
					
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date,
					local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, exception_name,
					a.client_id, b.computer_name, b.domain_name, c.username, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, a.user_id, 
						local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, 
						exception_name, incident_date AS incident_date_in_seconds
						FROM rpt_firewall_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.GroupId = par_group_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS b, rpt_user_master AS c
					WHERE 
					a.client_id=b.client_id AND a.user_id=c.user_id
					ORDER BY incident_date DESC;
				END IF;

			ELSEIF par_client_name='' AND par_group_id<>0 THEN
				SELECT incident_date,
				local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, exception_name,
				a.client_id, b.computer_name, b.domain_name, c.username, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, a.user_id, 
					local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, 
					exception_name, incident_date AS incident_date_in_seconds
					FROM rpt_firewall_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS b, rpt_user_master AS c
				WHERE 
				a.client_id=b.client_id AND a.user_id=c.user_id
				ORDER BY incident_date DESC;

			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date,
					local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, exception_name,
					a.client_id, b.computer_name, b.domain_name, c.username, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, a.user_id, 
						local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, 
						exception_name, incident_date AS incident_date_in_seconds
						FROM rpt_firewall_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						ORDER BY a.incident_date DESC
						LIMIT par_start_index,par_max_records
					) AS a, client_master AS b, rpt_user_master AS c
					WHERE 
					a.client_id=b.client_id AND a.user_id=c.user_id
					ORDER BY incident_date DESC;
					
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date,
					local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, exception_name,
					a.client_id, b.computer_name, b.domain_name, c.username, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, a.user_id, 
						local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, 
						exception_name, incident_date AS incident_date_in_seconds
						FROM rpt_firewall_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						ORDER BY a.incident_date DESC
						LIMIT par_start_index,par_max_records
					) AS a, client_master AS b, rpt_user_master AS c
					WHERE 
					a.client_id=b.client_id AND a.user_id=c.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
				SELECT incident_date,
				local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, exception_name,
				a.client_id, b.computer_name, b.domain_name, c.username, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, a.user_id, 
					local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, 
					exception_name, incident_date AS incident_date_in_seconds
					FROM rpt_firewall_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					ORDER BY a.incident_date DESC
					LIMIT par_start_index,par_max_records
				) AS a, client_master AS b, rpt_user_master AS c
				WHERE 
				a.client_id=b.client_id AND a.user_id=c.user_id
				ORDER BY incident_date DESC;
			END IF;
		ELSE
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username=par_username LIMIT 1;
			IF par_client_name<> '' THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master 
				WHERE (par_group_id=0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date,
					local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, exception_name,
					a.client_id, b.computer_name, b.domain_name, c.username, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, a.user_id, 
						local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, 
						exception_name, incident_date AS incident_date_in_seconds
						FROM rpt_firewall_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND (par_group_id=0 OR a.GroupId = par_group_id)
						AND a.user_id = var_user_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS b, rpt_user_master AS c
					WHERE 
					a.client_id=b.client_id AND a.user_id=c.user_id
					ORDER BY incident_date DESC;
					
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date,
					local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, exception_name,
					a.client_id, b.computer_name, b.domain_name, c.username, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, a.user_id, 
						local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, 
						exception_name, incident_date AS incident_date_in_seconds
						FROM rpt_firewall_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND (par_group_id=0 OR a.GroupId = par_group_id)
						AND a.user_id = var_user_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS b, rpt_user_master AS c
					WHERE 
					a.client_id=b.client_id AND a.user_id=c.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSE
				SELECT incident_date,
				local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, exception_name,
				a.client_id, b.computer_name, b.domain_name, c.username, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, a.user_id, 
					local_ip, remote_ip, `protocol`, `direction`, firewall_level, protocol_type, local_port, remote_port, 
					exception_name, incident_date AS incident_date_in_seconds
					FROM rpt_firewall_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND (par_group_id=0 OR a.GroupId = par_group_id)
					AND a.user_id = var_user_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS b, rpt_user_master AS c
				WHERE 
				a.client_id=b.client_id AND a.user_id=c.user_id
				ORDER BY incident_date DESC;
			END IF;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `ips_rpt_get_count_pr`;
DELIMITER $$
CREATE PROCEDURE `ips_rpt_get_count_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN 
		SELECT IFNULL(SUM(incident_count), 0) AS record_count FROM rpt_ips_chart_summary;
	ELSE
		IF par_username='' THEN
			IF par_client_name<> '' AND par_group_id <> 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_ips_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id;
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_ips_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id;
				END IF;

			ELSEIF par_client_name='' AND par_group_id<>0 THEN
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_ips_chart_summary AS a USE INDEX(idx_incident_date_only, idx_GroupId)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id;

			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_ips_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_ips_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str;
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_ips_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second);
			END IF;
		ELSE
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username=par_username LIMIT 1;
			IF par_client_name<> '' THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master WHERE (par_group_id=0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_ips_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (par_group_id=0 OR a.GroupId = par_group_id)
					AND a.user_id = var_user_id;
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_ips_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (par_group_id=0 OR a.GroupId = par_group_id)
					AND a.user_id = var_user_id;
				END IF;
			ELSE
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_ips_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_group_id=0 OR a.GroupId = par_group_id)
				AND a.user_id = var_user_id;
			END IF;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `ips_rpt_get_table_pr`;
DELIMITER $$
CREATE PROCEDURE `ips_rpt_get_table_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT incident_date, a.client_id, computer_name, domain_name, c.username, vulnerability, action_taken, 
		rule_desc, rule_ref, incident_date_in_seconds
		FROM
		(
			SELECT from_unixtime(incident_date) AS incident_date, 
			client_id, a.user_id, vulnerability, action_taken, rule_desc, rule_ref, GroupId, incident_date AS incident_date_in_seconds
			FROM rpt_ips_fact AS a 
			ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
		) AS a, client_master AS b, rpt_user_master AS c
		WHERE 
		a.client_id=b.client_id AND a.user_id=c.user_id
		ORDER BY incident_date DESC;
	ELSE
		IF par_username='' THEN
			IF par_client_name<> '' AND par_group_id <> 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, c.username, vulnerability, action_taken, 
					rule_desc, rule_ref, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, 
						client_id, a.user_id, vulnerability, action_taken, rule_desc, rule_ref, GroupId, incident_date AS incident_date_in_seconds
						FROM rpt_ips_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS b, rpt_user_master AS c
					WHERE 
					a.client_id=b.client_id AND a.user_id=c.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, c.username, vulnerability, action_taken, 
					rule_desc, rule_ref, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, 
						client_id, a.user_id, vulnerability, action_taken, rule_desc, rule_ref, GroupId, incident_date AS incident_date_in_seconds
						FROM rpt_ips_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.GroupId = par_group_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS b, rpt_user_master AS c
					WHERE 
					a.client_id=b.client_id AND a.user_id=c.user_id
					ORDER BY incident_date DESC;
				END IF;

			ELSEIF par_client_name='' AND par_group_id<>0 THEN
				SELECT incident_date, a.client_id, computer_name, domain_name, c.username, vulnerability, action_taken, 
				rule_desc, rule_ref, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, 
					client_id, a.user_id, vulnerability, action_taken, rule_desc, rule_ref, GroupId, incident_date AS incident_date_in_seconds
					FROM rpt_ips_fact AS a USE INDEX(idx_incident_date, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS b, rpt_user_master AS c
				WHERE 
				a.client_id=b.client_id AND a.user_id=c.user_id
				ORDER BY incident_date DESC;

			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, c.username, vulnerability, action_taken, 
					rule_desc, rule_ref, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, 
						client_id, a.user_id, vulnerability, action_taken, rule_desc, rule_ref, GroupId, incident_date AS incident_date_in_seconds
						FROM rpt_ips_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS b, rpt_user_master AS c
					WHERE
					a.client_id=b.client_id AND a.user_id=c.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, c.username, vulnerability, action_taken, 
					rule_desc, rule_ref, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, 
						client_id, a.user_id, vulnerability, action_taken, rule_desc, rule_ref, GroupId, incident_date AS incident_date_in_seconds
						FROM rpt_ips_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS b, rpt_user_master AS c
					WHERE
					a.client_id=b.client_id AND a.user_id=c.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
				SELECT incident_date, a.client_id, computer_name, domain_name, c.username, vulnerability, action_taken, 
				rule_desc, rule_ref, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, 
					client_id, a.user_id, vulnerability, action_taken, rule_desc, rule_ref, GroupId, incident_date AS incident_date_in_seconds
					FROM rpt_ips_fact AS a
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS b, rpt_user_master AS c
				WHERE 
				a.client_id=b.client_id AND a.user_id=c.user_id
				ORDER BY incident_date DESC;
			END IF;
		
		ELSE
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username=par_username LIMIT 1;
			
			IF par_client_name<> '' THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master 
				WHERE (par_group_id=0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, c.username, vulnerability, action_taken, 
					rule_desc, rule_ref, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, 
						client_id, a.user_id, vulnerability, action_taken, rule_desc, rule_ref, GroupId, incident_date AS incident_date_in_seconds
						FROM rpt_ips_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND (par_group_id=0 OR a.GroupId = par_group_id)
						AND a.user_id = var_user_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS b, rpt_user_master AS c
					WHERE 
					a.client_id=b.client_id AND a.user_id=c.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, c.username, vulnerability, action_taken, 
					rule_desc, rule_ref, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, 
						client_id, a.user_id, vulnerability, action_taken, rule_desc, rule_ref, GroupId, incident_date AS incident_date_in_seconds
						FROM rpt_ips_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND (par_group_id=0 OR a.GroupId = par_group_id)
						AND a.user_id = var_user_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS b, rpt_user_master AS c
					WHERE 
					a.client_id=b.client_id AND a.user_id=c.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSE
				SELECT incident_date, a.client_id, computer_name, domain_name, c.username, vulnerability, action_taken, 
				rule_desc, rule_ref, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, 
					client_id, a.user_id, vulnerability, action_taken, rule_desc, rule_ref, GroupId, incident_date AS incident_date_in_seconds
					FROM rpt_ips_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND (par_group_id=0 OR a.GroupId = par_group_id)
					AND a.user_id = var_user_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS b, rpt_user_master AS c
				WHERE 
				a.client_id=b.client_id AND a.user_id=c.user_id
				ORDER BY incident_date DESC;
			END IF;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `portscan_rpt_get_count_pr`;
DELIMITER $$
CREATE PROCEDURE `portscan_rpt_get_count_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN 
		SELECT IFNULL(SUM(incident_count), 0) AS record_count FROM rpt_port_scan_chart_summary;
	ELSE
		IF par_client_name<> '' AND par_group_id <> 0 THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_port_scan_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND FIND_IN_SET(a.client_id, var_client_ids_str)
				AND a.GroupId = par_group_id;
			ELSEIF var_client_count = 1 THEN
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_port_scan_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.client_id = var_client_ids_str
				AND a.GroupId = par_group_id;
			END IF;

		ELSEIF par_client_name='' AND par_group_id<>0 THEN
			SELECT IFNULL(SUM(incident_count), 0) AS record_count
			FROM rpt_port_scan_chart_summary AS a USE INDEX(idx_incident_date_only, idx_GroupId)
			WHERE
			a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
			AND a.GroupId = par_group_id;

		ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_port_scan_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND FIND_IN_SET(a.client_id, var_client_ids_str);
			ELSEIF var_client_count = 1 THEN
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_port_scan_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.client_id = var_client_ids_str;
			END IF;
		ELSEIF par_client_name='' AND par_group_id = 0 THEN
			SELECT IFNULL(SUM(incident_count), 0) AS record_count
			FROM rpt_port_scan_chart_summary AS a
			WHERE
			a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second);
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `portscan_rpt_get_table_pr`;
DELIMITER $$
CREATE PROCEDURE `portscan_rpt_get_table_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT incident_date, a.client_id, computer_name, domain_name, attacker_ip, attacker_mac, scanports, 
		action_taken, incident_date_in_seconds
		FROM
		(
			SELECT from_unixtime(incident_date) AS incident_date, 
			client_id, attacker_ip, attacker_mac, scanports, action_taken, GroupId, incident_date AS incident_date_in_seconds
			FROM rpt_port_scan_fact AS a
			ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
		) AS a, client_master AS b
		WHERE 
		a.client_id=b.client_id
		ORDER BY incident_date DESC;
	ELSE
		IF par_client_name<> '' AND par_group_id <> 0 THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT incident_date, a.client_id, computer_name, domain_name, attacker_ip, attacker_mac, scanports, 
				action_taken, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, 
					client_id, attacker_ip, attacker_mac, scanports, action_taken, GroupId, incident_date AS incident_date_in_seconds
					FROM rpt_port_scan_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS b
				WHERE 
				a.client_id=b.client_id
				ORDER BY incident_date DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT incident_date, a.client_id, computer_name, domain_name, attacker_ip, attacker_mac, scanports, 
				action_taken, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, 
					client_id, attacker_ip, attacker_mac, scanports, action_taken, GroupId, incident_date AS incident_date_in_seconds
					FROM rpt_port_scan_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS b
				WHERE 
				a.client_id=b.client_id
				ORDER BY incident_date DESC;
			END IF;

		ELSEIF par_client_name='' AND par_group_id<>0 THEN
			SELECT incident_date, a.client_id, computer_name, domain_name, attacker_ip, attacker_mac, scanports, 
			action_taken, incident_date_in_seconds
			FROM
			(
				SELECT from_unixtime(incident_date) AS incident_date, 
				client_id, attacker_ip, attacker_mac, scanports, action_taken, GroupId, incident_date AS incident_date_in_seconds
				FROM rpt_port_scan_fact AS a USE INDEX(idx_incident_date, idx_GroupId)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
			) AS a, client_master AS b
			WHERE 
			a.client_id=b.client_id
			ORDER BY incident_date DESC;

		ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT incident_date, a.client_id, computer_name, domain_name, attacker_ip, attacker_mac, scanports, 
				action_taken, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, 
					client_id, attacker_ip, attacker_mac, scanports, action_taken, GroupId, incident_date AS incident_date_in_seconds
					FROM rpt_port_scan_fact AS a USE INDEX(idx_incident_date, idx_client_id)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS b
				WHERE
				a.client_id=b.client_id
				ORDER BY incident_date DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT incident_date, a.client_id, computer_name, domain_name, attacker_ip, attacker_mac, scanports, 
				action_taken, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, 
					client_id, attacker_ip, attacker_mac, scanports, action_taken, GroupId, incident_date AS incident_date_in_seconds
					FROM rpt_port_scan_fact AS a USE INDEX(idx_incident_date, idx_client_id)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS b
				WHERE
				a.client_id=b.client_id
				ORDER BY incident_date DESC;
			END IF;
		ELSEIF par_client_name='' AND par_group_id = 0 THEN
			SELECT incident_date, a.client_id, computer_name, domain_name, attacker_ip, attacker_mac, scanports, 
			action_taken, incident_date_in_seconds
			FROM
			(
				SELECT from_unixtime(incident_date) AS incident_date, 
				client_id, attacker_ip, attacker_mac, scanports, action_taken, GroupId, incident_date AS incident_date_in_seconds
				FROM rpt_port_scan_fact AS a
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
			) AS a, client_master AS b
			WHERE 
			a.client_id=b.client_id
			ORDER BY incident_date DESC;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `pr_add_update_client`;
DELIMITER $$
CREATE PROCEDURE `pr_add_update_client`(
	IN `par_guid` varchar(45),
	IN `par_computer_name` varchar(45),
	IN `par_domain_name` varchar(45),
	IN `par_username` varchar(45),
	IN `par_group_id` int(11)
)
BEGIN
	DECLARE var_guid varchar(45);
	SET var_guid = (SELECT guid FROM client_master WHERE guid=par_guid LIMIT 1);
	IF var_guid IS NOT NULL THEN
		UPDATE `client_master`
		SET
		`computer_name` = `par_computer_name`,
		`domain_name` = `par_domain_name`,
		`username` = `par_username`,
		`GroupId` = `par_group_id`
		WHERE `guid` = par_guid;
	ELSE
		INSERT INTO `client_master`
		(
			`guid`,
			`computer_name`,
			`domain_name`,
			`username`,
			`GroupId`
		)
		VALUES
		(
			`par_guid`,
			`par_computer_name`,
			`par_domain_name`,
			`par_username`,
			`par_group_id`
		);
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `pr_add_update_group`;
DELIMITER $$
CREATE PROCEDURE `pr_add_update_group`(
	IN `par_group_id` INT(11),
	IN `par_parent_GroupId` INT(11),
	IN `par_policy_id` INT(11),
	IN `par_group_name` varchar(45)
)
BEGIN
	DECLARE var_GroupId INT(11);
	SET var_GroupId = (SELECT GroupId FROM groups WHERE GroupId = par_group_id LIMIT 1);
	IF var_GroupId IS NOT NULL THEN
		UPDATE `groups`
		SET
		`ParentGrpId` = `par_parent_GroupId`,
		`PolicyId` = `par_policy_id`,
		`group_name` = `par_group_name`
		WHERE `GroupId` = `par_group_id`;
	ELSE
		INSERT INTO `groups`
		(
			`GroupId`,
			`ParentGrpId`,
			`PolicyId`,
			`group_name`
		)
		VALUES
		(
			`par_group_id`,
			`par_parent_GroupId`,
			`par_policy_id`,
			`par_group_name`
		);
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `pr_get_group_list`;
DELIMITER $$
CREATE PROCEDURE `pr_get_group_list`()
begin

	SET time_zone = '+00:00';
	
	select GroupId, ParentGrpId, PolicyId AS policy_id, GroupName AS group_name from groups where GroupId <> 0 order by GroupId;
end $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `pr_insert_anti_malware_rpt`;
DELIMITER $$
CREATE PROCEDURE `pr_insert_anti_malware_rpt`(
	IN `par_incident_date` int(11),
	IN `par_malware_name` varchar(260),
	IN `par_malware_type` varchar(100),
	IN `par_action_taken` varchar(50),
	IN `par_machine_id` varchar(45)
)
BEGIN
	DECLARE var_client_id BIGINT(20) DEFAULT NULL;
	DECLARE var_GroupId INT(11) DEFAULT NULL;
	DECLARE var_md5 CHAR(64) DEFAULT NULL;
	DECLARE var_matched_id BIGINT(20) DEFAULT NULL;
		
	SET time_zone = '+00:00';
	
	SELECT client_id, GroupId INTO var_client_id, var_GroupId FROM client_master WHERE guid=par_machine_id; 
	IF var_client_id IS NOT NULL THEN
	
		INSERT INTO rpt_anti_malware_fact(incident_date,malware_name,malware_type,action_taken,client_id,GroupId) VALUES(par_incident_date,par_malware_name,par_malware_type,par_action_taken,var_client_id,var_GroupId);
		
		SET var_md5 = MD5(CONCAT(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id));
		SET var_matched_id = (SELECT rowid FROM rpt_anti_malware_chart_summary WHERE md5_match=var_md5);
		IF var_matched_id IS NOT NULL THEN
			UPDATE rpt_anti_malware_chart_summary SET incident_count=(incident_count+1) WHERE `rowid`=var_matched_id;
		ELSE
			INSERT INTO rpt_anti_malware_chart_summary(incident_date_only, client_id, GroupId, md5_match, incident_count) 
			VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_client_id, var_GroupId, var_md5, 1);
		END IF;
		
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `pr_insert_appcon_oa_rpt`;
DELIMITER $$
CREATE PROCEDURE `pr_insert_appcon_oa_rpt`(
	IN `par_incident_date` int(11),
	IN `par_application_name` VARCHAR(256),
	IN `par_app_category_name` VARCHAR(256),
	IN `par_application_version` VARCHAR(256),
	IN `par_application_path` VARCHAR(1024),
	IN `par_username` VARCHAR(512),
	IN `par_app_category_id` INT(5),
	IN `par_client_guid` varchar(45)
)
BEGIN	
	DECLARE var_client_id BIGINT(20) DEFAULT NULL;
	DECLARE var_GroupId INT(11) DEFAULT NULL;
	DECLARE var_md5 CHAR(64) DEFAULT NULL;
	DECLARE var_md5_match_id BIGINT(20) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	
	SELECT client_id, GroupId INTO var_client_id, var_GroupId FROM client_master WHERE guid=par_client_guid; 
	SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
	IF var_user_id IS NULL THEN
		INSERT INTO rpt_user_master(username) VALUES(par_username);
		SET var_user_id = (select last_insert_id());
	END IF;
	
	IF var_client_id IS NOT NULL THEN
		
		SET var_md5 = MD5(CONCAT(par_incident_date, var_GroupId, var_client_id, var_user_id, par_app_category_id));
		SELECT `rowid` INTO var_md5_match_id FROM rpt_appcon_oa_chart_summary WHERE md5_match=var_md5;
		
		IF var_md5_match_id IS NULL THEN
			INSERT INTO rpt_appcon_oa_chart_summary(incident_date_only, GroupId, client_id, user_id, app_category_id, app_category_name, md5_match, incident_count) 
			VALUES(par_incident_date,var_GroupId, var_client_id, var_user_id, par_app_category_id, par_app_category_name, var_md5, 1);
			SET var_md5_match_id = (select last_insert_id());
		END IF;
		
		INSERT INTO rpt_appcon_oa_fact(incident_date, application_name, app_category_id, app_category_name, application_version, application_path, client_id, user_id, GroupId) 
		VALUES(par_incident_date,par_application_name, par_app_category_id, par_app_category_name, par_application_version, par_application_path, var_client_id, var_user_id, var_GroupId);
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `pr_insert_appcon_od_fact`;
DELIMITER $$
CREATE PROCEDURE `pr_insert_appcon_od_fact`(
	IN `par_scan_info_id` CHAR(38),	
	IN `par_scan_info_type` int(11),	
	IN `par_application_name` varchar(256),
	IN `par_application_version` varchar(256),
	IN `par_app_category` varchar(256),
	IN `par_app_path` varchar(256),
    IN `par_install_date` int(11),
	IN `par_publisher` VARCHAR(256)
)
BEGIN

	SET time_zone = '+00:00';
	
	INSERT INTO `rpt_appcon_od_fact`
	(
		`scan_info_id`, `scan_info_type`, `install_date`, `application_name`, 
		`application_version`, `app_category`, `app_path`, `publisher`
	)
	VALUES
	(
		par_scan_info_id, par_scan_info_type, par_install_date, par_application_name, 
		par_application_version, par_app_category, par_app_path, par_publisher
	);
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `pr_insert_appcon_od_summarized_fact`;
DELIMITER $$
CREATE PROCEDURE `pr_insert_appcon_od_summarized_fact`(
	IN `par_incident_date` int(11),
    IN `par_module` varchar(256),
    IN `par_summary` varchar(256),
    IN `par_scan_info_type` int(11),
    IN `par_unauthorized_app_cnt` int(11),
	IN `par_authorized_app_cnt` int(11),
	IN `par_installed_app_cnt` int(11),
	IN `par_scan_info_id` CHAR(38),	
	IN `par_client_guid` varchar(45)
)
BEGIN
	DECLARE var_client_id BIGINT(20) DEFAULT NULL;
	DECLARE var_GroupId INT(11) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	
	SELECT client_id, GroupId INTO var_client_id, var_GroupId FROM client_master WHERE guid=par_client_guid;	
	IF var_client_id IS NOT NULL THEN
		
		INSERT INTO `rpt_appcon_od_summarized_fact`
		(
			`incident_date`, `scan_info_id`, `scan_info_type`, `module`, `summary`, 
			`unauthorized_app_cnt`, `authorized_app_cnt`, `installed_app_cnt`, `client_id`, `GroupId`
		)
		VALUES
		(
			par_incident_date, par_scan_info_id, par_scan_info_type, par_module, par_summary, 
			par_unauthorized_app_cnt, par_authorized_app_cnt, par_installed_app_cnt, var_client_id, var_GroupId
		);
		
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `pr_insert_assets_rpt`;
DELIMITER $$
CREATE PROCEDURE `pr_insert_assets_rpt`(
	IN `par_incident_date` int(11),
	IN `par_category_id` BIGINT(2),
	IN `par_username` varchar(512),
	IN `par_asset_name` varchar(300),
	IN `par_asset_id` int(2),	
	IN `par_details` varchar(512),
	IN `par_client_guid` varchar(45)
)
BEGIN
DECLARE var_client_id BIGINT(20) DEFAULT NULL;
DECLARE var_GroupId INT(11) DEFAULT NULL;
DECLARE var_md5 CHAR(64) DEFAULT NULL;
DECLARE var_matched_id BIGINT(20) DEFAULT NULL;
DECLARE var_user_id BIGINT(20) DEFAULT NULL;


SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username=par_username LIMIT 1;
IF var_user_id IS NULL THEN
	INSERT INTO rpt_user_master(username) VALUES(par_username);
	SET var_user_id = (select last_insert_id());
END IF;
	
SELECT client_id, GroupId INTO var_client_id, var_GroupId FROM client_master WHERE guid=par_client_guid; 
	IF var_client_id IS NOT NULL THEN		
		INSERT INTO rpt_asset_mgnt_fact(incident_date, asset_id, category_id, asset_name, details, client_id, GroupId, user_id) 
		VALUES(par_incident_date, par_asset_id, par_category_id, par_asset_name, par_details, var_client_id, var_GroupId, var_user_id);
		-- Update Summary Table --
		SET var_md5 = MD5(CONCAT(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, par_category_id));
		SET var_matched_id = (SELECT rowid FROM rpt_asset_mgnt_chart_summary WHERE md5_match=var_md5);
		IF var_matched_id IS NOT NULL THEN		
			UPDATE rpt_asset_mgnt_chart_summary SET incident_count=(incident_count+1) WHERE `rowid`=var_matched_id;
		ELSE
			INSERT INTO rpt_asset_mgnt_chart_summary(incident_date_only,GroupId,client_id, user_id, category_id,md5_match,incident_count)
			VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))),var_GroupId,var_client_id,var_user_id,par_category_id,var_md5,1);
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `pr_insert_ddos_rpt`;
DELIMITER $$
CREATE PROCEDURE `pr_insert_ddos_rpt`(
	IN `par_incident_date` int(11),
	IN `par_action_taken` varchar(256),
	IN `par_info_id` char(40),
	IN `par_attacker_ip` varchar(256),
	IN `par_attacker_mac` varchar(256),
	IN `par_machine_id` varchar(45)
)
BEGIN
	DECLARE var_client_id BIGINT(20) DEFAULT NULL;
	DECLARE var_GroupId INT(11) DEFAULT NULL;
	DECLARE var_md5 CHAR(64) DEFAULT NULL;
	DECLARE var_matched_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	
	SELECT client_id, GroupId INTO var_client_id, var_GroupId FROM client_master WHERE guid=par_machine_id; 
	IF var_client_id IS NOT NULL THEN
	
		INSERT INTO rpt_ddos_fact(incident_date,action_taken,ddos_info_id,attacker_ip,attacker_mac,client_id,GroupId) VALUES(par_incident_date,par_action_taken,par_info_id,par_attacker_ip,par_attacker_mac,var_client_id,var_GroupId);		
		
		SET var_md5 = MD5(CONCAT(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id));
		SET var_matched_id = (SELECT rowid FROM rpt_ddos_chart_summary WHERE md5_match=var_md5);
		IF var_matched_id IS NOT NULL THEN
			UPDATE rpt_ddos_chart_summary SET incident_count=(incident_count+1) WHERE `rowid`=var_matched_id;
		ELSE
			INSERT INTO rpt_ddos_chart_summary(incident_date_only, client_id, GroupId, md5_match, incident_count) 
			VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_client_id, var_GroupId, var_md5, 1);
		END IF;
		
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `pr_insert_device_control_rpt`;
DELIMITER $$
CREATE PROCEDURE `pr_insert_device_control_rpt`(
	IN `par_incident_date` int(11),
	IN `par_username` varchar(512),
	IN `par_device_name` varchar(256),
	IN `par_action_id` int(2),	
	IN `par_manufacturer` varchar(256),
	IN `par_size` int(11),
	IN `par_device_type_id` int(2),
	IN `par_serial_number` varchar(256),
	IN `par_client_guid` varchar(45)
)
BEGIN
	DECLARE var_client_id BIGINT(20) DEFAULT NULL;
	DECLARE var_GroupId INT(11) DEFAULT NULL;
	DECLARE var_md5 CHAR(64) DEFAULT NULL;
	DECLARE var_matched_id BIGINT(20) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	DECLARE var_device_id BIGINT(20) DEFAULT NULL;

	SET time_zone = '+00:00';
	
	SELECT client_id, GroupId INTO var_client_id, var_GroupId FROM client_master WHERE guid=par_client_guid;
	-- If username doesn't exist in rpt_user_master then add it as new user.
	SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username=par_username LIMIT 1;
	IF var_user_id IS NULL THEN
		INSERT INTO rpt_user_master(username) VALUES(par_username);
		SET var_user_id = (select last_insert_id());
	END IF;
	-- If device doesn't exist in rpt_device_master_dim then add it as new device.
	SET var_md5 = MD5(CONCAT(par_device_name, par_device_type_id, par_serial_number));
	SET var_device_id = (SELECT device_id FROM rpt_device_master_dim WHERE md5_match=var_md5);
	IF var_device_id IS NULL THEN
		INSERT INTO rpt_device_master_dim(device_name, device_type_id, serial_number, manufacturer, dev_size, md5_match) 
		VALUES(par_device_name, par_device_type_id, par_serial_number, par_manufacturer, par_size, var_md5);
		SET var_device_id = (select last_insert_id());
	END IF;
	
	IF var_client_id IS NOT NULL THEN		
		-- Update Summary Table 1 --
		SET var_md5 = MD5(CONCAT(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, par_device_type_id));
		SET var_matched_id = (SELECT rowid FROM rpt_device_chart_summary_1 WHERE md5_match=var_md5);
		IF var_matched_id IS NOT NULL THEN		
			UPDATE rpt_device_chart_summary_1 SET incident_count=(incident_count+1) WHERE `rowid`=var_matched_id;
		ELSE
			INSERT INTO rpt_device_chart_summary_1(incident_date_only, GroupId, client_id, user_id, device_type_id, md5_match, incident_count)
			VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, par_device_type_id, var_md5, 1);
		END IF;
		
		-- Update Summary Table 2 --
		SET var_md5 = MD5(CONCAT(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, var_device_id, par_device_type_id, par_action_id));
		SET var_matched_id = (SELECT rowid FROM rpt_device_chart_summary_2 WHERE md5_match=var_md5);
		IF var_matched_id IS NOT NULL THEN		
			UPDATE rpt_device_chart_summary_2 SET incident_count=(incident_count+1) WHERE `rowid`=var_matched_id;
		ELSE
			INSERT INTO rpt_device_chart_summary_2(incident_date_only, GroupId, client_id, user_id, device_type_id, action_id, md5_match, incident_count)
			VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))),var_GroupId, var_client_id, var_user_id, par_device_type_id, par_action_id, var_md5, 1);
		END IF;
		
		INSERT INTO rpt_device_control_fact(incident_date, device_id, device_type_id, action_id, user_id, client_id, GroupId) 
		VALUES(par_incident_date, var_device_id, par_device_type_id, par_action_id, var_user_id, var_client_id, var_GroupId);
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `pr_insert_dlp_oa_rpt`;
DELIMITER $$
CREATE PROCEDURE `pr_insert_dlp_oa_rpt`(
	IN `par_incident_date` int(11),
	IN `par_username` varchar(512),
	IN `par_file_path` varchar(1024),
	IN `par_content_type_id` int(2),
	IN `par_content_type` varchar(100),
	IN `par_matched_item_id` int(11),
	IN `par_matched_item` varchar(500),
	IN `par_channel_type_id` int(2),
	IN `par_channel_type` varchar(256),
	IN `par_channel_detail` varchar(200),
	IN `par_data_id` varchar(256),
	IN `par_action_id` int(11),
	IN `par_action_taken` varchar(100),
	IN `par_local_app_id` int(11),	
	IN `par_client_guid` varchar(45),
    IN `par_sender` varchar(60),
    IN `par_receiver` mediumtext,
    IN `par_subject` mediumtext	
)
BEGIN
	DECLARE var_client_id BIGINT(20) DEFAULT NULL;
	DECLARE var_GroupId INT(11) DEFAULT NULL;
	DECLARE var_md5 CHAR(64) DEFAULT NULL;
	DECLARE var_matched_id BIGINT(20) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	DECLARE var_application_id BIGINT(20) DEFAULT NULL;

	SELECT client_id, GroupId INTO var_client_id, var_GroupId FROM client_master WHERE guid=par_client_guid;	
	IF var_client_id IS NOT NULL THEN
		
		-- If username doesn't exist in rpt_user_master then add it as new user.
		SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username=par_username LIMIT 1;
		IF var_user_id IS NULL THEN
			INSERT INTO rpt_user_master(username) VALUES(par_username);
			SET var_user_id = (select last_insert_id());
		END IF;
		
		IF par_channel_type_id = 2 THEN -- Only in case of channel type is Application then maintain a list of applications.
		
			-- If Application doesn't exist in "rpt_dlp_application_master_dim" then add it as new one.
			SET var_application_id = (SELECT application_id FROM rpt_dlp_application_master_dim WHERE application_name=par_channel_detail);
			IF var_application_id IS NULL THEN
				INSERT INTO rpt_dlp_application_master_dim(application_name,channel_type) 
				VALUES(par_channel_detail, par_channel_type); -- "par_channel_detail" has application name.
				SET var_application_id = (select last_insert_id());
			END IF;
			
			-- Update "Application" Summary Table --
			SET var_md5 = MD5(CONCAT(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, var_application_id));
			SET var_matched_id = (SELECT rowid FROM rpt_dlp_oa_application_summary WHERE md5_match=var_md5);
			IF var_matched_id IS NOT NULL THEN
				UPDATE rpt_dlp_oa_application_summary SET incident_count=(incident_count+1) WHERE `rowid`=var_matched_id;
			ELSE
				INSERT INTO rpt_dlp_oa_application_summary(incident_date_only, GroupId, client_id, user_id, application_id, md5_match, incident_count)
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, var_application_id, var_md5, 1);
			END IF;
			
		END IF;
		
		INSERT INTO rpt_dlp_oa_fact(incident_date, client_id, user_id, GroupId, 
		file_path, content_type_id, content_type, matched_item_id, matched_item, channel_type_id, channel_type, channel_details, 
		data_id, action_id, action_taken, local_app_id, application_id, sender, receiver, `subject`)
		VALUES(par_incident_date, var_client_id, var_user_id, var_GroupId, 
		par_file_path, par_content_type_id, par_content_type, par_matched_item_id, par_matched_item, par_channel_type_id, par_channel_type, par_channel_detail, 
		par_data_id, par_action_id, par_action_taken, par_local_app_id, var_application_id, par_sender, par_receiver, par_subject);
		
		-- Update Content Type Summary Table --
	IF par_channel_type_id <> 5 AND par_channel_type_id <> 6 THEN
		SET var_md5 = MD5(CONCAT(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, par_channel_type_id));
		SET var_matched_id = (SELECT rowid FROM rpt_dlp_oa_content_type_summary WHERE md5_match=var_md5);
		IF var_matched_id IS NOT NULL THEN		
			IF par_content_type_id = 1 THEN
				UPDATE rpt_dlp_oa_content_type_summary SET files_copied_count=(files_copied_count+1) WHERE `rowid`=var_matched_id;
			ELSEIF par_content_type_id = 2 THEN
				UPDATE rpt_dlp_oa_content_type_summary SET confidential_data_count=(confidential_data_count+1) WHERE `rowid`=var_matched_id;
			ELSEIF par_content_type_id = 3 THEN
				UPDATE rpt_dlp_oa_content_type_summary SET user_define_dictionary_count=(user_define_dictionary_count+1) WHERE `rowid`=var_matched_id;
			END IF;
			
		ELSE
			IF par_content_type_id = 1 THEN
				INSERT INTO rpt_dlp_oa_content_type_summary(incident_date_only, GroupId, client_id, user_id, channel_type_id, md5_match, files_copied_count)
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, par_channel_type_id, var_md5, 1);
			ELSEIF par_content_type_id = 2 THEN
				INSERT INTO rpt_dlp_oa_content_type_summary(incident_date_only, GroupId, client_id, user_id, channel_type_id, md5_match, confidential_data_count)
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, par_channel_type_id, var_md5, 1);
			ELSEIF par_content_type_id = 3 THEN
				INSERT INTO rpt_dlp_oa_content_type_summary(incident_date_only, GroupId, client_id, user_id, channel_type_id, md5_match, user_define_dictionary_count)
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, par_channel_type_id, var_md5, 1);
			END IF;			
		END IF;
	END IF;	
		-- Update Common Summary Table --
		SET var_md5 = MD5(CONCAT(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, par_channel_type_id));
		SET var_matched_id = (SELECT rowid FROM rpt_dlp_oa_summary WHERE md5_match=var_md5);
		IF var_matched_id IS NOT NULL THEN
			IF par_channel_type_id = 1 THEN
				UPDATE rpt_dlp_oa_summary SET removable_device_count=(removable_device_count+1) WHERE `rowid`=var_matched_id;
			ELSEIF par_channel_type_id = 2 THEN
				UPDATE rpt_dlp_oa_summary SET application_count=(application_count+1) WHERE `rowid`=var_matched_id;
			ELSEIF par_channel_type_id = 3 THEN
				UPDATE rpt_dlp_oa_summary SET network_share_count=(network_share_count+1) WHERE `rowid`=var_matched_id;
			ELSEIF par_channel_type_id = 4 THEN
				UPDATE rpt_dlp_oa_summary SET clipboard_count=(clipboard_count+1) WHERE `rowid`=var_matched_id;
			ELSEIF par_channel_type_id = 5 THEN
				UPDATE rpt_dlp_oa_summary SET print_screen_count=(print_screen_count+1) WHERE `rowid`=var_matched_id;
			ELSEIF par_channel_type_id = 6 THEN
				UPDATE rpt_dlp_oa_summary SET print_count=(print_count+1) WHERE `rowid`=var_matched_id;
			END IF;
			
		ELSE
			
			IF par_channel_type_id = 1 THEN
				INSERT INTO rpt_dlp_oa_summary(incident_date_only, client_id, user_id, GroupId, channel_type_id, md5_match, removable_device_count)
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_client_id, var_user_id, var_GroupId, par_channel_type_id, var_md5, 1);
			ELSEIF par_channel_type_id = 2 THEN
				INSERT INTO rpt_dlp_oa_summary(incident_date_only, client_id, user_id, GroupId, channel_type_id, md5_match, application_count)
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_client_id, var_user_id, var_GroupId, par_channel_type_id, var_md5, 1);
			ELSEIF par_channel_type_id = 3 THEN
				INSERT INTO rpt_dlp_oa_summary(incident_date_only, client_id, user_id, GroupId, channel_type_id, md5_match, network_share_count)
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_client_id, var_user_id, var_GroupId, par_channel_type_id, var_md5, 1);
			ELSEIF par_channel_type_id = 4 THEN
				INSERT INTO rpt_dlp_oa_summary(incident_date_only, client_id, user_id, GroupId, channel_type_id, md5_match, clipboard_count)
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_client_id, var_user_id, var_GroupId, par_channel_type_id, var_md5, 1);
			ELSEIF par_channel_type_id = 5 THEN
				INSERT INTO rpt_dlp_oa_summary(incident_date_only, client_id, user_id, GroupId, channel_type_id, md5_match, print_screen_count)
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_client_id, var_user_id, var_GroupId, par_channel_type_id, var_md5, 1);
			ELSEIF par_channel_type_id = 6 THEN
				INSERT INTO rpt_dlp_oa_summary(incident_date_only, client_id, user_id, GroupId, channel_type_id, md5_match, print_count)
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_client_id, var_user_id, var_GroupId, par_channel_type_id, var_md5, 1);
			END IF;
		END IF;
		
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `pr_insert_dlp_od_fact`;
DELIMITER $$
CREATE PROCEDURE `pr_insert_dlp_od_fact`(
	IN `par_scan_info_id` char(38),
	IN `par_scan_start_Date` int(11),
	IN `par_incident_date` int(11),
	IN `par_file_path` varchar(1024),
	IN `par_file_type` varchar(512),
	IN `par_content_type_id` int(2),
	IN `par_content_type` varchar(100),
	IN `par_conf_data_category_id` int(2),
	IN `par_conf_data_category` varchar(200),
	IN `par_matched_item_id` int(11),
	IN `par_matched_item` varchar(500),
	IN `par_client_guid` varchar(45)	
)
BEGIN
	DECLARE var_client_id BIGINT(20) DEFAULT NULL;
	DECLARE var_GroupId INT(11) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	-- START : Only to maintain matched_item_id for User Defined Dictionary(par_content_type_id=3).
	DECLARE var_matched_item_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	
	IF par_content_type_id = 3 THEN
		SET var_matched_item_id = (SELECT mached_item_id FROM rpt_dlp_matched_item_master WHERE matched_item=par_matched_item LIMIT 1);
	
		IF var_matched_item_id IS NULL THEN
			INSERT INTO rpt_dlp_matched_item_master(matched_item) VALUES(par_matched_item);
			SET var_matched_item_id = (select last_insert_id());
		END IF;
	ELSE
		SET var_matched_item_id = par_matched_item_id;
	END IF;
	-- END : Only to maintain matched_item_id for User Defined Dictionary(par_content_type_id=3).
	
	SELECT client_id, GroupId INTO var_client_id, var_GroupId FROM client_master WHERE guid=par_client_guid;

	SELECT user_id INTO var_user_id FROM rpt_dlp_od_summarized_fact WHERE scan_info_id = par_scan_info_id LIMIT 1;
	
	IF var_client_id IS NOT NULL THEN
		
		INSERT INTO `rpt_dlp_od_fact`
		(
			`incident_date`,
			`scan_info_id`,
			`scan_start_date`,
			`client_id`,
			`user_id`,
			`GroupId`,
			`file_path`,
			`file_type`,
			`content_type_id`,
			`content_type`,
			`conf_data_cat_id`,
			`conf_data_cat`,
			`matched_item_id`,
			`matched_item`
		)
		VALUES
		(
			par_incident_date,
			par_scan_info_id,
			par_scan_start_Date,
			var_client_id,
			var_user_id,
			var_GroupId,
			par_file_path,
			par_file_type,
			par_content_type_id,
			par_content_type,
			par_conf_data_category_id,
			par_conf_data_category,
			var_matched_item_id,
			par_matched_item
		);
		
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `pr_insert_dlp_od_summarized_fact`;
DELIMITER $$
CREATE PROCEDURE `pr_insert_dlp_od_summarized_fact`(
	IN `par_scan_start_Date` int(11),
	IN `par_username` varchar(512),
	IN `par_scan_type_id` int(2),
	IN `par_scan_type` varchar(256),
	IN `par_scan_info_id` char(38),	
	IN `par_conf_data_inc_count` bigint(20),
	IN `par_dictionary_inc_count` bigint(20),
	IN `par_client_guid` varchar(45)	
)
BEGIN
	DECLARE var_client_id BIGINT(20) DEFAULT NULL;
	DECLARE var_GroupId INT(11) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	
	SELECT client_id, GroupId INTO var_client_id, var_GroupId FROM client_master WHERE guid=par_client_guid;	
	IF var_client_id IS NOT NULL THEN
		-- If username doesn't exist in rpt_user_master then add it as new user.
		SET var_user_id = (SELECT user_id FROM rpt_user_master WHERE username=par_username);
		IF var_user_id IS NULL THEN
			INSERT INTO rpt_user_master(username) VALUES(par_username);
			SET var_user_id = (select last_insert_id());
		END IF;
		
		INSERT INTO `rpt_dlp_od_summarized_fact`
		(
			`scan_info_id`,
			`scan_start_date`,
			`client_id`,
			`user_id`,
			`GroupId`,
			`scan_type_id`,
			`scan_type`,
			`confident_data_inc_count`,
			`data_dictionary_inc_count`
		)
		VALUES
		(
			par_scan_info_id,
			par_scan_start_Date,
			var_client_id,
			var_user_id,
			var_GroupId,
			par_scan_type_id,
			par_scan_type,
			par_conf_data_inc_count,
			par_dictionary_inc_count
		);
		
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `pr_insert_dlp_od_summary`;
DELIMITER $$
CREATE PROCEDURE `pr_insert_dlp_od_summary`(
	IN `par_scan_start_Date` int(11),
	IN `par_scan_info_id` char(38),	
	IN `par_content_type_id` int(2),
	IN `par_content_type` varchar(100),
	IN `par_conf_data_category_id` int(2),
	IN `par_conf_data_category` varchar(200),	
	IN `par_matched_item_id` int(11),
	IN `par_matched_item` varchar(500),
	IN `par_client_guid` varchar(45),
	IN `par_incident_count` bigint(20)
)
BEGIN
	DECLARE var_client_id BIGINT(20) DEFAULT NULL;
	DECLARE var_GroupId INT(11) DEFAULT NULL;
	DECLARE var_md5 CHAR(64) DEFAULT NULL;
	DECLARE var_md5_match_id BIGINT(20) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	-- START : Only to maintain matched_item_id for User Defined Dictionary(par_content_type_id=3).
	DECLARE var_matched_item_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	
	SELECT user_id INTO var_user_id FROM rpt_dlp_od_summarized_fact WHERE scan_info_id = par_scan_info_id LIMIT 1;
	
	
	IF par_content_type_id = 3 THEN
		SET var_matched_item_id = (SELECT mached_item_id FROM rpt_dlp_matched_item_master WHERE matched_item=par_matched_item LIMIT 1);
	
		IF var_matched_item_id IS NULL THEN
			INSERT INTO rpt_dlp_matched_item_master(matched_item) VALUES(par_matched_item);
			SET var_matched_item_id = (select last_insert_id());
		END IF;
	ELSE
		SET var_matched_item_id = par_matched_item_id;
	END IF;
	-- END : Only to maintain matched_item_id for User Defined Dictionary(par_content_type_id=3).
	
	SELECT client_id, GroupId INTO var_client_id, var_GroupId FROM client_master WHERE guid=par_client_guid;	
	IF var_client_id IS NOT NULL THEN
		
		SET var_md5 = MD5(CONCAT(par_content_type_id, var_matched_item_id, par_scan_info_id, par_conf_data_category_id));
		SELECT `rowid` INTO var_md5_match_id FROM rpt_dlp_od_chart_summary WHERE md5_match=var_md5;
		IF var_md5_match_id IS NOT NULL THEN
			UPDATE `rpt_dlp_od_chart_summary` 
			SET incident_count = incident_count + par_incident_count
			WHERE md5_match=var_md5;
		ELSE
			INSERT INTO `rpt_dlp_od_chart_summary`
			(
				`client_id`,
				`user_id`,
				`GroupId`,
				`scan_info_id`,
				`scan_start_date`,
				`content_type_id`,
				`content_type`,
				`conf_data_cat_id`,
				`conf_data_cat`,
				`matched_item_id`,
				`matched_item`,
				`incident_count`,
				`md5_match`
			)
			VALUES
			(
				var_client_id,
				var_user_id,
				var_GroupId,
				par_scan_info_id,
				par_scan_start_Date,
				par_content_type_id,
				par_content_type,
				par_conf_data_category_id,
				par_conf_data_category,
				var_matched_item_id,
				par_matched_item,
				par_incident_count,
				var_md5
			);
		END IF;
		
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `pr_insert_fam_rpt`;
DELIMITER $$
CREATE PROCEDURE `pr_insert_fam_rpt`(
	IN `par_incident_date` int(11),
	IN `par_username` varchar(512),
	IN `par_file_name` varchar(1024),
	IN `par_file_extention` varchar(10),
	IN `par_file_ext_chksum` bigint(20),
	IN `par_location_id` int(11),
	IN `par_event_id` int(11),
	IN `par_event_details` varchar(2048),
	IN `par_client_guid` varchar(45)
)
BEGIN
	DECLARE var_md5 CHAR(64) DEFAULT NULL;
	DECLARE var_matched_id BIGINT(20) DEFAULT NULL;
	DECLARE var_client_id BIGINT(20) DEFAULT NULL;
	DECLARE var_GroupId BIGINT(20) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	
	SELECT `client_id`,`GroupId` INTO var_client_id,var_GroupId  FROM client_master WHERE guid = par_client_guid LIMIT 1;
	
	IF var_client_id IS NOT NULL THEN
	
		SET var_md5 = MD5(CONCAT(`par_file_name`,`par_file_extention`));
		SET var_matched_id = (SELECT fam_file_id FROM rpt_fam_file_master_dim WHERE file_md5 = var_md5);
		SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username=par_username LIMIT 1;		
		IF var_user_id IS NULL THEN
			INSERT INTO rpt_user_master(username) VALUES(par_username);
			SET var_user_id = (select last_insert_id());
		END IF;
		
		IF var_matched_id IS NULL THEN
			INSERT INTO `rpt_fam_file_master_dim`(`file_name`,`file_extention`,`file_ext_chksum`,`file_md5`)
			VALUES(`par_file_name`,`par_file_extention`, `par_file_ext_chksum`, var_md5);
			SET var_matched_id = (select last_insert_id());
		END IF;
		INSERT INTO `rpt_fam_incident_fact`(
			`fam_file_id`,`location_id`,`event_id`,`event_details`,`client_id`,`user_id`,`GroupId`,`incident_date`, `file_extension`, `file_ext_chksum`
		)
		VALUES(
			var_matched_id, `par_location_id`,`par_event_id`,`par_event_details`, var_client_id, var_user_id, var_GroupId,`par_incident_date`, `par_file_extention`, `par_file_ext_chksum`
		);
		
		SET var_md5 = MD5(CONCAT(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id));
		SET var_matched_id = (SELECT rowid FROM rpt_fam_chart_summary WHERE md5_match=var_md5);
		IF var_matched_id IS NOT NULL THEN
			IF par_event_id=1 AND par_location_id=1 THEN 
				UPDATE rpt_fam_chart_summary SET files_copied_on_local=(files_copied_on_local+1) WHERE `rowid`=var_matched_id;
				
			ELSEIF par_event_id=1 AND par_location_id=2 THEN 
				UPDATE rpt_fam_chart_summary SET files_copied_on_removable=(files_copied_on_removable+1) WHERE `rowid`=var_matched_id;
				
			ELSEIF par_event_id=1 AND par_location_id=3 THEN 
				UPDATE rpt_fam_chart_summary SET files_copied_on_network=(files_copied_on_network+1) WHERE `rowid`=var_matched_id;
				
			ELSEIF par_event_id=2 AND par_location_id=1 THEN 
				UPDATE rpt_fam_chart_summary SET files_deleted_on_local=(files_deleted_on_local+1) WHERE `rowid`=var_matched_id;
				
			ELSEIF par_event_id=2 AND par_location_id=2 THEN 
				UPDATE rpt_fam_chart_summary SET files_deleted_on_removable=(files_deleted_on_removable+1) WHERE `rowid`=var_matched_id;
				
			ELSEIF par_event_id=2 AND par_location_id=3 THEN 
				UPDATE rpt_fam_chart_summary SET files_deleted_on_network=(files_deleted_on_network+1) WHERE `rowid`=var_matched_id;
				
			ELSEIF par_event_id=3 AND par_location_id=1 THEN 
				UPDATE rpt_fam_chart_summary SET files_renamed_on_local=(files_renamed_on_local+1) WHERE `rowid`=var_matched_id;
				
			ELSEIF par_event_id=3 AND par_location_id=2 THEN 
				UPDATE rpt_fam_chart_summary SET files_renamed_on_removable=(files_renamed_on_removable+1) WHERE `rowid`=var_matched_id;
				
			ELSEIF par_event_id=3 AND par_location_id=3 THEN 
				UPDATE rpt_fam_chart_summary SET files_renamed_on_network=(files_renamed_on_network+1) WHERE `rowid`=var_matched_id;
			END IF;
			
		ELSE
			IF par_event_id=1 AND par_location_id=1 THEN 
				INSERT INTO rpt_fam_chart_summary(incident_date_only, GroupId, client_id, user_id, md5_match, files_copied_on_local) 
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, var_md5, 1);
				
			ELSEIF par_event_id=1 AND par_location_id=2 THEN 
				INSERT INTO rpt_fam_chart_summary(incident_date_only, GroupId, client_id, user_id, md5_match, files_copied_on_removable) 
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, var_md5, 1);
				
			ELSEIF par_event_id=1 AND par_location_id=3 THEN 
				INSERT INTO rpt_fam_chart_summary(incident_date_only, GroupId, client_id, user_id, md5_match, files_copied_on_network) 
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, var_md5, 1);
				
			ELSEIF par_event_id=2 AND par_location_id=1 THEN 
				INSERT INTO rpt_fam_chart_summary(incident_date_only, GroupId, client_id, user_id, md5_match, files_deleted_on_local) 
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, var_md5, 1);
				
			ELSEIF par_event_id=2 AND par_location_id=2 THEN 
				INSERT INTO rpt_fam_chart_summary(incident_date_only, GroupId, client_id, user_id, md5_match, files_deleted_on_removable) 
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, var_md5, 1);
				
			ELSEIF par_event_id=2 AND par_location_id=3 THEN 
				INSERT INTO rpt_fam_chart_summary(incident_date_only, GroupId, client_id, user_id, md5_match, files_deleted_on_network) 
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, var_md5, 1);
				
			ELSEIF par_event_id=3 AND par_location_id=1 THEN 
				INSERT INTO rpt_fam_chart_summary(incident_date_only, GroupId, client_id, user_id, md5_match, files_renamed_on_local) 
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, var_md5, 1);
				
			ELSEIF par_event_id=3 AND par_location_id=2 THEN 
				INSERT INTO rpt_fam_chart_summary(incident_date_only, GroupId, client_id, user_id, md5_match, files_renamed_on_removable) 
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, var_md5, 1);
				
			ELSEIF par_event_id=3 AND par_location_id=3 THEN 
				INSERT INTO rpt_fam_chart_summary(incident_date_only, GroupId, client_id, user_id, md5_match, files_renamed_on_network) 
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, var_md5, 1);
			END IF;
		END IF;
		
		SET var_md5 = MD5(CONCAT(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), par_file_ext_chksum, var_client_id, var_user_id));
		SET var_matched_id = NULL; 
		SET var_matched_id = (SELECT rowid FROM rpt_fam_extn_summary WHERE md5_match=var_md5);
		IF var_matched_id IS NOT NULL THEN
			IF par_event_id=1 AND par_location_id=1 THEN 
				UPDATE rpt_fam_extn_summary SET files_copied_on_local=(files_copied_on_local+1) WHERE `rowid`=var_matched_id;
				
			ELSEIF par_event_id=1 AND par_location_id=2 THEN 
				UPDATE rpt_fam_extn_summary SET files_copied_on_removable=(files_copied_on_removable+1) WHERE `rowid`=var_matched_id;
				
			ELSEIF par_event_id=1 AND par_location_id=3 THEN 
				UPDATE rpt_fam_extn_summary SET files_copied_on_network=(files_copied_on_network+1) WHERE `rowid`=var_matched_id;
				
			ELSEIF par_event_id=2 AND par_location_id=1 THEN 
				UPDATE rpt_fam_extn_summary SET files_deleted_on_local=(files_deleted_on_local+1) WHERE `rowid`=var_matched_id;
				
			ELSEIF par_event_id=2 AND par_location_id=2 THEN 
				UPDATE rpt_fam_extn_summary SET files_deleted_on_removable=(files_deleted_on_removable+1) WHERE `rowid`=var_matched_id;
				
			ELSEIF par_event_id=2 AND par_location_id=3 THEN 
				UPDATE rpt_fam_extn_summary SET files_deleted_on_network=(files_deleted_on_network+1) WHERE `rowid`=var_matched_id;
				
			ELSEIF par_event_id=3 AND par_location_id=1 THEN 
				UPDATE rpt_fam_extn_summary SET files_renamed_on_local=(files_renamed_on_local+1) WHERE `rowid`=var_matched_id;
				
			ELSEIF par_event_id=3 AND par_location_id=2 THEN 
				UPDATE rpt_fam_extn_summary SET files_renamed_on_removable=(files_renamed_on_removable+1) WHERE `rowid`=var_matched_id;
				
			ELSEIF par_event_id=3 AND par_location_id=3 THEN 
				UPDATE rpt_fam_extn_summary SET files_renamed_on_network=(files_renamed_on_network+1) WHERE `rowid`=var_matched_id;
			END IF;
			
		ELSE
			IF par_event_id=1 AND par_location_id=1 THEN 
				INSERT INTO rpt_fam_extn_summary(incident_date_only, GroupId, client_id, user_id, file_extension, file_ext_chksum, md5_match, files_copied_on_local) 
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, par_file_extention, par_file_ext_chksum, var_md5, 1);
				
			ELSEIF par_event_id=1 AND par_location_id=2 THEN 
				INSERT INTO rpt_fam_extn_summary(incident_date_only, GroupId, client_id, user_id, file_extension, file_ext_chksum, md5_match, files_copied_on_removable) 
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, par_file_extention, par_file_ext_chksum, var_md5, 1);
				
			ELSEIF par_event_id=1 AND par_location_id=3 THEN 
				INSERT INTO rpt_fam_extn_summary(incident_date_only, GroupId, client_id, user_id, file_extension, file_ext_chksum, md5_match, files_copied_on_network) 
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, par_file_extention, par_file_ext_chksum, var_md5, 1);
				
			ELSEIF par_event_id=2 AND par_location_id=1 THEN 
				INSERT INTO rpt_fam_extn_summary(incident_date_only, GroupId, client_id, user_id, file_extension, file_ext_chksum, md5_match, files_deleted_on_local) 
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, par_file_extention, par_file_ext_chksum, var_md5, 1);
				
			ELSEIF par_event_id=2 AND par_location_id=2 THEN 
				INSERT INTO rpt_fam_extn_summary(incident_date_only, GroupId, client_id, user_id, file_extension, file_ext_chksum, md5_match, files_deleted_on_removable) 
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, par_file_extention, par_file_ext_chksum, var_md5, 1);
				
			ELSEIF par_event_id=2 AND par_location_id=3 THEN 
				INSERT INTO rpt_fam_extn_summary(incident_date_only, GroupId, client_id, user_id, file_extension, file_ext_chksum, md5_match, files_deleted_on_network) 
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, par_file_extention, par_file_ext_chksum, var_md5, 1);
				
			ELSEIF par_event_id=3 AND par_location_id=1 THEN 
				INSERT INTO rpt_fam_extn_summary(incident_date_only, GroupId, client_id, user_id, file_extension, file_ext_chksum, md5_match, files_renamed_on_local) 
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, par_file_extention, par_file_ext_chksum, var_md5, 1);
				
			ELSEIF par_event_id=3 AND par_location_id=2 THEN 
				INSERT INTO rpt_fam_extn_summary(incident_date_only, GroupId, client_id, user_id, file_extension, file_ext_chksum, md5_match, files_renamed_on_removable) 
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, par_file_extention, par_file_ext_chksum, var_md5, 1);
				
			ELSEIF par_event_id=3 AND par_location_id=3 THEN 
				INSERT INTO rpt_fam_extn_summary(incident_date_only, GroupId, client_id, user_id, file_extension, file_ext_chksum, md5_match, files_renamed_on_network) 
				VALUES(unix_timestamp(DATE(FROM_UNIXTIME(par_incident_date))), var_GroupId, var_client_id, var_user_id, par_file_extention, par_file_ext_chksum, var_md5, 1);
			END IF;
		END IF;
		
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `pr_insert_firewall_rpt`;
DELIMITER $$
CREATE PROCEDURE `pr_insert_firewall_rpt`(
	IN `par_incident_date` INT(11),
	IN `par_local_ip` VARCHAR(15),
	IN `par_remote_ip` VARCHAR(15),
	IN `par_protocol` VARCHAR(20),
	IN `par_direction` VARCHAR(50),
	IN `par_firewall_level` VARCHAR(20),
	IN `par_protocol_type` INT(11),
	IN `par_local_port` INT(11),
	IN `par_remote_port` INT(11),
	IN `par_exception_name` VARCHAR(100),
	IN `par_machine_id` varchar(45),
	IN `par_username` varchar(512)
)
BEGIN
	DECLARE var_client_id BIGINT(20) DEFAULT NULL;
	DECLARE var_GroupId INT(11) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	
	SELECT client_id, GroupId INTO var_client_id, var_GroupId FROM client_master WHERE guid=par_machine_id;
	SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username=par_username LIMIT 1;
	IF var_user_id IS NULL THEN
		INSERT INTO rpt_user_master(username) VALUES(par_username);
		SET var_user_id = (select last_insert_id());
	END IF;
	
	IF var_client_id IS NOT NULL THEN
		INSERT INTO `rpt_firewall_fact`(
			`incident_date`,
			`local_ip`,
			`remote_ip`,
			`protocol`,
			`direction`,
			`firewall_level`,
			`protocol_type`,
			`local_port`,
			`remote_port`,
			`exception_name`,
			`client_id`,
			`user_id`,
			`GroupId`
		)
		VALUES(
			par_incident_date,
			par_local_ip,
			par_remote_ip,
			par_protocol,
			par_direction,
			par_firewall_level,
			par_protocol_type,
			par_local_port,
			par_remote_port,
			par_exception_name,
			var_client_id,
			var_user_id,
			var_GroupId
		);
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `pr_insert_ips_rpt`;
DELIMITER $$
