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

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

$line_num = $_GET['line_num'];
$order_reference = $_GET['order_reference'];

$i=0;
while ($i<$line_num) {
	
	$delivery_no = $_GET['delivery_no'.$i];
    $pallets = $_GET['pallets_text'.$i];
	
	if ($pallets>0) {
	
	$table_name = $_SESSION['db_prefix']."_pallets";
	$return_query = "UPDATE $table_name SET return_date = '".$entry_date."', ret_quantity = ret_quantity + '".$pallets."'  WHERE delivery_no = '".$delivery_no."' ";
	execsql($return_query);
	
	$table_name = $_SESSION['db_prefix']."_pallets_returned";
	$pallets_return_query = "INSERT INTO $table_name (delivery_no, ret_quantity, return_date ) VALUES ('".$delivery_no."', '".$pallets."', '".$entry_date."'  ) ";
	execsql($pallets_return_query);
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$stock_query = "UPDATE $table_name SET quantity = quantity+'".$pallets."' WHERE store_item_id = '10001' ";
	execsql($stock_query); 
				
	$table_name = $_SESSION['db_prefix']."_store_items";
	$stock_query = "SELECT quantity FROM $table_name WHERE store_item_id = '10001' ";
	$stock_result = mySQL_query($stock_query);
	$stock_quantity = mysql_result($stock_result, 0, "quantity");
				
	$table_name2 = $_SESSION['db_prefix']."_item_history";	
	$historyQuery = "INSERT INTO $table_name2 (store_item_id, entry, reference, quantity, total, date, entryDate, userId) VALUES ('10001', '24', '".$delivery_no."', '".$pallets."',  '".$stock_quantity."', '".$entry_date."','".$entry_date."','".$_SESSION['username']."')";
	$historyResult = mySQL_query($historyQuery);
	
	$table_name = $_SESSION['db_prefix']."_tracking_store";
	$trackingQuery = "INSERT INTO $table_name (entry_date, branch, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' , 'Gaborone', '".$_SESSION['username']."', '26', '".$delivery_no."', '".$order_reference."' ,'".$pallets."','' )";
	$trackingResult = mySQL_query($trackingQuery);	
	
	}
	
	$i++;
}


	if (isset($_GET['close_button']) )
	   { ?>
       <script>
	 		 window.location="store_main.php?date=0&subCatSelect=Tiles";
		</script>
        <?php		 
	   }
	   
	   if (isset($_GET['new_button']) )
	   {  ?>
       <script>
	          window.location="pallets_return.php?new=0&order_reference=";
	   </script>
        <?php		 
	   }
	   ?>
