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

$table_name = $_SESSION['db_prefix']."_tile_deliver_lines_temp";
$quantity_query = "SELECT pallets  FROM $table_name WHERE order_reference = '".$_SESSION['order_reference']."' AND pallets > 0 AND user_name = '".$_SESSION['username']."'"; 
$quantity_result = mySQL_query($quantity_query);
$quantity_num = mysql_num_rows($quantity_result);
if ( $quantity_num> 0) {
		$a9 = mysql_result($quantity_result, 0, "pallets");
}
else { 
		$a9 = '';
}


$table_name = $_SESSION['db_prefix']."_orders";
$pallets_query = "UPDATE $table_name SET del_pallets = del_pallets + '".$a9."' 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, order_reference, delivery_no, quantity ) VALUES ( '".$_SESSION['deliver_date']."', '".$_SESSION['order_no']."', '".$_SESSION['order_reference']."', '".$delivery_no."','".$a9."' ) ";
execsql($pallets_query);

$table_name = $_SESSION['db_prefix']."_pallets_balance";
$pallets_del_query = "INSERT INTO $table_name (entry_reference, entry_type, cust_id, del_qty, del_date ) VALUES ('".$_SESSION['order_reference']."', 'del', '".$_SESSION['cust_id']."', '".$a9."', '".$_SESSION['deliver_date']."'  ) ";
execsql($pallets_del_query);

$table_name = $_SESSION['db_prefix']."_store_items";
$stock_query = "UPDATE $table_name SET quantity = quantity-'".$a9."' WHERE store_item_id = '10001' ";
execsql($stock_query); 
				
$table_name = $_SESSION['db_prefix']."_store_items";
$stock_query = "SELECT quantity FROM $table_name WHERE store_item_id = '10001' ";
$stock_result = mySQL_query($stock_query);
$stock_quantity = mysql_result($stock_result, 0, "quantity"); 
				
$table_name2 = $_SESSION['db_prefix']."_item_history";	
$historyQuery = "INSERT INTO $table_name2 (store_item_id, entry, reference, quantity, total, date, entryDate, userId) VALUES ('10001', '13', '".$delivery_no."', '".$a9."',  '".$stock_quantity."', '".$entry_date."','".$entry_date."','".$_SESSION['username']."')";
$historyResult = mySQL_query($historyQuery);


$table_name3 = $_SESSION['db_prefix']."_tile_deliver_lines_temp";
$items_query = "SELECT * FROM $table_name3 WHERE order_reference = '".$_SESSION['order_reference']."' 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");
   $replace_qty = mysql_result($items_result, $i, "replace_qty");
   $batch1 = mysql_result($items_result, $i, "batch_1");
   $batch2 = mysql_result($items_result, $i, "batch_2");
   
   

if ($quantity>0) { 
   $table_name4 = $_SESSION['db_prefix']."_tile_deliver_lines";  
   $insert_item_query = "INSERT INTO $table_name4 (delivery_no, deliver_date, cust_id, order_reference, store_item_id, quantity, replace_qty, batch_1, batch_2) VALUES ('".$delivery_no."', '".$_SESSION['deliver_date']."', '".$_SESSION['cust_id']."', '".$_SESSION['order_reference']."',   '".$store_item_id."', '".$quantity."', '".$replace_qty."', '".$batch1."', '".$batch2."') ";
execsql($insert_item_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 - $replace_qty;
   
   $batches = $batch1.'  '.$batch2;
      
   $stockQuery = "UPDATE $table_name2 SET quantity = '".$total_quantity."' , lastModDate = '".$_SESSION['order_date']."'  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, reference_2, quantity, total, date, entryDate, userId) VALUES ('".$store_item_id."', '13', '".$delivery_no."', '".$batches."', '".($quantity+$replace_qty)."', '".$total_quantity."','".$entry_date."','".$entry_date."', '".$_SESSION['username']."')";
	execsql($historyQuery);

}

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


$i++;
}


$table_name = $_SESSION['db_prefix']."_order_lines";
$complete_query = "SELECT order_line_id FROM $table_name WHERE (quantity-delivered)>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']."_orders";
		$deliver_query = "UPDATE $table_name SET delivered = 'yes' WHERE order_no = '".$_SESSION['order_no']."' ";
		execsql($deliver_query);
}
else {
	$table_name = $_SESSION['db_prefix']."_orders";
	$deliver_query = "UPDATE $table_name SET delivered = 'part' WHERE order_no = '".$_SESSION['order_no']."' ";
	execsql($deliver_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 ('".$entry_date."', '".$_SESSION['username']."', '13', '".$delivery_no."' ,'".$_SESSION['order_no']."', '".$_SESSION['order_reference']."', '".$_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['store_item_id'] = '';
$_SESSION['store_item_name'] = '';
	 
	 
$_SESSION['cust_id']='';
$_SESSION['order_date']='';
$_SESSION['direct']='';
$_SESSION['cust_name'] = ''; 

	
?>

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

