<?php


session_start();
if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}
if ($VoucherType == '1') 
$Title = 'List of Receipts';

if ($VoucherType == '2') 
$Title = 'List of Payments';

if ($VoucherType == '3') 
$Title = 'List of Debit Notes';

if ($VoucherType == '4') 
$Title = 'List of Credit Notes';

	 
	 //echo $Title;
$fromproject   = GetSQLValueString($fromproject, 'text');
$toproject     = GetSQLValueString($toproject, 'text');
$fromcostcent  = GetSQLValueString($fromcostcent, 'text');
$tocostcent    = GetSQLValueString($tocostcent, 'text');

 
print <<< HERE

<HTML>
<HEAD>
<TITLE> $Title </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");



$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'])
		or die("Could not connect to DB");

mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");

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


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 ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}


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


$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%') ".
			  " where user_account='".$HTTP_SESSION_VARS['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 ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%') ".
			   " where user_account='".$HTTP_SESSION_VARS['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]);




    echo "<table border=0 width=100% cellspacing=0 cellpadding=0>";
	
echo'<tr><td colspan=15>
<table width="100%" border="0" align="center">';

	echo "<tr><td height='60' colspan='3'><img src='img/imgheader1.jpg' width='115' height='50' /></td><td colspan='3' align='center'><b><font size=5>$Title</font></td><td colspan='5' 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>";

echo"</table></tD></tr>";	
	
	echo"<tr><td align='left'>";
	echo "<table border=0 width='860' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=70>
		  <col width=70>
		  <col width=80>
		  <col width=90>
		  <col width=100>
		  <col width=230>
		  <col width=60>
		  <col width=60>
		  <col width=80>
		  <col width=80>
		  <col width=80>
		  <col width=80>";   
	
	
	if ($fromref=='')
		$fromref = 'null';
	if ($toref=='')
		$toref = 'null';
		
	if ($fromledger=='') $fromledger = 'null';
	else $fromledger = "'".$fromledger."'"; 
	if ($toledger=='')	$toledger = 'null';
	else $toledger = "'".$toledger."'"; 
	
	if ($fromsalesman=='') $fromsalesman = 'null';
	else $fromsalesman = "'".$fromsalesman."'"; 
	if ($tosalesman=='') $tosalesman = 'null';
	else $tosalesman = "'".$tosalesman."'"; 
	
	
		
	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."'";	
	   
	$QueryRestrictPrj = " Inner join ( select distinct project_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
						" 			   where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
						"              and (project_code is not null and project_code != '') ".
						"			 ) QP  ";
						
	$QueryRestrictCst = " Inner join ( select distinct costcent_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
						"			   where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
						"              and (costcent_code is not null and costcent_code != '') ".
						"			 ) QC  ";   

	$query = " select  Date_Format(Trans.Trans_Date,'%d/%m/%Y'), Date_Format(TransDt_ValueDate,'%d/%m/%Y'), Trans_Reference,  ".
             " Trans_ManualNo, TransDt.Ledger_number, ledger.ledger_name, TransDt_Description, TransDt.Currency_code, Transdt_Amount, ".
			 " journal.Journal_refgovernment, TransDt_dbcd, TransDt.TransDt_id  ";
			 
	if ($varGrpBy == 2) 
		$query = $query." , salesmen.ledger_number as salesmencode, salesmen.ledger_name as salesmenname,  ";
	else
		$query = $query." , null as salesmencode, null as salesmenname,  ";
		
	$query = $query." Trans.Project_Code, Trans.Costcent_Code, Trans.journal_id  ";
	$query = $query." from Trans ".
					" left join journal on journal.journal_id=Trans.journal_id ".
					" inner join TransDt on TransDt.Trans_id=Trans.Trans_id  ".
					" inner join ledger on ledger.ledger_number=TransDt.ledger_number ";
			 
	if ($varGrpBy == 2) {		 
		$query = $query." inner join clients on clients.ledger_number = ledger.ledger_number ".
						" left join salesmen on clients.salesmen_code = salesmen.ledger_number ";
	}					
	
	if ($projectexist >	0 && $FI_RepRestrictProject == 1)
		$query = $query.$QueryRestrictPrj." on Trans.project_code like concat(QP.project_code,'%') ";
											 
	if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
		$query = $query.$QueryRestrictCst." on Trans.costcent_code like concat(QC.costcent_code,'%') ";
	
	$query = $query." where ( ($option = 1 and date_format(Trans.Trans_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Trans.Trans_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Trans.Trans_Date,'%Y-%m-%d')) ) ".
					" 		or ($option = 2 and date_format(TransDt_ValueDate,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(TransDt_ValueDate,'%Y-%m-%d')) and ifNull(". $todate.",date_format(TransDt_ValueDate,'%Y-%m-%d')) )".
					" 		or ($option = 3 and Trans_Reference between ifnull($fromref,0) and ifnull($toref,9999999999999) )) ".
					" and (($VoucherType = 1 and Trans.Trans_Type = 'RV') or ($VoucherType = 2 and Trans.Trans_Type = 'PV') or ($VoucherType = 3 and Trans.Trans_Type = 'DV') or ($VoucherType = 4 and Trans.Trans_Type = 'CV')) ".
					" and (($dbcdType = 1 and  TransDt_dbcd='C') or ($dbcdType = 2 and  TransDt_dbcd='D') or ($dbcdType = 3 and 1 = 1)) ".
					" and (ledger.ledger_number between ifnull(".$fromledger.",'0') and ifnull(".$toledger.",'9999999999999')) ".
					" and ifNull(Trans.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(Trans.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(Trans.Project_Code,'-1')) ".
					" and ifNull(Trans.COSTCENT_CODE,'-1') between ifNull(".$fromcostcent.",ifNull(Trans.COSTCENT_CODE,'-1')) and ifNull(". $tocostcent.",ifNull(Trans.COSTCENT_CODE,'-1'))  ".
					" And ((($varJournal = 2 and Trans.journal_id <>0) or (Trans.journal_id is not null) )or ($varJournal = 3 and (Trans.journal_id = 0 ) or  (Trans.journal_id is null) )) ";
			 
	if ($varGrpBy == 2) {
		$query = $query." and ifNull(salesmen.ledger_number,'-1') between ifNull(".$fromsalesman.",ifNull(salesmen.ledger_number,'-1')) and ifNull(". $tosalesman.",ifNull(salesmen.ledger_number,'-1')) ";
	}
	
	$query = $query." order by if($varGrpBy=3, Transdt.Ledger_number, if($varGrpBy=2, salesmencode, Null)), if($varGrpBy=2, TransDt.Ledger_number, Null ), Trans.Trans_Date, Trans_Reference, if($varGrpBy=1, TransDt.Ledger_number, Null ) ";
	
	
	//echo $query;
	echo"<thead>";
	echo "<tr height='20'>";
	echo "<td style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Date</b></td>";
	echo "<td align='right' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Value Date</b></td>";
    echo "<td align='center' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Reference</b></td>";
	echo "<td align='right' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Manual No.</b></td>";
	
	if ($varGrpBy == 3) {
		echo"<td colspan='2' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'>&nbsp;</td>";
	}	
    else {
		echo "<td align='center' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Ledger No.</b></td>";
		echo "<td align='left' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Ledger Name/Description</b></td>";
	}	
	echo "<td align='center' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>D/C</b></td>";
	echo "<td align='center' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Cur.</b></td>";
	echo "<td align='right' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Amount</b></td>";
	echo "<td align='right' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>JV Ref.</b></td>";
	
	if ($varProjectHeader == 1) {
		echo "<td align='center' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Project</b></td>";
		echo "<td align='center' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'><b>Costcenter</b></td>";
	} else {
		echo "<td align='center' colspan='2' style='border-left: 0 solid #000000; border-top: 0 solid #000000; border-bottom: 1 solid #000000;'>&nbsp;</td>";
	}
	
    echo "</tr>";
	echo"</thead>";
$t_LedgerNumber='';
$t_SalesmenCode='';

	
$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."'";
		}
	if ($varGrpBy == 3) {
		if ($t_LedgerNumber != $rows[4] && $t_LedgerNumber != ''){
			echo"<tr><td><font size='3'><b>Currency</b></font></td><td colspan='2'></td><td colspan='2'><font size='3'><b>Sum of Amount</b></font></td></tr>";

			$queryTotal= " select transdt.currency_code, sum(transdt_amount)".
						 " from Trans ".
						 " left join journal on journal.journal_id=Trans.journal_id ".
						 " inner join TransDt on TransDt.Trans_id=Trans.Trans_id  ".
						 " inner join ledger on ledger.ledger_number=TransDt.ledger_number ";
						 
			if ($projectexist >	0 && $FI_RepRestrictProject == 1)
				$queryTotal = $queryTotal.$QueryRestrictPrj." on Trans.project_code like concat(QP.project_code,'%') ";
													 
			if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
				$queryTotal = $queryTotal.$QueryRestrictCst." on Trans.costcent_code like concat(QC.costcent_code,'%') ";
		
			$queryTotal = $queryTotal.
						 " where ( ($option = 1 and date_format(Trans.Trans_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Trans.Trans_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Trans.Trans_Date,'%Y-%m-%d')) ) ".
						 " 		or ($option = 2 and date_format(TransDt_ValueDate,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(TransDt_ValueDate,'%Y-%m-%d')) and ifNull(". $todate.",date_format(TransDt_ValueDate,'%Y-%m-%d')) )".
						 " 		or ($option = 3 and Trans_Reference between ifnull($fromref,0) and ifnull($toref,9999999999999) )) ".
						 " and (($VoucherType = 1 and Trans.Trans_Type = 'RV') or ($VoucherType = 2 and Trans.Trans_Type = 'PV') or ($VoucherType = 3 and Trans.Trans_Type = 'DV') or ($VoucherType = 4 and Trans.Trans_Type = 'CV')) ".
						 " and (($dbcdType = 1 and  TransDt_dbcd='C') or ($dbcdType = 2 and  TransDt_dbcd='D') or ($dbcdType = 3 and 1 = 1)) ".
						 " and ifNull(Trans.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(Trans.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(Trans.Project_Code,'-1')) ".
						 " and ifNull(Trans.COSTCENT_CODE,'-1') between ifNull(".$fromcostcent.",ifNull(Trans.COSTCENT_CODE,'-1')) and ifNull(". $tocostcent.",ifNull(Trans.COSTCENT_CODE,'-1'))  ".
						 " and ledger.ledger_number = '".$t_LedgerNumber."' ".
						 " group by currency_code order by currency_code";

					//echo $queryTotal;exit;
			$queryTotal=mysql_query($queryTotal,$connection) or die(mysql_error());
			while($rowsTotal=mysql_fetch_row($queryTotal)){

				echo"<tr><td><i> $rowsTotal[0]</i></td><td colspan='2'></td><td colspan='2'>".number_format($rowsTotal[1],2)."</td></tr>";

			}
			
			echo"<tr><td colspan='11'>&nbsp;</td></tr>";
		}
		
		if ($t_LedgerNumber != $rows[4] || $t_LedgerNumber == ''){
			echo"<tr><td colspan='2'><font size=3><b><i>Ledger No.</font></i></b></td><td><b><i>$rows[4]</i></b></td><td colspan='8'><b><i>$rows[5]</i></b></td></tr>";
			
			$t_LedgerNumber=$rows[4];
		}
	}

	if ($varGrpBy == 2) {
		if ($t_SalesmenCode != $rows[12] && $t_SalesmenCode != '')
		{
			echo"<tr><td><font size='3'><b>Currency</b></font></td><td colspan='2'></td><td colspan='2'><font size='3'><b>Sum of Amount</b></font></td></tr>";

			$queryTotal= " select transdt.currency_code, sum(transdt_amount)".
						 " from Trans ".
						 " left join journal on journal.journal_id=Trans.journal_id ".
						 " inner join TransDt on TransDt.Trans_id=Trans.Trans_id  ".
						 " inner join ledger on ledger.ledger_number=TransDt.ledger_number ".
						 " inner join clients on clients.ledger_number = ledger.ledger_number ".
						 " left join salesmen on clients.salesmen_code = salesmen.ledger_number ";
						 
			if ($projectexist >	0 && $FI_RepRestrictProject == 1)
				$queryTotal = $queryTotal.$QueryRestrictPrj." on Trans.project_code like concat(QP.project_code,'%') ";
													 
			if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
				$queryTotal = $queryTotal.$QueryRestrictCst." on Trans.costcent_code like concat(QC.costcent_code,'%') ";
				
			$queryTotal = $queryTotal.
						 " where ( ($option = 1 and date_format(Trans.Trans_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Trans.Trans_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Trans.Trans_Date,'%Y-%m-%d')) ) ".
						 " 		or ($option = 2 and date_format(TransDt_ValueDate,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(TransDt_ValueDate,'%Y-%m-%d')) and ifNull(". $todate.",date_format(TransDt_ValueDate,'%Y-%m-%d')) )".
						 " 		or ( $option = 3 and Trans_Reference between ifnull($fromref,0) and ifnull($toref,9999999999999) )) ".
						 " and (($VoucherType = 1 and Trans.Trans_Type = 'RV') or ($VoucherType = 2 and Trans.Trans_Type = 'PV') or ($VoucherType = 3 and Trans.Trans_Type = 'DV') or ($VoucherType = 4 and Trans.Trans_Type = 'CV')) ".
						 " and (($dbcdType = 1 and  TransDt_dbcd='C') or ($dbcdType = 2 and  TransDt_dbcd='D') or ($dbcdType = 3 and 1 = 1)) ".
						 " and ifNull(Trans.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(Trans.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(Trans.Project_Code,'-1')) ".
						 " and ifNull(Trans.COSTCENT_CODE,'-1') between ifNull(".$fromcostcent.",ifNull(Trans.COSTCENT_CODE,'-1')) and ifNull(". $tocostcent.",ifNull(Trans.COSTCENT_CODE,'-1'))  ".
						 " and (ledger.ledger_number between ifnull(".$fromledger.",'0') and ifnull(".$toledger.",'9999999999999')) ".
						 " and salesmen.ledger_number = '".$t_SalesmenCode."' ".
						 " group by currency_code order by currency_code";

					//echo $queryTotal;exit;
			$queryTotal=mysql_query($queryTotal,$connection) or die(mysql_error());
			while($rowsTotal=mysql_fetch_row($queryTotal)){

				echo"<tr><td><i>$rowsTotal[0]</i></td><td colspan='2'></td><td colspan='2'>".number_format($rowsTotal[1],2)."</td></tr>";

			}
			
			echo"<tr><td colspan='11'>&nbsp;</td></tr>";
		}
		if ($t_SalesmenCode != $rows[12] || $t_SalesmenCode == ''){
			echo"<tr><td colspan='2'><font size=3><b><i>Salesman</font></i></b></td><td><b><i>$rows[12]</i></b></td><td colspan='8'><b><i>$rows[13]</i></b></td></tr>";
			
			$t_SalesmenCode=$rows[12];
		}
	}
	

	echo"<tr ></tr>";
		if ($varProjectDetail ==0){
			echo"<tr $color>
					<td style='border-right:1px solid black; padding-left:2px;'>$rows[0]</td>
					<td style='border-right:1px solid black; padding-left:2px;'>$rows[1]</td>
					<td align ='right' style='border-right:1px solid black; padding-right:2px;'> $rows[2]</td>
					<td align='right' style='border-right:1px solid black; padding-right:2px;'>&nbsp; $rows[3]</td>";
					
				if ($varGrpBy == 3) {
		if ($varDesc ==1)
			echo"<td colspan='2' style='border-right:1px solid black; padding-right:2px;'>$rows[6]</td>";
		else
			echo"<td colspan='2' style='border-right:1px solid black; padding-right:2px;'>&nbsp;</td>";
	}	
	else {
		echo"<td align='center' style='border-right:1px solid black; '>$rows[4]</td>
			 <td align='left' style='border-right:1px solid black; padding-left:2px;'>$rows[5]</td>";
	}	

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

	if ($varProjectHeader == 1) 
		echo "<td align='center' style='border-right:1px solid black; '>&nbsp;$rows[14]</td>
			  <td align='left' style='border-right:1px solid black; padding-left:2px;'>$rows[15]</td></tr>";
	else
		echo "<td align='left' style='border-right:1px solid black; padding-right:2px;'>&nbsp;</td ><td style='border-right:1px solid black; padding-right:2px;'></td></tr>";
	
		}else {
			echo"<tr $color>
					<td >$rows[0]</td>
					<td >$rows[1]</td>
					<td align ='right' > $rows[2]</td>
					<td align='right'>&nbsp; $rows[3]</td>";
			if ($varGrpBy == 3) {
				if ($varDesc ==1)
					echo"<td colspan='2' >$rows[6]</td>";
				else
					echo"<td colspan='2'>&nbsp;</td>";
			}	
			else {
				echo"<td align='center' >$rows[4]</td>
					<td align='left' >$rows[5]</td>";
			}	

			echo"<td align='center'>$rows[7]</td>
				 <td align='right' >".number_format($rows[8],2)."</td>
				 <td align='right'>$rows[9]</td>";

			if ($varProjectHeader == 1) 
				echo "<td align='center'>&nbsp;$rows[14]</td>
					  <td align='left' >$rows[15]</td></tr>";
			else
				echo "<td align='left' >&nbsp;</td ><td style='border-right:1px solid black; padding-right:2px;'></td></tr>";
			
		}
		
		
