<?php
include('../include/session.php');



date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 

$order_reference = $_GET['order_reference'];
$store_item_id = $_GET['store_item_id_select'];
$qty = $_GET['qty_text'];

$table_name = $_SESSION['db_prefix']."_orders";
$order_query = "SELECT order_no, cust_id, order_date FROM $table_name WHERE order_reference = '".$order_reference."' ";
$order_result = mySQL_query($order_query);
$order_no = mysql_result($order_result, 0, "order_no");
$cust_id = mysql_result($order_result, 0, "cust_id");
$order_date = mysql_result($order_result, 0, "order_date");

$table_name = $_SESSION['db_prefix']."_store_items";
$item_query = "SELECT item_name, subCategory, price FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
$item_result = mySQL_query($item_query);
$item_name = mysql_result($item_result, 0, "item_name");
$sub_category = mysql_result($item_result, 0, "subCategory");
$price = mysql_result($item_result, 0, "price");


$table_name = $_SESSION['db_prefix']."_order_lines";
$check_query = "SELECT order_reference FROM $table_name WHERE order_reference = '".$order_reference."' AND store_item_id = '".$store_item_id."'  ";
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);

if ($check_num==0) {

$table_name = $_SESSION['db_prefix']."_order_lines";
$add_query = "INSERT INTO $table_name (order_reference, order_no, order_date, cust_id, store_item_id, sub_category, quantity, price) VALUES ('".$order_reference."', '".$order_no."', '".$order_date."', '".$cust_id."', '".$store_item_id."', '".$sub_category."', '".$qty."', '".$price."' ) ";
execsql($add_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']."', '46', '".$order_reference."' , '".$store_item_id."','".$item_name."','".$qty."' )";
execsql($trackingQuery);


$error=0;

}
else {
$error=1;
}
?>


<script language="javascript" type="text/javascript">
        window.location="../orders/edit_order.php?order_reference=<?php echo $order_reference ?>&e=<?php echo $error ?>";
</script>