<?php


session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}
include("config.inc.php");
include("function.inc.php");
$exp = $_GET['exp'];
if($exp == 1){
	sleep(5);
$filename = "Transactions_Journal_listing.xls";
	header("Content-Disposition: attachment; filename=\"$filename\"");
	header("Content-Type: application/vnd.ms-excel");
}

set_time_limit(0);
$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);
}

$query_language  = "select user_account, Language from ".$_SESSION['dbLabel'].".login  where user_account = '".$_SESSION['useraccount']."'";
$result_language = mysql_query($query_language, $connection); 
$row_language    = mysql_fetch_array($result_language);
extract($row_language);
$query_labels = "SELECT LANGUAGE,
       TransactionJournalVoucher,
       Datee,
       Reference,
       Folio,
       Fromm,
       Too,
       OPTIONS,
       AllTypes,
       Exclude,
       CostcenterProject,
       CheckBill,
       PrintManualNo,
       ForceNewPage,
       WithMonthlyCalculation,
       JournalListing,
       Currency as Currencyy,
       NGN,
       USD,
       Typee,
	   Amount,
	   ManualNo,
	   LedgerNameDescription,
	   LedgerNo,
	   Credit,
	   Debit,
	   PreparedBy,
	   ControlledBy,
	   ApprovedBy
FROM  ".$_SESSION['dbLabel'].".transactionjv_labels
where language = '".$Language."'";
$result_labels = mysql_query($query_labels, $connection);
$row_labels    = mysql_fetch_array($result_labels);
extract($row_labels);

print <<< HERE
<HTML>
<HEAD>
<TITLE> $TransactionJournalVoucher </TITLE>
<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">
<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;
                 }
				 
				 
h4 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 2px;
   }
h3 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 2px;
	 width:220px;
   }
   
.header { font-size: 14px; 
          font-weight: bold;
          font-family:Times-New-Roman;
		}
   */
</style>


</HEAD>


HERE;







//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>';
						


//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage, ".
			 " Company_Name as CompanyName, Company_Memo as CompanyMemo, Company_City as CompanyWebsite, Company_Country as CompanyEmail  ".
			 " From Setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

$querymodule=" Select IfNull(Module_BelongsToGroup,0) As Module_BelongsToGroup From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

	//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]);

if ($NewPage != 1) {
	echo "<table border=0 width=100% cellspacing=0 cellpadding=0>";

echo "<thead><tr><td align='left'>";
	
	ShowReportHeader($TransactionJournalVoucher,'landscape', $connection);
	
	echo"</td></tr></thead><tbody>";
}




	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';     
	   
	if (empty($NewPage)) $NewPage = 0;
			
	$query = " select Journal_refgovernment, Journal_folio, ".
             " Journal_typeref,Journal_type, Date_Format(journal.Journal_date,'%d/%m/%Y'),Journaldt_valuedate, ".
             " journaldt.ledger_number,ledger.ledger_name,journaldt.manual_no,journaldt_dbcd, ".
			 " round(journaldt_amount,".$DecimalBase."),round(journaldt_amount1,".$DecimalBase1."),round(journaldt_amount2,".$DecimalBase2."),journaldt.Currency_code, journaldt_desc, journaldt_desc1, ".
			 " round(Amnt1_db ,".$DecimalBase1."), round(Amnt1_cd ,".$DecimalBase1."), round(Amnt2_db ,".$DecimalBase2."), round(Amnt2_cd,".$DecimalBase2."), journaldt.journaldt_id ".
			 " from Journal ".
			 " inner join journaldt on journaldt.journal_id=journal.journal_id ".
			 " inner join ledger on ledger.ledger_number=journaldt.ledger_number ".
			 " where ( ($option = 2 and  journal_refgovernment between ifnull($fromref,0) and ifnull($toref,9999999999999)) or ".
			 " ($option = 3 and Journal_folio between ifnull($fromfolio,0) and ifnull($tofolio,9999999999999)) 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  (($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)) ) ";
