<?
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());
//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());
}
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;
 }
}
function setmonth()
{
var x=document.getElementById("month")

var y=document.getElementById("tmonth")
document.getElementById("tmonth").length=1
y.options[y.selectedIndex].text=x.options[x.selectedIndex].text

}
</script>

</head>
<fieldset>
<legend><b>Generate Payroll</legend>
<center>
<form name='f1' action='<?print $PHP_SELF?>' onSubmit="return valid()" method='post'>
<table>
<tr><td>Select Month </td><td> From <input type=text name='fd' size=2> <select id='month' 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>2008</option>
<option>2009</option>
<option>2010</option>
<option>2011</option>
<option>2012</option>
<option>2013</option>
</select>
<br>
To&nbsp;&nbsp;&nbsp;&nbsp; <input type=text name='td' size=2> <select id='tmonth' 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>2008</option>
<option>2009</option>
<option>2010</option>
<option>2011</option>
<option>2012</option>
<option>2013</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;
//$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());
$sq="select distinct a.empcode,a.empname,a.br,year(a.doconf) from empmaster a,emp_pay b where a.emtype='normal' and 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 group by a.empcode order by a.br,a.empname";
//print $sq;
$re=mysqli_query($db,$sq) or die("ERR :".mysqli_error());
$i=1;
$celres=mysqli_query($db,"select * from celmast where brcode='$brcode'");
$celrow=mysqli_fetch_array($celres);
while($row=mysqli_fetch_array($re))
{
$sq="select * from $tbl where divi='$divi' and br='$br' 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>";
if($row[3]>0)
 print "<font color=red><b>*</b></font>";
else
 print "&nbsp;";
print "<input type=hidden name='empcode[]' value='$row[0]'> <input type=hidden name='br[]' value='$row[br]'><input type=hidden name='divi' value='$divi'>";
print $i."</td><td>&nbsp;".strtoupper($row[1]).'-'.strtoupper($row[2])."</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>

