<?php
session_start();
if(!isset($_SESSION['user'])) {
	header("location: ../index.php");
	exit;
}

include '../dbconnect.php';
require("../plugins/Classes/PHPExcel.php");
include '../plugins/Classes/PHPExcel/IOFactory.php';

$mf_id = $_GET['mfid'];

// $file_name="filter_data.xls";

function cellColor($cells,$color){
  	global $objPHPExcel;
  	$objPHPExcel->getActiveSheet()->getStyle($cells)->getFill()
  	->applyFromArray(array('type' => PHPExcel_Style_Fill::FILL_SOLID,
  		'startcolor' => array('rgb' => $color)
  	));
}

$styleArray = array(
	'font'  => array(
    'bold'  => true,
));

$objPHPExcel = new PHPExcel();

/*$row10 = mysql_fetch_array(mysql_query("select * from mismaster"));
$filename = "../img/$row10[HeaderLogo]";
if (file_exists($filename)) 
{
	$objDrawing = new PHPExcel_Worksheet_Drawing();
	$objDrawing->setName('Logo');
	$objDrawing->setDescription('Logo');
	$objDrawing->setPath("../img/$row10[HeaderLogo]");
	$objDrawing->setHeight(80);
	$objDrawing->setWorksheet($objPHPExcel->setActiveSheetIndex(0));
	$objPHPExcel->getActiveSheet()->mergeCells('A1:B1');
}else{
	$objPHPExcel->getActiveSheet()->SetCellValue('A1',$row10['MisName']);
	$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true);
	$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	$objPHPExcel->getActiveSheet()->mergeCells('A1:B1');	
}*/

// $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(60);

$xlsrow = 1;
$xlscol = 'A';

/*$objPHPExcel->getActiveSheet()->setCellValue($xlscol.$xlsrow, "Filter Name");
$objPHPExcel->getActiveSheet()->getStyle($xlscol.$xlsrow)->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getColumnDimension($xlscol)->setWidth(25);
$objPHPExcel->getActiveSheet()->getStyle($xlscol.$xlsrow)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
cellColor($xlscol.$xlsrow, 'FFCC00');
$xlscol++;

$objPHPExcel->getActiveSheet()->setCellValue($xlscol.$xlsrow, "Phone Number");
$objPHPExcel->getActiveSheet()->getStyle($xlscol.$xlsrow)->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getColumnDimension($xlscol)->setWidth(25);
$objPHPExcel->getActiveSheet()->getStyle($xlscol.$xlsrow)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
cellColor($xlscol.$xlsrow, 'FFCC00');
$xlscol++;

$xlsrow++;*/

$fqry = mysql_query("SELECT `mf_id`, `mf_name` FROM `crm_my_filter` WHERE `mf_id` = '$mf_id'");
$rslt = mysql_fetch_array($fqry);

$file_name = $rslt['mf_name'].'_'.date('d-m-Y');
$i = 0;
	$j = 0;
    $notfixdate = 0;
