<?php
ob_start();
session_name('CAKEPHP'); session_start();
include('exportconfig.php');
class db_query
{
  function getField($field, $table, $condition, $val)
  {
    global $db;
    $sql = "select " . $field . " from " . $table . " where " . $condition . "='" . $val . "'";
    $sqlres = mysqli_query($db, $sql);
    $sqlrow = mysqli_fetch_array($sqlres);
    return ($sqlrow[$field]);
  }
}

$data = '';
$s = new db_query();
$data .= "<table width='100%'>";
$data .= "<tr>";
$data .= "<th>Bill No</th>";
$data .= "<th>Bill Date</th>";
$data .= "<th>Name & Address of the Party</th>";
$data .= "<th>Item Supplied</th>";
$data .= "<th>Value</th>";
$data .= "</tr>";
$data1 = $_SESSION['data1'];
$datares = mysqli_query($db, $data1);
$total_amt = array();
$i = 0;
while ($datarow = mysqli_fetch_array($datares)) {
  $data .= "<tr>";
  $data .= "<td>";
  $data .= $datarow['invoiceNo'];
  $data .= "</td>";
  $data .= "<td>";
  $data .= $datarow['invoiceDate'];
  $data .= "</td>";
  $data .= "<td>";
  $vendor = $s->getField('c_name', 'clientmaster', 'id', $datarow['client_id']);
  $vendoradd = $s->getField('pre_address1', 'clientmaster', 'id', $datarow['client_id']);
  $data .= $vendor;
  $data .= $vendoradd;
  $data .= "</td>";
  $data .= "<td>";
  $itemname = $s->getField('item_name', 'item_master', 'StockID', $datarow['ItemCode']);
  $data .= $itemname;
  $data .= "</td>";
  $data .= "<td>";
  $data .= $datarow['grandTotal'];
  $data .= "</td>";
  $total_amt[] = $datarow['grandTotal'];
  $i++;
}

$data .= "<tr><td></td><td></td><td></td><td align='center'>Grand Total</td>";
//$data.=$total_amt;
$data .= "<td>" . array_sum($total_amt) . "</td></tr>";

$data .= "</tr>";


$data .= "</table>";

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