<?
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 secbrpass 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;
 }


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

$tbl1="payrolltmpsec".$brcode;
$sql="delete from $tbl1 where divi='$divi' and br='$br' and month(wgto)='$month' and year(wgto)='$yea'";


mysqli_query($db,$sql)or die('err'.mysqli_error());
$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),rent double(14,2))";
mysqli_query($db,$sql);

$tbl2="allow_payrolltmpsec".$brcode;
$sq="delete from $tbl2 where divi='$divi' and br='$br' and month(wdatefrm)='$month' and year(wdatefrm)='$yea' and month(wdateto)='$month' and year(wdateto)='$yea'";

mysqli_query($db,$sq) or die('delall err'.mysqli_error());

$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_payrolltmpsec".$brcode;
$sq="delete from $tbl3 where divi='$divi' and br='$br' and month(wdateto)='$month' and year(wdateto)='$yea'";

mysqli_query($db,$sq)or die('del ded '.mysqli_error());

$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,ptaxamt double(12,2))";
mysqli_query($db,$sql);

$sq="select * from br_master where brname='$br'";

$re=mysqli_query($db,$sq) or die("er1:".mysqli_error());
$brow=mysqli_fetch_array($re) or die("er1:".mysqli_error());



$sq="select distinct * from $tbl where divi='$divi' and br='$br' and month(dto)='$month' and year(dto)='$yea' order by empcode, dfrom desc";

 $res_tmp=mysqli_query($db,$sq)or die('Err divi'.mysqli_error());
