<?php
session_start();

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

include("config.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) or die("Could not connect to DB");
mysql_select_db($_SESSION['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>PURCHASE REPORT</title>
</head>
<body>
HERE;

$query = " Select Purchase_ID, Purchase_Reference, Date_Format(Purchase_Date,'%d/%m/%Y') As Purchase_Date, Purchase_Date As InvoiceDate, ".
			 " Date_Format(Journaldt_valuedate,'%d/%m/%Y') As MaturityDate, Purchases.Currency_code As CurrencyCode, ".
			 " Purchase_Remark, Purchase_InvoiceNo, Journal.Journal_RefGovernment, Purchases.Project_Code As ProjectCode, Purchases.Costcent_code As CostcentCode,  ".
			 " Purchases.Supplier_code As SupplierCode,	Suppliers.Ledger_name As SupplierName,	Suppliers.Ledger_nameF As SupplierNameF, Supplier_PaymentTerms,  ".
			 " Supplier_Representative As SupplierContact, Supplier_Address As SupplierAddress, Supplier_VAT, Supplier_VatNo As SupplierVatNo,  ".
			 " Round(Purchase_Taxable, ".$DecimalBase.") As PurchaseTaxable, Round(Purchase_NoneTaxable, ".$DecimalBase."), ".
			 " Round(Purchase_Total, ".$DecimalBase.") As GrossTotal, ".
			 " Round(Purchase_DiscountPer, ".$DecimalBase.") As PurchaseDiscountPer, Round(Purchase_DiscountAmount, ".$DecimalBase.") As PurchaseDiscountAmount,  ".
			 " Round(Purchase_VAT, ".$DecimalBase.") As Purchase_VAT, Round(Purchase_VATBase1, ".$DecimalBase.") As Purchase_VATBase1, ".
			 " Round((Purchase_Total + IfNull(Purchase_VAT,0) - IfNull(Purchase_DiscountAmount,0) + (ifNull(Purchase_Tax1,0) + ifNull(Purchase_Tax2,0) + ifNull(Purchase_Tax3,0) + ifNull(Purchase_Tax4,0) + ifNull(Purchase_Tax5,0))), ".$DecimalBase.") As Purchase_NetTotal,  ".
			 " Round((Purchase_TotalBase1 + IfNull(Purchase_VATBase1,0) - IfNull(Purchase_DiscountBase1,0) + (ifNull(Purchase_Tax1Base1,0) + ifNull(Purchase_Tax2Base1,0) + ifNull(Purchase_Tax3Base1,0) + ifNull(Purchase_Tax4Base1,0) + ifNull(Purchase_Tax5Base1,0))), ".$DecimalBase.") As Purchase_NetTotalBase1,  ".
			 " Round(Purchase_Charges, ".$DecimalBase.") As PurchaseCharges, Round(Purchase_ChargesBase1, ".$DecimalBase.") As PurchaseChargesBase1, Round(Purchase_ChargesBase2, ".$DecimalBase.") As PurchaseChargesBase2, ".
			 " Purchase_DebitAccount As PurchaseDebitAccount, if(Purchase_Mode=0, 'Credit', if(Purchase_Mode=1,'Cash', 'Check')) As PurchaseMode, ".
			 " Round((ifNull(Purchase_Tax1,0) + ifNull(Purchase_Tax2,0) + ifNull(Purchase_Tax3,0) + ifNull(Purchase_Tax4,0) + ifNull(Purchase_Tax5,0)), ".$DecimalBase.") As Purchase_TotalTaxes, suppliers.Supplier_Telephone1, suppliers.Supplier_Telephone2  ".
			 " From Purchases ".
			 " Left Join Suppliers on Purchases.Supplier_code = Suppliers.Ledger_Number ".
			 " Left Join Journal on Purchases.Journal_Id = Journal.Journal_Id ".
			 " Where Purchase_ID = ".$_GET['PurchaseId'];
			 
		 
	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
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;Goods Received Note</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: 14px'><i><b><u>$company_name</u></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'>$Purchase_Date</td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>Pur.#:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 13px'>$Purchase_Reference</td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'></td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 13px'></td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>Currency:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 13px'>$CurrencyCode</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 From:</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=745>:&nbsp; $SupplierCode&nbsp; $SupplierName</td>
</tr>

<tr>
<td style='font-size: 13px; font-family: Arial' width=75>Invoice No</td>
<td style='font-size: 13px; font-family: Arial' width=745>: $Purchase_InvoiceNo</td>
</tr>

<tr>
<td style='font-size: 13px; font-family: Arial' width=75>Remark </td>
<td style='font-size: 13px; font-family: Arial' width=745>: $Purchase_Remark</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>: $SupplierAddress</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>: $Supplier_Telephone1 $Supplier_Telephone2</td>
</tr>";
if($Supplier_VAT == 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>: $SupplierVatNo</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'>
</td>
</tr>";
echo "<tr style='height: 15px'>
<td colspan=2></td>
</tr>";
echo"</table>";
echo "<table width=800 border=1 style='border-collapse:collapse'>";
echo "<tr >";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 70px; background: #d0d0d0'>Code</th>";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 230px; 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 '>Unit</th>";
	echo "	 <th align='left'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 85px; background: #d0d0d0 '>Remark</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; background: #d0d0d0 '>Warehouse</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: 60px; background: #d0d0d0 '>Unit Price</th>";
	
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 85px; background: #d0d0d0 '>Total Price</th>";
	echo "</tr>";
	$querydt = " Select PurchasesDt.PurchaseDt_ID, PurchasesDt.Item_code as ItemCode, Item_Description As ItemDescription, Item_UnitSalesdesc as ItemUnit,Warehouse_Code as Warehouse, ".
			   " Item_DescriptionF1 As ItemDescriptionF1, Item_DescriptionF2 As ItemDescriptionF2, if(ifNull(PurchasesDt.Item_Vat,0) > 0, '*', ' ') as ItemIsVat,  ".
			   " PurchaseDt_Quantity as PurchaseDtQuantity, Round(PurchaseDt_Price, ".$DecimalBase.") As PurchaseDtPrice,Purchasedt_remark,items.item_nonstock, ".
			   " Round(PurchaseDt_DiscountPer, ".$DecimalBase.") As PurchaseDtDiscountPer, Round(PurchaseDt_PriceTTC, ".$DecimalBase.") As PurchaseDtPriceTTC,  ".
			   " Round(((PurchaseDt_Quantity*PurchaseDt_Price)-((PurchaseDt_Quantity*PurchaseDt_Price*ifNull(PurchaseDt_DiscountPer,0))/100)), ".$DecimalBase.") As PurchaseDtTotal, ".
			   " Round(((PurchaseDt_Quantity*PurchaseDt_PriceTTC)-((PurchaseDt_Quantity*PurchaseDt_PriceTTC*ifNull(PurchaseDt_DiscountPer,0))/100)), ".$DecimalBase.") As PurchaseDtTotalTTC, PurchaseDt_DiscountAmount ".
			   " From PurchasesDt Left Join Items on PurchasesDt.Item_code = Items.Item_code ".
			   " Where PurchasesDt.Purchase_ID = ".$_GET['PurchaseId'];
	$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 + ($PurchaseDtQuantity*$PurchaseDtPrice);
		$total_discountamount = $total_discountamount + ($PurchaseDtQuantity*$PurchaseDtPrice);
		$total_discountamount_oninvoice = $total_discountamount_oninvoice + ($PurchaseDtQuantity*$PurchaseDtPrice);
	
	
	echo "<tr >";

    if ($item_nonstock==0) {

	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 70px; vertical-align: top; '>$ItemCode</th>";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 230px; vertical-align: top;'>$ItemDescription</th>";
	
	} else {
		
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 70px; vertical-align: top; '></th>";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 230px; vertical-align: top;'>TRANSPORT</th>";
		
		
	}
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; vertical-align: top;'>$ItemUnit</th>";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 85px; vertical-align: top;'>$Purchasedt_remark</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: center;'>$Warehouse</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; vertical-align: middle; '>$PurchaseDtQuantity</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 60px; vertical-align: middle; text-align: right;'>".number_format($PurchaseDtPrice, $DecimalBase)."</th>";
	
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 85px; vertical-align: middle; text-align: right;'>".number_format($PurchaseDtTotal, $DecimalBase)."</th>";
	echo "</tr>";
	}
	
	//echo $total_discountamount_oninvoice;
	$total_afterdiscount=$total_beforediscount;
	echo "<tr>
	<td colspan=7 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'>".number_format($GrossTotal, $DecimalBase)."</td>
	</tr>
	<tr>
	<td colspan=7 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'>".number_format($PurchaseDiscountAmount, $DecimalBase)."</td>
	</tr>
	<tr>
	<td colspan=7 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'>".number_format($Purchase_VAT, $DecimalBase)."</td>
	</tr>
	<tr>
	<td colspan=7 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'>".number_format($Purchase_NetTotal, $DecimalBase)."</td>
	</tr>";
	
    if ($PurchaseCharges!=0) {
echo "	<tr>
	<td colspan=7 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'>Charges</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'>".number_format($PurchaseCharges, $DecimalBase)."</td>
	</tr
	<tr>
	<td colspan=7 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+ Charges</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'>".number_format($Purchase_NetTotal+$PurchaseCharges, $DecimalBase)."</td>
	</tr>";
	}
	echo "<tr style='height: 35px'>
	<th colspan=8></th>
	</tr>
	</tr>
	<tr>
	<td colspan=8 style='font-size: 13px; font-weight: none; font-family: Arial'>$setup_invoiceremarks</td>
	</tr>
	<tr style='height: 45px'>
	<th colspan=8></th>
	</tr>
	<tr>
	<th colspan=8 style='font-size: 14px; font-family: Arial'>THANK YOU FOR YOUR BUSINESS!</th>
	</tr>
	";
echo"</table>";
print <<<HERE
<script>
window.print();
</script>
</body>
</html>
HERE;
?>