<?
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");
$tbl="paytmp".$brcode;

if($del)
{
 mysqli_query($db,"delete from $tbl where br='$br' and divi='$divi'");
 print "<script> alert('Record has been deleted'); document.location='genpaymnu.php'; </script>";
 return;
}

$tbl1="payrolltmp".$brcode;

mysqli_query($db,"delete from $tbl1 where divi='$divi' and br='$br'")or die(mysqli_error($db));
$sql="create table $tbl1(empcode varchar(100) primary key,brcode varchar(100),ename varchar(100),design varchar(100),wgfrom date,wgto date,dayspaid int(5),wdays int(3),leave int(2),basicda double(14,2),ebasicda  double(14,2),hra double(14,2), ehra double(14,2),ta double(14,2),eta double(14,2),divi varchar(100), br varchar(100))";
mysqli_query($db,$sql);

$tbl2="allow_payrolltmp".$brcode;

mysqli_query($db,"delete from $tbl2 where divi='$divi' and br='$br'")or die(mysqli_error($db));
$sql="create table $tbl2(empcode varchar(100),brcode varchar(100),alloname varchar(100),alloamt double(14,2),ealloname varchar(100),ealloamt double(14,2),divi varchar(100), br varchar(100),wdatefrm date,wdateto date)";
mysqli_query($db,$sql) ;

$tbl3="ded_payrolltmp".$brcode;
mysqli_query($db,"delete from $tbl3 where divi='$divi' and br='$br'")or die("err".mysqli_error($db));
$sql="create table $tbl3(empcode varchar(100),brcode varchar(100),dedname varchar(100),dedamt double(14,2),pf double(14,2),esi double(14,2),otherded double(14,2),divi varchar(100), br varchar(100),wdatefrm date,wdateto date)";
mysqli_query($db,$sql);


$re=mysqli_query($db,"select * from branch where brcode='$brcode'") or die("er1:".mysqli_error($db));
$brow=mysqli_fetch_array($re);



$sq="select  distinct a.empcode, a.totsal,a.basic,a.bapf, a.baesi, a.da, a.dapf, a.daesi, a.ta,a.tapf, a.taesi, a.hra, a.hrapf, a.hraesi ,a.ondate, a.brcode from emp_pay a,empmaster b where b.empcode=a.empcode and b.divi='$divi' and b.br='$br' and a.brcode='$brcode' group by a.empcode order by a.ondate desc";
print $sq;
$emppayres=mysqli_query($db,$sq) or die("er2:".mysqli_error($db));
if(mysqli_num_rows($emppayres)<=0)
 {
   print "<script> alert('Record does not exist'); history.back(1); </script>";
 }
