<?php


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

include("config.inc.php");

header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

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

$querylog="SELECT FA_DailyReport as loginaccess , print as printaccess,ExporttoExcel 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);
}

//echo $loginaccess;
if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");
	
if ($ExporttoExcel==1) $ExporttoExcelStatus= 'button';
else $ExporttoExcelStatus= 'hidden';		
	
	
$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, Base1, Base2, Company_Name As CompanyName from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultsetup)){
  extract($rows);
}


$querysetup ="select base1, base2, if(DefaultBase=1, base1, base2) as defaultcurr from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
$rowsetup=mysql_fetch_row($resultsetup);
$defaultcurr = $rowsetup[2];


$querydate=" Select Date_Format(Min(Journal_date),'%d/%m/%Y') As DefaultFromDate, ".
           " Date_Format(Max(Journal_date),'%d/%m/%Y') As DefaultToDate ".
		   " From JournalDt ";
$resultdate=mysql_query($querydate,$connection) or die(mysql_error());
$rowdate=mysql_fetch_row($resultdate);

if (!isset($fromdate)) $fromdate = $rowdate[0];
if (!isset($todate))   $todate = $rowdate[1];
  
$querycurrency="select Currency_code from currency order by currency_code";
$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error());

print <<< HERE

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">

<script language="JavaScript" src="CalendarPopup.js" ></script><!-- Date only with year scrolling -->
<script type="text/javascript" language="javascript">
<!--


function MoveLevel12(option) {
	var ob = document.form1.availgroupby;
	var leng = ob.options.length;
	
	var vSelectedOptionValue='';
	var vSelectedOptionName='';
	var vSelectedCount=0;
	
	if(ob.selectedIndex == -1) {
		alert('No selection is done.');
		return;
	}
	
	while (ob.selectedIndex != -1)
    {
        if (ob.selectedIndex != -1) {
			vSelectedCount = vSelectedCount + 1;
			vSelectedOptionName  = ob.options[ob.selectedIndex].text;
			vSelectedOptionValue = ob.options[ob.selectedIndex].value;
		}	
        ob.options[ob.selectedIndex].selected = false;
    }

	if (vSelectedCount == 1) {
		if (option == 1) {
			document.form1.groupbylevel1name.value  = vSelectedOptionName;
			document.form1.groupbylevel1value.value = vSelectedOptionValue;
		}	
		else {
			document.form1.groupbylevel2name.value  = vSelectedOptionName;
			document.form1.groupbylevel2value.value = vSelectedOptionValue;
		}	
	}	
	else
		alert ('Not allowed multi selection for Level 1 and 2');
		
	
}

function RemoveLevel12(option) {
	var x = document.form1;
	
	if (option == 1) {
		x.groupbylevel1name.value = '';
		x.groupbylevel1value.value = '';
	}	
	else {
		x.groupbylevel2name.value = '';
		x.groupbylevel2value.value = '';
	}	
}

function MoveLevel3() {
	var ob = document.form1.availgroupby;
	var leng = ob.options.length;
	var TargetElement = document.form1.assigngroupby.options;
	
	var vExists = 0;
	
	if(ob.selectedIndex == -1) {
		alert('No selection is done.');
		return;
	}
	
	if(TargetElement[0] != null) {
		if (TargetElement[0].value=='') 
			TargetElement[0] = null;
	}	

		
	while (ob.selectedIndex != -1)
    {  
        if (ob.selectedIndex != -1) {
		
			vExists = 0;
			for(i=0; i < TargetElement.length; i++) {
				if (TargetElement[i].value == ob.options[ob.selectedIndex].value)
					vExists = 1;
			}
			
			if (vExists == 0)
				TargetElement[TargetElement.length] = new Option(ob.options[ob.selectedIndex].text, ob.options[ob.selectedIndex].value);
		}	  

		ob.options[ob.selectedIndex].selected = false;
    }

	
}

function RemoveLevel3() {
	var ob = document.form1.assigngroupby;
	var leng = ob.options.length;
	
	if(ob.selectedIndex == -1) {
		alert('No selection is done.');
		return;
	}
	
	while (ob.selectedIndex != -1)
    {  
        if (ob.selectedIndex != -1) 
			  ob.options[ob.selectedIndex] = null;
    }
	
}



function checkledger(Vcheck,Vobj){
	
	if (Vcheck.checked){ 
		Vobj.disabled=false;
	}
	else {
		Vobj.disabled=true;
	}
}

function select_alldbtypes(){
	for (var i=1; i <= document.form1.i.value; i++)
	{
		checkname='debit'+i;
	
		x = document.getElementById(checkname);
		if (document.form1.alltypesdebit.checked) {
			x.checked = true;
		}
		else {
			x.checked = false;
		}
    }
}

function select_allcrtypes(){
	for (var i=1; i <= document.form1.i.value; i++)
	{
		checkname='credit'+i;
	
		x = document.getElementById(checkname);
		if (document.form1.alltypescredit.checked) {
			x.checked = true;
		}
		else {
			x.checked = false;
		}
    }
}

function select_allroottypes(){
	for (var i=1; i <= document.form1.i.value; i++)
	{
		checkname='root'+i;
	
		x = document.getElementById(checkname);
		if (document.form1.alltypesroot.checked) {
			x.checked = true;
		}
		else {
			x.checked = false;
		}
    }
}

function select_alljvtypes() {
   
	var checkname;
	var x;
	var textname;
	for (var i=1; i <= document.form1.i.value; i++)
	{
		checkname='type'+i;
	
		x = document.getElementById(checkname);
		if (document.form1.alltypes.checked) {
			x.checked = true;
		}
		else {
			x.checked = false;
		}
    
		textname = 'Ledger'+i;
	
		if (document.form1.alltypes.checked)
			document.getElementById(textname).disabled = false;
	
		//else
		//document.getElementById(textname).disabled = true;
	}

}



