<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$user_level=$_SESSION['user_level'];
include_once("test.php");
include_once("opendb.php");
include_once('includes/top.php');

$sea=$_REQUEST['sea'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='ind.php' </script>";
  return;
} 
$page = (int) (!$_REQUEST["page"] ? 1 : $_GET["page"]);
	$limit = 20;
    	$startpoint = ($page * $limit) - $limit;
if($sea!='')
{
$query = "SELECT COUNT(*) as num FROM ledger where brcode = '$brcode' and led like '%$sea%' order by led";
	$total_pages = mysqli_fetch_array(mysqli_query($db,$query));
	$total_pages = $total_pages['num'];
}
else
{
$query = "SELECT COUNT(*) as num FROM ledger where brcode = '$brcode'";
	$total_pages = mysqli_fetch_array(mysqli_query($db,$query));
	$total_pages = $total_pages['num'];
}
$stages = 3;
	$page = $_REQUEST['page'];
	if($page){
		$start = ($page - 1) * $limit; 
	}else{
		$start = 0;	
		}
		if($sea!='')
{
    
    
$query2 = "SELECT * FROM ledger where brcode = '$brcode' and led like '%$sea%' order by led LIMIT $start, $limit";

	

if ($page == 0){$page = 1;}
	$prev = $page - 1;	
	$next = $page + 1;							
	$lastpage = ceil($total_pages/$limit);		
	$LastPagem1 = $lastpage - 1;
	$paginate = '';
	if($lastpage > 1)
	{	
$paginate .= "<div class='paginate'>";
		// Previous
		if ($page > 1){
			$paginate.= "<a href='$targetpage?sea=$sea&page=$prev'>previous</a>";
		}else{
			$paginate.= "<span class='disabled'>previous</span>";	}
			

		
		// Pages	
		if ($lastpage < 7 + ($stages * 2))	// Not enough pages to breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page){
					$paginate.= "<span class='current'>$counter</span>";
				}else{
					$paginate.= "<a href='$targetpage?sea=$sea&page=$counter'>$counter</a>";}					
			}
		}
		elseif($lastpage > 5 + ($stages * 2))	// Enough pages to hide a few?
		{
			// Beginning only hide later pages
			if($page < 1 + ($stages * 2))		
			{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?sea=$sea&page=$counter'>$counter</a>";}					
				}
				$paginate.= "...";
				$paginate.= "<a href='$targetpage?sea=$sea&page=$LastPagem1'>$LastPagem1</a>";
				$paginate.= "<a href='$targetpage?sea=$sea&page=$lastpage'>$lastpage</a>";		
			}
			// Middle hide some front and some back
			elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
			{
				$paginate.= "<a href='$targetpage?sea=$sea&page=1'>1</a>";
				$paginate.= "<a href='$targetpage?sea=$sea&page=2'>2</a>";
				$paginate.= "...";
				for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?sea=$sea&page=$counter'>$counter</a>";}					
				}
				$paginate.= "...";
				$paginate.= "<a href='$targetpage?sea=$sea&page=$LastPagem1'>$LastPagem1</a>";
				$paginate.= "<a href='$targetpage?sea=$sea&page=$lastpage'>$lastpage</a>";		
			}
			// End only hide early pages
			else
			{
				$paginate.= "<a href='$targetpage?sea=$sea&page=1'>1</a>";
				$paginate.= "<a href='$targetpage?sea=$sea&page=2'>2</a>";
				$paginate.= "...";
				for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?sea=$sea&page=$counter'>$counter</a>";}					
				}
			}
		}
					
				// Next
		if ($page < $counter - 1){ 
			$paginate.= "<a href='$targetpage?sea=$sea&page=$next'>next</a>";
		}else{
			$paginate.= "<span class='disabled'>next</span>";
			}
			
		$paginate.= "</div>";		
	
	
}

}
else
{
$query2 = "SELECT * FROM ledger where brcode = '$brcode' and led in ('STATE BANK OF INDIA, ELATHUR - 34831256116','SBI-OD-PAVANGAD-32258641397') LIMIT $start, $limit";
	//$result = mysqli_query($db,$query1);
// Initial page num setup
	if ($page == 0){$page = 1;}
	$prev = $page - 1;	
	$next = $page + 1;							
	$lastpage = ceil($total_pages/$limit);		
	$LastPagem1 = $lastpage - 1;					
	
	
	$paginate = '';
	if($lastpage > 1)
	{	
$paginate .= "<div class='paginate'>";
		// Previous
		if ($page > 1){
			$paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
		}else{
			$paginate.= "<span class='disabled'>previous</span>";	}
			

		
		// Pages	
		if ($lastpage < 7 + ($stages * 2))	// Not enough pages to breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page){
					$paginate.= "<span class='current'>$counter</span>";
				}else{
					$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
			}
		}
		elseif($lastpage > 5 + ($stages * 2))	// Enough pages to hide a few?
		{
			// Beginning only hide later pages
			if($page < 1 + ($stages * 2))		
			{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
				}
				$paginate.= "...";
				$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
				$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";		
			}
			// Middle hide some front and some back
			elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
			{
				$paginate.= "<a href='$targetpage?page=1'>1</a>";
				$paginate.= "<a href='$targetpage?page=2'>2</a>";
				$paginate.= "...";
				for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
				}
				$paginate.= "...";
				$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
				$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";		
			}
			// End only hide early pages
			else
			{
				$paginate.= "<a href='$targetpage?page=1'>1</a>";
				$paginate.= "<a href='$targetpage?page=2'>2</a>";
				$paginate.= "...";
				for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
				}
			}
		}
					
				// Next
		if ($page < $counter - 1){ 
			$paginate.= "<a href='$targetpage?page=$next'>next</a>";
		}else{
			$paginate.= "<span class='disabled'>next</span>";
			}
			
		$paginate.= "</div>";		
	
	
}
}

		
		
   
?>
<head>
<script>
 function sh()
 {
  window.open('prnledger.php');
 }
