<?php
include('../common/db_connection.php');
$Mainclass=new DB_Controller();
$Mainclass->Employee_COOKIE_Check();
$changeable_worktime=12;/*Only in Hour*/
if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === "off") {
    $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: ' . $location);
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<head>
<title>Attendance</title>


<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="Admindek Bootstrap admin template made using Bootstrap 4 and it has huge amount of ready made feature, UI components, pages which completely fulfills any dashboard needs." />
<meta name="keywords" content="bootstrap, bootstrap admin template, admin theme, admin dashboard, dashboard template, admin template, responsive" />
<meta name="author" content="colorlib" />

 <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800" rel="stylesheet"><link href="https://fonts.googleapis.com/css?family=Quicksand:500,700" rel="stylesheet">

<link rel="stylesheet" type="text/css" href="files/bower_components/bootstrap/css/bootstrap.min.css">

<link rel="stylesheet" href="files/assets/pages/waves/css/waves.min.css" type="text/css" media="all">

<link rel="stylesheet" type="text/css" href="files/assets/icon/feather/css/feather.css">

<link rel="stylesheet" type="text/css" href="files/assets/icon/themify-icons/themify-icons.css">

<link rel="stylesheet" type="text/css" href="files/assets/icon/icofont/css/icofont.css">

<link rel="stylesheet" type="text/css" href="files/assets/icon/font-awesome/css/font-awesome.min.css">

<link rel="stylesheet" type="text/css" href="files/assets/css/style.css">
<link rel="stylesheet" type="text/css" href="files/assets/css/pages.css">

<script language="javascript">
//==========for clock ===============//
function startTime()
{
 
  var date=new Date();
  var p=date.toLocaleTimeString();
  //alert(p);
  document.getElementById("clock").innerHTML=p;
  setTimeout(function(){startTime()},500);
}
//==========for clock ===============//
</script>

<script>

function getLocation() 
{
  if (navigator.geolocation) 
  {
    var optn = {
			enableHighAccuracy : true,
			timeout : Infinity,
			maximumAge : 0
		};
	navigator.geolocation.getCurrentPosition(showPosition, showError, optn);
  } 
  else 
  {
		alert('Geolocation is not supported in your browser');
  }
  
}

function showPosition(position) 
{
  var latloc=position.coords.latitude;  
  var longloc=position.coords.longitude;
  $("#latloc").val(latloc);
  $("#longloc").val(longloc);
}

function showError(error) {
	switch(error.code) {
		case error.PERMISSION_DENIED:
			alert("User denied the request for Geolocation.");
			break;
		case error.POSITION_UNAVAILABLE:
			alert("Location information is unavailable.");
			break;
		case error.TIMEOUT:
			alert("The request to get user location timed out.");
			break;
		case error.UNKNOWN_ERROR:
			alert("An unknown error occurred.");
			break;
	}
}
</script>




 <style type="text/css">
#tooltip
{
text-align: center;
color: #fff;
background: #111;
position: absolute;
z-index: 100;
padding: 5px 10px;
font-size:10px !important;
line-height:12px !important;
}

#tooltip:after /* triangle decoration */
{
width: 0;
height: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-top: 10px solid #111;
content: '';
position: absolute;
left: 50%;
bottom: -10px;
margin-left: -10px;
}

#tooltip.top:after
{
border-top-color: transparent;
border-bottom: 10px solid #111;
top: -20px;
bottom: auto;
}

#tooltip.left:after
{
left: 10px;
margin: 0;
}

#tooltip.right:after
{
right: 10px;
left: auto;
margin: 0;
}

.message_maindiv{
float:left;
height:auto;
padding:15px;
background-color:#fff8e6;
text-align:left;
font-size:12px;
color:#503a27;
}

.time_border{
 border-right:1px solid #263544;
}


@media only screen and (max-width:480px){
.time_border{
 border-right:0px solid #0066FF;
}
}



</style>
        
<script>
function isNumberKey(evt) 
{
	var charCode = (evt.which) ? evt.which : event.keyCode; 
	if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) 
	{
		return false;
	} 
	else 
	{
		return true;
	} 
} 

</script> 
       
 <script>
function page_submit()
{
	var pan=$("#pan").val();
	
	var Error=0;
	if(pan=='')
	{
		$("#pan").css('border-bottom','1px solid red');
		Error=1;
	}
	if(pan!='')
	{
		$("#pan").css('border-bottom','1px solid #ccc');
	}
	if(Error==1)
	{
		return false;
	}
	if(Error==0)
	{
		$("#main").submit();
	}
}
</script>   
</head>
<body onLoad="getLocation()">



