From 1fd40447e12e4855878a41ce6c217b89bc87d6bb Mon Sep 17 00:00:00 2001
From: manuel <manuel.montes@artica.es>
Date: Tue, 2 Apr 2019 16:24:29 +0200
Subject: [PATCH 1/2] Custom fields in availability report type

Former-commit-id: 1f04e5728cf7d0ed5079c16488493188379d536c
---
 pandora_console/extras/mr/27.sql              |  12 ++
 .../pandoradb_migrate_6.0_to_7.0.mysql.sql    |  12 ++
 .../reporting_builder.item_editor.php         | 199 ++++++++++++++++--
 .../godmode/reporting/reporting_builder.php   |  13 ++
 .../include/functions_reporting_html.php      | 193 +++++++++++++----
 5 files changed, 370 insertions(+), 59 deletions(-)

diff --git a/pandora_console/extras/mr/27.sql b/pandora_console/extras/mr/27.sql
index f85064c9eb..a98a94f6d1 100644
--- a/pandora_console/extras/mr/27.sql
+++ b/pandora_console/extras/mr/27.sql
@@ -2,5 +2,17 @@ START TRANSACTION;
 
 UPDATE tuser_task SET parameters = 'a:5:{i:0;a:6:{s:11:\"description\";s:28:\"Report pending to be created\";s:5:\"table\";s:7:\"treport\";s:8:\"field_id\";s:9:\"id_report\";s:10:\"field_name\";s:4:\"name\";s:4:\"type\";s:3:\"int\";s:9:\"acl_group\";s:8:\"id_group\";}i:1;a:2:{s:11:\"description\";s:46:\"Send to email addresses (separated by a comma)\";s:4:\"type\";s:4:\"text\";}i:2;a:2:{s:11:\"description\";s:7:\"Subject\";s:8:\"optional\";i:1;}i:3;a:3:{s:11:\"description\";s:7:\"Message\";s:4:\"type\";s:4:\"text\";s:8:\"optional\";i:1;}i:4;a:2:{s:11:\"description\";s:11:\"Report Type\";s:4:\"type\";s:11:\"report_type\";}}' where function_name = "cron_task_generate_report";
 
+ALTER TABLE `treport_content` ADD COLUMN `total_time` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `time_failed` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `time_in_ok_status` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `time_in_unknown_status` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `time_of_not_initialized_module` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `time_of_downtime` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `total_checks` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `checks_failed` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `checks_in_ok_status` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `unknown_checks` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `agent_max_value` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `agent_min_value` TINYINT(1) DEFAULT '1';
 
 COMMIT;
\ No newline at end of file
diff --git a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql
index 323e7ee0d8..515302d463 100644
--- a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql
+++ b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql
@@ -1379,6 +1379,18 @@ ALTER TABLE treport_content ADD COLUMN `hide_no_data` tinyint(1) default '0';
 ALTER TABLE treport_content ADD COLUMN `recursion` tinyint(1) default NULL;
 ALTER TABLE treport_content ADD COLUMN `show_extended_events` tinyint(1) default '0';
 UPDATE `treport_content` SET type="netflow_summary" WHERE type="netflow_pie" OR type="netflow_statistics";
