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

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

$start=$_GET['start'];
$end=$_GET['end'];


$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 attendance_date >= '".$start."' AND attendance_date <= '".$end."' ";
$time_result = mySQL_query($time_query);
$time_num = mysql_num_rows($time_result);
$recorded_time = mysql_result($time_result, 0, "recorded_time");
$required_time = mysql_result($time_result, 0, "required_time");
if ($required_time!=0) {
$attendance = ($recorded_time / $required_time) * 100;
} else {
	$attendance = 0;
}

$display_query = "SELECT COUNT(*), personnel_no, emp_id, 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 attendance_date >= '".$start."' AND attendance_date <= '".$end."' GROUP BY emp_id";
$display_result = mySQL_query($display_query);
$display_num = mysql_num_rows($display_result);

	
?>

<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/print.css" />
<?php
$report_name='Period attendance list';
include('../include/print_report_header.php');
?>
</head>


<body>

<script>
window.onload = function () {
  window.print();
  setTimeout(function(){window.close();}, 100);
}
</script>
   
<div class="print_standard_frame">  
<label class="label_std">Attendance hour list, period from <?php echo date("d-m-Y", strtotime($start)) ?> to <?php echo date("d-m-Y", strtotime($end)) ?></label>

<table width="683" id="print_tableprint"  >
  <thead>
    <tr>
      <th width="51">Emp no</th>
      <th width="204">Name</th>
      <th width="35">Zone</th>
      <th width="56" align="center">Days</th>
      <th width="56" align="center">Hours</th>
      <th width="72" align="center">Attendance</th>
      <th width="56" align="center">Overtime<br>
        150%</th>
      <th width="56" align="center">Overtime<br>
        200%</th>
    </tr>
  </thead>
    <?php 
	
	$p=0;
	while ($p<$display_num) {
		$emp_no = mysql_result($display_result, $p, "personnel_no");
			$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 = mySQL_query($employee_query);
			$employee_num = mysql_num_rows($employee_result);	
			$employee_name = mysql_result($employee_result, 0, "emp_first_name").' '.mysql_result($employee_result, 0, "emp_last_name"); 
			$emp_id = mysql_result($employee_result, 0, "emp_id");
			$time_zone = mysql_result($employee_result, 0, "time_zone");
		 $recorded_time = mysql_result($display_result, $p, "recorded_time");
		 $required_time = mysql_result($display_result, $p, "required_time");
		 $attendance = ($recorded_time / $required_time) * 100;
		 $no_of_days = mysql_result($display_result, $p, "count(*)");
		 
		 $worked_time= $recorded_time;
		 
		 $hours =  floor($worked_time/60); 
		 $mins =   $worked_time % 60; 
		   if ($hours<10) { $hours = 0 . $hours; }
		   if ($mins<10) { $mins = 0 . $mins; }
		 $worked_display_time = $hours . ":" . $mins ;
		 
		$employee_TT = $employee_name.'<br>Click for details';
		
		$table_name = $_SESSION['db_prefix']."_overtime_records";
		$overtime_query = "SELECT SUM(overtime_min_150) AS summed150, SUM(overtime_min_200) AS summed200 FROM $table_name WHERE overtime_date >= '".$start."' AND overtime_date <= '".$end."' AND emp_id = '".$emp_id."' ";
		$overtime_result = mySQL_query($overtime_query);
		$overtime_num = mysql_num_rows($overtime_result);
		$overtime_min_150 = mysql_result($overtime_result, 0, "summed150");
		$overtime_min_200 = mysql_result($overtime_result, 0, "summed200");
		
		$hours =  floor($overtime_min_150/60); 
		 $mins =   $overtime_min_150 % 60; 
		   if ($hours<10) { $hours = 0 . $hours; }
		   if ($mins<10) { $mins = 0 . $mins; }
		 $overtime_min_150_display = $hours . ":" . $mins ;
		 
		 $hours =  floor($overtime_min_200/60); 
		 $mins =   $overtime_min_200 % 60; 
		   if ($hours<10) { $hours = 0 . $hours; }
		   if ($mins<10) { $mins = 0 . $mins; }
		 $overtime_min_200_display = $hours . ":" . $mins ;
		
				
					
	?>	
		
	<tr>
      <td width="51"><?php echo $emp_no ?></td>
      <td width="204"><?php echo $employee_name ?></a></td>
      <td width="35" align="center"><?php echo $time_zone ?></td>
      <td width="56" align="center"><?php echo $no_of_days ?></td>
      <td width="56" align="center"><?php echo $worked_display_time ?></td>
      <td width="72" align="right"><?php echo number_format($attendance, 2, '.', ',') ?>%</td>
      <td width="56" align="right"><?php if ($overtime_min_150_display!=0) {echo $overtime_min_150_display;} ?></td>
      <td width="56" align="center"><?php if($overtime_min_200_display!=0) {echo $overtime_min_200_display;} ?></td>
    </tr>	
		
		
	<?php	
	$p++;
	}
	?>
  </table>
</div>



</div>

</body>

</html>