<?php


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

print <<< HERE

<HTML>
<HEAD>
<TITLE> Journal Listing </TITLE>

<style type="text/css">

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

</style>


</HEAD>


HERE;

include("config.inc.php");

$CostProj = $_GET['CostProj'];

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

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


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


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


	$twidth = 570;
	if ($varType==1 ) $twidth += 50;
	if ($varFolio==1 ) $twidth += 70;
	if ($varCurrency==1 ) $twidth += 240;
	if ($varNGN==1 ) $twidth += 240;
	if ($varUSD==1 ) $twidth += 240;
	if ($CostProj==1 ) $twidth += 360;
	
    echo "<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td align='left'>";
	echo "<table border=0 width='$twidth' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >";
	
	echo "<col width='90px'>";
if ($varType==1 )
	echo "<col width='50px'>";
	echo "<col width='60px'>";
if ($varFolio==1 )
	echo "<col width='70px'>";
	echo "<col width='150px'>";	
	echo "<col width='200px'>";
	echo "<col width='70px'>";
if ($varCurrency ==1 )	
	echo "<col width='120px'><col width='120px'>";
if ($varNGN ==1 )
	echo "<col width='120px'><col width='120px'>";
if ($varUSD==1 )
	echo "<col width='120px'><col width='120px'>";
if($CostProj == 1) 
	echo "<col width='180px'><col width='180px'>";
		
	

	echo "<tr><td height='60' colspan='3'><img src='img/imgheader1.jpg' width='115' height='50' /></td><td colspan='7' align='center'><b><font size=5>Journal Listing</font><br>by Date</br></td><td colspan='4' align='right' ><font size='2'><b>Printed&nbsp;Date&nbsp;&amp;&nbsp;Time:</b>&nbsp;&nbsp;";
	echo $printdate[2]."/".$printdate[1]."/".$printdate[0];
	echo "<br>".$dt[1]." ".$dt[2]. " ".$dt[3];
	echo "</font></td></tr>";

	$dispfromdate = $fromdate;
	$disptodate = $todate;
	
	if ($fromref=='')
		$fromref = 'null';
	if ($toref=='')
		$toref = 'null';
	if ($fromfolio=='')
		$fromfolio = 'null';
	if ($tofolio=='')
		$tofolio = 'null';
		
	 if (!empty($fromdate))
	   $fromdate = explodeword($fromdate);
	if (!empty($todate))
	   $todate = explodeword($todate);  

	if (empty($fromdate))   
	   $fromdate = 'null';
	else
	   $fromdate = "'".$fromdate."'"; 
	   
	if (empty($todate))   
	   $todate = 'null';
	else
	   $todate = "'".$todate."'";	
	   
	   $jvtypelen = 0;
	if (!empty($jvtype))
	   $jvtypelen = strlen($jvtype);
	   
	if (empty($jvtype))
	   $jvtype = 'null'; 
	
$TotalDebit_Amount = 0;
$TotalCredit_Amount = 0;
$TotalDebit_Amount1 = 0;
$TotalCredit_Amount1 = 0;
$TotalDebit_Amount2 = 0;
$TotalCredit_Amount2 = 0;	
					
	$query = "  select Date_Format(journal.Journal_date,'%d/%m/%Y'), Journal_refgovernment, journaldt.Ledger_number, Journaldt_desc, journaldt.Currency_code, journaldt_dbcd,
		     Journaldt_desc1,
			if (journaldt_dbcd = 'D' ,round(Journaldt_amount,".$DecimalBase."),0) As Debit_Amount,
			if (journaldt_dbcd = 'C' ,round(Journaldt_amount,".$DecimalBase."),0) As Credit_Amount,
			if (journaldt_dbcd = 'D' ,round(Journaldt_amount1,".$DecimalBase1."),0) As Debit_Amount1,
			if (journaldt_dbcd = 'C' ,round(Journaldt_amount1,".$DecimalBase1."),0) As Credit_Amount1,
			if (journaldt_dbcd = 'D' ,round(Journaldt_amount2,".$DecimalBase2."),0) As Debit_Amount2,
			if (journaldt_dbcd = 'C' ,round(Journaldt_amount2,".$DecimalBase2."),0) As Credit_Amount2,
			Journal_folio, Journal_type, Ledger_name,  concat(journal.Project_code,'-',Project_Name), concat(journal.Costcent_Code,'-',Costcent_name)
			from journal
			inner join journaldt on journaldt.journal_id=journal.journal_id
			inner join ledger on ledger.ledger_number=journaldt.ledger_number
			left join project on project.project_code = journal.project_code 
			left join costcent on costcent.Costcent_code = journal.Costcent_code ";
		
	$query .= " where ( ($option = 2 and  journal_refgovernment between ifnull($fromref,journal_refgovernment) and ifnull($toref,journal_refgovernment)) or
				 ($option = 3 and Journal_folio between ifnull($fromfolio,Journal_folio) and ifnull($tofolio,Journal_folio)) or
				 ($option = 1 and date_format(journal.Journal_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(journal.Journal_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(journal.Journal_date,'%Y-%m-%d')) ))
				 and (($jvtypelen > 0 and Journal.Journal_type in ($jvtype)) or ($jvtypelen = 0 and 1 = 1))";
				 
		 		
	
	//echo$query;	
