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

$late_email_message = '';
$absent_email_message = '';
$leave_email_message = '';

$i=0;
while ($i<$_SESSION['list_no']) {
	
$emp_no = $_POST['emp_no_text'.$i];
$emp_id = $_POST['emp_id_text'.$i];
$status = $_POST['attendance_status_text'.$i];
$status_out = $_POST['status_out_text'.$i];


$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET not_clocked_permission = '', not_clocked_reason = '', late_permission = '', late_reason = '', absent_permission = '', absent_reason = '', early_permission = '', early_reason = '', leave_reason = '', sick_reason = '' WHERE emp_id = '".$emp_id."' ";
execsql($update_query);




if ($status=='Not clocked') {
	
if (isset($_POST['not_clocked_permission_checkbox'.$i])) {
$not_clocked_permission = 'yes';
$not_clocked_reason = $_POST['not_clocked_reason_select'.$i];
$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET  not_clocked_permission = '".$not_clocked_permission."', not_clocked_reason = '".$not_clocked_reason."' WHERE emp_id = '".$emp_id."' ";
execsql($update_query);
}
else {
$not_clocked_permission = 'no';
$not_clocked_reason = $_POST['not_clocked_reason_select'.$i];
$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET  not_clocked_permission = '".$not_clocked_permission."', not_clocked_reason = '".$not_clocked_reason."' WHERE emp_id = '".$emp_id."' ";
execsql($update_query);
}
}


if ($status=='Present') {

}


if ($status=='Leave') {

$leave_reason = $_POST['leave_reason_select'.$i];		
$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET  leave_reason = '".$leave_reason."' WHERE emp_id = '".$emp_id."' ";
execsql($update_query);

$table_name = $_SESSION['db_prefix']."_leave_records";
$leave_balance_query = "SELECT balance FROM $table_name WHERE emp_id = '".$emp_id."' ORDER BY rec_date DESC LIMIT 1  ";
$leave_balance_result = mySQL_query($leave_balance_query);
$leave_balance_num = mysql_num_rows($leave_balance_result);
if ($leave_balance_num!=0) {
$leave_balance = mysql_result($leave_balance_result, 0, "balance");
}
else {
	$leave_balance=0;
}
$new_balance = $leave_balance-1;

$table_name = $_SESSION['db_prefix']."_leave_records";
$leave_query = "INSERT INTO $table_name (emp_id, rec_date, days_minus, balance, reason ) VALUES ('".$emp_id."', '".$attendance_date."', '1', '".$new_balance."', 'leave day' ) ";
execsql($leave_query);

if ($new_balance<=0) {
include('../include/leave_alert.php'); 

$leave_email_message = $leave_email_message.'<tr> <td>'.$emp_name.'</td><td>&nbsp;</td><td>'.date("d-m-Y", strtotime($check_date)).'</td></tr>';		

}

}

if ($status=='Sick') {
	
$sick_reason = $_POST['sick_reason_select'.$i];		
$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET  sick_reason = '".$sick_reason."' WHERE emp_id = '".$emp_id."' ";
execsql($update_query);
	
$table_name = $_SESSION['db_prefix']."_sick_records";
$sick_balance_query = "SELECT balance FROM $table_name WHERE emp_id = '".$emp_id."' ORDER BY rec_date DESC LIMIT 1  ";
$sick_balance_result = mySQL_query($sick_balance_query);
$sick_balance_num = mysql_num_rows($sick_balance_result);
if ($sick_balance_num!=0) {
$sick_balance = mysql_result($sick_balance_result, 0, "balance");
}
else {
	$sick_balance=0;
}

$new_balance = $sick_balance-1;

$table_name = $_SESSION['db_prefix']."_sick_records";
$leave_query = "INSERT INTO $table_name (emp_id, rec_date, days_plus, balance, reason ) VALUES ('".$emp_id."', '".$attendance_date."', '1', '".$new_balance."', 'sick day' ) ";
execsql($leave_query);
}

if ($status=='Late') {

if (isset($_POST['late_permission_checkbox'.$i])) {
$late_permission = 'yes';
$late_reason = $_POST['late_reason_select'.$i];	
$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET  late_permission = '".$late_permission."', late_reason = '".$late_reason."' WHERE emp_id = '".$emp_id."' ";
execsql($update_query);
} 
else { 
$late_permission = 'no';
$late_reason = $_POST['late_reason_select'.$i];	
$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET  late_permission = '".$late_permission."', late_reason = '".$late_reason."' WHERE emp_id = '".$emp_id."' ";
execsql($update_query);
} 

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

if ($late_num>=3) {	
	    $late_email_message = $late_email_message.'<tr> <td>'.$emp_name.'</td><td>'.$late_num.'</td><td>'.date("d-m-Y", strtotime($check_date)).'</td></tr>';		
}
}

if ($status=='On site clocked') {

}

if ($status=='Absent') {
	
if (isset($_POST['absent_permission_checkbox'.$i])) {
$absent_permission = 'yes';
$absent_reason = $_POST['absent_reason_select'.$i];		
$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET absent_permission = '".$absent_permission."', absent_reason = '".$absent_reason."' WHERE emp_id = '".$emp_id."' ";
execsql($update_query);
}
else {
$absent_permission = 'no';
$absent_reason = $_POST['absent_reason_select'.$i];		
$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET absent_permission = '".$absent_permission."', absent_reason = '".$absent_reason."' WHERE emp_id = '".$emp_id."' ";
execsql($update_query);
}

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

if ($absent_num>=3) {	
	    $absent_email_message = $absent_email_message.'<tr> <td>'.$emp_name.'</td><td>'.$absent_num.'</td><td>'.date("d-m-Y", strtotime($check_date)).'</td></tr>';		
}
}


if ($status_out=='Early') {
if (isset($_POST['early_permission_checkbox'.$i])) {
$early_permission = 'yes';
$early_reason = $_POST['early_reason_select'.$i];		
$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET early_permission = '".$early_permission."', early_reason = '".$early_reason."' WHERE emp_id = '".$emp_id."' ";
execsql($update_query);
}
else {
$early_permission = 'no';
$early_reason = $_POST['early_reason_select'.$i];		
$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET early_permission = '".$early_permission."', early_reason = '".$early_reason."' WHERE emp_id = '".$emp_id."' ";
execsql($update_query);
}
}


if ($status_out=='Not clocked') {
$not_clocked_out_permission = $_POST['not_clocked_permission_group'.$i];
$not_clocked_out_reason = $_POST['not_clocked_reason_select'.$i];	
$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET not_clocked_out_permission = '".$not_clocked_out_permission."', not_clocked_out_reason = '".$not_clocked_out_reason."'  WHERE emp_id = '".$$emp_id."' ";
execsql($update_query);
}

$i++;
}


