<?php

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

print <<< HERE
<HTML>
<HEAD>
<TITLE> Invoice Report </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

<style type="text/css">

table.master th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
table.master td { font-size: 14px; 
                  font-family:Times-New-Roman;
                 }
table.detail th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
				 
table.detail td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }				 
				 
table.detail td.sr  { font-size: 10px; 
					  font-style: italic; 
                      font-family:Times-New-Roman;
                    }
				 
table.totals td.labels { font-size: 14px; 
                         font-weight: bold;
                         font-family:Times-New-Roman;
						 border-width: 1px;
						 border-style: none solid solid solid;
  						 border-color: #666666;
						 padding-left:5px;
                       }					  
table.totals td.amounts { font-size: 14px; 
                          font-family:Times-New-Roman;
						  border-width: 1px;
						  border-style: none none solid none;
  						  border-color: #666666;
				        }
table.totals td.totalborder {
								font-size: 14px; 
								font-family:Times-New-Roman;
								border-width: 1px;
								border-style: none solid solid none;
								border-color: #666666;
							}	
table.totals td.labels1 { font-size: 14px; 
                          font-weight: bold;
                          font-family:Times-New-Roman;
					   	  border-width: 1px;
						  border-style: none none solid solid;
  						  border-color: #666666;
                        }							
				 
h4 { border-top-style: double; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }

h3 { border-top-style: double; 
	 border-top-color: black; 
	 border-top-width: 2px;
	 width: 180px;
   } 
   
</style>
</HEAD>
<BODY>
HERE;

include("config.inc.php");


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

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


//Read From Module Table
$querymodule=" Select ifNull(Module_ShowLayoutProj,0) As Module_ShowLayoutProj, ifNull(Module_ShowLayoutCost,0) As Module_ShowLayoutCost ".
			 " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

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



