<?php //error_reporting(0);
$PageSecurity = 3;
 include ('includes/session.inc');
  include ('includes/function_lib.php');
  include('includes/fun-php.php');
include('includes/fun-HRClass.php');
 
 global $db;
 
  echo '<link href="'.$rootpath. '/css/'. $_SESSION['Theme'] .'/default.css" rel="stylesheet" type="text/css" />';
?>
<script type="text/javascript" language="javascript">
  function toggle_visibility(id) {
	
	 
       var e = document.getElementById(id);
	       e.style.display = 'none';
		       //alert(e.style.display);
			   window.print();
    }

</script>
<?php 

 			
 			$years=$_SESSION['payyear'];
	   		$month=$_SESSION['paymonth'];	   
	        $empcode=$_SESSION['empcode'];
			$emptype=$_SESSION['emptype'];
			
	?><h2><center>Process Salary</center></h2>
 <table width="100%" border="1" cellspacing="2" cellpadding="2">
	<tr><td colspan="8"></td>
		<td colspan="6"><b>EARNINGS</b></td>
		<td  rowspan="2" valign="top"><b>GROSS WAGES </b></td>
		<td colspan="9"><b>DEDUCTIONS</b>	    </td>
		
		<td ><b>TOTAL</b></td><td ><b>			OT</b></td>
		<td colspan="3">&nbsp;</td><td rowspan="2"><strong>Employee Signature</strong></td>
	</tr>
        <tr>
        <td> Sr. No.</td>
        <td> Emp Code</td>
        <td>Emp Name</td>
        <td>Designation </td>
        <td >Month<br>Days</td>
        <td>Sal Days</td>
         <td>E.S.I.<br>No</td>
          <td>P.F.<br>No</td>
        <!-- <td >Monthly Salary</td> -->
        <td >Payable  Basic</td>
<?php
	  $net_earning=0;
	  $query_allowances =DB_query("Select * from allowmaster   ",$db); 
	  $j=1;
	  $n=DB_num_rows($query_allowances);
	  $nx= round(70/$n,2);
	  while($row_allowances=DB_fetch_array($query_allowances))
	  { 		  
?> 
    	<td><?php echo $row_allowances['allowcode'];?>
		<input name="allowmasterid[]" type="hidden"  value="<?php echo $row_allowances['id'];?>"/></td>         
     <?php } ?>
     
     <?php $query_allowances =DB_query("Select * from deduction_master   ",$db); 
	 	   $n=DB_num_rows($query_allowances);
	   	   $nx1= round(70/($n+3),1);
	  while($row_allowances=DB_fetch_array($query_allowances))
	  {	  
	  ?> 
    	<td ><?php echo $row_allowances['code'];?>
		<input name="deductionid[]" type="hidden"  value="<?php echo $row_allowances['id'];?>"/>		</td>         
     <?php } ?>
	<td>Adv Loan </td>     
	<td>Adv Bal </td>    
	<td>Total Deduction</td>
	<td>Gross Wages</td>
	<td>OT amount</td>
	<td>Net Pay</td>
	<td >In Words</td>  