while($empayrow=mysqli_fetch_array($emppayres))
{
$ebasic=0;
$eda=0;
$ehra=0;
$eta=0;
$al_pf=0;
$pf=0;
$al_esi=0;
$esi=0;
$totforesi=0;
$totded=0;
$teall=0;


 $empres=mysqli_query($db,"select * from empmaster where empcode='$empayrow[empcode]'") or die("er3:".mysqli_error($db));
 $emprow=mysqli_fetch_array($empres);
 
 $allores=mysqli_query($db,"select * from emp_pay_allow where empcode='$empayrow[empcode]'") or die("er4:".mysqli_error($db));
 $allorow=mysqli_fetch_array($allores);

 $dedres=mysqli_query($db,"select * from emp_pay_ded where empcode='$empayrow[empcode]'") or die("er5:".mysqli_error($db));
 $dedow=mysqli_fetch_array($dedres);

?>
<center><span style={letter-spacing:5}>
<U><b>PAY SLIP</b></U>
</SPAN>
<table width=100% BORDER=0>
<?
$brname=explode(',',$branch);
?>
<tr><td align=center COLSPAN=2><span style={letter-spacing:3}><b><?print STRTOUPPER($brname[0])?></b> </span> <br>
<?Print $brow[braddr].''.$brow[brcity]?>
</td></tr>
</table>

<?
PRINT "<CENTER><TABLE BORDER=0 width=100%><TR><TD width=50% align=right>";
 PRINT "<TABLE BORDER=0 width=70%><TR><TD width=200>Name</td><td>".strtoupper($emprow[empname])."</td></tr>";
print "<TR><TD width=200>Designation</td><td>".strtoupper($emprow[design])."</td></tr>";


 $sq="select *,(dto-dfrom)Tdays from $tbl where empcode='$empayrow[empcode]' and divi='$divi'";

 $tmpres=mysqli_query($db,$sq)or die('Err 1:'.mysqli_error($db));
 $tmprow=mysqli_fetch_array($tmpres);

 print "<tr><td>Wages Period </td><td>From ".datetohtml($tmprow[dfrom])."<br>To &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".datetohtml($tmprow[5])."</td></tr>";

 $totbasc=$empayrow[basic]+$empayrow[da];


 if($tmprow[leave]>$tmprow[aleave])
  $leavetaken=($tmprow[leave]-$tmprow[aleave]);
 else 
  $leavetaken=0;
 $totdays=$tmprow[Tdays]+1;

$w=($tmprow[att]-$tmprow[leave]);
if($w==0)
{
$totdays=0;
}

if($leavetaken>0&&$totdays>0)
 {
  $ebasic=($empayrow[basic]/$totdays)*($totdays-$leavetaken);
  $eda=($empayrow[da]/$totdays)*($totdays-$leavetaken);
  $ehra=($empayrow[hra]/$totdays)*($totdays-$leavetaken);
  $eta=($empayrow[ta]/$totdays)*($totdays-$leavetaken);
 }

else if($totdays>0)
 {
  $ebasic=($empayrow[basic]);
  $eda=($empayrow[da]);
  $ehra=($empayrow[hra]);
  $eta=($empayrow[ta]);
  }



 $ddres=mysqli_query($db,"select to_days('$tmprow[dto]')-to_days('$tmprow[dfrom]') from $tbl") or die("er6:".mysqli_error($db));
 $ddrow=mysqli_fetch_row($ddres);
 $wdays=$ddrow[0]+1;
 if($tmprow[leave]>$tmprow[aleave])
  $wdays-=($tmprow[leave]-$tmprow[aleave]);
 print "<tr><td>No of Days paid </td><td>".$wdays."</td></tr>";

 print "<tr><td>No of working days </td><td>".($tmprow[att]-$tmprow[leave])."</td></tr>";
 $nwd=$tmprow[att]-$tmprow[leave];
 print "<tr><td>No of Leave </td><td>".$tmprow[leave]."</td></tr>";
 print "<tr><td>Basic + DA</td><td>".putcoma($totbasc)."</td></tr>";

 $toterned=$ebasic+$eda;
 print "<tr><td>Earned (Basic + DA) </td><td>".putComa($toterned)."</td></tr>";

 print "<tr><td>HRA </td><td>".putcoma($empayrow[hra])."</td></tr>";
 print "<tr><td>Earned HRA </td><td>".putComa($ehra)."</td></tr>";

 print "<tr><td>TA</td><td>".putcoma($empayrow[ta])."</td></tr>";
 print "<tr><td>Earned TA </td><td>".putComa($eta)."</td></tr>";
 $tots=$ebasic+$eda+$ehra+$eta+$teall;

 //print "<tr><td><b>&nbsp;&nbsp;Total (A)</td><td><b>".putComa($tots)."</td></tr>";

$sql="insert into $tbl1 values('$empayrow[empcode]','$brcode','$emprow[empname]','$emprow[design]','$tmprow[dfrom]','$tmprow[5]','$wdays','$nwd','$tmprow[leave]','$totbasc','$toterned','$empayrow[hra]','$ehra','$empayrow[ta]','$eta', '$divi','$br')";
mysqli_query($db,$sql)or die(mysqli_error($db));
//print $sql;


$allores1=mysqli_query($db,"select * from emp_pay_allow where empcode='$empayrow[empcode]'") or die("er7:".mysqli_error($db));
while($allorow1=mysqli_fetch_array($allores1))
{
if($leavetaken>0)
 { 
 $eall=($allorow1[amt]/$totdays)*($totdays-$leavetaken);
 }
else
{
  $eall=($allorow1[amt]);
}
$teall+=$eall;

if($allowrow1[pf]=='y')
$al_pf+=$allowrow1[amt];

if($allowrow1[esi]=='y')
$al_esi+=$allowrow1[amt];

 $tots=$ebasic+$eda+$ehra+$eta+$teall;
 print "<tr><td>$allorow1[allow]</td><td>".putComa($allorow1[amt])."</td></tr>";
 print "<tr><td>Earned $allorow1[allow]</td><td>".putComa($eall)."</td></tr>";
 $eallow_name="Earned".$allorow1[allow];

$sql="insert into $tbl2 values('$empayrow[empcode]','$brcode','$allorow1[allow]','$allorow1[amt]','$eallow_name','$eall','$divi','$br','$tmprow[dfrom]','$tmprow[5]')";
mysqli_query($db,$sql)or die(mysqli_error($db));
//print $sql;
  }



print "<tr><td><b>&nbsp;&nbsp;Total (A)</td><td><b>".putComa($tots)."</td></tr>";

print "</tr></td></table>"; 


print "</TD><TD valign=top width=50%>";

print "<CENTER><TABLE BORDER=0 width=70%>";
if($w>0)
{
print "<tr><td colspan=2><U><b>DEDUCTIONS</td></tr>";
$dedres1=mysqli_query($db,"select * from emp_pay_ded where empcode='$empayrow[empcode]'") or die("er8:".mysqli_error($db));
 
$celres=mysqli_query($db,"select * from celmast where brcode='$brcode'") or die("er9:".mysqli_error($db));
$celrow=mysqli_fetch_array($celres);
$totforpf=$ebasic+$eda+$al_pf;
if($empayrow[tapf]=='y')
$totforpf+=$eta;

//print "totpf".$totforpf;
//print "ciel".$celrow[pf];
if($totforpf<$celrow[pf])
 $pf=($totforpf*$celrow[pfp])/100;
else
 $pf=($celrow[pf]*$celrow[pfp])/100;

//********* esi
if($empayrow[taesi]=='y')
$totforesi+=$eta;
if($empayrow[hraesi]=='y')
$totforesi+=$ehra;
if($empayrow[baesi]=='y')
$totforesi+=$ebasic;
if($empayrow[daesi]=='y')
$totforesi+=$eda;

$totforesi+=$teall;

if($totforesi<=$celrow[esi])
 $esi=($totforesi*$celrow[esip])/100;
else
$esi=0;

while($dedow1=mysqli_fetch_array($dedres1))
{
 print "<tr><td>&nbsp;&nbsp;$dedow1[allow]</td><td>".putcoma($dedow1[amt])."</td></tr>";
 $totded+=$dedow1[amt];

$sql="insert into $tbl3 values('$empayrow[empcode]','$brcode','$dedow1[allow]','$dedow1[amt]','$pf','$esi','$tmprow[ded]','$divi','$br','$tmprow[dfrom]','$tmprow[5]')";
mysqli_query($db,$sql)or die(mysqli_error($db));
//print $sql;
}

print "<tr><td>&nbsp;&nbsp;PF</td><td>".putComa($pf)."</td></tr>";
print "<tr><td>&nbsp;&nbsp;ESI</td><td>".putComa($esi)."</td></tr>";
print "<tr><td>&nbsp;&nbsp;Other deduction</td><td>".putComa($tmprow[ded])."</td></tr>";
$totd=$totded+$pf+$esi+$tmprow[ded];
print "<tr><td><b>&nbsp;&nbsp;&nbsp; Total (B)</td><td><b>".putComa($totd)."</td></tr>";
print "<tr><td>&nbsp;</td><td></td></tr>";
/*print "<tr><td>&nbsp;</td><td></td></tr>";
print "<tr><td>&nbsp;</td><td></td></tr>";
print "<tr><td>&nbsp;</td><td></td></tr>";
print "<tr><td>&nbsp;</td><td></td></tr>";
print "<tr><td>&nbsp;</td><td></td></tr>";
print "<tr><td>&nbsp;</td><td></td></tr>";*/
print "<tr><td>&nbsp;</td><td></td></tr>";
}
$spay=($ebasic+$eda+$ehra+$eta+$teall)-($tmprow[ded]+$pf+$esi+$totded);

print "<tr><td valign=bottom><FONT SIZE=4><B>Salary Payable (A - B)</td><td><FONT SIZE=4><B>".putComa($spay)."</td></tr>";
print "</TABLE>"; 

print "<table width=100%>";
print "<tr height=100 valign=bottom align=center><td>Signature of Employee<br>Date :".datetohtml($tmprow[5])."</td><td>Autorised signatory<br>&nbsp;</td></tr>";
//print "<tr><td> </td></tr></table>";
print "</TD></TR></TABLE>";

print "</TD></TR>";

print "<TR><TD colspan=2><center>";
print "<hr><br>&nbsp;";

}


?>

<STYLE>
body,td{font-familY:'arial narrow';font-size:14}
</style>
