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

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

$recording_date = $_POST['recording_date'];
$line_num = $_POST['line_num'];

$Y=date('Y', strtotime($recording_date));
$m=date('m', strtotime($recording_date));


$i=0;
while ($i<$line_num) {
	$emp_id = $_POST['emp_id_text'.$i];
	$emp_no = $_POST['emp_no_text'.$i];
	$time_zone = $_POST['time_zone_text'.$i];
	$status = $_POST['status_text'.$i];
	
	if ($time_zone==1) {	
	$table_name = $_SESSION['db_prefix']."_presets";
	$time_zone_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'clockin1' ";
	$time_zone_result = mySQL_query($time_zone_query);
	$inclock = mysql_result($time_zone_result, 0, "preset_value");
	
	$table_name = $_SESSION['db_prefix']."_presets";
	$time_zone_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'clockout1' ";
	$time_zone_result = mySQL_query($time_zone_query);
	$outclock = mysql_result($time_zone_result, 0, "preset_value");
	
	$table_name = $_SESSION['db_prefix']."_presets";
	$lunch_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'lunch' ";
	$lunch_result = mySQL_query($lunch_query);
	$lunch_min = mysql_result($lunch_result, 0, "preset_value");
	
	$lunch_period = $lunch_min;
}

if ($time_zone==2) {	
	$table_name = $_SESSION['db_prefix']."_presets";
	$time_zone_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'clockin2' ";
	$time_zone_result = mySQL_query($time_zone_query);
	$inclock = mysql_result($time_zone_result, 0, "preset_value");
	
	$table_name = $_SESSION['db_prefix']."_presets";
	$time_zone_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'clockout2' ";
	$time_zone_result = mySQL_query($time_zone_query);
	$outclock = mysql_result($time_zone_result, 0, "preset_value");
	
	$table_name = $_SESSION['db_prefix']."_presets";
	$lunch_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'lunch' ";
	$lunch_result = mySQL_query($lunch_query);
	$lunch_min = mysql_result($lunch_result, 0, "preset_value");
	
	$lunch_period = $lunch_min;
	
}

	$in_hours =  floor($inclock/60); 
	$in_mins =   $inclock % 60;
			

	if ($status=='present') {
		   $hour = $in_hours;
		   $min	=  $in_mins;
		   $check_in = $hour.':'.$min.':00';
		   $check_in_min = ($hour*60) + $min;
		   $late_reason = '';
		   $late_permission = '';
		   $absent_reason = '';
		   $absent_permission = '';
	}
	
	if ($status=='late') {
		   $hour = $_POST['hour_select'.$i];
		   $min	=  $_POST['min_select'.$i];
		   $check_in = $hour.':'.$min.':00';;
		   $check_in_min = ($hour*60) + $min;
		   $late_reason = $_POST['late_select'.$i];
		   if (isset($_POST['l-permission_checkbox'.$i]) ) { $late_permission = 'yes'; } else  { $late_permission = 'no' ;}
		   $absent_reason = '';
		   $absent_permission = '';
	}
	
	if ($status=='absent') {
		   $hour = '00';
		   $min	=  '00';
		   $check_in = $hour.':'.$min.':00';
		   $check_in_min = 0;
		   $absent_reason = $_POST['absent_select'.$i];
		   if (isset($_POST['a-permission_checkbox'.$i]) ) { $absent_permission = 'yes'; } else  { $absent_permission = 'no' ;}
		   $late_reason = '';
		   $late_permission = '';
		   $lunch_min=0;
		   
	}
	

$table_name = $_SESSION['db_prefix']."_time_sheet";
$time_query = "INSERT INTO $table_name (emp_no, emp_id, attendance_date, attendance_status, rec_in, check_in, check_in_min, lunch_min, absent_permission, absent_reason, late_permission, late_reason, time_zone, time_zone_in, time_zone_out, lunch_period ) VALUES ('".$emp_no."', '".$emp_id."', '".$recording_date."', '".$status."', '1', '".$check_in."', '".$check_in_min."', '".$lunch_min."',  '".$absent_permission."', '".$absent_reason."', '".$late_permission."', '".$late_reason."', '".$time_zone."', '".$inclock."', '".$outclock."', '".$lunch_period."' ) ";
execsql($time_query);
	
$i++;
}

$table_name = $_SESSION['db_prefix']."_tracking_hr";
$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '29', '".$recording_date."' ,'','','' )";
$trackingResult = mySQL_query($trackingQuery);

?>

<script language="javascript" type="text/javascript">
       window.location="time_recording.php?m=<?php echo $m ?>&Y=<?php echo $Y ?>";
</script>