<?php
include("../include/session.php");
	
	$d = date("d");
	$m = date("m");
	$y = date("Y");
	
	
	$mm = date("m");
	$yy = date("y");
	
	$user_id = $_SESSION['username'];
	date_default_timezone_set('Africa/Gaborone');
	$entry_date = date("Y-m-d H:i:s", time()); 
	
	$read_date = date("Y-m-d");	
	
	$table_name = $_SESSION['db_prefix']."_orders";
	$id_query = "SELECT order_reference  FROM $table_name ORDER BY order_id DESC LIMIT 1 ";
	$id_result = mySQL_query($id_query);
	$id_num = mysql_num_rows($id_result);
	if ($id_num==0) {
		$order_reference = $yy.$mm.'000';
	}
	else {
		$order_reference = mysql_result($id_result, 0, "order_reference");
	}
	
	
	if ( substr($order_reference, 2, 2)!=$mm ) {
	  $number="000";
	}
	else {
	  $number = substr($order_reference, 4, 3);
	}
	
	$order_reference = $yy.$mm.$number+1;
	

	$table_name = $_SESSION['db_prefix']."_orders";
	$order_query = "INSERT INTO $table_name (order_no, order_reference, cust_id, order_date, del_col, delivery_date, delivery_contact, delivery_phone, project_no, project_place, pallets, additional_info, ordered) VALUES ('".$_SESSION['order_no']."', '".$order_reference."', '".$_SESSION['cust_id']."', '".$_SESSION['order_date']."', '".$_SESSION['deliver_collect']."', '".$_SESSION['delivery_date']."', '".$_SESSION['deliver_contact']."', '".$_SESSION['deliver_phone']."', '".$_SESSION['project_no']."', '".$_SESSION['project_place']."', '".$_SESSION['pallets']."', '".$_SESSION['additional_info']."', 'yes' )";
	execsql($order_query);

		
$table_name3 = $_SESSION['db_prefix']."_order_lines_temp";
$items_query = "SELECT * FROM $table_name3 WHERE  user_name =  '".$_SESSION['username']."' ";
$items_result = mySQL_query($items_query);
$items_num = mysql_num_rows($items_result);

$i=0;
while ($i<$items_num) {
   $store_item_id = mysql_result($items_result, $i, "store_item_id");
   $quantity = mysql_result($items_result, $i, "quantity");
   $price = mysql_result($items_result, $i, "price");
   $sub_category = mysql_result($items_result, $i, "sub_category");

 
   $table_name4 = $_SESSION['db_prefix']."_order_lines";  
   $insert_item_query = "INSERT INTO $table_name4 (order_no, order_reference, order_date, cust_id, store_item_id, sub_category, quantity, price ) VALUES ('".$_SESSION['order_no']."', '".$order_reference."', '".$_SESSION['order_date']."', '".$_SESSION['cust_id']."',  '".$store_item_id."', '".$sub_category."', '".$quantity."', '".$price."' ) ";
execsql($insert_item_query);

$i++;
}	


	
	$table_name = $_SESSION['db_prefix']."_tracking_orders";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '42', '".$_SESSION['order_no']."', '".$_SESSION['cust_id']."','".$order_reference."', '' )";
	$trackingResult = mySQL_query($trackingQuery);
	
	//include('../include/orders_alert.php');
	
	$table_name = $_SESSION['db_prefix']."_orders_lines_temp";
	$delete_query = "DELETE FROM $table_name WHERE user_name = '".$_SESSION['username']."'   "; 
	execsql($delete_query);
	
	$_SESSION['cust_id'] = "";
	$_SESSION['comp_name'] = "";
	$_SESSION['deliver_collect'] = '';
	$_SESSION['order_no'] = '';
	$_SESSION['project_no'] = '';
	$_SESSION['project_place'] = '';
	$_SESSION['order_date'] = date('Y-m-d');
	$_SESSION['delivery_date'] = date('Y-m-d');
	$_SESSION['additional_info'] = '';
	$_SESSION['tba_deliver'] = '';
	$_SESSION['pallets'] = ''; 
	 	
    if (isset($_GET['new_button']) )
	   {
		 $action='newOrderWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		 $action='order.php?order_reference='.$order_reference ;
	  }
	 
?>

<script language="javascript" type="text/javascript">
        
		 window.location="<?php echo $action ?> ";
        
       </script>



