<!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");
}

 function logHtml($content){
        ////////////Debuging purpose //////////////////////////////////////

  echo '<script type="text/javascript">console.log("' .  str_replace("'","\'",$content) . '")</script>';

   }

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

<style type="text/css">

tbody:nth-child(1) > tr:nth-child(3) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(2) > tr:nth-child(3) > td:nth-child(1) > table:nth-child(1) {
    display: inline-table;
    width: 100%;
    margin: 0 auto;
}

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;
                 }					  
tr.trstyle 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(ClientSalesRMaskQty,0) As ClientSalesRMaskQty, 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 Sales 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 ($fromclient != '')    $SelectedOptions .= "-> Client From: ". $fromclient . "  To: ". $toclient ."<br>";
if ($fromsalesman != '')  $SelectedOptions .= "-> Salesman From: ". $fromsalesman . "  To: ". $tosalesman ."<br>";
if ($fromproject != '')   $SelectedOptions .= "-> Project From: ". $fromproject . "  To: ". $toproject . "<br>";
if ($fromcostcent != '')  $SelectedOptions .= "-> Costcent From: ". $fromcostcent . "  To: ". $tocostcent . "<br>";
//
if ( $prProfitMargin !=0)
if ($filtermargin != '')  $SelectedOptions .= "-> Profit Margin: ". $filtermargin . "<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');   
$fromclient    = GetSQLValueString($fromclient, 'text');
$toclient      = GetSQLValueString($toclient, 'text');
$fromsalesman  = GetSQLValueString($fromsalesman, 'text');
$tosalesman    = GetSQLValueString($tosalesman, 'text');  

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

$MasterByLabel = '';
if ($groupby == 2 )       $MasterByLabel  = 'Client';
else if ($groupby == 3 )  $MasterByLabel  = 'Salesman';
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='left'>Cur.</th>
			<th align='right'>Gross Total.</th>
			<th align='right'>Discount.</th>
			<th align='right'>VAT</th>
			<th align='right'>Net Total</th>";
		    
$col = 11;
			if ($prProfitMargin ==1 )
			{
				
				echo "<th align='right'>Total Cost</th> ";
		echo "    <th align='right'>Profit Amount</th>";
				$col++;
if($filtermargin != "Cost")	
{	
		echo "			<th align='right'>Profit Margin Selling</th> ";
		$col++;
}
else{

echo "			<th align='right'>Profit Margin Cost</th>";
			$col++;
	}
			}

								if($showpaymentmode==1)
	{
	echo"		<th align='right'>Cash Amount ($Base1)</th>
			<th align='right'>Bank Amount ($Base1)</th>
			<th align='right'>Card Amount ($Base1)</th>
			<th align='right'>Cash Amount ($Base2)</th>
			<th align='right'>Bank Amount ($Base2)</th>
			<th align='right'>Card Amount ($Base2)</th>";
			$col += 6;
	}

echo"	</tr>";

//echo"	</table></td></tr>";	
echo"  <tr><td colspan='$col'><hr width='100%' color='black' size='2'></td></tr>";

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

	//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='right'>Price</th>
			    <th align='right'>Discount</th>
				<th align='right'>Total</th>
				<th align='right'>Cost</th>";
		    

			 global $prProfitMargin ,$filtermargin;
