<?php
include('../include/session.php');
date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 

$order_reference = $_POST['order_reference_text'];


$table_name = $_SESSION['db_prefix']."_order_lines";
$saved_order_query = "SELECT order_line_id, product_item_id, quantity FROM $table_name WHERE order_reference = '".$order_reference."' ";
$saved_order_result = $con->query($saved_order_query);
$saved_order_num = mysqli_num_rows($saved_order_result);
$i=0;
while($row = $saved_order_result->fetch_assoc()) {
$order_line_id = $row["order_line_id"];
$saved_product_item_id = $row["product_item_id"];
$saved_quantity = $row["quantity"];

$product_item_id = $_POST['product_item_id'.$i];
$quantity = $_POST['quantity_text'.$i];


if ($saved_product_item_id!=$product_item_id) {
	    
		$table_name = $_SESSION['db_prefix']."_product_store";
		$name_query = "SELECT price FROM $table_name WHERE product_item_id = '".$product_item_id."'  ";
		$name_result = $con->query($name_query);
		$row = mysqli_fetch_assoc($name_result);
		$price = $row['price'];
		
	
	   $table_name = $_SESSION['db_prefix']."_order_lines";
	   $tile_update_query = "UPDATE $table_name SET product_item_id = '".$product_item_id."' WHERE order_reference = '".$order_reference."'  AND order_line_id = '".$order_line_id."' ";
	   $con->query($tile_update_query);
	   
	   $table_name = $_SESSION['db_prefix']."_product_store";
	   $name_query = "SELECT item_name FROM $table_name WHERE product_item_id = '".$saved_product_item_id."'  ";
	   $name_result = $con->query($name_query);
	   $row = mysqli_fetch_assoc($name_result);
	   $saved = $row['item_name'];

	  
	   
	   $table_name = $_SESSION['db_prefix']."_tracking_orders";
	   $tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '45', '".$order_reference."' ,'".$saved_product_item_id."','".$product_item_id."','Item' )";
		$tracking_result = $con->query($tracking_query);

}

$table_name = $_SESSION['db_prefix']."_order_lines";
$update_query = "UPDATE $table_name SET quantity = '".$quantity."' WHERE order_reference = '".$order_reference."'  AND product_item_id = '".$product_item_id."' ";
$update_result = $con->query($update_query);

if ($saved_quantity!=$quantity) {
		$table_name = $_SESSION['db_prefix']."_product_store";
		$name_query = "SELECT item_name FROM $table_name WHERE product_item_id = '".$saved_product_item_id."'  ";
		$name_result = $con->query($name_query);
		$row = mysqli_fetch_assoc($name_result);
		$item = $row['item_name'];
		
	
	   $table_name = $_SESSION['db_prefix']."_tracking_orders";
	   $tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '43', '".$order_reference."' ,'".$saved_quantity."','".$quantity."','Qty for ".$item."' )";
	   $tracking_result = $con->query($tracking_query);
}

$i++;
}

$del_col = $_POST['deliver_collect_select'];
$order_date = $_POST['order_date'];
$delivery_date = $_POST['delivery_date'];
$project_no = $_POST['project_no_text'];
$project_place = $_POST['project_place_select'];
$contact = $_POST['contact_text'];
$phone = $_POST['phone_text'];
$pallets = $_POST['pallets_text'];
$notes = $_POST['notes_text'];
$tba = $_POST['tba'];

if ($tba=='yes') { 
$delivery_date = '2000-01-01'; 
}

	


$table_name = $_SESSION['db_prefix']."_orders";
$order_query = "SELECT order_no, del_col, order_date, delivery_date, project_no, project_place, delivery_contact, delivery_phone, pallets, additional_info FROM $table_name WHERE order_reference = '".$order_reference."'  ";
$order_result = $con->query($order_query);
$row = mysqli_fetch_assoc($order_result);
$order_no = $row["order_no"];
$saved_del_col = $row["del_col"];
$saved_order_date = $row["order_date"];
$saved_delivery_date = $row["delivery_date"];
$saved_project_no = $row["project_no"];
$saved_project_place = $row["project_place"];
$saved_contact = $row["delivery_contact"];
$saved_phone = $row["delivery_phone"];
$saved_pallets = $row["pallets"];
$saved_notes = $row["additional_info"];

$table_name = $_SESSION['db_prefix']."_orders";
$update_query = "UPDATE $table_name SET del_col = '".$del_col."', order_date = '".$order_date."', delivery_date = '".$delivery_date."', project_no = '".$project_no."', project_place = '".$project_place."', delivery_contact = '".$contact."', delivery_phone = '".$phone."', pallets = '".$pallets."', additional_info = '".$notes."' WHERE order_reference = '".$order_reference."'  ";
$update_result = $con->query($update_query);

if ($saved_del_col!=$del_col) {
	    $table_name = $_SESSION['db_prefix']."_tracking_orders";
		$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '43', '".$order_reference."' ,'".$saved_del_col."','".$del_col."','Deliver/collect' )";
		$tracking_result = $con->query($tracking_query);
		}
		
if ($saved_order_date!=$order_date) {
	    $table_name = $_SESSION['db_prefix']."_tracking_orders";
		$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '43', '".$order_reference."' ,'".$saved_order_date."','".$order_date."','Order date' )";
		$tracking_result = $con->query($tracking_query);
		}
		
if ($saved_delivery_date!=$delivery_date) {
	    $table_name = $_SESSION['db_prefix']."_tracking_orders";
		$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '43', '".$order_reference."' ,'".$saved_delivery_date."','".$delivery_date."','Delivery date' )";
		$tracking_result = $con->query($tracking_query);
		}
		
if ($saved_project_no!=$project_no) {
	    $table_name = $_SESSION['db_prefix']."_tracking_orders";
		$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '43', '".$order_reference."' ,'".$saved_project_no."','".$project_no."','Plot no' )";
		$tracking_result = $con->query($tracking_query);
		}
		
if ($saved_project_place!=$project_place) {
	    $table_name = $_SESSION['db_prefix']."_tracking_orders";
		$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '43', '".$order_reference."' ,'".$saved_project_place."','".$project_place."','Deliver place' )";
		$tracking_result = $con->query($tracking_query);
		}

if ($saved_contact!=$contact) {
	    $table_name = $_SESSION['db_prefix']."_tracking_orders";
		$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '43', '".$order_reference."' ,'".$saved_contact."','".$contact."','Contact' )";
		$tracking_result = $con->query($tracking_query);
		}
		
if ($saved_phone!=$phone) {
	    $table_name = $_SESSION['db_prefix']."_tracking_orders";
		$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '43', '".$order_reference."' ,'".$saved_phone."','".$phone."','Contact phone' )";
		$tracking_result = $con->query($tracking_query);
		}		
				

if ($saved_pallets!=$pallets) {
	    $table_name = $_SESSION['db_prefix']."_tracking_orders";
		$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '43', '".$order_reference."' ,'".$saved_pallets."','".$pallets."','Pallets' )";
		$tracking_result = $con->query($tracking_query);
		}
		
if ($saved_notes!=$notes) {
	    $table_name = $_SESSION['db_prefix']."_tracking_orders";
		$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '43', '".$order_reference."' ,'".$saved_notes."','".$notes."','Notes:' )";
		$tracking_result = $con->query($tracking_query);
		}




if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="../store/order.php?order_reference=<?php echo $order_reference?>";
		</script>
        <?php		 
	   }
	   
	if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	        window.location="edit_order.php?order_reference=<?php echo $order_reference?>&e=0";
	   </script>
        <?php		 
	   }
	   ?>

