<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?php

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

print <<< HERE
<HTML>
<HEAD>
<TITLE> List of Purchases JV </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

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

table.master th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                 }				 
table.master td { font-size: 13px; 
                  font-family:Times-New-Roman;
                 }
				 
table.master tr.group { font-size: 13px; 
                        font-weight: bold;
                        font-family:Times-New-Roman;
                      }					 
				 
table.master td.sr  { font-size: 10px; 
					  font-style: italic; 
                      font-family:Times-New-Roman;
                    }	
					
table.group td { font-size: 12px; 
                 font-weight: bold;
				 font-style: italic;
                 font-family:Times-New-Roman;
               }				 

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

</style>


</HEAD>

HERE;



include("config.inc.php");

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

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

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}
$querylog="SELECT print as printaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')	";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}


if ($printaccess==0 or $printaccess=='')
echo'<BODY oncontextmenu="return false;">';
else
echo'<BODY>';


$querysetup="select grid_color1,grid_color2, DecimalBase, DecimalBase1, DecimalBase2, ifNull(PurchaseRMaskQty,0) As PurchaseRMaskQty, 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);
}


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


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

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


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


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


echo $printdate[2]."/".$printdate[1]."/".$printdate[0];
//echo DATE("d"."/"."m"."/"."Y");
echo "<br>".$dt[1]." ".$dt[2];

if ($curr == ''){
   $curr='Null';  
   $displaycur = '';
}   
else {
   $displaycur = $curr;
   $curr="'".$curr."'";
}  


$SelectedOptions = "Filter by: <br>";
if ($fromdate != '')      $SelectedOptions .= "-> Date From: ". $fromdate . "  To: ". $todate ."<br>";	
if ($fromref != '')       $SelectedOptions .= "-> Reference From: ". $fromref . "  To: ". $toref ."<br>";	
if ($displaycur != '')    $SelectedOptions .= "-> Currency: ". $displaycur ."<br>";	
if ($fromsupplier != '')  $SelectedOptions .= "-> Supplier From: ". $fromsupplier . "  To: ". $tosupplier ."<br>";
if ($fromproject != '')   $SelectedOptions .= "-> Project From: ". $fromproject . "  To: ". $toproject . "<br>";
if ($fromcostcent != '')  $SelectedOptions .= "-> Costcent From: ". $fromcostcent . "  To: ". $tocostcent . "<br>";


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


$fromproject   = GetSQLValueString($fromproject, 'text');
$toproject     = GetSQLValueString($toproject, 'text');
$fromcostcent  = GetSQLValueString($fromcostcent, 'text');
$tocostcent    = GetSQLValueString($tocostcent, 'text');
$fromdate	   = GetSQLValueString($fromdate, 'date');
$todate        = GetSQLValueString($todate, 'date');
$fromref       = GetSQLValueString($fromref, 'text');
$toref         = GetSQLValueString($toref, 'text');   
$fromsupplier  = GetSQLValueString($fromsupplier, 'text');
$tosupplier    = GetSQLValueString($tosupplier, 'text');

  
if (empty($option)) 	 $option = 2;	
if (empty($prtdetail))   $prtdetail = 0;


$MasterByLabel = '';
if ($groupby == 2 )       $MasterByLabel  = 'Supplier';
else if ($groupby == 4 )  $MasterByLabel  = 'Project';
else if ($groupby == 5 )  $MasterByLabel  = 'CostCenter';

if ($MasterByLabel != '')
	$SelectedOptions = $SelectedOptions."Group by: ".$MasterByLabel;

print <<<HERE
</font></td>
</tr>
<tr>
   <td colspan='4' width='100%'> <hr width='100%' color='black' size='1'> </td>
</tr>
</table>

<table class="params" width="100%" border="0" align="center" >
<tr>
  <td width="100%" valign='top'><b>$SelectedOptions</b></td>
</tr>
<tr height='5'><td><h5></td></tr>
</table>

HERE;

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

//Create Table Columns
CreateTableHeader();		

echo"	<tr>";
echo"		<th align='left'>Date</th>
			<th align='left'>Ref.</th>
			<th align='left'>JV Ref.</th>
			<th align='left'>Ledger No.</th>
			<th align='left'>Name</th>
			<th align='center'>Cur.</th>
			<th align='center'>VAT</th>
			<th align='center'>Amount</th>";
