<?php
	include ('includes/db1config.php');
	include ('../includes/mysql_connect.php');
	
	$customer  = "SELECT * from guestLogin";
	$custArray = mysql_query($customer);  // query executed
	echo mysql_error(); 

	while($nt2=mysql_fetch_array($custArray)){
		$arr[$nt2['UserKey']] = $nt2['FirstName'].'-'.$nt2['LastName'];
	}
	
	if( isset($_POST) ){
		if(isset($_POST['select_date']) && $_POST['select_date'] != ''){
			
			$select_date = $_POST['select_date'];//date('Y-m-d')
			$query       = "SELECT * FROM orders WHERE DATE(orderTime) = '".$select_date."' ORDER BY recordKey DESC";
			$rt          = mysql_query($query); // query executed
			echo mysql_error(); // if any error is there that will be printed to the screen
			
		} elseif( isset($_POST['customer_name']) && $_POST['customer_name'] != ''  && isset($_POST['begin_date']) && $_POST['begin_date'] == '' && isset($_POST['end_date']) && $_POST['end_date'] == '' && isset($_POST['order_number']) && $_POST['order_number'] == '' ) {
			
			$query  = "SELECT * FROM orders WHERE UserKey = '".$_POST['customer_name']."' ORDER BY recordKey DESC";
			$rt     = mysql_query($query); // query executed
			echo mysql_error(); 

		} elseif ( $_POST['customer_name'] == '' && $_POST['order_number'] == '' && isset($_POST['begin_date']) && isset($_POST['end_date']) && $_POST['begin_date'] != '' && $_POST['end_date'] != ''  ){
			
			$query  = "SELECT * FROM orders WHERE DATE(orderTime) >= '".$_POST['begin_date']."' AND DATE(orderTime)<= '".$_POST['end_date']."' ORDER BY recordKey DESC";
			$rt     = mysql_query($query); // query executed
			echo mysql_error(); 

		} else if( $_POST['begin_date'] == '' && $_POST['end_date'] == '' && $_POST['customer_name'] == '' && isset($_POST['order_number']) && $_POST['order_number'] != '' ) {

			$query  = "SELECT * FROM orders WHERE orderNumber = '".$_POST['order_number']."' ORDER BY recordKey DESC";
			$rt     = mysql_query($query); // query executed
			echo mysql_error();

		} else if( $_POST['customer_name'] != '' && isset($_POST['begin_date']) && isset($_POST['end_date']) && $_POST['begin_date'] != '' && $_POST['end_date'] != '' && $_POST['order_number'] == '') {
			
			$query  = "SELECT * FROM orders WHERE DATE(orderTime) >= '".$_POST['begin_date']."' AND DATE(orderTime)<= '".$_POST['end_date']."' AND UserKey = '".$_POST['customer_name']."' ORDER BY recordKey DESC";
			$rt     = mysql_query($query); // query executed
			echo mysql_error(); 

		} else if( $_POST['order_number'] != '' && isset($_POST['begin_date']) && isset($_POST['end_date']) && $_POST['begin_date'] != '' && $_POST['end_date'] != '' && $_POST['customer_name'] == '' ) {
			
			$query  = "SELECT * FROM orders WHERE DATE(orderTime) >= '".$_POST['begin_date']."' AND DATE(orderTime)<= '".$_POST['end_date']."' AND orderNumber = '".$_POST['order_number']."' ORDER BY recordKey DESC";
			$rt     = mysql_query($query); // query executed
			echo mysql_error(); 

		} else if( $_POST['order_number'] != '' && isset($_POST['customer_name']) && $_POST['customer_name'] != '' && $_POST['end_date'] == '' && $_POST['begin_date'] == '' ){
			
			$query  = "SELECT * FROM orders WHERE orderNumber = '".$_POST['order_number']."' AND UserKey = '".$_POST['customer_name']."' ORDER BY recordKey DESC";
			$rt     = mysql_query($query); // query executed
			echo mysql_error();

		} else if( $_POST['customer_name'] != '' && isset($_POST['begin_date']) && isset($_POST['end_date']) && $_POST['begin_date'] != '' && $_POST['end_date'] != '' && $_POST['order_number'] != '' ){ //all are present
			
			$query  = "SELECT * FROM orders WHERE DATE(orderTime) >= '".$_POST['begin_date']."' AND DATE(orderTime)<= '".$_POST['end_date']."' AND orderNumber = '".$_POST['order_number']."' AND UserKey = '".$_POST['customer_name']."'ORDER BY recordKey DESC";
			$rt     = mysql_query($query); // query executed
			echo mysql_error();

		} else {
			
			echo "nothing"; 
		}
		 
		
	    $str = '<center>
	    		
	            <table width=100% cellpadding=5 cellspacing=5 id="invoiceTable" >
	            <tr>
	                <td>
	                    <strong>Order Number</strong>
	                </td>
	                <td>
	                    <strong>Customer Name</strong>
	                </td>
	                <td>
	                    <strong>Order Date & Time</strong>
	                </td>
	                <td>
	                    <strong>Order Total</strong>
	                </td>
	                <td>
	                    <strong>Action(s)</strong>
	                </td>
	            </tr>
	            <tr class="appendInvoice">
	                <td colspan="8"><hr /></td>
	            </tr>';
	            $grandTotal = 0;
	            while ( $row = mysql_fetch_array($rt)) {
	            	// echo "<pre>";print_r($row);die('here');
	                    $str.='<tr class="orders">
	                            <td width=20%>' . $row['orderNumber'] . '</td>
	                            <td width=20%>' . $arr[$row['UserKey']] . '</td>
	                            <td width=25%>' . $row['orderTime'] . '</td>
	                            <td width=20%>' . $row['orderTotal'] . '</td>
	                            <td style="padding:4px 4px;" width=25%><a style="width:50px;" class="popup add-menu-item" id="save-button" data-order-id="'.$row['recordKey'].'" data-popup-id="edit">View Order</a></td></tr>';
                    $grandTotal+=$row['orderTotal'];
	            }

	            if( $grandTotal != 0 ){
	            	$str.='<tr><td colspan="4">Grand Total:- $'.$grandTotal.'</td></tr>';
	            }
	                
	            $str.='</table>
	            </table>
	            </center>';
	    echo $str;
	}
?>