<?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 = $con->query($id_query);
	$id_num = mysqli_num_rows($id_result);
	if ($id_num==0) {
		$order_reference = $yy.$mm.'000';
	}
	else {
		while($row = $id_result->fetch_assoc()) {
			$order_reference = $row["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, pallets_price, 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['pallets_price']."', '".$_SESSION['additional_info']."', 'yes' )";
	$order_result = $con->query($order_query);

		
$table_name3 = $_SESSION['db_prefix']."_order_lines_temp";
$items_query = "SELECT * FROM $table_name3 WHERE  user_name =  '".$_SESSION['username']."' ";
$items_result = $con->query($items_query);
$items_num = mysqli_num_rows($items_result);

$total_qty=0;
$order_alert = 'no';
$alert_array = array();
$alert2_array = array();
$alert3_array = array();
$alert4_array = array();
$alert_text = '';
$alert_text_line = '';
while($row = $items_result->fetch_assoc()) {
   $product_item_id = $row["product_item_id"];
   $quantity = $row["quantity"];
   $price = $row["price"];
   $sub_category = $row["sub_category"];
   $total_qty = $total_qty + $quantity;
   
   	$table_name = $_SESSION['db_prefix']."_customer";
	$cust_name_query = "SELECT comp_name FROM $table_name WHERE cust_id = '".$_SESSION['cust_id']."' ";
	$cust_name_result = $con->query($cust_name_query);
	$row = mysqli_fetch_assoc($cust_name_result);
	$cust_name = $row['comp_name'];
	
	$table_name = $_SESSION['db_prefix']."_product_store";
	$product_name_query = "SELECT item_name FROM $table_name WHERE product_item_id = '".$product_item_id."' ";
	$product_name_result = $con->query($product_name_query);
	$row = mysqli_fetch_assoc($product_name_result);
	$product_name = $row['item_name'];

	$order_alert = 'yes';
	$alert_array[] = $_SESSION['order_no'];
	$alert2_array[] = $cust_name;
	$alert3_array[] = $quantity;
	$alert4_array[] = $product_name;
	

   $alert_text_line = $alert_text_line.'<b>'.$quantity.' '.$product_name.'</b>, ';
   $alert_text = 'An order for '.$alert_text_line.' was received from <b>'.$cust_name.'</b> on '.date("l, d-m-Y", strtotime($_SESSION['order_date'])).'. Order no: '.$_SESSION['order_no'].'.';
 
   $table_name4 = $_SESSION['db_prefix']."_order_lines";  
   $insert_item_query = "INSERT INTO $table_name4 (order_no, order_reference, order_date, cust_id, product_item_id, sub_category, quantity, price ) VALUES ('".$_SESSION['order_no']."', '".$order_reference."', '".$_SESSION['order_date']."', '".$_SESSION['cust_id']."',  '".$product_item_id."', '".$sub_category."', '".$quantity."', '".$price."' ) ";
	$insert_item_result = $con->query($insert_item_query);

}	

	
	$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 = $con->query($trackingQuery);
	
	include('../include/order_qty_alert.php');
	
	$table_name = $_SESSION['db_prefix']."_order_lines_temp";
	$delete_query = "DELETE FROM $table_name WHERE user_name = '".$_SESSION['username']."'   "; 
	$delete_result = $con->query($delete_query);
	
	 $_SESSION['cust_id'] = "";
	 $_SESSION['comp_name'] = "";
	 $_SESSION['deliver_collect'] = '';
	 $_SESSION['order_no'] = '';
	 $_SESSION['deliver_contact'] = '';
	 $_SESSION['deliver_phone'] = '';
	 $_SESSION['project_no'] = '';
	 $_SESSION['project_place'] = '';
	 $_SESSION['project_place'] = '';
	 $_SESSION['project_no_original'] = '';
	 $_SESSION['order_date'] = date('Y-m-d');
	 $_SESSION['delivery_date'] = date('Y-m-d');
	 $_SESSION['additional_info'] = '';
	 $_SESSION['tba_deliver'] = '';
	 $_SESSION['pallets'] = '';
	 $_SESSION['pallets_price'] = '';
	 	
    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>