if($ShowJV==1){
	//maroun 09.03.2015 adding show detailed jv
	  $string = '';
      $string = $string.'Select JDT.JournalDt_ID, JDT.Journal_ID, JDT.Ledger_Number, ledger.Ledger_Name, JDT.Journaldt_Desc, JDT.Currency_Code,clients.Salesmen_Code,salesmen.ledger_name as salesman_name,';
      $string = $string.' if(JDT.Journaldt_Dbcd = "C",JDT.JournalDt_Amount,null) As CreditAmount,';
      $string = $string.' if(JDT.Journaldt_Dbcd = "D",JDT.JournalDt_Amount,null) As DebitAmount';
      $string = $string.' ,clients.salesmen_code,salesmen.ledger_name as salesman_name';
      $string = $string.' From JournalDt JDT';
      $string = $string.' Inner Join Ledger On Ledger.Ledger_Number = JDT.Ledger_Number';
      $string = $string.' Inner Join Journal On JDT.Journal_ID = Journal.Journal_ID';
      $string = $string.' left join clients on clients.ledger_number=ledger.ledger_number ';
      $string = $string.' left join salesmen on clients.salesmen_code=salesmen.ledger_number ';
      $string = $string.'Where Journal_Type <> "OP"';
      $string = $string." And JDT.Journal_ID = '".$rows[16]."'";
		
	$resultdetailed=mysql_query($string,$connection) or die(mysql_error());
	
	echo"<tr><td></td><td colspan=10><table width=960  style='table-layout:fixed;font-size:10px' border=0>
			<col width=50>
			<col width=200>
			<col width=40>
			<col width=40>
			<col width=60>
			<col width=370>";
		echo"<tr><td><u>Ledger</u></td><td colspan=4><u>Description</u></td></tr>";
		while($rowDet=mysql_fetch_row($resultdetailed)){
			if($rowDet[8] != 0)
				$amount = $rowDet[8];
			else	
				$amount = $rowDet[9];
				
			if($rowDet[8] != 0)
				$labeldc = 'C';
			else
				$labeldc = 'D';
				
				
			echo"<tr><td>".$rowDet[2]."</td><td>".$rowDet[3]."</td><td>".$rowDet[5]."</td><td>".$labeldc."</td><td align=right>".number_format($amount, 2)."</td><td>".$rowDet[4]."</td></tr>";
		
		}
		
	echo"</table></td></tr>";		
	echo"<tr><td colspan=12><hr></td></tr>";		


}