echo"	</tr>";	
echo"	</table></td></tr>";	
echo"  <tr><td colspan='3'><hr width='100%' color='black' size='2'></td></tr>";

if ($prtdetail == 1) {
	echo "  <tr><td colspan='3'>";

	//Create Detail Table Columns
	CreateTableDetail();		

	echo"	<tr>";
	echo"		<th >&nbsp;</th>
				<th align='left'>Ledger No.</th>
				<th align='left'>Name</th>
				<th align='left'>Description</th>
				<th align='center'>Qty</th>
				<th align='center'>Price</th>
				<th align='center'>Total</th>";
	echo"	</tr>";	
	echo"	</table></td></tr>";	
	echo"  <tr><td colspan='3'><hr width='100%' color='black' size='2'></td></tr>";

}
	
 
$QueryRestrictPrj = " Inner join ( select distinct project_code from ".$_SESSION['dbLabel'].".loginstk ".
					" 			   where loginstk.user_account='".$_SESSION['useraccount']."' ".
					"              and (project_code is not null and project_code != '') ".
					"			 ) Q4  ";
					
$QueryRestrictCst = " Inner join ( select distinct costcent_code from ".$_SESSION['dbLabel'].".loginstk ".
					"			   where loginstk.user_account='".$_SESSION['useraccount']."' ".
					"              and (costcent_code is not null and costcent_code != '') ".
					"			 ) Q5  ";
					


$query = '';
	
$query = $query." Select if($groupby=2, PurchasesJV.Supplier_Code, if($groupby=4, PurchasesJV.Project_Code, if($groupby=5, PurchasesJV.Costcenter_Code, ''))) As MasterCode,   ".
				" if($groupby=2, Suppliers.Ledger_Name, if($groupby=4, Project.Project_Name, if($groupby=5, Costcent.Costcent_Name, ''))) As MasterDescription,  ".
				" PurchasesJV.PurchaseJV_Id As PurchaseJVId, Date_Format(PurchasesJV.PurchaseJV_Date,'%d/%m/%Y') As PurchaseJVDate, PurchasesJV.PurchaseJV_Reference As PurchaseJVReference, ".
				" if(upper($curr)='".$Base1."', '".$Base1."', if(upper($curr)='".$Base2."', '".$Base2."', PurchasesJV.Currency_Code)) As CurrencyCode, ".
				" Journal.Journal_RefGovernment As JournalRef, PurchasesJV.Supplier_Code As SupplierCode, Suppliers.Ledger_Name As SupplierName, ".
				" if(upper($curr)='".$Base1."', ifNull(PurchasesJV.PurchaseJV_VatBase1,0), if(upper($curr)='".$Base2."', ifNull(PurchasesJV.PurchaseJV_VatBase2,0), ifNull(PurchasesJV.PurchaseJV_VAT,0))) As PurchaseJVVAT, ".
				" if(upper($curr)='".$Base1."', ifNull(PurchasesJV.PurchaseJV_TotalBase1,0), if(upper($curr)='".$Base2."', ifNull(PurchasesJV.PurchaseJV_TotalBase2,0), ifNull(PurchasesJV.PurchaseJV_Total,0))) As PurchaseJVTotal ".
				" From PurchasesJV  ".
				" Left Join Journal on Journal.Journal_Id = PurchasesJV.Journal_Id ".
				" Left Join Suppliers on Suppliers.Ledger_Number = PurchasesJV.Supplier_Code ".
				" Left Join Costcent on Costcent.Costcent_Code = PurchasesJV.Costcenter_Code ".
				" Left Join Project on Project.Project_Code = PurchasesJV.Project_Code ";

if ($projectexist >	0 && $FI_RepRestrictProject == 1)
	$query= $query.$QueryRestrictPrj." on PurchasesJV.project_code like concat(Q4.project_code,'%') ";
								 
if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
	$query= $query.$QueryRestrictCst." on PurchasesJV.Costcenter_Code like concat(Q5.costcent_code,'%') ";		 
					   

