<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}
if($pswd!='wage')
{
 print "<script>alert('Invalid Password'); </script>";
 return;
}
include("../opendb.php");
include("../test.php");
$entrydate=$eny.'-'.$enm.'-'.$end;
?>
<style>
tr,td{background-color:white}
</style>

<?
$tbl1="payrolltmp".$brcode;
$tbl2="allow_payrolltmp".$brcode;
$tbl3="ded_payrolltmp".$brcode;

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

$sq="select * from $tbl1 where month(wgfrom)='$monthfrm' and year(wgfrom)='$yearfrm' and divi='$divi' and br='$br' and brcode='$brcode' order by empcode";
$emppayres=mysqli_query($db,$sq) or die("er2:".mysqli_error());
$emppayres1=mysqli_query($db,$sq) or die("er2:".mysqli_error());

$sq="select * from $tbl2 where month(wdatefrm)='$monthfrm' and year(wdatefrm)='$yearfrm' and divi='$divi' and br='$br' and brcode='$brcode'";
$allow_res=mysqli_query($db,$sq) or die("er2:".mysqli_error());
$allow_res1=mysqli_query($db,$sq) or die("er2:".mysqli_error());
$allow_res2=mysqli_query($db,$sq) or die("er2:".mysqli_error());

$empayrow1=mysqli_fetch_array($emppayres1);
$sq="select * from $tbl3 where month(wdatefrm)='$monthfrm' and year(wdatefrm)='$yearfrm' and divi='$divi' and br='$br' and brcode='$brcode' and empcode='$empayrow1[empcode]'";

$ded_res=mysqli_query($db,$sq) or die("er2:".mysqli_error());
$ded_res1=mysqli_query($db,$sq);
$ded_res2=mysqli_query($db,$sq);

if(mysqli_num_rows($emppayres)<=0)
 {
   print "<script> alert('Record does not exist'); history.back(1); </script>";
   return;
 }
?>

<center><span style={letter-spacing:5}>
<U><b>WAGE REGISTER</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]
?>
<br>
Wage register from :<?print $monthfrm.'-'.$yearfrm?> to <?print $monthto.'-'.$yearto?>
</td></tr>
</table>

<CENTER><TABLE BORDER=0 cellspacing=1 cellpadding=4 bgcolor='silver' width=100%>
<tr><th>No<th>Name of Employee<th>Total Att.<th>No.of Days Paid<th>Total Salary (Basic+DA)<th>Earned (Basic+DA)<th>T.A <th>Earned <br>TA<th>CCA <th>E.CCA<th>HRA <th>Total
<?
while($allorow1=mysqli_fetch_array($allow_res2))
 {
 print "<th>$allorow1[alloname]";
 }
?>
<th colspan=3>Deduction
<th>Total Deduction<th>Net Salary Paid</tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<?
while($allorow2=mysqli_fetch_array($allow_res))
 {
 print "<td>$allorow2[2]</td>";
 }