if($ShowOtherSide==1){
	//maroun 09.03.2015 adding show other side
	
     $string = '';
	 $string = $string.'Select JDT.JournalDt_ID, JDT.Journal_ID, JDT.Ledger_Number, ledger.Ledger_Name, JDT.Journaldt_Desc, JDT.Currency_Code,';
     $string = $string.' if(JDT.Journaldt_Dbcd = "C",JDT.JournalDt_Amount,null) As CreditAmount,';
     $string = $string.' if(JDT.Journaldt_Dbcd = "D",JDT.JournalDt_Amount,null) As DebitAmount';
     $string = $string.' ,clients.salesmen_code,salesmen.ledger_name as salesman_name';
     $string = $string.' From JournalDt JDT';
     $string = $string.' Inner Join Ledger On Ledger.Ledger_Number = JDT.Ledger_Number';
     $string = $string.' Inner Join Journal On JDT.Journal_ID = Journal.Journal_ID';
     $string = $string.' left join clients on clients.ledger_number=ledger.ledger_number ';
     $string = $string.' left join salesmen on clients.salesmen_code=salesmen.ledger_number ';
     $string = $string.' Where Journal_Type <> "OP"';
	 $string = $string." and (($dbcdType = 1 and  JDT.journaldt_dbcd='D') or ($dbcdType = 2 and  JDT.journaldt_dbcd='C') or ($dbcdType = 3 and 1 = 1)) ";
 	 $string = $string." And JDT.Journal_ID = '".$rows[16]."'"; 
     $resultdetailed=mysql_query($string,$connection) or die(mysql_error());
//	echo $string."<br>";
	echo"<tr><td></td><td colspan=10><table width=960  style='table-layout:fixed;font-size:10px' border=0>
			<col width=50>
			<col width=200>
			<col width=40>
			<col width=40>
			<col width=60>
			<col width=370>";
		echo"<tr><td><u>Ledger</u></td><td colspan=4><u>Description</u></td></tr>";
		while($rowDet=mysql_fetch_row($resultdetailed)){
			if($rowDet[6] != 0)
				$amount = $rowDet[6];
			else	
				$amount = $rowDet[7];
				
			if($rowDet[6] != 0)
				$labeldc = 'C';
			else
				$labeldc = 'D';
				
				
			echo"<tr><td>".$rowDet[2]."</td><td>".$rowDet[3]."</td><td>".$rowDet[5]."</td><td>".$labeldc."</td><td align=right>".number_format($amount, 2)."</td><td>".$rowDet[4]."</td></tr>";
		
		}
		
	echo"</table></td></tr>";		
	echo"<tr><td colspan=12><hr></td></tr>";		


}


	if ($varDesc ==1 && $varGrpBy != 3 && $rows[6] != '')
		echo"<tr height=20 $color><td colspan='3'></td><td colspan='8'><i>$rows[6]</i></td></tr>";
	   
	 
	if ($varProjectDetail ==1){
		$sub_query = 	" select cstjourdt.project_code,cstjourdt.costcenter_code,CstjourDt_Per, ".
						" round(CstJourDt_Amount1,".$DecimalBase1.")".
						" from cstjourdt where cstjourdt.TransDt_id = '".$rows[11]."'";
		//echo $sub_query ;exit;				 
			$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 $color><td></td><td><i><b>Project</b></i></td><td><i><b>Costcenter</b></i></td><td><i><b>Percentage</b></i></td><td align='right'><i><b>Amount</b></i></td><td colspan='6'>&nbsp;</td></tr>";
				
			while($sub_rows=mysql_fetch_row($sub_result)){
				echo "<tr $color><td></td><td><i>$sub_rows[0]</i></td><td><i>$sub_rows[1]</i></td><td><i>$sub_rows[2]%</i></td><td align='right'><i>".number_format($sub_rows[3], $DecimalBase1)."</td><td colspan='6'>&nbsp;</td></tr>";
			}
		}	
	}  
	  
	  if ($varChecks ==1)
	{
		$sub_query1 = " select chkjourdt.Chk_Number,chkjourdt.Chk_Bank, Date_Format(chkjourdt.Chk_ValueDate,'%d/%m/%Y'),chkjourdt.Currency_code, chkjourdt.Chk_Amount  ".
					  " from chkjourdt where chkjourdt.TransDt_id = '".$rows[11]."'";
						  
			$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='11'>&nbsp;</td></tr>";
				echo "<tr ><td></td><td><b><i>Check No.</i></b></td><td colspan='2'><b><i>Bank</i></b></td><td><b><i>Value Date</b></i></td><td align='left'><b><i>Currency</b></i></td><td align='left'><b><i>Amount</b></i></td><td colspan='4'>&nbsp;</td></tr>";
					
			while($sub_rows1=mysql_fetch_row($sub_result1)){
					echo "<tr ><td></td><td><i>$sub_rows1[0]&nbsp</i></td><td colspan='2'><i>$sub_rows1[1]&nbsp</i></td><td ><i>$sub_rows1[2] &nbsp</i></td><td align='left'><i>$sub_rows1[3]&nbsp</i></td><td align='left'><i>".number_format($sub_rows1[4], $DecimalBase)."</i></td><td colspan='4'>&nbsp;</td></tr>";
				}
				
			}	
	
	} 
 $col+=1;
}

