<?php
session_start();  
include_once("../class/class.MySQL.php");

$db	=new MySQL();
 $limit=$page_rows;
   $page = $_GET['page'];   
	
	
	if($page) 
		$start = ($page - 1) * $limit;  //first item to display on this page
	else
	
		$start = 0;	
		if(isset($_GET['emp_post']))
		{
		
		
		if($_GET['emp_post']=='manager')
		{
		$where="a.e_post='manager'";
		}elseif($_GET['emp_post']=='supervisor')
		{
		$where="a.e_post='supervisor'";
		}elseif($_GET['emp_post']=='employee')
		{
		$where="a.e_post='employee'";
		}
		elseif($_GET['branch_id']=='')
		{
		$branchid = $_GET['branch_id'];
		$where="a.branchid='$branchid'";
		}
	
		elseif($_GET['emp_post']=='1'||$_GET['emp_post']=='')
		{
		$where="1";
		}
		
		
		
		}else{ $where="1"; }
		
		if(!empty($_GET['branch_id']))
		{
			$branchid = $_GET['branch_id'];
			$where="a.branchid='$branchid'";
		}
		
		if(!empty($_GET['emp_id']))
		{
			$empid = $_GET['emp_id'];
			$where="a.e_id='$empid'";
		}
		
		if(!empty($_GET['emp']))
		{
		$emp = $_GET['emp'];
		$where="a.emp_status='$emp'";
		}	
		
		
		if($_SESSION['usertype'] == 'SubadminHR')
		{
			$branchidhr = $_SESSION['branch_id'];
			$where="a.branchid='$branchidhr'";
		}		

    $join='a.e_id=b.e_id and a.e_id=c.e_id';
    $group='c.e_id'; 
	$ordr='c.time_in '; 

	/*	$name = $db->fetchAlljoineachrows_g_k('`employee` as a,`file` as b,`time` as c,`designation` as d', $where,$ordr,'a.e_id',$limit,$cols='a.e_fname,a.e_mname,a.e_gender,a.e_lname,a.e_mail,a.e_mob,a.e_post,a.e_id,a.employee_id,a.clock_status,a.status,b.photo,c.e_in,c.e_out,c.time_in,c.time_inloc,c.time_out,c.time_outloc,d.desig_name' ,' asc',$join,$start);AND b.clock_status !=1*/
			 $queryss = "SELECT * FROM ((SELECT a. * , c.e_date, c.time_in, c.time_out
FROM  `employee` AS a,  `time` AS c WHERE a.e_id = c.e_id AND a.clock_status =1 AND c.e_date = '$date')
UNION (SELECT b . * , d.e_date, d.time_in, d.time_out FROM  `employee` AS b,  `time` AS d
WHERE b.e_id = d.e_id  AND d.e_date!='$date')
) AS e GROUP BY e_id ORDER BY clock_status DESC , time_in ASC , e_id DESC";

	$results= mysql_query($queryss);
		$result=array();
				
              while ($rows = mysql_fetch_array($results))
			  {
                   $result[$a++]=$rows;
				   
            //  return $result;
	}
   $arr=array();
   $i=1;
   foreach($result as $name)
   {
   $em_id=$name['em_id'];
   $e_id=$name['e_id'];
   /*$join1="c.id='". $em_id."'";
   $join1.=' and a.e_id=b.e_id and a.e_id=c.e_id and a.e_post=d.desig_id' ;
   $ordr='c.time_in '; 
   $qry = $db->fetchAlljoineachrows('`employee` as a,`file` as b,`time` as c,`designation` as d', $where,'','a.e_id',$limit,$cols='a.e_fname,a.e_mname,a.e_gender,a.e_lname,a.e_mail,a.e_mob,a.e_post,a.e_id,a.employee_id,a.clock_status,a.status,b.photo,c.e_in,c.e_out,c.time_in,c.time_inloc,c.time_out,c.time_outloc,d.desig_name' ,' desc',$join1,$start );*/
   
  $sqls = mysql_query("select * from employee  where e_id='$name[e_id]'");
   $rowsd = mysql_fetch_array($sqls);
   $sqlsf = mysql_query("select * from file  where e_id='$name[e_id]'");
   $rowsdf = mysql_fetch_array($sqlsf);
 $rowsdf['photo'];
    $sqlsdsg = mysql_query("select * from designation  where desig_id='$rowsd[e_post]'");
   $rowsdg = mysql_fetch_array($sqlsdsg);
   
   $e_fname=$name['e_fname'];
   $e_mname=$name['e_mname'];
   $e_gender=$name['e_gender'];
   $e_lname=$name['e_lname'];
   $e_mail=$name['e_mail'];
   $e_mob=$name['e_mob'];
   $e_post=$rowsdg['desig_name'];
   $e_id=$name['e_id'];
   $clock_status=$name['clock_status'];
   $status=$name['status'];
   $photo=$rowsdf['photo'];
   $e_in=$name['e_in'];
   $e_out=$name['e_out'];
   $time_in=$name['time_in'];
   $time_inloc=$name['time_inloc'];
   $time_out=$name['time_out'];
   $time_outloc=$name['time_outloc'];
   $e_employee_id=$name['employee_id'];
   $arr[$i]['e_fname']= $e_fname;
   $arr[$i]['e_mname']= $e_mname;
   $arr[$i]['e_gender']= $e_gender;
   $arr[$i]['e_lname']= $e_lname;
   $arr[$i]['e_mail']= $e_mail;
   $arr[$i]['e_mob']= $e_mob;
	$arr[$i]['e_post']= $e_post;
	$arr[$i]['e_id']= $e_id;
	$arr[$i]['clock_status']= $clock_status;
	$arr[$i]['status']= $status;
	$arr[$i]['photo']= $photo;   
	$arr[$i]['e_in']= $e_in;  
	$arr[$i]['e_out']= $e_out;
	$arr[$i]['time_in']= $time_in;
	$arr[$i]['time_inloc']= $time_inloc;
	$arr[$i]['time_out']= $time_out;
	$arr[$i]['time_outloc']= $time_outloc;
	$arr[$i]['employee_id']= $e_employee_id;     
	$i++;
   }
	
	echo '{"name":'.json_encode($arr).'}';

?>
