<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php
include ("../navigation/header.php");
$page_name = "HR-main";
include ("../navigation/access.php");
$current_page = "attendance_list.php";

$date = $_GET['date'];

if ($date==3) { 
	   $attendance_date = $_GET['recording_date'];
	   $start = $attendance_date;
	   $end = $attendance_date;
}

include('../include/period_select_date.php');

$emp_id = $_GET['emp_id'];


$table_name = $_SESSION['db_prefix']."_time_sheet";
if ($emp_id == '') {
$display_query = "SELECT * FROM $table_name WHERE attendance_date >= '".$start."' AND attendance_date <= '".$end."'";
}
else {
$display_query = "SELECT * FROM $table_name WHERE emp_id = '".$emp_id."' AND attendance_date >= '".$start."' AND attendance_date <= '".$end."'";
}

if ($date == 3) {
$display_query = "SELECT * FROM $table_name WHERE attendance_date = '".$attendance_date."' ";
}

$display_result = $con->query($display_query);
$display_num = mysqli_num_rows($display_result);

$table_name = $_SESSION['db_prefix']."_presets";
$lunch_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'lunch' ";
$lunch_result = $con->query($lunch_query);
$row = mysqli_fetch_assoc($lunch_result);
$lunch = $row['preset_value'];


$table_name2 = $_SESSION['db_prefix']."_employee";
$employee_query = "SELECT emp_first_name, emp_last_name, emp_group FROM $table_name2 WHERE emp_id ='".$emp_id."' ";
$employee_result = $con->query($employee_query);
$row = mysqli_fetch_assoc($employee_result);
$a0 = $row['emp_first_name'];
$a1 = $row['emp_last_name'];
$a42 = $row['emp_group'];

if ($a42=='E') {
	  $a42='No';
	  $a43='';
}
else {
	  $a42='Yes';
	  $a43='(Management)';
}

$table_name = $_SESSION['db_prefix']."_time_sheet";
$time_query = "SELECT SUM(check_out_min - check_in_min - lunch_min) AS recorded_time, SUM(time_zone_out - time_zone_in - lunch_period) AS required_time FROM $table_name WHERE emp_id ='".$emp_id."' AND attendance_date >= '".$start."' AND attendance_date <= '".$end."' ";
$time_result = $con->query($time_query);
$row = mysqli_fetch_assoc($time_result);
$recorded_time = $row['recorded_time'];
$required_time = $row['required_time'];

if ($required_time=='') {$required_time = 1;}
$attendance = ($recorded_time / $required_time) * 100;	
?>
<!--style sheets-->
<link href="../css/main.css" rel="stylesheet" type="text/css" />
<!--jscript-->
<script language="JavaScript" src="../jscript/go_to_url.js"  type="text/javascript"></script> 

<!--style sheets-->
<link href="../css/main.css" rel="stylesheet" type="text/css">  
<link rel="stylesheet" type="text/css" href="../css/scroll_table.css" />
<link rel="stylesheet" type="text/css" href="../css/tooltip.css" />
<link rel="stylesheet" type="text/css" href="../css/tab.css" />

<!--jscript-->
<script language="JavaScript" src="../jscript/tooltip.js"  type="text/javascript"></script> 

</head>


<body>
<?php


