<?php
include ("../include/session.php");
if (!$session->logged_in) {
	header("Location: ../../main.php");
} else {
	include ("../header.php");
//	include ("../include/db.php");


	$i = 0;
	while (date("l", mktime(0, 0, 0, date("m"), date("d") - $i, date("Y"))) != "Saturday") {
		$date = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - $i, date("Y")));
		$i++;
	}

	$q = "SELECT empId, empFName, empLName FROM employees WHERE isEmployed != 1";
	$resultEmp = execsql($q);
	$numEmp = mysql_num_rows($resultEmp);
	$j = 0;
	$empArray = array ();
	while ($j < $numEmp) {
		$empArray[mysql_result($resultEmp, $j, "empId")] = mysql_result($resultEmp, $j, "empFName") . " " . mysql_result($resultEmp, $j, "empLName");
		$j++;
	}

	//attendance % for month
	$q1 = "SELECT * FROM timekeeping WHERE date >= '$date' ORDER BY date DESC";
	$result = execsql($q1);
	$numAtt = mysql_num_rows($result);

	$q2 = "SELECT * FROM employees WHERE isEmployed != 1";
	$res = execsql($q2);
	$numEmp = mysql_num_rows($res);
	$workingDays = 0;
	$k = 0;
	while (date("Y-m-d", mktime(0, 0, 0, date("m"), 01 + $k, date("Y"))) < date("Y-m-d")) {
		if (date("l", mktime(0, 0, 0, date("m"), date("d") - $i, date("Y"))) != "Sunday" || date("l", mktime(0, 0, 0, date("m"), date("d") - $i, date("Y"))) != "Saturday") {
			$workingDays++;
			$k++;
		}else{
			$k++;
		}
	}


	//daily attendance
	$q1 = "SELECT * FROM timekeeping WHERE date > '$date' ORDER BY date DESC";
	$result = execsql($q1);
	$num = mysql_num_rows($result);
	$j = 0;
	if ($num > 0) {
?>
<div class="headerbox">Weekly attendance</div> <br /><br /><br />
	<div class="subheaderbox">
	<?php 
	$workingDays++;
	echo "Weekly Attendance: " . number_format((((($workingDays * $numEmp) - $numAtt) / ($workingDays * $numEmp)) * 100),2) . "%";?></div><br />
	<table id="test1" cellpadding="0" cellspacing="0" border="0" class="rowstyle-alt colstyle-alt no-arrow">
    <thead>
	<tr>
	<th class="sortable-date-dmy">Date</th>
	<th class="sortable-text">Name</th>
	<th class="sortable-text">Absent/Late</th>
	<th class="sortable-text">Permission</th>
	
	<th class="sortable-text">Hours</th>
	<th class="sortable-text">Details</th>
	</tr>
      </thead>
    <tbody>
	
	
	<?php

		while ($j < $num) {
			if(mysql_result($result, $j, "permission") > 0){
				$permission = "Yes";
			}else {$permission = "No";}
			
			echo "<tr><td>" . date("d-m-Y",strtotime(mysql_result($result, $j, "date"))) .  "</td><td><a href=\"searchAndDisplayE.php?id=". mysql_result($result, $j, "empId") ."\">" .$empArray[mysql_result($result, $j, "empId")] . "</a></td><td>" . mysql_result($result, $j, "absent") ."</td><td>". $permission . "</td><td>" . mysql_result($result, $j, "hours") . "</td>";
			$details = mysql_result($result, $j, "details");
			?>
	  <td><a href="editComment.php?id=<?php echo mysql_result($result, $j, "timeId")?>&type=timekeeping&empId=<?php echo mysql_result($result, $j, "empId")?>"><?php 
		if($details ==""){
        	echo "-";	
        }else{
        	echo $details;	
        }
			echo "<td></td></tr>";
			$j++;
		}
	}?>
	</tbody>
	</table><?php
	include("../footer.php");
}
?>