<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="../css/main.css" rel="stylesheet" type="text/css" />

<?php

include ("../navigation/header.php");

$today = date("Y-m-d");
$firstday = date("Y").'-'.date("m").'-01';
$lastday = date("Y-m-d", mktime(0,0,0,date("m")+1,0,date("Y")));

$table_name = $_SESSION['db_prefix']."_time_sheet";
//$time_query = "SELECT SUM(check_out_min - check_in_min) AS recorded_time, SUM(time_zone_out - time_zone_in) AS required_time FROM $table_name WHERE attendance_date >= '".$firstday."' AND attendance_date <= '".$lastday."' AND rec_in = '1' AND rec_out = '1' ";

$table_name = $_SESSION['db_prefix']."_time_sheet";
$time_query = "SELECT SUM(check_out_min - check_in_min - lunch_min) AS recorded_time, SUM(time_zone_out - time_zone_in - lunch_period) AS required_time FROM $table_name WHERE  attendance_date >= '".$firstday."' AND attendance_date <= '".$lastday."' ";



$time_result = mySQL_query($time_query);
$time_num = mysql_num_rows($time_result);
$recorded_time = mysql_result($time_result, 0, "recorded_time");
$required_time = mysql_result($time_result, 0, "required_time");
if ($required_time!=0) {
$attendance = ($recorded_time / $required_time) * 100;
if ($attendance<=0) {$attendance = 0; }
} else {
	$attendance = 0;
}
?>

</head>

<body>
<?php

$page_name = "HR-main";
include ("../navigation/access.php");

