<?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'];

$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 = $con->query($time_zone_query);
	$row = mysqli_fetch_assoc($time_zone_result);
	$inclock = $row['preset_value'];

	
	$table_name = $_SESSION['db_prefix']."_presets";
	$time_zone_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'clockout1' ";
	$time_zone_result = $con->query($time_zone_query);
	$row = mysqli_fetch_assoc($time_zone_result);
	$outclock = $row['preset_value'];
	
	$table_name = $_SESSION['db_prefix']."_presets";
	$lunch_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'lunch' ";
	$lunch_result = $con->query($lunch_query);
	$row = mysqli_fetch_assoc($lunch_result);
	$lunch_min = $row['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 = $con->query($time_zone_query);
	$row = mysqli_fetch_assoc($time_zone_result);
	$inclock = $row['preset_value'];
	
	$table_name = $_SESSION['db_prefix']."_presets";
	$time_zone_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'clockout2' ";
	$time_zone_result = $con->query($time_zone_query);
	$row = mysqli_fetch_assoc($time_zone_result);
	$outclock = $row['preset_value'];
	
	$table_name = $_SESSION['db_prefix']."_presets";
	$lunch_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'lunch' ";
	$lunch_result = $con->query($lunch_query);
	$row = mysqli_fetch_assoc($lunch_result);
	$lunch_min = $row['preset_value'];
	
	$lunch_period = $lunch_min;
}

	$in_hours =  floor($inclock/60); 
	$in_mins =   $inclock % 60;
	
	$out_hours =  floor($outclock/60); 
	$out_mins =   $outclock % 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['intime_hour_text'.$i];
		   $min	=  $_POST['intime_min_text'.$i];
		   $check_in = $hour.':'.$min.':00';;
		   $check_in_min = ($hour*60) + $min;
		   
		   	   
		   $late_reason = $_POST['late_reason_text'.$i];
		   $late_permission = $_POST['late_permission_text'.$i];
		   $absent_reason = '';
		   $absent_permission = '';
	}
	
	if ($status=='absent') {
		   $hour = '00';
		   $min	=  '00';
		   $lunch_min = '0';
		   $check_in = $hour.':'.$min.':00';
		   $check_in_min = 0;
		   $check_out = $hour.':'.$min.':00';
		   $check_out_min = ($hour*60) + $min;
		   $absent_reason = $_POST['absent_select'.$i];
		   if (isset($_POST['a-permission_checkbox'.$i]) ) { $absent_permission = 'yes'; } else  { $absent_permission = 'no' ;}
		   $late_reason = '';
		   $late_permission = '';
	}
	
	if (isset($_POST['out_time_checkbox'.$i]) ) {
		   $hour = $out_hours;
		   $min	=  $out_mins;
		   $check_out = $hour.':'.$min.':00';
		   $check_out_min = ($hour*60) + $min;
	}
	
	if (isset($_POST['early_checkbox'.$i]) ) {
		   $hour = $_POST['early_hour_select'.$i];
		   $min	=  $_POST['early_min_select'.$i];
		   $check_out = $hour.':'.$min.':00';;
		   $check_out_min = ($hour*60) + $min;
		   $early_reason = $_POST['early_select'.$i];
		   if (isset($_POST['e-permission_checkbox'.$i]) ) { $early_permission = 'yes'; } else  { $early_permission = 'no' ;}
		   $absent_reason = '';
		   $absent_permission = '';
		   $status_out = 'early';
	}
	else {
		   $early_reason = '';
		   $early_permission = '';
		   $status_out = 'ok';
	}
	
	if (isset($_POST['absent_checkbox_checkbox'.$i]) ) {
		   $hour = '00';
		   $min	=  '00';
		   $lunch_min = '0';
		   $check_out = $hour.':'.$min.':00';
		   $check_out_min = ($hour*60) + $min;
	}
	

$table_name = $_SESSION['db_prefix']."_time_sheet";
$update_time_query = "UPDATE $table_name SET emp_no = '".$emp_no."', attendance_status = '".$status."', check_in = '".$check_in."', check_in_min = '".$check_in_min."', lunch_min = '".$lunch_min."', rec_out = '1', check_out = '".$check_out."', check_out_min = '".$check_out_min."', absent_permission = '".$absent_permission."', absent_reason = '".$absent_reason."', late_permission = '".$late_permission."', late_reason = '".$late_reason."', early_permission = '".$early_permission."', early_reason = '".$early_reason."', status_out = '".$status_out."', time_zone = '".$time_zone."', time_zone_in = '".$inclock."', time_zone_out = '".$outclock."' WHERE emp_id = '".$emp_id."' AND attendance_date = '".$recording_date."' ";
$update_time_result = $con->query($update_time_query);


	
	$i++;
}

include('../include/late_alert.php');

$table_name = $_SESSION['db_prefix']."_tracking_hr";
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '30', '".$recording_date."' ,'','','' )";
$tracking_result = $con->query($tracking_query);

?>

<script language="javascript" type="text/javascript">
          window.location="period_attendance_list.php?date=";
</script>