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

$order_no = $_GET['order_no'];
$cust_id = $_GET['cust_id'];
$branch = $_GET['branch'];
$direct = $_GET['direct'];
$pallets = $_GET['pallets_text'];
$pallets_price = $_GET['pallets_price_text'];

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


$table_name = $_SESSION['db_prefix']."_tile_deliver";
$deliver_query = "INSERT INTO $table_name ( delivery_no, order_no, cust_id, deliver_date, branch, status, direct, pallets, pallets_price ) VALUES ('".$delivery_no."', '".$order_no."' ,'".$cust_id."', '".$entry_date."', '".$branch."', 'delivered', '".$direct."', '".$pallets."', '".$pallets_price."' ) ";
execsql($deliver_query);

$table_name = $_SESSION['db_prefix']."_tile_orders";
$pallets_query = "UPDATE $table_name SET del_pallets = del_pallets + '".$pallets."' WHERE order_no = '".$order_no."' ";
execsql($pallets_query);

$table_name = $_SESSION['db_prefix']."_pallets";
	$pallets_recording_query = "INSERT INTO $table_name (del_date, order_no, delivery_no, quantity, branch) VALUES ('".$entry_date."', '".$order_no."', '".$delivery_no."', '".$pallets."', '".$branch."'   ) ";
execsql($pallets_recording_query);

$table_name = $_SESSION['db_prefix']."_tile_orders";
$update_query = "UPDATE $table_name SET status = 'delivered' WHERE order_no = '".$order_no."' ";
execsql($update_query);

$table_name = $_SESSION['db_prefix']."_tile_order_lines";
$line_query = "SELECT *  FROM $table_name WHERE order_no = '".$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_order_lines";
	$update_query = "UPDATE $table_name SET del_quantity = del_quantity + '".$quantity."'  WHERE order_no = '".$order_no."' AND store_item_id = '".$store_item_id."' ";
	execsql($update_query);
	
	$table_name = $_SESSION['db_prefix']."_tile_deliver_lines";
	$tile_query = "INSERT INTO $table_name (delivery_no, order_no, store_item_id, quantity, price, purchase_price, branch ) VALUES ('".$delivery_no."' ,'".$order_no."' , '".$store_item_id."' , '".$quantity."' , '".$price."' , '".$purchase_price."' , '".$branch."') ";
	execsql($tile_query);
	
	
if ($direct=='no') {
   $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 ('".$branch."', '".$store_item_id."', '21', '".$delivery_no."','".$quantity."', '".$total_quantity."','".$entry_date."','".$entry_date."', '".$_SESSION['username']."')";
	execsql($historyQuery);
}
	
	$i++;
}

$table_name = $_SESSION['db_prefix']."_tile_order_lines";
$complete_query = "SELECT order_line_id FROM $table_name WHERE (quantity-del_quantity)>0 AND order_no = '".$order_no."'";
$complete_result = mySQL_query($complete_query);
$complete_num = mysql_num_rows($complete_result);
if ($complete_num == 0) {
	    $table_name = $_SESSION['db_prefix']."_tile_orders";
		$deliver_query = "UPDATE $table_name SET status = 'delivered' WHERE order_no = '".$order_no."' ";
		execsql($deliver_query);
}
else {
	$table_name = $_SESSION['db_prefix']."_tile_orders";
	$deliver_query = "UPDATE $table_name SET status = 'part delivered' WHERE order_no = '".$order_no."' ";
	execsql($deliver_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."', '".$branch."', '".$_SESSION['username']."', '13', '".$delivery_no."' ,'".$order_no."', '".$branch."', '".$cust_id."')";
	$trackingResult = mySQL_query($trackingQuery);
?>


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