if ($prProfitMargin ==1 )
	{
		
		echo "    <th align='right'>Total Cost</th> ";

			if ($filtermargin != "Cost")
			{
			//	
				echo"	<th align='right'>Profit Amount</th>
				<th align='right'>Profit Margin Selling Price</th>";

			}
			else
			{
				//
				echo"	<th align='right'>Profit Amount</th>

				<th align='right'>Profit Margin Cost Price</th>";
			}

	}	

	echo"	</tr>";	
	echo"	</table></td></tr>";	
	echo"  <tr><td colspan='$col'><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, SalesJV.Client_Code, if($groupby=3, ifNull(SalesJV.Salesman_Code, 'None'), if($groupby=4, SalesJV.Project_Code, if($groupby=5, SalesJV.Costcenter_Code, '')))) As MasterCode,   ".
		" if($groupby=2, Clients.Ledger_Name, if($groupby=3, ifNull(Salesmen.Ledger_Name, 'None'), if($groupby=4, Project.Project_Name, if($groupby=5, Costcent.Costcent_Name, '')))) As MasterDescription,  ".
		" SalesJV.SalesJV_Id As SalesJVId, Date_Format(SalesJV.SalesJV_Date,'%d/%m/%Y') As SalesJVDate, SalesJV.SalesJV_Reference As SalesJVReference, ".
		" if(upper($curr)='".$Base1."', '".$Base1."', if(upper($curr)='".$Base2."', '".$Base2."', SalesJV.Currency_Code)) As CurrencyCode, ".
		" Journal.Journal_RefGovernment As JournalRef, SalesJV.Client_Code As ClientCode, Clients.Ledger_Name As ClientName, ".				
		" if(upper($curr)='".$Base1."', ifNull(SalesJV.SalesJV_TotalBase1,0), if(upper($curr)='".$Base2."', ifNull(SalesJV.SalesJV_TotalBase2,0), ifNull(SalesJV.SalesJV_Total,0))) As SalesJVGrossTotal  , ".
		" if(upper($curr)='".$Base1."', ifNull(SalesJV.salesjv_discountamount1,0), if(upper($curr)='".$Base2."', ifNull(SalesJV.salesjv_discountamount2,0), ifNull(SalesJV.salesjv_discountamount,0))) As SalesJVDiscount, ".
		" if(upper($curr)='".$Base1."', ifNull(SalesJV.SalesJV_VatBase1,0), if(upper($curr)='".$Base2."', ifNull(SalesJV.SalesJV_VatBase2,0), ifNull(SalesJV.SalesJV_VAT,0))) As SalesJVVAT, "
		."if(upper($curr)='$Base1', (ifNull(SalesJV.SalesJV_TotalBase1, 0) - ifNull(SalesJV.SalesJV_DiscountAmount1, 0) + ifNull(SalesJV.SalesJV_VatBase1, 0)), if(upper($curr)='$Base2', (ifNull(SalesJV.SalesJV_TotalBase2, 0) - ifNull(SalesJV.SalesJV_DiscountAmount2, 0) + ifNull(SalesJV.SalesJV_VatBase2, 0)), (ifNull(SalesJV.SalesJV_Total, 0) - ifNull(SalesJV.SalesJV_DiscountAmount, 0) + ifNull(SalesJV.SalesJV_VAT, 0)))) AS SalesNetTotal,"	
		."    Sum(if(upper($curr)='$Base1', Salesjvdt.costbase1  *salesjvdt_quantity, if(upper($curr)='$Base2', Salesjvdt.costbase2  *salesjvdt_quantity, Salesjvdt.cost  *salesjvdt_quantity)) )AS TotalCost,"	  

		." if(upper($curr)='$Base1',(SalesJv.SalesJv_Totalbase1 - SalesJV.SalesJV_DiscountAmount1) - (Sum(Salesjvdt.costbase1  *salesjvdt_quantity)), if(upper($curr)='$Base2', (SalesJv.SalesJv_Totalbase2 - SalesJV.SalesJV_DiscountAmount2) - (Sum(Salesjvdt.costbase2  *salesjvdt_quantity)), (SalesJv.SalesJv_Total - SalesJV.SalesJV_DiscountAmount) - (Sum(Salesjvdt.cost  *salesjvdt_quantity)))) AS ProfitAmount,"  

		."if(upper($curr)='$Base1', ( 100 *   (  (SalesJv.SalesJV_Totalbase1 - SalesJV.SalesJV_DiscountAmount1) - Sum(Salesjvdt.costbase1  *salesjvdt_quantity))) / (SalesJv.SalesJV_Totalbase1 - SalesJV.SalesJV_DiscountAmount1), if(upper($curr)='$Base2', ( 100 *   (  (SalesJv.SalesJV_Totalbase2 - SalesJV.SalesJV_DiscountAmount2) - Sum(Salesjvdt.costbase2  *salesjvdt_quantity))) / (SalesJv.SalesJV_Totalbase2 - SalesJV.SalesJV_DiscountAmount2), ( 100 *   (  (SalesJv.SalesJV_Total - SalesJV.SalesJV_DiscountAmount) - Sum(Salesjvdt.cost  *salesjvdt_quantity))) / (SalesJv.SalesJV_Total - SalesJV.SalesJV_DiscountAmount))) AS ProfitMarginSellingPrice,"

		."   if(upper($curr)='$Base1',  ( 100 *   (  (SalesJv.SalesJV_Totalbase1 - SalesJV.SalesJV_DiscountAmount1) - Sum(Salesjvdt.costbase1  *salesjvdt_quantity))) / (Sum(Salesjvdt.costbase1  *salesjvdt_quantity)) , if(upper($curr)='$Base2',  ( 100 *   (  (SalesJv.SalesJV_Totalbase2 - SalesJV.SalesJV_DiscountAmount2) - Sum(Salesjvdt.costbase2  *salesjvdt_quantity))) / (Sum(Salesjvdt.costbase2  *salesjvdt_quantity)) ,  ( 100 *   (  (SalesJv.SalesJV_Total - SalesJV.SalesJV_DiscountAmount) - Sum(Salesjvdt.cost  *salesjvdt_quantity))) / (Sum(Salesjvdt.cost  *salesjvdt_quantity)) )) AS ProfitMarginCost,"

				/* cash bank card for base1 and base2  */
				."  ifNull(SalesJV.salesjv_cashammount1,0)       as 'Cash Amount ($Base1)',  ".
				"  ifNull(SalesJV.salesjv_bankammount1,0)       as 'Bank Amount ($Base1)',  ".
				"  ifNull(SalesJV.SalesJv_CreditCardAmmount1,0) as 'Card Amount ($Base1)',  ".
				"  ifNull(SalesJV.salesjv_cashammount2,0)       as 'Cash Amount ($Base2)',   ".
				"  ifNull(SalesJV.salesjv_bankammount2,0)       as 'Bank Amount ($Base2)',  ".
				"  ifNull(SalesJV.SalesJv_CreditCardAmmount2,0) as 'Card Amount ($Base2)' ".

				/*"SalesJv_CashAmmount1 as CashAmmount1 ,  SalesJv_BankAmmount1 as BankAmmount1 , SalesJv_CreditCardAmmount1 as CrediCardAmmount1 ".*/
				" From SalesJV  ".
				" Left Join Journal on Journal.Journal_Id = SalesJV.Journal_Id ".
				" Left Join Clients on Clients.Ledger_Number = SalesJV.Client_Code ".
				" Left Join Salesmen on Salesmen.Ledger_Number = SalesJV.Salesman_Code ".
				" Left Join Costcent on Costcent.Costcent_Code = SalesJV.Costcenter_Code ".
				" Left Join Project on Project.Project_Code = SalesJV.Project_Code "
				."LEFT JOIN Salesjvdt on Salesjvdt.salesjv_id = salesjv.salesjv_id";

