<?
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>Overtime<th>ESI on Inc&OT <th>Total ESI <th>Mob.Bill <th>Mob.All <th>Mob.Ded <th>Advance <th>TDS <th> Prof.Tax <th>Kshemanidhi <th>Mediclaim <th>Other Ded<th>Laptop ";
$tot=0;
$r1=0;
$r2=0;
$r3=0;
$r4=0;
$r5=0;
$r6=0;
$r7=0;
$r8=0;
$r9=0;
$r10=0;
$r11=0;
$r12=0;
$r13=0;
$r14=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[9]</td><td align=right>$ro[3]</td>";
 print "<td align=right>".putComa($t)."</td>";
 $ro10 = $ro[10];
 print "<td align=right>$ro[10]</td><td align=right>$ro[11]</td><td align=right>$ro[12]</td><td align=right>$ro[13]</td><td align=right>$ro[14]</td><td align=right>$ro[15]</td><td align=right>$ro[16]</td><td align=right>$ro[17]</td><td align=right>$ro[18]</td align=right><td align=right>$ro[19]</td>";
 $r1+=$ro[1];
 $r2+=$ro[2];
 $r3+=$ro[3];
 $r4+=$ro[9];
 $r5+=$ro[10];
 $r6+=$ro[11];
 $r7+=$ro[12];
 $r8+=$ro[13];
 $r9+=$ro[14];
 $r10+=$ro[15];
 $r11+=$ro[16];
 $r12+=$ro[17];
 $r13+=$ro[18];
 $r14+=$ro[19];
}
 print "<tr><td align=center colspan=2><b>Total</td>";
 print "<td align=right><b>".putComa($r1)."</td>";
 print "<td align=right><b>".putComa($r2)."</td>";
 print "<td align=right><b>".putComa($r4)."</td>";
 print "<td align=right><b>".putComa($r3)."</td>";
 print "<td align=right><b>".putComa($tot)."</td>";
 print "<td align=right><b>".putComa($r5)."</td>";
 print "<td align=right><b>".putComa($r6)."</td>";
 print "<td align=right><b>".putComa($r7)."</td>";
 print "<td align=right><b>".putComa($r8)."</td>";
 print "<td align=right><b>".putComa($r9)."</td>";
 print "<td align=right><b>".putComa($r10)."</td>";
 print "<td align=right><b>".putComa($r11)."</td>";
 print "<td align=right><b>".putComa($r12)."</td>";
 print "<td align=right><b>".putComa($r13)."</td>";
 print "<td align=right><b>".putComa($r14)."</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($ot as $a)
  {$ot[$i]=$a;$i++;}
 $i=0;
 foreach($sal_inc as $a)
  {$sal_inc[$i]=$a;$i++;}
 $i=0;
 foreach($newesi as $a)
  {$newesi[$i]=$a;$i++;}

 $i=0;
 foreach($mob_bill as $a)
  {$mob_bill[$i]=$a;$i++;}
 $i=0;
 foreach($mob_dif as $a)
  {$mob_dif[$i]=$a;$i++;}
 $i=0;
 foreach($advance as $a)
  {$advance[$i]=$a;$i++;}
 $i=0;
 foreach($tds as $a)
  {$tds[$i]=$a;$i++;}
 $i=0;
 foreach($prof_tax as $a)
  {$prof_tax[$i]=$a;$i++;}
 $i=0;
 foreach($kshemanidhi as $a)
  {$kshemanidhi[$i]=$a;$i++;}
 $i=0;
 foreach($mediclaim as $a)
  {$mediclaim[$i]=$a;$i++;}
 $i=0;
 foreach($other_ded as $a)
  {$other_ded[$i]=$a;$i++;}
 $i=0;
 foreach($laptop as $a)
  {$laptop[$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++)
{
 $mob_ded=0;
 $mob_all=0;
 if ($mob_dif[$k]>=0)
	$mob_all = ($mob_dif[$k]);
 else
	$mob_ded = ($mob_dif[$k])*-1;
 $sql="insert into esiinc values('$ename[$k]','$esi[$k]','$inc[$k]','$newesi[$k]','$brcode','$br','$divi','$entrydate','$fordate','$ot[$k]','$mob_bill[$k]','$mob_all','$mob_ded','$advance[$k]','$tds[$k]','$prof_tax[$k]','$kshemanidhi[$k]','$mediclaim[$k]','$other_ded[$k]','$laptop[$k]','$sal_inc[$k]')";
 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 ot1='ot'+x;
    var sal_inc1='sal_inc'+x;

    var esival=document.getElementById(esi).value;
    var incval=document.getElementById(ince).value;
    var otval=document.getElementById(ot1).value;
    var sal_incval=document.getElementById(sal_inc1).value;
   
    var tot=parseFloat(esival)+parseFloat(incval)+parseFloat(otval)+parseFloat(sal_incval);
    if(parseFloat(esival)>0)
	{
		$incot=0;
		if(isNaN(parseFloat(incval))) 
			$incot=0;
		else
			$incot = parseFloat(incval);
		if(isNaN(parseFloat(otval)))
			$incot = $incot;
		else
			$incot += parseFloat(otval);
		if(isNaN(parseFloat(sal_incval)))
			$incot = $incot;
		else
			$incot += parseFloat(sal_incval);

	    	var per=(parseFloat($incot)*1.75)/100;
	}
    else
      	var per=0;
    document.getElementById(newes).value=(Math.ceil(parseFloat(per)));
 }
</script>
<script>
 function calc1(x)
 {
    var mob_all='mob_all'+x;
    var mob_bill1='mob_bill'+x;
    var mob_dif1='mob_dif'+x;
    var mob_all2=document.getElementById(mob_all).value;
    var mob_bill2=document.getElementById(mob_bill1).value;
<?
if ($brcode =='AUT009' || $brcode =='AUT103')
{
?>
    	var tot=parseFloat(mob_all2)-parseFloat(mob_bill2);
	if(tot<0)
	{
	    	var tot=parseFloat(mob_all2)-parseFloat(mob_bill2);
	}
	else
	{
	    	var tot=0;
	}

<?
}
else
{
?>
    	var tot=parseFloat(mob_all2)-parseFloat(mob_bill2);
<?
}
?>
    document.getElementById(mob_dif1).value=(Math.ceil(parseFloat(tot)));
 }
</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());
$medit = 0;
 if(mysqli_num_rows($res23)>0)
 {
   print "<font color=red>Record already exists, Over writing</font><br>&nbsp;";
	$medit = 1;
?>
 <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' method='post'>";
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>Overtime <th>Sales Inc.<th>New ESI<th>Mob.Allo<th>Mob.Bill<th>Mob.Diff<th>Advance<th>TDS<th>Prof.Tax<th>Kshemanidhi<th>Mediclaim<th>Others<th>Laptop";
$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=25 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)' value='".getesiinc($row[empcode],1,$month,$yea)."'></td>";
	 print "\n<td><input type=text name='ot[]' id='ot$i' size=6 onChange='calc($i)' value='".getesiinc($row[empcode],2,$month,$yea)."'></td>";
	 print "\n<td><input type=text name='sal_inc[]' id='sal_inc$i' size=6 onChange='calc($i)' value='".getesiinc($row[empcode],12,$month,$yea)."'></td>";
	 print "\n<td><input type=text name='newesi[]' readonly id='newesi$i' value='".getesiinc($row[empcode],3,$month,$yea)."' size=6></td>";
	
	 print "\n<td><input type=text readonly name='mob_all[]' id='mob_all$i' value='".getmob($row[empcode])."' size=6 style='border:none;background-color:e3ecd2'></td>";
	 if ($medit==1)
		{
		 print "\n<td><input type=text name='mob_bill[]' id='mob_bill$i' size=6 onChange='calc1($i)' value='".getesiinc($row[empcode],4,$month,$yea)."'></td>";
		 print "\n<td><input type=text name='mob_dif[]' readonly id='mob_dif$i'  value='".getesiinc($row[empcode],5,$month,$yea)."' size=6></td>";
	} 
	else
	{
	 print "\n<td><input type=text name='mob_bill[]' id='mob_bill$i' value=0 size=6 onChange='calc1($i)'></td>";
	 print "\n<td><input type=text name='mob_dif[]' readonly id='mob_dif$i' value='".getmob($row[empcode])."' size=6></td>";
}
 print "\n<td><input type=text name='advance[]' id='advance$i' value='".getesiinc($row[empcode],6,$month,$yea)."' size=6></td>";
 print "\n<td><input type=text name='tds[]' id='tds$i' value='".getesiinc($row[empcode],7,$month,$yea)."' size=4></td>";
 print "\n<td><input type=text name='prof_tax[]' id='prof_tax$i' value='".getesiinc($row[empcode],8,$month,$yea)."' size=4></td>";
 print "\n<td><input type=text name='kshemanidhi[]' id='kshemanidhi$i' value='".getesiinc($row[empcode],9,$month,$yea)."' size=4></td>";
 print "\n<td><input type=text name='mediclaim[]' id='mediclaim$i' value='".getesiinc($row[empcode],10,$month,$yea)."' size=4></td>";
 print "\n<td><input type=text name='other_ded[]' id='other_ded$i' value='".getesiinc($row[empcode],11,$month,$yea)."' size=4></td>";
 print "\n<td><input type=text readonly name='laptop[]' id='laptop$i' value='".getlaptop($row[empcode])."' size=6 style='border:none;background-color:e3ecd2'></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];
}
function getmob($x)
{
 $ere=mysqli_query($db,"select mob from emp_pay where empcode = '$x' order by ondate desc")or die(mysqli_error());
 $erow=mysqli_fetch_row($ere);
 return $erow[0];
}
function getlaptop($x)
{
 $ere=mysqli_query($db,"select amt from emp_pay_ded where allow = 'laptop' and empcode = '$x' order by ondata desc")or die(mysqli_error());
 $erow=mysqli_fetch_row($ere);
 return $erow[0];
}
function getesiinc($x,$no,$month,$yea)
{
 $ere=mysqli_query($db,"select inc,ot,newesi,mob_bill,mob_all,mob_ded,advance,tds,prof_tax,kshemanidhi,mediclaim,other_ded,sales_inc from esiinc,empmaster where ename = empname and month(fordate)='$month' and year(fordate)='$yea' and empmaster.empcode = '$x'")or die(mysqli_error());
 $erow=mysqli_fetch_row($ere);
 if($no==1)
   return $erow[0];
 if($no==2)
   return $erow[1];
 if($no==3)
   return $erow[2];
 if($no==4)
   return $erow[3];
 if($no==5)
 {
    if($erow[4]>0)
	return $erow[4];
    else
       if($erow[5]>0)
	   return $erow[5]*-1;
 }
 if($no==6)
   return $erow[6];
 if($no==7)
   return $erow[7];
 if($no==8)
   return $erow[8];
 if($no==9)
   return $erow[9];
 if($no==10)
   return $erow[10];
 if($no==11)
   return $erow[11];
 if($no==12)
   return $erow[12];

}
?>
