<?php

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

//Option Balance of Project-Costcent
if ($report == 3) {
	$groupby = 2;
	//$showdetail = 1;
}

//$option 1=Project   2=Costcenter
if ($option == 1) {
	$ReportName  	= 'Report of Shipment Ref';
	$ReportTitle 	= 'Balance of Shipment Ref per Account';
	$OptionLabel 	= 'Shipment Ref';
	$SubOptionLabel = 'Costcenter';
} 
else {
	$ReportName  	= 'Report of Costcenter';
	$ReportTitle 	= 'Balance of Costcenter per Account';
	$OptionLabel 	= 'Costcenter';
	$SubOptionLabel = 'Shipment Ref';
}

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

print <<< HERE
<HTML>
<HEAD>
<TITLE> $ReportName </TITLE>

<style type="text/css">
table.params td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }

			 
table.master th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
				 }
				 
table.master td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }

						
table.master tr.group { font-size: 12px; 
						font-weight: bold;
						font-family:Times-New-Roman;
					  }				 
table.master tr.subgroup { font-size: 12px; 
						   font-weight: bold;
						   font-family:Times-New-Roman;
						   color:#0026FF;
					     }
					  
table.master tr.totals { font-size: 12px; 
						 font-weight: bold;
						 font-family:Times-New-Roman;
					  }	
table.master tr.subtotals { font-size: 12px; 
							font-weight: bold;
							font-family:Times-New-Roman;
							color:#0026FF;
						}					  
				 

</style>




</HEAD>


HERE;
include("config.inc.php");

//Set Time Execution For the Report
ini_set('max_execution_time', $config['ReportTimeExecution']);

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

$querymodule="select ifNull(module_groupbyledger,0) As module_groupbyledger from module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultmodule)){
  extract($rows);
}

$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, Base1, Base2,report_color1, report_color2, Company_Name As CompanyName from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultsetup)){
  extract($rows);
}


if($fromledger!=''){
$queryledger="select ledger_name as fromledgername from ledger where ledger_number='$fromledger'";
$resultledger=mysql_query($queryledger,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultledger)){
  extract($rows);
  }
}

if($toledger!=''){
$queryledger2="select ledger_name as toledgername from ledger where ledger_number='$toledger'";
$resultledger2=mysql_query($queryledger2,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultledger2)){
  extract($rows);
  }
}

$querylog=" select ifNull(FI_RepRestrictProject,0) As FI_RepRestrictProject, ifNull(FI_RepRestrictCostcenter,0) As FI_RepRestrictCostcenter ".
          " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."') ";
$resultlog=mysql_query($querylog) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$_SESSION['dbLabel'].".loginstk on project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%') ".
			  " where user_account='".$_SESSION['useraccount']."' ".
			  " and (loginstk.project_code is not null and loginstk.project_code != '') ".
			  " order by project.project_code";
$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
$projectexist   = mysql_num_rows($projectresult);

$querycostcent=" select distinct costcent.costcent_code, costcent_name ".
               " from costcent left join ".$_SESSION['dbLabel'].".loginstk on costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%') ".
			   " where user_account='".$_SESSION['useraccount']."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " order by costcent.costcent_code";
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);



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


$SelectedGroupBy = "Group by:  ";
if ($groupby == 1 ) $SelectedGroupBy = $SelectedGroupBy . "Ledger";
if ($groupby == 2 ) $SelectedGroupBy = $SelectedGroupBy . $OptionLabel;


$SelectedOptions = "Filter by: <br>";
if ($fromdate != '')      $SelectedOptions .= "-> Date From: ". $fromdate . "  To: ". $todate ."<br>";	
if ($currency != '')      $SelectedOptions .= "-> Currency: ". $currency ."<br>";	
if ($fromledger != '')    $SelectedOptions .= "-> Ledger From: ". $fromledger . "  To: ". $toledger ."<br>";
if ($fromledgername != '')    $SelectedOptions .= "-> Ledger Name From: ". $fromledgername . "  To: ". $toledgername ."<br>";
if ($fromproject != '')   $SelectedOptions .= "-> Shipment Ref From: ". trim($fromproject,'%') . "  To: ". trim($toproject,'%') . "<br>";
if ($fromcostcent != '')  $SelectedOptions .= "-> Costcent From: ". trim($fromcostcent,'%') . "  To: ". trim($tocostcent,'%') . "<br>";
$SelectedOptions = $SelectedGroupBy . "<br>" . $SelectedOptions;

if (!empty($fromdate))
   $fromdate = explodeword($fromdate);
if (!empty($todate))
   $todate = explodeword($todate);     

$fromledger      = GetSQLValueString($fromledger, 'text');
$toledger        = GetSQLValueString($toledger, 'text');
$fromproject     = GetSQLValueString($fromproject, 'text');
$toproject    	 = GetSQLValueString($toproject, 'text');
$fromcostcent 	 = GetSQLValueString($fromcostcent, 'text');
$tocostcent  	 = GetSQLValueString($tocostcent, 'text');
$fromdate  	   	 = GetSQLValueString($fromdate, 'date');
$todate          = GetSQLValueString($todate, 'date');   


   
$jvtypelen = 0;
if (!empty($jvtype))
   $jvtypelen = strlen($jvtype);
   
if (empty($jvtype))   $jvtype = 'null';      



print <<< HERE
<table width="100%" border="0" align="left" >
<tr><td>
<table width="100%" border="0" align="center">
<tr>
  <td width='30%'><b>$CompanyName</b></td>
  <td width='50%' align='center'><font size=4><b>$ReportTitle</b></font></td>
  <td width='10%'><font size=1><b> Printed Date: </b> <br><b>Time: </b>
  <td width='10%'><font size=1>
