<?php ob_start();
session_name('CAKEPHP'); session_start();
$con = mysqli_connect("127.0.0.1", "root", "", "herp_amch");
$db  = $con;
// Check connection
if (!$con) {
	die("Connection failed: " . mysqli_connect_error());
}
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 .= "<table width='98%' border='0' cellspacing='1' cellpadding='2' align='center'>";
$data .= "<tr>";
$data .= "<th colspan='9'>Sales Order Report</th>";
$data .= "</tr>";
$data .= "<tr>";
$data .= "<th width='7%'>S. No.</th>";
$data .= "<th width='13%'>Branch</th>";
$data .= "<th width='7%'>Date</th>";
$data .= "<th width='12%'>Order No.</th>";
$data .= "<th width='12%'>Party P.O.No.</th>";
$data .= "<th width='17%'>Party Name</th>";
$data .= "<th width='8%'>Qty</th>";
$data .= "<th width='13%'>Pending Qty</th>";
$data .= "<th width='12%'>Status</th";
$data .= "</tr>";
$data5 = $_SESSION['data2'];
$result = mysqli_query($con, $data5);
$i = 1;
$total = 0;
while ($myrow = mysqli_fetch_array($result)) {
	$total = $total + $myrow['Quantity'];
	$data .= "<tr bgcolor='#FFFFFF'>";
	$data .= "<td>" . $i . "</td>";
	$data .= "<td>" . $sitename = $s->getField('locations', 'loccode', $myrow['branch'], 'locationname') . "</td>";
	$data .= "<td>" . $myrow['orderDate'] . "</td>";
	$data .= "<td>" . $myrow['orderNo'] . "</td>";
	$data .= "<td>" . $myrow['PORefNo'] . "</td>";
	$data .= "<td>" . $sitename1 = $s->getField('clientmaster', 'client_code', $myrow['client'], 'c_name') . "</td>";
	$data .= "<td>" . $myrow['Quantity'] . "</td>";
	$data .= "<td>" . $myrow['pendingQty'] . "</td>";
	$data .= "<td>" . $myrow['status'] . "</td>";
	$data .= "</tr>";

	$i++;
}
$data .= "<tr><th>&nbsp;</th><th>&nbsp;</th><th>&nbsp;</th><th>&nbsp;</th><th>&nbsp;</th><th>Total Qty</th><th>" . $total . "</th><th>&nbsp;</th><th>&nbsp;</th></tr>";
$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";
