<?php
header("Content-type: text/html; charset=windows-1256");

session_start();
if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}

if($export == 1){
	$filename = "Exp_LedgerList" . date('Ymd') . ".xls";
	header("Content-Disposition: attachment; filename=\"$filename\"");
	header("Content-Type: application/vnd.ms-excel");
}

include("config.inc.php");
include 'function.inc.php';

print <<< HERE
<HTML>
<HEAD>
<TITLE> List of Ledgers </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

<style type="text/css">
	
.data{
	
	 FONT-WEIGHT: normal;
	 FONT-SIZE: 10px;	 
	
	 FONT-FAMILY: arial, sans-serif;
	}
	
.details{
		font-style: normal;
		FONT-WEIGHT: normal;
		FONT-SIZE: 9px;	 
	
	 FONT-FAMILY: arial, sans-serif;
	}

.header{
	
	 FONT-WEIGHT: bold;
	 FONT-SIZE: 12px;	 
	
	 FONT-FAMILY: arial, sans-serif;
	}
	
.footerbox{
				
	border-left: 0 solid #000000; border-top: 1 solid #000000; border-bottom: 0 solid #000000;
	FONT-WEIGHT: normal;
	FONT-SIZE: 10;	 
	 color:#2D2D2D;
	 FONT-FAMILY: arial, sans-serif;
	 BACKGROUND-COLOR: #FFFFFF; 
	
	 height: 20;
}
</style>





</HEAD>

<BODY>
HERE;


$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'])
		or die("Could not connect to DB");

 
mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");

$query_login="select login_belongstogrp from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlogin=mysql_query($query_login,$connection) or die(mysql_error());

$row_login=mysql_fetch_row($resultlogin);
$belongstogroup=$row_login[0];

function current_server_time($connection){

  $string="Select date_format(NOW(),'%Y-%m-%d %h:%i %p') ";
  $result=mysql_query($string,$connection) or die(mysql_error());
  $now=mysql_fetch_row($result);

  return $now;
}


$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$printdate=split("-",$dt[0]);





echo"<table border=0 width='820px' cellspacing=0 cellpadding=0  style='table-layout:fixed;' >";

echo "<tr><td align='left'>";
if ($prtheader == 1){
	ShowReportHeader('List of Ledgers','portrait', $connection);
} else {
	ShowReportTitle('List of Ledgers','portrait', $connection);
}
	
	echo"</td></tr>";
echo "<tr><td><table border=0 width='900px' cellspacing=0 cellpadding=0  style='table-layout:fixed;' >
	<col width=130> 
	<col width=550>
	<col width=70>
	<col width=70>";
	
	echo "<tr height=30px>";
	echo "<td align='left'  class='header' 	style='border-top: 3px solid #000000; border-bottom: 1px solid #000000' >Number</td>";
	echo "<td align='left'  class='header' 	style='border-top: 3px solid #000000; border-bottom: 1px solid #000000'>Name</td>";
	echo "<td align='left' class='header' 	style='border-top: 3px solid #000000; border-bottom: 1px solid #000000'>Nature </td>";
	echo "<td align='left' class='header' 	style='border-top: 3px solid #000000; border-bottom: 1px solid #000000'>Currency </td>";
	echo"</tr>";
	echo"<tr height=10px></tr>";
	
	$query_ledger="select ledger_number,length(ledger_number),Ledger_name,if(Ledger_grpdet=1,'Group','Detail'), Currency_code
					from ledger
					where ifNull(Ledger_BelongsToGroup,0) >= ".$belongstogroup;
	if ($fromLedger != '') {
		$query_ledger= $query_ledger." and Ledger_number >= $fromLedger ";
	}
	if ($toLedger != '') {
		$query_ledger= $query_ledger." and Ledger_number <= $toLedger ";
	}
	if ($Grp == 'Auxiliary') {
		$query_ledger= $query_ledger." and Ledger_grpdet = 0 ";
	}
	if ($Grp == 'Group') {
		$query_ledger= $query_ledger." and Ledger_grpdet = 1 ";
	}
					
	$query_ledger= $query_ledger." order by ledger_number,ledger_grpdet";

	$result_ledger=mysql_query($query_ledger,$connection) or die(mysql_error());
	$totalrecords=mysql_num_rows($result_ledger);
	$class = -1;
	while($row=mysql_fetch_row($result_ledger)){
		
		$ledger_number=$row[0];
		$ledger_len=$row[1];
		
		if (substr($ledger_number,0,1) != $class){
			if ($class != -1)
				echo "<tr><td><div style='page-break-after:always;'></div></td></tr>";
			$class=substr($ledger_number,0,1);
			echo "<tr height=40px><td class='details' align='center'><font size='5'><b>Class $class</b></font></td></tr>";
		}
		
		if($ledger_len<='4' || $row[3] =='Group'){
			if($dt==1){
				$dt=0;
				echo"<tr height=10px></tr>";	
			}
		
		echo"<tr>";
		echo"<td class='data' valign='top'>";
		for($i=1;$i<$ledger_len;$i++){
			echo"&nbsp;&nbsp;";
		}

		echo "$row[0]</td>";
		
		echo"<td class='data' valign='top'>$row[2]</td>";
		echo"<td class='data' valign='top'>$row[3]</td>";				
		echo"<td class='data' valign='top'>$row[4]</td>";				
		echo"</tr>";
		echo"<tr height=5px></tr>";				
	}
		
	else{
		echo"<tr>";
		echo"<td class='details' align='center'>$row[0]</td>";
		echo"<td class='details'>$row[2] &nbsp;</td>";	
		echo"<td class='data' valign='top'>$row[3]</td>";	
		echo"<td class='data' valign='top'>$row[4]</td>";	
		
		echo"</tr>";
		$dt=1;
	}

}
		
echo"<tr><td  align='left' colspan='11' class='footerbox'>Total Records: $totalrecords</td>
   </tr>";
echo "</table></td></tr></table>";


print <<< HERE

<script language=javascript>
if (typeof(window.print) != 'undefined')
{
    window.print();
}
</script>
</BODY>
</HTML>
HERE;
?>