if ($varGrpBy == 3) {
	echo"<tr><td><font size='3'><b>Currency</b></font></td><td colspan='2'></td><td colspan='2'><font size='3'><b>Sum of Amount</b></font></td></tr>";

	$queryTotal= " select transdt.currency_code, sum(transdt_amount)".
				 " from Trans ".
				 " left join journal on journal.journal_id=Trans.journal_id ".
				 " inner join TransDt on TransDt.Trans_id=Trans.Trans_id  ".
				 " inner join ledger on ledger.ledger_number=TransDt.ledger_number ";
				 
	if ($projectexist >	0 && $FI_RepRestrictProject == 1)
		$queryTotal = $queryTotal.$QueryRestrictPrj." on Trans.project_code like concat(QP.project_code,'%') ";
													 
	if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
		$queryTotal = $queryTotal.$QueryRestrictCst." on Trans.costcent_code like concat(QC.costcent_code,'%') ";
				
	$queryTotal = $queryTotal.
				 " where ( ($option = 1 and date_format(Trans.Trans_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Trans.Trans_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Trans.Trans_Date,'%Y-%m-%d')) ) ".
				 " 		or ($option = 2 and date_format(TransDt_ValueDate,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(TransDt_ValueDate,'%Y-%m-%d')) and ifNull(". $todate.",date_format(TransDt_ValueDate,'%Y-%m-%d')) )".
				 " 		or ( $option = 3 and Trans_Reference between ifnull($fromref,0) and ifnull($toref,9999999999999) )) ".
				 " and (($VoucherType = 1 and Trans.Trans_Type = 'RV') or ($VoucherType = 2 and Trans.Trans_Type = 'PV') or ($VoucherType = 3 and Trans.Trans_Type = 'DV') or ($VoucherType = 4 and Trans.Trans_Type = 'CV')) ".
				 " and (($dbcdType = 1 and  TransDt_dbcd='C') or ($dbcdType = 2 and  TransDt_dbcd='D') or ($dbcdType = 3 and 1 = 1)) ".
				 " and ifNull(Trans.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(Trans.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(Trans.Project_Code,'-1')) ".
				 " and ifNull(Trans.COSTCENT_CODE,'-1') between ifNull(".$fromcostcent.",ifNull(Trans.COSTCENT_CODE,'-1')) and ifNull(". $tocostcent.",ifNull(Trans.COSTCENT_CODE,'-1'))  ".
				 " and ledger.ledger_number = '".$t_LedgerNumber."' ".
				 " group by currency_code order by currency_code";

	//echo $queryTotal;exit;
	$queryTotal=mysql_query($queryTotal,$connection) or die(mysql_error());
	while($rowsTotal=mysql_fetch_row($queryTotal)){
		echo"<tr><td><i>$rowsTotal[0]</i></td><td colspan='2'></td><td colspan='2'>".number_format($rowsTotal[1],2)."</td></tr>";
	}
			
	echo"<tr><td colspan='11'>&nbsp;</td></tr>";
	 
}
		

