<?php
$PageSecurity = 2;
$title = _('Pay Chart') ;
include('includes/SQL_CommonFunctions.inc');
$_SESSION['modid']=$_REQUEST['modid'];
$_SESSION['frmid']=$_REQUEST['frmid'];
$frmpermission= $px->GetFileAccess($_SESSION['modid'],$_SESSION['frmid'],$_SESSION['ACGRoleId']);
$_SESSION['frmAdd']=$frmpermission[0];
$_SESSION['frmEdit']=$frmpermission[1];
$_SESSION['frmDelete']=$frmpermission[2];echo "<br>";
//if($_SESSION['frmAdd']!='1')
//{echo "<strong><h3 align='center'>Sorry. You do not have the authority. Please check with the Administrator</h3></strong>";exit;}
$current_month=date('m');
$current_year=date('Y');
$date=date('d/m/Y');
switch($current_month)
{
	case 01:
	$month="January";
	break;
	case 02:
	$month="February";
	break;
	case 03:
	$month="March";
	break;
	case 04:
	$month="April";
	break;
	case 05:
	$month="May";
	break;
	case 06:
	$month="June";
	break;
	case 07:
	$month="July";
	break;
	case 08:
	$month="August";
	break;
	case 09:
	$month="September";
	break;
	case 10:
	$month="October";
	break;
	case 11:
	$month="November";
	break;
	case 12:
	$month="December";
	break;
	default :
	$month="";}
