<form action="" method="post">
<table border="0" cellpadding="3" cellspacing="0" width="100%">
<tr class="row1">
	<td width="12%" align="right">Employee Branch</td>	
	<td width="20%"><select name="emp_site" id="emp_site"  onchange="chk_employee();">
	<option value="000">---Select---</option>
	<?php 
	 $site_sql=DB_query("Select loccode, locationname from locations  
	 	where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db);
	while($site_row=DB_fetch_array($site_sql))
	{
	?>
	<option value="<?php echo $site_row['loccode'];?>"<?php echo ($_REQUEST['emp_site']==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
	<?php } ?>
	</select>  
	</td>
	<td width="12%" align="right">Year</td>		
	<td width="20%"><select name="year" id="year"  onchange="chk_employee();">
 	<?php
	for($i=date('Y');$i>2000;$i--)
 	{
 		$p='';
 		if( $i==$_REQUEST['year'])
			$p='selected';
 	?>
	<option value="<?php echo $i;?>" <?php echo $p;?>><?php echo $i;?></option>
 	<?php } ?>
 	</select>
	</td>
    <td width="12%" align="right">Employee Name</td>
	<?php   if($_REQUEST['year']!=''){
        $query="SELECT id,emp_code ,emp_name FROM empmaster WHERE CompanyId='".$_SESSION['ACGCompanyId']."' 
        AND emp_site='".$_REQUEST['emp_site']."' AND ((YEAR(dol)>='".$_REQUEST['year']."' OR dol='00-00-0000') 
        	AND YEAR(doj)<='".$_REQUEST['year']."') order by emp_code ASC";
        }else{
        $query="SELECT id,emp_code ,emp_name FROM empmaster WHERE CompanyId='".$_SESSION['ACGCompanyId']."'
         AND ((YEAR(dol)>='".date('Y')."' OR dol='00-00-0000') AND YEAR(doj)<='".date('Y')."') order by emp_code ASC";  
        }
	$sql_emp=DB_query($query,$db);
 	if(!$sql_emp){die('Error.'.DB_error());	}
 	?>
	<td><select name="emp_name" id="emp_name" >
	<option value="0">ALL</option>
	<?php 	
	while($rowEmp=DB_fetch_array($sql_emp))
	{
	 $p='';
	 if($_REQUEST['emp_name']==$rowEmp['id'])
	 $p='selected';
	?>
	<option value="<?php echo $rowEmp['id'] ?>" <?php echo $p; ?>><?php echo $rowEmp['emp_name'].'('. $rowEmp['emp_code'].')' ?></option>
	 <?php 
	}
	?></select>	
	 </td>
</tr>
<tr class="row1">	
	<td colspan="6" style="text-align:center;"><input type="submit" name="Search" class="search" value="Search"/> </td>
</tr>
</table>
</form>
<br>
<?php
if($_REQUEST['Search'])
{   
	$_SESSION['leave_year']=$_REQUEST['year'];
	$filter=array();
	$fstatus=true;
	//echo $_REQUEST['emp_name'];
	if($_REQUEST['emp_name']!='0')
	{
		$filter[]="id='".$_REQUEST['emp_name']."'";
	}
	
	if($_REQUEST['emp_site']!='000')
	{
		$filter[]="emp_site='".$_REQUEST['emp_site']."'";
	}
        if($_REQUEST['year']!='')
	{
		$filter[]="year(leave_eff_date)<='".$_REQUEST['year']."' AND ((YEAR(dol)>='".$_REQUEST['year']."' OR dol='00-00-0000') AND YEAR(doj)<='".$_REQUEST['year']."')";
	}
	if(count($filter)>0)
	{
		$fdata=implode(" and " ,$filter);
		$GetRecord = "Select * from empmaster where $fdata ";	   
	}
	else
	{
		$GetRecord = "Select * from empmaster where 1=1";
	}
	
	$i=0;	   
	$ResultData = DB_query($GetRecord,$db);
	while($RowData = DB_fetch_array($ResultData)){?>
<?php 
//echo "select emp_name,emp_code,emp_site,designation,leave_eff_date from empmaster where id='".$RowData['id']."'";
$SQLEmpDet=DB_query("select emp_name,emp_code,emp_site,designation,leave_eff_date,doj from empmaster where id='".$RowData['id']."'",$db);
$EmpADetail=DB_fetch_array($SQLEmpDet);
$desgiName=$s->GetFieldValue('desigmaster',"id='".$EmpADetail['designation']."'",'designame')?>
<form action="" method="post">
		<h1>
EMP Code : <?php echo $EmpADetail['emp_code'];?> , EMP Name : <?php echo $EmpADetail['emp_name'];?> , Desgination : <?php echo $desgiName; ?> 
</h1>

	<h3>Leave Summary</h3>
<table border="0" cellpadding="3" cellspacing="0" width="100%" class="thborder">
	<tr>
		<th>Leave Type</th>
		<th width="12%">Allowed Leave</th>
		<th width="12%">Applied Leave</th>
		<th width="12%">Leave Taken</th>
		<th width="12%">Cancel Leave</th>
		<th width="12%">Remaining Leave</th>
		<th width="8%">Detailed View</th>
		<th width="8%">Leave Apply</th>
	</tr>
	<?php  
	 $fin_yr_start=$_SESSION['financialYrStart'];
	 $fin_yr=explode("-",$_SESSION['financialYrStart']);
	 $get_fin_yr=$fin_yr[0];
         $servicedays='90';
	      $allowd_date=date('Y-m-d', strtotime($EmpADetail['doj']. ' + '.$servicedays.' days'));
	//  echo date('Y-m-d');
	    if($allowd_date>date('Y-m-d'))
	    $datediffA='1';
	    else
	   $datediffA='0';	
	     $GetEmpLeave = "Select * from emp_leave where  emp_id='".$RowData['id']."' ";	   
	  $ResultEmpLeave = DB_query($GetEmpLeave,$db);
	  while($RowEmpLeave=DB_fetch_array($ResultEmpLeave)){
         $leaveType=$s->getField('leavemaster','id',$RowEmpLeave['leave_type'],'leavetype');
         $leavecode=$s->getField('leavemaster','id',$RowEmpLeave['leave_type'],'leavecode');
          $allowed_leave1=$RowEmpLeave['leave_days'];

        if($leaveType=='c')
        {
        	 $eff_date=date('Y',strtotime($EmpADetail['leave_eff_date']));
        	 //if(($leavecode=='PL' || $leavecode='EL') && date("Y")==$eff_date)
        	 if(($leavecode=='PL' || $leavecode='EL'))
               {
        	$privious_opening = "Select opening as total_opening from hr_leave_opening where empid='".$RowData['id']."' 
          and leave_type='".$RowEmpLeave['leave_type']."'   and opening_year='".$get_fin_yr."' group by leave_type";	   
	      $ResultData1 = DB_query($privious_opening,$db);
	      $RowData1 = DB_fetch_array($ResultData1);
		
          if($RowData1['total_opening']=='')
          	{ 
          		$opening_leave=0;
          	}
          	else 
      		{ 
      			$opening_leave=$RowData1['total_opening'];
      		}
          //echo $opening_leave;         
        	$allowed_leave=$opening_leave+$allowed_leave1;
        }
      /*  else	
        $allowed_leave=leave_allowed($RowData['id'],$RowEmpLeave['leave_type'],$_REQUEST['year'],$EmpADetail['leave_eff_date'],$RowEmpLeave['leave_days']);*/
        
 //echo $leaveType;
        if($leaveType=='e')
        {
		         $eff_date=date('Y',strtotime($EmpADetail['leave_eff_date']));
		         $privious_opening = "Select opening as total_opening from hr_leave_opening where empid='".$RowData['id']."' 
		          and leave_type='".$RowEmpLeave['leave_type']."'   and opening_year='".$_REQUEST['year']."' group by leave_type";	   
			      $ResultData1 = DB_query($privious_opening,$db);
			      $RowData1 = DB_fetch_array($ResultData1);
				
		          if($RowData1['total_opening']==''){ $opening_leave=0;}else { $opening_leave=$RowData1['total_opening'];}
		          //echo $opening_leave;     
		    	//echo $eff_date;
		          
		          if(($leavecode=='PL' || $leavecode=='EL') && date("Y")!=$eff_date)
		           {       	
		          //echo $opening_leave;         
		        	 $allowed_leave=$opening_leave;        
		        	}
		           else
		           {    
		         			$allowed_leave=$RowEmpLeave['leave_days']+$opening_leave;    
		          }			
          }
         }
         //echo $AppliedLeaveDays."###".$cancelleave."###".$TakenLeaveDays;
	$cancelleave=Cancel_leave_calculation($RowData['id'],$RowEmpLeave['leave_type'],'');
	$AppliedLeaveDays=leave_calculationForApplied($RowData['id'],$RowEmpLeave['leave_type'],'');
	$TakenLeaveDays=leave_calculation($RowData['id'],$RowEmpLeave['leave_type'],'');
	$totalapplyleave=$AppliedLeaveDays-$cancelleave-$TakenLeaveDays;
	$RemainingLeaveDays=$allowed_leave-$TakenLeaveDays;

	if($RemainingLeaveDays=='0' )
	$disable="disabled";
	else
	$disable='';
	?>
	<tr>
		<td><?php
		echo $s->getField('leavemaster','id',$RowEmpLeave['leave_type'],'leavename');
		echo "( ".$RowEmpLeave['leave_type']." )";?></td>
		<td><?php  echo $allowed_leave?></td>
		<td><?php if($totalapplyleave!=''){echo $totalapplyleave;}else{echo '0';}?></td>
		<td><?php if($TakenLeaveDays!=''){ echo $TakenLeaveDays;}else{echo '0';}?></td>
		<td><?php if($cancelleave!=''){echo $cancelleave;}else{echo '0';}?></td>
		<td><?php if($RemainingLeaveDays!=''){echo $RemainingLeaveDays;}else{echo '0';}?></td>
		<td><input type="hidden" id="empId_<?php echo $i; ?>" value="<?php echo $RowData['id']; ?>" />
		    <input type="hidden" id="LeaveType_<?php echo $i; ?>" value="<?php echo $RowEmpLeave['leave_type']; ?>" />
		     <input type="hidden" id="alloweddate_<?php echo $i; ?>" value="<?php echo $datediffA; ?>" />
		<?php if($_SESSION['frmEdit']=='1'){?><input type="button" name="viewleaveA" id="viewleaveA_<?php echo $i; ?>" value="View Leave" class="search" /><?php } else {echo "No Permission";}?></td>
		<td><input type="button" name="LeaveApplly" id="LeaveApplly_<?php echo $i; ?>" onclick="fillbranch('<?php echo $EmpADetail['emp_site']; ?>')" value="Apply Leave" class="search" <?php echo $disable; ?> /></td>
	</tr>
	<?php $i++;} ?>				
	</table>
</form>
<?php }}  ?>


<script>
function chk_employee()
{
if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{//alert(xmlhttp.responseText);
	  document.getElementById("emp_name").innerHTML=xmlhttp.responseText;
	}
	}
	
	var year=document.getElementById("year").value;
	var site=document.getElementById("emp_site").value;
	xmlhttp.open("GET","check_data_hr.php?section=chk_employee_all&site="+site+"&year="+year,true);
	xmlhttp.send();
}
</script>