HERE;

echo $printdate[2]."/".$printdate[1]."/".$printdate[0];
echo "<br>".$dt[1]." ".$dt[2];
  
print <<<HERE
</font></td>
</tr>
<tr>
   <td colspan='4' width='100%'> <hr width='100%' color='black' size='1'> </td>
</tr>
</table>


<table class="params" width="100%" border="0" align="center" >
<tr>
  <td width="100%" valign='top'><b>$SelectedOptions</b></td>
</tr>
<tr>
  <td colspan='15'><hr width='100%' color='black' size='1'></td>
</tr>
</table>
HERE;

echo "<table  border='0' width='100%' align='center' class='master' style='table-layout:fixed; border-collapse:collapse;' cellpadding='0' cellspacing='0'>
	  <col width=20>	
	  <col width=80>
	  <col width=200>";
	  	if ($groupby==3) 
					echo "<col width=50>";
	  echo"<col width=110>
	  <col width=110>
	  <col width=110>";
	  
if ($groupby == 1) {
	if ($showdetail == 1) {	
		echo"<thead><tr>
				<th align='left' colspan='2'>Ledger No.</th>
				<th align='left' colspan='4'>Ledger Name</th>
			 </tr>";
		echo"<tr>
				<th align='left'>&nbsp;</th>
				<th align='left'>$OptionLabel</th>
				<th align='left'>$OptionLabel Name</th>
				<th align='center'>Debit</th>
				<th align='center'>Credit</th>
				<th align='center'>Balance $currency</th>
			 </tr>";	 
	} else {
		echo"<tr>
				<th align='left' colspan='2'>Ledger No.</th>
				<th align='left'>Ledger Name</th>
				<th align='center'>Debit</th>
				<th align='center'>Credit</th>
				<th align='center'>Balance $currency</th>
			</tr>";
	}
} else {
	if ($showdetail == 1) {	
		echo"<thead><tr>
				<th align='left' colspan='2'>$OptionLabel</th>
				<th align='left' colspan='4'>$OptionLabel Name</th>
			 </tr>";
			 
		if ($report == 3) {
			echo"<tr>
					<th align='left' colspan='2' style='color:#0026FF;'>$SubOptionLabel</th>
					<th align='left' colspan='4' style='color:#0026FF;'>$SubOptionLabel Name</th>
				</tr>";
		}
		
		echo"<tr>
				<th align='left'>&nbsp;</th>
				<th align='left'>Ledger No.</th>
				<th align='left'>Ledger Name</th>";
				
					if ($groupby==3) 
					echo"<th align='center'>Date</th>";
				echo"<th align='center'>Debit</th>";
				
			
					
				echo"<th align='center'>Credit</th>
				<th align='center'>Balance $currency</th>
			 </tr>";	 	 
	} else {
		if ($report == 3) {
			echo"<tr>
					<th align='left' colspan='2'>$OptionLabel</th>
					<th align='left' colspan='4'>$OptionLabel Name</th>
				 </tr>";
			echo"<tr>
					<th align='left' colspan='2'>$SubOptionLabel</th>
					<th align='left'>$SubOptionLabel Name</th>
					<th align='center'>Debit</th>
					<th align='center'>Credit</th>
					<th align='center'>Balance $currency</th>
				</tr>";					 
			
		} else {
			echo"<tr>
					<th align='left' colspan='2'>$OptionLabel</th>
					<th align='left'>$OptionLabel Name</th>
					<th align='center'>Debit</th>
					<th align='center'>Credit</th>
					<th align='center'>Balance $currency</th>
				</tr>";	
		}
	}
}	
if ($groupby==3) 
echo"<tr><td colspan='7'><hr width='100%' color='black' size='2'></td></tr>";
else
echo"<tr><td colspan='6'><hr width='100%' color='black' size='2'></td></tr>";
echo"</thead><tbody>";

$queryjournal=$queryjournal." select MasterCode, MasterDescription, DetailCode, DetailDescription, Currency_code, DebitAmount, ".	
							" DebitAmountBase1, DebitAmountBase2, CreditAmount, CreditAmountBase1, CreditAmountBase2, Balance, ".
							" BalanceBase1, BalanceBase2, SubMasterCode, SubMasterDescription , Journal_date,Journaldt_desc";
if($groupby == 1)	$queryjournal=$queryjournal.", left(MasterCode,3) ";
$queryjournal=$queryjournal." from ( ";
$queryjournal=$queryjournal." select if($groupby=1, Ledger.Ledger_number, if($option=1, if($parentprjcost=1, ifNull(Project.Parent_Code, ifNull(Project.Project_Code, 'None')), ifNull(CstJour.Project_code, 'None')), if($parentprjcost=1, ifNull(Costcent.Parent_Code, ifNull(Costcent.Costcent_Code, 'None')), ifNull(CstJour.Costcent_Code, 'None')))) As MasterCode, ".
							" if($groupby=1, Ledger.Ledger_name, if($option=1, ifNull(project.project_name, 'None'), ifNull(costcent.costcent_name, 'None'))) As MasterDescription , ".
							" if($report=3, if($option=2, if($parentprjcost=1, ifNull(Project.Parent_Code, ifNull(Project.Project_Code, 'None')), ifNull(CstJour.Project_code, 'None')), if($parentprjcost=1, ifNull(Costcent.Parent_Code, ifNull(Costcent.Costcent_Code, 'None')), ifNull(CstJour.Costcent_Code, 'None'))), Null) As SubMasterCode, ".
							" if($report=3, if($option=2, ifNull(project.project_name, 'None'), ifNull(costcent.costcent_name, 'None')), Null) As SubMasterDescription , ".
							" if($showdetail=1, if($groupby=1, if($option=1, if($parentprjcost=1, ifNull(Project.Parent_Code, ifNull(Project.Project_Code, 'None')), ifNull(CstJour.Project_code, 'None')), if($parentprjcost=1, ifNull(Costcent.Parent_Code, ifNull(Costcent.Costcent_Code, 'None')), ifNull(CstJour.Costcent_Code, 'None'))), if($groupby=3,JournalDt.Ledger_Number, Ledger.Ledger_number)), '') As DetailCode, ".
							" if($showdetail=1, if($groupby=1, if($option=1, ifNull(project.project_name, 'None'), ifNull(costcent.costcent_name, 'None')), Ledger.Ledger_name), '') As DetailDescription , ".
							" JournalDt.Currency_code,Journaldt_desc, ";
					
