<?php
session_name('CAKEPHP'); session_start();
include('exportconfig.php');
$leadid = $_SESSION['leadid'];
$leadRes = mysqli_query($db, $leadid);
$data = '';
$data .= "<table>";
$data .= "<tr>";
$data .= "<td><center>Lead No</center></td>";
$data .= "<td><center>Lead Category</center></td>";
$data .= "<td><center>Lead Stage</center></td>";
$data .= "<td><center>Lead Status</center></td>";
$data .= "<td><center>Subject</center></td>";
$data .= "<td><center>First Name </center></td>";
$data .= "<td ><center>Last Name</center></td>";
$data .= "<td><center>Company Name</center></td>";
$data .= "<td><center>Department</center></td>";
$data .= "<td><center>Designation</center></td>";
$data .= "<td><center>Address</center></td>";
$data .= "<td><center>State</center></td>";
$data .= "<td><center>City</center></td>";
$data .= "<td><center>Email</center></td>";
$data .= "<td><center>Phone</center></td>";
$data .= "<td><center>Mobile No</center></td>";
$data .= "<td><center>Fax No</center></td>";
$data .= "<td><center>Group Name</center></td>";
$data .= "<td><center>Item Name</center></td>";
$data .= "<td><center>Priority</center></td>";
$data .= "</tr>";
$i = 0;
while ($leadrow = mysqli_fetch_array($leadRes)) {
    $data .= "<tr>";
    $data .= "<td><center>" . $leadrow['leadcode'] . "</center></td>";
    $data .= "<td><center>" . $leadrow['lead_Category'] . "</center></td>";
    $data .= "<td><center>" . $leadrow['lead_Stage'] . "</center></td>";
    $data .= "<td><center>" . $leadrow['lead_status'] . "</center></td>";
    $data .= "<td><center>" . $leadrow['lead_subject'] . "</center></td>";
    $data .= "<td><center>" . $leadrow['fname'] . "</center></td>";
    $data .= "<td><center>" . $leadrow['lname'] . $leadrow['lname'] . "</center></td>";
    $data .= "<td><center>" . $leadrow['company_name'] . "</center></td>";
    $DepartmentSql = "Select * from deptmaster where id='" . $leadrow['department'] . "'";
    $DepartmentRes = mysqli_query($db, $DepartmentSql);
    $DepartmentRow = mysqli_fetch_array($DepartmentRes);
    $data .= "<td><center>" . $DepartmentRow['deptname'] . "</center></td>";
    $DesignationtSql = "Select * from desigmaster where id='" . $leadrow['designation'] . "'";
    $DesignationRes = mysqli_query($db, $DesignationtSql);
    $DesignationRow = mysqli_fetch_array($DesignationRes);
    $data .= "<td><center>" . $DesignationRow['designame'] . "</center></td>";
    $data .= "<td><center>" . $leadrow['address1'] . $leadrow['address2'] . $leadrow['address3'] . "</center></td>";

    $StateSql = "Select * from state_master where id='" . $leadrow['state'] . "'";
    $StateRes = mysqli_query($db, $StateSql);
    $StateRow = mysqli_fetch_array($StateRes);
    $data .= "<td><center>" . $StateRow['state'] . "</center></td>";
    $CitySql = "Select * from city_master where id='" . $leadrow['city'] . "'";
    $CityRes = mysqli_query($db, $CitySql);
    $CityRow = mysqli_fetch_array($CityRes);
    $data .= "<td><center>" . $CityRow['city'] . "</center></td>";
    $data .= "<td><center>" . $leadrow['email'] . "</center></td>";
    $data .= "<td><center>" . $leadrow['phone'] . "</center></td>";
    $data .= "<td><center>" . $leadrow['mobile'] . "</center></td>";
    $data .= "<td><center>" . $leadrow['fax'] . "</center></td>";
    $GroupSql = "Select * from securityroles where secroleid='" . $leadrow['groupid'] . "'";
    $GroupRes = mysqli_query($db, $GroupSql);
    $GroupRow = mysqli_fetch_array($GroupRes);
    $data .= "<td><center>" . $GroupRow['secrolename'] . "</center></td>";
    $ItemSql = "Select * from crm_lead_items where mst_leadid='" . $leadrow['leadid'] . "'";
    $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 .= "</center></td>";
    $data .= "<td><center>" . $leadrow['lead_Priority'] . "</center></td>";
    $data .= "</tr>";
    $i++;
}
$data .= "</table>";
$header = '';
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=leadsExport.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
