<?php
// session_start();



/////////////////////////////////////////////////////// 
//BLOCK USED TO REDIRECT TO COMPANY SPECIFIC REPORTS//
/////////////////////////////////////////////////////
 $curfolder = substr(getcwd(), strrpos(getcwd(), '\\') + 1);
 $curfolder = strtolower($curfolder);
 if ($curfolder == "contract")
 {
$pagename = basename($_SERVER['REQUEST_URI']);
$pagenamewithoutparams =  basename($_SERVER['PHP_SELF']);
$CompYear = explode('_',$dbName);
$companyname = strtoupper($CompYear[0]);
$clientrelatedpage = getcwd()  . "\\" .$companyname . "\\".$$pagenamewithoutparams  ;

 
if (file_exists($clientrelatedpage.$pagenamewithoutparams )) {
header("location:$companyname/$pagename");
} 

if (!file_exists($clientrelatedpage)) {
    mkdir($clientrelatedpage, 0777, true);
}
	
}
////////////////////////////////////////////////////////////////////////////////////////// 
///adjust include,css, javascript library etc... in cloned report ex ////////////////////
// $success = include "config.inc.php"; if (!$success) {include "../config.inc.php";}///
//js exemplage <script src = '../scriptname.js' >  </script>     //////////////////////
//////////////////////////////////////////////////////////////////////////////////////



if($dbName==""){
header("location:../index.php");
}

// include("config.inc.php"); 

$connection = mysql_connect($dbServer,$dbUser,$dbPass,true,65536) or die("Could not connect to DB");
mysql_select_db($dbName,$connection) or die("Could not find DB");

$query_setup  = "select company_name, company_memo, DecimalBase, Setup_VatPercentage, setup_invoiceremarks from setup";
$result_setup = mysql_query($query_setup, $connection); 
$row_setup    = mysql_fetch_array($result_setup);
extract($row_setup);
print <<<HERE
<html>
<head>
<title>INVOICE REPORT</title>

<style>
@media print {
    .break {page-break-after: always;}
}
</style>



</head>
<body>
HERE;


// from here 

$i = 0;

$u = 0;

	  
	$nxtsalesid = $_GET['SalesId'];


 

/* remove comments then continue the code */

/* end remove comments and continue the code */



while( ($i<$u)   ||   ($i==0 && $u==0))
	