function PrintPrev(printaccess)
{
	width = screen.width - 10;
	height = screen.height - 66;
	var str='';
	var ledgers;
	var checkboxname;
	var ledgernumbers='';
	var ledgernumbersTemp='';
	var totemp=0;
 
  
  	 
	for (var i=0; i < document.form1.currency.length; i++)
	{
		if (document.form1.currency[i].checked)
		{
		var varcurrency = document.form1.currency[i].value;
		}
	}
	
	
	
    varalltypes = 0;
    varalltypesdebit = 0;
    varalltypescredit = 0;
	varalltypesroot = 0;
	
	if (document.form1.alltypes.checked==true)
		varalltypes = 1;	

	if (document.form1.alltypesdebit.checked==true)
		varalltypesdebit = 1;	
	
	if (document.form1.alltypescredit.checked==true)
		varalltypescredit = 1;	
	
	if (document.form1.alltypesroot.checked==true)
		varalltypesroot = 1;	
	
	
	var x,D,C, varjvtype,varcheckDebit,checkDebit, ledgername, varledgers;
	varjvtype = '';
	varcheckDebit = '';
	varcheckCredit = '';
	varledgers = '';
	varjvroot = '';
  
	for (var i=1; i <= document.form1.i.value; i++)
	{
		checkname='type'+i;
		checkDebit='debit'+i;
		checkCredit='credit'+i;
		ledgername='ledger'+i;
		checkroot='root'+i;
	
		D = document.getElementById(checkDebit);
		C = document.getElementById(checkCredit);
		x = document.getElementById(checkname);
		m = document.getElementById(ledgername);
		r = document.getElementById(checkroot);

		if (varalltypesdebit == 0 && D.checked) {
			if (varcheckDebit.length > 0) varcheckDebit += ",";
			varcheckDebit += "'"+x.value+"'";
		}	
		
		if (varalltypescredit == 0 && C.checked) {
			if (varcheckCredit.length > 0) varcheckCredit += ",";
			varcheckCredit += "'"+x.value+"'";
		}	
	 
		if (varalltypes == 0 && x.checked)  {
			if (varjvtype.length > 0) varjvtype += ",";
			varjvtype += "'"+x.value+"'";
		}	
		
		if (m.value != '') {
			if (varledgers.length > 0) varledgers  += "-";
			varledgers += x.value+":"+m.value;
		}
		
		if (varalltypesroot == 0 && r.checked)  {
			if (varjvroot.length > 0) varjvroot += ",";
			varjvroot += "'"+r.value+"'";
		}
   
	}
  
  
 
	for (var i=0; i < document.form1.reptype.length; i++)
	{
		if (document.form1.reptype[i].checked)
		{
		var varreptype = document.form1.reptype[i].value;
		}
	}
  
  
	//Getting Group By Levels 1 and 3
	vargroupby1   = document.form1.groupbylevel1value.value;
	vargroupby2   = document.form1.groupbylevel2value.value;
	  
	//Getting Assigngroupby Values for level 3
	var ob = document.form1.assigngroupby;
	var vargroupby3 = '';
	for (var i = 0; i < ob.options.length; i++) {
		if (vargroupby3.length > 0)
			vargroupby3 += ",";
				
		vargroupby3 += ob.options[i].value;
	}
	  
	// If No Group By Option Selected
	if (vargroupby3 == '') vargroupby3 = 1;
  
	varfromdate  = document.form1.fromdate.value;
	vartodate    = document.form1.todate.value;
	varfromcur   = document.form1.fromcur.value;
	varfromledger  = document.form1.fromledger.value;
	vartoledger    = document.form1.toledger.value;
	varfromprj     = document.form1.fromproject.value;
	varfromcst     = document.form1.fromcostcent.value;
	varcltprj      = document.form1.clientproject.value;
	varcltcst      = document.form1.clientcostcent.value;
	varsubproj     = document.form1.subproj.value;
	varsubcostcent = document.form1.subcostcent.value;
  
	varsubproj     = varsubproj.replace(/%/g,'-');
	varsubcostcent = varsubcostcent.replace(/%/g,'-');
 
    
	
	str =   'Rep_DailyReport.php?fromdate='+varfromdate+'&todate='+vartodate+'&fromledger='+varfromledger
           +'&toledger='+vartoledger+'&fromcur='+varfromcur
		   +'&curr='+varcurrency
		   +'&alltypes='+varalltypes
		   +'&alltypesdebit='+varalltypesdebit
		   +'&alltypescredit='+varalltypescredit
		   +'&alltypesroot='+varalltypesroot
		   +'&jvtype='+varjvtype+'&jvtype_root='+varjvroot
		   +'&fromproject='+varfromprj+'&fromcst='+varfromcst
		   +'&cltprj='+varcltprj+'&cltcst='+varcltcst
		   +'&reptype='+varreptype
		   +'&groupby1='+vargroupby1+'&groupby2='+vargroupby2+'&groupby3='+vargroupby3
		   +'&subproj='+varsubproj+'&subcostcent='+varsubcostcent
		   +'&jvtype_db='+varcheckDebit
		   +'&jvtype_cr='+varcheckCredit
		   +'&ledgernumbers='+varledgers
		   ;
		   
	if (printaccess==1)
		window.open(str);	   
	else	
		window.open(str,"Report", "width=900,height=600,screenX=0,left=50,screenY=0,top=0,status=no,menubar=no,scrollbars=yes,resizable=yes");
 
}