</script>
<title>Xpress</title>
   <script src="rightclick_deactive.js"></script>
    <script type="text/javascript" src="scripts/jquery-latest.js"></script>
	<link type="text/css" href="includes/style1.css" rel="stylesheet" />
	<link type="text/css" href="css/form.css" rel="stylesheet" />
    <script type="text/javascript">
        $(function() {
          if ($.browser.msie && $.browser.version.substr(0,1)<7)
          {
			$('li').has('ul').mouseover(function(){
				$(this).children('ul').show();
				}).mouseout(function(){
				$(this).children('ul').hide();
				})
          }
        });        
    </script>
<link href="css/pagination.css" rel="stylesheet" type="text/css" />
<link href="css/grey.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.paginate {
	font-family:Arial, Helvetica, sans-serif;
	padding: 3px;
	margin: 3px;
}

.paginate a {
	padding:2px 5px 2px 5px;
	margin:2px;
	border:1px solid #999;
	text-decoration:none;
	color: #666;
}
.paginate a:hover, .paginate a:active {
	border: 1px solid #999;
	color: #000;
}
.paginate span.current {
    margin: 2px;
	padding: 2px 5px 2px 5px;
		border: 1px solid #999;
		
		font-weight: bold;
		background-color: #999;
		color: #FFF;
	}
	.paginate span.disabled {
		padding:2px 5px 2px 5px;
		margin:2px;
		border:1px solid #eee;
		color:#DDD;
	}      
</style>
</head>



<body>

<?php include('../top.php'); ?>
<table class="container"><tr><td>
<table border=0 width=100% class='form'>
<tr><td>
<font style="font-family: 'Trebuchet MS',Verdana,Helvetica,sans-serif; font-size:15px;">  &nbsp;&nbsp;Accounts >> Reports >> Ledger Datewise</font>
<tr><td>
<font face=verdana size=3 color='dark brown'>Current Ledgers</b></font>
</td><td align=right width=30 ><a href='javascript:sh()'><img src='images/printer.jpg' border=0></a> </td></tr>
<tr><td colspan=2 ></td></tr>
<tr><td colspan=2>
<table border=0 cellspacing=1 cellpadding=1 class='report' >
<tr><th width=5><center><b>No.</th><th width=300><center><b>Ledger Name </th><th><center><b>Under Group</th><th><center><b>Op Balance<th><center><b>Dr/Cr</th>

<?
if ($user=='anilesh')
{
print "<form name='f1' action='$PHP_SELF'><input type=text name='sea' readonly > <input type=submit value='Search' name='sealed'></form><br>&nbsp;";
}
else
{
print "<form name='f1' action='$PHP_SELF'><input type=text name='sea' > <input type=submit value='Search' name='sealed'></form><br>&nbsp;";
}
$result = mysqli_query($db,$query2);
$i=1;
while($row=mysqli_fetch_array($result))
{

 if($row[0]=='Cash' || $row[0]=='Petty Cash')
{

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-test.php?cd=".str_replace('&','-',$row[0])."'>$row[0]</a></td><td>$row[1]</td><td align=right>".putComa($row[2])."</td><td align=right>$row[3]</td>";
print "<td>".$i++."</td><td><a href='vmledger-testnew1dw.php?cd=".$row[0]."'>$row[0]</a></td><td>$row[1]</td><td align=right>".number_format($row[2],2,',','.')."</td><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
{

//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-test.php?cd=".str_replace('&','~',$row[0])."&sea=$sea'>$row[0]</a></td><td>$row[1]</td><td align=right>".putComa($row[2])."</td><td align=right>$row[3]</td>";
//print "<td>".$i++."</td><td><a href='vmledger-test.php?cd=".$row[0]."&sea=$sea'>$row[0]</a></td><td>$row[1]</td><td align=right>".putComa($row[2])."</td><td align=right>$row[3]</td>";

?>

<td><? echo $i++?></td><td><a href='vmledger-testnew1dw.php?cd=<? echo str_replace('&','~',$row[0]) ?>'><? echo $row[0] ?></a></td><td><? echo $row[1]?></td><td align=right><? echo $row[2] ?></td><td align=right><?echo $row[3]?></td>



<?

 if($user_level=='4')
print "\n<td><input type=submit value='Edit' style={width:37}> <input type=button value='Delete' onClick=delfun('".$row['code']."'); style={width:52}></td></tr></form>";
}
print "</tr>";
}
?>
<div align=center>
<?

if($sea!='')
{
 echo $total_pages.' Results';
 // pagination
 echo $paginate;	
}
else
{
 echo $total_pages.' Results';
 // pagination
 echo $paginate;	
}
?>
</div>

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