<?php
include ('../include/session.php');

$email = $_GET['email_text'];
$access_id = $_GET['access_id'];
$user_name = $_GET['user_name'];

$table_name = $_SESSION['db_prefix']."_users";
$email_query = "UPDATE $table_name SET email = '".$email."' WHERE access_id = '".$access_id."' ";
execsql($email_query);


//==================Start HR
if ((isset($_GET['hr_attendance_report_checkbox'])) OR (isset($_GET['hr_attendance_email_checkbox']))) {
	
	if (isset($_GET['hr_attendance_report_checkbox'])) { $report = 'yes';} else { $report = 'no'; }
	if (isset($_GET['hr_attendance_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	
	
	$table_name = $_SESSION['db_prefix']."_reports";
	$check_query = "SELECT report_name  FROM $table_name WHERE report_name = 'daily attendance' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_reports";
	$report_query = "INSERT INTO $table_name (report_name, send_report, send_email, user_name ) VALUES ('daily attendance',  '".$report."', '".$email."', '". $user_name."' )";
	execsql($report_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_reports";
		$report_query = "UPDATE $table_name SET send_report = '".$report."', send_email = '".$email."' WHERE report_name = 'daily attendance' AND user_name = '".$user_name."' ";
		execsql($report_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_reports";
		$delete_query = "DELETE FROM $table_name WHERE report_name = 'daily attendance' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}


//==================Start Sales
if ((isset($_GET['daily_sales_report_checkbox'])) OR (isset($_GET['daily_sales_email_checkbox']))) {
	
	if (isset($_GET['daily_sales_report_checkbox'])) { $report = 'yes';} else { $report = 'no'; }
	if (isset($_GET['daily_sales_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	
	
	$table_name = $_SESSION['db_prefix']."_reports";
	$check_query = "SELECT report_name  FROM $table_name WHERE report_name = 'daily sales' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_reports";
	$report_query = "INSERT INTO $table_name (report_name, send_report, send_email, user_name ) VALUES ('daily sales',  '".$report."', '".$email."', '". $user_name."' )";
	execsql($report_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_reports";
		$report_query = "UPDATE $table_name SET send_report = '".$report."', send_email = '".$email."' WHERE report_name = 'daily sales' AND user_name = '".$user_name."' ";
		execsql($report_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_reports";
		$delete_query = "DELETE FROM $table_name WHERE report_name = 'daily sales' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}

if ((isset($_GET['quote_follow_up_report_checkbox'])) OR (isset($_GET['quote_follow_up_email_checkbox']))) {
	
	if (isset($_GET['quote_follow_up_report_checkbox'])) { $report = 'yes';} else { $report = 'no'; }
	if (isset($_GET['quote_follow_up_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	
	
	$table_name = $_SESSION['db_prefix']."_reports";
	$check_query = "SELECT report_name  FROM $table_name WHERE report_name = 'daily follow up' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_reports";
	$report_query = "INSERT INTO $table_name (report_name, send_report, send_email, user_name ) VALUES ('daily follow up',  '".$report."', '".$email."', '". $user_name."' )";
	execsql($report_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_reports";
		$report_query = "UPDATE $table_name SET send_report = '".$report."', send_email = '".$email."' WHERE report_name = 'daily follow up' AND user_name = '".$user_name."' ";
		execsql($report_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_reports";
		$delete_query = "DELETE FROM $table_name WHERE report_name = 'daily follow up' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}	
	
	
if ((isset($_GET['quote_notes_report_checkbox'])) OR (isset($_GET['quote_notes_email_checkbox']))) {
	
	if (isset($_GET['quote_notes_report_checkbox'])) { $report = 'yes';} else { $report = 'no'; }
	if (isset($_GET['quote_notes_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	
	
	$table_name = $_SESSION['db_prefix']."_reports";
	$check_query = "SELECT report_name  FROM $table_name WHERE report_name = 'daily quote notes' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_reports";
	$report_query = "INSERT INTO $table_name (report_name, send_report, send_email, user_name ) VALUES ('daily quote notes',  '".$report."', '".$email."', '". $user_name."' )";
	execsql($report_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_reports";
		$report_query = "UPDATE $table_name SET send_report = '".$report."', send_email = '".$email."' WHERE report_name = 'daily quote notes' AND user_name = '".$user_name."' ";
		execsql($report_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_reports";
		$delete_query = "DELETE FROM $table_name WHERE report_name = 'daily quote notes' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}

//==================Stores
if ((isset($_GET['min_stock_report_checkbox'])) OR (isset($_GET['min_stock_email_checkbox']))) {
	
	if (isset($_GET['min_stock_report_checkbox'])) { $report = 'yes';} else { $report = 'no'; }
	if (isset($_GET['min_stock_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	
	
	$table_name = $_SESSION['db_prefix']."_reports";
	$check_query = "SELECT report_name  FROM $table_name WHERE report_name = 'min stock' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_reports";
	$report_query = "INSERT INTO $table_name (report_name, send_report, send_email, user_name ) VALUES ('min stock',  '".$report."', '".$email."', '". $user_name."' )";
	execsql($report_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_reports";
		$report_query = "UPDATE $table_name SET send_report = '".$report."', send_email = '".$email."' WHERE report_name = 'min stock' AND user_name = '".$user_name."' ";
		execsql($report_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_reports";
		$delete_query = "DELETE FROM $table_name WHERE report_name = 'min stock' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}


//==================Start transport
if ((isset($_GET['transport_license_report_checkbox'])) OR (isset($_GET['transport_license_email_checkbox']))) {
	
	if (isset($_GET['transport_license_report_checkbox'])) { $report = 'yes';} else { $report = 'no'; }
	if (isset($_GET['transport_license_email_checkbox'])) { $email = 'yes';} else { $email = 'no'; }
	
	
	$table_name = $_SESSION['db_prefix']."_reports";
	$check_query = "SELECT report_name  FROM $table_name WHERE report_name = 'license expiry' AND user_name = '".$user_name."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	
	if($check_num==0) {
			
	$table_name = $_SESSION['db_prefix']."_reports";
	$report_query = "INSERT INTO $table_name (report_name, send_report, send_email, user_name ) VALUES ('license expiry',  '".$report."', '".$email."', '". $user_name."' )";
	execsql($report_query);
	
	}
	else {
		$table_name = $_SESSION['db_prefix']."_reports";
		$report_query = "UPDATE $table_name SET send_report = '".$report."', send_email = '".$email."' WHERE report_name = 'license expiry' AND user_name = '".$user_name."' ";
		execsql($report_query);
	}
}
	else {
		$table_name = $_SESSION['db_prefix']."_reports";
		$delete_query = "DELETE FROM $table_name WHERE report_name = 'license expiry' AND user_name = '".$user_name."' ";
		execsql($delete_query);
	}


	if (isset($_GET['save_close_button']) )
	   { ?>
       <script>
	  window.location="user_list.php";
		</script>
        <?php		 
	   }
	          
       if (isset($_GET['save_button']) )
	   {  ?>
       <script>
	     window.location="reports.php?set=1&user_select=<?php echo $access_id?>";
	   </script>
        <?php		 
	   }
	   
?>