function ExportExcel()
{
	width = screen.width - 10;
	height = screen.height - 66;
	var str='';
	var ledgers;
	var checkboxname;
	var ledgernumbers='';
	var ledgernumbersTemp='';
	var totemp=0;
 
  
  	 
	for (var i=0; i < document.form1.currency.length; i++)
	{
		if (document.form1.currency[i].checked)
		{
		var varcurrency = document.form1.currency[i].value;
		}
	}
	
	varverticalexcel = 0;
	if (document.form1.verticalexcel.checked==true)
		varverticalexcel = 1;	
	
	
    varalltypes = 0;
    varalltypesdebit = 0;
    varalltypescredit = 0;
	varalltypesroot = 0;
	
	if (document.form1.alltypes.checked==true)
		varalltypes = 1;	

	if (document.form1.alltypesdebit.checked==true)
		varalltypesdebit = 1;	
	
	if (document.form1.alltypescredit.checked==true)
		varalltypescredit = 1;	
		
	if (document.form1.alltypesroot.checked==true)
		varalltypesroot = 1;	
	
	
	var x,D,C, varjvtype,varcheckDebit,checkDebit, ledgername, varledgers;
	varjvtype = '';
	varcheckDebit = '';
	varcheckCredit = '';
	varledgers = '';
	varjvroot = '';
  
	for (var i=1; i <= document.form1.i.value; i++)
	{
		checkname='type'+i;
		checkDebit='debit'+i;
		checkCredit='credit'+i;
		ledgername='ledger'+i;
		checkroot='root'+i;
	
		D = document.getElementById(checkDebit);
		C = document.getElementById(checkCredit);
		x = document.getElementById(checkname);
		m = document.getElementById(ledgername);
		r = document.getElementById(checkroot);

		if (varalltypesdebit == 0 && D.checked) {
			if (varcheckDebit.length > 0) varcheckDebit += ",";
			varcheckDebit += "'"+x.value+"'";
		}	
		
		if (varalltypescredit == 0 && C.checked) {
			if (varcheckCredit.length > 0) varcheckCredit += ",";
			varcheckCredit += "'"+x.value+"'";
		}	
	 
		if (varalltypes == 0 && x.checked)  {
			if (varjvtype.length > 0) varjvtype += ",";
			varjvtype += "'"+x.value+"'";
		}	
		
		if (m.value != '') {
			if (varledgers.length > 0) varledgers  += "-";
			varledgers += x.value+":"+m.value;
		}
		
		if (varalltypesroot == 0 && r.checked)  {
			if (varjvroot.length > 0) varjvroot += ",";
			varjvroot += "'"+r.value+"'";
		}
   
	}
  
  
 
	for (var i=0; i < document.form1.reptype.length; i++)
	{
		if (document.form1.reptype[i].checked)
		{
		var varreptype = document.form1.reptype[i].value;
		}
	}
  
  
	//Getting Group By Levels 1 and 3
	vargroupby1   = document.form1.groupbylevel1value.value;
	vargroupby2   = document.form1.groupbylevel2value.value;
	  
	//Getting Assigngroupby Values for level 3
	var ob = document.form1.assigngroupby;
	var vargroupby3 = '';
	for (var i = 0; i < ob.options.length; i++) {
		if (vargroupby3.length > 0)
			vargroupby3 += ",";
				
		vargroupby3 += ob.options[i].value;
	}
	  
	// If No Group By Option Selected
	if (vargroupby3 == '') vargroupby3 = 1;
  
	varfromdate  = document.form1.fromdate.value;
	vartodate    = document.form1.todate.value;
	varfromcur   = document.form1.fromcur.value;
	varfromledger  = document.form1.fromledger.value;
	vartoledger    = document.form1.toledger.value;
	varfromprj     = document.form1.fromproject.value;
	varfromcst     = document.form1.fromcostcent.value;
	varcltprj      = document.form1.clientproject.value;
	varcltcst      = document.form1.clientcostcent.value;
	varsubproj     = document.form1.subproj.value;
	varsubcostcent = document.form1.subcostcent.value;
  
	varsubproj     = varsubproj.replace(/%/g,'-');
	varsubcostcent = varsubcostcent.replace(/%/g,'-');
 
	//If Summary Option Is Selected With Vertical Option
	if (varverticalexcel == 1 && varreptype == 2) FileName = 'Exp_DailyReportVertical.php'; else FileName = 'Exp_DailyReport.php';
	
	str =   FileName+'?fromdate='+varfromdate+'&todate='+vartodate+'&fromledger='+varfromledger
           +'&toledger='+vartoledger+'&fromcur='+varfromcur
		   +'&curr='+varcurrency
		   +'&alltypes='+varalltypes
		   +'&alltypesdebit='+varalltypesdebit
		   +'&alltypescredit='+varalltypescredit
		   +'&alltypesroot='+varalltypesroot
		   +'&jvtype='+varjvtype+'&jvtype_root='+varjvroot
		   +'&fromproject='+varfromprj+'&fromcst='+varfromcst
		   +'&cltprj='+varcltprj+'&cltcst='+varcltcst
		   +'&reptype='+varreptype
		   +'&groupby1='+vargroupby1+'&groupby2='+vargroupby2+'&groupby3='+vargroupby3
		   +'&subproj='+varsubproj+'&subcostcent='+varsubcostcent
		   +'&jvtype_db='+varcheckDebit
		   +'&jvtype_cr='+varcheckCredit
		   +'&ledgernumbers='+varledgers
		   ;
		   
	
	window.open(str);	   
	
}



function setlist()
{
	width = screen.width - 10;
	height = screen.height - 66;
	var str='';
  	
  	 
	for (var i=0; i < document.form1.currency.length; i++)
	{
		if (document.form1.currency[i].checked)
		{
		var varcurrency = document.form1.currency[i].value;
		}
	}
    
	varalltypes = 0;
	if (document.form1.alltypes.checked)
		varalltypes = 1;	


	var x, varjvtype;
	varjvtype = '';
	for (var i=1; i <= document.form1.i.value; i++)
	{
		checkname='type'+i;
	
		x = document.getElementById(checkname);
		if (varalltypes == 0 && x.checked) 
			varjvtype = varjvtype+",'"+x.value+"'";
	}
	
	varjvtype = varjvtype.substr(1);
  
	varfromdate   = document.form1.fromdate.value;
	vartodate     = document.form1.todate.value;
	varfromcur    = document.form1.fromcur.value;
	varfromledger = document.form1.fromledger.value;
	vartoledger   = document.form1.toledger.value;
   
    
	str =   'Form_DailyReport.php?List=yes&fromdate='+varfromdate+'&todate='+vartodate+'&fromledger='+varfromledger
           +'&toledger='+vartoledger+'&fromcur='+varfromcur+'&curr='+varcurrency+'&alltypes='+varalltypes+'&jvtype='+varjvtype;
	
	document.form1.action=str;
	document.form1.submit();

}