if ($groupby==3)  
$queryjournal= $queryjournal.
					" (if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount) , 0),0)) DebitAmount, ".
					" (if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount1, Journaldt_amount1), 0),0)) DebitAmountBase1, ".
					" (if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount2, Journaldt_amount2), 0),0)) DebitAmountBase2, ".
					" (if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'C', if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount) , 0),0)) CreditAmount, ".
					" (if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'C', if(CstJour.Cst_increment is not null, Cstjour_amount1, Journaldt_amount1), 0),0)) CreditAmountBase1, ".
					" (if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'C', if(CstJour.Cst_increment is not null, Cstjour_amount2, Journaldt_amount2), 0),0)) CreditAmountBase2, ".
					" (if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount), (-1)*if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount)),0)) Balance, ".
					" (if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount1, Journaldt_amount1), (-1)*if(CstJour.Cst_increment is not null, Cstjour_amount1, Journaldt_amount1)),0)) BalanceBase1, ".
					" (if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount2, Journaldt_amount2), (-1)*if(CstJour.Cst_increment is not null, Cstjour_amount2, Journaldt_amount2)),0)) BalanceBase2 ".
					" ,date_format(JournalDt.Journal_date,'%d/%m/%Y') as Journal_date ";
else
$queryjournal= $queryjournal.
					" Sum(if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount) , 0),0)) DebitAmount, ".
					" Sum(if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount1, Journaldt_amount1), 0),0)) DebitAmountBase1, ".
					" Sum(if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount2, Journaldt_amount2), 0),0)) DebitAmountBase2, ".
					" Sum(if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'C', if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount) , 0),0)) CreditAmount, ".
					" Sum(if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'C', if(CstJour.Cst_increment is not null, Cstjour_amount1, Journaldt_amount1), 0),0)) CreditAmountBase1, ".
					" Sum(if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'C', if(CstJour.Cst_increment is not null, Cstjour_amount2, Journaldt_amount2), 0),0)) CreditAmountBase2, ".
					" Sum(if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount), (-1)*if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount)),0)) Balance, ".
					" Sum(if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount1, Journaldt_amount1), (-1)*if(CstJour.Cst_increment is not null, Cstjour_amount1, Journaldt_amount1)),0)) BalanceBase1, ".
					" Sum(if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount2, Journaldt_amount2), (-1)*if(CstJour.Cst_increment is not null, Cstjour_amount2, Journaldt_amount2)),0)) BalanceBase2 ".
					" ,date_format(JournalDt.Journal_date,'%d/%m/%Y') as Journal_date ";
 

$queryjournal= $queryjournal." from JournalDt ".
							 " Inner join Ledger on JournalDt.Ledger_number = Ledger.Ledger_number ".
							 " Inner join Journal on JournalDt.Journal_id = Journal.Journal_id ".
							 " Left outer Join CstJour on Journaldt.JournalDt_ID = Cstjour.JournalDt_ID ".
							 " Left outer Join project on project.Project_code = Cstjour.Project_code ".
							 " Left outer Join costcent on costcent.Costcent_Code = Cstjour.Costcent_Code ";

if ($projectexist > 0 && $FI_RepRestrictProject == 1)
		$queryjournal=$queryjournal." Inner join ( select distinct project_code from ".$_SESSION['dbLabel'].".loginstk ".
									"			   where loginstk.user_account='".$_SESSION['useraccount']."' ".
									"              and (loginstk.project_code is not null and loginstk.project_code != '') ".
									"		     ) Q1 on CstJour.project_code like concat(Q1.project_code,'%') ";
									
if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
		$queryjournal=$queryjournal." Inner join ( select distinct costcent_code from ".$_SESSION['dbLabel'].".loginstk ".
									"			   where loginstk.user_account='".$_SESSION['useraccount']."' ".
									"              and (costcent_code is not null and costcent_code != '') ".
									"			 ) Q2 on CstJour.Costcent_code like concat(Q2.costcent_code,'%')  ";								

$queryjournal= $queryjournal.  
				" where  ledger.Ledger_number between ifNull(".$fromledger.",ledger.Ledger_number) and ifNull(". $toledger.",ledger.Ledger_number)  ".
				" and date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d'))   ".
				" and (($alltypes = 1 and 1 = 1) or ($alltypes = 0 and $jvtypelen = 0 and 1 = 1) or ($alltypes = 0 and $exclude = 0 and $jvtypelen > 0 and Journal_type in ($jvtype)) or ($alltypes = 0 and $exclude = 1 and $jvtypelen > 0 and Journal_type not in ($jvtype)) ) ";
