<?php 
if(!empty($_GET['supplier'])){
    $supplier = $_GET['supplier'];
    $supchage = "AND mobileproducts.supplier='$supplier'";
}else{
    $supchage ="";
}   
if(!empty($_GET['start_date']) && !empty($_GET['end_date'])){
    $startdate = $_GET['start_date'];
    $enddate = $_GET['end_date'];
    $datechage = "AND mobileproducts.add_date BETWEEN '$startdate' AND '$enddate'";
}else{
    $datechage = "";
}





?><?php

include 'dbconfig.php';
require_once("conn.php");
require_once 'db_connection.php'; 

require("PHPExcel/PHPExcel.php");
$excel= new PHPExcel();

$excel->setActiveSheetIndex()
->setCellValue("A1","Product Code")
->setCellValue("B1","Item Name")
->setCellValue("C1","Category")
->setCellValue("D1","Sub Category")
->setCellValue("E1","Stock")
->setCellValue("F1","Supplier");

$excel->getActiveSheet()->getStyle("A1:F1")->getFont()->setBold( true );

$row_count=2;

$qury=mysqli_query($conn,"SELECT * FROM mobileproducts WHERE prid!='0' $supchage $datechage order by prcode ASC");
while($resalt=mysqli_fetch_array($qury)){
    $id1 = $resalt['category'];  

    $query1 = $DB_con->prepare('SELECT catename FROM mobilecategory WHERE ctid='.$id1);

    $query1->execute();

    $result1 = $query1->fetch();
    
    $id3 = $resalt['subcategory'];  

    $query3 = $DB_con->prepare('SELECT subname FROM mobilesubcategory WHERE sbid='.$id3);

    $query3->execute();

    $result3 = $query3->fetch();


    $productid = $resalt['prid'];


    $sql2 = "SELECT * FROM tbl_stock WHERE prid='$productid'";  
    $result2 = mysqli_query($conn,$sql2);
    $row2 = mysqli_fetch_array($result2);



    $id2 = $resalt['supplier'];  

												$query2 = $DB_con->prepare('SELECT spname,vtype FROM mobilesuppliers WHERE spid='.$id2);

												$query2->execute();

												$result2 = $query2->fetch();

												
   
$excel->setActiveSheetIndex()
->setCellValue("A".$row_count,$resalt['prcode'])
->setCellValue("B".$row_count,$resalt['productimgname'])
->setCellValue("C".$row_count,$result1['catename'])
->setCellValue("D".$row_count,$result3['subname'])
->setCellValue("E".$row_count,$row2['qty'])
->setCellValue("F".$row_count,$result2['spname']."".$result2['vtype']);
$row_count++;

}
	
header('content-Type:Application.openxmlformats-officedocument.spreadsheetml.sheet');
header('content-Disposition:attachment; filename="all_suppler_wise_product_report.xlsx"');
header('cache-Control: max-age=0');

$file=PHPExcel_IOFactory::CreateWriter($excel,'Excel2007');
$file->save('php://output');

?>