<?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('_',$_SESSION["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($_SESSION['dbName']==""){
header("location:../index.php");
}

$success = include "config.inc.php"; if (!$success) {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);


if($exp ==1){
	$filename = "PurchasesOrder_" . date('Ymd') . ".xls";
	header("Content-Disposition: attachment; filename=\"$filename\"");
	header("Content-Type: application/vnd.ms-excel");
} 

print <<<HERE
<html>
<head>
<title>PURCHASE ORDER REPORT</title>
</head>
<body>
HERE;

$query = " Select Porder_ID, Porder_Reference, Date_Format(Porder_Date,'%d/%m/%Y') As PorderDate, ".
	         " Date_Format(Porder_RequestDate,'%d/%m/%Y') As RequestDate, Porder_RequestBy, Porder.Currency_code As CurrencyCode, ".
			 " Porder.Supplier_Code As SupplierCode, Suppliers.Ledger_name As SupplierName, porder_paymentcondition as paymentterms, ".
			 " Supplier_VAT, Porder_SupplierContact, Porder_Remark, Porder.Project_Code As ProjectCode, Project_Name, Porder.Costcent_code As Costcentcode, Costcent_name,  ".
			 " Date_Format(Expiry_date,'%d/%m/%Y') As ExpiryDate, if(ifNull(Porder_Import,0)=0, 'Local', 'Export') As PorderCategory, ".
			 " if(ifNull(Porder.Porder_Status,'A')='A','Approved',if(ifNull(Porder.Porder_Status,'A')='P','Pending',if(ifNull(Porder.Porder_Status,'A')='R','Rejected','Canceled'))) As PorderStatus, ".
			 " Status_Remark, if(porder_confirmed=1,'Yes','No') As PorderConfirmed, Porder_DeliveryPlace, Porder_DeliveryContact,  ".
			 " Round(ifNull(Porder_Taxable,0), ".$DecimalBase.") As PorderTaxable, Round(ifNull(Porder_NoneTaxable,0), ".$DecimalBase.") As PorderNoneTaxable, ".
			 " Round(ifNull(Porder_Total,0), ".$DecimalBase.") As GrossTotal, ".
			 " Round(ifNull(Porder_DiscountPer,0), ".$DecimalBase."), Round(ifNull(Porder_DiscountAmount,0), ".$DecimalBase.") As PorderDiscountAmount,  ".
			 " ifNull(Porder_VATChckbx,0) As Porder_VatChkbx, Round(ifNull(Porder_Vat,0), ".$DecimalBase.") As Porder_VAT, ".
			 " Round((IfNull(Porder_Total,0) + IfNull(Porder_Vat,0) - IfNull(Porder_DiscountAmount,0) + (ifNull(Porder_Tax1,0) + ifNull(Porder_Tax2,0) + ifNull(Porder_Tax3,0) + ifNull(Porder_Tax4,0) + ifNull(Porder_Tax5,0))), ".$DecimalBase.") As Porder_NetTotal,  ".
			 " Round((ifNull(Porder_Tax1,0) + ifNull(Porder_Tax2,0) + ifNull(Porder_Tax3,0) + ifNull(Porder_Tax4,0) + ifNull(Porder_Tax5,0)), ".$DecimalBase.") As Porder_TotalTaxes, suppliers.Supplier_Address As SupplierAddress, suppliers.Supplier_Telephone1, suppliers.Supplier_Telephone2, suppliers.Supplier_VatNo As SupplierVatNo ".
			 " ,approvedby,currency_name, user_account ".
			 " From Porder ".
			 " Left Join Suppliers on Porder.Supplier_code = Suppliers.Ledger_Number ".
			 " Left Join Project on Porder.Project_Code = Project.Project_Code ".
			 " Left Join CostCent on Porder.Costcent_code = CostCent.CostCent_Code ".
			 "left join currency on currency.currency_code=Porder.Currency_code".
	         " Where Porder_ID = ".$PorderId;
			 
			 
	$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;'>Purchase Order</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'>$PorderDate</td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>PO #:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 13px'>$Porder_Reference</td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>Prepared By :</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 13px'>$user_account</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>
</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>:$SupplierCode &nbsp; &nbsp; $SupplierName</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>;
<tr>
<td style='font-size: 13px; font-family: Arial' width=55>Payment Terms</td>
<td style='font-size: 13px; font-family: Arial' width=745>: $paymentterms</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'>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: 285px; 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: 100px; background: #d0d0d0 '>Unit Price</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0 '>Disc.</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 PorderDt.PorderDt_ID, PorderDt.Item_CodePacked, PorderDt.Item_code As ItemCode, Item_Description As ItemDescription, if(Items.Item_NonStock=1, PorderDt_Remark, '') As PorderDtRemark, ".
			   " PorderDt.Warehouse_Code  As WarehouseCode, PorderDt_Remark, PorderDt_Quantity, Round(PorderDt_Price, ".$DecimalBase.") As PorderDtPrice, Round(PorderDt_PriceTTC, ".$DecimalBase.")  As PorderDtPriceTTC, ".
			   " Round(PorderDt_Discount, ".$DecimalBase.") As PorderDtDiscountPer, ".
			   " Round(((PorderDt_Quantity*PorderDt_Price)-((PorderDt_Quantity*PorderDt_Price*ifNull(PorderDt_Discount,0))/100)), ".$DecimalBase.") As PorderDtTotal, ".
			   " Round(((PorderDt_Quantity*PorderDt_PriceTTC)-((PorderDt_Quantity*PorderDt_PriceTTC*ifNull(PorderDt_Discount,0))/100)), ".$DecimalBase.") As PorderDtTotalTTC ".
			   " From PorderDt Left Join Items on PorderDt.Item_code = Items.Item_code ".
			   " Where PorderDt.Porder_ID = ".$PorderId;
	$resultdt = mysql_query($querydt, $connection);
	$total_beforediscount=0;
	$total_discountamount=0;
	$total_discountamount_oninvoice=0;
	$totalqty=0;
	while($rowdt = mysql_fetch_array($resultdt)){
		extract($rowdt);
		$total_beforediscount = $total_beforediscount + ($PorderDt_Quantity*$PorderDtPrice);
		$total_discountamount = $total_discountamount + ($PorderDt_Quantity*$PorderDtPrice);
		$total_discountamount_oninvoice = $total_discountamount_oninvoice + ($PorderDt_Quantity*$PorderDtPrice);
		
		echo "<tr >";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; vertical-align: top; '>$ItemCode</th>";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 285px; 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; '>$PorderDt_Quantity</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;'>".number_format($PorderDtPrice, $DecimalBase)."</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; vertical-align: middle; text-align: right;'>$PorderDtDiscountPer %</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;'>".number_format($PorderDtTotal, $DecimalBase)."</th>";
	echo "</tr>";
	$totalqty=$totalqty+$PorderDt_Quantity;
	}
	//echo $total_discountamount_oninvoice;
	$total_afterdiscount=$total_beforediscount;
	
	echo "<tr>

	<td colspan=2 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'>Total Qty</td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; vertical-align: middle; border-top: none;border-bottom: none;text-align:center;font-weight: bold;background: #d0d0d0;'>$totalqty</td>
	
	<td colspan=2 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($total_beforediscount, $DecimalBase)."</td>
	</tr>
	<tr>
	<td colspan=2 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;'></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;border-bottom: none;background: #d0d0d0;'></td>
	
	<td colspan=2 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=2 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;'></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;border-bottom: none;background: #d0d0d0;'></td>
	
	<td colspan=2 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($Porder_VAT, $DecimalBase)."</td>
	</tr>
	<tr>
	<td colspan=2 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;'></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;background: #d0d0d0;'></td>
	
	<td colspan=2 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($total_afterdiscount, $DecimalBase)."</td>
	</tr>
	<tr style='height: 35px'>
	<th colspan=6></th>
	</tr>
	</tr>
	<tr>
	<td colspan=6 style='font-size: 13px; font-weight: none; font-family: Arial'>$setup_invoiceremarks</td>
	</tr>
	<tr style='height: 45px'>
	<th colspan=6></th>
	</tr>
	<tr>
	<th colspan=6 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;
?>