$query1= "select BASE1, BASE2 from setup";
$result1=mysql_query($query1, $connection);
$row1=mysql_fetch_array($result1);
extract($row1);	
	
echo "<tr height='20'><td></td></tr>";
	    echo "<tr><td><b>Date &nbsp;&nbsp; From </b></td><td colspan='2'>  &nbsp; $dispfromdate</td></tr>";
		echo "<tr><td align= 'right'><b>To</b></td><td colspan='2'> &nbsp; $disptodate</td></tr>";	

echo   "<tr height='30'><td></td></tr>";
echo   "<tr><td style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Date</b></td>";

if ($varType==1 )
	echo "<td align='right' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Type</b></td>";
	
	echo"<td align='center' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Ref.</b></td>";
		
if ($varFolio==1 )
		echo "<td align='left' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Folio</b></td>";
		
	echo" <td colspan='2' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Ledger No./Description</b></td>
		<td style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Cur.</b></td>";
		
if ($varCurrency ==1 )
		echo "<td  align='right' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Debit</b></td>
		<td  align= 'right' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Credit</b></td>";
		   
if ($varNGN ==1 )
		echo "<td align= 'right' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Debit $BASE1</b></td>
		<td  align= 'right' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Credit $BASE1</b></td>";
		
if ($varUSD==1 )
		echo "<td align='right' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Debit $BASE2</b></td>
		<td align='right' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Credit $BASE2</b></td>";
	
		
if($CostProj == 1) 
	echo "<td align='right' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Project</b></td>
		<td align='right' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Coscenter</b></td>";
			
