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

include '../dbconnect.php';

if ($_GET['sql'] == 'crm_sql') {
	$id = $_GET['mf_id'];
	// $fqry = mysql_query("SELECT cmf.`mf_id`, cmf.`mf_name`, cmfv.`my_filter`, cmfv.`my_filter_table_field`, cmfv.`my_filter_operator`, cmfv.`my_filter_options` FROM `crm_my_filter` AS cmf INNER JOIN `crm_my_filter-values` AS cmfv ON cmf.`mf_id` = cmfv.`mf_id` WHERE cmf.`mf_id` = '$id'");
	$fqry = mysql_query("SELECT `mf_id`, `mf_name`FROM `crm_my_filter` WHERE `mf_id` = '$id'");
	$rslt = mysql_fetch_array($fqry);

	$fqry1 = mysql_query("SELECT `my_filter`, `my_filter_table_field`, `my_filter_operator`, `my_filter_options` FROM `crm_my_filter-values` WHERE `mf_id` = '$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 ";
	} else {
		$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";
	}
	$i = 0;
	$j = 0;
    $notfixdate = 0;
	while ($frow = mysql_fetch_array($fqry1)) {
		/* Unit ID*/
		if ($frow['my_filter_table_field'] == 'UnitId') {
			$query .= '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 .= 'goh.'.$frow['my_filter_table_field']." ".$frow['my_filter_operator']." ".$frow['my_filter_options']." AND ";

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

				$query .= 'goh.'.$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 = 'goh.'.$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 ";
		} else {
			// $query.='1=1 AND ';
		}

		if ($frow['my_filter_table_field'] == 'TotalOrder') {
			$query .= 'goh.COUNT(InvoicNumber)'." ".$frow['my_filter_operator']." '".$frow['my_filter_options']."' AND ";
		} else {
			// $query.='1=1 AND ';
		}

		/* Source Of Order*/
		if ($frow['my_filter'] == 'Order Source') {
			$query .= 'goh.'.$frow['my_filter_table_field'].'  IN ('.$frow['my_filter_options'].') AND ';
			
			
		}
		if ($frow['my_filter'] == 'Order Source Not') {
			
				$query .= 'goh.'.$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 .= ') AND ';
		} 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 ';
		}
        
	}
        if($notfixdate == 1){
            $query .= $fielName." NOT BETWEEN ".$startDate." AND ".$endDate."  AND ";
        }
	$query .= 'promo_approval=1 AND ';
	$query .= '1=1';
	echo $query;
	/*$crm_qry = mysql_query($query);
	while ($crm_row = mysql_fetch_array($crm_qry)) {
		print_r($crm_row);
	}*/
}

/*$last_visit = $_POST['operator_last_visit'];
$myqry = '';

$myqry = "SELECT * FROM `guest_order_history` WHERE ";

if ($last_visit == 'today') {

	$myqry .= "SaleDate = CURDATE()";

} elseif ($last_visit == 'yesterday') {

	$myqry .= "SaleDate = SUBDATE(CURDATE(), INTERVAL 1 DAY)";

} elseif ($last_visit == 'last7days') {

	$myqry .= "SaleDate >= SUBDATE(CURDATE(), INTERVAL 6 DAY) AND SaleDate <= CURDATE()";

} elseif ($last_visit == 'last30days') {

	$myqry .= "SaleDate >= SUBDATE(CURDATE(), INTERVAL 29 DAYS) AND SaleDate <= CURDATE()";

} elseif ($last_visit == 'thismonth') {

	$myqry .= "SaleDate >= LAST_DAY( CURDATE( ) - INTERVAL 1 MONTH ) + INTERVAL 1 DAY AND SaleDate <= LAST_DAY( CURDATE( ) )";

} elseif ($last_visit == 'lastmonth') {

	$myqry .= "SaleDate >= LAST_DAY( CURDATE( ) - INTERVAL 2 MONTH ) + INTERVAL 1 DAY AND SaleDate <= LAST_DAY( DATE_SUB( CURDATE( ) , INTERVAL 1 MONTH ) )";

}

echo $myqry;*/
?>