<?php
session_name('CAKEPHP'); session_start();
include('exportconfig.php');
$quotessql = $_SESSION['quotesID'];
$QuotesRes = mysqli_query($db, $quotessql);
class db_query
{
	function getField($table, $cond, $val, $field)
	{
		global $db;
		$sql = "select " . $field . " from " . $table . " where " . $cond . "='" . $val . "'";
		$res = mysqli_query($db, $sql);
		$row = mysqli_fetch_array($res);
		return ($row[$field]);
	}
}
$data = '';
$s = new db_query();
$data .= "<table>";
$data .= "<tr>";
$data .= "<td><center>Branch</center></td>";
$data .= "<td><center>Quote No</center></td>";
$data .= "<td><center>Date</center></td>";
$data .= "<td><center>Account Name</center></td>";
$data .= "<td><center>Quotation Type</center></td>";
$data .= "<td><center>Against Enquiry</center></td>";
$data .= "<td><center>Valid Till</center></td>";
$data .= "<td><center>Item Code</center></td>";
$data .= "<td><center>Grand total</center></td>";
$data .= "</tr>";
$i = 0;
while ($QuotesRow = mysqli_fetch_array($QuotesRes)) {
	$data .= "<tr>";
	$SiteSql = "select * from locations where loccode='" . $QuotesRow['site'] . "'";
	$SiteRes = mysqli_query($db, $SiteSql);
	$SiteRow = mysqli_fetch_array($SiteRes);
	$data .= "<td><center>" . $SiteRow['locationname'] . "</center></td>";
	$data .= "<td><center>" . $QuotesRow['quotationNo'] . "</center></td>";
	$data .= "<td><center>" . $QuotesRow['quotationDate'] . "</center></td>";
	$PartySql = "select * from accountledger where AccountsID='" . $QuotesRow['party'] . "'";
	$PartyRes = mysqli_query($db, $PartySql);
	$PartyRow = mysqli_fetch_array($PartyRes);
	$data .= "<td><center>" . $PartyRow['AccountName'] . "</center></td>";
	if ($QuotesRow['quotationType'] == 'M') {
		$data .= "<td><center>Material</center></td>";
	} elseif ($QuotesRow['quotationType'] == 'E') {
		$data .= "<td><center>Export</center></td>";
	} else {
		$data .= "<td><center>Retail</center></td>";
	}


	$data .= "<td><center>";
	$EnquirySql = "select * from crm_leads where leadid='" . $QuotesRow['enquiryNo'] . "'";
	$EnquiryRes = mysqli_query($db, $EnquirySql);
	$EnquiryRow = mysqli_fetch_array($EnquiryRes);
	$data .= $EnquiryRow['leadcode'];
	$data .= "</center></td>";
	$data .= "<td><center>" . $QuotesRow['validTill'] . "</center></td>";
	$ItemSql = "SELECT * FROM `crm_lead_items` where mst_leadid='" . $QuotesRow['enquiryNo'] . "'";
	$ItemRes = mysqli_query($db, $ItemSql);
	$data .= "<td><center>";
	while ($ItemRow = mysqli_fetch_array($ItemRes)) {
		$ItemSql1 = "select * from item_master where id='" . $ItemRow['item_code'] . "'";
		$ItemRes1 = mysqli_query($db, $ItemSql1);
		$ItemRow1 = mysqli_fetch_array($ItemRes1);
		$data .= $ItemRow1['StockID'];

		$data .= "  ";
	}
	$data .= "</center></td>";
	$data .= "<td><center>" . $QuotesRow['grandTotal'] . "</center></td>";


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

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