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

$userId = $_SESSION['username'];
date_default_timezone_set('Africa/Khartoum');
$entryDate = date("Y-m-d H:i:s",time()-3600); 
$reference = "";
$entry = "20";	


$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['del_note']."', '".$_SESSION['supplier_id']."', '".$_SESSION['value_tot']."', '".$_SESSION['transport_tot']."' ) ";
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_name4 = $_SESSION['db_prefix']."_receive_store_lines";
$table_name2 = $_SESSION['db_prefix']."_store_items";
$table_name3 = $_SESSION['db_prefix']."_item_history";
$table_name5 = $_SESSION['db_prefix']."_receive_store_lines_temp";
$table_name6 = $_SESSION['db_prefix']."_tracking_store";

$receive_query = "insert into $table_name4 (receive_id, store_item_id, quantity, price, bulk_price)
    select '".$receive_id."', store_item_id, quantity, price, bulk_price from $table_name5 where  user =  '".$_SESSION['username']."' ";
    execsql($receive_query); 
	
$items_query = "SELECT store_item_id, quantity, price, bulk_price FROM $table_name4 WHERE receive_id = '".$receive_id."' ";
$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");
	$bulk_price = mysql_result($items_result, $i, "bulk_price");
    $price = mysql_result($items_result, $i, "price");
	
    $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']."', bulk_price = '".$bulk_price."' WHERE store_item_id = '".$store_item_id."'";
	$stockResult = mySQL_query($stockQuery);
	
		
	$historyQuery = "INSERT INTO $table_name3 (store_item_id, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$store_item_id."', '20', '".$receive_id."','".$quantity."', '".$total_quantity."','".$_SESSION['receive_date']."','".$entryDate."','".$userId."')";
	execsql($historyQuery);
	
	
	$table_name = $_SESSION['db_prefix']."_tracking_store";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '12', '".$store_item_id."', '".$_SESSION['supplier_id']."' ,'".$receive_id."','".$_SESSION['del_note']."' )";
	$trackingResult = mySQL_query($trackingQuery);
	
	
    $i++;
	
}

$table_name = $_SESSION['db_prefix']."_receive_store_lines_temp";
$delete2_query = "DELETE FROM $table_name WHERE  user =  '".$_SESSION['username']."' "; 
execsql($delete2_query);

	
	if (isset($_POST['new_button']) )
	   {
		 $action='receivingTimberWiz0.php';
	   }
		 
	if (isset($_POST['finish_button']) )
	  {
		$action='receive_note.php?receive_id='.$receive_id ;
	  }

	
	$d = date("d");
	$m = date("m");
	$y = date("Y");
	$modDate=date('Y-m-d');
	$active="1";

	$userId = $_SESSION['username'];
	date_default_timezone_set('Africa/Khartoum');
	$entryDate = date("Y-m-d H:i:s",time()-3600); 
	
	
	$_SESSION['supplier_id'] = '';	
	 $_SESSION['del_note'] = '';	
     $_SESSION['receive_date']  = date('Y-m-d');
	 $_SESSION['receive_id'] = '';
	 $_SESSION['sup_name'] = '';

?>

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

