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

date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time());
$attendance_date = $_POST['attendance_date']; 
$list_no = $_POST['list_no_text'];
$i=0;

$table_name = $_SESSION['db_prefix']."_presets";
$clockin1_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'clockin1' ";
$clockin1_result = $con->query($clockin1_query);
$row = mysqli_fetch_assoc($clockin1_result);
$clockin1 = $row["preset_value"];


$table_name = $_SESSION['db_prefix']."_presets";
$clockout1_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'clockout1' ";
$clockout1_result = $con->query($clockout1_query);
$row = mysqli_fetch_assoc($clockout1_result);
$clockout1 = $row["preset_value"];


$table_name = $_SESSION['db_prefix']."_presets";
$clockin2_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'clockin2' ";
$clockin2_result = $con->query($clockin2_query);
$row = mysqli_fetch_assoc($clockin2_result);
$clockin2 = $row["preset_value"];


$table_name = $_SESSION['db_prefix']."_presets";
$clockout2_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'clockout2' ";
$clockout2_result = $con->query($clockout2_query);
$row = mysqli_fetch_assoc($clockout2_result);
$clockout2 = $row["preset_value"];


while ($i<$list_no) {
$emp_id = $_POST['emp_id'.$i];
$time_zone = $_POST['time_zone'.$i];
$time_sheet_id = $_POST['time_sheet_id'.$i];	
$intime_hour = $_POST['intime_hour_select'.$i];
if ($intime_hour=='') {$intime_hour='00';}
$intime_min = $_POST['intime_min_select'.$i];
if ($intime_min=='') {$intime_min='00';}
$check_in = $intime_hour.':'.$intime_min.':00';
$outtime_hour = $_POST['outtime_hour_select'.$i];
if ($outtime_hour=='') {$outtime_hour='00';}
$outtime_min = $_POST['outtime_min_select'.$i];
if ($outtime_min=='') {$outtime_min='00';}
$check_out = $outtime_hour.':'.$outtime_min.':00';


$table_name = $_SESSION['db_prefix']."_time_sheet";
$update_query = "UPDATE $table_name SET late_permission = '', late_reason = '', absent_permission = '', absent_reason = '', early_permission = '', early_reason = ''  WHERE time_sheet_id = '".$time_sheet_id."' ";
$update_result = $con->query($update_query);

$status = $_POST['status_select'.$i];

$check_in_min = ($intime_hour*60) + $intime_min;
$check_out_min = ($outtime_hour*60) + $outtime_min;

if ($time_zone==1) {
if (($check_in_min<$clockin1) && ($check_in_min>0) ) {$check_in_min=$clockin1;} 
if (($check_out_min>$clockout1) && ($check_out_min>0) ) {$check_out_min=$clockout1;}
}

if ($time_zone==2) {
if (($check_in_min<$clockin2) && ($check_in_min>0) ) {$check_in_min=$clockin2;} 
if (($check_out_min>$clockout2) && ($check_out_min>0) ) {$check_out_min=$clockout2;}
}


if ($status=='present') {
$table_name = $_SESSION['db_prefix']."_time_sheet";
$update_query = "UPDATE $table_name SET emp_id = '".$emp_id."', attendance_status = '".$status."', check_in = '".$check_in."', check_in_min = '".$check_in_min."', check_out = '".$check_out."', check_out_min = '".$check_out_min."', absent_permission='', absent_reason='', late_permission='', late_reason='' WHERE time_sheet_id = '".$time_sheet_id."' ";
$update_result = $con->query($update_query);
}


if ($status=='late') {
if (isset($_POST['late_permission_checkbox'.$i])) {
$late_permission = 'yes';
}
else {
	$late_permission = 'no';
}
$late_reason = $_POST['late_reason_select'.$i];	
$table_name = $_SESSION['db_prefix']."_time_sheet";
$update_query = "UPDATE $table_name SET emp_id = '".$emp_id."', attendance_status = '".$status."', check_in = '".$check_in."', check_in_min = '".$check_in_min."', check_out = '".$check_out."', check_out_min = '".$check_out_min."', late_permission = '".$late_permission."', late_reason = '".$late_reason."', absent_permission='', absent_reason='' WHERE time_sheet_id = '".$time_sheet_id."' ";
$update_result = $con->query($update_query);
}


if ($status=='absent') {
if (isset($_POST['absent_permission_checkbox'.$i])) {
$absent_permission = 'yes';
}
else {
	$absent_permission = 'no';
}
$absent_reason = $_POST['absent_reason_select'.$i];
$table_name = $_SESSION['db_prefix']."_time_sheet";
$update_query = "UPDATE $table_name SET emp_id = '".$emp_id."', attendance_status = '".$status."', check_in = '".$check_in."', check_in_min = '".$check_in_min."', check_out = '".$check_out."', check_out_min = '".$check_out_min."', late_permission = '', late_reason = '', absent_permission='".$absent_permission."', absent_reason='".$absent_reason."' WHERE time_sheet_id = '".$time_sheet_id."' ";
$update_result = $con->query($update_query);
}

$i++;
}


$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']."', '35', '".$attendance_date."' ,'','','' )";
$tracking_result = $con->query($tracking_query);
	


if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	 	  window.location="../hr_module/hr_main.php?date=0";
		</script>
        <?php		 
	   }
	   
	
	   ?>