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

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

$store_item_id = $_GET['store_item_id_text'];
$line_num = $_GET['line_num'];
$receive_date = $_GET['receive_date'];

$table_name = $_SESSION['db_prefix']."_store_register_temp";
$delete_query = "DELETE FROM $table_name WHERE user_name = '".$_SESSION['username']."' "; 
execsql($delete_query);

$error=0;
$i=0;
while ($i<$line_num) {
		
	if (isset($_GET['line_checkbox'.$i])) {
	
	$serial_no= $_GET['serial_no_text'.$i];
	$notes= $_GET['notes_text'.$i];
	$double_error=0;	
			
	$table_name = $_SESSION['db_prefix']."_store_register_temp";
	$check_query = "SELECT serial_number FROM $table_name WHERE store_item_id = '".$store_item_id."' AND serial_number = '".$serial_no."' ";
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	if ($check_num>0) {
		  $double_error=1;
		  $error=1;
	}
		
	$table_name = $_SESSION['db_prefix']."_store_register";
	$check2_query = "SELECT serial_number FROM $table_name WHERE store_item_id = '".$store_item_id."' AND serial_number = '".$serial_no."' ";
	$check2_result = mySQL_query($check2_query);
	$check2_num = mysql_num_rows($check2_result);
	if ($check2_num>0) {
		  $double_error=2;
		  $error=1;
	}
	
		
    $table_name = $_SESSION['db_prefix']."_store_register_temp";
	$temp_register_query = "INSERT INTO $table_name (store_item_id, serial_number, double_error, notes, user_name ) VALUES ('".$store_item_id."', '".$serial_no ."', '".$double_error."', '".$notes."', '".$_SESSION['username']."' )";
	execsql($temp_register_query);
	
	}
		
	$i++;
}


if ($error>0) {
	?>
         <script>
	 		 window.location="store_registration.php?double_error=2&store_item_id=<?php echo $store_item_id ?> ";
		</script>
<?php
}
else {
	
$table_name1 = $_SESSION['db_prefix']."_store_register_temp";
$table_name2 = $_SESSION['db_prefix']."_store_register";
$copy_query = "INSERT INTO $table_name2 (store_item_id, serial_number, notes, date_received ) SELECT store_item_id, serial_number, notes, '".$receive_date."' FROM $table_name1 WHERE double_error=0";	
execsql($copy_query);
	
$table_name = $_SESSION['db_prefix']."_store_register_temp";
$delete_query = "DELETE FROM $table_name WHERE user_name = '".$_SESSION['username']."' "; 
execsql($delete_query);		
	
$table_name = $_SESSION['db_prefix']."_tracking_store";
$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '83', '".$store_item_id."' ,'".$receive_date."','','' )";
   $trackingResult = mySQL_query($trackingQuery);

?>
<script>
	  window.location="../store/store_main.php?date=0";
</script>
<?php
}
?>
