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

$value = $_GET['value'];
$sub_category = $_GET['sub_category'];

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

$table_name = $_SESSION['db_prefix']."_receive_store";
$receive_query = "INSERT INTO $table_name (receive_date, delivery_note, supplier_id, value, transport ) VALUES ('".$_SESSION['receive_date']."', '".$_SESSION['delivery_note_no']."', '".$_SESSION['supplier_id']."', '".$value."', '".$_SESSION['transport_cost']."'  ) ";
$receive_result = $con->query($receive_query);

$table_name = $_SESSION['db_prefix']."_receive_store";
$rec_id_query = "SELECT receive_id  FROM $table_name ORDER BY receive_id DESC LIMIT 1  ";
$rec_id_result = $con->query($rec_id_query);
while($row = $rec_id_result->fetch_assoc()) {
$receive_id = $row["receive_id"];
}
	

$table_name3 = $_SESSION['db_prefix']."_receive_store_lines_temp";
$items_query = "SELECT * FROM $table_name3 WHERE  user_name =  '".$_SESSION['username']."' ";
$items_result = $con->query($items_query);
$items_num = mysqli_num_rows($items_result);

while($row = $items_result->fetch_assoc()) {
		$store_item_id = $row["store_item_id"];
  		$quantity = $row["quantity"];
   		$price = $row["price"];
   
 
   	$table_name4 = $_SESSION['db_prefix']."_receive_store_lines";  
   	$insert_item_query = "INSERT INTO $table_name4 (receive_id, sub_category, store_item_id, quantity, purchase_price ) VALUES ('".$receive_id."', '".$sub_category."', '".$store_item_id."',  '".$quantity."', '".$price."'  ) ";
	$insert_item_result = $con->query($insert_item_query);

     
   $table_name2 = $_SESSION['db_prefix']."_store_items";
   $stock_total_query = "SELECT quantity FROM $table_name2 WHERE store_item_id = '".$store_item_id."'";
   $stock_total_result = $con->query($stock_total_query);
   while($row = $stock_total_result->fetch_assoc()) {
			$total_quantity = $row["quantity"];
	}
   $total_quantity = $total_quantity + $quantity;
      
   $stock_query = "UPDATE $table_name2 SET supplier_id = '".$_SESSION['supplier_id']."', quantity = '".$total_quantity."' , lastModDate = '".$_SESSION['receive_date']."', purchase_price = '".$price."'  WHERE store_item_id = '".$store_item_id."'";
	$stock_result = $con->query($stock_query);
		
	$table_name3 = $_SESSION['db_prefix']."_item_history";
	$history_query = "INSERT INTO $table_name3 (store_item_id, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$store_item_id."', '11', '".$receive_id."','".$quantity."', '".$total_quantity."','".$_SESSION['receive_date']."','".$entry_date."','".$userId."')";
	$history_result = $con->query($history_query);
	
	$table_name = $_SESSION['db_prefix']."_tracking_store";
	$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."',  '".$_SESSION['username']."', '11', '".$store_item_id."', '".$_SESSION['supplier_id']."' ,'".$receive_id."','' )";
	$tracking_result = $con->query($tracking_query);
}
	
	


$table_name = $_SESSION['db_prefix']."_receive_store_lines_temp";
$delete2_query = "DELETE FROM $table_name WHERE  user_name =  '".$_SESSION['username']."' "; 
$delete2_result = $con->query($delete2_query);


	if (isset($_GET['new_button']) )
	   {
		 $action='receivingWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='receive_note.php?receive_id='.$receive_id ;
	  }
	
	 $_SESSION['supplier_id'] = '';	
	 $_SESSION['del_note'] = '';	
     $_SESSION['receive_date']  = date('Y-m-d');
	 $_SESSION['receive_id'] = '';
	 
	 
	 
	 $_SESSION['store_item_id'] = '';
	 $_SESSION['store_item_name'] = '';
	 
	 $_SESSION['receive_line_id'] = '';
	 $_SESSION['sup_name'] = '';
	 $_SESSION['total'] = '';
	 
	 $_SESSION['value'] = '';
	
?>

<script language="javascript" type="text/javascript">
        
		  window.location="<?php echo $action?>";
        
       </script>