+ALTER TABLE `treport_content` ADD COLUMN `total_time` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `time_failed` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `time_in_ok_status` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `time_in_unknown_status` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `time_of_not_initialized_module` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `time_of_downtime` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `total_checks` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `checks_failed` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `checks_in_ok_status` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `unknown_checks` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `agent_max_value` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content` ADD COLUMN `agent_min_value` TINYINT(1) DEFAULT '1';
 
 -- ---------------------------------------------------------------------
 -- Table `tmodule_relationship`
diff --git a/pandora_console/godmode/reporting/reporting_builder.item_editor.php b/pandora_console/godmode/reporting/reporting_builder.item_editor.php
index 62d58c9261..85b5ee128d 100755
--- a/pandora_console/godmode/reporting/reporting_builder.item_editor.php
+++ b/pandora_console/godmode/reporting/reporting_builder.item_editor.php
@@ -119,6 +119,20 @@ $visual_format = 0;
 // Others
 $filter_search = '';
 
+// Added for select fields.
+$total_time = true;
+$time_failed = true;
+$time_in_ok_status = true;
+$time_in_unknown_status = true;
+$time_of_not_initialized_module = true;
+$time_of_downtime = true;
+$total_checks = true;
+$checks_failed = true;
+$checks_in_ok_status = true;
+$unknown_checks = true;
+$agent_max_value = true;
+$agent_min_value = true;
+
 switch ($action) {
     case 'new':
         $actionParameter = 'save';
@@ -501,6 +515,18 @@ switch ($action) {
                     $sunday = $item['sunday'];
                     $time_from = $item['time_from'];
                     $time_to = $item['time_to'];
+                    $total_time = $item['total_time'];
+                    $time_failed = $item['time_failed'];
+                    $time_in_ok_status = $item['time_in_ok_status'];
+                    $time_in_unknown_status = $item['time_in_unknown_status'];
+                    $time_of_not_initialized_module = $item['time_of_not_initialized_module'];
+                    $time_of_downtime = $item['time_of_downtime'];
+                    $total_checks = $item['total_checks'];
+                    $checks_failed = $item['checks_failed'];
+                    $checks_in_ok_status = $item['checks_in_ok_status'];
+                    $unknown_checks = $item['unknown_checks'];
+                    $agent_max_value = $item['agent_max_value'];
+                    $agent_min_value = $item['agent_min_value'];
                 break;
 
                 case 'group_report':
@@ -1597,20 +1623,6 @@ $class = 'databox filters';
                     REPORT_EXCEPTION_CONDITION_NOT_OK     => __('Not OK'),
                 ];
                 html_print_select($list_exception_condition, 'exception_condition', $exception_condition);
-
-
-                /*
-                    echo ;
-                    html_print_radio_button ('radiobutton_exception_condition', 0, '', $exception_condition);
-                    echo __('>=');
-                    html_print_radio_button ('radiobutton_exception_condition', 1, '', $exception_condition);
-                    echo __('<');
-                    html_print_radio_button ('radiobutton_exception_condition', 2, '', $exception_condition);
-                    echo __('OK');
-                    html_print_radio_button ('radiobutton_exception_condition', 3, '', $exception_condition);
-                    echo __('Not OK');
-                    html_print_radio_button ('radiobutton_exception_condition', 4, '', $exception_condition);
-                */
                 ?>
             </td>
         </tr>
@@ -1620,6 +1632,65 @@ $class = 'databox filters';
             <td><?php html_print_select($show_graph_options, 'combo_graph_options', $show_graph); ?></td>
             
         </tr>
+        <tr id="row_select_fields" style="" class="datos">
+        <td style="font-weight:bold;margin-right:150px;">
+            <?php
+            echo __('Select fields to show');
+            ?>
+            </td>
+            <td>
+            <table border="0">
+            <td>
+            <p style="margin-right:30px;">
+                <?php
+                echo __('Total time');
+                html_print_checkbox('total_time', 1, $total_time);
+                ?>
+             </p>
+            </td>
+            <td>
+            <p style="margin-right:30px;">
+                <?php
+                echo __('Time failed');
+                html_print_checkbox('time_failed', 1, $time_failed);
+                ?>
+                </p>
+            </td>
+            <td>
+            <p style="margin-right:30px;">
+                <?php
+                echo __('Time in OK status');
+                html_print_checkbox('time_in_ok_status', 1, $time_in_ok_status);
+                ?>
+                </p>
+            </td>
+            <td>
+            <p style="margin-right:30px;">
+                <?php
+                echo __('Time in unknown status');
+                html_print_checkbox('time_in_unknown_status', 1, $time_in_unknown_status);
+                ?>
+                </p>
+            </td>
+            <td>
+            <p style="margin-right:30px;">
+                <?php
+                echo __('Time of not initialized module');
+                html_print_checkbox('time_of_not_initialized_module', 1, $time_of_not_initialized_module);
+                ?>
+                </p>
+            </td>
+            <td>
+            <p style="margin-right:30px;">
+                <?php
+                echo __('Time of downtime');
+                html_print_checkbox('time_of_downtime', 1, $time_of_downtime);
+                ?>
+                </p>
+            </td>
+            </table>
+            </td>
+        </tr>
         
         <tr id="row_show_address_agent" style="" class="datos">
             <td style="font-weight:bold;">
@@ -1629,7 +1700,7 @@ $class = 'databox filters';
             </td>
             <td>
                 <?php
-                html_print_checkbox(
+                html_print_checkbox_switch(
                     'checkbox_show_address_agent',
                     1,
                     $show_address_agent
@@ -1637,12 +1708,13 @@ $class = 'databox filters';
                 ?>
             </td>
         </tr>
+       
         
         <tr id="row_show_resume" style="" class="datos">
             <td style="font-weight:bold;"><?php echo __('Show resume').ui_print_help_tip(__('Show a summary chart with max, min and average number of total modules at the end of the report and Checks.'), true); ?></td>
             <td>
                 <?php
-                html_print_checkbox(
+                html_print_checkbox_switch(
                     'checkbox_show_resume',
                     1,
                     $show_resume
@@ -1650,6 +1722,78 @@ $class = 'databox filters';
                 ?>
             </td>
         </tr>
+        <tr id="row_select_fields2" style="" class="datos">
+        <td style="font-weight:bold;margin-right:150px;">
+            <?php
+            echo __('<p style= "margin-left:15px;">Select fields to show</p>');
+            ?>
+            </td>
+            <td>
+            <table border="0">
+            <td>
+            <p style="margin-right:30px;">
+                <?php
+                echo __('Total checks');
+                html_print_checkbox('total_checks', 1, $total_checks);
+                ?>
+                </p>
+            </td>
+            <td>
+            <p style="margin-right:30px;">
+                <?php
+                echo __('Checks failed');
+                html_print_checkbox('checks_failed', 1, $checks_failed);
+                ?>
+             </p>
+            </td>
+            <td>
+            <p style="margin-right:30px;">
+                <?php
+                echo __('Checks in OK status');
+                html_print_checkbox('checks_in_ok_status', 1, $checks_in_ok_status);
+                ?>
+                </p>
+            </td>
+            <td>
+            <p style="margin-right:30px;">
+                <?php
+                echo __('Unknown checks');
+                html_print_checkbox('unknown_checks', 1, $unknown_checks);
+                ?>
+                </p>
+            </td>
+            <td>
+            </table>
+            </td>
+        </tr>
+        <tr id="row_select_fields3" style="" class="datos">
+        <td style="font-weight:bold;margin-right:150px;">
+            <?php
+            echo __('<p style= "margin-left:15px;">Select fields to show</p>');
+            ?>
+            </td>
+            <td>
+            <table border="0">
+            <td>
+            <p style="margin-right:30px;">
+                <?php
+                echo __('Agent max value');
+                html_print_checkbox('agent_max_value', 1, $agent_max_value);
+                ?>
+             </p>
+            </td>   
+            <td>
+            <p style="margin-right:30px;">
+                <?php
+                echo __('Agent min values');
+                html_print_checkbox('agent_min_value', 1, $agent_min_value);
+                ?>
+                </p>
+            </td>
+            <td>
+            </table>
+            </td>
+        </tr>
 
         <tr id="row_show_summary_group" style="" class="datos">
             <td style="font-weight:bold;"><?php echo __('Show Summary group'); ?></td>
@@ -2625,6 +2769,17 @@ $(document).ready (function () {
         }
     
     });
+
+    $("#checkbox-checkbox_show_resume").change(function(){
+        if($(this).is(":checked")){
+            $("#row_select_fields2").show();
+            $("#row_select_fields3").show();
+        }
+        else{
+            $("#row_select_fields2").hide();
+            $("#row_select_fields3").hide();
+        }
+    });
     
 });
 
@@ -3139,6 +3294,9 @@ function chooseType() {
     $("#row_event_type").hide();
     $("#row_event_status").hide();
     $("#row_source").hide();
+    $('#row_select_fields').hide();
+    $("#row_select_fields2").hide();
+    $("#row_select_fields3").hide();
     
     // SLA list default state
     $("#sla_list").hide();
@@ -3547,6 +3705,15 @@ function chooseType() {
             $("#row_show_resume").show();
             $("#row_working_time").show();
             $('#row_hide_notinit_agents').show();
+            $('#row_select_fields').show();
+             if($("#checkbox-checkbox_show_resume").is(":checked")){
+                $("#row_select_fields2").show();
+                 $("#row_select_fields3").show();
+             }
+             else{
+                $("#row_select_fields2").hide();
+                 $("#row_select_fields3").hide();
+             }
             $("#row_historical_db_check").hide();
             break;
         
diff --git a/pandora_console/godmode/reporting/reporting_builder.php b/pandora_console/godmode/reporting/reporting_builder.php
index 3acfee7c88..a8fe0f5f91 100755
--- a/pandora_console/godmode/reporting/reporting_builder.php
+++ b/pandora_console/godmode/reporting/reporting_builder.php
@@ -1282,6 +1282,19 @@ switch ($action) {
                         $values['friday'] = get_parameter('friday', 0);
                         $values['saturday'] = get_parameter('saturday', 0);
                         $values['sunday'] = get_parameter('sunday', 0);
+                        $values['total_time'] = get_parameter('total_time', 0);
+                        $values['time_failed'] = get_parameter('time_failed', 0);
+                        $values['time_in_ok_status'] = get_parameter('time_in_ok_status', 0);
+                        $values['time_in_unknown_status'] = get_parameter('time_in_unknown_status', 0);
+                        $values['time_of_not_initialized_module'] = get_parameter('time_of_not_initialized_module', 0);
+                        $values['time_of_downtime'] = get_parameter('time_of_downtime', 0);
+                        $values['total_checks'] = get_parameter('total_checks', 0);
+                        $values['checks_failed'] = get_parameter('checks_failed', 0);
+                        $values['checks_in_ok_status'] = get_parameter('checks_in_ok_status', 0);
+                        $values['unknown_checks'] = get_parameter('unknown_checks', 0);
+                        $values['agent_max_value'] = get_parameter('agent_max_value', 0);
+                        $values['agent_min_value'] = get_parameter('agent_min_value', 0);
+
                         switch ($config['dbtype']) {
                             case 'mysql':
                             case 'postgresql':
diff --git a/pandora_console/include/functions_reporting_html.php b/pandora_console/include/functions_reporting_html.php
index 7265c721ec..4d9b21e2bb 100644
--- a/pandora_console/include/functions_reporting_html.php
+++ b/pandora_console/include/functions_reporting_html.php
@@ -2907,7 +2907,24 @@ function reporting_html_availability($table, $item, $pdf=0)
         io_safe_output($style),
         true
     );
-
+    $sql = 'SELECT 
+     total_time,
+     time_failed,
+     time_in_ok_status,
+     time_in_unknown_status,
+     time_of_not_initialized_module,
+     time_of_downtime, 
+     total_checks,
+     checks_failed,
+     checks_in_ok_status,
+     unknown_checks,
+     agent_max_value,
+     agent_min_value
+     FROM treport_content 
+     WHERE id_rc ='.$item['id_rc'];
+    $fields = db_get_all_rows_sql(
+        $sql
+    );
     $same_agent_in_resume = '';
 
     global $config;
@@ -2927,32 +2944,62 @@ function reporting_html_availability($table, $item, $pdf=0)
             $table1->head[1] = __('Module');
         }
 
-        $table1->head[2] = __('Total time');
-        $table1->head[3] = __('Time failed');
-        $table1->head[4] = __('Time OK');
-        $table1->head[5] = __('Time Uknown');
-        $table1->head[6] = __('Time Not Init Module');
-        $table1->head[7] = __('Time Downtime');
+        if ($fields[0]['total_time']) {
+            $table1->head[2] = __('Total time');
+        } else {
+            $table1->head[2] = __('');
+        }
+
+        if ($fields[0]['time_failed']) {
+            $table1->head[3] = __('Time failed');
+        } else {
+            $table1->head[3] = __('');
+        }
+
+        if ($fields[0]['time_in_ok_status']) {
+            $table1->head[4] = __('Time OK');
+        } else {
+            $table1->head[4] = __('');
+        }
+
+        if ($fields[0]['time_in_unknown_status']) {
+            $table1->head[5] = __('Time Unknown');
+        } else {
+            $table1->head[5] = __('');
+        }
+
+        if ($fields[0]['time_of_not_initialized_module']) {
+            $table1->head[6] = __('Time Not Init Module');
+        } else {
+            $table1->head[6] = __('');
+        }
+
+        if ($fields[0]['time_of_downtime']) {
+            $table1->head[7] = __('Time Downtime');
+        } else {
+            $table1->head[7] = __('');
+        }
+
         $table1->head[8] = __('% Ok');
 
         $table1->headstyle = [];
         $table1->headstyle[0]  = 'text-align: left';
         $table1->headstyle[1]  = 'text-align: left';
-        $table1->headstyle[2]  = 'text-align: right';
-        $table1->headstyle[3]  = 'text-align: right';
-        $table1->headstyle[4]  = 'text-align: right';
-        $table1->headstyle[5]  = 'text-align: right';
-        $table1->headstyle[6]  = 'text-align: right';
+        $table1->headstyle[2]  = 'text-align: center';
+        $table1->headstyle[3]  = 'text-align: center';
+        $table1->headstyle[4]  = 'text-align: center';
+        $table1->headstyle[5]  = 'text-align: center';
+        $table1->headstyle[6]  = 'text-align: center';
         $table1->headstyle[7]  = 'text-align: right';
         $table1->headstyle[8]  = 'text-align: right';
 
         $table1->style[0]  = 'text-align: left';
         $table1->style[1]  = 'text-align: left';
-        $table1->style[2]  = 'text-align: right';
-        $table1->style[3]  = 'text-align: right';
-        $table1->style[4]  = 'text-align: right';
-        $table1->style[5]  = 'text-align: right';
-        $table1->style[6]  = 'text-align: right';
+        $table1->style[2]  = 'text-align: center';
+        $table1->style[3]  = 'text-align: center';
+        $table1->style[4]  = 'text-align: center';
+        $table1->style[5]  = 'text-align: center';
+        $table1->style[6]  = 'text-align: center';
         $table1->style[7]  = 'text-align: right';
         $table1->style[8]  = 'text-align: right';
 
@@ -2970,10 +3017,29 @@ function reporting_html_availability($table, $item, $pdf=0)
             $table2->head[1] = __('Module');
         }
 
-        $table2->head[2] = __('Total checks');
-        $table2->head[3] = __('Checks failed');
-        $table2->head[4] = __('Checks OK');
-        $table2->head[5] = __('Checks Uknown');
+        if ($fields[0]['total_checks']) {
+            $table2->head[2] = __('Total checks');
+        } else {
+            $table2->head[2] = __('');
+        }
+
+        if ($fields[0]['checks_failed']) {
+            $table2->head[3] = __('Checks failed');
+        } else {
+            $table2->head[3] = __('');
+        }
+
+        if ($fields[0]['checks_in_ok_status']) {
+            $table2->head[4] = __('Checks OK');
+        } else {
+            $table2->head[4] = __('');
+        }
+
+        if ($fields[0]['unknown_checks']) {
+            $table2->head[5] = __('Checks Uknown');
+        } else {
+            $table2->head[5] = __('');
+        }
 
         $table2->headstyle = [];
         $table2->headstyle[0] = 'text-align: left';
@@ -2995,69 +3061,97 @@ function reporting_html_availability($table, $item, $pdf=0)
             $table_row[] = $row['agent'];
             $table_row[] = $row['availability_item'];
 
-            if ($row['time_total'] != 0) {
+            if ($row['time_total'] != 0 && $fields[0]['total_time']) {
                 $table_row[] = human_time_description_raw(
                     $row['time_total'],
                     true
                 );
-            } else {
+            } else if ($row['time_total'] == 0 && $fields[0]['total_time']) {
                 $table_row[] = '--';
-            }
+            } else {
+                $table_row[] = '';
+            };
 
-            if ($row['time_error'] != 0) {
+            if ($row['time_error'] != 0 && $fields[0]['time_failed']) {
                 $table_row[] = human_time_description_raw(
                     $row['time_error'],
                     true
                 );
-            } else {
+            } else if ($row['time_error'] == 0 && $fields[0]['time_failed']) {
                 $table_row[] = '--';
-            }
+            } else {
+                $table_row[] = '';
+            };
 
-            if ($row['time_ok'] != 0) {
+            if ($row['time_ok'] != 0 && $fields[0]['time_in_ok_status']) {
                 $table_row[] = human_time_description_raw(
                     $row['time_ok'],
                     true
                 );
-            } else {
+            } else if ($row['time_ok'] == 0 && $fields[0]['time_in_ok_status']) {
                 $table_row[] = '--';
-            }
+            } else {
+                $table_row[] = '';
+            };
 
-            if ($row['time_unknown'] != 0) {
+            if ($row['time_unknown'] != 0 && $fields[0]['time_in_unknown_status']) {
                 $table_row[] = human_time_description_raw(
                     $row['time_unknown'],
                     true
                 );
-            } else {
+            } else if ($row['time_unknown'] == 0 && $fields[0]['time_in_unknown_status']) {
                 $table_row[] = '--';
-            }
+            } else {
+                $table_row[] = '';
+            };
 
-            if ($row['time_not_init'] != 0) {
+            if ($row['time_not_init'] != 0 && $fields[0]['time_of_not_initialized_module']) {
                 $table_row[] = human_time_description_raw(
                     $row['time_not_init'],
                     true
                 );
-            } else {
+            } else if ($row['time_not_init'] == 0 && $fields[0]['time_of_not_initialized_module']) {
                 $table_row[] = '--';
-            }
+            } else {
+                $table_row[] = '';
+            };
 
-            if ($row['time_downtime'] != 0) {
+            if ($row['time_downtime'] != 0 && $fields[0]['time_of_downtime']) {
                 $table_row[] = human_time_description_raw(
                     $row['time_downtime'],
                     true
                 );
-            } else {
+            } else if ($row['time_downtime'] == 0 && $fields[0]['time_of_downtime']) {
                 $table_row[] = '--';
-            }
+            } else {
+                $table_row[] = '';
+            };
 
             $table_row[] = '<span style="font-size: 1.2em; font-weight:bold;">'.sla_truncate($row['SLA'], $config['graph_precision']).'%</span>';
 
             $table_row2 = [];
             $table_row2[] = $row['agent'];
             $table_row2[] = $row['availability_item'];
-            $table_row2[] = $row['checks_total'];
-            $table_row2[] = $row['checks_error'];
-            $table_row2[] = $row['checks_ok'];
-            $table_row2[] = $row['checks_unknown'];
+            if ($fields[0]['total_checks']) {
+                $table_row2[] = $row['checks_total'];
+            } else {
+                $table_row2[] = '';
+            };
+            if ($fields[0]['checks_failed']) {
+                $table_row2[] = $row['checks_error'];
+            } else {
+                $table_row2[] = '';
+            };
+            if ($fields[0]['checks_in_ok_status']) {
+                $table_row2[] = $row['checks_ok'];
+            } else {
+                $table_row2[] = '';
+            };
+            if ($fields[0]['unknown_checks']) {
+                $table_row2[] = $row['checks_unknown'];
+            } else {
+                $table_row2[] = '';
+            };
 
             $table1->data[] = $table_row;
             $table2->data[] = $table_row2;
@@ -3131,6 +3225,19 @@ function reporting_html_availability($table, $item, $pdf=0)
                 ).'%',
                 'avg'      => '<span style="font-size: 1.2em; font-weight:bold;">'.sla_truncate($item['resume']['avg'], $config['graph_precision']).'%</span>',
             ];
+            if ($fields[0]['agent_max_value'] == 0) {
+                $table1->head['max_text'] = '';
+                $table1->data[0]['max_text'] = '';
+                $table1->head['max'] = '';
+                $table1->data[0]['max'] = '';
+            }
+
+            if ($fields[0]['agent_min_value'] == 0) {
+                $table1->head['min_text'] = '';
+                $table1->data[0]['min_text'] = '';
+                $table1->head['min'] = '';
+                $table1->data[0]['min'] = '';
+            }
 
             if ($pdf === 0) {
                 $table->colspan[3][0] = 3;

From 49c913c6de9910a29db4ea743acb88fa8a4f7326 Mon Sep 17 00:00:00 2001
From: manuel <manuel.montes@artica.es>
Date: Thu, 4 Apr 2019 17:31:11 +0200
Subject: [PATCH 2/2] Added custom fields to report templates

Former-commit-id: 525d21d17835a3bb08f47df273128e658a35bcd9
---
 pandora_console/extras/mr/27.sql              |  13 +
 .../pandoradb_migrate_6.0_to_7.0.mysql.sql    |  12 +
 .../godmode/reporting/reporting_builder.php   | 251 ++++++++++++++----
 .../include/functions_reporting.php           | 116 +++++---
 .../include/functions_reporting_html.php      |  76 ++----
 5 files changed, 332 insertions(+), 136 deletions(-)

diff --git a/pandora_console/extras/mr/27.sql b/pandora_console/extras/mr/27.sql
index a98a94f6d1..e7d7a92f04 100644
--- a/pandora_console/extras/mr/27.sql
+++ b/pandora_console/extras/mr/27.sql
@@ -15,4 +15,17 @@ ALTER TABLE `treport_content` ADD COLUMN `unknown_checks` TINYINT(1) DEFAULT '1'
 ALTER TABLE `treport_content` ADD COLUMN `agent_max_value` TINYINT(1) DEFAULT '1';
 ALTER TABLE `treport_content` ADD COLUMN `agent_min_value` TINYINT(1) DEFAULT '1';
 
+ALTER TABLE `treport_content_template` ADD COLUMN `total_time` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `time_failed` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `time_in_ok_status` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `time_in_unknown_status` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `time_of_not_initialized_module` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `time_of_downtime` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `total_checks` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `checks_failed` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `checks_in_ok_status` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `unknown_checks` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `agent_max_value` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `agent_min_value` TINYINT(1) DEFAULT '1';
+
 COMMIT;
\ No newline at end of file
diff --git a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql
index 515302d463..62fb21f817 100644
--- a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql
+++ b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql
@@ -764,6 +764,18 @@ ALTER TABLE treport_content_template ADD COLUMN `lapse_calc` tinyint(1) default
 ALTER TABLE treport_content_template ADD COLUMN `lapse` int(11) default '300';
 ALTER TABLE treport_content_template ADD COLUMN `visual_format` tinyint(1) default '0';
 ALTER TABLE treport_content_template ADD COLUMN `hide_no_data` tinyint(1) default '0';
+ALTER TABLE `treport_content_template` ADD COLUMN `total_time` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `time_failed` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `time_in_ok_status` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `time_in_unknown_status` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `time_of_not_initialized_module` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `time_of_downtime` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `total_checks` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `checks_failed` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `checks_in_ok_status` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `unknown_checks` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `agent_max_value` TINYINT(1) DEFAULT '1';
+ALTER TABLE `treport_content_template` ADD COLUMN `agent_min_value` TINYINT(1) DEFAULT '1';
 
 -- -----------------------------------------------------
 -- Table `treport_content_sla_com_temp` (treport_content_sla_combined_template)
diff --git a/pandora_console/godmode/reporting/reporting_builder.php b/pandora_console/godmode/reporting/reporting_builder.php
index a8fe0f5f91..0d591f390f 100755
--- a/pandora_console/godmode/reporting/reporting_builder.php
+++ b/pandora_console/godmode/reporting/reporting_builder.php
@@ -70,7 +70,7 @@ global $config;
 // IMPORTANT NOTE: All reporting pages are used also for metaconsole reporting functionality
 // So, it's very important to specify full url and paths to resources because metaconsole has a different
 // entry point: enterprise/meta/index.php than normal console !!!
-// Login check
+// Login check.
 check_login();
 
 enterprise_hook('open_meta_frame');
@@ -89,7 +89,7 @@ if (!$report_r && !$report_w && !$report_m) {
 
 require_once $config['homedir'].'/include/functions_reports.php';
 
-// Load enterprise extensions
+// Load enterprise extensions.
 enterprise_include('operation/reporting/custom_reporting.php');
 enterprise_include_once('include/functions_metaconsole.php');
 
@@ -98,7 +98,7 @@ if (enterprise_include_once('include/functions_reporting.php') !== ENTERPRISE_NO
     $enterpriseEnable = true;
 }
 
-// Constant with fonts directory
+// Constant with fonts directory.
 define('_MPDF_TTFONTPATH', 'include/fonts/');
 
 $activeTab = get_parameter('tab', 'main');
@@ -162,6 +162,10 @@ if ($idReport != 0) {
                 $edit = true;
             }
         break;
+
+        default:
+            // Default.
+        break;
     }
 
     if (! $edit) {
@@ -227,6 +231,10 @@ switch ($action) {
 								ORDER BY "order"'
                             );
                         break;
+
+                        default:
+                            // Default.
+                        break;
                     }
 
                     if ($items === false) {
@@ -234,7 +242,7 @@ switch ($action) {
                     }
 
 
-                    // Clean the repeated order values
+                    // Clean the repeated order values.
                     $order_temp = 1;
                     foreach ($items as $item) {
                         switch ($config['dbtype']) {
@@ -254,6 +262,10 @@ switch ($action) {
                                     ['id_rc' => $item['id_rc']]
                                 );
                             break;
+
+                            default:
+                                // Default.
+                            break;
                         }
 
                         $order_temp++;
@@ -281,6 +293,10 @@ switch ($action) {
 								ORDER BY "order"'
                             );
                         break;
+
+                        default:
+                            // Default.
+                        break;
                     }
 
                     if ($items === false) {
@@ -344,6 +360,10 @@ switch ($action) {
                                     ['id_rc' => $id]
                                 );
                             break;
+
+                            default:
+                                // Default.
+                            break;
                         }
                     }
 
@@ -415,6 +435,10 @@ switch ($action) {
                         }
                     }
                 break;
+
+                default:
+                    // Default.
+                break;
             }
         }
     break;
@@ -444,9 +468,9 @@ switch ($action) {
             break;
         }
 
-        // Page header for metaconsole
-        if ($enterpriseEnable and defined('METACONSOLE')) {
-            // Bread crumbs
+        // Page header for metaconsole.
+        if ($enterpriseEnable && defined('METACONSOLE')) {
+            // Bread crumbs.
             ui_meta_add_breadcrumb(
                 [
                     'link' => 'index.php?sec=reporting&sec2=godmode/reporting/reporting_builder&pure='.$pure,
@@ -456,10 +480,11 @@ switch ($action) {
 
             ui_meta_print_page_header($nav_bar);
 
-            // Print header
+            // Print header.
             ui_meta_print_header(__('Reporting'), '', $buttons);
         }
-        // Page header for normal console
+
+        // Page header for normal console.
         else {
             ui_print_page_header(__('Custom reporting'), 'images/op_reporting.png', false, '', false, $buttons, false, '', 60);
         }
@@ -471,7 +496,7 @@ switch ($action) {
                 case 'group_view':
                     if ($config['id_user'] == $report['id_user'] || is_user_admin($config['id_user'])) {
                         $delete = true;
-                        // owner can delete
+                        // Owner can delete.
                     } else {
                         $delete = check_acl(
                             $config['id_user'],
@@ -484,7 +509,7 @@ switch ($action) {
                 case 'group_edit':
                     if ($config['id_user'] == $report['id_user'] || is_user_admin($config['id_user'])) {
                         $delete = true;
-                        // owner can delete
+                        // Owner can delete.
                     } else {
                         $delete = check_acl(
                             $config['id_user'],
@@ -501,6 +526,10 @@ switch ($action) {
                         $delete = true;
                     }
                 break;
+
+                default:
+                    // Default.
+                break;
             }
 
             if (! $delete) {
@@ -571,7 +600,7 @@ switch ($action) {
         ui_require_jquery_file('bgiframe');
         ui_require_jquery_file('autocomplete');
 
-        // Show only selected groups
+        // Show only selected groups.
         if ($id_group > 0) {
             $group = ["$id_group" => $id_group];
         } else {
@@ -595,7 +624,7 @@ switch ($action) {
         }
 
         // Fix : group filter was not working
-        // Show only selected groups
+        // Show only selected groups.
         if ($id_group > 0) {
             $group = ["$id_group" => $id_group];
             $filter['id_group'] = $id_group;
@@ -603,8 +632,8 @@ switch ($action) {
             $group = false;
         }
 
-        // Filter normal and metaconsole reports
-        if ($config['metaconsole'] == 1 and defined('METACONSOLE')) {
+        // Filter normal and metaconsole reports.
+        if ($config['metaconsole'] == 1 && defined('METACONSOLE')) {
             $filter['metaconsole'] = 1;
         } else {
             $filter['metaconsole'] = 0;
@@ -670,12 +699,12 @@ switch ($action) {
             $table->size['csv'] = '5%';
 
             $next = 4;
-            // Calculate dinamically the number of the column
+            // Calculate dinamically the number of the column.
             if (enterprise_hook('load_custom_reporting_1') !== ENTERPRISE_NOT_HOOK) {
                 $next = 7;
             }
 
-            // Admin options only for RM flag
+            // Admin options only for RM flag.
             if (check_acl($config['id_user'], 0, 'RM')) {
                 $table->head[$next] = __('Private');
                 $table->size[$next] = '2%';
@@ -743,7 +772,7 @@ switch ($action) {
 
                 $data[1] = ui_print_truncate_text($report['description'], 70);
 
-                // Remove html and xml button if items are larger than limit
+                // Remove html and xml button if items are larger than limit.
                 $item_count = db_get_num_rows('SELECT * FROM treport_content WHERE id_report='.$report['id_report']);
                 $report['overload'] = $item_count >= $config['report_limit'];
                 if ($report['overload']) {
@@ -752,7 +781,7 @@ switch ($action) {
                 } else if (!$report['non_interactive']) {
                     $data[2] = '<a href="'.$config['homeurl'].'index.php?sec=reporting&sec2=operation/reporting/reporting_viewer&id='.$report['id_report'].'&pure='.$pure.'">'.html_print_image('images/html.png', true, ['title' => __('HTML view')]).'</a>';
                     $data[3] = '<a href="'.ui_get_full_url(false, false, false, false).'ajax.php?page='.$config['homedir'].'/operation/reporting/reporting_xml&id='.$report['id_report'].'">'.html_print_image('images/xml.png', true, ['title' => __('Export to XML')]).'</a>';
-                    // I chose ajax.php because it's supposed to give XML anyway
+                    // I chose ajax.php because it's supposed to give XML anyway.
                 } else {
                     $data[2] = html_print_image(
                         'images/html_disabled.png',
@@ -765,14 +794,14 @@ switch ($action) {
                 }
 
 
-                // Calculate dinamically the number of the column
+                // Calculate dinamically the number of the column.
                 $next = 4;
                 if (enterprise_hook('load_custom_reporting_2') !== ENTERPRISE_NOT_HOOK) {
                     $next = 7;
                 }
 
 
-                // Admin options only for RM flag
+                // Admin options only for RM flag.
                 if (check_acl($config['id_user'], 0, 'RM')) {
                     if ($report['private'] == 1) {
                         $data[$next] = __('Yes');
@@ -820,6 +849,10 @@ switch ($action) {
                             $delete = true;
                         }
                     break;
+
+                    default:
+                        // Default.
+                    break;
                 }
 
                 if ($edit || $delete) {
@@ -915,7 +948,6 @@ switch ($action) {
         }
 
         enterprise_hook('close_meta_frame');
-
     return;
 
         break;
@@ -943,6 +975,10 @@ switch ($action) {
                 $idGroupReport = $report['id_group'];
                 $description = $report['description'];
             break;
+
+            default:
+                // Default.
+            break;
         }
     break;
 
@@ -976,6 +1012,10 @@ switch ($action) {
                         $id_group_edit = 0;
                         $private = 1;
                     break;
+
+                    default:
+                        // Default.
+                    break;
                 }
 
                 if ($action == 'update') {
@@ -1026,10 +1066,10 @@ switch ($action) {
                     $action = 'edit';
                 } else if ($action == 'save') {
                     if ($reportName != '' && $idGroupReport != '') {
-                        // This flag allow to differentiate between normal console and metaconsole reports
+                        // This flag allow to differentiate between normal console and metaconsole reports.
                         $metaconsole_report = (int) is_metaconsole();
 
-                        // Juanma (07/05/2014) New feature: Custom front page for reports
+                        // Juanma (07/05/2014) New feature: Custom front page for reports.
                         if ($config['custom_report_front']) {
                             $custom_font = $config['custom_report_front_font'];
                             $logo = $config['custom_report_front_logo'];
@@ -1098,7 +1138,6 @@ switch ($action) {
                 $good_format = false;
                 switch ($action) {
                     case 'update':
-
                         $values = [];
                         $values['id_report'] = $idReport;
                         // ---------------------------------------------------
@@ -1132,7 +1171,6 @@ switch ($action) {
                             break;
 
                             case 'event_report_log':
-
                                 $agents_to_report = get_parameter('id_agents3');
                                 $source = get_parameter('source', '');
                                 $search = get_parameter('search', '');
@@ -1154,7 +1192,7 @@ switch ($action) {
                                 $values['top_n_value'] = get_parameter('quantity');
                                 $interval_max = get_parameter('max_interval');
                                 $interval_min = get_parameter('min_interval');
-                                // Checks intervals fields
+                                // Checks intervals fields.
                                 if (preg_match('/^(\-)*[0-9]*\.?[0-9]+$/', $interval_max) and preg_match('/^(\-)*[0-9]*\.?[0-9]+$/', $interval_min)) {
                                     $good_format = true;
                                 }
@@ -1221,9 +1259,21 @@ switch ($action) {
 
                             case 'availability':
                                 // HACK it is saved in show_graph field.
-                                // Show interfaces instead the modules
+                                // Show interfaces instead the modules.
                                 $values['show_graph'] = get_parameter('checkbox_show_address_agent');
                                 $values['period'] = get_parameter('period');
+                                $values['total_time'] = get_parameter('total_time');
+                                $values['time_failed'] = get_parameter('time_failed');
+                                $values['time_in_ok_status'] = get_parameter('time_in_ok_status');
+                                $values['time_in_unknown_status'] = get_parameter('time_in_unknown_status');
+                                $values['time_of_not_initialized_module'] = get_parameter('time_of_not_initialized_module');
+                                $values['time_of_downtime'] = get_parameter('time_of_downtime');
+                                $values['total_checks'] = get_parameter('total_checks');
+                                $values['checks_failed'] = get_parameter('checks_failed');
+                                $values['checks_in_ok_status'] = get_parameter('checks_in_ok_status');
+                                $values['unknown_checks'] = get_parameter('unknown_checks');
+                                $values['agent_max_value'] = get_parameter('agent_max_value');
+                                $values['agent_min_value'] = get_parameter('agent_min_value');
                                 $good_format = true;
                             break;
 
@@ -1258,6 +1308,7 @@ switch ($action) {
                                 $values['text'] = get_parameter('text');
                                 $values['show_graph'] = get_parameter('combo_graph_options');
                                 $good_format = true;
+                            break;
                         }
 
                         $values['id_agent'] = get_parameter('id_agent');
@@ -1306,6 +1357,10 @@ switch ($action) {
                                 $values['time_from'] = '#to_date(\''.get_parameter('time_from').'\',\'hh24:mi:ss\')';
                                 $values['time_to'] = '#to_date(\''.get_parameter('time_to').'\', \'hh24:mi:ss\')';
                             break;
+
+                            default:
+                                // Default.
+                            break;
                         }
 
                         $values['group_by_agent'] = get_parameter('checkbox_row_group_by_agent');
@@ -1326,7 +1381,7 @@ switch ($action) {
                             $values['server_name'] = get_parameter('combo_server');
                         }
 
-                        if ((($values['type'] == 'custom_graph') or ($values['type'] == 'automatic_custom_graph')) && ($values['id_gs'] == 0 || $values['id_gs'] == '')) {
+                        if ((($values['type'] == 'custom_graph') || ($values['type'] == 'automatic_custom_graph')) && ($values['id_gs'] == 0 || $values['id_gs'] == '')) {
                             $resultOperationDB = false;
                             break;
                         }
@@ -1343,7 +1398,7 @@ switch ($action) {
 
                         $event_filter_search = get_parameter('filter_search', '');
 
-                        // If metaconsole is activated
+                        // If metaconsole is activated.
                         if ($config['metaconsole'] == 1 && defined('METACONSOLE')) {
                             if (($values['type'] == 'custom_graph') or ($values['type'] == 'automatic_custom_graph')) {
                                 $id_gs = substr($values['id_gs'], 0, strpos($values['id_gs'], '|'));
@@ -1354,7 +1409,7 @@ switch ($action) {
                                 }
                             }
 
-                            // Get agent and server name
+                            // Get agent and server name.
                             $agent_name_server = io_safe_output(get_parameter('agent'));
 
                             if (isset($agent_name_server)) {
@@ -1364,7 +1419,7 @@ switch ($action) {
                                     $server_name = substr($agent_name_server, $separator_pos);
                                     $server_name = str_replace('(', '', $server_name);
                                     $server_name = str_replace(')', '', $server_name);
-                                    // Will update server_name variable
+                                    // Will update server_name variable.
                                     $values['server_name'] = trim($server_name);
                                     $agent_name = substr($agent_name_server, 0, $separator_pos);
                                 }
@@ -1460,6 +1515,10 @@ switch ($action) {
                                     $style['label'] = '';
                                 }
                             break;
+
+                            default:
+                                // Default.
+                            break;
                         }
 
                         $values['style'] = io_safe_input(json_encode($style));
@@ -1474,6 +1533,10 @@ switch ($action) {
                                         unset($values['type']);
                                     }
                                 break;
+
+                                default:
+                                    // Default.
+                                break;
                             }
 
                             $resultOperationDB = db_process_sql_update(
@@ -1495,7 +1558,7 @@ switch ($action) {
                         $values['description'] = get_parameter('description');
                         $label = get_parameter('label', '');
 
-                        // Add macros name
+                        // Add macros name.
                         $items_label = [];
                         $items_label['type'] = get_parameter('type');
                         $items_label['id_agent'] = get_parameter('id_agent');
@@ -1505,7 +1568,7 @@ switch ($action) {
                         $values['name'] = reporting_label_macro($items_label, $name_it);
 
                         // Support for projection graph, prediction date and SLA reports
-                        // 'top_n_value', 'top_n' and 'text' fields will be reused for these types of report
+                        // 'top_n_value', 'top_n' and 'text' fields will be reused for these types of report.
                         switch ($values['type']) {
                             case 'projection_graph':
                                 $values['period'] = get_parameter('period1');
@@ -1607,7 +1670,7 @@ switch ($action) {
                             case 'availability':
                                 $values['period'] = get_parameter('period');
                                 // HACK it is saved in show_graph field.
-                                // Show interfaces instead the modules
+                                // Show interfaces instead the modules.
                                 $values['show_graph'] = get_parameter('checkbox_show_address_agent');
                                 $good_format = true;
                             break;
@@ -1682,6 +1745,10 @@ switch ($action) {
                                     $values['only_display_wrong'] = $only_display_wrong_tmp;
                                 }
                             break;
+
+                            default:
+                                // Default.
+                            break;
                         }
 
                         $values['monday'] = get_parameter('monday', 0);
@@ -1691,6 +1758,18 @@ switch ($action) {
                         $values['friday'] = get_parameter('friday', 0);
                         $values['saturday'] = get_parameter('saturday', 0);
                         $values['sunday'] = get_parameter('sunday', 0);
+                        $values['total_time'] = get_parameter('total_time', 0);
+                        $values['time_failed'] = get_parameter('time_failed', 0);
+                        $values['time_in_ok_status'] = get_parameter('time_in_ok_status', 0);
+                        $values['time_in_unknown_status'] = get_parameter('time_in_unknown_status', 0);
+                        $values['time_of_not_initialized_module'] = get_parameter('time_of_not_initialized_module', 0);
+                        $values['time_of_downtime'] = get_parameter('time_of_downtime', 0);
+                        $values['total_checks'] = get_parameter('total_checks', 0);
+                        $values['checks_failed'] = get_parameter('checks_failed', 0);
+                        $values['checks_in_ok_status'] = get_parameter('checks_in_ok_status', 0);
+                        $values['unknown_checks'] = get_parameter('unknown_checks', 0);
+                        $values['agent_max_value'] = get_parameter('agent_max_value', 0);
+                        $values['agent_min_value'] = get_parameter('agent_min_value', 0);
                         switch ($config['dbtype']) {
                             case 'mysql':
                             case 'postgresql':
@@ -1702,6 +1781,10 @@ switch ($action) {
                                 $values['time_from'] = '#to_date(\''.get_parameter('time_from').'\',\'hh24:mi:ss\')';
                                 $values['time_to'] = '#to_date(\''.get_parameter('time_to').'\', \'hh24:mi:ss\')';
                             break;
+
+                            default:
+                                // Default.
+                            break;
                         }
 
                         $values['group_by_agent'] = get_parameter('checkbox_row_group_by_agent', 0);
@@ -1729,8 +1812,8 @@ switch ($action) {
                             }
                         }
 
-                        if (($values['type'] == 'sql') or ($values['type'] == 'sql_graph_hbar')
-                            or ($values['type'] == 'sql_graph_vbar') or ($values['type'] == 'sql_graph_pie')
+                        if (($values['type'] == 'sql') || ($values['type'] == 'sql_graph_hbar')
+                            || ($values['type'] == 'sql_graph_vbar') || ($values['type'] == 'sql_graph_pie')
                         ) {
                             $values['treport_custom_sql_id'] = get_parameter('id_custom');
                             if ($values['treport_custom_sql_id'] == 0) {
@@ -1772,7 +1855,7 @@ switch ($action) {
 
                                 $event_filter_search = get_parameter('filter_search', '');
 
-                                // Added for events items
+                                // Added for events items.
                                 $style['show_summary_group']    = $show_summary_group;
                                 $style['filter_event_severity'] = json_encode($filter_event_severity);
                                 $style['filter_event_type']     = json_encode($filter_event_type);
@@ -1794,6 +1877,10 @@ switch ($action) {
                                             $style['label'] = '';
                                         }
                                     break;
+
+                                    default:
+                                        // Default.
+                                    break;
                                 }
                             break;
 
@@ -1838,6 +1925,10 @@ switch ($action) {
                                     $style['label'] = '';
                                 }
                             break;
+
+                            default:
+                                // Default.
+                            break;
                         }
 
                         $values['style'] = io_safe_input(json_encode($style));
@@ -1852,6 +1943,10 @@ switch ($action) {
                                         unset($values['type']);
                                     }
                                 break;
+
+                                default:
+                                    // Default.
+                                break;
                             }
 
                             $result = db_process_sql_insert(
@@ -1881,6 +1976,10 @@ switch ($action) {
 											WHERE id_report = '.$idReport
                                         );
                                     break;
+
+                                    default:
+                                        // Default.
+                                    break;
                                 }
 
                                 if ($max === false) {
@@ -1906,6 +2005,10 @@ switch ($action) {
                                             ['id_rc' => $idItem]
                                         );
                                     break;
+
+                                    default:
+                                        // Default.
+                                    break;
                                 }
 
                                 $resultOperationDB = true;
@@ -1913,7 +2016,8 @@ switch ($action) {
 
                             break;
                         }
-                        // If fields dont have good format
+
+                        // If fields dont have good format.
                         else {
                             $resultOperationDB = false;
                         }
@@ -1921,7 +2025,7 @@ switch ($action) {
             break;
 
             default:
-                if ($enterpriseEnable and $activeTab != 'advanced') {
+                if ($enterpriseEnable && $activeTab != 'advanced') {
                     $resultOperationDB = reporting_enterprise_update_action();
                 }
             break;
@@ -1979,7 +2083,7 @@ switch ($action) {
             case 'agent':
             case 'type':
 
-                // Sort functionality for normal console
+                // Sort functionality for normal console.
                 if (!defined('METACONSOLE')) {
                     switch ($field) {
                         case 'module':
@@ -2013,6 +2117,10 @@ switch ($action) {
                         case 'type':
                             $sql = 'SELECT id_rc FROM treport_content WHERE %s ORDER BY type %s';
                         break;
+
+                        default:
+                            // Default.
+                        break;
                     }
 
                     $sql = sprintf($sql, 'id_report = '.$idReport, '%s');
@@ -2024,11 +2132,15 @@ switch ($action) {
                         case 'down':
                             $sql = sprintf($sql, 'DESC');
                         break;
+
+                        default:
+                            // Default.
+                        break;
                     }
 
                     $ids = db_get_all_rows_sql($sql);
                 }
-                // Sort functionality for metaconsole
+                // Sort functionality for metaconsole.
                 else if ($config['metaconsole'] == 1) {
                     switch ($field) {
                         case 'agent':
@@ -2046,14 +2158,14 @@ switch ($action) {
                                 foreach ($report_items as $report_item) {
                                     $connection = metaconsole_get_connection($report_item['server_name']);
                                     if (metaconsole_load_external_db($connection) != NOERR) {
-                                        // ui_print_error_message ("Error connecting to ".$server_name);
+                                        // ui_print_error_message ("Error connecting to ".$server_name);.
                                     }
 
                                     switch ($field) {
                                         case 'agent':
                                             $agents_name = agents_get_agents(['id_agente' => $report_item['id_agent']], 'nombre');
 
-                                            // Item without agent
+                                            // Item without agent.
                                             if (!$agents_name) {
                                                 $element_name = '';
                                             } else {
@@ -2065,13 +2177,17 @@ switch ($action) {
                                         case 'module':
                                             $module_name = modules_get_agentmodule_name($report_item['id_agent_module']);
 
-                                            // Item without module
+                                            // Item without module.
                                             if (!$module_name) {
                                                 $element_name = '';
                                             } else {
                                                 $element_name = $module_name;
                                             }
                                         break;
+
+                                        default:
+                                            // Default.
+                                        break;
                                     }
 
                                     metaconsole_restore_db();
@@ -2079,7 +2195,7 @@ switch ($action) {
                                     $temp_sort[$report_item['id_rc']] = $element_name;
                                 }
 
-                                // Performes sorting
+                                // Performes sorting.
                                 switch ($dir) {
                                     case 'up':
                                         asort($temp_sort);
@@ -2088,6 +2204,10 @@ switch ($action) {
                                     case 'down':
                                         arsort($temp_sort);
                                     break;
+
+                                    default:
+                                        // Default.
+                                    break;
                                 }
 
                                 foreach ($temp_sort as $temp_element_key => $temp_element_val) {
@@ -2096,13 +2216,13 @@ switch ($action) {
                                     $i++;
                                 }
 
-                                // Free resources
+                                // Free resources.
                                 unset($temp_sort);
                                 unset($report_items);
                             }
                         break;
 
-                        // Type case only depends of local database
+                        // Type case only depends of local database.
                         case 'type':
                             $sql = 'SELECT id_rc
 								FROM treport_content
@@ -2121,10 +2241,13 @@ switch ($action) {
                                 case 'down':
                                     $sql = sprintf($sql, 'DESC');
                                 break;
+
+                                default:
+                                    // Default.
+                                break;
                             }
 
                             $ids = db_get_all_rows_sql($sql);
-
                         break;
                     }
                 }
@@ -2163,6 +2286,10 @@ switch ($action) {
 							WHERE id_rc = '.$idItem
                         );
                     break;
+
+                    default:
+                        // Default.
+                    break;
                 }
 
                 // db_get_value_filter('order', 'treport_content', array('id_rc' => $idItem));
@@ -2174,6 +2301,10 @@ switch ($action) {
                     case 'down':
                         $newOrder = ($oldOrder + 1);
                     break;
+
+                    default:
+                        // Default.
+                    break;
                 }
 
                 switch ($config['dbtype']) {
@@ -2211,6 +2342,10 @@ switch ($action) {
                             false
                         );
                     break;
+
+                    default:
+                        // Default.
+                    break;
                 }
 
                 if ($resultOperationDB !== false) {
@@ -2240,13 +2375,17 @@ switch ($action) {
                                 false
                             );
                         break;
+
+                        default:
+                            // Default.
+                        break;
                     }
                 }
             break;
         }
     break;
 
-    // Added for report templates
+    // Added for report templates.
     default:
         if ($enterpriseEnable) {
             $buttons = [
@@ -2270,7 +2409,7 @@ switch ($action) {
                 break;
             }
 
-            // Page header for metaconsole
+            // Page header for metaconsole.
             if ($enterpriseEnable and defined('METACONSOLE')) {
                 // Bread crumbs
                 ui_meta_add_breadcrumb(
@@ -2282,10 +2421,10 @@ switch ($action) {
 
                 ui_meta_print_page_header($nav_bar);
 
-                // Print header
+                // Print header.
                 ui_meta_print_header(__('Reporting'), '', $buttons);
             }
-            // Page header for normal console
+            // Page header for normal console.
             else {
                 ui_print_page_header($subsection, 'images/op_reporting.png', false, '', false, $buttons, false, '', 60);
             }
@@ -2348,9 +2487,9 @@ if ($idReport != 0) {
     $textReportName = __('Create Custom Report');
 }
 
-// Page header for metaconsole
+// Page header for metaconsole.
 if ($enterpriseEnable and defined('METACONSOLE')) {
-    // Bread crumbs
+    // Bread crumbs.
     ui_meta_add_breadcrumb(
         [
             'link' => 'index.php?sec=reporting&sec2=godmode/reporting/reporting_builder&pure='.$pure,
@@ -2360,7 +2499,7 @@ if ($enterpriseEnable and defined('METACONSOLE')) {
 
     ui_meta_print_page_header($nav_bar);
 
-    // Print header
+    // Print header.
     ui_meta_print_header(__('Reporting').$textReportName, '', $buttons);
 } else {
     ui_print_page_header(
diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php
index 1ff0529651..56f29b24a0 100755
--- a/pandora_console/include/functions_reporting.php
+++ b/pandora_console/include/functions_reporting.php
@@ -41,11 +41,11 @@ require_once $config['homedir'].'/include/functions_network.php';
 //
 // CONSTANTS DEFINITIONS                //
 //
-// Priority modes
+// Priority modes.
 define('REPORT_PRIORITY_MODE_OK', 1);
 define('REPORT_PRIORITY_MODE_UNKNOWN', 2);
 
-// Status
+// Status.
 define('REPORT_STATUS_ERR', 0);
 define('REPORT_STATUS_OK', 1);
 define('REPORT_STATUS_UNKNOWN', 2);
@@ -62,7 +62,7 @@ function reporting_user_can_see_report($id_report, $id_user=null)
         $id_user = $config['id_user'];
     }
 
-    // Get Report record (to get id_group)
+    // Get Report record (to get id_group).
     $report = db_get_row('treport', 'id_report', $id_report);
 
     // Check ACL on the report to see if user has access to the report.
@@ -108,6 +108,10 @@ function reporting_get_type($content)
         case REPORT_OLD_TYPE_SUMATORY:
             $content['type'] = 'sumatory';
         break;
+
+        default:
+            // Default.
+        break;
     }
 
     return $content['type'];
@@ -205,7 +209,7 @@ function reporting_make_reporting_data(
         $server_name = $content['server_name'];
 
         // General reports with 0 period means last value
-        // Avoid to overwrite it by template value
+        // Avoid to overwrite it by template value.
         if (!empty($period) && ($content['type'] !== 'general' && $content['period'] != 0)) {
             $content['period'] = $period;
         }
@@ -229,7 +233,7 @@ function reporting_make_reporting_data(
 
             if (in_array('label', $content['style'])) {
                 if ($content['id_agent'] == 0) {
-                    // Metaconsole connection
+                    // Metaconsole connection.
                     if ($metaconsole_on && $server_name != '') {
                         $connection = metaconsole_get_connection($server_name);
                         if (!metaconsole_load_external_db($connection)) {
@@ -240,7 +244,7 @@ function reporting_make_reporting_data(
 
                     array_push($agents_to_macro, modules_get_agentmodule_agent($graph_item['id_agent_module']));
                     if ($metaconsole_on) {
-                        // Restore db connection
+                        // Restore db connection.
                         metaconsole_restore_db();
                     }
                 }
@@ -291,7 +295,7 @@ function reporting_make_reporting_data(
             $content['name'] = reporting_label_macro($items_label, $content['style']['name_label']);
 
             if ($metaconsole_on) {
-                // Restore db connection
+                // Restore db connection.
                 metaconsole_restore_db();
             }
         }
@@ -781,6 +785,10 @@ function reporting_make_reporting_data(
                     $pdf
                 );
             break;
+
+            default:
+                // Default.
+            break;
         }
 
         $index_content++;
@@ -847,7 +855,7 @@ function reporting_SLA(
         include_once $config['homedir'].'/include/functions_planned_downtimes.php';
         $metaconsole_on = is_metaconsole();
 
-        // checking if needed to show graph or table
+        // checking if needed to show graph or table.
         if ($content['show_graph'] == 0 || $content['show_graph'] == 1) {
             $show_table = 1;
         } else {
@@ -1241,7 +1249,6 @@ function reporting_SLA(
                     }
 
                     $return['charts'] = $temp;
-
                 break;
 
                 case 2:
@@ -1278,7 +1285,10 @@ function reporting_SLA(
                     }
 
                     $return['charts'] = $temp;
+                break;
 
+                default:
+                    // Default.
                 break;
             }
         }
@@ -1317,7 +1327,7 @@ function reporting_event_top_n(
 
         case REPORT_TOP_N_AVG:
         default:
-            // If nothing is selected then it will be shown the average data
+            // If nothing is selected then it will be shown the average data.
             $type_top_n = __('Avg');
         break;
     }
@@ -1334,7 +1344,7 @@ function reporting_event_top_n(
     $return['top_n'] = $content['top_n_value'];
 
     if (empty($content['subitems'])) {
-        // Get all the related data
+        // Get all the related data.
         $sql = sprintf(
             'SELECT id_agent_module, server_name
 			FROM treport_content_item
@@ -1347,7 +1357,7 @@ function reporting_event_top_n(
         $tops = $content['subitems'];
     }
 
-    // Get chart
+    // Get chart.
     reporting_set_conf_charts(
         $width,
         $height,
@@ -1371,7 +1381,7 @@ function reporting_event_top_n(
         $data_top = [];
 
         foreach ($tops as $key => $row) {
-            // Metaconsole connection
+            // Metaconsole connection.
             $server_name = $row['server_name'];
             if (($config['metaconsole'] == 1) && $server_name != '' && defined('METACONSOLE')) {
                 $connection = metaconsole_get_connection($server_name);
@@ -1401,7 +1411,7 @@ function reporting_event_top_n(
 
                 case REPORT_TOP_N_AVG:
                 default:
-                    // If nothing is selected then it will be shown the average data
+                    // If nothing is selected then it will be shown the average data.
                     $value = reporting_get_agentmodule_data_average($row['id_agent_module'], $content['period']);
                 break;
             }
@@ -1415,7 +1425,7 @@ function reporting_event_top_n(
                 $units[$key] = $unit;
             }
 
-            // Restore dbconnection
+            // Restore dbconnection.
             if (($config['metaconsole'] == 1) && $server_name != '' && defined('METACONSOLE')) {
                 metaconsole_restore_db();
             }
@@ -1428,21 +1438,25 @@ function reporting_event_top_n(
 
             // Order to show.
             switch ($order_uptodown) {
-                // Descending
+                // Descending.
                 case 1:
                     array_multisort($data_top, SORT_DESC, $agent_name, SORT_ASC, $module_name, SORT_ASC, $id_agent_module, SORT_ASC, $units, SORT_ASC);
                 break;
 
-                // Ascending
+                // Ascending.
                 case 2:
                     array_multisort($data_top, SORT_ASC, $agent_name, SORT_ASC, $module_name, SORT_ASC, $id_agent_module, SORT_ASC, $units, SORT_ASC);
                 break;
 
-                // By agent name or without selection
+                // By agent name or without selection.
                 case 0:
                 case 3:
                     array_multisort($agent_name, SORT_ASC, $data_top, SORT_ASC, $module_name, SORT_ASC, $id_agent_module, SORT_ASC, $units, SORT_ASC);
                 break;
+
+                default:
+                    // Default.
+                break;
             }
 
             array_splice($data_top, $top_n_value);
@@ -1458,7 +1472,7 @@ function reporting_event_top_n(
             $data_top_values['id_agent_module'] = $id_agent_module;
             $data_top_values['units'] = $units;
 
-            // Define truncate size depends the graph width
+            // Define truncate size depends the graph width.
             $truncate_size = ($width / (4 * ($config['font_size'])) - 1);
 
             if ($order_uptodown == 1 || $order_uptodown == 2) {
@@ -1584,7 +1598,7 @@ function reporting_event_top_n(
                     $ttl
                 );
 
-                // Display bars graph
+                // Display bars graph.
                 $return['charts']['bars'] = hbar_graph(
                     $data_hbar,
                     $width,
@@ -1609,7 +1623,7 @@ function reporting_event_top_n(
             $return['resume'] = null;
 
             if ($content['show_resume'] && count($data_top_values) > 0) {
-                // Get the very first not null value
+                // Get the very first not null value.
                 $i = 0;
                 do {
                     $min = $data_top_values['data_top'][$i];
@@ -1689,14 +1703,14 @@ function reporting_event_report_group(
 
     $event_filter = $content['style'];
     $return['show_summary_group'] = $event_filter['show_summary_group'];
-    // filter
+    // Filter.
     $show_summary_group         = $event_filter['show_summary_group'];
     $filter_event_severity      = json_decode($event_filter['filter_event_severity'], true);
     $filter_event_type          = json_decode($event_filter['filter_event_type'], true);
     $filter_event_status        = json_decode($event_filter['filter_event_status'], true);
     $filter_event_filter_search = $event_filter['event_filter_search'];
 
-    // graphs
+    // Graphs.
     $event_graph_by_agent                 = $event_filter['event_graph_by_agent'];
     $event_graph_by_user_validator        = $event_filter['event_graph_by_user_validator'];
     $event_graph_by_criticity             = $event_filter['event_graph_by_criticity'];
@@ -1858,7 +1872,7 @@ function reporting_event_report_group(
         metaconsole_restore_db();
     }
 
-    // total_events
+    // total_events.
     if ($return['data'] != '') {
         $return['total_events'] = count($return['data']);
     } else {
@@ -1911,14 +1925,14 @@ function reporting_event_report_module(
 
     $event_filter = $content['style'];
     $return['show_summary_group'] = $event_filter['show_summary_group'];
-    // filter
+    // Filter.
     $show_summary_group         = $event_filter['show_summary_group'];
     $filter_event_severity      = json_decode($event_filter['filter_event_severity'], true);
     $filter_event_type          = json_decode($event_filter['filter_event_type'], true);
     $filter_event_status        = json_decode($event_filter['filter_event_status'], true);
     $filter_event_filter_search = $event_filter['event_filter_search'];
 
-    // graphs
+    // Graphs.
     $event_graph_by_user_validator        = $event_filter['event_graph_by_user_validator'];
     $event_graph_by_criticity             = $event_filter['event_graph_by_criticity'];
     $event_graph_validated_vs_unvalidated = $event_filter['event_graph_validated_vs_unvalidated'];
@@ -1930,7 +1944,7 @@ function reporting_event_report_module(
         $metaconsole_dbtable = false;
     }
 
-    // data events
+    // Data events.
     $data = reporting_get_module_detailed_event(
         $content['id_agent_module'],
         $content['period'],
@@ -1959,7 +1973,7 @@ function reporting_event_report_module(
         metaconsole_restore_db();
     }
 
-    // total_events
+    // Total_events.
     if ($return['data'][0]['data'] != '') {
         $return['total_events'] = count($return['data'][0]['data']);
     } else {
@@ -2276,6 +2290,10 @@ function reporting_exception(
             $return['subtitle'] = __('Exception - Modules at critical or warning status');
             $return['subtype'] = __('Modules at critical or warning status');
         break;
+
+        default:
+            // Default.
+        break;
     }
 
     $return['description'] = $content['description'];
@@ -2286,7 +2304,7 @@ function reporting_exception(
     $return['resume'] = [];
 
     if (empty($content['subitems'])) {
-        // Get all the related data
+        // Get all the related data.
         $sql = sprintf(
             '
 			SELECT id_agent_module, server_name, operation
@@ -2303,10 +2321,10 @@ function reporting_exception(
     if ($exceptions === false) {
         $return['failed'] = __('There are no Agent/Modules defined');
     } else {
-        // Get the very first not null value
+        // Get the very first not null value.
         $i = 0;
         do {
-            // Metaconsole connection
+            // Metaconsole connection.
             $server_name = $exceptions[$i]['server_name'];
             if (($config['metaconsole'] == 1) && $server_name != '' && defined('METACONSOLE')) {
                 $connection = metaconsole_get_connection($server_name);
@@ -2340,12 +2358,16 @@ function reporting_exception(
                             $content['period']
                         );
                     break;
+
+                    default:
+                        // Default.
+                    break;
                 }
             }
 
             $i++;
 
-            // Restore dbconnection
+            // Restore dbconnection.
             if (($config['metaconsole'] == 1) && $server_name != '' && defined('METACONSOLE')) {
                 metaconsole_restore_db();
             }
@@ -2357,7 +2379,7 @@ function reporting_exception(
 
         $i = 0;
         foreach ($exceptions as $exc) {
-            // Metaconsole connection
+            // Metaconsole connection.
             $server_name = $exc['server_name'];
             if (($config['metaconsole'] == 1) && $server_name != '' && defined('METACONSOLE')) {
                 $connection = metaconsole_get_connection($server_name);
@@ -6270,6 +6292,19 @@ function reporting_availability($report, $content, $date=false, $time=false)
     $return['resume']['avg'] = $avg;
     $return['resume']['max_text'] = $max_text;
     $return['resume']['max'] = $max;
+    $return['fields'] = [];
+    $return['fields']['total_time'] = $content['total_time'];
+    $return['fields']['time_failed'] = $content['time_failed'];
+    $return['fields']['time_in_ok_status'] = $content['time_in_ok_status'];
+    $return['fields']['time_in_unknown_status'] = $content['time_in_unknown_status'];
+    $return['fields']['time_of_not_initialized_module'] = $content['time_of_not_initialized_module'];
+    $return['fields']['time_of_downtime'] = $content['time_of_downtime'];
+    $return['fields']['total_checks'] = $content['total_checks'];
+    $return['fields']['checks_failed'] = $content['checks_failed'];
+    $return['fields']['checks_in_ok_status'] = $content['checks_in_ok_status'];
+    $return['fields']['unknown_checks'] = $content['unknown_checks'];
+    $return['fields']['agent_max_value'] = $content['agent_max_value'];
+    $return['fields']['agent_min_value'] = $content['agent_min_value'];
 
     return reporting_check_structure_content($return);
 }
@@ -7443,6 +7478,21 @@ function reporting_check_structure_content($report)
         $report['date']['to'] = '';
     }
 
+    if (!isset($report['fields'])) {
+        $return['fields']['total_time'] = '';
+        $return['fields']['time_failed'] = '';
+        $return['fields']['time_in_ok_status'] = '';
+        $return['fields']['time_in_unknown_status'] = '';
+        $return['fields']['time_of_not_initialized_module'] = '';
+        $return['fields']['time_of_downtime'] = '';
+        $return['fields']['total_checks'] = '';
+        $return['fields']['checks_failed'] = '';
+        $return['fields']['checks_in_ok_status'] = '';
+        $return['fields']['unknown_checks'] = '';
+        $return['fields']['agent_max_value'] = '';
+        $return['fields']['agent_min_value'] = '';
+    }
+
     return $report;
 }
 
diff --git a/pandora_console/include/functions_reporting_html.php b/pandora_console/include/functions_reporting_html.php
index 4d9b21e2bb..dc6010e34a 100644
--- a/pandora_console/include/functions_reporting_html.php
+++ b/pandora_console/include/functions_reporting_html.php
@@ -2907,24 +2907,6 @@ function reporting_html_availability($table, $item, $pdf=0)
         io_safe_output($style),
         true
     );
-    $sql = 'SELECT 
-     total_time,
-     time_failed,
-     time_in_ok_status,
-     time_in_unknown_status,
-     time_of_not_initialized_module,
-     time_of_downtime, 
-     total_checks,
-     checks_failed,
-     checks_in_ok_status,
-     unknown_checks,
-     agent_max_value,
-     agent_min_value
-     FROM treport_content 
-     WHERE id_rc ='.$item['id_rc'];
-    $fields = db_get_all_rows_sql(
-        $sql
-    );
     $same_agent_in_resume = '';
 
     global $config;
@@ -2944,37 +2926,37 @@ function reporting_html_availability($table, $item, $pdf=0)
             $table1->head[1] = __('Module');
         }
 
-        if ($fields[0]['total_time']) {
+        if ($item['fields']['total_time']) {
             $table1->head[2] = __('Total time');
         } else {
             $table1->head[2] = __('');
         }
 
-        if ($fields[0]['time_failed']) {
+        if ($item['fields']['time_failed']) {
             $table1->head[3] = __('Time failed');
         } else {
             $table1->head[3] = __('');
         }
 
-        if ($fields[0]['time_in_ok_status']) {
+        if ($item['fields']['time_in_ok_status']) {
             $table1->head[4] = __('Time OK');
         } else {
             $table1->head[4] = __('');
         }
 
-        if ($fields[0]['time_in_unknown_status']) {
+        if ($item['fields']['time_in_unknown_status']) {
             $table1->head[5] = __('Time Unknown');
         } else {
             $table1->head[5] = __('');
         }
 
-        if ($fields[0]['time_of_not_initialized_module']) {
+        if ($item['fields']['time_of_not_initialized_module']) {
             $table1->head[6] = __('Time Not Init Module');
         } else {
             $table1->head[6] = __('');
         }
 
-        if ($fields[0]['time_of_downtime']) {
+        if ($item['fields']['time_of_downtime']) {
             $table1->head[7] = __('Time Downtime');
         } else {
             $table1->head[7] = __('');
@@ -3017,25 +2999,25 @@ function reporting_html_availability($table, $item, $pdf=0)
             $table2->head[1] = __('Module');
         }
 
-        if ($fields[0]['total_checks']) {
+        if ($item['fields']['total_checks']) {
             $table2->head[2] = __('Total checks');
         } else {
             $table2->head[2] = __('');
         }
 
-        if ($fields[0]['checks_failed']) {
+        if ($item['fields']['checks_failed']) {
             $table2->head[3] = __('Checks failed');
         } else {
             $table2->head[3] = __('');
         }
 
-        if ($fields[0]['checks_in_ok_status']) {
+        if ($item['fields']['checks_in_ok_status']) {
             $table2->head[4] = __('Checks OK');
         } else {
             $table2->head[4] = __('');
         }
 
-        if ($fields[0]['unknown_checks']) {
+        if ($item['fields']['unknown_checks']) {
             $table2->head[5] = __('Checks Uknown');
         } else {
             $table2->head[5] = __('');
@@ -3061,67 +3043,67 @@ function reporting_html_availability($table, $item, $pdf=0)
             $table_row[] = $row['agent'];
             $table_row[] = $row['availability_item'];
 
-            if ($row['time_total'] != 0 && $fields[0]['total_time']) {
+            if ($row['time_total'] != 0 && $item['fields']['total_time']) {
                 $table_row[] = human_time_description_raw(
                     $row['time_total'],
                     true
                 );
-            } else if ($row['time_total'] == 0 && $fields[0]['total_time']) {
+            } else if ($row['time_total'] == 0 && $item['fields']['total_time']) {
                 $table_row[] = '--';
             } else {
                 $table_row[] = '';
             };
 
-            if ($row['time_error'] != 0 && $fields[0]['time_failed']) {
+            if ($row['time_error'] != 0 && $item['fields']['time_failed']) {
                 $table_row[] = human_time_description_raw(
                     $row['time_error'],
                     true
                 );
-            } else if ($row['time_error'] == 0 && $fields[0]['time_failed']) {
+            } else if ($row['time_error'] == 0 && $item['fields']['time_failed']) {
                 $table_row[] = '--';
             } else {
                 $table_row[] = '';
             };
 
-            if ($row['time_ok'] != 0 && $fields[0]['time_in_ok_status']) {
+            if ($row['time_ok'] != 0 && $item['fields']['time_in_ok_status']) {
                 $table_row[] = human_time_description_raw(
                     $row['time_ok'],
                     true
                 );
-            } else if ($row['time_ok'] == 0 && $fields[0]['time_in_ok_status']) {
+            } else if ($row['time_ok'] == 0 && $item['fields']['time_in_ok_status']) {
                 $table_row[] = '--';
             } else {
                 $table_row[] = '';
             };
 
-            if ($row['time_unknown'] != 0 && $fields[0]['time_in_unknown_status']) {
+            if ($row['time_unknown'] != 0 && $item['fields']['time_in_unknown_status']) {
                 $table_row[] = human_time_description_raw(
                     $row['time_unknown'],
                     true
                 );
-            } else if ($row['time_unknown'] == 0 && $fields[0]['time_in_unknown_status']) {
+            } else if ($row['time_unknown'] == 0 && $item['fields']['time_in_unknown_status']) {
                 $table_row[] = '--';
             } else {
                 $table_row[] = '';
             };
 
-            if ($row['time_not_init'] != 0 && $fields[0]['time_of_not_initialized_module']) {
+            if ($row['time_not_init'] != 0 && $item['fields']['time_of_not_initialized_module']) {
                 $table_row[] = human_time_description_raw(
                     $row['time_not_init'],
                     true
                 );
-            } else if ($row['time_not_init'] == 0 && $fields[0]['time_of_not_initialized_module']) {
+            } else if ($row['time_not_init'] == 0 && $item['fields']['time_of_not_initialized_module']) {
                 $table_row[] = '--';
             } else {
                 $table_row[] = '';
             };
 
-            if ($row['time_downtime'] != 0 && $fields[0]['time_of_downtime']) {
+            if ($row['time_downtime'] != 0 && $item['fields']['time_of_downtime']) {
                 $table_row[] = human_time_description_raw(
                     $row['time_downtime'],
                     true
                 );
-            } else if ($row['time_downtime'] == 0 && $fields[0]['time_of_downtime']) {
+            } else if ($row['time_downtime'] == 0 && $item['fields']['time_of_downtime']) {
                 $table_row[] = '--';
             } else {
                 $table_row[] = '';
@@ -3132,22 +3114,22 @@ function reporting_html_availability($table, $item, $pdf=0)
             $table_row2 = [];
             $table_row2[] = $row['agent'];
             $table_row2[] = $row['availability_item'];
-            if ($fields[0]['total_checks']) {
+            if ($item['fields']['total_checks']) {
                 $table_row2[] = $row['checks_total'];
             } else {
                 $table_row2[] = '';
             };
-            if ($fields[0]['checks_failed']) {
+            if ($item['fields']['checks_failed']) {
                 $table_row2[] = $row['checks_error'];
             } else {
                 $table_row2[] = '';
             };
-            if ($fields[0]['checks_in_ok_status']) {
+            if ($item['fields']['checks_in_ok_status']) {
                 $table_row2[] = $row['checks_ok'];
             } else {
                 $table_row2[] = '';
             };
-            if ($fields[0]['unknown_checks']) {
+            if ($item['fields']['unknown_checks']) {
                 $table_row2[] = $row['checks_unknown'];
             } else {
                 $table_row2[] = '';
@@ -3190,7 +3172,7 @@ function reporting_html_availability($table, $item, $pdf=0)
     if ($item['resume']['resume'] && !empty($item['data'])) {
         $table1->width = '99%';
         $table1->data = [];
-        if ((strpos($item['resume']['min_text'], $same_agent_in_resume) === false)) {
+        if (empty($same_agent_in_resume) || (strpos($item['resume']['min_text'], $same_agent_in_resume) === false)) {
             $table1->head = [];
             $table1->head['max_text'] = __('Agent max value');
             $table1->head['max']      = __('Max Value');
@@ -3225,14 +3207,14 @@ function reporting_html_availability($table, $item, $pdf=0)
                 ).'%',
                 'avg'      => '<span style="font-size: 1.2em; font-weight:bold;">'.sla_truncate($item['resume']['avg'], $config['graph_precision']).'%</span>',
             ];
-            if ($fields[0]['agent_max_value'] == 0) {
+            if ($item['fields']['agent_max_value'] == false) {
                 $table1->head['max_text'] = '';
                 $table1->data[0]['max_text'] = '';
                 $table1->head['max'] = '';
                 $table1->data[0]['max'] = '';
             }
 
-            if ($fields[0]['agent_min_value'] == 0) {
+            if ($item['fields']['agent_min_value'] == false) {
                 $table1->head['min_text'] = '';
                 $table1->data[0]['min_text'] = '';
                 $table1->head['min'] = '';