//(($jvtypelen > 0 and Journal.Journal_type in ($jvtype)) or ($jvtypelen = 0 and 1 = 1))
	 if ($Module_BelongsToGroup == 1)				
			$query= $query." and ifNull(Journal.Journal_BelongsToGroup,0) >= ifNull($FI_BelongsToGrp,0)  ";		
//echo $query;
$t_Ref='';


$vTotAmt_DbB1 = 0;
$vTotAmt_DbB2 = 0;
$vTotAmt_CrB1 = 0;
$vTotAmt_CrB2 = 0;



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

	if ($t_Ref!=$rows[0]) {
		if ($t_Ref != '') {

			echo"<tr><td colspan='9' style='border-left: 0 solid #000000; border-top: 1px solid #000000; border-bottom: 0 solid #000000;'>&nbsp;</td></tr>";
			echo"<tr><td colspan='6'>&nbsp;</td><td align='right'  style='border-left: 1px solid #000000; border-top: 1px solid #000000; border-bottom: 0 solid #000000;'><b>$Debit&nbsp</b></td>
					<td align='right'  style='border-left: 0 solid #000000; border-top: 1px solid #000000; border-bottom: 0 solid #000000;'><b>".number_format($vTotAmt_DbB1, $DecimalBase1)."</b></td>
					<td align='right'  style='border-left: 0 solid #000000; border-top: 1px solid #000000; border-bottom: 0 solid #000000;border-right: 1px solid #000000;'><b>".number_format($vTotAmt_DbB2, $DecimalBase2)."</b></td></tr>";
			echo"<tr><td colspan='6'>&nbsp;</td><td align='right'  style='border-left: 1px solid #000000; border-top: 0 solid #000000; border-bottom: 1px solid #000000;'><b>$Credit</b></td>
					<td align='right'  style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1px solid #000000;'><b>".number_format($vTotAmt_CrB1, $DecimalBase1)."</b></td>
					<td align='right'  style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1px solid #000000;border-right: 1px solid #000000;'><b>".number_format($vTotAmt_CrB2, $DecimalBase2)."</b></td></tr>";
			
			if ($NewPage == 1) {
				echo "</table>";
			}
				 
			if ($NewPage == 1) {
			 
				echo "<tr><td colspan='10'><table border=0 width=70% cellspacing=0 cellpadding=0>
						<col width=275>
						<col width=270>
						<col width=275>
						<tr height='25px'><td colspan='3' style='border-top: 2px solid black;'>&nbsp;</td></tr>
						<tr><td class='header' valign='top' align='center'>$PreparedBy  </td>
							<td class='header' valign='top' align='center'> $ControlledBy</td>
							<td class='header' valign='top' align='center'>$ApprovedBy </td>
						</tr>	
						<tr height='25px'><td colspan='3'>&nbsp;</td></tr>
						<tr height='10px'><td align='center' valign='bottom'><h3></td><td align='center' valign='bottom'><h3></td><td align='center' valign='bottom'><h3></td></tr>";
				echo"</table></td></tr>";
			   
				//echo "<tr><td>";
				echo "<div style='page-break-after:always;'></div>";
				//echo "</td></tr>"; 
			}
			
			
			
			$vTotAmt_DbB1 = 0;
			$vTotAmt_DbB2 = 0;
			$vTotAmt_CrB1 = 0;
			$vTotAmt_CrB2 = 0;
		
		
		}
		if ($NewPage == 1 ) {
			echo"<tr><td align='left'>";
			//ShowReportHeader('Transaction Journal Voucher', $connection);
			ShowReportHeader('Transaction Journal Voucher','landscape', $connection);
			echo"</td></tr>";
		}
		//echo "<tr><td align='left'>";
		echo "<tr><td colspan='10'><table border=0 width='820' cellspacing=0 cellpadding=0 class='report-master' style='table-layout:fixed;' >
			  <col width=80>
			  <col width=140>
			  <col width=80>
			  <col width=80>
			  <col width=50>
			  <col width=90>
			  <col width=110>
			  <col width=110>
			  <col width=110>";    
		 
		if ($t_Ref != '') { 
			echo"<tr height='30px'><td colspan='9'>&nbsp;</td></tr>"; 
		}
		
		
		
		
		echo"<tr class='subgroup'><td><b>$Reference</b></td><td colspan='6'>$rows[0]</td>";
		echo"<td align='right'><b>$Folio</b></td><td align='right'>$rows[1]</td></tr>";
		echo"<tr class='group'><td style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>$Typee</b></td>
					<td colspan='6' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'>$rows[2]</td>";
		echo"		<td align='right' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>$Datee</b></td>
					<td align='right' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'>$rows[4]</td></tr>";

		
		$vTotAmt_DbB1 = $rows[16];
		$vTotAmt_DbB2 = $rows[18];
		$vTotAmt_CrB1 = $rows[17];
		$vTotAmt_CrB2 = $rows[19];
		

				
		$i=1;
	}

	 

	if($i=='1') {
		echo"<tr><th>$LedgerNo</th><th colspan='2'>$LedgerNameDescription</th>";
		
		if ($varmanual ==1 )
			echo"<th><b>$ManualNo</b></th>";
		else
			echo"<th>&nbsp;</td>";
			
		echo"<th>D/C.</th><th>Cur.</th><th align='right'>$Amount</th><th align='right'>$base1</th><th align='right'>$base2</th></tr>";
	}	

	echo "<tr><td>$rows[6]</td><td colspan='2'>$rows[7]</td>";
	
	if ($varmanual ==1 )
		echo"<td>$rows[8]</td>";
	else
		echo"<td>&nbsp;</td>";
		
	echo"<td>$rows[9]</td><td>$rows[13]</td><td align='right'>".number_format($rows[10], $DecimalBase)."</td><td align='right'>".number_format($rows[11], $DecimalBase1)."</td><td align='right'>".number_format($rows[12], $DecimalBase2)."</td></tr>";
	echo "<tr><td>&nbsp;</td><td colspan='7'><font size=1><i>$rows[14]</i></font></td><td>$rows[15]</td></tr>";		


	if ($varcst ==1) 
	{
		$sub_query = " select cstjour.project_code,cstjour.costcent_code,Cstjour_prcnt, ".
					 " round(Cstjour_amount,".$DecimalBase."),round(Cstjour_amount1,".$DecimalBase1."), round(Cstjour_amount2,".$DecimalBase1.") ".
					 " from cstjour where cstjour.journaldt_id = '".$rows[20]."'";
					 
		$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());			 
		$sub_RowCount = mysql_num_rows($sub_result);
		if (empty($sub_RowCount)) $sub_RowCount = 0;
		
		if ($sub_RowCount > 0) {
			echo"<tr height=5px><td colspan='9'>&nbsp;</td></tr>";
			echo"<tr class='subdivisions-header'><td></td><td><i><b>Project</b></i></td><td colspan='3'><i><b>Costcenter</b></i></td><td><i><b>Percentage</b></i></td><td align='right'><i><b>Amount</b></i></td><td></td></td></tr>";
			
			while($sub_rows=mysql_fetch_row($sub_result)){
				echo "<tr><td></td><td><i>$sub_rows[0]</i></td><td colspan='3'><i>$sub_rows[1]</i></td><td><i>".number_format($sub_rows[2], $DecimalBase)."%</i></td><td align='right'><i>".number_format($sub_rows[3], $DecimalBase)."</i></td><td align='right'><i>".number_format($sub_rows[4], $DecimalBase1)."</td><td align='right'><i>".number_format($sub_rows[5], $DecimalBase2)."</i></td></tr>";
			}
			echo"<tr height=10><td colspan='9'>&nbsp;</td></tr>";
	
	}
		
	}

	
	if ($varcheck ==1)
	{
		$sub_query1 = " select Chk_Number,Chk_Bank, Date_Format(Chk_ValueDate,'%d/%m/%Y'),chkjour.Currency_code, Chk_Amount  ".
					  " from chkjour where chkjour.journaldt_id = '".$rows[20]."'";
					  
		$sub_result1=mysql_query($sub_query1,$connection) or die(mysql_error());			 
		$sub_RowCount1 = mysql_num_rows($sub_result1);
		if (empty($sub_RowCount1)) $sub_RowCount1 = 0;
		
		if ($sub_RowCount1 > 0) {
			echo"<tr height=10><td colspan='9'>&nbsp;</td></tr>";
			echo "<tr><td></td><td><b><i>Check No.</i></b></td><td><b><i>Bank</i></b></td><td colspan='2'><b><i>Value Date</b></i></td><td align='left'><b><i>Currency</b></i></td><td align='right'><b><i>Amount</b></i></td></tr>";
				
			while($sub_rows1=mysql_fetch_row($sub_result1)){
				echo "<tr><td></td><td><i>$sub_rows1[0]&nbsp</i></td><td><i>$sub_rows1[1]&nbsp</i></td><td colspan='2'><i>$sub_rows1[2] &nbsp</i></td><td><i>$sub_rows1[3]&nbsp</i></td><td align='right'><i>".number_format($sub_rows1[4], $DecimalBase)."</i></td></tr>";
			}
			
		}	
	
	}

	$i=$i+1;
	$t_Ref=$rows[0];

}

