<?php

//    header("Content-Disposition: attachment;filename=Exp_GeneralLedger.xls"); 
include("config.inc.php");
require_once('Spreadsheet/Excel/Writer.php');
	
session_start();
if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}






$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");

//maroun 30.03.2011
$querylog="SELECT print as printaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')	";
	$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultlog)){
		extract($rows);
	}

						
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, 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 ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['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]);

//Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();
 
//Sending HTTP headers
$workbook->send('Exp_GeneralLedger.xls');
 
//Creating a worksheet
$worksheet=&$workbook->addWorksheet('Export');
$worksheet->setLandscape();
 
//set all columns same width
$columnWidth = 10;


//Title Format 
$sheetTitleFormat =& $workbook->addFormat(array('bold'=>1,
  'size'=>10));

//Column Title Format  
$columnTitleFormat =& $workbook->addFormat(array('bold'=>1,
  'top'=>1,
  'bottom'=>1 ,
  'size'=>15,
  'color'=>0x0A,
  ));
  
 
  
// Regular Format
$regularFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'left',
  'textWrap'=>0 ,
  'bold'=>1
  ));
$TitleFormat =& $workbook->addFormat(array('size'=>12,
  'align'=>'left',
  'textWrap'=>0 ,
  'bold'=>1
  ));  
// Merge cells from row 0, col 0 to row 2, col 2
$worksheet->setMerge(1, 3, 1, 4);
$worksheet->setMerge(1, 6, 1, 7);
$worksheet->setMerge(1, 8, 1, 9);


 
$worksheet->setColumn (0, $numColumns, $columnWidth);
$worksheet->setColumn (2, 2, 52);

$worksheet->write(1, 0, $CompanyName, $regularFormat);
$worksheet->write(1, 3, "List of Ledgers", $TitleFormat);
$worksheet->write(1, 6, "Printed Date:", $regularFormat);
$worksheet->write(1, 8, $printdate[2]."/".$printdate[1]."/".$printdate[0], $regularFormat);
$worksheet->write(2, 6, "Time:", $regularFormat);
$worksheet->write(2, 8, $dt[1]." ".$dt[2], $regularFormat);


	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.",'0') and ifnull(".$toledger.",'9999999999999')) 
         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) ";

	$worksheet->write(4, 0, "Ledger No.", $regularFormat);
	$worksheet->write(4, 2, "Name", $regularFormat);
	$worksheet->write(4, 7, "Nature", $regularFormat);
	
$rownum	=5;

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

		
	$ledgerlen = strlen($rows[0]);
	$rownum = $rownum + 1;
	
	
if ($ledgerlen == 1){
$worksheet->write($rownum, 0, "CLASS", $regularFormat);
$worksheet->write($rownum, 2, "$rows[0]", $regularFormat);
$rownum = $rownum + 1;

}




if ($rows[2]=='Group')
$worksheet->write($rownum, 0, "$rows[0]", $regularFormat);
else
$worksheet->write($rownum, 1, "$rows[0]", $regularFormat);

$worksheet->write($rownum, 2, "$rows[1]", $regularFormat);


if ($rows[2]=='Group')
$worksheet->write($rownum, 7, "$rows[2]", $regularFormat);
else
$worksheet->write($rownum, 8, "$rows[2]", $regularFormat);

}

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

$workbook->close();
?>