<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}

include("../opendb.php");
include("../test.php");
$psres=mysqli_query($db,"select brpass from br_master where brname='$br'");
$psrow=mysqli_fetch_array($psres);
 if($psrow[0]!=$pswd)
 {
   print "<center><blink><font color=red> Invalid Password</font>";
   return;
 }

if($save)
{
$i=0;
foreach($empcode as $a)
{
 $empcode[$i]=$a; 	
 $i++;
}
//********
$i=0;
foreach($att as $a)
{
 $att[$i]=$a; 	
 $i++;
}

//********
$i=0;
foreach($leav as $a)
{
 $leav[$i]=$a; 	
 $i++;
}

//********
$i=0;
foreach($ded as $a)
{
 $ded[$i]=$a; 	
 $i++;
}
$dfrom=$yea.'-'.$month.'-'.$fd;
$dto=$tyea.'-'.$tmonth.'-'.$td;
$tbl="paytmp".$brcode;
//mysqli_query($db,"drop table $tbl");
$sql="delete from $tbl where divi='$divi' and br='$br[0]' and month(dfrom)='$month' and year(dfrom)='$yea'";
//print $sql."<br>";
mysqli_query($db,$sql);
$sql="create table $tbl (empcode varchar(100), att int(5), leave double(14,2), ded double(14,2), dfrom date, dto date, wdays int(3), aleave int(2),br varchar(100),divi varchar(100))";
//print $sql;
//mysqli_query($db,$sql) or die(mysqli_error($db));
//mysqli_query($db,"delete from $tbl");
for($k=0;$k<$i;$k++)
{
$sql="insert into $tbl values('$empcode[$k]','$att[$k]','$leav[$k]','$ded[$k]','$dfrom','$dto','$wdays','$aleave','$br[$k]','$divi')";
 mysqli_query($db,$sql)or die(mysqli_error($db));
}
print "<b>Leave has been set";
//print "<script>
}
?>

<head>
<style>
body,td{font-family:'arial narrow';letter-spacing:1;font-size:13}
th{background-color:c1d799}
.mnu{margin-left:5}
body{BACKGROUND-COLOR:fcfff1}
input{text-align:right}
</style>
<script>
function valid()
{
if(document.f1.fd.value.length<=0)
 {
   alert('Invalid from date');
	document.f1.fd.focus();
   return false;
 }
if(document.f1.td.value.length<=0)
 {
   alert('Invalid to date');
	document.f1.td.focus();
   return false;
 }

if(document.f1.wdays.value.length<=0)
 {
   alert('Invalid working days');
   document.f1.wdays.focus();
   return false;
 }
}
</script>

</head>
<form action='<?print $PHP_SELF?>' name='f1' method='post' onSubmit="return valid()">
<fieldset>
<legend><b>Generate Payroll</legend>
<center>

<table>
<tr><td>Select Month </td><td> From <input type=text name='fd' size=2> <select name='month'>
<option value='1'>January</option>
<option value='2'>February</option>
<option value='3'>March</option>
<option value='4'>April</option>
<option value='5'>May</option>
<option value='6'>Jun</option>
<option value='7'>July</option>
<option value='8'>August</option>
<option value='9'>September</option>
<option value='10'>October</option>
<option value='11'>November</option>
<option value='12'>December</option>
</select>
<select name='yea'>
<option>2014</option>
<option>2015</option>
<option>2016</option>
<option>2013</option>
<option>2012</option>
<option>2011</option>
<option>2005</option>
<option>2006</option>
<option>2007</option>
<option>2008</option>
<option>2009</option>
<option>2010</option>