if ($varGrpBy == 2) {
	echo"<tr><td><font size='3'><b>Currency</b></font></td><td colspan='2'></td><td colspan='2'><font size='3'><b>Sum of Amount</b></font></td></tr>";

	$queryTotal= " select transdt.currency_code, sum(transdt_amount)".
				 " from Trans ".
				 " left join journal on journal.journal_id=Trans.journal_id ".
				 " inner join TransDt on TransDt.Trans_id=Trans.Trans_id  ".
				 " inner join ledger on ledger.ledger_number=TransDt.ledger_number ".
				 " inner join clients on clients.ledger_number = ledger.ledger_number ".
				 " left join salesmen on clients.salesmen_code = salesmen.ledger_number ";
				 
	if ($projectexist >	0 && $FI_RepRestrictProject == 1)
		$queryTotal = $queryTotal.$QueryRestrictPrj." on Trans.project_code like concat(QP.project_code,'%') ";
													 
	if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
		$queryTotal = $queryTotal.$QueryRestrictCst." on Trans.costcent_code like concat(QC.costcent_code,'%') ";			 
				
	$queryTotal = $queryTotal.
				 " where ( ($option = 1 and date_format(Trans.Trans_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Trans.Trans_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Trans.Trans_Date,'%Y-%m-%d')) ) ".
				 " 		or ($option = 2 and date_format(TransDt_ValueDate,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(TransDt_ValueDate,'%Y-%m-%d')) and ifNull(". $todate.",date_format(TransDt_ValueDate,'%Y-%m-%d')) )".
				 " 		or ($option = 3 and Trans_Reference between ifnull($fromref,0) and ifnull($toref,9999999999999) )) ".
				 " and (($VoucherType = 1 and Trans.Trans_Type = 'RV') or ($VoucherType = 2 and Trans.Trans_Type = 'PV') or ($VoucherType = 3 and Trans.Trans_Type = 'DV') or ($VoucherType = 4 and Trans.Trans_Type = 'CV')) ".
				 " and (($dbcdType = 1 and  TransDt_dbcd='C') or ($dbcdType = 2 and  TransDt_dbcd='D') or ($dbcdType = 3 and 1 = 1)) ".
				 " and ifNull(Trans.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(Trans.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(Trans.Project_Code,'-1')) ".
				 " and ifNull(Trans.COSTCENT_CODE,'-1') between ifNull(".$fromcostcent.",ifNull(Trans.COSTCENT_CODE,'-1')) and ifNull(". $tocostcent.",ifNull(Trans.COSTCENT_CODE,'-1'))  ".
				 " and (ledger.ledger_number between ifnull(".$fromledger.",'0') and ifnull(".$toledger.",'9999999999999')) ".
				 " and salesmen.ledger_number = '".$t_SalesmenCode."' ".
				 " group by currency_code order by currency_code";

	//echo $queryTotal;exit;
	$queryTotal=mysql_query($queryTotal,$connection) or die(mysql_error());
	while($rowsTotal=mysql_fetch_row($queryTotal)){
		echo"<tr><td><i>$rowsTotal[0]</i></td><td colspan='2'></td><td colspan='2'>".number_format($rowsTotal[1],2)."</td></tr>";
	}
			
	echo"<tr><td colspan='11'>&nbsp;</td></tr>";
}
	