$query= $query.	" Where (PurchasesJV.PurchaseJV_Reference Is Not Null And PurchasesJV.PurchaseJV_Reference != '')  ".
				" And ( ($option = 2 and  ifNull(PurchaseJV_Reference,'-1') between ifNull(".$fromref.", ifNull(PurchaseJV_Reference,'-1')) and ifNull(".$toref.", ifNull(PurchaseJV_Reference,'-1'))) or ".
			    "       ($option = 1 and date_format(PurchasesJV.PurchaseJV_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(PurchasesJV.PurchaseJV_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(PurchasesJV.PurchaseJV_Date,'%Y-%m-%d')) )) ".
				" And ifNull(PurchasesJV.Supplier_Code,'-1') between ifNull(".$fromsupplier.",ifNull(PurchasesJV.Supplier_Code,'-1')) and ifNull(". $tosupplier.",ifNull(PurchasesJV.Supplier_Code,'-1')) ".
				" And ifNull(PurchasesJV.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(PurchasesJV.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(PurchasesJV.Project_Code,'-1')) ".
				" And ifNull(PurchasesJV.Costcenter_Code,'-1') between ifNull(".$fromcostcent.",ifNull(PurchasesJV.Costcenter_Code,'-1')) and ifNull(". $tocostcent.",ifNull(PurchasesJV.Costcenter_Code,'-1')) ";
			
$query= $query." Order By MasterCode, if($option=1, PurchaseJV_Date, PurchaseJV_Reference ), if($option=1, PurchaseJV_Reference, Null) ";


	
//echo $query; exit;	

$ArrayTotalByMaster   = Array();
$ArrayTotalByReport   = Array();
$ArrayCurrency	      = Array();