$gdate="$yea-$month-30";
while($row_tmp=mysqli_fetch_array($res_tmp))
{
 $sq="select distinct * from emp_pay where empcode='$row_tmp[empcode]' and ondate<='$gdate' order by ondate desc,empcode limit 1";

$emppayres=mysqli_query($db,$sq) or die("er2:".mysqli_error());
if(mysqli_num_rows($emppayres)>0)
 {
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;
$totd=0;
$teall=0;
$tots=0;
//$rentfortot=$empayrow[rent];

 $empres=mysqli_query($db,"select distinct * from empmaster where empcode='$empayrow[empcode]'") or die("er3:".mysqli_error());
 $emprow=mysqli_fetch_array($empres);
 $trainee=$emprow[trainee];
 $ptax=$emprow[ptax];
 $allores=mysqli_query($db,"select ondata from emp_pay_allow where ondata<='$gdate'  and  empcode='$empayrow[empcode]'order by ondata desc limit 1") or die("er4:".mysqli_error());
 $allorowdt=mysqli_fetch_array($allores);

 $dedres=mysqli_query($db,"select * from emp_pay_ded where empcode='$empayrow[empcode]' and ondata<='$gdate' order by ondata desc limit 1") or die("er5:".mysqli_error());
 $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[addr1].''.$brow[addr2].''.$brow[city]?>
</td></tr>
</table>

<?
PRINT "<CENTER><TABLE BORDER=1 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' and br='$br' and month(dto)='$month' and year(dto)='$yea' order by dto desc limit 1";
//print '<br>'.$sq;
 $tmpres=mysqli_query($db,$sq)or die('Err 1:'.mysqli_error());
 $tmprow=mysqli_fetch_array($tmpres);
if($trainee=='No')
 print "<tr><td>Wages Period </td><td>From ".datetohtml($tmprow[dfrom])."<br>To &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".datetohtml($tmprow[5])."</td></tr>";
else
 print "<tr><td>Stipend 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[leav]>$tmprow[aleave])
  $leavetaken=($tmprow[leav]-$tmprow[aleave]);
 else 
  $leavetaken=0;
 $totdays=$tmprow[Tdays]+1;

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


if($emprow[lop]!='y')
{
 $leavetaken=0;
$w=$tmprow[att]; 
}

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());
 $ddrow=mysqli_fetch_row($ddres);
 $wdays=$ddrow[0]+1;
 if($tmprow[leav]>$tmprow[aleave])
  $wdays-=($tmprow[leav]-$tmprow[aleave]);
 print "<tr><td>No of Days </td><td>".$totdays."</td></tr>";
 print "<tr><td>No of working days </td><td>".$tmprow[att]."</td></tr>";
 print "<tr><td>No of days present</td><td>".($tmprow[att]-$tmprow[leav])."</td></tr>";
 print "<tr><td>No of Days paid </td><td>".$wdays."</td></tr>";


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

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

 print "<tr><td>CCA </td><td>".putcoma($empayrow[hra])."</td></tr>";
//**************************
$calcforesi+=$empayrow[hra];
//**************************

 print "<tr><td>Earned CCA </td><td>".putComa($ehra)."</td></tr>";

 print "<tr><td>TA</td><td>".putcoma($empayrow[ta])."</td></tr>";
//**************************
$calcforesi+=$empayrow[ta];
//**************************

 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','$empayrow[rent]')";
mysqli_query($db,$sql)or die('ins err:'.$tbl1.mysqli_error());


$q="select distinct allow,amt from emp_pay_allow where empcode='$empayrow[empcode]' and ondata='$allorowdt[0]' order by ondata desc";

$allores1=mysqli_query($db,$q) or die("er7:".mysqli_error());
while($allorow1=mysqli_fetch_array($allores1))
{
$p="select pf,esi,lop from allomast where allname='$allorow1[allow]'";

$all_res=mysqli_query($db,$p)or die('alll Error'.mysqli_error());
$allowrow111=mysqli_fetch_array($all_res);


if($leavetaken>0 &&$allowrow111[lop]=='y')
 { 
 $eall=($allorow1[amt]/$totdays)*($totdays-$leavetaken);
 }
else 
{
  $eall=($allorow1[amt]);
}
$teall+=$eall;
//******************************************* 

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

if($allowrow1[esi]=='y')
{
// check ceiling
$al_esi+=$allorow1[amt];
}
//**********************************************88888888
 $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('tbl2 err:'.mysqli_error());
//print $sql;
  }

//print "<tr><td>Rent</td><td><b>".putComa($empayrow[rent])."</td></tr>";
//$tots+=$empayrow[rent];
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>";

$celres=mysqli_query($db,"select * from celmast where brcode='$brcode'") or die("er9:".mysqli_error());
$celrow=mysqli_fetch_array($celres);


if($empayrow[bapf]=='y')
$totforpf=$ebasic+$eda+$al_pf;
else
$totforpf=0;



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($empayrow[rentesi]=='y')
$totforesi+=$empayrow[rent];
*/


//if($totforesi<=$celrow[esi])
if($tots<=$celrow[esi])
 {
 $sq="select * from $tbl2 where empcode='$empayrow[empcode]' and month(wdatefrm)='$month' and year(wdatefrm)='$yea' order by wdatefrm desc";

$a1=mysqli_query($db,$sq)or die('err'.mysqli_error());
while($a1row=mysqli_fetch_array($a1))
{
  $a2=mysqli_query($db,"select * from allomast where allname='$a1row[alloname]' and brcode='$brcode'")or die(mysqli_error());
  $a2row=mysqli_fetch_array($a2);
  if($a2row[esi]=='')
    {$totforesi-=$a1row[ealloamt];

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

//$esi=ceil($esi);
$esi=$esi;
//$pf=ceil($pf);
$dedres1=mysqli_query($db,"select * from emp_pay_ded where empcode='$empayrow[empcode]' and ondata<='$gdate' order by ondata desc limit 1") or die("er8:".mysqli_error());
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]','0')";
mysqli_query($db,$sql)or die(mysqli_error());
//print $sql;*/
}
if($brcode=='AUT009')
{
$pf=round($pf);
$esi=ceil($esi);

}

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>";

if($ptax=='Yes')
{
 if($tots>=5000 && $tots<8000)
   $ptaxamt=0;
 else if($tots>=8000 && $tots<10000)
   $ptaxamt=0;
 else if($tots>=10000 && $tots<15000)
   $ptaxamt=150;
 else if($tots>=15000)
   $ptaxamt=200;
}// end if ptax
if($ptaxamt>0)
{
print "<tr><td>&nbsp;&nbsp;Professional Tax</td><td>".putComa($ptaxamt)."</td></tr>";
}
$sql="insert into $tbl3 values('$empayrow[empcode]','$brcode','$dedow1[allow]','$dedow1[amt]','$pf','$esi','$tmprow[ded]','$divi','$br','$tmprow[dfrom]','$tmprow[5]','$ptaxamt')";
mysqli_query($db,$sql)or die('tbl3 err:'.mysqli_error());


$totd=$totded+$pf+$esi+$tmprow[ded]+$ptaxamt;


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>";
$tots=0;
}
} // end while tmp
$spay=($ebasic+$eda+$ehra+$eta+$teall)-($tmprow[ded]+$pf+$esi+$totded+$ptaxamt);
//$spay+=$rentfortot;
$spay=ceil($spay);
if($trainee=='No')
print "<tr><td valign=bottom><FONT SIZE=3><B>Salary Payable (A - B)</td><td><FONT SIZE=3><B>".putComa($spay)."</td></tr>";
else
print "<tr><td valign=bottom><FONT SIZE=3><B>Stipend Payable (A - B)</td><td><FONT SIZE=3><B>".putComa($spay)."</td></tr>";

print "</TABLE>"; 

print "<table width=100%>";
if($trainee=='No')
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>";
else
print "<tr height=100 valign=bottom align=center><td>Signature of Apprentice<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;</td></tr></table>";
} // end if tmp
$ptaxamt=0;
}


?>

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