echo"<tr><td colspan='11'>&nbsp;</td></tr>";
echo"<td colspan='2'><font size='4'>Total of Currencies</font></td>";
echo"<tr height=14></tr>";
echo"<tr><td><font size='3'><b>Currency</b></font></td><td colspan='2'></td><td colspan='2'><font size='3'><b>Sum of Amount</b></font></td></tr>";

$queryTotal= " select  transdt.currency_code, sum(transdt_amount)".
			 " from Trans ".
			 " left join journal on journal.journal_id=Trans.journal_id ".
			 " inner join TransDt on TransDt.Trans_id=Trans.Trans_id  ".
			 " inner join ledger on ledger.ledger_number=TransDt.ledger_number ";
			 
if ($varGrpBy == 2) {		 
	$queryTotal = $queryTotal." inner join clients on clients.ledger_number = ledger.ledger_number ".
							  " left join salesmen on clients.salesmen_code = salesmen.ledger_number ";
}				 

if ($projectexist >	0 && $FI_RepRestrictProject == 1)
	$queryTotal = $queryTotal.$QueryRestrictPrj." on Trans.project_code like concat(QP.project_code,'%') ";
													 
if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
	$queryTotal = $queryTotal.$QueryRestrictCst." on Trans.costcent_code like concat(QC.costcent_code,'%') ";
		
