<?
include_once('includes/top.php');
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
include_once("includes/function.php");
$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];
?>
<head>
<script>
function sh()
{
  	window.open('prnledger.php');
}
</script>
</head>
<title>Online Shopping and Inventory Management System</title>
    <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>
.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>
<script>
  function outfun(str){

   window.open('outstanding_repo.php?cd='+str);

  }
 function editfun(str){

   window.open('vmledger_edit.php?cd='+str);

  }

 function openled(str){

   window.open('editledgerall1718.php?ledg='+str);

  }
</script>
<body>


<br>
<table class="container"><tr><td>
<font style="font-family: 'Trebuchet MS',Verdana,Helvetica,sans-serif; font-size:15px; color:white;">  &nbsp;&nbsp;Accounts >> Search Ledger</font>
<br>
</center>
<body>
<?
$targetpage = "sealedgall1718.php";
$sea=$_REQUEST['sea'];
$limit = 20; 
if($sea!='')
{
	$query = "SELECT COUNT(*) as num FROM psn_ampleicher1718.ledger where 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 psn_ampleicher1718.ledger";
	$total_pages = mysqli_fetch_array(mysqli_query($db,$query));
	$total_pages = $total_pages[num];
}
$stages = 3;
$page = mysqli_escape_string($_GET['page']);
if($page){
	$start = ($page - 1) * $limit; 
}else{
	$start = 0;	
}
if($sea!='')
{
	$query1 = "SELECT distinct led  FROM psn_ampleicher1718.ledger where  led like '%$sea%' order by led LIMIT $start, $limit";
	//$result = mysqli_query($db,$query1);
	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
{
	$query1 = "SELECT distinct led FROM psn_ampleicher1718.ledger  order by led LIMIT $start, $limit";

// 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>";		
	}
}
?>
<table border=0 width=100% class='form'>
<tr><td>
<font face=verdana size=3 color='dark brown'>Current Ledgers</b></font>
</td></tr>
<tr><td colspan=2 ></td></tr>
<tr><td colspan=2>
<table border=0 cellspacing=1 cellpadding=1 class='report'>
<tr ><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>

<?
$result = mysqli_query($db,$query1);
//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);
//$page="sealedge.php?sea=$sea&";
//if($sea!='')
//{
//$statement1 = "ledger where led like '%$sea%' order by led";
//$sq="SELECT * FROM {$statement1} LIMIT {$startpoint} , {$limit}";
//echo $sq;
//$sq="select * from ledger where  led like '%$sea%' order by led";
//}
//else
//{
//$page="listledgers.php";
//$statement = "ledger";
//$sq="SELECT * FROM {$statement} LIMIT {$startpoint} , {$limit}";
//echo $sq;
//$sq="select * from ledger order by led limit ".($pg*25).",25";
//}
//$result=mysqli_query($db,$sq)or die(mysqli_error($db));


$i=$pg*25+1;
while($rowl=mysqli_fetch_array($result))
{
	$query3 = "SELECT * FROM psn_ampleicher1718.ledger  where led='$rowl[0]'";
        $result3 = mysqli_query($db,$query3);
        $row=mysqli_fetch_array($result3)
		?>
		<form action='editledgerall1718.php'><input type=hidden name='ledg' value='<? echo $row[0] ?>'>
		<tr>
		<td><? echo $i++ ?></td><td><? echo $row[0] ?></a></td><td><? echo $row[1] ?></td><td align=right></td>
		<style="width:37">
		<td align=right></td>
		<td align=right><? echo $row[3] ?></td>
		<?
		 if($user_level>0)
			$id=$row['code'];
		 ?>
		<td style="width:280px">
		<input type='button' value='Edit' onClick="openled('<? echo $row[led]; ?>');" > 
		<input type='button' value='Outstanding' onClick="outfun('<? echo $row[led]; ?>');" >
		</td></tr></form>
		<?
	
	?>
	</tr>
	<?
}
?>
<div align=center>
<?php
if($sea!='')
{
	echo $total_pages.' Results';
 	// pagination
 	echo $paginate;	
}
else
{
 	echo $total_pages.' Results';
 	// pagination
 	echo $paginate;	
}
?>
</div>
</table>
</td></tr></table>