</select>
<br>
To&nbsp;&nbsp;&nbsp;&nbsp; <input type=text name='td' size=2> <select name='tmonth'>
<option value='1'>January</option>
<option value='2'>February</option>
<option value='3'>March</option>
<option value='4'>April</option>
<option value='5'>May</option>
<option value='6'>Jun</option>
<option value='7'>July</option>
<option value='8'>August</option>
<option value='9'>September</option>
<option value='10'>October</option>
<option value='11'>November</option>
<option value='12'>December</option>
</select>
<select name='tyea'>
<option>2014</option>
<option>2015</option>
<option>2016</option>
<option>2013</option>
<option>2012</option>
<option>2011</option>
<option>2005</option>
<option>2006</option>
<option>2007</option>
<option>2008</option>
<option>2009</option>
<option>2010</option>
</select>

</td></tr>
<tr><td>No of Attendance</td><td><input type=text name='wdays' size=2 onChange='setwd()' maxlength=2></td></tr>
<tr><td>Leave Allowed</td><td><input type=text name='aleave' size=2 value=2 maxlength=2></td></tr>
</table>
</fieldset>


<table border=1>
<th>No.<th>Name <th>Att.<th>Leave <th>Ded 
<?
$tbl="paytmp".$brcode;
$tbl1="payrolltmp".$brcode;
//$re=mysqli_query($db,"select distinct a.empcode,a.empname,a.br from empmaster a,emp_pay b where a.empcode=b.empcode and a.divi='$divi' and a.br='$br' and a.brcode='$brcode' and year(a.doconf)>0  group by a.empcode order by a.br,a.empname") or die("ERR :".mysqli_error($db));
//$sq="select distinct a.empcode,a.empname,a.br,year(a.doconf) from empmaster a,emp_pay b where a.empcode=b.empcode and a.divi='$divi' and a.br='$br' and a.brcode='$brcode' and a.apinpay!='n' and year(a.doret)<=0 and month(b.ondate)='$month' and year(b.ondate)='$yea' group by a.empcode order by a.br,a.empname";
//$sq="select distinct a.empcode,b.empname,a.att,a.leave,b.br,a.ded from $tbl a,empmaster b where a.empcode=b.empcode and month(a.dfrom)='$month' and b.divi='$divi' and b.br='$br' and b.brcode='$brcode' group by a.empcode";

$sq="select distinct a.empcode,a.att,a.leave,a.ded,a.br from $tbl a where month(a.dfrom)='$month' and year(a.dfrom)='$yea' and a.divi='$divi' and a.br='$br'";


$re=mysqli_query($db,$sq) or die("ERR :".mysqli_error($db));
$i=1;
$celres=mysqli_query($db,"select * from celmast where brcode='$brcode'");
$celrow=mysqli_fetch_array($celres);
while($row=mysqli_fetch_array($re))
{
 $enameres=mysqli_query($db,"select empname from empmaster where empcode='$row[empcode]' and brcode='$brcode'")or die(mysqli_error($db));
 $enamerow=mysqli_fetch_array($enameres);
$sq="select * from $tbl where divi='$divi' and br='$br' and month(dfrom)='$month' and year(dfrom)='$yea' and empcode='$row[empcode]'";
$tbl_res=mysqli_query($db,$sq);
$tbl_row=mysqli_fetch_array($tbl_res);
$calcpf=0;
$pf=0;
print "\n<tr><td>";
print "<input type=hidden name='empcode[]' value='$row[0]'> \n<input type=hidden name='br[]' value='$row[br]'><input type=hidden name='divi' value='$divi'>";
print $i."</td><td>&nbsp;".strtoupper($enamerow[0]).'-'.strtoupper($row[br])."</td>";
print "<td><input type=text id='att$i' name='att[]' size=2 value='$tbl_row[att]'></td>";
print "<td align=center><input type=text id='$i' name='leav[]' size=2 value='$tbl_row[leave]'></td>";
print "<td align=center><input type=text name='ded[]' size=4 value='$tbl_row[ded]'></td>";
$i++;
}
?>
</table>
<input type=submit value='Save' name='save'>
</form>

<script>
 function setwd()
 {
  for(k=1;k<<?print $i?>;k++)
  { var x="att"+k;
     document.getElementById(x).value=document.f1.wdays.value;
  }
 }
</script>