if($SalesOrderJVId != '')
{
			 
	$query = " Select SalesOrderJV_ID, SalesOrderJV_Reference, Date_Format(SalesOrderJV_Date,'%d/%m/%Y') As SalesOrderJV_Date, ".
	         " Date_Format(JournalDt_ValueDate,'%d/%m/%Y') As MaturityDate, ".
			 " SalesOrderJV.Client_Code As ClientCode, Clients.Ledger_Name As ClientName, Client_VAT, Client_Telephone1, Client_Address As ClientAddress,   ".
			 " Client_VATNo, SalesOrderJV.Currency_Code As CurrencyCode, ".
			 " Sales_Remark, SalesOrderJV.Project_Code As ProjectCode, Project_Name, SalesOrderJV.Costcenter_code As Costcentcode, Costcent_name,  ".
			 " Round(ifNull(SalesOrderJV_Taxable,0), ".$DecimalBase.") As SalesOrderJVTaxable, Round(ifNull(SalesOrderJV_NoneTaxable,0), ".$DecimalBase.") As SalesOrderJVNoneTaxable, ".
			 " Round(ifNull(SalesOrderJV_Total,0), ".$DecimalBase.") As GrossTotal, ".
			 " Round(ifNull(SalesOrderJV_DiscountPer,0), ".$DecimalBase.") As SalesOrderJVDiscountPer, Round(ifNull(SalesOrderJV_DiscountAmount,0), ".$DecimalBase.") As SalesOrderJVDiscountAmount,  ".
			 " Round(ifNull(SalesOrderJV_VAT,0), ".$DecimalBase.") As SalesOrderJV_VAT, ".
			 " Round((ifNull(SalesOrderJV_Total,0) + IfNull(SalesOrderJV_VAT,0) - IfNull(SalesOrderJV_DiscountAmount,0) ), ".$DecimalBase.") As SalesOrderJV_NetTotal , Sales_Remark ".
			 " From SalesOrderJV ".
			 " Left Join Clients on SalesOrderJV.Client_Code = Clients.Ledger_Number ".
			 " Left Join Project on SalesOrderJV.Project_Code = Project.Project_Code ".
			 " Left Join CostCent on SalesOrderJV.Costcenter_code = CostCent.CostCent_Code ".
	         " Where SalesOrderJV_ID = '".$SalesOrderJVId."'";
			 
	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
	
	$title = 'Invoice No. '.$SalesOrderJV_Reference;
		
	echo "<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td align='left'>";
	echo "<table border=0 width='635' cellspacing=2 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=135>
		  <col width=300>
		  <col width=95>
		  <col width=110>";
	
	echo "<tr>
			<td valign='top'><img src='LogoFile.php' width='115' height='50' /></td>
			<td align='left'><b><font size=4>$CompanyName</font></b>
			<br><span style='font-size:9px;'>".nl2br($CompanyMemo)."</span></td>
			<td colspan='2'>&nbsp;</td>
		</tr>";	  
	
	echo "<tr><td colspan='4'>&nbsp;</td></tr>";
	echo "<tr><td rowspan='2'>&nbsp;</td>
			  <td align='center' rowspan='2'><b><font size=5>$title </font></td>
			  <td align='right' colspan=2 ><font size='2'><b>Printed&nbsp;Date:&nbsp;&nbsp;</b>";
	echo $printdate[2]."/".$printdate[1]."/".$printdate[0];
	echo "</font></td></tr>";
	echo "<tr><td align='right' colspan=2 ><font size='2'><b>&nbsp;Time:&nbsp;&nbsp;</b>";
	echo $dt[1]." ".$dt[2];
	echo "</font></td></tr>";
	echo "</table>";
	
	echo "<table border=0 width='635' cellspacing=2 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=115>
		  <col width=320>
		  <col width=95>
		  <col width=110>";
	//echo "<tr height='10'><td colspan=4><hr></td></tr>";
	echo "<tr height='5'><td colspan=4><h4></td></tr>";
	
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Client</th>";
	echo "  <td align='left' >$ClientCode&nbsp;&nbsp;$ClientName</td>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Date</th>";
	echo "  <td align='left' >$SalesJV_Date</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "  <th align='left' valign='top' bgcolor='#D8D8D8'>Address</th>";
	echo "  <td align='left'>$ClientAddress</td>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Currency</th>";
	echo "  <td align='left' >$CurrencyCode</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "  <th align='left' valign='top' bgcolor='#D8D8D8'>Telephone 1</th>";
	echo "  <td align='left' colspan='3'>$Client_Telephone1</td>";
	echo "</tr>";
	
	echo "<tr>";
	
	if ($Module_ShowLayoutProj	== 1) {
		echo "  <th align='left' bgcolor='#D8D8D8'>Project</th>";
		echo "  <td align='left' >$ProjectCode &nbsp;&nbsp;$Project_Name</td>";
	} else {
		echo "  <td align='left' colspan='2'>&nbsp;</td>";
	}	

	echo "  <th align='left' valign='top' bgcolor='#D8D8D8'>Maturity</th>";
	echo "  <td align='left' >$MaturityDate</td>";
	echo "</tr>";
	
	echo "<tr>";
	
	if ($Module_ShowLayoutCost == 1) {	
		echo "  <th align='left' bgcolor='#D8D8D8'>CostCenter</th>";
		echo "  <td align='left'>$Costcentcode&nbsp;&nbsp;$Costcent_name</td>";
	} else {
		echo "  <td align='left' colspan='2'>&nbsp;</td>";
	}	

	echo "  <th align='left' valign='top' bgcolor='#D8D8D8'>VAT No.</th>";
	echo "  <td align='left' >$SetupVatNo</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Remark</th>";
	echo "  <td align='left' colspan='3'>$Sale_Remark</td>";
	echo "</tr>";
	
		
	echo "<tr height='5'><td colspan=4><h4></td></tr>";
	echo "</table></td></tr>";

		   
	$querydt = " Select SalesOrderJVDt.SalesOrderJVDt_Id, SalesOrderJVDt.Ledger_Number, SalesOrderJVDt_Description, SalesOrderJvDt_Description2, ".
			   " ifNull(SalesOrderJVDt_Quantity,0) As SalesOrderJVDt_Quantity, Round(ifNull(SalesOrderJVDt_Price,0), ".$DecimalBase.") As SalesOrderJVDtPrice, SalesOrderJVDt_PriceBase1, SalesOrderJVDt_PriceBase2, ".
			   " Round(ifNull(SalesOrderJVDt_DiscountPer,0), ".$DecimalBase.") As SalesOrderJVDtDiscountPer,  ".
			   " Round((ifNull(SalesOrderJVDt_Quantity,0)*ifNull(SalesOrderJVDt_Price,0)*(1 - ifNull(SalesOrderJVDt_DiscountPer,0)/100)), ".$DecimalBase.") As SalesOrderJVDtTotal ".
			   " From SalesOrderJVDt ".
			   " Where SalesOrderJVDt.SalesOrderJV_Id = '".$SalesOrderJVId."' ".
			   " Order By SalesOrderJVDt.Ledger_Number  ";
					   
	//echo $querydt;
	echo "<tr><td align='left'>";
	echo "<table border=0 width='635' cellspacing=2 class='detail' style='table-layout:fixed;' >
		  <col width=295>
		  <col width=70>
		  <col width=100>
		  <col width=50>
		  <col width=120>";
	echo "<tr bgcolor=#D8D8D8>";
	echo "   <th align='left'>Description</th>";
	echo "	 <th align='center'>Qty.</th>";
	echo "	 <th align='center'>Unit Price</th>";
	echo "	 <th align='center'>Disc.</th>";
	echo "	 <th align='center'>Total Price</th>";
	echo "</tr>";			
		  
	echo "<tr height='5'><td colspan=5><h4></td></tr>";
	
	$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultdt)){
		extract($rows);
	
		echo "<tr>";
		echo "  <td align='left' valign='top'>$SalesOrderJVDt_Description</td>";
		echo "	<td align='center' valign='top'>$SalesOrderJVDt_Quantity</td>";
		echo "	<td align='right' valign='top'>".number_format($SalesOrderJVDtPrice, $DecimalBase)."</td>";
		echo "	<td align='right' valign='top'>".number_format($SalesOrderJVDtDiscountPer, $DecimalBase)."</td>";
		echo "	<td align='right' valign='top'>".number_format($SalesOrderJVDtTotal, $DecimalBase)."</td>";
		echo "</tr>";
		
		
		if ($SalesOrderJvDt_Description2 != '') {
			echo "<tr>";
			echo "  <td align='left' valign='top' colspan='5'>$SalesOrderJvDt_Description2</td>";
			echo "</tr>";
		}
		
	}
	
	echo "<tr height='5'><td colspan=5><h4></td></tr>";
	echo "</table></td></tr>";
	
	
		
	if (empty($SalesOrderJV_NetTotal)) $SalesOrderJV_NetTotal = 0;
	$queryalpha  = " SELECT Alpha_Num ('ENGLISH', '".$CurrencyCode."', ".$SalesOrderJV_NetTotal.") As AlphaAmount FROM Dual ";
	$resultalpha = mysql_query($queryalpha,$connection) or die(mysql_error());
	$rowalpha=mysql_fetch_array($resultalpha);
		
	echo "<tr height='30'><td colspan=7 valign='top' style='font-size:12px; font-family:Times-New-Roman;'>".$rowalpha[0]."</td></tr>";
		
	echo "<tr><td align='left'>";
	echo "<table border=0 width='635' cellspacing=0 cellpadding=1  class='totals' style='table-layout:fixed;' >
		  <col width=240>
		  <col width=140>
		  <col width=120>
		  <col width=100>
		  <col width=20>";
	
		
	echo "<tr>";
	echo "   <td align='left'>&nbsp;</td>";
	echo "   <td align='left'>&nbsp;</td>";
	echo "	 <td align='left' class='labels'>Gross Total</td>";
	echo "	 <td align='right' class='amounts'>".number_format($GrossTotal, $DecimalBase)."</td>";
	echo "	 <td align='center' class='totalborder'>&nbsp;</td>";
	echo "</tr>";
		
	echo "<tr>";
	echo "   <td align='left'>&nbsp;</td>";
	echo "   <td align='left'>&nbsp;</td>";
	echo "	 <td align='left' class='labels'>Discount</td>";
	echo "	 <td align='right' class='amounts'>".number_format($SalesOrderJVDiscountAmount, $DecimalBase)."</td>";
	echo "	 <td align='center' class='totalborder'>&nbsp;</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "   <td align='left'>&nbsp;</td>";
	echo "   <td align='left'>&nbsp;</td>";
	echo "	 <td align='left' class='labels'>VAT&nbsp;$Setup_VatPercentage&nbsp;(%)</td>";
	echo "	 <td align='right' class='amounts'>".number_format($SalesOrderJV_VAT, $DecimalBase)."</td>";
	echo "	 <td align='center' class='totalborder'>&nbsp;</td>";
	echo "</tr>";
		
		
	echo "<tr>";
	echo "   <td align='left'>&nbsp;</td>";
	echo "   <td align='left'>&nbsp;</td>";
	echo "	 <td align='left' class='labels'>Net Total&nbsp;$CurrencyCode</td>";
	echo "	 <td align='right' class='amounts'>".number_format($SalesOrderJV_NetTotal, $DecimalBase)."</td>";
	echo "	 <td align='center' class='totalborder'>&nbsp;</td>";
	echo "</tr>";
		
		
		
	echo "</table></td></tr>";
	
	echo "</table>";
	echo "<br>";
	

}


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 format_currency($amount, $DecimalBase) { 

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

print <<< HERE

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

