<?php ob_start();
session_name('CAKEPHP'); session_start();
include('exportconfig.php');
class db_query
{
	function getField($table, $condition, $val, $field)
	{

		global $db;
		$sql = mysqli_query($db, "select " . $field . " from " . $table . " where " . $condition . " ='" . $val . "'");
		$r = mysqli_fetch_array($sql);
		//echo "select ".$field." from ".$table." where ".$condition." ='".$val."'"; 
		return ($r[$field]);
	}
}
$s = new db_query();
$data = '';
$data .= "<h2 align='center'><strong>Purchase Order Register</strong></h2>";

$data .= "<table width='40%' border='0' cellspacing='2' cellpadding='2'>
  <tr>
    <th><strong>Branch</strong></th>
    <td>";

$sql = "select * from locations where loccode='" . $_REQUEST['site'] . "'";
$sitesql = mysqli_query($db, $sql);
$siteres = mysqli_fetch_array($sitesql);
$data .= $siteres['locationname'];
$data .= "</td>
  </tr>
  <tr>
    <th><strong>Vendor</strong></th>
    <td>";

$v_sql = mysqli_query($db, "Select vendor_code,vendor_name from suppliers where vendor_code='" . $_REQUEST['vendor_code'] . "'");
$v_row = mysqli_fetch_array($v_sql);

$data .= $v_row['vendor_name'];

$data .= "</td>
  </tr>
  <tr>
    <th><strong>From</strong></th>
    <td><strong>";
if ($_REQUEST['fromdate'] != '') {

	$data .= $_REQUEST['fromdate'];
} else {
	$data .= "Not Selected";
}


$data .= "
      To</strong> ";
if ($_REQUEST['todate'] != '') {
	$data .= $_REQUEST['todate'];
} else {
	$data .= "Not Selected";
}

$data .= " </td>
	  
  </tr>
  <tr>
    <th><strong>Item Code </strong></th>
    <td>";

if ($_REQUEST['itemCode'] != '0') {

	$item_sql = mysqli_query($db, "Select StockID from item_master where  StockID='" . $_REQUEST['itemCode'] . "'");
	$item_row = mysqli_fetch_array($item_sql);
	$data .= $item_row['StockID'];
} else {
	$data .= "Not Selected";
}

$data .= "</td>
  </tr>
  <tr>
    <th><strong>Item Name </strong></th>
    <td>";
if ($_REQUEST['itemCode'] != '0') {

	$name_sql = mysqli_query($db, "Select  item_name from item_master where  StockID='" . $_REQUEST['itemCode'] . "'");
	$name_row = mysqli_fetch_array($name_sql);
	$data .= $name_row['item_name'];
} else {
	$data .= "Not Selected";
}

$data .= "</td>
  </tr>
</table>";

$data .= "<table width='100%' border='0' cellspacing='2' cellpadding='2'>";
$data .= "<tr>";
$data .= "<th colspan='10'>Purchase Order Report</th>";
$data .= "</tr>";
$data .= "<tr>";
$data .= "<th >SR No.</th>";
$data .= "<th >Branch</th>";
$data .= "<th >Date</th>";
$data .= "<th >Vendor</th>";
$data .= "<th >P. O. No. </th>";
$data .= "<th>V. Quote No.</th>";
$data .= "<th>Item Desc</th>";


$data .= "<th >Order Qty</th>";
$data .= "<th>Delivery Schedule</th>";

$data .= "</tr>";
$data5 = $_SESSION['data1'];
$result = mysqli_query($db, $data5);

$i = 1;
$total = 0;
while ($myrow = mysqli_fetch_array($result)) {

	$data .= "<tr bgcolor='#FFFFFF'>";
	$data .= "<td>" . $i . "</td>";
	$data .= "<td>" . $sitename = $s->getField('locations', 'loccode', $myrow['sitecode'], 'locationname') . "</td>";
	$data .= "<td>" . $myrow['orddate'] . "</td>";

	$vendor_name = $s->getField('suppliers', 'id', $myrow['supplierno'], 'vendor_name');
	$data .= "<td>" . $vendor_name . "</td>";
	$data .= "<td>" . $s->getField('purchorders', 'mst_vendor_quotesid', $myrow['id'], 'PO_Orderno') . "</td>";
	$vendor_quot = $s->getField('venderquotations', 'id', $myrow['mst_vendor_quotesid'], 'vernder_quotation');

	$data .= "<td>" . $vendor_quot . "</td>";
	$proddes = mysqli_query($db, "select * from  purchorderdetails where orderno='" . $myrow['orderno'] . "'");
	$dbdata = mysqli_fetch_array($proddes);
	$data .= "<td>" . $dbdata['prod_dec'] . "</td>";

	$data .= "<td>";
	if ($_REQUEST['site'] != '0') {
		$data .= $myrow['sum(quatity)'];
	} else {
		$data .= $myrow['quatity'];
	}
	$data .= "</td>";
	$data .= "<td>" . $sitename2 = $s->getField('venderquotations', 'id', $myrow['mst_vendor_quotesid'], 'delivery_date') . "</td>";

	$data .= "</tr>";
	$i++;
}

$header = '';
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=data1.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