function LoadOptions() {
	x = document.form1;
    win = window.open('List_ReportsOptionsTemplates.php?formname=dailyreport', 'List_ReportsOptionsTemplates', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=1040,height=450,top=100,left=0');		
	win.creator = self;
	win.focus();		

}



function SelectOptions() {
	x = document.form1;
	vTemplateId = x.templateid.value;
	
    win = window.open('Form_ReportsOptions.php?isseloption=1&templateid='+vTemplateId+'&formname=dailyreport', 'Form_ReportsOptions', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=1200,height=550,top=100,left=0');
	win.creator = self;
	win.focus();		

}


function ClearPage() {
	x = document.form1;
	
	//x.templateid.value = '';
	//x.action='Form_DailyReport.php';
	//x.submit();
	
	window.location ='Form_DailyReport.php';
	
}

function AddOption(selectId, text, value) {
	var ob = document.form1.assigngroupby;
	var i = ob.options.length;
	var o = new Option(text,value);
	ob.options[i] = o;
	
}

function RemoveOption(selectId) {
	var ob = document.form1.assigngroupby;
	
	//Empty Assigngroupby Values
	for (var k=0; k < ob.options.length; k++) {
		ob.options[k].selected = true;
	}
	
	while (ob.selectedIndex != -1)
	{  
		if (ob.selectedIndex != -1) 
		  ob.options[ob.selectedIndex] = null;
	}
	

}



-->
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Daily Report</title>
<style type="text/css">
<!--
body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 14px;
}
a:link {
	text-decoration: none;
}
a:visited {
	text-decoration: none;
}
a:hover {
	text-decoration: none;
}
a:active {
	text-decoration: none;
}
.style1 {
	color: #FF0000;
	font-family: "Times New Roman", Times, serif;
	font-size: 20px;
}
.style8 {font-size: 11px; color: #999999; font-weight: bold; }
.style11 {font-size: 11px}
body {
	margin-top: 0px;
}
-->
</style>

<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
	<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}
.style11 {color: #999999; font-weight: bold; }
.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; }
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

</style>	
	
	</head>

<body  >
HERE;


echo "<form id='form1' name='form1' method='post' action='$_SERVER[PHP_SELF]' >
		<input type='hidden' name='templateid' value='$templateid'>";
		
echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";


echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>Daily Report</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";

echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='30'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px' align='left'>";
				
echo" <button type='button' id='btnClear' name='btnClear' onclick='ClearPage();return false;' title='Clear'><span><span>Clear</span></span></button>";
	echo"<button type='button' id='btnPreview' name='btnPreview'  onClick=\"PrintPrev('".$printaccess."')\"; title='Preview'><span><span>Preview</span></span></button>";
//echo" <input type='button' name='btnSearch' id='btnSearch' value='Search' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;' onclick='setlist()' title='Search' />   ";  
	echo"<button type='button' id='btnPreview' name='btnPreview'  onClick=\"setlist()\"; title='Search'><span><span>Search</span></span></button>";

	if ($ExporttoExcel==1){
		echo" <button type='button' id='btnExcel' name='btnExcel' onClick=\"ExportExcel()\"; title='Export Excel'><span><span>Export Excel</span></span></button>";

	}	
	  
	echo" <button type='button' id='btnLoadOptions' name='btnLoadOptions' onClick=\"LoadOptions()\"; title='Load Template'><span><span>Load Options</span></span></button>";  
	echo" <button type='button' id='btnSelectOptions' name='btnSelectOptions' onClick=\"SelectOptions()\"; title='Select Options'><span><span>Select Options</span></span></button>"; 
	
	

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

		
echo"<tr><td id='content-body'>
<table border='0'  align='center' valign='top' width=100% class='tablecontent'>
   <tr bgcolor='#F7F7F7'><td valign='top' align='left' colspan='10'>";		
   		
echo '  <table height="534" border="0" align="center" width="100%">';


if (!isset($alltypes))       $alltypes = 1;
if (!isset($alltypesdebit))  $alltypesdebit = 1;
if (!isset($alltypescredit)) $alltypescredit = 1;

//Load Reports Options Template
if ($templateid != '') {
	$querytemplate = " Select Groupby_Level1Value, Groupby_Level2Value, Groupby_Level3Value, Groupby_Level1Name, Groupby_Level2Name, Groupby_Level3Name, ".
					 " JournalType_alltypes, JournalType_exclude, JournalType_types,  AllTypesCredit, AllTypesDebit, FromLedger_Number, ToLedger_Number, ".
					 " FromClient_Code, ToClient_Code, FromSupplier_Code, ToSupplier_Code, FromSalesman_Code, ToSalesman_Code, FromItem_Code, ToItem_Code, ".
					 " FromWarehouse_Code, ToWarehouse_Code, FromGroup_Code, ToGroup_Code, FromSubgroup_Code, ToSubgroup_Code, FromProject_Code, ToProject_Code, ".
					 " FromMasterProject_Code, ToMasterProject_Code, FromCostcent_Code, ToCostcent_Code, FromMasterCostcent_Code, ToMasterCostcent_Code,  ".
					 " JournalType_db, JournalType_cr, LedgersPerType ".
					 " From ReportsOptions_Templates ".
					 " Where ReportsOptions_Templates.User_Account = '".$HTTP_SESSION_VARS['useraccount']."' ".
					 " And   Template_Id = '".$templateid."'";
	//echo $querytemplate;			   
	$resulttemplate=mysql_query($querytemplate,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resulttemplate)){
	  extract($rows);
	}	

	$groupbylevel1value = $Groupby_Level1Value;
	$groupbylevel2value = $Groupby_Level2Value;
	$groupbylevel3value = $Groupby_Level3Value;
	$groupbylevel1name  = $Groupby_Level1Name;
	$groupbylevel2name  = $Groupby_Level2Name;
	$groupbylevel3name  = $Groupby_Level3Name;	
	
	
	$fromproject    = $FromProject_Code;
	$fromcostcent   = $FromCostcent_Code;
	$clientproject  = $FromMasterProject_Code;
	$clientcostcent = $FromMasterCostcent_Code;
	
	$fromledger 	= $FromLedger_Number;
	$toledger 		= $ToLedger_Number;
	
	$alltypes       = $JournalType_alltypes;
	$alltypesdebit  = $AllTypesDebit;
	$alltypescredit = $AllTypesCredit;
	
	$journaltypes    = $JournalType_types;
	$journaltypes_db = $JournalType_db;
	$journaltypes_cr = $JournalType_cr;
	$journalledgers  = $LedgersPerType;
	
	$reportoptions = "Group by:  ";
	if ($Groupby_Level1Name != '' ) $reportoptions = $reportoptions . $Groupby_Level1Name.", ";
	if ($Groupby_Level2Name != '' ) $reportoptions = $reportoptions . $Groupby_Level2Name.", ";
	if ($Groupby_Level3Name != '' ) $reportoptions = $reportoptions . $Groupby_Level3Name.", ";
	$reportoptions = substr($reportoptions, 0, strlen($reportoptions)-2) . "\n" . "Filter by: \n";
	
	if ($FromLedger_Number != '')		  $reportoptions .= "-> Ledger From: ". $FromLedger_Number . "  To: ". $ToLedger_Number ."\n";
	if ($FromProject_Code != '')   		  $reportoptions .= "-> Project From: ". $FromProject_Code . "\n";
	if ($FromCostcent_Code != '') 		  $reportoptions .= "-> Costcent From: ". $FromCostcent_Code . "\n";
	if ($FromMasterProject_Code != '')    $reportoptions .= "-> Master Project From: ". $FromMasterProject_Code . "\n";
	if ($FromMasterCostcent_Code != '')   $reportoptions .= "-> Master Costcent From: ". $FromMasterCostcent_Code . "\n";

  
}

echo"<tr>";
echo"	<td valign='middle' bgcolor='#F5F5F5' colspan='2'><span class='style8'>Date</span></td>";
echo"	<td valign='middle' bgcolor='#F5F5F5' ><span class='style8'>Currency</span></td>";
echo"	<td valign='middle' bgcolor='#F5F5F5' ><span class='style8'>Reports</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5' colspan='5'><span class='style8'>Journal Type</span></td>";
echo"</tr>";

