<?
session_start();
include("../test.php");
include("../opendb.php");
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='../ind.php' </script>";
  return;
}
$us=mysqli_query($db,"select level from bruser where username='$user'")or die(mysqli_error($db));
$r=mysqli_fetch_row($us);
$user_level=$r[0];
 print "User Level:".$user_level;
?>
<head>
<script>
 function sh()
 {
  window.open('prnledger.php');
 }
</script>
</head>
<style>
 td{font-family:verdana;font-size:14;color:black;}
 th{font-family:verdana;font-size:14;color:white;background-color:#5b5858}
 body{background-color:ivory}
</style>
</center>
<body>
<table border=0 width=100%>
<tr><td>
<font face=verdana size=3 color='dark brown'>Current Ledgers</b></font>
</td><td align=right width=30 bgcolor=silver><a href='javascript:sh()'><img src='Type001.gif' border=0></a> </td></tr>
<tr><td colspan=2 bgcolor=green></td></tr>
<tr><td colspan=2>
<table border=0 cellspacing=1 cellpadding=1 bgcolor=black width=>
<tr bgcolor='silver'><td width=5><center><b>No.</td><td width=300><center><b>Ledger Name </td><td><center><b>Under Group</td><td><center><b>Op Balance
<td><center><b>CL Balance</b></td>
<td><center><b>Ledger<br>Type</td>

<?
if($user_level>0)
 print "<td align=center><b>Action</td>";
print "</tr>";

$cnt=mysqli_query($db,"select * from ledger where brcode='$brcode'")or die(mysqli_error($db));
$num_rows=mysqli_num_rows($cnt);

if($num_rows>25)
{
 print "Page ";
 for($k=0;$k<=$num_rows/25;$k++)
 {
   print "<a href='listledgers.php?pg=".($k)."'>".($k+1)."</a> ";
 }
}

print "<form name='f1' action='$PHP_SELF'><input type=text name='sea' > <input type=submit value='Search' name='sealed'></form><br>&nbsp;";
print "Current page:".($pg+1);
if($sealed)
 $sq="select * from ledger where ( brcode='$brcode') and led like '%$sea%' order by led";
else
 $sq="select * from ledger where  brcode='$brcode' order by led limit ".($pg*25).",25";

$result=mysqli_query($db,$sq)or die(mysqli_error($db));

$i=$pg*25+1;
while($row=mysqli_fetch_array($result))
{$bal=0;
 if($row[0]=='Cash' || $row[0]=='Petty Cash')
{
$balres=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where brcode='$brcode' and partic='$row[0]'");
 $balrow=mysqli_fetch_row($balres);
 if($row[drcr]=='Dr')
 {$bal=($balrow[0]+$row[2])-$balrow[1]; //print '<br>bal'.$row[2];
 }
 else
 $bal=$balrow[1]+$row[2]-$balrow[0];

print"\n<form action='editledger.php'><input type=hidden name='ledg' value='$row[0]'>";
print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'>";
print "<td>".$i++."</td><td><a href='vmledger.php?cd=".str_replace('&','-',$row[0])."'>$row[0]</a></td><td>$row[1]</td><td align=right>".putComa($row[2])."</td>";
print "<td align=right>$bal</td>";
print "<td align=right>$row[3]</td>";
if($user_level>=4)
 print "\n<td colspan=2><input type=submit value='Edit' style={width:37}></td></tr> </form>";
}
else
{
$balres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where brcode='$brcode' and partic='$row[0]'");
 $balrow=mysqli_fetch_row($balres);
 if($row[drcr]=='Dr')
 {$bal=($balrow[0]+$row[2])-$balrow[1]; //print '<br>bal'.$row[2];
 }
 else
 $bal=$balrow[1]+$row[2]-$balrow[0];

//print"<form action='editledger.php'><input type=hidden name=ledg value='$row[0]'>";
print"\n<form action='delledger3.php'><input type=hidden name='ledg' value='$row[0]'>";
print "\n<tr bgcolor='#fffcd6' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'>";
print "<td>".$i++."</td><td><a href='vmledger.php?cd=".str_replace('&','~',$row[0])."'>$row[0]</a></td><td>$row[1]</td><td align=right>".putComa($row[2])."</td>";
print "<td align=right>".number_format($bal,2)."</td>";
print "<td align=right>$row[3]</td>";
 if($user_level>0)
print "\n<td><input type=submit value='Delete' style={width:45}> </td></tr></form>";
}
print "</tr>";
}
?>
</table>

<script>
function delfun(x)
{
 document.location='delledger.php?led='+x;
}
</script>