$queryTotal = $queryTotal." where ( ($option = 1 and date_format(Trans.Trans_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Trans.Trans_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Trans.Trans_Date,'%Y-%m-%d')) ) ".
						  " or ($option = 2 and date_format(TransDt_ValueDate,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(TransDt_ValueDate,'%Y-%m-%d')) and ifNull(". $todate.",date_format(TransDt_ValueDate,'%Y-%m-%d')) )".
						  " or ( $option = 3 and Trans_Reference between ifnull($fromref,0) and ifnull($toref,9999999999999) )) ".
						  " and (($VoucherType = 1 and Trans.Trans_Type = 'RV') or ($VoucherType = 2 and Trans.Trans_Type = 'PV') or ($VoucherType = 3 and Trans.Trans_Type = 'DV') or ($VoucherType = 4 and Trans.Trans_Type = 'CV')) ".
						  " and (($dbcdType = 1 and  TransDt_dbcd='C') or ($dbcdType = 2 and  TransDt_dbcd='D') or ($dbcdType = 3 and  TransDt_dbcd='D')) ".
						  " And ifNull(Trans.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(Trans.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(Trans.Project_Code,'-1')) ".
						  " And ifNull(Trans.COSTCENT_CODE,'-1') between ifNull(".$fromcostcent.",ifNull(Trans.COSTCENT_CODE,'-1')) and ifNull(". $tocostcent.",ifNull(Trans.COSTCENT_CODE,'-1')) ".
						  " and (ledger.ledger_number between ifnull(".$fromledger.",'0') and ifnull(".$toledger.",'9999999999999')) ";
						  
if ($varGrpBy == 2) {
	$queryTotal = $queryTotal." and ifNull(salesmen.ledger_number,'-1') between ifNull(".$fromsalesman.",ifNull(salesmen.ledger_number,'-1')) and ifNull(". $tosalesman.",ifNull(salesmen.ledger_number,'-1')) ";
}
$queryTotal = $queryTotal. " group by currency_code order by currency_code";

//echo $queryTotal;exit;
$queryTotal=mysql_query($queryTotal,$connection) or die(mysql_error());
while($rowsTotal=mysql_fetch_row($queryTotal)){

	echo"<tr><td><i>$rowsTotal[0]</i></td><td colspan='2'></td><td colspan='2'>".number_format($rowsTotal[1],2)."</td></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;



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

?>