<?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'];
$week_day=$_POST['week_day'];


$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];
	$early_out = $_POST['early_out'.$i];
	
	$status_out='';
	$early_permission='';
	$early_reason='';
	
	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";
	$time_zone_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'lunch1' ";
	$time_zone_result = mySQL_query($time_zone_query);
	$lunch_min = mysql_result($time_zone_result, 0, "preset_value");
	$lunch_period = 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 = 'grace1' ";
	$time_zone_result = mySQL_query($time_zone_query);
	$grace_min = mysql_result($time_zone_result, 0, "preset_value");
	

}

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";
	$time_zone_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'lunch2' ";
	$time_zone_result = mySQL_query($time_zone_query);
	$lunch_min = mysql_result($time_zone_result, 0, "preset_value");
	
}

if ($week_day!='Saturday') {
	$in_hours =  floor($inclock/60); 
	$in_mins =   $inclock % 60;
	
	$out_hours =  floor($outclock/60); 
	$out_mins =   $outclock % 60;
			

	if ($status=='present') {
		   $in_hour = $in_hours;
		   $in_min	=  $in_mins;
		   $check_in = $in_hour.':'.$in_min.':00';
		   $check_in_min = ($in_hour*60) + $in_min;
		   
		   $out_hour = $out_hours;
		   $out_min	=  $out_mins;
		   $check_out = $out_hour.':'.$out_min.':00';
		   $check_out_min = ($out_hour*60) + $out_min;
		   
		   
		   
		   $late_reason = '';
		   $late_permission = '';
		   $absent_reason = '';
		   $absent_permission = '';
		   $leave_reason = '';
		   $sick_reason = '';
	}
	
	if ($status=='late') {
		
		   $in_hour = $_POST['hour_select'.$i];
		   $in_min	=  $_POST['min_select'.$i];
		   $check_in = $in_hour.':'.$in_min.':00';;
		   $check_in_min = ($in_hour*60) + $in_min;
		   
		  		      
		   
		   $out_hour = $out_hours;
		   $out_min	=  $out_mins;
		   $check_out = $out_hour.':'.$out_min.':00';
		   $check_out_min = ($out_hour*60) + $out_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 = '';
		   $leave_reason = '';
		   $sick_reason = '';
		   
		  
	}
	
	if ($status=='absent') {
		   $in_hour = '00';
		   $in_min	=  '00';
		   $check_in = $in_hour.':'.$in_min.':00';
		   $check_in_min = 0;
		   
		   $out_hour = '00';
		   $out_min	=  '00';
		   $check_out = $out_hour.':'.$out_min.':00';
		   $check_out_min = ($out_hour*60) + $out_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 = '';
		   $leave_reason = '';
		   $sick_reason = '';
		   $lunch_min=0;
		   
				   
	}
	
	if ($status=='leave') {
		   if (isset($_POST['a-leave_checkbox'.$i]) ) { $annual_leave = 'yes'; } else  { $annual_leave = 'no' ;}
		   if ($annual_leave=='yes') {
			   		$in_hour = $in_hours;
		   			$in_min	=  $in_mins;
		   			$check_in = $in_hour.':'.$in_min.':00';
		   			$check_in_min = ($in_hour*60) + $in_min;
		   
		   			$out_hour = $out_hours;
		   			$out_min	=  $out_mins;
		   			$check_out = $out_hour.':'.$out_min.':00';
		   			$check_out_min = ($out_hour*60) + $out_min;
					
					$leave_reason = 'annual';
					$status = 'annual leave';
					
					if (isset($_POST['leave-half_day_checkbox'.$i]) ) { 
						  $leave_reason = 'leave (half day)';
					      $day='0.5';
						  } else {
							  $status = 'annual leave';
							  $leave_reason = 'annual';
							  $day=1;
							  }
					
		   }
		   else {
			   	$in_hour = '00';
		   		$in_min	=  '00';
		   		$check_in = $in_hour.':'.$in_min.':00';
		  		$check_in_min = 0;
		   
		   		$out_hour = '00';
		   		$out_min	=  '00';
		   		$check_out = $out_hour.':'.$out_min.':00';
		   		$check_out_min = ($out_hour*60) + $out_min;
												
				$status = 'unpaid leave';
				$leave_reason = 'unpaid leave'; 
				$lunch_min=0;
		   		}
		   
		   
		   $late_reason = '';
		   $late_permission = '';
		   $absent_reason = '';
		   $absent_permission = '';
		   $sick_reason = ''; 	
	}
	
	if ($status=='sick') {
		  if (isset($_POST['p-sick_checkbox'.$i]) ) { $paid_sick = 'yes'; } else  { $paid_sick = 'no' ;}
		   if ($paid_sick=='yes') {
		   			$in_hour = $in_hours;
		   			$in_min	=  $in_mins;
		   			$check_in = $in_hour.':'.$in_min.':00';
		   			$check_in_min = ($in_hour*60) + $in_min;
		   
		   			$out_hour = $out_hours;
		   			$out_min	=  $out_mins;
		   			$check_out = $out_hour.':'.$out_min.':00';
		   			$check_out_min = ($out_hour*60) + $out_min;
					
					$status = 'paid sick';
					$sick_reason = 'paid';
					
										
					if (isset($_POST['sick-half_day_checkbox'.$i]) ) { 
						  $sick_reason = 'paid (half day)';
					      $day='0.5';
						  } else {
							  $status = 'paid sick';
							  $sick_reason = 'paid';
							  $day=1;
							  }
					
					$table_name = $_SESSION['db_prefix']."_sick_records";
					$sick_query = "SELECT balance FROM $table_name WHERE emp_id = '".$emp_id."' ORDER BY sick_id DESC ";
					$sick_result = mySQL_query($sick_query);
					$sick_num = mysql_num_rows($sick_result);
					if ($sick_num!=0) {
							$balance = mysql_result($sick_result, 0, "balance")-$day;
					}
					else {
						$balance = $day;
					}
				
					$table_name = $_SESSION['db_prefix']."_sick_records";
					$new_sick_query = "INSERT INTO $table_name (emp_id, rec_date, days_minus, balance, reason) VALUES ('".$emp_id."', '".$recording_date."', '".$day."', '".$balance."' , 'paid sick'  ) ";
					execsql($new_sick_query);
					
		   }
		   else {
			  	$in_hour = '00';
		   		$in_min	=  '00';
		   		$check_in = $in_hour.':'.$in_min.':00';
		  		$check_in_min = 0;
		   
		   		$out_hour = '00';
		   		$out_min	=  '00';
		   		$check_out = $out_hour.':'.$out_min.':00';
		   		$check_out_min = ($out_hour*60) + $out_min;
				
				$status = 'unpaid sick';
				$sick_reason = 'unpaid';
				$lunch_min=0;
		   }
		   
		   
		   $late_reason = '';
		   $late_permission = '';
		   $absent_reason = '';
		   $absent_permission = '';	
		   $leave_reason = '';
	}
	
	
if ($status=='public') {
		   if (isset($_POST['a-leave_checkbox'.$i]) ) {
			   		$in_hour = $in_hours;
		   			$in_min	=  $in_mins;
		   			$check_in = $in_hour.':'.$in_min.':00';
		   			$check_in_min = ($in_hour*60) + $in_min;
		   
		   			$out_hour = $out_hours;
		   			$out_min	=  $out_mins;
		   			$check_out = $out_hour.':'.$out_min.':00';
		   			$check_out_min = ($out_hour*60) + $out_min;
					
					$leave_reason = 'public';
					
		   }

		   $late_reason = '';
		   $late_permission = '';
		   $absent_reason = '';
		   $absent_permission = '';
		   $sick_reason = ''; 	
	}
	
if ($status=='unpaid public') {
		   if (isset($_POST['u-leave_checkbox'.$i]) ) {
			   		$in_hour = $in_hours;
		   			$in_min	=  $in_mins;
		   			$check_in = $in_hour.':'.$in_min.':00';
		   			$check_in_min = ($in_hour*60) + $in_min;
		   
		   			$out_hour = $out_hours;
		   			$out_min	=  $out_mins;
		   			$check_out = $out_hour.':'.$out_min.':00';
		   			$check_out_min = ($out_hour*60) + $out_min;
					
					$leave_reason = 'unpaid public';
					
		   }

		   $late_reason = '';
		   $late_permission = '';
		   $absent_reason = '';
		   $absent_permission = '';
		   $sick_reason = ''; 	
	}	
	

	

	if ($early_out=='yes') {
		   
		   $status_out = 'early';
		   
		   $out_hour = $_POST['early_hour_select'.$i];
		   $out_min	=  $_POST['early_min_select'.$i];
		   $check_out = $out_hour.':'.$out_min.':00';
		   $check_out_min = ($out_hour*60) + $out_min;
		   
		   $early_reason = $_POST['early_select'.$i];
		   if (isset($_POST['e-permission_checkbox'.$i]) ) { $early_permission = 'yes'; } else  { $early_permission = 'no' ;}

		   if ($early_permission == 'yes') {
					      $check_out_min=$outclock;
						  $lunch_min=$lunch_period;
					   }
					   
						   
		   
					   
//		   }
		   
	}
	

		
	//$lunch_min = $sat_lunch_min;
//	$inclock=$sat_inclock;
//	$outclock=$sat_outclock;
//	$lunch_period=$sat_lunch_period;
//	
}
	

$table_name = $_SESSION['db_prefix']."_time_sheet";
$time_query = "INSERT INTO $table_name (personnel_no, emp_id, attendance_date, attendance_status, rec_in, check_in, check_in_min, lunch_min, check_out, check_out_min, 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 ) VALUES ('".$emp_no."', '".$emp_id."', '".$recording_date."', '".$status."', '1', '".$check_in."', '".$check_in_min."', '".$lunch_min."', '".$check_out."', '".$check_out_min."', '".$absent_permission."', '".$absent_reason."', '".$leave_reason."', '".$sick_reason."','".$late_permission."', '".$late_reason."', '".$status_out."', '".$early_permission."', '".$early_reason."', '".$time_zone."', '".$inclock."', '".$outclock."', '".$lunch_period."') ";
execsql($time_query);
	
$i++;
}


$select_date1 = $recording_date;
$select_date2 = $recording_date;
include('daily_attendance_report_compile.php');	
include('../include/late_alert.php');
include('../include/absent_alert.php');
//include('../include/sick_alert.php');
include('../include/leave_alert.php');				

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

?>

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