<?php
include("../../../include/startup.php"); 
if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_package_price'){
	$packages_id = $_REQUEST['packages_id'];
	$row = utility::getTableRowByField("fixed_packages","id",$packages_id);
	$returnArray["price"] = $row["price"];
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_hourly_packages_price'){
	$hourly_packages_id = $_REQUEST['hourly_packages_id'];
	$row = utility::getTableRowByField("hourly_packages","id",$hourly_packages_id);
	$returnArray["price"] = $row["price"];
	
	if($hourly_packages_id == 1){
		$months = 3;
	}else if($hourly_packages_id == 2){
		$months = 6;
	}else if($hourly_packages_id == 3){
		$months = 12;
	}
	//$days_in_a_month = utility::getTableFieldById('keycode','code','name','days_in_a_month');
	//$hours_in_a_day = utility::getTableFieldById('keycode','code','name','hours_in_a_day');
	//$total_hours = $months * $days_in_a_month * $hours_in_a_day;
	$total_hours = $row["total_consultancy_hours"];
	$returnArray["total_hours"] = $total_hours;
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='send_mail'){
	include('send_mail_modal.php');
	die;
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='mail_send'){
	$job_id = $_REQUEST['job_id'];
	$admin_id = utility::getTableFieldById("job_admin_mapping","admin_id","job_id",$job_id);
	$mail_format_subject = addslashes($_REQUEST['mail_format_subject']);
	$mail_format_body =  str_replace("Â","",$_REQUEST['mail_format_body']);
	$mail_format_body = addslashes($mail_format_body);
	$email_id = utility::getTableFieldById("job","email","id",$job_id);
	$email_id_test = "parvindar1995@gmail.com";
	
	$email_date = date("Y-m-d");
	$sql = "INSERT INTO email_track(job_id,email_subject,email_body,email_id,email_date) VALUES('$job_id','$mail_format_subject','$mail_format_body','$email_id','$email_date')";
	mysqli_query($conn,$sql);
	$email_track_id = mysqli_insert_id($conn);
	/***************************** File Upload *************************************************/
	$digits = 5;
	
	$file_path 		= array();
	foreach($_FILES['mail_document'] as $file_pro => $file_value){
		foreach($_FILES['mail_document'][$file_pro] as $k => $v){
			if(!(isset($v) && $v !="")){
				continue;
			}
			${$file_pro}[$k] = $v;
		}
    }
	foreach($name as $key => $file_name){
		$my_path = $_SERVER['DOCUMENT_ROOT']."/{$other_fields["project_folder_name"]}admin/document_upload/";
		
		$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
		$uploaddir = '../../document_upload/';
		$document_name_array = explode(".",$file_name);
		$document_name = $document_name_array[0]."_".$random_number.".".$document_name_array[1];
		$uploadfile = $uploaddir . $document_name;
		array_push($file_path,$my_path.$document_name);
		move_uploaded_file($tmp_name[$key], $uploadfile);
		$sql= "INSERT into mail_document_upload(job_id,email_track_id,document_name) VALUES('$job_id','$email_track_id','$document_name')";
		mysqli_query($conn,$sql);
	}
	/***************************** End File Upload *************************************************/	
	$mail_format_subject = $_REQUEST['mail_format_subject'];
	$mail_format_body = $_REQUEST['mail_format_body'];
	//echo $functions->HTMLemailWithoutSignatureBlock($mail_format_body,$admin_id);
	//die;
	//////////////////////// send mail ////////////////////
	$site_email = $config->siteEmail;
	if(utility::mailSMTP_new($email_id, $mail_format_subject, $functions->HTMLemailWithoutSignatureBlock($mail_format_body,$admin_id), $file_path,$site_email,$site_email,"","","",$admin_id)){
		utility::mailSMTP_new($email_id_test, $mail_format_subject, $functions->HTMLemailWithoutSignatureBlock($mail_format_body,$admin_id), $file_path,$site_email,$site_email,"","","",$admin_id);
		echo "Mail send successfully";
	}else{
		echo "There is some error.";
	}
	die;
	///////////////// End send mail  //////////////////////
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='timer_time'){
	//date_default_timezone_set('Europe/London');
	$job_id = $_REQUEST['job_id'];
	$button = $_REQUEST['button'];
	if($button == "start"){
		$_SESSION["timer_running"] = 1;
		$sql = "SELECT * FROM timer WHERE job_id = $job_id ";
		$rs = mysqli_query($conn,$sql);
		if(mysqli_num_rows($rs) > 0){
			$row = mysqli_fetch_assoc($rs);
			$is_running = $row["is_running"];
			$changed_on = date('Y-m-d H:i:s');
			if($is_running == 0){
				mysqli_query($conn,"UPDATE timer SET is_running = '1',changed_on = '$changed_on' WHERE job_id = $job_id ");
				$t_time = $row['timer_time'];
				$t_time_array = explode(":",$t_time);
				$t_time_hours = $t_time_array[0];
				$t_time_mins = $t_time_array[1];
				$t_time_secs = $t_time_array[2];
				$returnArray = array();
				$returnArray['t_time_hours'] = ltrim($t_time_hours,'0'); 
				$returnArray['t_time_mins'] = ltrim($t_time_mins,'0');
				$returnArray['t_time_secs'] = ltrim($t_time_secs,'0'); 
				echo json_encode($returnArray);
				die;
			}
		}else{
			$changed_on = date('Y-m-d H:i:s');
			mysqli_query($conn,"INSERT into timer(job_id,is_start,is_running,timer_time,changed_on) VALUES('$job_id','1','1','00:00:00','$changed_on')");
		}
		die;
	}else if($button == "pause"){
		$changed_on = date('Y-m-d H:i:s');
		$timer_value = $_REQUEST['timer_value'];
		mysqli_query($conn,"UPDATE timer SET timer_time = '$timer_value',is_running = '0',changed_on = '$changed_on' WHERE job_id = $job_id ");
	}else if($button == "page_reload"){
		$timer_value = $_REQUEST['timer_value'];
		$sql = "SELECT * FROM timer WHERE job_id = $job_id AND is_start = '1' ";
		$rs = mysqli_query($conn,$sql);
		if(mysqli_num_rows($rs) > 0){
			$row = mysqli_fetch_assoc($rs);
			$is_running = $row["is_running"];
			if($is_running == 1){
				$changed_on = $row["changed_on"];
				$seconds  = strtotime(date('Y-m-d H:i:s')) - strtotime($changed_on);
				$timer_time = $gsi->getTimerTimeInSecondByJobId($job_id);
				$seconds  = $seconds + $timer_time;
	 
				$hours = floor($seconds / 3600);
				$mins = floor(($seconds - ($hours*3600)) / 60);
				$secs = floor($seconds % 60);
				$timer_value_new = $hours.":".$mins.":".$secs;
				$changed_on = date('Y-m-d H:i:s');
				mysqli_query($conn,"UPDATE timer SET timer_time = '$timer_value_new',changed_on = '$changed_on' WHERE job_id = $job_id ");
				$t_time = utility::getTableFieldById("timer","timer_time","job_id",$job_id);
				$t_time_array = explode(":",$t_time);
				$t_time_hours = $t_time_array[0];
				$t_time_mins = $t_time_array[1];
				$t_time_secs = $t_time_array[2];
				$returnArray = array();
				$returnArray['timer_value_new'] = $t_time; 
				$returnArray['t_time_hours'] = $hours; 
				$returnArray['t_time_mins'] = $mins; 
				$returnArray['t_time_secs'] = $secs; 
				echo json_encode($returnArray);
				die;
			}else{
				$returnArray = array();
				$t_time = utility::getTableFieldById("timer","timer_time","job_id",$job_id);
				$t_time_array = explode(":",$t_time);
				$t_time_hours = $t_time_array[0];
				$t_time_mins = $t_time_array[1];
				$t_time_secs = $t_time_array[2];
				$returnArray = array();
				$returnArray['timer_value_new'] = $t_time; 
				$returnArray['t_time_hours'] = ltrim($t_time_hours,'0'); 
				$returnArray['t_time_mins'] = ltrim($t_time_mins,'0');
				$returnArray['t_time_secs'] = ltrim($t_time_secs,'0'); 
				$returnArray['pause'] = 1; 
				echo json_encode($returnArray);
				die;
			}
		}
	}else if($button == "clear"){
		unset($_SESSION["timer_running"]);
		$timer_value = $_REQUEST['timer_value'];
		mysqli_query($conn,"UPDATE timer SET timer_time = '$timer_value',is_start = '0' WHERE job_id = $job_id ");
		$timer_time_in_second = $gsi->getTimerTimeInSecondByJobId($job_id);
		$timer_time_in_minute = $timer_time_in_second / 60;
		if($timer_time_in_minute <= 15){
			$timesheet_time = "00:15:00";
		}else if($timer_time_in_minute > 15 && $timer_time_in_minute <= 30){
			$timesheet_time = "00:30:00";
		}else if($timer_time_in_minute > 30 && $timer_time_in_minute <= 45){
			$timesheet_time = "00:45:00";
		}else if($timer_time_in_minute > 45 && $timer_time_in_minute <= 60){
			$timesheet_time = "00:60:00";
		}else if($timer_time_in_minute > 60 && $timer_time_in_minute <= 75){
			$timesheet_time = "01:15:00";
		}else if($timer_time_in_minute > 75 && $timer_time_in_minute <= 90){
			$timesheet_time = "01:30:00";
		}else{
			$timesheet_time = $timer_value;
		}
		///////////////////////// Timesheet Enrty /////////////////////////////////////
		$source =  $gsi->getEnqQuoteJobStatusById($job_id);	
		$admin_id = $_SESSION['user_id'];
		$timesheet_text = "Timesheet entry";
		//$entry_for_month = $gsi->getEntryForMonthByJobId($job_id);
		$timesheet_date	 = date("Y-m-d");
		//$timesheet_time = $timer_value;
		$created_on = date("Y-m-d");
		$sql= "INSERT INTO timesheet (job_id,source,admin_id,timesheet_text,timesheet_date,timesheet_time,updated_by,created_on) VALUES('$job_id','$source','$admin_id','$timesheet_text','$timesheet_date','$timesheet_time','".$_SESSION['user_id']."','$created_on')";
		mysqli_query($conn,$sql);
		///////////////////////// End Timesheet Enrty /////////////////////////////////////
		mysqli_query($conn,"DELETE FROM timer WHERE job_id = $job_id ");
	}
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_address_by_org_id'){
	$dataArray = array();
	$dataArray["status"] = 0;
	$org_id = $_REQUEST['org_id'];
	$sql = "SELECT * FROM organisation_management WHERE id = '$org_id' ";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$row = mysqli_fetch_assoc($rs);
		$row=utility::validate_crypt_data($row);
		
		$dataArray["address1"] 		= $row['address1'];
		$dataArray["address2"] 		= $row['address2'];
		$dataArray["address3"] 		= $row['address3'];
		$dataArray["city"] 			= $row['city'];
		$dataArray["county"] 		= $row['county'];
		$dataArray["postcode"] 		= $row['postcode'];
		$dataArray["phone_no"] 		= $row['phone_no'];
		$dataArray["landline_phone_no"] = $row['landline_phone_no'];
		$dataArray["status"]		= 1;
	}
	echo json_encode($dataArray);

}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='validate_client_email'){
	$email = utility::app_crypt($_REQUEST['email'],'e');
	$id = $_REQUEST['id'];
	$where = "";
	if(isset($id) && $id !=""){
		$where = " AND id != '$id' ";
	}
	$returnArray = array();
	$sql = "SELECT * FROM job WHERE email = '$email' $where";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$row = mysqli_fetch_assoc($rs);
		$returnArray['status'] = "failure";
	}else{
		$returnArray['status'] = "success";
	}
	echo json_encode($returnArray);	
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='validate_enquiry_email'){
	$email = utility::app_crypt($_REQUEST['email'],'e');
	$id = $_REQUEST['id'];
	$where = "";
	if(isset($id) && $id !=""){
		$where = " AND id != '$id' ";
	}
	$returnArray = array();
	$sql = "SELECT * FROM job WHERE email = '$email' $where";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$row = mysqli_fetch_assoc($rs);
		$returnArray['status'] = "failure";
	}else{
		$returnArray['status'] = "success";
	}
	echo json_encode($returnArray);	
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='count_no_of_appointment'){
	$returnArray = array();
	$returnArray["status"] = 0;
	$job_id = $_REQUEST['job_id'];
	$sql = "SELECT count(id) as total_appointment FROM appointment WHERE job_id = '$job_id' ";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$row = mysqli_fetch_assoc($rs);
		$returnArray["total_appointment"] = $row['total_appointment'];
		$returnArray["status"] = "success";
	}
	echo json_encode($returnArray);

}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='count_no_of_notes'){
	$returnArray = array();
	$returnArray["status"] = 0;
	$job_id = $_REQUEST['job_id'];
	$sql = "SELECT count(id) as total_notes FROM notes WHERE note_type = 0 AND job_id = '$job_id' ";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$row = mysqli_fetch_assoc($rs);
		$returnArray["total_notes"] = $row['total_notes'];
		$returnArray["status"] = "success";
	}
	echo json_encode($returnArray);

}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='count_no_of_reminder'){
	$returnArray = array();
	$returnArray["status"] = 0;
	$job_id = $_REQUEST['job_id'];
	$sql = "SELECT count(id) as total_reminder FROM notes WHERE note_type = 1 AND job_id = '$job_id' ";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$row = mysqli_fetch_assoc($rs);
		$returnArray["total_reminder"] = $row['total_reminder'];
		$returnArray["status"] = "success";
	}
	echo json_encode($returnArray);

}
die;
?>