if ($permission_num==1) {
	include ("../navigation/hr_sidebar.php");
?>
<?php

require_once('../include/tc_calendar.php');
	$date = $_GET['date'];
	
include('../include/period_select_date.php');

$today = date("Y-m-d");

	
$table_name = $_SESSION['db_prefix']."_time_sheet";
$late_query = "SELECT * FROM $table_name WHERE attendance_status = 'Late' AND attendance_date >= '".$start."' AND attendance_date <= '".$end."'";
$late_result = mySQL_query($late_query);
$late_num = mysql_num_rows($late_result);

//$table_name2 = $_SESSION['db_prefix']."_time_record";
//$early_query = "SELECT emp_id, clock_in, clock_out, status, reason2, date, permission FROM $table_name2 WHERE date >= '".$start."' AND date <= '".$end."' AND status_2 = 'E' ORDER BY date DESC ";
//$early_result = mySQL_query($early_query);
//$early_num = mysql_num_rows($early_result);
$early_num = 0;

$table_name = $_SESSION['db_prefix']."_time_sheet";
$absent_query = "SELECT * FROM $table_name WHERE attendance_status = 'absent' AND attendance_date >= '".$start."' AND attendance_date <= '".$end."'";
$absent_result = mySQL_query($absent_query);
$absent_num = mysql_num_rows($absent_result);



?>
<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/tooltip.css" />
<!--jscript-->
<script type="text/javascript" src="../jscript/tablesort.js"></script>
<script language="javascript" src="../jscript/calendar.js"></script>

<?php //include ('../include/tooltip.php'); ?>
<div class="main_frame" style="height:auto;">
  <div class="page_head_frame">
          <?php $attendance_label = "label_attendance_green" ;
		if ($attendance<96)  { $attendance_label = "label_attendance_orange" ; }
        if ($attendance<92)  { $attendance_label = "label_attendance_red" ; }?>
        <table width="700" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="437"><label class="label_page_head">HR</label></td>
            <td width="263" align="center">attendance this month, <b><?php echo date('M-Y')?></b> :<br />              <label class="<?php echo $attendance_label ?>"><?php echo number_format($attendance, 2, '.', ',')?> %</label></td>
            </tr>
        </table>
        </div>
        <br />
        <br /><br />
     <div class="page_middle_frame" style="width:700px;">
      
        <?php
		$page='hr_main.php';
		$reference_name='';
		$reference='';
        include('../include/period_select_block.php');
		?>
       
        <div class="scroll_frame_medium" style="height:200px; width:680px;"> Late this period:
          <table width="670" id="table_list" class="sortable-onload-0r rowstyle-alt no-arrow "   >
            <tr>
              <th class="sortable-date-dmy">date</th>
              <th class="sortable-text">name</th>
              <th class="sortable-text">department</th>
              <th >clock-in</th>
              <th colspan="2" class="sortable-text">reason/permission</th>
            </tr>
            <?php 
$i=0;

while ($i<$late_num) {
	
	$a1 = mysql_result($late_result, $i, "emp_id");
	   	$table_name = $_SESSION['db_prefix']."_employee";
    	$employee_query = "SELECT emp_first_name, emp_last_name, department  FROM $table_name WHERE emp_id = $a1 ";
		$employee_result = mySQL_query($employee_query);
        $a1a = mysql_result($employee_result, 0, "emp_first_name")." ".mysql_result($employee_result, 0, "emp_last_name");
		$a7 = mysql_result($employee_result, 0, "department");
		$a2 = mysql_result($late_result, $i, "check_in");
		$a8 = mysql_result($late_result, $i, "attendance_date");
		$a6 = mysql_result($late_result, $i, "late_reason");
		$a9 = mysql_result($late_result, $i, "late_permission");
		
		$employeeTT = "<label class=tip_text_label>".$a1a."</label><br><label class=tip_click_label>Click for details</label>";  
  
	?>
            <tr>
              <td width="70" align="left"><?php echo date("d-m-Y", strtotime($a8 ))?></td>
              <td width="160" align="left"><a href="../hr_module/employee_details.php?emp_id=<?php echo $a1?>" onMouseover="ddrivetip('<?php echo $employeeTT ?>  ', 150)"; onMouseout="hideddrivetip()"><?php echo $a1a ?></a></a></td>
              <td width="128" align="left"><?php echo $a7 ?></td>
              <td width="72" align="left"><?php  echo $a2 ?></td>
              <td width="184" align="left"><?php echo $a6 ?></td>
              <td width="38" align="left"><?php echo $a9 ?></td>
            </tr>
            <?php
  $i++;
}
?>
          </table>
          <div align="left">
            <label class="label_end">end of list</label>
          </div>
        </div>
    <div class="scroll_frame_medium" style="height:200px; width:680px;"> Absent this period:
          <table width="670" id="table_list_2" class="sortable-onload-0r rowstyle-alt no-arrow "  >
            <tr>
              <th class="sortable-text">date</th>
              <th class="sortable-text">name</th>
              <th class="sortable-text">department</th>
              <th colspan="2" class="sortable-text">reason/permission</th>
            </tr>
            <?php 
$i=0;

while ($i<$absent_num) {
	
	$b1 = mysql_result($absent_result, $i, "emp_id");
	   	$table_name = $_SESSION['db_prefix']."_employee";
    	$employee_query = "SELECT emp_first_name, emp_last_name, department  FROM $table_name WHERE emp_id = $b1 ";
		$employee_result = mySQL_query($employee_query);
        $b1a = mysql_result($employee_result, 0, "emp_first_name")." ".mysql_result($employee_result, 0, "emp_last_name");
		$b7 = mysql_result($employee_result, 0, "department");
	$b6 = mysql_result($absent_result, $i, "absent_reason");
	$b8 = mysql_result($absent_result, $i, "attendance_date");
	$b9 = mysql_result($absent_result, $i, "absent_permission");
	
	$employeeTT = "<label class=tip_text_label>".$b1a."</label><br><label class=tip_click_label>Click for details</label>";  
	?>
            <tr>
              <td width="70" align="left"><?php echo date("d-m-Y", strtotime($b8 ))?></td>
              <td width="161" align="left"><a href="../hr_module/employee_details.php?emp_id=<?php echo $b1?>" onMouseover="ddrivetip('<?php echo $employeeTT ?>  ', 150)"; onMouseout="hideddrivetip()"><?php echo $b1a ?></a></td>
              <td width="126" align="left"><?php echo $b7 ?></td>
              <td width="261" align="left"><?php echo $b6 ?></td>
              <td width="38" align="left"><?php echo $b9 ?></td>
            </tr>
            <?php
  $i++;
}
?>
          </table>
          <div align="left">
            <label class="label_end">end of list</label>
          </div>
        </div>
        <div class="scroll_frame_medium" style="height:100px; width:680px;"> Early departure this period:
          <table width="670" id="table_list_3" class="sortable-onload-0r rowstyle-alt no-arrow "   >
            <tr>
              <th class="sortable-date-dmy">date</th>
              <th class="sortable-text">name</th>
              <th class="sortable-text">department</th>
              <th >clock-out</th>
              <th colspan="2" class="sortable-text">reason/permission</th>
            </tr>
            <?php 
$i=0;

while ($i<$early_num) {
	
	$a1 = mysql_result($early_result, $i, "emp_id");
	   	$table_name = $_SESSION['db_prefix']."_employee";
    	$employee_query = "SELECT emp_first_name, emp_last_name, department  FROM $table_name WHERE emp_id = $a1 ";
		$employee_result = mySQL_query($employee_query);
        $a1a = mysql_result($employee_result, 0, "emp_first_name")." ".mysql_result($employee_result, 0, "emp_last_name");
		$a7 = mysql_result($employee_result, 0, "department");
		$a2 = mysql_result($early_result, $i, "clock_out");
		   $hours =  floor($a2/60); 
		   $mins =   $a2 % 60; 
		   if ($hours<10) { $hours = 0 . $hours; }
		   if ($mins<10) { $mins = 0 . $mins; }
		   $a2a = $hours . ":" . $mins ;
		$a8 = mysql_result($early_result, $i, "date");
		$a6 = mysql_result($early_result, $i, "reason2");
  		$a9 = mysql_result($early_result, $i, "permission");
		
		$employeeTT = "<label class=tip_text_label>".$a1a."</label><br><label class=tip_click_label>Click for details</label>";  
	?>
            <tr>
              <td width="70" align="left"><?php echo date("d-m-Y", strtotime($a8 ))?></td>
              <td width="160" align="left"><a href="../hr_module/employee_details.php?emp_id=<?php echo $a1?>" onMouseover="ddrivetip('<?php echo $employeeTT ?>  ', 150)"; onMouseout="hideddrivetip()"><?php echo $a1a ?></a></a></td>
              <td width="128" align="left"><?php echo $a7 ?></td>
              <td width="72" align="left"><?php  echo $a2a ?></td>
              <td width="186" align="left"><?php echo $a6 ?></td>
              <td width="36" align="left"><?php echo $a9 ?></td>
            </tr>
            <?php
  $i++;
}
?>
          </table>
       
        </div>
</div>
</div>
<?php include ('../navigation/side_bar.php'); ?>





<?php } else { ?>
<script> needToConfirm = false; </script>
<?php
$_SESSION['denied_return'] = $_SESSION['location'];	
include ("../navigation/hr_sidebar.php");
include ('../navigation/side_bar.php'); 
?>
<body onload="accessdenied(); return false">
</body>
<?php }
?>

</body>
</html>