<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Pay Salary</h1>

<?php //error_reporting(0);
include('./includes/fun-php.php');
include('./includes/fun-HRClass.php');
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];

$rurl="index.php?route=HRModule/master_payslip&action=View&modid=".$modid."&frmid=".$frmid."";
 $rurl_view="Print_PaySlipReport.php?modid=".$modid."&frmid=".$frmid."";
 $frurl_view="index.php?route=HRModule/PaySlipReport&action=View&modid=".$modid."&frmid=".$frmid."";
if($_REQUEST['Search']   )
		{
		$paystatus=true;  
		$_SESSION['payyear']=$_REQUEST['payyear'];
		$_SESSION['paymonth']=$_REQUEST['paymonth'];
		$_SESSION['empcode']=$_REQUEST['empcode'];
		$_SESSION['emptype']=$_REQUEST['emptype'];
		$_SESSION['emp_salprocess_site']=$_REQUEST['site'];
		$years=$_SESSION['payyear'];
		$month=$_SESSION['paymonth'];	   
		$empcode=$_SESSION['empcode'];
		$emptype=$_SESSION['emptype'];
		$emp_salprocess_site=$_SESSION['emp_salprocess_site'];
			
    }	 
     ?>
     <script language="javascript">
	 var th = ['','thousand','million', 'billion','trillion'];
// uncomment this line for English Number System
// var th = ['','thousand','million', 'milliard','billion'];

var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine']; var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen']; 

var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety']; 

function toWords(s)
{ 

	s = s.toString();
	s = s.replace(/[\, ]/g,''); if (s != parseFloat(s)) return 'not a number'; var x = s.indexOf('.'); if (x == -1) x = s.length; if (x > 15) return 'too big'; var n = s.split(''); var str = ''; var sk = 0; for (var i=0; i < x; i++) {if ((x-i)%3==2) {if (n[i] == '1') {str += tn[Number(n[i+1])] + ' '; i++; sk=1;} else if (n[i]!=0) {str += tw[n[i]-2] + ' ';sk=1;}} else if (n[i]!=0) {str += dg[n[i]] +' '; if ((x-i)%3==0) str += 'hundred ';sk=1;} if ((x-i)%3==1) {if (sk) str += th[(x-i-1)/3] + ' ';sk=0;}} if (x != s.length) {var y = s.length; str += 'point '; for (var i=x+1; i<y; i++) str += dg[n[i]] +' '; str+=' paise';} 
	var xx=str.replace(/\s+/g,' ');
	return(xx);	 
	 
	
	}


function calOTAmt()
{
	
	var addinsal =  document.getElementById("addinsal").value;
	
if(addinsal=='Y'){	
	  var OTBasicAmount = parseInt(document.getElementById("OTBasicAmount").value);	
	  var OThours = parseInt(document.getElementById("OThours").value);	
	  var workingday= parseInt(document.getElementById("workingdays").value);	
	   var Amt=parseInt(OTBasicAmount/workingday);
	   var HourAmt=parseFloat(Amt/8);
	   var ansD = document.getElementById("OTAmount");
	   var calAmt=parseInt(OThours * HourAmt);
	    ansD.value = calAmt;
	    	var ansD1 = document.getElementById("net_earning");
	    	val1=	ansD1.value;
			ansD1.value =parseInt(val1)+ calAmt ;	
			 document.getElementById("netpay").value=parseInt(document.getElementById("netpay").value) + 	calAmt;
			  document.getElementById("wordamt").value=toWords(parseInt(document.getElementById("netpay").value));
}
	 
	 
}

function calEarningOTAmt(j)
{
		var basicsal =  document.getElementById("basicsal").value;
		var C=0; 
		var C1=0;
		for(i=0;i<j;i++)
		{
			var C = parseInt(C)+ parseInt(document.getElementById("allowcode_value_"+i).value);
			var C1 = parseInt(C1)+ parseInt(document.getElementById("allowdeduction_value_"+i).value);		 
		}
		 document.getElementById("net_earning").value=C;
		 document.getElementById("earning").value=C;
		 document.getElementById("deduction").value=C1; 		 	 
		 document.getElementById("netpay").value=C-C1;
	document.getElementById("wordamt").value=toWords(C-C1);
}
 
     function calculation_deduction()
	 {
                var val1 = parseInt(document.getElementById("total_deduction").value);
				var val2 = parseInt(document.getElementById("loan").value);
				var val3 = parseInt(document.getElementById("ptax").value);
				var val4 = parseInt(document.getElementById("advancetax").value);
				
				var ansD = document.getElementById("deduction");							
     			ansD.value = val1 + val2+ val3+ val4;
				
 				var ded = parseInt(document.getElementById("deduction").value);
				var earning = parseInt(document.getElementById("earning").value);
				var ansD1 = document.getElementById("netpay");			
				ansD1.value = earning - ansD.value;	
				document.getElementById("wordamt").value=toWords(ansD1.value);
	 }