if($projectper == 1)
$queryjournal= $queryjournal." and upper(ifNull(CstJour.project_code,'-1')) like ifNull(upper (".$fromproject."), upper(ifNull(CstJour.project_code,'-1'))) ";
else 				
$queryjournal= $queryjournal.				
				" and upper(ifNull(CstJour.project_code,'-1')) between ifNull(upper (".$fromproject."), upper(ifNull(CstJour.project_code,'-1'))) and ifNull(upper(".$toproject."), upper(ifNull(CstJour.project_code,'-1'))) ";

if($costcentper == 1)
$queryjournal= $queryjournal." and upper(ifNull(CstJour.Costcent_Code,'-1')) like ifNull(upper(".$fromcostcent."), upper(ifNull(CstJour.Costcent_Code,'-1'))) ";
else				
$queryjournal= $queryjournal.
				" and upper(ifNull(CstJour.Costcent_Code,'-1')) between ifNull(upper(".$fromcostcent."), upper(ifNull(CstJour.Costcent_Code,'-1'))) and ifNull(upper(".$tocostcent."), upper(ifNull(CstJour.Costcent_Code,'-1'))) ";

if ($groupby!=3) {
	$queryjournal= $queryjournal." and (($balance = 1 and 1 = 1) or  ".
								 "		($balance = 2 and if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount), (-1)*if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount)),0) > 0) or ".
								 "		($balance = 3 and if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount), (-1)*if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount)),0) < 0) or ".
								 "		($balance = 4 and if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount), (-1)*if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount)),0) = 0)) ";
}
 
if ($groupby==3) 
	$queryjournal= $queryjournal." Order By MasterCode, SubMasterCode, DetailCode,journaldt.journal_date ";
else
	$queryjournal= $queryjournal." Group By MasterCode, SubMasterCode, DetailCode ".
								 " Order By MasterCode, SubMasterCode, DetailCode ";
							 
$queryjournal= $queryjournal." ) Q3  ";
							 //" where (($balance = 1 and 1 = 1) or ($balance = 2 and Balance > 0) or ($balance = 3 and Balance < 0) or ($balance = 4 and Balance = 0)) ";
							 
if ($groupby==3) {
	$queryjournal= $queryjournal." where (($balance = 1 and 1 = 1) or ($balance = 2 and Balance > 0) or ($balance = 3 and Balance < 0) or ($balance = 4 and Balance = 0)) ";
}							 
							 
if ($groupby==3) 							 
	$queryjournal= $queryjournal." ";
else
	$queryjournal= $queryjournal." Order By left(MasterCode,3),MasterCode, SubMasterCode, DetailCode ";
//echo $queryjournal;
//exit;


$MasterByCode = '';
$MasterByName = '';
$SubMasterByCode = '';
$SubMasterByName = '';

$ArrayTotalByReport    = Array();
$ArrayTotalByMaster    = Array();
$ArrayTotalBySubMaster = Array();
$ArrayTotalByLedger = Array();

$resultjournal=mysql_query($queryjournal,$connection) or die(mysql_error());






$queryjournal2=$queryjournal2." select if($groupby=1, Ledger.Ledger_number, if($option=1, if($parentprjcost=1, ifNull(Project.Parent_Code, ifNull(Project.Project_Code, 'None')), ifNull(CstJour.Project_code, 'None')), if($parentprjcost=1, ifNull(Costcent.Parent_Code, ifNull(Costcent.Costcent_Code, 'None')), ifNull(CstJour.Costcent_Code, 'None')))) As MasterCode, ".
							" if($groupby=1, Ledger.Ledger_name, if($option=1, ifNull(project.project_name, 'None'), ifNull(costcent.costcent_name, 'None'))) As MasterDescription , ".
							" if($report=3, if($option=2, if($parentprjcost=1, ifNull(Project.Parent_Code, ifNull(Project.Project_Code, 'None')), ifNull(CstJour.Project_code, 'None')), if($parentprjcost=1, ifNull(Costcent.Parent_Code, ifNull(Costcent.Costcent_Code, 'None')), ifNull(CstJour.Costcent_Code, 'None'))), Null) As SubMasterCode, ".
							" if($report=3, if($option=2, ifNull(project.project_name, 'None'), ifNull(costcent.costcent_name, 'None')), Null) As SubMasterDescription , ".
							" if($showdetail=1, if($groupby=1, if($option=1, if($parentprjcost=1, ifNull(Project.Parent_Code, ifNull(Project.Project_Code, 'None')), ifNull(CstJour.Project_code, 'None')), if($parentprjcost=1, ifNull(Costcent.Parent_Code, ifNull(Costcent.Costcent_Code, 'None')), ifNull(CstJour.Costcent_Code, 'None'))), if($groupby=3,JournalDt.Ledger_Number, Ledger.Ledger_number)), '') As DetailCode, ".
							" if($showdetail=1, if($groupby=1, if($option=1, ifNull(project.project_name, 'None'), ifNull(costcent.costcent_name, 'None')), Ledger.Ledger_name), '') As DetailDescription , ".
							" JournalDt.Currency_code,Journaldt_desc, ";
					