if ($projectexist >	0 && $FI_RepRestrictProject == 1)
	$query= $query.$QueryRestrictPrj." on SalesJV.project_code like concat(Q4.project_code,'%') ";

if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
	$query= $query.$QueryRestrictCst." on SalesJV.Costcenter_Code like concat(Q5.costcent_code,'%') ";		 

$query= $query.	" Where (SalesJV.SalesJV_Reference Is Not Null And SalesJV.SalesJV_Reference != '')  ".
				" And ( ($option = 2 and  ifNull(SalesJV_Reference,'-1') between ifNull(".$fromref.", ifNull(SalesJV_Reference,'-1')) and ifNull(".$toref.", ifNull(SalesJV_Reference,'-1'))) or ".
			    "       ($option = 1 and date_format(SalesJV.SalesJV_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(SalesJV.SalesJV_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(SalesJV.SalesJV_Date,'%Y-%m-%d')) )) ".
				" And ifNull(SalesJV.Client_Code,'-1') between ifNull(".$fromclient.",ifNull(SalesJV.Client_Code,'-1')) and ifNull(". $toclient.",ifNull(SalesJV.Client_Code,'-1')) ".
				" And ifNull(SalesJV.Salesman_Code,'-1') between ifNull(".$fromsalesman.",ifNull(SalesJV.Salesman_Code,'-1')) and ifNull(". $tosalesman.",ifNull(SalesJV.Salesman_Code,'-1')) ".
				" And ifNull(SalesJV.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(SalesJV.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(SalesJV.Project_Code,'-1')) ".
				" And ifNull(SalesJV.Costcenter_Code,'-1') between ifNull(".$fromcostcent.",ifNull(SalesJV.Costcenter_Code,'-1')) and ifNull(". $tocostcent.",ifNull(SalesJV.Costcenter_Code,'-1')) ";

$query= $query." GROUP BY salesjv.salesjv_id  Order By MasterCode, if($option=1, salesjv.SalesJV_Date, SalesJV_Reference ), if($option=2, SalesJV_Reference, Null) ";

 //echo $query; exit;	

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


