<?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'];
//$pallets_price = $_GET['pallets_price'];
if (isset($_GET['pallets_text'])) {$pallets = $_GET['pallets_text'];} else {$pallets = 0; }
if (isset($_GET['pallets_price'])) {$pallets_price = $_GET['pallets_price'];} else {$pallets_price = 0; } 

$table_name = $_SESSION['db_prefix']."_deliver_product";
$no_query = "SELECT delivery_no FROM $table_name  ORDER BY delivery_no DESC  LIMIT 1";
$no_result = $con->query($no_query);
$no_num = mysqli_num_rows($no_result);
if ($no_num==0) { $delivery_no = '1000';
}
else {
	$row = mysqli_fetch_assoc($no_result);
	$delivery_no = $row['delivery_no'] + 1;
}

$table_name = $_SESSION['db_prefix']."_deliver_product";
$deliver_query = "INSERT INTO $table_name (delivery_no, order_no, order_reference, cust_id, del_col, deliver_date, delivery_time,  pallets, pallets_price, 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."', '".$pallets_price."', '".$_SESSION['delivery_address']."', '".$_SESSION['driver']."', '".$_SESSION['vehicle']."' ) ";
$deliver_result = $con->query($deliver_query);

$table_name = $_SESSION['db_prefix']."_deliver_product_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 = $con->query($quantity_query);
$quantity_num = mysqli_num_rows($quantity_result);
if ( $quantity_num> 0) {
		$row = mysqli_fetch_assoc($quantity_result);
		$a9 = $row['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']."' ";
$pallets_result = $con->query($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."' ) ";
$pallets_result = $con->query($pallets_query);

$table_name = $_SESSION['db_prefix']."_store_items";
$stock_query = "UPDATE $table_name SET quantity = quantity-'".$a9."' WHERE store_item_id = '10000' ";
$stock_result = $con->query($stock_query); 
				
$table_name = $_SESSION['db_prefix']."_store_items";
$stock_query = "SELECT quantity FROM $table_name WHERE store_item_id = '10000' ";
$stock_result = $con->query($stock_query);
$row = mysqli_fetch_assoc($stock_result);
$stock_quantity = $row["quantity"];
				
$table_name2 = $_SESSION['db_prefix']."_item_history";	
$history_query = "INSERT INTO $table_name2 (store_item_id, entry, reference, quantity, total, date, entryDate, userId) VALUES ('10000', '13', '".$delivery_no."', '".$a9."',  '".$stock_quantity."', '".$entry_date."','".$entry_date."','".$_SESSION['username']."')";
$history_result = $con->query($history_query);


$table_name3 = $_SESSION['db_prefix']."_deliver_product_lines_temp";
$items_query = "SELECT * FROM $table_name3 WHERE order_reference = '".$_SESSION['order_reference']."' AND  user_name =  '".$_SESSION['username']."' ";
$items_result = $con->query($items_query);
$items_num = mysqli_num_rows($items_result);


$stock_alert = 'no';
$alert_array = array();
$alert2_array = array();
$alert_text = '';
while($row = $items_result->fetch_assoc()) {
	$product_store_line_id = $row["product_store_line_id"];
	$product_item_id = $row["product_item_id"];
	$quantity = $row["quantity"];
    $batch_code = $row["batch_code"];
	$price = $row["price"];

if ($quantity>0) { 
   $table_name4 = $_SESSION['db_prefix']."_deliver_product_lines";  
   $insert_item_query = "INSERT INTO $table_name4 (delivery_no, deliver_date, cust_id, order_reference, product_item_id, quantity, batch_code, price) VALUES ('".$delivery_no."', '".$_SESSION['deliver_date']."', '".$_SESSION['cust_id']."', '".$_SESSION['order_reference']."',   '".$product_item_id."', '".$quantity."', '".$batch_code."', '".$price."') ";
$insert_item_result = $con->query($insert_item_query);

   $table_name2 = $_SESSION['db_prefix']."_product_store";
   $stock_total_query = "SELECT item_name, quantity, minimumStock FROM $table_name2 WHERE product_item_id = '".$product_item_id."'";
   $stock_total_result = $con->query($stock_total_query);
   $row = mysqli_fetch_assoc($stock_total_result);
   $total_quantity = $row['quantity'];
   $min_stock = $row['minimumStock'];
   $product_item_name = $row['item_name'];

   $total_quantity = $total_quantity - $quantity;
   
   if ($min_stock > $total_quantity) {
	$stock_alert = 'yes';
	$alert_array[] = $product_item_id;
	$alert2_array[] = $total_quantity;
	
	$alert_text = $alert_text.'The stock quantity for <b>'.$product_item_name.'</b> is below the minimum stock of <b>'.$min_stock.'</b>.<br>The present stock quantity is now <b>'.$total_quantity.'</b>.<br><br>';
	}
   
       
   $stock_query = "UPDATE $table_name2 SET quantity = '".$total_quantity."' , lastModDate = '".$_SESSION['order_date']."'  WHERE product_item_id = '".$product_item_id."'";
   $stock_result = $con->query($stock_query);
		
	$table_name3 = $_SESSION['db_prefix']."_product_history";
	$history_query = "INSERT INTO $table_name3 (product_item_id, entry, reference, reference_2, quantity, total, date, entryDate, userId) VALUES ('".$product_item_id."', '13', '".$delivery_no."', '".$batch_code."', '".$quantity."', '".$total_quantity."','".$entry_date."','".$entry_date."', '".$_SESSION['username']."')";
	$history_result = $con->query($history_query);
	
	$table_name = $_SESSION['db_prefix']."_product_store_lines";
	$product_stock_query = "UPDATE $table_name SET quantity_out = quantity_out + '".$quantity."', delivery_note_no = CONCAT(delivery_note_no, '  ".$delivery_no."')  WHERE product_store_line_id = '".$product_store_line_id."' ";
	$product_stock_result = $con->query($product_stock_query);
	
}

$table_name = $_SESSION['db_prefix']."_order_lines";
$update_query = "UPDATE $table_name SET delivered = delivered + '".$quantity."'  WHERE order_no = '".$_SESSION['order_no']."' AND product_item_id = '".$product_item_id."' ";
$update_result = $con->query($update_query);

}


$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 = $con->query($complete_query);
$complete_num = mysqli_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']."' ";
		$deliver_result = $con->query($deliver_query);
}
else {
	$table_name = $_SESSION['db_prefix']."_orders";
	$deliver_query = "UPDATE $table_name SET delivered = 'part' WHERE order_no = '".$_SESSION['order_no']."' ";
	$deliver_result = $con->query($deliver_query);
}

$table_name = $_SESSION['db_prefix']."_tracking_store";
	$tracking_query = "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']."')";
	$tracking_result = $con->query($tracking_query);
	
if ($stock_alert=='yes') {
		include('../include/min_sales_product_alert.php');
}

$table_name = $_SESSION['db_prefix']."_deliver_product_lines_temp";
$delete2_query = "DELETE FROM $table_name WHERE  user_name =  '".$_SESSION['username']."'"; 
$delete2_result = $con->query($delete2_query);


	if (isset($_GET['new_button']) )
	   {
		 $action='deliver_Wiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='deliver_product_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>