if ($groupby==3)  
$queryjournal2= $queryjournal2.
					" (if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount) , 0),0)) DebitAmount, ".
					" (if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount1, Journaldt_amount1), 0),0)) DebitAmountBase1, ".
					" (if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount2, Journaldt_amount2), 0),0)) DebitAmountBase2, ".
					" (if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'C', if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount) , 0),0)) CreditAmount, ".
					" (if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'C', if(CstJour.Cst_increment is not null, Cstjour_amount1, Journaldt_amount1), 0),0)) CreditAmountBase1, ".
					" (if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'C', if(CstJour.Cst_increment is not null, Cstjour_amount2, Journaldt_amount2), 0),0)) CreditAmountBase2, ".
					" (if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount), (-1)*if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount)),0)) Balance, ".
					" (if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount1, Journaldt_amount1), (-1)*if(CstJour.Cst_increment is not null, Cstjour_amount1, Journaldt_amount1)),0)) BalanceBase1, ".
					" (if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount2, Journaldt_amount2), (-1)*if(CstJour.Cst_increment is not null, Cstjour_amount2, Journaldt_amount2)),0)) BalanceBase2 ".
					" ,date_format(JournalDt.Journal_date,'%d/%m/%Y') as Journal_date ";
else
$queryjournal2= $queryjournal2.
					" Sum(if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount) , 0),0)) DebitAmount, ".
					" Sum(if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount1, Journaldt_amount1), 0),0)) DebitAmountBase1, ".
					" Sum(if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount2, Journaldt_amount2), 0),0)) DebitAmountBase2, ".
					" Sum(if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'C', if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount) , 0),0)) CreditAmount, ".
					" Sum(if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'C', if(CstJour.Cst_increment is not null, Cstjour_amount1, Journaldt_amount1), 0),0)) CreditAmountBase1, ".
					" Sum(if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'C', if(CstJour.Cst_increment is not null, Cstjour_amount2, Journaldt_amount2), 0),0)) CreditAmountBase2, ".
					" Sum(if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount), (-1)*if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount)),0)) Balance, ".
					" Sum(if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount1, Journaldt_amount1), (-1)*if(CstJour.Cst_increment is not null, Cstjour_amount1, Journaldt_amount1)),0)) BalanceBase1, ".
					" Sum(if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount2, Journaldt_amount2), (-1)*if(CstJour.Cst_increment is not null, Cstjour_amount2, Journaldt_amount2)),0)) BalanceBase2 ".
					" ,date_format(JournalDt.Journal_date,'%d/%m/%Y') as Journal_date ";
 

$queryjournal2= $queryjournal2." from JournalDt ".
							 " Inner join Ledger on JournalDt.Ledger_number = Ledger.Ledger_number ".
							 " Inner join Journal on JournalDt.Journal_id = Journal.Journal_id ".
							 " Left outer Join CstJour on Journaldt.JournalDt_ID = Cstjour.JournalDt_ID ".
							 " Left outer Join project on project.Project_code = Cstjour.Project_code ".
							 " Left outer Join costcent on costcent.Costcent_Code = Cstjour.Costcent_Code ";

if ($projectexist > 0 && $FI_RepRestrictProject == 1)
		$queryjournal2=$queryjournal2." Inner join ( select distinct project_code from ".$_SESSION['dbLabel'].".loginstk ".
									"			   where loginstk.user_account='".$_SESSION['useraccount']."' ".
									"              and (loginstk.project_code is not null and loginstk.project_code != '') ".
									"		     ) Q1 on CstJour.project_code like concat(Q1.project_code,'%') ";
									
if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
		$queryjournal2=$queryjournal2." Inner join ( select distinct costcent_code from ".$_SESSION['dbLabel'].".loginstk ".
									"			   where loginstk.user_account='".$_SESSION['useraccount']."' ".
									"              and (costcent_code is not null and costcent_code != '') ".
									"			 ) Q2 on CstJour.Costcent_code like concat(Q2.costcent_code,'%')  ";								
$querymas="select left($fromledger,3)";
$resultmas=mysql_query($querymas,$connection) or die(mysql_error());
$rowsmas=mysql_fetch_array($resultmas);
// echo $rowsmas[0];exit;
$queryjournal2= $queryjournal2.  
				" where  ledger.Ledger_number like '".$rowsmas[0]."%'  ".
				" and date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d'))   ".
				" and (($alltypes = 1 and 1 = 1) or ($alltypes = 0 and $jvtypelen = 0 and 1 = 1) or ($alltypes = 0 and $exclude = 0 and $jvtypelen > 0 and Journal_type in ($jvtype)) or ($alltypes = 0 and $exclude = 1 and $jvtypelen > 0 and Journal_type not in ($jvtype)) ) ";
if($projectper == 1)
$queryjournal2= $queryjournal2." and upper(ifNull(CstJour.project_code,'-1')) like ifNull(upper (".$fromproject."), upper(ifNull(CstJour.project_code,'-1'))) ";
else 				
$queryjournal2= $queryjournal2.				
				" and upper(ifNull(CstJour.project_code,'-1')) between ifNull(upper (".$fromproject."), upper(ifNull(CstJour.project_code,'-1'))) and ifNull(upper(".$toproject."), upper(ifNull(CstJour.project_code,'-1'))) ";

if($costcentper == 1)
$queryjournal2= $queryjournal2." and upper(ifNull(CstJour.Costcent_Code,'-1')) like ifNull(upper(".$fromcostcent."), upper(ifNull(CstJour.Costcent_Code,'-1'))) ";
else				
$queryjournal2= $queryjournal2.
				" and upper(ifNull(CstJour.Costcent_Code,'-1')) between ifNull(upper(".$fromcostcent."), upper(ifNull(CstJour.Costcent_Code,'-1'))) and ifNull(upper(".$tocostcent."), upper(ifNull(CstJour.Costcent_Code,'-1'))) ";