{
		
$query = " Select Sale_ID, Sale_Reference, Date_Format(Sale_Date,'%d/%m/%Y') As Sale_Date, Sale_Date As InvoiceDate, ".
	         " Date_Format(JournalDt_ValueDate,'%d/%m/%Y') As MaturityDate, ifNull(Sales.Journal_ID,0) As JournalID, ".
			 " Sales.Client_code As ClientCode, Clients.Ledger_name As ClientName, Client_VAT, Client_Telephone1, Client_CreditLimit, Client_Address As ClientAddress,   ".
			 " Client_VATNo, Sales.Currency_code As CurrencyCode, Client_Address, Sale_ShippedTo, Sale_Marks, Sale_ShippedVia,Sale_ManualNo, ".
			 " Sale_Remark, Sales.Project_Code As ProjectCode, Project_Name, Sales.Costcenter_code As Costcentcode, Costcent_name,  ".
			 " Sale_Taxable As SaleTaxable, Sale_NoneTaxable As SaleNoneTaxable, ".
			 " Sale_Total As GrossTotal, ".
			 " Sale_DiscountPer As SaleDiscountPer, Sale_DiscountAmount As SaleDiscountAmount,  ".
			 " Sale_VAT As Sale_VAT, ".
			 " (ifNull(Sale_Total,0) + IfNull(Sale_VAT,0) - IfNull(Sale_DiscountAmount,0) + (ifNull(Sale_Tax1,0) + ifNull(Sale_Tax2,0) + ifNull(Sale_Tax3,0) + ifNull(Sale_Tax4,0) + ifNull(Sale_Tax5,0))) As Sale_NetTotal,  ".
			 " (ifNull(Sale_Tax1,0) + ifNull(Sale_Tax2,0) + ifNull(Sale_Tax3,0) + ifNull(Sale_Tax4,0) + ifNull(Sale_Tax5,0)) As Sale_TotalTaxes, Sale_DiscountTTC As SaleDiscountTTC, ".
			 " (ifNull(Sale_Total,0) + IfNull(Sale_VAT,0) - IfNull(Sale_DiscountTTC,0) + (ifNull(Sale_Tax1,0) + ifNull(Sale_Tax2,0) + ifNull(Sale_Tax3,0) + ifNull(Sale_Tax4,0) + ifNull(Sale_Tax5,0))) As Sale_NetTotalTTC  ".
			 " , Client_ShowCreditLimit, Client_ShowMaturityDate, clients.Client_Telephone2 ".
			 " From Sales ".
			 " Left Join Clients on Sales.Client_code = Clients.Ledger_Number ".
			 " Left Join Project on Sales.Project_Code = Project.Project_Code ".
			 " Left Join CostCent on Sales.Costcenter_code = CostCent.CostCent_Code ".
	         " Where Sale_ID = ".$nxtsalesid;
			 
	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
if($CurrencyCode=='NGN'){
	$sign='&#8358;';
}
echo "<table width=800>
<tr>
<td width=50%><img src='LogoFile.php' width=180 /></td>
<td width=50% style='text-align: center; font-family: Arial; font-size: 40px;  color: #a6a6a6;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Invoice</td>
</tr>
</table>
<table width=800>
<tr style='height: 20px'>
<td></td>
</tr>
</table>
<table width=800>
<tr>
<td width=500 style='font-family: Arial; font-size: 18px'><i><b>$company_name</b></i>
<br><b>
<pre>$company_memo</pre>
</td>
<td style='width: 300px; vertical-align: top;'>
<table width=100%>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>DATE:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 13px'>$Sale_Date</td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>TIN:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 13px'>21204511-0001</td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>Invoice #:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 13px'>$Sale_Reference</td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>Customer ID:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 13px'>$ClientCode</td>
</tr>
</table>
</td>
</tr>
<tr style='height: 15px'>
<td></td>
</tr>
</table>
<table width=800>
<tr>
<td style='font-weight: bold; font-size: 13px; font-family: Arial'>Bill To:</td>
</tr>
</table>
<table width=800>
<tr>
<td style='font-size: 13px; font-family: Arial' width=55>Name</td>
<td style='font-size: 13px; font-family: Arial' width=350>: $ClientName</td>
<td style='font-size: 13px; font-family: Arial' width=195>Delivery Note No</td>
<td style='font-size: 13px; font-family: Arial' width=250>: $Sale_ManualNo</td>
</tr>
<tr>
<td style='font-size: 13px; font-family: Arial' width=55>Address</td>
<td style='font-size: 13px; font-family: Arial' width=745>: $ClientAddress</td>
</tr>
<tr>
<td style='font-size: 13px; font-family: Arial' width=55>Phone</td>
<td style='font-size: 13px; font-family: Arial' width=745>: $Client_Telephone1 $Client_Telephone2</td>
</tr>";
if(0 == 1){
echo"<tr>
<td style='font-size: 13px; font-family: Arial' width=55>VAT #</td>
<td style='font-size: 13px; font-family: Arial' width=745>: $Client_VATNo</td>
</tr>";
}
echo "<tr style='height: 15px'>
<td colspan=2></td>
</tr>";
// echo "<tr>
// <td colspan=2 style='font-size: 13px; font-family: Arial; font-weight: bold'>Comments or special instructions:</td>
// </tr>";
echo "<tr style='height: 15px'>
<td colspan=2></td>
</tr>";
echo"</table>";
echo "<table width=800 style='border-collapse:collapse'>";
echo "<tr >";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0'>Code</th>";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 235px; background: #d0d0d0 '>Description</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; background: #d0d0d0 '>Qty</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; background: #d0d0d0 '>Item Unit</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 75px; background: #d0d0d0 '>Unit Price</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; background: #d0d0d0 '>Disc.</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; background: #d0d0d0 '>VAT</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; background: #d0d0d0 '>Total Price</th>";
	echo "</tr>";
	$querydt = " Select SalesDt.SaleDt_id, SalesDt.Item_CodePacked, SalesDt.Item_code, Item_Description As ItemDescription, if(Items.Item_NonStock=1, SalesDt_Remark, '') As SalesDtRemark, SalesDt.Warehouse_Code As WarehouseCode, ".
			   " SalesDt_Remark, SaleDt_Quantity, SaleDt_Price As SaleDtPrice, SaleDt_PriceBase1, SaleDt_PriceBase2,Items.Item_UnitSalesDesc as itemunit ,".
			   " SaleDt_DiscountPer As SaleDtDiscountPer,SalesDt.Item_VAT as Item_VAT,  ".
			   " ((SaleDt_Quantity*SaleDt_Price)-((SaleDt_Quantity*SaleDt_Price*ifNull(SaleDt_DiscountPer,0))/100)) As SaleDtTotal, ".
			   " SaleDt_PriceTTC As SaleDtPriceTTC, ".
			   " ((SaleDt_Quantity*SaleDt_PriceTTC)-((SaleDt_Quantity*SaleDt_PriceTTC*ifNull(SaleDt_DiscountPer,0))/100)) As SaleDtTotalTTC ".
			   " From SalesDt Left Join Items on SalesDt.Item_code = Items.Item_code ".
			   " Where SalesDt.Sale_ID = ".$SalesId.
			   //" Order By SalesDt.Item_CodePacked, SalesDt.Item_code  ";//stopped by maroun 04.01.2014
			   " Order By SalesDt.SaleDt_id  ";
	$resultdt = mysql_query($querydt, $connection);
	$total_beforediscount=0;
	$total_discountamount=0;
	$total_discountamount_oninvoice=0;
	while($rowdt = mysql_fetch_array($resultdt)){
		extract($rowdt);
		$total_beforediscount = $total_beforediscount + ($SaleDt_Quantity*$SaleDtPrice);
		$total_discountamount = $total_discountamount + ($SaleDt_Quantity*$SaleDtPrice);
		$total_discountamount_oninvoice = $total_discountamount_oninvoice + ($SaleDt_Quantity*$SaleDtPrice);
		
		echo "<tr >";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; vertical-align: top; '>$Item_code</th>";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 235px; vertical-align: top;'>$ItemDescription</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; vertical-align: middle; '>$SaleDt_Quantity</th>";
		echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; vertical-align: middle; '>$itemunit</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 75px; vertical-align: middle; text-align: right;'>$sign ".number_format($SaleDtPrice, $DecimalBase)."</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; vertical-align: middle; text-align: right;'>$SaleDtDiscountPer %</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; vertical-align: middle; text-align: right;'>".number_format($Item_VAT, $DecimalBase)." %</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;'>$sign ".number_format($SaleDtTotal, $DecimalBase)."</th>";
	echo "</tr>";
	}
	//echo $total_beforediscount;
	$total_afterdiscount=$total_beforediscount-$total_discountamount-$total_discountamount_oninvoice;

	
	echo "<tr>
	
	<td colspan=6 valign='top' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0; text-align: left; font-weight: bold; border-top: none;border-bottom: none;'>Amount in Words:</td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0; text-align: right; border-bottom: none; font-weight: bold'>Gross Total</td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; vertical-align: middle; text-align: right; border-bottom: none'>$sign ".number_format($GrossTotal, $DecimalBase)."</td>
	</tr>
	<tr>
	<td colspan=6 valign='top' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0; text-align: left; font-weight: bold; border-top: none;border-bottom: none;'></td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0; text-align: right; border-bottom: none; font-weight: bold; border-top: none'>Discount</td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; vertical-align: middle; text-align: right; border-bottom: none; border-top: none'>$sign ".number_format($SaleDiscountAmount, $DecimalBase)."</td>
	</tr>
	<tr>";
	if (empty($Sale_NetTotal)) $Sale_NetTotal = 0;
	$queryalpha  = " SELECT Alpha_Num ('ENGLISH', '".$CurrencyCode."', ".$Sale_NetTotal.") As AlphaAmount FROM Dual ";
	$resultalpha = mysql_query($queryalpha,$connection) or die(mysql_error());
	$rowalpha=mysql_fetch_array($resultalpha);
		
	echo "<td colspan=6 valign='top'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0; text-align: left; font-weight: bold; border-top: none;border-bottom: none;' >".$rowalpha[0]."</td>";
	echo"	
	
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0; text-align: right; border-bottom: none; font-weight: bold; border-top: none'>VAT $Setup_VatPercentage%</td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; vertical-align: middle; text-align: right; border-bottom: none; border-top: none'>$sign ".number_format($Sale_VAT, $DecimalBase)."</td>
	</tr>
	<tr>";
	
	echo"
	<td colspan=6 valign='top' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0; text-align: left; font-weight: bold; border-top: none'></td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0; text-align: right; font-weight: bold; border-top: none'>NET Total</td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;  border-top: none'>$sign ".number_format($Sale_NetTotal, $DecimalBase)."</td>
	</tr>
	<tr style='height: 35px'>
	<th colspan=7></th>
	</tr>
	</tr>
	<tr>
	<td colspan=7 style='font-size: 13px; font-weight: none; font-family: Arial'>$setup_invoiceremarks</td>
	</tr>
	<tr style='height: 45px'>
	<th colspan=7></th>
	</tr>
	<tr>
	<th colspan=7 style='font-size: 14px; font-family: Arial'> We Thank You For Your Purchase.</th>
	</tr>
	<tr>
	<th colspan=7 style='font-size: 14px; font-family: Arial'>&nbsp; </th>
	</tr>
	";

	
echo "<table border=1 width='800' cellspacing=0 cellpadding=1  class='totals' style='table-layout:fixed;' >
			  <col width=250>
			  <col width=250>
			  <col width=250>";
		
		echo "<tr> ";
		echo "   <td align='left' ><b>&nbsp;Bank Detail</td>";
		echo "   <td align='left' colspan=2 ><b>&nbsp;Acct Name: THE FATBUTCHER LTD.</td>";
		echo"</tr>";	  
			  
			  
		echo "<tr>";
		echo "   <td align='left' >&nbsp;ACCESS BANK</td>";
		echo "   <td align='left' >&nbsp;GT Bank </td>";
		echo "   <td align='left' >&nbsp;ZENITH BANK</td>";
		echo "</tr>";
		/*
		echo "<tr>";
	    echo "   <td align='left' >&nbsp;Acct Name: Oda Foods Distributions Ltd</td>";
		echo "   <td align='left' >&nbsp;Acct Name: Oda Foods Distributions Ltdc</td>";
		echo "   <td align='left' >&nbsp;Acct Name: Oda Foods Distributions Ltd</td>";
		echo "</tr>";*/
		
		echo "<tr>";
		echo "   <td align='left' >&nbsp;Acct Number: 1410503961</td>";
		echo "   <td align='left' >&nbsp;Acct Number: 0455835319</td>";
		echo "   <td align='left' >&nbsp;Acct Number: 1016114699</td>";
		echo "</tr>";

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

if ( !isset ( $_GET['SalesId'] ))
{
	echo "<div  class = 'break'></div>";
}
$i++;
 
  
 $nxtsalesid = $tags[$i];
 
 }

// to here
print <<<HERE
<script>
window.print();
</script>
</body>
</html>
HERE;
?>