From 1465400fb649eab00924bfaf7206f1d0a0989fa5 Mon Sep 17 00:00:00 2001
From: Daniel Cebrian <daniel.cebrian@pandorafms.com>
Date: Tue, 19 Sep 2023 16:22:58 +0200
Subject: [PATCH 1/3] #12001 fixed filter in inventary

---
 .../operation/inventory/inventory.php         | 72 ++++++++++---------
 1 file changed, 40 insertions(+), 32 deletions(-)

diff --git a/pandora_console/operation/inventory/inventory.php b/pandora_console/operation/inventory/inventory.php
index c753bd599b..2648649d80 100755
--- a/pandora_console/operation/inventory/inventory.php
+++ b/pandora_console/operation/inventory/inventory.php
@@ -32,55 +32,65 @@ use PandoraFMS\Enterprise\Metaconsole\Node;
 require_once $config['homedir'].'/include/functions_users.php';
 require_once $config['homedir'].'/include/functions_inventory.php';
 
-// Get different date to search the report.
-$utimestamp = (int) get_parameter('date_end', 0);
-$datetime_end = strtotime($utimestamp.' '.$time_end);
 
 // Calculate new inteval for all reports.
+$date_end = get_parameter('date_end', 0);
+$time_end = get_parameter('time_end');
+$datetime_end = strtotime($date_end.' '.$time_end);
+
 $custom_date = get_parameter('custom_date', 0);
-$date = get_parameter('date', SECONDS_1DAY);
-$date_text = get_parameter('date_text', SECONDS_1DAY);
-$date_init_less = (strtotime(date('Y-m-j')) - SECONDS_1DAY);
+$range = get_parameter('utimestamp', SECONDS_1DAY);
+$date_text = get_parameter('utimestamp_text', SECONDS_1DAY);
+$date_init_less = (strtotime(date('Y/m/d')) - SECONDS_1DAY);
 $date_init = get_parameter('date_init', date(DATE_FORMAT, $date_init_less));
 $time_init = get_parameter('time_init', date(TIME_FORMAT, $date_init_less));
