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;

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 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);



$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<420) && ($check_in_min>0) ) {$check_in_min=420;} 
if (($check_out_min>1020) && ($check_out_min>0) ) {$check_out_min=1020;}
}

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

if ($status=='Not clocked') {
if (isset($_POST['not_clocked_permission_checkbox'.$i])) {
$not_clocked_permission = 'yes';
}
else {
	$not_clocked_permission = 'no';
}
$not_clocked_reason = $_POST['not_clocked_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."', not_clocked_permission = '".$not_clocked_permission."', not_clocked_reason = '".$not_clocked_reason."' WHERE time_sheet_id = '".$time_sheet_id."' ";
execsql($update_query);
}

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."', not_clocked_permission='', not_clocked_reason='', absent_permission='', absent_reason='', late_permission='', late_reason='' WHERE time_sheet_id = '".$time_sheet_id."' ";
execsql($update_query);
}

if ($status=='Leave') {
$leave_reason = $_POST['leave_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."',  leave_reason='".$leave_reason."' WHERE time_sheet_id = '".$time_sheet_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' ) ";
//echo '<br>*****'.$leave_query;
//execsql($leave_query);

}

if ($status=='Sick') {
$sick_reason = $_POST['sick_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."',  sick_reason='".$sick_reason."' WHERE time_sheet_id = '".$time_sheet_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';
}
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."', not_clocked_permission='', not_clocked_reason='', absent_permission='', absent_reason='' WHERE time_sheet_id = '".$time_sheet_id."' ";
execsql($update_query);
}

if ($status=='On site clocked') {
$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 = '', not_clocked_permission='', not_clocked_reason='', absent_permission='', absent_reason='' WHERE time_sheet_id = '".$time_sheet_id."' ";
execsql($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 = '', not_clocked_permission='', not_clocked_reason='', absent_permission='".$absent_permission."', absent_reason='".$absent_reason."' WHERE time_sheet_id = '".$time_sheet_id."' ";
execsql($update_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']."', '35', '".$attendance_date."' ,'','','' )";
   
$trackingResult = mySQL_query($trackingQuery);	
	


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