<?php


session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}
	 
print <<< HERE

<HTML>
<HEAD>
<TITLE> General Ledger </TITLE>

<style type="text/css">

table.master th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
				  border-top: 2 solid #000000;
				  border-bottom: 1 solid #000000;
				 }
table.master td { font-size: 14px; 
                  font-family:Times-New-Roman;
                 }

</style>


</HEAD>


HERE;

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


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

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

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}
//maroun 30.03.2011
$querylog="SELECT print as printaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')	";
	$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultlog)){
		extract($rows);
	}


if ($printaccess==0 or $printaccess=='')
echo'<BODY oncontextmenu="return false;">';
else
echo'<BODY>';
						
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, report_color1, report_color2 grid_color1, grid_color2, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}
	//User Access
$querylog=" select ifNull(login_belongstogrp,0) As FI_BelongsToGrp from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

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



$flag=0;
    echo "<table border=1 width=100% cellspacing=0 cellpadding=0><tr><td align='left'></td></tr></table>";
	echo "<table border=1 width='950' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=150>
		  <col width=400>
		  <col width=200> 
		  <col width=200>";   
	

	echo "<tr><td height='60'><img src='img/imgheader1.jpg' width='115' height='50' /></td><td colspan='2' align='center'><b><font size=5>General Ledger </font></td><td  align='right' ><font size='2'><b>Printed&nbsp;Date&nbsp;&amp;&nbsp;Time:</b>&nbsp;&nbsp;";
	echo $printdate[2]."/".$printdate[1]."/".$printdate[0];
	echo "<br>".$dt[1]." ".$dt[2]. " ".$dt[3];
	echo "</font></td></tr>";
	//ShowReportHeader('General Ledger','portrait', $connection);

	
	

	if ($fromledger=='')
		$fromledger = 'null';
	else
		$fromledger = "'".$fromledger."'"; 
		
	if ($toledger=='')
		$toledger = 'null';
	else
		$toledger = "'".$toledger."'"; 
		
    

	   

$query= " select Ledger_number, Ledger_name, if(Ledger_grpdet=1, 'Group', 'Detail') from ledger 
         where (ledger_number between ifnull(".$fromledger.",ledger_number) and ifnull(".$toledger.",ledger_number)) 
         and (($varPrtAux=0 and  Ledger_grpdet=1) or ($varPrtAux=1 and  Ledger_grpdet in(0,1) ) )
		 
		 order by if($option=1, Ledger_number,Null), if($option=2, Ledger_name, Null) ";
//echo $query;
	
	echo "<tr height='20'>";
	echo "<td align='center' style='border-left: 0 solid #000000; border-top: 1 solid #000000; border-bottom: 1 solid #000000;'><b>Number</b></td>";
	echo "<td align='center' style='border-left: 0 solid #000000; border-top: 1 solid #000000; border-bottom: 1 solid #000000;'><b>Description</b></td>";
	echo "<td align='center' style='border-left: 0 solid #000000; border-top: 1 solid #000000; border-bottom: 1 solid #000000;'><b>Nature</b></td>";
    echo"<td style='border-left: 0 solid #000000; border-top: 1 solid #000000; border-bottom: 1 solid #000000;'>&nbsp;</td>";
    echo "</tr>";
	

$i=1;
$result=mysql_query($query,$connection) or die(mysql_error());
$col=0;
while($rows=mysql_fetch_row($result)){

		if($col%2==0){
			$color = "style='background-color:".$report_color1."'";
		}else{
			$color = "style='background-color:".$report_color2."'";
			}
	$ledgerlen = strlen($rows[0]);


	if ($ledgerlen == 1) {
	   echo "</table>";
	   
	   if ($flag==1) {
	   echo "<br><div style='page-break-after:always;'></div><br>";
	   }
	   $flag=1;
		echo "<table border=0 width='950' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
			  <col width=150>
			  <col width=400>
			  <col width=200> 
			  <col width=200>";  
		echo"<tr><td><b>&nbsp; &nbsp; CLASS $rows[0]</b></td></tr>";
	}
	else if ($ledgerlen == 1 && $varNewPage ==1){
	echo "<tr><td>test<div style='page-break-after:always;'></div></td></tr>";
	echo"<tr><td><b>&nbsp; &nbsp; CLASS $rows[0]</b></td></tr>";

	} 

	echo"<tr height='20' $color>";
	if ($rows[2]=='Group')
		echo"<td style='border-right:1px solid black;'><b>$rows[0]</b></td>";
	else
		echo"<td style='border-right:1px solid black;'>&nbsp; &nbsp; $rows[0]</td>";

	if ($rows[2]=='Group')
		echo"<td style='border-right:1px solid black; padding-left:2px;'><b>$rows[1]</b></td>";
	else
		echo"<td style='border-right:1px solid black; padding-left:2px;'>$rows[1]</td>";

	if ($rows[2]=='Group')
		echo"<td align ='center' style='border-right:1px solid black; padding-left:2px;'><b>$rows[2]</b></td>";
	else
		echo"<td align ='center' style='border-right:1px solid black; '>&nbsp; &nbsp; $rows[2]</td>";
	echo"</tr>";
	
	$col+=1;
}
$i=$i+1;	


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;
}

function explodeword($date)
{

	$trim = explode('/',$date);
	$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
	return $trim;
}

if ($printaccess==1)
print <<< HERE





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