if ($groupby!=3) {
	$queryjournal2= $queryjournal2." and (($balance = 1 and 1 = 1) or  ".
								 "		($balance = 2 and if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount), (-1)*if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount)),0) > 0) or ".
								 "		($balance = 3 and if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount), (-1)*if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount)),0) < 0) or ".
								 "		($balance = 4 and if(date_format(JournalDt.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(JournalDt.Journal_date,'%Y-%m-%d')), if(Journaldt_dbcd = 'D', if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount), (-1)*if(CstJour.Cst_increment is not null, Cstjour_amount, Journaldt_amount)),0) = 0)) ";
}

$resultjournal2=mysql_query($queryjournal2,$connection) or die(mysql_error());
$rowsjournal2=mysql_fetch_array($resultjournal2);
$col=0;
$prevjour ='';
$countline = 1;
$totalgroupDebit=0;
$totalgroupCredit=0;
$totalgroupBalance=0;
while($rowsjournal=mysql_fetch_row($resultjournal)){

		if($col%2==0){
			$color = "style='background-color:".$report_color1."'";
		}else{
			$color = "style='background-color:".$report_color2."'";
		}
		
    // Group By Options Names and Totals
	if (($report == 3) && ($SubMasterByCode != $rowsjournal[14] && $SubMasterByCode != '')) {
		
		echo "<tr>";
		echo "   <td colspan='6'><hr width='100%' color='black' size='1'></td>";
		echo "</tr>";
			
		$StyleClass  = 'subtotals';
		$TotalsLabel = 'Total by '. $SubOptionLabel .'('. $currency .')';
		
		echo "<tr class='subtotals'>";
		echo "   <td align='right' valign='top' colspan='3'>". $TotalsLabel ."</td>";
		echo "	 <td align='right' valign='top'>". format_currency($ArrayTotalBySubMaster['Debit'],   $DecimalBase) ."</td>";
		echo "	 <td align='right' valign='top'>". format_currency($ArrayTotalBySubMaster['Credit'],  $DecimalBase) ."</td>";
		echo "	 <td align='right' valign='top'>". format_currency($ArrayTotalBySubMaster['Balance'], $DecimalBase) ."</td>";
		echo "</tr>";
		
	}
	
	
	if (($groupby == 3) && ($led != $rowsjournal[2] && $led != '')) {
		    
		echo "<tr>";
		echo "   <td colspan='7'><hr width='100%' color='black' size='1'></td>";
		echo "</tr>";
			
		$StyleClass  = 'totals';
		$TotalsLabel = 'Total by Ledger ('. $currency .')';
		
		
		echo "<tr class='totals'>";
		echo "   <td align='right' valign='top' colspan='4'>". $TotalsLabel ."</td>";
		echo "	 <td align='right' valign='top'>". format_currency($ArrayTotalBySubMaster['Debit'],   $DecimalBase) ."</td>";
		echo "	 <td align='right' valign='top'>". format_currency($ArrayTotalBySubMaster['Credit'],  $DecimalBase) ."</td>";
		echo "	 <td align='right' valign='top'>". format_currency($ArrayTotalBySubMaster['Balance'], $DecimalBase) ."</td>";
		echo "</tr>";
		
		echo "<tr height=25px><td>&nbsp;</td></tr>";
		
		unset($ArrayTotalBySubMaster);
	}
	
	if ((($showdetail == 1) || ($report == 3 && $showdetail == 0)) && ($MasterByCode != $rowsjournal[0] && $MasterByCode != '')) {
		    
		echo "<tr>";
		echo "   <td colspan='6'><hr width='100%' color='black' size='1'></td>";
		echo "</tr>";
			
		
		$StyleClass  = 'totals';
		if ($groupby == 1) $TotalsLabel = 'Total by Ledger ('. $currency .')';
		else $TotalsLabel = 'Total by '. $OptionLabel .'('. $currency .')';
		
		echo "<tr class='totals'>";
		echo "   <td align='right' valign='top' colspan='3'>". $TotalsLabel ."</td>";
		echo "	 <td align='right' valign='top'>". format_currency($ArrayTotalByMaster['Debit'],   $DecimalBase) ."</td>";
		echo "	 <td align='right' valign='top'>". format_currency($ArrayTotalByMaster['Credit'],  $DecimalBase) ."</td>";
		echo "	 <td align='right' valign='top'>". format_currency($ArrayTotalByMaster['Balance'], $DecimalBase) ."</td>";
		echo "</tr>";

		$totalgroupDebit=$totalgroupDebit+$ArrayTotalByMaster['Debit'];
		$totalgroupCredit=$totalgroupCredit+$ArrayTotalByMaster['Credit'];
		$totalgroupBalance=$totalgroupBalance+$ArrayTotalByMaster['Balance'];
		
	}
	
	

	if ((($showdetail == 1) || ($report == 3 && $showdetail == 0)) && ($MasterByCode == '' || $MasterByCode != $rowsjournal[0])) { 	
		if($groupby == 1) {
			if($prevjour != $rowsjournal[18]){
			if($countline == 1){
				echo "<tr><td colspan='6'></td></tr>";
			}else{ echo "<tr><td colspan='6'>
			<hr width='100%' color='black' size='2'></td></tr>";
			echo "<tr><td align='right' colspan='3'><b>Total In Group ($prevjour)</b></td>";
			
			if(($fromledger == $toledger) && $fromledger!='Null' && $toledger!='Null'){
				echo "	 <td align='right' valign='top'><b>". format_currency($rowsjournal2[9],   $DecimalBase) ."</b></td>";
				echo "	 <td align='right' valign='top'><b>". format_currency($rowsjournal2[12],  $DecimalBase) ."</b></td>";
				echo "	 <td align='right' valign='top'><b>". format_currency($rowsjournal2[15], $DecimalBase) ."</b></td>";
			}else{
				echo "	 <td align='right' valign='top'><b>". format_currency($totalgroupDebit,   $DecimalBase) ."</b></td>";
				echo "	 <td align='right' valign='top'><b>". format_currency($totalgroupCredit,  $DecimalBase) ."</b></td>";
				echo "	 <td align='right' valign='top'><b>". format_currency($totalgroupBalance, $DecimalBase) ."</b></td>";
			}
			
			// echo "	 <td align='right' valign='top'><b>". format_currency($totalgroupDebit,   $DecimalBase) ."</b></td>";
			// echo "	 <td align='right' valign='top'><b>". format_currency($totalgroupCredit,  $DecimalBase) ."</b></td>";
			// echo "	 <td align='right' valign='top'><b>". format_currency($totalgroupBalance, $DecimalBase) ."</b></td>";
			echo " </tr>";
			}
			echo "<tr><td colspan='2' align='center'><font size='2'><b><i>$rowsjournal[18]</i></b></font></td></tr>";
			$totalgroupDebit=0;
			$totalgroupCredit=0;
			$totalgroupBalance=0;
		}
		
		}
		$prevjour = $rowsjournal[18];
		$countline++;
		echo "<tr class='group'>";
		echo "   <td align='left' valign='top' colspan='2'>". $rowsjournal[0] ."</td>";
		echo "   <td align='left' valign='top' colspan='4'>". $rowsjournal[1] ."</td>";
		echo "</tr>"; 
		
		$MasterByCode = $rowsjournal[0];
		$MasterByName = $rowsjournal[1];
		
		unset($ArrayTotalByMaster);
		unset($ArrayTotalBySubMaster);
	}

	if (($report == 3 && $showdetail == 1) && ($SubMasterByCode == '' || $SubMasterByCode != $rowsjournal[14])) { 	
		echo "<tr class='subgroup'>";
		echo "   <td align='left' valign='top' colspan='2'>". $rowsjournal[14] ."</td>";
		echo "   <td align='left' valign='top' colspan='4'>". $rowsjournal[15] ."</td>";
		echo "</tr>"; 
		
		$SubMasterByCode = $rowsjournal[14];
		$SubMasterByName = $rowsjournal[15];
		
		unset($ArrayTotalBySubMaster);
		
	}	
	if ($showdetail == 1 && $groupby==3) 
		echo "<tr >";
	else 
		echo"<tr $color>";
	
	if ($showdetail == 1) {
		
		echo "<td align='left' valign='top'>&nbsp;</td>";
		
		if ($groupby==3) {
			if ($led != $rowsjournal[2])
			{
					
					echo "<td align='left' valign='top'><b>". $rowsjournal[2] ."</b></td>";
					echo "<td align='left' valign='top'><b>". $rowsjournal[3] ."</b></td>";
					echo"</tr>";
					echo"<tr >";
					echo "<td align='left' valign='top'>&nbsp;</td>";
					echo "<td align='left' valign='top'>&nbsp;</td>";
					echo "<td align='left' valign='top'>". $rowsjournal[17] ."</td>";
			}
			else {	
				echo "<td align='left' valign='top'></td>";
				echo "<td align='left' valign='top'>". $rowsjournal[17] ."</td>";
			}
			
		}
		else {
			echo "<td align='left' valign='top' style='border-right:1px solid black;'>". $rowsjournal[2] ."</td>";
			echo "<td align='left' valign='top' style='border-right:1px solid black; padding-left:2px;'>". $rowsjournal[3] ."</td>";
		}
		
	} 
	else if ($showdetail == 0) {
		 
		if ($report == 3) {
			echo "<td align='left' valign='top' colspan='2'>". $rowsjournal[14] ."</td>";
			echo "<td align='left' valign='top' >". $rowsjournal[15] ."</td>";
		}
		else {
			echo "<td align='left' valign='top' colspan='2'>". $rowsjournal[0] ."</td>";
			echo "<td align='left' valign='top' >". $rowsjournal[1] ."</td>";
		}	
	} 
	
	if ($groupby==3) 
		echo "<td align='right' valign='top' >". $rowsjournal[16] ."</td>";
		
	if ($currency == $Base1) {
		echo "<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". format_currency($rowsjournal[6], $DecimalBase1) ."</td>";
		echo "<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". format_currency($rowsjournal[9], $DecimalBase1) ."</td>";
		echo "<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". format_currency($rowsjournal[12], $DecimalBase1) ."</td>";
		
		$ArrayTotalByMaster['Debit']    = $ArrayTotalByMaster['Debit']   + $rowsjournal[6];
		$ArrayTotalByMaster['Credit']   = $ArrayTotalByMaster['Credit']  + $rowsjournal[9];
		$ArrayTotalByMaster['Balance'] 	= $ArrayTotalByMaster['Balance'] + $rowsjournal[12];
		
		$ArrayTotalBySubMaster['Debit']    = $ArrayTotalBySubMaster['Debit']   + $rowsjournal[6];
		$ArrayTotalBySubMaster['Credit']   = $ArrayTotalBySubMaster['Credit']  + $rowsjournal[9];
		$ArrayTotalBySubMaster['Balance']  = $ArrayTotalBySubMaster['Balance'] + $rowsjournal[12];
	}
	else if ($currency == $Base2) {
		echo "<td align='right' valign='top' >". format_currency($rowsjournal[7], $DecimalBase2) ."</td>";
		echo "<td align='right' valign='top' >". format_currency($rowsjournal[10], $DecimalBase2) ."</td>";
		echo "<td align='right' valign='top' >". format_currency($rowsjournal[13], $DecimalBase2) ."</td>";
		
		$ArrayTotalByMaster['Debit']    = $ArrayTotalByMaster['Debit']   + $rowsjournal[7];
		$ArrayTotalByMaster['Credit']   = $ArrayTotalByMaster['Credit']  + $rowsjournal[10];
		$ArrayTotalByMaster['Balance'] 	= $ArrayTotalByMaster['Balance'] + $rowsjournal[13];
		
		$ArrayTotalBySubMaster['Debit']    = $ArrayTotalBySubMaster['Debit']   + $rowsjournal[7];
		$ArrayTotalBySubMaster['Credit']   = $ArrayTotalBySubMaster['Credit']  + $rowsjournal[10];
		$ArrayTotalBySubMaster['Balance']  = $ArrayTotalBySubMaster['Balance'] + $rowsjournal[13];
	}
	    
	echo "</tr>"; 

	$led = $rowsjournal[2];	
	$col+=1;
}


