<?php 
require_once("../includes/config.php");
$arrstatus = getFullStatus("master",$link1);
////// filters value/////
//// extract all encoded variables
$ustatus=base64_decode($_REQUEST['status']);
$prodcat=base64_decode($_REQUEST['prod_cat']);
$prodsubcat=base64_decode($_REQUEST['product_sub_category']);
$brand=base64_decode($_REQUEST['brandname']);

## selected  Status
if(!empty($ustatus)){
	$status="status='".$ustatus."'";
}else{
	$status="1";
}
## selected product category
if(!empty($prodcat)){
	$productCategory="productid='".$prodcat."'";
}else{
	$productCategory="1";
}
## selected product sub category
if(!empty($prodsubcat)){
	$subCategory="psubcatid='".$prodsubcat."'";
}else{
	$subCategory="1";
}
## selected brand
if(!empty($brand)){
	$brand_name="brandid='".$brand."'";
}else{
	$brand_name="1";
}
//////End filters value/////
/** Include PHPExcel */
require_once("../ExcelExportAPI/Classes/PHPExcel.php");

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("Candour Software")
->setLastModifiedBy("Candour Software")
->setTitle("Office 2007 XLSX Partcode")
->setSubject("Office 2007 XLSX Partcode")
->setDescription("Partcode for Office 2007 XLSX.")
->setKeywords("office 2007 openxml php")
->setCategory("Partcode");

// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'S.No.')
->setCellValue('B1', 'Product Category')
->setCellValue('C1', 'Product Sub Category')
->setCellValue('D1', 'Brand')
->setCellValue('E1', 'Product Type')
->setCellValue('F1', 'Partcode')
->setCellValue('G1', 'Vendor Partcode')
->setCellValue('H1', 'Vendor Part Name')
->setCellValue('I1', 'HSN Code')
->setCellValue('J1', 'Part Name')
->setCellValue('K1', 'Model Name')
->setCellValue('L1', 'BOM Unit')
->setCellValue('M1', 'Purchase Unit')
->setCellValue('N1', 'Goods Type')
->setCellValue('O1', 'Serialized')
->setCellValue('P1', 'Status')
->setCellValue('Q1', 'Create By')
->setCellValue('R1', 'Create Date')
->setCellValue('S1', 'Update By')
->setCellValue('T1', 'Update Date')
->setCellValue('U1', 'Dimension');
///// Add Color in header
cellColor('A1:U1', '90EE90');

$i=2;
$count=1;
/////// run query to fetch data
$sql=mysqli_query($link1,"SELECT * FROM partcode_master where ".$status." and ".$productCategory." and ".$subCategory." and ".$brand_name."")or die("er1".mysqli_error($link1));
while($row_loc = mysqli_fetch_array($sql)){
	$createby = getAnyDetails($row_loc['createby'],"name","username","admin_users",$link1);
	$updateby = getAnyDetails($row_loc['updateby'],"name","username","admin_users",$link1);
	$prod_det = explode("~",getAnyDetails($row_loc["psubcatid"],"prod_sub_cat,product_category","psubcatid","product_sub_category",$link1));
	$objPHPExcel->setActiveSheetIndex(0)
				->setCellValue('A'.$i, $count)
				->setCellValue('B'.$i, $prod_det[1])
				->setCellValue('C'.$i, $prod_det[0])
				->setCellValue('D'.$i, getAnyDetails($row_loc["brandid"],"brand","brandid","brand_master",$link1))
				->setCellValue('E'.$i, getAnyDetails($row_loc["itemtypeid"],"parttype","typeid","part_type_master",$link1))
				->setCellValue('F'.$i, $row_loc['partcode'])
				->setCellValue('G'.$i, $row_loc['vendor_code'])
				->setCellValue('H'.$i, $row_loc['vendor_part_name'])
				->setCellValue('I'.$i, $row_loc['hsn_code'])
				->setCellValue('J'.$i, $row_loc['part_name'])
				->setCellValue('K'.$i, $row_loc['material'])
				->setCellValue('L'.$i, $row_loc['bom_unit'])
				->setCellValue('M'.$i, $row_loc['purchase_unit'])
				->setCellValue('N'.$i, $row_loc['goods_type'])
				->setCellValue('O'.$i, $row_loc['serialized'])
				->setCellValue('P'.$i, $arrstatus[$row_loc['status']])
				->setCellValue('Q'.$i, $createby)
				->setCellValue('R'.$i, $row_loc['createdate'])
				->setCellValue('S'.$i, $updateby)
				->setCellValue('T'.$i, $row_loc['updatedate'])
				->setCellValue('U'.$i, $row_loc['pro_dimension']);
				$i++;	
				$count++;		
}
///// apply border on export sheet
$styleArray = array(
  'borders' => array(
    'allborders' => array(
      'style' => PHPExcel_Style_Border::BORDER_MEDIUM
    )
  )
);

$objPHPExcel->getActiveSheet()->getStyle('A1:U'.($i-1))->applyFromArray($styleArray);
unset($styleArray);

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('partcodemaster');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a clients web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="partcodelist.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;

?>