</tr>
        <?php
 if($_SESSION['empcode']!='')
	$GetRecord = "Select * from empmaster where  emp_code='".$_SESSION['empcode']."' ";
 else if($_SESSION['emptype']!='')
   $GetRecord = "Select * from empmaster where  employment_type='".$_SESSION['emptype']."' ";
 else
	 $GetRecord = "Select * from empmaster where  1=1 ";
 	 
 	  //echo $GetRecord;		   
      $ResultData = DB_query($GetRecord,$db);
	  // echo DB_num_rows($ResultData);
	  $fstatus=1;
	  $i=1;
   	  while($RowData = DB_fetch_array($ResultData))
   	  {
   	  	 $emp_id=	$RowData['id'];
	  $condition="payslip_month='".$month."' and payslip_year='".$years."' and emp_id='".$emp_id."'";
 	   $Locked= $s->GetFieldValue('master_process_salary',$condition ,'locked');
	   if($Locked=='Y')
	    $fstatus=0;
 	 //($Locked=='' || $Locked=='N')
	 {
	    $fstatus=1;
		  $lt=0;	
	  	  $emp_id=	$RowData['id'];
		  $gross_sal=$RowData['gross_sal'];
		  $basic_sal=$RowData['basic_salary'];
		  $basic_perc=$RowData['basic_perc'];
		 $over_timestatus= $RowData['over_time'];
   	      $workingdays= cal_days_in_month(CAL_GREGORIAN, $month, $years);
	      $one_day_sal= round(($basic_sal/$workingdays),2)		;   	  			
   	  	 $payable_day = payable_day($emp_id,$month, $years,'P');
   	  	 $holiday=Count_Holidays($month, $years );	
		 
		   
   $leave_sql="Select * from leavemaster";
   $leave_res=DB_query($leave_sql,$db);  
   while($leave_row=DB_fetch_array($leave_res))
   {
    $leave_val=payable_day_leavetype($emp_id,$month, $years,$leave_row['leavecode']);
	$lt=$lt+$leave_val;   
    }  
   	 $weeklyoff= CountSatSun($month, $years ,$RowData['emp_weekly_off']);
	 $lt=$lt+$weeklyoff;	   
	        $absent= payable_day($emp_id,$month, $years,'A');    
        	 $absent_total_days =$workingdays-$absent-($payable_day+$holiday+$lt ); 
             $absent_withoutinfo=$workingdays -($payable_day+$holiday+$lt );  
	          $ACGpaydays=$workingdays- $absent_withoutinfo;
		      $paid_gross=$ACGpaydays*$one_day_sal;
 
         ?>
      <tr>
        <td><?php echo $i;?></td>
        <td ><?php echo $RowData['emp_code']; ?>&nbsp;</td>
       <td ><?php echo $RowData['emp_name']."<br/><br/>".$RowData['bank_name']." A/C No. ".$RowData['bank_account']; ?>&nbsp;</td>
       <td><?php echo $fnLib->getField('desigmaster','desigcode',$RowData['designation'],'designame');?></td>
        <td ><?php echo  $workingdays= cal_days_in_month(CAL_GREGORIAN, $month, $years);
         ?></td>
        <td ><?php echo $ACGpaydays; ?></td>
           <td ><?php echo $RowData['PFno'] ?>&nbsp;</td>
              <td ><?php echo $RowData['ESIno'] ?>&nbsp;</td>
        <!--  <td ><?php echo $RowData['gross_sal']; ?></td> -->
        <td ><?php 
		echo $RowData['basic_salary']."<br>-----<br>";
		echo $gbasic= round(($paid_gross),2);; ?>		</td>
       
       
       	<?php $deduction_amount=array();$allowance_amount=array();
       	
		
  	$q1 =DB_query("Select * from allowmaster   ",$db); 
	  
	  while($rq1=DB_fetch_array($q1))
	  { 
		
	    $query =DB_query("Select * from emp_salary where emp_id='".$emp_id."' and allowance_deduction='".$rq1['id']."' and all_ded_prefix='A'",$db);
		if(DB_num_rows($query))
		{
       	 while($row=DB_fetch_array($query))
	 	 {
       	?>
       	<td >
  
       		<?php  
       
	//echo	 $row['all_ded_prefix'];
       	if($row['percentage']==0.00 )
       	{
       	$allowance=($row['percentage_value']/$workingdays)*$ACGpaydays;
		}
		else {
				$allowance=.01*$gbasic*$row['percentage']*$ACGpaydays;	
		}
		 
		 echo $row['percentage_value']."<br>-----<br>";
		 echo (round($allowance)!=0?round($allowance):'-');
		 //if($row['all_ded_prefix']=='A') 
		$allowance_amount[]=round($allowance);
		/*	else
		$deduction_amount[]=round($allowance);*/
		$_SESSION['Mstemp_paysal'][]=array($emp_id,$row['allowance_deduction'],round($allowance),$row['all_ded_prefix']);
		 ?>		 </td>
       	 <?php }
		}else{echo "<td>-</td>";}
	  }	 
?>
<td>	<?php    echo $gbasic+array_sum($allowance_amount);?>       </td>
       <?php 
//------------------------------------------------------------------------------------------
		
  	$q2 =DB_query("Select * from deduction_master   ",$db); 
	  
	  while($rq2=DB_fetch_array($q2))
	  { 
	//	echo "Select * from emp_salary where emp_id='".$emp_id."' and allowance_deduction='".$rq2['id']."' and all_ded_prefix='D'<br>";
	    $query =DB_query("Select * from emp_salary where emp_id='".$emp_id."' and allowance_deduction='".$rq2['id']."' and all_ded_prefix='D'",$db);
		if(DB_num_rows($query))
		{
       	 while($row=DB_fetch_array($query))
	 	 {
       	?>
       	<td >
  
       		<?php  
       
	// echo	 '==>'.$row['percentage'];
       	if($row['percentage']==0.00 )
       	{
       	$allowance=($row['percentage_value']/$workingdays)*$ACGpaydays;
		}
		else {
				$allowance=.01*$gbasic*$row['percentage']*$ACGpaydays;	
		}
		 
		  
		   if(strtolower($rq2['name'])=='pf')
		   {
		 
		   		echo round($gbasic*.0833);
				echo "<br>";
				echo round($gbasic*.0367);
				echo "<br>";
				 echo (round($gbasic*.0833)+round($gbasic*.0367));
				 $deduction_amount[]=round($gbasic*.0833)+round($gbasic*.0367);
		   }else{
		 
		  echo (round($allowance)!=0?round($allowance):'-');
		  $deduction_amount[]=round($allowance);
		  }
		
		/* if($row['all_ded_prefix']=='A') 
		$allowance_amount[]=round($allowance);
		else*/
		
		$_SESSION['Mstemp_paysal'][]=array($emp_id,$row['allowance_deduction'],round($allowance),$row['all_ded_prefix']);
		 ?>		 </td>
       	 <?php }
		}else{echo "<td>-</td>";}
	  }	
?>
  		 <td><?php   $AdvLoan=advance_loan($emp_id,$month,$years);
		 echo $AdvLoan[0];
		 ?></td>
		 <td>  <?php echo EmpBalanceAmount($emp_id,$AdvLoan[1]);?></td>    
	     <td>&nbsp;<?php echo round(array_sum($deduction_amount),0)+$AdvLoan[0]; 	?></td>
         <td>
		 <?php   
	   			 $netpayable=$paid_gross+round(array_sum($allowance_amount)-array_sum($deduction_amount),0);   
				 if($AdvLoan!=0)
				 $netpayable=$netpayable-$AdvLoan[0];
	   				echo ($netpayable!=0?$netpayable:0);
	   	?>       </td>
      
          <td><?php //echo $OT=round(GetOverTime($emp_id,$month,$years),0);
          	if($over_timestatus=='Yes')
          	   $OT=round(GetOverTime($emp_id,$month,$years),0);
			 
			
			 echo ($OT!=0?$OT:'-');
          	 ?></td>
        

          <td><?php   $t1=$OT+$netpayable;
          	
          	echo round($t1);?></td>
         <td width="20%"><?php       
		   echo ucwords(number_to_words(round($t1,0))); ?></td>
		   <td>Paid By <?php echo $RowData['payment_mode']?></td>
   </tr>
       <?php 
       
 
}

$i++;

} 
 ?>    </table>
  
  
   
 <?php 
 if($_REQUEST['action']!='Download'){
echo   $data=$link;}
else{
$data=$link;
}
?>
<?php 
 if($_REQUEST['action']!='Download'){?>
<div id="foo"><p align="center">
<a href="#" onclick="javascript:toggle_visibility('foo');" >Print</a>    
 </p>
</div>
	<?php } ?>

</div>
<?php 
if($_REQUEST['action']=='Download'){
header("Content-type: application/x-msdownload"); 
header("Content-Disposition: attachment; filename=Requisition_Register.xls"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
print "$header\n$data";   }
?>