if ($t_Ref != '') {
		echo"<tr ><td colspan='9' style='border-left: 0 solid #000000; border-top: 1px solid #000000; border-bottom: 0 solid #000000;'>&nbsp;</td></tr>";	
	echo"<tr><td colspan='6'></td><td align='right'  style='border-left: 1px solid #000000; border-top: 1 solid #000000; border-bottom: 0 solid #000000;'><b>$Debit&nbsp</b></td>
		<td align='right'  style='border-left: 0 solid #000000; border-top: 1px solid #000000; border-bottom: 0 solid #000000;'><b>".number_format($vTotAmt_DbB1, $DecimalBase1)."</b></td>
		<td align='right'  style='border-left: 0 solid #000000; border-top: 1px solid #000000; border-bottom: 0 solid #000000;border-right: 1px solid #000000;'><b>".number_format($vTotAmt_DbB2, $DecimalBase2)."</b></td></tr>";
	echo"<tr><td colspan='6'></td><td align='right'  style='border-left: 1 solid #000000; border-top: 0 solid #000000; border-bottom: 1px solid #000000;'><b>$Credit</b></td>
			<td align='right'  style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1px solid #000000;'><b>".number_format($vTotAmt_CrB1, $DecimalBase1)."</b></td>
			<td align='right'  style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1px solid #000000;border-right: 1 solid #000000;'><b>".number_format($vTotAmt_CrB2, $DecimalBase2)."</b></td></tr>";
			
	echo "<tr><td colspan='9'><table border=0 width='820' cellspacing=0 cellpadding=0>
			<col width=275>
			<col width=270>
			<col width=275>
			<tr height='25px'><td colspan='3' style='border-top: 2px solid black;'>&nbsp;</td></tr>
			<tr><td class='header' valign='top' align='center'>$PreparedBy  </td>
				<td class='header' valign='top' align='center'> $ControlledBy</td>
				<td class='header' valign='top' align='center'>$ApprovedBy </td>
			</tr>	
			<tr height='25px'><td colspan='3'>&nbsp;</td></tr>
			<tr height='10px'><td align='center' valign='bottom'><h3></td><td align='center' valign='bottom'><h3></td><td align='center' valign='bottom'><h3></td></tr>";
	echo"</table></td></tr>";
	
	echo "</table>";
	echo "</td></tr>";
			
	$vTotAmt_DbB1 = 0;
	$vTotAmt_DbB2 = 0;
	$vTotAmt_CrB1 = 0;
	$vTotAmt_CrB2 = 0;
		

}

  echo"</tbody>";
	

echo "</table>";

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