if ($permission_num==1) {
	include ("../navigation/hr_sidebar.php");
?>


<div class="main_frame">
   <div class="page_head_frame">
      <?php 
		$attendance_label = "label_attendance_green" ;
		if ($attendance<96)  { $attendance_label = "label_attendance_orange" ; }
        if ($attendance<92)  { $attendance_label = "label_attendance_red" ; }
		?>
       <div style="float:left;">
       <label class="label_page_head">Employee</label>&nbsp;&nbsp;<label class="label_page_head_2"><?php echo $a0." ".$a1 ?></label>
       </div>
       <div style="float:right; margin-right:20px;">
       <?php echo $a43 ?>&nbsp;&nbsp; attendance selected period: 
       <label class="<?php echo $attendance_label ?>"><?php echo number_format($attendance, 2, '.', ',')?> %</label>
       </div>
</div>
      
<div class="page_middle_frame">

<div class="tab_button_holder_frame">
      <a href="employee_details.php?emp_id=<?php echo $emp_id ?>"><div class="<?php if ($current_page =='employee_details.php') {echo 'tab_button_active'; } else { echo 'tab_button'; }?>"><div align="center">Details</div></div></a>
      <a href="attendance_list.php"><div class="<?php if ($current_page =='attendance_list.php') {echo 'tab_button_active'; } else { echo 'tab_button'; }?>"><div align="center">Attendance</div></div></a>
      <a href="employee_warnings_list.php?emp_id=<?php echo $emp_id ?>&date="><div class="<?php if ($current_page =='discipline_list.php') {echo 'tab_button_active'; } else { echo 'tab_button'; }?>"><div align="center">Discipline</div></div></a>
      <a href="issued_employee_list.php?emp_id=<?php echo $emp_id ?>&date="><div class="<?php if ($current_page =='issued_employee_list.php') {echo 'tab_button_active'; } else { echo 'tab_button'; }?>"><div align="center">Store issues</div></div></a>
     </div>
     
<div class="tab_middle_frame">    
  
<?php 
$page='attendance_list.php';
$reference_name='emp_id';
$reference=$emp_id;
$reference_name_2='';
$reference_2='';
include('../include/period_select_block.php');
?>
  

<div class="page_inside_frame">
<script type="text/javascript" language="javascript" src="../jscript/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#scroll_table').dataTable( {
					"sScrollY": "500px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 00, "desc" ], [ 2, "asc" ]],
					"aoColumns": [
								{"iDataSort": 8},
                              	{"bSortable": true},
                             	{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bVisible": false}
								 ]
				} );
			} );
		</script>
