<center> <h2>Process Salary</h2></center>
<?php //error_reporting(0);
include('includes/fun-php.php');
include('includes/fun-HRClass.php');
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$rurl="index.php?route=SalaryProcess&action=View&modid=".$modid."&frmid=".$frmid."";
 $rurl_view="index.php?route=View_master_payslip&action=View&modid=".$modid."&frmid=";
 ?><table width="100%" border="0" cellpadding="0" cellspacing="0">
   
  <tr>
    <td align="left"  scope="row">Role : Monthly wages Calculation...</td>
  </tr>
  <tr>
    <td align="left"  scope="row">&nbsp;</td>
  </tr>
  <tr>
    <td align="left" >
<form action="" method="post">
	<table border="0" cellpadding="0" cellspacing="0"  width="50%%">
	<tr>
		
		
		<td>Employee Type: 
	<select name="emptype" id="emptype">
		<option value="Permanent"<?php echo($_REQUEST['emptype']=='Permanent'?'selected':'')?>>Permanent</option>
		<option value="Temporary"<?php echo($_REQUEST['emptype']=='Temporary'?'selected':'')?>>Temporary</option>
	</select>	
</td> 
		
		
		<td>Site</td>
		<td><select name="site" id="site">
<option value="000">---Select Site---</option>
<?php 
if($_SESSION['AssignedSites']!='0'){
$site_sql=DB_query("Select loccode, locationname from locations where loccode in (".$_SESSION['AssignedSites'].")",$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 ($_REQUEST['AttSite']==$site_row['loccode']?'selected':'');?>><?php echo $site_row['locationname'];?></option>
<?php } ?>
</select></td>
		
		<td>Employee Code: 
	<select name="empcode" id="empcode">
		<option value="">All</option>
		<?php 
		$emp_sql=DB_query("Select emp_code,emp_name from empmaster order by emp_name ",$db);
		while($emp_row=DB_fetch_array($emp_sql))
		{?>
			<option value="<?php echo $emp_row['emp_code'];?>"<?php if($_REQUEST['empcode']==$emp_row['emp_code'])echo "selected";?>><?php echo $emp_row['emp_name'];?></option>
		<?php 	
		}
		?>
		
	</select>	
<!-- <input type="text" name="empcode" value="<?php //echo $_REQUEST['empcode']; ?>">--> </td> 
 <td>Month :
 <select name="paymonth"  style="width:120px;">
 <?php
 $arr=array(1=>"Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
 foreach ($arr as $key => $value) {
 	$p='';
 	if( $key==$_REQUEST['paymonth'])
	$p='selected';
   ?><option value="<?php echo $key;?>" <?php echo $p;?> ><?php echo $value;?></option>
 <?php } ?></select>
	  </td> 
  
   <td>year :
 <select name="payyear"  style="width:120px;">
 <?php
 
 for($i=date('Y');$i>2000;$i--)
 {
 	$p='';
 	if( $i==$_REQUEST['payyear'])
	$p='selected';
 ?><option value="<?php echo $i;?>" <?php echo $p;?>><?php echo $i;?></option>
 <?php 
 }
 ?></select>
	  </td>       
	<td>	<input type="submit" name="Search"   value="Search"/> </td></tr></table>
	
	
</form>
    </td>
  </tr>
  
</table>
<!---->
<div style="width: 800px; overflow: scroll;">
<?php 
//echo $_SESSION['empcode'];
if($_REQUEST['UnProcess'])
{
	 
	$dd="'".implode("',' ",$_SESSION['AcgMst_Process_Salary'])."'";
	$empid="'".implode("',' ",$_SESSION['AcgMst_Emp_Process_Salary'])."'";
	
	
	$condition="id in ($dd) and emp_id in($empid)";
	$s->DeleteRecord('master_process_salary',$condition);
	
	
	$condition="mst_prosal in ($dd) and emp_id in($empid)";
	$s->DeleteRecord('payslip_salary',$condition);
}
if($_REQUEST['Save'])
{
 	
	$_REQUEST['Search']=1;
	 
	 for($i=0;$i<count($_SESSION['Mstemp']);$i++){
    $data['emp_id'] =$_SESSION['Mstemp'][$i][0];
	$data['payslip_month'] = $_SESSION['Mstemp'][$i][1];
	$data['payslip_year'] =$_SESSION['Mstemp'][$i][2];
	
	$data['basic_salary'] = $_SESSION['Mstemp'][$i][3];
	$data['workingdays'] = $_SESSION['Mstemp'][$i][4];
	$data['addinsal'] = $_SESSION['Mstemp'][$i][5];;
	$data['OThours'] =$_SESSION['Mstemp'][$i][6];
	$data['OTBasicAmount'] =$_SESSION['Mstemp'][$i][7];
		$data['OTAmount'] =$_SESSION['Mstemp'][$i][8];
	$data['advancetax'] =$_SESSION['Mstemp'][$i][9];
	$data['loan'] =$_SESSION['Mstemp'][$i][10];
	$data['ptax'] =$_SESSION['Mstemp'][$i][11];
 	$data['net_earning'] = $_SESSION['Mstemp'][$i][12];
	$data['total_deduction'] =$_SESSION['Mstemp'][$i][13];	
	 $data['netpay'] = $_SESSION['Mstemp'][$i][14];
	$data['wordamt'] = $_SESSION['Mstemp'][$i][15];	
	$data['locked'] = 'Y';	
	 
	$insert=$s->RecordInsert('master_process_salary', $data);
	$last=  DB_Last_Insert_ID($db,'master_process_salary', 'id');
		  
	for($j=0;$j<count($_SESSION['Mstemp_paysal']);$j++)
	{
		$data_arr['mst_prosal']=$last;
		$data_arr['emp_id']=$_SESSION['Mstemp_paysal'][$j][0];;
		$data_arr['allowance_id']=$_SESSION['Mstemp_paysal'][$j][1];;
		 $data_arr['allowance_amount']=$_SESSION['Mstemp_paysal'][$j][2];;
		  $data_arr['allowance_type']=$_SESSION['Mstemp_paysal'][$j][3];;
		  $insert=$s->RecordInsert('payslip_salary', $data_arr);
	 
 	}	
//	print_r($data);
	}
	 
	
	
	
	 $s->PageLocation($rurl);
	
	
}

    	if($_REQUEST['Search']   )
		{
			 	
		
			 $_SESSION['Mstemp_paysal']=array();			 
			 $_SESSION['Mstemp']=array();
			$paystatus=true;  
			$_SESSION['payyear']=$_REQUEST['payyear'];
			$_SESSION['paymonth']=$_REQUEST['paymonth'];
			$_SESSION['empcode']=$_REQUEST['empcode'];
			 $_SESSION['emptype']=$_REQUEST['emptype'];
 
	   		$years=$_SESSION['payyear'];
	   		$month=$_SESSION['paymonth'];	   
	        $empcode=$_SESSION['empcode'];
			$emptype=$_SESSION['emptype'];
?>
<form action="" method="post">
<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="8"><b>DEDUCTIONS</b></td>
		
		<td ><b>Advance Loan</b>
		<td ><b>TOTAL</b></td><td ><b>			OT</b></td>
		<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</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>Total Deduction</td>    <td>&nbsp;</td>
      <td>Gross Wages</td>
      
    <td>OT amount</td>
    <td>Net Pay</td>
     <td >In Words</td>  <td >Loc</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 );
	 $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 echo round(array_sum($deduction_amount),0);
	   	?>
       </td>
	      <td>&nbsp;<?php  echo $AdvLoan=advance_loan($emp_id,$month,$years);?></td>
       <td>	<?php   
	   			 $netpayable=$paid_gross+round(array_sum($allowance_amount)-array_sum($deduction_amount),0);   
				 if($AdvLoan!=0)
				 $netpayable=$netpayable-$AdvLoan;
	   				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 width="2%">
<?php 
  $condition="payslip_month='".$month."' and payslip_year='".$years."' and emp_id='".$emp_id."'";
 $Lockedid= $s->GetFieldValue('master_process_salary',$condition ,'id');
$_SESSION['AcgMst_Process_Salary'][]= $Lockedid;
$_SESSION['AcgMst_Emp_Process_Salary'][]= $emp_id;
echo ($Locked==''?'N':'Y');
//echo  $s->getField('master_process_salary','payslip_month',$month,'locked'); ?>&nbsp;</td>
        </tr>
       <?php 
       
  
$_SESSION['Mstemp'][]=array($RowData['id'],$month,$years,$gross_sal,$workingdays,'',0,0,$OT,0,0,0,array_sum($allowance_amount),array_sum($deduction_amount),$t1,ucwords(number_to_words($t1)));
	 


}

$i++;

} 
//echo $fstatus;
//if($fstatus==0){
?>
<tr><td colspan="28">
	<?php   //echo $m=substr(date('m'), -1); 
	if($month!=date('m') && $years!=date('Y'))
	{?>
	<input type="submit" name="Save"  value="Process Salary" />
	
	<input type="submit" name="UnProcess"  value="UnProcess Salary" />
	<?php } ?>
</td></tr>
<?php //}?>
    </table>
    
    	<div id="foo">
  	  <p align="center">
	<a href="#"  onclick="popup('Print-SalaryProcess.php',600,600) ">Click Here to Download</a></p>
  	</div> 
  	
    </form>
    
       <?php } ?></div>