<?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 store_item_id, quantity FROM $table_name WHERE order_reference = '".$order_reference."' ";
$saved_order_result = mySQL_query($saved_order_query);
$saved_order_num = mysql_num_rows($saved_order_result);
$i=0;
while ($i<$saved_order_num) {
$saved_store_item_id = mysql_result($saved_order_result, $i, 'store_item_id');
$saved_quantity = mysql_result($saved_order_result, $i, 'quantity');

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

//if ($quantity<>0) {

if ($saved_store_item_id!=$store_item_id) {
	    
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT price FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
		$name_result = mySQL_query($name_query);
		$price = mysql_result($name_result, 0, "price");
	
	   $table_name = $_SESSION['db_prefix']."_order_lines";
	   $tile_update_query = "UPDATE $table_name SET store_item_id = '".$store_item_id."' WHERE order_reference = '".$order_reference."'  AND store_item_id = '".$saved_store_item_id."' ";
	   execsql($tile_update_query);
	   
	   $table_name = $_SESSION['db_prefix']."_store_items";
	   $name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$saved_store_item_id."'  ";
	   $name_result = mySQL_query($name_query);
	   $saved = mysql_result($name_result, 0, "item_name");
	  
	   
	   $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']."', '45', '".$order_reference."' ,'".$saved_store_item_id."','".$store_item_id."','Item' )";
		execsql($trackingQuery);
		
		//$table_name = $_SESSION['db_prefix']."_store_items";
//		$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
//		$name_result = mySQL_query($name_query);
//		$new = mysql_result($name_result, 0, "item_name");	
//		
//		$note_text = 'Item was changed from '.$saved.' to '.$new.'.'; 
//		$table_name = $_SESSION['db_prefix']."_quotation_notes";
//		$notes_query = "INSERT INTO $table_name (order_number, note_date, note_text, seller ) VALUES ('".$order_reference."', '".$entry_date."', '".$note_text."', '".$_SESSION['username']."' )";
//		execsql($notes_query);
}

$table_name = $_SESSION['db_prefix']."_order_lines";
$update_query = "UPDATE $table_name SET quantity = '".$quantity."' WHERE order_reference = '".$order_reference."'  AND store_item_id = '".$store_item_id."' ";
execsql($update_query);

if ($saved_quantity!=$quantity) {
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$saved_store_item_id."'  ";
		$name_result = mySQL_query($name_query);
		$item = mysql_result($name_result, 0, "item_name");
	
	   $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']."', '43', '".$order_reference."' ,'".$saved_quantity."','".$quantity."','Qty for ".$item."' )";
	   execsql($trackingQuery);
		
		//$note_text = 'Quantity for '.$item.' was changed from '.$saved_quantity.' to '.$quantity.'.'; 
//		$table_name = $_SESSION['db_prefix']."_quotation_notes";
//		$notes_query = "INSERT INTO $table_name (quote_number, note_date, note_text, seller ) VALUES ('".$quote_no."', '".$entry_date."', '".$note_text."', '".$_SESSION['username']."' )";
//		execsql($notes_query);
}
//}
//else {
//	$table_name = $_SESSION['db_prefix']."_store_items";
//	$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$saved_store_item_id."'  ";
//	$name_result = mySQL_query($name_query);
//	$item = mysql_result($name_result, 0, "item_name");
//	
//	$table_name = $_SESSION['db_prefix']."_order_lines";
//	$delete_query = "DELETE FROM $table_name WHERE order_reference = '".$order_reference."'  AND store_item_id = '".$store_item_id."' ";
//	execsql($delete_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']."', '44', '".$order_reference."' ,'','','".$item."' )";
//	execsql($trackingQuery);
//		
//	//$note_text = 'Item '.$item.' was deleted from the quote.'; 
////	$table_name = $_SESSION['db_prefix']."_quotation_notes";
////	$notes_query = "INSERT INTO $table_name (quote_number, note_date, note_text, seller ) VALUES ('".$quote_no."', '".$entry_date."', '".$note_text."', '".$_SESSION['username']."' )";
////	execsql($notes_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 = mySQL_query($order_query);
$order_no = mysql_result($order_result, 0, "order_no");
$saved_del_col = mysql_result($order_result, 0, "del_col");
$saved_order_date = mysql_result($order_result, 0, "order_date");
$saved_delivery_date = mysql_result($order_result, 0, "delivery_date");
$saved_project_no = mysql_result($order_result, 0, "project_no");
$saved_project_place = mysql_result($order_result, 0, "project_place");
$saved_contact = mysql_result($order_result, 0, "delivery_contact");
$saved_phone = mysql_result($order_result, 0, "delivery_phone");
$saved_pallets = mysql_result($order_result, 0, "pallets");
$saved_notes = mysql_result($order_result, 0, "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."'  ";
execsql($update_query);

if ($saved_del_col!=$del_col) {
	    $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']."', '43', '".$order_reference."' ,'".$saved_del_col."','".$del_col."','Deliver/collect' )";
		execsql($trackingQuery);
		}
		
if ($saved_order_date!=$order_date) {
	    $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']."', '43', '".$order_reference."' ,'".$saved_order_date."','".$order_date."','Order date' )";
		execsql($trackingQuery);
		}
		
if ($saved_delivery_date!=$delivery_date) {
	    $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']."', '43', '".$order_reference."' ,'".$saved_delivery_date."','".$delivery_date."','Delivery date' )";
		execsql($trackingQuery);
		}
		
if ($saved_project_no!=$project_no) {
	    $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']."', '43', '".$order_reference."' ,'".$saved_project_no."','".$project_no."','Plot no' )";
		execsql($trackingQuery);
		}
		
if ($saved_project_place!=$project_place) {
	    $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']."', '43', '".$order_reference."' ,'".$saved_project_place."','".$project_place."','Deliver place' )";
		execsql($trackingQuery);
		}

if ($saved_contact!=$contact) {
	    $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']."', '43', '".$order_reference."' ,'".$saved_contact."','".$contact."','Contact' )";
		execsql($trackingQuery);
		}
		
if ($saved_phone!=$phone) {
	    $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']."', '43', '".$order_reference."' ,'".$saved_phone."','".$phone."','Contact phone' )";
		execsql($trackingQuery);
		}		
				

if ($saved_pallets!=$pallets) {
	    $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']."', '43', '".$order_reference."' ,'".$saved_pallets."','".$pallets."','Pallets' )";
		execsql($trackingQuery);
		}
		
if ($saved_notes!=$notes) {
	    $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']."', '43', '".$order_reference."' ,'".$saved_notes."','".$notes."','Notes:' )";
		execsql($trackingQuery);
		}



//$extra_note = $_POST['note_text'];
//if ($extra_note!='') {
//	    $note_text = $extra_note; 
//		$table_name = $_SESSION['db_prefix']."_quotation_notes";
//		$notes_query = "INSERT INTO $table_name (quote_number, note_date, note_text, seller ) VALUES ('".$quote_no."', '".$entry_date."', '".$note_text."', '".$_SESSION['username']."' )";
//		execsql($notes_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		 
	   }
	   ?>

