<?
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");
?>
<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
tr{background-color:e3ecd2}
th{background-color:c1d799}
.mnu{margin-left:5}
body{BACKGROUND-COLOR:fcfff1}
</style>
<?
if($repo)
{
?>
<center>
<hr color=orange>
<h5>ESI-INCENTIVE ENTRY FOR THE MONTH <?PRINT $month?> AND YEAR <?PRINT $yea?>
</H5>
<hr color=orange>
<?
 $sq="select * from esiinc where brcode='$brcode' and month(fordate)='$month' and year(fordate)='$yea' and br='$br' and divi='$divi'";
$res=mysqli_query($db,$sq) or die(mysqli_error());
 if(mysqli_num_rows($res)<=0)
 {
   print "Record does not exist";
   return;
  }
$i=1;
print "<table cellspacing=1 cellpadding=2 bgcolor='gray'> <th> No <th>Name <th>ESI <th>Inc <th>ESI on Inc <th>Total ESI <th>Branch <th>Division";
$tot=0;
while($ro=mysqli_fetch_array($res))
{
 print "<tr bgcolor='white'><td>".$i++."</td><td>$ro[0]</td>";
 $t=$ro[1]+$ro[3];
 $tot+=$t;
 print "<td align=right>$ro[1]</td><td align=right>$ro[2]</td><td align=right>$ro[3]</td>";
 print "<td align=right>".putComa($t)."</td>";
 print "<td>$ro[5]</td><td>$ro[6]</td>";
}
 print "<tr><td align=center colspan=5><b>Total</td><td align=right><b>".putComa($tot)."</td>";
 print "<td></td><td></td></tr>";
return;
}

if($save)
{
 $i=0;
 foreach($empname as $a)
  {$ename[$i]=$a;$i++;}
 $i=0;
 foreach($esi as $a)
  {$esi[$i]=$a;$i++;}
 $i=0;
 foreach($inc as $a)
  {$inc[$i]=$a;$i++;}
 $i=0;
 foreach($newesi as $a)
  {$newesi[$i]=$a;$i++;}

$entrydate=$y.'-'.$m.'-'.$d;
$fordate=$fory.'-'.$formonth.'-1';
mysqli_query($db,"delete from esiinc where brcode='$brcode' and fordate='$fordate' and br='$br' and divi='$divi'");

for($k=0;$k<$i;$k++)
{
 $sql="insert into esiinc values('$ename[$k]','$esi[$k]','$inc[$k]','$newesi[$k]','$brcode','$br','$divi','$entrydate','$fordate')";
 mysqli_query($db,$sql) or die(mysqli_error());
}
 print "Record has been saved";
return;
}
?>
<script>
 function calc(x)
 {
   var esi=x;
   var ince='inc'+x;
   var newes='newesi'+x;

    var esival=document.getElementById(esi).value;
    var incval=document.getElementById(ince).value;
    var tot=parseFloat(esival)+parseFloat(incval);
if(parseFloat(esival))=0
{
 print "<font color=red>Record does not exist</font>";
}
    var per=(parseFloat(incval)*1.75)/100;

  document.getElementById(newes).value=(parseFloat(per).toFixed(2));

 }
</script>
<?
$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;
 }
?>
<center>
<hr color=orange>
<h5>ESI-INCENTIVE ENTRY FOR THE MONTH <?PRINT $month?> AND YEAR <?PRINT $yea?>
</H5>
<hr color=orange>

<?
 $sq="select * from esiinc where brcode='$brcode' and month(fordate)='$month' and year(fordate)='$yea' and br='$br' and divi='$divi'";
$res23=mysqli_query($db,$sq) or die(mysqli_error());
 if(mysqli_num_rows($res23)>0)
 {
   print "<font color=red>Record already exists, Over writing</font><br>&nbsp;";
?>
 <script>
 if(!confirm('Do you want to over write records?'))
  document.location='esiin.php';
 </script>
<?
 }


$tbl3="ded_payrolltmp".$brcode;

$sql="select * from branch where brcode='$brcode'";
$brres=mysqli_query($db,$sql);
$brow=mysqli_fetch_array($brres);

$sq="select distinct empcode,esi from $tbl3 where month(wdatefrm)='$month' and year(wdatefrm)='$yea' and divi='$divi' and br='$br' and brcode='$brcode'  order by empcode ";

$emppayres=mysqli_query($db,$sq) or die("er2:".mysqli_error());
if(mysqli_num_rows($emppayres)<=0)
{
 print "<font color=red>Record does not exist</font>";
}

print "<form action='$PHP_SELF'>";
print "<input type=hidden name='br' value='$br'>";
print "<input type=hidden name='divi' value='$divi'>";
print "<input type=hidden name='formonth' value='$month'>";
print "<input type=hidden name='fory' value='$yea'>";

?>
Entry Date dd <input type=text name='d' size=2 value='<?print date('d')?>'>
mm <input type=text name='m' size=2 value='<?print date('m')?>'>
yy <input type=text name='y' size=4 value='<?print date('Y')?>'>

<?
print "<table cellspacing=1 cellpadding=2 bgcolor='gray'>";
print "<th>No <th>Name <th>ESI <th>Incentive <th>New ESI";
$i=1;
while($row=mysqli_fetch_array($emppayres))
{
 print "\n<tr bgcolor='white'><td>".$i++."</td>";
 print "<td><input type=text readonly name='empname[]' value='".getempname($row[empcode])."' size=40 style='border:none;background-color:e3ecd2'></td>";
 print "\n<td><input type=text name='esi[]' readonly id='$i' size=6 value='$row[1]'></td>";
 print "\n<td><input type=text name='inc[]' id='inc$i' size=6 onChange='calc($i)'></td>";
 print "\n<td><input type=text name='newesi[]' readonly id='newesi$i' size=6></td>";
}
print "<tr><th colspan=6><input type=submit value='Save' name='save'></th></tr></table></form>";
function getempname($x)
{
 $ere=mysqli_query($db,"select empname from empmaster where empcode='$x'")or die(mysqli_error());
 $erow=mysqli_fetch_row($ere);
 return $erow[0];
}
?>
