<?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']."'  ) ";
execsql($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 = mySQL_query($rec_id_query);
$receive_id = mysql_result($rec_id_result, 0, "receive_id");
	

$table_name3 = $_SESSION['db_prefix']."_receive_store_lines_temp";
$items_query = "SELECT * FROM $table_name3 WHERE  user_name =  '".$_SESSION['username']."' ";
$items_result = mySQL_query($items_query);
$items_num = mysql_num_rows($items_result);

$i=0;
while ($i<$items_num) {
   $store_item_id = mysql_result($items_result, $i, "store_item_id");
   $quantity = mysql_result($items_result, $i, "quantity");
   $price = mysql_result($items_result, $i, "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."'  ) ";
execsql($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 = mySQL_query($stock_total_query);
   $total_quantity = mysql_result($stock_total_result ,0, "quantity");
	
   $total_quantity = $total_quantity + $quantity;
      
   $stockQuery = "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."'";
	$stockResult = mySQL_query($stockQuery);
		
	$table_name3 = $_SESSION['db_prefix']."_item_history";
	$historyQuery = "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."')";
	execsql($historyQuery);
	
	$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']."', '11', '".$store_item_id."', '".$_SESSION['supplier_id']."' ,'".$receive_id."','' )";
	$trackingResult = mySQL_query($trackingQuery);

$i++;
}
	
	


$table_name = $_SESSION['db_prefix']."_receive_store_lines_temp";
$delete2_query = "DELETE FROM $table_name WHERE  user_name =  '".$_SESSION['username']."' "; 
execsql($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>

