<?
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'");
print"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;
 }


$tbl="paytmp".$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='genpaymnu.php'; </script>";
 return;
}

$tbl1="payrolltmp".$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($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),rent double(14,2))";
mysqli_query($db,$sql);

$tbl2="allow_payrolltmp".$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($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;
$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($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,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($db));
$brow=mysqli_fetch_array($re) or die("er1:".mysqli_error($db));



$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($db));
$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($db));
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;
$tots1=0;
$newtea=0;
$eall=0;
//$rentfortot=$empayrow[rent];

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

  
$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($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[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($db));
 $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[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($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]);
  }

$eta=ceil($eta);
$ehra=ceil($ehra);


 $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 </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[leave])."</td></tr>";
 print "<tr><td>No of Days paid </td><td>".$wdays."</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>";
//**************************
$calcforesi=$totbasc;
//**************************

 $toterned=ceil($ebasic+$eda);
 print "<tr><td>Earned (Basic + DA) </td><td>".putComa(ceil($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(ceil($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(ceil($eta))."</td></tr>";
 
 $tots=$ebasic+$eda+$ehra+$eta+$teall;
//$tots=$ebasic+$eda+$eta+$teall;

 //print "<tr><td><b>&nbsp;&nbsp;Total (A)</td><td><b>".putComa(ceil($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($db));


$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($db));
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($db));
	$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[esi]!='y')
	{
		$newtea=$teall;
		// check ceiling
		$newtea-=$allorow1[amt];
	}
//******************************************* 
	if($allowrow1[pf]=='y')
		$al_pf+=$allorow1[amt];
	if($allowrow1[esi]=='y')
	{
		// check ceiling
		$al_esi+=$allorow1[amt];
	}
//**********************************************88888888
	if ($newtea<0) 
	{
		$newtea=0;
	}
	$tots=$ebasic+$eda+$ehra+$eta+$teall;
	//$tots=$ebasic+$eda+$eta+$teall;
	$tots1=$ebasic+$eda+$ehra+$eta+$newtea;
	// print "<tr><td>$tots</td></tr>";
	if ($allorow1[allow]=='RENT.'||$allorow1[allow]=='RENT')
	{
//		print "<tr><td>HRA</td><td>".putComa($allorow1[amt])."</td></tr>";
		print "<tr><td>HRA</td><td>".putComa($eall)."</td></tr>";
	}
	else
	{
		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($db));
	//print $sql;
}

//$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";
$sq="select * from esiinc where month(fordate)='$month' and year(fordate)='$yea' and divi='$divi' and br='$br' and brcode='$brcode' and ename='$emprow[empname]'";
$mesi_inc=mysqli_query($db,$sq) or die("er2:".mysqli_error($db));
$mesi_inc1=mysqli_fetch_array($mesi_inc);

print "<tr><td>Incentive</td><td>".putComa($mesi_inc1[inc])."</td></tr>";
$mallow = $mesi_inc1[ot]+$mesi_inc1[mob_all];
print "<tr><td>Allowances</td><td>".putComa($mallow)."</td></tr>";
//print "<tr><td>Rent</td><td><b>".putComa($empayrow[rent])."</td></tr>";
//$tots+=$empayrow[rent];
$tots=$tots+$mallow+$mesi_inc1[inc];
print "<tr><td><b>&nbsp;&nbsp;Total (A)</td><td><b>".putComa(ceil($tots))."</td></tr>";
$tot_pay = $tots;
$tots1=$ebasic+$eda+$ehra+$eta+$newtea;
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><b>PF No.:  ".$emprow[pf]."</td></tr>";
	print "<tr><td colspan=2><b>ESI No.:  ".$emprow[esi]."</td></tr>";
	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($db));
	$celrow=mysqli_fetch_array($celres);
	$totforpf=$ebasic+$eda+$al_pf+$ehra+$eta;
	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;
	$pf = ceil($pf);
//********* 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($tots1<=$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($db));
		while($a1row=mysqli_fetch_array($a1))
		{
			$a2=mysqli_query($db,"select * from allomast where allname='$a1row[alloname]' and brcode='$brcode'")or die(mysqli_error($db));
			$a2row=mysqli_fetch_array($a2);
			if($a2row[esi]=='')
    			{
				$totforesi-=$a1row[ealloamt];
			}
		}
	if($totforesi>=100)
	{
		$esi=($totforesi*$celrow[esip])/100;  
	}
}
else
$esi=0;
$esi=ceil($esi);
//$esi=$esi+$mesi_inc1[newesi];
//$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($db));
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($db));
//print $sql;*/
}
if($brcode=='AUT009' || $brcode=='AUT103')
{
	$pf=round($pf);
	$esi=ceil($esi);
}
$mtots = $tots-$eall-$mesi_inc1[inc];
print "<tr><td>&nbsp;&nbsp;PF</td><td>".putComa($pf)."</td></tr>";
print "<tr><td>&nbsp;&nbsp;ESI</td><td>".putComa($esi)."</td></tr>";
if($ptax=='Yes')
{
	if($mtots>=5000 && $mtots<8000)
		$ptaxamt=0;
	else if($mtots>=8000 && $mtots<10000)
   		$ptaxamt=0;
	else if($mtots>=10000 && $mtots<15000)
   		$ptaxamt=150;
	else if($mtots>=15000)
   	$ptaxamt=200;
}// end if ptax
if($ptaxamt>0)
{
	print "<tr><td>&nbsp;&nbsp;Professional Tax</td><td>".putComa($ptaxamt)."</td></tr>";
}
if($mesi_inc1[prof_tax]>0)
{
	print "<tr><td>&nbsp;&nbsp;Professional Tax</td><td>".putComa($mesi_inc1[prof_tax])."</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($db));



$madvances=$tmprow[ded]+$mesi_inc1[mob_ded]+$mesi_inc1[advance]+$mesi_inc1[tds]+$mesi_inc1[kshemanidhi]+$mesi_inc1[mediclaim]+$mesi_inc1[other_ded]+$mesi_inc1[laptop];
$totd=$totded+$pf+$esi+$ptaxamt+$madvances+$mesi_inc1[prof_tax];
print "<tr><td>&nbsp;&nbsp;Advances</td><td>".putComa($madvances)."</td></tr>";

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=$tot_pay-$totd;
//$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>