$Arraytotalpayment = 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='$col'><hr width='100%' color='black' size='1'></td>";
		echo "</tr>";

		//echo "<tr><td colspan='$col'>";

		// Display Totals By Group	
		$StyleClass  = 'totals';
		$TotalsLabel = 'Total by '.$MasterByLabel;

		DisplayTotals($curr, $Base1, $Base2, $ArrayCurrency, $ArrayTotalByMaster, $DecimalBase, $TotalsLabel, $StyleClass);		

		DisplayTotalsPayment($curr, $Base1, $Base2, $ArrayCurrency, $Arraytotalpayment, $DecimalBase, $TotalsLabel, $StyleClass);		
		//echo"</td></tr>";
		echo "<tr><td colspan='$col'>&nbsp;</td></tr>";	

	}

	if (($groupby != 1) && ($MasterByCode == '' || $MasterByCode!= $rows[0])) {
		echo "<tr class='group'>";
		echo "   <td align='left' valign='top' colspan='$col' >". $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='left' 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 "	<td align='right' valign='top' >". format_currency($rows[11], $DecimalBase) ."</td>";
    echo "	<td align='right' valign='top' >". format_currency($rows[12], $DecimalBase) ."</td>";
  

				if ($prProfitMargin ==1 )
			{
	  echo "	<td align='right' valign='top' >". format_currency($rows[13], $DecimalBase) ."</td>";			

    echo "	<td align='right' valign='top' >". format_currency($rows[14], $DecimalBase) ."</td>";

			if ($filtermargin != "Cost")
			{
    echo "	<td align='right' valign='top' >". format_currency($rows[15], $DecimalBase) ."</td>";
	}
	else{
    echo "	<td align='right' valign='top' >". format_currency($rows[16], $DecimalBase) ."</td>";
	}
			}
	if($showpaymentmode==1)
	{
    echo "	<td align='right' valign='top' >". format_currency($rows[17], $DecimalBase) ."</td>";
    echo "	<td align='right' valign='top' >". format_currency($rows[18], $DecimalBase) ."</td>";
    echo "	<td align='right' valign='top' >". format_currency($rows[19], $DecimalBase) ."</td>";
    echo "	<td align='right' valign='top' >". format_currency($rows[20], $DecimalBase) ."</td>";
    echo "	<td align='right' valign='top' >". format_currency($rows[21], $DecimalBase) ."</td>";
    echo "	<td align='right' valign='top' >". format_currency($rows[22], $DecimalBase) ."</td>";
	}
	echo "</tr>";
	//echo"</table></td></tr>";

	if ($prtdetail == 1) {
		$queryDetail= " SELECT SalesJVDt.Ledger_Number,
       Ledger.Ledger_Name,
       SalesJVDt_Description,
       SalesJVDt_Quantity,
         if(upper($curr)='$Base1', SalesJVDt_Pricebase1, if(upper($curr)='$Base2', SalesJVDt_Pricebase2, SalesJVDt_Price)) AS price ,

		   if(upper($curr)='$Base1', SalesJVDT_DiscountAmount1, if(upper($curr)='$Base2', SalesJVDT_DiscountAmount1, SalesJVDT_DiscountAmount)) AS Discount ,

		   if(upper($curr)='$Base1',  (ifNull(SalesJVDt_Quantity, 0) * ifNull(SalesJVDt_Pricebase1, 0) * (1 - ifNull(SalesJVDt_DiscountPer, 0)/100)) , if(upper($curr)='$Base2',  (ifNull(SalesJVDt_Quantity, 0) * ifNull(SalesJVDt_Pricebase2, 0) * (1 - ifNull(SalesJVDt_DiscountPer, 0)/100)) , (ifNull(SalesJVDt_Quantity, 0) * ifNull(SalesJVDt_Price, 0) * (1 - ifNull(SalesJVDt_DiscountPer, 0)/100)) )) AS SalesJvDt_Total ,

         if(upper($curr)='$Base1', costbase1, if(upper($curr)='$Base2', costbase2, cost)) AS cost ,

          if(upper($curr)='$Base1', costbase1*SalesJVDt_Quantity, if(upper($curr)='$Base2', costbase2*SalesJVDt_Quantity, cost*SalesJVDt_Quantity)) AS ToTalcost ,

 if(upper($curr)='$Base1',  ( (salesjvdt_pricebase1*salesjvdt_quantity- SalesJVdt_DiscountAmount1) - (costbase1  *salesjvdt_quantity)) , if(upper($curr)='$Base2',  ( (salesjvdt_pricebase2*salesjvdt_quantity- SalesJVdt_DiscountAmount2) - (costbase2  *salesjvdt_quantity)) , ( (salesjvdt_price*salesjvdt_quantity- SalesJVdt_DiscountAmount) - (cost  *salesjvdt_quantity)) )) AS ProfitAmount ,

     if(upper($curr)='$Base1', ( 100 *   (  (salesjvdt_quantity*salesjvdt_pricebase1 - SalesJVdt_DiscountAmount1) -  (costbase1  *salesjvdt_quantity))) / (salesjvdt_pricebase1- SalesJVdt_DiscountAmount1 ), if(upper($curr)='$Base2', ( 100 *   (  (salesjvdt_quantity*salesjvdt_pricebase2 - SalesJVdt_DiscountAmount2) -  (costbase2  *salesjvdt_quantity))) / (salesjvdt_pricebase2- SalesJVdt_DiscountAmount2 ), ( 100 *   (  (salesjvdt_quantity*salesjvdt_price - SalesJVdt_DiscountAmount) -  (cost  *salesjvdt_quantity))) / (salesjvdt_price- SalesJVdt_DiscountAmount ))) AS profitsellingmargin , 

    if(upper($curr)='$Base1', ( 100 *   (  (salesjvdt_quantity*salesjvdt_pricebase1 - SalesJVdt_DiscountAmount1) -  (costbase1  *salesjvdt_quantity))) / ( (costbase1  *salesjvdt_quantity) ), if(upper($curr)='$Base2', ( 100 *   (  (salesjvdt_quantity*salesjvdt_pricebase2 - SalesJVdt_DiscountAmount2) -  (costbase2  *salesjvdt_quantity))) / ( (costbase2  *salesjvdt_quantity) ), ( 100 *   (  (salesjvdt_quantity*salesjvdt_price - SalesJVdt_DiscountAmount) -  (cost  *salesjvdt_quantity))) / ( (cost  *salesjvdt_quantity) ))) AS ProfitMarginCost

    ".

					  " From SalesJVDt Inner Join Ledger on Ledger.Ledger_Number = SalesJVDt.Ledger_Number ".
					  " where SalesJVDt.SalesJV_Id = '".$rows[2]."'";

		 //echo $queryDetail; exit;            

		echo "<tr><td colspan='$col'>";

		//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='center' valign='top' >". number_format($rowsDetail[3], $ClientSalesRMaskQty) ."</td>";

			echo "	<td align='right' valign='top' >". format_currency($rowsDetail[4], $DecimalBase) ."</td>";
			echo "	<td align='right' valign='top' >". format_currency($rowsDetail[5], $DecimalBase) ."</td>";
			echo "	<td align='right' valign='top' >". format_currency($rowsDetail[6], $DecimalBase) ."</td>";
			echo "	<td align='right' valign='top' >". format_currency($rowsDetail[7], $DecimalBase) ."</td>";
			echo "	<td align='right' valign='top' >". format_currency($rowsDetail[8], $DecimalBase) ."</td>";

	    global $prProfitMargin ,$filtermargin;
			if ($prProfitMargin ==1 )
	{

			if ($filtermargin != "Cost")
			{
			//	
				echo "	<td align='right' valign='top' >". format_currency($rowsDetail[9], $DecimalBase) ."</td>";
		    	echo "	<td align='right' valign='top' >". format_currency($rowsDetail[10], $DecimalBase) ."</td>";		

			}
			else
			{
				//
			echo "	<td align='right' valign='top' >". format_currency($rowsDetail[9], $DecimalBase) ."</td>";
			echo "	<td align='right' valign='top' >". format_currency($rowsDetail[11], $DecimalBase) ."</td>";	
			}

	}

			echo "</tr>";
		}

		echo"</table></td></tr>";
		echo "<tr><td colspan='$col'>&nbsp;</td></tr>";		

	}

	// Report
	$ArrayTotalByReport['grosstotal'][$rows[5]]        = $ArrayTotalByReport['grosstotal'][$rows[5]] + $rows[9];
	$ArrayTotalByReport['discount'][$rows[5]]        = $ArrayTotalByReport['discount'][$rows[5]] + $rows[10];

	$ArrayTotalByReport['Vat'][$rows[5]]        = $ArrayTotalByReport['Vat'][$rows[5]] + $rows[11];
	$ArrayTotalByReport['NetTotal'][$rows[5]]     = $ArrayTotalByReport['NetTotal'][$rows[5]] + $rows[12];

	$ArrayTotalByReport['totalcost'][$rows[5]]     = $ArrayTotalByReport['totalcost'][$rows[5]] + $rows[13];
	$ArrayTotalByReport['profitammount'][$rows[5]]     = $ArrayTotalByReport['profitammount'][$rows[5]] + $rows[14];

