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

$table_name = $_SESSION['db_prefix']."_time_sheet";
$delete_query = "DELETE FROM $table_name WHERE attendance_date = '".$attendance_date."' "; 
execsql($delete_query);

$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_min1 = mysql_result($time_zone_result, 0, "preset_value");
$lunch_period1= 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_min2 = mysql_result($time_zone_result, 0, "preset_value");
$lunch_period2 = mysql_result($time_zone_result, 0, "preset_value");


$error_message='';
$i=0;
while ($i<$_SESSION['list_no']) {
$line = $_POST['line'.$i];	
$emp_id = $_POST['emp_id'.$i];
$emp_no = $_POST['emp_no'.$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';




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

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


if ($status=='Not clocked') {

if ($time_zone==1) {
		$lunch_min = 0;
		$lunch_period = 0;	

if ($check_in_min!=00){
	$check_in_min=00;
	$check_in='00:00:00';
	
	}
}

if ($time_zone==2) {	
		$lunch_min = 0;
		$lunch_period = 0;

if ($check_in_min!=0){
	$check_in_min=00;
	$check_in='00:00:00';
	
	}

if ($check_out_min!=1050)
	{$check_out_min=1050;
	$check_out='17:30:00';
	}
}
	

$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET emp_id = '".$emp_id."', attendance_status = 'Not clocked', check_in = '".$check_in."', check_in_min = '".$check_in_min."', lunch_min = '".$lunch_min."', check_out = '".$check_out."', check_out_min = '".$check_out_min."', lunch_period = '".$lunch_period."' WHERE time_sheet_id = '".$time_sheet_id."' ";
execsql($update_query);
}

if ($status=='Present') {
		$lunch_min = $lunch_min1;
		$lunch_period = $lunch_period1;
	
	
if ($time_zone==1) {	
if ($check_out_min==0)
	{$check_out_min=1020;
	$check_out='17:00:00';
	
	}
}

if ($time_zone==2) {
		$lunch_min = $lunch_min2;
		$lunch_period = $lunch_period2;
		
if ($check_out_min==0)
	{$check_out_min=1050;
	$check_out='17:30:00';
	
	}
}	
	

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

$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET emp_id = '".$emp_id."', attendance_status = 'Present', check_in = '".$check_in."', check_in_min = '".$check_in_min."', lunch_min = '".$lunch_min."', check_out = '".$check_out."', check_out_min = '".$check_out_min."', lunch_period = '".$lunch_period."' WHERE time_sheet_id = '".$time_sheet_id."' ";
execsql($update_query);
}


if ($status=='Leave') {
	
if ($time_zone==1) {
		$lunch_min = $lunch_min1;
		$lunch_period = $lunch_period1;
		
if ($check_in_min!=420){
	$check_in_min=420;
	$check_in='07:00:00';
	
	}

if ($check_out_min!=1020){
	$check_out_min=1020;
	$check_out='17:00:00';
	}
}

if ($time_zone==2) {
		$lunch_min = $lunch_min2;
		$lunch_period = $lunch_period2;
			
if ($check_in_min!=450){
	$check_in_min=450;
	$check_in='07:30:00';
	}

if ($check_out_min!=1050)
	{$check_out_min=1050;
	$check_out='17:30:00';
	}
}
	
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;}
}

	
$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET emp_id = '".$emp_id."', attendance_status = 'Leave', check_in = '".$check_in."', check_in_min = '".$check_in_min."', lunch_min = '".$lunch_min."', check_out = '".$check_out."', check_out_min = '".$check_out_min."', lunch_period = '".$lunch_period."' WHERE time_sheet_id = '".$time_sheet_id."' ";
execsql($update_query);

}