echo "</tr>";
$result=mysql_query($query,$connection) or die(mysql_error());
$col=0;
while($rows=mysql_fetch_row($result)){
	if($col%2==0){
		$color = "style='background-color:".$report_color1."'";
	}else{
		$color = "style='background-color:".$report_color2."'";
	}
	
	echo"<tr $color>";
	echo"<td  style='border-right:1px solid black;'>$rows[0] </td>";
if ($varType==1 )
	echo"<td align='left'  style='border-right:1px solid black; padding-left:2px;'>$rows[14]</td>";

	echo"<td align='center' style='border-right:1px solid black;'>$rows[1]</td>";

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

   
echo"<td colspan='2' style='border-right:1px solid black; padding-left:2px;'>$rows[2] - $rows[15]</td>
	<td style='border-right:1px solid black;  padding-left:2px;'>$rows[4]</td>";

if ($varCurrency ==1 )
	echo "<td align='right' style='border-right:1px solid black; padding-right:2px;'>$rows[7]</td>
		<td align='right' style='border-right:1px solid black; padding-right:2px;'>$rows[8]</td>";
   
if ($varNGN ==1 )
	echo"<td align='right' style='border-right:1px solid black; padding-right:2px;'>$rows[9]</td>
	   <td align='right' style='border-right:1px solid black; padding-right:2px;'>$rows[10]</td>";

if ($varUSD==1 )
	echo"<td align='right' style='border-right:1px solid black; padding-right:2px;'>$rows[11]</td>
	   <td align='right' style='border-right:1px solid black; padding-right:2px;'>$rows[12]</td>";

if ($CostProj==1 )
	echo"<td align='right' style='border-right:1px solid black; padding-right:2px;'>$rows[16]</td>
	   <td align='right' style='border-right:1px solid black; padding-right:2px;'>$rows[17]</td>";
echo "</tr>";

echo"<tr height='20px' $color>";	   
	echo "<td  style='border-right:1px solid black; '>&nbsp;</td>";
if ($varType==1 )
	echo "<td  style='border-right:1px solid black; '>&nbsp;</td>";
	echo "<td  style='border-right:1px solid black; '>&nbsp;</td>";
if ($varFolio==1 )
	echo "<td  style='border-right:1px solid black; '>&nbsp;</td>";
	echo "<td colspan='2' style='padding-left:20px;border-right:1px solid black;'><i>$rows[3] &nbsp; $rows[6] </i></td>";	
	echo "<td style='border-right:1px solid black; '>&nbsp;</td>";
if ($varCurrency ==1 )	
	echo "<td style='border-right:1px solid black; '>&nbsp;</td><td style='border-right:1px solid black; '>&nbsp;</td>";
if ($varNGN ==1 )
	echo "<td  style='border-right:1px solid black; '>&nbsp;</td><td  style='border-right:1px solid black; '>&nbsp;</td>";
if ($varUSD==1 )
	echo "<td  style='border-right:1px solid black; '>&nbsp;</td><td  style='border-right:1px solid black; '>&nbsp;</td>";
if($CostProj == 1) 
	echo "<td  style='border-right:1px solid black; '>&nbsp;</td><td  style='border-right:1px solid black; '>&nbsp;</td>";  

echo"</tr>";
$TotalDebit_Amount += $rows[7];
$TotalCredit_Amount += $rows[8];
$TotalDebit_Amount1 += $rows[9];
$TotalCredit_Amount1 += $rows[10];
$TotalDebit_Amount2 += $rows[11];
$TotalCredit_Amount2 += $rows[12];
$col+=1;
	
}

 echo"<tr height='30px'><td></td>";
if ($varType==1 )
	echo "<td></td>";
	echo "<td></td>";
if ($varFolio==1 )
	echo "<td></td>";
	echo "<td></td>";	
	echo "<td></td>";
	echo "<td><b>Total</b></td>";
if ($varCurrency ==1 )	
	echo "<td></td><td></td>";
if ($varNGN ==1 )
	echo"<td><b>Debit $BASE1</b></td><td colspan='2' align='center'><b>Credit $BASE1</b></td>";
if ($varUSD==1 )
	echo"<td align='right'><b>Debit $BASE2</b></td><td colspan='2' align='center'><b>Credit $BASE2</b></td>";
if($CostProj == 1) 
	echo "<td></td><td></td>";
echo "</tr>";		
	

 echo"<tr><td></td>";
if ($varType==1 )
	echo "<td></td>";
	echo "<td></td>";
if ($varFolio==1 )
	echo "<td></td>";
	echo "<td></td>";	
	echo "<td></td>";
	echo "<td></td>";
if ($varCurrency ==1 )	
	echo "<td></td><td></td>";
if ($varNGN ==1 )
	echo"<td><b>".number_format($TotalDebit_Amount1, $DecimalBase1)."</b></td><td colspan='2' align='center'><b>".number_format($TotalCredit_Amount1, $DecimalBase1)."</b></td>";
if ($varUSD==1 )
	echo"<td align='right'><b>".number_format ($TotalDebit_Amount2, $DecimalBase2)."</b></td><td colspan='2' align='center'><b>".number_format ($TotalCredit_Amount2, $DecimalBase2)."</b></td>";
if($CostProj == 1) 
	echo "<td></td><td></td>";
echo "</tr>";	
	

function current_server_time($connection){

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

  return $now;
}

function explodeword($date)
{

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

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