?>
<td></td><td></td><td></td><td align=center>PF<td align=center>ESI<td align=center>Adv/<br>other</td>
<?
$i=1;
$totebasicda=0;
$totbasicda=0;
$totta=0;
$toteta=0;
$tothra=0;
$totehra=0;
$totnet=0;
$totpf=0;
$totesi=0;
$ttotded=0;
$gtotded=0;
$tottilhra=0;
while($empayrow=mysqli_fetch_array($emppayres))
{


$jvres=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
$jvrow=mysqli_fetch_array($jvres);
$jvno=($jvrow[0]+1);


$totded=0;
$totfornet=0;
$tottilhra=0;
print "\n<tr><td>".$i++."</td><td>".strtoupper($empayrow[ename])."</td><td align=center>$empayrow[wdays]</td>";
print "<td  align=center>$empayrow[dayspaid]</td><td align=right>$empayrow[basicda]</td><td align=right>$empayrow[ebasicda]</td><td align=right>$empayrow[ta]</td><td align=right>$empayrow[eta]</td><td align=right>$empayrow[hra]</td><td align=right>$empayrow[ehra]</td>";
//print "<td align=right>$empayrow[rent]</td>";
$totebasicda+=$empayrow[ebasicda];
$totbasicda+=$empayrow[basicda];
$totta+=$empayrow[ta];
$toteta+=$empayrow[eta];
$tothra+=$empayrow[hra];
$totehra+=$empayrow[ehra];
//$totrent+=$empayrow[rent];
//$empayrow[basicda]+
$tottilhra=($empayrow[ebasicda]+$empayrow[eta]+$empayrow[ehra]); //+$empayrow[rent]);
$gtottilhra+=$tottilhra;

$totfornet=($empayrow[ebasicda]+$empayrow[eta]+$empayrow[ehra]); //+$empayrow[rent]);
print "<td align=right>".putComa($tottilhra)."</td>";
$jsql1="insert into journal2 values('$entrydate','$jvno','Dr','SALARY AND WAGES','$tottilhra','0','salary paid to ".strtoupper($empayrow[ename])." for the month $monthto-$yearto','$brcode','salary','','$empayrow[empcode]','')";
//print $jsql1.'<br>';
while($allorow=mysqli_fetch_array($allow_res1))
 {
 print "<td>".putComa($allorow[ealloamt])."</td>";
 }
$sq="select * from $tbl3 where month(wdatefrm)='$monthfrm' and year(wdatefrm)='$yearfrm' and divi='$divi' and br='$br' and brcode='$brcode' and empcode='$empayrow[empcode]'";

$ded_res=mysqli_query($db,$sq) or die("er2:".mysqli_error());
$dedow1=mysqli_fetch_array($ded_res);
print "<td align=right>".putcoma($dedow1[pf])."</td>";
print "<td align=right>".putComa($dedow1[esi])."</td>";
print "<td align=right>".putComa($dedow1[otherded])."</td>";
$totded+=$dedow1[pf]+$dedow1[esi]+$dedow1[otherded];
$totfornet-=$totded;
$totnet+=$totfornet;
$totpf+=$dedow1[pf];
$totesi+=$dedow1[esi];
$ttotded+=$dedow1[otherded];
$gtotded+=$totded;


$jsql2="insert into journal2 values('$entrydate','$jvno','Cr','PROVIDENT FUND PAYABLE','0','$dedow1[pf]', 'pf paid to ".strtoupper($empayrow[ename])." for the month $monthto-$yearto' ,'$brcode','salary','','$empayrow[empcode]','')";

$jsql3="insert into journal2 values('$entrydate','$jvno','Cr','EMPLYEES STATE INSURANCE PAYABLE','0','$dedow1[esi]', 'esi paid to ".strtoupper($empayrow[ename])." for the month $monthto-$yearto' ,'$brcode','salary','','$empayrow[empcode]','')";

//" to ESI Payable ".$dedow1[esi];
$jsql4="insert into journal2 values('$entrydate','$jvno','Cr','ADVANCE-".strtoupper($empayrow[ename])."','0','$dedow1[otherded]', 'advance paid to ".strtoupper($empayrow[ename])." for the month $monthto-$yearto' ,'$brcode','salary','','$empayrow[empcode]','')";

//" to ADVANCE-".strtoupper($empayrow[ename]) .' '.$dedow1[otherded];

print "<td align=right>".putcoma($totded)."</td><td align=right>".putComa($totfornet)."</td>";
print "</tr>";
$jsql5="insert into journal2 values('$entrydate','$jvno','Cr','SALARY AND WAGES PAYABLE','0','$totfornet', 'salary paid to ".strtoupper($empayrow[ename])." for the month $monthto-$yearto' ,'$brcode','salary','','$empayrow[empcode]','')";

$jsql10="insert into payjv (date,jvno,empcode,ename,brcode,br) values('$entrydate','$jvno','$empayrow[empcode]', '$empayrow[ename]', '$empayrow[brcode]','$empayrow[br]')";

//" to Salary Payable ".$totfornet;
//print "<tr><td colspan=20>$jsql1 <br>$jsql2<br>$jsql3<br>$jsql4 <br>$jsql5</td></tr>";

if($divi=='vehicle')
{
 mysqli_query($db,$jsql1) or die('j1'.mysqli_error());
 mysqli_query($db,$jsql2) or die('j2'.mysqli_error());
 mysqli_query($db,$jsql3) or die('j3'.mysqli_error());
 if($dedow1[otherded]>0)
  mysqli_query($db,$jsql4) or die('j4'.mysqli_error());
 mysqli_query($db,$jsql5) or die('j5'.mysqli_error());
 mysqli_query($db,$jsql10) or die('j10'.mysqli_error());
}
}
print "<tr><td colspan=4 align=center><b>Total</td><td align=right>". putComa($totbasicda)."</td><td align=right>".putComa($totebasicda)."</td><td align=right>".putComa($totta)."</td>";
print "<td align=right>".putComa($toteta)."</td><td align=right>".putComa($tothra)."</td>";
print "<td align=right>".putComa($totehra)."</td>"; //<td>".putComa($totrent)."</td>";
print "<td>".putComa($gtottilhra)."</td>";
print "<td>".putComa($totpf)."</td><td align=right>".putComa($totesi)."</td><td align=right>".putComa($ttotded)."<td align=right>".putComa($gtotded)."</td>";
print "<td align=right>".putComa($totnet)."</td>";
$pfcontri=($totpf*13.11/12);
//$esicontri=($totesi*13.11/12);
$esicontri=($totesi*4.75/1.75);
$jvno++;
$jsql6="insert into journal2 values('$entrydate','$jvno','Dr','EMPLOYER CONTRIBUTION TO PF','$pfcontri','0', 'PF emp contribution for the month $monthto-$yearto', '$brcode','salary','','$empayrow[empcode]','')";

$jsql7="insert into journal2 values('$entrydate','$jvno','Cr','PROVIDENT FUND PAYABLE','0','$pfcontri', 'PF emp contribution for the month $monthto-$yearto', '$brcode','salary','','$empayrow[empcode]','')";

 $jvno++;
$jsql8="insert into journal2 values('$entrydate','$jvno','Dr','EMPLOYER CONTRIBUTION TO ESI','$esicontri','0', 'ESI emp contribution for the month $monthto-$yearto', '$brcode','salary','','$empayrow[empcode]','')";

$jsql9="insert into journal2 values('$entrydate','$jvno','Cr','EMPLYEES STATE INSURANCE PAYABLE','0','$esicontri', 'ESI emp contribution for the month $monthto-$yearto', '$brcode','salary','','$empayrow[empcode]','')";


print "</table>";
//print $sql6.'<br>'.$sql7.'<br>';
//print $sql8.'<br>'.$sql9;
//print '<br>'.$sql10;

if($divi=='vehicle')
{ print "<br><b>post..";

 mysqli_query($db,$jsql6) or die('j6'.mysqli_error());
 mysqli_query($db,$jsql7) or die('j7'.mysqli_error());

 mysqli_query($db,$jsql8) or die('j8'.mysqli_error());
 mysqli_query($db,$jsql9) or die('j9'.mysqli_error());
}
?>

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