$MasterByCode = '';
$MasterByName = '';

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

	if (($groupby != 1) && ($MasterByCode != $rows[0] && $MasterByCode != '')) {
		    
		echo "<tr>";
		echo "   <td colspan='3'><hr width='100%' color='black' size='1'></td>";
		echo "</tr>";
			
		echo "<tr><td colspan='3'>";

		// Display Totals By Group	
		$StyleClass  = 'totals';
		$TotalsLabel = 'Total by '.$MasterByLabel;
		
		DisplayTotals($curr, $Base1, $Base2, $ArrayCurrency, $ArrayTotalByMaster, $DecimalBase, $TotalsLabel, $StyleClass);		

		echo"</td></tr>";
		echo "<tr><td colspan='3'>&nbsp;</td></tr>";	
			
	}
	
	if (($groupby != 1) && ($MasterByCode == '' || $MasterByCode!= $rows[0])) {
		echo "<tr class='group'>";
		echo "   <td align='left' valign='top' colspan='3' >". $rows[0]." - ".$rows[1] ."</td>";
		echo "</tr>"; 
		
	    $MasterByCode = $rows[0];
		$MasterByName = $rows[1];
		
		unset($ArrayTotalByMaster);
	}
	
	
	
	echo "<tr><td colspan='3'>";
	
	//Create Table Columns
	CreateTableHeader();		

	if ($prtdetail == 1) echo "<tr class='group'>";
	else echo "<tr>";
	
	echo "	<td align='left' valign='top' >". $rows[3] ."</td>";
	echo "	<td align='left' valign='top' >". $rows[4] ."</td>";
	echo "	<td align='left' valign='top' >". $rows[6] ."</td>";
	echo "	<td align='left' valign='top' >". $rows[7] ."</td>";
	echo "	<td align='left' valign='top' >". $rows[8] ."</td>";
	echo "	<td align='center' valign='top' >". $rows[5] ."</td>";
	echo "	<td align='right' valign='top' >". format_currency($rows[9], $DecimalBase) ."</td>";
	echo "	<td align='right' valign='top' >". format_currency($rows[10], $DecimalBase) ."</td>";
	echo "</tr>";
	echo"</table></td></tr>";

	if ($prtdetail == 1) {
		$queryDetail= " Select PurchasesJVDt.Ledger_Number, Ledger.Ledger_Name, PurchaseJVDt_Description, PurchaseJVDt_Quantity, ".
					  " round(PurchaseJVDt_Price,".$DecimalBase."), round(PurchaseJVDt_PriceBase1,".$DecimalBase."), round(PurchaseJVDt_PriceBase2,".$DecimalBase."), ".
					  " PurchaseJVDt_DiscPer, ".
					  " (ifNull(PurchaseJVDt_Quantity,0) * ifNull(PurchaseJVDt_Price,0) * (1 - ifNull(PurchaseJVDt_DiscPer,0)/100)) As PurchaseJVDt_Total,  ".
					  " (ifNull(PurchaseJVDt_Quantity,0) * ifNull(PurchaseJVDt_PriceBase1,0) * (1 - ifNull(PurchaseJVDt_DiscPer,0)/100)) As PurchaseJVDt_TotalBase1,  ".
					  " (ifNull(PurchaseJVDt_Quantity,0) * ifNull(PurchaseJVDt_PriceBase2,0) * (1 - ifNull(PurchaseJVDt_DiscPer,0)/100)) As PurchaseJVDt_TotalBase2  ".
					  " From PurchasesJVDt Inner Join Ledger on Ledger.Ledger_Number = PurchasesJVDt.Ledger_Number ".
					  " where PurchasesJVDt.PurchaseJV_Id = '".$rows[2]."'";

		//echo $queryDetail;            
		
		echo "<tr><td colspan='3'>";
	
		//Create Table Columns
		CreateTableDetail();	
		
		$resultDetail=mysql_query($queryDetail,$connection) or die(mysql_error());
		while($rowsDetail=mysql_fetch_row($resultDetail)){

			echo "<tr>";
			echo "	<td align='left' valign='top' >&nbsp;</td>";
			echo "	<td align='left' valign='top' >". $rowsDetail[0] ."</td>";
			echo "	<td align='left' valign='top' >". $rowsDetail[1] ."</td>";
			echo "	<td align='left' valign='top' >". $rowsDetail[2] ."</td>";
			echo "	<td align='right' valign='top' >". number_format($rowsDetail[3], $PurchaseRMaskQty) ."</td>";
			
			if (trim($curr, "'") == $Base1) {
				echo "	<td align='right' valign='top' >". format_currency($rowsDetail[5], $DecimalBase) ."</td>";
				echo "	<td align='right' valign='top' >". format_currency($rowsDetail[9], $DecimalBase) ."</td>";
			}	
			else if (trim($curr, "'") == $Base2) {
				echo "	<td align='right' valign='top' >". format_currency($rowsDetail[6], $DecimalBase) ."</td>";
				echo "	<td align='right' valign='top' >". format_currency($rowsDetail[10], $DecimalBase) ."</td>";
			}
			else {
				echo "	<td align='right' valign='top' >". format_currency($rowsDetail[4], $DecimalBase) ."</td>";
				echo "	<td align='right' valign='top' >". format_currency($rowsDetail[8], $DecimalBase) ."</td>";
			}
			
			echo "</tr>";
		}
		
		echo"</table></td></tr>";
		echo "<tr><td colspan='3'>&nbsp;</td></tr>";		
	
	}
	
	
	$ArrayTotalByReport['Vat'][$rows[5]]        = $ArrayTotalByReport['Vat'][$rows[5]] + $rows[9];
	$ArrayTotalByReport['Amount'][$rows[5]]     = $ArrayTotalByReport['Amount'][$rows[5]] + $rows[10];
				
	$ArrayTotalByMaster['Vat'][$rows[5]]        = $ArrayTotalByMaster['Vat'][$rows[5]] + $rows[9];
	$ArrayTotalByMaster['Amount'][$rows[5]]     = $ArrayTotalByMaster['Amount'][$rows[5]] + $rows[10];
	
	
	$RefFound = 0;
	$count = count ($ArrayCurrency); 
	for ($j=0; $j<$count; $j++){
		if ($ArrayCurrency[$j] == $rows[5])
			$RefFound = 1;
	}
				
	if ($RefFound == 0) 
		$ArrayCurrency[$count] = $rows[5];
	
}