echo" <tr height='15px'>";	  
echo"   <td width='5%'  valign='middle' bgcolor='#F5F5F5' ><span class='style8'>From</span></td>";
echo"   <td width='15%' valign='middle' bgcolor='#F5F5F5' ><span class='style8'>";
echo"      <input type='text' name='fromdate' value='$fromdate' maxlength='10' size='10'>";
echo"      <A HREF=\"#\" onClick=\"displayCalendar(document.form1.fromdate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span></td>";
echo"   <td width='15%' rowspan=2 valign='top' bgcolor='#F5F5F5'><span class='style8'><label>";
echo"      <input name='currency' type='radio' ";
if ($curr=='currency1')
	echo " checked ";
echo" value='currency1'>Currency</label></span>";
echo"      <span class='style8'><select name='fromcur' id='fromcur'>";
		//echo"<option></option>";
		while($curf=mysql_fetch_row($resultcurrency)){
		echo"<option ";
			if ($fromcur==$curf[0])
				echo " selected ";
		echo" value='$curf[0]'>$curf[0] </option>";
		}
		echo"</select></span>";

echo"   <br><span class='style8'><label>";
echo"      <input name='currency' type='radio' value='$rowsetup[0]' "; if($defaultcurr == $rowsetup[0]) echo "checked"; echo">$rowsetup[0]</label></span>";
echo"   <br><span class='style8'><label>";
echo"      <input name='currency' type='radio' value='$rowsetup[1]' "; if($defaultcurr == $rowsetup[1]) echo "checked"; echo">$rowsetup[1]</label></span></td>";

echo"   <td width='20%' rowspan=2 valign='top' bgcolor='#F5F5F5'>";
echo"   <span class='style8'><label>";
		if ($reptype=="1" || $reptype=="" )
           echo "<input name='reptype' type='radio' value='1'  checked/>";
		else
			echo "<input name='reptype' type='radio' value='1'  />";
		echo "Daily</label></span>";
echo"   <br><span class='style8'><label>";
		
		if ($reptype=="2" )
           echo "<input name='reptype' type='radio' value='2'  checked />";
		else	   
		   echo "<input name='reptype' type='radio' value='2'  />";
		   
		echo "Summary</label></span>";
echo"	<br><br><span class='style8'><input type='checkbox' name='verticalexcel' id='verticalexcel' value='1'  />Vertical Summary Excel</span></td>";
//echo"		<input type='checkbox' name='showbusinesstype' id='showbusinesstype' value='$showbusinesstype'>Show Business Type</span></td>";

echo"   <td width='6%' bgcolor='#F5F5F5'><span class='style8'> ";
echo"   <input name='alltypes' type='checkbox' id='alltypes' value='$alltypes' ";
		if ($alltypes==1) echo " checked ";
echo"    onclick='select_alljvtypes();' /> All </td>";

echo"	<td width='8%'  bgcolor='#F5F5F5' ><label><input name='alltypesdebit' type='checkbox' id='alltypesdebit'  onclick='select_alldbtypes();'  "; if ($alltypesdebit==1)  echo " checked "; echo" value='$alltypesdebit' ><span class='style8'> All</span></label></td>";
echo"	<td width='8%'  bgcolor='#F5F5F5' ><label><input name='alltypescredit' type='checkbox' id='alltypescredit'  onclick='select_allcrtypes();'  "; if ($alltypescredit==1) echo " checked "; echo" value='$alltypescredit' ><span class='style8'> All</span></label></td>";
echo"	<td width='7%'  bgcolor='#F5F5F5' ><label><input name='alltypesroot' type='checkbox' id='alltypesroot'  onclick='select_allroottypes();'  "; if ($alltypesroot==1) echo " checked "; echo" value='$alltypesroot' ><span class='style8'> All</span></label></td>";
echo"	<td width='16%'  bgcolor='#F5F5F5' ><span class='style8'>Ledgers(Ex:234100009,3)</span></td>";
echo"</tr>";


echo"<tr>";
echo"   <td width='5%' valign='top' bgcolor='#F5F5F5'><span class='style8'>To</span></td>";
echo"   <td width='15%' valign='top' bgcolor='#F5F5F5'><span class='style8'>";
echo"      <input type='text' name='todate' value='$todate'  maxlength='10' size='10'>";
echo"      <A HREF=\"#\" onClick=\"displayCalendar(document.form1.todate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span></td>";

echo"<td colspan=5 rowspan='2' valign='top' bgcolor='#F5F5F5'><div style='overflow:auto; height:180px; color:red width:500px;' >
      <table border='0' width='100%'>";
	

  
$i = 0;

$queryjvtype="select type_name from jourtype order by type_name";
$resultjvtype=mysql_query($queryjvtype,$connection) or die(mysql_error());

while($jvtype=mysql_fetch_row($resultjvtype)){
	
	$i = $i + 1;
	
	$type  = 'type'.$i;
    $$type = $jvtype[0];
	
	$debit  = 'debit'.$i;
	$credit = 'credit'.$i;
	$ledger = 'ledger'.$i;
	$root = 'root'.$i;
	
	$JvSelected = '';
	$JvDbSelected = '';
	$JvCrSelected = '';
	
	if ($journaltypes != '' && $alltypes != 1) {
		$pos = strrpos($journaltypes, $jvtype[0]);
		//echo $pos;
		if ($pos !== false) { 
			$JvSelected = ' checked ';
		}
	}
	else if ($alltypes == 1) {
		$JvSelected   = ' checked ';
	}
	
	if ($journaltypes_db != '' && $alltypesdebit != 1) {
		$pos = strrpos($journaltypes_db, $jvtype[0]);
	
		if ($pos !== false) { 
			$JvDbSelected = ' checked ';
		}
	}
	else if ($alltypesdebit == 1) {
		$JvDbSelected = ' checked ';
	}
	
	if ($journaltypes_cr != '' && $alltypescredit != 1) {
		$pos = strrpos($journaltypes_cr, $jvtype[0]);
	
		if ($pos !== false) { 
			$JvCrSelected = ' checked ';
		}
	}
	else if ($alltypescredit == 1) {
		$JvCrSelected = ' checked ';
	}
	
	if ($journalledgers != ''){
		$trims = explode('-', $journalledgers);

		$k = 0;
		foreach ($trims as $trim) {
			$ledgers  = explode(":", $trim);
			
			if ($ledgers[0] == $jvtype[0])
				$$ledger = $ledgers[1];
					
			$k = $k + 1;
		}
	}
	
	echo"<tr bgcolor='E8E8E8' height='20'>";
    echo"<td width='70px;'><span class='style8'><label><input type='checkbox' name='$type' id='$type' value='".$$type."' $JvSelected onclick='checkledger(this,$ledger)' /> $jvtype[0] </label></span></td>";
	echo"<td width='85px;'><span class='style8'><label><input type='checkbox' name='$debit' id='$debit' value='".$$type."' $JvDbSelected /> Debit </label></span></td>";
	echo"<td width='85px;'><span class='style8'><label><input type='checkbox' name='$credit' id='$credit' value='".$$type."' $JvCrSelected /> Credit </label></span></td>";
	echo"<td width='75px;'><span class='style8'><label><input type='checkbox' name='$root' id='$root' value='".$$type."' /> Root </label></span></td>";
	echo"<td width='140px;'><span class='style8'><label><input type='text' style='width:130px; height:20' name='".$ledger."' id='$ledger' value='".$$ledger."'></label></span></td>";
	echo"</tr>";	

}

