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


$pallets = $_GET['pallets_text'];

$table_name = $_SESSION['db_prefix']."_tile_orders";
$pallets_price_query = "SELECT pallets_price FROM $table_name WHERE order_no = '".$_SESSION['order_no']."' ";
$pallets_price_result = mySQL_query($pallets_price_query);
$pallets_price = mysql_result($pallets_price_result, 0, "pallets_price");


$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, direct, deliver_date, delivery_time, branch, pallets, pallets_price, delivery_address, delivered_by, vehicle_reg ) VALUES ('".$delivery_no."', '".$_SESSION['order_no']."', '".$_SESSION['cust_id']."', '".$_SESSION['direct']."', '".$_SESSION['deliver_date']."', '".$_SESSION['hour'].":".$_SESSION['min']."', '".$_SESSION['branch']."', '".$pallets."', '".$pallets_price."', '".$_SESSION['delivery_address']."', '".$_SESSION['driver']."', '".$_SESSION['vehicle']."' ) ";
execsql($deliver_query);


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

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


$table_name3 = $_SESSION['db_prefix']."_tile_deliver_lines_temp";
$items_query = "SELECT * FROM $table_name3 WHERE order_no = '".$_SESSION['order_no']."' AND  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");
   $purchase_price = mysql_result($items_result, $i, "purchase_price");

if ($quantity>0) { 
   $table_name4 = $_SESSION['db_prefix']."_tile_deliver_lines";  
   $insert_item_query = "INSERT INTO $table_name4 (delivery_no, order_no, store_item_id, quantity, price, purchase_price, branch ) VALUES ('".$delivery_no."', '".$_SESSION['order_no']."',   '".$store_item_id."', '".$quantity."', '".$price."', '".$purchase_price."', '".$_SESSION['branch']."' ) ";
execsql($insert_item_query);

if ($_SESSION['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']."'  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."', '21', '".$delivery_no."','".$quantity."', '".$total_quantity."','".$entry_date."','".$entry_date."', '".$_SESSION['username']."')";
	execsql($historyQuery);
}

}

$table_name = $_SESSION['db_prefix']."_tile_order_lines";
$update_query = "UPDATE $table_name SET del_quantity = del_quantity + '".$quantity."'  WHERE order_no = '".$_SESSION['order_no']."' AND store_item_id = '".$store_item_id."' ";
execsql($update_query);




$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 = '".$_SESSION['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 = '".$_SESSION['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 = '".$_SESSION['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."', '".$_SESSION['branch']."', '".$_SESSION['username']."', '13', '".$delivery_no."' ,'".$_SESSION['order_no']."', '".$_SESSION['branch']."', '".$_SESSION['cust_id']."')";
	$trackingResult = mySQL_query($trackingQuery);

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


	if (isset($_GET['new_button']) )
	   {
		 $action='deliver_tilesWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='tile_deliver_display.php?delivery_no='.$delivery_no ;
	  }
	
$_SESSION['delivery_address']='';
$_SESSION['driver']='';
$_SESSION['vehicle']='';
$_SESSION['hour']='';
$_SESSION['min']='';
	 
$_SESSION['deliver_date'] = '';
 
$_SESSION['order_no'] = '';
$_SESSION['branch'] = '';
	 
$_SESSION['store_item_id'] = '';
$_SESSION['store_item_name'] = '';
	 
	 
$_SESSION['cust_id']='';
$_SESSION['receive_date']='';
$_SESSION['direct']='';
$_SESSION['cust_name'] = ''; 

	
?>

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

