<?php
include("../include/config.php");
require_once "../include/Database_functions.php";
?>

  <table  border="0" cellspacing="0" cellpadding="0" class="tableList"  style="margin-left:20px; ">
<tr class="heading">
    <td>No</td>    
    <td>Employee</td>   
    <td>Designation</td>   
    <td>Basic Pay</td>   
    <td>Deductions</td>   
    <td>Earnings</td>
    <td>No Of Leaves</td>
    <td>LOP</td>
    <td>Total Payable</td>
    <td>Pay Slip</td>
    <td>Payment Status</td>
  </tr>
<?php

$empid ='';
$gMonth='';
$gYear='';

if(isset($_REQUEST['empid'])){
$empid=$_REQUEST['empid'];
}

if(isset($_REQUEST['gMonth'])){
$gMonth=$_REQUEST['gMonth'];
}
else
{
$gMonth=date('m');
}

if(isset($_REQUEST['gYear'])){
$gYear=$_REQUEST['gYear'];
}
else
{
$gYear=date('Y');
}

$sq_arr="select emp.e_id as e_id,emp.e_fname as e_fname,emp.e_lname as e_lname,desg.desig_name as desig_name,deductear.* from cf_deduction_earning as deductear join cf_employee as emp on deductear.emp_id = emp.e_id left join cf_designation as desg on emp.desigid = desg.desig_id where  1 ";


if($empid!=''){
$sq_arr.=" and employee_id = '".$empid."' ";
}

/*if($gMonth!="" && $gYear!=""){
$sq_arr.=" and month(day_from) = '$gMonth' and year(day_from) = '$gYear'";
}*/

$sq_arr.=" order by emp.e_id desc ";
$query=$sq_arr;
echo $query;
$function="Searchdate";
include("../paging.php");
$sq_arr.=" LIMIT $start, $limit";

if(isset($_REQUEST['page'])){
$page=$_REQUEST['page'];
}else{
$page=10;
}
$previousleavedays ='';
$currentleavedays ='';
$array=mysql_query($sq_arr);
if($array && mysql_num_rows($array)>0){
$pagecnt=$start;
while($res=mysql_fetch_array($array))
{

$pagecnt++;
$deduction = $res['HRA_ded']+$res['LOP_ded']+$res['PF_ded']+$res['other_ded'];
$earnings = $res['basic_ear']+$res['da_ear']+$res['ta_ear'];

//echo $attMonth;

$sqlsal_1= mysql_query("select sum(no_of_days) as numdays,cf_payroll_user_leave_requests.* from cf_payroll_user_leave_requests where employee_id = '".$res['e_id']."' and month(day_from) != '".$gMonth."' and year(day_from) = '".$gYear."'");
$rws1 = mysql_fetch_array($sqlsal_1);
$previousleavedays = $rws1['numdays'];


$sqlsal_2= mysql_query("select sum(no_of_days) as numdays,cf_payroll_user_leave_requests.* from cf_payroll_user_leave_requests where employee_id = '".$res['e_id']."' and month(day_from) = '".$gMonth."' and year(day_from) = '".$gYear."'");
$rws2 = mysql_fetch_array($sqlsal_2);
$currentleavedays = $rws2['numdays'];


$lop='';
$salarycut='';
if($previousleavedays >= 12)
{ 
	$lop = $currentleavedays;	    
}
else
{
	$previousleavedays + $currentleavedays;
	$lop =  ($previousleavedays + $currentleavedays) - 12 ;
}

if($lop<0)
{
$lossofpay=0;
}
else
{
$lossofpay=$lop;
}


$sqlresult= mysql_query("select * from cf_deduction_earning where emp_id = '".$res['e_id']."'");
$rwsa1 = mysql_fetch_array($sqlresult);
$sal_perday = ($rwsa1['basic_ear']*12)/365;
if($lop > 0 )
{
	$salarycut = $sal_perday*$lop;	
	$salarycut1 = round($salarycut,2);	
}
else
{
	$salarycut1 = 0;	
}


if($lop < 0 )
{
	$lop =0;
}

$totpay = $earnings-$salarycut1-$deduction;
$pay_period=$gYear.'-'.$gMonth.'-01';

$pay_status= mysql_query("select * from cf_payroll_salary_pay where emp_id = '".$res['e_id']."' and pay_period='".$pay_period."'");
if($pay_status && mysql_num_rows($pay_status)>0)
{
$payment_status='Paid';
}
else
{
$payment_status='Not Paid';
}

?> 
  <tr>
    <td><?php echo $pagecnt;?></td>
    <td><?php echo $res['e_fname'].' '.$res['e_lname'];?></td> 
    <td><?php echo $res['desig_name'];?></td>  
    <td><?php echo $res['basic_ear'];?></td>
    <td><?php echo $deduction;?></td>  
    <td><?php echo $earnings;?></td>     
    <td><?php echo $lop;?></td>   
    <td><?php echo $salarycut1;?></td>   
    <td><?php echo $totpay;?></td>   
	<td>
	<?php
	if($payment_status=='Not Paid')
	{
	?><a href="voucher-form.php?vouchertype=salary&empid=<?=$res['e_id']?>&salamt=<?=$totpay?>&salmonth=<?=$attMonth?>&salyear=<?=$attYear?>"><img src="images/payslip.jpg" width="20" height="20"></a>
	<?php
	
	}
	
	else
	{
	?><a href="#"><img src="images/payslip.jpg" width="20" height="20"></a>
		<?php
		}
		?>
	</td>    
	<td><?php echo $payment_status;?></td>
  </tr>
  <?php 
    }}else{?>
	 <tr>
    <td class="errorMessage"  style="border-right:0px;border-bottom:0px; "><font color="#FF0000">No Records Found</font></td></tr>
	<? }
  ?>
 
  
</table>
  
   <div align="right" style="padding-top:10px;"><?=$pagination?></div>