<div id="pcoded" class="pcoded">
<div class="pcoded-overlay-box"></div>
<div class="pcoded-container navbar-wrapper">

<?php include('register-top-header.php'); ?>





<div class="pcoded-main-container">
<div class="pcoded-wrapper">

<?php include('register-side-menu.php'); ?>



<div class="pcoded-content">

<div class="page-header card">
<div class="row align-items-end">
<div class="col-lg-8">
<div class="page-header-title">

<div class="d-inline">
<h5 style="text-transform:uppercase; font-size:25px; font-weight:600;">Attendance In/Out</h5>

</div>
</div>
</div>

</div>
</div>

<div class="pcoded-inner-content">
<div class="main-body">
<div class="page-wrapper">

<div class="page-body">
<div class="row">
<div class="col-sm-12">

<div class="card">

<div class="card-block">
<?php
$fetch_attendance=$Mainclass->Fetch_needed_datas("attendance_master","*","Emp_Temp_Id","'".$_COOKIE['Payroll_Emp_Attendance']."' order by SLN desc limit 1");

$actual_in_time=$fetch_attendance['In_Time'];
$roster_in_time=$fetch_attendance['Attendance_Date']." ".$fetch_attendance['Roster_In_Time'].":00";
$Late_Count=0;
$in_time="";
$msg="";
$roster_check=$Mainclass->Num_rows_Check("roster_plan_master","Emp_Temp_Id","'".$_COOKIE['Payroll_Emp_Attendance']."' and Roster_Year='".date('Y')."' and Roster_Month='".date('m')."'");
if($roster_check>0)
{
	$roster_fetch=$Mainclass->Fetch_needed_datas("roster_plan_master",date('d')."_Day as today_date,Working_Hour","Emp_Temp_Id","'".$_COOKIE['Payroll_Emp_Attendance']."' and Roster_Year='".date('Y')."' and Roster_Month='".date('m')."'");
	$in_time=$roster_fetch['today_date'];
}
if($in_time!="OFF" && $in_time!="LEAVE")
{
if(strtotime(date('Y-m-d H:i:s'))>strtotime(date('Y-m-d ').$in_time.":00"))
{
	$Late_Count=1;
}
}
/* check and update existing entry without out */
if(strtotime(date('Y-m-d H:i:s'))>strtotime('+'.$changeable_worktime.' hour',strtotime($fetch_attendance['In_Time'])) && $fetch_attendance['Out_Time']=="0000-00-00 00:00:00")
{
	$w_hour_for=$Mainclass->Fetch_needed_datas("working_hours_master","Working_Hours","Emp_Id ","'".$_COOKIE['Payroll_Emp_Attendance']."'");
	$w_hour=$w_hour_for['Working_Hours'];
	$hour_minutes=explode(":",$w_hour);
	$added_work_time=date("Y-m-d H:i:s",strtotime('+'.$hour_minutes[0].' hour +'.$hour_minutes[1].' minutes',strtotime($fetch_attendance['In_Time'])));

$Mainclass->Update_Data("attendance_master","Out_Time='".$added_work_time."',Out_With='Auto',Working_hour='".$w_hour."'","SLN='".$fetch_attendance['SLN']."'");
	
}

if(isset($_POST['AttendanceIn']))
{
	$check_rows=$Mainclass->Num_rows_Check("attendance_master","Emp_Temp_Id","'".$_COOKIE['Payroll_Emp_Attendance']."' order by SLN desc limit 1");
	
	
	/*if($_POST['latloc']!="" && $_POST['longloc']!="")
	{*/
		
			
			if(strtotime(date('Y-m-d H:i:s'))>strtotime('+'.$changeable_worktime.' hour',strtotime($fetch_attendance['In_Time'])))
			{
				if($in_time!="LEAVE" && $in_time!="OFF")
				{
			        $Mainclass->Insert_Data("attendance_master","Emp_Temp_Id,Attendance_Date,In_Time,Lat_Location,Long_Location,Late_Count,Roster_In_Time,Attendence_From","'".$_COOKIE['Payroll_Emp_Attendance']."','".date('Y-m-d')."','".date('Y-m-d H:i:s')."','".$_POST['latloc']."','".$_POST['longloc']."','".$Late_Count."','".$in_time."','".$_POST['place_att']."'");
			?>
			<script>
			window.location.href="attendance.php";
			</script>
			<?php
				}
				else
				{
				    if($in_time=="LEAVE")
				    {
					$msg="Today is your leave day";
				    }
				    if($in_time=="OFF")
				    {
					$msg="Today is your off day";
				    }
				    if($in_time=="")
				    {
				?>
				<script>
				alert("Today is your off day");
				</script>
				<?php
				    }
				}
					
			}
			else
			{
				$msg=1;
				?>
			<script>
			alert("You have already given your attendance !!");
			</script>
			<?php
			}
		
	 /*}*/
	
}