// Group By Options Names and Totals
if (($report == 3) && ($SubMasterByCode != $rowsjournal[14] && $SubMasterByCode != '')) {
		    
	echo "<tr>";
	echo "   <td colspan='6'><hr width='100%' color='black' size='1'></td>";
	echo "</tr>";
			
	$StyleClass  = 'subtotals';
	$TotalsLabel = 'Total by '. $SubOptionLabel .'('. $currency .')';
		
	echo "<tr class='subtotals'>";
	echo "   <td align='right' valign='top' colspan='3'>". $TotalsLabel ."</td>";
	echo "	 <td align='right' valign='top'>". format_currency($ArrayTotalBySubMaster['Debit'],   $DecimalBase) ."</td>";
	echo "	 <td align='right' valign='top'>". format_currency($ArrayTotalBySubMaster['Credit'],  $DecimalBase) ."</td>";
	echo "	 <td align='right' valign='top'>". format_currency($ArrayTotalBySubMaster['Balance'], $DecimalBase) ."</td>";
	echo "</tr>";
		
}

if ((($showdetail == 1) || ($report == 3 && $showdetail == 0)) && ($MasterByCode != $rowsjournal[0] && $MasterByCode != '')) {
		  
	if ($groupby==3) 		  {
	echo "<tr>";
	echo "   <td colspan='7'><hr width='100%' color='black' size='1'></td>";
	echo "</tr>";
	} else {
	echo "<tr>";
	echo "   <td colspan='6'><hr width='100%' color='black' size='1'></td>";
	echo "</tr>";
		}
			
	$StyleClass  = 'totals';
	if ($groupby == 1) $TotalsLabel = 'Total by Ledger ('. $currency .')';
	else $TotalsLabel = 'Total by '. $OptionLabel .'('. $currency .')';
		
	echo "<tr class='totals'>";
	
	if ($groupby==3) 
	echo "   <td align='right' valign='top' colspan='4'>". $TotalsLabel ."</td>";
	else
	echo "   <td align='right' valign='top' colspan='3'>". $TotalsLabel ."</td>";
	
	echo "	 <td align='right' valign='top'>". format_currency($ArrayTotalByMaster['Debit'],   $DecimalBase) ."</td>";
	echo "	 <td align='right' valign='top'>". format_currency($ArrayTotalByMaster['Credit'],  $DecimalBase) ."</td>";
	echo "	 <td align='right' valign='top'>". format_currency($ArrayTotalByMaster['Balance'], $DecimalBase) ."</td>";
	echo "</tr>";

	$totalgroupDebit=$totalgroupDebit+$ArrayTotalByMaster['Debit'];
	$totalgroupCredit=$totalgroupCredit+$ArrayTotalByMaster['Credit'];
	$totalgroupBalance=$totalgroupBalance+$ArrayTotalByMaster['Balance'];
	
		echo "<tr><td align='right' colspan='3'><b>Total In Group ($prevjour)</b></td>";
		if(($fromledger == $toledger) && $fromledger!='Null' && $toledger!='Null'){
			echo "	 <td align='right' valign='top'><b>". format_currency($rowsjournal2[9],   $DecimalBase) ."</b></td>";
			echo "	 <td align='right' valign='top'><b>". format_currency($rowsjournal2[12],  $DecimalBase) ."</b></td>";
			echo "	 <td align='right' valign='top'><b>". format_currency($rowsjournal2[15], $DecimalBase) ."</b></td>";
		}else{
			echo "	 <td align='right' valign='top'><b>". format_currency($totalgroupDebit,   $DecimalBase) ."</b></td>";
			echo "	 <td align='right' valign='top'><b>". format_currency($totalgroupCredit,  $DecimalBase) ."</b></td>";
			echo "	 <td align='right' valign='top'><b>". format_currency($totalgroupBalance, $DecimalBase) ."</b></td>";
		}
		echo " </tr>";
		
}


	   

echo "</table></tr></td></table>";
  echo"</tbody>";

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

function format_currency($amount, $DecimalBase) { 

    if (number_format(abs($amount),$DecimalBase) == 0)
	    return 0; 
	else if($amount < 0) 
        return "(".number_format(abs($amount),$DecimalBase).")"; 
    else return number_format($amount, $DecimalBase); 
} 

function GetSQLValueString($theValue, $theType) 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Null";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "Null";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "Null";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Null";
      break;
  }
  return $theValue;
}

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

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


?>