function chk_type_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("empcode").innerHTML=xmlhttp.responseText;
	}
	}
	var site=document.getElementById("site").value;
	var month=document.getElementById("paymonth").value;
	var year=document.getElementById("payyear").value;
	var type=document.getElementById("emptype").value;
	xmlhttp.open("GET","check_data_hr.php?section=chk_type_employee&site="+site+"&month="+month+"&year="+year+"&type="+type,true);
	xmlhttp.send();
}
     </script>
     
<?php 
 
function OThours($empid,$month, $years)
{
	global $db;
	     $GetRecord = "Select sum(ot_hrs) as OThours from daily_attendance where employee='".$empid."' and  month(attendance_date)='".$month."'  and  year(attendance_date)='".$years."'  group by year(attendance_date) ,month(attendance_date) ";	   	   
       $ResultData = DB_query($GetRecord,$db);
	  // echo DB_num_rows($ResultData);
   	  $RowData = DB_fetch_array($ResultData);
	   return( $RowData['OThours']);
}
$curernt_year=$_REQUEST['payyear'];
$curernt_month=$_REQUEST['paymonth'];
//echo $curernt_month.$curernt_year."is on a " . date("w", mktime(0, 0, 0, 7, $curernt_month, $curernt_year));

?>  
 
<form action="" method="post">
 <table width="100%" align="center" border="0" cellspacing="0" cellpadding="3" style="border-collapse:collapse;">
	<tr class="row1">		
    <td width="13%" align="right">Branch</td>
	<td width="20%"><select name="site" id="site"  onchange="chk_type_employee();">
		<option value="">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))
			{
			$t='';
			if(trim($_REQUEST['site'])==trim($site_row['loccode']))	
				$t="selected";
		?>
		<option value="<?php echo $site_row['loccode'];?>"<?php echo $t; ?> ><?php echo $site_row['locationname'];?>
		</option>
		<?php }?>
	</select>
  </td>