echo "<input type='hidden' name='i' value='$i'>";
echo "</td></tr></table></div></td>";   
echo"</tr>";


echo" <tr>";
echo"   <td width='55%' colspan='4' valign='top' bgcolor='#F5F5F5'>";
echo" 	<table border='0' align='left' width='100%' cellpadding=0 cellspacing=3>";

echo"   <tr height='15'>";
echo"    <td colspan='3'  bgcolor='#F5F5F5'><span class='style8'>Group By </span></td>";
echo"   </tr>";

echo"   <tr>";
		
echo"     <td width='35%' valign='top' bgcolor='#F5F5F5'>";
echo"		<select name='availgroupby' multiple='multiple' size='8'>    
				<option value='20' name='Transaction Project(Parent)'>Transaction Project(Parent)</option>
				<option value='4' name='Transaction Project(Detail)'>Transaction Project(Detail)</option>
				<option value='21' name='Archieve Project(Parent)'>Archieve Project(Parent)</option>
				<option value='5' name='Archieve Project(Detail)'>Archieve Project(Detail)</option>
				<option value='22' name='Transaction Costcenter(Parent)'>Transaction Costcenter(Parent)</option>
				<option value='6' name='Transaction Costcenter(Detail)'>Transaction Costcenter(Detail)</option>
				<option value='23' name='Archieve Costcenter(Parent)'>Archieve Costcenter(Parent)</option>
				<option value='7' name='Archieve Costcenter(Detail)'>Archieve Costcenter(Detail)</option>
				<option value='9'  name='Business Type'>Business Type</option>
				<option value='10' name='Profit Center'>Profit Center</option>
			</select>";
echo"   </td>";

echo"   <td width='25%' valign='middle' bgcolor='#F5F5F5'>";
echo"   	<div style='text-align: center;' class='style8'>Level 1 <input type='button' name='btnSelLevel1' value=' > ' onClick='MoveLevel12(1);' title='Add'>  <input type='button' name='btnUnSelLevel1' value=' < ' onClick='RemoveLevel12(1);' title='Remove'></div><br>";
echo"   	<div style='text-align: center;' class='style8'>Level 2 <input type='button' name='btnSelLevel2' value=' > ' onClick='MoveLevel12(2);' title='Add'>  <input type='button' name='btnUnSelLevel2' value=' < ' onClick='RemoveLevel12(2);' title='Remove'></div><br>";
echo"   	<div style='text-align: center;' class='style8'>Level 3 <input type='button' name='btnSelLevel3' value=' > ' onClick='MoveLevel3();'   title='Add'>  <input type='button' name='btnUnSelLevel3' value=' < ' onClick='RemoveLevel3();'   title='Remove'></div>";
echo"   </td>";


echo"   <td width='40%' valign='top' bgcolor='#F5F5F5'>";
echo"   	<span class='style8'><input type='hidden' name='groupbylevel1value' value='$groupbylevel1value' style='width:190px;'></span>";
echo"   	<span class='style8'><input type='hidden' name='groupbylevel2value' value='$groupbylevel2value' style='width:190px;'></span>";
echo"   	<span class='style8'><input type='text' name='groupbylevel1name' value='$groupbylevel1name' style='width:190px;' readonly></span><br>";
echo"   	<span class='style8'><input type='text' name='groupbylevel2name' value='$groupbylevel2name' style='width:190px;' readonly></span><br>";
echo"   	<span class='style8'>
			<select name='assigngroupby' multiple='multiple' size='5' style='width:190px;'>  ";  
			
			if ($groupbylevel3value !=''){
				$level3values = explode(",", $groupbylevel3value);
				$level3names  = explode(",", $groupbylevel3name);
				
				$k = 0;
				foreach ($level3values as $level3value) { echo $level3value;
					$level3names  = explode(",", $groupbylevel3name);
					echo"<option value='$level3value' name='$level3names[$k]'>$level3names[$k]</option>";
					
					$k = $k + 1;
				}

			
			}
			else
				echo"<option value='' name=''></option>";
				
echo"		</select></span>";
echo"   </td></tr>";
echo"   </table></td>";

echo" </tr>";

echo" <tr>";
echo"   <td colspan='9' bgcolor='#F5F5F5'><span class='style8'>Selected Options</span></td>";
echo" </tr>";


echo"   <tr>";
echo"   <td colspan='9' bgcolor='#F5F5F5' ><span class='style8'>";
echo"  		<textarea name='reportoptions' rows='6' cols='100' readonly >".htmlspecialchars(str_replace("<br>", "\n", $reportoptions), ENT_QUOTES)."</textarea>
			</span>
		</td>";
echo" </tr>";


//echo"<tr><td colspan=8 bgcolor='#F5F5F5' align='center'><span class='style8'><input type='checkbox' name='totemp' value='$totemp'>Save Options to template</td></tr>";
	

echo"   <input type='hidden' name='fromledger' id='fromledger' size='10' value='".$fromledger."' />
		<input type='hidden' name='toledger' id='toledger' size='10' value='".$toledger."' />
		<input type='hidden' name='fromproject' id='fromproject' size='10' value='".$fromproject."' />
		<input type='hidden' name='fromcostcent' id='fromcostcent' size='10' value='".$fromcostcent."' />
		<input type='hidden' name='clientcostcent' id='clientcostcent' size='10' value='".$clientcostcent."' />
		<input type='hidden' name='clientproject' id='clientproject' size='10' value='".$clientproject."' />
		<input type='hidden' name='subproj' id='subproj' style='width:120px' value='".$subproj."' />
		<input type='hidden' name='subcostcent' id='subcostcent' style='width:120px' value='".$subcostcent."' />";
    