global $prProfitMargin;

global $filtermargin;

	if ($prProfitMargin ==1 )
	{

			if ($filtermargin != "Cost")
			{
			//	
				$ArrayTotalByReport['profitmargin'][$rows[5]]     = $ArrayTotalByReport['profitmargin'][$rows[5]] + $rows[15];
			}
			else
			{
				//
					$ArrayTotalByReport['profitmargin'][$rows[5]]     = $ArrayTotalByReport['profitmargin'][$rows[5]] + $rows[16];
			}

	}

	$Arraytotalpayment['cashbase1']       = $Arraytotalpayment['cashbase1']     + $rows[17];
	$Arraytotalpayment['bankbase1']       = $Arraytotalpayment['bankbase1']     + $rows[18];
	$Arraytotalpayment['creditbase1']     = $Arraytotalpayment['creditbase1']   + $rows[19];
	$Arraytotalpayment['cashbase2']       = $Arraytotalpayment['cashbase2']     + $rows[20];
	$Arraytotalpayment['bankbase2']       = $Arraytotalpayment['bankbase2']     + $rows[21];
	$Arraytotalpayment['creditbase2']     = $Arraytotalpayment['creditbase2']   + $rows[22];

	 // Master 
    $ArrayTotalByMaster['grosstotal'][$rows[5]]        = $ArrayTotalByMaster['grosstotal'][$rows[5]] + $rows[9];
    $ArrayTotalByMaster['discount'][$rows[5]]        = $ArrayTotalByMaster['discount'][$rows[5]] + $rows[10];
	$ArrayTotalByMaster['Vat'][$rows[5]]        = $ArrayTotalByMaster['Vat'][$rows[5]] + $rows[11];
	$ArrayTotalByMaster['NetTotal'][$rows[5]]     = $ArrayTotalByMaster['NetTotal'][$rows[5]] + $rows[12];

		$ArrayTotalByMaster['totalcost'][$rows[5]]     = $ArrayTotalByMaster['totalcost'][$rows[5]] + $rows[13];
	$ArrayTotalByMaster['profitammount'][$rows[5]]     = $ArrayTotalByMaster['profitammount'][$rows[5]] + $rows[14];

	if ($prProfitMargin ==1 )
	{

			if ($filtermargin != "Cost")
			{
			//	
				$ArrayTotalByMaster['profitmargin'][$rows[5]]     = $ArrayTotalByMaster['profitmargin'][$rows[5]] + $rows[15];
			}
			else
			{
				//
					$ArrayTotalByMaster['profitmargin'][$rows[5]]     = $ArrayTotalByMaster['profitmargin'][$rows[5]] + $rows[16];
			}

	}

	$ArrayTotalByMaster['cashbase1'][$rows[5]]       = $ArrayTotalByMaster['cashbase1'][$rows[5]] + $rows[17];
	$ArrayTotalByMaster['bankbase1'][$rows[5]]       = $ArrayTotalByMaster['bankbase1'][$rows[5]] + $rows[18];
	$ArrayTotalByMaster['creditbase1'][$rows[5]]     = $ArrayTotalByMaster['creditbase1'][$rows[5]] + $rows[19];
	$ArrayTotalByMaster['cashbase2'][$rows[5]]       = $ArrayTotalByMaster['cashbase2'][$rows[5]] + $rows[20];
	$ArrayTotalByMaster['bankbase2'][$rows[5]]       = $ArrayTotalByMaster['bankbase2'][$rows[5]] + $rows[21];
	$ArrayTotalByMaster['creditbase2'][$rows[5]]     = $ArrayTotalByMaster['creditbase2'][$rows[5]] + $rows[22];

	$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='$col'><hr width='100%' color='black' size='1'></td>";
	echo "</tr>";

	//echo "  <tr><td colspan='$col'>";

	$StyleClass  = 'totals';
	$TotalsLabel = 'Total by '.$MasterByLabel;

	DisplayTotals($curr, $Base1, $Base2, $ArrayCurrency, $ArrayTotalByMaster, $DecimalBase, $TotalsLabel, $StyleClass);		
    DisplayTotalsPayment($curr, $Base1, $Base2, $ArrayCurrency, $Arraytotalpayment, $DecimalBase, $TotalsLabel, $StyleClass);		
	//echo"	</td></tr>";
	echo "<tr><td colspan='$col'>&nbsp;</td></tr>";	

}

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

