<?php

session_name('CAKEPHP'); session_start();
ob_start();
error_reporting(FALSE);
$PageSecurity = 1;
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 = '';
$i = 1;
$sql = "Select * from salesorder where branch='" . $_REQUEST['loccode'] . "'";
$result = mysqli_query($db, $sql);


$datarow = mysqli_fetch_array($result);

$add_query = "select * from companies where 1=1";
$result = mysqli_query($db, $add_query);
$num_rows = mysqli_num_rows($result);

if ($num_rows > 0) {
	$row = mysqli_fetch_array($result);
	$org_name = $row["CompanyName"];
}

$data .= "<table width='100%' border='0'>
		  <tr>
		  	<td> Approve Sale Order List from " . $_REQUEST['fromdate'] . " to " . $_REQUEST['todate'] . "<br></td>
		  </tr>
		  <tr>
			<td>SOP No: ";
$data .= $datarow['orderNo'] . "</td>
			<td>" . $org_name . "</td>
			<td >Delivery Date:" . $datarow['DeliveryDate'] . "</td>
		  </tr></table><br>
		  ";

$data .= "<table width='100%' border='0'>
  <tr>
    <td>SNo.</td>
    <td >Code</td>
    <td >Item/Shade Name </td>
    <td>Item Desc</td>
    <td >Qty</td>
    <td >Remark</td>
  </tr>
  ";


$sql1 = mysqli_query($db, "select * from salesorder_details where masterId='" . $datarow['id'] . "'");

while ($sql1Row = mysqli_fetch_array($sql1)) {
	//echo $purchrow['ItemCode'];
	$data .= "<tr> <td>" . $i++ . "</td>";
	$StockID = $s->getField('item_master', 'id', $sql1Row['ItemCode'], 'StockID');
	//$StockID= GetFieldValue('item_master',"id='".$sql1Row['ItemCode']."'",'StockID');
	$data .= "<td>" . $StockID . "</td>";
	$item_name = $s->getField('item_master', 'id', $sql1Row['ItemCode'], 'item_name');
	//$item_name = GetFieldValue('item_master',"id='".$sql1Row['ItemCode']."'",'item_name');
	$data .= "<td>" . $item_name . "</td>";
	$item_desc = $s->getField('item_master', 'id', $sql1Row['ItemCode'], 'item_desc');
	//$item_desc = GetFieldValue('item_master',"id='".$sql1Row['ItemCode']."'",'item_desc');	//echo $row['ItemCode'];
	$data .= "<td>" . $item_desc . "</td>";
	$data .= "<td>" . $sql1Row['Quantity'] . "</td>";
	$data .= "<td>" . $sql1Row['SalesCategory'] . "</td></tr>";

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

$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";
