<?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;
	}


//require_once ('mysql_connect.php');
$ordernumber          = $_GET["onum"];
$archive = $_POST['chkArchive'];
$delete = $_POST['chkDelete'];


  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 orderHistory";


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





$query = "SELECT * FROM orderHistory order by orderTime desc";
 $rt=mysql_query($query);
          // query executed
		echo mysql_error();
    echo '<div align="center"><table width=90% border=1 bordercolor="blue" cellspacing=0 cellpadding=4>';
    echo '<tr><td><div align="center">Order #</div><td><div align="center">Del/PU</div><td><div align="center">Order <br>Total</div><td><div align="center">Pymt <br>form</div><td><div align="center">Order <br>Detail</div>';
 while($nt=mysql_fetch_array($rt)){
     echo '<tr><td><div align="center">';
     echo $nt[orderNumber] . '</div><td><div align="center">';

     if($nt[delivery]==1){echo '<font color="red">Delivery</font>';}else{echo 'Pick Up';}

     echo '</div><td><div align="center">$' . $nt[orderTotal] . '</div><td><div align="center">' . $nt[payMethod] . '</div><td><div align="center">
     <form action="orderdetail.php?archive=1&onum=' . $nt[orderNumber] . '" target=blank" method="post">
		<input type="submit" name="view" value="View/Print">';
		if(!empty($nt[note])){
			echo '<br><div align="left">Notes:'.$nt[note] . '</div>';
		}
		echo'</form></div>';
 }
    echo '</table></div></div>';

	
		



		

	

	

?>