<?php
include('../include/session.php');
date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 


$table_name = $_SESSION['db_prefix']."_tile_receive";
$no_query = "SELECT receive_no FROM $table_name  ORDER BY receive_no DESC  LIMIT 1";
$no_result = mySQL_query($no_query);
$no_num = mysql_num_rows($no_result);
if ($no_num==0) { $receive_no = '1000';
}
else {
$receive_no = mysql_result($no_result, 0, "receive_no") + 1;
}


$table_name = $_SESSION['db_prefix']."_tile_receive";
$order_query = "INSERT INTO $table_name ( receive_no, order_no, cust_id, order_date, receive_date, branch ) VALUES ('".$receive_no."' ,'".$_SESSION['order_no']."' ,'".$_SESSION['cust_id']."', '".$_SESSION['order_date']."', '".$entry_date."', '".$_SESSION['branch']."' ) ";
execsql($order_query);


$table_name = $_SESSION['db_prefix']."_tile_order_lines";
$line_query = "SELECT *  FROM $table_name WHERE order_no = '".$_SESSION['order_no']."' ";
$line_result = mySQL_query($line_query);
$line_num = mysql_num_rows($line_result);
$i=0;
while ($i<$line_num) {
	$quantity = $_GET['quantity_text'.$i];
		
	$store_item_id = mysql_result($line_result, $i, "store_item_id");
	$price = mysql_result($line_result, $i, "price");
	$purchase_price = mysql_result($line_result, $i, "purchase_price");
	$branch = mysql_result($line_result, $i, "branch");
	
	$table_name = $_SESSION['db_prefix']."_tile_receive_lines";
	$tile_query = "INSERT INTO $table_name (receive_no, order_no, store_item_id, quantity, price, purchase_price, branch ) VALUES ('".$receive_no."' , '".$_SESSION['order_no']."' , '".$store_item_id."' , '".$quantity."' , '".$price."' , '".$purchase_price."' , '".$branch."') ";
	execsql($tile_query);
	
	$table_name = $_SESSION['db_prefix']."_tile_order_lines";
	$update_query = "UPDATE $table_name SET rec_quantity =  rec_quantity + '".$quantity."' WHERE order_no = '".$_SESSION['order_no']."' AND store_item_id = '".$store_item_id."' ";
execsql($update_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 = 'Radstone', quantity = '".$total_quantity."' , lastModDate = '".$_SESSION['receive_date']."', price = '".$price."', purchase_price = '".$purchase_price."'  WHERE store_item_id = '".$store_item_id."'";
	$stockResult = mySQL_query($stockQuery);
		
	$table_name3 = $_SESSION['db_prefix']."_item_history";
	$historyQuery = "INSERT INTO $table_name3 (branch, store_item_id, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$_SESSION['branch']."', '".$store_item_id."', '20', '".$receive_no."','".$quantity."', '".$total_quantity."','".$entry_date."','".$entry_date."', '".$_SESSION['username']."')";
	execsql($historyQuery);
				
	$i++;
}

	$table_name = $_SESSION['db_prefix']."_tile_order_lines";
	$status_query = "SELECT  order_no FROM $table_name WHERE order_no = '".$_SESSION['order_no']."' AND (quantity-rec_quantity) <> 0 ";
	$status_result = mySQL_query($status_query);
	$status_num = mysql_num_rows($status_result);
	
	if ($status_num>0) {
		  $status = 'part received';
	}
	else {
		$status = 'received';
	}
		
	$table_name = $_SESSION['db_prefix']."_tile_orders";
	$update_query = "UPDATE $table_name SET status = '".$status."', receive_date = '".$entry_date."' WHERE order_no = '".$_SESSION['order_no']."' ";
	execsql($update_query);
	
	$table_name = $_SESSION['db_prefix']."_tile_receive";
	$update_query = "UPDATE $table_name SET status = '".$status."', receive_date = '".$entry_date."' WHERE order_no = '".$_SESSION['order_no']."' ";
	execsql($update_query);
	
	$table_name = $_SESSION['db_prefix']."_tracking_store";
	$trackingQuery = "INSERT INTO $table_name (entry_date, branch, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."', '".$_SESSION['branch']."', '".$_SESSION['username']."', '12', '".$receive_no."' ,'".$_SESSION['order_no']."', '".$_SESSION['branch']."', '".$_SESSION['cust_id']."')";
	$trackingResult = mySQL_query($trackingQuery);


if (isset($_GET['finish_button'])) {
		$action = 'tile_receive_display.php?receive_no='.$receive_no; 
		}
if (isset($_GET['new_button'])) {
		$action = 'tile_receive_list.php?date=&branch=';  
		}
?>


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