<table width="594" class="data_display" id="scroll_table" >
  <thead>
    <tr>
      <td width="53">Date</td>
      <td width="40">Emp no</td>
      <td width="144">Name</td>
      <td width="83">Status</td>
      <td width="35">Zone</td>
      <td width="46" align="center">Check in</td>
      <td width="46" align="center">Check out</td>
      <td width="58">Hours</td>
      <td width="31">&nbsp;</td>
    </tr>
  </thead>
    <?php 
	
	while($row = $display_result->fetch_assoc()) {
		$emp_no = $row["emp_no"];
		$attendance_date = $row["attendance_date"];
		$attendance_status = $row["attendance_status"];
		$status_out = $row["status_out"];
		$time_zone = $row["time_zone"];
		$check_in = $row["check_in"];
		$check_in_min = $row["check_in_min"];
		$check_out = $row["check_out"];
		$check_out_min = $row["check_out_min"];
		$late_permission = $row["late_permission"];
		$late_reason = $row["late_reason"];
		$early_permission = $row["early_permission"];
		$early_reason = $row["early_reason"];
		$absent_permission = $row["absent_permission"];
		$absent_reason = $row["absent_reason"];
		$intime = $row["time_zone_in"];
		$outtime = $row["time_zone_out"];
		
			$table_name2 = $_SESSION['db_prefix']."_employee";
			$employee_query = "SELECT emp_id, emp_first_name, emp_last_name, time_zone FROM $table_name2 WHERE emp_no ='".$emp_no."' ";
			$employee_result = $con->query($employee_query);
			$row = mysqli_fetch_assoc($employee_result);
			$employee_name = $row['emp_first_name'].' '.$row['emp_last_name'];
			$emp_id = $row['emp_id'];	
			
		
		if (($check_out_min>0) AND ($check_in_min>0)) {
		$attendance_time = $check_out_min - $check_in_min - $lunch;
		$attendance_hours =  floor($attendance_time/60); 
		$attendance_mins =   $attendance_time % 60; 
		if ($attendance_hours<10) { $attendance_hours = 0 . $attendance_hours; }
		if ($attendance_mins<10) { $attendance_mins = 0 . $attendance_mins; }
		$attendance_time_display = $attendance_hours.':'.$attendance_mins;
		}
		else {
			$attendance_time_display = '--';
		}
		
		$table_name = $_SESSION['db_prefix']."_time_sheet_report";
		$report_query = "SELECT report_line FROM $table_name WHERE emp_no = '".$emp_no."' ";
		$report_result = $con->query($report_query);
		$report_num = mysqli_num_rows($report_result);
		if ($report_num>0) {
			  $report = 'yes';
			  $line='Report:';
			  $i=0;
			  while($row = $report_result->fetch_assoc()) {
				$line = $row["report_line"];
			  }
		}
		else {
			$report = 'no';
			$line='';
			}
			
		$report_TT = $line;
		$employee_TT = $employee_name.'<br>Click for details';
		$late_TT="'Permission: ".$late_permission.".<br>Reason: ".$late_reason.".'";
		$early_TT="'Permission: ".$early_permission.".<br>Reason: ".$early_reason.".'";
		$absent_TT="'Permission: ".$absent_permission.".<br>Reason: ".$absent_reason.".'";
		
		//$annual_leave_TT="'Permission: Yes<br>Reason: Annual leave'";
		//$sick_TT="'Permission: Yes<br>Reason: Sick'";
		
		$check_in_display = '<font color=black>'.date("H:i", strtotime($check_in)) . '</font>';
		$check_out_display = '<font color=black>'.date("H:i", strtotime($check_out)) . '</font>';
		
		if ($attendance_status=='late') {
		       $check_in_display = '<a href=# onMouseover="ddrivetip('.$late_TT.', 175)"; onMouseout="hideddrivetip()"><font color=red>'.date("H:i", strtotime($check_in)) . '</font></a>';
		}
		
		
		
		if ($attendance_status=='absent') {
		       $check_in_display = '<a href=# onMouseover="ddrivetip('.$absent_TT.', 175)"; onMouseout="hideddrivetip()"><font color=blue>'.'---' . '</font></a>';
			   $check_out_display = '<a href=# onMouseover="ddrivetip('.$absent_TT.', 175)"; onMouseout="hideddrivetip()"><font color=blue>'.'---' . '</font></a>';
		}
		
		//if ($attendance_status=='Annual leave') {
//		       $check_in_display = '<a href=# onMouseover="ddrivetip('.$annual_leave_TT.', 175)"; onMouseout="hideddrivetip()"><font color=blue>'.date("H:i", strtotime($check_in)) . '</font></a>';
//			   $check_out_display = '<a href=# onMouseover="ddrivetip('.$annual_leave_TT.', 175)"; onMouseout="hideddrivetip()"><font color=blue>'.date("H:i", strtotime($check_out)) . '</font></a>';
//		}
		
		//if ($attendance_status=='Sick') {
//		    $check_in_display = '<a href=# onMouseover="ddrivetip('.$sick_TT.', 175)"; onMouseout="hideddrivetip()"><font color=purple>'.date("H:i", strtotime($check_in)) . '</font></a>';
//		    $check_out_display = '<a href=# onMouseover="ddrivetip('.$sick_TT.', 175)"; onMouseout="hideddrivetip()"><font color=purple>'.date("H:i", strtotime($check_out)) . '</font></a>';
//		}
		
   

		
	   
		
		if ($status_out=='early') {
		       $check_out_display = '<a href=# onMouseover="ddrivetip('.$early_TT.', 175)"; onMouseout="hideddrivetip()"><font color=red>'.date("H:i", strtotime($check_out)) . '</font></a>';
		}
		
		//if ($status_out=='Leave') {
//		       $check_out_display = '<a href=# onMouseover="ddrivetip('.$leave_TT.', 175)"; onMouseout="hideddrivetip()"><font color=blue>'.date("H:i", strtotime($check_out)) . '</font></a>';
//		}
//		
//		if ($status_out=='Sick') {
//		       $check_out_display = '<a href=# onMouseover="ddrivetip('.$sick_TT.', 175)"; onMouseout="hideddrivetip()"><font color=purple>'.date("H:i", strtotime($check_out)) . '</font></a>';
//		}

	?>	
		
	<tr>
      <td width="53"><?php echo date("d-m-Y", strtotime($attendance_date)) ?></td>
      <td width="40"><?php echo $emp_no ?></td>
      <td width="144"><a href="employee_details.php?emp_id=<?php echo $emp_id?> " onMouseover="ddrivetip('<?php echo $employee_TT?>', 150)"; onMouseout="hideddrivetip()"><?php echo $employee_name ?></a></td>
      <td width="83"><?php echo $attendance_status ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
      <td width="35" align="center"><?php echo $time_zone ?></td>
      <td width="46" align="center"><?php echo $check_in_display ?></td>
      <td width="46" align="center"><?php echo $check_out_display ?></td>
      <td width="58" align="center"><?php echo $attendance_time_display ?></td>
      
      <td width="31"><?php echo $attendance_date ?></td>
    </tr>	
		
		
	<?php	
	}
	?>
  </table>
</div>
</div>
</div>
</div>

</body>
<?php } else {
$_SESSION['denied_return'] = "hr_module/hr_main.php?date=";	
include ("../navigation/hr_sidebar.php");?>
	<body onload="accessdenied(); return false">
	</body>
<?php }
?>
<?php include ('../navigation/side_bar.php'); ?>
</html>