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

$userId = $_SESSION['username'];
date_default_timezone_set('Africa/Gaborone');
$entryDate = 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, purchase_order, supplier_id, value, transport, company ) VALUES ('".$_SESSION['receive_date']."', '".$_SESSION['del_note']."', '".$_SESSION['purchase_order']."', '".$_SESSION['supplier_id']."'  , '".$_SESSION['value']."' , '".$_SESSION['transport']."', '".$_SESSION['company']."' ) ";
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) {
   $category = mysql_result($items_result, $i, "category");
   $sub_category = mysql_result($items_result, $i, "sub_category");
   $store_item_id = mysql_result($items_result, $i, "store_item_id");
   $item_name = mysql_result($items_result, $i, "item_name");
   $quantity = mysql_result($items_result, $i, "quantity");
   $price = mysql_result($items_result, $i, "price");
   $ordered_for = mysql_result($items_result, $i, "order_no");
   $special_order_id = mysql_result($items_result, $i, "special_order_id");


 
   $table_name4 = $_SESSION['db_prefix']."_receive_store_lines";  
   $insert_item_query = "INSERT INTO $table_name4 (receive_id, category, sub_category, store_item_id, item_name, order_no, quantity, price ) VALUES ('".$receive_id."', '".$category."', '".$sub_category."', '".$store_item_id."', '".$item_name."', '".$ordered_for."', '".$quantity."', '".$price."' ) ";
execsql($insert_item_query);

if ($store_item_id > 0 ) { 
     
   $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']."', 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."', '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_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '71', '".$store_item_id."', '".$_SESSION['supplier_id']."' ,'".$receive_id."','".$_SESSION['del_note']."' )";
	$trackingResult = mySQL_query($trackingQuery);
}
else {
			
	$increment_query = 'ALTER TABLE man_store_items_special AUTO_INCREMENT=100001';
	execsql($increment_query);
	
	$table_name = $_SESSION['db_prefix']."_store_items_special";
	$special_query = "INSERT INTO $table_name (item_name, supplier_id, quantity, price, special_order_id, ordered_for, receive_date, receive_id ) VALUES ('".$item_name."', '".$_SESSION['supplier_id']."', '".$quantity."', '".$price."',  '".$special_order_id."', '".$ordered_for."', '".$_SESSION['receive_date']."', '".$receive_id."') ";
	execsql($special_query);
	
	$table_name = $_SESSION['db_prefix']."_special_orders";
	$order_query = "UPDATE $table_name SET date_received = '".$_SESSION['receive_date']."', quantity_received = '".$quantity."', price_received = '".$price."' WHERE special_order_id = '".$special_order_id."' ";
	execsql($order_query);
	
	$table_name = $_SESSION['db_prefix']."_store_items_special";
	$special_id_query = "SELECT special_store_item_id  FROM $table_name WHERE special_order_id = '".$special_order_id."' ";
	$special_id_result = mySQL_query($special_id_query);
	$special_id_num = mysql_num_rows($special_id_result);
	$special_id = mysql_result($special_id_result, 0, "special_store_item_id");
	
	$table_name = $_SESSION['db_prefix']."_order_job_lines";
	$update_query = "UPDATE $table_name SET store_item_id = '".$special_id."' WHERE item_name = '".$item_name."' AND job_card_no = '".$ordered_for."' ";
	execsql($update_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 ('".$entryDate."' ,'".$_SESSION['username']."', '79', '".$item_name."', '".$_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_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['purchase_order'] = '';
	 $_SESSION['company'] = '';
	 $_SESSION['value'] = '';
	 $_SESSION['transport'] = '';
	 
	 $_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>