$StyleClass  = 'totals';
$TotalsLabel = 'Totals';
DisplayTotals($curr, $Base1, $Base2, $ArrayCurrency, $ArrayTotalByReport, $DecimalBase, $TotalsLabel, $StyleClass);		
DisplayTotalsPayment($curr, $Base1, $Base2, $ArrayCurrency, $Arraytotalpayment, $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= 66px>
				<col width=50px>
				<col width=60px>
				<col width=80px>
				<col width=160px>
				<col width=63px>
				<col width=70px>
				<col width=70px>
				<col width=75px>
				<col width=90px>
				<col width=90px>";

				global $prProfitMargin; global $filtermargin;

						if ($prProfitMargin ==1 )
			{	
		echo"		<col width=90px > ";

			if ($filtermargin != "Cost")
			{
				echo "<col width=70px>";
			}
			else{
				echo "<col width=70px>";
			}

			}
				global $showpaymentmode;
					if($showpaymentmode==1)
	{

	echo "	    <col width=80px>
				<col width=80px>
				<col width=80px>
				<col width=80px>
				<col width=80px>
				<col width=80px>		 ";
	}				

}

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=100>
				<col width=100> 
				<col width=100> 
				<col width=100>";

				global $prProfitMargin;
				if ($prProfitMargin ==1 )
	{
		        echo"<col width=100>

				<col width=200>";    
	}

}