+$datetime_init = strtotime($date_init.' '.$time_init);
 if ($custom_date === '1') {
     if ($datetime_init >= $datetime_end) {
         $datetime_init = $date_init_less;
     }
 
+    $date_init = date('Y/m/d H:i:s', $datetime_init);
+    $date_end = date('Y/m/d H:i:s', $datetime_end);
     $period = ($datetime_end - $datetime_init);
 } else if ($custom_date === '2') {
-    $date_units = get_parameter('date_units');
-    $utimestamp = date('Y/m/d H:i:s');
-    $date_start = date('Y/m/d H:i:s', (strtotime($utimestamp) - ($date_text * $date_units)));
-    $period = (strtotime($utimestamp) - strtotime($date_start));
-} else if (in_array($date, ['this_week', 'this_month', 'past_week', 'past_month'])) {
-    if ($date === 'this_week') {
+    $date_units = get_parameter('utimestamp_units');
+    $date_end = date('Y/m/d H:i:s');
+    $date_init = date('Y/m/d H:i:s', (strtotime($date_end) - ((int) $date_text * (int) $date_units)));
+    $period = (strtotime($date_end) - strtotime($date_init));
+} else if (in_array($range, ['this_week', 'this_month', 'past_week', 'past_month'])) {
+    if ($range === 'this_week') {
         $monday = date('Y/m/d', strtotime('last monday'));
 
         $sunday = date('Y/m/d', strtotime($monday.' +6 days'));
         $period = (strtotime($sunday) - strtotime($monday));
         $date_init = $monday;
-        $utimestamp = $sunday;
-    } else if ($date === 'this_month') {
-        $utimestamp = date('Y/m/d', strtotime('last day of this month'));
+        $date_end = $sunday;
+    } else if ($range === 'this_month') {
+        $date_end = date('Y/m/d', strtotime('last day of this month'));
         $first_of_month = date('Y/m/d', strtotime('first day of this month'));
-        $period = (strtotime($utimestamp) - strtotime($first_of_month));
-    } else if ($date === 'past_month') {
-        $utimestamp = date('Y/m/d', strtotime('last day of previous month'));
+        $date_init = $first_of_month;
+        $period = (strtotime($date_end) - strtotime($first_of_month));
+    } else if ($range === 'past_month') {
+        $date_end = date('Y/m/d', strtotime('last day of previous month'));
         $first_of_month = date('Y/m/d', strtotime('first day of previous month'));
-        $period = (strtotime($utimestamp) - strtotime($first_of_month));
-    } else if ($date === 'past_week') {
-        $utimestamp = date('Y-m-d', strtotime('sunday', strtotime('last week')));
-        $first_of_week = date('Y-m-d', strtotime('monday', strtotime('last week')));
-        $period = (strtotime($utimestamp) - strtotime($first_of_week));
+        $date_init = $first_of_month;
+        $period = (strtotime($date_end) - strtotime($first_of_month));
+    } else if ($range === 'past_week') {
+        $date_end = date('Y/m/d', strtotime('sunday', strtotime('last week')));
+        $first_of_week = date('Y/m/d', strtotime('monday', strtotime('last week')));
+        $date_init = $first_of_week;
+        $period = (strtotime($date_end) - strtotime($first_of_week));
     }
 } else {
-    $utimestamp = date('Y/m/d H:i:s');
-    $date_start = date('Y/m/d H:i:s', (strtotime($utimestamp) - $date));
-    $period = (strtotime($utimestamp) - strtotime($date_start));
+    $date_end = date('Y/m/d H:i:s');
+    $date_init = date('Y/m/d H:i:s', (strtotime($date_end) - $range));
+    $period = (strtotime($date_end) - strtotime($date_init));
 }
 
+$date_init = strtotime($date_init);
+$utimestamp = strtotime($date_end);
+
 if (is_ajax() === true) {
     $get_csv_url = (bool) get_parameter('get_csv_url');
     $get_data_basic_info = (bool) get_parameter('get_data_basic_info');
@@ -383,7 +393,6 @@ $inventory_id_group = (int) get_parameter('id_group');
 $inventory_search_string = (string) get_parameter('search_string');
 $order_by_agent = (bool) get_parameter('order_by_agent');
 $export = (string) get_parameter('export');
-$utimestamp = (int) get_parameter('utimestamp');
 $submit_filter = (bool) get_parameter('srcbutton');
 
 $pagination_url_parameters = [
@@ -667,7 +676,6 @@ $params['input_name'] = 'agent';
 $params['value'] = $inventory_agent;
 $params['selectbox_id'] = 'module_inventory_general_view';
 $params['javascript_is_function_select'] = true;
-$params['javascript_function_action_after_select'] = 'this.form.submit';
 $params['use_hidden_input_idagent'] = true;
 $params['print_hidden_input_idagent'] = true;
 $params['hidden_input_idagent_id'] = 'hidden-autocomplete_id_agent';
@@ -734,10 +742,10 @@ if (is_metaconsole() === false) {
             'utimestamp',
             true,
             get_parameter('utimestamp', SECONDS_1DAY),
-            $date_init,
-            $time_init,
-            date('Y/m/d'),
-            date('H:i:s'),
+            date('Y/m/d', $date_init),
+            date('H:i:s', $date_init),
+            date('Y/m/d', $utimestamp),
+            date('H:i:s', $utimestamp),
             $date_text
         )
     );

From 32b7a103ebbfc394a132b815b7e88146f9b2bb75 Mon Sep 17 00:00:00 2001
From: Daniel Cebrian <daniel.cebrian@pandorafms.com>
Date: Tue, 19 Sep 2023 17:26:36 +0200
Subject: [PATCH 2/3] #12001 fixed list modules when select agent

---
 pandora_console/operation/inventory/inventory.php | 1 -
 1 file changed, 1 deletion(-)

diff --git a/pandora_console/operation/inventory/inventory.php b/pandora_console/operation/inventory/inventory.php
index 2648649d80..bdfaa529fc 100755
--- a/pandora_console/operation/inventory/inventory.php
+++ b/pandora_console/operation/inventory/inventory.php
@@ -674,7 +674,6 @@ $params['return'] = true;
 $params['show_helptip'] = true;
 $params['input_name'] = 'agent';
 $params['value'] = $inventory_agent;
-$params['selectbox_id'] = 'module_inventory_general_view';
 $params['javascript_is_function_select'] = true;
 $params['use_hidden_input_idagent'] = true;
 $params['print_hidden_input_idagent'] = true;

From 73d179e52fcbaf5ad2d51cc97990bdbf912e6704 Mon Sep 17 00:00:00 2001
From: Daniel Cebrian <daniel.cebrian@pandorafms.com>
Date: Wed, 20 Sep 2023 12:14:55 +0200
Subject: [PATCH 3/3] #12001 added autoselect module in inventory

---
 .../include/ajax/inventory.ajax.php           | 45 +++++++++++++++++++
 .../operation/inventory/inventory.php         | 36 ++++++++++++++-
 2 files changed, 80 insertions(+), 1 deletion(-)
 create mode 100644 pandora_console/include/ajax/inventory.ajax.php

diff --git a/pandora_console/include/ajax/inventory.ajax.php b/pandora_console/include/ajax/inventory.ajax.php
new file mode 100644
index 0000000000..71fac8d0ec
--- /dev/null
+++ b/pandora_console/include/ajax/inventory.ajax.php
@@ -0,0 +1,45 @@
+<?php
+/**
+ * Ajax script for Inventory
+ *
+ * @category   Inventory
+ * @package    Pandora FMS
+ * @subpackage Enterprises
+ * @version    1.0.0
+ * @license    See below
+ *
+ *    ______                 ___                    _______ _______ ________
+ * |   __ \.-----.--.--.--|  |.-----.----.-----. |    ___|   |   |     __|
+ * |    __/|  _  |     |  _  ||  _  |   _|  _  | |    ___|       |__     |
+ * |___|   |___._|__|__|_____||_____|__| |___._| |___|   |__|_|__|_______|
+ *
+ * ============================================================================
+ * Copyright (c) 2005-2023 Pandora FMS
+ * Please see https://pandorafms.com/community/ for full contribution list
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation for version 2.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * ============================================================================
+ */
+
+ check_login();
+
+if (is_ajax() === true) {
+    $id_agent = get_parameter('id_agent', '0');
+    $id_server = get_parameter('id_server', '0');
+    if (is_metaconsole() === true) {
+        $agent_modules = [];
+        $server_name = metaconsole_get_names(['id' => $id_server]);
+        if (is_array($server_name) === true && count($server_name) > 0) {
+            $agent_modules = inventory_get_agent_modules($id_agent, 'all', $id_server, reset($server_name));
+        }
+    } else {
+        $agent_modules = inventory_get_agent_modules($id_agent);
+    }
+
+    echo json_encode($agent_modules);
+}
diff --git a/pandora_console/operation/inventory/inventory.php b/pandora_console/operation/inventory/inventory.php
index bdfaa529fc..432ee3e8d8 100755
--- a/pandora_console/operation/inventory/inventory.php
+++ b/pandora_console/operation/inventory/inventory.php
@@ -680,6 +680,7 @@ $params['print_hidden_input_idagent'] = true;
 $params['hidden_input_idagent_id'] = 'hidden-autocomplete_id_agent';
 $params['hidden_input_idagent_name'] = 'agent_id';
 $params['hidden_input_idagent_value'] = $inventory_id_agent;
+$params['javascript_function_action_after_select'] = 'loadModulesFromAgent';
 if ($is_metaconsole === true) {
     $params['print_input_id_server'] = true;
     $params['input_id_server_id'] = 'hidden-autocomplete_id_server';
@@ -1361,7 +1362,7 @@ ui_require_jquery_file('ui.datepicker-'.get_user_language(), 'include/javascript
 /* <![CDATA[ */
     $(document).ready (function () {
         <?php if (is_metaconsole() === true) : ?>
-        active_inventory_submit();
+        //active_inventory_submit();
         <?php endif; ?>
         $("#id_group").click (
             function () {
@@ -1439,5 +1440,38 @@ ui_require_jquery_file('ui.datepicker-'.get_user_language(), 'include/javascript
             closeText: '<?php echo __('Close'); ?>'
         });*/
     });
+
+    function loadModulesFromAgent(e){
+        const id_agent = $('#hidden-autocomplete_id_agent').val();
+        const text_agent = $('#text-agent').val();
+        let server = 0;
+        if($('#hidden-autocomplete_id_server').length > 0) {
+            server = $('#hidden-autocomplete_id_server').val();
+        }
+
+        if(text_agent === 'All') return;
+        jQuery.ajax ({
+            data: {
+                id_agent,
+                page: 'include/ajax/inventory.ajax',
+                id_server: server
+            },
+            type: "POST",
+            url: action="<?php echo ui_get_full_url('ajax.php', false, false, false); ?>",
+            dataType: "json",
+            success: function (data) {
+                if (data) {
+                    console.log(data);
+                    $("#module_inventory_general_view").empty();
+                    $("#module_inventory_general_view").append ($("<option value=basic>Basic info</option>"));
+                    $("#module_inventory_general_view").append ($("<option value=0>All</option>"));
+                    jQuery.each (data, function (id, value) {
+                        $("#module_inventory_general_view").append ($("<option value=" + id + ">" + value + "</option>"));
+                    });
+                }
+            }
+        });
+    }
+
 /* ]]> */
 </script>