$fqry1 = mysql_query("SELECT `my_filter`, `my_filter_table_field`, `my_filter_operator`, `my_filter_options` FROM `crm_my_filter-values` WHERE `mf_id` = '$mf_id'");
$count = mysql_num_rows($fqry1);

	if ($count > 0) 
	{
		$query = "SELECT distinct(goh.`GuestPhone`) AS phone,goh.FirstName,um.UnitName FROM `guest_order_history` AS goh INNER JOIN `homedeliveryitemdetails` AS hdid ON goh.`OrderId` = hdid.`OrderId` INNER JOIN unit_master as um ON goh.UnitId = um.UnitId WHERE ";
	
		$and = '';
		while ($frow = mysql_fetch_array($fqry1)) 
		{
			/* Unit ID*/
			if ($frow['my_filter_table_field'] == 'UnitId') {
				$query .= $and.'goh.'.$frow['my_filter_table_field'].' IN ('.$frow['my_filter_options'].') AND ';
			}

			/* Sale Date*/
		if ($frow['my_filter_table_field'] == 'SaleDate') {
			if ($frow['my_filter'] == 'Sale Date Fixed') {

				$query .= $frow['my_filter_table_field']." ".$frow['my_filter_operator']." ".$frow['my_filter_options']." AND ";

			} elseif ($frow['my_filter'] == 'Sale Date Dynamic') {

				$query .= $frow['my_filter_table_field']." ".$frow['my_filter_operator']." ".$frow['my_filter_options']." AND ";

			}
                        elseif ($frow['my_filter'] == 'Sale Date Fixed Not') {
                                $notfixdate = 1;
                                if($i==0){
                                    $startDate = $frow['my_filter_options'];
                                }else{
                                    $endDate = $frow['my_filter_options'];
                                }
                                $fielName = $frow['my_filter_table_field'];
                                $i++;
			}
			
		}

			/* Bill Value*/
			if ($frow['my_filter_table_field'] == 'TotalAmt') {
				$query .= 'goh.'.$frow['my_filter_table_field']." ".$frow['my_filter_operator']." '".$frow['my_filter_options']."' AND ";
			}

			/* Source Of Order*/
			if ($frow['my_filter'] == 'Order Source') {
				$query .= $frow['my_filter_table_field'].'  IN ('.$frow['my_filter_options'].') AND ';
				
				
			}
			/*if ($frow['my_filter'] == 'Order Source Not') {
				
					$query .= $frow['my_filter_table_field'].' NOT IN ('.$frow['my_filter_options'].') AND ';
				
			}*/

			/* GroupName/Itemname*/
			if ($frow['my_filter_table_field'] == 'GroupName') {
				$exp_grp = explode(',', $frow['my_filter_options']);
				$query .= $and.'(';
				$ctr = 1;
				foreach ($exp_grp as $value) {
					if ($ctr == 1) {
						$query .= 'hdid.'.$frow['my_filter_table_field'].' = \''.$value.'\' ';
					} else {
						$query .= 'OR hdid.'.$frow['my_filter_table_field'].' = \''.$value.'\' ';
					}
					$ctr+=1;
				}
				$query .= ') ';
			} else if ($frow['my_filter_table_field'] == 'ItemName') {
				$exp_itm = explode(',', $frow['my_filter_options']);
				$fltr1 = 1;
				$query .= $and.'(';
				foreach ($exp_itm as $value1) {
					if ($fltr1 == 1) {
						$query .= 'hdid.'.$frow['my_filter_table_field'].' = \''.$value1.'\' ';
					} else {
						$query .= 'OR hdid.'.$frow['my_filter_table_field'].' = \''.$value1.'\' ';
					}
					$fltr1 += 1;
				}
				$query .= ') ';
			}

			$and = ' AND ';
		}
	
	}
	   if($notfixdate == 1){
            $query .= $fielName." NOT BETWEEN ".$startDate." AND ".$endDate."  AND ";
        }
	$query .= 'promo_approval=1 AND ';
	$query .= '1=1';
        // echo $query;
       // exit;
	$con_qry = mysql_query($query);

	while ($result = mysql_fetch_array($con_qry)) 
	{
		$xlscol = 'A';
		$objPHPExcel->getActiveSheet()->setCellValue($xlscol.$xlsrow, $result['phone']);
		$xlscol++;
                $objPHPExcel->getActiveSheet()->setCellValue($xlscol.$xlsrow, $result['FirstName']);
		$xlscol++;
                $objPHPExcel->getActiveSheet()->setCellValue($xlscol.$xlsrow, $result['UnitName']);
		$xlscol++;

		$xlsrow++;
	}


header('Content-Type: application/vnd.ms-excel'); 
header('Content-Disposition: attachment;filename='.$file_name.'.xls'); 
header('Cache-Control: max-age=0'); 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
$objWriter->save('php://output');