if ($status=='Sick') {
		$lunch_min = $lunch_min1;
		$lunch_period = $lunch_period1;
	
if ($time_zone==1) {	
if ($check_in_min!=420){
	$check_in_min=420;
	$check_in='07:00:00';
	}

if ($check_out_min!=1020){
	$check_out_min=1020;
	$check_out='17:00:00';
	}
}

if ($time_zone==2) {
		$lunch_min = $lunch_min2;
		$lunch_period = $lunch_period2;
			
if ($check_in_min!=450){
	$check_in_min=450;
	$check_in='07:30:00';
	execsql($report_query);
}

if ($check_out_min!=1050)
	{$check_out_min=1050;
	$check_out='17:30:00';
	}
}

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;}
}
	
$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET emp_id = '".$emp_id."', attendance_status = 'Sick', check_in = '".$check_in."', check_in_min = '".$check_in_min."', lunch_min = '".$lunch_min."', check_out = '".$check_out."', check_out_min = '".$check_out_min."', lunch_period = '".$lunch_period."' WHERE time_sheet_id = '".$time_sheet_id."' ";
execsql($update_query);


}

if ($status=='Late') {

if ($time_zone==1) {
		$lunch_min = $lunch_min1;
		$lunch_period = $lunch_period1;
			
if ($check_out_min==0)
	{$check_out_min=1020;
	$check_out='17:00:00';
	}
}

if ($time_zone==2) {
		$lunch_min = $lunch_min2;
		$lunch_period = $lunch_period2;
			
if ($check_out_min==0)
	{$check_out_min=1050;
	}
}

if ($time_zone==1) {
if (($check_out_min>1020) && ($check_out_min>0) ) {$check_out_min=1020;}
}

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


$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET emp_id = '".$emp_id."', attendance_status = 'Late', check_in = '".$check_in."', check_in_min = '".$check_in_min."', lunch_min = '".$lunch_min."', check_out = '".$check_out."', check_out_min = '".$check_out_min."', lunch_period = '".$lunch_period."' WHERE time_sheet_id = '".$time_sheet_id."' ";
execsql($update_query);


}

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

if ($time_zone==1) {
		$lunch_min = $lunch_min1;
		$lunch_period = $lunch_period1;
		
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) {
		$lunch_min = $lunch_min2;
		$lunch_period = $lunch_period2;
		
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;}
}	
	
	
$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET emp_id = '".$emp_id."', attendance_status = 'On site', check_in = '".$check_in."', check_in_min = '".$check_in_min."', lunch_min = '".$lunch_min."', check_out = '".$check_out."', check_out_min = '".$check_out_min."', lunch_period = '".$lunch_period."', WHERE time_sheet_id = '".$time_sheet_id."' ";
execsql($update_query);
}

if ($status=='Absent') {
$lunch_min = 0;
$lunch_period = 0;	
$check_in_min = 0;
$check_out_min = 0;	
	
$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET emp_id = '".$emp_id."', attendance_status = 'Absent', check_in = '".$check_in."', check_in_min = '".$check_in_min."', lunch_min = '".$lunch_min."', check_out = '".$check_out."', check_out_min = '".$check_out_min."', lunch_period = '".$lunch_period."' WHERE time_sheet_id = '".$time_sheet_id."' ";
execsql($update_query);







}

if ($status_out=='Early') {

$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET emp_id = '".$emp_id."', status_out = 'Early', check_out = '".$check_out."', check_out_min = '".$check_out_min."'  WHERE time_sheet_id = '".$time_sheet_id."' ";
execsql($update_query);
}

if ($status_out=='Not clocked') {

$table_name = $_SESSION['db_prefix']."_time_sheet_temp";
$update_query = "UPDATE $table_name SET emp_id = '".$emp_id."', status_out = 'Not clocked', check_out = '".$check_out."', check_out_min = '".$check_out_min."'  WHERE time_sheet_id = '".$time_sheet_id."' ";
execsql($update_query);
}

$i++;
}

	
 ?>
   <script language="javascript" type="text/javascript">
        window.location="recordTimesWizbase.php?p=4&s=&r=yes&error_message=<?php echo $error_message?>";
   </script>
