<!--Notice: (c)2013 This page is part of the management program for Tlhomeso Timbers, Gaborone, 
and developed by Customized Software (Pty) Ltd, Gaborone, Botswana. 
Customized Software holds the copyright on the source code on this and all other pages in the 
management program. For more info contact Customized Software on email: info@bwcustomized.com-->

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


$emp_id = $_GET['emp_id'];
$date = $_SESSION['out_date'];
$status_out = $_GET['status_out'];

$table_name = $_SESSION['db_prefix']."_time_record";
$time_query = "SELECT clock_in FROM $table_name WHERE emp_id='".$emp_id."' AND date='".$date."' LIMIT 1 ";
//echo $time_query;

$time_result = mySQL_query($time_query);
$clock_in = mysql_result($time_result, 0, "clock_in");


//echo '<br>'.$emp_id;
//echo 'status:'.$status_out;
//echo '<br>clock in:'.$clock_in;


$table_name = $_SESSION['db_prefix']."_time_record";	

if ($status_out=="E") {
$time_keeping = "";
$hours = $_GET['out_hours_select'];
$min = $_GET['out_min_select'];
$reason2 = $_GET['reason_select'];
$clock_out = ($hours*60)+$min;
$e_min = 1020 - $clock_out;
$status_2 = 'E';
$w_min = $clock_out-$clock_in-60;

if ($clock_out<=780) {
		  $w_min = $clock_out - $clock_in - 15;
	}

if (($clock_out>780) AND ($clock_out<825) ){
	//$i=0;
//	while ($i<45) {
//	if ($clock_out==(780+$i)) { 
	    //  $w_min = ($clock_out - $clock_in  - $i);
		$w_min = 345;
	//}
//	$i++;
//	}
	}
	
	if ($clock_out>825) {
		  $w_min = $clock_out - $clock_in - 60;
	}

}

if ($status_out=="P") {
	$time_keeping = $_GET['time_keeping'];
	$clock_out = 1020;
	
	$w_min = $clock_out - $clock_in - 60;
	
	
	if (($clock_in>559) AND ($clock_in<615) ){
	$i=0;
	while ($i<15) {
	if ($clock_in==600+$i) { 
	      $w_min = ($clock_out - $clock_in - 60 + $i); 
	}
	$i++;
	}
	}
	
	
	if ($clock_in>=615) {
		  $w_min = $clock_out - $clock_in - 45;
	}
	$status_2 = 'T';
	
}
//echo '<br>w min:'.$w_min;

date_default_timezone_set('Africa/Khartoum');
$entry_time = date("Y-m-d H:i:s",time()-3600); 

//echo "<br>status: ".$status_2;


if ($status_out=="E") {
     $check_query = "UPDATE $table_name SET  reason2 = '".$reason2."', checked='2', status_2='".$status_2."',  clock_out = '".$clock_out."', w_min='".$w_min."',  e_min='".$e_min."', entry_time = '".$entry_time."', entry_by = '".$_SESSION['username']."' WHERE emp_id = '". $emp_id  ."' AND date = '". $_SESSION['out_date'] ."'";
}
else {


	$check_query = "UPDATE $table_name SET   clock_out = '".$clock_out."',  w_min='".$w_min."',  checked='2', status_2='".$status_2."',  entry_time = '".$entry_time."', entry_by = '".$_SESSION['username']."' WHERE emp_id = '". $emp_id  ."' AND date = '". $_SESSION['out_date'] ."'";
	
	

}
$check_result = mySQL_query($check_query);


//echo "<br>".$check_query;
if ($_SESSION['set'] == 0) {
	$location='timeOutWizbase.php?p=3&s=3';
}
if ($_SESSION['set'] == 1) {
	$location='add_timeOutWizbase.php?p=3&s=3';
}
?>

<script language="javascript" type="text/javascript">
        <!--
		 window.location="<?php echo $location?>";
        -->
       </script>