echo" <tr height='15'>";
echo"   <td colspan='9' bgcolor='#F5F5F5'><span class='style8'>&nbsp;</span></td>";
echo" </tr>";
   /* <tr>
      <td colspan='9' align='center' bgcolor='#F5F5F5'>
	  <input type='button' name='btnClear' value='Clear' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;' onClick=\"ClearPage()\"; />
	  <input type='button' name='btnPreview' id='btnPreview' value='Preview' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;' onClick=\"PrintPrev('".$printaccess."')\"; />
	  <input type='".$ExporttoExcelStatus."' name='btnExcel' id='btnExcel' value='Export Excel' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;' onClick=\"ExportExcel()\";/>	  
	  <input type='button' name='btnSearch' id='btnSearch' value='Search' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;' onclick='setlist()' title='Search' />     
	  <input type='button' name='btnLoadOptions' id='btnLoadOptions' value='Load Options' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;' onClick=\"LoadOptions()\"; title='Load Template' />
	  <input type='button' name='btnSelectOptions' id='btnSelectOptions' value='Select Options' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;' onClick=\"SelectOptions()\"; title='Select Options' />
      </td>
    </tr>";*/



if ($List=='yes')
{

	if ($curr == 'currency1')  $curr='null';  
	else 	   $curr="'".$curr."'";
	
	if (empty($jvtype))	  $jvtype = 'null';      
	$jvtypelen = 0;
	if (!empty($jvtype))   $jvtypelen = strlen($jvtype);
		   
 
	$fromcur     	 = GetSQLValueString($fromcur, 'text');
	$fromledger      = GetSQLValueString($fromledger, 'text');
	$toledger        = GetSQLValueString($toledger, 'text');
		   
	if (!empty($fromdate)) $fromdate = explodeword($fromdate);
	if (!empty($todate))   $todate = explodeword($todate);  

	$fromdate  	   	 = GetSQLValueString($fromdate, 'date');
	$todate          = GetSQLValueString($todate, 'date');   
	$fromproject     = GetSQLValueString($fromproject, 'text');
	$fromcostcent  	 = GetSQLValueString($fromcostcent, 'text');	     
			 
			 
	
	$query = " Select date_format(journal.journal_date ,'%d/%m/%Y') as journal_date , journaldt.ledger_number ,  ledger.ledger_name , ".
	         " journal.Journal_type ";

	$a=0;
	$b=0;
	$c=0;
	$d=0;
	$e=0;
	$f=0;
	$g=0;
	$h=0;
	$i=0;
	$j=0;
	$k=0;
	$l=0;
	$m=0;
	$n=0;
	$o=0;
	$kk=0;
	$ll=1;
	if (strchr($jvtype,'CV')!='' or strchr($jvtype,'DV')!='' or strchr($jvtype,'PV')!='' or strchr($jvtype,'RV')!='' or $jvtype=='null')
	{
	$query = $query." ,if(journal.Journal_type='CV' or journal.Journal_type='DV' or journal.Journal_type='PV' or journal.Journal_type='RV',trans.trans_reference ";
	$a=1;
	}
	if (strchr($jvtype,'AD')!='' or $jvtype=='null')
	{$query = $query." ,if(journal.Journal_type='AD',adjustment.adjustment_reference ";
	$b=1;}

	if (strchr($jvtype,'CL')!='' or strchr($jvtype,'EX')!='' or strchr($jvtype,'JV')!=''  or strchr($jvtype,'OP')!='' or $jvtype=='null')
	{$query = $query." ,if(journal.Journal_type='CL' or journal.Journal_type='EX' or journal.Journal_type='JV' or journal.Journal_type='OP',journal.Journal_refgovernment ";
	$c=1;
	}
	if (strchr($jvtype,'DR')!='' or strchr($jvtype,'DI')!=''  or $jvtype=='null')
	{$query = $query." ,if(journal.Journal_type='DI' or journal.Journal_type='DR' ,Deliveries.Delivery_Reference ";
	$d=1;
	}

	if (strchr($jvtype,'FV')!='' or $jvtype=='null')
	{$query = $query." ,if(journal.Journal_type='FV',assets.assets_ref ";
	$e=1;
	}
	if (strchr($jvtype,'JP')!='' or $jvtype=='null')
	{$query = $query." ,if(journal.Journal_type='JP',purchasesjv.PurchaseJV_Reference ";
	$f=1;
	}
	if (strchr($jvtype,'JS')!='' or $jvtype=='null')
	{$query = $query." ,if(journal.Journal_type='JS',salesjv.salesjv_reference ";
	$g=1;
	}
	if (strchr($jvtype,'PI')!='' or strchr($jvtype,'PR')!=''  or $jvtype=='null')
	{$query = $query." ,if(journal.Journal_type='PI' or journal.Journal_type='PR',purchases.purchase_reference ";
	$h=1;
	}
	if (strchr($jvtype,'RI')!='' or strchr($jvtype,'RR')!='' or $jvtype=='null')
	{$query = $query." ,if(journal.Journal_type='RI' or journal.Journal_type='RR',retail.sale_reference ";
	$i=1;
	}
	if (strchr($jvtype,'SE')!='')
	{$query = $query." ,if(journal.Journal_type='SE' ,stkeval.stkeval_reference ";
	$k=1;
	}
	if ((strchr($jvtype,'SI')!='' or strchr($jvtype,'SR')!='' or $jvtype=='null') and $Module_Delivery==0)
	{$query = $query." ,if(journal.Journal_type='SI' or journal.Journal_type='SR' ,deliveries.Delivery_Reference ";
	$l=1;
	}
	else
	if ((strchr($jvtype,'SI') or strchr($jvtype,'SR')!='' or $jvtype=='null') and $Module_Delivery==1)
	{$query = $query." ,if(journal.Journal_type='SI' or journal.Journal_type='SR' ,sales.sale_reference ";
	$m=1;
	}
	if (strchr($jvtype,'TP')!=''  or $jvtype=='null')
	{$query = $query." ,if(journal.Journal_type='TP' ,transp.transp_reference ";
	$n=1;
	}
	if (strchr($jvtype,'TV')!='' or strchr($jvtype,'SV')!=''  or $jvtype=='null')
	{$query = $query." ,if(journal.Journal_type='TV' or journal.Journal_type='SV',salejour.salejour_reference ";
	$o=1;
	}

	$query = $query." ,trans.trans_reference ";
	$kk=$a+$b+$c+$d+$e+$f+$g+$h+$i+$j+$k+$l+$m+$n+$o;

	if ($kk>0)
	while ($ll<=$kk)
	{
	$query = $query  .")";
	$ll=$ll+1;
	}


	$query = $query." as typenumber ";


	$query = $query.",journal.journal_refgovernment, journaldt_amount1, journaldt_amount2";

	$query = $query.", sum(ifnull(if(Journaldt_dbcd = 'D', if(upper($curr)='".$Base1."', journaldt_amount1, if(upper($curr)='".$Base2."', journaldt_amount2, journaldt_amount)),0),0)) as DebitAmount ";
	$query = $query.", sum(ifnull(if(Journaldt_dbcd = 'C', if(upper($curr)='".$Base1."', journaldt_amount1, if(upper($curr)='".$Base2."', journaldt_amount2, journaldt_amount)),0),0)) as CreditAmount ";
	$query = $query.",ifnull(jourtype.jourtype_group,jourtype.type_name) as jourtype_group
	from journaldt
	Left Outer join Ledger on JournalDt.Ledger_number = Ledger.Ledger_number
	Inner join Journal on JournalDt.Journal_id = Journal.Journal_id
	left join jourtype on jourtype.type_name = journal.Journal_type
	left join trans on trans.Journal_id = journal.Journal_id
	";

	if (strchr($jvtype,'AD')!='' or $jvtype=='null')
	$query = $query . " left join adjustment on adjustment.journal_id = journal.journal_id ";
	if (strchr($jvtype,'FV')!='' or $jvtype=='null')
	$query = $query . " left join assets on assets.journal_id = journal.journal_id ";
	if (strchr($jvtype=='JP')!='' or $jvtype=='null')
	$query = $query . " left join purchasesjv on purchasesjv.journal_id = journal.journal_id ";

	if (strchr($jvtype,'JS')!='' or $jvtype=='null')
	$query = $query . " left join salesjv on salesjv.journal_id = journal.journal_id ";

	if (strchr($jvtype,'PI')!='' or strchr($jvtype,'PR')!=''  or $jvtype=='null')
	$query = $query . " left join purchases on purchases.journal_id = journal.journal_id ";
	if (strchr($jvtype,'RI')!='' or strchr($jvtype,'RR')!='' or $jvtype=='null')
	$query = $query . " left join retail on retail.journal_id = journal.journal_id ";
	if (strchr($jvtype,'SE')!='')
	$query = $query . " left join stkeval on stkeval.journal_id = journal.journal_id ";

	if ($jvtype=='SI' or $jvtype=='SR'  or $jvtype=='null'  and $Module_Delivery==1)
	$query = $query . " left join sales on sales.journal_id = journal.journal_id ";
	if (strchr($jvtype,'TP')!=''  or $jvtype=='null')
	$query = $query . " left join transp on transp.journal_id = journal.journal_id ";
	if (strchr($jvtype,'TV')!='' or strchr($jvtype,'SV')!=''  or $jvtype=='null')
	$query = $query . " left join salejour on salejour.journal_id = journal.journal_id ";
	if (strchr($jvtype,'DI')!='' or strchr($jvtype,'DR')!=''  or $jvtype=='null' or (strchr($jvtype,'SI')!='' or strchr($jvtype,'SR')!=''  or $jvtype=='null'  and $Module_Delivery==0))
	$query = $query . " left join Deliveries on journal.journal_id = deliveries.journal_id ";


	$query = $query . " where 
	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 ledger.Ledger_number between ifNull(".$fromledger.",ledger.Ledger_number) and ifNull(". $toledger.",ledger.Ledger_number) 
	";

	
	if (($fromcur != 'null' )  and ($curr == 'null'))
		$query = $query." and Upper(JournalDt.Currency_code) = Upper(".$fromcur.") ";
			  
	$query = $query." and (($alltypes = 1 and 1 = 1) or ($alltypes = 0 and $jvtypelen = 0 and 1 = 1) or ($alltypes = 0 and $jvtypelen > 0 and journal.Journal_type in ($jvtype)) ) ";
	
	$query = $query."group by ifnull(jourtype.jourtype_group,jourtype.type_name),JournalDt.Journal_date,ledger.ledger_number,journal.journal_refgovernment ,journaldt.journaldt_id
						order by jourtype_group,JournalDt.Journal_date,journal_refgovernment";
		
		
		
		//	$query = $query."group by JournalDt.Journal_date,ledger.ledger_number,journal_type
		//	order by journal_type,JournalDt.Journal_date,journal_refgovernment";

			//echo $query;		
	$query1="select grid_color1,grid_color2 from setup";
	$result1=mysql_query($query1,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result1)){
		extract($rows);

	}	
	
	echo"<table width='990'>";
	echo'<tr bgcolor="#CCD8E6">';
	echo'<td  height="25"><span class="style25">Date </span></td>
		<td  height="25"><span class="style25">Ledger Num</span></td>
		<td  height="25"><span class="style25">Particulars</span></td>
		<td height="25"><span class="style25">Voucher Type</span></td>
		<td  height="25"><span class="style25">Type number</span></td>
		<td  height="25"><span class="style25">Voucher Number</span></td>
		<td  height="25"><span class="style25">Debit Amt.(Inwards only)</span></td>
		<td  height="25"><span class="style25"> Credit Amt.(Outwards only)</span></td>
		</tr>';
		//echo"</table>";
		$result=mysql_query($query,$connection) or die(mysql_error());		
		while($rows=mysql_fetch_array($result)){
				
			$ct+=1;
			if($ct%2==0)
			{
				$Color="style='background-color:".$grid_color1."'";
			}else{
				$Color="style='background-color:".$grid_color2."'";	
			}

			echo'<tr '.$Color.'>';
			echo"<td height='22'><span class='style15'>".$rows[0]."</span></td>";
			echo"<td height='22'><span class='style15'>".$rows[1]."</span></td>";						
			echo"<td height='22'><span class='style15'>".$rows[2]."</span></td>";						
			echo"<td height='22'><span class='style15'>".$rows[3]."</span></td>";						
			echo"<td height='22'><span class='style15'>".$rows[4]."</span></td>";						
			echo"<td height='22'><span class='style15'>".$rows[5]."</span></td>";						
			echo"<td height='22' align='right'><span class='style15'>".format_currency($rows[8], 2)."</span></td>";						
			echo"<td height='22' align='right'><span class='style15'>".format_currency($rows[9], 2)."</span></td>";						
			echo"</tr>";
		
		
		}
}
		
	echo"   </table></td></tr>"; 
	echo"   </table></td></tr><tr height='30'><td align='center' id='content-bottom' ></td></tr>"; 
echo"</table>";
echo"</form></body></html>";


function explodeword($date)
{

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

function format_currency($amount, $DecimalBase) { 

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