<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
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());
$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:12;color:black;}
 th{font-family:verdana;font-size:12;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>Closing</b></td>
<td><center><b>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' limit 25")or die(mysqli_error());
$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);
$sealed=$_REQUEST['sealed'];
if($sealed)
{
    $sea=$_REQUEST['sea'];
 $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());
$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[code]</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='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>".number_format($bal,2)."</td>";
		print "<td align=right>$row[3]</td>";
		if($user_level>0)
		$parvl=$row['led'];
		$partcv=explode(" ",$parvl);
		$neprtcd=$partcv[0].'%20'.$partcv[1].'%20'.$partcv[2].'%20'.$partcv[3].'%20'.$partcv[4].'%20'.$partcv[5].'%20'.$partcv[6].'%20'.$partcv[7].'%20'.$partcv[8];

	if(($user=='mathew')||($user=='sreejith')||($user=='minoshh')||($user=='minosh')||($user=='latheef')||($user=='vibin')||($user=='antony')||($user=='shameem'))
{
print "\n<td><input type=submit value='Edit' style={width:37}> <input type=button value='Del' onClick=delfun('$neprtcd'); style={width:37}></td></tr></form>";
}
//	print "\n<td><input type=submit value='Edit' style={width:37}> <input type=button value='Del' onClick=delfun('".$row[code]."'); style={width:37}></td></tr></form>";			
	}
	print "</tr>";
}
?>
</table>
<script>
function delfun(x)
{
x=x.replace(/%20/g, " "); 
 	
 	
	document.location='delledger.php?code='+x;
}
</script>