<?
if(isset($_REQUEST['op'])){ $op = $_REQUEST['op']; }
else { $op = "nothing"; }
if ($op == "in"){
$query = "INSERT INTO Clocks VALUES (NULL, ".$_SESSION['currentuser']['userid'].", NOW(), 0)";
mysql_query($query);
session_write_close();
header("Location: ".$GLOBALS['config']['baseurl']."/index.php?do=timeclock");
}
else if($op == "out")
{
$query = "INSERT INTO Clocks VALUES (NULL, ".$_SESSION['currentuser']['userid'].", NOW(), 1)";
mysql_query($query);
session_write_close();
header("Location: ".$GLOBALS['config']['baseurl']."/index.php?do=timeclock");
}
$pp_start_date = date("M d, Y", current_pp_start());
$pp_end_date = date("M d, Y", current_pp_start()+1209600);
function clocktype_to_string($type)
{
switch($type){
case 0:
return "in";
case 1:
return "out";
}
}
function total_pay_period_hours()
{
$pp_start = current_pp_start();
$pp_end = $pp_start + 1209600; // 1209600 = number of seconds in a fortnight
$query = "SELECT ClockId, UNIX_TIMESTAMP(Time) as 'Time', Type, UserId FROM Clocks WHERE UNIX_TIMESTAMP(Time) BETWEEN ".$pp_start." AND ".$pp_end." AND UserId = ".$_SESSION['currentuser']['userid']." ORDER BY Time ASC";
$result = mysql_query($query);
$array = mysql_fetch_assoc($result);
while($array['Type'] != 0)
{
$array = mysql_fetch_assoc($result);
}
$total_hours = 0;
$clockin = 0.0001;
$clockout = 0.0001;
while(true){
if($array['Type'] == 0 && $clockout != 0){
$clockin = $array['Time'];
$clockout = 0;
}else if($array['Type'] == 1 && $clockin != 0){
$clockout = $array['Time'];
$total_hours += (abs($clockout - $clockin)) / 3600;
$clockin = 0;
}
$array = mysql_fetch_assoc($result);
if ($array == null)
{
break;
}
}
return $total_hours;
}
function current_pp_start()
{
$first_pp_start = mktime(0, 0, 0, 9, 18, 2005);
for (
$this_pp_start = $first_pp_start;
0 < 1;
$this_pp_start += 1209600
) {
if($this_pp_start > time())
{
return $this_pp_start - 1209600;
}
}
}
function total_hours()
{
$query = "SELECT ClockId, UNIX_TIMESTAMP(Time) as 'Time', Type, UserId FROM Clocks WHERE UserId = ".$_SESSION['currentuser']['userid']." ORDER BY Time ASC";
$result = mysql_query($query);
$array = mysql_fetch_assoc($result);
while($array['Type'] != 0)
{
$array = mysql_fetch_assoc($result);
}
$total_hours = 0;
$clockin = 0.0001;
$clockout = 0.0001;
while(true){
if($array['Type'] == 0 && $clockout != 0){
$clockin = $array['Time'];
$clockout = 0;
}else if($array['Type'] == 1 && $clockin != 0){
$clockout = $array['Time'];
$total_hours += (abs($clockout - $clockin)) / 3600;
$clockin = 0;
}
$array = mysql_fetch_assoc($result);
if ($array == null)
{
break;
}
}
return $total_hours;
}
function elapsed_time($timestring)
{
//Get the time difference.
$x = split(",", date("Y,m,d,H,i,s", time() - strtotime($timestring)));
//Return an associative array. Subtract 01.01.1970 because
//time() and strtotime() return time values WRT the Unix Epoch.
return array("years" => $x[0] - 1970,
"months" => $x[1] - 1,
"days" => $x[2] - 1,
"hours" => $x[3],
"minutes" => $x[4],
"seconds" => $x[5]);
}
function pp_hours_one($id)
{
$pp_start = current_pp_start();
$pp_end = $pp_start + 1209600;
$olddate = "";
echo("<table>");
echo("<tr>");
echo("<td width='200'><u>Date</u></td><td width='100'><u>In</u></td><td width='100'><u>Out</u></td>");
echo("</tr>");
$query = "SELECT Time, UserId, Type, DATE_FORMAT(Time, '%W %M %D, %Y') as 'Date', DATE_FORMAT(Time, '%H:%i') as 'HMSTime' FROM Clocks WHERE UserId = ".$id." AND UNIX_TIMESTAMP(Time) BETWEEN ".$pp_start." AND ".$pp_end." ORDER BY Time ASC";
$result = mysql_query($query);
$first = true;
while ($array = mysql_fetch_assoc($result)){
$date = $array['Date'];
$type = $array['Type'];
$time = $array['HMSTime'];
if ($olddate != $date && $first != true){
echo("<tr><td colspan='3'> <hr></td></tr>");
}
if($type == 0) {
echo("<tr>");
if ($olddate == $date){
echo("<td></td>");
} else {
echo("<td>".$date."</td>");
}
echo("<td>".$time."</td>");
} else if($type == 1) {
echo("<td>".$time."</td>");
echo("</tr>");
}
$olddate=$date;
$first = false;
}
echo("</table>");
}
function pp_all_emp_hours()
{
$query = "SELECT UserId, FirstName, LastName from Users ORDER BY LastName";
$result1 = mysql_query($query);
echo("<table>");
echo("<tr>");
echo("<td><u>Last Name</u></td><td><u>First Name</u></td><td><u>TotalHoursWorked</u></td>");
echo("</tr>");
$pp_start = current_pp_start();
$pp_end = $pp_start + 1209600;
while($user_array = mysql_fetch_assoc($result1)){
$firstname = $user_array['FirstName'];
$lastname = $user_array['LastName'];
$userid = $user_array['UserId'];
$query = "SELECT ClockId, UNIX_TIMESTAMP(Time) as 'Time', Type, UserId FROM Clocks WHERE UNIX_TIMESTAMP(Time) BETWEEN ".$pp_start." AND ".$pp_end." AND UserId = ".$userid." ORDER BY Time ASC";
$result = mysql_query($query);
$array = mysql_fetch_assoc($result);
while($array['Type'] != 0)
{
$array = mysql_fetch_assoc($result);
}
$total_hours = 0;
$clockin = 0.0001;
$clockout = 0.0001;
while(true){
if($array['Type'] == 0 && $clockout != 0){
$clockin = $array['Time'];
$clockout = 0;
}else if($array['Type'] == 1 && $clockin != 0){
$clockout = $array['Time'];
$total_hours += (abs($clockout - $clockin)) / 3600;
$clockin = 0;
}
$array = mysql_fetch_assoc($result);
if ($array == null){
echo("<tr>");
printf("<td><a href='%s/index.php?do=supervisor&op=%s'>%s</a>, </td><td>%s </td><td>%.2f</td>",$GLOBALS['config']['baseurl'], $userid, $lastname, $firstname, $total_hours);
echo("</tr>");
break;
}
}
}
echo("</table>");
}
?>