function DisplayTotals($PCurr, $PBase1, $PBase2, $PArrayCurrency, $PArrayTotal, $PDecimalBase, $PTotalsLabel, $PStyleClass) {
global $prProfitMargin; global $filtermargin;
global $showpaymentmode;
/* echo "<table  border='0' align='left' class='".$PStyleClass."' style='table-layout:fixed' cellpadding='0' cellspacing='0'>
			    <col width= 80>
				<col width=70>
				<col width=70>
				<col width=80>
				<col width=144>
				<col width=63>
				<col width=70>
				<col width=70>
				<col width=75>
				<col width=90>
				<col width=90>"; 

						if ($prProfitMargin ==1 )
			{	
		echo"		<col width=90 > ";

			if ($filtermargin != "Cost")
			{
				echo "<col width=70>";
			}
			else{
				echo "<col width=70>";
			}

			}

					if($showpaymentmode==1)
	{

	echo "	    <col width=80>
				<col width=80>
				<col width=80>
				<col width=80>
				<col width=80>
				<col width=80>		 ";
	}								
 */
	$PCurr = trim($PCurr,"'");
	if ($PCurr == $PBase1) {

 echo "
<tr class='trstyle'>
<td align='left' colspan='6' valign= 'top'> $PTotalsLabel  $PBase1  </td>"

."<td align='right' valign= 'top'> " .format_currency($PArrayTotal['grosstotal'][$PBase1], $PDecimalBase) ." </td>"
."<td align='right' valign= 'top'> " .format_currency($PArrayTotal['discount'][$PBase1], $PDecimalBase) ." </td>"
."<td align='right' valign= 'top'> " .format_currency($PArrayTotal['Vat'][$PBase1], $PDecimalBase) ." </td>"
."<td align='right' valign= 'top'>" . format_currency($PArrayTotal['NetTotal'][$PBase1], $PDecimalBase) ."</td>"
."<td align='right' valign= 'top'>" . format_currency($PArrayTotal['totalcost'][$PBase1], $PDecimalBase) ."</td>";

 if ($prProfitMargin ==1)

	 {

 echo "<td align='right' valign= 'top'> " .format_currency($PArrayTotal['profitammount'][$PBase1], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> " .format_currency($PArrayTotal['profitmargin'][$PBase1], $PDecimalBase) ." </td>";

	 }

if ($showpaymentmode == 1)
{

 echo "<td align='right' valign= 'top'> "  ."</td>"; //.format_currency($PArrayTotal['cashbase1'][$PBase1], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> "  ."</td>"; //.format_currency($PArrayTotal['bankbase1'][$PBase1], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> "  ."</td>"; //.format_currency($PArrayTotal['creditbase1'][$PBase1], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> " ."</td>"; // .format_currency($PArrayTotal['cashbase2'][$PBase1], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> "  ."</td>"; //.format_currency($PArrayTotal['bankbase2'][$PBase1], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> " ."</td>"; // .format_currency($PArrayTotal['creditbase2'][$PBase1], $PDecimalBase) ." </td>";

	}
echo "</tr> "; 	

	}

		else if ($PCurr == $PBase2) {

 echo "
<tr class='trstyle'>
<td align='left' colspan='6' valign= 'top'> $PTotalsLabel  $PBase2  </td>"

."<td align='right' valign= 'top'> " .format_currency($PArrayTotal['grosstotal'][$PBase2], $PDecimalBase) ." </td>"
."<td align='right' valign= 'top'> " .format_currency($PArrayTotal['discount'][$PBase2], $PDecimalBase) ." </td>"
."<td align='right' valign= 'top'> " .format_currency($PArrayTotal['Vat'][$PBase2], $PDecimalBase) ." </td>"
."<td align='right' valign= 'top'>" . format_currency($PArrayTotal['NetTotal'][$PBase2], $PDecimalBase) ."</td>"
."<td align='right' valign= 'top'>" . format_currency($PArrayTotal['totalcost'][$PBase2], $PDecimalBase) ."</td>";

 if ($prProfitMargin ==1)

	 {

 echo "<td align='right' valign= 'top'> " .format_currency($PArrayTotal['profitammount'][$PBase2], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> " .format_currency($PArrayTotal['profitmargin'][$PBase2], $PDecimalBase) ." </td>";

	 }

if ($showpaymentmode == 1)
{

 echo "<td align='right' valign= 'top'> " ."</td>"; // .format_currency($PArrayTotal['cashbase1'][$PBase2], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> " ."</td>"; // .format_currency($PArrayTotal['bankbase1'][$PBase2], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> "  ."</td>"; //.format_currency($PArrayTotal['creditbase1'][$PBase2], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> "  ."</td>"; //.format_currency($PArrayTotal['cashbase2'][$PBase2], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> " ."</td>"; // .format_currency($PArrayTotal['bankbase2'][$PBase2], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> " ."</td>"; // .format_currency($PArrayTotal['creditbase2'][$PBase2], $PDecimalBase) ." </td>";

	}
echo "</tr> "; 	
	}

		else {
		$count = count ($PArrayCurrency); 
		for ($i=0; $i<$count; $i++){

			$Currency = $PArrayCurrency[$i];

			if ($PArrayTotal['NetTotal'][$Currency] != 0) {

				 echo "
<tr class='trstyle'>
<td align='left' colspan='6' valign= 'top'> $PTotalsLabel  $Currency  </td>"

."<td align='right' valign= 'top'> " .format_currency($PArrayTotal['grosstotal'][$Currency], $PDecimalBase) ." </td>"
."<td align='right' valign= 'top'> " .format_currency($PArrayTotal['discount'][$Currency], $PDecimalBase) ." </td>"
."<td align='right' valign= 'top'> " .format_currency($PArrayTotal['Vat'][$Currency], $PDecimalBase) ." </td>"
."<td align='right' valign= 'top'>" . format_currency($PArrayTotal['NetTotal'][$Currency], $PDecimalBase) ."</td>"
."<td align='right' valign= 'top'>" . format_currency($PArrayTotal['totalcost'][$Currency], $PDecimalBase) ."</td>";

 if ($prProfitMargin ==1)

	 {

 echo "<td align='right' valign= 'top'> " .format_currency($PArrayTotal['profitammount'][$Currency], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> " .format_currency($PArrayTotal['profitmargin'][$Currency], $PDecimalBase) ." </td>";

	 }

if ($showpaymentmode == 1)
{

 echo "<td align='right' valign= 'top'> " ."</td>"; // .format_currency($PArrayTotal['cashbase1'][$Currency], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> " ."</td>"; // .format_currency($PArrayTotal['bankbase1'][$Currency], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> " ."</td>"; // .format_currency($PArrayTotal['creditbase1'][$Currency], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> " ."</td>"; // .format_currency($PArrayTotal['cashbase2'][$Currency], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> " ."</td>"; // .format_currency($PArrayTotal['bankbase2'][$Currency], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> "  ."</td>"; //.format_currency($PArrayTotal['creditbase2'][$Currency], $PDecimalBase) ." </td>";

	}
echo "</tr> "; 	

			}	
		}

	}

//	echo"</table>";

}

//
function DisplayTotalsPayment($PCurr, $PBase1, $PBase2, $PArrayCurrency, $Arraytotalpayment, $PDecimalBase, $PTotalsLabel, $PStyleClass) {

	{
		global $prProfitMargin; global $filtermargin;
		global $showpaymentmode;
 
 
  

if ($showpaymentmode == 1)
{
	
	//base 1
	 echo "
<tr class='trstyle'>
<td align='left' colspan='6' valign= 'top'> Total Payment  $PBase1  </td>"

."<td align='right' valign= 'top'> " ."" ." </td>"
."<td align='right' valign= 'top'> " ."" ." </td>"
."<td align='right' valign= 'top'> " ."" ." </td>"
."<td align='right' valign= 'top'>" ."" ."</td>"
."<td align='right' valign= 'top'>" ."" ."</td>";  

 if ($prProfitMargin ==1)

	 {

   echo "<td align='right' valign= 'top'> " ."" ." </td>";
 echo "<td align='right' valign= 'top'> " .""." </td>";  

	 }

 echo "<td align='right' valign= 'top'> "  .format_currency($Arraytotalpayment['cashbase1'], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> "  .format_currency($Arraytotalpayment['bankbase1'], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> "  .format_currency($Arraytotalpayment['creditbase1'], $PDecimalBase) ." </td>";

  echo "<td align='right' valign= 'top'> " ."</td>"; // .format_currency($PArrayTotal['cashbase2'][$PBase1], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> "  ."</td>"; //.format_currency($PArrayTotal['bankbase2'][$PBase1], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> " ."</td>"; // .format_currency($PArrayTotal['creditbase2'][$PBase1], $PDecimalBase) ." </td>"; 
 
 
 //base2 
 
  echo "
<tr class='trstyle'>
<td align='left' colspan='6' valign= 'top'> Total Payment  $PBase2  </td>"

."<td align='right' valign= 'top'> " ."" ." </td>"
."<td align='right' valign= 'top'> " ."" ." </td>"
."<td align='right' valign= 'top'> " ."" ." </td>"
."<td align='right' valign= 'top'>" ."" ."</td>"
."<td align='right' valign= 'top'>" ."" ."</td>";  

 if ($prProfitMargin ==1)

	 {

   echo "<td align='right' valign= 'top'> " ."" ." </td>";
 echo "<td align='right' valign= 'top'> " .""." </td>";  

	 }

	 
	   echo "<td align='right' valign= 'top'> " ."</td>"; // .format_currency($PArrayTotal['cashbase2'][$PBase1], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> "  ."</td>"; //.format_currency($PArrayTotal['bankbase2'][$PBase1], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> " ."</td>"; // .format_currency($PArrayTotal['creditbase2'][$PBase1], $PDecimalBase) ." </td>"; 
 
 
 echo "<td align='right' valign= 'top'> "  .format_currency($Arraytotalpayment['cashbase2'], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> "  .format_currency($Arraytotalpayment['bankbase2'], $PDecimalBase) ." </td>";
 echo "<td align='right' valign= 'top'> "  .format_currency($Arraytotalpayment['creditbase2'], $PDecimalBase) ." </td>";



	}
echo "</tr> "; 	
//	}

	}

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

?>