<?php
if (($_SESSION['FirstName'] == "")||($_SESSION['adminkey'] !=="security")) {
	echo 'You are not authorized to view this page. <br /><br />You may <a href="index.php">log in here.</a>';
	exit;
	}

if($_POST['ref_code']){	//a ref code here means a tip is being processed on this reference code
	$tipamount=$_POST['tipamount'];
	if(!empty($tipamount)){	//if tip amount not empty, process now
		include('includes/capture_tip.php');
		if($approved=='1'){
			$query = "update orders set cc_tip_paid='1' where cc_ref_code='$_POST[ref_code]'";
			mysql_query($query);
		} else {
			echo "error processing tip.";
		}
	}
}
//require_once ('mysql_connect.php');
$ordernumber          = $_GET["onum"];
$archive = $_POST['chkArchive'];
$delete = $_POST['chkDelete'];
$process = $_GET['process'];

 if (!empty($process)) {
	  //set order new flag to 0 since order has been marked as processed
 $query = "update orders set new = 0, in_production=NOW() where orderNumber = '" . $process . "'";
           mysql_query($query);
 }
  if (!empty($archive)) {
        $aquery = "INSERT INTO orderHistory (orderNumber, approved, delivery, payMethod, comments, orderTotal, UserKey,
        note, orderTime) SELECT orderNumber, approved, delivery, payMethod, comments, orderTotal, UserKey, note, orderTime
        FROM orders WHERE orderNumber = '" . $ordernumber ."'" ;
        //echo $aquery . '<br />';
        if (!mysql_query($aquery)){
					echo $aquery;
  					die('Error: ' . mysql_error());
			     }
        $aquery = "INSERT INTO itemHistory (UserKey, itemNumber, qty, recordKey, parentLineNum, size, subitems, halfA, halfB,
        crust, categoryKey, couponCode, orderNumber, freeTopping) SELECT UserKey, itemNumber, qty, recordKey, parentLineNum,
        size, subitems, halfA, halfB, crust, categoryKey, couponCode, orderNumber, freeTopping
        FROM orderItem WHERE orderNumber = '" . $ordernumber ."'" ;
        //echo $aquery ;
        if (!mysql_query($aquery)){
					echo $aquery;
  					die('Error: ' . mysql_error());
			     }
  $delete = 1; //set so that data will be deleted after archiving
  
  
}

if (!empty($delete)) {
  //remove line items for selected order number
			$query = "Delete From orderItem WHERE orderNumber = '" . $ordernumber ."'" ;

			if (!mysql_query($query)){
					echo $query;
  					die('Error: ' . mysql_error());


			}
			//remove order record for selected order number
			$query = "Delete From orders WHERE orderNumber = '" . $ordernumber ."'" ;

			if (!mysql_query($query)){
					echo $query;
  					die('Error: ' . mysql_error());
 			 } else {
         if(!empty($archive )){
          echo "<div align=center><font color=red>Order # '" . $ordernumber ."' archived successfully.</font></div>";
         }   else {
				  echo "<div align=center><font color=red>Order # '" . $ordernumber ."' deleted successfully.</font></div>";
				 }
			}

		}





		$query = "SELECT * from orders";


		$rt=mysql_query($query);          // query executed 
		echo mysql_error();                    // if any error is there that will be printed to the screen





$query = "SELECT * FROM orders order by new desc, orderTime desc";
 $rt=mysql_query($query);
          // query executed
		echo mysql_error();
    echo '<div align="center"><div style="padding:0% 2% 0% 2%"><font color="red">Click the "Process order" link to remove the NEW status from an order and move it to the bottom.<br /><br /></font></div></div>
	<table id="live-order-table" width=100% border=0 bordercolor="#000" cellspacing=0 cellpadding=0>';
    echo '<tr id="live-header"><th><div align="center">Order #</div><th><div align="center">Type/Payment</div><th><div align="center">Order Detail</div><th><div align="center">Final</div>';
 while($nt=mysql_fetch_array($rt)){
     $note = "";
     echo '<tr><td class="order-no-column"><div>';
     if ($nt['new']==1){echo '<span class="new-indicator">NEW</span>' ;}
     echo $nt[orderNumber] . '<br /><span id="mobile-hide">Date: </span>'.date ('m/d/Y', strtotime($nt[orderTime])).'<br /><span id="mobile-hide">Time: </span>'.date ('h:i:A', strtotime($nt[orderTime])).'</div><td class="type-column"><div>Type: ';

     if($nt[delivery]==1){echo '<font color="red">Delivery</font>';}else{echo 'Pick Up';}
	//echo '</div><td class="order-total-column"><div>$' . $nt[orderTotal] . '</div><td class="payment-method-column"><div>' . $nt[payMethod] . '</div><td class="order-detail-column"><div>';
	if($nt[note]!="n/a" && $nt[note]!=""){
		$note = "<br><i><b>* See cust notes</b></i>"; 	
    }
    echo '<br>Total: $' . $nt[orderTotal] . '<br>Pymt: ' . $nt[payMethod] . $note .'</div>';
    echo '<td class="order-detail-column"><div>';
	if((!empty($nt[cc_ref_code]))&&($nt[payMethod]=="card")&&($nt[cc_tip_paid]=='0')){ //show tip form for cc orders with a reference code, and that have not already been charged a tip
		echo '<form action="'.$_SERVER[PHP_SELF].'" method="post">Add tip to card: <input type="text" size=4 name="tipamount"> <input type="hidden" size=4 name="ref_code" value="'.$nt[cc_ref_code].'"><input type="submit" id="update" name="addtip" value="Process"></form>';
	}
    echo '<form action="orderdetail.php?onum=' . $nt[orderNumber] . '" target=blank" method="post">
		<input id="live-order-button" type="submit" name="view" id="update" value="View/Print"><a id="live-order-button2" href="' . $_SERVER['PHP_SELF'] . '?process=' . $nt[orderNumber] . '">Process <span id="mobile-hide">Order</span></a>';
		//if(!empty($nt[note])){
		//	echo '<br /><div align="left">Notes:'.$nt[note] . '</div>';
		// }
		echo'</form></div><td class="final-column"><div><form action="' . $_SERVER['PHP_SELF'] . '?onum=' . $nt[orderNumber] . '" method="post">
		<input type="checkbox" name="chkArchive"> Archive<br /><input type="checkbox" name="chkDelete"> Delete<br /><input type="submit" id="save-button" name="view" value="Submit">
		</form></div>';
 }
    echo '</table></div></div>';

	
		



		

	

	

?>