From c72d0dd0d4613ac92882c4cfe30d952fd33bedc2 Mon Sep 17 00:00:00 2001
From: daniel <daniel.barbero@artica.es>
Date: Tue, 11 Dec 2018 10:00:05 +0100
Subject: [PATCH] fixed error status agent in custom field view

Former-commit-id: 3516b02e27b28c856b665bc896d076867827f85a
---
 .../include/ajax/custom_fields.php            | 28 +++++--------------
 .../include/functions_custom_fields.php       | 21 +++++++++++++-
 2 files changed, 27 insertions(+), 22 deletions(-)

diff --git a/pandora_console/include/ajax/custom_fields.php b/pandora_console/include/ajax/custom_fields.php
index f57d7cd48e..1d83d0e78e 100644
--- a/pandora_console/include/ajax/custom_fields.php
+++ b/pandora_console/include/ajax/custom_fields.php
@@ -73,6 +73,7 @@ if($build_table_custom_fields){
 		id_server int(10),
 		id_agent int(10),
 		name_custom_fields varchar(2048),
+		`status` int(2),
 		KEY `data_index_temp_1` (`id_server`, `id_agent`)
 	)";
 	db_process_sql($table_temporary);
@@ -80,7 +81,7 @@ if($build_table_custom_fields){
 	//insert values array in table temporary
 	$values_insert = array();
 	foreach ($indexed_descriptions as $key => $value) {
-		$values_insert[] = "(".$value['id_server'].", ".$value['id_agente'].", '".$value['description']."')";
+		$values_insert[] = "(".$value['id_server'].", ".$value['id_agente'].", '".$value['description']."', ".$value['status'].")";
 	}
 	$values_insert_implode = implode(",", $values_insert);
 	$query_insert ="INSERT INTO temp_custom_fields VALUES ". $values_insert_implode;
@@ -104,25 +105,7 @@ if($build_table_custom_fields){
 			tma.direccion,
 			tma.server_name,
 			temp.name_custom_fields,
-			(CASE
-				WHEN tma.critical_count > 0
-					THEN 1
-				WHEN tma.critical_count = 0
-					AND tma.warning_count > 0
-					THEN 2
-				WHEN tma.critical_count = 0
-					AND tma.warning_count = 0
-					AND tma.unknown_count > 0
-					THEN 3
-				WHEN tma.critical_count = 0
-					AND tma.warning_count = 0
-					AND tma.unknown_count = 0
-					AND tma.notinit_count <> tma.total_count
-					THEN 0
-				WHEN tma.total_count = tma.notinit_count
-					THEN 5
-				ELSE 0
-			END) AS `status`
+			temp.status
 		FROM tmetaconsole_agent tma
 		INNER JOIN temp_custom_fields temp
 			ON temp.id_agent = tma.id_tagente
@@ -413,7 +396,10 @@ if($build_table_child_custom_fields){
 		}
 	}
 
-	$status_agent = agents_get_status($id_agent, true);
+	//status agents from tagente
+	$sql_info_agents = "SELECT * fROM tagente WHERE id_agente =" . $id_agent;
+	$info_agents = db_get_row_sql($sql_info_agents);
+	$status_agent = agents_get_status_from_counts($info_agents);
 
 	if (is_metaconsole()) {
 			metaconsole_restore_db();
diff --git a/pandora_console/include/functions_custom_fields.php b/pandora_console/include/functions_custom_fields.php
index 404fd2cfbf..c182e76479 100644
--- a/pandora_console/include/functions_custom_fields.php
+++ b/pandora_console/include/functions_custom_fields.php
@@ -451,7 +451,26 @@ function agent_counters_custom_fields($filters){
 				$query_data = sprintf("SELECT
 						tcd.description,
 						ta.id_agente,
-						%d AS id_server
+						%d AS id_server,
+						(CASE
+							WHEN ta.critical_count > 0
+								THEN 1
+							WHEN ta.critical_count = 0
+								AND ta.warning_count > 0
+								THEN 2
+							WHEN ta.critical_count = 0
+								AND ta.warning_count = 0
+								AND ta.unknown_count > 0
+								THEN 3
+							WHEN ta.critical_count = 0
+								AND ta.warning_count = 0
+								AND ta.unknown_count = 0
+								AND ta.notinit_count <> ta.total_count
+								THEN 0
+							WHEN ta.total_count = ta.notinit_count
+								THEN 5
+							ELSE 0
+						END) AS `status`
 					FROM tagente ta
 					LEFT JOIN tagent_secondary_group tasg
 						ON ta.id_agente = tasg.id_agent