if ($late_email_message!='') {
		 // include('../include/late_email_alert.php');
	}

if ($absent_email_message!='') {
		//  include('../include/absent_email_alert.php');
	}
	
if ($leave_email_message!='') {
		//  include('../include/leave_email_alert.php');
	}


if (isset($_POST['prev_button']) )
	   {
		 $action = 'recordTimesWizbase.php?p=3&s=';
	   }
		 
	if (isset($_POST['finish_button']) )
	  {
		$table_name1 = $_SESSION['db_prefix']."_time_sheet_temp";
		$table_name2 = $_SESSION['db_prefix']."_time_sheet";
		$copy_query = "INSERT INTO $table_name2 (personnel_no, emp_id, attendance_date, attendance_status, check_in, check_in_min, lunch_min, check_lunch_out, check_lunch_out_min, check_lunch_in, check_lunch_in_min, check_out, check_out_min, not_clocked_permission, not_clocked_reason, not_clocked_out_permission, not_clocked_out_reason, absent_permission, absent_reason, leave_reason, sick_reason, late_permission, late_reason, status_out, early_permission, early_reason, time_zone, time_zone_in, time_zone_out, lunch_period) SELECT personnel_no, emp_id, attendance_date, attendance_status, check_in, check_in_min, lunch_min, check_lunch_out, check_lunch_out_min, check_lunch_in, check_lunch_in_min, check_out, check_out_min, not_clocked_permission, not_clocked_reason, not_clocked_out_permission, not_clocked_out_reason, absent_permission, absent_reason, leave_reason, sick_reason, late_permission, late_reason, status_out, early_permission, early_reason, time_zone, time_zone_in, time_zone_out, lunch_period FROM $table_name1";
		echo '<br>*****'.$copy_query;
		execsql($copy_query);
		
		
$select_date1 = $attendance_date;
$select_date2 = $attendance_date;	
//include('daily_attendance_report_compile.php');			
	
	
		$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']."', '30', '".				$attendance_date."' ,'','','' )";
   
   		$trackingResult = mySQL_query($trackingQuery);	
   
	
		$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
		$delete_query = "DELETE FROM $table_name "; 
		execsql($delete_query);  
		  
		$action = 'attendance_list.php?date=3&day_date='.$attendance_date.'&emp_id=';
	  }

?>

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