if(isset($_POST['AttendanceOut']))
{
	$dateDifference= intval((strtotime(date("Y-m-d H:i:s"))-strtotime($actual_in_time))/60);
	
	$hours_difference=intval($dateDifference/60);	
	$minutes_difference=$dateDifference%60;
	if($hours_difference<10)
	{
		$hours_difference='0'.$hours_difference;
	}
	if($minutes_difference<10)
	{
		$minutes_difference='0'.$minutes_difference;
	}
	
	$Mainclass->Update_Data("attendance_master","Out_Time='".date('Y-m-d H:i:s')."',Out_With='Manual',Working_hour='".$hours_difference.":".$minutes_difference."',Out_Lat_Location='".$_POST['latloc']."',Out_Long_Location='".$_POST['longloc']."'","SLN='".$fetch_attendance['SLN']."'");
	?>
		<script>
		window.location.href="attendance.php";
		</script>
		<?php
}
?>
<form id="main12" method="post">
<input type="hidden" id="latloc" name="latloc">
<input type="hidden" id="longloc" name="longloc">
<div class="row">
<div class="col-sm-6 time_border" style="text-align:center; padding:60px 0px;">

<h2 style="font-weight:800;"><?php echo date('dS M, Y');?></h2> <br>

 <?php if($in_time!='LEAVE' && $in_time!='OFF'  && $in_time!=''){ ?><span style="font-size:18px; color:#006600; margin-top:-10px; display:block;">Roster Time : <?php echo date('h:i a',strtotime($in_time));?></span><br><?php }?>


 <span id="clock" style="font-size:18px; color:#000000; margin-top:-10px; display:block; line-height:30px;"></span>

</div>
<?php
$attendance_rows=$Mainclass->Num_rows_Check("attendance_master","Emp_Temp_Id","'".$_COOKIE['Payroll_Emp_Attendance']."' and Out_Time='0000-00-00 00:00:00' order by SLN desc limit 1");

?>
<div class="col-sm-6" style="text-align:center; padding:80px 0px 50px 0px;">

<?php
if($attendance_rows==0)
{ 
?>
<select name="place_att" id="place_att" class="form-control fill" required>
<option value="">Select Place</option>
<option value="Home">Work from Home</option>
<option value="Office">Office</option>
<option value="Field">Field</option>
</select></br>
 <input type="submit" id="AttendanceIn" name="AttendanceIn" value="Attendance In" class="btn btn-success btn-round waves-effect waves-light" style="padding:10px 50px; font-weight:500; font-size:18px;"><br/>
<span Style="color:#FF0000;"><?php echo $msg; ?></span>
 <?php
}
$date1 = strtotime($fetch_attendance['In_Time']);  

$date2 = strtotime(date('Y-m-d H:i:s'));  

 

// Formulate the Difference between two dates

$diff = abs($date2 - $date1);  

 

 

// To get the year divide the resultant date into

// total seconds in a year (365*60*60*24)

$years = floor($diff / (365*60*60*24));  

 

 

// To get the month, subtract it with years and

// divide the resultant date into

// total seconds in a month (30*60*60*24)

$months = floor(($diff - $years * 365*60*60*24)

                              / (30*60*60*24));  

 

 

// To get the day, subtract it with years and  

// months and divide the resultant date into

// total seconds in a days (60*60*24)

$days = floor(($diff - $years * 365*60*60*24 -  

            $months*30*60*60*24)/ (60*60*24));

 

 

// To get the hour, subtract it with years,  

// months & seconds and divide the resultant

// date into total seconds in a hours (60*60)

$hours = floor(($diff - $years * 365*60*60*24  

      - $months*30*60*60*24 - $days*60*60*24)

                                  / (60*60));  

 

 

// To get the minutes, subtract it with years,

// months, seconds and hours and divide the  

// resultant date into total seconds i.e. 60

$minutes = floor(($diff - $years * 365*60*60*24  

        - $months*30*60*60*24 - $days*60*60*24  

                         - $hours*60*60)/ 60);  

 

 