$sql1="select * from empmaster";
$result1=DB_query($sql1,$db);
?>
<script type="text/javascript">
function cal_earning(str)
{
	
	var salary=document.getElementById("salary_"+str).value;
	var days=document.getElementById("no_of_days_"+str).value;
	var total=(salary/31)*days;
	document.getElementById("earnings_"+str).value=total.toFixed(2);
}
function cal_tot_earning(str)
{
	var earn=document.getElementById("earnings_"+str).value;
	document.getElementById("tot_earning_"+str).value=earn;
}
function cal_total(str)
{
	var tot_earning=document.getElementById("tot_earning_"+str).value;
	var deduction=document.getElementById("advance_"+str).value;
	var total=tot_earning-deduction;
	document.getElementById("final_total_"+str).value=total;
	document.getElementById("net_salary_"+str).value=total;
}
function cal_leaves(str,j)
{
	var absent=document.getElementById("leaves_"+j).value;
	var remaining=str-absent;
	document.getElementById("remain_leaves_"+j).value=remaining;
}
</script>
<?php 
if($_REQUEST['Save']!='')
{
	for($cr=1;$cr<$_REQUEST['ctr'];$cr++)
	{
		$insert_sql="Insert into pay_chart_master set
										id='',
										emp_id='".$_REQUEST['employee_id'][$cr]."', 
										salary='".$_REQUEST['salary'][$cr]."',  
										no_of_days='".$_REQUEST['no_of_days'][$cr]."', 
										leaves='".$_REQUEST['leaves'][$cr]."', 
										earnings='".$_REQUEST['earnings'][$cr]."', 
										tot_earning='".$_REQUEST['tot_earning'][$cr]."',  
										advance='".$_REQUEST['advance'][$cr]."',  
										final_total='".$_REQUEST['final_total'][$cr]."',   
										net_salary='".$_REQUEST['net_salary'][$cr]."', 
										tot_leaves='".$_REQUEST['tot_leaves'][$cr]."', 
										remain_leaves='".$_REQUEST['remain_leaves'][$cr]."', 
										sign_remarks='".$_REQUEST['sign_remarks'][$cr]."',
										created_date='".FormatDateForSQL($date)."',
										month='".date('m')."',  
										year='".date('Y')."', 
										created_by='".$_SESSION['UserID']."'
										";
		//echo "<pre>";
		//print_r($insert_sql);
		$insert_res=DB_query($insert_sql,$db);
	}
	echo "<script>location.href='index.php?route=pay_chart&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
?>
<h2 align="center"><strong>Pay Chart For The M/o - <?php echo $month.", ".$current_year;?></strong></h3>
<form action="" method="post">
<div align="center">
<div style="overflow:auto;width:800px;height:250px">
<table width="100%" border="0" cellspacing="1" cellpadding="2">
  <tr>
    <th><strong>EMPLOYEE</strong></th>
    <th><strong>EMPLOYEE</strong></th>
    <th>&nbsp;</th>
	<th>&nbsp;</th>
    <th>&nbsp;</th>
    <th><strong>ENTITLED</strong></th>
    <th><strong>PAID</strong></th>
    <th>&nbsp;</th>
	<th>&nbsp;</th>
    <th><strong>TOTAL</strong></th>
    <th><strong>DEDUCTIONS</strong></th>
	<th>&nbsp;</th>
    <th><strong>NET</strong></th>
	<th><strong>TOTAL</strong></th>
	<th><strong>REMAINING</strong></th>
    <th><strong>SIGN/</strong></th>
  </tr>
  <tr>
    <th><strong>CODE</strong></th>
    <th><strong>NAME</strong></th>
    <th><strong>SITE</strong></th>
	<th><strong>DEPARTMENT</strong></th>
    <th><strong>DESIGNATION</strong></th>
    <th><strong>SALARY</strong></th>
    <th><strong>DAYS(P)</strong></th>
	<th><strong>LEAVES(A)</strong></th>
    <th><strong>EARNINGS</strong></th>
    <th><strong>EARNINGS</strong></th>
    <th><strong>ADVANCE</strong></th>
    <th><strong>TOTAL</strong></th>
    <th><strong>SALARY</strong></th>
	<th><strong>LEAVES</strong></th>
	<th><strong>LEAVES</strong></th>
    <th><strong>REMARKS</strong></th>
  </tr>
  <?php 
  $i=1;  
  if($i%2==0)
  {$color="#DEC0DD";}
  else
  {$color="#B8CBCB";}
  while($row1=DB_fetch_array($result1))
  {
     $sql2="select count(*) as pdays from daily_attendance where employee='".$row1['id']."' and attendance_month='".$current_month."' and attendance_year='".$current_year."' and pres_abs='P'";
	$result2=DB_query($sql2,$db);
  	$myrow=DB_fetch_array($result2);
	$sql3="select count(*) as adays from daily_attendance where employee='".$row1['id']."' and attendance_month='".$current_month."' and attendance_year='".$current_year."' and pres_abs='A'";
	$result3=DB_query($sql3,$db);
  	$myrow3=DB_fetch_array($result3);
	
	$leave_sql=DB_query("select sum(leave_days) as leaves from emp_leave where emp_id='".$row1['id']."'",$db);
	$leave_row=DB_fetch_array($leave_sql);
  ?>
  <tr bgcolor="<?php echo $color;?>">
    <td><?php echo $row1['emp_code']; ?></td>
    <td><?php echo $row1['emp_name']; ?><input type="hidden" name="employee_id[<?php echo $i;?>]" value="<?php echo $row1['id']; ?>" /></td>
    <td><?php 
	$site_sql=DB_query("Select locationname from locations where loccode='".$row1['emp_site']."'",$db);
	$site_result=DB_fetch_array($site_sql);
	echo $site_result['locationname']; ?></td>
	<td><?php 
	$dept_sql=DB_query("Select deptname from deptmaster where deptcode='".$row1['department']."'",$db);
	$dept_result=DB_fetch_array($dept_sql);
	echo $dept_result['deptname']; ?></td>
    <td><?php 
	$desig_sql=DB_query("Select designame from desigmaster where desigcode='".$row1['designation']."'",$db);
	$desig_result=DB_fetch_array($desig_sql);
	echo $desig_result['designame']; ?></td>  
    <td><input name="salary[<?php echo $i;?>]" type="text" id="salary_<?php echo $i;?>" value="<?php echo $row1['gross_sal']; ?>" size="10"  readonly="true"/></td>
    <td><input name="no_of_days[<?php echo $i;?>]" type="text" id="no_of_days_<?php echo $i;?>" value="<?php echo $myrow['pdays']?>" size="5" readonly="true"/></td>
	<td><input name="leaves[<?php echo $i;?>]" type="text" id="leaves_<?php echo $i;?>" value="<?php echo $myrow3['adays']?>" size="5" readonly="true"/></td>
    <td><input name="earnings[<?php echo $i;?>]" id="earnings_<?php echo $i;?>" type="text" value="" size="10" onfocus="cal_earning(<?php echo $i;?>)"/></td>
    <td><input name="tot_earning[<?php echo $i;?>]" id="tot_earning_<?php echo $i;?>" type="text" value="" size="10" onfocus="cal_tot_earning(<?php echo $i;?>)"/></td>
    <td><input name="advance[<?php echo $i;?>]" id="advance_<?php echo $i;?>" type="text" value="<?php echo $row1['adv_amount'];?>" size="10" onblur="cal_total(<?php echo $i;?>)"/></td>
    <td><input name="final_total[<?php echo $i;?>]" id="final_total_<?php echo $i;?>" type="text" value="" size="10" /></td>
    <td><input name="net_salary[<?php echo $i;?>]" id="net_salary_<?php echo $i;?>" type="text" value="" size="10" /></td>
	 <td><input name="tot_leaves[<?php echo $i;?>]" type="text" id="tot_leaves_<?php echo $i;?>" value="<?php echo $leave_row['leaves'];?>" size="8" onfocus="cal_leaves(this.value,<?php echo $i;?>)"/></td>
	<td><input name="remain_leaves[<?php echo $i;?>]" type="text" id="remain_leaves_<?php echo $i;?>" value="" size="10" /></td>
    <td><input name="sign_remarks[<?php echo $i;?>]" id="sign_remarks_<?php echo $i;?>" type="text" value="" size="20" /></td>
  </tr>
  <?php $i++;} ?>
</table>
<input type="hidden" name="ctr" id="ctr" value="<?php echo $i;?>" />
</div>
</div>
<br />
<p align="center"><input type="submit" name="Save" value="Save" /></p>
</form>
<?php
include('includes/footer.inc');
?>