// Display Totals By Group	
if ($groupby != 1 && $MasterByCode != '') {
		    
	echo "<tr>";
	echo "   <td colspan='3'><hr width='100%' color='black' size='1'></td>";
	echo "</tr>";
			
	echo "  <tr><td colspan='3'>";

	
	$StyleClass  = 'totals';
	$TotalsLabel = 'Total by '.$MasterByLabel;
	
	DisplayTotals($curr, $Base1, $Base2, $ArrayCurrency, $ArrayTotalByMaster, $DecimalBase, $TotalsLabel, $StyleClass);		

	echo"	</td></tr>";
	echo "<tr><td colspan='3'>&nbsp;</td></tr>";	
			
}

// Display Totals By Report			
echo "<tr>";
echo "   <td colspan='3'><hr width='100%' color='black' size='1'></td>";
echo "</tr>";
echo "  <tr style='background-color: #F0F0F0;'><td colspan='3'>";


$StyleClass  = 'totals';
$TotalsLabel = 'Totals';
DisplayTotals($curr, $Base1, $Base2, $ArrayCurrency, $ArrayTotalByReport, $DecimalBase, $TotalsLabel, $StyleClass);		

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

echo "</table>";
	


function CreateTableHeader() {

	echo "  <table  border='0' align='left' class='master' style='table-layout:fixed' cellpadding='0' cellspacing='0'>
				<col width=80>
				<col width=90>
				<col width=80>
				<col width=100>
				<col width=240>
				<col width=80>
				<col width=100>
				<col width=120>";    
				
	

}

function CreateTableDetail() {

	echo "  <table  border='0' align='left' class='detail' style='table-layout:fixed' cellpadding='0' cellspacing='0'>
				<col width=20>
				<col width=70>
				<col width=170>
				<col width=300>
				<col width=70>
				<col width=100>
				<col width=120>
				<col width=40>";    
				
	

}

function DisplayTotals($PCurr, $PBase1, $PBase2, $PArrayCurrency, $PArrayTotal, $PDecimalBase, $PTotalsLabel, $PStyleClass) {

	echo "<table  border='0' align='left' class='".$PStyleClass."' style='table-layout:fixed' cellpadding='0' cellspacing='0'>
			<col width=80>
			<col width=90>
			<col width=80>
			<col width=100>
			<col width=240>
			<col width=80>
			<col width=100>
			<col width=120>";  
	
	$PCurr = trim($PCurr,"'");
	
	if ($PCurr == $PBase1) {
		echo "	<tr>";
		echo "  	<td align='right' colspan='6' valign='top' >".$PTotalsLabel."  ".$PBase1."</td>";
		echo "		<td align='right'  valign='top' >". format_currency($PArrayTotal['Vat'][$PBase1], $PDecimalBase) ."</td>";
		echo "		<td align='right' valign='top' >". format_currency($PArrayTotal['Amount'][$PBase1], $PDecimalBase) ."</td>";
		echo"	</tr>";	
	}
	else if ($PCurr == $PBase2) {
		echo "	<tr>";
		echo "  	<td align='right' colspan='6' valign='top' >".$PTotalsLabel."  ".$PBase2."</td>";
		echo "		<td align='right'  valign='top' >". format_currency($PArrayTotal['Vat'][$PBase2], $PDecimalBase) ."</td>";
		echo "		<td align='right' valign='top' >". format_currency($PArrayTotal['Amount'][$PBase2], $PDecimalBase) ."</td>";
		echo"	</tr>";	
	}
	else {
		$count = count ($PArrayCurrency); 
		for ($i=0; $i<$count; $i++){
		
			$Currency = $PArrayCurrency[$i];
			
			if ($PArrayTotal['Amount'][$Currency] != 0) {
				echo "	<tr>";
				echo "  	<td align='right' colspan='6' valign='top' >".$PTotalsLabel."  ".$Currency."</td>";
				echo "		<td align='right'  valign='top' >". format_currency($PArrayTotal['Vat'][$Currency], $PDecimalBase) ."</td>";
				echo "		<td align='right' valign='top' >". format_currency($PArrayTotal['Amount'][$Currency], $PDecimalBase) ."</td>";
				echo"	</tr>";	
			}	
		}
	
	}
	
	
	echo"</table>";


}


function format_currency($amount, $DecimalBase) { 

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


function current_server_time($connection){

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

  return $now;
}

function explodeword($date)
{

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

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

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

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



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

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

?>