// To get the minutes, subtract it with years,

// months, seconds, hours and minutes  

$seconds = floor(($diff - $years * 365*60*60*24  

        - $months*30*60*60*24 - $days*60*60*24

               - $hours*60*60 - $minutes*60));

if($attendance_rows>0 && $fetch_attendance['Out_Time']=="0000-00-00 00:00:00" && $diff>=$date1)
{ 
?>
  <input type="submit" id="AttendanceOut" name="AttendanceOut" value="Attendance Out" class="btn btn-danger btn-round waves-effect waves-light" style="padding:10px 50px; font-weight:500; font-size:18px;">
<?php
}
?>
 </div>
 
  

</div>
<div class="row" style="margin-top:20px;">
<?php
if($attendance_rows>0)
{ 
	if($fetch_attendance['Out_Time']=="0000-00-00 00:00:00")
	{
	?>
	<div class="col-sm-12"><div class="alert alert-success background-success" style="font-weight:600; text-align:center; text-transform:uppercase;" > 
									Your attendance time - in for <?php echo date('d M, Y',strtotime($fetch_attendance['In_Time']));?> is <?php echo date('h:i:s a',strtotime($fetch_attendance['In_Time']));?> !!
									</div></div>
	<?php
	}
	/* if($fetch_attendance['Out_Time']!="0000-00-00 00:00:00")
	{
	?>
	<div class="col-sm-12"><div class="alert alert-success background-success" style="font-weight:600; text-align:center; text-transform:uppercase;" > 
                                You have given your attendance today!! See you tomorrow.
                                </div></div>
	<?php
	} */
	

	if($fetch_attendance['Late_Count']=="1" && $fetch_attendance['Out_Time']=="0000-00-00 00:00:00")
	{
?>

<?php if($roster_fetch['today_date']!="")
{
	?>	
<div class="col-sm-12"><div class="alert alert-danger background-danger" style="font-weight:600; text-align:center; text-transform:uppercase;" > 
                               You are Late By : <?php if(strtotime($fetch_attendance['In_Time'])>strtotime($fetch_attendance['Attendance_Date']." ".$fetch_attendance['Roster_In_Time']))
								{
									$dateDiff = intval((strtotime($actual_in_time)-strtotime($roster_in_time))/60);

									$hours = intval($dateDiff/60);
									$minutes = $dateDiff%60;
									if($hours!=0)
									{
										echo $hours." Hr ";
									}
									if($minutes!=0)
									{
										echo $minutes." Min";
									}
								}?>
                                </div></div>                                
<?php
	}
	}
}
if($msg==1)
	{
	?>
	<div class="col-sm-12"><div class="alert alert-danger background-danger" style="font-weight:600; text-align:center; text-transform:uppercase;" > 
                                You have already given your attendance !!
                                </div></div>
	<?php
	}
?>                                  
</div>





</form>
</div>
</div>










</div>
</div>
</div>

</div>
</div>
</div>
</div>

<div id="styleSelector">
</div>
</div>
</div>
</div>
</div>




<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/bower_components/jquery/js/jquery.min.js"></script>
<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/bower_components/jquery-ui/js/jquery-ui.min.js"></script>
<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/bower_components/popper.js/js/popper.min.js"></script>
<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/bower_components/bootstrap/js/bootstrap.min.js"></script>

<script src="../files/assets/pages/waves/js/waves.min.js" type="c3ff8485e8526425ac6f5b03-text/javascript"></script>

<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/bower_components/jquery-slimscroll/js/jquery.slimscroll.js"></script>

<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/bower_components/modernizr/js/modernizr.js"></script>
<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/bower_components/modernizr/js/css-scrollbars.js"></script>
<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/assets/pages/form-validation/validate.js"></script>

<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/assets/pages/form-validation/form-validation.js"></script>
<script src="files/assets/js/pcoded.min.js" type="c3ff8485e8526425ac6f5b03-text/javascript"></script>
<script src="files/assets/js/vertical/vertical-layout.min.js" type="c3ff8485e8526425ac6f5b03-text/javascript"></script>
<script src="files/assets/js/jquery.mCustomScrollbar.concat.min.js" type="c3ff8485e8526425ac6f5b03-text/javascript"></script>
<script type="c3ff8485e8526425ac6f5b03-text/javascript" src="files/assets/js/script.js"></script>


<script src="files/assets/js/rocket-loader.min.js" data-cf-settings="c3ff8485e8526425ac6f5b03-|49" defer=""></script>





</body>
</html>
<script>startTime();</script>