<?php  
$table="emp_leave_register";
$table1="emp_leave";
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$status=true;
$rurl="index.php?route=HRModule/leave_request&modid=".$modid."&frmid=".$frmid."";
$viewrurl="index.php?route=HRModule/view_leave_request&modid=".$modid."&frmid=".$frmid."";


function leave_calculation($empid,$type)
{
	global $db;
	$calLeaveRecord = "Select sum(total_days) as total from emp_leave_apply where empid='".$empid."' and leave_type='".$type."' group by leave_type  ";	   
	$ResultData = DB_query($calLeaveRecord,$db);
	$RowData = DB_fetch_array($ResultData);
	return($RowData['total']);
}
?>
<script src="js/function-ajax.js" type="text/javascript"></script>
<center> <h2>Employee Leave Register </h2></center>

<form action="" method="post">
	<table border="0" cellpadding="0" cellspacing="0"  width="55%">
	<tr>
	<td>	Employee Name:  
	
	<?php  $query="Select id,emp_code ,  emp_name from empmaster where 1=1 order by emp_name ";	
	$sql_emp=DB_query($query,$db);
 	if(!$sql_emp){die('Error.'.DB_error());	}
 	?>
	<select name="emp_name" style="width:auto">
	<option value="0">ALL</option>
	<?php 	
	while($rowEmp=DB_fetch_array($sql_emp))
	{
	 $p='';
	 if($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>
	 
	<td>	Employee site:		
	<select name="emp_site" id="emp_site" tabindex="2">
	<option value="000">---Select---</option>
	<?php 
	 if($_SESSION['AssignedBranch']!=''){ 
	  $site_sql=DB_query("Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' 
	  	and loccode in('".$_SESSION['AssignedBranch']."')",$db);}
	  else
	  {$site_sql=DB_query("Select loccode,locationname from locations",$db);}
	while($site_row=DB_fetch_array($site_sql))
	{
	?>
	<option value="<?php echo $site_row['loccode'];?>"<?php echo ($emp_site==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
	<?php } ?>
	</select>  
	</td>
	<td><input type="submit" name="Search"   value="Search"/> </td>
	</tr>
</table>
</form>
<br>



<?php
if($_REQUEST['Search'])
{
	$filter=array();
	$fstatus=true;
	if($_REQUEST['emp_name']!='0')
	{
		$filter[]="id='".$_REQUEST['emp_name']."'";
	}
	
	if($_REQUEST['emp_site']!='000')
	{
		$filter[]="emp_site='".$_REQUEST['emp_site']."'";
	}
	if(count($filter)>0)
	{
		$fdata=implode(" and " ,$filter);
		$GetRecord = "Select * from empmaster where $fdata ";	   
	}
	else
	{
		$GetRecord = "Select * from empmaster where 1=1";
	}
		   
	 $ResultData = DB_query($GetRecord,$db);
	 while($RowData = DB_fetch_array($ResultData)){?>
    <form action="" method="post">
    <table border="0" cellpadding="0" cellspacing="0"  width="100%">
<tr>
	<td>Employee Code:</td>
	<td>Employee Name:</td>
	<td>Designation:</td>
	<td>Unit:</td>
</tr>
<tr>
	<td><input type="text" name="emp_code" value="<?php echo $RowData['emp_code']; ?>"></td>
	<td><input type="text" name="emp_name" value="<?php echo $RowData['emp_name']; ?>" size="50"></td>
	<td><select name="designation" id="designation"  > 
	<?php $qdesig=DB_query("select desigcode,designame from desigmaster where desigcode='".$RowData['designation']."'",$db);
	while ($rsdesig=DB_fetch_array($qdesig)){?>
	<option value="<?php echo $rsdesig['desigcode'];?>" <?php echo ($designation==$rsdesig['desigcode']?'selected':'')?>><?php echo $rsdesig['designame'];?></option>
	<?php }?>
	</select>
	</td>
	<td><select name="emp_site" id="emp_site"  >
	<?php 
	$site_sql=DB_query("Select loccode,locationname from locations where loccode='".$RowData['emp_site']."'",$db);
	while($site_row=DB_fetch_array($site_sql)){?>
	<option value="<?php echo $site_row['loccode'];?>"<?php echo ($emp_site==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
	<?php } ?>
	</select>  
	</td>
</tr>
   <tr>
	<td colspan="4"  valign="top">
	<fieldset><legend> <strong>Leave summary</strong></legend>	
	<div align="right"><a href="<?php echo $rurl; ?>&empid=<?php echo $RowData['id'];?>"> Leave Apply</a></div>
	<table border="0" width="100%" cellpadding="3" cellspacing="3">
	<tr bgcolor="#999999"><td><strong>Leave Type</strong></td>
	<td><strong>Allowed Leave</strong></td>
	<td><strong>Leave Taken</strong></td>
	<td><strong>Remaining Leave</strong></td>
	<td><strong>Detailed View</strong></td>
	</tr>
	
	<?php 
		$GetEmpLeave = "Select * from emp_leave where  emp_id='".$RowData['id']."' ";	   
		$ResultEmpLeave = DB_query($GetEmpLeave,$db);
		while($RowEmpLeave=DB_fetch_array($ResultEmpLeave))
		{	
	?>
	<tr>
	<td><?php
	echo $s->getField('leavemaster','id',$RowEmpLeave['leave_type'],'leavename');
	echo "( ".$RowEmpLeave['leave_type']." )";?> </td>
	<td><?php  echo $RowEmpLeave['leave_days']?> </td>
	<td> <?php echo $leaves= leave_calculation($RowData['id'],$RowEmpLeave['leave_type'])?></td>
	<td><?php echo ($leaves!=0 ? $RowEmpLeave['leave_days']-$leaves:'0');?> </td>
	<td><a href="<?php echo $viewrurl; ?>&empid=<?php echo $RowData['id']; ?>&leave_type=<?php echo $RowEmpLeave['leave_type']?>">View</a></td>
	</tr>
	<?php } ?>				
	</table>
	</fieldset>
	
	</td>
	
	
	</tr>
	
	</table>
	</form>
	<?php }}  ?>