<?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);
print <<<HERE
<html>
<head>
<title>PURCHASE REQUEST REPORT</title>
</head>
<body>
HERE;

$query = " Select PRqst_id, PRqst_Reference, Date_Format(PRqst_Date,'%d/%m/%Y') As PRqstDate, ".
	         " Date_Format(PRqst_RequestDate,'%d/%m/%Y') As RequestDate, PRqst_RequestBy, PRqst.Currency_code As CurrencyCode, ".
			 " null As SupplierCode, null As SupplierName, null, ".
			 " null, null, PRqst_Remark, PRqst.Project_Code As ProjectCode, Project_Name, PRqst.Costcent_code As Costcentcode, Costcent_name,  ".
			 " null, if(ifNull(PRqst_Import,0)=0, 'Local', 'Export') As PRqstCategory, ".
			 " if(ifNull(PRqst.PRqst_Status,'A')='A','Approved',if(ifNull(PRqst.PRqst_Status,'A')='P','Pending',if(ifNull(PRqst.PRqst_Status,'A')='R','Rejected','Canceled'))) As PRqstStatus, ".
			 " Status_Remark, if(PRqst_confirmed=1,'Yes','No') As PRqstConfirmed, PRqst_DeliveryPlace, PRqst_DeliveryContact,  ".
			 " Round(ifNull(PRqst_Taxable,0), ".$DecimalBase.") As PRqstTaxable, Round(ifNull(PRqst_NoneTaxable,0), ".$DecimalBase.") As PRqstNoneTaxable, ".
			 " Round(ifNull(PRqst_Total,0), ".$DecimalBase.") As GrossTotal, ".
			 " Round(ifNull(PRqst_DiscountPer,0), ".$DecimalBase."), Round(ifNull(PRqst_DiscountAmount,0), ".$DecimalBase.") As PRqstDiscountAmount,  ".
			 " ifNull(PRqst_VATChckbx,0) As PRqst_VatChkbx, Round(ifNull(PRqst_Vat,0), ".$DecimalBase.") As PRqst_VAT, ".
			 " Round((IfNull(PRqst_Total,0) + IfNull(PRqst_Vat,0) - IfNull(PRqst_DiscountAmount,0) + (ifNull(PRqst_Tax1,0) + ifNull(PRqst_Tax2,0) + ifNull(PRqst_Tax3,0) + ifNull(PRqst_Tax4,0) + ifNull(PRqst_Tax5,0))), ".$DecimalBase.") As PRqst_NetTotal,  ".
			 " Round((ifNull(PRqst_Tax1,0) + ifNull(PRqst_Tax2,0) + ifNull(PRqst_Tax3,0) + ifNull(PRqst_Tax4,0) + ifNull(PRqst_Tax5,0)), ".$DecimalBase.") As PRqst_TotalTaxes  ".
			 " From Prequest as PRqst ".
			 " Left Join Project on PRqst.Project_Code = Project.Project_Code ".
			 " Left Join CostCent on PRqst.Costcent_code = CostCent.CostCent_Code ".
	         " Where PRqst_ID = ".$PrequestId;
			 
			 
	$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 Request</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'>$PRqstDate</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'>$PRqst_Reference</td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>Supplier ID:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 13px'>$SupplierCode</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>: $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>";
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 PRqstDt.PRqstDt_ID, PRqstDt.Item_CodePacked, PRqstDt.Item_code As ItemCode, Items.Item_Description As ItemDescription, if(Items.Item_NonStock=1, PRqstDt_Remark, '') As PRqstDtRemark, ".
			   " PRqstDt.Warehouse_Code  As WarehouseCode, PRqstDt_Remark, PRqstDt_Quantity, Round(PRqstDt_Price, ".$DecimalBase.") As PRqstDtPrice, Round(PRqstDt_PriceTTC, ".$DecimalBase.")  As PRqstDtPriceTTC, ".
			   " Round(PRqstDt_Discount, ".$DecimalBase.") As PRqstDtDiscountPer, ".
			   " Round(((PRqstDt_Quantity*PRqstDt_Price)-((PRqstDt_Quantity*PRqstDt_Price*ifNull(PRqstDt_Discount,0))/100)), ".$DecimalBase.") As PRqstDtTotal, ".
			   " Round(((PRqstDt_Quantity*PRqstDt_PriceTTC)-((PRqstDt_Quantity*PRqstDt_PriceTTC*ifNull(PRqstDt_Discount,0))/100)), ".$DecimalBase.") As PRqstDtTotalTTC ".
			   " From prequestdt as PRqstDt Left Join Items on PRqstDt.Item_code = Items.Item_code ".
			   " Where PRqstDt.PRqst_id = ".$PrequestId;
	$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 + ($PRqstDt_Quantity*$PRqstDtPrice);
		$total_discountamount = $total_discountamount + ($PRqstDt_Quantity*$PRqstDtPrice);
		$total_discountamount_oninvoice = $total_discountamount_oninvoice + ($PRqstDt_Quantity*$PRqstDtPrice);
		
		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; '>$PRqstDt_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($PRqstDtPrice, $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;'>$PRqstDtDiscountPer %</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($PRqstDtTotal, $DecimalBase)."</th>";
	echo "</tr>";
	}
	//echo $total_discountamount_oninvoice;
	$total_afterdiscount=$total_beforediscount;
	echo "<tr>
	<td colspan=5 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=5 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=5 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=5 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;
?>