<td width="13%" align="right">Month</td>
<td width="20%"><select name="paymonth" id="paymonth"  onchange="chk_type_employee();">
 	<?php
 		$arr=array(1=>"Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
 		foreach ($arr as $key => $value) {
 			$p='';
 			if( $key==$_SESSION['paymonth'])
				$p='selected';
   	?>
	<option value="<?php echo $key;?>" <?php echo $p;?> ><?php echo $value;?></option>
 	<?php 
	} 
	?>
</select>
</td>   
<td width="13%" align="right">Year</td>
<td><select name="payyear" id="payyear"   onchange="chk_type_employee();">
 	<?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> 
</tr>
<tr class="row1">
    <td align="right">Employee Type</td>
	<td>
        <select name="emptype" id="emptype" onchange="chk_type_employee();" >
<!--			<option value="" >---</option>-->
			<option value="R"<?php echo($staff_type=='R'?'selected':'')?>>Regular Employee</option>
			<option value="C"<?php echo($staff_type=='C'?'selected':'')?>>Contract Employee</option>
			<option value="P"<?php echo($staff_type=='P'?'selected':'')?>>Probation Employee</option>
	</select>	
</td> 
<td align="right">Employee Code</td>
	<td>
	<div id='hid-empdiv'> 
		<select name="empcode" id="empcode" >
			<option value="">All</option>
			<?php 
			if($_REQUEST['paymonth']!='' && $_REQUEST['payyear']!='' && $_REQUEST['emptype']!=''){
                        $chk_date=$_REQUEST['payyear']."-".$_REQUEST['paymonth']."-01";
                        $chk_date1=$_REQUEST['payyear']."-".$_REQUEST['paymonth']."-31";
                        $emp_sql=DB_query("SELECT id,emp_code ,emp_name FROM empmaster WHERE CompanyId='".$_SESSION['ACGCompanyId']."' AND emp_site='".$_REQUEST['site']."' AND employment_type='".$_REQUEST['emptype']."' AND (dol>='".$chk_date."' OR dol='0000-00-00') AND doj<='".$chk_date1."' 
              ORDER BY emp_name ASC",$db);
                        }else{
                        $chk_date=date('Y')."-".date('m')."-01";
                        $chk_date1=date('Y')."-".date('m')."-31";
                        $emp_sql=DB_query("SELECT id,emp_code ,emp_name FROM empmaster WHERE CompanyId='".$_SESSION['ACGCompanyId']."' AND employment_type='Permanent' AND (dol>='".$chk_date."' OR dol='0000-00-00') AND doj<='".$chk_date1."' ORDER BY emp_name ASC",$db);
                        }
			while($emp_row=DB_fetch_array($emp_sql))
			{
			?>
			<option value="<?php echo $emp_row['id'];?>"<?php if($_SESSION['empcode']==$emp_row['id'])echo "selected";?>><?php echo $emp_row['emp_name'].' ( '.$emp_row['emp_code'].' )';?></option>
			<?php 	
			}
			?>
		</select>
	</div>
	</td>
	<td></td>
	<td></td>
</tr>
<tr class="row1">
	<td colspan="6" style="text-align:center;"><input type="submit" name="Search" value="Search" class="search"/></td>
</tr>   
</table>
</form>

<br>
<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
	<tr>
		<th width="10%">Emp Code</th>
		<th>Emp Name</th>
		<th width="10%" class="text-center">Basic Salary</th>
		<th width="8%">PaySlip Month</th>
		<th width="6%">PaySlip Years</th>
		<th width="6%" class="text-center">Earning</th>
		<th width="6%" class="text-center">Deduction</th>
		<th width="6%" class="text-center">NetPay</th> 
		<th width="5%" class="text-center">Print</th>		
	</tr>
	<?php
	if($_REQUEST['Search'] )
  	{
	   $paystatus=true; 
	   $branch = $_REQUEST['site']; 
	   $emptype = $_REQUEST['emptype'];
	   
	   $years=$_REQUEST['payyear'];
	   $month=$_REQUEST['paymonth'];	
	   $empcode=$_REQUEST['empcode'];	
	   $filter=array();
	   if($empcode!=0)
	   $filter[]="emp_id='".$empcode."'";
	   
	   if($month!='' && $years!='')
	   $filter[]="payslip_month='".$month."' and  payslip_year ='".$years."'";	   
	   if(count($filter)>0){
	   $condition=implode(' and ',$filter);
	    $GetProcess = "Select * from master_process_salary  where $condition and status='Y'";			  		 	   
			}
	
	elseif($_SESSION['payyear']!='' && $_SESSION['paymonth']!=''&& $_SESSION['empcode']!='' )
	  	  $GetProcess = "Select * from master_process_salary where emp_id='".$_SESSION['empcode']."' and payslip_month='".$_SESSION['paymonth']."'  and  payslip_year ='". $_SESSION['payyear'] ."'";	 

	/*else{   
  	  $GetProcess = "Select * from master_process_salary where locked='Y' and payslip_month='".(date(m)-1)."'  and  payslip_year ='". date(Y) ."'  ";		 	   
	}*/
	
		$_SESSION['payyear']=$_REQUEST['payyear'];
		$_SESSION['paymonth']=$_REQUEST['paymonth'];
		$_SESSION['empcode']=$_REQUEST['empcode'];
		$_SESSION['emptype']=$_REQUEST['emptype'];
		$_SESSION['emp_salprocess_site']=$_REQUEST['site'];

	//echo $GetProcess;
       $ResultProcess_SalData = DB_query($GetProcess,$db);
	 if(DB_num_rows($ResultProcess_SalData)>0)
	 {
   	   while($RowData = DB_fetch_array($ResultProcess_SalData))
	   {
	  ?> 
		<tr>
		<td><?php echo $s->getField('empmaster','id',$RowData['emp_id'],'emp_code');?></td>
		<td><?php echo $s->getField('empmaster','id',$RowData['emp_id'],'emp_name');?></td>
		<td align="right"><?php echo $RowData['basic_salary'];?></td>
		<td><?php echo GetMonth( $RowData['payslip_month']);?></td>
		<td><?php echo $RowData['payslip_year'];?></td>
		<td align="right"><?php echo $RowData['gross_salary'];?></td>
		<td align="right"><?php echo round($RowData['gross_salary']-$RowData['net_earning']+$RowData['othe_ded']); ?></td>
		<td align="right"><?php echo round($RowData['net_earning']);?></td>		 
		<td align="center">
 		<!-- <a href="<?php //echo $frurl_view.'&id='.$RowData['id'].'&emp_id='.$RowData['emp_id'].'&paymonth='.$_SESSION['paymonth'].'&payyear='.$_SESSION['payyear'].'&action=Edit' ; ?>">FPrint</a>-->
		<a href="<?php echo $rurl_view.'&id='.$RowData['id'].'&emp_id='.$RowData['emp_id'].'&paymonth='.$_SESSION['paymonth'].'&payyear='.$_SESSION['payyear'] ; ?>" target="_blank"><img src="images/icon_print.jpg" width="18" title="Print Document" /></a>		
		</td>		
	 </tr>
	<?php }
	 }
	 else{
	   echo "<tr><td colspan='9' align='center'>No Record found</td><tr>";
	 }?>
 <?php } ?>
</table>
 </div>
</div>