<?php
if (!defined('_VIS_'))
    die('Error: Call from undefined function');
class subreport
{
    var $error;
    
    function subreport()
    {
        $database = new class_database();
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'setting.php');
        $object = new class_setting($database);
        if (!defined("VIS_TIMEZONE")) {
            $dom_vars = $object->get_domain(VIS_TABLE_PREFIX . 'domain.is_enabled=1', '', 0, '', 0, 4);
            if ($dom_vars)
                list($dinfo, $domain) = $dom_vars;
            if ($domain != -1)
                $domain = $domain[0];
            
            if ($domain['timezone_dst'] == 1)
                $domain['timezone'] += 3600;
            define('VIS_TIMEZONE', $domain['timezone']);
        } else {
            
        }
    }
    
   
//domain report
function domain_report($operation)
{	
global $vis_language;
$report_title = $vis_language['TXTDOMAINREPORT'];
$export       = 1;
if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
				$database = new class_database('mysql', 1);
else
				$database = new class_database();
include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
$tkt_object = new class_ticket($database);
$filter     = VIS_TABLE_PREFIX . 'domain.is_enabled=1 ';
$domain     = $tkt_object->get_domain($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
								$filter = $operation['filter'];
				else {
								if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
												$data = $operation;
								else
												$data = parse_form_array($operation['vis_action']);
								if (count($data) == 0) {
												include(VIS_PATH_TEMPLATE . 'report_form.php');
												return true;
								}
								if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								/***** date difference ****/
								if ($data['duration'] == 4) {
												list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
												if (!$ret) {
																echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
																return false;
												}
								}
								if ($data['duration'] == 0 && $data['domain_id'] == 0) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								} else if ($data['duration'] != 0 && $data['domain_id'] == 0) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDOMAINFORRES'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								} else if ($data['duration'] == 0 && $data['domain_id'] != 0) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								
								$currentdate = date('Y-m-d h:i:s');
								if ($data['duration'] == 1) //if select Last 30 days duration
												{
												$monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
												$filter    = 'receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and domain_id=' . $data['domain_id'].'  and is_deleted=0 and is_spam=0 and draft_id=0';
								} else if ($data['duration'] == 2) //if select Last 7 days duration
												{
												$weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
												$filter   = 'receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and domain_id=' . $data['domain_id'].'  and is_deleted=0 and is_spam=0 and draft_id=0';
								}
								
								else if ($data['duration'] == 3) //if select Last 24 hours duration
												{
												$hour = strtotime("-1 day ") - VIS_TIMEZONE;
												
												$filter = ' receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and domain_id=' . $data['domain_id'].'  and is_deleted=0 and is_spam=0 and draft_id=0';
								} else if ($data['duration'] == 4) //if select Custom duration
												{
												$from   = strtotime($data['from_date']) - VIS_TIMEZONE;
												$to     = strtotime($data['to_date']) - VIS_TIMEZONE;
												$filter = 'receive_date between ' . $from . ' AND ' . $to . ' and domain_id=' . $data['domain_id'].'  and is_deleted=0 and is_spam=0 and draft_id=0';
												
								}
				}
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
				
				$object = new class_report($database);
				//$report=$object->get_ticket_count_by_department($filter, $staff_id, VIS_LOCATION_AREA);  print_r($report);
				
				list($report, $counts) = $object->get_ticket_count_by_department($filter, '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
				
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
								
								
								include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
								$export = new export();
								
								$j = 0;
								foreach ($report as $i) {
												foreach ($i as $key => $val) {
																if ($key == 'department_name') {
																				$report_exp[$j][$key] = $val;
																} elseif ($key == 'total') {
																				$report_exp[$j][$key] = $val;
																} elseif ($key == 'date_created') {
																				$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
																} elseif ($key == 'openicket') {
																				$report_exp[$j][$key] = $val;
																} elseif ($key == 'waitticket') {
																				$report_exp[$j][$key] = $val;
																} elseif ($key == 'closeticket') {
																				$report_exp[$j][$key] = $val;
																} 
																
												}
												
$report_exp1[$j]['department_name']=$report_exp[$j]['department_name'];
$report_exp1[$j]['total']=$report_exp[$j]['total'];
$report_exp1[$j]['open']=$report_exp[$j]['openicket'];
$report_exp1[$j]['close']=$report_exp[$j]['closeticket'];
$report_exp1[$j]['other_than_open_and_close']=$report_exp[$j]['waitticket'];
$j++;
								}
$report_exp1[$j]['summary'] = $vis_language['TXTSUMMARY'];
$report_exp1[$j]['total'] = $vis_language['TXTTOTALTICKET'].': '.$counts['total'];
$report_exp1[$j]['closetotal'] = $vis_language['TXTTOTALOPEN'].': '.$counts['closetotal'];
$report_exp1[$j]['opentotal'] = $vis_language['TXTTOTALCLOSE'].': '.$counts['opentotal'];
$report_exp1[$j]['awaiting'] = $vis_language['TXTTOTALPROGRESS'].': '.$counts['awaiting'];
								$completed_date = date('Y-m-d H:i:s');
								$report_exp1['Report_title'] = $report_title;	// To display title which shows on report form
				//$report_exp1['Report_title'] = $operation['export_name']; // To display title which entered in request report popup form
				
								$filename = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
								if ($operation['export_type'] == 1) {
												$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
												if (file_exists('../downloads/' . $filename . '.csv')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 2) {
												$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.html')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 3) {
												
												
												$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.xls')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								}
								
								if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
												$database->database_disconnect();
												$database = new class_database();
												include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
												$object = new class_report($database);
												
								}
								$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
								if (!$result)
												return false;
								$this->email_send_download($operation);
								return true;
				}
				
				else
								include(VIS_PATH_TEMPLATE . 'domain_report.php');
				
				
				
} else
				include(VIS_PATH_TEMPLATE . 'report_form.php');
$database->database_disconnect();
return true;
}
function channel_wise_tickets_report($operation)
{	
global $vis_language;
$report_title = $vis_language['TXTTKTCHNLGRP'];
$export       = 1;
if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
				$database = new class_database('mysql', 1);
else
				$database = new class_database();
include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
$tkt_object = new class_ticket($database);
include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'staff.php');
$object_staff  = new class_staff($database);
$departments   = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
$channels      = $tkt_object->get_channel($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
$staff_filter  = "staff_id =" . VIS_STAFF_ID;
$staff_details = $object_staff->get_staff($staff_filter, $userid, $user);
// print_r($staff_details[0]['department_access']);
$dept_ids      = '';
for ($d = 0; $d < count($departments); $d++) {
				$dept_ids .= $departments[$d]['department_id'] . ',';
}
if ($dept_ids != '') {
				$dept_ids = substr($dept_ids, 0, -1);
}
if ($data['department_id'] == 0) {
				$dept_id = ($staff_details[0]['department_access'] == '' || $staff_details[0]['department_access'] == 0) ? $dept_ids : $staff_details[0]['department_access'];
}
$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
								$filter = $operation['filter'];
				else {
								if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
												$data = $operation;
								else
												$data = parse_form_array($operation['vis_action']);
								if (count($data) == 0) {
												include(VIS_PATH_TEMPLATE . 'report_form.php');
												return true;
								}
								if ($data['department_id'] == '') {
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                return false;
            }
								if ($data['department_id'] != 0) {
												$filter = 'c.channel_id=' . $data['channel_id'] . ' and t.department_id=' . $data['department_id'];
								} else {
												$filter = 'c.channel_id='.$data['channel_id'].' and t.department_id in ('.$dept_id.')'; 
												//$filter = 'c.channel_id=' . $data['channel_id'];
								}
				}
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
				$object = new class_report($database);
				list($totalTicksClosed, $report) = $object->get_channelwise_tickets_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
				//print_r($totalTicksClosed);
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
								include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
								$export         = new export();
$j=0;
foreach ($report as $i){
foreach ($i as $key => $val){
if($key == 'ticket_hash'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'subject'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'receive_date'){
$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
}
elseif($key == 'department_name'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'channel_name'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'first_name'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'last_name'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'group_name'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'email'){
$report_exp[$j][$key] = $val;
}
												}
$report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash']; 
$report_exp1[$j]['subject'] = $report_exp[$j]['subject'];
								$report_exp1[$j]['create_date'] = $report_exp[$j]['receive_date'];	
								$report_exp1[$j]['department'] = $report_exp[$j]['department_name'];
								$report_exp1[$j]['channel_name'] = $report_exp[$j]['channel_name']; 				
$report_exp1[$j]['client_name'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name']; 
$report_exp1[$j]['email'] = $report_exp[$j]['email'];
$report_exp1[$j]['Organization'] = $report_exp[$j]['group_name'];
								$j++;
				}
$report_exp1[$j]['total_ticket'] = $vis_language['TXTTOTALNOTKT'].": ".count($report);
$report_exp1[$j]['total_closed'] = $vis_language['TXTTOTALTICKETSCLOSED'].": ".$totalTicksClosed['totalTicksClosed'];
								$completed_date = date('Y-m-d H:i:s');
								$report_exp1['Report_title'] = $report_title;
				
								$filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
								if ($operation['export_type'] == 1) {
												$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
												if (file_exists('../downloads/' . $filename . '.csv')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 2) {
												$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.html')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 3) {
												
												
												$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.xls')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								}
								
								if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
												$database->database_disconnect();
												$database = new class_database();
												include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
												$object = new class_report($database);
												
								}
								$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
								if (!$result)
												return false;
								$this->email_send_download($operation);
								return true;
				} else
								include(VIS_PATH_TEMPLATE . 'tickets_groupby_channel.php');
} else
				include(VIS_PATH_TEMPLATE . 'report_form.php');
$database->database_disconnect();
return true;
}
	function first_response_report($operation)
	{	
		global $vis_language;
		$report_title = $vis_language['TXTFIRSTRESPONSE'];
		if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
			$database = new class_database('mysql', 1);
		else
			$database = new class_database();
		$export = 1;
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
		$tkt_object       = new class_ticket($database);
		$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
		$departments      = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1',$staff_id, VIS_LOCATION_AREA);
		if((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '')
		{
			if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
				$filter = $operation['filter'];
			else {
				if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
					$data = $operation;
				else
					$data = parse_form_array($operation['vis_action']);
				
				if (count($data) == 0) {
					include(VIS_PATH_TEMPLATE . 'report_form.php');
					return true;
				}
				if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
					echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
					return false;
				}
				if ($data['duration'] == 4) {
					list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
					if (!$ret) {
						echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
						return false;
					}
				}
				if ($data['duration'] == 0 && $data['department_id'] == 0) {
					echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
					return false;
				} else if ($data['duration'] != 0 && $data['department_id'] == 0) {
					echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
					return false;
				} else if ($data['duration'] == 0 && $data['department_id'] != 0) {
					echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
					return false;
				}
				$currentdate = date('Y-m-d h:i:s');
				
				if ($data['duration'] == 1) //if select Last 30 days duration
				{
					$department_id = ($data['department_id'] == 1) ? '' : $data['department_id'];
					$monthDate     = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
					$filter        = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and is_visible=1 and reply_forward=1';
					$filter .= ($data['department_id'] != 1) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
								
				} 
				else if ($data['duration'] == 2) //if select Last 7 days duration
				{
					$department_id = ($data['department_id'] == 1) ? '' : $data['department_id'];
					$weekDate      = strtotime("-1 week ") - VIS_TIMEZONE;
					$filter        = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and ' . VIS_TABLE_PREFIX . 'ticket_post.reply_forward=1';
					$filter .= ($data['department_id'] != 1) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
				} 
				else if ($data['duration'] == 3) //if select Last 24 hours duration
				{
					$department_id = ($data['department_id'] == 1) ? '' : $data['department_id'];
					$hour          = strtotime("-1 day ") - VIS_TIMEZONE;
					$filter        = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and ' . VIS_TABLE_PREFIX . 'ticket_post.reply_forward=1';
					$filter .= ($data['department_id'] != 1) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
				} 
				else if ($data['duration'] == 4) //if select Custom duration
				{
					$department_id = ($data['department_id'] == 1) ? '' : $data['department_id'];
					$from          = strtotime($data['from_date']) - VIS_TIMEZONE;
					$to            = strtotime($data['to_date']) - VIS_TIMEZONE;
					$filter        = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $from . ' AND ' . $to . ' and ' . VIS_TABLE_PREFIX . 'ticket_post.reply_forward=1';
					$filter .= ($data['department_id'] != 1) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
				}			
			}
				
			include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
			$object       = new class_report($database);
			$total_post   = 0;
			$total_ticket = array();
			$total_time   = 0;
			$filter .= ' and ' . VIS_TABLE_PREFIX . 'ticket.is_deleted!=1 and ' . VIS_TABLE_PREFIX . 'ticket.is_spam!=1';
			$post_details = $object->get_first_response_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
			if ($post_details != -1)
				for ($i = 0; $i < count($post_details); $i++) {
					if (!isset($report[$post_details[$i]['ticket_id']]['start_time'])){
						$report[$post_details[$i]['ticket_id']]['ticket_hash']  = $post_details[$i]['ticket_hash'];
						$report[$post_details[$i]['ticket_id']]['subject']      = $post_details[$i]['subject'];
						$report[$post_details[$i]['ticket_id']]['ticket_entry'] = $post_details[$i]['ticket_entry'];
						$report[$post_details[$i]['ticket_id']]['first_name']      = $post_details[$i]['first_name'];
						$report[$post_details[$i]['ticket_id']]['last_name']      = $post_details[$i]['last_name'];
						$report[$post_details[$i]['ticket_id']]['email']      = $post_details[$i]['email'];
						$report[$post_details[$i]['ticket_id']]['group_name']      = $post_details[$i]['group_name'];
						$report[$post_details[$i]['ticket_id']]['start_time']   = $post_details[$i]['receive_date'];
						//$report[$post_details[$i]['ticket_id']]['timestamp']=$post_details[$i]['receive_date']-$post_details[$i]['ticket_entry'];
						$report[$post_details[$i]['ticket_id']]['ticket_id'] = $post_details[$i]['ticket_id'];
						$report[$post_details[$i]['ticket_id']]['timestamp'] = $post_details[$i]['receive_date'] - $post_details[$i]['ticket_entry'];
						$report[$post_details[$i]['ticket_id']]['is_posted'] = true;
						$total_ticket[$post_details[$i]['ticket_id']]        = $post_details[$i]['ticket_id'];
						$total_time  = $report[$post_details[$i]['ticket_id']]['timestamp'];
						$totalavg_time+= $report[$post_details[$i]['ticket_id']]['timestamp'];
						$total_post++;							
					} else {
						//$report[$post_details[$i]['ticket_id']]['timestamp']= $post_details[$i]['receive_date'] - $report[$post_details[$i]['ticket_id']]['start_time'];
						if ($report[$post_details[$i]['ticket_id']]['timestamp'] < 0)
							$report[$post_details[$i]['ticket_id']]['timestamp'] = 0;
						
						$report[$post_details[$i]['ticket_id']]['count']++;
						$total_ticket[$post_details[$i]['ticket_id']] = $post_details[$i]['ticket_id'];
						$total_time += $post_details[$i]['receive_date'] - $report[$post_details[$i]['ticket_id']]['start_time'];
					}
				} 
			else
				$report = -1;
		
			if(isset($operation['vis_export']) && $operation['vis_export'] == 1){
				include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
				$export = new export();
				$j      = 0;
				foreach ($report as $i) {
					if(!$i['is_posted'])
						continue;
					
					foreach ($i as $key => $val) {
						if ($key == 'is_posted' && !$val)
							continue;
						if ($key == 'ticket_hash' || $key == 'subject')
							$report_exp[$j][$key] = $val;
						elseif ($key == 'ticket_entry') {
							$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
						} elseif ($key == 'start_time') {
							$report_exp[$j]['reply_date'] = format_datetime_rpt($val,$staff_id);                          
						} elseif ($key == 'timestamp')
							if ($val != 0)
								$report_exp[$j]['first_response_time'] = count_remain_time($val);
							else
								$report_exp[$j]['avg_response_time'] = 0;
						elseif ($key == 'first_name' || $key == 'last_name' || $key == 'email' || $key == 'group_name')
							$report_exp[$j][$key] = $val;														
					}
					$report_exp1[$j]['ticket_hash']= $report_exp[$j]['ticket_hash'];
					$report_exp1[$j]['subject']= $report_exp[$j]['subject'];
					$report_exp1[$j]['client_name']= $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					$report_exp1[$j]['email']= $report_exp[$j]['email'];
					$report_exp1[$j]['organization']= $report_exp[$j]['group_name'];
					$report_exp1[$j]['creation_date']= $report_exp[$j]['ticket_entry'];
					$report_exp1[$j]['reply_date']= $report_exp[$j]['reply_date'];
					$report_exp1[$j]['first_response_time']=$report_exp[$j]['first_response_time'];
					$j++;
				}
				$report_exp1[$j]['total_avg_response_time'] = $vis_language['TXTTOTALAVGRESPONSETIME'].": ".count_remain_time( $totalavg_time/$total_post);
				$completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;	// To display title which shows on report form
				//$report_exp1['Report_title'] = $operation['export_name']; // To display title which entered in request report popup form
				$filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
				if ($operation['export_type'] == 1) {
					$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
					if (file_exists('../downloads/' . $filename . '.csv')) {
						$filename = $filename;
					} else {
						$filename = '';
					}
				} else if ($operation['export_type'] == 2) {
					$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
					if (file_exists('../downloads/' . $filename . '.html')) {
						$filename = $filename;
					} else {
						$filename = '';
					}
				} else if ($operation['export_type'] == 3) {
					$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
					if (file_exists('../downloads/' . $filename . '.xls')) {
						$filename = $filename;
					} else {
						$filename = '';
					}
				}
				
				if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
					$database->database_disconnect();
					$database = new class_database();
					include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
					$object = new class_report($database);
				}
				$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
				if (!$result)
					return false;
				
				$this->email_send_download($operation);
				return true;
			} 
			else
				include(VIS_PATH_TEMPLATE . 'first_response_report.php');
		} 
		else
			include(VIS_PATH_TEMPLATE . 'report_form.php');
		
		$database->database_disconnect();
		return true;
	}
	
	function response_report($operation)
	{	
		global $vis_language;
		$report_title = $vis_language['TXTAVGRESPONSEREPORT'];
		if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
			$database = new class_database('mysql', 1);
		else
			$database = new class_database();
		$export = 1;
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
		$tkt_object       = new class_ticket($database);
		$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
		$departments      = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1',$staff_id, VIS_LOCATION_AREA);
		if((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != ''){
			if(isset($operation['vis_export']) && $operation['vis_export'] == 1)
				$filter = $operation['filter'];
			else {
				if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
					$data = $operation;
				else
					$data = parse_form_array($operation['vis_action']);
				
				if (count($data) == 0) {
					include(VIS_PATH_TEMPLATE . 'report_form.php');
					return true;
				}
				if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
					echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
					return false;
				}
				if ($data['duration'] == 4) {
					list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
					if (!$ret) {
						echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
						return false;
					}
				}
				if ($data['duration'] == 0 && $data['department_id'] == 0) {
					echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
					return false;
				} else if ($data['duration'] != 0 && $data['department_id'] == 0) {
					echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
					return false;
				} else if ($data['duration'] == 0 && $data['department_id'] != 0) {
					echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
					return false;
				}
				$currentdate = date('Y-m-d h:i:s');
				if ($data['duration'] == 1) //if select Last 30 days duration
				{
					$monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
					//$filter    = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and department_id=' . $data['department_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and is_visible=1';
					$filter = VIS_TABLE_PREFIX.'auditlog.entry_date between '.$monthDate.' AND '.strtotime($currentdate).' and '.VIS_TABLE_PREFIX.'ticket.department_id='.$data['department_id'].' and ('.VIS_TABLE_PREFIX.'auditlog.operation_id <=2 or '.VIS_TABLE_PREFIX.'auditlog.operation_id=50 or '.VIS_TABLE_PREFIX.'auditlog.operation_id = 51)';
				} else if ($data['duration'] == 2) //if select Last 7 days duration
				{
					$weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
					//$filter   = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and department_id=' . $data['department_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and is_visible=1';
					$filter = VIS_TABLE_PREFIX.'auditlog.entry_date between '.$weekDate.' AND '.strtotime($currentdate).' and '.VIS_TABLE_PREFIX.'ticket.department_id='.$data['department_id'].' and ('.VIS_TABLE_PREFIX.'auditlog.operation_id <=2 or '.VIS_TABLE_PREFIX.'auditlog.operation_id=50 or '.VIS_TABLE_PREFIX.'auditlog.operation_id = 51)';
				} else if ($data['duration'] == 3) //if select Last 24 hours duration
				{
					$hour   = strtotime("-1 day ") - VIS_TIMEZONE;
					//$filter = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and department_id=' . $data['department_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and is_visible=1';
					$filter = VIS_TABLE_PREFIX.'auditlog.entry_date between '.$hour.' AND '.strtotime($currentdate).' and '.VIS_TABLE_PREFIX.'ticket.department_id='.$data['department_id'].' and ('.VIS_TABLE_PREFIX.'auditlog.operation_id <=2 or '.VIS_TABLE_PREFIX.'auditlog.operation_id=50 or '.VIS_TABLE_PREFIX.'auditlog.operation_id = 51)';
				} else if ($data['duration'] == 4) //if select Custom duration
				{
					$from   = strtotime($data['from_date']) - VIS_TIMEZONE;
					$to     = strtotime($data['to_date']) - VIS_TIMEZONE;
					//$filter = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $from . ' AND ' . $to . ' and department_id=' . $data['department_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and is_visible=1';
					$filter = VIS_TABLE_PREFIX.'auditlog.entry_date between '.$from.' AND '.$to.' and '.VIS_TABLE_PREFIX.'ticket.department_id='.$data['department_id'].' and ('.VIS_TABLE_PREFIX.'auditlog.operation_id <=2 or '.VIS_TABLE_PREFIX.'auditlog.operation_id=50 or '.VIS_TABLE_PREFIX.'auditlog.operation_id = 51)';
				}
			}
				
			include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
			$object       = new class_report($database);
			$total_post   = 0;
			$total_ticket = array();
			$total_time   = 0;
			$filter .= ' and ' . VIS_TABLE_PREFIX . 'ticket.is_deleted!=1 and ' . VIS_TABLE_PREFIX . 'ticket.is_spam!=1';
			$post_details = $object->get_performance_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
			if ($post_details != -1)
				for ($i = 0; $i < count($post_details); $i++) {
					// ===================== Code to calculate timestamp (Starts Here) =====================
					if($post_details[$i]['operation_id']==0){
						$report[$post_details[$i]['ticket_id']]['post_date']=$post_details[$i]['entry_date'];
					}else if($post_details[$i]['operation_id']==50){
						$report[$post_details[$i]['ticket_id']]['pause_date']=$post_details[$i]['entry_date'];
					}else if($post_details[$i]['operation_id']==51){
						$report[$post_details[$i]['ticket_id']]['resume_date']=$post_details[$i]['entry_date'];
						$report[$post_details[$i]['ticket_id']]['slapasue_diff']= $report[$post_details[$i]['ticket_id']]['resume_date'] - $report[$post_details[$i]['ticket_id']]['pause_date'];
						$report[$post_details[$i]['ticket_id']]['sla_cal_date']=$report[$post_details[$i]['ticket_id']]['sla_cal_date'] + ($report[$post_details[$i]['ticket_id']]['resume_date'] - $report[$post_details[$i]['ticket_id']]['pause_date']);
						$report[$post_details[$i]['ticket_id']]['resume_date']='';
						$report[$post_details[$i]['ticket_id']]['pause_date']='';
					}else if($post_details[$i]['operation_id']==1 && $post_details[$i]['operator']!=1){
						$report[$post_details[$i]['ticket_id']]['post_diff']=($post_details[$i]['entry_date'] - $report[$post_details[$i]['ticket_id']]['post_date']);
						$report[$post_details[$i]['ticket_id']]['maintaint_date']=($post_details[$i]['entry_date'] - $report[$post_details[$i]['ticket_id']]['post_date']) - ($report[$post_details[$i]['ticket_id']]['sla_cal_date']);
					}
					// ===================== Code to calculate timestamp (Ends Here) =====================
					if($post_details[$i]['reply_forward'] == 1){
						$reply_count = $reply_count+$post_details[$i]['reply_forward'];
					}
					if (!isset($report[$post_details[$i]['ticket_id']]['start_time'])) {
						$report[$post_details[$i]['ticket_id']]['ticket_hash']  = $post_details[$i]['ticket_hash'];
						$report[$post_details[$i]['ticket_id']]['subject']      = $post_details[$i]['subject'];
						$report[$post_details[$i]['ticket_id']]['first_name']      = $post_details[$i]['first_name'];
						$report[$post_details[$i]['ticket_id']]['last_name']      = $post_details[$i]['last_name'];
						$report[$post_details[$i]['ticket_id']]['email']      = $post_details[$i]['email'];
						$report[$post_details[$i]['ticket_id']]['group_name']      = $post_details[$i]['group_name'];
						
						$report[$post_details[$i]['ticket_id']]['timestamp']      = ($post_details[$i]['old_info'] != '' && $post_details[$i]['old_info'] != 0) ? $post_details[$i]['old_info'] - $post_details[$i]['receive_date'] : 0;
						$report[$post_details[$i]['ticket_id']]['ticket_entry'] = $post_details[$i]['ticket_entry'];
						$report[$post_details[$i]['ticket_id']]['start_time']   = $post_details[$i]['receive_date'];
						//$report[$post_details[$i]['ticket_id']]['timestamp']    = 0; //$post_details[$i]['receive_date']-$post_details[$i]['ticket_entry'];
						$report[$post_details[$i]['ticket_id']]['ticket_id']    = $post_details[$i]['ticket_id'];
						
						$report[$post_details[$i]['ticket_id']]['solve_time']     = 0;
						$report[$post_details[$i]['ticket_id']]['count']        = 0;
						
						$report[$post_details[$i]['ticket_id']]['total_sla_seconds'] = 0;
							$report[$post_details[$i]['ticket_id']]['used_sla_seconds'] = 0;
						//if ($post_details[$i]['submitter'] == 1)
						if ($post_details[$i]['operator'] == 1)
							$report[$post_details[$i]['ticket_id']]['is_posted'] = false;
						else {
							$report[$post_details[$i]['ticket_id']]['is_posted'] = true;
							$total_post++;
							$total_ticket[$post_details[$i]['ticket_id']] = $post_details[$i]['ticket_id'];
							//$total_time += $post_details[$i]['receive_date'] - $report[$post_details[$i]['ticket_id']]['start_time'];
							//$total_time += $sla_response_time;
							$total_time = 0;
							$replied[$post_details[$i]['ticket_id']] = 1;
						}
					} elseif ($post_details[$i]['operator'] == 1) {
						//elseif ($post_details[$i]['submitter'] == 1)
						$report[$post_details[$i]['ticket_id']]['start_time'] = $post_details[$i]['receive_date'];
					} elseif ($post_details[$i]['operation_id'] == 2) {
						$report[$post_details[$i]['ticket_id']]['solve_time']   = $post_details[$i]['entry_date'];
						$report[$post_details[$i]['ticket_id']]['resolve_time'] = $post_details[$i]['resolution_time'] == 0 ? 0 : ($post_details[$i]['resolution_time'] - $post_details[$i]['entry_date']);
					} elseif($post_details[$i]['old_info'] != '' && $post_details[$i]['old_info'] != 0) {
						// ===================== Code to calculate timestamp (Start) =====================
						$total_sla_seconds = ($post_details[$i]['due_time'] - $report[$post_details[$i]['ticket_id']]['start_time']);
						$sla_response_time = round($report[$post_details[$i]['ticket_id']]['maintaint_date']);
						$report[$post_details[$i]['ticket_id']]['total_sla_seconds'] = $total_sla_seconds;
						$report[$post_details[$i]['ticket_id']]['used_sla_seconds'] = $sla_response_time;
						//$report[$post_details[$i]['ticket_id']]['timestamp'] += $post_details[$i]['receive_date'] - $report[$post_details[$i]['ticket_id']]['start_time'];
						$report[$post_details[$i]['ticket_id']]['timestamp'] = $sla_response_time;																
						$report[$post_details[$i]['ticket_id']]['count']++;
						$report[$post_details[$i]['ticket_id']]['is_posted'] = true;
						$total_post++;
						$total_ticket[$post_details[$i]['ticket_id']] = $post_details[$i]['ticket_id'];
						//$total_time += $post_details[$i]['receive_date'] - $report[$post_details[$i]['ticket_id']]['start_time'];
						$total_time += $sla_response_time;
						$replied[$post_details[$i]['ticket_id']] = 1;
					}
				} 
			else
				$report = -1;
			
			$tcket_cnt_reply=$reply_count;
			if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
				include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
				$export = new export();
				$j      = 0;
				foreach ($report as $i) {
					if (!$i['is_posted'])
						continue;
						
					foreach ($i as $key => $val) {
						if ($key == 'is_posted' && !$val)
							continue;
						if ($key == 'ticket_hash' || $key == 'subject')
							$report_exp[$j][$key] = $val;
						elseif ($key == 'ticket_entry') {
							$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);  
						} 
						elseif ($key == 'timestamp')
							if ($val != 0 || $i['count'] != 0)
								$report_exp[$j]['avg_response_time'] = count_remain_time($val / $i['count']);
							else
								$report_exp[$j]['avg_response_time'] = "0S";
						elseif ($key == 'first_name' || $key == 'last_name' || $key == 'email' || $key == 'group_name')
							$report_exp[$j][$key] = $val;				
					}
					$report_exp1[$j]['ticket_hash']= $report_exp[$j]['ticket_hash'];
					$report_exp1[$j]['subject']= $report_exp[$j]['subject'];
					$report_exp1[$j]['client_name']= $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					$report_exp1[$j]['email']= $report_exp[$j]['email'];
					$report_exp1[$j]['organization']= $report_exp[$j]['group_name'];
					$report_exp1[$j]['creation_date']= $report_exp[$j]['ticket_entry'];
					$report_exp1[$j]['avg_response_time']= $report_exp[$j]['avg_response_time'];
					$j++;
				}
				//add summary
				$total_ticket_cnt=count($total_ticket);
				$avgTimeCount =count_remain_time($total_time/$total_post) ;
				$report_exp1[$j]['totalpostcount']=$vis_language['TXTTOTALPOSTCOUNT'].": ".$total_post;
				//$report_exp1[$j]['totalreplied']=$vis_language['TXTTOTALTICKETREPLIED'].": ".$tcket_cnt_reply.'/'.count($total_ticket);
				$report_exp1[$j]['totalreplied']=$vis_language['TXTTOTALTICKETREPLIED'].": ".count($replied).'/'.count($report);
				$report_exp1[$j]['avgreponsetime']=$vis_language['TXTTOTALAVGRESPONSETIME'].": ".($total_post==0?'0':(($avgTimeCount > 0)?$avgTimeCount:'0S'));
				$completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;	// To display title which shows on report form
				//$report_exp1['Report_title'] = $operation['export_name']; // To display title which entered in request report popup form
				$filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
				if ($operation['export_type'] == 1) {
					$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
					if (file_exists('../downloads/' . $filename . '.csv')) {
						$filename = $filename;
					} else {
						$filename = '';
					}
				} else if ($operation['export_type'] == 2) {
					$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
					if (file_exists('../downloads/' . $filename . '.html')) {
						$filename = $filename;
					} else {
						$filename = '';
					}
				} else if ($operation['export_type'] == 3) {
					$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
					if (file_exists('../downloads/' . $filename . '.xls')) {
						$filename = $filename;
					} else {
						$filename = '';
					}
				}
				if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
					$database->database_disconnect();
					$database = new class_database();
					include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
					$object = new class_report($database);
				}
				$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
				if (!$result)
					return false;
				
				$this->email_send_download($operation);
				return true;
			} 
			else
				include(VIS_PATH_TEMPLATE . 'performance_report.php');
		} else
			include(VIS_PATH_TEMPLATE . 'report_form.php');
		
		$database->database_disconnect();
		return true;
	}
	
function dept_report($operation)
{	
global $vis_language;
$report_title = $vis_language['TXTTOTALTICKETSRESOLVED'];
if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
				$database = new class_database('mysql', 1);
else
				$database = new class_database();
$export = 1;
include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
$tkt_object       = new class_ticket($database);
$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
$departments      = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', $staff_id, VIS_LOCATION_AREA);
if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
								$filter = $operation['filter'];
				else {
								if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
												$data = $operation;
								else
												$data = parse_form_array($operation['vis_action']);
								
								if (count($data) == 0) {
												include(VIS_PATH_TEMPLATE . 'report_form.php');
												return true;
								}
								if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								if ($data['duration'] == 4) {
												list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
												if (!$ret) {
																echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
																return false;
												}
								}
								if ($data['duration'] == 0) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								
								if ($data['department_id'] == '') {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPARTMENT'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								
								if ($data['department_id'] != '' && $data['department_id'] != 0) {
												$depfilter = ' and ' . VIS_TABLE_PREFIX . 'ticket.department_id=' . $data['department_id'];
								} else {
												$dept_access = '(';
												for ($i = 0; $i < count($departments); $i++) {
																$dept_access .= $departments[$i]['department_id'] . ',';
												}
												$dept_access = substr($dept_access, 0, -1);
												$dept_access .= ')';
												
												$depfilter = ' and ' . VIS_TABLE_PREFIX . 'ticket.department_id in ' . $dept_access;
								}
								
								$currentdate = date('Y-m-d h:i:s');
								if ($data['duration'] == 1) //if select Last 30 days duration
												{
												$monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
												$filter    = 'operation_id<=2 and receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' ' . $depfilter;
								} else if ($data['duration'] == 2) //if select Last 7 days duration
												{
												$weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
												$filter   = 'operation_id<=2 and receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' ' . $depfilter;
								} else if ($data['duration'] == 3) //if select Last 24 hours duration
												{
												$hour   = strtotime("-1 day ") - VIS_TIMEZONE;
												$filter = 'operation_id<=2 and receive_date between  ' . $hour . ' AND ' . strtotime($currentdate) . ' ' . $depfilter;
								} else if ($data['duration'] == 4) //if select Custom duration
												{
												$from   = strtotime($data['from_date']) - VIS_TIMEZONE;
												$to     = strtotime($data['to_date']) - VIS_TIMEZONE;
												$filter = 'operation_id<=2 and receive_date between ' . $from . ' AND ' . $to . ' ' . $depfilter;
								}
				}
				
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
				$object = new class_report($database);
				$filter .= ' and ' . VIS_TABLE_PREFIX . 'ticket.is_spam!=1 ';
				$post_details = $object->get_dept_report($filter, $staff_id, VIS_LOCATION_AREA); 
				
				if ($post_details != -1)
								for ($i = 0; $i < count($post_details); $i++) {
												if (!isset($report[$post_details[$i]['ticket_id']]['start_time'])) {
																$report[$post_details[$i]['ticket_id']]['first_name']   = $post_details[$i]['first_name'];
																$report[$post_details[$i]['ticket_id']]['last_name']   = $post_details[$i]['last_name'];
																$report[$post_details[$i]['ticket_id']]['fname']   = $post_details[$i]['fname'];
																$report[$post_details[$i]['ticket_id']]['lname']   = $post_details[$i]['lname'];
																$report[$post_details[$i]['ticket_id']]['email']   = $post_details[$i]['email'];
																$report[$post_details[$i]['ticket_id']]['group_name']   = $post_details[$i]['group_name'];
																$report[$post_details[$i]['ticket_id']]['ticket_hash']  = $post_details[$i]['ticket_hash'];
																$report[$post_details[$i]['ticket_id']]['subject']      = $post_details[$i]['subject'];
																$report[$post_details[$i]['ticket_id']]['ticket_entry'] = $post_details[$i]['receive_date'];
																$report[$post_details[$i]['ticket_id']]['ticket_id']    = $post_details[$i]['ticket_id'];
																$report[$post_details[$i]['ticket_id']]['status_id']    = $post_details[$i]['status_id'];
																$report[$post_details[$i]['ticket_id']]['count']        = 0;
																$report[$post_details[$i]['ticket_id']]['status_name']  = $post_details[$i]['status_name'];
												}
												
								} else
								$report = -1;
				                
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
					
							include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
								$export = new export();
								$j      = 0;
								foreach ($report as $i) {
												foreach ($i as $key => $val) {
													
																if ($key == 'ticket_entry') {
																			
$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
																} elseif ($key == 'ticket_hash' || $key == 'subject' || $key == 'status_name' || $key == 'first_name' || $key == 'last_name'|| $key == 'fname' || $key == 'lname' || $key == 'email' || $key == 'group_name')
																				$report_exp[$j][$key] = $val;
												}
						if($report_exp[$j]['first_name']=='' && $report_exp[$j]['last_name']==''){
						$report_exp[$j]['first_name'] = 'Nobody';	
						} 						
$report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash'];
$report_exp1[$j]['subject'] = $report_exp[$j]['subject'];
$report_exp1[$j]['staff'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
$report_exp1[$j]['client_name'] = $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
$report_exp1[$j]['email'] = $report_exp[$j]['email'];
$report_exp1[$j]['organization'] = $report_exp[$j]['group_name'];
$report_exp1[$j]['creation_date'] = $report_exp[$j]['ticket_entry'];
$report_exp1[$j]['status'] = $report_exp[$j]['status_name'];
												$j++;
								}
								$report_exp1[$j]['total_ticket_close']=$vis_language['TXTTOTALTICKETSCLOSED'].': '.count($report);
								$completed_date = date('Y-m-d H:i:s');
								$report_exp1['Report_title'] = $report_title;
								$filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
								if ($operation['export_type'] == 1) {
												$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
												if (file_exists('../downloads/' . $filename . '.csv')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 2) {
												$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.html')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 3) {
												
												
												$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.xls')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								}
								
								if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
												$database->database_disconnect();
												$database = new class_database();
												include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
												$object = new class_report($database);
												
								}
								$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
								if (!$result)
												return false;
								$this->email_send_download($operation);
								return true;
				} else {
								include(VIS_PATH_TEMPLATE . 'dept_report.php');
				}
} else
				include(VIS_PATH_TEMPLATE . 'report_form.php');
$database->database_disconnect();
return true;
}
function not_answered_ticket_report($operation) // not answered ticket
{	
global $vis_language;
$report_title = $vis_language['TXTNOTANSWEREDTICKET'];
$export       = 1;
if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
				$database = new class_database('mysql', 1);
else
				$database = new class_database();
include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
$tkt_object  = new class_ticket($database);
$departments = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
								$filter = $operation['filter'];
				else {
								if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
												$data = $operation;
								else
												$data = parse_form_array($operation['vis_action']);
								if (count($data) == 0) {
												include(VIS_PATH_TEMPLATE . 'report_form.php');
												return true;
								}
								if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								if ($data['duration'] == 4) {
												list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
												if (!$ret) {
																echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
																return false;
												}
								}
								if ($data['duration'] == 0 && $data['department_id'] == 0) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								} else if ($data['duration'] != 0 && $data['department_id'] == 0) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								} else if ($data['duration'] == 0 && $data['department_id'] != 0) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								$currentdate = date('Y-m-d h:i:s');
								if ($data['duration'] == 1) //if select Last 30 days duration
												{
												$monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
												$filter    = 'receive_date between  ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and department_id=' . $data['department_id'];
								} else if ($data['duration'] == 2) //if select Last 7 days duration
												{
												$weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
												$filter   = 'receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and department_id=' . $data['department_id'];
								} else if ($data['duration'] == 3) //if select Last 24 hours duration
												{
												$hour   = strtotime("-1 day ") - VIS_TIMEZONE;
												$filter = 'receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and department_id=' . $data['department_id'];
								} else if ($data['duration'] == 4) //if select Custom duration
												{
												$from   = strtotime($data['from_date']) - VIS_TIMEZONE;
												$to     = strtotime($data['to_date']) - VIS_TIMEZONE;
												$filter = 'receive_date between ' . $from . ' AND ' . $to . ' and department_id=' . $data['department_id'];
								}
				}
				
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
				$object = new class_report($database);
				$filter .= ' and ' . VIS_TABLE_PREFIX . 'ticket.is_spam!=1';
				$report = $object->get_not_answered_ticket($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
				
				//$custFilter1=' (location in (3))  and is_enabled=1 '; 
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'setting.php');
				$sett_obj = new class_setting($database);
				
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
								include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
								$export = new export();
								$j=0;
foreach ($report as $i){
foreach ($i as $key => $val){
if($key == 'ticket_hash'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'subject'){
$report_exp[$j][$key] = $val;
}elseif($key == 'contact_number'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'receive_date'){
$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
}
elseif($key == 'first_name'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'last_name'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'email'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'group_name'){
$report_exp[$j][$key] = $val;
}
}
$report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash']; 
$report_exp1[$j]['subject'] = $report_exp[$j]['subject']; 
$report_exp1[$j]['contact_number'] = $report_exp[$j]['contact_number']; 
$report_exp1[$j]['created_on'] = $report_exp[$j]['receive_date'];
$report_exp1[$j]['client_name'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name']; 
$report_exp1[$j]['email'] = $report_exp[$j]['email']; 
$report_exp1[$j]['Organization'] = $report_exp[$j]['group_name'];
												$j++;
								}
								$report_exp1[$j]['total_unanswered_tickets'] =$vis_language['TXTNOTANSWEREDTICKET'].': '.count($report);
								$completed_date = date('Y-m-d H:i:s');
								$filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
								if ($operation['export_type'] == 1) {
												$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
												if (file_exists('../downloads/' . $filename . '.csv')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 2) {
												$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.html')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 3) {
												
												
												$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.xls')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								}
								
								if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
												$database->database_disconnect();
												$database = new class_database();
												include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
												$object = new class_report($database);
												
								}
								$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
								if (!$result)
												return false;
									$this->email_send_download($operation);
								return true;
				} else
								include(VIS_PATH_TEMPLATE . 'not_answered_ticket.php');
} else
				include(VIS_PATH_TEMPLATE . 'report_form.php');
$database->database_disconnect();
return true;
}
function time_tracking_department_report($operation)
{	   
global $vis_language;
$report_title = $vis_language['TXTTRACKINGDEPT'];
if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
				$database = new class_database('mysql', 1);
else
				$database = new class_database();
$export      = 1;
$report_type = 10;
include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
$tkt_object  = new class_ticket($database);
$departments = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1){
								$filter = $operation['filter'];
$data = $operation;
}else {
								if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
$data = $operation;
								else
												$data = parse_form_array($operation['vis_action']);
if (count($data) == 0) {
												include(VIS_PATH_TEMPLATE . 'report_form.php');
												return true;
								}
								if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								if ($data['duration'] == 4) {
												list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
												if (!$ret) {
																echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
																return false;
												}
								}
								if ($data['duration'] == 0 || $data['department_id'] == 0) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								$currentdate = date('Y-m-d h:i:s');
								if ($data['duration'] == 1) //if select Last 30 days duration
												{
												$monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
												$filter    = 'created_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and department_id=' . $data['department_id'];
								} else if ($data['duration'] == 2) //if select Last 7 days duration
												{
												$weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
												$filter   = 'created_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and department_id=' . $data['department_id'];
								} else if ($data['duration'] == 3) //if select Last 24 hours duration
												{
												$hour   = strtotime("-1 day ") - VIS_TIMEZONE;
												$filter = 'created_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and department_id=' . $data['department_id'];
								} else if ($data['duration'] == 4) //if select Custom duration
												{
												$from   = strtotime($data['from_date']) - VIS_TIMEZONE;
												$to     = strtotime($data['to_date']) - VIS_TIMEZONE;
												$filter = 'created_date between ' . $from . ' AND ' . $to . ' and department_id=' . $data['department_id'];
								}
				}
$department_details = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'department.department_id=' . $data['department_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
				$domain_details     = $tkt_object->get_domain(VIS_TABLE_PREFIX . 'domain.domain_id=' . $department_details[0]['domain_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
$currency           = $domain_details[0]['currency'];
				//check amount setting
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'setting.php');
				$object_sett    = new class_setting($database);
				$module_setting = $object_sett->get_settings(' module_id=2 and title="vis_show_time_track_amount" and domain_id=' . $domain_details[0]['domain_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
				if (empty($module_setting) || $module_setting == -1) {
								$module_setting = $object_sett->get_settings(' module_id=2 and title="vis_show_time_track_amount" and domain_id=0', VIS_STAFF_ID, VIS_LOCATION_AREA);
				}
				$amountshowflag = 1;
				if ($module_setting != -1) {
								$amountshowflag = $module_setting[0]['content'];
				}
				//end amount setting check
				
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
				$object          = new class_report($database);
				$total_bill_time = 0;
				$total_work_time = 0;
				$total_amount    = 0;
				$filter .= ' and ' . VIS_TABLE_PREFIX . 'ticket.is_deleted!=1 and ' . VIS_TABLE_PREFIX . 'ticket.is_spam!=1';
				$post_details = $object->get_time_tracking_department_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
				if ($post_details != -1)
								for ($i = 0; $i < count($post_details); $i++) {
												$report[$i]['ticket_hash']     = $post_details[$i]['ticket_hash'];
												$report[$i]['subject']     = $post_details[$i]['subject'];
												$report[$i]['ticket_id']       = $post_details[$i]['ticket_id'];
												$report[$i]['description']     = $post_details[$i]['description'];
												$report[$i]['first_name']     = $post_details[$i]['first_name'];
												$report[$i]['last_name']     = $post_details[$i]['last_name'];
												$report[$i]['email']     = $post_details[$i]['email'];
												$report[$i]['group_name']     = $post_details[$i]['group_name'];
												$report[$i]['bill_time']       = $post_details[$i]['bill_time'];
												$report[$i]['work_time']       = $post_details[$i]['work_time'];
												$report[$i]['amount']          = $post_details[$i]['amount'];
												$report[$i]['created_date']    = $post_details[$i]['created_date'];
												$report[$i]['staff_name']      = $post_details[$i]['staff_name'];
											// $report[$i]['department_name'] = $post_details[$i]['department_name'];
												$total_bill_time += $post_details[$i]['bill_time'];
												$total_work_time += $post_details[$i]['work_time'];
												$total_amount += $post_details[$i]['amount'];
								} else
								$report = -1;
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
								include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
								$export = new export();
								$j      = 0;
								foreach ($report as $i) {
												foreach ($i as $key => $val) {
																if ($key == 'ticket_hash')
																				$report_exp[$j][$key] = $val;
																elseif ($key == 'subject')
																				$report_exp[$j][$key] = $val;
																elseif ($key == 'created_date')
																				$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
																elseif ($key == 'work_time')
																				$report_exp[$j][$key] = $val;
																elseif ($key == 'bill_time')
																				$report_exp[$j][$key] = $val;
																elseif ($key == 'amount')
																				$report_exp[$j][$key] = $val;
elseif ($key == 'description')
																				$report_exp[$j][$key] = $val;
																//elseif ($key == 'department_name')
																			// $report_exp[$j][$key] = $val;
																elseif ($key == 'staff_name')
																				$report_exp[$j][$key] = $val;
																elseif ($key == 'first_name' || $key == 'last_name' || $key == 'email' || $key == 'group_name')
																				$report_exp[$j][$key] = $val;
												}
$report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash'];
$report_exp1[$j]['subject'] = $report_exp[$j]['subject'];
$report_exp1[$j]['description'] = $report_exp[$j]['description'];
$report_exp1[$j]['staff'] = $report_exp[$j]['staff_name'];
$report_exp1[$j]['client_name'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
$report_exp1[$j]['email'] = $report_exp[$j]['email'];
$report_exp1[$j]['organization'] = $report_exp[$j]['group_name'];
$report_exp1[$j]['created_on'] = $report_exp[$j]['created_date'];
$report_exp1[$j]['bill_time'.'('.$vis_language['TXTMIN'].')'] = $report_exp[$j]['bill_time'];
$report_exp1[$j]['work_time'.'('.$vis_language['TXTMIN'].')'] = $report_exp[$j]['work_time'];
//$report_exp1[$j]['department'] = $report_exp[$j]['department_name'];
$report_exp1[$j]['amount'.' ('.$currency.')'] = $report_exp[$j]['amount'];
												$j++;
								}
$report_exp1[$j]['ticket_hash'] = '';
$report_exp1[$j]['subject'] = '';
$report_exp1[$j]['description'] = '';
$report_exp1[$j]['staff'] = '';
								$report_exp1[$j]['created_on'] = '';
								$report_exp1[$j]['bill_time']   = $vis_language['TXTTOTALBILLTIME'].":".$total_bill_time . ' ' . $vis_language['TXTMIN'];
								$report_exp1[$j]['work_time']   = $vis_language['TXTTOTALWORKTIME'].":".$total_work_time . ' ' . $vis_language['TXTMIN'];                
								$report_exp1[$j]['total_amount']   = $vis_language['TXTTOTALAMOUNT'].":".$total_amount . ' ' . $currency;
								
								$completed_date = date('Y-m-d H:i:s');
								$report_exp1['Report_title'] = $report_title;
								
								$filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
								if ($operation['export_type'] == 1) {
												$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
												if (file_exists('../downloads/' . $filename . '.csv')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 2) {
												$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.html')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 3) {
												
												
												$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.xls')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								}
								if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
												$database->database_disconnect();
												$database = new class_database();
												include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
												$object = new class_report($database);
								}
								$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
								if (!$result)
												return false;
								$this->email_send_download($operation);
								return true;
				} else
								include(VIS_PATH_TEMPLATE . 'time_tracking_report.php');
} else
				include(VIS_PATH_TEMPLATE . 'report_form.php');
$database->database_disconnect();
return true;
}
function ticket_activity_report($operation)
{	
global $vis_language;
$report_title = $vis_language['TXTTICKETACTIVITY'];
$export       = 1;
if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
				$database = new class_database('mysql', 1);
else
			$database = new class_database();
include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
$object=new class_ticket($database);
$ticket=$object->get_ticket(  VIS_STAFF_ID, VIS_LOCATION_AREA);
$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
								$filter = $operation['filter'];
				else {
								if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
												$data = $operation;
								else
												$data = parse_form_array($operation['vis_action']);
								if (count($data) == 0) {
												include(VIS_PATH_TEMPLATE . 'report_form.php');
												return true;
								}
								if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								/***** date difference ****/
								if ($data['duration'] == 4) {
												list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
												if (!$ret) {
																echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
																return false;
												}
								}
									if ($data['duration'] == 0 && $data['vis_new_ticket_list_client1'] == "") {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								} else if ($data['duration'] != 0 && $data['vis_new_ticket_list_client1'] == "") {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECT'] ." ".$vis_language['TXTTICKETHASH']. '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								} else if ($data['duration'] == 0 && $data['vis_new_ticket_list_client1'] != "") {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								} 
								
								$currentdate = date('Y-m-d h:i:s');
								if ($data['duration'] == 1) //if select Last 30 days duration
												{
												$monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
														$filter    = 'avis_auditlog.entry_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and avis_auditlog.ticket_hash='.'"' .$data['vis_new_ticket_list_client1'].'"';
								} else if ($data['duration'] == 2) //if select Last 7 days duration
												{
												$weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
													$filter    = 'avis_auditlog.entry_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and avis_auditlog.ticket_hash='.'"' .$data['vis_new_ticket_list_client1'].'"';
								}
								
								else if ($data['duration'] == 3) //if select Last 24 hours duration
												{
												$hour = strtotime("-1 day ") - VIS_TIMEZONE;
												
												$filter    = 'avis_auditlog.entry_date  between ' . $hour . ' AND ' . strtotime($currentdate) . ' and avis_auditlog.ticket_hash='.'"' .$data['vis_new_ticket_list_client1'].'"';
								} else if ($data['duration'] == 4) //if select Custom duration
												{
												$from   = strtotime($data['from_date']) - VIS_TIMEZONE;
												$to     = strtotime($data['to_date']) - VIS_TIMEZONE;
													$filter = 'avis_auditlog.entry_date  between ' . $from . ' AND ' . $to . ' and avis_auditlog.ticket_hash='.'"' .$data['vis_new_ticket_list_client1'].'"';
								}
					$filter .= " and avis_auditlog.access_id!=1 and avis_auditlog.access_id!=2 and avis_auditlog.access_id!=3";
				}
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
				
				$object = new class_report($database);
			
$audit_log	= $object->ticket_activity($filter,$filter1,VIS_STAFF_ID, VIS_LOCATION_AREA); 
if($audit_log!=-1) {
$close_time=$audit_log[0]['closed_time'];
$creation_time=$audit_log [0]['creation_date'];
if( $close_time!=0){
$total_time=$audit_log[0]['closed_time']-$audit_log[0]['creation_date'];
}else{
$total_time="Not applicable";
} 
$avgTimeCount =count_remain_time($total_time ) ;
}
$audit_count=($audit_log==-1)?0:count($audit_log);
												for($i=0 ; $i<$audit_count ; $i++)
												{
switch($audit_log[$i]['operation_id']) {
case '0':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGNEWTKTCRT'];
	break;
	
case '1':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTKTRPLD'];
	break;
case '2':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTSTATUSCHANGEDFROM'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
	
case '3':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTPRIORITYCHANGEDFROM'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
	
case '4':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTDEPARTMENTCHANGEDFROM'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
	
case '5':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTTICKETTYPECHANGEDFROM'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
	
case '6':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTFLAGCHANGEDFROM'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
	
case '7':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');  
	$object=new class_setting($database);
	$staff_new_info=$object->get_staff(VIS_TABLE_PREFIX.'staff.staff_id='.$audit_log[$i]['new_info'], '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA,1);
	$new_info =  $staff_new_info[1][0]['first_name'].' '.$staff_new_info[1][0]['last_name'];
	if($audit_log[$i]['new_info']!=0 && $staff_new_info[1]!=-1)
																																$audit_log[$i]['audit_log_operation'] = $vis_language['MSGTKTASSIG'].' '.$vis_language['TXTTO'].' '. $new_info;
																												elseif($audit_log[$i]['new_info']==0 && $audit_log[$i]['old_info']>0){ 
																																$staff_old_info=$object->get_staff(VIS_TABLE_PREFIX.'staff.staff_id='.$audit_log[$i]['old_info'], '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA,1);
																																$old_info =  $staff_old_info[1][0]['first_name'].' '.$staff_old_info[1][0]['last_name'];
																																$audit_log[$i]['audit_log_operation'] = $vis_language['TXTTICKETUNASSIGNEDFROM'].' '. $old_info;
																												}
	else
																																$audit_log[$i]['audit_log_operation'] = $vis_language['TXTTICKETASSIGNEDTONOBODY'];
	break;
	
case '8':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');  
	$object=new class_setting($database);
	$staff_details=$object->get_staff(VIS_TABLE_PREFIX.'staff.staff_id='.$audit_log[$i]['operator_id'], '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
	$staff_name =  $staff_details[1][0]['first_name'].' '.$staff_details[1][0]['last_name'];
	if($audit_log[$i]['old_info']!=0 )
		$audit_log[$i]['audit_log_operation']=$vis_language['MSGTKTTRSH'];
	elseif($audit_log[$i]['old_info']==0 )
		$audit_log[$i]['audit_log_operation']=$vis_language['TXTTKTMOVEINBOCFROMTRASH'];
	break;
	
case '9':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');  
	$object=new class_setting($database);
	$staff_details=$object->get_staff(VIS_TABLE_PREFIX.'staff.staff_id='.$audit_log[$i]['operator_id'], '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
	$staff_name =  $staff_details[1][0]['first_name'].' '.$staff_details[1][0]['last_name'];
	$audit_log[$i]['audit_log_operation']=$audit_log[$i+1]['old_info'] .' deleted';
	break;
case '10':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
	$object=new class_ticket($database);
	$audit_log[$i]['audit_log_operation']=$audit_log[$i]['new_info'] .' merged to '.$audit_log[$i]['old_info'];
	break;
	
case '11':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTKTFRWD'];
	break;
	
case '12':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTKTDELETED'];
	break;
	
case '13':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');  
	$object=new class_setting($database);
	$sla_old_info=$object->get_sla(VIS_TABLE_PREFIX.'sla.sla_id='.$audit_log[$i]['old_info'], '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
	$sla_new_info=$object->get_sla(VIS_TABLE_PREFIX.'sla.sla_id='.$audit_log[$i]['new_info'], '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
				$old_info =  $sla_old_info[1][0]['sla_name'];
	$new_info =  $sla_new_info[1][0]['sla_name'];
	if($audit_log[$i]['new_info']!=0 && $audit_log[$i]['old_info']!=0)
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTSLACHANGEDFROM'].' '. $old_info. ' to '. $new_info;
	else if($audit_log[$i]['old_info']==0 && $audit_log[$i]['new_info']!=0)
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTSLASETTO'].' '. $new_info;
	else if($audit_log[$i]['new_info']==0)
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTAPPLIEDCUSTOMSLAPLAN'];
	break;
	
case '14':
	if($audit_log[$i]['new_info']!=0 && $audit_log[$i]['old_info']==0)
		$audit_log[$i]['audit_log_operation']=$vis_language['MSGSPAMMRK'];
	elseif($audit_log[$i]['new_info']==0 && $audit_log[$i]['old_info']!=0)
		$audit_log[$i]['audit_log_operation']=$vis_language['TXTNOTSPAMMARKED'];
	break;
	
case '15':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGUNSPAM'];
	break;
case '16':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTNEWTASKATTACHED'].' '. $audit_log[$i]['old_info'];
	break;
case '17':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTDETACHEDATTACHEDTASK'].' '. $audit_log[$i]['old_info'];
	break;
case '18':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTNEWCIATTACHED'].' '. $audit_log[$i]['old_info'];
	break;
case '19':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTDETACHEDATTACHEDCI'].' '. $audit_log[$i]['old_info'];
	break;
case '20':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTNEWPROBLEMATTACHED'].' '. $audit_log[$i]['old_info'];
	break;
case '21':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTDETACHEDATTACHEDPROBLEM'].' '. $audit_log[$i]['old_info'];
	break;
case '22':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTNEWCHANGEATTACHED'].' '. $audit_log[$i]['old_info'];
	break;
case '23':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTDETACHEDATTACHEDCHANGE'].' '. $audit_log[$i]['old_info'];
	break;	
case '24':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
	$object=new class_ticket($database);
	$audit_log[$i]['audit_log_operation']=$audit_log[$i]['new_info'] .' '.$vis_language['TXTLINKEDTO'].' '.$audit_log[$i]['old_info'];
	break;	
case '25':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
	$object=new class_ticket($database);
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETPOSTDELETE'];
	break;	
case '26':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
	$object=new class_ticket($database);
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTTICKETPOSTSPLITWITH'].' '.$audit_log[$i]['new_info'];
	break;	
case '27':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTCHANNELCHANGEDFROM'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
case '28':
	$audit_log[$i]['audit_log_operation']=$audit_log[$i]['old_info'] .' '.$vis_language['TXTUNLINKEDFROM'].' '.$audit_log[$i]['new_info'];
	break;	
case '29':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTSET'].' '.$audit_log[$i]['new_info'] .' '.$vis_language['TXTLABEL'];
	break;	
case '30':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTPINNED'].' '.$audit_log[$i]['old_info'];
	break;
case '31':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTREMOVEDLABEL'].' '.$audit_log[$i]['new_info'];
	break;
case '32':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETPROPERTYUPDATED'];
	break;
case '33':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETNOTEADDED'];
	break;
case '34':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETNOTEDELETED'];
	break;
case '35':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETCOMMENTADDED'];
	break;
case '36':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETCOMMENTDELETED'];
	break;
case '37':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETPOSTUPDATED'];
	break;
case '38':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGADDEDNEWRECIPIENT'].' '.$audit_log[$i]['new_info'];
	break;
case '39':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGADDEDNEWPEOPLE'].' '.$audit_log[$i]['new_info'];
	break;
case '40':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGREMOVEDRECIPIENT'].' '.$audit_log[$i]['new_info'];
	break;
case '41':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGREMOVEDPEOPLE'].' '.$audit_log[$i]['new_info'];
	break;
case '42':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGADDEDNEWTAG'].' '.$audit_log[$i]['new_info'];
	break;
case '43':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGREMOVEDTAG'].' '.$audit_log[$i]['new_info'];
	break;
case '44':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGNEWSCHEDULETKTCRT'];
	break;
case '45':
	$audit_log[$i]['audit_log_operation']=($audit_log[$i]['old_info'] == '')?$vis_language['MSGTICKETLEVELCHANGED'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info']:$vis_language['MSGTICKETLEVELCHANGED'].' from '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
case '46':
	$audit_log[$i]['audit_log_operation']=($audit_log[$i]['old_info'] == '')?$vis_language['MSGTICKETCATEGORYCHANGED'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info']:$vis_language['MSGTICKETCATEGORYCHANGED'].' from '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
case '47':
	$audit_log[$i]['audit_log_operation']=($audit_log[$i]['old_info'] == '')?$vis_language['MSGTICKETSUBCATEGORYCHANGED'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info']:$vis_language['MSGTICKETSUBCATEGORYCHANGED'].' from '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
case '48':
	$audit_log[$i]['audit_log_operation']=($audit_log[$i]['old_info'] == '')?$vis_language['MSGTICKETITEMCHANGED'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info']:$vis_language['MSGTICKETITEMCHANGED'].' from '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
case '49':
	$audit_log[$i]['audit_log_operation']=($audit_log[$i]['old_info'] == '')?$vis_language['MSGITILLOCATIONCHANGED'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info']:$vis_language['MSGITILLOCATIONCHANGED'].' from '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
case '50':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTSLAPAUSED'];
	break;
case '51':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTSLARESUMED'];
	break;
	case '52':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTREADTICKETFORSTAFF'];
	break;
	case '53':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTSENDTICKETAPPROVAL'].' to '.$audit_log[$i]['old_info'];
	break;
	case '54':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTCHANGETICKETAPPROVALSTATUS'].' by '.$audit_log[$i]['old_info'];
	break;
	case '55':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTSENDTICKETAPPROVALREMINDER'].' to '.$audit_log[$i]['old_info'];
	break;
}
}
				
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
								
								
								include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
								$export = new export();
								$operators=array(1=>$vis_language['TXTCLIENT'], 2=>$vis_language['TXTSTAFF'], 3=>$vis_language['TXTSTAFF']);
								$j = 0;
						foreach ($audit_log as $i)
{
												foreach ($i as $key => $val) {
																if ($key == 'ticket_hash') {
																				$report_exp[$j][$key] = $val; 
																}  elseif ($key == 'subject') {
																				$report_exp[$j][$key] = $val;//preg_replace('/\\\\/','',$val);
																}  elseif ($key == 'audit_log_operation') {
																				$report_exp[$j][$key] = $val;
																}  elseif ($key == 'operator' ) {
																				
$report_exp[$j][$key]=($i['operator_id']==0?$vis_language['TXTSYSTEM']:($operators[$i['operator']].' ('.$i['operators'].')'));
																} elseif ($key == 'department_name') {
																				$report_exp[$j][$key] = $val;
																}
           elseif ($key == 'fname' || $key == 'lname' || $key == 'email' || $key == 'group_name') {
				$report_exp[$j][$key] = $val;
		}																
} 
$report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash']; 
$report_exp1[$j]['subject'] = $report_exp[$j]['subject'];
$report_exp1[$j]['client_name'] = $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
$report_exp1[$j]['email'] = $report_exp[$j]['email'];
$report_exp1[$j]['organization'] = $report_exp[$j]['group_name'];
$report_exp1[$j]['operation'] = $report_exp[$j]['audit_log_operation']; 
$report_exp1[$j]['operator'] = $report_exp[$j]['operator'];
$report_exp1[$j]['department'] = $report_exp[$j]['department_name'];
$j++;
								} 
$report_exp1[$j]['total']=$vis_language['TXTTOTAL'].' :'. $avgTimeCount;
$report_exp1[$j]['creation_date'] = $vis_language['TXTCREATIONDATE'].':'.format_datetime_rpt($creation_time,$staff_id);
$report_exp1[$j]['closed_time'] =$vis_language['TXTCLOSETICKETDATE'].':'.format_datetime_rpt($close_time,$staff_id);				
$completed_date = date('Y-m-d H:i:s');
$report_exp1['Report_title'] = $report_title;
							 	$filename = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
								if ($operation['export_type'] == 1) {
												$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
												if (file_exists('../downloads/' . $filename . '.csv')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 2) {
												$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.html')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 3) {
												
												
												$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.xls')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								}
								
								if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
												$database->database_disconnect();
												$database = new class_database();
												include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
												$object = new class_report($database);
												
								}else {
									include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
												$object = new class_report($database);
								}
								 
								 
								  $result = $object->update_download_list($filename, $completed_date, $operation['export_id']); 
								if (!$result)
												return false;
								$this->email_send_download($operation);
								return true;
				}
				
				else
								include(VIS_PATH_TEMPLATE . 'ticket_activity.php');
				
				
				
} else
				include(VIS_PATH_TEMPLATE . 'report_form.php');
$database->database_disconnect();
return true;
}
function ticket_linking_report($operation) //ticket linking relation report
{	
global $vis_language;
$report_title =$vis_language['TXTTICKETLINKREPORT'];
if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
				$database = new class_database('mysql', 1);
else
				$database = new class_database();
$export = 1;
include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
$tkt_object = new class_ticket($database);
$domain     = $tkt_object->get_domain(VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
// list($clinfo, $clients) = $cli_object->get_client(VIS_TABLE_PREFIX . 'domain.is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
								$filter = $operation['filter'];
				else {
								if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
												$data = $operation;
								else
												$data = parse_form_array($operation['vis_action']);
								if (count($data) == 0) {
												include(VIS_PATH_TEMPLATE . 'report_form.php');
												return true;
								}
								if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								if ($data['duration'] == 4) {
												list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
												if (!$ret) {
																echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
																return false;
												}
								}
								if ($data['duration'] == 0 &&  $data['domain_id'] == 0) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								} else if ($data['duration'] != 0 && $data['domain_id'] == 0) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDOMAINFORRES'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								} else if ($data['duration'] == 0 && $data['domain_id'] != 0) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								
								$currentdate = date('Y-m-d h:i:s');
								if ($data['duration'] == 1) //if select Last 30 days duration
												{
												$monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
												$filter    = ' receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and link_type=' .$data['vis_link_type'] . ' and ' . VIS_TABLE_PREFIX . 'ticket.domain_id=' . $data['domain_id'];
								} else if ($data['duration'] == 2) //if select Last 7 days duration
												{
												$weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
												$filter   = 'receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and link_type=' .$data['vis_link_type'] . ' and ' . VIS_TABLE_PREFIX . 'ticket.domain_id=' . $data['domain_id'];
								} else if ($data['duration'] == 3) //if select Last 24 hours duration
												{
												$hour   = strtotime("-1 day ") - VIS_TIMEZONE;
												$filter = 'receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and link_type=' .$data['vis_link_type'] . ' and ' . VIS_TABLE_PREFIX . 'ticket.domain_id=' . $data['domain_id'];
								} else if ($data['duration'] == 4) //if select Custom duration
												{
												$from   = strtotime($data['from_date']) - VIS_TIMEZONE;
												$to     = strtotime($data['to_date']) - VIS_TIMEZONE;
												$filter = ' receive_date between ' . $from . ' AND ' . $to . ' and link_type=' .$data['vis_link_type'] . ' and ' . VIS_TABLE_PREFIX . 'ticket.domain_id=' . $data['domain_id'];
								}
				}
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
				$object = new class_report($database);
				$filter .= ' and ' . VIS_TABLE_PREFIX . 'ticket.is_spam!=1 ';
				$post_details    = $object->get_ticket_linking_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'setting.php');
				$sett_obj = new class_setting($database);
	
				$link=array(1=>$vis_language['TXTRELATESTO'],2=>$vis_language['TXTISRELATEDTO'],3=> $vis_language['TXTISAPARENTOF'],4=>$vis_language['TXTISACHILDOF'],5=>$vis_language['TXTDEPENDSON'],6=>$vis_language['TXTISDEPENDEDON'],7=>$vis_language['TXTDUPLICATES'],8=>$vis_language['TXTISADUPLICATEDBY']);
				
			$link_name=$link[$data['vis_link_type']];
				
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
								include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
								$export = new export();
								$j=0;
foreach ($post_details  as $i){   
foreach ($i as $key => $val){
if($key == 'ticket_hash'){ 
$report_exp[$j][$key] = $val;
}
elseif($key == 'subject'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'receive_date'){
$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
}
elseif($key == 'first_name'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'last_name'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'email'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'group_name'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'link_hash_tckt'){
$report_exp[$j][$key] = $val;
}
elseif($key=='link_type')
{
$report_exp[$j][$key] = $link[$val];
$linktype=$report_exp[$j][$key] ;
}
} 
$report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash']; 
$report_exp1[$j]['subject'] = $report_exp[$j]['subject']; 
$report_exp1[$j]['client_name'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name']; 
$report_exp1[$j]['email'] = $report_exp[$j]['email']; 
$report_exp1[$j]['organization'] = $report_exp[$j]['group_name'];
$report_exp1[$j]['created_on'] = $report_exp[$j]['receive_date'];
$report_exp1[$j][$linktype] = $report_exp[$j]['link_hash_tckt']; 
												$j++; 
								}
						
								$completed_date = date('Y-m-d H:i:s');
								$filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
								if ($operation['export_type'] == 1) {
												$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
												if (file_exists('../downloads/' . $filename . '.csv')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 2) {
												$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.html')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 3) {
												
												
												$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.xls')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								}
								
								if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
												$database->database_disconnect();
												$database = new class_database();
												include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
												$object = new class_report($database);
												
								}
								$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
								if (!$result)
												return false;
									$this->email_send_download($operation);
								return true;
				} else
								include(VIS_PATH_TEMPLATE . 'ticket_linking_report.php');
} else
				include(VIS_PATH_TEMPLATE . 'report_form.php');
$database->database_disconnect();
return true;
}
function merged_tickets_report($operation)
{	
global $vis_language;
$report_title = $vis_language['TXTMERGEDTICKETS'];
$export       = 1;
if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
				$database = new class_database('mysql', 1);
else
				$database = new class_database();
include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
$tkt_object = new class_ticket($database);
$departments   = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'staff.php');
$object_staff  = new class_staff($database);
$staff_filter  = "staff_id =" . VIS_STAFF_ID;
$staff_details = $object_staff->get_staff($staff_filter, $userid, $user);
$dept_ids      = '';
for ($d = 0; $d < count($departments); $d++) {
				$dept_ids .= $departments[$d]['department_id'] . ',';
}
if ($dept_ids != '') {
				$dept_ids = substr($dept_ids, 0, -1);
}
if ($data['department_id'] == 0) {
				$dept_id = ($staff_details[0]['department_access'] == '' || $staff_details[0]['department_access'] == 0) ? $dept_ids : $staff_details[0]['department_access'];
}
$staff_list    = $object_staff->get_staff('is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA, '');
$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
								$filter = $operation['filter'];
				else {
								if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
												$data = $operation;
								else
												$data = parse_form_array($operation['vis_action']);
								if (count($data) == 0) {
												include(VIS_PATH_TEMPLATE . 'report_form.php');
												return true;
								}
								
								if ($data['staff_id'] != 0) {
												$filter = 'operator_id=' . $data['staff_id'];
								} else {
												$filter='a.department_id in'.'('.$dept_id.')' ;
								}
				}
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
				$object = new class_report($database);
				$report = $object->get_merged_tickets_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
								include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
								$export         = new export();
$j=0;
foreach ($report as $i){
foreach ($i as $key => $val){
if($key == 'ticket_hash'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'subject'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'first_name'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'last_name'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'receive_date'){
$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
}
elseif($key == 'old_info'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'fname' || $key == 'lname' || $key == 'email' || $key == 'group_name'){
$report_exp[$j][$key] = $val;
}
}
$report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash']; 
$report_exp1[$j]['subject'] = $report_exp[$j]['subject'];
$report_exp1[$j]['client_name'] = $report_exp[$j]['fname'].' '.$report_exp[$j]['lname']; 
$report_exp1[$j]['email'] = $report_exp[$j]['email'];
$report_exp1[$j]['organization'] = $report_exp[$j]['group_name'];
$report_exp1[$j]['staff_name'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name']; 
$report_exp1[$j]['merged_date'] = $report_exp[$j]['receive_date']; 
$report_exp1[$j]['merge_ticket'] = $report_exp[$j]['old_info']; 
												$j++;
								}					
								$completed_date = date('Y-m-d H:i:s');
								$report_exp1['Report_title'] = $report_title;
				
								$filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
								if ($operation['export_type'] == 1) {
												$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
												if (file_exists('../downloads/' . $filename . '.csv')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 2) {
												$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.html')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 3) {
												
												
												$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.xls')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								}
								if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
												$database->database_disconnect();
												$database = new class_database();
												include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
												$object = new class_report($database);
												
								}
								$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
								if (!$result)
												return false;
								$this->email_send_download($operation);
								return true;
				} else
								include(VIS_PATH_TEMPLATE . 'merged_tickets_report.php');
} else
				include(VIS_PATH_TEMPLATE . 'report_form.php');
$database->database_disconnect();
return true;
}
function deleted_tickets_report($operation)
{	
global $vis_language;
$report_title = $vis_language['TXTDELETEDTICKETS'];
$export       = 1;
if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
				$database = new class_database('mysql', 1);
else
				$database = new class_database();
include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
$tkt_object = new class_ticket($database);
include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'staff.php');
$object_staff  = new class_staff($database);
$departments   = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
$staff_filter  = "staff_id =" . VIS_STAFF_ID;
$staff_details = $object_staff->get_staff($staff_filter, $userid, $user);
// print_r($staff_details[0]['department_access']);
$dept_ids      = '';
for ($d = 0; $d < count($departments); $d++) {
				$dept_ids .= $departments[$d]['department_id'] . ',';
}
if ($dept_ids != '') {
				$dept_ids = substr($dept_ids, 0, -1);
}
if ($data['department_id'] == 0) {
				$dept_id = ($staff_details[0]['department_access'] == '' || $staff_details[0]['department_access'] == 0) ? $dept_ids : $staff_details[0]['department_access'];
}
//print_r($staff_details);
$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
								$filter = $operation['filter'];
				else {
								if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
												$data = $operation;
								else
												$data = parse_form_array($operation['vis_action']);
								if (count($data) == 0) {
												include(VIS_PATH_TEMPLATE . 'report_form.php');
												return true;
								}
								
								if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								if ($data['duration'] == 4) {
												list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
												if (!$ret) {
																echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
																return false;
												}
								}
								if ($data['duration'] == 0) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								
								if($data['department_id']==''){
									echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPARTMENT'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
								}
								
								$currentdate = date('Y-m-d h:i:s');
								if ($data['duration'] == 1) //if select Last 30 days duration
												{
												$monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
												if ($data['department_id'] != 0) {
																$filter = 'entry_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and a.department_id=' . $data['department_id'];
												} else {
																$filter =  'entry_date between  '.$monthDate. ' AND '.strtotime($currentdate).' and a.department_id in ('.$dept_id.')';
																//$filter = 'entry_date between  ' . $monthDate . ' AND ' . strtotime($currentdate);
												}
								} else if ($data['duration'] == 2) //if select Last 7 days duration
												{
												$weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
												if ($data['department_id'] != 0) {
																$filter = 'entry_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and a.department_id=' . $data['department_id'];
												} else {
																$filter   =  'entry_date between '.$weekDate.' AND '.strtotime($currentdate).' and a.department_id in ('.$dept_id.')'; 
																//$filter = 'entry_date between ' . $weekDate . ' AND ' . strtotime($currentdate);
												}
								} else if ($data['duration'] == 3) //if select Last 24 hours duration
												{
												$hour = strtotime("-1 day ") - VIS_TIMEZONE;
												if ($data['department_id'] != 0) {
																$filter = 'entry_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and a.department_id=' . $data['department_id'];
												} else {
																	$filter =  'entry_date between '.$hour.' AND '.strtotime($currentdate).' and a.department_id in ('.$dept_id.')';
																//$filter = 'entry_date between ' . $hour . ' AND ' . strtotime($currentdate);
												}
								} else if ($data['duration'] == 4) //if select Custom duration
												{
												$from = strtotime($data['from_date']) - VIS_TIMEZONE;
												$to   = strtotime($data['to_date']) - VIS_TIMEZONE;
												if ($data['department_id'] != 0) {
																$filter = 'entry_date between ' . $from . ' AND ' . $to . ' and a.department_id=' . $data['department_id'];
												} else {
																$filter = 'entry_date between '.$from.' AND '.$to.' and a.department_id in ('.$dept_id.')';
															// $filter = 'entry_date between ' . $from . ' AND ' . $to;
												}
								}
				}
				
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
				$object      = new class_report($database);
				$report      = $object->get_deleted_tickets_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
				$custFilter1 = ' (location in (3))  and is_enabled=1 ';
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'setting.php');
				$sett_obj     = new class_setting($database);
				$custom_group = $sett_obj->get_custom_group_list($custFilter1, VIS_STAFF_ID, VIS_LOCATION_AREA);
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
								include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
								$export         = new export();
$j=0;
foreach ($report as $i){
foreach ($i as $key => $val){
if($key == 'ticket_hash'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'subject'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'first_name'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'last_name'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'receive_date'){
$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
}
elseif($key == 'department_name'){
$report_exp[$j][$key] = $val;
}
}
$report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash']; 
$report_exp1[$j]['subject'] = $report_exp[$j]['subject']; 
$report_exp1[$j]['staff_name'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name']; 
$report_exp1[$j]['deleted_date'] = $report_exp[$j]['receive_date']; 
$report_exp1[$j]['department'] = $report_exp[$j]['department_name']; 
												$j++;
								}
								$completed_date = date('Y-m-d H:i:s');
								$report_exp1['Report_title'] = $report_title;
								
								$filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
								if ($operation['export_type'] == 1) {
												$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
												if (file_exists('../downloads/' . $filename . '.csv')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 2) {
												$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.html')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 3) {
												
												
												$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.xls')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								}
								
if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
												$database->database_disconnect();
												$database = new class_database();
												include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
												$object = new class_report($database);
												
								}
								
								$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
								if (!$result)
												return false;
								$this->email_send_download($operation);
								return true;
				} else
								include(VIS_PATH_TEMPLATE . 'tickets_deleted_by_deptwise.php');
} else
				include(VIS_PATH_TEMPLATE . 'report_form.php');
$database->database_disconnect();
return true;
}
function customfield_data_report($operation)
{	
global $vis_language;
$report_title = $vis_language['TXTCSTFLDRPT'];
$export       = 1;
if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
				$database = new class_database('mysql', 1);
else
				$database = new class_database();
include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
$tkt_object = new class_ticket($database);
include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'staff.php');
$object_staff = new class_staff($database);
include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'setting.php');
$object_setting = new class_setting($database);
$filter         = 'location in(1,11)';
$custom_group   = $object_setting->get_custom_group_list($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
for ($i = 0; $i < count($custom_group); $i++) {
				$filter = ' ' . VIS_TABLE_PREFIX . 'customfield.customgroup_id=' . $custom_group[$i]['customgroup_id'] . ' and ' . VIS_TABLE_PREFIX . 'customfield.is_enabled=1 ';
				list($customfield_info, $customfield_details) = $object_setting->get_customfield(0, $filter, '', 0, 0, VIS_STAFF_ID, VIS_LOCATION_AREA);
				$customfield_array[] = $customfield_details;
}
//print_r($customfield_array);
$staff_filter  = "staff_id =" . VIS_STAFF_ID;
$staff_details = $object_staff->get_staff($staff_filter, $userid, $user);
$departments   = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
$dept_ids    = '';
for ($d = 0; $d < count($departments); $d++) {
				$dept_ids .= $departments[$d]['department_id'] . ',';
}
if ($dept_ids != '') {
				$dept_ids = substr($dept_ids, 0, -1);
}
$dept_id  = ($staff_details[0]['department_access'] == '' || $staff_details[0]['department_access'] == 0) ? $dept_ids : $staff_details[0]['department_access'];
$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
								$filter = $operation['filter'];
				else {
								if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
												$data = $operation;
								else
												$data = parse_form_array($operation['vis_action']);
								if (count($data) == 0) {
												include(VIS_PATH_TEMPLATE . 'report_form.php');
												return true;
								}
								if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								if ($data['duration'] == 4) {
												list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
												if (!$ret) {
																echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
																return false;
												}
								}
								if ($data['duration'] == 0) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
if ($data['field_type'] == "") {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								
								$currentdate = date('Y-m-d h:i:s');
								if ($data['duration'] == 1) //if select Last 30 days duration
												{
												$monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
												if ($data['department_id'] != 0 && $data['field_type'] != 0) {
																$filter = 't.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and t.department_id=' . $data['department_id'] . ' and cf.field_group=' . $data['field_type'].' and cf.is_enabled = 1 ';
												} else if ($data['department_id'] == 0 && $data['field_type'] != 0) {
																$filter = 't.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and t.department_id in ' . '(' . $dept_id . ')' . ' and cf.field_group=' . $data['field_type'].' and cf.is_enabled = 1 ';
												} else if ($data['department_id'] != 0 && $data['field_type'] == 0) {
																$filter = 't.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and t.department_id=' . $data['department_id'].' and cf.is_enabled=1 ';
												} else {
																$filter = 't.receive_date between  ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and t.department_id in (' . $dept_id . ')'.' and cf.is_enabled = 1 ';
												}
								} else if ($data['duration'] == 2) //if select Last 7 days duration
												{
												$weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
												if ($data['department_id'] != 0 && $data['field_type'] != 0) {
																$filter = 't.receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and t.department_id=' . $data['department_id'] . ' and cf.field_group=' . $data['field_type'].' and cf.is_enabled = 1 ';
												} else if ($data['department_id'] == 0 && $data['field_type'] != 0) {
																$filter = 't.receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and t.department_id in ' . '(' . $dept_id . ')' . ' and cf.field_group=' . $data['field_type'].' and cf.is_enabled = 1 ';
												} else if ($data['department_id'] != 0 && $data['field_type'] == 0) {
																$filter = 't.receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and t.department_id=' . $data['department_id'].' and cf.is_enabled = 1 ';
												} else {
																$filter = 't.receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and t.department_id in (' . $dept_id . ')'.' and cf.is_enabled = 1 ';
												}
								} else if ($data['duration'] == 3) //if select Last 24 hours duration
												{
												$hour = strtotime("-1 day ") - VIS_TIMEZONE;
												if ($data['department_id'] != 0 && $data['field_type'] != 0) {
																$filter = 't.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and t.department_id=' . $data['department_id'] . ' and cf.field_group=' . $data['field_type'].' and cf.is_enabled = 1 ';
												} else if ($data['department_id'] == 0 && $data['field_type'] != 0) {
																$filter = 't.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and t.department_id in ' . '(' . $dept_id . ')' . ' and cf.field_group=' . $data['field_type'].' and cf.is_enabled = 1 ';
												} else if ($data['department_id'] != 0 && $data['field_type'] == 0) {
																$filter = 't.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and t.department_id=' . $data['department_id'].' and cf.is_enabled = 1 ';
												} else {
																$filter = 't.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and t.department_id in (' . $dept_id . ')'.' and cf.is_enabled = 1 ';
												}
								} else if ($data['duration'] == 4) //if select Custom duration
												{
												$from = strtotime($data['from_date']) - VIS_TIMEZONE;
												$to   = strtotime($data['to_date']) - VIS_TIMEZONE;
												if ($data['department_id'] != 0 && $data['field_type'] != 0) {
																$filter = 't.receive_date between ' . $from . ' AND ' . $to . ' and t.department_id=' . $data['department_id'] . ' and cf.field_group=' . $data['field_type'].' and cf.is_enabled = 1 ';
												} else if ($data['department_id'] == 0 && $data['field_type'] != 0) {
																$filter = 't.receive_date between ' . $from . ' AND ' . $to . ' and t.department_id in ' . '(' . $dept_id . ')' . ' and cf.field_group=' . $data['field_type'].' and cf.is_enabled = 1 ';
												} else if ($data['department_id'] != 0 && $data['field_type'] == 0) {
																$filter = 't.receive_date between ' . $from . ' AND ' . $to . ' and t.department_id=' . $data['department_id'].' and cf.is_enabled = 1 ';
												}
												
												else {
																$filter = 't.receive_date between ' . $from . ' AND ' . $to . ' and t.department_id in (' . $dept_id . ')'.' and cf.is_enabled = 1 ';
												}
								}
				}
				
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
				$object      = new class_report($database);
				$report      = $object->get_customfield_data_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
				$custFilter1 = ' (location in (3))  and is_enabled=1 ';
				
				$custom_group = $object_setting->get_custom_group_list($custFilter1, VIS_STAFF_ID, VIS_LOCATION_AREA);
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
								include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
								$export         = new export();
$j=0;
foreach ($report as $i){
foreach ($i as $key => $val){
if($key == 'ticket_hash'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'receive_date'){
$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
}
elseif($key == 'field_name'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'content'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'first_name' || $key == 'last_name' || $key == 'email' || $key == 'group_name'){
$report_exp[$j][$key] = $val;
}
}
$report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash'];
$report_exp1[$j]['client_name'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
$report_exp1[$j]['email'] = $report_exp[$j]['email'];
$report_exp1[$j]['group_name'] = $report_exp[$j]['group_name'];
$report_exp1[$j]['create_date'] = $report_exp[$j]['receive_date'];				
$report_exp1[$j]['field_name'] = $report_exp[$j]['field_name']; 
$report_exp1[$j]['field_value'] = $report_exp[$j]['content']; 
												$j++;
								}
								$completed_date = date('Y-m-d H:i:s');
								$report_exp1['Report_title'] = $report_title;
				
								$filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
								if ($operation['export_type'] == 1) {
												$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
												if (file_exists('../downloads/' . $filename . '.csv')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 2) {
												$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.html')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 3) {
												
												
												$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.xls')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								}
								
								if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
												$database->database_disconnect();
												$database = new class_database();
												include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
												$object = new class_report($database);
												
								}
								$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
								if (!$result)
												return false;
								$this->email_send_download($operation);
								return true;
				} else
								include(VIS_PATH_TEMPLATE . 'customfield_data_report.php');
} else
				include(VIS_PATH_TEMPLATE . 'report_form.php');
$database->database_disconnect();
return true;
}
//custom reports
function view_custom_report($operation) 
{
        $database=new class_database();
        $operation['vis_current_operation']='view_custom_report'; 
        $operation['vis_current_module']='report'; 
		 if(isset($operation['vis_action'])){
            $action=explode('/', $operation['vis_action']);
			}
		$operation['custom_show_report']=7;
        
        $staff=new class_staff($database);
		$operation['custom_show_report']=(isset($operation['custom_show_report']))?7:'';
        list($view_info, $view_details)=$staff->get_ticket_view(VIS_STAFF_ID, VIS_LOCATION_AREA);	
        $page=(isset($operation['vis_page']) && is_numeric($operation['vis_page']))?$operation['vis_page']:0;
        $limit=($page*$view_info['page_count']).','.$view_info['page_count'];
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        $object=new class_ticket($database);
		$filter='vscope=1 and is_report=1 and owner='.VIS_STAFF_ID;
			
        $filters=$object->get_filters_details($filter, VIS_STAFF_ID, VIS_LOCATION_AREA,$limit);
        $filters_count=($filters!=-1)?count($filters):0;
        $filters_detail_total=$object->get_filters_details($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $filters_total=($filters_detail_total!=-1)?count($filters_detail_total):0;
        $filters_info['count']=$filters_total;
		$view_info['page_count']=($filters_info['count']==0)?0:ceil($filters_info['count']/$view_info['page_count']);
		$filters_info['page']=$page;	
			if($filters!=-1){ for($i=0;$i<count($filters);$i++) {
			$criteria_detail=$object->get_search_criteria('(FC.base_criteria=16 or FC.base_criteria=12) and FC.filter_id='.$filters[$i]['filter_id'], VIS_STAFF_ID, VIS_LOCATION_AREA,1);
			$durationdate=explode('=',$criteria_detail[0]['content']);
			$filters[$i]['fromdate']=$durationdate[0];
			$filters[$i]['todate']=$durationdate[1];
			}
			}
		
        $database->database_disconnect();
								if($operation['vis_filter']==1){
								include(VIS_PATH_TEMPLATE.'custom_filter_list.php');
								}else{
        include(VIS_PATH_TEMPLATE.'custom_filter.php');
								}
        return true;
    }		
				
function custom_report_detail($operation) 
{
        global $vis_language;
        $spliter=(strstr($operation['vis_action'], '/'))?'/':',';
        $action=explode($spliter, $operation['vis_action']);
        if(!isset($action[4]) || trim($action[4])=='') {
            $message=$vis_language['MSGFILTIDNTFND'];
            return false;
        }
        if(isset($action[4]) && $action[4]!='' && is_numeric($action[4])) {
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $ticket=new class_ticket($database);
        $client=new class_client($database);
        $setting=new class_setting($database);
        include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'client.php'); 
								include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'ticket.php'); 
        $object_client = new client();
								$object_ticket = new ticket();
        $ticket_types=$object_ticket->get_properties('get_types', '');
        $domain_list=$object_client->get_domain_access_list();
        $user_groups=$client->get_client_group(' (domain_id in ('.$domain_list.') or domain_id=0 ) and is_enable=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $domains=$ticket->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1','','','', VIS_STAFF_ID, VIS_LOCATION_AREA); 
        $departments=$ticket->get_department(VIS_TABLE_PREFIX.'department.is_enabled=1 and '.VIS_TABLE_PREFIX.'domain.is_enabled=1',VIS_STAFF_ID, VIS_LOCATION_AREA);
        $status=$object_ticket->get_properties('get_status', '');
        $priorities=$object_ticket->get_properties('get_priorities', '');
        $flags=$object_ticket->get_properties('get_flags', '');
        $owner=$object_ticket->get_properties('get_staff', '');
        list($sinfo, $sla)=$setting->get_sla(VIS_TABLE_PREFIX.'sla.is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $search_details=$ticket->get_search_detail('F.filter_id='.$action[4], VIS_STAFF_ID, VIS_LOCATION_AREA);
        $search_details=$search_details[0];
        $criteria_detail=$ticket->get_search_criteria('FC.filter_id='.$action[4], VIS_STAFF_ID, VIS_LOCATION_AREA);
        $lang_dt=$setting->get_language('is_enabled=1', '', 0, '', 0, VIS_LOCATION_AREA);
        list($info, $client_language)=$lang_dt;
		 $label_details=$object_ticket->get_properties('get_labels', ''); // label list 
		 $channel_details=$object_ticket->get_properties('get_channel', '');
		 $category_details=$object_ticket->get_properties('get_category', '');		
		 $subcategory_details=$object_ticket->get_properties('get_subcategory', '');
		 $item_details=$object_ticket->get_properties('get_item', '');
		 $level_details=$object_ticket->get_properties('get_level', '');
		 $location_details=$object_ticket->get_properties('get_location', '');
        $database->database_disconnect();
        include(VIS_PATH_TEMPLATE.'filter_update.php');
        return true;
	}
    }					
		
function update_custom_report($operation) 
{
        global $vis_language;
        $data=parse_form_array($operation['vis_action']); 
        if(!isset($data['vis_filter_title']) || $data['vis_filter_title']=='') {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGREQFLDTIT'].'"); </script>';
            return false;
        }
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        $object=new class_ticket($database); 
								include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$setting_object=new class_setting($database);
        $ret=$object->update_filter($data, VIS_STAFF_ID, VIS_LOCATION_AREA);
								if($ret){
									$search_details=$object->get_search_detail('F.filter_id='.$data['vis_filter_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
									$search_details=$search_details[0];
									 if($search_details['vscope']==1){
												$msg='Update custom report -'.$search_details['filter_name']; $moduleid=12;
												$setting_object->add_staff_activity_log($msg,VIS_STAFF_ID,$moduleid, VIS_LOCATION_AREA);	
												}
									}
        $database->database_disconnect();
		$operation['vis_current_operation']='view_custom_report';
		$operation['vis_current_module']='report';			
$operation['vis_is_report']=(isset($operation['vis_is_report']))?$operation['vis_is_report']:0;		 
        if(!$ret) {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
            return false;
        
     }  else {
		
		  echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'"); $("#vis_workpanel_div").visExecution("load_module","'.$vis_language['TXTFILTERS'].'",{"vis_module":"'.$operation['vis_current_module'].'", "vis_operation":"'.$operation['vis_current_operation'].'"}); </script>';}
        return true;
    }		
				
function new_custom_report($operation) 
{
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $ticket=new class_ticket($database);
        $client=new class_client($database);
        $setting=new class_setting($database);
        include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'client.php'); 
								include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'ticket.php');
        $object_client = new client();
								$object_ticket = new ticket();
       
             $operation['show_report']=6;
       
        $filters=$ticket->get_filters_details('vscope=1 and owner='.VIS_STAFF_ID, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $ticket_types=$object_ticket->get_properties('get_types', '');
        $domain_list=$object_client->get_domain_access_list();
        $user_groups=$client->get_client_group(' (domain_id in ('.$domain_list.') or domain_id=0 ) and is_enable=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $domains=$ticket->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1',VIS_STAFF_ID, VIS_LOCATION_AREA);
        $departments=$ticket->get_department(VIS_TABLE_PREFIX.'department.is_enabled=1 and '.VIS_TABLE_PREFIX.'domain.is_enabled=1',VIS_STAFF_ID, VIS_LOCATION_AREA);
        $status=$object_ticket->get_properties('get_status', '');
        $priorities=$object_ticket->get_properties('get_priorities', '');
        $flags=$object_ticket->get_properties('get_flags', '');
        $owner=$object_ticket->get_properties('get_staff', '');
        list($sinfo, $sla)=$setting->get_sla(VIS_TABLE_PREFIX.'sla.is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $lang_dt=$setting->get_language('is_enabled=1', '', 0, '', 0, VIS_LOCATION_AREA);
        list($info, $client_language)=$lang_dt;
        $label_details=$object_ticket->get_properties('get_labels', ''); // label list 
        $channel_details=$object_ticket->get_properties('get_channel', '');
		
		$category_details=$object_ticket->get_properties('get_category', '');
		$subcategory_details=$object_ticket->get_properties('get_subcategory', '');
		$item_details=$object_ticket->get_properties('get_item', '');
		$level_details=$object_ticket->get_properties('get_level', '');
		$location_details=$object_ticket->get_properties('get_location', '');
        $database->database_disconnect();
        include(VIS_PATH_TEMPLATE.'search.php');
        return true;
    }	
				
function save_customfilter($operation) 
{
        include(VIS_PATH_TEMPLATE.'save_customfilter.php');
        return true;
    }		
				
function custom_search_result($operation) 
{
        global $vis_language;
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        if(VIS_LOCATION_AREA==0) $data=$operation;
        else $data=parse_form_array($operation['vis_action']);
        $object=new class_ticket($database);
        if(isset($data['vis_report']) && $data['vis_report']==1){
            $from_date = strtotime($data['vis_from_date']);
            $to_date = strtotime($data['vis_to_date']);
												
           list($ret, $msg) = $this->report_date_range_validation($data['vis_from_date'], $data['vis_to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '"); </script>';
                        return false;
                    }
        }
        $filterid=$object->set_filter($data, 0, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $database->database_disconnect();
        if(VIS_LOCATION_AREA==0) {
            if(!$filterid) echo 'error';
            else {
                $operation['vis_filter_id']=$filterid;
                return $this->get_tickets($operation);
            }
        }
        else {
            if(!$filterid)
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
            else
				echo '<script> $("#vis_workpanel_div").visExecution("load_module","'.$vis_language['TXTREPORTS'].'", {"vis_module":"report", "vis_operation":"custom_report_result", "vis_search":'.$operation['vis_search'].', "vis_filter_id":'.$filterid.', "vis_action":"#/report/custom_report_result/'.$vis_language['TXTSEARCHRESULT'].'/'.$filterid.'", "vis_report":1}); </script>';
        }
        return true;
    }	
							
function custom_search_save($operation) 
{
        global $vis_language;
        $data=parse_form_array($operation['vis_action']);
        if(!isset($operation['vis_filter_title']) || $operation['vis_filter_title']=='') {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGFLRTTTMISS'].'"); </script>';
            return false;
        }
        $data['vis_filter_title']= $operation['vis_filter_title'];
		$data['vis_is_report']= $operation['vis_is_report'];
		$is_report=$data['vis_is_report'];
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        $object=new class_ticket($database);
		
		list($ret, $msg) = $this->report_date_range_validation($data['vis_from_date'], $data['vis_to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '"); </script>';
                        return false;
                    }
		
        $filterid=$object->set_filter($data, 1, VIS_STAFF_ID, VIS_LOCATION_AREA);
								include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
						 $setting_object=new class_setting($database);		
								if($filterid){
								$search_details=$object->get_search_detail('F.filter_id='.$filterid, VIS_STAFF_ID, VIS_LOCATION_AREA);
									$search_details=$search_details[0];
									 if($search_details['vscope']==1){
												$msg='Add custom report -'.$search_details['filter_name']; $moduleid=12;
												$setting_object->add_staff_activity_log($msg,VIS_STAFF_ID,$moduleid, VIS_LOCATION_AREA);	
												}
								}
												$database->database_disconnect();
												
        if(!$filterid)
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
								else
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'"); $("#vis_workpanel_div").visExecution(\'load_module\',\''.$vis_language['TXTSEARCHRESULT'].'\', {\'vis_module\':\'report\', \'vis_operation\':\'custom_report_result\', \'vis_filter_id\':\''.$filterid.'\', \'vis_action\':\'#/report/custom_report_result/'.$search_details['filter_name'].'/'.$filterid.'\'});$(".modal").modal("toggle"); </script>';
        return true;
    }
				
function custom_report_result($operation) 
{ 
global $vis_language;
				
					if(isset($operation['vis_filter_id']) && is_numeric($operation['vis_filter_id'])) $filter_id=$operation['vis_filter_id']; 
        else if(isset($operation['vis_action']) && $operation['vis_action'] != ""){
            $action=explode("/", $operation['vis_action']);
            if(isset($action[4]) && is_numeric($action[4])) {
                $operation['vis_filter_id'] = $filter_id=$action[4];
														}
        }
								
								if(isset($operation['vis_page']) && is_numeric($operation['vis_page'])) {
            $page=$operation['vis_page'];
								}else{
									$page=0;
								}
								
						$database=new class_database();
      include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
						include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
      $ticket_object=new class_ticket($database); 
						$staff=new class_staff($database);
						$filter=$ticket_object->get_filter($filter_id, VIS_STAFF_ID, VIS_LOCATION_AREA);
						//$filter['filter']
						$operation['view_title']=$filter['filter_name'];
						
        $temp=$staff->get_ticket_view(VIS_STAFF_ID ,VIS_LOCATION_AREA);
        list($view_info, $view_details)=$temp;
        $limit=($page*$view_info['page_count']).','.$view_info['page_count'];
								list($ticket_info, $ticket_details)=$ticket_object->get_ticket($filter['filter'], $limit, $page, $sort_by, VIS_STAFF_ID, VIS_LOCATION_AREA);
							
								
								$view_info['page_count']=($ticket_info['total']==0)?0:ceil($ticket_info['total']/$view_info['page_count']);
        $ticket_info['ticket_count']=(!$ticket_details || $ticket_details==-1)?0:count($ticket_details);
        $ticket_info['page']=$page;//*$view_info['page_count'];
        
								if((isset($operation['vis_filter_enable']) && $operation['vis_filter_enable']==1) || (isset($operation['vis_page']) && is_numeric($operation['vis_page']))){
				ob_start();
				include(VIS_PATH_TEMPLATE.'custom_rep_ticket_list.php');
				$tkt_list = ob_get_clean();
				ob_end_flush();
				$select='';
				$paging_list='';
				echo json_encode( array('total_cnt'=>$ticket_info['total'], 'paging'=>$select, 'list'=> $tkt_list, 'filter_id'=>$operation['vis_filter_id'],'total_pages'=>$view_info['page_count'],'total_tickets'=>(($ticket_details!=-1)?count($ticket_details):0) ));
			}
			else{
				//--filter list data
				include(VIS_PATH_TEMPLATE.'custom_rep_ticket.php');
			}
					$database->database_disconnect();
					return true;
					
				}//end fun
				
function get_custom_delete($operation) 
{
        global $vis_language;
		 
        $message_info['vis_form_title']=$vis_language['TXTDELETE'];
        $message_info['vis_message']=$vis_language['MSGSURE'];
        $message_info['vis_event']="$(this).visExecution('load_operations', {'vis_module':'report', 'vis_operation':'custom_report_delete', 'vis_filter_id':".$operation['vis_filter_id'].", 'vis_is_report':".$operation['vis_is_report']."})";
		 
        include(VIS_PATH_TEMPLATE.'confirm_delete.php');
        return true;
    }
function custom_report_delete($operation) 
{
        global $vis_language;
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        $object=new class_ticket($database);
								include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
						 $setting_object=new class_setting($database);	
							
        $result=$object->delete_filter($operation['vis_filter_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
		if($result){
								$search_details=$object->get_search_detail('F.filter_id='.$operation['vis_filter_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
									$search_details=$search_details[0];
									 if($search_details['vscope']==1){
											$msg='Delete custom report -'.$search_details['filter_name']; $moduleid=12; 
												$setting_object->add_staff_activity_log($msg,VIS_STAFF_ID,$moduleid, VIS_LOCATION_AREA);	
												}
								}
        $database->database_disconnect();
		 
         if(!$result){
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); $("#vis_filter_delete_area").dialog("close"); </script>';
      }  else{
			
			  echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'"); $("#vis_workpanel_div").visExecution("load_module", "'.$vis_language['TXTCUSTOM'].'", {"vis_module":"report", "vis_operation":"view_custom_report"});</script>'; 
			} 
        return $result;
    }
				
//cutom report fun end	
//sla reports
function client_sla_report($operation)
{
        global $vis_language;
        $report_title = $vis_language['TXTCLIENTSLA'];
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        $export = 1;
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'client.php');
        $cli_object = new class_client($database);
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object = new class_ticket($database);
        $domain     = $tkt_object->get_domain(VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
        list($clinfo, $clients) = $cli_object->get_client(VIS_TABLE_PREFIX . 'domain.is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
        if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
              
                if ($data['duration'] == 0 && $data['vis_client_id'] == 0  && $data['domain_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['domain_id'] == 0  && $data['vis_client_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDOMAINFORRES'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                else if ($data['duration'] != 0 && $data['domain_id'] == 0  && $data['vis_client_id'] != 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDOMAINFORRES'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                else if ($data['duration'] != 0 && $data['domain_id'] != 0  && $data['vis_client_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELCLIENTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                 else if ($data['duration'] == 0 &&  $data['domain_id'] != 0  &&  $data['vis_client_id'] != 0) 
                 {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                 else if ($data['duration'] == 0 &&  $data['domain_id'] != 0  &&  $data['vis_client_id'] == 0) 
                 {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                
                $currentdate = date('Y-m-d h:i:s');
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter    = 'operation_id<=2 and receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and '. VIS_TABLE_PREFIX .'ticket.client_id=' . $data['vis_client_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket.domain_id=' . $data['domain_id'];
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    $weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter   = 'operation_id<=2 and receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and '. VIS_TABLE_PREFIX .'ticket.client_id=' . $data['vis_client_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket.domain_id=' . $data['domain_id'];
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $hour   = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter = 'operation_id<=2 and receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and '. VIS_TABLE_PREFIX .'ticket.client_id=' . $data['vis_client_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket.domain_id=' . $data['domain_id'];
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    $from   = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to     = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter = 'operation_id<=2 and receive_date between ' . $from . ' AND ' . $to . ' and '. VIS_TABLE_PREFIX .'ticket.client_id=' . $data['vis_client_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket.domain_id=' . $data['domain_id'];
                }
            }
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object = new class_report($database);
            $filter .= ' and ' . VIS_TABLE_PREFIX . 'ticket.is_spam!=1 and ' . VIS_TABLE_PREFIX . 'ticket.resolution_time!=0 and ' . VIS_TABLE_PREFIX . 'ticket.due_time!=0';
            $post_details    = $object->client_sla_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
            $resolutionCount = $responseCount = 0;
            $report_avg      = 0;
            $report_cnt      = 0;
            $report          = array();
            if ($post_details != -1)
                for ($i = 0; $i < count($post_details); $i++) {
				
					// ===================== Code to calculate timestamp (Starts Here) =====================
					if($post_details[$i]['operation_id']==0){
						$report[$post_details[$i]['ticket_id']]['post_date']=$post_details[$i]['entry_date'];
					}else if($post_details[$i]['operation_id']==50){
						$report[$post_details[$i]['ticket_id']]['pause_date']=$post_details[$i]['entry_date'];
					}else if($post_details[$i]['operation_id']==51){
						$report[$post_details[$i]['ticket_id']]['resume_date']=$post_details[$i]['entry_date'];
						$report[$post_details[$i]['ticket_id']]['slapasue_diff']= $report[$post_details[$i]['ticket_id']]['resume_date'] - $report[$post_details[$i]['ticket_id']]['pause_date'];
						$report[$post_details[$i]['ticket_id']]['sla_cal_date']=$report[$post_details[$i]['ticket_id']]['sla_cal_date'] + ($report[$post_details[$i]['ticket_id']]['resume_date'] - $report[$post_details[$i]['ticket_id']]['pause_date']);
						$report[$post_details[$i]['ticket_id']]['resume_date']='';
						$report[$post_details[$i]['ticket_id']]['pause_date']='';
					}else if($post_details[$i]['operation_id']==1 && $post_details[$i]['operator']!=1){
						$report[$post_details[$i]['ticket_id']]['post_diff']=($post_details[$i]['entry_date'] - $report[$post_details[$i]['ticket_id']]['post_date']);
						
						$report[$post_details[$i]['ticket_id']]['maintaint_date']=($post_details[$i]['entry_date'] - $report[$post_details[$i]['ticket_id']]['post_date']) - ($report[$post_details[$i]['ticket_id']]['sla_cal_date']);
					}
					// ===================== Code to calculate timestamp (Ends Here) =====================
                    if (!isset($report[$post_details[$i]['ticket_id']]['start_time'])) {
                        $report[$post_details[$i]['ticket_id']]['ticket_hash']     = $post_details[$i]['ticket_hash'];
                        $report[$post_details[$i]['ticket_id']]['subject']         = $post_details[$i]['subject'];
                        $report[$post_details[$i]['ticket_id']]['ticket_entry']    = $post_details[$i]['receive_date'];
                        $report[$post_details[$i]['ticket_id']]['start_time']      = $post_details[$i]['entry_date'];
                        $report[$post_details[$i]['ticket_id']]['ticket_priority'] = $post_details[$i]['ticket_priority'];
                        $report[$post_details[$i]['ticket_id']]['department_name'] = $post_details[$i]['department_name'];
						$report[$post_details[$i]['ticket_id']]['first_name'] = $post_details[$i]['first_name'];
						$report[$post_details[$i]['ticket_id']]['last_name'] = $post_details[$i]['last_name'];
						$report[$post_details[$i]['ticket_id']]['email'] = $post_details[$i]['email'];
						$report[$post_details[$i]['ticket_id']]['group_name'] = $post_details[$i]['group_name'];
                        $report[$post_details[$i]['ticket_id']]['timestamp']       = ($post_details[$i]['old_info'] != '' && $post_details[$i]['old_info'] != 0) ? $post_details[$i]['old_info'] - $post_details[$i]['receive_date'] : 0;
                        $report[$post_details[$i]['ticket_id']]['ticket_id']       = $post_details[$i]['ticket_id'];
                        $report[$post_details[$i]['ticket_id']]['status_id']       = $post_details[$i]['status_id'];
                        $report[$post_details[$i]['ticket_id']]['remain_resolve']  = $post_details[$i]['remain_resolve'];
                        
                        $report[$post_details[$i]['ticket_id']]['pause_rtime']  = $post_details[$i]['pause_rtime'];
                        $report[$post_details[$i]['ticket_id']]['count']           = 0;
                        $report[$post_details[$i]['ticket_id']]['resolve_time']    = $post_details[$i]['resolution_time'];
                        $report[$post_details[$i]['ticket_id']]['status_name']     = $post_details[$i]['status_name'];
                        $report[$post_details[$i]['ticket_id']]['sla_name']        = $post_details[$i]['sla_name'];
						$report[$post_details[$i]['ticket_id']]['slapause_time']   = $post_details[$i]['slapause_time'];
                        $report[$post_details[$i]['ticket_id']]['solve_time']      = 0;
						$report[$post_details[$i]['ticket_id']]['total_sla_seconds'] = 0;
						$report[$post_details[$i]['ticket_id']]['used_sla_seconds'] = 0;
                       
                        if ($post_details[$i]['operator'] == 1) {
                            $report[$post_details[$i]['ticket_id']]['is_posted'] = false;
                        } else {
                            $report[$post_details[$i]['ticket_id']]['is_posted'] = true;
                            $report_avg += ($post_details[$i]['old_info'] != '' && $post_details[$i]['old_info'] != 0) ? $post_details[$i]['old_info'] - $post_details[$i]['receive_date'] : 0;
                            $report_cnt++;
                            $replied[$post_details[$i]['ticket_id']] = 1;
                        }
                       
                    } elseif ($post_details[$i]['operator'] == 1) {
                        $report[$post_details[$i]['ticket_id']]['start_time'] = $post_details[$i]['entry_date'];
                    } elseif ($post_details[$i]['operation_id'] == 2) {
                        $report[$post_details[$i]['ticket_id']]['solve_time']   = $post_details[$i]['entry_date'];
                        $report[$post_details[$i]['ticket_id']]['resolve_time'] = $post_details[$i]['resolution_time'] == 0 ? 0 : ($post_details[$i]['resolution_time'] - $post_details[$i]['entry_date']);
                    } elseif ($post_details[$i]['old_info'] != '' && $post_details[$i]['old_info'] != 0) {
                        // ===================== Code to calculate timestamp (Start) =====================
						$total_sla_seconds = ($post_details[$i]['due_time'] - $report[$post_details[$i]['ticket_id']]['start_time']);// Total SLA
						// Total SLA Used
						$sla_response_time = round($report[$post_details[$i]['ticket_id']]['maintaint_date']);
						
						$report[$post_details[$i]['ticket_id']]['total_sla_seconds'] = $total_sla_seconds;
						$report[$post_details[$i]['ticket_id']]['used_sla_seconds'] = $sla_response_time;
						$report[$post_details[$i]['ticket_id']]['timestamp'] = $sla_response_time;
						// Old Calculations
						//$report[$post_details[$i]['ticket_id']]['timestamp'] += ($post_details[$i]['entry_date'] - $report[$post_details[$i]['ticket_id']]['start_time'] - $sla_pause_time) * 100 / ($post_details[$i]['old_info'] - $report[$post_details[$i]['ticket_id']]['start_time']);
						// ===================== Code to calculate timestamp (End) =======================		
						
						$report[$post_details[$i]['ticket_id']]['count']++;
                        $report[$post_details[$i]['ticket_id']]['is_posted'] = true;
                      	$report_avg += $sla_response_time;
						//$report_avg += ($post_details[$i]['entry_date'] - $report[$post_details[$i]['ticket_id']]['start_time'] - $sla_pause_time) * 100 / ($post_details[$i]['old_info'] - $report[$post_details[$i]['ticket_id']]['start_time']);
                         
					   $report_cnt++;
                        $replied[$post_details[$i]['ticket_id']] = 1;
                    }
                     
                } else
                $report = -1;
            
            if ($report != -1) {
                foreach ($report as $ticket_id => $details) {
                    $isUndueSet = 0;
                    if (($details['is_posted'] && $details['count'] != 0)) {
                        $responseCount++;
                    }
                    if ($details['resolve_time'] < 0 && $details['status_id'] == 3) {
                        $isUndueSet                                = 0;
                        $report[$ticket_id]['avg_resolution_time'] = $vis_language['TXTUNDUE'];
                    } else if (($details['resolve_time'] < 0 || $details['remain_resolve'] < 0) && $details['status_id'] != 3) {
                        $isUndueSet                                = 0;
                        $report[$ticket_id]['avg_resolution_time'] = $vis_language['TXTOVERDUE'];
                    } else if (($details['resolve_time'] > 0 || $details['remain_resolve'] > 0) && $details['status_id'] != 3) {
                        $isUndueSet                                = 0;
                        $report[$ticket_id]['avg_resolution_time'] = $vis_language['TXTUNDUE'];
                    } else {
                        $isUndueSet = 1;
                        $resolutionCount++;
                        if($details['solve_time'] > 0){
                        	$report[$ticket_id]['avg_resolution_time'] = count_remain_time($details['solve_time'] - $details['start_time']);
						}else{
							$report[$ticket_id]['avg_resolution_time'] = 0;
						}
                    }
                    $report[$ticket_id]['response_sla']   = ($details['is_posted'] && $details['count'] != 0) ? $vis_language['TXTMAINTAIN'] : $vis_language['TXTNOTMAINTAIN'];
                    $report[$ticket_id]['resolution_sla'] = ($isUndueSet == 1) ? $vis_language['TXTMAINTAIN'] : $vis_language['TXTNOTMAINTAIN'];
					
					if($details['total_sla_seconds'] > $details['used_sla_seconds']){
						$cal_timestamp = round(($details['timestamp']/$details['count']),2);
					}else{
						$cal_timestamp = round($details['timestamp'],2);
					}
                    //$report[$ticket_id]['avg_response_time']=($details['is_posted'] && $details['count']!=0)?count_remain_time(round(($details['timestamp']/$details['count']),2)):'Not responded';
					$report[$ticket_id]['avg_response_time']=($details['is_posted'] && $details['count']!=0) ? count_remain_time($cal_timestamp):'Not responded';
                }
            }
            
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                $j      = 0;
                foreach ($report as $i) {
                    foreach ($i as $key => $val) {
                        if ($key == 'sla_time')
                            $report_exp[$j]['SLA'] = count_remain_time($val);
                        elseif ($key == 'ticket_entry')
                            $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        elseif ($key == 'due_time')
                            $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        elseif ($key == 'resolve_time')
                            $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        elseif ($key == 'ticket_hash' || $key == 'subject' || $key == 'department_name' || $key == 'ticket_priority' || $key == 'status_name' || $key == 'sla_name' || $key == 'first_name' || $key == 'last_name' || $key == 'email' || $key == 'group_name')
                            $report_exp[$j][$key] = $val;
                        else if ($key == 'avg_resolution_time')
                            $report_exp[$j][$key] = $val;
                            
                        else if ($key == 'pause_rtime')
                            $report_exp[$j][$key] = $val;
                        else if ($key == 'response_sla')
                            $report_exp[$j][$key] = $val;
                        else if ($key == 'resolution_sla')
                            $report_exp[$j][$key] = $val;
                        else if ($key == 'avg_response_time')
                         $report_exp[$j][$key] = $val;
                    }
				$sla_name = ($report_exp[$j]['sla_name']!= '')?$report_exp[$j]['sla_name']:$vis_language['TXTCUSTDUETIME'];
					$report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash'];
					$report_exp1[$j]['subject'] = $report_exp[$j]['subject'];
					$report_exp1[$j]['creation_date'] = $report_exp[$j]['ticket_entry'];
					$report_exp1[$j]['department'] = $report_exp[$j]['department_name'];
					$report_exp1[$j]['client_name'] = $report_exp[$j]['first_name'].' '. $report_exp[$j]['last_name'];
					$report_exp1[$j]['email'] = $report_exp[$j]['email'];
					$report_exp1[$j]['organization'] = $report_exp[$j]['group_name'];
					$report_exp1[$j]['sla_plan'] = $sla_name;
					$report_exp1[$j]['priority'] = $report_exp[$j]['ticket_priority'];
					$report_exp1[$j]['status'] = $report_exp[$j]['status_name'];
			
					$report_exp1[$j]['avg_response_time'] = $report_exp[$j]['avg_response_time'];
					$resolution_flag = ($report_exp[$j]['pause_rtime']!='') ? $report_exp[$j]['pause_rtime']:$report_exp[$j]['avg_resolution_time'];
					$report_exp1[$j]['resolution_time'] = $resolution_flag;
					$report_exp1[$j]['response_sla'] = $report_exp[$j]['response_sla'];
					$report_exp1[$j]['resolution_sla'] = $report_exp[$j]['resolution_sla'];
                    
					$j++;
                }
				
				$report_exp1[$j]['total_replied']=$vis_language['TXTTOTALTICKETREPLIED'].' :'.count($replied).'/'.count($report);
				$report_exp1[$j]['total_avg_response']=$vis_language['TXTTOTALAVGRESPONSESLAMAINTAIN'].' :'.(($report_cnt==0)?0:round((($responseCount/count($report))*100), 2)).'%';
				$report_exp1[$j]['total_avg_resolution']=$vis_language['TXTTOTALAVGRESOLUTIONSLAMAINTAIN'].' :'.(($report_cnt==0)?0:round((($resolutionCount/count($report))*100), 2)).'%';
				
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                 if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
                if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                 $this->email_send_download($operation);
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'client_sla_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
    }
				
	function sla_report($operation)
	{
        global $vis_language;
        $report_title = $vis_language['TXTDEPTSLAREPORT'];
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        $export = 1;
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        
        $tkt_object       = new class_ticket($database);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
        $departments      = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', $staff_id, VIS_LOCATION_AREA);
        
        if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                if ($data['duration'] == 0 && $data['department_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['department_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] == 0 && $data['department_id'] != 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                
                $currentdate = date('Y-m-d h:i:s');
                if ($data['duration'] == 1){ //if select Last 30 days duration
                    $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;                    
                    $filter    = 'operation_id<=2 and receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and ' . VIS_TABLE_PREFIX . 'ticket.department_id=' . $data['department_id'];
                }
				else if ($data['duration'] == 2){ //if select Last 7 days duration
                    $weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter   = 'operation_id<=2 and receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and ' . VIS_TABLE_PREFIX . 'ticket.department_id=' . $data['department_id'];
                }
				else if ($data['duration'] == 3){ //if select Last 24 hours duration
                    $hour   = strtotime("-1 day ") - VIS_TIMEZONE;                   
                    $filter = 'operation_id<=2 and receive_date between  ' . $hour . ' AND ' . strtotime($currentdate) . ' and ' . VIS_TABLE_PREFIX . 'ticket.department_id=' . $data['department_id'];
                } else if ($data['duration'] == 4){ //if select Custom duration
                    $from   = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to     = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter = 'operation_id<=2 and receive_date between ' . $from . ' AND ' . $to . ' and ' . VIS_TABLE_PREFIX . 'ticket.department_id=' . $data['department_id'];
                }
            }
            
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object = new class_report($database);
            $filter .= ' and ' . VIS_TABLE_PREFIX . 'ticket.is_spam!=1 and ' . VIS_TABLE_PREFIX . 'ticket.resolution_time!=0 and ' . VIS_TABLE_PREFIX . 'ticket.due_time!=0';
            $post_details    = $object->get_sla_report($filter, $staff_id, VIS_LOCATION_AREA);
            $report_avg      = 0;
            $report_cnt      = 0;
            $resolutionCount = $responseCount = 0;
            $replied         = array();
            
            if ($post_details != -1)
                for ($i = 0; $i < count($post_details); $i++) {
				
					// ===================== Code to calculate timestamp (Starts Here) =====================
					if($post_details[$i]['operation_id']==0){
						$report[$post_details[$i]['ticket_id']]['post_date']=$post_details[$i]['entry_date'];
					}else if($post_details[$i]['operation_id']==50){
						$report[$post_details[$i]['ticket_id']]['pause_date']=$post_details[$i]['entry_date'];
					}else if($post_details[$i]['operation_id']==51){
						$report[$post_details[$i]['ticket_id']]['resume_date']=$post_details[$i]['entry_date'];
						$report[$post_details[$i]['ticket_id']]['slapasue_diff']= $report[$post_details[$i]['ticket_id']]['resume_date'] - $report[$post_details[$i]['ticket_id']]['pause_date'];
						$report[$post_details[$i]['ticket_id']]['sla_cal_date']=$report[$post_details[$i]['ticket_id']]['sla_cal_date'] + ($report[$post_details[$i]['ticket_id']]['resume_date'] - $report[$post_details[$i]['ticket_id']]['pause_date']);
						$report[$post_details[$i]['ticket_id']]['resume_date']='';
						$report[$post_details[$i]['ticket_id']]['pause_date']='';
					}else if($post_details[$i]['operation_id']==1 && $post_details[$i]['operator']!=1){
						$report[$post_details[$i]['ticket_id']]['post_diff']=($post_details[$i]['entry_date'] - $report[$post_details[$i]['ticket_id']]['post_date']);
						
						$report[$post_details[$i]['ticket_id']]['maintaint_date']=($post_details[$i]['entry_date'] - $report[$post_details[$i]['ticket_id']]['post_date']) - ($report[$post_details[$i]['ticket_id']]['sla_cal_date']);
					}
					// ===================== Code to calculate timestamp (Ends Here) =====================
                    if (!isset($report[$post_details[$i]['ticket_id']]['start_time'])) {
                        $report[$post_details[$i]['ticket_id']]['ticket_hash']    = $post_details[$i]['ticket_hash'];
                        $report[$post_details[$i]['ticket_id']]['subject']        = $post_details[$i]['subject'];
                        $report[$post_details[$i]['ticket_id']]['ticket_entry']   = $post_details[$i]['receive_date'];
                        $report[$post_details[$i]['ticket_id']]['start_time']     = $post_details[$i]['entry_date'];
						$report[$post_details[$i]['ticket_id']]['first_name'] = $post_details[$i]['first_name'];
						$report[$post_details[$i]['ticket_id']]['last_name'] = $post_details[$i]['last_name'];
						$report[$post_details[$i]['ticket_id']]['email'] = $post_details[$i]['email'];
						$report[$post_details[$i]['ticket_id']]['group_name'] = $post_details[$i]['group_name'];
                        $report[$post_details[$i]['ticket_id']]['timestamp']      = ($post_details[$i]['old_info'] != '' && $post_details[$i]['old_info'] != 0) ? $post_details[$i]['old_info'] - $post_details[$i]['receive_date'] : 0;
                        $report[$post_details[$i]['ticket_id']]['ticket_id']      = $post_details[$i]['ticket_id'];
                        $report[$post_details[$i]['ticket_id']]['status_id']      = $post_details[$i]['status_id'];
                        $report[$post_details[$i]['ticket_id']]['remain_resolve'] = $post_details[$i]['remain_resolve'];
                        
                        $report[$post_details[$i]['ticket_id']]['pause_rtime'] = $post_details[$i]['pause_rtime'];
                        $report[$post_details[$i]['ticket_id']]['count']          = 0;
                        $report[$post_details[$i]['ticket_id']]['resolve_time']   = $post_details[$i]['resolution_time'];
                        $report[$post_details[$i]['ticket_id']]['status_name']    = $post_details[$i]['status_name'];
                        $report[$post_details[$i]['ticket_id']]['sla_name']       = $post_details[$i]['sla_name'];
						$report[$post_details[$i]['ticket_id']]['slapause_time']  = $post_details[$i]['slapause_time'];
                        $report[$post_details[$i]['ticket_id']]['solve_time']     = 0;
						$report[$post_details[$i]['ticket_id']]['total_sla_seconds'] = 0;
						$report[$post_details[$i]['ticket_id']]['used_sla_seconds'] = 0;
						
						
                        if ($post_details[$i]['operator'] == 1) {
                            $report[$post_details[$i]['ticket_id']]['is_posted'] = false;
                        } else {
                            $report[$post_details[$i]['ticket_id']]['is_posted'] = true;
                            $report_avg += ($post_details[$i]['old_info'] != '' && $post_details[$i]['old_info'] != 0) ? $post_details[$i]['old_info'] - $post_details[$i]['receive_date'] : 0;
                            $report_cnt++;
                            $replied[$post_details[$i]['ticket_id']] = 1;
                        }
                    } elseif ($post_details[$i]['operator'] == 1) {
                        $report[$post_details[$i]['ticket_id']]['start_time'] = $post_details[$i]['entry_date'];
                    } elseif ($post_details[$i]['operation_id'] == 2) {
                        $report[$post_details[$i]['ticket_id']]['solve_time']   = $post_details[$i]['entry_date'];
                        $report[$post_details[$i]['ticket_id']]['resolve_time'] = $post_details[$i]['resolution_time'] == 0 ? 0 : ($post_details[$i]['resolution_time'] - $post_details[$i]['entry_date']);
                    } elseif ($post_details[$i]['old_info'] != '' && $post_details[$i]['old_info'] != 0) {
                        // ===================== Code to calculate timestamp (Start) =====================
						$total_sla_seconds = ($post_details[$i]['due_time'] - $report[$post_details[$i]['ticket_id']]['start_time']);// Total SLA
						$sla_response_time = round($report[$post_details[$i]['ticket_id']]['maintaint_date']);
						$report[$post_details[$i]['ticket_id']]['total_sla_seconds'] = $total_sla_seconds;
						$report[$post_details[$i]['ticket_id']]['used_sla_seconds'] = $sla_response_time;
						$report[$post_details[$i]['ticket_id']]['timestamp'] = $sla_response_time;
						
						// Old Calculations
						//$report[$post_details[$i]['ticket_id']]['timestamp'] += ($post_details[$i]['entry_date'] - $report[$post_details[$i]['ticket_id']]['start_time'] - $report[$post_details[$i]['ticket_id']]['slapause_time']) * 100 / ($post_details[$i]['old_info'] - $sla_pause_time);
						// ===================== Code to calculate timestamp (End) =======================
						
                        $report[$post_details[$i]['ticket_id']]['count']++;
                        $report[$post_details[$i]['ticket_id']]['is_posted'] = true;
                        $report_avg += $sla_response_time;
						//$report_avg += ($post_details[$i]['entry_date'] - $report[$post_details[$i]['ticket_id']]['start_time'] - $sla_pause_time) * 100 / ($post_details[$i]['old_info'] - $report[$post_details[$i]['ticket_id']]['start_time']);
                        $report_cnt++;
                        $replied[$post_details[$i]['ticket_id']] = 1;
                    }
                } else
                $report = -1;
            
            if ($report != -1) {
                foreach ($report as $ticket_id => $details) {
                    $isUndueSet = 0;
                    if (($details['is_posted'] && $details['count'] != 0)) {
                        $responseCount++;
                    }
                    if ($details['resolve_time'] < 0 && $details['status_id'] == 3) {
                        $isUndueSet                                = 0;
                        $report[$ticket_id]['avg_resolution_time'] = $vis_language['TXTUNDUE'];
                    } else if (($details['resolve_time'] < 0 || $details['remain_resolve'] < 0) && $details['status_id'] != 3) {
                        $isUndueSet                                = 0;
                        $report[$ticket_id]['avg_resolution_time'] = $vis_language['TXTOVERDUE'];
                    } else if (($details['resolve_time'] > 0 || $details['remain_resolve'] > 0) && $details['status_id'] != 3) {
                        $isUndueSet                                = 0;
                        $report[$ticket_id]['avg_resolution_time'] = $vis_language['TXTUNDUE'];
                    } else {
                        $isUndueSet = 1;
                        $resolutionCount++;
                        if($details['solve_time'] > 0){
                        	$report[$ticket_id]['avg_resolution_time'] = count_remain_time($details['solve_time'] - $details['start_time']);
						}else{
							$report[$ticket_id]['avg_resolution_time'] = 0;	
						}
                    }
                    $report[$ticket_id]['response_sla']   = ($details['is_posted'] && $details['count'] != 0) ? $vis_language['TXTMAINTAIN'] : $vis_language['TXTNOTMAINTAIN'];
                    $report[$ticket_id]['resolution_sla'] = ($isUndueSet == 1) ? $vis_language['TXTMAINTAIN'] : $vis_language['TXTNOTMAINTAIN'];
                }
            }
            
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                $j      = 0;
                foreach ($report as $i) {
                    foreach ($i as $key => $val) {
                        if ($key == 'timestamp'){
							if($i['total_sla_seconds'] > $i['used_sla_seconds']){
								$cal_timestamp = round(($i['timestamp']/$i['count']),2);
							}else{
								$cal_timestamp = round($i['timestamp'],2);
							}
							$report_exp[$j]['SLA'] = ($i['is_posted'] && $i['count']!=0) ? count_remain_time($cal_timestamp) : 'Not responded';
						} elseif ($key == 'ticket_entry') {
                            //$creater_staff_id=$staff_id;
                            $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        } elseif ($key == 'ticket_hash' || $key == 'subject' || $key == 'status_name' || $key == 'sla_name')
                            $report_exp[$j][$key] = $val;
                        else if ($key == 'avg_resolution_time')
                            $report_exp[$j][$key] = $val;
                            
                        else if ($key == 'pause_rtime')
                            $report_exp[$j][$key] = $val;
                        else if ($key == 'response_sla')
                            $report_exp[$j][$key] = $val;
                        else if ($key == 'resolution_sla')
                            $report_exp[$j][$key] = $val;
						else if($key == 'first_name' || $key == 'last_name' || $key == 'email' || $key == 'group_name')
						    $report_exp[$j][$key] = $val;
                    }
					
					$sla_name = ($report_exp[$j]['sla_name']!= '')?$report_exp[$j]['sla_name']:$vis_language['TXTCUSTDUETIME'];
					$report_exp1[$j]['ticket_hash']=$report_exp[$j]['ticket_hash'];
					$report_exp1[$j]['subject']=$report_exp[$j]['subject'];
					$report_exp1[$j]['client_name'] = $report_exp[$j]['first_name'].' '. $report_exp[$j]['last_name'];
					$report_exp1[$j]['email'] = $report_exp[$j]['email'];
					$report_exp1[$j]['organization'] = $report_exp[$j]['group_name'];
					$report_exp1[$j]['Creation_date']=$report_exp[$j]['ticket_entry'];
					$report_exp1[$j]['status']=$report_exp[$j]['status_name'];
					$report_exp1[$j]['sla_plan']=$sla_name;
					$report_exp1[$j]['avg_response_time']=$report_exp[$j]['SLA'];
					$resolution_flag = ($report_exp[$j]['pause_rtime']!='') ? $report_exp[$j]['pause_rtime']:$report_exp[$j]['avg_resolution_time'];
					$report_exp1[$j]['avg_resolution_time']=$resolution_flag;
					$report_exp1[$j]['response_sla']=$report_exp[$j]['response_sla'];
					$report_exp1[$j]['resolution_sla']=$report_exp[$j]['resolution_sla'];
																					
					$j++;
                }
				
				
				// add summary
				$report_exp1[$j]['totalticketreply']=	$vis_language['TXTTOTALTICKETREPLIED'].": ".count($replied).'/'.count($report);
				$report_exp1[$j]['totalresavgmainten']=	$vis_language['TXTTOTALAVGRESPONSESLAMAINTAIN'].": ".(($report_cnt==0)?0:round((($responseCount/count($report))*100), 2)).'%';
				$report_exp1[$j]['totalresulationmainten']=	$vis_language['TXTTOTALAVGRESOLUTIONSLAMAINTAIN'].": ".(($report_cnt==0)?0:round((($resolutionCount/count($report))*100), 2)).'%';
																
																
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
                if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
               if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'sla_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
    }
function ticket_escalation_staff_report($operation) 
{
        
        global $vis_language;
        $report_title = $vis_language['TXTTICKETESCGROUPSTAFFOWNER'];
        $export       = 1;
        //$report_type=9;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object = new class_ticket($database);
        $staff      = $tkt_object->get_staff_list(VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
        if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
               if ($data['duration'] == 0 && $data['staff_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['staff_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELSTAFFFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] == 0 && $data['staff_id'] != 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                
                $currentdate = date('Y-m-d h:i:s');
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter    = 'receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and staff_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'staff.staff_id=' . $data['staff_id'];
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    $weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter   = 'receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and staff_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'staff.staff_id=' . $data['staff_id'];
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $hour   = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter = 'receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and staff_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'staff.staff_id=' . $data['staff_id'];
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    $from   = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to     = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter = 'receive_date between ' . $from . ' AND ' . $to . ' and staff_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'staff.staff_id=' . $data['staff_id'];
                }
            }
            
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object = new class_report($database);
            $filter .= ' and ' . VIS_TABLE_PREFIX . 'ticket.is_spam!=1';
            $report = $object->get_ticket_escalation_staff_report($filter, $data['staff_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);       
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'setting.php');
            $sett_obj = new class_setting($database);
            
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                $j      = 0;
                foreach ($report as $i) {
                    foreach ($i as $key => $val) {
                        
                        if ($key == 'ticket_hash')
                            $report_exp[$j][$key] = $val;
						 elseif ($key == 'subject')
                            $report_exp[$j][$key] = $val;
                        elseif ($key == 'receive_date')
                            $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);                       
                        elseif ($key == 'new_info')
                            $report_exp[$j][$key] = $val;
                        elseif ($key == 'entry_date')
                            $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        elseif ($key == 'first_name')         
							$report_exp[$j][$key] = $val;
						elseif ($key == 'last_name')         
							$report_exp[$j][$key] = $val;	
                        
                    }
					$report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash'];
					$report_exp1[$j]['subject'] = $report_exp[$j]['subject'];
					$report_exp1[$j]['created_on'] = $report_exp[$j]['receive_date'];
					 $report_exp1[$j]['assigned_to'] = $report_exp[$j]['first_name'].''.$report_exp[$j]['last_name'];
					  $report_exp1[$j]['assigned_on'] = $report_exp[$j]['entry_date'];
                    $j++;
                }
                
                
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
                
																if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'ticket_escalation_staff_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
    }
    
function ticket_escalation_department_report($operation)
{
        
        global $vis_language;
        $report_title = $vis_language['TXTTICKETESCGROUPDEPARTMENT'];
        $export       = 1;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object  = new class_ticket($database);
        $departments = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
        if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                if ($data['duration'] == 0 && $data['department_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['department_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] == 0 && $data['department_id'] != 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                $currentdate = date('Y-m-d h:i:s');
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter    = 'receive_date between  ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and ' . VIS_TABLE_PREFIX . 'department.department_id=' . $data['department_id'];
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    $weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter   = 'receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and ' . VIS_TABLE_PREFIX . 'department.department_id=' . $data['department_id'];
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $hour   = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter = 'receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and ' . VIS_TABLE_PREFIX . 'department.department_id=' . $data['department_id'];
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    $from   = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to     = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter = 'receive_date between ' . $from . ' AND ' . $to . ' and ' . VIS_TABLE_PREFIX . 'department.department_id=' . $data['department_id'];
                }
            }
            
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object = new class_report($database);
            $filter .= ' and ' . VIS_TABLE_PREFIX . 'ticket.is_spam!=1';
            $report = $object->get_ticket_escalation_department_report($filter, $data['department_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
            
            //$custFilter1=' (location in (3))  and is_enabled=1 '; 
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'setting.php');
            $sett_obj = new class_setting($database);
            
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                
                
                $j = 0;
                foreach ($report as $i) {
                    foreach ($i as $key => $val) {
                        
                        if ($key == 'ticket_hash')
                            $report_exp[$j][$key] = $val;
						 elseif ($key == 'subject')
                            $report_exp[$j][$key] = $val;
                        elseif ($key == 'receive_date')
                            $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);                       
                        elseif ($key == 'new_info')
                            $report_exp[$j][$key] = $val;
                         elseif ($key == 'entry_date')
                            $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);    
                        
                    }
					 $report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash'];
					  $report_exp1[$j]['subject'] = $report_exp[$j]['subject'];
					 $report_exp1[$j]['created_on'] = $report_exp[$j]['receive_date'];					
					 $report_exp1[$j]['assigned_to'] = $report_exp[$j]['new_info'];
					  $report_exp1[$j]['assigned_on'] = $report_exp[$j]['entry_date'];
					 
                    $j++;
                }
                
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
                
																if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'ticket_escalation_department_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
    }
				
//staff report
function work_report($operation)
{
        global $vis_language;
        $report_title = $vis_language['TXTSTAFFWORK'];
        $export       = 1;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object = new class_ticket($database);
        $staff      = $tkt_object->get_staff_list(VIS_STAFF_ID, VIS_LOCATION_AREA);
        if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)	
                 $filter = $operation['filter'];                 
            else {
 
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                /***** date difference ****/
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                if ($data['duration'] == 0 && $data['staff_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['staff_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELSTAFFFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] == 0 && $data['staff_id'] != 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                
                $currentdate = date('Y-m-d h:i:s') ;
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate. '-1 month'))) - VIS_TIMEZONE;
                   $filter    = ' operation_id!=9 AND operation_id<100 AND entry_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and operator!=1 and operator_id=' . $data['staff_id'];
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    $weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter   = ' operation_id!=9 AND operation_id<100 AND entry_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and operator!=1 and operator_id=' . $data['staff_id'];
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $hour   = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter = ' operation_id!=9 AND operation_id<100 AND entry_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and operator!=1 and operator_id=' . $data['staff_id'];
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    $from   = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to     = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter = ' operation_id!=9 AND operation_id<100 and entry_date between ' . $from . ' AND ' . $to . ' and operator!=1 and operator_id=' . $data['staff_id'];
                }
            }
$filter.= ' and access_id=0 ';
            $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
            $status   = $tkt_object->get_status_list('', $staff_id, VIS_LOCATION_AREA);
            for ($i = 0; $i < count($status); $i++)
                $status_list[$status[$i]['status_id']] = $status[$i]['status_name'];
            $priority = $tkt_object->get_priorities_list($staff_id, VIS_LOCATION_AREA, '');
            for ($i = 0; $i < count($priority); $i++)
                $priority_list[$priority[$i]['priority_id']] = $priority[$i]['priority_name'];
            $department = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', $staff_id, VIS_LOCATION_AREA);
            for ($i = 0; $i < count($department); $i++)
                $department_list[$department[$i]['department_id']] = $department[$i]['department_name'];
            $staff_list[0] = 'Unassigned';
            for ($i = 0; $i < count($staff); $i++)
                $staff_list[$staff[$i]['staff_id']] = $staff[$i]['staff_name'];
            
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object = new class_report($database);
            $audit_log = $object->get_work_report($filter, $staff_id, VIS_LOCATION_AREA); 
			
			
			 
if($audit_log!=-1) {
$close_time=$audit_log[0]['closed_time'];
$creation_time=$audit_log [0]['creation_date'];
if( $close_time!=0){
$total_time=$audit_log[0]['closed_time']-$audit_log[0]['creation_date'];
}else{
$total_time="Not applicable";
} 
$avgTimeCount =count_remain_time($total_time ) ;
}
$audit_count=($audit_log==-1)?0:count($audit_log);

												for($i=0 ; $i<$audit_count ; $i++)
												{
switch($audit_log[$i]['operation_id']) {
case '0':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGNEWTKTCRT'];
	break;
	
case '1':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTKTRPLD'];
	break;
case '2':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTSTATUSCHANGEDFROM'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
	
case '3':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTPRIORITYCHANGEDFROM'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
	
case '4':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTDEPARTMENTCHANGEDFROM'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
	
case '5':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTTICKETTYPECHANGEDFROM'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
	
case '6':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTFLAGCHANGEDFROM'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
	
case '7':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');  
	$object=new class_setting($database);
	$staff_new_info=$object->get_staff(VIS_TABLE_PREFIX.'staff.staff_id='.$audit_log[$i]['new_info'], '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA,1);
	$new_info =  $staff_new_info[1][0]['first_name'].' '.$staff_new_info[1][0]['last_name'];
	if($audit_log[$i]['new_info']!=0 && $staff_new_info[1]!=-1)
																																$audit_log[$i]['audit_log_operation'] = $vis_language['MSGTKTASSIG'].' '.$vis_language['TXTTO'].' '. $new_info;
																												elseif($audit_log[$i]['new_info']==0 && $audit_log[$i]['old_info']>0){ 
																																$staff_old_info=$object->get_staff(VIS_TABLE_PREFIX.'staff.staff_id='.$audit_log[$i]['old_info'], '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA,1);
																																$old_info =  $staff_old_info[1][0]['first_name'].' '.$staff_old_info[1][0]['last_name'];
																																$audit_log[$i]['audit_log_operation'] = $vis_language['TXTTICKETUNASSIGNEDFROM'].' '. $old_info;
																												}
	else
																																$audit_log[$i]['audit_log_operation'] = $vis_language['TXTTICKETASSIGNEDTONOBODY'];
	break;
	
case '8':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');  
	$object=new class_setting($database);
	$staff_details=$object->get_staff(VIS_TABLE_PREFIX.'staff.staff_id='.$audit_log[$i]['operator_id'], '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
	$staff_name =  $staff_details[1][0]['first_name'].' '.$staff_details[1][0]['last_name'];
	if($audit_log[$i]['old_info']!=0 )
		$audit_log[$i]['audit_log_operation']=$vis_language['MSGTKTTRSH'];
	elseif($audit_log[$i]['old_info']==0 )
		$audit_log[$i]['audit_log_operation']=$vis_language['TXTTKTMOVEINBOCFROMTRASH'];
	break;
	
case '9':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');  
	$object=new class_setting($database);
	$staff_details=$object->get_staff(VIS_TABLE_PREFIX.'staff.staff_id='.$audit_log[$i]['operator_id'], '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
	$staff_name =  $staff_details[1][0]['first_name'].' '.$staff_details[1][0]['last_name'];
	$audit_log[$i]['audit_log_operation']=$audit_log[$i+1]['old_info'] .' deleted';
	break;
case '10':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
	$object=new class_ticket($database);
	$audit_log[$i]['audit_log_operation']=$audit_log[$i]['new_info'] .' merged to '.$audit_log[$i]['old_info'];
	break;
	
case '11':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTKTFRWD'];
	break;
	
case '12':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTKTDELETED'];
	break;
	
case '13':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');  
	$object=new class_setting($database);
	$sla_old_info=$object->get_sla(VIS_TABLE_PREFIX.'sla.sla_id='.$audit_log[$i]['old_info'], '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
	$sla_new_info=$object->get_sla(VIS_TABLE_PREFIX.'sla.sla_id='.$audit_log[$i]['new_info'], '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
				$old_info =  $sla_old_info[1][0]['sla_name'];
	$new_info =  $sla_new_info[1][0]['sla_name'];
	if($audit_log[$i]['new_info']!=0 && $audit_log[$i]['old_info']!=0)
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTSLACHANGEDFROM'].' '. $old_info. ' to '. $new_info;
	else if($audit_log[$i]['old_info']==0 && $audit_log[$i]['new_info']!=0)
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTSLASETTO'].' '. $new_info;
	else if($audit_log[$i]['new_info']==0)
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTAPPLIEDCUSTOMSLAPLAN'];
	break;
	
case '14':
	if($audit_log[$i]['new_info']!=0 && $audit_log[$i]['old_info']==0)
		$audit_log[$i]['audit_log_operation']=$vis_language['MSGSPAMMRK'];
	elseif($audit_log[$i]['new_info']==0 && $audit_log[$i]['old_info']!=0)
		$audit_log[$i]['audit_log_operation']=$vis_language['TXTNOTSPAMMARKED'];
	break;
	
case '15':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGUNSPAM'];
	break;
case '16':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTNEWTASKATTACHED'].' '. $audit_log[$i]['old_info'];
	break;
case '17':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTDETACHEDATTACHEDTASK'].' '. $audit_log[$i]['old_info'];
	break;
case '18':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTNEWCIATTACHED'].' '. $audit_log[$i]['old_info'];
	break;
case '19':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTDETACHEDATTACHEDCI'].' '. $audit_log[$i]['old_info'];
	break;
case '20':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTNEWPROBLEMATTACHED'].' '. $audit_log[$i]['old_info'];
	break;
case '21':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTDETACHEDATTACHEDPROBLEM'].' '. $audit_log[$i]['old_info'];
	break;
case '22':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTNEWCHANGEATTACHED'].' '. $audit_log[$i]['old_info'];
	break;
case '23':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTDETACHEDATTACHEDCHANGE'].' '. $audit_log[$i]['old_info'];
	break;	
case '24':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
	$object=new class_ticket($database);
	$audit_log[$i]['audit_log_operation']=$audit_log[$i]['new_info'] .' '.$vis_language['TXTLINKEDTO'].' '.$audit_log[$i]['old_info'];
	break;	
case '25':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
	$object=new class_ticket($database);
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETPOSTDELETE'];
	break;	
case '26':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
	$object=new class_ticket($database);
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTTICKETPOSTSPLITWITH'].' '.$audit_log[$i]['new_info'];
	break;	
case '27':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTCHANNELCHANGEDFROM'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
case '28':
	$audit_log[$i]['audit_log_operation']=$audit_log[$i]['old_info'] .' '.$vis_language['TXTUNLINKEDFROM'].' '.$audit_log[$i]['new_info'];
	break;	
case '29':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTSET'].' '.$audit_log[$i]['new_info'] .' '.$vis_language['TXTLABEL'];
	break;	
case '30':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTPINNED'].' '.$audit_log[$i]['old_info'];
	break;
case '31':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTREMOVEDLABEL'].' '.$audit_log[$i]['new_info'];
	break;
case '32':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETPROPERTYUPDATED'];
	break;
case '33':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETNOTEADDED'];
	break;
case '34':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETNOTEDELETED'];
	break;
case '35':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETCOMMENTADDED'];
	break;
case '36':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETCOMMENTDELETED'];
	break;
case '37':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETPOSTUPDATED'];
	break;
case '38':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGADDEDNEWRECIPIENT'].' '.$audit_log[$i]['new_info'];
	break;
case '39':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGADDEDNEWPEOPLE'].' '.$audit_log[$i]['new_info'];
	break;
case '40':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGREMOVEDRECIPIENT'].' '.$audit_log[$i]['new_info'];
	break;
case '41':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGREMOVEDPEOPLE'].' '.$audit_log[$i]['new_info'];
	break;
case '42':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGADDEDNEWTAG'].' '.$audit_log[$i]['new_info'];
	break;
case '43':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGREMOVEDTAG'].' '.$audit_log[$i]['new_info'];
	break;
case '44':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGNEWSCHEDULETKTCRT'];
	break;
case '45':
	$audit_log[$i]['audit_log_operation']=($audit_log[$i]['old_info'] == '')?$vis_language['MSGTICKETLEVELCHANGED'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info']:$vis_language['MSGTICKETLEVELCHANGED'].' from '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
case '46':
	$audit_log[$i]['audit_log_operation']=($audit_log[$i]['old_info'] == '')?$vis_language['MSGTICKETCATEGORYCHANGED'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info']:$vis_language['MSGTICKETCATEGORYCHANGED'].' from '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
case '47':
	$audit_log[$i]['audit_log_operation']=($audit_log[$i]['old_info'] == '')?$vis_language['MSGTICKETSUBCATEGORYCHANGED'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info']:$vis_language['MSGTICKETSUBCATEGORYCHANGED'].' from '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
case '48':
	$audit_log[$i]['audit_log_operation']=($audit_log[$i]['old_info'] == '')?$vis_language['MSGTICKETITEMCHANGED'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info']:$vis_language['MSGTICKETITEMCHANGED'].' from '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
case '49':
	$audit_log[$i]['audit_log_operation']=($audit_log[$i]['old_info'] == '')?$vis_language['MSGITILLOCATIONCHANGED'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info']:$vis_language['MSGITILLOCATIONCHANGED'].' from '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
case '50':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTSLAPAUSED'];
	break;
case '51':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTSLARESUMED'];
	break;
	case '52':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTREADTICKETFORSTAFF'];
	break;
	case '53':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTSENDTICKETAPPROVAL'].' to '.$audit_log[$i]['old_info'];
	break;
	case '54':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTCHANGETICKETAPPROVALSTATUS'].' by '.$audit_log[$i]['old_info'];
	break;
	case '55':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTSENDTICKETAPPROVALREMINDER'].' to '.$audit_log[$i]['old_info'];
	break;
}
}
			
			
			
			if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
								
								
								include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
								$export = new export();
								$operators=array(1=>$vis_language['TXTCLIENT'], 2=>$vis_language['TXTSTAFF'], 3=>$vis_language['TXTSTAFF']);
								 
						foreach ($audit_log as $i)
								{
									
												foreach ($i as $key => $val) {
																if ($key == 'ticket_hash') {
																				$report_exp[$j][$key] = $val; 
																}  elseif ($key == 'entry_date') {
																				 $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
																				 
																}  elseif ($key == 'audit_log_operation') {
																				$report_exp[$j][$key] = $val;
																} 														
												} 
												
									$report_exp1[$j]['operation_date']=$report_exp[$j]['entry_date'];
									$report_exp1[$j]['ticket_hash']=$report_exp[$j]['ticket_hash'];
									$report_exp1[$j]['operation']=$report_exp[$j]['audit_log_operation'];
									$j++;
								} 
			
			 
           /* if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
				
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export     = new export();
                $operations=array(0=>$vis_language['MSGNEWTKTCRT'], 
				                  1=>$vis_language['MSGTKTRPLD'], 
								  2=>$vis_language['MSGSTSUPD'], 
								  3=>$vis_language['MSGPRIOUPD'], 
								  4=>$vis_language['MSGDEPTCHNG'], 
								  5=>$vis_language['MSGTKTTYPCHG'], 
								  6=>$vis_language['MSGFLGCHG'], 
								  7=>$vis_language['MSGTKTASSIG'], 
								  8=>$vis_language['TXTTKTUPDATE'], 
								  9=>$vis_language['MSGTKTMRGD'], 
								  10=>$vis_language['MSGTKTMRGD'], 
								  11=>$vis_language['MSGTKTFRWD'], 
								  12=>$vis_language['MSGTKTDELETED'], 
								  13=>$vis_language['MSGSLACHG'], 
								  14=>$vis_language['MSGSPAMMRK'], 
								  15=>$vis_language['MSGUNSPAM'],
								  16=>$vis_language['MSGTASKLINKED'],
								  18=>$vis_language['MSGCILINKED'],
								  20=>$vis_language['MSGPROBLEMLINKED'],
								  22=>$vis_language['MSGCHANGESLINKED'],
								  24=>$vis_language['MSGTICKETLINKED'],
								  25=>$vis_language['MSGTICKETPOSTDELETE'], 
								  26=>$vis_language['MSGTICKETSPLIT'], 
								  44=>$vis_language['MSGNEWSCHEDULETKTCRT'], 
								  45=>$vis_language['MSGTICKETLEVELCHANGED'], 
								  46=>$vis_language['MSGTICKETCATEGORYCHANGED'], 
								  47=>$vis_language['MSGTICKETSUBCATEGORYCHANGED'], 
								  48=>$vis_language['MSGTICKETITEMCHANGED'], 
								  49=>$vis_language['MSGITILLOCATIONCHANGED'],
										50=>$vis_language['TXTSLAPAUSED'],
										51=>$vis_language['TXTSLARESUMED'],
										52=>$vis_language['TXTREADTICKETFORSTAFF'],
										53=>$vis_language['TXTSENDTICKETAPPROVAL'],
										54=>$vis_language['TXTCHANGETICKETAPPROVALSTATUS'],
										55=>$vis_language['TXTSENDTICKETAPPROVALREMINDER']);
                               
					 if ($report != -1) {
                     for ($i = 0; $i < count($report); $i++) {
                        foreach ($report[$i] as $key => $val) {
							  
                            if ($key == 'ticket_hash' || $key == 'subject')
                                $report_exp[$i][$key] = $val;
                            elseif ($key == 'entry_date')
                                $report_exp[$i][$vis_language['TXTOPERATIONDATE']] = format_datetime_rpt($val,$staff_id);
                            elseif ($key == 'operation_id') {
								
                                $report_exp[$i]['operation'] = $operations[$val];
								if ($report[$i]['operation_id'] == 2)
                                    $report_exp[$i]['operation'] .= ' From ' . $report[$i]['old_info'] . ' To ' . $report[$i]['new_info'];
                                if ($report[$i]['operation_id'] == 3)
                                    $report_exp[$i]['operation'] .= ' From ' . $report[$i]['old_info'] . ' To ' . $report[$i]['new_info'];
                                if ($report[$i]['operation_id'] == 4)
                                    $report_exp[$i]['operation'] .= ' From ' . $report[$i]['old_info'] . ' To ' . $report[$i]['new_info'];
                                if ($report[$i]['operation_id'] == 7)
                                    $report_exp[$i]['operation'] .= ' To ' . $staff_list[$report[$i]['new_info']];
								if ($report[$i]['operation_id'] == 8)  
								$report_exp[$i]['operation'] .= ' ' .($report[$i]['old_info']==0?$vis_language['TXTMOVETOINBOX']:$vis_language['MSGTKTTRSH']);
								if ($report[$i]['operation_id'] == 10)
                                    $report_exp[$i]['operation'] .= ' ' . $report[$i]['old_info'];
								if ($report[$i]['operation_id'] == 14) 
								 $report_exp[$i]['operation'] .= ' '.($report[$i]['old_info']==0?$vis_language['MSGSPAMMRK']:$vis_language['MSGUNSPAM']);
							    if ($report[$i]['operation_id']==45 || $report[$i]['operation_id']==46 || $report[$i]['operation_id']==47 || $report[$i]['operation_id']==48 || $report[$i]['operation_id']==49)
									
								 $report_exp[$i]['operation'] .= ' '.($report[$i]['old_info']=='')?' to '.$report[$i]['new_info']:' from '.$report[$i]['old_info'].' to '.$report[$i]['new_info'];
                            }
                        }
                    }
					
                }*/
				
				$completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
                if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }else{
					 $database = new class_database();
					 include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
				}
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
               // echo "enter" . $operation['export_id'];
                $this->email_send_download($operation);
                
                
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'work_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
    }
				
function performance_report($operation)
{
        global $vis_language;
        $report_title = $vis_language['TXTSTAFFAVGRESPONSETIME'];
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        $export = 1;
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object       = new class_ticket($database);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
        $staff            = $tkt_object->get_staff_list($staff_id, VIS_LOCATION_AREA);
        if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                if ($data['duration'] == 0 && $data['staff_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['staff_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELSTAFFFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] == 0 && $data['staff_id'] != 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                
                $currentdate = date('Y-m-d h:i:s');
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter    = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and (submitter=1 or (submitter=0 and submitter_id=' . $data['staff_id'] . ')) and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and is_visible=1';
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    $weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter   = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and (submitter=1 or (submitter=0 and submitter_id=' . $data['staff_id'] . ')) and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and is_visible=1';
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $hour   = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and (submitter=1 or (submitter=0 and submitter_id=' . $data['staff_id'] . ')) and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and is_visible=1';
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    $from   = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to     = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $from . ' AND ' . $to . ' and (submitter=1 or (submitter=0 and submitter_id=' . $data['staff_id'] . ')) and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and is_visible=1';
                }
            }
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object       = new class_report($database);
            $total_post   = 0;
            $total_ticket = array();
            $total_time   = 0;
			$reply_count = 0;
            $filter .= ' and ' . VIS_TABLE_PREFIX . 'ticket.is_deleted!=1 and ' . VIS_TABLE_PREFIX . 'ticket.is_spam!=1';
            $post_details = $object->get_staff_performance_report($filter, $staff_id, VIS_LOCATION_AREA);
			//print_r($post_details);
            if ($post_details != -1)
                for ($i = 0; $i < count($post_details); $i++) {
					 if($post_details[$i]['reply_forward'] == 1){
					  $reply_count = $reply_count+$post_details[$i]['reply_forward'];}
					
                    if (!isset($report[$post_details[$i]['ticket_id']]['start_time'])) {
                        $report[$post_details[$i]['ticket_id']]['ticket_hash']  = $post_details[$i]['ticket_hash'];
                        $report[$post_details[$i]['ticket_id']]['subject']      = $post_details[$i]['subject'];
						$report[$post_details[$i]['ticket_id']]['first_name']      = $post_details[$i]['first_name'];
					    $report[$post_details[$i]['ticket_id']]['last_name']      = $post_details[$i]['last_name'];
					    $report[$post_details[$i]['ticket_id']]['email']      = $post_details[$i]['email'];
					    $report[$post_details[$i]['ticket_id']]['group_name']      = $post_details[$i]['group_name'];
						$report[$post_details[$i]['ticket_id']]['ticket_entry'] = $post_details[$i]['ticket_entry'];
                         $report[$post_details[$i]['ticket_id']]['start_time']   = $post_details[$i]['receive_date'];
						
                        $report[$post_details[$i]['ticket_id']]['timestamp']    = 0; //$post_details[$i]['receive_date']-$post_details[$i]['ticket_entry'];                    
                        $report[$post_details[$i]['ticket_id']]['ticket_id']    = $post_details[$i]['ticket_id'];
						$report[$post_details[$i]['ticket_id']]['count']        = 0;
						 					 
                        if ($post_details[$i]['submitter'] == 1)
                            $report[$post_details[$i]['ticket_id']]['is_posted'] = false;
                        else {
                            $report[$post_details[$i]['ticket_id']]['is_posted'] = true;
                            $total_post++;
                            $total_ticket[$post_details[$i]['ticket_id']] = $post_details[$i]['ticket_id'];
                         $total_time += $post_details[$i]['receive_date'] - $report[$post_details[$i]['ticket_id']]['start_time'];
						 	 
                        }
                    } elseif ($post_details[$i]['submitter'] == 1) {
                        $report[$post_details[$i]['ticket_id']]['start_time'] = $post_details[$i]['receive_date'];
						
                    } else {
						
                        $report[$post_details[$i]['ticket_id']]['timestamp'] += $post_details[$i]['receive_date'] - $report[$post_details[$i]['ticket_id']]['start_time'];
                        $report[$post_details[$i]['ticket_id']]['count']++;
                        $report[$post_details[$i]['ticket_id']]['is_posted'] = true;
                        $total_post++;
                        $total_ticket[$post_details[$i]['ticket_id']] = $post_details[$i]['ticket_id'];
                        $total_time += $post_details[$i]['receive_date'] - $report[$post_details[$i]['ticket_id']]['start_time'];
						 
                    }
					 
                } else
                $report = -1;
			//print_r($report);
			 if($report!=-1) {
		     foreach($report as $ticket_id => $details) {   
			 if(!$details['is_posted'])   continue;
			 $counter++;
		      }
	        }
			 if($report!=-1 && $counter!='') {	
	         $avgTimeCount =count_remain_time($total_time/$total_post) ;
			 }
			  $total_ticket_cnt= $reply_count;
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                $j      = 0;
                foreach ($report as $i) {
                    if (!$i['is_posted'])
                        continue;
                    foreach ($i as $key => $val) {
                        if ($key == 'ticket_hash' || $key == 'subject')
                            $report_exp[$j][$key] = $val;
                        elseif ($key == 'ticket_entry')
                            $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);  
                        elseif ($key == 'first_name' || $key == 'last_name' || $key == 'email' || $key == 'group_name')
						$report_exp[$j][$key] = $val;
                         elseif ($key == 'timestamp')
                            if ($val != 0 || $i['count'] != 0)
                                $report_exp[$j]['avg_response_time'] = count_remain_time($val / $i['count']);
                            else
                                $report_exp[$j]['avg_response_time'] = '0S';
                    }
					$report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash'];
					$report_exp1[$j]['subject'] = $report_exp[$j]['subject'];
					$report_exp1[$j]['client_name']= $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					$report_exp1[$j]['email']= $report_exp[$j]['email'];
					$report_exp1[$j]['organization']= $report_exp[$j]['group_name'];
					$report_exp1[$j]['creation_date'] = $report_exp[$j]['ticket_entry'];
					$report_exp1[$j]['avg_response_time'] = $report_exp[$j]['avg_response_time'];
                    $j++;
                }
				$report_exp1[$j]['total_post']=$vis_language['TXTTOTALPOSTCOUNT'].' :'.$total_post;
				$report_exp1[$j]['total_ticket_cnt']=$vis_language['TXTTOTALTICKETREPLIED'].' :'.$total_ticket_cnt.'/'.count($total_ticket);
				$report_exp1[$j]['avgTimeCount']=$vis_language['TXTTOTALAVGRESPONSETIME'].' :'.($total_post==0?'0':(($avgTimeCount > 0)?$avgTimeCount:'0S'));
				
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
                if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'performance_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
    }
function time_tracking_staff_report($operation)
{
		global $vis_language;
        $report_title = $vis_language['TXTTRACKINGSTAFF'];
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        $export      = 1;
        $report_type = 9;
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object = new class_ticket($database);
        $staff      = $tkt_object->get_staff_list(VIS_STAFF_ID, VIS_LOCATION_AREA);
        $domain     = $tkt_object->get_domain(VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
        if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1){
			 $filter = $operation['filter'];
			 $data = $operation;
			 $temp_filter = explode("domain_id=",$filter);	
			 $dom_id = $temp_filter[1];			 
			 $domain_details = $tkt_object->get_domain(VIS_TABLE_PREFIX . 'domain.domain_id=' . $dom_id, VIS_STAFF_ID, VIS_LOCATION_AREA);
			 $currency       = $domain_details[0]['currency'];
			}
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                if ($data['duration'] == 0 && $data['staff_id'] == 0 && $data['domain_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }else if ($data['duration'] != 0 && $data['staff_id'] == 0 && $data['domain_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELSTAFFFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }else if ($data['duration'] != 0 && $data['staff_id'] == 0 && $data['domain_id'] != 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELSTAFFFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }  else if ($data['duration'] == 0 && $data['staff_id'] != 0 && $data['domain_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] == 0 && $data['staff_id'] != 0 && $data['domain_id'] != 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }else if ($data['duration'] != 0 && $data['staff_id'] != 0 && $data['domain_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDOMAINFORRES'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                
               
                $domain_details = $tkt_object->get_domain(VIS_TABLE_PREFIX . 'domain.domain_id=' . $data['domain_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
				
                $currency       = $domain_details[0]['currency'];
                
                //check amount setting
                include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'setting.php');
                $object_sett    = new class_setting($database);
                $module_setting = $object_sett->get_settings(' module_id=2 and title="vis_show_time_track_amount" and domain_id=' . $domain_details[0]['domain_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
                if (empty($module_setting) || $module_setting == -1) {
                    $module_setting = $object_sett->get_settings(' module_id=2 and title="vis_show_time_track_amount" and domain_id=0', VIS_STAFF_ID, VIS_LOCATION_AREA);
                }
                $amountshowflag = 1;
                if ($module_setting != -1) {
                    $amountshowflag = $module_setting[0]['content'];
                }
                //end amount setting check
                
                $currentdate = date('Y-m-d h:i:s');
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter    = 'created_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and staff_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'domain.domain_id=' . $data['domain_id'];
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    $weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter   = 'created_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and staff_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'domain.domain_id=' . $data['domain_id'];
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $hour   = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter = 'created_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and staff_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'domain.domain_id=' . $data['domain_id'];
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    $from   = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to     = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter = 'created_date between ' . $from . ' AND ' . $to . ' and staff_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'domain.domain_id=' . $data['domain_id'];
                }
            }
            $filter .= ' AND  avis_ticket.is_deleted=0 AND avis_ticket.is_spam=0 ';
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object          = new class_report($database);
            $total_bill_time = 0;
            $total_work_time = 0;
            $total_amount    = 0;
            $post_details    = $object->get_time_tracking_staff_report($filter, $data['staff_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
            if ($post_details != -1)
                for ($i = 0; $i < count($post_details); $i++) {
                    $report[$i]['ticket_hash']     = $post_details[$i]['ticket_hash'];
                    $report[$i]['ticket_id']       = $post_details[$i]['ticket_id'];
					 $report[$i]['subject']     = $post_details[$i]['subject'];
                    $report[$i]['description']     = $post_details[$i]['description'];
					$report[$i]['first_name']     = $post_details[$i]['first_name'];
					$report[$i]['last_name']     = $post_details[$i]['last_name'];
					$report[$i]['email']     = $post_details[$i]['email'];
					$report[$i]['group_name']     = $post_details[$i]['group_name'];
                    $report[$i]['bill_time']       = $post_details[$i]['bill_time'];
                    $report[$i]['work_time']       = $post_details[$i]['work_time'];
                    $report[$i]['amount']          = $post_details[$i]['amount'];
                    $report[$i]['created_date']    = $post_details[$i]['created_date'];
                    $report[$i]['staff_name']      = $post_details[$i]['staff_name'];
                    $report[$i]['department_name'] = $post_details[$i]['department_name'];
                    $total_bill_time += $post_details[$i]['bill_time'];
                    $total_work_time += $post_details[$i]['work_time'];
                    $total_amount += $post_details[$i]['amount'];
                } else
                $report = -1;
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                $j      = 0;
				$min = $vis_language['TXTMIN'];
				foreach ($report as $i) {
                    foreach ($i as $key => $val) {
                        if ($key == 'ticket_hash')
                            $report_exp[$j][$key] = $val;
						elseif ($key == 'subject')
                            $report_exp[$j][$key] = $val;
                        elseif ($key == 'created_date')
                            $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        elseif ($key == 'work_time')
                            $report_exp[$j][$key] = $val;
                        elseif ($key == 'bill_time')
                            $report_exp[$j][$key] = $val;
                        elseif ($key == 'amount')
                            $report_exp[$j][$key] = $val;
                        elseif ($key == 'description')
                            $report_exp[$j][$key] = $val;
                        elseif ($key == 'staff_name')
                            $report_exp[$j][$key] = $val;
                        elseif ($key == 'department_name')
                            $report_exp[$j][$key] = $val;
						elseif ($key == 'first_name' || $key == 'last_name' || $key == 'email' || $key == 'group_name')
						$report_exp[$j][$key] = $val;
                    }
					 $report_exp1[$j]['ticket_hash'] =  $report_exp[$j]['ticket_hash']; 
					 $report_exp1[$j]['subject'] =  $report_exp[$j]['subject'];
					 $report_exp1[$j]['description'] =  $report_exp[$j]['description'];
                     $report_exp1[$j]['staff'] =  $report_exp[$j]['staff_name']; 
					 $report_exp1[$j]['client_name'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					 $report_exp1[$j]['email'] = $report_exp[$j]['email'];
					 $report_exp1[$j]['organization'] = $report_exp[$j]['group_name'];
					 $report_exp1[$j]['created_on'] =  $report_exp[$j]['created_date']; 
					 $report_exp1[$j]['department_name'] =  $report_exp[$j]['department_name'];
					 $report_exp1[$j]['bill_time'.' ('.$min.')'] =  $report_exp[$j]['bill_time'];
					 $report_exp1[$j]['work_time'.' ('.$min.')'] =  $report_exp[$j]['work_time'];
					 $report_exp1[$j]['amount'.'('.$currency.')'] =  $report_exp[$j]['amount'];
                     					 
                    $j++;
                }
                //$report_exp1[$j]['ticket_hash'] = '';
                $report_exp[$j]['total'] = '';
				$report_exp1[$j]['total_bill_time']   = $vis_language['TXTTOTALBILLTIME'].":". $total_bill_time . ' ' . $vis_language['TXTMIN'];
                $report_exp1[$j]['total_work_time']   = $vis_language['TXTTOTALWORKTIME'].":". $total_work_time . ' ' . $vis_language['TXTMIN'];
                $report_exp1[$j]['total_amount']      = $vis_language['TXTTOTALAMOUNT'].":".$total_amount . ' ' . $currency;
                
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
                if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                 $this->email_send_download($operation);
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'time_tracking_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
    }
				
 
	function staff_leaderboard_first_avgresponse_report($operation)
{   
		global $vis_language;
        $report_title = $vis_language['TXTSTAFFLEADERFIRSTRESPONSE'];
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        $export = 1;
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object = new class_ticket($database);
        $filter     = VIS_TABLE_PREFIX . 'domain.is_enabled=1 ';
        $domain     = $tkt_object->get_domain($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
		$staff            = $tkt_object->get_staff_list($staff_id, VIS_LOCATION_AREA); 
		$departments      = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', $staff_id, VIS_LOCATION_AREA);
		
	   if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                /***** date difference ****/
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                if ($data['duration'] == 0 && $data['domain_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['domain_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDOMAINFORRES'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] == 0 && $data['domain_id'] != 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
			 $currentdate = date('Y-m-d h:i:s');
                
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    $monthDate     = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'ticket.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) .' and avis_ticket.domain_id=' . $data['domain_id'];
                   
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    $weekDate      = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'ticket.receive_date between ' . $weekDate    . ' AND ' . strtotime($currentdate) .' and avis_ticket.domain_id=' . $data['domain_id'];
                   
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $hour          = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'ticket.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) .' and avis_ticket.domain_id=' . $data['domain_id'];
                   
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    $from          = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to            = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'ticket.receive_date between ' . $from . ' AND ' . $to . ' and avis_ticket.domain_id=' . $data['domain_id'];
                   
                }
                
            }
			//echo  $filter1 = ' and '.VIS_TABLE_PREFIX.'ticket_post.draft_id=0 and is_visible=1 and '.VIS_TABLE_PREFIX.'ticket.is_deleted!=1 and '.VIS_TABLE_PREFIX.'ticket.is_spam!=1 and (submitter=1 or (submitter=0 and submitter_id='.$response_details1['submitter_id'] .')) ';
		     
			
			
			include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object = new class_report($database);
             
			
			list($totalTicksClosed,$post_details) = $object->get_staff_leaderboard_report1($filter, VIS_STAFF_ID, VIS_LOCATION_AREA); 
			
			$report_avg      = 0;
            $report_cnt      = 0;
            $resolutionCount = $responseCount = 0;
			$totalclose      = 0;
            $replied         = array();
			$report_details  = array();
			  $avgFirstresponse=0;
			foreach($staff as $key=>$Cdetails){
				
			 $totalclosed = 0;
			 $avgFirstresponse=0;
			 $avgresponsetime=0;
			 $avgresponsesla=0;
			 $totalavg_time =$total_post=0;
			  
			$report=array();	
            foreach($totalTicksClosed as $key=>$Cntdetails)	
			{
             if($Cdetails['staff_id'] == $Cntdetails['staff_id'])
             {
				  $totalclosed=  $Cntdetails['total_ticket_count']; 
			 }	
			}	
             $Cdetails['total_ticket_count'] = $totalclosed;			
			//avg first response time
			for ($i = 0; $i < count($post_details); $i++) {
			
			if($Cdetails['staff_id'] == $post_details[$i]['submitter_id']) 
				
			
			{
			 
								 
												if (!isset($report[$post_details[$i]['ticket_id']]['start_time'])) {
																
																$report[$post_details[$i]['ticket_id']]['ticket_hash']  = $post_details[$i]['ticket_hash'];
																$report[$post_details[$i]['ticket_id']]['subject']      = $post_details[$i]['subject'];
																$report[$post_details[$i]['ticket_id']]['ticket_entry'] = $post_details[$i]['ticket_entry'];
																$report[$post_details[$i]['ticket_id']]['start_time']   = $post_details[$i]['receive_date'];
																//$report[$post_details[$i]['ticket_id']]['timestamp']=$post_details[$i]['receive_date']-$post_details[$i]['ticket_entry'];
																
																$report[$post_details[$i]['ticket_id']]['ticket_id'] = $post_details[$i]['ticket_id'];
																
																
																$report[$post_details[$i]['ticket_id']]['timestamp'] = $post_details[$i]['receive_date'] - $post_details[$i]['ticket_entry'];
																$report[$post_details[$i]['ticket_id']]['is_posted'] = true;
																$total_ticket[$post_details[$i]['ticket_id']]        = $post_details[$i]['ticket_id'];
																$total_time                                          = $report[$post_details[$i]['ticket_id']]['timestamp'];
															
															$totalavg_time+= $report[$post_details[$i]['ticket_id']]['timestamp'];
															$total_post++;
															$avgFirstresponse = ($total_post==0)?0:count_remain_time($totalavg_time/$total_post) ;
																/*if(VIS_STAFF_ID==1){if($Cdetails['staff_id']==502)echo 'avgtime--'.$totalavg_time.'<br>totalpost---'.$total_post.'<br>ttm--'.date('H:i:s',($totalavg_time/$total_post)).'------|'.$post_details[$i]['receive_date'].' - '.$post_details[$i]['ticket_entry'].'<br>dttme'.format_datetime('-41480'); }*/
												} 
												
												
												 
								
			
			
			
			    
				 
                 }
			}
			
	            $Cdetails['avgFirstresponse'] =  $avgFirstresponse;
		    //avg first response time completed
			 
			$report_details[] = $Cdetails;
		
		
	}
		//print_r($report_details);
		
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                $j = 0;
               foreach ($report_details as $i) {
                    foreach ($i as $key => $val) {
                        if ($key == 'staff_name') {
                            $report_exp[$j][$key] = $val;
                        }  
                        elseif ($key == 'total_ticket_count') {
                           $report_exp[$j][$key] = $val; 
                        } 
						elseif ($key == 'avgFirstresponse') {
                            $report_exp[$j][$key] = $val; 
                        }   
                        
                    }
					 $report_exp1[$j]['staff_name'] = $report_exp[$j]['staff_name'];
					 $report_exp1[$j]['solved_tickets'] = $report_exp[$j]['total_ticket_count'];
					 $report_exp1[$j]['avg_first_reply_time'] = ucfirst($report_exp[$j]['avgFirstresponse']);
					// $report_exp1[$j]['avg_response_time'] = $report_exp[$j]['avgresponsetime'];
					// $report_exp1[$j]['avg_sla_maintain'] = $report_exp[$j]['avgresponsesla'];
                    $j++;
                }
				//print_r($report_exp1);
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
                
				if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                
                $this->email_send_download($operation);
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'staff_leaderboard_first_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
		
	}
	
	function staff_leaderboard_avgresponse_report($operation)
		{   
		global $vis_language;
		  $report_title = $vis_language['TXTSTAFFLEADERAVGRESPONSE'];
        
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        $export = 1;
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object = new class_ticket($database);
        $filter     = VIS_TABLE_PREFIX . 'domain.is_enabled=1 ';
        $domain     = $tkt_object->get_domain($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
								$page=(isset($operation['vis_page']))?$operation['vis_page']:0;
								 if(isset($operation['vis_export']) && $operation['vis_export']==true){ $limit = '';
								}else{ $limit=($page*10).',10';  }
								
	$staff            = $tkt_object->get_staff_list($staff_id, VIS_LOCATION_AREA,$limit); 
			$staffcount      = $tkt_object->get_staff_list($staff_id, VIS_LOCATION_AREA); 
			if($staffcount!=-1){$pagecount=(count($staffcount)/10);}else{ $pagecount=0;}
			
			
		$departments      = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', $staff_id, VIS_LOCATION_AREA);
		
	   if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                /***** date difference ****/
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                if ($data['duration'] == 0 && $data['domain_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['domain_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDOMAINFORRES'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] == 0 && $data['domain_id'] != 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
			 $currentdate = date('Y-m-d h:i:s');
                
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    $monthDate     = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) .' and avis_ticket.domain_id=' . $data['domain_id'];
                   
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    $weekDate      = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) .' and avis_ticket.domain_id=' . $data['domain_id'];
                   
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $hour          = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) .' and avis_ticket.domain_id=' . $data['domain_id'];
                   
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    $from          = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to            = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $from . ' AND ' . $to . ' and avis_ticket.domain_id=' . $data['domain_id'];
                   
                }
                
            }
			//echo  $filter1 = ' and '.VIS_TABLE_PREFIX.'ticket_post.draft_id=0 and is_visible=1 and '.VIS_TABLE_PREFIX.'ticket.is_deleted!=1 and '.VIS_TABLE_PREFIX.'ticket.is_spam!=1 and (submitter=1 or (submitter=0 and submitter_id='.$response_details1['submitter_id'] .')) ';
		     
			//if(VIS_STAFF_ID==1){ echo $filter; die();}
			
			include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object = new class_report($database);
             
			
			list($totalTicksClosed) = $object->get_staff_leaderboard_report2($filter, VIS_STAFF_ID, VIS_LOCATION_AREA); 
			
			$report_avg      = 0;
            $report_cnt      = 0;
            $resolutionCount = $responseCount = 0;
			$totalclose      = 0;
            $replied         = array();
			$report_details  = array();
			$avgresponsetime=0;
			foreach($staff as $key=>$Cdetails){
				
			 $totalclosed = 0;
			 $avgFirstresponse=0;
			 $avgresponsetime=0;
			 $avgresponsesla=0;
			 $total_time =0;
			 $total_post=0; 
			 $reportsr=array(); $reportr=array();
            foreach($totalTicksClosed as $key=>$Cntdetails)	
			{
             if($Cdetails['staff_id'] == $Cntdetails['staff_id'])
             {
				  $totalclosed=  $Cntdetails['total_ticket_count']; 
			 }	
			}	
             $Cdetails['total_ticket_count'] = $totalclosed;
			//$filter1=explode('and avis_ticket.domain_id=',$filter);
			// $filters=$filter1[0].$filter_new = ' and (submitter=1 or (submitter=0 and submitter_id='.$Cdetails['staff_id'].')) and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and is_visible=1 and ' . VIS_TABLE_PREFIX . 'ticket.is_deleted!=1 and ' . VIS_TABLE_PREFIX . 'ticket.is_spam!=1';
			$filters=$filter.$filter_new = ' and (submitter=1 or (submitter=0 and submitter_id='.$Cdetails['staff_id'].')) and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and is_visible=1 and ' . VIS_TABLE_PREFIX . 'ticket.is_deleted!=1 and ' . VIS_TABLE_PREFIX . 'ticket.is_spam!=1';
			$response_details = $object->get_staff_performance_report($filters, $staff_id, VIS_LOCATION_AREA); 
			//avg response time
			for ($i = 0; $i < count($response_details); $i++) {
				//if($Cdetails['staff_id'] == $response_details[$i]['submitter_id']){
					if($reportr[$response_details[$i]['ticket_id']]['start_time'] > $response_details[$i]['receive_date']){
						$reportr[$response_details[$i]['ticket_id']]['timestamp'] += $report[$response_details[$i]['ticket_id']]['start_time'] - $response_details[$i]['receive_date'];
						$reportr[$response_details[$i]['ticket_id']]['count']++;
						$reportr[$response_details[$i]['ticket_id']]['is_posted'] = true;
						$total_post++;
						$total_time += $reportr[$response_details[$i]['ticket_id']]['start_time'] - $response_details[$i]['receive_date'];
					}
					if (!isset($reportr[$response_details[$i]['ticket_id']]['start_time'])) {
                        if($reportr[$response_details[$i]['ticket_id']]['start_time'] > $response_details[$i]['receive_date']){
							$reportr[$response_details[$i]['ticket_id']]['timestamp'] += $report[$response_details[$i]['ticket_id']]['start_time'] - $response_details[$i]['receive_date'];
							$reportr[$response_details[$i]['ticket_id']]['count']++;
							$reportr[$response_details[$i]['ticket_id']]['is_posted'] = true;
							$total_post++;
							$total_time += $reportr[$response_details[$i]['ticket_id']]['start_time'] - $response_details[$i]['receive_date'];
						}
					
						$reportr[$response_details[$i]['ticket_id']]['ticket_entry'] = $response_details[$i]['ticket_entry'];
                        $reportr[$response_details[$i]['ticket_id']]['start_time']   = $response_details[$i]['receive_date'];
						
                        if ($response_details[$i]['submitter'] == 1)
                            $reportr[$response_details[$i]['ticket_id']]['is_posted'] = false;
                        else {
                            $reportr[$response_details[$i]['ticket_id']]['is_posted'] = true;
                            $total_post++;
                            $total_ticket[$response_details[$i]['ticket_id']] = $response_details[$i]['ticket_id'];
                            $total_time += $response_details[$i]['receive_date'] - $reportr[$response_details[$i]['ticket_id']]['start_time'];
							
                        }
                    } elseif ($response_details[$i]['submitter'] == 1) {
                        $reportr[$response_details[$i]['ticket_id']]['start_time'] = $response_details[$i]['receive_date'];
						
                    } else {
						$reportr[$response_details[$i]['ticket_id']]['timestamp'] += $response_details[$i]['receive_date'] - $reportr[$response_details[$i]['ticket_id']]['start_time'];
                        $reportr[$response_details[$i]['ticket_id']]['count']++;
                        $reportr[$response_details[$i]['ticket_id']]['is_posted'] = true;
                        $total_post++;
                        $total_ticket[$response_details[$i]['ticket_id']] = $response_details[$i]['ticket_id'];
                        $total_time += $response_details[$i]['receive_date'] - $reportr[$response_details[$i]['ticket_id']]['start_time'];
                    }
				//}
            }
			$avgresponsetime = ($total_post==0)?0:count_remain_time ($total_time/$total_post);
		    $Cdetails['avgresponsetime'] =  $avgresponsetime;
		     
			 $report_details[] = $Cdetails;
			
		
		
	}
		//print_r($report_details);
		
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                $j = 0;
               foreach ($report_details as $i) {
                    foreach ($i as $key => $val) {
                        if ($key == 'staff_name') {
                            $report_exp[$j][$key] = $val;
                        }  
                        elseif ($key == 'total_ticket_count') {
                           $report_exp[$j][$key] = $val; 
                        } 
						elseif ($key == 'avgresponsetime') {
                            $report_exp[$j][$key] = $val; 
                        }   
                        
                    }
					 $report_exp1[$j]['staff_name'] = $report_exp[$j]['staff_name'];
					 $report_exp1[$j]['solved_tickets'] = $report_exp[$j]['total_ticket_count'];
					// $report_exp1[$j]['avg_response_time'] = $report_exp[$j]['avgresponsetime'];
					 $report_exp1[$j]['avg_response_time'] = ucfirst($report_exp[$j]['avgresponsetime']);
					// $report_exp1[$j]['avg_sla_maintain'] = $report_exp[$j]['avgresponsesla'];
                    $j++;
                }
				 
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
                
				if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                
                $this->email_send_download($operation);
                return true;
            } else
             if(isset($operation['vis_filter']) && $operation['vis_filter']==1){
													ob_start();
													include(VIS_PATH_TEMPLATE . 'staff_leaderborad_avgresponse_report.php');
													$rep_list = ob_get_clean();
													ob_end_flush();
													echo json_encode( array('list'=> $rep_list));
												}else{
             include(VIS_PATH_TEMPLATE . 'staff_leaderborad_avgresponse_report.php');
												}
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
		
	} 
	
	function staff_leaderboard_avgresponse_maintain_report($operation)
		{   
		global $vis_language;
		 $report_title = $vis_language['TXTSTAFFLEADERAVDSLA'];
        
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        $export = 1;
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object = new class_ticket($database);
        $filter     = VIS_TABLE_PREFIX . 'domain.is_enabled=1 ';
        $domain     = $tkt_object->get_domain($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
		$staff            = $tkt_object->get_staff_list($staff_id, VIS_LOCATION_AREA); 
		$departments      = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', $staff_id, VIS_LOCATION_AREA);
		
	   if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                /***** date difference ****/
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                if ($data['duration'] == 0 && $data['domain_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['domain_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDOMAINFORRES'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] == 0 && $data['domain_id'] != 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
			 $currentdate = date('Y-m-d h:i:s');
                
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    $monthDate     = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) .' and avis_ticket.domain_id=' . $data['domain_id'];
                   
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    $weekDate      = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) .' and avis_ticket.domain_id=' . $data['domain_id'];
                   
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $hour          = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) .' and avis_ticket.domain_id=' . $data['domain_id'];
                   
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    $from          = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to            = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $from . ' AND ' . $to . ' and avis_ticket.domain_id=' . $data['domain_id'];
                   
                }
                
            }
			//echo  $filter1 = ' and '.VIS_TABLE_PREFIX.'ticket_post.draft_id=0 and is_visible=1 and '.VIS_TABLE_PREFIX.'ticket.is_deleted!=1 and '.VIS_TABLE_PREFIX.'ticket.is_spam!=1 and (submitter=1 or (submitter=0 and submitter_id='.$response_details1['submitter_id'] .')) ';
		     
			
			
			include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object = new class_report($database);
             
			
			list($totalTicksClosed,$sla_details) = $object->get_staff_leaderboard_report3($filter, VIS_STAFF_ID, VIS_LOCATION_AREA); 
			
			$report_avg      = 0;
            $report_cnt      = 0;
            $resolutionCount = $responseCount = 0;
			$totalclose      = 0;
            $replied         = array();
			$report_details  = array();
			//foreach($staff as $key=>$Cdetails){
			
			for($a=0;$a<count($staff);$a++){	
			 $totalclosed = 0;
			 $avgFirstresponse=0;
			 $avgresponsetime=0;
			 $avgresponsesla=0;
			 $total_post=$total_posts=$total_time=0;
			$total_post1=$total_time1=0;
			$report_avg      = 0;
			$responseCount=$avgresponsesla=0;
			$reportsr=array();  
			unset($reportsr);
			$report_cnt      = 0;
			$totalposts=0;
			 
            foreach($totalTicksClosed as $key=>$Cntdetails)	
			{
             if($staff[$a]['staff_id'] == $Cntdetails['staff_id'])
             {
				  $totalclosed=  $Cntdetails['total_ticket_count']; 
			 }	
			}	
              $staff[$a]['total_ticket_count'] = $totalclosed;
		 	for ($i = 0; $i < count($sla_details); $i++) {
				if($staff[$a]['staff_id'] == $sla_details[$i]['operator_id'] || $sla_details[$i]['operator_id']==0){ 	
				if(!isset($postslaarr1[$sla_details[$i]['ticket_id']]['sla_nonmaintaint_reply']))$postslaarr1[$sla_details[$i]['ticket_id']]['sla_nonmaintaint_reply']=0;
				if(!isset($postslaarr1[$sla_details[$i]['ticket_id']]['sla_maintaint_reply']))$postslaarr1[$sla_details[$i]['ticket_id']]['sla_maintaint_reply']=0;
				if(!isset($postslaarr1[$sla_details[$i]['ticket_id']]['ticket_id']))$postslaarr1[$sla_details[$i]['ticket_id']]['ticket_id']=$sla_details[$i]['ticket_id'];
				 				
			if($sla_details[$i]['operation_id']==0){
				$postsla1[$sla_details[$i]['ticket_id']]['post_date']=$sla_details[$i]['entry_date'];
			}else if($sla_details[$i]['operation_id']==50){
				$postsla1[$sla_details[$i]['ticket_id']]['pasue_date']=$sla_details[$i]['entry_date'];
			}else if($sla_details[$i]['operation_id']==51){
				$postsla1[$sla_details[$i]['ticket_id']]['resume_date']=$sla_details[$i]['entry_date'];
				$postsla1[$sla_details[$i]['ticket_id']]['sla_repa_date']=$postsla1[$sla_details[$i]['ticket_id']]['sla_repa_date'] + ($postsla1[$sla_details[$i]['ticket_id']]['resume_date'] - $postsla1[$sla_details[$i]['ticket_id']]['pasue_date']);
				$postsla1[$sla_details[$i]['ticket_id']]['resume_date']='';
				$postsla1[$sla_details[$i]['ticket_id']]['pasue_date']='';
			}else if($sla_details[$i]['operation_id']==1 && $sla_details[$i]['operator']!=1){
				$postsla1[$sla_details[$i]['ticket_id']]['maintaint_date']=($sla_details[$i]['entry_date'] - $postsla1[$sla_details[$i]['ticket_id']]['post_date']) - ($postsla1[$sla_details[$i]['ticket_id']]['sla_repa_date']);
				$postslaarr1[$sla_details[$i]['ticket_id']]['sla_maintaint_reply']++;
				if($sla_details[$i]['old_info'] > $postsla1[$sla_details[$i]['ticket_id']]['maintaint_date']){
					$postslaarr1[$sla_details[$i]['ticket_id']]['sla_maintaint_reply']++;
					$postslaarr1[$sla_details[$i]['ticket_id']]['ticket_id']=$sla_details[$i]['ticket_id'];
					$postslaarr1[$sla_details[$i]['ticket_id']]['operator_id']=$sla_details[$i]['operator_id'];
					$postsla1[$sla_details[$i]['ticket_id']]['sla_repa_date']='';
					$postsla1[$sla_details[$i]['ticket_id']]['maintaint_date']='';
					$postslaarr1[$sla_details[$i]['ticket_id']]['sla_nonmaintaint_reply']=$postslaarr1[$sla_details[$i]['ticket_id']]['sla_nonmaintaint_reply'];
					$postslaarr1[$sla_details[$i]['ticket_id']]['sla_nonmaintaint_reply']++;
				}else{
					$postslaarr1[$sla_details[$i]['ticket_id']]['sla_nonmaintaint_reply']++;
					$postslaarr1[$sla_details[$i]['ticket_id']]['ticket_id']=$sla_details[$i]['ticket_id'];
					$postslaarr1[$sla_details[$i]['ticket_id']]['sla_maintaint_reply']=$postslaarr1[$sla_details[$i]['ticket_id']]['sla_maintaint_reply'];
					$postslaarr1[$sla_details[$i]['ticket_id']]['operator_id']=$sla_details[$i]['operator_id'];
				}
			}
			}
		}
		$staff[$a]['totady_relycount']=0;
$staff[$a]['totady_ticketcount']=0;
foreach($postslaarr1 as $ticketids=>$details1){
if($staff[$a]['staff_id']==$details1['operator_id'] || $details1['operator_id']==0){	
		  
			if($details1['sla_maintaint_reply']>0 ){
			  $staff[$a]['totady_relycount']++; 
			}  	
		 $staff[$a]['totady_ticketcount']++;  
		 
	}
	} 	 
		//$avgresponsesla1 = (($report_cnt1==0) ? 0 : round((($responseCount1/count($reportsr))*100))).'%';
		    $staff[$a]['avgresponsesla'] = (($staff[$a]['totady_ticketcount']==0) ? 0 :round((($staff[$a]['totady_relycount']/$staff[$a]['totady_ticketcount'])*100))).'%';
			//avg response sla ends here
			
			$report_details[] = $staff[$a];
 
	}
		//print_r($report_details);
		
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                $j = 0;
               foreach ($report_details as $i) {
                    foreach ($i as $key => $val) {
                        if ($key == 'staff_name') {
                            $report_exp[$j][$key] = $val;
                        }  
                        elseif ($key == 'total_ticket_count') {
                           $report_exp[$j][$key] = $val; 
                        } 
						elseif ($key == 'avgresponsesla') {
                            $report_exp[$j][$key] = $val; 
                        }   
                        
                    }
					 $report_exp1[$j]['staff_name'] = $report_exp[$j]['staff_name'];
					 $report_exp1[$j]['solved_tickets'] = $report_exp[$j]['total_ticket_count'];
					 $report_exp1[$j]['avg_sla_maintain'] = ucfirst($report_exp[$j]['avgresponsesla']);
                    $j++;
                }
				 
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
																				
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
                
				if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                
                $this->email_send_download($operation);
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'staff_leaderborad_avgresponse_maintain.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
		
	}
	
//billing report
function billing_report($operation)
{
        global $vis_language;
        $report_title = $vis_language['TXTBILLINGSUMMARY'];
        $export       = 1;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object       = new class_ticket($database);
        $filter           = VIS_TABLE_PREFIX . 'domain.is_enabled=1';
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
        $domain           = $tkt_object->get_domain($filter, $staff_id, VIS_LOCATION_AREA);
        if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                $bfilter = $operation['filter'];
                $filter  = 'avis_client.domain_id=' . $operation['department_id'];
            } else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                
                if ($data['duration'] == 0 && $data['domain_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['domain_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDOMAINFORRES'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] == 0 && $data['domain_id'] != 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                $filter = 'avis_client.domain_id=' . $data['domain_id'];
                
                
                $currentdate = date('Y-m-d h:i:s');
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $bfilter   = ' entry_date between ' . $monthDate . ' AND ' . strtotime($currentdate);
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    $weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
                    $bfilter  = ' entry_date between ' . $weekDate . ' AND ' . strtotime($currentdate);
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $hour    = strtotime("-1 day ") - VIS_TIMEZONE;
                    $bfilter = ' entry_date between ' . $hour . ' AND ' . strtotime($currentdate);
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    $from    = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to      = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $bfilter = ' entry_date between ' . $from . ' AND ' . $to;
                }
            }
            $filter .= ' AND ' . VIS_TABLE_PREFIX . 'client.is_spam=0 AND ' . VIS_TABLE_PREFIX . 'client.is_banned=0 '; // reject ban/spam report
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object = new class_report($database);
            $report = $object->get_billing_report($filter, $bfilter, VIS_STAFF_ID, VIS_LOCATION_AREA);
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
				$j = 0;
                foreach ($report as $i) {
                    foreach ($i as $key => $val) {
						if($key == 'first_name'){
						$report_exp[$j][$key] = $val;
						}
						elseif($key == 'last_name'){
						$report_exp[$j][$key] = $val;
						}
						elseif($key == 'email'){
						$report_exp[$j][$key] = $val;
						}
						elseif($key == 'group_name'){
						$report_exp[$j][$key] = $val;
						}
						elseif($key == 'contact_number'){
						$report_exp[$j][$key] = $val;
						}
						elseif($key == 'client_type'){
						$report_exp[$j][$key] = ($val==0)?$vis_language['TXTEMAILCLIENT']:$vis_language['TXTCALLCLIENT'];
						}
						elseif($key == 'credit'){
						$report_exp[$j][$key] = $val;
						}
						elseif($key == 'paid_count'){
						$report_exp[$j][$key] = $val;
						}
						elseif($key == 'unpaid_count'){
						$report_exp[$j][$key] = $val;
						}
						elseif($key == 'cancel_count'){
						$report_exp[$j][$key] = $val;
						}
						
                        
                    }
					$report_exp1[$j]['client'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					$report_exp1[$j]['email'] = $report_exp[$j]['email'];
					$report_exp1[$j]['organization'] = $report_exp[$j]['group_name'];
					$report_exp1[$j]['client_type'] = $report_exp[$j]['client_type'];
					$report_exp1[$j]['contact_number'] = $report_exp[$j]['contact_number'];
					$report_exp1[$j]['credit'.' ('.$report[0]['currency'].')'] = $report_exp[$j]['credit'];
					$report_exp1[$j]['total_invoice']=($report_exp[$j]['paid_count']+$report_exp[$j]['unpaid_count']+$report_exp[$j]['cancel_count']);
					$report_exp1[$j]['paid'] = $report_exp[$j]['paid_count'];
					$report_exp1[$j]['unpaid'] = $report_exp[$j]['unpaid_count'];
					$report_exp1[$j]['cancelled'] = $report_exp[$j]['cancel_count'];
					
					  $j++;
                    }
                
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
                if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'billing_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
    }
				
function billing_client_report($operation) 
{
        global $vis_language;
        $report_title = $vis_language['TXTCLIENTINVOICESREPORT'];
        $export       = 1;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
        if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                $filter   = $operation['filter'];
                $domainId = $operation['department_id'];
            } else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                
                if ($data['duration'] == 0 && $data['vis_client_id'] == '') {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['vis_client_id'] == '') {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['MSGSELCLIENT'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] == 0 && $data['vis_client_id'] != '') {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                
                
                $currentdate = date('Y-m-d h:i:s');
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter    = ' entry_date between ' . $monthDate . ' AND ' . strtotime($currentdate);
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    $weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter   = ' entry_date between ' . $weekDate . ' AND ' . strtotime($currentdate);
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $hour   = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter = ' entry_date between ' . $hour . ' AND ' . strtotime($currentdate);
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    $from   = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to     = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter = ' entry_date between ' . $from . ' AND ' . $to;
                }
                $filter .= ' AND ' . VIS_TABLE_PREFIX . 'invoice.client_id=' . $data['vis_client_id'] . ' AND ' . VIS_TABLE_PREFIX . 'client.domain_id=' . $data['domain_id'];
                $domainId = $data['domain_id'];
            }
            $filter .= ' AND ' . VIS_TABLE_PREFIX . 'client.is_spam=0 AND ' . VIS_TABLE_PREFIX . 'client.is_banned=0  '; // reject ban/spam clienet report
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'setting.php');
            $setting = new class_setting($database);
            list($dominfo, $domain) = $setting->get_domain(VIS_TABLE_PREFIX . 'domain.domain_id=' . $domainId, '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
            
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'billing.php');
            $billing  = new class_billing($database);
            $invoices = $billing->get_invoice($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
         
		  //echo "<pre>";print_r($domain);//die('--hi');
			
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                
                include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                $object_report = new class_report($database);
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export         = new export();
                $invoice_status = array(
                    0 => $vis_language['TXTUNPAID'],
                    1 => $vis_language['TXTPAID'],
                    2 => $vis_language['TXTCANCELED'],
                    3 => $vis_language['TXTINPROCESS']
                );
																$j=0;
																$total=0;$paid=0;$unpaid=0;
                for ($i = 0; $i < count($invoices); $i++) {
                    foreach ($invoices[$i] as $key => $val) {
                        if ($key == 'first_name' || $key == 'last_name' || $key == 'email' || $key == 'group_name' || $key == 'total' || $key == 'invoice_hash' || $key == 'entry_date' || $key == 'due_date' || $key == 'invoice_status' || $key == 'paid_date') {
                            if ($key == 'entry_date' || $key == 'due_date' || $key == 'paid_date')
                                $report_exp[$i][$key] = format_datetime_rpt($val,$satff_id);
							else if ($key == 'invoice_status')
                                $report_exp[$i][$key] = $invoice_status[$val];
                            else
                                $report_exp[$i][$key] = $val;
                        }
                    }
					 
					$report_exp1[$j]['invoice_hash'] = $report_exp[$i]['invoice_hash'];
					$report_exp1[$j]['client_name'] = $report_exp[$i]['first_name'].' '.$report_exp[$i]['last_name'];
					$report_exp1[$j]['email'] = $report_exp[$i]['email'];
					$report_exp1[$j]['organization'] = $report_exp[$i]['group_name'];
					$report_exp1[$j]['create_date'] = $report_exp[$i]['entry_date'];
					$report_exp1[$j]['due_date'] = $report_exp[$i]['due_date'];
					if($invoices[$i]['paid_date']!=0){
						$report_exp1[$j]['paid_date'] = $report_exp[$i]['paid_date'];
					}else{
						$report_exp1[$j]['paid_date'] = '---';
					}
					$report_exp1[$j]['total'.'('.$domain[0]['currency'].')'] = $report_exp[$i]['total'];
					$report_exp1[$j]['status'] = $report_exp[$i]['invoice_status'];
					
					$j++;
																				
		            if($invoices[$i]['invoice_status'] == 0)$unpaid +=$invoices[$i]['total'];
					else if($invoices[$i]['invoice_status'] == 1)$paid +=$invoices[$i]['total'];
					$total +=$invoices[$i]['total'];
																				
                }
				$report_exp1[$j]['total_amount']=$vis_language['TXTTOTAL'].':'.$total.' '.$domain[0]['currency'];
				$report_exp1[$j]['total_paid']=$vis_language['TXTTOTALPAID'].':'.$paid.' '.$domain[0]['currency'];
				$report_exp1[$j]['total_unpaid']=$vis_language['TXTTOTALUNPAID'].':'.$unpaid.' '.$domain[0]['currency'];
				
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
                if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object_report->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'billing_client_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
    }
				
function billing_invoice_report($operation)
{ 
        global $vis_language;
        $report_title = $vis_language['TXTBILLINGINVOICEREPORT'];
        $export       = 1;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object       = new class_ticket($database);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
        $departments      = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', $staff_id, VIS_LOCATION_AREA);
		
		$domain_details = $tkt_object->get_domain(VIS_TABLE_PREFIX . 'domain.is_enabled = 1', $creater_staff_id, VIS_LOCATION_AREA);
		//print_r($domain_details);
			 $currency       = $domain_details[0]['currency'];
			  
        if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                $filter = $operation['filter'];
            } else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                
                if ($data['duration'] == 0 && $data['department_id'] == -1 && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['department_id'] == -1 && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    
                    return false;
                } else if ($data['duration'] != 0 && $data['department_id'] == -1 && $data['status_id'] != -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    
                    return false;
                } else if ($data['duration'] == 0 && $data['department_id'] == -1 && $data['status_id'] != -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    
                    return false;
                }
                
                else if ($data['duration'] == 0 && $data['department_id'] != -1 && $data['status_id'] != -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    
                    return false;
                } else if ($data['duration'] == 0 && $data['department_id'] != -1 && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    
                    return false;
                } else if ($data['duration'] != 0 && $data['department_id'] != -1 && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTBILLINGSTATUSSEL']." ".$vis_language['TXTBILLINGSTATUS'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['department_id'] == -1 && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTBILLINGSTATUSSEL']." ".$vis_language['TXTBILLINGSTATUS'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                
                $currentdate = date('Y-m-d h:i:s');
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    
                    $department_id = ($data['department_id'] == 0) ? '' : $data['department_id'];
                    $status_id     = ($data['status_id'] == 3) ? '' : $data['status_id'];
                    
                    $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter    = VIS_TABLE_PREFIX . 'invoice.entry_date between  ' . $monthDate . ' AND ' . strtotime($currentdate);
                    $filter .= ($data['department_id'] != 0) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
                    $filter .= ($data['status_id'] == 3) ? '' : ' and avis_invoice.invoice_status=' . $data['status_id'];
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    
                    $department_id = ($data['department_id'] == 0) ? '' : $data['department_id'];
                    $status_id     = ($data['status_id'] == 3) ? '' : $data['status_id'];
                    
                    $weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter   = VIS_TABLE_PREFIX . 'invoice.entry_date between ' . $weekDate . ' AND ' . strtotime($currentdate);
                    $filter .= ($data['department_id'] != 0) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
                    $filter .= ($data['status_id'] == 3) ? '' : ' and avis_invoice.invoice_status=' . $data['status_id'];
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $department_id = ($data['department_id'] == 0) ? '' : $data['department_id'];
                    $status_id     = ($data['status_id'] == 3) ? '' : $data['status_id'];
                    $hour          = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'invoice.entry_date between ' . $hour . ' AND ' . strtotime($currentdate);
                    $filter .= ($data['department_id'] != 0) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
                    $filter .= ($data['status_id'] == 3) ? '' : ' and avis_invoice.invoice_status=' . $data['status_id'];
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    $department_id = ($data['department_id'] == 0) ? '' : $data['department_id'];
                    $status_id     = ($data['status_id'] == 3) ? '' : $data['status_id'];
                    $from          = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to            = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'invoice.entry_date between ' . $from . ' AND ' . $to;
                    $filter .= ($data['department_id'] != 0) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
                    $filter .= ($data['status_id'] == 3) ? '' : ' and avis_invoice.invoice_status=' . $data['status_id'];
                }
            }
            
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object = new class_report($database);
            $report = $object->get_billing_invoice_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA); 
           $total=0;$paid=0;$unpaid=0;$cancelled=0;
		   foreach ($report as $ticket_id => $details) {
            $total += $details['amount'];
			if($details['invoice_status'] == 0)$unpaid +=$details['amount'];
			else if($details['invoice_status'] == 1)$paid +=$details['amount'];
			else if($details['invoice_status'] == 2)$cancelled +=$details['amount'];
            }
            
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                $j      = 0;
				 
                foreach ($report as $ticket_id => $i) {
					 
                    foreach ($i as $key => $val) {
                        if ($key == 'invoice_status') {
                            $report_exp[$j][$key] = $val;
							if($val==0)
							{
								$report_exp[$j][$key] = $vis_language['TXTUNPAID'];
							}elseif($val==1)
							{
								$report_exp[$j][$key] = $vis_language['TXTPAID'];
							}elseif($val==2)
							{
								$report_exp[$j][$key] = $vis_language['TXTCANCELED']; 
							}elseif($val==3)
							{
								$report_exp[$j][$key] = $vis_language['TXTINPROCESS'];
							}
                        } elseif ($key == 'invoice_hash') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'entry_date') {
                           
							$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        } elseif ($key == 'first_name') {
                            $report_exp[$j][$key] = $val;
                        } 
						 elseif ($key == 'last_name') {
                            $report_exp[$j][$key] = $val;
                        }elseif ($key == 'email') {
                            $report_exp[$j][$key] = $val;
                        }elseif ($key == 'group_name') {
                            $report_exp[$j][$key] = $val;
                        }elseif ($key == 'due_date') {
                          
							$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        } elseif ($key == 'paid_date') {
                           
							$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
						} elseif ($key == 'ticket_hash') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'amount') {
                            $report_exp[$j][$key] = $val;
                        }
                        
                    }
					$report_exp1[$j]['reference_ticket']=$report_exp[$j]['ticket_hash'];
					$report_exp1[$j]['client_name']=$report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					$report_exp1[$j]['email']=$report_exp[$j]['email'];
					$report_exp1[$j]['organization']=$report_exp[$j]['group_name'];
				    $report_exp1[$j]['invoice_hash']=$report_exp[$j]['invoice_hash'];
					$report_exp1[$j]['invoice_date']=$report_exp[$j]['entry_date'];
					$report_exp1[$j]['due_date']=$report_exp[$j]['due_date'];
					$report_exp1[$j]['paid_date']=$report_exp[$j]['paid_date'];
					$report_exp1[$j]['amount'.'('.$currency.')']=$report_exp[$j]['amount'];
					$report_exp1[$j]['paid_status']=$report_exp[$j]['invoice_status'];
					
                    $j++;
					
                }
                $report_exp [$j]['total']=$vis_language['TXTTOTAL'].' :'.$total .' '. $currency;
				$report_exp [$j]['paid']=$vis_language['TXTTOTALPAID'].' :'.$paid .' '. $currency;
				$report_exp [$j]['unpaid']=$vis_language['TXTTOTALUNPAID'].' :'.$unpaid .' '. $currency;
				$report_exp [$j]['cancelled']=$vis_language['TXTTOTALCANCELLED'].' :'.$cancelled .' '. $currency;
				 
				$report_exp1[$j]['total']= $report_exp[$j]['total'];
				$report_exp1[$j]['paid']= $report_exp[$j]['paid'];
				$report_exp1[$j]['unpaid']=$report_exp[$j]['unpaid'];
				$report_exp1[$j]['cancelled']=$report_exp[$j]['cancelled']; 
					
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
																
				if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'invoice_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
    }
				
function order_report($operation)
{
        
        global $vis_language;
        $report_title = $vis_language['TXTORDERREPORT'];
        $export       = 1;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object       = new class_ticket($database);
        $creater_staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
        $departments      = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', $staff_id, VIS_LOCATION_AREA);
		$domain_details = $tkt_object->get_domain(VIS_TABLE_PREFIX . 'domain.is_enabled = 1', $staff_id, VIS_LOCATION_AREA);
		
	     $currency       = $domain_details[0]['currency'];
        if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                $filter = $operation['filter'];
                
            } else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                
                if ($data['duration'] == 0 && $data['department_id'] == -1 && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['department_id'] == -1 && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['department_id'] == -1 && $data['status_id'] != -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] == 0 && $data['department_id'] == -1 && $data['status_id'] != -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] == 0 && $data['department_id'] != -1 && $data['status_id'] != -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] == 0 && $data['department_id'] != -1 && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['department_id'] != -1 && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTBILLINGSTATUSSEL']." ".$vis_language['TXTSTATUS'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['department_id'] == -1 && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTBILLINGSTATUSSEL']." ".$vis_language['TXTSTATUS'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                
                $currentdate = date('Y-m-d h:i:s');
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    
                    $department_id = ($data['department_id'] == 0) ? '' : $data['department_id'];
                    $status_id     = ($data['status_id'] == 4) ? '' : $data['status_id'];
                    $monthDate     = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'order.entry_date between  ' . $monthDate . ' AND ' . strtotime($currentdate);
                    $filter .= ($data['department_id'] != 0) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
                    $filter .= ($data['status_id'] == 4) ? '' : ' and avis_invoice.invoice_status=' . $data['status_id'];
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    
                    $department_id = ($data['department_id'] == 0) ? '' : $data['department_id'];
                    $status_id     = ($data['status_id'] == 4) ? '' : $data['status_id'];
                    
                    $weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter   = VIS_TABLE_PREFIX . 'order.entry_date between ' . $weekDate . ' AND ' . strtotime($currentdate);
                    $filter .= ($data['department_id'] != 0) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
                    $filter .= ($data['status_id'] == 4) ? '' : ' and avis_invoice.invoice_status=' . $data['status_id'];
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $department_id = ($data['department_id'] == 0) ? '' : $data['department_id'];
                    $status_id     = ($data['status_id'] == 3) ? '' : $data['status_id'];
                    $hour          = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'order.entry_date between ' . $hour . ' AND ' . strtotime($currentdate);
                    $filter .= ($data['department_id'] != 0) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
                    $filter .= ($data['status_id'] == 4) ? '' : ' and avis_invoice.invoice_status=' . $data['status_id'];
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    $department_id = ($data['department_id'] == 0) ? '' : $data['department_id'];
                    $status_id     = ($data['status_id'] == 3) ? '' : $data['status_id'];
                    $from          = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to            = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'order.entry_date between ' . $from . ' AND ' . $to;
                    $filter .= ($data['department_id'] != 0) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
                    $filter .= ($data['status_id'] == 4) ? '' : ' and avis_invoice.invoice_status=' . $data['status_id'];
                }
            }
            
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object = new class_report($database);
            
            $report = $object->get_order_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
            $total=0;$paid=0;$unpaid=0;$cancelled=0;
			foreach ($report as $ticket_id => $details) {
            $total += $details['amount'];
			if($details['invoice_status'] == 0)$unpaid +=$details['amount'];
            else if($details['invoice_status'] == 1)$paid +=$details['amount'];
			else if($details['invoice_status'] == 2)$cancelled +=$details['amount'];
                
            }
		
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                $j      = 0;
                foreach ($report as $ticket_id => $i) {
                    foreach ($i as $key => $val) {
                        if ($key == 'invoice_hash') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'entry_date') {
                          
							$report_exp[$j][$key] = format_datetime_rpt($val,$creater_staff_id);
                        } elseif ($key == 'first_name') {
                            $report_exp[$j][$key] = $val;
                        } 
						elseif ($key == 'last_name') {
                            $report_exp[$j][$key] = $val;
                        }  elseif ($key == 'email') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'group_name') {
                            $report_exp[$j][$key] = $val;
                        }
						elseif ($key == 'paid_date') {
                        
							$report_exp[$j][$key] = format_datetime_rpt($val,$creater_staff_id);
                        } elseif ($key == 'amount') {
                            $report_exp[$j][$key] = $val;
				     	} elseif ($key == 'order_id') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'invoice_status') {
                            $report_exp[$j][$key] = $val;
						if($val==0)
							{
							 $report_exp[$j][$key] = $vis_language['TXTUNPAID'];
						}elseif($val==1)
							{
							 $report_exp[$j][$key] = $vis_language['TXTPAID'];
						}elseif($val==2)
							{
							 $report_exp[$j][$key] = $vis_language['TXTCANCELED']; 
						}elseif($val==3)
							{
							 $report_exp[$j][$key] = $vis_language['TXTINPROCESS'];
							}
                        }
						elseif ($key == 'order_status') {
						     $report_exp[$j][$key] = $val;
                             if($val==1)
							{
							 $report_exp[$j][$key] = $vis_language['TXTACTIVE'];
						}elseif($val==2)
							{
							 $report_exp[$j][$key] = $vis_language['TXTPENDING'];
						}elseif($val==3)
							{
							 $report_exp[$j][$key] = $vis_language['TXTINCOMPLETE']; 
						}elseif($val==4)
							{
							 $report_exp[$j][$key] = $vis_language['TXTCANCELED'];
							}							 
						}
                        
                    }
																				
										$report_exp1[$j]['order_id']=$report_exp[$j]['order_id'];		
										$report_exp1[$j]['invoice_hash']=$report_exp[$j]['invoice_hash'];
										$report_exp1[$j]['client_name']=$report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
										$report_exp1[$j]['email']=$report_exp[$j]['email'];
										$report_exp1[$j]['organization']=$report_exp[$j]['group_name'];
										$report_exp1[$j]['paid_date']=$report_exp[$j]['paid_date'];
										$report_exp1[$j]['amount'.'('.$currency.')']=$report_exp[$j]['amount'];
										$report_exp1[$j]['payment_status']=$report_exp[$j]['invoice_status'];
										$report_exp1[$j]['status']=$report_exp[$j]['order_status'];
																		
																				
																				
                    $j++;
                }
				
				$report_exp1[$j]['total']=$vis_language['TXTTOTAL'].' :'.$total.' '. $currency;
				$report_exp1[$j]['paid']=$vis_language['TXTTOTALPAID'].' :'.$paid.' '. $currency;
				$report_exp1[$j]['unpaid']=$vis_language['TXTTOTALUNPAID'].' :'.$unpaid.' '. $currency;
				$report_exp1[$j]['cancelled']=$vis_language['TXTTOTALCANCELLED'].' :'.$cancelled.' '. $currency;
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
																
																if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'order_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
    }
				
function transaction_report($operation)
{
        global $vis_language;
        $report_title = $vis_language['TXTTRANSACTIONREPORT'];
        $export       = 1;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object = new class_ticket($database);
        
        $creater_staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
        $departments      = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', $staff_id, VIS_LOCATION_AREA);
        $domain_details = $tkt_object->get_domain(VIS_TABLE_PREFIX . 'domain.is_enabled = 1', $staff_id, VIS_LOCATION_AREA);
		
	     $currency       = $domain_details[0]['currency'];
        
        if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                $filter = $operation['filter'];
            } else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                
                if ($data['duration'] == 0 && $data['department_id'] == -1 && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['department_id'] == -1 && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    
                    return false;
                } else if ($data['duration'] != 0 && $data['department_id'] == -1 && $data['status_id'] != -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    
                    return false;
                } else if ($data['duration'] == 0 && $data['department_id'] == -1 && $data['status_id'] != -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    
                    return false;
                }
                
                else if ($data['duration'] == 0 && $data['department_id'] != -1 && $data['status_id'] != -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    
                    return false;
                } else if ($data['duration'] == 0 && $data['department_id'] != -1 && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    
                    return false;
                } else if ($data['duration'] != 0 && $data['department_id'] != -1 && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTBILLINGSTATUSSEL']." ".$vis_language['TXTSTATUS'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['department_id'] == -1 && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTBILLINGSTATUSSEL']." ".$vis_language['TXTSTATUS'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                
                $currentdate = date('Y-m-d h:i:s');
                
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    
                    $department_id = ($data['department_id'] == 0) ? '' : $data['department_id'];
                    $status_id     = ($data['status_id'] == 4) ? '' : $data['status_id'];
                    
                    $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter    = VIS_TABLE_PREFIX . 'balance.entry_date between  ' . $monthDate . ' AND ' . strtotime($currentdate);
                    $filter .= ($data['department_id'] != 0) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
                    $filter .= ($data['status_id'] == 4) ? '' : ' and avis_invoice.invoice_status=' . $data['status_id'];
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    
                    $department_id = ($data['department_id'] == 0) ? '' : $data['department_id'];
                    $status_id     = ($data['status_id'] == 4) ? '' : $data['status_id'];
                    
                    $weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter   = VIS_TABLE_PREFIX . 'balance.entry_date between ' . $weekDate . ' AND ' . strtotime($currentdate);
                    $filter .= ($data['department_id'] != 0) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
                    $filter .= ($data['status_id'] == 4) ? '' : ' and avis_invoice.invoice_status=' . $data['status_id'];
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $department_id = ($data['department_id'] == 0) ? '' : $data['department_id'];
                    $status_id     = ($data['status_id'] == 3) ? '' : $data['status_id'];
                    $hour          = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'balance.entry_date between ' . $hour . ' AND ' . strtotime($currentdate);
                    $filter .= ($data['department_id'] != 0) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
                    $filter .= ($data['status_id'] == 4) ? '' : ' and avis_invoice.invoice_status=' . $data['status_id'];
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    $department_id = ($data['department_id'] == 0) ? '' : $data['department_id'];
                    $status_id     = ($data['status_id'] == 3) ? '' : $data['status_id'];
                    $from          = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to            = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter        = VIS_TABLE_PREFIX . 'balance.entry_date between ' . $from . ' AND ' . $to;
                    $filter .= ($data['department_id'] != 0) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
                    $filter .= ($data['status_id'] == 4) ? '' : ' and avis_invoice.invoice_status=' . $data['status_id'];
                }
            }
            
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object = new class_report($database);
            
            $report = $object->get_transcation_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
            
            foreach ($report as $ticket_id => $details) {
                $total += $details['amount'];
			 }
            
            
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                $j      = 0;
                foreach ($report as $ticket_id => $i) {
                    foreach ($i as $key => $val) {
                        if ($key == 'transaction_id') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'invoice_status') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'invoice_hash') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'description') {
                            $report_exp[$j][$key] = str_replace(array("\n","\r\n","\r"), ' ', $val);
                        } elseif ($key == 'first_name') {
                            $report_exp[$j][$key] = $val;
                        } 
						elseif ($key == 'last_name') {
                            $report_exp[$j][$key] = $val;
                        }
						elseif ($key == 'email') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'group_name') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'entry_date') {
                           
							$report_exp[$j][$key] = format_datetime_rpt($val,$creater_staff_id);
                        } elseif ($key == 'amount') {
                            $report_exp[$j][$key] = $val;
                        }
                    }
					$report_exp1[$j]['invoice_hash'] = $report_exp[$j]['invoice_hash'];
					$report_exp1[$j]['client_name'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					$report_exp1[$j]['email'] = $report_exp[$j]['email'];
					$report_exp1[$j]['organization'] = $report_exp[$j]['group_name'];
					$report_exp1[$j]['description'] = $report_exp[$j]['description'];
					$report_exp1[$j]['transaction_id'] = $report_exp[$j]['transaction_id'];
					
					if($report_exp[$j]['invoice_status']==0){$status = $vis_language['TXTUNPAID'];}
					elseif($report_exp[$j]['invoice_status']==1){$status = $vis_language['TXTPAID'];}
					elseif($report_exp[$j]['invoice_status']==2){$status = $vis_language['TXTCANCELED'];}
					elseif($report_exp[$j]['invoice_status']==3){$status = $vis_language['TXTINPROGRESS'];}
					elseif($report_exp[$j]['invoice_status']==4){$status = 'All';}
					else{$status='---';}
					
					$report_exp1[$j]['status'] = $status;
					
					//$report_exp1[$j]['create_date'] = $report_exp[$j]['entry_date'];
					$report_exp1[$j]['transaction_date'] = $report_exp[$j]['entry_date'];
					$report_exp1[$j]['amount'.' ('.$currency.')'] = $report_exp[$j]['amount'];
					
                    $j++;
                }
                
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
																
																if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
																
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'transaction_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
      
    }
				
	function client_order_report($operation)
	{
        
        global $vis_language;
        $report_title = $vis_language['TXTCLIENTORDERREPORT'];
        $export       = 1;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object = new class_ticket($database);
		$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
		$domain_details = $tkt_object->get_domain(VIS_TABLE_PREFIX . 'domain.is_enabled = 1', $staff_id, VIS_LOCATION_AREA);
		
	     $currency       = $domain_details[0]['currency'];
        if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                $filter = $operation['filter'];
            } else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                
                if ($data['duration'] == 0 && $data['client_id'] == '' && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['vis_client_id'] == '' && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['MSGSELCLIENT'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                    
                } else if ($data['duration'] != 0 && $data['vis_client_id'] == '' && $data['status_id'] != -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['MSGSELCLIENT'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                    
                } else if ($data['duration'] == 0 && $data['vis_client_id'] == '' && $data['status_id'] != -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    
                    return false;
                }
                
                else if ($data['duration'] == 0 && $data['vis_client_id'] != '' && $data['status_id'] != -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    
                    return false;
                } else if ($data['duration'] == 0 && $data['vis_client_id'] != '' && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    
                    
                    return false;
                } else if ($data['duration'] != 0 && $data['vis_client_id'] != '' && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTBILLINGSTATUSSEL']." ".$vis_language['TXTSTATUS'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['vis_client_id'] == '' && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTBILLINGSTATUSSEL']." ".$vis_language['TXTSTATUS'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                
                $currentdate = date('Y-m-d h:i:s');
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    $status_id = ($data['status_id'] == 4) ? '' : $data['status_id'];
                    $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter    = VIS_TABLE_PREFIX . 'invoice.entry_date between  ' . $monthDate . ' AND ' . strtotime($currentdate);
                    $filter .= ' AND ' . VIS_TABLE_PREFIX . 'client.client_id=' . $data['vis_client_id'];
                    $filter .= ($data['status_id'] == 4) ? '' : ' and avis_invoice.invoice_status=' . $data['status_id'];
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    $status_id = ($data['status_id'] == 4) ? '' : $data['status_id'];
                    $weekDate  = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter    = VIS_TABLE_PREFIX . 'invoice.entry_date between ' . $weekDate . ' AND ' . strtotime($currentdate);
                    $filter .= ' AND ' . VIS_TABLE_PREFIX . 'client.client_id=' . $data['vis_client_id'];
                    $filter .= ($data['status_id'] == 4) ? '' : ' and avis_invoice.invoice_status=' . $data['status_id'];
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $status_id = ($data['status_id'] == 3) ? '' : $data['status_id'];
                    $hour      = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter    = VIS_TABLE_PREFIX . 'invoice.entry_date between ' . $hour . ' AND ' . strtotime($currentdate);
                    $filter .= ' AND ' . VIS_TABLE_PREFIX . 'client.client_id=' . $data['vis_client_id'];
                    $filter .= ($data['status_id'] == 4) ? '' : ' and avis_invoice.invoice_status=' . $data['status_id'];
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    
                    $status_id = ($data['status_id'] == 3) ? '' : $data['status_id'];
                    $from      = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to        = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter    = VIS_TABLE_PREFIX . 'invoice.entry_date between ' . $from . ' AND ' . $to;
                    $filter .= ' AND ' . VIS_TABLE_PREFIX . 'client.client_id=' . $data['vis_client_id'];
                    $filter .= ($data['status_id'] == 4) ? '' : ' and avis_invoice.invoice_status=' . $data['status_id'];
                }
            }
            
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object = new class_report($database);
            
            $report = $object->get_client_order_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
            $total=0;$paid=0;$unpaid=0;$cancelled=0;$inprocess=0;
			foreach ($report as $ticket_id => $details) {
				$total += $details['amount'];
			if($details['invoice_status'] == 0)$unpaid +=$details['amount'];
            else if($details['invoice_status'] == 1)$paid +=$details['amount'];
			else if($details['invoice_status'] == 2)$cancelled +=$details['amount'];
			else if($details['invoice_status'] == 3)$inprocess +=$details['amount'];
				
			}
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                $j      = 0;
                foreach ($report as $ticket_id => $i) {
				 
     				foreach ($i as $key => $val) {
                        if ($key == 'order_id') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'invoice_hash') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'first_name') {
                            $report_exp[$j][$key] = $val;
                        }
						elseif ($key == 'last_name') {
                            $report_exp[$j][$key] = $val;
                        }
						elseif ($key == 'email') {
                            $report_exp[$j][$key] = $val;
                        }
						elseif ($key == 'group_name') {
                            $report_exp[$j][$key] = $val;
                        }elseif ($key == 'entry_date') {
                          
							$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        } elseif ($key == 'due_date') {
                          
							$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        } elseif ($key == 'paid_date') {
                            
							$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        } elseif ($key == 'amount') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'invoice_status') {
                            $report_exp[$j][$key] = $val;
						if($val==0)
							{
							 $report_exp[$j][$key] = $vis_language['TXTUNPAID'];
						}elseif($val==1)
							{
							 $report_exp[$j][$key] = $vis_language['TXTPAID'];
						}elseif($val==2)
							{
							 $report_exp[$j][$key] = $vis_language['TXTCANCELED']; 
						}elseif($val==3)
							{
							 $report_exp[$j][$key] = $vis_language['TXTINPROCESS'];
							}
                        }
                    }
					$report_exp1[$j]['order_id'] = $report_exp[$j]['order_id'];
					$report_exp1[$j]['invoice_hash'] = $report_exp[$j]['invoice_hash'];
					$report_exp1[$j]['client_name'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					$report_exp1[$j]['email'] = $report_exp[$j]['email'];
					$report_exp1[$j]['organization'] = $report_exp[$j]['group_name'];
					$report_exp1[$j]['create_date'] = $report_exp[$j]['entry_date'];
					$report_exp1[$j]['due_date'] = $report_exp[$j]['due_date'];
					$report_exp1[$j]['paid_date'] = $report_exp[$j]['paid_date'];
					$report_exp1[$j]['amount'.' ('.$currency.')'] = $report_exp[$j]['amount'];
					$report_exp1[$j]['status'] = $report_exp[$j]['invoice_status'];
					
                    $j++;
                }
				
				$report_exp1[$j]['total']=$vis_language['TXTTOTAL'].' :'.$total.' '.$currency;
				$report_exp1[$j]['paid']=$vis_language['TXTTOTALPAID'].' :'.$paid.' '.$currency;
				$report_exp1[$j]['unpaid']=$vis_language['TXTTOTALUNPAID'].' :'.$unpaid.' '.$currency;
				$report_exp1[$j]['cancelled']=$vis_language['TXTTOTALCANCELLED'].' :'.$cancelled.' '.$currency;
				$report_exp1[$j]['inprocess']=$vis_language['TXTTOTALINPROCESS'].' :'.$inprocess.' '.$currency;
				 
				
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
																
																if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'client_order_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
        
    }
				
function client_transaction_report($operation)
{
        
        global $vis_language;
        $report_title = $vis_language['TXTCLIENTTRANSACTIONREPORT'];
        $export       = 1;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object = new class_ticket($database);
		$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
		$domain_details = $tkt_object->get_domain(VIS_TABLE_PREFIX . 'domain.is_enabled = 1', $staff_id, VIS_LOCATION_AREA);
		
	     $currency       = $domain_details[0]['currency'];
        if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                $filter = $operation['filter'];
            } else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                
                if ($data['duration'] == 0 && $data['client_id'] == '' && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['vis_client_id'] == '' && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['MSGSELCLIENT'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                    
                } else if ($data['duration'] != 0 && $data['vis_client_id'] == '' && $data['status_id'] != -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['MSGSELCLIENT'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                    
                } else if ($data['duration'] == 0 && $data['vis_client_id'] == '' && $data['status_id'] != -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    
                    return false;
                }
                
                else if ($data['duration'] == 0 && $data['vis_client_id'] != '' && $data['status_id'] != -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    
                    return false;
                } else if ($data['duration'] == 0 && $data['vis_client_id'] != '' && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    
                    return false;
                } else if ($data['duration'] != 0 && $data['vis_client_id'] != '' && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTBILLINGSTATUSSEL']." ".$vis_language['TXTSTATUS'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['vis_client_id'] == '' && $data['status_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTBILLINGSTATUSSEL']." ".$vis_language['TXTSTATUS'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                
                $currentdate = date('Y-m-d h:i:s');
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    $status_id = ($data['status_id'] == 4) ? '' : $data['status_id'];
                    $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter    = VIS_TABLE_PREFIX . 'balance.entry_date between  ' . $monthDate . ' AND ' . strtotime($currentdate);
                    $filter .= ' AND ' . VIS_TABLE_PREFIX . 'client.client_id=' . $data['vis_client_id'];
                    $filter .= ($data['status_id'] == 4) ? '' : ' and ' . VIS_TABLE_PREFIX . 'invoice.invoice_status=' . $data['status_id'];
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    $status_id = ($data['status_id'] == 4) ? '' : $data['status_id'];
                    $weekDate  = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter    = VIS_TABLE_PREFIX . 'balance.entry_date between ' . $weekDate . ' AND ' . strtotime($currentdate);
                    $filter .= ' AND ' . VIS_TABLE_PREFIX . 'client.client_id=' . $data['vis_client_id'];
                    $filter .= ($data['status_id'] == 4) ? '' : ' and ' . VIS_TABLE_PREFIX . 'invoice.invoice_status=' . $data['status_id'];
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $status_id = ($data['status_id'] == 3) ? '' : $data['status_id'];
                    $hour      = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter    = VIS_TABLE_PREFIX . 'balance.entry_date between ' . $hour . ' AND ' . strtotime($currentdate);
                    $filter .= ' AND ' . VIS_TABLE_PREFIX . 'client.client_id=' . $data['vis_client_id'];
                    $filter .= ($data['status_id'] == 4) ? '' : ' and ' . VIS_TABLE_PREFIX . 'invoice.invoice_status=' . $data['status_id'];
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    
                    $status_id = ($data['status_id'] == 3) ? '' : $data['status_id'];
                    $from      = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to        = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter    = VIS_TABLE_PREFIX . 'balance.entry_date between ' . $from . ' AND ' . $to;
                    $filter .= ' AND ' . VIS_TABLE_PREFIX . 'client.client_id=' . $data['vis_client_id'];
                    $filter .= ($data['status_id'] == 4) ? '' : ' and ' . VIS_TABLE_PREFIX . 'invoice.invoice_status=' . $data['status_id'];
                }
            }
            
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object = new class_report($database);
            $report = $object->get_client_transaction_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
            $total=0;$paid=0;$unpaid=0;$cancelled=0;$inprocess=0;
			foreach ($report as $ticket_id => $details) {
			$total += $details['amount'];
			if($details['invoice_status'] == 0)$unpaid +=$details['amount'];
            else if($details['invoice_status'] == 1)$paid +=$details['amount'];
			else if($details['invoice_status'] == 2)$cancelled +=$details['amount'];
			else if($details['invoice_status'] == 3)$inprocess +=$details['amount'];
				
			}
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                $j      = 0;
                foreach ($report as $ticket_id => $i) {
                    foreach ($i as $key => $val) {
                        if ($key == 'transaction_id') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'invoice_hash') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'description') {
                            $report_exp[$j][$key] = str_replace(array("\n","\r\n","\r"), ' ', $val);
                        } elseif ($key == 'first_name') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'last_name') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'email') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'group_name') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'entry_date') {
                           
							$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        } elseif ($key == 'amount') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'invoice_status') {
                            $report_exp[$j][$key] = $val;
						if($val==0)
							{
							 $report_exp[$j][$key] = $vis_language['TXTUNPAID'];
						}elseif($val==1)
							{
							 $report_exp[$j][$key] = $vis_language['TXTPAID'];
						}elseif($val==2)
							{
							 $report_exp[$j][$key] = $vis_language['TXTCANCELED']; 
						}elseif($val==3)
							{
							 $report_exp[$j][$key] = $vis_language['TXTINPROCESS'];
							}
                        }
                        
                    }
					$report_exp1[$j]['invoice_hash'] = $report_exp[$j]['invoice_hash'];
					$report_exp1[$j]['description'] = $report_exp[$j]['description'];
					$report_exp1[$j]['client_name'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					$report_exp1[$j]['email'] = $report_exp[$j]['email'];
					$report_exp1[$j]['organization'] = $report_exp[$j]['group_name'];
					$report_exp1[$j]['transaction_id'] = $report_exp[$j]['transaction_id'];
					//$report_exp1[$j]['create_date'] = $report_exp[$j]['entry_date'];
					$report_exp1[$j]['transaction_date'] = $report_exp[$j]['entry_date'];
					$report_exp1[$j]['amount'.' ('.$currency.')'] = $report_exp[$j]['amount'];
					$report_exp1[$j]['status'] = $report_exp[$j]['invoice_status'];
					
                    $j++;
                }
				
				$report_exp1[$j]['total']=$vis_language['TXTTOTAL'].' :'.$total.' '.$currency;
				$report_exp1[$j]['paid']=$vis_language['TXTTOTALPAID'].' :'.$paid.' '.$currency;
				$report_exp1[$j]['unpaid']=$vis_language['TXTTOTALUNPAID'].' :'.$unpaid.' '.$currency;
				$report_exp1[$j]['cancelled']=$vis_language['TXTTOTALCANCELLED'].' :'.$cancelled.' '.$currency;
				$report_exp1[$j]['inprocess']=$vis_language['TXTTOTALINPROCESS'].' :'.$inprocess.' '.$currency;
                
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
				
				$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
					
					 $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } 
                  else if ($operation['export_type'] == 3) {
					//print_r($report_exp); 
                    
				    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
																
																if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'client_transaction_report.php');
            
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
        
    }
				
function ticket_replied_by_staff_report($operation)
{
        global $vis_language;
        $report_title = $vis_language['TXTTICKETREPLIEDBYSTAFF'];
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        $export = 1;
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object       = new class_ticket($database);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
        $staff            = $tkt_object->get_staff_list($staff_id, VIS_LOCATION_AREA);
        if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                if ($data['duration'] == 0 && $data['staff_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['staff_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELSTAFFFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] == 0 && $data['staff_id'] != 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                
                $currentdate = date('Y-m-d h:i:s');
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter    = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and '.VIS_TABLE_PREFIX.'ticket_post.submitter=0 and '.VIS_TABLE_PREFIX.'ticket_post.submitter_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and '.VIS_TABLE_PREFIX.'ticket_post.is_visible=1 and '.VIS_TABLE_PREFIX.'ticket_post.reply_forward=1';
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    $weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter   = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and '.VIS_TABLE_PREFIX.'ticket_post.submitter=0 and '.VIS_TABLE_PREFIX.'ticket_post.submitter_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and '.VIS_TABLE_PREFIX.'ticket_post.is_visible=1 and '.VIS_TABLE_PREFIX.'ticket_post.reply_forward=1';
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $hour   = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and '.VIS_TABLE_PREFIX.'ticket_post.submitter=0 and '.VIS_TABLE_PREFIX.'ticket_post.submitter_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and '.VIS_TABLE_PREFIX.'ticket_post.is_visible=1 and '.VIS_TABLE_PREFIX.'ticket_post.reply_forward=1';
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    $from   = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to     = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $from . ' AND ' . $to . ' and '.VIS_TABLE_PREFIX.'ticket_post.submitter=0 and '.VIS_TABLE_PREFIX.'ticket_post.submitter_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and '.VIS_TABLE_PREFIX.'ticket_post.is_visible=1 and '.VIS_TABLE_PREFIX.'ticket_post.reply_forward=1';
                }
            }
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object       = new class_report($database);
            //$filter .= ' and ' . VIS_TABLE_PREFIX . 'ticket.is_deleted!=1 and ' . VIS_TABLE_PREFIX . 'ticket.is_spam!=1';
            $report = $object->ticket_post_by_staff_report($filter, $staff_id, VIS_LOCATION_AREA);
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                $j=0;
			   foreach ($report as $i){
			  foreach ($i as $key => $val){
			if($key == 'ticket_hash' || $key == 'subject'){
				$report_exp[$j][$key] = $val;
					}
				elseif($key == 'fname' || $key == 'lname' || $key == 'email' || $key == 'group_name'){
				$report_exp[$j][$key] = $val;
					}
				elseif($key == 'create_date'){
				$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id); 
				}
				elseif($key == 'totalpost'){
				$report_exp[$j][$key] = $val;
				}
	            }
					
				$report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash'];
				$report_exp1[$j]['subject'] 	= $report_exp[$j]['subject'];
				$report_exp1[$j]['client_name'] 	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
				$report_exp1[$j]['email'] 	= $report_exp[$j]['email'];
				$report_exp1[$j]['organization'] 	= $report_exp[$j]['group_name'];
				$report_exp1[$j]['create_date'] = $report_exp[$j]['create_date'];
				$report_exp1[$j]['reply_count'] = $report_exp[$j]['totalpost'];
																				
                    $j++;
                }
																
 			
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
                if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'ticket_replied_by_staff_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
    }
				
function ticket_forwarded_by_staff_report($operation)
{
        global $vis_language;
        $report_title = $vis_language['TXTTICKETFORWARDEDBYSTAFF'];
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        $export = 1;
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object       = new class_ticket($database);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
        $staff            = $tkt_object->get_staff_list($staff_id, VIS_LOCATION_AREA);
        if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                if ($data['duration'] == 0 && $data['staff_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['staff_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELSTAFFFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] == 0 && $data['staff_id'] != 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                
                $currentdate = date('Y-m-d h:i:s');
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter    = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and '.VIS_TABLE_PREFIX.'ticket_post.submitter=0 and '.VIS_TABLE_PREFIX.'ticket_post.submitter_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and '.VIS_TABLE_PREFIX.'ticket_post.reply_forward=2';
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    $weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter   = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and '.VIS_TABLE_PREFIX.'ticket_post.submitter=0 and '.VIS_TABLE_PREFIX.'ticket_post.submitter_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and '.VIS_TABLE_PREFIX.'ticket_post.reply_forward=2';
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $hour   = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and '.VIS_TABLE_PREFIX.'ticket_post.submitter=0 and '.VIS_TABLE_PREFIX.'ticket_post.submitter_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and '.VIS_TABLE_PREFIX.'ticket_post.reply_forward=2';
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    $from   = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to     = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter = VIS_TABLE_PREFIX . 'ticket_post.receive_date between ' . $from . ' AND ' . $to . ' and '.VIS_TABLE_PREFIX.'ticket_post.submitter=0 and '.VIS_TABLE_PREFIX.'ticket_post.submitter_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket_post.draft_id=0 and '.VIS_TABLE_PREFIX.'ticket_post.reply_forward=2';
                }
            }
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object       = new class_report($database);
            //$filter .= ' and ' . VIS_TABLE_PREFIX . 'ticket.is_deleted!=1 and ' . VIS_TABLE_PREFIX . 'ticket.is_spam!=1';
            $report = $object->ticket_post_by_staff_report($filter, $staff_id, VIS_LOCATION_AREA);
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                $j=0;
				foreach ($report as $i){
				foreach ($i as $key => $val){
				if($key == 'ticket_hash' || $key == 'subject'){
				$report_exp[$j][$key] = $val;
				}
				elseif($key == 'fname' || $key == 'lname' || $key == 'email' || $key == 'group_name'){
				$report_exp[$j][$key] = $val;
					}
				elseif($key == 'create_date'){
				$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id); 
				}
				elseif($key == 'totalpost'){
				$report_exp[$j][$key] = $val;
			    }
				}
					
			    $report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash'];
				$report_exp1[$j]['subject'] 				= $report_exp[$j]['subject'];
				$report_exp1[$j]['client_name'] 	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
				$report_exp1[$j]['email'] 	= $report_exp[$j]['email'];
				$report_exp1[$j]['organization'] 	= $report_exp[$j]['group_name'];
				$report_exp1[$j]['create_date'] = $report_exp[$j]['create_date'];
				$report_exp1[$j]['forward_count'] = $report_exp[$j]['totalpost'];
																				
                $j++;
                }
																
 			
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
                if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'ticket_forwarded_by_staff_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
    }
				
function ticket_viewed_by_staff_report($operation)
{
        global $vis_language;
        $report_title = $vis_language['TXTTICKETVIEWEDBYSTAFF'];
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        $export = 1;
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object       = new class_ticket($database);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
        $staff            = $tkt_object->get_staff_list($staff_id, VIS_LOCATION_AREA);
        if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                if ($data['duration'] == 0 && $data['staff_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['staff_id'] == 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELSTAFFFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] == 0 && $data['staff_id'] != 0) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                
                $currentdate = date('Y-m-d h:i:s');
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter    = VIS_TABLE_PREFIX . 'auditlog.entry_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and '.VIS_TABLE_PREFIX.'auditlog.operation_id=52 and '.VIS_TABLE_PREFIX.'auditlog.operator_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'auditlog.operator=2';
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    $weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter   = VIS_TABLE_PREFIX . 'auditlog.entry_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and '.VIS_TABLE_PREFIX.'auditlog.operation_id=52 and '.VIS_TABLE_PREFIX.'auditlog.operator_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'auditlog.operator=2';
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $hour   = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter = VIS_TABLE_PREFIX . 'auditlog.entry_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and '.VIS_TABLE_PREFIX.'auditlog.operation_id=52 and '.VIS_TABLE_PREFIX.'auditlog.operator_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'auditlog.operator=2';
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    $from   = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to     = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter = VIS_TABLE_PREFIX . 'auditlog.entry_date between ' . $from . ' AND ' . $to . ' and '.VIS_TABLE_PREFIX.'auditlog.operation_id=52 and '.VIS_TABLE_PREFIX.'auditlog.operator_id=' . $data['staff_id'] . ' and ' . VIS_TABLE_PREFIX . 'auditlog.operator=2';
                }
            }
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
            $object       = new class_report($database);
            //$filter .= ' and ' . VIS_TABLE_PREFIX . 'ticket.is_deleted!=1 and ' . VIS_TABLE_PREFIX . 'ticket.is_spam!=1';
            $report = $object->ticket_viewed_by_staff_report($filter, $staff_id, VIS_LOCATION_AREA);
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                $j=0;
																			foreach ($report as $i){
																					foreach ($i as $key => $val){
																							if($key == 'ticket_hash' || $key == 'subject'){
																							$report_exp[$j][$key] = $val;
																							}
																							elseif($key == 'entry_date'){
																						$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id); 
																							}
																							
																					}
					
																				$report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash'];
																				$report_exp1[$j]['subject'] 				= $report_exp[$j]['subject'];
																				$report_exp1[$j]['ticket_view_date'] = $report_exp[$j]['entry_date'];
																						
                    $j++;
                }
																
 			
                $completed_date = date('Y-m-d H:i:s');
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
                if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'ticket_viewed_by_staff_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
    }
				
	function assets_linkedto_problems_report($operation)
	{
		global $vis_language;
        $report_title = $vis_language['TXTASSETLINKTOPROBLEM'];
        $export       = 1;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
		 include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'cmdb.php');
	      $cmdb_obj = new class_cmdb($database);
	      $assets = $cmdb_obj->get_assets('', '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
	      include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
         $setting_object=new class_setting($database);
	     $citype_details=$setting_object->get_configuration_item_category('','is_enabled=1');
	  
		 if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
				
                if ($data['vis_asset_id'] == -1  && $data['vis_ci_type'] == '') {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
				
				 if ($data['vis_asset_id'] == -1 && $data['vis_ci_type'] != '') {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECT'].' '.$vis_language['TXTASSETNAME'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                 if ($data['vis_asset_id'] != -1 && $data['vis_ci_type'] == '') {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECT'].' '.$vis_language['TXTCITYPE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
				
				 if ($data['vis_asset_id'] != -1) {
                    $filter = 'rel.child_id= '.$data['vis_asset_id'].' and rel.attach_type = 4 and cmast.ci_type_id = '.$data['vis_ci_type'];
                }
						
			}
			 include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
             $object = new class_report($database);
			 $report=array();
           // $report = $object->get_assets_linkedto_problems($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
			   list($custom_fields_details,$report) = $object->get_assets_linkedto_problems($filter,$filter1, VIS_STAFF_ID, VIS_LOCATION_AREA);
			   
			   $temp=array();
         $temp1=array();		
         $temp2=array();	 
		 $a=0;
		  
		for($i=0;$i<count($custom_fields_details);$i++) 
		{
			$a=$custom_fields_details[$i]['assets_id'];
			if($custom_fields_details[$i]['assets_id']!=$temp1[$a]['assets_id'])
			{$temp['assets_id']=$custom_fields_details[$i]['assets_id'];
		    $a=$custom_fields_details[$i]['assets_id'];
			}
			$temp[$custom_fields_details[$i]['field_name']]=$custom_fields_details[$i]['content'];
			$temp1[$a]=$temp;			 
		}
 
			$temp2=reset($temp1);
		     
			foreach($report as $key => $csm)
			{
				if(in_array($report[$key]['assets_id'],$temp1[$csm['assets_id']])){
					foreach($temp1[$csm['assets_id']] as $keys=>$val){
					if($keys!='assets_id')
					$report[$key][$keys]=$val;
				}
			}else{
					foreach($temp2 as $keys=>$val){
					if($keys!='assets_id')
					$report[$key][$keys]='';
					}
				}																					
			}
			    if($report!=1){
					for($i=0; $i<count($report); $i++){
						$is_expired =0;
						if($report[$i]['ci_type_id']==2 || $report[$i]['ci_type_id']==3){
							$total_month = ($report[$i]['warranty_year']*12)+$report[$i]['warranty_month'];
							$current_date = strtotime(date('Y-m-d h:i:s'));
							//$expiry_date = strtotime(date('Y-m-d h:i:s', $report[$i]['installation_date']) . $total_month.' month');
							if($report[$i]['warranty_year']==0 && $report[$i]['warranty_month']==0){
								$expiry_date = $report[$i]['expiry_date'];
							}else{
								$expiry_date = strtotime($total_month.' month',$report[$i]['installation_date']);
							}
							if($expiry_date <= $current_date){
								//$report[$i]['status']=format_datetime($expiry_date);
								$report[$i]['status']=date(VIS_EDITDATETIMEPICKER_FORMAT,$expiry_date);
							} else{
								//$report[$i]['status']='--';
								$report[$i]['status']=date(VIS_EDITDATETIMEPICKER_FORMAT,$expiry_date);
								//format_datetime_rpt($expiry_date,$staff_id);
							}
						}else{
							$report[$i]['status']='--';
						}																				
					}//end for
																							
				}
		     if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export         = new export();
				 include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'report.php');
                $report_obj         = new report();
				$j=0;
                foreach ($report as $assets_id => $i) {
                    foreach ($i as $key => $val) {
						if ($key == 'ci_type_id') {
                            $report_exp[$j][$key] = $val;
                        }
                        if ($key == 'name') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'description') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'assigned_on') {
                           $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        }elseif ($key == 'domain_name') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'department_name') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'location_name') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'problem_hash') {
                            $report_exp[$j][$key] =($val);
                        } elseif ($key == 'subject') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'usedby') {
                            $report_exp[$j][$key] = $val;
                        }
						elseif($key == 'impact_name' || $key == 'product_name' || $key =='vender_name' || $key == 'asset_state' || $key == 'depreciation_type' || $key == 'salvage'  || $key == 'warranty_year'  || $key == 'warranty_month'|| $key == 'asset_tag' || $key == 'licence_key' || $key == 'installation_path' || $key == 'first_name' || $key == 'last_name' || $key == 'email' || $key == 'fname' || $key == 'lname' || $key == 'clnt_fname' || $key == 'clnt_lname' || $key == 'licence_type' || $key == 'status' || $key == 'staff_id') {
						$report_exp[$j][$key] = $val;	
						}
						elseif($key == 'installation_date' || $key == 'expiry_date'  || $key == 'last_audit_date'){
						$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);	
						}
						elseif($key == 'details')
						{
						$text = $val;
						$new_val =  $report_obj->strip_html_tags($text);
						 $report_exp[$j][$key] = $new_val;
						}
						  
					 }
					  //for hardware assets
					 if($report_exp[$j]['ci_type_id'] == 2)
					{
					$report_exp1[$j]['Problem_hash'] = $report_exp[$j]['problem_hash'];
					$report_exp1[$j]['Subject'] = $report_exp[$j]['subject'];
					$report_exp1[$j]['Asset_name'] = $report_exp[$j]['name'];
					$report_exp1[$j]['Department'] = $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
					$report_exp1[$j]['CI_Type'] = $report_exp[$j]['description'];
					$report_exp1[$j]['description'] = $report_exp[$j]['details'];
					if($report_exp[$j]['staff_id']===''){
						$report_exp1[$j]['managed_by'] = '---';
					}else{
						$report_exp1[$j]['managed_by'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					}
					$report_exp1[$j]['email_id'] = $report_exp[$j]['email'];
					if(isset($report_exp[$j]['usedby'])){
						$report_exp1[$j]['used by'] = $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];
					}/*elseif($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
					{
					$report_exp1[$j]['used_by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
					}
					else{
					$report_exp1[$j]['used_by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
					}*/
					 
					$report_exp1[$j]['Assigned_on'] = $report_exp[$j]['assigned_on'];
					$report_exp1[$j]['Location'] = $report_exp[$j]['location_name'];
					$report_exp1[$j]['impact'] = $report_exp[$j]['impact_name'];
					$report_exp1[$j]['product'] = $report_exp[$j]['product_name'];
					$report_exp1[$j]['vender'] = $report_exp[$j]['vender_name'];
					$report_exp1[$j]['warranty_year'] = $report_exp[$j]['warranty_year'];
					$report_exp1[$j]['warranty_month'] = $report_exp[$j]['warranty_month'];
					$report_exp1[$j]['acquisition_date'] = $report_exp[$j]['installation_date'];
					$report_exp1[$j]['warranty_expiry_date'] = $report_exp[$j]['expiry_date'];
					$report_exp1[$j]['asset_state'] = $report_exp[$j]['asset_state'];
					$report_exp1[$j]['asset_tag'] = $report_exp[$j]['asset_tag'];
					$report_exp1[$j]['serial_no'] = $report_exp[$j]['licence_key'];
					$report_exp1[$j]['last_audit_date'] = $report_exp[$j]['last_audit_date'];
					$report_exp1[$j]['cost'] = $report_exp[$j]['installation_path'];
					$report_exp1[$j]['salvage'] = $report_exp[$j]['salvage'];
					$report_exp1[$j]['depreciation_type'] = $report_exp[$j]['depreciation_type'];
					$report_exp1[$j]['expiry_date'] = $report_exp[$j]['status'];
					}
					// for software assets
					elseif($report_exp[$j]['ci_type_id'] == 3)
					 {
				    $report_exp1[$j]['Problem_hash'] = $report_exp[$j]['problem_hash'];
					$report_exp1[$j]['Subject'] = $report_exp[$j]['subject'];
					$report_exp1[$j]['Asset_name'] = $report_exp[$j]['name'];
					$report_exp1[$j]['Department'] = $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
					$report_exp1[$j]['CI_Type'] = $report_exp[$j]['description'];
					$report_exp1[$j]['description'] = $report_exp[$j]['details'];
					if($report_exp[$j]['staff_id']===''){
						$report_exp1[$j]['managed_by'] = '---';
					}else{
						$report_exp1[$j]['managed_by'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					}
					$report_exp1[$j]['email_id'] = $report_exp[$j]['email'];
					if(isset($report_exp[$j]['usedby'])){
						$report_exp1[$j]['used by'] = $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];
					}/*elseif($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
					{
						$report_exp1[$j]['used_by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
					}
					else{
						$report_exp1[$j]['used_by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
					}*/
					//$report_exp1[$j]['email'] = $report_exp[$j]['usedby'];
					$report_exp1[$j]['Assigned_on'] = $report_exp[$j]['assigned_on'];
					$report_exp1[$j]['Location'] = $report_exp[$j]['location_name'];
					$report_exp1[$j]['impact'] = $report_exp[$j]['impact_name'];
					$report_exp1[$j]['product'] = $report_exp[$j]['product_name'];
					$report_exp1[$j]['vender'] = $report_exp[$j]['vender_name'];
					$report_exp1[$j]['warranty_year'] = $report_exp[$j]['warranty_year'];
					$report_exp1[$j]['warranty_month'] = $report_exp[$j]['warranty_month'];
					$report_exp1[$j]['installation_date'] = $report_exp[$j]['installation_date'];
					$report_exp1[$j]['licence_expiry_date'] = $report_exp[$j]['expiry_date'];
					$report_exp1[$j]['licence_type'] = $report_exp[$j]['licence_type'];
					$report_exp1[$j]['cost'] = $report_exp[$j]['asset_tag'];
					$report_exp1[$j]['licence_key'] = $report_exp[$j]['licence_key'];
					$report_exp1[$j]['installation_path'] = $report_exp[$j]['installation_path'];
					$report_exp1[$j]['last_audit_date'] = $report_exp[$j]['last_audit_date'];
					$report_exp1[$j]['expiry_date'] = $report_exp[$j]['status'];	 
					}
					 //for other types of assets
					 else
					{
					$report_exp1[$j]['Problem_hash'] = $report_exp[$j]['problem_hash'];
					$report_exp1[$j]['Subject'] = $report_exp[$j]['subject'];
					$report_exp1[$j]['Asset_name'] = $report_exp[$j]['name'];
					$report_exp1[$j]['Department'] = $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
					$report_exp1[$j]['CI_Type'] = $report_exp[$j]['description'];
					$report_exp1[$j]['description'] = $report_exp[$j]['details'];
					if($report_exp[$j]['staff_id']===''){
						$report_exp1[$j]['managed_by'] = '---';
					}else{
						$report_exp1[$j]['managed_by'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					}
					$report_exp1[$j]['email_id'] = $report_exp[$j]['email'];
					
					
					$report_exp1[$j]['used_by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
					
					$report_exp1[$j]['email'] = $report_exp[$j]['usedby'];
					$report_exp1[$j]['Assigned_on'] = $report_exp[$j]['assigned_on'];
					$report_exp1[$j]['Location'] = $report_exp[$j]['location_name'];
					$report_exp1[$j]['impact'] = $report_exp[$j]['impact_name'];
						 
					}
					 if($report_exp[$j]['ci_type_id'] == 2){
							 
						foreach($report_exp[$j] as $key=>$val){
						 
						if($key!='group_name' && $key !='ci_type_id' && $key !='clnt_fname' && $key !='clnt_lname' && $key !='fname' && $key !='lname' && $key !='impact_id' && $key !='staff_id' && $key !='location_id' && $key !='vendor_id' && $key !='department_access' && $key !='product_id' && $key !='assets_state' && $key!='email' && $key!='name' && $key!='assigned_on' && $key!='impact_name' && $key!='product_name' && $key!='vender_name' && $key!='usedby' && $key !='location' && $key !='licence_type' && $key !='CI_Type' && $key!='department_name' && $key!='location_name' && $key!='problem_hash' && $key!='subject' && $key!='expiry_date' && $key!='status' && $key!='') {
						
							$report_exp1[$j][$key]=$val;
							
						}
						 
					}
				 } elseif($report_exp[$j]['ci_type_id'] == 3){
					 
					 foreach($report_exp[$j] as $key=>$val){
						 
						if($key!='group_name' && $key !='ci_type_id'  && $key !='clnt_fname' && $key !='clnt_lname' && $key !='impact_id' && $key !='fname' && $key !='lname' && $key !='staff_id' && $key !='location_id' && $key !='vendor_id' && $key !='department_access' && $key !='product_id' && $key !='assets_state' && $key!='email' && $key!='name' && $key!='assigned_on' && $key!='impact_name' && $key!='product_name' && $key!='vender_name' && $key!='usedby' && $key !='location' && $key !='CI_Type' && $key!='department_name' && $key!='location_name' && $key!='problem_hash' && $key!='subject' && $key!='expiry_date' && $key!='status' && $key!='') {
						
							$report_exp1[$j][$key]=$val;
							
						}
						 
					}
					 
				 }else{
					  foreach($report_exp[$j] as $key=>$val){        
						 
						if($key!='group_name' && $key!='ci_type_id' && $key !='ci_property_type' && $key !='domain_name' && $key !='department_name' && $key !='first_name' && $key !='last_name' && $key !='clnt_fname' && $key !='clnt_lname' && $key !='impact_id' && $key !='fname' && $key !='lname' && $key !='staff_id' && $key !='location_id' && $key !='vendor_id' && $key !='department_access' && $key !='product_id' && $key !='asset_state' && $key!='assets_id' && $key!='name' && $key!='usedby' && $key!='assigned_on' && $key !='attachment_id' && $key !='warranty_year' && $key !='warranty_month' && $key !='installation_date' && $key!='expiry_date' && $key !='asset_tag' && $key !='licence_key' && $key !='installation_path' && $key !='last_audit_date' && $key !='salvage' && $key !='depreciation_type' && $key !='impact_name' && $key !='product_name' && $key !='vender_name' && $key !='licence_type'  && $key !='location_name' && $key!='email' && $key!='problem_hash' && $key!='subject' && $key!='expiry_date' && $key!='status' && $key !=''){
						 
						
							$report_exp1[$j][$key]=$val;
							
						}
						 
					} 
				 }
                    $j++;
                }
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;	// To display title which shows on report form
				//$report_exp1['Report_title'] = $operation['export_name']; // To display title which entered in request report popup form
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
																
																if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            }else
                include(VIS_PATH_TEMPLATE . 'assets_linkedto_problems_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
         $database->database_disconnect();
        return true;
	}
	function assets_groupby_status_report($operation)
	{   
		global $vis_language;
		$report_title = $vis_language['TXTASSETGROUPSTATUS'];
		$export       = 1;
		if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
			$database = new class_database('mysql', 1);
		else
			$database = new class_database();
			
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$setting_object=new class_setting($database);
		$citype_details=$setting_object->get_configuration_item_category('','is_enabled=1');
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
		$tkt_object = new class_ticket($database);
		$staff      = $tkt_object->get_staff_list(VIS_STAFF_ID, VIS_LOCATION_AREA);
		$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
		
		
		if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
			if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
				$filter = $operation['filter'];
			else {
				if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
					$data = $operation;
				else
					$data = parse_form_array($operation['vis_action']);
					
				if (count($data) == 0) {
					include(VIS_PATH_TEMPLATE . 'report_form.php');
					return true;
				}
			
				  if ($data['status_id'] == -1  && $data['vis_ci_type'] == '') {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
				
				 if ($data['status_id'] == -1 && $data['vis_ci_type'] != '') {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECT'].' '.$vis_language['TXTSTATUS'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                 if ($data['status_id'] != -1 && $data['vis_ci_type'] == '') {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECT'].' '.$vis_language['TXTCITYPE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
				
			
				if ($data['status_id'] != -1 && $data['vis_ci_type'] != '') {
					$filter = $data['status_id'].' and '.$data['vis_ci_type']; 
				}
			}
			
			include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
			$object = new class_report($database);
			$temp = explode('and',$filter);  
			//print_r($temp); exit;
			$statusid =  $temp[0];
			$ci_type = $temp[1];
			
			if($statusid == 1 && $ci_type==2) //not expired hardware
			{	
				$filter = ' cmast.ci_type_id = 2 '; 
			} 
			elseif($statusid == 2 && $ci_type== 2) // expired hardware
			{ 	
				$filter = ' cmast.ci_type_id = 2 ';	
			} 
			elseif($statusid == 1 && $ci_type==3) //not expired software
			{
				$filter = ' cmast.ci_type_id = 3 ';
			} 
			elseif($statusid == 2 && $ci_type==3) //expired software
			{ 
				$filter = ' cmast.ci_type_id = 3 '; 
			} 
			
			$report1=array();	
			$report=array();					
			$report1 = $object->assets_groupby_status($filter, VIS_STAFF_ID, VIS_LOCATION_AREA); 
			
			/*if($report!=-1){ 
				for($i=0; $i<=count($report); $i++){
					$is_expired =0;
					if($report[$i]['ci_type_id']==2 || $report[$i]['ci_type_id']==3){
						$total_month = ($report[$i]['warranty_year']*12)+$report[$i]['warranty_month'];
						$current_date = strtotime(date('Y-m-d h:i:s'));
						//$expiry_date = strtotime(date('Y-m-d h:i:s', $report[$i]['installation_date']) . $total_month.' month');
						$expiry_date = strtotime($total_month.' month',$report[$i]['installation_date']);
						if($expiry_date <= $current_date){
							$is_expired=1;
							//$report[$i]['status']= format_datetime($expiry_date);
							$report[$i]['status']='--';
						} else{
							//$report[$i]['status']='--';
							$report[$i]['status']=format_datetime_rpt($expiry_date,$staff_id);
						}
						
						if($is_expired==1 && $statusid==1){
							unset($report[$i]);
						}elseif($is_expired!=1 && $statusid==2){
							unset($report[$i]);
						}
					}else{
					$report[$i]['status']='--';
					}
				}//end for
				if($report!=-1)
				$report=array_values($report);
			}*/
			if($report1!=-1){
				for($i=0; $i<count($report1); $i++){
					$is_expired =0;
					if($report1[$i]['ci_type_id']==2 || $report1[$i]['ci_type_id']==3){
						$total_month = ($report1[$i]['warranty_year']*12)+$report1[$i]['warranty_month'];
						$current_date = strtotime(date('Y-m-d h:i:s'));
						//$expiry_date = strtotime(date('Y-m-d h:i:s', $report[$i]['installation_date']) . $total_month.' month');
						if($report1[$i]['warranty_year']==0 && $report1[$i]['warranty_month']==0 && ($report1[$i]['expiry_date'] > $report1[$i]['installation_date'])){
							$expiry_date = $report1[$i]['expiry_date'];
						}else{
							$expiry_date = strtotime($total_month.' month',$report1[$i]['installation_date']);
						}
						if($expiry_date <= $current_date){
							$is_expired=1;
							$report1[$i]['is_expired']=1;
							//$report[$i]['status']=format_datetime($expiry_date);
							$report1[$i]['status']=date(VIS_EDITDATETIMEPICKER_FORMAT,$expiry_date);
						} else{
							$is_expired=2;
							$report1[$i]['is_expired']=2;
							//$report[$i]['status']='--';
							$report1[$i]['status']=date(VIS_EDITDATETIMEPICKER_FORMAT,$expiry_date);
							//format_datetime_rpt($expiry_date,$staff_id);
						}
						
						/*if($is_expired==1 && $statusid==1){
							unset($report[$i]);
							//$report = array();
						}elseif($is_expired==1 && $statusid==2){
							unset($report[$i]);
							//$report = array();
						}*/
					}else{
						$report1[$i]['status']='--';
					}
				}//end for
				
				if($report1!=-1 && !empty($report1)){
					
						for($k=0; $k<count($report1); $k++){
							if($report1[$k]['ci_type_id']==2 || $report1[$k]['ci_type_id']==3){
								if($report1[$k]['is_expired']!=$statusid){
								$report[]=$report1[$k];
								//echo 'test';
								}
							}else{
								$report[]=$report1[$k];
							}
						}
					//$report=array_values($report);
				}
				//echo '<pre>';
				//print_r($report1);
				//print_r($report);
			}
			
			if(isset($operation['vis_export']) && $operation['vis_export'] == 1) {
				include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
				$export         = new export();
				include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'report.php');
				$report_obj         = new report();
				$j=0;
				//echo "<pre>";print_r($report);echo "</pre>";
				foreach ($report as $assets_id => $i) {
					foreach ($i as $key => $val){
						if ($key == 'ci_type_id') {
							$report_exp[$j][$key] = $val;
						}
						elseif ($key == 'assigned_on') {
							$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
						}
						elseif($key == 'name' || $key == 'CNAME' || $key == 'domain_name' || $key == 'department_name' || $key == 'LNAME' || $key == 'first_name' || $key == 'last_name' || $key == 'usedby'  ){
							$report_exp[$j][$key] = $val;
						} 
						elseif($key == 'impact_name' || $key == 'product_name' || $key =='vender_name' || $key == 'asset_state' || $key == 'depreciation_type' || $key == 'salvage'  || $key == 'warranty_year'  || $key == 'warranty_month'|| $key == 'asset_tag' || $key == 'licence_key' || $key == 'installation_path' || $key == 'email' || $key == 'fname' || $key == 'lname' || $key == 'clnt_fname' || $key == 'clnt_lname' || $key == 'licence_type' || $key == 'config_item' || $key == 'staff_id') {
							$report_exp[$j][$key] = $val;	
						}
						elseif($key == 'installation_date' || $key == 'expiry_date'  || $key == 'last_audit_date'){
							$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);	
						}
						elseif($key == 'details')
						{
							$text = $val;
							$new_val =  $report_obj->strip_html_tags($text);
							$report_exp[$j][$key] = $new_val;
						}elseif($key =='status'){
							$report_exp[$j][$key] =date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
						}
						
					}
				
					//for hardware assets
					if($report_exp[$j]['ci_type_id'] == 2)
					{
						$report_exp1[$j]['Asset_name'] = $report_exp[$j]['name'];
						$report_exp1[$j]['Department'] = $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
						$report_exp1[$j]['ci_Type'] = $report_exp[$j]['config_item'];
						$report_exp1[$j]['description'] = $report_exp[$j]['details'];
						if($report_exp[$j]['staff_id']===''){
							$report_exp1[$j]['managed_by'] = '---';
						}else{
							$report_exp1[$j]['managed_by'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
						}
						$report_exp1[$j]['email_id'] = $report_exp[$j]['email'];
						if($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
						{
							$report_exp1[$j]['used_by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
						}
						else{
							$report_exp1[$j]['used_by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
						}
						$report_exp1[$j]['email'] = $report_exp[$j]['usedby'];
						$report_exp1[$j]['Assigned_on'] = $report_exp[$j]['assigned_on'];
						$report_exp1[$j]['Location'] = $report_exp[$j]['LNAME'];
						$report_exp1[$j]['impact'] = $report_exp[$j]['impact_name'];
						$report_exp1[$j]['product'] = $report_exp[$j]['product_name'];
						$report_exp1[$j]['vender'] = $report_exp[$j]['vender_name'];
						$report_exp1[$j]['warranty_year'] = $report_exp[$j]['warranty_year'];
						$report_exp1[$j]['warranty_month'] = $report_exp[$j]['warranty_month'];
						$report_exp1[$j]['acquisition_date'] = $report_exp[$j]['installation_date'];
						$report_exp1[$j]['warranty_expiry_date'] = $report_exp[$j]['expiry_date'];
						$report_exp1[$j]['asset_state'] = $report_exp[$j]['asset_state'];
						$report_exp1[$j]['asset_tag'] = $report_exp[$j]['asset_tag'];
						$report_exp1[$j]['serial_no'] = $report_exp[$j]['licence_key'];
						$report_exp1[$j]['last_audit_date'] = $report_exp[$j]['last_audit_date'];
						$report_exp1[$j]['cost'] = $report_exp[$j]['installation_path'];
						$report_exp1[$j]['salvage'] = $report_exp[$j]['salvage'];
						$report_exp1[$j]['depreciation_type'] = $report_exp[$j]['depreciation_type'];
						$report_exp1[$j]['expiry_date'] = $report_exp[$j]['status'];
					}
					
					// for software assets
					elseif($report_exp[$j]['ci_type_id'] == 3)
					{
						$report_exp1[$j]['Asset_name'] = $report_exp[$j]['name'];
						$report_exp1[$j]['Department'] = $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
						$report_exp1[$j]['ci_Type'] = $report_exp[$j]['config_item'];
						$report_exp1[$j]['description'] = $report_exp[$j]['details'];
						if($report_exp[$j]['staff_id']===''){
							$report_exp1[$j]['managed_by'] = '---';
						}else{
							$report_exp1[$j]['managed_by'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
						}
						$report_exp1[$j]['email_id'] = $report_exp[$j]['email'];
						if($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
						{
							$report_exp1[$j]['used_by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
						}
						else{
							$report_exp1[$j]['used_by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
						}
						$report_exp1[$j]['email'] = $report_exp[$j]['usedby'];
						$report_exp1[$j]['Assigned_on'] = $report_exp[$j]['assigned_on'];
						$report_exp1[$j]['Location'] = $report_exp[$j]['LNAME'];
						$report_exp1[$j]['impact'] = $report_exp[$j]['impact_name'];
						$report_exp1[$j]['product'] = $report_exp[$j]['product_name'];
						$report_exp1[$j]['vender'] = $report_exp[$j]['vender_name'];
						$report_exp1[$j]['warranty_year'] = $report_exp[$j]['warranty_year'];
						$report_exp1[$j]['warranty_month'] = $report_exp[$j]['warranty_month'];
						$report_exp1[$j]['installation_date'] = $report_exp[$j]['installation_date'];
						$report_exp1[$j]['licence_expiry_date'] = $report_exp[$j]['expiry_date'];
						$report_exp1[$j]['licence_type'] = $report_exp[$j]['licence_type'];
						$report_exp1[$j]['cost'] = $report_exp[$j]['asset_tag'];
						$report_exp1[$j]['licence_key'] = $report_exp[$j]['licence_key'];
						$report_exp1[$j]['installation_path'] = $report_exp[$j]['installation_path'];
						$report_exp1[$j]['last_audit_date'] = $report_exp[$j]['last_audit_date'];
						$report_exp1[$j]['expiry_date'] = $report_exp[$j]['status'];	 
					}
					
					$j++;
				}
				
				$completed_date = date('Y-m-d h:i:s');
				$report_exp1['Report_title'] = $report_title;
				//echo "<pre>";print_r($report_exp1);echo "</pre>";die;
				$filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
				if ($operation['export_type'] == 1) {
					$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
					if (file_exists('../downloads/' . $filename . '.csv')) {
						$filename = $filename;
					} else {
						$filename = '';
					}
				} else if ($operation['export_type'] == 2) {
					$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
					
					if (file_exists('../downloads/' . $filename . '.html')) {
						$filename = $filename;
					} else {
						$filename = '';
					}
				} else if ($operation['export_type'] == 3) {
					$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
					if (file_exists('../downloads/' . $filename . '.xls')) {
						$filename = $filename;
					} else {
						$filename = '';
					}
				}
				
				if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
					$database->database_disconnect();
					$database = new class_database();
					include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
					$object = new class_report($database);
				}
				$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
				if (!$result)
					return false;
					
				$this->email_send_download($operation);
				return true;
			}
			else
				include(VIS_PATH_TEMPLATE . 'assets_groupby_status_report.php');
		} 
		else
			include(VIS_PATH_TEMPLATE . 'report_form.php');
		$database->database_disconnect();
		return true;
	}
	
	function assets_groupby_location_report($operation)
	{
		global $vis_language;
        $report_title = $vis_language['TXTASSETGROUPBYLOCATIONS'];
        $export       = 1;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
		
	    				 
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object = new class_ticket($database);
        $staff      = $tkt_object->get_staff_list(VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
						if(VIS_LOCATION_AREA==2){						
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $object=new class_setting($database);
        $citype_details=$object->get_configuration_item_category('','is_enabled=1');
		$location='';								
		//$filters = " and (".VIS_TABLE_PREFIX."itil_location.department_access = 0)";
		$filters='';
		$location_details=$object->get_locations($filters,0, -1, '', VIS_STAFF_ID);
		$search = (count($location_details)>10)? 'data-live-search="true"' : '';
		$location .= "<option value=''>".$vis_language['TXTSELECT']."</option>";
		$location .= "<option value='0'>".$vis_language['TXTALL']."</option>";
		$location .= $this->vis_show_subcategories($location_details,'','location');
						}
	      
		 if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
				
                if ($data['location_id'] == '') {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTLOCATION'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
				if ($data['vis_ci_type'] == '') {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTCITYPE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
				
				 if ($data['location_id'] != -1 && $data['vis_ci_type'] != '') {
						if($data['location_id']!=0){
								$filter = 'cmast.location_id='.$data['location_id'].' and cmast.ci_type_id = '.$data['vis_ci_type']; 
						}else{
								$filter = 'cmast.location_id!='.$data['location_id'].' and cmast.ci_type_id = '.$data['vis_ci_type']; 
						}
					}$filter1 =' where avis_customfield.customgroup_id ='.$data['vis_ci_type'];
				  
				  $filter=$filter.'#'.$filter1;
				  }
				  
				  $temp_filtr = explode('#',$filter);
					 
					$filter = $temp_filtr[0];
					$filter1= $temp_filtr[1];
			 include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
             $object = new class_report($database);
           //  $report = $object->assets_groupby_location($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
			  $report=array();
           list($custom_fields_details,$report) = $object->assets_groupby_location($filter,$filter1, VIS_STAFF_ID, VIS_LOCATION_AREA);   
			$temp=array();
			$temp1=array();		
			$temp2=array();	 
			$a=0;
		  
		for($i=0;$i<count($custom_fields_details);$i++) 
		{
			$a=$custom_fields_details[$i]['assets_id'];
			if($custom_fields_details[$i]['assets_id']!=$temp1[$a]['assets_id'])
			{$temp['assets_id']=$custom_fields_details[$i]['assets_id'];
		    $a=$custom_fields_details[$i]['assets_id'];
			}
			$temp[$custom_fields_details[$i]['field_name']]=$custom_fields_details[$i]['content'];
			$temp1[$a]=$temp;	 
		}
         
			$temp2=reset($temp1);
		     
		  foreach($report as $key => $csm)
		   {
		 if(in_array($report[$key]['assets_id'],$temp1[$csm['assets_id']])){
			foreach($temp1[$csm['assets_id']] as $keys=>$val){
			 if($keys!='assets_id')
			$report[$key][$keys]=$val;
		   }
		}else{
		 foreach($temp2 as $keys=>$val){
		  if($keys!='assets_id')
		 $report[$key][$keys]='';
		   }
		  }
		   
		}
													
													if($report!=1){
													
														for($i=0; $i<count($report); $i++){
															
															$is_expired =0;
														if($report[$i]['ci_type_id']==2 || $report[$i]['ci_type_id']==3){
															$total_month = ($report[$i]['warranty_year']*12)+$report[$i]['warranty_month'];
															$current_date = strtotime(date('Y-m-d h:i:s'));
															//$expiry_date = strtotime(date('Y-m-d h:i:s', $report[$i]['installation_date']) . $total_month.' month');
															if($report[$i]['warranty_year']==0 && $report[$i]['warranty_month']==0){
																$expiry_date = $report[$i]['expiry_date'];
															}else{
																$expiry_date = strtotime($total_month.' month',$report[$i]['installation_date']);
															}
															if($expiry_date <= $current_date){
																//$report[$i]['status']=format_datetime($expiry_date);
																$report[$i]['status']=date(VIS_EDITDATETIMEPICKER_FORMAT,$expiry_date);
															}else{
																//$report[$i]['status']='--';
																$report[$i]['status']=date(VIS_EDITDATETIMEPICKER_FORMAT,$expiry_date);
																//format_datetime_rpt($expiry_date,$staff_id);
															} 
														}else{
															$report[$i]['status']='--';
														}
																												
															}//end for
																									
													}
												
													
		     if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export         = new export();
				include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'report.php');
                $report_obj         = new report();
				$j=0;
                foreach ($report as $assets_id => $i) {
																	
		         foreach ($i as $key => $val){
					 if ($key == 'ci_type_id') {
                            $report_exp[$j][$key] = $val;
                        }
				elseif ($key == 'assigned_on') {
                           $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        }
						elseif($key == 'installation_date'){
						$report_exp[$j][$key] = date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
					}
					elseif($key == 'expiry_date'){
						$report_exp[$j][$key] = date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
					}
					elseif($key == 'last_audit_date'){
						$report_exp[$j][$key] = date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
					}
						elseif ($key == 'description') {
                            $report_exp[$j][$key] = $val;
                        }
			    elseif($key == 'name' || $key == 'CNAME' || $key == 'domain_name' || $key == 'department_name' || $key == 'LNAME' || $key == 'usedby' || $key =='status' ){
				 $report_exp[$j][$key] = $val;
				}
                elseif($key == 'impact_name' || $key == 'product_name' || $key =='vender_name' || $key == 'asset_state' || $key == 'depreciation_type' || $key == 'salvage'  || $key == 'warranty_year'  || $key == 'warranty_month'|| $key == 'asset_tag' || $key == 'licence_key' || $key == 'installation_path' || $key == 'first_name' || $key == 'last_name' || $key == 'email' || $key == 'fname' || $key == 'lname' || $key == 'clnt_fname' || $key == 'clnt_lname' || $key == 'licence_type' || $key == 'staff_id') {
						$report_exp[$j][$key] = $val;	
						}
                
                 elseif($key == 'details')
						{
						$text = $val;
						$new_val =  $report_obj->strip_html_tags($text);
						 $report_exp[$j][$key] = $new_val;
						}elseif($key!='group_name' && $key !='ci_type_id' && $key !='ci_property_type' && $key !='impact_id' && $key !='staff_id' && $key !='location_id' && $key !='vendor_id' && $key !='department_access' && $key !='product_id' && $key !='assets_state'  ) {
							$report_exp[$j][$key] = $val;
						}						
			}
                
					 //for hardware assets
					 if($report_exp[$j]['ci_type_id'] == 2)
					{
					$report_exp1[$j]['Asset_name'] = $report_exp[$j]['name'];
					$report_exp1[$j]['Department'] = $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
					$report_exp1[$j]['CI_Type'] = $report_exp[$j]['description'];
					$report_exp1[$j]['description'] = $report_exp[$j]['details'];
					if($report_exp[$j]['staff_id']===''){
						$report_exp1[$j]['managed_by'] = '---';
					}else{
						$report_exp1[$j]['managed_by'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					}
					$report_exp1[$j]['email_id'] = $report_exp[$j]['email'];
					if(isset($report_exp[$j]['usedby']))
					{
						$report_exp1[$j]['used by'] = $report_exp[$j]['usedby'];
					}elseif($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
					{
					$report_exp1[$j]['used by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
					}
					else{
					$report_exp1[$j]['used by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
					}
					$report_exp1[$j]['email'] = $report_exp[$j]['usedby'];
					$report_exp1[$j]['Assigned_on'] = $report_exp[$j]['assigned_on'];
					$report_exp1[$j]['Location'] = $report_exp[$j]['LNAME'];
					$report_exp1[$j]['impact'] = $report_exp[$j]['impact_name'];
					$report_exp1[$j]['product'] = $report_exp[$j]['product_name'];
					$report_exp1[$j]['vender'] = $report_exp[$j]['vender_name'];
					$report_exp1[$j]['warranty_year'] = $report_exp[$j]['warranty_year'];
					$report_exp1[$j]['warranty_month'] = $report_exp[$j]['warranty_month'];
					$report_exp1[$j]['acquisition_date'] = $report_exp[$j]['installation_date'];
					$report_exp1[$j]['warranty_expiry_date'] = $report_exp[$j]['expiry_date'];
					$report_exp1[$j]['asset_state'] = $report_exp[$j]['asset_state'];
					$report_exp1[$j]['asset_tag'] = $report_exp[$j]['asset_tag'];
					$report_exp1[$j]['serial_no'] = $report_exp[$j]['licence_key'];
					$report_exp1[$j]['last_audit_date'] = $report_exp[$j]['last_audit_date'];
					$report_exp1[$j]['cost'] = $report_exp[$j]['installation_path'];
					$report_exp1[$j]['salvage'] = $report_exp[$j]['salvage'];
					$report_exp1[$j]['depreciation_type'] = $report_exp[$j]['depreciation_type'];
					$report_exp1[$j]['expiry_date'] = $report_exp[$j]['status'];
					}
					
					// for software assets
					 elseif($report_exp[$j]['ci_type_id'] == 3)
					 {
					$report_exp1[$j]['Asset_name'] = $report_exp[$j]['name'];
					$report_exp1[$j]['Department'] = $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
					$report_exp1[$j]['CI_Type'] = $report_exp[$j]['description'];
					$report_exp1[$j]['description'] = $report_exp[$j]['details'];
					if($report_exp[$j]['staff_id']===''){
						$report_exp1[$j]['managed_by'] = '---';
					}else{
						$report_exp1[$j]['managed_by'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					}
					$report_exp1[$j]['email_id'] = $report_exp[$j]['email'];
					if($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
					{
					$report_exp1[$j]['used_by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
					}
					else{
					$report_exp1[$j]['used_by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
					}
					$report_exp1[$j]['email'] = $report_exp[$j]['usedby'];
					$report_exp1[$j]['Assigned_on'] = $report_exp[$j]['assigned_on'];
					$report_exp1[$j]['Location'] = $report_exp[$j]['LNAME'];
					$report_exp1[$j]['impact'] = $report_exp[$j]['impact_name'];
					$report_exp1[$j]['product'] = $report_exp[$j]['product_name'];
					$report_exp1[$j]['vender'] = $report_exp[$j]['vender_name'];
					$report_exp1[$j]['warranty_year'] = $report_exp[$j]['warranty_year'];
					$report_exp1[$j]['warranty_month'] = $report_exp[$j]['warranty_month'];
					$report_exp1[$j]['installation_date'] = $report_exp[$j]['installation_date'];
					$report_exp1[$j]['licence_expiry_date'] = $report_exp[$j]['expiry_date'];
					$report_exp1[$j]['licence_type'] = $report_exp[$j]['licence_type'];
					$report_exp1[$j]['cost'] = $report_exp[$j]['asset_tag'];
					$report_exp1[$j]['licence_key'] = $report_exp[$j]['licence_key'];
					$report_exp1[$j]['installation_path'] = $report_exp[$j]['installation_path'];
					$report_exp1[$j]['last_audit_date'] = $report_exp[$j]['last_audit_date'];
					$report_exp1[$j]['expiry_date'] = $report_exp[$j]['status'];
						 
					 }
					 //for other types of assets
					 else
					{
				    $report_exp1[$j]['Asset_name'] = $report_exp[$j]['name'];
					$report_exp1[$j]['Department'] = $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
					$report_exp1[$j]['CI_Type'] = $report_exp[$j]['description'];
					$report_exp1[$j]['description'] = $report_exp[$j]['details'];
					if($report_exp[$j]['staff_id']===''){
						$report_exp1[$j]['managed_by'] = '---';
					}else{
						$report_exp1[$j]['managed_by'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					}
					$report_exp1[$j]['email_id'] = $report_exp[$j]['email'];
					 if(isset($report_exp[$j]['usedby']))
					{
						$report_exp1[$j]['used by'] = $report_exp[$j]['usedby'];
					}elseif($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
					{
					$report_exp1[$j]['used by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
					}
					else{
					$report_exp1[$j]['used by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
					}
					
					$report_exp1[$j]['Assigned_on'] = $report_exp[$j]['assigned_on'];
					$report_exp1[$j]['Location'] = $report_exp[$j]['LNAME'];
					$report_exp1[$j]['impact'] = $report_exp[$j]['impact_name'];
						 
					}
					if($report_exp[$j]['ci_type_id'] == 2){
						foreach($report_exp[$j] as $key=>$val){
						if($key!='group_name' && $key !='ci_type_id' && $key !='ci_property_type' && $key !='domain_name' && $key !='department_name' && $key !='clnt_fname' && $key !='clnt_lname' && $key !='fname' && $key !='first_name' && $key !='last_name' && $key !='lname' && $key !='impact_id' && $key !='staff_id' && $key !='location_id' && $key !='vendor_id' && $key !='department_access' && $key !='product_id' && $key !='asset_state' && $key!='email' && $key!='name' && $key!='assigned_on' && $key!='impact_name' && $key !='licence_type' && $key!='product_name' && $key!='vender_name' && $key!='usedby' && $key!='expiry_date' && $key!='status' && $key!=''){
							$report_exp1[$j][$key]=$val;
						}
					}
				 } elseif($report_exp[$j]['ci_type_id'] == 3){
					 
					 foreach($report_exp[$j] as $key=>$val){
						 
						 
						if($key!='group_name' && $key!='ci_type_id' && $key !='ci_property_type' && $key !='domain_name' && $key !='department_name' && $key !='first_name' && $key !='last_name' && $key !='clnt_fname' && $key !='clnt_lname' && $key !='impact_id' && $key !='fname' && $key !='lname' && $key !='staff_id' && $key !='location_id' && $key !='vendor_id' && $key !='department_access' && $key !='product_id' && $key !='asset_state' && $key!='assets_id' && $key!='name' && $key!='usedby' && $key!='assigned_on' && $key !='attachment_id' && $key !='warranty_year' && $key !='warranty_month' && $key !='installation_date' && $key!='expiry_date' && $key !='asset_tag' && $key !='licence_key' && $key !='installation_path' && $key !='last_audit_date' && $key !='salvage' && $key !='depreciation_type' && $key !='impact_name' && $key !='product_name' && $key !='vender_name'   && $key !='location_name' && $key!='email' && $key!='status' && $key !=''){
							$report_exp1[$j][$key]=$val;
						}
					}
				 }else{
					  
					 
					  foreach($report_exp[$j] as $key=>$val){        
						 
						 
						if($key!='group_name' && $key!='ci_type_id' && $key !='ci_property_type' && $key !='domain_name' && $key !='department_name' && $key !='first_name' && $key !='last_name' && $key !='clnt_fname' && $key !='clnt_lname' && $key !='impact_id' && $key !='fname' && $key !='lname' && $key !='staff_id' && $key !='location_id' && $key !='vendor_id' && $key !='department_access' && $key !='product_id' && $key !='asset_state' && $key!='assets_id' && $key!='name' && $key!='usedby' && $key!='assigned_on' && $key !='attachment_id' && $key !='warranty_year' && $key !='warranty_month' && $key !='installation_date' && $key!='expiry_date' && $key !='asset_tag' && $key !='licence_key' && $key !='installation_path' && $key !='last_audit_date' && $key !='salvage' && $key !='depreciation_type' && $key !='impact_name' && $key !='product_name' && $key !='vender_name' && $key !='licence_type' && $key !='location_name' && $key!='email' && $key!='status' && $key !=''){
						
							$report_exp1[$j][$key]=$val;
							
						}
						 
					}
					 
				 }
                    $j++;
                }
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;	// To display title which shows on report form
				//$report_exp1['Report_title'] = $operation['export_name']; // To display title which entered in request report popup form
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
																
																if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            }else
                include(VIS_PATH_TEMPLATE . 'assets_groupby_location_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
         $database->database_disconnect();
        return true;
	}
	
	function task_by_status_report($operation)
	{
	 	global $vis_language;
        $report_title = $vis_language['TXTSTATUSTASK'];
        $export       = 1;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
             $database = new class_database();
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object = new class_ticket($database);
        $staff      = $tkt_object->get_staff_list(VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
        if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
		    if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
					
        
				 if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
				
				if($data['vis_status']==0){
					echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTVALIDSTATUS'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
					return false;	
				}
				
				if ($data['vis_status'] == 1){
					$filter = VIS_TABLE_PREFIX.'task.task_status=1 and ('.VIS_TABLE_PREFIX.'task.enddate-unix_timestamp()) > 0 ';
				}elseif ($data['vis_status'] == 2){
					$filter = '('.VIS_TABLE_PREFIX.'task.enddate-unix_timestamp()) <= 0 and ('.VIS_TABLE_PREFIX.'task.task_status!=3 and '.VIS_TABLE_PREFIX.'task.task_status!=4)';
				}elseif ($data['vis_status'] == 3){
					$filter = VIS_TABLE_PREFIX.'task.task_status=3';
				}elseif ($data['vis_status'] == 4){
					$filter = VIS_TABLE_PREFIX.'task.task_status=4';
				}elseif ($data['vis_status'] == 5){
					$filter = '('.VIS_TABLE_PREFIX.'task.enddate-unix_timestamp()) > 0 and '.VIS_TABLE_PREFIX.'task.task_status=5';
				} 
				$filter .= ' and '.VIS_TABLE_PREFIX.'task.autotask_flag=0';
				//$filter=($filter!='')?$filter.' and '.VIS_TABLE_PREFIX.'task.staff_id='.VIS_STAFF_ID:' '.VIS_TABLE_PREFIX.'task.staff_id='.VIS_STAFF_ID;
				//$data['staff_id']=VIS_STAFF_ID;
            }
            include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
           	$object = new class_report($database);
          	$report = $object->task_groupby_status_report($filter, $staff_id, VIS_LOCATION_AREA);
			if (isset($operation['vis_export']) && $operation['vis_export'] == 1){
			
				$filtern=VIS_TABLE_PREFIX.'customgroup.location = 13';
				$custom_fields_details=$object->task_custom_field($filtern); 
				
				//start custom data
				$temp=array();
        $temp1=array();		
        $temp2=array();	 
		$a=0;
		  
		for($i=0;$i<count($custom_fields_details);$i++) 
		{
			$a=$custom_fields_details[$i]['task_id'];
			if($custom_fields_details[$i]['task_id']!=$temp1[$a]['task_id'])
			{$temp['task_id']=$custom_fields_details[$i]['task_id'];
		    $a=$custom_fields_details[$i]['task_id'];
			}
			$temp[$custom_fields_details[$i]['field_name']]=$custom_fields_details[$i]['content'];
			$temp1[$a]=$temp;			 
		}
 
			$temp2=reset($temp1);
		     
			foreach($report as $key => $csm)
			{
				if(in_array($report[$key]['task_id'],$temp1[$csm['task_id']])){
					foreach($temp1[$csm['task_id']] as $keys=>$val){
					if($keys!='task_id')
					$report[$key][$keys]=$val;
				}
			}else{
					foreach($temp2 as $keys=>$val){
					if($keys!='task_id')
					$report[$key][$keys]='';
					}
				}
			}
			 
				// get custom filed data loop done
				
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                $j = 0;
				foreach ($report as $i) {
                    foreach ($i as $key => $val) {
                        if ($key == 'task_title') {
                            $report_exp[$j][$key] = $val;
                        }elseif ($key == 'startdate') {
                            $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        }elseif ($key == 'enddate') {
                            $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        }elseif ($key == 'title') {
                            $report_exp[$j][$key] = $val;
                        }elseif ($key == 'task_status') {
							if($report[$j]['enddate_diff']<=0){
								if($val!=3 && $val!=4 && $val!=5){
									$report_exp[$j][$key] =$vis_language['TXTOVERDUE']; 
								}else{
									if($val==1) {
										$report_exp[$j][$key] =$vis_language['TXTINPROGRESS']; 
									}else if($val==3){
										$report_exp[$j][$key] =$vis_language['TXTCOMPLETED']; 
									}else if($val==4){ 
										$report_exp[$j][$key] =$vis_language['TXTCANCELED'];  
									}else if($val==5){ 
										$report_exp[$j][$key] =$vis_language['TXTNOTSTARTED']; 
									}
								} 
							}else{ 
								if($val==1) {
									$report_exp[$j][$key] =$vis_language['TXTINPROGRESS']; 
								}else if($val==2){
									$report_exp[$j][$key] =$vis_language['TXTOVERDUE'];  
								}else if($val==3){
									$report_exp[$j][$key] =$vis_language['TXTCOMPLETED'];  
								}else if($val==4){ 
									$report_exp[$j][$key] =$vis_language['TXTCANCELED']; 
								}else if($val==5){ 
									$report_exp[$j][$key] =$vis_language['TXTNOTSTARTED']; 	
								}
							} 
						} elseif($key!='task_title' && $key!='startdate' && $key!='enddate' && $key!='title' && $key!='enddate_diff' && $key!='task_status' && $key!='priority'){
							$report_exp[$j][$key] = $val;
						}
					}
 
					// If custom field is present then create column form them
					 
					// Custom field column creation ends here
					 
					$j++;
                }
               	$completed_date = date('Y-m-d H:i:s');
				$report_exp['Report_title'] = $report_title;
				
				$filename = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {                
                    $export->export_xlsx($report_exp, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
																
															if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            }
            else{
                include(VIS_PATH_TEMPLATE . 'task_by_status_report.php'); 
													}
            
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        
        return true; 
		}	
	
	
	
	
	
	
// comman function
//date range validation check
function report_date_range_validation($from_date, $to_date)
{
        global $vis_language;
        $vis_from_date = strtotime($from_date);
        $vis_to_date   = strtotime($to_date);
        
        $datetime1 = new DateTime($from_date);
        $datetime2 = new DateTime($to_date);
        
        $dateDiff = get_timespan_difference($datetime1, $datetime2);
        
        if ($vis_from_date > $vis_to_date) {
            return array(
                false,
                $vis_language['MSGFROMLESSTHANTO']
            );
        } else if ($dateDiff['y'] > 0) {
            return array(
                false,
                $vis_language['MSGDURATIONLESSTHANTHREEMONTHS']
            );
        } elseif ($dateDiff['m'] > 3) {
            return array(
                false,
                $vis_language['MSGDURATIONLESSTHANTHREEMONTHS']
            );
        } elseif ($dateDiff['m'] == 3) {
            if ($dateDiff['d'] > 0) {
                return array(
                    false,
                    $vis_language['MSGDURATIONLESSTHANTHREEMONTHS']
                );
            }
        }
        return array(
            true,
            ''
        );
    }	
function custom_date_diffrence($from_date, $to_date)
{
	$from_date = strtotime($from_date);
 $to_date   = strtotime($to_date);
			$diff = abs($to_date - $from_date);
		$years = floor($diff / (365*60*60*24));
		$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
		$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
//			$dateDiff = get_timespan_difference($from_date, $to_date);
            
            if($from_date > $to_date){
                return array(false, $vis_language['MSGFROMLESSTHANTO']);
            }else if($years>0){
               return array(false, $vis_language['MSGDURATIONLESSTHANTHREEMONTHS']);
            }elseif($months>3){
                return array(false, $vis_language['MSGDURATIONLESSTHANTHREEMONTHS']);
            }elseif($months<=3){
                   if($months==3){
																					if($days>0){
																									return array(false, $vis_language['MSGDURATIONLESSTHANTHREEMONTHS']);
																						}	
																			}
														}
														return array(
            true,
            ''
        );
}
//email send				
function email_send_download($operation)
{
	include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'report.php');
 $report_obj         = new report();
$report_obj->email_send_download($operation);
//echo true;
}
				
	function vis_show_subcategories($config_details,$call,$flag) {
		if($flag=='ci')
			$id='item_id';
		elseif($flag=='location')
			$id='location_id';
			
		for($i=0;$i<count($config_details);$i++) { 
				$options .='<option value="'.$config_details[$i][$id].'">'.$config_details[$i]['name'].'</option>';
				if(isset($config_details[$i]['subcat']) && $config_details[$i]['subcat']!=NULL) { 
					$options .=$this->vis_show_subcategories($config_details[$i]['subcat'],'sub_cat',$flag); 
				}
			}
		return $options;
	}			
	
	
	 function assets_asset_type_report($operation)
		{
		global $vis_language;
        $report_title = ucfirst(strtolower($vis_language['TXTASSETTYPE']));
        $export       = 1;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
					 
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object = new class_ticket($database);
        $staff      = $tkt_object->get_staff_list(VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
		if(VIS_LOCATION_AREA==2){						
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
        
			$config_details=$object->get_configuration_item($filter='', 0, -1, $sort_by='', VIS_LOCATION_AREA); 
   
						}
	      
		 if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
				
                if ($data['location_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTASSETTYPE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
				
				if ($data['location_id'] != -1) {
				$filter = 'cmast.ci_type_id='.$data['location_id']; 
				
				}
				$filter1 =' where  avis_customfield_data.parent_id ='.$data['location_id'];		
				$filter=$filter.'#'.$filter1;
			}
			$temp_filtr = explode('#',$filter);
					 
					$filter = $temp_filtr[0];
					$filter1= $temp_filtr[1];
			 include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
             $object = new class_report($database);
            // $report = $object->assets_by_asset_type($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
			 $report = array();
             list($custom_fields_details,$report) = $object->assets_by_asset_type($filter,$filter1, VIS_STAFF_ID, VIS_LOCATION_AREA);
			  $temp=array();
         $temp1=array();		
         $temp2=array();	 
		 $a=0;
		  
		for($i=0;$i<count($custom_fields_details);$i++) 
		{
			$a=$custom_fields_details[$i]['assets_id'];
			if($custom_fields_details[$i]['assets_id']!=$temp1[$a]['assets_id'])
			{$temp['assets_id']=$custom_fields_details[$i]['assets_id'];
		    $a=$custom_fields_details[$i]['assets_id'];
			}
			$temp[$custom_fields_details[$i]['field_name']]=$custom_fields_details[$i]['content'];
			$temp1[$a]=$temp;			 
		}
 
			$temp2=reset($temp1);
		     
			foreach($report as $key => $csm)
			{
				if(in_array($report[$key]['assets_id'],$temp1[$csm['assets_id']])){
					foreach($temp1[$csm['assets_id']] as $keys=>$val){
					if($keys!='assets_id')
					$report[$key][$keys]=$val;
				}
			}else{
					foreach($temp2 as $keys=>$val){
					if($keys!='assets_id')
					$report[$key][$keys]='';
					}
				}
		   
			
																									
													}
			  
													
													if($report!=1){
													
														for($i=0; $i<count($report); $i++){
															
														$is_expired =0;
														if($report[$i]['ci_type_id']==2 || $report[$i]['ci_type_id']==3){
															$total_month = ($report[$i]['warranty_year']*12)+$report[$i]['warranty_month'];
															$current_date = strtotime(date('Y-m-d h:i:s'));
															//$expiry_date = strtotime(date('Y-m-d h:i:s', $report[$i]['installation_date']) . $total_month.' month');
															if($report[$i]['warranty_year']==0 && $report[$i]['warranty_month']==0){
																$expiry_date = $report[$i]['expiry_date'];
															}else{
																$expiry_date = strtotime($total_month.' month',$report[$i]['installation_date']);
															}
															if($expiry_date <= $current_date){
																//$report[$i]['status']=format_datetime($expiry_date);
																$report[$i]['status']=date(VIS_EDITDATETIMEPICKER_FORMAT,$expiry_date);
															} else{
																//$report[$i]['status']='--';
																$report[$i]['status']=date(VIS_EDITDATETIMEPICKER_FORMAT,$expiry_date);
																//format_datetime_rpt($expiry_date,$staff_id);
															}
														}else{
															$report[$i]['status']='--';
														}
																												
															}//end for
																									
													}
												
					 				 			
		     if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export         = new export();
				include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'report.php');
                $report_obj         = new report();
				$j=0;
				foreach ($report as $assets_id => $i) {
			    foreach ($i as $key => $val){
				if ($key == 'ci_type_id') {
                        $report_exp[$j][$key] = $val;
                        }elseif ($key == 'assigned_on') {
                           $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        }elseif($key == 'installation_date'){
						$report_exp[$j][$key] = date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
				}elseif($key == 'expiry_date'){
						$report_exp[$j][$key] = date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
				}elseif($key == 'last_audit_date'){
						$report_exp[$j][$key] = date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
				}elseif ($key == 'description') {
                            $report_exp[$j][$key] = $val;
                        }
				elseif($key == 'name' || $key == 'ci_type' || $key == 'domain_name' || $key == 'department_name' || $key == 'location' ||  $key == 'usedby' || $key == 'status')	{			
				$report_exp[$j][$key] = $val;
				}
                 elseif($key == 'impact_name' || $key == 'product_name' || $key =='vender_name' || $key == 'asset_state' || $key == 'depreciation_type' || $key == 'salvage'  || $key == 'warranty_year'  || $key == 'warranty_month'|| $key == 'asset_tag' || $key == 'licence_key' || $key == 'installation_path' || $key == 'first_name' || $key == 'last_name' || $key == 'email' || $key == 'fname' || $key == 'lname' || $key == 'clnt_fname' || $key == 'clnt_lname' || $key == 'licence_type' || $key == 'usedby' || $key == 'staff_id') {
						$report_exp[$j][$key] = $val;	
						}
                /*elseif($key == 'installation_date' || $key == 'expiry_date'  || $key == 'last_audit_date'){
						$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);	
						}*/
                 elseif($key == 'details')
						{
						$text = $val;
						$new_val =  $report_obj->strip_html_tags($text);
						 $report_exp[$j][$key] = $new_val;
						}elseif( $key!='clnt_fname' && $key!='clnt_lname' ) {
							$report_exp[$j][$key] = $val;
						}			
		    }
                
					 //for hardware assets
					 if($report_exp[$j]['ci_type_id'] == 2)
					{
					$report_exp1[$j]['Asset_name'] = $report_exp[$j]['name'];
					$report_exp1[$j]['Department'] = $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
					$report_exp1[$j]['CI_Type'] = $report_exp[$j]['description'];
					$report_exp1[$j]['description'] = $report_exp[$j]['details'];
					if($report_exp[$j]['staff_id']===''){
						$report_exp1[$j]['managed_by'] = '---';
					}else{
						$report_exp1[$j]['managed_by'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					}
					$report_exp1[$j]['email_id'] = $report_exp[$j]['email'];
					
					/*if(isset($report_exp[$j]['usedby']))
					{
						$report_exp1[$j]['used_by'] = $report_exp[$j]['usedby'];
					}elseif($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
					{
					$report_exp1[$j]['used_by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
					}
					else{
					$report_exp1[$j]['used_by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
					}*/
					if($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
					{
						$report_exp1[$j]['used_by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
					}
					else{
						$report_exp1[$j]['used_by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
					}
					 
					$report_exp1[$j]['Assigned_on'] = $report_exp[$j]['assigned_on'];
					$report_exp1[$j]['Location'] = $report_exp[$j]['location'];
					$report_exp1[$j]['impact'] = $report_exp[$j]['impact_name'];
					$report_exp1[$j]['product'] = $report_exp[$j]['product_name'];
					$report_exp1[$j]['vender'] = $report_exp[$j]['vender_name'];
					$report_exp1[$j]['warranty_year'] = $report_exp[$j]['warranty_year'];
					$report_exp1[$j]['warranty_month'] = $report_exp[$j]['warranty_month'];
					$report_exp1[$j]['acquisition_date'] = $report_exp[$j]['installation_date'];
					$report_exp1[$j]['warranty_expiry_date'] = $report_exp[$j]['expiry_date'];
					$report_exp1[$j]['asset_state'] = $report_exp[$j]['asset_state'];
					$report_exp1[$j]['asset_tag'] = $report_exp[$j]['asset_tag'];
					$report_exp1[$j]['serial_no'] = $report_exp[$j]['licence_key'];
					$report_exp1[$j]['last_audit_date'] = $report_exp[$j]['last_audit_date'];
					$report_exp1[$j]['cost'] = $report_exp[$j]['installation_path'];
					$report_exp1[$j]['salvage'] = $report_exp[$j]['salvage'];
					$report_exp1[$j]['depreciation_type'] = $report_exp[$j]['depreciation_type'];
					$report_exp1[$j]['expiry_date'] = $report_exp[$j]['status'];
					}
					
					// for software assets
					 elseif($report_exp[$j]['ci_type_id'] == 3)
					 {
					$report_exp1[$j]['Asset_name'] = $report_exp[$j]['name'];
					$report_exp1[$j]['Department'] = $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
					$report_exp1[$j]['CI_Type'] = $report_exp[$j]['description'];
					$report_exp1[$j]['description'] = $report_exp[$j]['details'];
					if($report_exp[$j]['staff_id']===''){
						$report_exp1[$j]['managed_by'] = '---';
					}else{
						$report_exp1[$j]['managed_by'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					}
					$report_exp1[$j]['email_id'] = $report_exp[$j]['email'];
					/*if(isset($report_exp[$j]['usedby']))
					{
						$report_exp1[$j]['used_by'] = $report_exp[$j]['usedby'];
					}elseif($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
					{
					$report_exp1[$j]['used_by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
					}
					else{
					$report_exp1[$j]['used_by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
					}*/
					if($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
					{
						$report_exp1[$j]['used_by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
					}
					else{
						$report_exp1[$j]['used_by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
					}
					 
					$report_exp1[$j]['Assigned_on'] = $report_exp[$j]['assigned_on'];
					$report_exp1[$j]['Location'] = $report_exp[$j]['location'];
					$report_exp1[$j]['impact'] = $report_exp[$j]['impact_name'];
					$report_exp1[$j]['product'] = $report_exp[$j]['product_name'];
					$report_exp1[$j]['vender'] = $report_exp[$j]['vender_name'];
					$report_exp1[$j]['warranty_year'] = $report_exp[$j]['warranty_year'];
					$report_exp1[$j]['warranty_month'] = $report_exp[$j]['warranty_month'];
					$report_exp1[$j]['installation_date'] = $report_exp[$j]['installation_date'];
					$report_exp1[$j]['licence_expiry_date'] = $report_exp[$j]['expiry_date'];
					$report_exp1[$j]['licence_type'] = $report_exp[$j]['licence_type'];
					$report_exp1[$j]['cost'] = $report_exp[$j]['asset_tag'];
					$report_exp1[$j]['licence_key'] = $report_exp[$j]['licence_key'];
					$report_exp1[$j]['installation_path'] = $report_exp[$j]['installation_path'];
					$report_exp1[$j]['last_audit_date'] = $report_exp[$j]['last_audit_date'];
					$report_exp1[$j]['expiry_date'] = $report_exp[$j]['status'];	 
					 }
					 //for other types of assets
					 else
					{
				    $report_exp1[$j]['Asset_name'] = $report_exp[$j]['name'];
					$report_exp1[$j]['Department'] = $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
					$report_exp1[$j]['CI_Type'] = $report_exp[$j]['description'];
					$report_exp1[$j]['description'] = $report_exp[$j]['details'];
					if($report_exp[$j]['staff_id']===''){
						$report_exp1[$j]['managed_by'] = '---';
					}else{
						$report_exp1[$j]['managed_by'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					}
					$report_exp1[$j]['email_id'] = $report_exp[$j]['email'];
					/*if(isset($report_exp[$j]['usedby']))
					{
						$report_exp1[$j]['used_by'] = $report_exp[$j]['usedby'];
					}elseif($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
					{
					$report_exp1[$j]['used_by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
					}
					else{
					$report_exp1[$j]['used_by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
					}*/
					if($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
					{
						$report_exp1[$j]['used_by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
					}
					else{
						$report_exp1[$j]['used_by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
					}
					
					$report_exp1[$j]['Assigned_on'] = $report_exp[$j]['assigned_on'];
					$report_exp1[$j]['Location'] = $report_exp[$j]['location'];
					$report_exp1[$j]['impact'] = $report_exp[$j]['impact_name'];
					$report_exp1[$j]['expiry_date'] = $report_exp[$j]['status'];	 
					}
					 if($report_exp[$j]['ci_type_id'] == 2){
							 
						foreach($report_exp[$j] as $key=>$val){
						 
						if($key!='group_name' && $key !='ci_type_id' && $key !='ci_property_type ' && $key !='domain_name' && $key !='department_name' && $key !='clnt_fname' && $key !='clnt_lname' && $key !='fname' && $key !='firstname' && $key !='lastname' && $key !='lname' && $key !='impact_id' && $key !='staff_id' && $key !='location_id' && $key !='vendor_id' && $key !='department_access' && $key !='product_id' && $key !='assets_state' && $key!='email' && $key!='name' && $key!='assigned_on' && $key!='impact_name' && $key!='product_name' && $key!='vender_name' && $key!='usedby' && $key !='location' && $key !='licence_type' && $key !='domain_id' && $key !='CI_Type' && $key !='expiry_date' && $key !='status' && $key!='') {
						
							$report_exp1[$j][$key]=$val;
							
						}
						 
					}
				 } elseif($report_exp[$j]['ci_type_id'] == 3){
					 
					 foreach($report_exp[$j] as $key=>$val){
						 
						if($key!='group_name' && $key !='ci_property_type ' && $key !='ci_type_id' && $key !='domain_name' && $key !='department_name' && $key !='clnt_fname' && $key !='clnt_lname' && $key !='impact_id' && $key !='fname' && $key !='lname' && $key !='staff_id' && $key !='location_id' && $key !='vendor_id' && $key !='department_access' && $key !='product_id' && $key !='assets_state' && $key!='email' && $key!='name' && $key!='assigned_on' && $key!='impact_name' && $key!='product_name' && $key!='vender_name' && $key!='usedby' && $key !='location' && $key !='CI_Type' && $key !='expiry_date' && $key !='domain_id' && $key !='status' && $key!='') {
						
							$report_exp1[$j][$key]=$val;
							
						}
						 
					}
					 
				 }else{
					  
					 
					  foreach($report_exp[$j] as $key=>$val){        
						
						if($key!='group_name' && $key!='ci_type_id' && $key !='ci_property_type' && $key !='domain_name' && $key !='department_name' && $key !='first_name' && $key !='last_name' && $key !='clnt_fname' && $key !='clnt_lname' && $key !='impact_id' && $key !='fname' && $key !='lname' && $key !='staff_id' && $key !='location_id' && $key !='vendor_id' && $key !='department_access' && $key !='product_id' && $key !='asset_state' && $key!='assets_id' && $key!='name' && $key!='usedby' && $key!='assigned_on' && $key !='attachment_id' && $key !='warranty_year' && $key !='warranty_month' && $key !='installation_date' && $key!='expiry_date' && $key !='asset_tag' && $key !='licence_key' && $key !='installation_path' && $key !='last_audit_date' && $key !='salvage' && $key !='depreciation_type' && $key !='impact_name' && $key !='product_name' && $key !='domain_id' && $key !='vender_name' && $key !='licence_type' && $key !='location_name' && $key!='email' && $key !='status' && $key !=''){ 
						 
						
							$report_exp1[$j][$key]=$val;
							
						}
						 
					}
					 
				 }
					
                    $j++;
                }
				   
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = ucfirst(strtolower($report_title));	// To display title which shows on report form
				//$report_exp1['Report_title'] = $operation['export_name']; // To display title which entered in request report popup form
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
																
																if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            }else
                include(VIS_PATH_TEMPLATE . 'asset_by_asset_type_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
         $database->database_disconnect();
        return true;
	}
	
	
	
	function assets_asset_product_report($operation)
	{
		global $vis_language;
        $report_title = $vis_language['TXTASSTEPRODUCT'];
        $export       = 1;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
		  
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object = new class_ticket($database);
        $staff      = $tkt_object->get_staff_list(VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
		if(VIS_LOCATION_AREA==2){						
			include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
			$object=new class_setting($database);
			include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'subsetting.php');		 
			$sub_object=new class_subsetting($database);
			//echo '<pre>';
			$citype_details=$object->get_configuration_item_category('','is_enabled=1'); 
			list($ci_product_info, $ci_product_details)=$object->get_ci_product(VIS_LOCATION_AREA); 
			list($vendor_info, $vendor_details)=$object->get_vendor( VIS_LOCATION_AREA); 
		} 
		 if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                
				if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
				
				if($data['product_id'] == -1 && $data['vis_ci_type'] == '' && $data['vendor_id'] == -1){ // Newly added condition by Sachin E.
					echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
				}
				
                if ($data['product_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTPRODUCT'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
				if ($data['vis_ci_type'] == '') {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTITILSELECTCITYPE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
				
				if ($data['vendor_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTVENDOR'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
				
				if ($data['product_id'] != -1 && $data['vendor_id']!=-1 && $data['vis_ci_type']!='') {
					$filter = ' cmast.product_id='.$data['product_id']. ' and cmast.vendor_id='.$data['vendor_id'].' and cmast.ci_type_id = '.$data['vis_ci_type']; 
				}
				
			}
			include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
			$object = new class_report($database);
			$report = $object->assets_by_porduct_type($filter, VIS_STAFF_ID, VIS_LOCATION_AREA); 
													
			if($report!=1){
			
				for($i=0; $i<count($report); $i++){
					
					$is_expired =0;
				if($report[$i]['ci_type_id']==2 || $report[$i]['ci_type_id']==3){
					$total_month = ($report[$i]['warranty_year']*12)+$report[$i]['warranty_month'];
					$current_date = strtotime(date('Y-m-d h:i:s'));
					//$expiry_date = strtotime(date('Y-m-d h:i:s', $report[$i]['installation_date']) . $total_month.' month');
					if($report[$i]['warranty_year']==0 && $report[$i]['warranty_month']==0){
						$expiry_date = $report[$i]['expiry_date'];
					}else{
						$expiry_date = strtotime($total_month.' month',$report[$i]['installation_date']);
					}
					if($expiry_date <= $current_date){
						//$report[$i]['status']=format_datetime($expiry_date);
						$report[$i]['status']=date(VIS_EDITDATETIMEPICKER_FORMAT,$expiry_date);
							} else{
					//$report[$i]['status']='--';
					$report[$i]['status']=date(VIS_EDITDATETIMEPICKER_FORMAT,$expiry_date);
					//format_datetime_rpt($expiry_date,$staff_id);
				}
				}else{
					$report[$i]['status']='--';
				}
																		
					}//end for
															
			}
												
													
		     if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export         = new export();
				include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'report.php');
                $report_obj         = new report();
				$j=0;
                foreach ($report as $assets_id => $i) {
				foreach ($i as $key => $val){
				if ($key == 'ci_type_id') {
                            $report_exp[$j][$key] = $val;
                        }elseif ($key == 'description') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'assigned_on') {
                           $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        }elseif($key == 'installation_date'){
					$report_exp[$j][$key] = date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
				} elseif($key == 'expiry_date'){
					$report_exp[$j][$key] = date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
				} elseif($key == 'last_audit_date'){
					$report_exp[$j][$key] = date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
				}
				elseif($key == 'name' || $key == 'CNAME' || $key == 'domain_name' || $key == 'department_name' || $key == 'LNAME' || $key == 'usedby' ||  $key =='porduct_name' || $key == 'vendor_name' || $key =='config_item' || $key == 'status'){
				$report_exp[$j][$key] = $val;
				} 
				elseif($key == 'impact_name' || $key == 'asset_state' || $key == 'depreciation_type' || $key == 'salvage'  || $key == 'warranty_year'  || $key == 'warranty_month'|| $key == 'asset_tag' || $key == 'licence_key' || $key == 'installation_path' || $key == 'first_name' || $key == 'last_name' || $key == 'email' || $key == 'fname' || $key == 'lname' || $key == 'clnt_fname' || $key == 'clnt_lname' || $key == 'licence_type' || $key == 'staff_id') {
						$report_exp[$j][$key] = $val;	
						}
				/*elseif($key == 'installation_date' || $key == 'expiry_date'  || $key == 'last_audit_date'){
						$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);	
						}*/
				elseif($key == 'details')
						{
						$text = $val;
						$new_val =  $report_obj->strip_html_tags($text);
						 $report_exp[$j][$key] = $new_val;
						}
			}
                     //for hardware assets
					 if($report_exp[$j]['ci_type_id'] == 2)
					{
					$report_exp1[$j]['Asset_name'] = $report_exp[$j]['name'];
					$report_exp1[$j]['Department'] = $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
					$report_exp1[$j]['CI_Type'] = $report_exp[$j]['config_item'];
					$report_exp1[$j]['description'] = $report_exp[$j]['details'];
					if($report_exp[$j]['staff_id']===''){
						$report_exp1[$j]['managed_by'] = '---';
					}else{
						$report_exp1[$j]['managed_by'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					}
					$report_exp1[$j]['email_id'] = $report_exp[$j]['email'];
					if($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
					{
					$report_exp1[$j]['used_by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
					}
					else{
					$report_exp1[$j]['used_by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
					}
					$report_exp1[$j]['email'] = $report_exp[$j]['usedby'];
					$report_exp1[$j]['Assigned_on'] = $report_exp[$j]['assigned_on'];
					$report_exp1[$j]['Location'] = $report_exp[$j]['LNAME'];
					$report_exp1[$j]['impact'] = $report_exp[$j]['impact_name'];
					$report_exp1[$j]['product'] = $report_exp[$j]['porduct_name'];
					$report_exp1[$j]['vender'] = $report_exp[$j]['vendor_name'];
					$report_exp1[$j]['warranty_year'] = $report_exp[$j]['warranty_year'];
					$report_exp1[$j]['warranty_month'] = $report_exp[$j]['warranty_month'];
					$report_exp1[$j]['acquisition_date'] = $report_exp[$j]['installation_date'];
					$report_exp1[$j]['warranty_expiry_date'] = $report_exp[$j]['expiry_date'];
					$report_exp1[$j]['asset_state'] = $report_exp[$j]['asset_state'];
					$report_exp1[$j]['asset_tag'] = $report_exp[$j]['asset_tag'];
					$report_exp1[$j]['serial_no'] = $report_exp[$j]['licence_key'];
					$report_exp1[$j]['last_audit_date'] = $report_exp[$j]['last_audit_date'];
					$report_exp1[$j]['cost'] = $report_exp[$j]['installation_path'];
					$report_exp1[$j]['salvage'] = $report_exp[$j]['salvage'];
					$report_exp1[$j]['depreciation_type'] = $report_exp[$j]['depreciation_type'];
					$report_exp1[$j]['expiry_date'] = $report_exp[$j]['status'];
					}
					
					// for software assets
					 elseif($report_exp[$j]['ci_type_id'] == 3)
					 {
					$report_exp1[$j]['Asset_name'] = $report_exp[$j]['name'];
					$report_exp1[$j]['Department'] = $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
					$report_exp1[$j]['CI_Type'] = $report_exp[$j]['config_item'];
					$report_exp1[$j]['description'] = $report_exp[$j]['details'];
					if($report_exp[$j]['staff_id']===''){
						$report_exp1[$j]['managed_by'] = '---';
					}else{
						$report_exp1[$j]['managed_by'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					}
					$report_exp1[$j]['email_id'] = $report_exp[$j]['email'];
					if($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
					{
					$report_exp1[$j]['used_by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
					}
					else{
					$report_exp1[$j]['used_by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
					}
					$report_exp1[$j]['email'] = $report_exp[$j]['usedby'];
					$report_exp1[$j]['Assigned_on'] = $report_exp[$j]['assigned_on'];
					$report_exp1[$j]['Location'] = $report_exp[$j]['LNAME'];
					$report_exp1[$j]['impact'] = $report_exp[$j]['impact_name'];
					$report_exp1[$j]['product'] = $report_exp[$j]['porduct_name'];
					$report_exp1[$j]['vender'] = $report_exp[$j]['vendor_name'];
					$report_exp1[$j]['warranty_year'] = $report_exp[$j]['warranty_year'];
					$report_exp1[$j]['warranty_month'] = $report_exp[$j]['warranty_month'];
					$report_exp1[$j]['installation_date'] = $report_exp[$j]['installation_date'];
					$report_exp1[$j]['licence_expiry_date'] = $report_exp[$j]['expiry_date'];
					$report_exp1[$j]['licence_type'] = $report_exp[$j]['licence_type'];
					$report_exp1[$j]['cost'] = $report_exp[$j]['asset_tag'];
					$report_exp1[$j]['licence_key'] = $report_exp[$j]['licence_key'];
					$report_exp1[$j]['installation_path'] = $report_exp[$j]['installation_path'];
					$report_exp1[$j]['last_audit_date'] = $report_exp[$j]['last_audit_date'];
					$report_exp1[$j]['expiry_date'] = $report_exp[$j]['status'];
						 
					 }
					 //for other types of assets
					 else
					{
				    $report_exp1[$j]['Asset_name'] = $report_exp[$j]['name'];
					$report_exp1[$j]['Department'] = $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
					$report_exp1[$j]['CI_Type'] = $report_exp[$j]['config_item'];
					$report_exp1[$j]['description'] = $report_exp[$j]['details'];
					if($report_exp[$j]['staff_id']===''){
						$report_exp1[$j]['managed_by'] = '---';
					}else{
						$report_exp1[$j]['managed_by'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					}
					$report_exp1[$j]['email_id'] = $report_exp[$j]['email'];
					if($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
					{
					$report_exp1[$j]['used_by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
					}
					else{
					$report_exp1[$j]['used_by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
					}
					$report_exp1[$j]['email'] = $report_exp[$j]['usedby'];
					$report_exp1[$j]['Assigned_on'] = $report_exp[$j]['assigned_on'];
					$report_exp1[$j]['Location'] = $report_exp[$j]['LNAME'];
					$report_exp1[$j]['impact'] = $report_exp[$j]['impact_name'];
					$report_exp1[$j]['expiry_date'] = $report_exp[$j]['status'];
						 
					}
					
                    $j++;
                }
 
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;	// To display title which shows on report form
				//$report_exp1['Report_title'] = $operation['export_name']; // To display title which entered in request report popup form
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
																
				if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            }else
                include(VIS_PATH_TEMPLATE . 'asset_by_asset_product_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
         $database->database_disconnect();
        return true;
	}
	
	function hardware_assets_by_state_report($operation)
	{
		global $vis_language;
        $report_title = $vis_language['TXTHARDWREASSETBYSTATE'];
        $export       = 1;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
				 
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object = new class_ticket($database);
        $staff      = $tkt_object->get_staff_list(VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
	
						if(VIS_LOCATION_AREA==2){						
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'subsetting.php');		 
		$sub_object=new class_subsetting($database);
		//echo '<pre>';
		
	     $citype_details=$object->get_configuration_item_category('','is_enabled=1');
		 
		list($asset_state_info, $asset_state_details)=$object->get_asset_state('avis_itil_asset_state.is_enabled=1');  
		} 
		 if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
				
                if ($data['asset_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
				  
				
				if ($data['asset_id'] != -1) {
						 
							  $filter = 'cmast.assets_state='.$data['asset_id'].' and cmast.ci_type_id=2 '; 
						
					}
			}
			 include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
             $object = new class_report($database);
             $report = $object->assets_hardware_by_asset_state($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);  
													
						if($report!=1){
						
							for($i=0; $i<count($report); $i++){
								
								$is_expired =0;
								if($report[$i]['ci_type_id']==2){
									$total_month = ($report[$i]['warranty_year']*12)+$report[$i]['warranty_month'];
									$current_date = strtotime(date('Y-m-d h:i:s'));
									//$expiry_date = strtotime(date('Y-m-d h:i:s', $report[$i]['installation_date']) . $total_month.' month');
									if($report[$i]['warranty_year']==0 && $report[$i]['warranty_month']==0){
										$expiry_date = $report[$i]['expiry_date'];
									}else{
										$expiry_date = strtotime($total_month.' month',$report[$i]['installation_date']);
									}
									if($expiry_date <= $current_date){
										//$report[$i]['status']=format_datetime($expiry_date);
										$report[$i]['status']=date(VIS_EDITDATETIMEPICKER_FORMAT,$expiry_date);
									} else{
										//$report[$i]['status']='--';
										$report[$i]['status']=date(VIS_EDITDATETIMEPICKER_FORMAT,$expiry_date);
										//format_datetime_rpt($expiry_date,$staff_id);
									}
								}else{
									$report[$i]['status']='--';
								}
																					
								}//end for
																		
						}
												
													
		     if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export         = new export();
				include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'report.php');
                $report_obj         = new report();
				$j=0;
                foreach ($report as $assets_id => $i) {
			    foreach ($i as $key => $val){
				if ($key == 'ci_type_id') {
                    $report_exp[$j][$key] = $val;
                        }
		        elseif($key == 'name' || $key == 'CNAME' || $key == 'domain_name' || $key == 'department_name' || $key == 'LNAME' || $key == 'first_name' || $key == 'last_name' || $key == 'usedby' || $key == 'status'|| $key == 'description'){
				$report_exp[$j][$key] = $val;
						}
                elseif ($key == 'assigned_on') {
                           $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        }
				elseif($key == 'installation_date'){
					$report_exp[$j][$key] = date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
				}
				elseif($key == 'expiry_date'){
					$report_exp[$j][$key] = date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
				}
				elseif($key == 'last_audit_date'){
					$report_exp[$j][$key] = date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
				}
                elseif($key == 'impact_name' || $key == 'product_name' || $key =='vender_name' || $key == 'asset_state' || $key == 'depreciation_type' || $key == 'salvage'  || $key == 'warranty_year'  || $key == 'warranty_month'|| $key == 'asset_tag' || $key == 'licence_key' || $key == 'installation_path' ||  $key == 'email' || $key == 'fname' || $key == 'lname' || $key == 'clnt_fname' || $key == 'clnt_lname' || $key == 'licence_type' || $key == 'ci_name' || $key == 'staff_id') {
						$report_exp[$j][$key] = $val;	
						}
                /*elseif($key == 'installation_date' || $key == 'expiry_date'  || $key == 'last_audit_date'){
						$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);	
						}*/
                elseif($key == 'details')
						{
						$text = $val;
						$new_val =  $report_obj->strip_html_tags($text);
						 $report_exp[$j][$key] = $new_val;
						}						
				}
				    $report_exp1[$j]['Asset_name'] = $report_exp[$j]['name'];
					$report_exp1[$j]['Department'] = $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
					$report_exp1[$j]['CI_Type'] = $report_exp[$j]['ci_name'];
					$report_exp1[$j]['description'] = $report_exp[$j]['details'];
					if($report_exp[$j]['staff_id']===''){
						$report_exp1[$j]['managed_by'] = '---';
					}else{
						$report_exp1[$j]['managed_by'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					}
					$report_exp1[$j]['email_id'] = $report_exp[$j]['email'];
					if(isset($report_exp[$j]['usedby'])){
						$report_exp1[$j]['used by'] = $report_exp[$j]['usedby'];
						
					}elseif($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
					{
					$report_exp1[$j]['used by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
					}
					else{
					$report_exp1[$j]['used by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
					}
					 
					$report_exp1[$j]['Assigned_on'] = $report_exp[$j]['assigned_on'];
					$report_exp1[$j]['Location'] = $report_exp[$j]['LNAME'];
					$report_exp1[$j]['impact'] = $report_exp[$j]['impact_name'];
					$report_exp1[$j]['product'] = $report_exp[$j]['product_name'];
					$report_exp1[$j]['vender'] = $report_exp[$j]['vender_name'];
					$report_exp1[$j]['warranty_year'] = $report_exp[$j]['warranty_year'];
					$report_exp1[$j]['warranty_month'] = $report_exp[$j]['warranty_month'];
					$report_exp1[$j]['acquisition_date'] = $report_exp[$j]['installation_date'];
					$report_exp1[$j]['warranty_expiry_date'] = $report_exp[$j]['expiry_date'];
					$report_exp1[$j]['asset_state'] = $report_exp[$j]['description'];
					$report_exp1[$j]['asset_tag'] = $report_exp[$j]['asset_tag'];
					$report_exp1[$j]['serial_no'] = $report_exp[$j]['licence_key'];
					$report_exp1[$j]['last_audit_date'] = $report_exp[$j]['last_audit_date'];
					$report_exp1[$j]['cost'] = $report_exp[$j]['installation_path'];
					$report_exp1[$j]['salvage'] = $report_exp[$j]['salvage'];
					$report_exp1[$j]['depreciation_type'] = $report_exp[$j]['depreciation_type'];
					$report_exp1[$j]['expiry_date'] = $report_exp[$j]['status'];
					
                    $j++;
                }
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;	// To display title which shows on report form
				//$report_exp1['Report_title'] = $operation['export_name']; // To display title which entered in request report popup form
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
																
																if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            }else
                include(VIS_PATH_TEMPLATE . 'hardware_asset_by_state.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
         $database->database_disconnect();
        return true;
	}
	
	function assets_depreciation_report($operation)
	{   //print_r($operation);
		global $vis_language;
        $report_title = $vis_language['TXTASSETDEPRECIATION'];
        $export       = 1;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
				 
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object = new class_ticket($database);
        $staff      = $tkt_object->get_staff_list(VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
	
					if(VIS_LOCATION_AREA==2){						
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'subsetting.php');		 
		$sub_object=new class_subsetting($database);
		
		$citype_details=$object->get_configuration_item_category('','is_enabled=1'); 
		$depreciation_details=$object->get_finance_management_list($filter4='',$data='',VIS_STAFF_ID, VIS_LOCATION_AREA,$limit='');  
		} 
		if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
				
                if ($data['depr_type_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTASSETDEP'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
				if ($data['depr_type_id'] != -1 && $data['depr_type_id'] != 0) {
						 
					 $filter = ' cmast.depreciation_type='.$data['depr_type_id'].' and cmast.ci_type_id=2 '; 
					}
				if ($data['depr_type_id'] == 0) {
						 
					 $filter = ' cmast.depreciation_type!=0 and cmast.ci_type_id=2 '; 
					}
						 					}
			 include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
             $object = new class_report($database);
             $report = $object->assets_by_depriciation_type($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);  
				
                    if($report!=1){
													
						for($i=0; $i<count($report); $i++){
															
							$is_expired =0;
						    if($report[$i]['ci_type_id']==2){
								$total_month = ($report[$i]['warranty_year']*12)+$report[$i]['warranty_month'];
								$current_date = strtotime(date('Y-m-d h:i:s'));
								//$expiry_date = strtotime(date('Y-m-d h:i:s', $report[$i]['installation_date']) . $total_month.' month');
								if($report[$i]['warranty_year']==0 && $report[$i]['warranty_month']==0){
									$expiry_date = $report[$i]['expiry_date'];
								}else{
									$expiry_date = strtotime($total_month.' month',$report[$i]['installation_date']);
								}
								if($expiry_date <= $current_date){
									//$report[$i]['status']=format_datetime($expiry_date);
									$report[$i]['status']=date(VIS_EDITDATETIMEPICKER_FORMAT,$expiry_date);
								} else{
									//$report[$i]['status']='--';
									$report[$i]['status']=date(VIS_EDITDATETIMEPICKER_FORMAT,$expiry_date);
									//format_datetime_rpt($expiry_date,$staff_id);
								}
							}else{
								$report[$i]['status']='--';
							}
																												
						}//end for
																									
					}				
				
			    if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export         = new export();
				include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'report.php');
                $report_obj         = new report();
				$j=0;
                foreach ($report as $assets_id => $i) {
					
				foreach ($i as $key => $val){
				if ($key == 'ci_type_id') {
                    $report_exp[$j][$key] = $val;
                        }
			    elseif($key == 'name' || $key == 'CNAME' || $key == 'domain_name' || $key == 'department_name' || $key == 'LNAME' || $key == 'first_name' || $key == 'last_name' || $key == 'usedby' || $key == 'status'|| $key == 'description' || $key=='depriciation_name' || $key=='useful_life' ){
				$report_exp[$j][$key] = $val;
				}
				elseif ($key == 'assigned_on') {
                $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                }
				elseif($key == 'installation_date'){
					$report_exp[$j][$key] = date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
				} elseif($key == 'expiry_date'){
					$report_exp[$j][$key] = date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
				} elseif($key == 'last_audit_date'){
					$report_exp[$j][$key] = date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
				}
                elseif($key == 'impact_name' || $key == 'product_name' || $key =='vender_name' || $key == 'asset_state' || $key == 'depreciation_type' || $key == 'salvage'  || $key == 'warranty_year'  || $key == 'warranty_month'|| $key == 'asset_tag' || $key == 'licence_key' || $key == 'installation_path' ||  $key == 'email' || $key == 'fname' || $key == 'lname' || $key == 'clnt_fname' || $key == 'clnt_lname' || $key == 'licence_type' || $key == 'config_item' || $key == 'staff_id') {
						$report_exp[$j][$key] = $val;	
						}
                 
                elseif($key == 'details')
						{
						$text = $val;
						$new_val =  $report_obj->strip_html_tags($text);
						 $report_exp[$j][$key] = $new_val;
						}
                 				
			}
                    $report_exp1[$j]['Asset_name'] = $report_exp[$j]['name'];
					$report_exp1[$j]['Department'] = $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
					$report_exp1[$j]['CI_Type'] = $report_exp[$j]['config_item'];
					$report_exp1[$j]['description'] = $report_exp[$j]['details'];
					if($report_exp[$j]['staff_id']===''){
						$report_exp1[$j]['managed_by'] = '---';
					}else{
						$report_exp1[$j]['managed_by'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					}
					$report_exp1[$j]['email_id'] = $report_exp[$j]['email'];
					if(isset($report_exp[$j]['usedby'])){
						$report_exp1[$j]['use by'] = $report_exp[$j]['usedby'];
					}elseif($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
					{
					$report_exp1[$j]['used by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
					}
					else{
					$report_exp1[$j]['used by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
					}
					
					$report_exp1[$j]['assigned_on'] = $report_exp[$j]['assigned_on'];
					$report_exp1[$j]['location'] = $report_exp[$j]['LNAME'];
					$report_exp1[$j]['impact'] = $report_exp[$j]['impact_name'];
					$report_exp1[$j]['product'] = $report_exp[$j]['product_name'];
					$report_exp1[$j]['vender'] = $report_exp[$j]['vender_name'];
					$report_exp1[$j]['warranty_year'] = $report_exp[$j]['warranty_year'];
					$report_exp1[$j]['warranty_month'] = $report_exp[$j]['warranty_month'];
					$report_exp1[$j]['acquisition_date'] = $report_exp[$j]['installation_date'];
					$report_exp1[$j]['warranty_expiry_date'] = $report_exp[$j]['expiry_date'];
					$report_exp1[$j]['asset_state'] = $report_exp[$j]['description'];
					$report_exp1[$j]['asset_tag'] = $report_exp[$j]['asset_tag'];
					$report_exp1[$j]['serial_no'] = $report_exp[$j]['licence_key'];
					$report_exp1[$j]['last_audit_date'] = $report_exp[$j]['last_audit_date'];
					$report_exp1[$j]['cost'] = $report_exp[$j]['installation_path'];
					$report_exp1[$j]['salvage'] = $report_exp[$j]['salvage'];
					$report_exp1[$j]['depreciation_type'] = $report_exp[$j]['depriciation_name'];
					$report_exp1[$j]['useful_life(in_years)'] = $report_exp[$j]['useful_life'];
					$report_exp1[$j]['expired_date'] = $report_exp[$j]['status'];
					
                    $j++;
                }
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;	// To display title which shows on report form
				//$report_exp1['Report_title'] = $operation['export_name']; // To display title which entered in request report popup form
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
																
																if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            }else
                include(VIS_PATH_TEMPLATE . 'assets_by_depriciation_type.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
         $database->database_disconnect();
        return true;
		
	}
	
	
	function assets_depreciation_notification_report($operation)
	{   //print_r($operation);
		global $vis_language;
        $report_title = $vis_language['TXTASSETDEPRECIATIONNOTIFY'];
        $export       = 1;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
				 
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        $tkt_object = new class_ticket($database);
        $staff      = $tkt_object->get_staff_list(VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
	
					if(VIS_LOCATION_AREA==2){						
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'subsetting.php');		 
		$sub_object=new class_subsetting($database);
		
		$citype_details=$object->get_configuration_item_category('','is_enabled=1'); 
		$depreciation_details=$object->get_finance_management_list($filter4='',$data='',VIS_STAFF_ID, VIS_LOCATION_AREA,$limit='');  
		} 
		if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
				
                if ($data['depr_type_id'] == -1) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
				if ($data['depr_type_id'] != -1 && $data['depr_type_id'] != 0) {
						 
					 $filter = ' cmast.depreciation_type='.$data['depr_type_id'].' and cmast.ci_type_id=2 '; 
					}
				if ($data['depr_type_id'] == 0) {
						 
					 $filter = ' cmast.depreciation_type!=0 and cmast.ci_type_id=2 '; 
					}
	 			}
			 include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
             $object = new class_report($database);
             $report = $object->assets_depriciation_notification($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);  
				
                    if($report!=1){
													
						for($i=0; $i<count($report); $i++){
															
							$is_expired =0;
						    if($report[$i]['ci_type_id']==2){
								$total_month = ($report[$i]['warranty_year']*12)+$report[$i]['warranty_month'];
							  	$current_date = strtotime(date('Y-m-d h:i:s'));
							  	//$expiry_date = strtotime(date('Y-m-d h:i:s', $report[$i]['installation_date']) . $total_month.' month');
								if($report[$i]['warranty_year']==0 && $report[$i]['warranty_month']==0){
									$expiry_date = $report[$i]['expiry_date'];
								}else{
									$expiry_date = strtotime($total_month.' month',$report[$i]['installation_date']);
								}
								if($expiry_date <= $current_date){
									//$report[$i]['status']=format_datetime($expiry_date);
									$report[$i]['status']=date(VIS_EDITDATETIMEPICKER_FORMAT,$expiry_date);
								} else{
									//$report[$i]['status']='--';
									$report[$i]['status']=date(VIS_EDITDATETIMEPICKER_FORMAT,$expiry_date);
									//format_datetime_rpt($expiry_date,$staff_id);
								}
							}else{
								$report[$i]['status']='--';
							}
					    
							if($report[$i]['status']!='--')
							{
								$expdate = date('Y-m-d', $report[$i]['expiry_date']);
								$currentdate = date('Y-m-d H:i:s');
								
								//$diff = abs(strtotime($expdate) - strtotime($currentdate));
								$diff = abs(strtotime($report[$i]['status']) - strtotime($currentdate));
								$years = floor($diff / (365*60*60*24));
								$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
								$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
								
								if($months==12)
								{
									$months=$months-12;
									$years=$years+1;
								}
								
								$report[$i]['rem_warranty'] = $years.' '.'years'.' '.$months.' '.'months'.' '.$days.' '.'days';
						   }
						   else
						   {
						   		$report[$i]['rem_warranty'] = '--';
						   }
                            							
						}//end for
																									
					}				
				
			    if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export         = new export();
				include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'report.php');
                $report_obj         = new report();
				$j=0;
                foreach ($report as $assets_id => $i) {
					
	             foreach ($i as $key => $val){
					 if ($key == 'ci_type_id') {
                    $report_exp[$j][$key] = $val;
                        }
	            elseif($key == 'name' || $key == 'CNAME' || $key == 'domain_name' || $key == 'department_name' || $key == 'LNAME' || $key == 'first_name' || $key == 'last_name' || $key == 'usedby' || $key == 'status'|| $key == 'description' || $key =='useful_life' || $key=='depriciation_name' || $key=='rem_warranty' ){
		         $report_exp[$j][$key] = $val;
					}
				elseif ($key == 'assigned_on') {
                $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                }
				elseif($key == 'installation_date'){
							$report_exp[$j][$key] = date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
						} elseif($key == 'expiry_date'){
							$report_exp[$j][$key] = date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
						} elseif($key == 'last_audit_date'){
							$report_exp[$j][$key] = date(VIS_EDITDATETIMEPICKER_FORMAT,$val);
						}
                elseif($key == 'impact_name' || $key == 'product_name' || $key =='vender_name' || $key == 'asset_state' || $key == 'salvage'  || $key == 'warranty_year'  || $key == 'warranty_month'|| $key == 'asset_tag' || $key == 'licence_key' || $key == 'installation_path' ||  $key == 'email' || $key == 'fname' || $key == 'lname' || $key == 'clnt_fname' || $key == 'clnt_lname' || $key == 'config_item' || $key == 'staff_id') {
						$report_exp[$j][$key] = $val;	
						}
                
                elseif($key == 'details')
						{
						$text = $val;
						$new_val =  $report_obj->strip_html_tags($text);
						 $report_exp[$j][$key] = $new_val;
						}
                					
			}         
			          $report_exp1[$j]['Asset_name'] = $report_exp[$j]['name'];
					$report_exp1[$j]['Department'] = $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
					$report_exp1[$j]['CI_Type'] = $report_exp[$j]['config_item'];
					$report_exp1[$j]['description'] = $report_exp[$j]['details'];
					if($report_exp[$j]['staff_id']===''){
						$report_exp1[$j]['managed_by'] = '---';
					}else{
						$report_exp1[$j]['managed_by'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					}
					$report_exp1[$j]['email_id'] = $report_exp[$j]['email'];
					 if(isset($report_exp[$j]['usedby'])){
						 $report_exp1[$j]['used by'] = $report_exp[$j]['usedby'];
						 
					 }elseif($report_exp[$j]['clnt_fname']=='' || $report_exp[$j]['clnt_lname']=='')
					{
					$report_exp1[$j]['used_by']	= $report_exp[$j]['fname'].' '.$report_exp[$j]['lname'];
					}
					else{
					$report_exp1[$j]['used_by']	= $report_exp[$j]['clnt_fname'].' '.$report_exp[$j]['clnt_lname'];	
					}
					//$report_exp1[$j]['email'] = $report_exp[$j]['usedby'];
					$report_exp1[$j]['assigned_on'] = $report_exp[$j]['assigned_on'];
					$report_exp1[$j]['location'] = $report_exp[$j]['LNAME'];
					$report_exp1[$j]['impact'] = $report_exp[$j]['impact_name'];
					$report_exp1[$j]['product'] = $report_exp[$j]['product_name'];
					$report_exp1[$j]['vender'] = $report_exp[$j]['vender_name'];
					$report_exp1[$j]['warranty_year'] = $report_exp[$j]['warranty_year'];
					$report_exp1[$j]['warranty_month'] = $report_exp[$j]['warranty_month'];
					$report_exp1[$j]['acquisition_date'] = $report_exp[$j]['installation_date'];
					$report_exp1[$j]['warranty_expiry_date'] = $report_exp[$j]['expiry_date'];
					$report_exp1[$j]['asset_state'] = $report_exp[$j]['description'];
					$report_exp1[$j]['asset_tag'] = $report_exp[$j]['asset_tag'];
					$report_exp1[$j]['serial_no'] = $report_exp[$j]['licence_key'];
					$report_exp1[$j]['last_audit_date'] = $report_exp[$j]['last_audit_date'];
					$report_exp1[$j]['cost'] = $report_exp[$j]['installation_path'];
					$report_exp1[$j]['salvage'] = $report_exp[$j]['salvage'];
					$report_exp1[$j]['depreciation_type'] = $report_exp[$j]['depriciation_name'];
					$report_exp1[$j]['useful_life(in_years)'] = $report_exp[$j]['useful_life'];
					$report_exp1[$j]['remaining_warranty'] = $report_exp[$j]['rem_warranty'];
					$report_exp1[$j]['expiry_date'] = $report_exp[$j]['status'];
					
                    $j++;
                }
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;	// To display title which shows on report form
				//$report_exp1['Report_title'] = $operation['export_name']; // To display title which entered in request report popup form
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
																
				if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            }else
                include(VIS_PATH_TEMPLATE . 'assets_depreciation_notification.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
         $database->database_disconnect();
        return true;
		
	}
	
	function clients_by_organization_report($operation)
	{	
		global $vis_language;
		$report_title = $vis_language['TXTCLIENTBYORGANIZATION'];
		if (isset($operation['slave_db']) && $operation['slave_db'] == 1){
            $database = new class_database('mysql', 1);
        }else{
            $database = new class_database();
		}
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'client.php');
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
        $object        = new class_report($database);
        $tkt_object    = new class_ticket($database);
        $client_object = new class_client($database);
        include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'client.php');
        $object_client = new client();
        $domain_list   = $object_client->get_domain_access_list();
        $clientGroup   = $client_object->get_client_group1(' (domain_id in (' . $domain_list . ') or domain_id=0 ) and is_enable=1', VIS_STAFF_ID, VIS_LOCATION_AREA); //get client group list
        $filter        = VIS_TABLE_PREFIX . 'domain.is_enabled=1';
        $domain        = $tkt_object->get_domain($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
        $export        = 1;
		if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
			if (isset($operation['vis_export']) && $operation['vis_export'] == 1)				
                $filter = $operation['filter'];
			else { 
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != ''){
					$data = $operation;
                }else					
                    $data = parse_form_array($operation['vis_action']);
				if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
				 if ($data['domain_id'] == "" && $data['group_id'] == "") {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['domain_id']!= "" && $data['group_id'] == "") {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECT'] ." ".$vis_language['TXTORGANIZATION']. '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    
                    return false;
                } else if ($data['domain_id'] == "" && $data['group_id'] != "") {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDOMAINFORRES'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    
                    return false;
                } 
				 
				
				if ($data['domain_id'] == 0) {
                    $filter = "";
                } else{
                    $filter = ' (' . VIS_TABLE_PREFIX . 'client.domain_id = ' . $data['domain_id'].' or ' . VIS_TABLE_PREFIX . 'client.optionB_domain_id = ' . $data['domain_id'].')';
																}
				
				if ($data['group_id'] == '0' || $data['group_id'] == ''){
					//$filter    = "";
				}else{
					$filter   .= ($filter!='')?' and '.VIS_TABLE_PREFIX . 'client.group_id = ' . $data['group_id']:VIS_TABLE_PREFIX . 'client.group_id = ' . $data['group_id'];
				}
			}
		
		 	$post_details = $object->get_client_organization_report1($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
			
			if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
				include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
				$j      = 0;
				foreach ($post_details as $i) {
					foreach ($i as $key => $val) {
						if ($key == 'first_name') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'last_name') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'email') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'contact_number'){
							$report_exp[$j][$key] = $val;
						} elseif ($key == 'group_name'){
							$report_exp[$j][$key] = $val;
						}
					}
					$report_exp1[$j]['client'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					$report_exp1[$j]['email'] = $report_exp[$j]['email'];
					if($report_exp[$j]['contact_number']!=''){
						$report_exp1[$j]['contact_number'] = $report_exp[$j]['contact_number'];
					}else{
						$report_exp1[$j]['contact_number'] = '-';
					}
					$report_exp1[$j]['organizations'] = $report_exp[$j]['group_name'];
                    $j++;
				}
				$completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
                if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
			}
			else
				include(VIS_PATH_TEMPLATE.'clients_by_organization.php');
		}
		else 
			include(VIS_PATH_TEMPLATE.'report_form.php');
		
		$database->database_disconnect();
		return true;
	}
	//organization wise invoice report
	function invoiceby_organization_report($operation)
	{
		global $vis_language;
		$report_title = $vis_language['TXTINVOICEBYORGANIZATION'];
		if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
        $export = 1;
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'client.php');
        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
        $object        = new class_report($database);
        $tkt_object    = new class_ticket($database);
        $client_object = new class_client($database);
		include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'client.php');
        $object_client = new client();
		$domain_list   = $object_client->get_domain_access_list();
        $clientGroup   = $client_object->get_client_group1(' (domain_id in (' . $domain_list . ') or domain_id=0 ) and is_enable=1', VIS_STAFF_ID, VIS_LOCATION_AREA); //get client group list
        $filter        = VIS_TABLE_PREFIX . 'domain.is_enabled=1';
        $domain        = $tkt_object->get_domain($filter, VIS_STAFF_ID, VIS_LOCATION_AREA); // get domain list
		$currency = $domain[0]['currency'];
		$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
        $export        = 1;
		if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
		if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                        return false;
                    }
                }
                if ($data['duration'] == 0 && $data['domain_id'] == "" && $data['group_id'] == "") {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['domain_id'] == "" && $data['group_id'] != "" ) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message",  "' . $vis_language['TXTSELDOMAINFORRES'].'");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['domain_id'] != "" && $data['group_id'] == "") {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECT'] ." ".$vis_language['TXTORGANIZATION']. '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] == 0 && $data['domain_id'] != "" && $data['group_id'] != "") {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                } else if ($data['duration'] != 0 && $data['domain_id'] == "" && $data['group_id'] == "") {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message","' .  $vis_language['TXTSELDOMAINFORRES']  .  '" );$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }	
               
               $domainId = ' AND ' . VIS_TABLE_PREFIX . 'client.domain_id = ' . $data['domain_id'];
                
				if ($data['group_id'] == '0' || $data['group_id'] == ''){
					$groupId    = "";
				}else{
					$groupId   = VIS_TABLE_PREFIX . 'client.group_id = ' . $data['group_id'].' AND ';
				}
                $currentdate = date('Y-m-d h:i:s');
                if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter    = $groupId . VIS_TABLE_PREFIX . 'invoice.entry_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . $domainId;
                } else if ($data['duration'] == 2) //if select Last 7 days duration
                    {
                    $weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
                    $filter   = $groupId . VIS_TABLE_PREFIX . 'invoice.entry_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . $domainId;
                } else if ($data['duration'] == 3) //if select Last 24 hours duration
                    {
                    $hour   = strtotime("-1 day ") - VIS_TIMEZONE;
                    $filter = $groupId . VIS_TABLE_PREFIX . 'invoice.entry_date between ' . $hour . ' AND ' . strtotime($currentdate) . $domainId;
                } else if ($data['duration'] == 4) //if select Custom duration
                    {
                    $from   = strtotime($data['from_date']) - VIS_TIMEZONE;
                    $to     = strtotime($data['to_date']) - VIS_TIMEZONE;
                    $filter = $groupId . VIS_TABLE_PREFIX . 'invoice.entry_date between "' . $from . '" AND "' . $to . '"' . $domainId;
                }
            }
		 $report = $object->get_invoiceby_organization_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA); //get invoice by organization report
		 $total=0;$paid=0;$unpaid=0;$cancelled=0;
		   foreach ($report as $ticket_id => $details) {
            $total += $details['amount'];
			if($details['invoice_status'] == 0)$unpaid +=$details['amount'];
			else if($details['invoice_status'] == 1)$paid +=$details['amount'];
			else if($details['invoice_status'] == 2)$cancelled +=$details['amount'];
            }
            
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export = new export();
                $j      = 0;
				 
                foreach ($report as $ticket_id => $i) {
					 
                    foreach ($i as $key => $val) {
                        if ($key == 'invoice_hash') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'entry_date') {
                           
							$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        } elseif ($key == 'first_name') {
                            $report_exp[$j][$key] = $val;
                        } 
						elseif ($key == 'last_name') {
                            $report_exp[$j][$key] = $val;
                        }elseif ($key == 'email') {
                            $report_exp[$j][$key] = $val;
                        }elseif ($key == 'group_name') {
                            $report_exp[$j][$key] = $val;
                        }elseif ($key == 'due_date') {
                          
							$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
                        } elseif ($key == 'paid_date') {
                           
							$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
						} elseif ($key == 'ticket_hash') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'amount') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'invoice_status') {
                            $report_exp[$j][$key] = $val;
							if($val==0)
							{
								$report_exp[$j][$key] = $vis_language['TXTUNPAID'];
							}elseif($val==1)
							{
								$report_exp[$j][$key] = $vis_language['TXTPAID'];
							}elseif($val==2)
							{
								$report_exp[$j][$key] = $vis_language['TXTCANCELED']; 
							}elseif($val==3)
							{
								$report_exp[$j][$key] = $vis_language['TXTINPROCESS'];
							}
                        }
                        
                    }
					$report_exp1[$j]['reference_ticket']=$report_exp[$j]['ticket_hash'];
					$report_exp1[$j]['client_name']=$report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					$report_exp1[$j]['email']=$report_exp[$j]['email'];
					$report_exp1[$j]['organization']=$report_exp[$j]['group_name'];
				    $report_exp1[$j]['invoice_hash']=$report_exp[$j]['invoice_hash'];
					$report_exp1[$j]['invoice_date']=$report_exp[$j]['entry_date'];
					$report_exp1[$j]['due_date']=$report_exp[$j]['due_date'];
					$report_exp1[$j]['paid_date']=$report_exp[$j]['paid_date'];
					$report_exp1[$j]['amount'.'('.$currency.')']=$report_exp[$j]['amount'];
					$report_exp1[$j]['paid_status']=$report_exp[$j]['invoice_status'];
					
                    $j++;
					
                }
                $report_exp [$j]['total']=$vis_language['TXTTOTAL'].' :'.$total .' '. $currency;
				$report_exp [$j]['paid']=$vis_language['TXTTOTALPAID'].' :'.$paid .' '. $currency;
				$report_exp [$j]['unpaid']=$vis_language['TXTTOTALUNPAID'].' :'.$unpaid .' '. $currency;
				$report_exp [$j]['cancelled']=$vis_language['TXTTOTALCANCELLED'].' :'.$cancelled .' '. $currency;
				 
					$report_exp1[$j]['total']= $report_exp[$j]['total'];
					$report_exp1[$j]['paid']= $report_exp[$j]['paid'];
					$report_exp1[$j]['unpaid']=$report_exp[$j]['unpaid'];
					$report_exp1[$j]['cancelled']=$report_exp[$j]['cancelled']; 
					
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
																
																if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            } else
                include(VIS_PATH_TEMPLATE . 'invoiceby_organization_report.php');
        } else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
        $database->database_disconnect();
        return true;
	}
	
	// Function get all organizations domain wise 
	function get_domainwise_organizations($operation){
		global $vis_language;
		$domain_id = $_POST['domain_id'];
		$database = new class_database();
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'client.php');
		$client_object = new class_client($database);
		include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'client.php');
        $object_client = new client();
		$domain_list   = $object_client->get_domain_access_list();
        $clientGroup   = $client_object->get_client_group1(' (domain_id in (' . $domain_list . ') or domain_id=0 ) and is_enable=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
        $object = new class_report($database);
		if($domain_id=='' || $domain_id!='0'){ $filter = ' where ((domain_id = '.$domain_id.') or domain_id=0)';}else{ $filter=""; }
		$result = $object->get_orgs_by_domain($filter);
	    if(count($clientGroup) > 10) $sk = 'data-live-search="true"'; else $sk = '';
					$list_data = '';
		//$list_data = '<div class="col-sm-2" id="orgdivid">';
		//$list_data .= '<div class="text-muted pull-center label_page">'.$vis_language['TXTORGANIZATION'].':</div>';
		$list_data .= '<select id="cltbyorg" name="group_id" class="selectpicker" '.$sk.'>';
		$list_data .= '<option value="">'.$vis_language['TXTSELORG'].'</option>';
		
		if($result!= -1){
			$list_data .= '<option value="0">All</option>';
			for($i=0;$i<count($result);$i++){
				$list_data .= "<option value='".$result[$i]['group_id']."'>".$result[$i]['group_name']."</option>";
			}
		}
		$list_data .= '</select></div>';
		echo $list_data;
	}
	
	// Function to generate report to show the overdue tickets
	function overdue_tickets_report($operation){	
		global $vis_language;
		$report_title = $vis_language['TXTOVERDUETICKET'];
		$export       = 1;
		if(isset($operation['slave_db']) && $operation['slave_db'] == 1)
			$database = new class_database('mysql', 1);
		else
			$database = new class_database();
		
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
		$tkt_object = new class_ticket($database);
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'staff.php');
		$object_staff  = new class_staff($database);
		$departments   = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
		$domain        = $tkt_object->get_domain($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		
		$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
		if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
			if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
				$filter = $operation['filter'];
			else 
			{
				if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
					$data = $operation;
				else
					$data = parse_form_array($operation['vis_action']);
				
				if (count($data) == 0) {
					include(VIS_PATH_TEMPLATE . 'report_form.php');
					return true;
				}
				if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
					echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
					return false;
				}
				if ($data['duration'] == 4) {
					list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
					if (!$ret) {
						echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
						return false;
					}
				}
				if ($data['duration'] == 0) {
					echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
					return false;
				}
				if ($data['domain_id'] == '') {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' .  $vis_language['TXTSELDOMAINFORRES']  .  '" );$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
				$currentdate = date('Y-m-d h:i:s');
				if ($data['duration'] == 1){ // previous 1 month
					$currentdate = date('Y-m-d h:i:s');
					$prevDt1 = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month');
					$prevDt2 = strtotime($currentdate);
					$filter    = VIS_TABLE_PREFIX . 'ticket.receive_date between "' . $prevDt1 . '" AND "' . $prevDt2 . '" and ';
				} elseif ($data['duration'] == 2){ // previous 1 week
					$start_week = strtotime("-1 week ");
					$end_week   = strtotime(date('Y-m-d h:i:s'));
					$filter     = VIS_TABLE_PREFIX . 'ticket.receive_date between "' . $start_week . '" AND "' . $end_week . '" and ';
				} else if($data['duration'] == 3){ //previous 1 day
					$d   = strtotime("-1 day ") - VIS_TIMEZONE;
					$currentdate = strtotime(date('Y-m-d h:i:s'));
					$filter    = VIS_TABLE_PREFIX . 'ticket.receive_date between "' . $d . '" AND "' . $currentdate . '" and ';
				} elseif ($data['duration'] == 4){ // custom date range
					$from = strtotime($data['from_date']) - VIS_TIMEZONE;
					$to = strtotime($data['to_date']) - VIS_TIMEZONE;
					$filter    = VIS_TABLE_PREFIX . 'ticket.receive_date between ' . $from . ' AND ' . $to . ' and ';
				}
			}
			if($data['domain_id']!='' && $data['domain_id']!=0){
				$filter .= VIS_TABLE_PREFIX . 'ticket.domain_id = '.$data['domain_id'].' and ';
			}
			if($data['department_id']!=0){
				$filter .= ' d.department_id = '.$data['department_id'].' and ';
			}
			$filter_arr = $tkt_object->get_filter($data['filter_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
			$filter    .= $filter_arr['filter'];
			include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
			$object      = new class_report($database);
			$report      = $object->get_overdue_ticket_reports($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
			if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
				include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
				$export         = new export();
				$j=0;
				foreach ($report as $i){
					foreach ($i as $key => $val){
						if($key == 'ticket_hash'){
							$report_exp[$j][$key] = $val;
						} else if($key == 'subject'){
							$report_exp[$j][$key] = $val;
						} else if($key == 'first_name' || $key == 'last_name' || $key == 'email' || $key == 'group_name'){
							$report_exp[$j][$key] = $val;
						} else if($key == 'receive_date'){
							$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
						} 
					}
					$report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash'];
					$report_exp1[$j]['subject'] = $report_exp[$j]['subject'];
					$report_exp1[$j]['client_name'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					$report_exp1[$j]['email'] = $report_exp[$j]['email'];
					$report_exp1[$j]['organization'] = $report_exp[$j]['group_name'];
					$report_exp1[$j]['create_date'] = $report_exp[$j]['receive_date'];
					$j++;
				}
				$completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
				
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
                if(isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                
				$this->email_send_download($operation);
                return true;
			}else
				include(VIS_PATH_TEMPLATE . 'tickets_overdue_report.php');
		}else
			include(VIS_PATH_TEMPLATE . 'report_form.php');
	
		$database->database_disconnect();
		return true;
	}
	
	// Function to get all departments domain wise
	function get_domainwise_departments($operation){
		global $vis_language;
		$domain_id = $_POST['domain_id'];
		$database = new class_database();
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'client.php');
		$client_object = new class_client($database);
		include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'client.php');
        $object_client = new client();
		$domain_list   = $object_client->get_domain_access_list();
        $clientGroup   = $client_object->get_client_group1(' (domain_id in (' . $domain_list . ') or domain_id=0 ) and is_enable=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
        $object = new class_report($database);
		if($domain_id=='' || $domain_id!='0'){ $filter = '  domain_id = '.$domain_id;}else{ $filter=""; }
		$result = $object->get_depts_by_domain($filter);
		if(count($clientGroup) > 10) $sk = 'data-live-search="true"'; else $sk = '';
		$list_data='';
		$list_data .= '<select class="selectpicker" name="department_id" class="selectpicker" '.$sk.'>';
		$list_data .= '<option value="0" selected="selected">'.$vis_language['TXTALLDEPARTMENTS'].'</option>';
		if($result!= -1){
			for($i=0;$i<count($result);$i++){
				$list_data .= "<option value='".$result[$i]['department_id']."'>".$result[$i]['department_name']."</option>";
			}
		}
		$list_data .= '</select>';
		echo $list_data;
	}
    //custom reports
	function unoverdue_ticket_report($operation)
	{	
		global $vis_language;
		
		$report_title = $vis_language['TXTUNDUEREPORT'];
		$export       = 1;
		if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
						$database = new class_database('mysql', 1);
		else
						$database = new class_database();
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
		$tkt_object = new class_ticket($database);
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'staff.php');
		$object_staff = new class_staff($database);
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'setting.php');
		$object_setting = new class_setting($database);
		$filter         = 'location in(1,11)';
		$staff_filter   = "staff_id =" . VIS_STAFF_ID;
		$staff_details = $object_staff->get_staff($staff_filter, $userid, $user);
		$departments   = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
		$dept_ids    = '';
		for ($d = 0; $d < count($departments); $d++) {
						$dept_ids .= $departments[$d]['department_id'] . ',';
		}
		if ($dept_ids != '') {
						$dept_ids = substr($dept_ids, 0, -1);
		}
		$dept_id  = ($staff_details[0]['department_access'] == '' || $staff_details[0]['department_access'] == 0) ? $dept_ids : $staff_details[0]['department_access'];
		$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
		if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
						if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
										$filter = $operation['filter'];
						else {
										if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
														$data = $operation;
										else
														$data = parse_form_array($operation['vis_action']);
										if (count($data) == 0) {
														include(VIS_PATH_TEMPLATE . 'report_form.php');
														return true;
										}
										if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
														echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
														return false;
										}
										if ($data['duration'] == 4) {
														list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
														if (!$ret) {
																		echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
																		return false;
														}
										}
										if ($data['duration'] == 0 && $data['department_id'] == '') {
														echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
														return false;
										}
										
										if ($data['duration'] != 0 && $data['department_id'] == '') {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
						$currentdate = date('Y-m-d h:i:s');
						if ($data['duration'] == 1) //if select Last 30 days duration
						{
							$monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;                    
							if($data['department_id']!='' && $data['department_id']!=0){
								$filter    = ' receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and ' . VIS_TABLE_PREFIX . 'ticket.department_id=' . $data['department_id'];
							}else{
								$filter    = ' receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate); 	
							}
						} else if ($data['duration'] == 2) //if select Last 7 days duration
							{
							$weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
							if($data['department_id']!='' && $data['department_id']!=0){
							$filter   = ' receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and ' . VIS_TABLE_PREFIX . 'ticket.department_id=' . $data['department_id'];
							 }else{
							$filter    = ' receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate); 	
							}
						} else if ($data['duration'] == 3) //if select Last 24 hours duration
							{
							$hour   = strtotime("-1 day ") - VIS_TIMEZONE;                   
							if($data['department_id']!='' && $data['department_id']!=0){
							$filter = ' receive_date between  ' . $hour . ' AND ' . strtotime($currentdate) . ' and ' . VIS_TABLE_PREFIX . 'ticket.department_id=' . $data['department_id'];
							 }else{
							$filter    = ' receive_date between ' . $hour . ' AND ' . strtotime($currentdate); 	
							}
						} else if ($data['duration'] == 4) //if select Custom duration
							{
							$from   = strtotime($data['from_date']) - VIS_TIMEZONE;
							$to     = strtotime($data['to_date']) - VIS_TIMEZONE;
							if($data['department_id']!='' && $data['department_id']!=0){
							$filter = ' receive_date between ' . $from . ' AND ' . $to . ' and ' . VIS_TABLE_PREFIX . 'ticket.department_id=' . $data['department_id'];
							 }else{
							$filter    = ' receive_date between ' . $from . ' AND ' . $to; 	
							}
							}
						}
						include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
						$object      = new class_report($database);
						$report      = $object->get_unoverdue_ticket($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
						if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
										include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
											$export         = new export();
											$j=0;
											foreach ($report as $i){
											foreach ($i as $key => $val){
											if($key == 'ticket_hash'){
											$report_exp[$j][$key] = $val;
											}
											elseif($key == 'subject'){
											$report_exp[$j][$key] = $val;
											}
											elseif($key == 'department_name'){
											$report_exp[$j][$key] = $val;
											}
											elseif($key == 'receive_date'){
											$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
											}
											elseif($key == 'status_name'){
											$report_exp[$j][$key] = $val;
											}
											}
											$report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash'];
											$report_exp1[$j]['subject'] = $report_exp[$j]['subject'];	
											$report_exp1[$j]['department_name'] = $report_exp[$j]['department_name'];
											$report_exp1[$j]['create_date'] = $report_exp[$j]['receive_date']; 									
											$report_exp1[$j]['status_name'] = $report_exp[$j]['status_name']; 
											
											$j++;
										}
										$completed_date = date('Y-m-d H:i:s');
										$report_exp1['Report_title'] = $report_title;
										
										$filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
										if ($operation['export_type'] == 1) {
														$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
														if (file_exists('../downloads/' . $filename . '.csv')) {
																		$filename = $filename;
														} else {
																		$filename = '';
														}
										} else if ($operation['export_type'] == 2) {
														$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
														
														if (file_exists('../downloads/' . $filename . '.html')) {
																		$filename = $filename;
														} else {
																		$filename = '';
														}
										} else if ($operation['export_type'] == 3) {
														
														
														$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
														
														if (file_exists('../downloads/' . $filename . '.xls')) {
																		$filename = $filename;
														} else {
																		$filename = '';
														}
										}
										
										if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
														$database->database_disconnect();
														$database = new class_database();
														include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
														$object = new class_report($database);
														
										}
										$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
										if (!$result)
														return false;
										$this->email_send_download($operation);
										return true;
						} else 
							
										include(VIS_PATH_TEMPLATE . 'unoverdue_ticket_report.php');
		} else
						include(VIS_PATH_TEMPLATE . 'report_form.php');
		$database->database_disconnect();
		return true;
	}
	//total tickets unoverdue reports
	
	function ticket_activity_by_department_report($operation)
		{	
				global $vis_language;
			$report_title =  $vis_language['TXTTICKETACTIVITYBYDEPTREP'];
			$export       = 1;
			if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
				$database = new class_database('mysql', 1);
			else
			$database = new class_database();
			 
			include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
			$tkt_object = new class_ticket($database);
			$departments   = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
			 
			$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
			if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
								$filter = $operation['filter'];
				else {
								if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
												$data = $operation;
								else
												$data = parse_form_array($operation['vis_action']);
								if (count($data) == 0) {
												include(VIS_PATH_TEMPLATE . 'report_form.php');
												return true;
								}
								if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								/***** date difference ****/
								if ($data['duration'] == 4) {
												list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
												if (!$ret) {
																echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
																return false;
												}
								}
									if ($data['duration'] == 0 && $data['department_id'] == 0) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								} else if ($data['duration'] != 0 && $data['department_id'] == 0) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								} else if ($data['duration'] == 0 && $data['department_id'] != 0) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								} 
								
								$currentdate = date('Y-m-d h:i:s');
								if ($data['duration'] == 1) //if select Last 30 days duration
												{
												$monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
														$filter    = 'avis_auditlog.entry_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and avis_auditlog.department_id='.$data['department_id'];
								} else if ($data['duration'] == 2) //if select Last 7 days duration
												{
												$weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
													$filter    = 'avis_auditlog.entry_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and avis_auditlog.department_id='.$data['department_id'] ;
								}
								
								else if ($data['duration'] == 3) //if select Last 24 hours duration
												{
												$hour = strtotime("-1 day ") - VIS_TIMEZONE;
												
												$filter    = 'avis_auditlog.entry_date  between ' . $hour . ' AND ' . strtotime($currentdate) . ' and avis_auditlog.department_id='.$data['department_id'];
								} else if ($data['duration'] == 4) //if select Custom duration
												{
												$from   = strtotime($data['from_date']) - VIS_TIMEZONE;
												$to     = strtotime($data['to_date']) - VIS_TIMEZONE;
													$filter = 'avis_auditlog.entry_date  between ' . $from . ' AND ' . $to . ' and avis_auditlog.department_id='.$data['department_id'];
								}
						$filter .= " and avis_auditlog.access_id!=1 and avis_auditlog.access_id!=2 and avis_auditlog.access_id!=3";
				}
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
				
				$object = new class_report($database);
			$audit_log = array();
$audit_log	= $object->ticket_activity_by_department_report($filter,$filter1,VIS_STAFF_ID, VIS_LOCATION_AREA);  
if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
$filtern=VIS_TABLE_PREFIX.'customfield_data.group_type = 1 and '.VIS_TABLE_PREFIX.'ticket.ticket_id IN(select DISTINCT ticket_id from '.VIS_TABLE_PREFIX.'auditlog where '.$filter.')';
$custom_fields_details=$object->get_custom_fields_data($filtern);  
}
   
if($audit_log!=-1) {
$close_time=$audit_log[0]['closed_time'];
$creation_time=$audit_log [0]['creation_date'];
if( $close_time!=0){
$total_time=$audit_log[0]['closed_time']-$audit_log[0]['creation_date'];
}else{
$total_time=" ";
} 
$avgTimeCount =count_remain_time($total_time ) ;
}
 
$audit_count=($audit_log==-1)?0:count($audit_log);
												for($i=0 ; $i<$audit_count ; $i++)
												{
													
switch($audit_log[$i]['operation_id']) {
case '0':
 
	 
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGNEWTKTCRT'];
 
	break;
	
case '1':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTKTRPLD'];
	break;
case '2':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTSTATUSCHANGEDFROM'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
	
case '3':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTPRIORITYCHANGEDFROM'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
	
case '4':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTDEPARTMENTCHANGEDFROM'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
	
case '5':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTTICKETTYPECHANGEDFROM'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
	
case '6':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTFLAGCHANGEDFROM'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
	
case '7':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');  
	$object=new class_setting($database);
	$staff_new_info=$object->get_staff(VIS_TABLE_PREFIX.'staff.staff_id='.$audit_log[$i]['new_info'], '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA,1);
	$new_info =  $staff_new_info[1][0]['first_name'].' '.$staff_new_info[1][0]['last_name'];
	if($audit_log[$i]['new_info']!=0 && $staff_new_info[1]!=-1)
																																$audit_log[$i]['audit_log_operation'] = $vis_language['MSGTKTASSIG'].' '.$vis_language['TXTTO'].' '. $new_info;
																												elseif($audit_log[$i]['new_info']==0 && $audit_log[$i]['old_info']>0){ 
																																$staff_old_info=$object->get_staff(VIS_TABLE_PREFIX.'staff.staff_id='.$audit_log[$i]['old_info'], '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA,1);
																																$old_info =  $staff_old_info[1][0]['first_name'].' '.$staff_old_info[1][0]['last_name'];
																																$audit_log[$i]['audit_log_operation'] = $vis_language['TXTTICKETUNASSIGNEDFROM'].' '. $old_info;
																												}
	else
																																$audit_log[$i]['audit_log_operation'] = $vis_language['TXTTICKETASSIGNEDTONOBODY'];
	break;
	
case '8':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');  
	$object=new class_setting($database);
	$staff_details=$object->get_staff(VIS_TABLE_PREFIX.'staff.staff_id='.$audit_log[$i]['operator_id'], '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
	$staff_name =  $staff_details[1][0]['first_name'].' '.$staff_details[1][0]['last_name'];
	if($audit_log[$i]['old_info']!=0 )
		$audit_log[$i]['audit_log_operation']=$vis_language['MSGTKTTRSH'];
	elseif($audit_log[$i]['old_info']==0 )
		$audit_log[$i]['audit_log_operation']=$vis_language['TXTTKTMOVEINBOCFROMTRASH'];
	break;
	
case '9':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');  
	$object=new class_setting($database);
	$staff_details=$object->get_staff(VIS_TABLE_PREFIX.'staff.staff_id='.$audit_log[$i]['operator_id'], '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
	$staff_name =  $staff_details[1][0]['first_name'].' '.$staff_details[1][0]['last_name'];
	$audit_log[$i]['audit_log_operation']=$audit_log[$i+1]['old_info'] .' deleted';
	break;
case '10':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
	$object=new class_ticket($database);
	$audit_log[$i]['audit_log_operation']=$audit_log[$i]['new_info'] .' merged to '.$audit_log[$i]['old_info'];
	break;
	
case '11':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTKTFRWD'];
	break;
	
case '12':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTKTDELETED'];
	break;
	
case '13':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');  
	$object=new class_setting($database);
	$sla_old_info=$object->get_sla(VIS_TABLE_PREFIX.'sla.sla_id='.$audit_log[$i]['old_info'], '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
	$sla_new_info=$object->get_sla(VIS_TABLE_PREFIX.'sla.sla_id='.$audit_log[$i]['new_info'], '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
				$old_info =  $sla_old_info[1][0]['sla_name'];
	$new_info =  $sla_new_info[1][0]['sla_name'];
	if($audit_log[$i]['new_info']!=0 && $audit_log[$i]['old_info']!=0)
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTSLACHANGEDFROM'].' '. $old_info. ' to '. $new_info;
	else if($audit_log[$i]['old_info']==0 && $audit_log[$i]['new_info']!=0)
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTSLASETTO'].' '. $new_info;
	else if($audit_log[$i]['new_info']==0)
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTAPPLIEDCUSTOMSLAPLAN'];
	break;
	
case '14':
	if($audit_log[$i]['new_info']!=0 && $audit_log[$i]['old_info']==0)
		$audit_log[$i]['audit_log_operation']=$vis_language['MSGSPAMMRK'];
	elseif($audit_log[$i]['new_info']==0 && $audit_log[$i]['old_info']!=0)
		$audit_log[$i]['audit_log_operation']=$vis_language['TXTNOTSPAMMARKED'];
	break;
	
case '15':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGUNSPAM'];
	break;
case '16':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTNEWTASKATTACHED'].' '. $audit_log[$i]['old_info'];
	break;
case '17':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTDETACHEDATTACHEDTASK'].' '. $audit_log[$i]['old_info'];
	break;
case '18':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTNEWCIATTACHED'].' '. $audit_log[$i]['old_info'];
	break;
case '19':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTDETACHEDATTACHEDCI'].' '. $audit_log[$i]['old_info'];
	break;
case '20':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTNEWPROBLEMATTACHED'].' '. $audit_log[$i]['old_info'];
	break;
case '21':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTDETACHEDATTACHEDPROBLEM'].' '. $audit_log[$i]['old_info'];
	break;
case '22':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTNEWCHANGEATTACHED'].' '. $audit_log[$i]['old_info'];
	break;
case '23':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTDETACHEDATTACHEDCHANGE'].' '. $audit_log[$i]['old_info'];
	break;	
case '24':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
	$object=new class_ticket($database);
	$audit_log[$i]['audit_log_operation']=$audit_log[$i]['new_info'] .' '.$vis_language['TXTLINKEDTO'].' '.$audit_log[$i]['old_info'];
	break;	
case '25':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
	$object=new class_ticket($database);
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETPOSTDELETE'];
	break;	
case '26':
	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
	$object=new class_ticket($database);
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTTICKETPOSTSPLITWITH'].' '.$audit_log[$i]['new_info'];
	break;	
case '27':
	$audit_log[$i]['audit_log_operation'] = $vis_language['TXTCHANNELCHANGEDFROM'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
case '28':
	$audit_log[$i]['audit_log_operation']=$audit_log[$i]['old_info'] .' '.$vis_language['TXTUNLINKEDFROM'].' '.$audit_log[$i]['new_info'];
	break;	
case '29':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTSET'].' '.$audit_log[$i]['new_info'] .' '.$vis_language['TXTLABEL'];
	break;	
case '30':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTPINNED'].' '.$audit_log[$i]['old_info'];
	break;
case '31':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTREMOVEDLABEL'].' '.$audit_log[$i]['new_info'];
	break;
case '32':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETPROPERTYUPDATED'];
	break;
case '33':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETNOTEADDED'];
	break;
case '34':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETNOTEDELETED'];
	break;
case '35':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETCOMMENTADDED'];
	break;
case '36':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETCOMMENTDELETED'];
	break;
case '37':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGTICKETPOSTUPDATED'];
	break;
case '38':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGADDEDNEWRECIPIENT'].' '.$audit_log[$i]['new_info'];
	break;
case '39':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGADDEDNEWPEOPLE'].' '.$audit_log[$i]['new_info'];
	break;
case '40':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGREMOVEDRECIPIENT'].' '.$audit_log[$i]['new_info'];
	break;
case '41':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGREMOVEDPEOPLE'].' '.$audit_log[$i]['new_info'];
	break;
case '42':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGADDEDNEWTAG'].' '.$audit_log[$i]['new_info'];
	break;
case '43':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGREMOVEDTAG'].' '.$audit_log[$i]['new_info'];
	break;
case '44':
	$audit_log[$i]['audit_log_operation']=$vis_language['MSGNEWSCHEDULETKTCRT'];
	break;
case '45':
	$audit_log[$i]['audit_log_operation']=($audit_log[$i]['old_info'] == '')?$vis_language['MSGTICKETLEVELCHANGED'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info']:$vis_language['MSGTICKETLEVELCHANGED'].' from '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
case '46':
	$audit_log[$i]['audit_log_operation']=($audit_log[$i]['old_info'] == '')?$vis_language['MSGTICKETCATEGORYCHANGED'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info']:$vis_language['MSGTICKETCATEGORYCHANGED'].' from '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
case '47':
	$audit_log[$i]['audit_log_operation']=($audit_log[$i]['old_info'] == '')?$vis_language['MSGTICKETSUBCATEGORYCHANGED'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info']:$vis_language['MSGTICKETSUBCATEGORYCHANGED'].' from '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
case '48':
	$audit_log[$i]['audit_log_operation']=($audit_log[$i]['old_info'] == '')?$vis_language['MSGTICKETITEMCHANGED'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info']:$vis_language['MSGTICKETITEMCHANGED'].' from '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
case '49':
	$audit_log[$i]['audit_log_operation']=($audit_log[$i]['old_info'] == '')?$vis_language['MSGITILLOCATIONCHANGED'].' '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info']:$vis_language['MSGITILLOCATIONCHANGED'].' from '. $audit_log[$i]['old_info']. ' to '. $audit_log[$i]['new_info'];
	break;
case '50':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTSLAPAUSED'];
	break;
case '51':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTSLARESUMED'];
	break;
	case '52':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTREADTICKETFORSTAFF'];
	break;
	case '53':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTSENDTICKETAPPROVAL'].' to '.$audit_log[$i]['old_info'];
	break;
	case '54':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTCHANGETICKETAPPROVALSTATUS'].' by '.$audit_log[$i]['old_info'];
	break;
	case '55':
	$audit_log[$i]['audit_log_operation']=$vis_language['TXTSENDTICKETAPPROVALREMINDER'].' to '.$audit_log[$i]['old_info'];
	break;
	case '56':
			$audit_log[$i]['audit_log_operation']=$vis_language['MSGCUSTOMEFIELDDATAMODIFIED'].' by '.$audit_log[$i]['old_info'];
		break;
	
}
    $audit_log1=$audit_log;
	   
	}
 
		
		 $temp=array();
			$temp1=array();		
			$temp2=array();	
			$filedarr=array(); 
		 $a=0;
			
			//start custom data
			if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
			if(count($custom_fields_details)>0){
								for($b=0;$b<count($custom_fields_details);$b++){
								$filedarr[]=$custom_fields_details[$b]['field_name'];			 
								}
							if(count($filedarr)>=0){
									$filedarr=array_unique($filedarr);		 
									$filedarr=array_values($filedarr);
								}
									
							for($i=0;$i<count($custom_fields_details);$i++) 
							{
								$a=$custom_fields_details[$i]['ticket_id'];
								if($custom_fields_details[$i]['ticket_id']!=$temp1[$a]['ticket_id'])
								{$temp['ticket_id']=$custom_fields_details[$i]['ticket_id'];
											$a=$custom_fields_details[$i]['ticket_id'];
											if(!empty($filedarr)){for($j=0; $j<count($filedarr); $j++){$temp[$filedarr[$j]]='-';}}
								}
								$temp[$custom_fields_details[$i]['field_name']]=$custom_fields_details[$i]['content'];
								$temp1[$a]=$temp;
							}
														
								$temp2=reset($temp1);
												
									foreach($audit_log as $key => $csm){
								if(in_array($audit_log[$key]['ticket_id'],$temp1[$csm['ticket_id']])){
										foreach($temp1[$csm['ticket_id']] as $keys=>$val){
													if($keys!='ticket_id') $audit_log[$key][$keys]=$val;
										}
							}else{
											foreach($temp2 as $keys=>$val){
															if($keys!='ticket_id') $audit_log[$key][$keys]='';
											}
									}
										
							} 
	}// get custom filed data loop done
			}
	
		//print_r($audit_log);
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
								
								
								include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
								$export = new export();
								$operators=array(1=>$vis_language['TXTCLIENT'], 2=>$vis_language['TXTSTAFF'], 3=>$vis_language['TXTSTAFF'],0=>$vis_language['TXTSYSTEM']);
						
						$j = 0;		 
						foreach ($audit_log as $i)
								{
									
												foreach ($i as $key => $val) {
																if ($key == 'ticket_hash') {
																				$report_exp[$j][$key] = $val; 
																}  elseif ($key == 'subject') {
																				$report_exp[$j][$key] = $val;
																}  elseif ($key == 'audit_log_operation') {
																				$report_exp[$j][$key] = $val;
																}  elseif ($key == 'creation_date') {
																				 $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
																				 
																}  /*elseif ($key == 'operator' ) {
																				
$report_exp[$j][$key]=($i['operator_id']==0?$vis_language['TXTSYSTEM']:($operators[$i['operator']].' ('.$i['operators'].')'));
																}*/ elseif ($key == 'department_name') {
																				$report_exp[$j][$key] = $val;
																}elseif ($key == 'modified_date') {
																				$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
																}  elseif ($key == 'status_title') {
																				$report_exp[$j][$key] = $val;
																}  elseif ($key == 'type_title') {
																				$report_exp[$j][$key] = $val;
																} elseif ($key == 'owner') {
																				$report_exp[$j][$key] = $val;
																} elseif ($key == 'level_title') {
																				$report_exp[$j][$key] = $val;
																} elseif ($key == 'category_title') {
																				$report_exp[$j][$key] = $val;
																} elseif ($key == 'subcategory_title') {
																				$report_exp[$j][$key] = $val;
																} elseif ($key == 'item_title') {
																				$report_exp[$j][$key] = $val;
																} elseif ($key == 'subcategory_title') {
																				$report_exp[$j][$key] = $val;
																}elseif ($key != 'ticket_id' && $key != 'old_info' && $key != 'new_info' && $key != 'closed_time' && $key != 'item_id' && $key != 'operation_id' && $key != 'department_id' && $key != 'operator_id' && $key != 'ticket_entry' && $key != 'entry_date' && $key != 'receive_date') {
																				$report_exp[$j][$key] = $val;
																}																				
} 
 
  
 
$j++;
								} 
 
  
		 							
$completed_date = date('Y-m-d H:i:s');
$report_exp['Report_title'] = $report_title;
								$filename = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
								if ($operation['export_type'] == 1) {
												$export->export_csv($report_exp, $filename, '', VIS_SLAVE_DB);
												if (file_exists('../downloads/' . $filename . '.csv')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 2) {
												$export->export_html($report_exp, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.html')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 3) {
												
												
												$export->export_xlsx($report_exp, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.xls')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								}
								
								if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
												$database->database_disconnect();
												$database = new class_database();
												include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
												$object = new class_report($database);
												
								}else {
									include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
												$object = new class_report($database);
								}
								$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
								if (!$result)
												return false;
								$this->email_send_download($operation);
								return true;
				}
				
				else
								include(VIS_PATH_TEMPLATE . 'ticket_avtivity_by_department_report.php');
				
				
				
} else
				include(VIS_PATH_TEMPLATE . 'report_form.php');
$database->database_disconnect();
return true;
}
function ticket_ci_relation_report($operation)
{
	global $vis_language;
    $report_title = $vis_language['TXTTICKETCIRELATION'];
    $export       = 1;
    if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
	$database = new class_database('mysql', 1);
    else
	$database = new class_database();
	include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
    $tkt_object      = new class_ticket($database);
	$departments     = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
	if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
	if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
		$filter = $operation['filter'];
		else {
		if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
			$data = $operation;
		    else
			$data = parse_form_array($operation['vis_action']);
								
		    if (count($data) == 0) {
			include(VIS_PATH_TEMPLATE . 'report_form.php');
			return true;
			}
			if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
			return false;
			}
			if ($data['duration'] == 4) {
			list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
			if (!$ret) {
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
			return false;
				}
			}
			if ($data['duration'] == 0 && $data['department_id'] == 0) {
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
	
				return false;
		    } else if ($data['duration'] != 0 && $data['department_id'] == 0) {
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
	
				return false;
			} else if ($data['duration'] == 0 && $data['department_id'] != 0) {
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
	
				return false;
			}
			$currentdate = date('Y-m-d h:i:s');
		    if ($data['duration'] == 1) //if select Last 30 days duration
			{
			$monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
			$filter    = VIS_TABLE_PREFIX . 'ticket.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and '. VIS_TABLE_PREFIX .'ticket.department_id=' . $data['department_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket.draft_id=0  and '.VIS_TABLE_PREFIX . 'itil_relation.attach_type=0 ';
			} 
			else if ($data['duration'] == 2) //if select Last 7 days duration
			{
			$weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
			$filter   = VIS_TABLE_PREFIX . 'ticket.receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and '. VIS_TABLE_PREFIX .'ticket.department_id=' . $data['department_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket.draft_id=0 and '.VIS_TABLE_PREFIX . 'itil_relation.attach_type=0 ';
			} 
			else if ($data['duration'] == 3) //if select Last 24 hours duration
			{
			$hour   = strtotime("-1 day ") - VIS_TIMEZONE;
			$filter = VIS_TABLE_PREFIX . 'ticket.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and '. VIS_TABLE_PREFIX .'ticket.department_id=' . $data['department_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket.draft_id=0 and '.VIS_TABLE_PREFIX . 'itil_relation.attach_type=0 ';
			} 
			else if ($data['duration'] == 4) //if select Custom duration
			{
			$from   = strtotime($data['from_date']) - VIS_TIMEZONE;
			$to     = strtotime($data['to_date']) - VIS_TIMEZONE;
			$filter = VIS_TABLE_PREFIX . 'ticket.receive_date between ' . $from . ' AND ' . $to . ' and '. VIS_TABLE_PREFIX .'ticket.department_id=' . $data['department_id'] . ' and ' . VIS_TABLE_PREFIX . 'ticket.draft_id=0 and '.VIS_TABLE_PREFIX . 'itil_relation.attach_type=0 ';
			}
		}
		   $filter .= ' and ' . VIS_TABLE_PREFIX . 'ticket.is_deleted!=1 and ' . VIS_TABLE_PREFIX . 'ticket.is_spam!=1';
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
				$object       = new class_report($database);
				$report = $object->get_ticket_ci_relation_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
				include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
				$export = new export();
				$j      = 0;
				foreach ($report as $i) {
				foreach ($i as $key => $val) {
				if ($key == 'ticket_hash' || $key == 'subject')
				$report_exp[$j][$key] = $val;
				elseif ($key == 'receive_date') {
                $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);  
				} 
			    elseif ($key == 'first_name' || $key == 'last_name' || $key == 'email' || $key == 'group_name' || $key == 'ci_name')
				$report_exp[$j][$key] = $val;
																
				}
				if($report_exp[$j]['ci_name']==''){ $report_exp[$j]['ci_name'] = '-';}
				
					$report_exp1[$j]['ticket_hash']= $report_exp[$j]['ticket_hash'];
					$report_exp1[$j]['subject']= $report_exp[$j]['subject'];
					$report_exp1[$j]['client_name']= $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					$report_exp1[$j]['email']= $report_exp[$j]['email'];
					$report_exp1[$j]['organization']= $report_exp[$j]['group_name'];
					$report_exp1[$j]['creation_date']= $report_exp[$j]['receive_date'];
					$report_exp1[$j]['attach_ci']= $report_exp[$j]['ci_name'];
					$j++;
			}
		$completed_date = date('Y-m-d H:i:s');
		$report_exp1['Report_title'] = $report_title;
		
		$filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
		if ($operation['export_type'] == 1) {
		$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
		if (file_exists('../downloads/' . $filename . '.csv')) {
		$filename = $filename;
		} else {
		$filename = '';
		}
		} else if ($operation['export_type'] == 2) {
		$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
												
		if (file_exists('../downloads/' . $filename . '.html')) {
			$filename = $filename;
		} else {
		$filename = '';
			}
		} else if ($operation['export_type'] == 3) {
		$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
												
		if (file_exists('../downloads/' . $filename . '.xls')) {
		$filename = $filename;
		} else {
		$filename = '';
		}
	    }
		if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
		$database->database_disconnect();
		$database = new class_database();
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
		$object = new class_report($database);
		}
		$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
		if (!$result)
		return false;
		$this->email_send_download($operation);
		return true;
		} else
		include(VIS_PATH_TEMPLATE . 'ticket_ci_relation_report.php');
   } else
	include(VIS_PATH_TEMPLATE . 'report_form.php');
$database->database_disconnect();
return true;
}
function problem_ci_relation_report($operation)
{
	global $vis_language;
    $report_title = $vis_language['TXTPROBLEMCIRELATION'];
    $export       = 1;
    if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
	$database = new class_database('mysql', 1);
    else
	$database = new class_database();
	include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
    $tkt_object      = new class_ticket($database);
	$departments     = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
	if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
	if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
		$filter = $operation['filter'];
		else {
		if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
			$data = $operation;
		    else
			$data = parse_form_array($operation['vis_action']);
								
		    if (count($data) == 0) {
			include(VIS_PATH_TEMPLATE . 'report_form.php');
			return true;
			}
			if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
			return false;
			}
			if ($data['duration'] == 4) {
			list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
			if (!$ret) {
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
			return false;
				}
			}
			if ($data['duration'] == 0 && $data['department_id'] == 0) {
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
			return false;
		    } else if ($data['duration'] != 0 && $data['department_id'] == 0) {
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
			return false;
			} else if ($data['duration'] == 0 && $data['department_id'] != 0) {
		    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
			return false;
			}
			$currentdate = date('Y-m-d h:i:s');
		    if ($data['duration'] == 1) //if select Last 30 days duration
			{
			$monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
			$filter    = VIS_TABLE_PREFIX . 'itil_problems.date_created between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and '. VIS_TABLE_PREFIX .'itil_problems.department_id=' . $data['department_id'].' and '.VIS_TABLE_PREFIX . 'itil_problems.status_id!=3 and '.VIS_TABLE_PREFIX . 'itil_relation.attach_type=4 ';
			} 
			else if ($data['duration'] == 2) //if select Last 7 days duration
			{
			$weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
			$filter   = VIS_TABLE_PREFIX . 'itil_problems.date_created between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and '. VIS_TABLE_PREFIX .'itil_problems.department_id=' . $data['department_id'] .' and '.VIS_TABLE_PREFIX . 'itil_problems.status_id!=3 and '.VIS_TABLE_PREFIX . 'itil_relation.attach_type=4 ';
			} 
			else if ($data['duration'] == 3) //if select Last 24 hours duration
			{
			$hour   = strtotime("-1 day ") - VIS_TIMEZONE;
			$filter = VIS_TABLE_PREFIX . 'itil_problems.date_created between ' . $hour . ' AND ' . strtotime($currentdate) . ' and '. VIS_TABLE_PREFIX .'itil_problems.department_id=' . $data['department_id'] .' and '.VIS_TABLE_PREFIX . 'itil_problems.status_id!=3 and '.VIS_TABLE_PREFIX . 'itil_relation.attach_type=4 ';
			} 
			else if ($data['duration'] == 4) //if select Custom duration
			{
			$from   = strtotime($data['from_date']) - VIS_TIMEZONE;
			$to     = strtotime($data['to_date']) - VIS_TIMEZONE;
			$filter = VIS_TABLE_PREFIX . 'itil_problems.date_created between ' . $from . ' AND ' . $to . ' and '. VIS_TABLE_PREFIX .'itil_problems.department_id=' . $data['department_id'] .' and '.VIS_TABLE_PREFIX . 'itil_problems.status_id!=3 and '.VIS_TABLE_PREFIX . 'itil_relation.attach_type=4 ' ;
			}
		}
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
				$object       = new class_report($database);
				$report = $object->get_problem_ci_relation_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
				include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
				$export = new export();
				$j      = 0;
				foreach ($report as $i) {
				foreach ($i as $key => $val) {
				if ($key == 'problem_hash' || $key == 'subject')
				$report_exp[$j][$key] = $val;
				elseif ($key == 'date_created') {
                $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);  
				} 
				elseif ($key == 'department_name' || $key == 'domain_name')
				$report_exp[$j][$key] = $val;
			    elseif ($key == 'ci_name')
				$report_exp[$j][$key] = $val;
				elseif ($key == 'first_name' || $key == 'last_name')
				$report_exp[$j][$key] = $val;
				}
				if($report_exp[$j]['ci_name']==''){ $report_exp[$j]['ci_name'] = '-';}
				
					$report_exp1[$j]['problem_hash']= $report_exp[$j]['problem_hash'];
					$report_exp1[$j]['subject']= $report_exp[$j]['subject'];
					$report_exp1[$j]['From']= $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					$report_exp1[$j]['department']= $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
					$report_exp1[$j]['creation_date']= $report_exp[$j]['date_created'];
					$report_exp1[$j]['attach_ci']= $report_exp[$j]['ci_name'];
					$j++;
			}
		$completed_date = date('Y-m-d H:i:s');
		$report_exp1['Report_title'] = $report_title;
		
		$filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
		if ($operation['export_type'] == 1) {
		$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
		if (file_exists('../downloads/' . $filename . '.csv')) {
		$filename = $filename;
		} else {
		$filename = '';
		}
		} else if ($operation['export_type'] == 2) {
		$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
												
		if (file_exists('../downloads/' . $filename . '.html')) {
			$filename = $filename;
		} else {
		$filename = '';
			}
		} else if ($operation['export_type'] == 3) {
		$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
												
		if (file_exists('../downloads/' . $filename . '.xls')) {
		$filename = $filename;
		} else {
		$filename = '';
		}
	    }
		if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
		$database->database_disconnect();
		$database = new class_database();
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
		$object = new class_report($database);
		}
		$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
		if (!$result)
		return false;
		$this->email_send_download($operation);
		return true;
		} else
		include(VIS_PATH_TEMPLATE . 'problem_ci_relation_report.php');
   } else
	include(VIS_PATH_TEMPLATE . 'report_form.php');
$database->database_disconnect();
return true;
}
function changes_ci_relation_report($operation)
{
	global $vis_language;
    $report_title = $vis_language['TXTCHANGESCIRELATION'];
    $export       = 1;
    if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
	$database = new class_database('mysql', 1);
    else
	$database = new class_database();
	include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
    $tkt_object      = new class_ticket($database);
	$departments     = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
	if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
	if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
		$filter = $operation['filter'];
		else {
		if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
			$data = $operation;
		    else
			$data = parse_form_array($operation['vis_action']);
								
		    if (count($data) == 0) {
			include(VIS_PATH_TEMPLATE . 'report_form.php');
			return true;
			}
			if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
			return false;
			}
			if ($data['duration'] == 4) {
			list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
			if (!$ret) {
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
			return false;
				}
			}
			if ($data['duration'] == 0 && $data['department_id'] == 0) {
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
			return false;
		    } else if ($data['duration'] != 0 && $data['department_id'] == 0) {
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
			return false;
			} else if ($data['duration'] == 0 && $data['department_id'] != 0) {
		    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
			return false;
			}
			$currentdate = date('Y-m-d h:i:s');
		    if ($data['duration'] == 1) //if select Last 30 days duration
			{
			$monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
			$filter    = VIS_TABLE_PREFIX . 'itil_changes.date_created between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and '. VIS_TABLE_PREFIX .'itil_changes.department_id=' . $data['department_id'].' and '.VIS_TABLE_PREFIX . 'itil_changes.status_id!=3 and '.VIS_TABLE_PREFIX . 'itil_relation.attach_type=9 ';
			} 
			else if ($data['duration'] == 2) //if select Last 7 days duration
			{
			$weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
			$filter   = VIS_TABLE_PREFIX . 'itil_changes.date_created between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and '. VIS_TABLE_PREFIX .'itil_changes.department_id=' . $data['department_id'].' and '.VIS_TABLE_PREFIX . 'itil_changes.status_id!=3 and '.VIS_TABLE_PREFIX . 'itil_relation.attach_type=9 ' ;
			} 
			else if ($data['duration'] == 3) //if select Last 24 hours duration
			{
			$hour   = strtotime("-1 day ") - VIS_TIMEZONE;
			$filter = VIS_TABLE_PREFIX . 'itil_changes.date_created between ' . $hour . ' AND ' . strtotime($currentdate) . ' and '. VIS_TABLE_PREFIX .'itil_changes.department_id=' . $data['department_id'].' and '.VIS_TABLE_PREFIX . 'itil_changes.status_id!=3 and '.VIS_TABLE_PREFIX . 'itil_relation.attach_type=9 ';
			} 
			else if ($data['duration'] == 4) //if select Custom duration
			{
			$from   = strtotime($data['from_date']) - VIS_TIMEZONE;
			$to     = strtotime($data['to_date']) - VIS_TIMEZONE;
			$filter = VIS_TABLE_PREFIX . 'itil_changes.date_created between ' . $from . ' AND ' . $to . ' and '. VIS_TABLE_PREFIX .'itil_changes.department_id=' . $data['department_id'].' and '.VIS_TABLE_PREFIX . 'itil_changes.status_id!=3 and '.VIS_TABLE_PREFIX . 'itil_relation.attach_type=9 ';
			}
		}
		        include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
				$object       = new class_report($database);
				$report = $object->get_changes_ci_relation_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
				include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
				$export = new export();
				$j      = 0;
				foreach ($report as $i) {
				foreach ($i as $key => $val) {
				if ($key == 'changes_hash' || $key == 'subject')
				$report_exp[$j][$key] = $val;
				elseif ($key == 'date_created') {
                $report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);  
				} 
			    elseif ($key == 'first_name' || $key == 'last_name' || $key == 'department_name' || $key == 'domain_name' || $key == 'ci_name')
				$report_exp[$j][$key] = $val;
																
				}
				if($report_exp[$j]['ci_name']==''){ $report_exp[$j]['ci_name'] = '-';}
				
					$report_exp1[$j]['changes_hash']= $report_exp[$j]['changes_hash'];
					$report_exp1[$j]['subject']= $report_exp[$j]['subject'];
					$report_exp1[$j]['From']= $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
					$report_exp1[$j]['department']= $report_exp[$j]['domain_name'].' : '.$report_exp[$j]['department_name'];
					$report_exp1[$j]['creation_date']= $report_exp[$j]['date_created'];
					$report_exp1[$j]['attach_ci']= $report_exp[$j]['ci_name'];
					$j++;
			}
		$completed_date = date('Y-m-d H:i:s');
		$report_exp1['Report_title'] = $report_title;
		
		$filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
		if ($operation['export_type'] == 1) {
		$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
		if (file_exists('../downloads/' . $filename . '.csv')) {
		$filename = $filename;
		} else {
		$filename = '';
		}
		} else if ($operation['export_type'] == 2) {
		$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
												
		if (file_exists('../downloads/' . $filename . '.html')) {
			$filename = $filename;
		} else {
		$filename = '';
			}
		} else if ($operation['export_type'] == 3) {
		$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
												
		if (file_exists('../downloads/' . $filename . '.xls')) {
		$filename = $filename;
		} else {
		$filename = '';
		}
	    }
		if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
		$database->database_disconnect();
		$database = new class_database();
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
		$object = new class_report($database);
		}
		$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
		if (!$result)
		return false;
		$this->email_send_download($operation);
		return true;
		} else
		include(VIS_PATH_TEMPLATE . 'changes_ci_relation_report.php');
   } else
	include(VIS_PATH_TEMPLATE . 'report_form.php');
$database->database_disconnect();
return true;
}
	function task_linkedto_ticket_report($operation)
	{
		global $vis_language;
        $report_title = $vis_language['TXTTASKATTACHTOTICKET'];
        $export       = 1;
        if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
            $database = new class_database('mysql', 1);
        else
            $database = new class_database();
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
			$tkt_object      = new class_ticket($database);
			$departments     = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
			 $staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
		 if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
            if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
                $filter = $operation['filter'];
            else {
                if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
                    $data = $operation;
                else
                    $data = parse_form_array($operation['vis_action']);
                if (count($data) == 0) {
                    include(VIS_PATH_TEMPLATE . 'report_form.php');
                    return true;
                }
                 if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
                    return false;
                }
                if ($data['duration'] == 4) {
                    list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
                    if (!$ret) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '"); </script>';
                        return false;
                    }
                }
               if ($data['duration'] == 0 && $data['department_id'] == -1) {
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
			return false;
		    } else if ($data['duration'] != 0 && $data['department_id'] == -1) {
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPTFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
			return false;
			} else if ($data['duration'] == 0 && $data['department_id'] != -1) {
		    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
			return false;
			}
                  
				  
				
				$currentdate = date('Y-m-d h:i:s');
		    if ($data['duration'] == 1) //if select Last 30 days duration
			{
			$monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
			$filter    = VIS_TABLE_PREFIX . 'ticket.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and '. VIS_TABLE_PREFIX .'ticket.department_id=' . $data['department_id'];
			} 
			else if ($data['duration'] == 2) //if select Last 7 days duration
			{
			$weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
			$filter   = VIS_TABLE_PREFIX . 'ticket.receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and '. VIS_TABLE_PREFIX .'ticket.department_id=' . $data['department_id'] ;
			} 
			else if ($data['duration'] == 3) //if select Last 24 hours duration
			{
			$hour   = strtotime("-1 day ") - VIS_TIMEZONE;
			$filter = VIS_TABLE_PREFIX . 'ticket.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and '. VIS_TABLE_PREFIX .'ticket.department_id=' . $data['department_id'];
			} 
			else if ($data['duration'] == 4) //if select Custom duration
			{
			$from   = strtotime($data['from_date']) - VIS_TIMEZONE;
			$to     = strtotime($data['to_date']) - VIS_TIMEZONE;
			$filter = VIS_TABLE_PREFIX . 'ticket.receive_date between ' . $from . ' AND ' . $to . ' and '. VIS_TABLE_PREFIX .'ticket.department_id=' . $data['department_id'];
			}
			}
			 include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
             $object = new class_report($database);
			 $report=array();
			 $report = $object->get_ticket_task_relation_report($filter,VIS_STAFF_ID, VIS_LOCATION_AREA);    
		    
		     if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
                include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
                $export         = new export();
				 include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'report.php');
                $report_obj         = new report();
				$j=0;
                foreach ($report as $task_id => $i) {
                    foreach ($i as $key => $val) {
						if ($key == 'ticket_hash') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'subject') {
                            $report_exp[$j][$key] = $val;
							
                        } elseif ($key == 'receive_date') {	
                           $report_exp[$j][$key] = $val;
                        }  elseif ($key == 'task_title') {
                            $report_exp[$j][$key] = $val;
                        } elseif ($key == 'task_status') {
							if($report[$j]['enddate_diff']<=0){
								if($val!=3 && $val!=4 && $val!=5 && $val!=1){
									$report_exp[$j][$key] =$vis_language['TXTOVERDUE']; 
								}else{
									if($val==1) {
										$report_exp[$j][$key] =$vis_language['TXTINPROGRESS']; 
									}else if($val==3){
										$report_exp[$j][$key] =$vis_language['TXTCOMPLETED']; 
									}else if($val==4){ 
										$report_exp[$j][$key] =$vis_language['TXTCANCELED'];  
									}else if($val==5){ 
										$report_exp[$j][$key] =$vis_language['TXTNOTSTARTED']; 
									}
								} 
							}else{ 
								if($val==1) {
									$report_exp[$j][$key] =$vis_language['TXTINPROGRESS']; 
								}else if($val==2){
									$report_exp[$j][$key] =$vis_language['TXTOVERDUE'];  
								}else if($val==3){
									$report_exp[$j][$key] =$vis_language['TXTCOMPLETED'];  
								}else if($val==4){ 
									$report_exp[$j][$key] =$vis_language['TXTCANCELED']; 
								}else if($val==5){ 
									$report_exp[$j][$key] =$vis_language['TXTNOTSTARTED']; 	
								}
							} 
						}     
                      
					 }
					$report_exp1[$j]['ticket_hash']= $report_exp[$j]['ticket_hash'];
					$report_exp1[$j]['receive_date']= $report_exp[$j]['receive_date'];
					$report_exp1[$j]['subject']= $report_exp[$j]['subject'];
					$report_exp1[$j]['task']= $report_exp[$j]['task_title'];
					$report_exp1[$j]['task status']= $report_exp[$j]['task_status'];
                    $j++;
                }
                $completed_date = date('Y-m-d H:i:s');
				$report_exp1['Report_title'] = $report_title;
                $filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
                if ($operation['export_type'] == 1) {
                    $export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
                    if (file_exists('../downloads/' . $filename . '.csv')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 2) {
                    $export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.html')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                } else if ($operation['export_type'] == 3) {
                    
                    
                    $export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
                    
                    if (file_exists('../downloads/' . $filename . '.xls')) {
                        $filename = $filename;
                    } else {
                        $filename = '';
                    }
                }
																
																if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
                    $database->database_disconnect();
                    $database = new class_database();
                    include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
                    $object = new class_report($database);
                    
                }
                $result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
                if (!$result)
                    return false;
                $this->email_send_download($operation);
                return true;
            }else
                include(VIS_PATH_TEMPLATE . 'ticket_task_relation_report.php');
		 } 
		 
		
		else
            include(VIS_PATH_TEMPLATE . 'report_form.php');
         $database->database_disconnect();
        return true;
	}
		
	
function customfield_staff_report($operation)
{	
global $vis_language;
$report_title = $vis_language['TXTCFRFSTCRPT'];
$export       = 1;
if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
				$database = new class_database('mysql', 1);
else
				$database = new class_database();
include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
$tkt_object = new class_ticket($database);
include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'staff.php');
$object_staff = new class_staff($database);
include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'setting.php');
$object_setting = new class_setting($database);
$filter         = 'location in (2)';
$custom_group   = $object_setting->get_custom_group_list($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
for ($i = 0; $i < count($custom_group); $i++) {
				$filter = ' ' . VIS_TABLE_PREFIX . 'customfield.customgroup_id=' . $custom_group[$i]['customgroup_id'] . ' and ' . VIS_TABLE_PREFIX . 'customfield.is_enabled=1 ';
				list($customfield_info, $customfield_details) = $object_setting->get_customfield(0, $filter, '', 0, 0, VIS_STAFF_ID, VIS_LOCATION_AREA);
				$customfield_array[] = $customfield_details;
}
//print_r($customfield_array);
 $staff_details= $tkt_object->get_staff_list(VIS_STAFF_ID, VIS_LOCATION_AREA);

//$departments   = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
$dept_ids    = '';
for ($d = 0; $d < count($staff_details); $d++) {
				$dept_ids .= $staff_details[$d]['staff_id'] . ',';
}
if ($dept_ids != '') {
				$dept_ids = substr($dept_ids, 0, -1);
//echo "<pre>";print_r($dept_id);echo "</pre>";die;

}
$dept_id  = ($staff_details[0]['staff_id'] == '' || $staff_details[0]['staff_id'] == 0) ? $dept_ids : $staff_details[0]['staff_id'];

$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
								$filter = $operation['filter'];
				else {
								if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
												$data = $operation;
								else
												$data = parse_form_array($operation['vis_action']);
								if (count($data) == 0) {
												include(VIS_PATH_TEMPLATE . 'report_form.php');
												return true;
								}
								if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								if ($data['duration'] == 4) {
												list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
												if (!$ret) {
																echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
																return false;
												}
								}
								if ($data['duration'] == 0) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								if ($data['field_type'] == "") {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								
								$currentdate = date('Y-m-d h:i:s');
								if ($data['duration'] == 1) //if select Last 30 days duration
												{
												$monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
												if ($data['staff_id'] != 0 && $data['field_type'] != 0) {
																$filter = 't.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and st.staff_id=' . $data['staff_id'] . ' and cf.field_group=' . $data['field_type'].' and cf.is_enabled = 1 ';
												} else if ($data['staff_id'] == 0 && $data['field_type'] != 0) {
																$filter = 't.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and cf.field_group=' . $data['field_type'].' and cf.is_enabled = 1 ';
												} else if ($data['staff_id'] != 0 && $data['field_type'] == 0) {
																$filter = 't.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) . ' and st.staff_id=' . $data['staff_id'].' and cf.is_enabled=1 ';
												} else {
																$filter = 't.receive_date between  ' . $monthDate . ' AND ' . strtotime($currentdate) .' and cf.is_enabled = 1 ';
												}
								} else if ($data['duration'] == 2) //if select Last 7 days duration
												{
												$weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
												if ($data['staff_id'] != 0 && $data['field_type'] != 0) {
																$filter = 't.receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and st.staff_id=' . $data['staff_id'] . ' and cf.field_group=' . $data['field_type'].' and cf.is_enabled = 1 ';
												} else if ($data['staff_id'] == 0 && $data['field_type'] != 0) {
																$filter = 't.receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and cf.field_group=' . $data['field_type'].' and cf.is_enabled = 1 ';
												} else if ($data['staff_id'] != 0 && $data['field_type'] == 0) {
																$filter = 't.receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) . ' and st.staff_id=' . $data['staff_id'].' and cf.is_enabled = 1 ';
												} else {
																$filter = 't.receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) .' and cf.is_enabled = 1 ';
												}
								} else if ($data['duration'] == 3) //if select Last 24 hours duration
												{
												$hour = strtotime("-1 day ") - VIS_TIMEZONE;
												if ($data['staff_id'] != 0 && $data['field_type'] != 0) {
																$filter = 't.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and st.staff_id=' . $data['staff_id'] . ' and cf.field_group=' . $data['field_type'].' and cf.is_enabled = 1 ';
												} else if ($data['staff_id'] == 0 && $data['field_type'] != 0) {
																$filter = 't.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and cf.field_group=' . $data['field_type'].' and cf.is_enabled = 1 ';
												} else if ($data['staff_id'] != 0 && $data['field_type'] == 0) {
																$filter = 't.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) . ' and st.staff_id=' . $data['staff_id'].' and cf.is_enabled = 1 ';
												} else {
																$filter = 't.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) .' and cf.is_enabled = 1 ';
												}
								} else if ($data['duration'] == 4) //if select Custom duration
												{
												$from = strtotime($data['from_date']) - VIS_TIMEZONE;
												$to   = strtotime($data['to_date']) - VIS_TIMEZONE;
												if ($data['staff_id'] != 0 && $data['field_type'] != 0) {
																$filter = 't.receive_date between ' . $from . ' AND ' . $to . ' and st.staff_id=' . $data['staff_id'] . ' and cf.field_group=' . $data['field_type'].' and cf.is_enabled = 1 ';
												} else if ($data['staff_id'] == 0 && $data['field_type'] != 0) {
																$filter = 't.receive_date between ' . $from . ' AND ' . $to . ' and cf.field_group=' . $data['field_type'].' and cf.is_enabled = 1 ';
												} else if ($data['staff_id'] != 0 && $data['field_type'] == 0) {
																$filter = 't.receive_date between ' . $from . ' AND ' . $to . ' and st.staff_id=' . $data['staff_id'].' and cf.is_enabled = 1 ';
												}
												
												else {
																$filter = 't.receive_date between ' . $from . ' AND ' . $to .' and cf.is_enabled = 1 ';
												}
								}
				}
				
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
				$object      = new class_report($database);
				$report      = $object->get_customfield_staff_report($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
				$custFilter1 = ' location =2  and is_enabled=1 ';
				
				$custom_group = $object_setting->get_custom_group_list($custFilter1, VIS_STAFF_ID, VIS_LOCATION_AREA);
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
								include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
								$export         = new export();
$j=0;
foreach ($report as $i){      
foreach ($i as $key => $val){
if($key == 'ticket_hash'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'receive_date'){
$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
}
elseif($key == 'field_name'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'content'){
$report_exp[$j][$key] = $val;
}
elseif($key == 'first_name' || $key == 'last_name' || $key == 'email' ){
$report_exp[$j][$key] = $val;
}
}
$report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash'];
$report_exp1[$j]['client_name'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
$report_exp1[$j]['email'] = $report_exp[$j]['email'];
$report_exp1[$j]['create_date'] = $report_exp[$j]['receive_date'];				
$report_exp1[$j]['field_name'] = $report_exp[$j]['field_name']; 
$report_exp1[$j]['field_value'] = $report_exp[$j]['content']; 
												$j++;
								}
								$completed_date = date('Y-m-d H:i:s');
								$report_exp1['Report_title'] = $report_title;
				
								$filename       = rand(strtotime($completed_date), strtotime($s_request_date));
								
								if ($operation['export_type'] == 1) {
									
												$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
												if (file_exists('../downloads/' . $filename . '.csv')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 2) {
												$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.html')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 3) {
												
												
												$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.xls')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								}
								
								if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
												$database->database_disconnect();
												$database = new class_database();
												include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
												$object = new class_report($database);
												
								}
								$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
								if (!$result)
												return false;
								$this->email_send_download($operation);
								return true;
				} else
								include(VIS_PATH_TEMPLATE . 'customfield_staff_report.php');
} else
				include(VIS_PATH_TEMPLATE . 'report_form.php');
$database->database_disconnect();
return true;
}
/*************************** New Report 1-5-2017 *********************************************/
function ticket_closed_by_user_report($operation)
{	   
	global $vis_language;

	$report_title = $vis_language['TXTTKTCLOSEDUSER'];
	if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
		$database = new class_database('mysql', 1);
	else
		$database = new class_database();
	
	$export      = 1;
	$report_type = 95;
	include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
	$tkt_object  = new class_ticket($database);
	$departments = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
	$staff = $tkt_object->get_staff_list(VIS_STAFF_ID, VIS_LOCATION_AREA);
	
	if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
		if (isset($operation['vis_export']) && $operation['vis_export'] == 1){
			$filter = $operation['filter'];
			$data = $operation;
		}else {
			if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
				$data = $operation;
			else
				$data = parse_form_array($operation['vis_action']);
			
			$staff_id = $data['staff_id'];
	
			if (count($data) == 0) {
				include(VIS_PATH_TEMPLATE . 'report_form.php');
				return true;
			}
			
			if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
				return false;
			}
			if ($data['duration'] == 4) {
				list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
				if (!$ret) {
					echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
					return false;
				}
			}
			if ($data['duration'] == 0 || $data['department_id'] == 0) {
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
				return false;
			}
			$currentdate = date('Y-m-d h:i:s');
			if ($data['duration'] == 1) //if select Last 30 days duration
			{ 
				$monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
				//$filter    = 'avis_ticket.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) .' and avis_ticket.status_id=3 and '.VIS_TABLE_PREFIX.'auditlog.operator_id='.$staff_id.' and avis_ticket.department_id=' . $data['department_id'];
				$filter    = 'avis_ticket.receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) .' and '.VIS_TABLE_PREFIX.'ticket.status_id=3 and '.VIS_TABLE_PREFIX.'ticket_post.submitter=0 and '.VIS_TABLE_PREFIX.'ticket.last_replier_id='.$staff_id.' and avis_ticket.department_id=' . $data['department_id'];
			} 
			else if ($data['duration'] == 2) //if select Last 7 days duration
			{
				$weekDate = strtotime("-1 week ") - VIS_TIMEZONE;
				//$filter    = 'avis_ticket.receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) .' and avis_ticket.status_id=3 and '.VIS_TABLE_PREFIX.'auditlog.operator_id='.$staff_id.' and avis_ticket.department_id=' . $data['department_id'];
				$filter    = 'avis_ticket.receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) .' and avis_ticket.status_id=3 and '.VIS_TABLE_PREFIX.'ticket_post.submitter=0 and '.VIS_TABLE_PREFIX.'ticket.last_replier_id='.$staff_id.' and '.VIS_TABLE_PREFIX.'ticket.department_id=' . $data['department_id'];
			} 
			else if ($data['duration'] == 3) //if select Last 24 hours duration
			{
				$hour   = strtotime("-1 day ") - VIS_TIMEZONE;
				//$filter    = 'avis_ticket.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) .' and avis_ticket.status_id=3 and '.VIS_TABLE_PREFIX.'auditlog.operator_id='.$staff_id.' and avis_ticket.department_id=' . $data['department_id'];
				$filter    = 'avis_ticket.receive_date between ' . $hour . ' AND ' . strtotime($currentdate) .' and avis_ticket.status_id=3 and '.VIS_TABLE_PREFIX.'ticket_post.submitter=0 and '.VIS_TABLE_PREFIX.'ticket.last_replier_id='.$staff_id.' and '.VIS_TABLE_PREFIX.'ticket.department_id=' . $data['department_id'];
			} 
			else if ($data['duration'] == 4) //if select Custom duration
			{
				$from   = strtotime($data['from_date']) - VIS_TIMEZONE;
				$to     = strtotime($data['to_date']) - VIS_TIMEZONE;
				//$filter    = 'avis_ticket.receive_date between ' . $from . ' AND ' . $to .' and avis_ticket.status_id=3  and '.VIS_TABLE_PREFIX.'auditlog.operator_id='.$staff_id.' and avis_ticket.department_id=' . $data['department_id'];
				$filter    = 'avis_ticket.receive_date between ' . $from . ' AND ' . $to .' and avis_ticket.status_id=3  and '.VIS_TABLE_PREFIX.'ticket_post.submitter=0 and '.VIS_TABLE_PREFIX.'ticket.last_replier_id='.$staff_id.' and '.VIS_TABLE_PREFIX.'ticket.department_id=' . $data['department_id'];
			}
		}
		
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
		$object          = new class_report($database);
		
		$filter .= ' and ' . VIS_TABLE_PREFIX . 'ticket.is_deleted!=1 and ' . VIS_TABLE_PREFIX . 'ticket.is_spam!=1';
		
		$post_details = $object->get_ticked_closed_by_user_report($filter, $staff_id, VIS_LOCATION_AREA);
		if ($post_details != -1)
			for($i=0;$i<count($post_details);$i++){
				$report[$i]['ticket_hash']     = $post_details[$i]['ticket_hash'];
				$report[$i]['ticket_id']       = $post_details[$i]['ticket_id'];
				$report[$i]['subject']         = $post_details[$i]['subject'];
				$report[$i]['email']           = $post_details[$i]['email'];
				$report[$i]['first_name']      = $post_details[$i]['first_name'];
				$report[$i]['last_name']       = $post_details[$i]['last_name'];
				$report[$i]['receive_date']       = $post_details[$i]['receive_date'];
			}
		else
			$report = -1;
		
		if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
			include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
			$export = new export();
			$j      = 0;

			foreach ($report as $i) {
				foreach ($i as $key => $val) {
					if ($key == 'ticket_hash')
						$report_exp[$j][$key] = $val;
					elseif ($key == 'subject')
						$report_exp[$j][$key] = $val;
					elseif ($key == 'ticket_id')
						$report_exp[$j][$key] = $val;
					elseif ($key == 'email')
						$report_exp[$j][$key] = $val;
					elseif ($key == 'first_name')
						$report_exp[$j][$key] = $val;
					elseif ($key == 'last_name') 
						$report_exp[$j][$key] = $val;
					elseif ($key == 'receive_date')
						$report_exp[$j][$key] = format_datetime($val);
				}
				$report_exp1[$j]['ticket_hash'] = $report_exp[$j]['ticket_hash'];
				$report_exp1[$j]['subject'] = $report_exp[$j]['subject'];
				$report_exp1[$j]['email'] = $report_exp[$j]['email'];
				$report_exp1[$j]['client_name'] = $report_exp[$j]['first_name'].' '.$report_exp[$j]['last_name'];
				$report_exp1[$j]['created_on'] = $report_exp[$j]['receive_date'];
				$j++;
			}

			$report_exp1[$j]['total_closed'] = $vis_language['TXTTOTALTICKETSCLOSED'].": ".count($post_details);
			$report_exp1[$j]['ticket_hash'] = '';
			$report_exp1[$j]['subject'] = '';
			$report_exp1[$j]['email'] = '';
			$report_exp1[$j]['client_name']= '';
			$report_exp1[$j]['created_on'] = '';

			$completed_date = date('Y-m-d H:i:s');
			$report_exp1['Report_title'] = $report_title;

			$filename       = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
			if ($operation['export_type'] == 1) {
				$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
				if (file_exists('../downloads/' . $filename . '.csv')) {
					$filename = $filename;
				} else {
					$filename = '';
				}
			} else if ($operation['export_type'] == 2) {
				$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
				if (file_exists('../downloads/' . $filename . '.html')) {
					$filename = $filename;
				} else {
					$filename = '';
				}
			} else if ($operation['export_type'] == 3) {
				$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);

				if (file_exists('../downloads/' . $filename . '.xls')) {
					$filename = $filename;
				} else {
					$filename = '';
				}
			}
			if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
				$database->database_disconnect();
				$database = new class_database();
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
				$object = new class_report($database);
			}
			$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
			if (!$result)
				return false;
			
			$this->email_send_download($operation);
			return true;
		} 
		else
			include(VIS_PATH_TEMPLATE . 'ticket_closed_by_user_report.php');
	} else
		include(VIS_PATH_TEMPLATE . 'report_form.php');
		
	$database->database_disconnect();
	return true;
}
function ticket_flag_by_user($operation)
{	
	global $vis_language;
	$report_title = $vis_language['TXTTICKETFLAGTOUSRREP'];
	$export       = 1;
	if (isset($operation['slave_db']) && $operation['slave_db'] == 1)
					$database = new class_database('mysql', 1);
	else
					$database = new class_database();
	include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'ticket.php');
	$tkt_object = new class_ticket($database);
	$filter     = VIS_TABLE_PREFIX . 'domain.is_enabled=1 ';
	$tkt_object      = new class_ticket($database);
	$departments     = $tkt_object->get_department(VIS_TABLE_PREFIX . 'department.is_enabled=1 and ' . VIS_TABLE_PREFIX . 'domain.is_enabled=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
	$flags        = $tkt_object->get_flags_list(VIS_STAFF_ID, VIS_LOCATION_AREA, 'is_enabled=1');
	$staff_details= $tkt_object->get_staff_list(VIS_STAFF_ID, VIS_LOCATION_AREA);
	$domain     = $tkt_object->get_domain($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
	$staff_id = (isset($operation['vis_export']) && $operation['vis_export'] == 1) ? $operation['staff_id'] : VIS_STAFF_ID;
	if ((isset($operation['vis_action']) && $operation['vis_action'] != '') || isset($operation['vis_submit']) && $operation['vis_submit'] != '') {
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1)
								$filter = $operation['filter'];
				else {
								if (isset($operation['vis_submit']) && $operation['vis_submit'] != '')
												$data = $operation;
								else
												$data = parse_form_array($operation['vis_action']);
								if (count($data) == 0) {
												include(VIS_PATH_TEMPLATE . 'report_form.php');
												return true;
								}
								if (($data['from_date'] == '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] != '' && $data['to_date'] == '' && $data['duration'] == 4) || ($data['from_date'] == '' && $data['to_date'] != '' && $data['duration'] == 4)) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								/***** date difference ****/
								if ($data['duration'] == 4) {
												list($ret, $msg) = $this->report_date_range_validation($data['from_date'], $data['to_date']);
												if (!$ret) {
																echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $msg . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
																return false;
												}
								}
								if ($data['staff_id'] == 0 ) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								if ($data['flag_id'] == -1 ) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								if ($data['duration'] == 0 && $data['department_id'] == -1 ) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTFORGENERATE'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								} else if ($data['duration'] != 0 && $data['department_id'] == -1 ) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELECTDEPARTMENT'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								} else if ($data['duration'] == 0 && $data['department_id'] != -1 ) {
												echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['TXTSELDURATIONFORGEN'] . '");$("#vis_schedule_rpt_'.$operation['vis_operation'].'").hide();$("#vis_export_rpt_'.$operation['vis_operation'].'").hide(); </script>';
												return false;
								}
								
								$currentdate = date('Y-m-d h:i:s');						
								
					if ($data['duration'] == 1) //if select Last 30 days duration
                    {
                    $department_id = ($data['department_id'] == 0) ? '' : $data['department_id'];
					$flag_id = ($data['flag_id'] == 0) ? '' : $data['flag_id'];
                    $monthDate     = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month') - VIS_TIMEZONE;
                    $filter        = 'receive_date between ' . $monthDate . ' AND ' . strtotime($currentdate) .' and avis_staff.staff_id='.$data['staff_id'];
                    $filter .= ($data['department_id'] != 0) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
                    $filter .= ($data['flag_id'] != 0) ? ' and avis_ticket.flag_id=' . $data['flag_id'] : '';
					
					} else if ($data['duration'] == 2) //if select Last 7 days duration
						{
						$department_id = ($data['department_id'] == 0) ? '' : $data['department_id'];
						$flag_id = ($data['flag_id'] == 0) ? '' : $data['flag_id'];
						$weekDate      = strtotime("-1 week ") - VIS_TIMEZONE;
						$filter        = 'receive_date between ' . $weekDate . ' AND ' . strtotime($currentdate) .' and avis_staff.staff_id='.$data['staff_id'];
						$filter .= ($data['department_id'] != 0) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
						$filter .= ($data['flag_id'] != 0) ? ' and avis_ticket.flag_id=' . $data['flag_id'] : '';						
						
					} else if ($data['duration'] == 3) //if select Last 24 hours duration
						{
						$department_id = ($data['department_id'] == 0) ? '' : $data['department_id'];
						$flag_id = ($data['flag_id'] == 0) ? '' : $data['flag_id'];
						$hour          = strtotime("-1 day ") - VIS_TIMEZONE;
						$filter        = 'receive_date between ' . $hour . ' AND ' . strtotime($currentdate) .' and avis_staff.staff_id='.$data['staff_id'];
						$filter .= ($data['department_id'] != 0) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
						$filter .= ($data['flag_id'] != 0) ? ' and avis_ticket.flag_id=' . $data['flag_id'] : '';								
						
					} else if ($data['duration'] == 4) //if select Custom duration
						{
						$department_id = ($data['department_id'] == 0) ? '' : $data['department_id'];
						$flag_id = ($data['flag_id'] == 0) ? '' : $data['flag_id'];
						$from          = strtotime($data['from_date']) - VIS_TIMEZONE;
						$to            = strtotime($data['to_date']) - VIS_TIMEZONE;
						$filter        = 'receive_date between ' . $from . ' AND ' . $to .' and avis_staff.staff_id='.$data['staff_id'];
						$filter .= ($data['department_id'] != 0) ? ' and avis_ticket.department_id=' . $data['department_id'] : '';
						$filter .= ($data['flag_id'] != 0) ? ' and avis_ticket.flag_id=' . $data['flag_id'] : '';							
						
					}
						
				}
				include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
				
				$object = new class_report($database);
				//$report=$object->get_ticket_count_by_department($filter, $staff_id, VIS_LOCATION_AREA);  print_r($report);
				
				$report = $object-> get_ticket_flag_count_to_user($filter,VIS_STAFF_ID, VIS_LOCATION_AREA);
				
				
				if (isset($operation['vis_export']) && $operation['vis_export'] == 1) {
								
								
								include_once(VIS_PATH_FUNCTION . VIS_FUNCTION_PREFIX . 'export.php');
								$export = new export();
								
								$j = 0;
								foreach ($report as $i) {
												foreach ($i as $key => $val) {
																if ($key == 'ticket_hash') {
																				$report_exp[$j][$key] = $val;
																} elseif ($key == 'subject') {
																				$report_exp[$j][$key] = $val;
																} elseif ($key == 'Creation_date') {
																				$report_exp[$j][$key] = format_datetime_rpt($val,$staff_id);
																}  elseif ($key == 'department_name') {
																				$report_exp[$j][$key] = $val;
																}																elseif ($key == 'flag_name') {
																				$report_exp[$j][$key] = $val;
																}
												}
												
									$report_exp1[$j]['ticket_hash']=$report_exp[$j]['ticket_hash'];
									$report_exp1[$j]['subject']=$report_exp[$j]['subject'];
									$report_exp1[$j]['Creation_date']=$report_exp[$j]['Creation_date'];
									$report_exp1[$j]['department_name']=$report_exp[$j]['department_name'];
									$report_exp1[$j]['flag_name']=$report_exp[$j]['flag_name'];
									$j++;
								}
								$completed_date = date('Y-m-d H:i:s');
								$report_exp1['Report_title'] = $report_title;	// To display title which shows on report form
				//$report_exp1['Report_title'] = $operation['export_name']; // To display title which entered in request report popup form
				
								$filename = $operation['export_name'].'_'.rand(strtotime($completed_date), strtotime($s_request_date));
								if ($operation['export_type'] == 1) {
												$export->export_csv($report_exp1, $filename, '', VIS_SLAVE_DB);
												if (file_exists('../downloads/' . $filename . '.csv')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 2) {
												$export->export_html($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.html')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								} else if ($operation['export_type'] == 3) {
												
												
												$export->export_xlsx($report_exp1, $filename, '', VIS_SLAVE_DB);
												
												if (file_exists('../downloads/' . $filename . '.xls')) {
																$filename = $filename;
												} else {
																$filename = '';
												}
								}
								
								if (isset($operation['slave_db']) && $operation['slave_db'] == 1) {
												$database->database_disconnect();
												$database = new class_database();
												include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'report.php');
												$object = new class_report($database);
												
								}
								$result = $object->update_download_list($filename, $completed_date, $operation['export_id']);
								if (!$result)
												return false;
								$this->email_send_download($operation);
								return true;
				}
				
				else
								include(VIS_PATH_TEMPLATE . 'flag_report.php');
				
				
				
} else
				include(VIS_PATH_TEMPLATE . 'report_form.php');
$database->database_disconnect();
return true;
} 	
}
		
?>